1 // Created on: 1994-03-21
2 // Created by: Bruno DUMORTIER
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 #include <GeomAPI_PointsToBSpline.ixx>
19 #include <AppDef_BSplineCompute.hxx>
20 #include <AppDef_MultiLine.hxx>
21 #include <AppParCurves_MultiBSpCurve.hxx>
22 #include <math_Vector.hxx>
23 #include <BSplCLib.hxx>
24 #include <AppDef_MultiLine.hxx>
25 #include <AppDef_MultiPointConstraint.hxx>
26 #include <AppParCurves_HArray1OfConstraintCouple.hxx>
27 #include <AppParCurves_Constraint.hxx>
28 #include <AppDef_Variational.hxx>
31 //=======================================================================
32 //function : GeomAPI_PointsToBSpline
34 //=======================================================================
36 GeomAPI_PointsToBSpline::GeomAPI_PointsToBSpline()
38 myIsDone = Standard_False;
42 //=======================================================================
43 //function : GeomAPI_PointsToBSpline
45 //=======================================================================
47 GeomAPI_PointsToBSpline::GeomAPI_PointsToBSpline
48 (const TColgp_Array1OfPnt& Points,
49 const Standard_Integer DegMin,
50 const Standard_Integer DegMax,
51 const GeomAbs_Shape Continuity,
52 const Standard_Real Tol3D)
54 myIsDone = Standard_False;
55 Init(Points,DegMin,DegMax,Continuity,Tol3D);
58 //=======================================================================
59 //function : GeomAPI_PointsToBSpline
61 //=======================================================================
63 GeomAPI_PointsToBSpline::GeomAPI_PointsToBSpline
64 (const TColgp_Array1OfPnt& Points,
65 const Approx_ParametrizationType ParType,
66 const Standard_Integer DegMin,
67 const Standard_Integer DegMax,
68 const GeomAbs_Shape Continuity,
69 const Standard_Real Tol3D)
71 myIsDone = Standard_False;
72 Init(Points,ParType,DegMin,DegMax,Continuity,Tol3D);
76 //=======================================================================
77 //function : GeomAPI_PointsToBSpline
79 //=======================================================================
81 GeomAPI_PointsToBSpline::GeomAPI_PointsToBSpline
82 (const TColgp_Array1OfPnt& Points,
83 const TColStd_Array1OfReal& Params,
84 const Standard_Integer DegMin,
85 const Standard_Integer DegMax,
86 const GeomAbs_Shape Continuity,
87 const Standard_Real Tol3D)
89 myIsDone = Standard_False;
90 Init(Points,Params,DegMin,DegMax,Continuity,Tol3D);
93 //=======================================================================
94 //function : GeomAPI_PointsToBSpline
96 //=======================================================================
98 GeomAPI_PointsToBSpline::GeomAPI_PointsToBSpline
99 (const TColgp_Array1OfPnt& Points,
100 const Standard_Real W1,
101 const Standard_Real W2,
102 const Standard_Real W3,
103 const Standard_Integer DegMax,
104 const GeomAbs_Shape Continuity,
105 const Standard_Real Tol3D)
107 myIsDone = Standard_False;
108 Init(Points,W1,W2,W3,DegMax,Continuity,Tol3D);
111 //=======================================================================
114 //=======================================================================
116 void GeomAPI_PointsToBSpline::Init
117 (const TColgp_Array1OfPnt& Points,
118 const Standard_Integer DegMin,
119 const Standard_Integer DegMax,
120 const GeomAbs_Shape Continuity,
121 const Standard_Real Tol3D)
123 myIsDone = Standard_False;
124 Init(Points,Approx_ChordLength,DegMin,DegMax,Continuity,Tol3D);
126 //=======================================================================
129 //=======================================================================
131 void GeomAPI_PointsToBSpline::Init
132 (const TColgp_Array1OfPnt& Points,
133 const Approx_ParametrizationType ParType,
134 const Standard_Integer DegMin,
135 const Standard_Integer DegMax,
136 const GeomAbs_Shape Continuity,
137 const Standard_Real Tol3D)
139 Standard_Real Tol2D = 0.; // dummy argument for BSplineCompute.
141 Standard_Integer nbit = 2;
142 Standard_Boolean UseSquares = Standard_False;
143 if(Tol3D <= 1.e-3) UseSquares = Standard_True;
146 AppDef_BSplineCompute TheComputer
147 (DegMin,DegMax,Tol3D,Tol2D,nbit,Standard_True,ParType,UseSquares);
149 switch( Continuity) {
151 TheComputer.SetContinuity(0); break;
155 TheComputer.SetContinuity(1); break;
159 TheComputer.SetContinuity(2); break;
162 TheComputer.SetContinuity(3);
165 TheComputer.Perform(Points);
167 AppParCurves_MultiBSpCurve TheCurve = TheComputer.Value();
169 TColgp_Array1OfPnt Poles(1,TheCurve.NbPoles());
171 TheCurve.Curve(1, Poles);
173 myCurve = new Geom_BSplineCurve(Poles,
175 TheCurve.Multiplicities(),
177 myIsDone = Standard_True;
181 //=======================================================================
184 //=======================================================================
186 void GeomAPI_PointsToBSpline::Init
187 (const TColgp_Array1OfPnt& Points,
188 const TColStd_Array1OfReal& Params,
189 const Standard_Integer DegMin,
190 const Standard_Integer DegMax,
191 const GeomAbs_Shape Continuity,
192 const Standard_Real Tol3D)
194 if (Params.Length() != Points.Length()) Standard_OutOfRange::Raise("");
196 Standard_Real Tol2D = 0.; // dummy argument for BSplineCompute.
197 Standard_Integer Nbp = Params.Length();
198 math_Vector theParams(1,Nbp);
202 Standard_Real Uf = Params(Params.Lower());
203 Standard_Real Ul = Params(Params.Upper()) - Uf;
204 for (Standard_Integer i=2; i<Nbp; i++) {
205 theParams(i) = (Params(i)-Uf)/Ul;
208 AppDef_BSplineCompute TheComputer
209 (DegMin,DegMax,Tol3D,Tol2D,0,
210 Standard_True,Approx_IsoParametric,Standard_True);
212 TheComputer.SetParameters(theParams);
214 switch( Continuity) {
216 TheComputer.SetContinuity(0); break;
220 TheComputer.SetContinuity(1); break;
224 TheComputer.SetContinuity(2); break;
227 TheComputer.SetContinuity(3);
230 TheComputer.Perform(Points);
232 AppParCurves_MultiBSpCurve TheCurve = TheComputer.Value();
234 TColgp_Array1OfPnt Poles(1,TheCurve.NbPoles());
235 TColStd_Array1OfReal Knots(TheCurve.Knots().Lower(),
236 TheCurve.Knots().Upper());
238 TheCurve.Curve(1, Poles);
239 Knots = TheCurve.Knots();
240 BSplCLib::Reparametrize(Params(Params.Lower()),
241 Params(Params.Upper()),
244 myCurve = new Geom_BSplineCurve(Poles,
246 TheCurve.Multiplicities(),
248 myIsDone = Standard_True;
251 //=======================================================================
254 //=======================================================================
256 void GeomAPI_PointsToBSpline::Init
257 (const TColgp_Array1OfPnt& Points,
258 const Standard_Real W1,
259 const Standard_Real W2,
260 const Standard_Real W3,
261 const Standard_Integer DegMax,
262 const GeomAbs_Shape Continuity,
263 const Standard_Real Tol3D)
265 Standard_Integer NbPoint = Points.Length(), i;
268 Standard_Integer nbit = 2;
269 if(Tol3D <= 1.e-3) nbit = 0;
273 AppDef_MultiLine multL(NbPoint);
274 for(i = 1; i <= NbPoint; ++i) {
275 AppDef_MultiPointConstraint mpc(1, 0);
276 mpc.SetPoint(1, Points.Value(Points.Lower() + i - 1));
277 multL.SetValue(i, mpc);
280 Handle(AppParCurves_HArray1OfConstraintCouple) TABofCC =
281 new AppParCurves_HArray1OfConstraintCouple(1, NbPoint);
282 AppParCurves_Constraint Constraint=AppParCurves_NoConstraint;
284 for(i = 1; i <= NbPoint; ++i) {
285 AppParCurves_ConstraintCouple ACC(i,Constraint);
286 TABofCC->SetValue(i,ACC);
290 AppDef_Variational Variation(multL, 1, NbPoint, TABofCC);
292 //===================================
293 Standard_Integer theMaxSegments = 1000;
294 Standard_Boolean theWithMinMax = Standard_False;
295 //===================================
297 Variation.SetMaxDegree(DegMax);
298 Variation.SetContinuity(Continuity);
299 Variation.SetMaxSegment(theMaxSegments);
301 Variation.SetTolerance(Tol3D);
302 Variation.SetWithMinMax(theWithMinMax);
303 Variation.SetNbIterations(nbit);
305 Variation.SetCriteriumWeight(W1, W2, W3);
307 if(!Variation.IsCreated()) {
311 if(Variation.IsOverConstrained()) {
316 Variation.Approximate();
318 catch (Standard_Failure) {
322 if(!Variation.IsDone()) {
326 AppParCurves_MultiBSpCurve TheCurve = Variation.Value();
328 TColgp_Array1OfPnt Poles(1,TheCurve.NbPoles());
330 TheCurve.Curve(1, Poles);
332 myCurve = new Geom_BSplineCurve(Poles,
334 TheCurve.Multiplicities(),
336 myIsDone = Standard_True;
341 //=======================================================================
342 //function : Handle(Geom_BSplineCurve)&
344 //=======================================================================
346 const Handle(Geom_BSplineCurve)& GeomAPI_PointsToBSpline::Curve() const
349 StdFail_NotDone::Raise("GeomAPI_PointsToBSpline::Curve ");
355 //=======================================================================
356 //function : Geom_BSplineCurve
358 //=======================================================================
360 GeomAPI_PointsToBSpline::operator Handle(Geom_BSplineCurve)() const
366 //=======================================================================
369 //=======================================================================
371 Standard_Boolean GeomAPI_PointsToBSpline::IsDone() const