0022241: The bug is appendix to the Salome Bug 0021148
[occt.git] / src / Extrema / Extrema_FuncExtSS.cxx
1 // File:        Extrema_FuncExtSS.cxx
2 // Created:     Tue Jan  9 10:13:04 1996
3 // Author:      Laurent PAINNOT
4 //              <lpa@nonox>
5
6
7 #include <Extrema_FuncExtSS.ixx>
8 #include <Standard_TypeMismatch.hxx>
9 #include <gp_Vec.hxx>
10
11
12 /*----------------------------------------------------------------------------
13  Si on note Du1s et Dv1s, les derivees en u1 et v1, les 2 fonctions a annuler sont:
14  Si on note Du2s et Dv2s, les derivees en u2 et v2, les 2 fonctions a annuler sont:
15
16  { F1(u1,v1,u2,v2) = (S1(u1,v1)-S2(u2,v2)).Du1s1(u1,v1) }
17  { F2(u1,v1,u2,v2) = (S1(u1,v1)-S2(u2,v2)).Dv1s1(u1,v1) }
18  { F3(u1,v1,u2,v2) = (S1(u1,v1)-S2(u2,v2)).Du2s2(u2,v2) }
19  { F4(u1,v1,u2,v2) = (S1(u1,v1)-S2(u2,v2)).Dv2s2(u2,v2) }
20
21  { du1f1(u1,v1,u2,v2) = Du1s1(u1,v1).Du1s1(u1,v1)+(S1(u1,v1)-S2(u2,v2)).Du1u1s1(u1,v1) 
22                       = ||Du1s1(u1,v1)||**2      +(S1(u1,v1)-S2(u2,v2)).Du1u1s1(u1,v1) }
23  { dv1f1(u1,v1,u2,v2) = Dv1s1(u1,v1).Du1s1(u1,v1)+(S1(u1,v1)-S2(u2,v2)).Du1v1s1(u1,v1) }
24  { du2f1(u1,v1,u2,v2) = -Du2s2(u2,v2).Du1s1(u1,v1) }
25  { dv2f1(u1,v1,u2,v2) = -Dv2s2(u2,v2).Du1s1(u1,v1) }
26
27  { du1f2(u1,v1,u2,v2) = Du1s1(u1,v1).Dv1s1(u1,v1)+(S1(u1,v1)-S2(u2,v2)).Du1v1s1(u1,v1) }
28  { dv1f2(u1,v1,u2,v2) = Dv1s1(u1,v1).Dv1s1(u1,v1)+(S1(u1,v1)-S2(u2,v2)).Dv1v1s1(u1,v1) 
29                       = ||Dv1s1(u1,v1)||**2      +(S1(u1,v1)-S2(u2,v2)).Dv1v1s1(u1,v1) }
30  { du2f2(u1,v1,u2,v2) = -Du2s2(u2,v2).Dv1s1(u1,v1) }
31  { dv2f2(u1,v1,u2,v2) = -Dv2s2(u2,v2).Dv1s1(u1,v1) }
32
33  { du1f3(u1,v1,u2,v2) = Du1s1(u1,v1).Du2s2(u2,v2) }
34  { dv1f3(u1,v1,u2,v2) = Dv1s1(u1,v1).Du2s2(u2,v2) }
35  { du2f3(u1,v1,u2,v2) = -Du2s2(u2,v2).Du2s2(u2,v2)+(S1(u1,v1)-S2(u2,v2)).Du2u2s2(u2,v2) 
36                       = -||Du2s2(u2,v2)||**2      +(S1(u1,v1)-S2(u2,v2)).Du2u2s2(u2,v2) }
37  { dv2f3(u1,v1,u2,v2) = -Dv2s2(u2,v2).Du2s2(u2,v2)+(S1(u1,v1)-S2(u2,v2)).Dv2u2s2(u2,v2) }
38
39  { du1f4(u1,v1,u2,v2) = Du1s1(u1,v1).Dv2s2(u2,v2) }
40  { dv1f4(u1,v1,u2,v2) = Dv1s1(u1,v1).Dv2s2(u2,v2) }
41  { du2f4(u1,v1,u2,v2) = -Du2s2(u2,v2).Dv2s2(u2,v2)+(S1(u1,v1)-S2(u2,v2)).Du2v2s2(u2,v2) }
42  { dv2f4(u1,v1,u2,v2) = -Dv2s2(u2,v2).Dv2s2(u2,v2)+(S1(u1,v1)-S2(u2,v2)).Dv2v2s2(u2,v2) 
43                       = -||Dv2s2(u2,v2)||**2      +(S1(u1,v1)-S2(u2,v2)).Dv2v2s2(u2,v2) }
44
45 ----------------------------------------------------------------------------*/
46
47
48 //=======================================================================
49 //function : Extrema_FuncExtSS
50 //purpose  : 
51 //=======================================================================
52
53 Extrema_FuncExtSS::Extrema_FuncExtSS ()
54 {
55   myS1init = Standard_False;
56   myS2init = Standard_False;
57 }
58
59 //=======================================================================
60 //function : Extrema_FuncExtSS
61 //purpose  : 
62 //=======================================================================
63
64 Extrema_FuncExtSS::Extrema_FuncExtSS (const Adaptor3d_Surface& S1,
65                                       const Adaptor3d_Surface& S2)
66 {
67   myS1 = (Adaptor3d_SurfacePtr)&S1;
68   myS2 = (Adaptor3d_SurfacePtr)&S2;
69   myS1init = Standard_True;
70   myS2init = Standard_True;
71 }
72
73 //=======================================================================
74 //function : Initialize
75 //purpose  : 
76 //=======================================================================
77
78 void Extrema_FuncExtSS::Initialize(const Adaptor3d_Surface& S1,
79                                    const Adaptor3d_Surface& S2)
80 {
81   myS1 = (Adaptor3d_SurfacePtr)&S1;
82   myS2 = (Adaptor3d_SurfacePtr)&S2;
83   myS1init = Standard_True;
84   myS2init = Standard_True;
85   myPoint1.Clear();
86   myPoint2.Clear();
87   mySqDist.Clear();
88 }
89
90
91
92 //=======================================================================
93 //function : NbVariables
94 //purpose  : 
95 //=======================================================================
96
97 Standard_Integer Extrema_FuncExtSS::NbVariables () const { return 4;}
98
99
100 //=======================================================================
101 //function : NbEquations
102 //purpose  : 
103 //=======================================================================
104
105 Standard_Integer Extrema_FuncExtSS::NbEquations () const { return 4;}
106
107
108 //=======================================================================
109 //function : Value
110 //purpose  : 
111 //=======================================================================
112
113 Standard_Boolean Extrema_FuncExtSS::Value (const math_Vector& UV, 
114                                            math_Vector& F)
115 {
116   if (!myS1init || !myS2init) Standard_TypeMismatch::Raise();
117   myU1 = UV(1);
118   myV1 = UV(2);
119   myU2 = UV(3);
120   myV2 = UV(4);
121   gp_Vec Du1s1, Dv1s1;
122   gp_Vec Du2s2, Dv2s2;
123   myS1->D1(myU1,myV1,myP1,Du1s1,Dv1s1);
124   myS2->D1(myU2,myV2,myP2,Du2s2,Dv2s2);
125
126   gp_Vec P1P2 (myP2,myP1);
127
128   F(1) = P1P2.Dot(Du1s1);
129   F(2) = P1P2.Dot(Dv1s1);
130   F(3) = P1P2.Dot(Du2s2);
131   F(4) = P1P2.Dot(Dv2s2);
132
133   return Standard_True;  
134 }
135
136 //=======================================================================
137 //function : Derivatives
138 //purpose  : 
139 //=======================================================================
140
141 Standard_Boolean Extrema_FuncExtSS::Derivatives (const math_Vector& UV, 
142                                                  math_Matrix& Df)
143 {
144   math_Vector F(1,4);
145   return Values(UV,F,Df);
146 }
147
148 //=======================================================================
149 //function : Values
150 //purpose  : 
151 //=======================================================================
152
153 Standard_Boolean Extrema_FuncExtSS::Values (const math_Vector& UV, 
154                                             math_Vector& F,
155                                             math_Matrix& Df)
156 {
157   if (!myS1init || !myS2init) Standard_TypeMismatch::Raise();
158   myU1 = UV(1);
159   myV1 = UV(2);
160   myU2 = UV(3);
161   myV2 = UV(4);
162   gp_Vec Du1s1, Dv1s1, Du1u1s1, Dv1v1s1, Du1v1s1;
163   gp_Vec Du2s2, Dv2s2, Du2u2s2, Dv2v2s2, Du2v2s2;
164   myS1->D2(myU1,myV1,myP1,Du1s1,Dv1s1,Du1u1s1,Dv1v1s1,Du1v1s1);
165   myS2->D2(myU2,myV2,myP2,Du2s2,Dv2s2,Du2u2s2,Dv2v2s2,Du2v2s2);
166
167   gp_Vec P1P2 (myP2,myP1);
168
169   F(1) = P1P2.Dot(Du1s1);
170   F(2) = P1P2.Dot(Dv1s1);
171   F(3) = P1P2.Dot(Du2s2);
172   F(4) = P1P2.Dot(Dv2s2);
173
174   Df(1,1) = Du1s1.SquareMagnitude() + P1P2.Dot(Du1u1s1);
175   Df(1,2) = Dv1s1.Dot(Du1s1) + P1P2.Dot(Du1v1s1);
176   Df(1,3) = -Du2s2.Dot(Du1s1);
177   Df(1,4) = -Dv2s2.Dot(Du1s1);
178
179   Df(2,1) = Df(1, 2);   // Du1s1.Dot(Dv1s1) + P1P2.Dot(Du1v1s1);
180   Df(2,2) = Dv1s1.SquareMagnitude() + P1P2.Dot(Dv1v1s1);
181   Df(2,3) = -Du2s2.Dot(Dv1s1);
182   Df(2,4) = -Dv2s2.Dot(Dv1s1);
183
184   Df(3,1) = -Df(1,3);   // Du1s1.Dot(Du2s2);
185   Df(3,2) = -Df(2,3);   // Dv1s1.Dot(Du2s2);
186   Df(3,3) = -Du2s2.SquareMagnitude() + P1P2.Dot(Du2u2s2);
187   Df(3,4) = -Dv2s2.Dot(Du2s2) + P1P2.Dot(Du2v2s2);
188
189   Df(4,1) = -Df(1,4);   // Du1s1.Dot(Dv2s2);
190   Df(4,2) = -Df(2,4);   // Dv1s1.Dot(Dv2s2);
191   Df(4,3) = Df(3,4);    // -Du2s2.Dot(Dv2s2) + P1P2.Dot(Du2v2s2);
192   Df(4,4) = -Dv2s2.SquareMagnitude() + P1P2.Dot(Dv2v2s2);
193
194   return Standard_True;
195 }
196
197 //=======================================================================
198 //function : GetStateNumber
199 //purpose  : 
200 //=======================================================================
201
202 Standard_Integer Extrema_FuncExtSS::GetStateNumber ()
203 {
204   if (!myS1init || !myS2init) Standard_TypeMismatch::Raise();
205 #if 0
206   math_Vector Sol(1, 4), UVSol(1, 4);
207   UVSol(1) = myU1; UVSol(2) = myV1; UVSol(3) = myU2; UVSol(4) = myV2;
208   Value(UVSol, Sol);
209   cout <<"F(1)= "<<Sol(1)<<" F(2)= "<<Sol(2)<<" F(3)= "<<Sol(3)<<" F(4)= "<<Sol(4)<<endl;
210 #endif
211
212   mySqDist.Append(myP1.SquareDistance(myP2));
213   myPoint1.Append(Extrema_POnSurf(myU1,myV1,myP1));
214   myPoint2.Append(Extrema_POnSurf(myU2,myV2,myP2));
215   return 0;
216 }
217
218 //=======================================================================
219 //function : NbExt
220 //purpose  : 
221 //=======================================================================
222
223 Standard_Integer Extrema_FuncExtSS::NbExt () const
224 {
225   return mySqDist.Length();
226 }
227
228 //=======================================================================
229 //function : SquareDistance
230 //purpose  : 
231 //=======================================================================
232
233 Standard_Real Extrema_FuncExtSS::SquareDistance (const Standard_Integer N) const
234 {
235   if (!myS1init || !myS2init) Standard_TypeMismatch::Raise();
236   return mySqDist.Value(N);
237 }
238
239 //=======================================================================
240 //function : PointOnS1
241 //purpose  : 
242 //=======================================================================
243
244 Extrema_POnSurf Extrema_FuncExtSS::PointOnS1 (const Standard_Integer N) const
245 {
246   if (!myS1init || !myS2init) Standard_TypeMismatch::Raise();
247   return myPoint1.Value(N);
248 }
249 //=======================================================================
250 //function : PointOnS2
251 //purpose  : 
252 //=======================================================================
253
254 Extrema_POnSurf Extrema_FuncExtSS::PointOnS2 (const Standard_Integer N) const
255 {
256   if (!myS1init || !myS2init) Standard_TypeMismatch::Raise();
257   return myPoint2.Value(N);
258 }
259
260