0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[occt.git] / src / AppParCurves / AppParCurves_BSpFunction.gxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
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>
20 #include <gp_Pnt.hxx>
21 #include <gp_Pnt2d.hxx>
22 #include <gp_Vec.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>
29
30
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) :
40          MyMultiLine(SSP),
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)
57 {
58   Standard_Integer i; 
59   for (i = Parameters.Lower(); i <= Parameters.Upper(); i++)
60     myParameters(i) = Parameters(i);
61   FirstP = FirstPoint;
62   LastP = LastPoint;
63   myConstraints = TheConstraints;
64   NbP = LastP-FirstP+1;
65   Adeb = FirstP;
66   Afin = LastP;
67   nbpoles = NbPol;
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;
75
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;
82     }
83     else if (myindex == LastP) {
84       if (Cons >= 1) Afin = Afin-1;
85     }
86     else {
87       if (Cons >= 1) Contraintes = Standard_True;
88     }
89   }
90
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;
96
97   NbCu = nb3d+nb2d;
98   tabdim = new TColStd_HArray1OfInteger(0, NbCu-1);
99
100   if (Contraintes) {
101     for (i = 1; i <= NbCu; i++) {
102       if (i <= nb3d) tabdim->SetValue(i-1, 3);
103       else tabdim->SetValue(i-1, 2);
104     }
105
106     TColgp_Array1OfPnt TabP(1, mynb3d);
107     TColgp_Array1OfPnt2d TabP2d(1, mynb2d);
108     
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));
116         }
117         else {
118           TabP2d(j).Coord(PTLX(i, j), PTLY(i, j));
119         }
120       }
121     }
122   }
123 }
124
125
126 AppParCurves_Constraint AppParCurves_BSpFunction::FirstConstraint
127   (const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
128    const Standard_Integer FirstPoint) const
129 {
130   Standard_Integer i, myindex;
131   Standard_Integer low = TheConstraints->Lower(), upp= TheConstraints->Upper();
132   AppParCurves_ConstraintCouple mycouple;
133   AppParCurves_Constraint Cons = AppParCurves_NoConstraint;
134
135   for (i = low; i <= upp; i++) {
136     mycouple = TheConstraints->Value(i);
137     Cons = mycouple.Constraint();
138     myindex = mycouple.Index();
139     if (myindex == FirstPoint) {
140       break;
141     }
142   }
143   return Cons;
144 }
145
146
147 AppParCurves_Constraint AppParCurves_BSpFunction::LastConstraint
148   (const Handle(AppParCurves_HArray1OfConstraintCouple)& TheConstraints,
149    const Standard_Integer LastPoint) const
150 {
151   Standard_Integer i, myindex;
152   Standard_Integer low = TheConstraints->Lower(), upp= TheConstraints->Upper();
153   AppParCurves_ConstraintCouple mycouple;
154   AppParCurves_Constraint Cons = AppParCurves_NoConstraint;
155
156   for (i = low; i <= upp; i++) {
157     mycouple = TheConstraints->Value(i);
158     Cons = mycouple.Constraint();
159     myindex = mycouple.Index();
160     if (myindex == LastPoint) {
161       break;
162     }
163   }
164   return Cons;
165 }
166
167
168
169
170 Standard_Boolean AppParCurves_BSpFunction::Value (const math_Vector& X, 
171                                                   Standard_Real& F) {
172
173   myParameters = X;
174
175   // Resolution moindres carres:
176   // ===========================
177   MyLeastSquare.Perform(myParameters, mylambda1, mylambda2);
178   if (!(MyLeastSquare.IsDone())) { 
179     Done = Standard_False;
180     return Standard_False;
181   }
182   if (!Contraintes) {
183     MyLeastSquare.Error(FVal, ERR3d, ERR2d);
184     F = FVal;
185   }
186
187   // Resolution avec contraintes:
188   // ============================
189   else { 
190   }  
191   return Standard_True;
192 }
193
194
195
196
197 void AppParCurves_BSpFunction::Perform(const math_Vector& X) {
198   Standard_Integer j;
199
200   myParameters = X;
201   // Resolution moindres carres:
202   // ===========================
203   MyLeastSquare.Perform(myParameters, mylambda1, mylambda2);
204
205   if (!(MyLeastSquare.IsDone())) { 
206     Done = Standard_False;
207     return;
208   }
209
210   for(j = myParameters.Lower(); j <= myParameters.Upper(); j++) {
211     ValGrad_F(j) = 0.0;
212   }
213
214   if (!Contraintes) {
215     MyLeastSquare.ErrorGradient(ValGrad_F, FVal, ERR3d, ERR2d);
216   }
217   else {
218   }
219 }
220
221
222
223 void AppParCurves_BSpFunction::SetFirstLambda(const Standard_Real l1)
224 {
225   mylambda1 = l1;
226 }
227
228 void AppParCurves_BSpFunction::SetLastLambda(const Standard_Real l2)
229 {
230   mylambda2 = l2;
231 }
232
233
234
235 Standard_Integer AppParCurves_BSpFunction::NbVariables() const{ 
236   return NbP;
237 }
238
239
240 Standard_Boolean AppParCurves_BSpFunction::Gradient (const math_Vector& X,
241                                                      math_Vector& G) {
242
243   Perform(X);
244   G = ValGrad_F;
245
246   return Standard_True;
247 }
248
249
250 Standard_Boolean AppParCurves_BSpFunction::Values (const math_Vector& X, 
251                                                    Standard_Real& F, 
252                                                    math_Vector& G) {
253
254
255   Perform(X);
256   F = FVal;
257   G = ValGrad_F;
258
259 /*
260   math_Vector mygradient = G;
261   math_Vector myx = X;
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++) {
266     myx = X;
267     myx(i) = X(i) + 1.0e-10;
268     Value(myx, F2);
269     mygradient(i) = (F2 - myf)/(1.0e-10);
270   }
271
272 cout << " Gradient calcule : " << G2 << endl;
273 cout << " Gradient interpole : " <<  mygradient << endl;
274 */
275   return Standard_True;
276 }
277
278
279 AppParCurves_MultiBSpCurve AppParCurves_BSpFunction::CurveValue() {
280   if (!Contraintes)  MyMultiBSpCurve = MyLeastSquare.BSplineValue();
281   return MyMultiBSpCurve;
282 }
283
284
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));
291 }
292
293 Standard_Real AppParCurves_BSpFunction::MaxError3d() const
294 {
295   return ERR3d;
296 }
297
298 Standard_Real AppParCurves_BSpFunction::MaxError2d() const
299 {
300   return ERR2d;
301 }
302
303
304
305 const math_Vector& AppParCurves_BSpFunction::NewParameters() const
306 {
307   return myParameters;
308 }
309
310
311 const math_Matrix& AppParCurves_BSpFunction::FunctionMatrix() const
312 {
313   return MyLeastSquare.FunctionMatrix();
314 }
315
316 const math_Matrix& AppParCurves_BSpFunction::DerivativeFunctionMatrix() const
317 {
318   return MyLeastSquare.DerivativeFunctionMatrix();
319 }
320
321
322 const math_IntegerVector& AppParCurves_BSpFunction::Index() const
323 {
324   return MyLeastSquare.KIndex();
325 }