Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Geom2dAPI / Geom2dAPI_PointsToBSpline.cxx
CommitLineData
7fd59977 1// File: Geom2dAPI_PointsToBSpline.cxx
2// Created: Wed Mar 23 15:28:27 1994
3// Author: Bruno DUMORTIER
4// <dub@fuegox>
5
6#include <Geom2dAPI_PointsToBSpline.ixx>
7
8#include <AppDef_BSplineCompute.hxx>
9#include <AppDef_MultiLine.hxx>
10#include <AppParCurves_MultiBSpCurve.hxx>
11#include <BSplCLib.hxx>
12#include <TColStd_Array1OfInteger.hxx>
13#include <TColStd_Array1OfReal.hxx>
14#include <TColgp_Array1OfPnt2d.hxx>
15#include <math_Vector.hxx>
16#include <AppDef_MultiPointConstraint.hxx>
17#include <AppParCurves_HArray1OfConstraintCouple.hxx>
18#include <AppDef_TheVariational.hxx>
19
20
21//=======================================================================
22//function : Geom2dAPI_PointsToBSpline
23//purpose :
24//=======================================================================
25
26Geom2dAPI_PointsToBSpline::Geom2dAPI_PointsToBSpline()
27{
28 myIsDone = Standard_False;
29}
30
31
32//=======================================================================
33//function : Geom2dAPI_PointsToBSpline
34//purpose :
35//=======================================================================
36
37Geom2dAPI_PointsToBSpline::Geom2dAPI_PointsToBSpline
38 (const TColgp_Array1OfPnt2d& Points,
39 const Standard_Integer DegMin,
40 const Standard_Integer DegMax,
41 const GeomAbs_Shape Continuity,
42 const Standard_Real Tol2D)
43{
44 Init(Points,DegMin,DegMax,Continuity,Tol2D);
45}
46
47
48//=======================================================================
49//function : Geom2dAPI_PointsToBSpline
50//purpose :
51//=======================================================================
52
53Geom2dAPI_PointsToBSpline::Geom2dAPI_PointsToBSpline
54 (const TColStd_Array1OfReal& YValues,
55 const Standard_Real X0,
56 const Standard_Real DX,
57 const Standard_Integer DegMin,
58 const Standard_Integer DegMax,
59 const GeomAbs_Shape Continuity,
60 const Standard_Real Tol2D)
61{
62 Init(YValues,X0,DX,DegMin,DegMax,Continuity,Tol2D);
63}
64
65//=======================================================================
66//function : Geom2dAPI_PointsToBSpline
67//purpose :
68//=======================================================================
69
70Geom2dAPI_PointsToBSpline::Geom2dAPI_PointsToBSpline
71 (const TColgp_Array1OfPnt2d& Points,
72 const Approx_ParametrizationType ParType,
73 const Standard_Integer DegMin,
74 const Standard_Integer DegMax,
75 const GeomAbs_Shape Continuity,
76 const Standard_Real Tol2D)
77{
78 myIsDone = Standard_False;
79 Init(Points,ParType,DegMin,DegMax,Continuity,Tol2D);
80}
81
82//=======================================================================
83//function : Geom2dAPI_PointsToBSpline
84//purpose :
85//=======================================================================
86
87Geom2dAPI_PointsToBSpline::Geom2dAPI_PointsToBSpline
88 (const TColgp_Array1OfPnt2d& Points,
89 const TColStd_Array1OfReal& Params,
90 const Standard_Integer DegMin,
91 const Standard_Integer DegMax,
92 const GeomAbs_Shape Continuity,
93 const Standard_Real Tol2D)
94{
95 myIsDone = Standard_False;
96 Init(Points,Params,DegMin,DegMax,Continuity,Tol2D);
97}
98
99
100//=======================================================================
101//function : Geom2dAPI_PointsToBSpline
102//purpose :
103//=======================================================================
104
105Geom2dAPI_PointsToBSpline::Geom2dAPI_PointsToBSpline
106 (const TColgp_Array1OfPnt2d& Points,
107 const Standard_Real W1,
108 const Standard_Real W2,
109 const Standard_Real W3,
110 const Standard_Integer DegMax,
111 const GeomAbs_Shape Continuity,
112 const Standard_Real Tol2D)
113{
114 myIsDone = Standard_False;
115 Init(Points,W1,W2,W3,DegMax,Continuity,Tol2D);
116}
117
118
119//=======================================================================
120//function : Init
121//purpose :
122//=======================================================================
123
124void Geom2dAPI_PointsToBSpline::Init
125 (const TColgp_Array1OfPnt2d& Points,
126 const Approx_ParametrizationType ParType,
127 const Standard_Integer DegMin,
128 const Standard_Integer DegMax,
129 const GeomAbs_Shape Continuity,
130 const Standard_Real Tol2D)
131{
132 Standard_Real Tol3D = 0.; // dummy argument for BSplineCompute.
133
134
135 Standard_Integer nbit = 2;
136 Standard_Boolean UseSquares = Standard_False;
137 if(Tol2D <= 1.e-3) UseSquares = Standard_True;
138
139 AppDef_BSplineCompute TheComputer
140 (DegMin,DegMax,Tol3D,Tol2D,nbit,Standard_True,ParType,UseSquares);
141
142 switch( Continuity) {
143 case GeomAbs_C0:
144 TheComputer.SetContinuity(0); break;
145
146 case GeomAbs_G1:
147 case GeomAbs_C1:
148 TheComputer.SetContinuity(1); break;
149
150 case GeomAbs_G2:
151 case GeomAbs_C2:
152 TheComputer.SetContinuity(2); break;
153
154 default:
155 TheComputer.SetContinuity(3);
156 }
157
158 TheComputer.Perform(Points);
159
160 AppParCurves_MultiBSpCurve TheCurve = TheComputer.Value();
161
162 TColgp_Array1OfPnt2d Poles(1,TheCurve.NbPoles());
163
164 TheCurve.Curve(1, Poles);
165
166 myCurve = new Geom2d_BSplineCurve(Poles,
167 TheCurve.Knots(),
168 TheCurve.Multiplicities(),
169 TheCurve.Degree());
170 myIsDone = Standard_True;
171}
172
173//=======================================================================
174//function : Init
175//purpose :
176//=======================================================================
177
178void Geom2dAPI_PointsToBSpline::Init
179 (const TColStd_Array1OfReal& YValues,
180 const Standard_Real X0,
181 const Standard_Real DX,
182 const Standard_Integer DegMin,
183 const Standard_Integer DegMax,
184 const GeomAbs_Shape Continuity,
185 const Standard_Real Tol2D)
186{
187 // first approximate the Y values (with dummy 0 as X values)
188
189 Standard_Real Tol3D = 0.; // dummy argument for BSplineCompute.
190 TColgp_Array1OfPnt2d Points(YValues.Lower(),YValues.Upper());
191 math_Vector Param(YValues.Lower(),YValues.Upper());
192 Standard_Real length = DX * (YValues.Upper() - YValues.Lower());
193 Standard_Integer i;
194
195 for (i = YValues.Lower(); i <= YValues.Upper(); i++) {
196 Param(i) = (X0+(i-1)*DX)/(X0+length);
197 Points(i).SetCoord(0.0, YValues(i));
198 }
199
200 AppDef_BSplineCompute TheComputer
201 (Param, DegMin,DegMax,Tol3D,Tol2D,0, Standard_True, Standard_True);
202
203 switch( Continuity) {
204 case GeomAbs_C0:
205 TheComputer.SetContinuity(0); break;
206
207 case GeomAbs_G1:
208 case GeomAbs_C1:
209 TheComputer.SetContinuity(1); break;
210
211 case GeomAbs_G2:
212 case GeomAbs_C2:
213 TheComputer.SetContinuity(2); break;
214
215 default:
216 TheComputer.SetContinuity(3);
217 }
218
219 TheComputer.Perform(Points);
220
221 const AppParCurves_MultiBSpCurve& TheCurve = TheComputer.Value();
222
223 Standard_Integer Degree = TheCurve.Degree();
224 TColgp_Array1OfPnt2d Poles(1,TheCurve.NbPoles());
225 Standard_Integer nk = TheCurve.Knots().Length();
226 TColStd_Array1OfReal Knots(1,nk);
227 TColStd_Array1OfInteger Mults(1,nk);
228
229 TheCurve.Curve(1, Poles);
230
231
232
233 // compute X values for the poles
234 TColStd_Array1OfReal XPoles(1,Poles.Upper());
235
236 // start with a line
237 TColStd_Array1OfReal TempPoles(1,2);
238 TColStd_Array1OfReal TempKnots(1,2);
239 TColStd_Array1OfInteger TempMults(1,2);
240 TempMults.Init(2);
241 TempPoles(1) = X0;
242 TempPoles(2) = X0 + length;
243 TempKnots(1) = 0.;
244 TempKnots(2) = 1.;
245
246 // increase the Degree
247 TColStd_Array1OfReal NewTempPoles(1,Degree+1);
248 TColStd_Array1OfReal NewTempKnots(1,2);
249 TColStd_Array1OfInteger NewTempMults(1,2);
250 BSplCLib::IncreaseDegree(1,Degree,Standard_False,1,
251 TempPoles,TempKnots,TempMults,
252 NewTempPoles,NewTempKnots,NewTempMults);
253
254
255 // insert the Knots
256 BSplCLib::InsertKnots(Degree,Standard_False,1,
257 NewTempPoles,NewTempKnots,NewTempMults,
258 TheCurve.Knots(),TheCurve.Multiplicities(),
259 XPoles,Knots,Mults,
260 Epsilon(1));
261
262 // scale the knots
263 for (i = 1; i <= nk; i++) {
264 Knots(i) = X0 + length * Knots(i);
265 }
266
267 // set the Poles
268 for (i = 1; i <= Poles.Upper(); i++) {
269 Poles(i).SetX(XPoles(i));
270 }
271
272
273
274 myCurve = new Geom2d_BSplineCurve(Poles, Knots, Mults, Degree);
275 myIsDone = Standard_True;
276}
277
278//=======================================================================
279//function : Init
280//purpose :
281//=======================================================================
282
283void Geom2dAPI_PointsToBSpline::Init
284 (const TColgp_Array1OfPnt2d& Points,
285 const Standard_Integer DegMin,
286 const Standard_Integer DegMax,
287 const GeomAbs_Shape Continuity,
288 const Standard_Real Tol2D)
289{
290 myIsDone = Standard_False;
291 Init(Points,Approx_ChordLength,DegMin,DegMax,Continuity,Tol2D);
292}
293
294
295//=======================================================================
296//function : Init
297//purpose :
298//=======================================================================
299
300void Geom2dAPI_PointsToBSpline::Init
301 (const TColgp_Array1OfPnt2d& Points,
302 const TColStd_Array1OfReal& Params,
303 const Standard_Integer DegMin,
304 const Standard_Integer DegMax,
305 const GeomAbs_Shape Continuity,
306 const Standard_Real Tol2D)
307{
308 if (Params.Length() != Points.Length()) Standard_OutOfRange::Raise("");
309
310 Standard_Real Tol3D = 0.; // dummy argument for BSplineCompute.
311 Standard_Integer Nbp = Params.Length();
312 math_Vector theParams(1,Nbp);
313 theParams(1) = 0.;
314 theParams(Nbp) = 1.;
315
316 Standard_Real Uf = Params(Params.Lower());
317 Standard_Real Ul = Params(Params.Upper()) - Uf;
318 for (Standard_Integer i=2; i<Nbp; i++) {
319 theParams(i) = (Params(i)-Uf)/Ul;
320 }
321
322 AppDef_BSplineCompute TheComputer
323 (DegMin,DegMax,Tol3D,Tol2D,0,
324 Standard_True,Approx_IsoParametric,Standard_True);
325
326 TheComputer.SetParameters(theParams);
327
328 switch( Continuity) {
329 case GeomAbs_C0:
330 TheComputer.SetContinuity(0); break;
331
332 case GeomAbs_G1:
333 case GeomAbs_C1:
334 TheComputer.SetContinuity(1); break;
335
336 case GeomAbs_G2:
337 case GeomAbs_C2:
338 TheComputer.SetContinuity(2); break;
339
340 default:
341 TheComputer.SetContinuity(3);
342 }
343
344 TheComputer.Perform(Points);
345
346 AppParCurves_MultiBSpCurve TheCurve = TheComputer.Value();
347
348 TColgp_Array1OfPnt2d Poles(1,TheCurve.NbPoles());
349
350 TheCurve.Curve(1, Poles);
351
352 myCurve = new Geom2d_BSplineCurve(Poles,
353 TheCurve.Knots(),
354 TheCurve.Multiplicities(),
355 TheCurve.Degree());
356 myIsDone = Standard_True;
357
358}
359
360
361//=======================================================================
362//function : Init
363//purpose :
364//=======================================================================
365
366void Geom2dAPI_PointsToBSpline::Init
367 (const TColgp_Array1OfPnt2d& Points,
368 const Standard_Real W1,
369 const Standard_Real W2,
370 const Standard_Real W3,
371 const Standard_Integer DegMax,
372 const GeomAbs_Shape Continuity,
373 const Standard_Real Tol2D)
374{
375 Standard_Integer NbPoint = Points.Length(), i;
376
377
378 Standard_Integer nbit = 2;
379 if(Tol2D <= 1.e-3) nbit = 0;
380
381 //Variational algo
382
383 AppDef_MultiLine multL(NbPoint);
384 for(i = 1; i <= NbPoint; ++i) {
385 AppDef_MultiPointConstraint mpc(0, 1);
386 mpc.SetPoint2d(1, Points.Value(Points.Lower() + i - 1));
387 multL.SetValue(i, mpc);
388 }
389
390 Handle(AppParCurves_HArray1OfConstraintCouple) TABofCC =
391 new AppParCurves_HArray1OfConstraintCouple(1, NbPoint);
392 AppParCurves_Constraint Constraint=AppParCurves_NoConstraint;
393
394 for(i = 1; i <= NbPoint; ++i) {
395 AppParCurves_ConstraintCouple ACC(i,Constraint);
396 TABofCC->SetValue(i,ACC);
397 }
398
399
400 AppDef_TheVariational Variation(multL, 1, NbPoint, TABofCC);
401
402//===================================
403 Standard_Integer theMaxSegments = 1000;
404 Standard_Boolean theWithMinMax = Standard_False;
405//===================================
406
407 Variation.SetMaxDegree(DegMax);
408 Variation.SetContinuity(Continuity);
409 Variation.SetMaxSegment(theMaxSegments);
410
411 Variation.SetTolerance(Tol2D);
412 Variation.SetWithMinMax(theWithMinMax);
413 Variation.SetNbIterations(nbit);
414
415 Variation.SetCriteriumWeight(W1, W2, W3);
416
417 if(!Variation.IsCreated()) {
418 return;
419 }
420
421 if(Variation.IsOverConstrained()) {
422 return;
423 }
424
425 try {
426 Variation.Approximate();
427 }
428 catch (Standard_Failure) {
429 return;
430 }
431
432 if(!Variation.IsDone()) {
433 return;
434 }
435
436 AppParCurves_MultiBSpCurve TheCurve = Variation.Value();
437
438 TColgp_Array1OfPnt2d Poles(1,TheCurve.NbPoles());
439
440 TheCurve.Curve(1, Poles);
441
442 myCurve = new Geom2d_BSplineCurve(Poles,
443 TheCurve.Knots(),
444 TheCurve.Multiplicities(),
445 TheCurve.Degree());
446
447 myIsDone = Standard_True;
448
449}
450
451//=======================================================================
452//function : Handle_Geom2d_BSplineCurve&
453//purpose :
454//=======================================================================
455
456const Handle(Geom2d_BSplineCurve)& Geom2dAPI_PointsToBSpline::Curve() const
457{
458 if ( !myIsDone)
459 StdFail_NotDone::Raise(" ");
460 return myCurve;
461}
462
463
464
465//=======================================================================
466//function : Geom2d_BSplineCurve
467//purpose :
468//=======================================================================
469
470Geom2dAPI_PointsToBSpline::operator Handle(Geom2d_BSplineCurve)() const
471{
472 return myCurve;
473}
474
475//=======================================================================
476//function : Geom2d_BSplineCurve
477//purpose :
478//=======================================================================
479
480Standard_Boolean Geom2dAPI_PointsToBSpline::IsDone() const
481{
482 return myIsDone;
483}
484
485