1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
19 #include <AppParCurves_MultiBSpCurve.hxx>
20 #include <AppParCurves_MultiPoint.hxx>
21 #include <TColStd_Array1OfReal.hxx>
22 #include <TColStd_Array1OfInteger.hxx>
23 #include <TColStd_HArray1OfInteger.hxx>
25 #include <gp_Pnt2d.hxx>
27 #include <gp_Vec2d.hxx>
28 #include <TColgp_Array1OfPnt.hxx>
29 #include <TColgp_Array1OfPnt2d.hxx>
30 #include <math_Vector.hxx>
31 #include <AppParCurves_ConstraintCouple.hxx>
32 #include <AppParCurves_HArray1OfConstraintCouple.hxx>
35 AppParCurves_BSpFunction::
36 AppParCurves_BSpFunction(const MultiLine& SSP,
37 const Standard_Integer FirstPoint,
38 const Standard_Integer LastPoint,
39 const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
40 const math_Vector& Parameters,
41 const TColStd_Array1OfReal& Knots,
42 const TColStd_Array1OfInteger& Mults,
43 const Standard_Integer NbPol) :
45 MyMultiBSpCurve(NbPol),
46 myParameters(Parameters.Lower(), Parameters.Upper()),
47 ValGrad_F(FirstPoint, LastPoint),
48 MyF(FirstPoint, LastPoint,
49 1, ToolLine::NbP3d(SSP)+ToolLine::NbP2d(SSP), 0.0),
50 PTLX(FirstPoint, LastPoint,
51 1, ToolLine::NbP3d(SSP)+ToolLine::NbP2d(SSP), 0.0),
52 PTLY(FirstPoint, LastPoint,
53 1, ToolLine::NbP3d(SSP)+ToolLine::NbP2d(SSP), 0.0),
54 PTLZ(FirstPoint, LastPoint,
55 1, ToolLine::NbP3d(SSP)+ToolLine::NbP2d(SSP), 0.0),
56 A(FirstPoint, LastPoint, 1, NbPol),
57 DA(FirstPoint, LastPoint, 1, NbPol),
58 MyLeastSquare(SSP, Knots, Mults, FirstPoint, LastPoint,
59 FirstConstraint(TheConstraints, FirstPoint),
60 LastConstraint(TheConstraints, LastPoint), NbPol)
63 for (i = Parameters.Lower(); i <= Parameters.Upper(); i++)
64 myParameters(i) = Parameters(i);
67 myConstraints = TheConstraints;
72 MyMultiBSpCurve.SetKnots(Knots);
73 MyMultiBSpCurve.SetMultiplicities(Mults);
74 Contraintes = Standard_False;
75 Standard_Integer myindex;
76 Standard_Integer low = TheConstraints->Lower(), upp= TheConstraints->Upper();
77 AppParCurves_ConstraintCouple mycouple;
78 AppParCurves_Constraint Cons;
80 for (i = low; i <= upp; i++) {
81 mycouple = TheConstraints->Value(i);
82 Cons = mycouple.Constraint();
83 myindex = mycouple.Index();
84 if (myindex == FirstP) {
85 if (Cons >= 1) Adeb = Adeb+1;
87 else if (myindex == LastP) {
88 if (Cons >= 1) Afin = Afin-1;
91 if (Cons >= 1) Contraintes = Standard_True;
95 Standard_Integer nb3d = ToolLine::NbP3d(SSP);
96 Standard_Integer nb2d = ToolLine::NbP2d(SSP);
97 Standard_Integer mynb3d= nb3d, mynb2d=nb2d;
98 if (nb3d == 0) mynb3d = 1;
99 if (nb2d == 0) mynb2d = 1;
102 tabdim = new TColStd_HArray1OfInteger(0, NbCu-1);
105 for (i = 1; i <= NbCu; i++) {
106 if (i <= nb3d) tabdim->SetValue(i-1, 3);
107 else tabdim->SetValue(i-1, 2);
110 TColgp_Array1OfPnt TabP(1, mynb3d);
111 TColgp_Array1OfPnt2d TabP2d(1, mynb2d);
113 for ( i = FirstP; i <= LastP; i++) {
114 if (nb3d != 0 && nb2d != 0) ToolLine::Value(SSP, i, TabP, TabP2d);
115 else if (nb3d != 0) ToolLine::Value(SSP, i, TabP);
116 else ToolLine::Value(SSP, i, TabP2d);
117 for (Standard_Integer j = 1; j <= NbCu; j++) {
118 if (tabdim->Value(j-1) == 3) {
119 TabP(j).Coord(PTLX(i, j), PTLY(i, j),PTLZ(i, j));
122 TabP2d(j).Coord(PTLX(i, j), PTLY(i, j));
130 AppParCurves_Constraint AppParCurves_BSpFunction::FirstConstraint
131 (const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
132 const Standard_Integer FirstPoint) const
134 Standard_Integer i, myindex;
135 Standard_Integer low = TheConstraints->Lower(), upp= TheConstraints->Upper();
136 AppParCurves_ConstraintCouple mycouple;
137 AppParCurves_Constraint Cons = AppParCurves_NoConstraint;
139 for (i = low; i <= upp; i++) {
140 mycouple = TheConstraints->Value(i);
141 Cons = mycouple.Constraint();
142 myindex = mycouple.Index();
143 if (myindex == FirstPoint) {
151 AppParCurves_Constraint AppParCurves_BSpFunction::LastConstraint
152 (const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
153 const Standard_Integer LastPoint) const
155 Standard_Integer i, myindex;
156 Standard_Integer low = TheConstraints->Lower(), upp= TheConstraints->Upper();
157 AppParCurves_ConstraintCouple mycouple;
158 AppParCurves_Constraint Cons = AppParCurves_NoConstraint;
160 for (i = low; i <= upp; i++) {
161 mycouple = TheConstraints->Value(i);
162 Cons = mycouple.Constraint();
163 myindex = mycouple.Index();
164 if (myindex == LastPoint) {
174 Standard_Boolean AppParCurves_BSpFunction::Value (const math_Vector& X,
179 // Resolution moindres carres:
180 // ===========================
181 MyLeastSquare.Perform(myParameters, mylambda1, mylambda2);
182 if (!(MyLeastSquare.IsDone())) {
183 Done = Standard_False;
184 return Standard_False;
187 MyLeastSquare.Error(FVal, ERR3d, ERR2d);
191 // Resolution avec contraintes:
192 // ============================
195 return Standard_True;
201 void AppParCurves_BSpFunction::Perform(const math_Vector& X) {
205 // Resolution moindres carres:
206 // ===========================
207 MyLeastSquare.Perform(myParameters, mylambda1, mylambda2);
209 if (!(MyLeastSquare.IsDone())) {
210 Done = Standard_False;
214 for(j = myParameters.Lower(); j <= myParameters.Upper(); j++) {
219 MyLeastSquare.ErrorGradient(ValGrad_F, FVal, ERR3d, ERR2d);
227 void AppParCurves_BSpFunction::SetFirstLambda(const Standard_Real l1)
232 void AppParCurves_BSpFunction::SetLastLambda(const Standard_Real l2)
239 Standard_Integer AppParCurves_BSpFunction::NbVariables() const{
244 Standard_Boolean AppParCurves_BSpFunction::Gradient (const math_Vector& X,
250 return Standard_True;
254 Standard_Boolean AppParCurves_BSpFunction::Values (const math_Vector& X,
264 math_Vector mygradient = G;
266 Standard_Real myf = FVal;
267 Standard_Real F2 = FVal;
268 math_Vector G2 = ValGrad_F;
269 for (Standard_Integer i = 1; i <= X.Length(); i++) {
271 myx(i) = X(i) + 1.0e-10;
273 mygradient(i) = (F2 - myf)/(1.0e-10);
276 cout << " Gradient calcule : " << G2 << endl;
277 cout << " Gradient interpole : " << mygradient << endl;
279 return Standard_True;
283 AppParCurves_MultiBSpCurve AppParCurves_BSpFunction::CurveValue() {
284 if (!Contraintes) MyMultiBSpCurve = MyLeastSquare.BSplineValue();
285 return MyMultiBSpCurve;
289 Standard_Real AppParCurves_BSpFunction::Error(const Standard_Integer IPoint,
290 const Standard_Integer CurveIndex) {
291 const math_Matrix& DD = MyLeastSquare.Distance();
292 Standard_Real d = DD.Value(IPoint, CurveIndex);
293 if (!Contraintes) return d;
294 else return Sqrt(MyF(IPoint, CurveIndex));
297 Standard_Real AppParCurves_BSpFunction::MaxError3d() const
302 Standard_Real AppParCurves_BSpFunction::MaxError2d() const
309 const math_Vector& AppParCurves_BSpFunction::NewParameters() const
315 const math_Matrix& AppParCurves_BSpFunction::FunctionMatrix() const
317 return MyLeastSquare.FunctionMatrix();
320 const math_Matrix& AppParCurves_BSpFunction::DerivativeFunctionMatrix() const
322 return MyLeastSquare.DerivativeFunctionMatrix();
326 const math_IntegerVector& AppParCurves_BSpFunction::Index() const
328 return MyLeastSquare.KIndex();