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