summaryrefslogtreecommitdiff
path: root/inc/GCPnts_QuasiUniformDeflection.gxx
blob: 0ea22b8d3fa5711ec43270d8393841b22867a14b (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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include <StdFail_NotDone.hxx>
#include <Standard_DomainError.hxx>
#include <Standard_OutOfRange.hxx>
#include <Standard_ConstructionError.hxx>
#include <Standard_NotImplemented.hxx>
#include <GCPnts_DeflectionType.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColStd_SequenceOfReal.hxx>
#include <BSplCLib.hxx>
#include <gp_Circ.hxx>
#include <gp_Circ2d.hxx>
#include <Precision.hxx>

static void QuasiFleche(const TheCurve&,
			const Standard_Real,
			const Standard_Real,
			const gp_Pnt&,
			const gp_Vec&,
			const Standard_Real,
			const gp_Pnt&,
			const gp_Vec&,
			const Standard_Integer,
			const Standard_Real,
			TColStd_SequenceOfReal&,
			TColgp_SequenceOfPnt&);

static void QuasiFleche(const TheCurve&,
			const Standard_Real,
			const Standard_Real,
			const gp_Pnt&,
			const Standard_Real,
			const gp_Pnt&,
			const Standard_Integer,
			TColStd_SequenceOfReal&,
			TColgp_SequenceOfPnt&);


//=======================================================================
//function : PerformLinear
//purpose  :
//=======================================================================
static Standard_Boolean PerformLinear (const TheCurve& C,
                                       TColStd_SequenceOfReal& Parameters,
                                       TColgp_SequenceOfPnt& Points,
                                       const Standard_Real U1,
                                       const Standard_Real U2)
{
  gp_Pnt aPoint;
  Parameters.Append (U1);
  aPoint = Value (C, U1);
  Points.Append (aPoint);

  Parameters.Append (U2);
  aPoint = Value (C, U2);
  Points.Append (aPoint);
  return Standard_True;
}


//=======================================================================
//function : PerformCircular
//purpose  :
//=======================================================================
static Standard_Boolean PerformCircular (const TheCurve& C,
                                         TColStd_SequenceOfReal& Parameters,
                                         TColgp_SequenceOfPnt& Points,
                                         const Standard_Real Deflection,
                                         const Standard_Real U1,
                                         const Standard_Real U2)

{
  gp_Pnt aPoint;
  Standard_Real Angle = Max (1.0e0 - (Deflection / C.Circle().Radius()), 0.0e0);
  Angle = 2.0e0 * ACos (Angle);
  Standard_Integer NbPoints = (Standard_Integer )((U2 - U1) / Angle);
  NbPoints += 2;
  Angle = (U2 - U1) / (Standard_Real) (NbPoints - 1);
  Standard_Real U = U1;
  for (Standard_Integer i = 1; i <= NbPoints; ++i)
  {
    Parameters.Append (U);
    aPoint = Value (C,U);
    Points.Append (aPoint);
    U += Angle;
  }
  return Standard_True;
}


//=======================================================================
//function : GetDefType
//purpose  :
//=======================================================================
static GCPnts_DeflectionType GetDefType (TheCurve& C)
{
  if (C.NbIntervals(GeomAbs_C1) > 1)
    return GCPnts_DefComposite;
  // pour forcer les decoupages aux cassures. G1 devrait marcher,
  // mais donne des exceptions...

  switch (C.GetType())
  {
    case GeomAbs_Line:   return GCPnts_Linear;
    case GeomAbs_Circle: return GCPnts_Circular;
    case GeomAbs_BSplineCurve:
    {
      Handle_TheBSplineCurve BS = C.BSpline();
      return (BS->NbPoles() == 2) ? GCPnts_Linear : GCPnts_Curved;
    }
    case GeomAbs_BezierCurve:
    {
      Handle_TheBezierCurve BZ = C.Bezier();
      return (BZ->NbPoles() == 2) ? GCPnts_Linear : GCPnts_Curved;
    }
    default: return GCPnts_Curved;
  }
}


//=======================================================================
//function : PerformCurve
//purpose  :
//=======================================================================
static Standard_Boolean PerformCurve (TColStd_SequenceOfReal& Parameters,
                                      TColgp_SequenceOfPnt& Points,
                                      const TheCurve& C,
                                      const Standard_Real Deflection,
                                      const Standard_Real U1,
                                      const Standard_Real U2,
                                      const Standard_Real EPSILON,
                                      const GeomAbs_Shape Continuity)
{
  Standard_Integer Nbmin = 21;

  gp_Pnt Pdeb;
  if (Continuity <= GeomAbs_G1)
  {

    Pdeb = Value (C, U1);
    Parameters.Append (U1);
    Points.Append (Pdeb);

    gp_Pnt Pfin (Value (C, U2));
    QuasiFleche (C, Deflection * Deflection,
		U1, Pdeb,
		U2, Pfin,
		Nbmin,
		Parameters, Points);
  }
  else
  {
    gp_Pnt Pfin;
    gp_Vec Ddeb, Dfin;
    D1 (C, U1, Pdeb, Ddeb);
    Parameters.Append (U1);
    Points.Append (Pdeb);

    D1 (C, U2, Pfin, Dfin);
    QuasiFleche (C, Deflection * Deflection,
                 U1, Pdeb,
                 Ddeb,
                 U2, Pfin,
                 Dfin,
                 Nbmin,
                 EPSILON * EPSILON,
                 Parameters, Points);
  }
//  cout << "Nb de pts: " << Points.Length()<< endl;
  return Standard_True;
}


//=======================================================================
//function : PerformComposite
//purpose  :
//=======================================================================
static Standard_Boolean PerformComposite (TColStd_SequenceOfReal& Parameters,
                                          TColgp_SequenceOfPnt& Points,
                                          TheCurve& C,
                                          const Standard_Real Deflection,
                                          const Standard_Real U1,
                                          const Standard_Real U2,
                                          const Standard_Real EPSILON,
                                          const GeomAbs_Shape Continuity)
{
//
//  coherence avec Intervals
//
  Standard_Integer NbIntervals = C.NbIntervals (GeomAbs_C2);
  Standard_Integer PIndex;
  TColStd_Array1OfReal TI (1, NbIntervals + 1);
  C.Intervals (TI, GeomAbs_C2);
  BSplCLib::Hunt (TI, U1, PIndex);

  // iterate by continuous segments
  Standard_Real Ua = U1;
  for (Standard_Integer Index = PIndex;;)
  {
    Standard_Real Ub = Min (U2, TI (Index + 1));
    if (!PerformCurve (Parameters, Points, C, Deflection,
			           Ua, Ub, EPSILON, Continuity))
      return Standard_False;

    ++Index;
    if (Index > NbIntervals || U2 < TI (Index))
      return Standard_True;

    // remove last point to avoid duplication
    Parameters.Remove (Parameters.Length());
    Points.Remove (Points.Length());

    Ua = Ub;
  }
 
#ifndef _MSC_VER
  return Standard_True;
#endif
}


//=======================================================================
//function : GCPnts_QuasiUniformDeflection
//purpose  :
//=======================================================================
GCPnts_QuasiUniformDeflection::GCPnts_QuasiUniformDeflection
                               (TheCurve& C,
                                const Standard_Real Deflection,
                                const Standard_Real U1,
                                const Standard_Real U2,
                                const GeomAbs_Shape Continuity)
{
  Initialize (C, Deflection, U1, U2, Continuity);
}


//=======================================================================
//function : GCPnts_QuasiUniformDeflection
//purpose  :
//=======================================================================
GCPnts_QuasiUniformDeflection::GCPnts_QuasiUniformDeflection
                               (TheCurve& C,
                                const Standard_Real Deflection,
                                const GeomAbs_Shape Continuity)
{
  Initialize (C, Deflection, Continuity);
}


//=======================================================================
//function : Initialize
//purpose  :
//=======================================================================
void GCPnts_QuasiUniformDeflection::Initialize (TheCurve& C,
                                                const Standard_Real Deflection,
                                                const GeomAbs_Shape Continuity)
{
  Initialize (C, Deflection, C.FirstParameter(),
              C.LastParameter(), Continuity);
}


//=======================================================================
//function : Initialize
//purpose  :
//=======================================================================

void GCPnts_QuasiUniformDeflection::Initialize
                     (TheCurve& C,
				      const Standard_Real Deflection,
				      const Standard_Real theU1,
				      const Standard_Real theU2,
				      const GeomAbs_Shape Continuity)
{
  myCont = (Continuity > GeomAbs_G1) ? GeomAbs_C1 : GeomAbs_C0;
  Standard_Real EPSILON = C.Resolution (Precision::Confusion());
  EPSILON = Min (EPSILON, 1.e50);
  myDeflection = Deflection;
  myDone = Standard_False;
  myParams.Clear();
  myPoints.Clear();
  GCPnts_DeflectionType Type = GetDefType (C);

  Standard_Real U1 = Min (theU1, theU2);
  Standard_Real U2 = Max (theU1, theU2);

  if (Type == GCPnts_Curved || Type == GCPnts_DefComposite)
  {
    if (C.GetType() == GeomAbs_BSplineCurve || C.GetType() == GeomAbs_BezierCurve)
    {
      Standard_Real maxpar = Max (Abs (C.FirstParameter()), Abs (C.LastParameter()));
      if (EPSILON < Epsilon (maxpar)) return;
    }
  }

  switch (Type)
  {
    case GCPnts_Linear:
      myDone = PerformLinear (C, myParams, myPoints, U1, U2);
      break;
    case GCPnts_Circular:
      myDone = PerformCircular (C, myParams, myPoints, Deflection, U1, U2);
      break;
    case GCPnts_Curved:
      myDone = PerformCurve (myParams, myPoints, C, Deflection,
                             U1, U2, EPSILON, myCont);
      break;
    case GCPnts_DefComposite:
      myDone = PerformComposite (myParams, myPoints, C, Deflection,
                                 U1, U2, EPSILON, myCont);
      break;
  }
}


//=======================================================================
//function : QuasiFleche
//purpose  :
//=======================================================================
void QuasiFleche (const TheCurve& C,
                  const Standard_Real Deflection2,
                  const Standard_Real Udeb,
                  const gp_Pnt& Pdeb,
                  const gp_Vec& Vdeb,
                  const Standard_Real Ufin,
                  const gp_Pnt& Pfin,
                  const gp_Vec& Vfin,
                  const Standard_Integer Nbmin,
                  const Standard_Real Eps,
                  TColStd_SequenceOfReal& Parameters,
                  TColgp_SequenceOfPnt& Points)
{
  Standard_Integer Ptslength = Points.Length();
  Standard_Real Udelta = Ufin - Udeb;
  gp_Pnt Pdelta;
  gp_Vec Vdelta;
  if (Nbmin > 2)
  {
    Udelta /= (Nbmin - 1);
    D1 (C, Udeb + Udelta, Pdelta, Vdelta);
  }
  else
  {
    Pdelta = Pfin;
    Vdelta = Vfin;
  }

  Standard_Real Norme = gp_Vec (Pdeb, Pdelta).SquareMagnitude();
  Standard_Real theFleche = 0;
  Standard_Boolean flecheok = Standard_False;
  if (Norme > Eps)
  {
    // Evaluation de la fleche par interpolation . Voir IntWalk_IWalking_5.gxx
    Standard_Real N1 = Vdeb.SquareMagnitude();
    Standard_Real N2 = Vdelta.SquareMagnitude();
    if (N1 > Eps && N2 > Eps)
    {
      Standard_Real Normediff = (Vdeb.Normalized().XYZ() - Vdelta.Normalized().XYZ()).SquareModulus();
      if (Normediff > Eps)
      {
        theFleche = Normediff * Norme / 64.;
        flecheok = Standard_True;
      }
    }
  }
  if (!flecheok)
  {
    gp_Pnt Pmid ((Pdeb.XYZ() + Pdelta.XYZ()) * 0.5);
    gp_Pnt Pverif (Value(C, Udeb + Udelta * 0.5));
    theFleche = Pmid.SquareDistance (Pverif);
  }

  if (theFleche < Deflection2)
  {
    Parameters.Append (Udeb + Udelta);
    Points.Append (Pdelta);
  }
  else
  {
    QuasiFleche (C, Deflection2, Udeb, Pdeb,
                 Vdeb,
                 Udeb + Udelta, Pdelta,
                 Vdelta,
                 3,
                 Eps,
                 Parameters, Points);
  }

  if (Nbmin > 2)
  {
    QuasiFleche (C, Deflection2, Udeb + Udelta, Pdelta,
                 Vdelta,
                 Ufin, Pfin,
                 Vfin,
                 Nbmin - (Points.Length() - Ptslength),
                 Eps,
                 Parameters, Points);
  }
}


//=======================================================================
//function : QuasiFleche
//purpose  :
//=======================================================================
void QuasiFleche (const TheCurve& C,
                  const Standard_Real Deflection2,
                  const Standard_Real Udeb,
                  const gp_Pnt& Pdeb,
                  const Standard_Real Ufin,
                  const gp_Pnt& Pfin,
                  const Standard_Integer Nbmin,
                  TColStd_SequenceOfReal& Parameters,
                  TColgp_SequenceOfPnt& Points)
{
  Standard_Integer Ptslength = Points.Length();
  Standard_Real Udelta = Ufin - Udeb;
  gp_Pnt Pdelta;
  if (Nbmin > 2)
  {
    Udelta /= (Nbmin-1);
    Pdelta = Value (C, Udeb + Udelta);
  }
  else
  {
    Pdelta = Pfin;
  }

  gp_Pnt Pmid ((Pdeb.XYZ() + Pdelta.XYZ()) * 0.5);
  gp_Pnt Pverif (Value (C, Udeb + Udelta * 0.5));
  Standard_Real theFleche = Pmid.SquareDistance (Pverif);

  if (theFleche < Deflection2)
  {
    Parameters.Append(Udeb + Udelta);
    Points.Append (Pdelta);
  }
  else
  {
    QuasiFleche (C, Deflection2, Udeb, Pdeb,
                 Udeb + Udelta * 0.5, Pverif,
                 2,
                 Parameters, Points);

    QuasiFleche (C, Deflection2, Udeb + Udelta * 0.5, Pverif,
                 Udeb + Udelta, Pdelta,
                 2,
                 Parameters, Points);
  }

  if (Nbmin > 2)
  {
    QuasiFleche (C, Deflection2, Udeb + Udelta, Pdelta,
                 Ufin, Pfin,
                 Nbmin - (Points.Length() - Ptslength),
                 Parameters, Points);
  }
}