0032806: Coding - get rid of unused headers [Contap to Extrema]
[occt.git] / src / Extrema / Extrema_FuncExtCS.cxx
1 // Created on: 1996-01-09
2 // Created by: Laurent PAINNOT
3 // Copyright (c) 1996-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
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.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Adaptor3d_Surface.hxx>
19 #include <Extrema_FuncExtCS.hxx>
20 #include <Extrema_POnCurv.hxx>
21 #include <Extrema_POnSurf.hxx>
22 #include <gp_Vec.hxx>
23 #include <math_Matrix.hxx>
24 #include <Precision.hxx>
25 #include <Standard_TypeMismatch.hxx>
26
27 /*-----------------------------------------------------------------------------
28  Fonction permettant de rechercher une distance extremale entre une courbe C 
29 et une surface S.
30  Cette classe herite de math_FunctionWithDerivative et est utilisee par
31 les algorithmes math_FunctionRoot et math_FunctionRoots.
32 { F1(t,u,v) = (C(t)-S(u,v)).Dtc(t) }
33 { F2(t,u,v) = (C(t)-S(u,v)).Dus(u,v) }
34 { F3(t,u,v) = (C(t)-S(u,v)).Dvs(u,v) }
35 { Dtf1(t,u,v) = Dtc(t).Dtc(t)+(C(t)-S(u,v)).Dttc(t) 
36               = ||Dtc(t)||**2+(C(t)-S(u,v)).Dttc(t) }
37 { Duf1(t,u,v) = -Dus(u,v).Dtc(t) }
38 { Dvf1(t,u,v) = -Dvs(u,v).Dtc(t) }
39 { Dtf2(t,u,v) = Dtc(t).Dus(u,v) }
40 { Duf2(t,u,v) = -Dus(u,v).Dus(u,v)+(C(t)-S(u,v)).Duus(u,v)
41               = -||Dus(u,v)||**2+(C(t)-S(u,v)).Duus(u,v) }
42 { Dvf2(t,u,v) = -Dvs(u,v).Dus(u,v)+(C(t)-S(u,v)).Duvs(u,v) }
43 { Dtf3(t,u,v) = Dtc(t).Dvs(u,v) }
44 { Duf3(t,u,v) = -Dus(u,v).Dvs(u,v)+(C(t)-S(u,v)).Duvs(u,v) }
45 { Dvf3(t,u,v) = -Dvs(u,v).Dvs(u,v)+(C(t)-S(u,v)).Dvvs(u,v) }
46 ----------------------------------------------------------------------------*/
47 //=======================================================================
48 //function : Extrema_FuncExtCS
49 //purpose  : 
50 //=======================================================================
51  Extrema_FuncExtCS::Extrema_FuncExtCS()
52  : myC(NULL),
53    myS(NULL),
54    myt(0.0),
55    myU(0.0),
56    myV(0.0)
57 {
58   myCinit = Standard_False;
59   mySinit = Standard_False;
60 }
61
62 //=======================================================================
63 //function : Extrema_FuncExtCS
64 //purpose  : 
65 //=======================================================================
66
67  Extrema_FuncExtCS::Extrema_FuncExtCS(const Adaptor3d_Curve& C, 
68                                       const Adaptor3d_Surface& S)
69 {
70   Initialize(C, S);
71 }
72
73 //=======================================================================
74 //function : Initialize
75 //purpose  : 
76 //=======================================================================
77
78 void Extrema_FuncExtCS::Initialize(const Adaptor3d_Curve& C, 
79                                    const Adaptor3d_Surface& S)
80 {
81   myC = &C;
82   myS = &S;
83   myCinit = Standard_True;
84   mySinit = Standard_True;
85   myPoint1.Clear();
86   myPoint2.Clear();
87   mySqDist.Clear();
88 }
89
90 //=======================================================================
91 //function : NbVariables
92 //purpose  : 
93 //=======================================================================
94
95 Standard_Integer Extrema_FuncExtCS::NbVariables() const 
96 {
97   return (3);
98 }
99
100 //=======================================================================
101 //function : NbEquations
102 //purpose  : 
103 //=======================================================================
104
105 Standard_Integer Extrema_FuncExtCS::NbEquations() const 
106 {
107   return (3);
108 }
109
110 //=======================================================================
111 //function : Value
112 //purpose  : 
113 //=======================================================================
114
115 Standard_Boolean Extrema_FuncExtCS::Value(const math_Vector& UV, 
116                                           math_Vector& F)
117 {
118   if (!myCinit || !mySinit) throw Standard_TypeMismatch();
119
120   myt = UV(1);
121   myU = UV(2);
122   myV = UV(3);
123
124 //  gp_Vec Dtc, Dttc;
125   gp_Vec Dtc;
126 ///  gp_Vec Dus, Dvs, Duvs, Duus, Dvvs;
127   gp_Vec Dus, Dvs;
128   myC->D1(myt, myP1, Dtc);
129   myS->D1(myU,myV,myP2,Dus,Dvs);
130
131   gp_Vec P1P2 (myP2,myP1);
132
133   F(1) = P1P2.Dot(Dtc);
134   F(2) = P1P2.Dot(Dus);
135   F(3) = P1P2.Dot(Dvs);
136   
137   return  Standard_True;
138 }
139
140 //=======================================================================
141 //function : Derivatives
142 //purpose  : 
143 //=======================================================================
144
145 Standard_Boolean Extrema_FuncExtCS::Derivatives(const math_Vector& UV, 
146                                                 math_Matrix& DF)
147 {
148   math_Vector F(1,3);
149   return Values(UV,F,DF);
150 }
151
152 //=======================================================================
153 //function : Values
154 //purpose  : 
155 //=======================================================================
156
157 Standard_Boolean Extrema_FuncExtCS::Values(const math_Vector& UV, 
158                                            math_Vector& F, 
159                                            math_Matrix& Df)
160 {
161   if (!myCinit || !mySinit) throw Standard_TypeMismatch();
162
163   myt = UV(1);
164   myU = UV(2);
165   myV = UV(3);
166
167   gp_Vec Dtc, Dttc;
168   gp_Vec Dus, Dvs, Duvs, Duus, Dvvs;
169   myC->D2(myt, myP1, Dtc, Dttc);
170   myS->D2(myU,myV,myP2,Dus,Dvs,Duus,Dvvs,Duvs);
171
172   gp_Vec P1P2 (myP2,myP1);
173
174   F(1) = P1P2.Dot(Dtc);
175   F(2) = P1P2.Dot(Dus);
176   F(3) = P1P2.Dot(Dvs);
177
178   Df(1,1) = Dtc.SquareMagnitude() + P1P2.Dot(Dttc);
179   Df(1,2) = -Dus.Dot(Dtc);
180   Df(1,3) = -Dvs.Dot(Dtc);
181
182   Df(2,1) = -Df(1, 2);   // Dtc.Dot(Dus);
183   Df(2,2) = -Dus.SquareMagnitude()+P1P2.Dot(Duus);
184   Df(2,3) = -Dvs.Dot(Dus)+P1P2.Dot(Duvs);
185
186   Df(3,1) = -Df(1,3);    // Dtc.Dot(Dvs);
187   Df(3,2) = Df(2,3);     // -Dus.Dot(Dvs)+P1P2.Dot(Duvs);
188   Df(3,3) = -Dvs.SquareMagnitude()+P1P2.Dot(Dvvs);
189
190   return Standard_True;
191
192 }
193
194 //=======================================================================
195 //function : GetStateNumber
196 //purpose  : 
197 //=======================================================================
198
199 Standard_Integer Extrema_FuncExtCS::GetStateNumber()
200 {
201   if (!myCinit || !mySinit) throw Standard_TypeMismatch();
202 #if 0
203   math_Vector Sol(1, 3), UVSol(1, 3);
204   UVSol(1) = myt; UVSol(2) = myU; UVSol(3) = myV;
205   Value(UVSol, Sol);
206   std::cout <<"F(1)= "<<Sol(1)<<" F(2)= "<<Sol(2)<<" F(3)= "<<Sol(3)<<std::endl;
207 #endif
208   //comparison of solution with previous solutions
209   Standard_Real tol2d = Precision::SquarePConfusion();
210   Standard_Integer i = 1, nbSol = mySqDist.Length();
211   for( ; i <=  nbSol; i++)
212   {
213     Standard_Real aT = myPoint1(i).Parameter();
214     aT -= myt; aT *= aT;
215     if( aT <= tol2d )
216       break;
217   }
218   if (i <= nbSol)
219     return 0;
220   mySqDist.Append(myP1.SquareDistance(myP2));
221   myPoint1.Append(Extrema_POnCurv(myt,myP1));
222   myPoint2.Append(Extrema_POnSurf(myU,myV,myP2));
223   return 0;
224 }
225
226 //=======================================================================
227 //function : NbExt
228 //purpose  : 
229 //=======================================================================
230
231 Standard_Integer Extrema_FuncExtCS::NbExt() const 
232 {
233   return mySqDist.Length();
234 }
235
236 //=======================================================================
237 //function : SquareDistance
238 //purpose  : 
239 //=======================================================================
240
241 Standard_Real Extrema_FuncExtCS::SquareDistance(const Standard_Integer N) const 
242 {
243   if (!myCinit || !mySinit) throw Standard_TypeMismatch();
244   return mySqDist.Value(N);
245 }
246
247 //=======================================================================
248 //function : PointOnCurve
249 //purpose  : 
250 //=======================================================================
251
252 const Extrema_POnCurv& Extrema_FuncExtCS::PointOnCurve(const Standard_Integer N) const 
253 {
254   if (!myCinit || !mySinit) throw Standard_TypeMismatch();
255   return myPoint1.Value(N);
256 }
257
258 //=======================================================================
259 //function : PointOnSurface
260 //purpose  : 
261 //=======================================================================
262
263 const Extrema_POnSurf& Extrema_FuncExtCS::PointOnSurface(const Standard_Integer N) const 
264 {
265   if (!myCinit || !mySinit) throw Standard_TypeMismatch();
266   return myPoint2.Value(N);
267 }