0023024: Update headers of OCCT files
[occt.git] / src / Approx / Approx_CurveOnSurface.cxx
1 // Created on: 1997-10-06
2 // Created by: Roman BORISOV
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22 #include <Precision.hxx>
23 #include <Approx_CurveOnSurface.ixx>
24 #include <gp_Pnt.hxx>
25 #include <gp_Vec.hxx>
26 #include <GeomAdaptor_HSurface.hxx>
27 #include <Adaptor3d_CurveOnSurface.hxx>
28 #include <TColStd_HArray1OfReal.hxx>
29 #include <AdvApprox_ApproxAFunction.hxx>
30 #include <Adaptor3d_HCurve.hxx>
31 #include <TColgp_Array1OfPnt.hxx>
32 #include <GeomAdaptor_HCurve.hxx>
33 #include <Geom2dAdaptor_HCurve.hxx>
34 #include <Adaptor3d_HCurveOnSurface.hxx>
35 #include <TColgp_Array1OfPnt2d.hxx>
36 #include <TColStd_Array1OfReal.hxx>
37 #include <AdvApprox_PrefAndRec.hxx>
38 #include <AdvApprox_DichoCutting.hxx>
39
40 //=======================================================================
41 //class : Approx_CurveOnSurface_Eval
42 //purpose: evaluator class for approximation of both 2d and 3d curves
43 //=======================================================================
44
45 class Approx_CurveOnSurface_Eval : public AdvApprox_EvaluatorFunction
46 {
47  public:
48   Approx_CurveOnSurface_Eval (const Handle(Adaptor3d_HCurve)& theFunc, 
49                               const Handle(Adaptor2d_HCurve2d)& theFunc2d, 
50                               Standard_Real First, Standard_Real Last)
51     : fonct(theFunc), fonct2d(theFunc2d) 
52       { StartEndSav[0] = First; StartEndSav[1] = Last; }
53   
54   virtual void Evaluate (Standard_Integer *Dimension,
55                          Standard_Real     StartEnd[2],
56                          Standard_Real    *Parameter,
57                          Standard_Integer *DerivativeRequest,
58                          Standard_Real    *Result, // [Dimension]
59                          Standard_Integer *ErrorCode);
60   
61  private:
62   Handle(Adaptor3d_HCurve) fonct;
63   Handle(Adaptor2d_HCurve2d) fonct2d;
64   Standard_Real StartEndSav[2];
65 };
66
67 void Approx_CurveOnSurface_Eval::Evaluate (Standard_Integer *Dimension,
68                                            Standard_Real     StartEnd[2],
69                                            Standard_Real    *Param, // Parameter at which evaluation
70                                            Standard_Integer *Order, // Derivative Request
71                                            Standard_Real    *Result,// [Dimension]
72                                            Standard_Integer *ErrorCode)
73 {
74   *ErrorCode = 0;
75   Standard_Real par = *Param;
76
77 // Dimension is incorrect
78   if (*Dimension != 5) {
79     *ErrorCode = 1;
80   }
81
82 // Parameter is incorrect
83   if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1]) 
84     {
85       fonct = fonct->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
86       fonct2d = fonct2d->Trim(StartEnd[0],StartEnd[1],
87                                 Precision::PConfusion());
88       StartEndSav[0]=StartEnd[0];
89       StartEndSav[1]=StartEnd[1];
90     }
91   gp_Pnt pnt;
92
93
94   gp_Pnt2d pnt2d;
95
96   switch (*Order) {
97   case 0: 
98     {
99       fonct2d->D0(par, pnt2d);
100       fonct->D0(par, pnt);
101       Result[0] = pnt2d.X();
102       Result[1] = pnt2d.Y();
103       Result[2] = pnt.X();
104       Result[3] = pnt.Y();
105       Result[4] = pnt.Z();
106       break;
107     }
108   case 1:
109     {
110       gp_Vec v1;
111       gp_Vec2d v21;
112       fonct2d->D1(par, pnt2d, v21);
113       fonct->D1(par,pnt, v1);
114       Result[0] = v21.X();
115       Result[1] = v21.Y();
116       Result[2] = v1.X();
117       Result[3] = v1.Y();
118       Result[4] = v1.Z();
119       break;
120   }
121   case 2:
122     {
123       gp_Vec v1, v2;
124       gp_Vec2d v21, v22;    
125       fonct2d->D2(par, pnt2d, v21, v22);
126       fonct->D2(par, pnt, v1, v2);         
127       Result[0] = v22.X();
128       Result[1] = v22.Y();
129       Result[2] = v2.X();
130       Result[3] = v2.Y();
131       Result[4] = v2.Z();
132       break;
133     }
134   default:
135     Result[0] = Result[1] = Result[2] = Result[3] = Result[4] = 0.;
136     *ErrorCode = 3;
137     break;
138   }
139 }
140
141 //=======================================================================
142 //class : Approx_CurveOnSurface_Eval3d
143 //purpose: evaluator class for approximation of 3d curve
144 //=======================================================================
145
146 class Approx_CurveOnSurface_Eval3d : public AdvApprox_EvaluatorFunction
147 {
148  public:
149   Approx_CurveOnSurface_Eval3d (const Handle(Adaptor3d_HCurve)& theFunc, 
150                                 Standard_Real First, Standard_Real Last)
151     : fonct(theFunc) { StartEndSav[0] = First; StartEndSav[1] = Last; }
152   
153   virtual void Evaluate (Standard_Integer *Dimension,
154                          Standard_Real     StartEnd[2],
155                          Standard_Real    *Parameter,
156                          Standard_Integer *DerivativeRequest,
157                          Standard_Real    *Result, // [Dimension]
158                          Standard_Integer *ErrorCode);
159   
160  private:
161   Handle(Adaptor3d_HCurve) fonct;
162   Standard_Real StartEndSav[2];
163 };
164
165 void Approx_CurveOnSurface_Eval3d::Evaluate (Standard_Integer *Dimension,
166                                              Standard_Real     StartEnd[2],
167                                              Standard_Real    *Param, // Parameter at which evaluation
168                                              Standard_Integer *Order, // Derivative Request
169                                              Standard_Real    *Result,// [Dimension]
170                                              Standard_Integer *ErrorCode)
171 {
172   *ErrorCode = 0;
173   Standard_Real par = *Param;
174
175 // Dimension is incorrect
176   if (*Dimension != 3) {
177     *ErrorCode = 1;
178   }
179
180 // Parameter is incorrect
181   if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1]) 
182     {
183       fonct = fonct->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
184       StartEndSav[0]=StartEnd[0];
185       StartEndSav[1]=StartEnd[1];
186     }
187
188   gp_Pnt pnt;
189
190   switch (*Order) {
191   case 0:
192     pnt = fonct->Value(par);
193     Result[0] = pnt.X();
194     Result[1] = pnt.Y();
195     Result[2] = pnt.Z();
196     break;
197   case 1:
198     {
199       gp_Vec v1;
200       fonct->D1(par, pnt, v1);
201       Result[0] = v1.X();
202       Result[1] = v1.Y();
203       Result[2] = v1.Z();
204       break;
205     }
206   case 2:
207     {
208       gp_Vec v1, v2;
209       fonct->D2(par, pnt, v1, v2);
210       Result[0] = v2.X();
211       Result[1] = v2.Y();
212       Result[2] = v2.Z();
213       break;
214     }
215   default:
216     Result[0] = Result[1] = Result[2] = 0.;
217     *ErrorCode = 3;
218     break;
219   }
220 }
221
222 //=======================================================================
223 //class : Approx_CurveOnSurface_Eval2d
224 //purpose: evaluator class for approximation of 2d curve
225 //=======================================================================
226
227 class Approx_CurveOnSurface_Eval2d : public AdvApprox_EvaluatorFunction
228 {
229  public:
230   Approx_CurveOnSurface_Eval2d (const Handle(Adaptor2d_HCurve2d)& theFunc2d, 
231                                 Standard_Real First, Standard_Real Last)
232     : fonct2d(theFunc2d) { StartEndSav[0] = First; StartEndSav[1] = Last; }
233   
234   virtual void Evaluate (Standard_Integer *Dimension,
235                          Standard_Real     StartEnd[2],
236                          Standard_Real    *Parameter,
237                          Standard_Integer *DerivativeRequest,
238                          Standard_Real    *Result, // [Dimension]
239                          Standard_Integer *ErrorCode);
240   
241  private:
242   Handle(Adaptor2d_HCurve2d) fonct2d;
243   Standard_Real StartEndSav[2];
244 };
245
246 void Approx_CurveOnSurface_Eval2d::Evaluate (Standard_Integer *Dimension,
247                                              Standard_Real     StartEnd[2],
248                                              Standard_Real    *Param, // Parameter at which evaluation
249                                              Standard_Integer *Order, // Derivative Request
250                                              Standard_Real    *Result,// [Dimension]
251                                              Standard_Integer *ErrorCode)
252 {
253   *ErrorCode = 0;
254   Standard_Real par = *Param;
255
256 // Dimension is incorrect
257   if (*Dimension != 2) {
258     *ErrorCode = 1;
259   }
260
261 // Parameter is incorrect
262   if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1]) 
263     {
264       fonct2d = fonct2d->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
265       StartEndSav[0]=StartEnd[0];
266       StartEndSav[1]=StartEnd[1];
267     }
268  
269   gp_Pnt2d pnt;
270
271   switch (*Order) {
272   case 0:
273     {
274       pnt = fonct2d->Value(par);
275       Result[0] = pnt.X();
276       Result[1] = pnt.Y();
277       break;
278     }
279   case 1:
280     {
281       gp_Vec2d v1;
282       fonct2d->D1(par, pnt, v1);
283       Result[0] = v1.X();
284       Result[1] = v1.Y();
285       break;
286     }
287   case 2:
288     {
289       gp_Vec2d v1, v2;
290       fonct2d->D2(par, pnt, v1, v2);
291       Result[0] = v2.X();
292       Result[1] = v2.Y();
293       break;
294     }
295   default:
296     Result[0] = Result[1] = 0.;
297     *ErrorCode = 3;
298     break;
299   }
300 }
301
302  Approx_CurveOnSurface::Approx_CurveOnSurface(const Handle(Adaptor2d_HCurve2d)& C2D,
303                                               const Handle(Adaptor3d_HSurface)& Surf,
304                                               const Standard_Real First,
305                                               const Standard_Real Last,
306                                               const Standard_Real Tol,
307                                               const GeomAbs_Shape S,
308                                               const Standard_Integer MaxDegree,
309                                               const Standard_Integer MaxSegments, 
310                                               const Standard_Boolean only3d, 
311                                               const Standard_Boolean only2d)
312 {
313   myIsDone = Standard_False;
314   if(only3d && only2d) Standard_ConstructionError::Raise();
315   GeomAbs_Shape Order  = S;
316
317   Handle( Adaptor2d_HCurve2d ) TrimmedC2D = C2D->Trim( First, Last, Precision::PConfusion() );
318
319   Adaptor3d_CurveOnSurface COnS( TrimmedC2D, Surf );
320   Handle(Adaptor3d_HCurveOnSurface) HCOnS = new Adaptor3d_HCurveOnSurface();
321   HCOnS->Set(COnS);
322
323   Standard_Integer Num1DSS = 0, Num2DSS=0, Num3DSS=0;
324   Handle(TColStd_HArray1OfReal) OneDTol;
325   Handle(TColStd_HArray1OfReal) TwoDTolNul;
326   Handle(TColStd_HArray1OfReal) ThreeDTol;
327
328   // create evaluators and choose appropriate one
329   Approx_CurveOnSurface_Eval3d Eval3dCvOnSurf (HCOnS,             First, Last);
330   Approx_CurveOnSurface_Eval2d Eval2dCvOnSurf (       TrimmedC2D, First, Last);
331   Approx_CurveOnSurface_Eval   EvalCvOnSurf   (HCOnS, TrimmedC2D, First, Last);
332   AdvApprox_EvaluatorFunction* EvalPtr;
333   if ( only3d ) EvalPtr = &Eval3dCvOnSurf;
334   else if ( only2d ) EvalPtr = &Eval2dCvOnSurf;
335   else EvalPtr = &EvalCvOnSurf;
336
337   // Initialization for 2d approximation
338   if(!only3d) {
339     Num1DSS = 2;
340     OneDTol = new TColStd_HArray1OfReal(1,Num1DSS);
341
342     Standard_Real TolU, TolV;
343
344     TolU = Surf->UResolution(Tol)/2;
345     TolV = Surf->VResolution(Tol)/2;
346
347     OneDTol->SetValue(1,TolU);
348     OneDTol->SetValue(2,TolV);
349   }
350   
351   if(!only2d) {
352     Num3DSS=1;
353     ThreeDTol = new TColStd_HArray1OfReal(1,Num3DSS);
354     ThreeDTol->Init(Tol/2);
355   }
356
357   myError2dU = 0;
358   myError2dV = 0;
359   myError3d = 0;
360
361   Standard_Integer NbInterv_C2 = HCOnS->NbIntervals(GeomAbs_C2);
362   TColStd_Array1OfReal CutPnts_C2(1, NbInterv_C2 + 1);
363   HCOnS->Intervals(CutPnts_C2, GeomAbs_C2);
364   Standard_Integer NbInterv_C3 = HCOnS->NbIntervals(GeomAbs_C3);
365   TColStd_Array1OfReal CutPnts_C3(1, NbInterv_C3 + 1);
366   HCOnS->Intervals(CutPnts_C3, GeomAbs_C3);
367   
368   AdvApprox_PrefAndRec CutTool(CutPnts_C2,CutPnts_C3);
369   AdvApprox_ApproxAFunction aApprox (Num1DSS, Num2DSS, Num3DSS, 
370                                      OneDTol, TwoDTolNul, ThreeDTol,
371                                      First, Last, Order,
372                                      MaxDegree, MaxSegments,
373                                      *EvalPtr, CutTool);
374
375   myIsDone = aApprox.IsDone();
376   myHasResult = aApprox.HasResult();
377   
378   if (myHasResult) {
379     Handle(TColStd_HArray1OfReal)    Knots = aApprox.Knots();
380     Handle(TColStd_HArray1OfInteger) Mults = aApprox.Multiplicities();
381     Standard_Integer Degree = aApprox.Degree();
382
383     if(!only2d) 
384       {
385         TColgp_Array1OfPnt Poles(1,aApprox.NbPoles());
386         aApprox.Poles(1,Poles);
387         myCurve3d = new Geom_BSplineCurve(Poles, Knots->Array1(), Mults->Array1(), Degree);
388         myError3d = aApprox.MaxError(3, 1);
389       }
390     if(!only3d) 
391       {
392         TColgp_Array1OfPnt2d Poles2d(1,aApprox.NbPoles());
393         TColStd_Array1OfReal Poles1dU(1,aApprox.NbPoles());
394         aApprox.Poles1d(1, Poles1dU);
395         TColStd_Array1OfReal Poles1dV(1,aApprox.NbPoles());
396         aApprox.Poles1d(2, Poles1dV);
397         for(Standard_Integer i = 1; i <= aApprox.NbPoles(); i++)
398           Poles2d.SetValue(i, gp_Pnt2d(Poles1dU.Value(i), Poles1dV.Value(i)));
399         myCurve2d = new Geom2d_BSplineCurve(Poles2d, Knots->Array1(), Mults->Array1(), Degree);
400         
401         myError2dU = aApprox.MaxError(1, 1);
402         myError2dV = aApprox.MaxError(1, 2);
403       }
404   }
405   
406 //    }
407
408 }
409
410  Standard_Boolean Approx_CurveOnSurface::IsDone() const
411 {
412   return myIsDone;
413 }
414
415  Standard_Boolean Approx_CurveOnSurface::HasResult() const
416 {
417   return myHasResult;
418 }
419
420  Handle(Geom_BSplineCurve) Approx_CurveOnSurface::Curve3d() const
421 {
422   return myCurve3d;
423 }
424
425  Handle(Geom2d_BSplineCurve) Approx_CurveOnSurface::Curve2d() const
426 {
427   return myCurve2d;
428 }
429
430  Standard_Real Approx_CurveOnSurface::MaxError3d() const
431 {
432   return myError3d;
433 }
434
435  Standard_Real Approx_CurveOnSurface::MaxError2dU() const
436 {
437   return myError2dU;
438 }
439
440  Standard_Real Approx_CurveOnSurface::MaxError2dV() const
441 {
442   return myError2dV;
443 }
444