1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <AppParCurves_MultiBSpCurve.hxx>
16 #include <AppParCurves_MultiPoint.hxx>
17 #include <TColStd_Array1OfReal.hxx>
18 #include <TColStd_Array1OfInteger.hxx>
19 #include <TColStd_HArray1OfInteger.hxx>
21 #include <gp_Pnt2d.hxx>
23 #include <gp_Vec2d.hxx>
24 #include <TColgp_Array1OfPnt.hxx>
25 #include <TColgp_Array1OfPnt2d.hxx>
26 #include <math_Vector.hxx>
27 #include <AppParCurves_ConstraintCouple.hxx>
28 #include <AppParCurves_HArray1OfConstraintCouple.hxx>
31 AppParCurves_BSpFunction::
32 AppParCurves_BSpFunction(const MultiLine& SSP,
33 const Standard_Integer FirstPoint,
34 const Standard_Integer LastPoint,
35 const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
36 const math_Vector& Parameters,
37 const TColStd_Array1OfReal& Knots,
38 const TColStd_Array1OfInteger& Mults,
39 const Standard_Integer NbPol) :
41 MyMultiBSpCurve(NbPol),
42 myParameters(Parameters.Lower(), Parameters.Upper()),
43 ValGrad_F(FirstPoint, LastPoint),
44 MyF(FirstPoint, LastPoint,
45 1, ToolLine::NbP3d(SSP)+ToolLine::NbP2d(SSP), 0.0),
46 PTLX(FirstPoint, LastPoint,
47 1, ToolLine::NbP3d(SSP)+ToolLine::NbP2d(SSP), 0.0),
48 PTLY(FirstPoint, LastPoint,
49 1, ToolLine::NbP3d(SSP)+ToolLine::NbP2d(SSP), 0.0),
50 PTLZ(FirstPoint, LastPoint,
51 1, ToolLine::NbP3d(SSP)+ToolLine::NbP2d(SSP), 0.0),
52 A(FirstPoint, LastPoint, 1, NbPol),
53 DA(FirstPoint, LastPoint, 1, NbPol),
54 MyLeastSquare(SSP, Knots, Mults, FirstPoint, LastPoint,
55 FirstConstraint(TheConstraints, FirstPoint),
56 LastConstraint(TheConstraints, LastPoint), NbPol)
59 for (i = Parameters.Lower(); i <= Parameters.Upper(); i++)
60 myParameters(i) = Parameters(i);
63 myConstraints = TheConstraints;
68 MyMultiBSpCurve.SetKnots(Knots);
69 MyMultiBSpCurve.SetMultiplicities(Mults);
70 Contraintes = Standard_False;
71 Standard_Integer myindex;
72 Standard_Integer low = TheConstraints->Lower(), upp= TheConstraints->Upper();
73 AppParCurves_ConstraintCouple mycouple;
74 AppParCurves_Constraint Cons;
76 for (i = low; i <= upp; i++) {
77 mycouple = TheConstraints->Value(i);
78 Cons = mycouple.Constraint();
79 myindex = mycouple.Index();
80 if (myindex == FirstP) {
81 if (Cons >= 1) Adeb = Adeb+1;
83 else if (myindex == LastP) {
84 if (Cons >= 1) Afin = Afin-1;
87 if (Cons >= 1) Contraintes = Standard_True;
91 Standard_Integer nb3d = ToolLine::NbP3d(SSP);
92 Standard_Integer nb2d = ToolLine::NbP2d(SSP);
93 Standard_Integer mynb3d= nb3d, mynb2d=nb2d;
94 if (nb3d == 0) mynb3d = 1;
95 if (nb2d == 0) mynb2d = 1;
98 tabdim = new TColStd_HArray1OfInteger(0, NbCu-1);
101 for (i = 1; i <= NbCu; i++) {
102 if (i <= nb3d) tabdim->SetValue(i-1, 3);
103 else tabdim->SetValue(i-1, 2);
106 TColgp_Array1OfPnt TabP(1, mynb3d);
107 TColgp_Array1OfPnt2d TabP2d(1, mynb2d);
109 for ( i = FirstP; i <= LastP; i++) {
110 if (nb3d != 0 && nb2d != 0) ToolLine::Value(SSP, i, TabP, TabP2d);
111 else if (nb3d != 0) ToolLine::Value(SSP, i, TabP);
112 else ToolLine::Value(SSP, i, TabP2d);
113 for (Standard_Integer j = 1; j <= NbCu; j++) {
114 if (tabdim->Value(j-1) == 3) {
115 TabP(j).Coord(PTLX(i, j), PTLY(i, j),PTLZ(i, j));
118 TabP2d(j).Coord(PTLX(i, j), PTLY(i, j));
126 AppParCurves_Constraint AppParCurves_BSpFunction::FirstConstraint
127 (const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
128 const Standard_Integer FirstPoint) const
130 Standard_Integer i, myindex;
131 Standard_Integer low = TheConstraints->Lower(), upp= TheConstraints->Upper();
132 AppParCurves_ConstraintCouple mycouple;
133 AppParCurves_Constraint Cons = AppParCurves_NoConstraint;
135 for (i = low; i <= upp; i++) {
136 mycouple = TheConstraints->Value(i);
137 Cons = mycouple.Constraint();
138 myindex = mycouple.Index();
139 if (myindex == FirstPoint) {
147 AppParCurves_Constraint AppParCurves_BSpFunction::LastConstraint
148 (const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
149 const Standard_Integer LastPoint) const
151 Standard_Integer i, myindex;
152 Standard_Integer low = TheConstraints->Lower(), upp= TheConstraints->Upper();
153 AppParCurves_ConstraintCouple mycouple;
154 AppParCurves_Constraint Cons = AppParCurves_NoConstraint;
156 for (i = low; i <= upp; i++) {
157 mycouple = TheConstraints->Value(i);
158 Cons = mycouple.Constraint();
159 myindex = mycouple.Index();
160 if (myindex == LastPoint) {
170 Standard_Boolean AppParCurves_BSpFunction::Value (const math_Vector& X,
175 // Resolution moindres carres:
176 // ===========================
177 MyLeastSquare.Perform(myParameters, mylambda1, mylambda2);
178 if (!(MyLeastSquare.IsDone())) {
179 Done = Standard_False;
180 return Standard_False;
183 MyLeastSquare.Error(FVal, ERR3d, ERR2d);
187 // Resolution avec contraintes:
188 // ============================
191 return Standard_True;
197 void AppParCurves_BSpFunction::Perform(const math_Vector& X) {
201 // Resolution moindres carres:
202 // ===========================
203 MyLeastSquare.Perform(myParameters, mylambda1, mylambda2);
205 if (!(MyLeastSquare.IsDone())) {
206 Done = Standard_False;
210 for(j = myParameters.Lower(); j <= myParameters.Upper(); j++) {
215 MyLeastSquare.ErrorGradient(ValGrad_F, FVal, ERR3d, ERR2d);
223 void AppParCurves_BSpFunction::SetFirstLambda(const Standard_Real l1)
228 void AppParCurves_BSpFunction::SetLastLambda(const Standard_Real l2)
235 Standard_Integer AppParCurves_BSpFunction::NbVariables() const{
240 Standard_Boolean AppParCurves_BSpFunction::Gradient (const math_Vector& X,
246 return Standard_True;
250 Standard_Boolean AppParCurves_BSpFunction::Values (const math_Vector& X,
260 math_Vector mygradient = G;
262 Standard_Real myf = FVal;
263 Standard_Real F2 = FVal;
264 math_Vector G2 = ValGrad_F;
265 for (Standard_Integer i = 1; i <= X.Length(); i++) {
267 myx(i) = X(i) + 1.0e-10;
269 mygradient(i) = (F2 - myf)/(1.0e-10);
272 cout << " Gradient calcule : " << G2 << endl;
273 cout << " Gradient interpole : " << mygradient << endl;
275 return Standard_True;
279 AppParCurves_MultiBSpCurve AppParCurves_BSpFunction::CurveValue() {
280 if (!Contraintes) MyMultiBSpCurve = MyLeastSquare.BSplineValue();
281 return MyMultiBSpCurve;
285 Standard_Real AppParCurves_BSpFunction::Error(const Standard_Integer IPoint,
286 const Standard_Integer CurveIndex) {
287 const math_Matrix& DD = MyLeastSquare.Distance();
288 Standard_Real d = DD.Value(IPoint, CurveIndex);
289 if (!Contraintes) return d;
290 else return Sqrt(MyF(IPoint, CurveIndex));
293 Standard_Real AppParCurves_BSpFunction::MaxError3d() const
298 Standard_Real AppParCurves_BSpFunction::MaxError2d() const
305 const math_Vector& AppParCurves_BSpFunction::NewParameters() const
311 const math_Matrix& AppParCurves_BSpFunction::FunctionMatrix() const
313 return MyLeastSquare.FunctionMatrix();
316 const math_Matrix& AppParCurves_BSpFunction::DerivativeFunctionMatrix() const
318 return MyLeastSquare.DerivativeFunctionMatrix();
322 const math_IntegerVector& AppParCurves_BSpFunction::Index() const
324 return MyLeastSquare.KIndex();