summaryrefslogtreecommitdiff
path: root/src/Approx/Approx_CurveOnSurface.cxx
blob: ce825a80d65d1ada09b37fcd08d61fed4e4d0097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// File:	Approx_CurveOnSurface.cxx
// Created:	Mon Oct  6 14:34:17 1997
// Author:	Roman BORISOV
//		<rbv@velox.nnov.matra-dtv.fr>

#include <Precision.hxx>
#include <Approx_CurveOnSurface.ixx>
#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#include <GeomAdaptor_HSurface.hxx>
#include <Adaptor3d_CurveOnSurface.hxx>
#include <TColStd_HArray1OfReal.hxx>
#include <AdvApprox_ApproxAFunction.hxx>
#include <Adaptor3d_HCurve.hxx>
#include <TColgp_Array1OfPnt.hxx>
#include <GeomAdaptor_HCurve.hxx>
#include <Geom2dAdaptor_HCurve.hxx>
#include <Adaptor3d_HCurveOnSurface.hxx>
#include <TColgp_Array1OfPnt2d.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <AdvApprox_PrefAndRec.hxx>
#include <AdvApprox_DichoCutting.hxx>

//=======================================================================
//class : Approx_CurveOnSurface_Eval
//purpose: evaluator class for approximation of both 2d and 3d curves
//=======================================================================

class Approx_CurveOnSurface_Eval : public AdvApprox_EvaluatorFunction
{
 public:
  Approx_CurveOnSurface_Eval (const Handle(Adaptor3d_HCurve)& theFunc, 
                              const Handle(Adaptor2d_HCurve2d)& theFunc2d, 
                              Standard_Real First, Standard_Real Last)
    : fonct(theFunc), fonct2d(theFunc2d) 
      { StartEndSav[0] = First; StartEndSav[1] = Last; }
  
  virtual void Evaluate (Standard_Integer *Dimension,
		         Standard_Real     StartEnd[2],
                         Standard_Real    *Parameter,
                         Standard_Integer *DerivativeRequest,
                         Standard_Real    *Result, // [Dimension]
                         Standard_Integer *ErrorCode);
  
 private:
  Handle(Adaptor3d_HCurve) fonct;
  Handle(Adaptor2d_HCurve2d) fonct2d;
  Standard_Real StartEndSav[2];
};

void Approx_CurveOnSurface_Eval::Evaluate (Standard_Integer *Dimension,
                                           Standard_Real     StartEnd[2],
                                           Standard_Real    *Param, // Parameter at which evaluation
                                           Standard_Integer *Order, // Derivative Request
                                           Standard_Real    *Result,// [Dimension]
                                           Standard_Integer *ErrorCode)
{
  *ErrorCode = 0;
  Standard_Real par = *Param;

// Dimension is incorrect
  if (*Dimension != 5) {
    *ErrorCode = 1;
  }

// Parameter is incorrect
  if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1]) 
    {
      fonct = fonct->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
      fonct2d = fonct2d->Trim(StartEnd[0],StartEnd[1],
				Precision::PConfusion());
      StartEndSav[0]=StartEnd[0];
      StartEndSav[1]=StartEnd[1];
    }
  gp_Pnt pnt;


  gp_Pnt2d pnt2d;

  switch (*Order) {
  case 0: 
    {
      fonct2d->D0(par, pnt2d);
      fonct->D0(par, pnt);
      Result[0] = pnt2d.X();
      Result[1] = pnt2d.Y();
      Result[2] = pnt.X();
      Result[3] = pnt.Y();
      Result[4] = pnt.Z();
      break;
    }
  case 1:
    {
      gp_Vec v1;
      gp_Vec2d v21;
      fonct2d->D1(par, pnt2d, v21);
      fonct->D1(par,pnt, v1);
      Result[0] = v21.X();
      Result[1] = v21.Y();
      Result[2] = v1.X();
      Result[3] = v1.Y();
      Result[4] = v1.Z();
      break;
  }
  case 2:
    {
      gp_Vec v1, v2;
      gp_Vec2d v21, v22;    
      fonct2d->D2(par, pnt2d, v21, v22);
      fonct->D2(par, pnt, v1, v2);         
      Result[0] = v22.X();
      Result[1] = v22.Y();
      Result[2] = v2.X();
      Result[3] = v2.Y();
      Result[4] = v2.Z();
      break;
    }
  default:
    Result[0] = Result[1] = Result[2] = Result[3] = Result[4] = 0.;
    *ErrorCode = 3;
    break;
  }
}

//=======================================================================
//class : Approx_CurveOnSurface_Eval3d
//purpose: evaluator class for approximation of 3d curve
//=======================================================================

class Approx_CurveOnSurface_Eval3d : public AdvApprox_EvaluatorFunction
{
 public:
  Approx_CurveOnSurface_Eval3d (const Handle(Adaptor3d_HCurve)& theFunc, 
                                Standard_Real First, Standard_Real Last)
    : fonct(theFunc) { StartEndSav[0] = First; StartEndSav[1] = Last; }
  
  virtual void Evaluate (Standard_Integer *Dimension,
		         Standard_Real     StartEnd[2],
                         Standard_Real    *Parameter,
                         Standard_Integer *DerivativeRequest,
                         Standard_Real    *Result, // [Dimension]
                         Standard_Integer *ErrorCode);
  
 private:
  Handle(Adaptor3d_HCurve) fonct;
  Standard_Real StartEndSav[2];
};

void Approx_CurveOnSurface_Eval3d::Evaluate (Standard_Integer *Dimension,
                                             Standard_Real     StartEnd[2],
                                             Standard_Real    *Param, // Parameter at which evaluation
                                             Standard_Integer *Order, // Derivative Request
                                             Standard_Real    *Result,// [Dimension]
                                             Standard_Integer *ErrorCode)
{
  *ErrorCode = 0;
  Standard_Real par = *Param;

// Dimension is incorrect
  if (*Dimension != 3) {
    *ErrorCode = 1;
  }

// Parameter is incorrect
  if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1]) 
    {
      fonct = fonct->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
      StartEndSav[0]=StartEnd[0];
      StartEndSav[1]=StartEnd[1];
    }

  gp_Pnt pnt;

  switch (*Order) {
  case 0:
    pnt = fonct->Value(par);
    Result[0] = pnt.X();
    Result[1] = pnt.Y();
    Result[2] = pnt.Z();
    break;
  case 1:
    {
      gp_Vec v1;
      fonct->D1(par, pnt, v1);
      Result[0] = v1.X();
      Result[1] = v1.Y();
      Result[2] = v1.Z();
      break;
    }
  case 2:
    {
      gp_Vec v1, v2;
      fonct->D2(par, pnt, v1, v2);
      Result[0] = v2.X();
      Result[1] = v2.Y();
      Result[2] = v2.Z();
      break;
    }
  default:
    Result[0] = Result[1] = Result[2] = 0.;
    *ErrorCode = 3;
    break;
  }
}

//=======================================================================
//class : Approx_CurveOnSurface_Eval2d
//purpose: evaluator class for approximation of 2d curve
//=======================================================================

class Approx_CurveOnSurface_Eval2d : public AdvApprox_EvaluatorFunction
{
 public:
  Approx_CurveOnSurface_Eval2d (const Handle(Adaptor2d_HCurve2d)& theFunc2d, 
                                Standard_Real First, Standard_Real Last)
    : fonct2d(theFunc2d) { StartEndSav[0] = First; StartEndSav[1] = Last; }
  
  virtual void Evaluate (Standard_Integer *Dimension,
		         Standard_Real     StartEnd[2],
                         Standard_Real    *Parameter,
                         Standard_Integer *DerivativeRequest,
                         Standard_Real    *Result, // [Dimension]
                         Standard_Integer *ErrorCode);
  
 private:
  Handle(Adaptor2d_HCurve2d) fonct2d;
  Standard_Real StartEndSav[2];
};

void Approx_CurveOnSurface_Eval2d::Evaluate (Standard_Integer *Dimension,
                                             Standard_Real     StartEnd[2],
                                             Standard_Real    *Param, // Parameter at which evaluation
                                             Standard_Integer *Order, // Derivative Request
                                             Standard_Real    *Result,// [Dimension]
                                             Standard_Integer *ErrorCode)
{
  *ErrorCode = 0;
  Standard_Real par = *Param;

// Dimension is incorrect
  if (*Dimension != 2) {
    *ErrorCode = 1;
  }

// Parameter is incorrect
  if(StartEnd[0] != StartEndSav[0] || StartEnd[1]!= StartEndSav[1]) 
    {
      fonct2d = fonct2d->Trim(StartEnd[0],StartEnd[1],Precision::PConfusion());
      StartEndSav[0]=StartEnd[0];
      StartEndSav[1]=StartEnd[1];
    }
 
  gp_Pnt2d pnt;

  switch (*Order) {
  case 0:
    {
      pnt = fonct2d->Value(par);
      Result[0] = pnt.X();
      Result[1] = pnt.Y();
      break;
    }
  case 1:
    {
      gp_Vec2d v1;
      fonct2d->D1(par, pnt, v1);
      Result[0] = v1.X();
      Result[1] = v1.Y();
      break;
    }
  case 2:
    {
      gp_Vec2d v1, v2;
      fonct2d->D2(par, pnt, v1, v2);
      Result[0] = v2.X();
      Result[1] = v2.Y();
      break;
    }
  default:
    Result[0] = Result[1] = 0.;
    *ErrorCode = 3;
    break;
  }
}

 Approx_CurveOnSurface::Approx_CurveOnSurface(const Handle(Adaptor2d_HCurve2d)& C2D,
					      const Handle(Adaptor3d_HSurface)& Surf,
					      const Standard_Real First,
					      const Standard_Real Last,
					      const Standard_Real Tol,
					      const GeomAbs_Shape S,
					      const Standard_Integer MaxDegree,
					      const Standard_Integer MaxSegments, 
					      const Standard_Boolean only3d, 
					      const Standard_Boolean only2d)
{
  myIsDone = Standard_False;
  if(only3d && only2d) Standard_ConstructionError::Raise();
  GeomAbs_Shape Order  = S;

  Handle( Adaptor2d_HCurve2d ) TrimmedC2D = C2D->Trim( First, Last, Precision::PConfusion() );

  Adaptor3d_CurveOnSurface COnS( TrimmedC2D, Surf );
  Handle(Adaptor3d_HCurveOnSurface) HCOnS = new Adaptor3d_HCurveOnSurface();
  HCOnS->Set(COnS);

  Standard_Integer Num1DSS = 0, Num2DSS=0, Num3DSS=0;
  Handle(TColStd_HArray1OfReal) OneDTol;
  Handle(TColStd_HArray1OfReal) TwoDTolNul;
  Handle(TColStd_HArray1OfReal) ThreeDTol;

  // create evaluators and choose appropriate one
  Approx_CurveOnSurface_Eval3d Eval3dCvOnSurf (HCOnS,             First, Last);
  Approx_CurveOnSurface_Eval2d Eval2dCvOnSurf (       TrimmedC2D, First, Last);
  Approx_CurveOnSurface_Eval   EvalCvOnSurf   (HCOnS, TrimmedC2D, First, Last);
  AdvApprox_EvaluatorFunction* EvalPtr;
  if ( only3d ) EvalPtr = &Eval3dCvOnSurf;
  else if ( only2d ) EvalPtr = &Eval2dCvOnSurf;
  else EvalPtr = &EvalCvOnSurf;

  // Initialization for 2d approximation
  if(!only3d) {
    Num1DSS = 2;
    OneDTol = new TColStd_HArray1OfReal(1,Num1DSS);

    Standard_Real TolU, TolV;

    TolU = Surf->UResolution(Tol)/2;
    TolV = Surf->VResolution(Tol)/2;

    OneDTol->SetValue(1,TolU);
    OneDTol->SetValue(2,TolV);
  }
  
  if(!only2d) {
    Num3DSS=1;
    ThreeDTol = new TColStd_HArray1OfReal(1,Num3DSS);
    ThreeDTol->Init(Tol/2);
  }

  myError2dU = 0;
  myError2dV = 0;
  myError3d = 0;

  Standard_Integer NbInterv_C2 = HCOnS->NbIntervals(GeomAbs_C2);
  TColStd_Array1OfReal CutPnts_C2(1, NbInterv_C2 + 1);
  HCOnS->Intervals(CutPnts_C2, GeomAbs_C2);
  Standard_Integer NbInterv_C3 = HCOnS->NbIntervals(GeomAbs_C3);
  TColStd_Array1OfReal CutPnts_C3(1, NbInterv_C3 + 1);
  HCOnS->Intervals(CutPnts_C3, GeomAbs_C3);
  
  AdvApprox_PrefAndRec CutTool(CutPnts_C2,CutPnts_C3);
  AdvApprox_ApproxAFunction aApprox (Num1DSS, Num2DSS, Num3DSS, 
	      			     OneDTol, TwoDTolNul, ThreeDTol,
				     First, Last, Order,
				     MaxDegree, MaxSegments,
				     *EvalPtr, CutTool);

  myIsDone = aApprox.IsDone();
  myHasResult = aApprox.HasResult();
  
  if (myHasResult) {
    Handle(TColStd_HArray1OfReal)    Knots = aApprox.Knots();
    Handle(TColStd_HArray1OfInteger) Mults = aApprox.Multiplicities();
    Standard_Integer Degree = aApprox.Degree();

    if(!only2d) 
      {
	TColgp_Array1OfPnt Poles(1,aApprox.NbPoles());
	aApprox.Poles(1,Poles);
	myCurve3d = new Geom_BSplineCurve(Poles, Knots->Array1(), Mults->Array1(), Degree);
	myError3d = aApprox.MaxError(3, 1);
      }
    if(!only3d) 
      {
	TColgp_Array1OfPnt2d Poles2d(1,aApprox.NbPoles());
	TColStd_Array1OfReal Poles1dU(1,aApprox.NbPoles());
	aApprox.Poles1d(1, Poles1dU);
	TColStd_Array1OfReal Poles1dV(1,aApprox.NbPoles());
	aApprox.Poles1d(2, Poles1dV);
	for(Standard_Integer i = 1; i <= aApprox.NbPoles(); i++)
	  Poles2d.SetValue(i, gp_Pnt2d(Poles1dU.Value(i), Poles1dV.Value(i)));
	myCurve2d = new Geom2d_BSplineCurve(Poles2d, Knots->Array1(), Mults->Array1(), Degree);
	
	myError2dU = aApprox.MaxError(1, 1);
	myError2dV = aApprox.MaxError(1, 2);
      }
  }
  
//    }

}

 Standard_Boolean Approx_CurveOnSurface::IsDone() const
{
  return myIsDone;
}

 Standard_Boolean Approx_CurveOnSurface::HasResult() const
{
  return myHasResult;
}

 Handle(Geom_BSplineCurve) Approx_CurveOnSurface::Curve3d() const
{
  return myCurve3d;
}

 Handle(Geom2d_BSplineCurve) Approx_CurveOnSurface::Curve2d() const
{
  return myCurve2d;
}

 Standard_Real Approx_CurveOnSurface::MaxError3d() const
{
  return myError3d;
}

 Standard_Real Approx_CurveOnSurface::MaxError2dU() const
{
  return myError2dU;
}

 Standard_Real Approx_CurveOnSurface::MaxError2dV() const
{
  return myError2dV;
}