summaryrefslogtreecommitdiff
path: root/inc/GCPnts_UniformDeflection.gxx
blob: 577bb376af6036bebc4975a8e716930dc1e62eaa (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
#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 <CPnts_UniformDeflection.hxx>
#include <TColStd_Array1OfReal.hxx>
#include <TColStd_SequenceOfReal.hxx>
#include <BSplCLib.hxx>
#include <gp_Circ.hxx>
#include <gp_Circ2d.hxx>
#include <Precision.hxx>


//=======================================================================
//function : Controle
//purpose  :
//=======================================================================
static void Controle (const TheCurve& C,
                      TColStd_SequenceOfReal& Parameters,
                      TColgp_SequenceOfPnt& Points,
                      const Standard_Real U2)
{
  Standard_Integer nbp = Points.Length();

  if (nbp > 2)
  {
    Standard_Real Ua = Parameters (nbp - 2);
    Standard_Real Ub = Parameters (nbp - 1);
    if (U2 - Ub < 0.33 * (U2 - Ua))
    {
      Standard_Real Uc = (U2 + Ua) * 0.5;
      Parameters (nbp - 1) = Uc;
      Points (nbp - 1) = Value (C, Uc);
    }
  }
}


//=======================================================================
//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;
}


static GCPnts_DeflectionType GetDefType (TheCurve& C)
{
  if (C.NbIntervals (GeomAbs_C2) > 1)
    return GCPnts_DefComposite;

  switch (C.GetType())
  {
    case GeomAbs_Line:   return GCPnts_Linear;
    case GeomAbs_Circle: return GCPnts_Circular;
    case GeomAbs_BSplineCurve:
    {
      Handle_TheBSplineCurve aBSpline = C.BSpline();
      return (aBSpline->NbPoles() == 2) ? GCPnts_Linear : GCPnts_Curved;
    }
    case GeomAbs_BezierCurve:
    {
      Handle_TheBezierCurve aBezier = C.Bezier();
      return (aBezier->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 Standard_Boolean WithControl)
{
  CPnts_UniformDeflection Iterator (C, Deflection, U1, U2, EPSILON, WithControl);
  for(; Iterator.More(); Iterator.Next())
  {
    Parameters.Append (Iterator.Value());
    Points.Append (Iterator.Point());
  }
  return Iterator.IsAllDone();
}


//=======================================================================
//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 Standard_Boolean WithControl)
{
  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, WithControl))
    {
      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 (Parameters.Length());

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


//=======================================================================
//function : GCPnts_UniformDeflection
//purpose  :
//=======================================================================
GCPnts_UniformDeflection::GCPnts_UniformDeflection (TheCurve& C,
                                                    const Standard_Real Deflection,
                                                    const Standard_Real U1,
                                                    const Standard_Real U2,
                                                    const Standard_Boolean WithControl)
{
  Initialize (C, Deflection, U1, U2, WithControl);
}


//=======================================================================
//function : GCPnts_UniformDeflection
//purpose  :
//=======================================================================
GCPnts_UniformDeflection::GCPnts_UniformDeflection (TheCurve& C,
                                                    const Standard_Real Deflection,
                                                    const Standard_Boolean WithControl)
{
  Initialize(C, Deflection, WithControl);
}


//=======================================================================
//function : Initialize
//purpose  :
//=======================================================================
void   GCPnts_UniformDeflection::Initialize (TheCurve& C,
                                             const Standard_Real Deflection,
                                             const Standard_Boolean WithControl)
{
  Initialize (C, Deflection, C.FirstParameter(), C.LastParameter(), WithControl);
}


//=======================================================================
//function : Initialize
//purpose  :
//=======================================================================
void GCPnts_UniformDeflection::Initialize (TheCurve& C,
                                           const Standard_Real Deflection,
                                           const Standard_Real theU1,
                                           const Standard_Real theU2,
                                           const Standard_Boolean WithControl)
{
  Standard_Real EPSILON = C.Resolution (Precision::Confusion());
  myDeflection = Deflection;
  myDone = Standard_False;
  myParams.Clear();
  myPoints.Clear();
  Standard_Real U1 = Min (theU1, theU2);
  Standard_Real U2 = Max (theU1, theU2);
  GCPnts_DeflectionType Type = GetDefType (C);
  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, WithControl);
      break;
    case GCPnts_DefComposite:
      myDone = PerformComposite (myParams, myPoints, C, Deflection,
                                 U1, U2, EPSILON, WithControl);
      break;
  }

  // controle des derniers points:
  Controle (C, myParams, myPoints, U2);
}