0023024: Update headers of OCCT files
[occt.git] / src / AppParCurves / AppParCurves_BSpFunction.gxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 //
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.
8 //
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.
11 //
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.
18
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>
24 #include <gp_Pnt.hxx>
25 #include <gp_Pnt2d.hxx>
26 #include <gp_Vec.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>
33
34
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) :
44          MyMultiLine(SSP),
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)
61 {
62   Standard_Integer i; 
63   for (i = Parameters.Lower(); i <= Parameters.Upper(); i++)
64     myParameters(i) = Parameters(i);
65   FirstP = FirstPoint;
66   LastP = LastPoint;
67   myConstraints = TheConstraints;
68   NbP = LastP-FirstP+1;
69   Adeb = FirstP;
70   Afin = LastP;
71   nbpoles = NbPol;
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;
79
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;
86     }
87     else if (myindex == LastP) {
88       if (Cons >= 1) Afin = Afin-1;
89     }
90     else {
91       if (Cons >= 1) Contraintes = Standard_True;
92     }
93   }
94
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;
100
101   NbCu = nb3d+nb2d;
102   tabdim = new TColStd_HArray1OfInteger(0, NbCu-1);
103
104   if (Contraintes) {
105     for (i = 1; i <= NbCu; i++) {
106       if (i <= nb3d) tabdim->SetValue(i-1, 3);
107       else tabdim->SetValue(i-1, 2);
108     }
109
110     TColgp_Array1OfPnt TabP(1, mynb3d);
111     TColgp_Array1OfPnt2d TabP2d(1, mynb2d);
112     
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));
120         }
121         else {
122           TabP2d(j).Coord(PTLX(i, j), PTLY(i, j));
123         }
124       }
125     }
126   }
127 }
128
129
130 AppParCurves_Constraint AppParCurves_BSpFunction::FirstConstraint
131   (const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
132    const Standard_Integer FirstPoint) const
133 {
134   Standard_Integer i, myindex;
135   Standard_Integer low = TheConstraints->Lower(), upp= TheConstraints->Upper();
136   AppParCurves_ConstraintCouple mycouple;
137   AppParCurves_Constraint Cons = AppParCurves_NoConstraint;
138
139   for (i = low; i <= upp; i++) {
140     mycouple = TheConstraints->Value(i);
141     Cons = mycouple.Constraint();
142     myindex = mycouple.Index();
143     if (myindex == FirstPoint) {
144       break;
145     }
146   }
147   return Cons;
148 }
149
150
151 AppParCurves_Constraint AppParCurves_BSpFunction::LastConstraint
152   (const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
153    const Standard_Integer LastPoint) const
154 {
155   Standard_Integer i, myindex;
156   Standard_Integer low = TheConstraints->Lower(), upp= TheConstraints->Upper();
157   AppParCurves_ConstraintCouple mycouple;
158   AppParCurves_Constraint Cons = AppParCurves_NoConstraint;
159
160   for (i = low; i <= upp; i++) {
161     mycouple = TheConstraints->Value(i);
162     Cons = mycouple.Constraint();
163     myindex = mycouple.Index();
164     if (myindex == LastPoint) {
165       break;
166     }
167   }
168   return Cons;
169 }
170
171
172
173
174 Standard_Boolean AppParCurves_BSpFunction::Value (const math_Vector& X, 
175                                                   Standard_Real& F) {
176
177   myParameters = X;
178
179   // Resolution moindres carres:
180   // ===========================
181   MyLeastSquare.Perform(myParameters, mylambda1, mylambda2);
182   if (!(MyLeastSquare.IsDone())) { 
183     Done = Standard_False;
184     return Standard_False;
185   }
186   if (!Contraintes) {
187     MyLeastSquare.Error(FVal, ERR3d, ERR2d);
188     F = FVal;
189   }
190
191   // Resolution avec contraintes:
192   // ============================
193   else { 
194   }  
195   return Standard_True;
196 }
197
198
199
200
201 void AppParCurves_BSpFunction::Perform(const math_Vector& X) {
202   Standard_Integer j;
203
204   myParameters = X;
205   // Resolution moindres carres:
206   // ===========================
207   MyLeastSquare.Perform(myParameters, mylambda1, mylambda2);
208
209   if (!(MyLeastSquare.IsDone())) { 
210     Done = Standard_False;
211     return;
212   }
213
214   for(j = myParameters.Lower(); j <= myParameters.Upper(); j++) {
215     ValGrad_F(j) = 0.0;
216   }
217
218   if (!Contraintes) {
219     MyLeastSquare.ErrorGradient(ValGrad_F, FVal, ERR3d, ERR2d);
220   }
221   else {
222   }
223 }
224
225
226
227 void AppParCurves_BSpFunction::SetFirstLambda(const Standard_Real l1)
228 {
229   mylambda1 = l1;
230 }
231
232 void AppParCurves_BSpFunction::SetLastLambda(const Standard_Real l2)
233 {
234   mylambda2 = l2;
235 }
236
237
238
239 Standard_Integer AppParCurves_BSpFunction::NbVariables() const{ 
240   return NbP;
241 }
242
243
244 Standard_Boolean AppParCurves_BSpFunction::Gradient (const math_Vector& X,
245                                                      math_Vector& G) {
246
247   Perform(X);
248   G = ValGrad_F;
249
250   return Standard_True;
251 }
252
253
254 Standard_Boolean AppParCurves_BSpFunction::Values (const math_Vector& X, 
255                                                    Standard_Real& F, 
256                                                    math_Vector& G) {
257
258
259   Perform(X);
260   F = FVal;
261   G = ValGrad_F;
262
263 /*
264   math_Vector mygradient = G;
265   math_Vector myx = X;
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++) {
270     myx = X;
271     myx(i) = X(i) + 1.0e-10;
272     Value(myx, F2);
273     mygradient(i) = (F2 - myf)/(1.0e-10);
274   }
275
276 cout << " Gradient calcule : " << G2 << endl;
277 cout << " Gradient interpole : " <<  mygradient << endl;
278 */
279   return Standard_True;
280 }
281
282
283 AppParCurves_MultiBSpCurve AppParCurves_BSpFunction::CurveValue() {
284   if (!Contraintes)  MyMultiBSpCurve = MyLeastSquare.BSplineValue();
285   return MyMultiBSpCurve;
286 }
287
288
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));
295 }
296
297 Standard_Real AppParCurves_BSpFunction::MaxError3d() const
298 {
299   return ERR3d;
300 }
301
302 Standard_Real AppParCurves_BSpFunction::MaxError2d() const
303 {
304   return ERR2d;
305 }
306
307
308
309 const math_Vector& AppParCurves_BSpFunction::NewParameters() const
310 {
311   return myParameters;
312 }
313
314
315 const math_Matrix& AppParCurves_BSpFunction::FunctionMatrix() const
316 {
317   return MyLeastSquare.FunctionMatrix();
318 }
319
320 const math_Matrix& AppParCurves_BSpFunction::DerivativeFunctionMatrix() const
321 {
322   return MyLeastSquare.DerivativeFunctionMatrix();
323 }
324
325
326 const math_IntegerVector& AppParCurves_BSpFunction::Index() const
327 {
328   return MyLeastSquare.KIndex();
329 }