b311480e |
1 | // Copyright (c) 1995-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
7fd59977 |
15 | //-- IntAna_IntLinTorus.cxx |
16 | //-- lbr : la methode avec les coefficients est catastrophique. |
17 | //-- Mise en place d'une vraie solution. |
18 | |
42cf5bc1 |
19 | #include <ElCLib.hxx> |
20 | #include <ElSLib.hxx> |
7fd59977 |
21 | #include <gp_Dir.hxx> |
42cf5bc1 |
22 | #include <gp_Lin.hxx> |
7fd59977 |
23 | #include <gp_Pnt.hxx> |
42cf5bc1 |
24 | #include <gp_Torus.hxx> |
7fd59977 |
25 | #include <gp_Trsf.hxx> |
42cf5bc1 |
26 | #include <IntAna_IntLinTorus.hxx> |
27 | #include <math_DirectPolynomialRoots.hxx> |
28 | #include <Standard_OutOfRange.hxx> |
29 | #include <StdFail_NotDone.hxx> |
30 | #include <TColStd_Array1OfReal.hxx> |
7fd59977 |
31 | |
32 | IntAna_IntLinTorus::IntAna_IntLinTorus () : done(Standard_False) |
33 | {} |
34 | |
35 | IntAna_IntLinTorus::IntAna_IntLinTorus (const gp_Lin& L, const gp_Torus& T) { |
36 | Perform(L,T); |
37 | } |
38 | |
39 | |
40 | void IntAna_IntLinTorus::Perform (const gp_Lin& L, const gp_Torus& T) { |
41 | gp_Pnt PL=L.Location(); |
42 | gp_Dir DL=L.Direction(); |
43 | |
44 | // Reparametrize the line: |
45 | // set its location as nearest to the location of torus |
46 | gp_Pnt TorLoc = T.Location(); |
47 | Standard_Real ParamOfNewPL = gp_Vec(PL, TorLoc).Dot(gp_Vec(DL)); |
48 | gp_Pnt NewPL( PL.XYZ() + ParamOfNewPL * DL.XYZ() ); |
49 | |
50 | //-------------------------------------------------------------- |
51 | //-- Coefficients de la ligne dans le repere du cone |
52 | //-- |
53 | gp_Trsf trsf; |
54 | trsf.SetTransformation(T.Position()); |
55 | NewPL.Transform(trsf); |
56 | DL.Transform(trsf); |
57 | |
58 | Standard_Real a,b,c,x1,y1,z1,x0,y0,z0; |
59 | Standard_Real a0,a1,a2,a3,a4; |
60 | Standard_Real R,r,R2,r2; |
61 | |
62 | x1 = DL.X(); y1 = DL.Y(); z1 = DL.Z(); |
63 | x0 = NewPL.X(); y0 = NewPL.Y(); z0 = NewPL.Z(); |
64 | R = T.MajorRadius(); R2 = R*R; |
65 | r = T.MinorRadius(); r2 = r*r; |
66 | |
67 | a = x1*x1+y1*y1+z1*z1; |
68 | b = 2.0*(x1*x0+y1*y0+z1*z0); |
69 | c = x0*x0+y0*y0+z0*z0 - (R2+r2); |
70 | |
71 | a4 = a*a; |
72 | a3 = 2.0*a*b; |
73 | a2 = 2.0*a*c+4.0*R2*z1*z1+b*b; |
74 | a1 = 2.0*b*c+8.0*R2*z1*z0; |
75 | a0 = c*c+4.0*R2*(z0*z0-r2); |
76 | |
77 | Standard_Real u,v; |
78 | math_DirectPolynomialRoots mdpr(a4,a3,a2,a1,a0); |
79 | if(mdpr.IsDone()) { |
80 | Standard_Integer nbsolvalid = 0; |
81 | Standard_Integer n = mdpr.NbSolutions(); |
82 | for(Standard_Integer i = 1; i<=n ; i++) { |
83 | Standard_Real t = mdpr.Value(i); |
84 | t += ParamOfNewPL; |
85 | gp_Pnt PSolL(ElCLib::Value(t,L)); |
86 | ElSLib::Parameters(T,PSolL,u,v); |
87 | gp_Pnt PSolT(ElSLib::Value(u,v,T)); |
88 | a0 = PSolT.SquareDistance(PSolL); |
89 | |
90 | if(a0>0.0000000001) { |
91 | #if 0 |
92 | cout<<" ------- Erreur : P Ligne < > P Tore "<<endl; |
93 | cout<<"Ligne : X:"<<PSolL.X()<<" Y:"<<PSolL.Y()<<" Z:"<<PSolL.Z()<<" l:"<<t<<endl; |
94 | cout<<"Tore : X:"<<PSolT.X()<<" Y:"<<PSolT.Y()<<" Z:"<<PSolT.Z()<<" u:"<<u<<" v:"<<v<<endl; |
95 | #endif |
96 | } |
97 | else { |
98 | theParam[nbsolvalid] = t; |
99 | theFi[nbsolvalid] = u; |
100 | theTheta[nbsolvalid] = v; |
101 | thePoint[nbsolvalid] = PSolL; |
102 | nbsolvalid++; |
103 | } |
104 | } |
105 | nbpt = nbsolvalid; |
106 | done = Standard_True; |
107 | } |
108 | else { |
109 | nbpt = 0; |
110 | done = Standard_False; |
111 | } |
112 | } |
113 | |
114 | |
115 | #if 0 |
116 | |
117 | static void MULT_A3_B1(Standard_Real& c4, |
118 | Standard_Real& c3, |
119 | Standard_Real& c2, |
120 | Standard_Real& c1, |
121 | Standard_Real& c0, |
122 | const Standard_Real a3, |
123 | const Standard_Real a2, |
124 | const Standard_Real a1, |
125 | const Standard_Real a0, |
126 | const Standard_Real b1, |
127 | const Standard_Real b0) { |
128 | c4 = a3 * b1; |
129 | c3 = a3 * b0 + a2 * b1; |
130 | c2 = a2 * b0 + a1 * b1; |
131 | c1 = a1 * b0 + a0 * b1; |
132 | c0 = a0 * b0; |
133 | } |
134 | |
135 | static void MULT_A2_B2(Standard_Real& c4, |
136 | Standard_Real& c3, |
137 | Standard_Real& c2, |
138 | Standard_Real& c1, |
139 | Standard_Real& c0, |
140 | const Standard_Real a2, |
141 | const Standard_Real a1, |
142 | const Standard_Real a0, |
143 | const Standard_Real b2, |
144 | const Standard_Real b1, |
145 | const Standard_Real b0) { |
146 | c4 = a2 * b2; |
147 | c3 = a2 * b1 + a1 * b2; |
148 | c2 = a2 * b0 + a1 * b1 + a0 * b2; |
149 | c1 = a1 * b0 + a0 * b1; |
150 | c0 = a0 * b0; |
151 | } |
152 | |
153 | static void MULT_A2_B1(Standard_Real& c3, |
154 | Standard_Real& c2, |
155 | Standard_Real& c1, |
156 | Standard_Real& c0, |
157 | const Standard_Real a2, |
158 | const Standard_Real a1, |
159 | const Standard_Real a0, |
160 | const Standard_Real b1, |
161 | const Standard_Real b0) { |
162 | c3 = a2 * b1; |
163 | c2 = a2 * b0 + a1 * b1; |
164 | c1 = a1 * b0 + a0 * b1; |
165 | c0 = a0 * b0; |
166 | } |
167 | |
168 | void IntAna_IntLinTorus::Perform (const gp_Lin& L, const gp_Torus& T) { |
169 | TColStd_Array1OfReal C(1,31); |
170 | T.Coefficients(C); |
171 | const gp_Pnt& PL=L.Location(); |
172 | const gp_Dir& DL=L.Direction(); |
173 | |
174 | //---------------------------------------------------------------- |
175 | //-- X = ax1 l + ax0 |
176 | //-- X2 = ax2 l2 + 2 ax1 ax0 l + bx2 |
177 | //-- X3 = ax3 l3 + 3 ax2 ax0 l2 + 3 ax1 bx2 l + bx3 |
178 | //-- X4 = ax4 l4 + 4 ax3 ax0 l3 + 6 ax2 bx2 l2 + 4 ax1 bx3 l + bx4 |
179 | |
180 | Standard_Real ax1,ax2,ax3,ax4,ax0,bx2,bx3,bx4; |
181 | Standard_Real ay1,ay2,ay3,ay4,ay0,by2,by3,by4; |
182 | Standard_Real az1,az2,az3,az4,az0,bz2,bz3,bz4; |
183 | Standard_Real c0,c1,c2,c3,c4; |
184 | ax1=DL.X(); ax0=PL.X(); ay1=DL.Y(); ay0=PL.Y(); az1=DL.Z(); az0=PL.Z(); |
185 | ax2=ax1*ax1; ax3=ax2*ax1; ax4=ax3*ax1; bx2=ax0*ax0; bx3=bx2*ax0; bx4=bx3*ax0; |
186 | ay2=ay1*ay1; ay3=ay2*ay1; ay4=ay3*ay1; by2=ay0*ay0; by3=by2*ay0; by4=by3*ay0; |
187 | az2=az1*az1; az3=az2*az1; az4=az3*az1; bz2=az0*az0; bz3=bz2*az0; bz4=bz3*az0; |
188 | |
189 | //--------------------------------------------------------------------------- Terme X**4 |
190 | Standard_Real c=C(1); |
191 | Standard_Real a4 = c *ax4; |
192 | Standard_Real a3 = c *4.0*ax3*ax0; |
193 | Standard_Real a2 = c *6.0*ax2*bx2; |
194 | Standard_Real a1 = c *4.0*ax1*bx3; |
195 | Standard_Real a0 = c *bx4; |
196 | //--------------------------------------------------------------------------- Terme Y**4 |
197 | c = C(2); |
198 | a4+= c*ay4; |
199 | a3+= c*4.0*ay3*ay0; |
200 | a2+= c*6.0*ay2*by2; |
201 | a1+= c*4.0*ay1*by3; |
202 | a0+= c*by4; |
203 | //--------------------------------------------------------------------------- Terme Z**4 |
204 | c = C(3); |
205 | a4+= c*az4 ; |
206 | a3+= c*4.0*az3*az0; |
207 | a2+= c*6.0*az2*bz2; |
208 | a1+= c*4.0*az1*bz3; |
209 | a0+= c*bz4; |
210 | //--------------------------------------------------------------------------- Terme X**3 Y |
211 | c = C(4); |
212 | MULT_A3_B1(c4,c3,c2,c1,c0, ax3, 3.0*ax2*ax0, 3.0*ax1*bx2, bx3, ay1,ay0); |
213 | a4+= c*c4; a3+= c*c3; a2+= c*c2; a1+= c*c1; a0+= c*c0; |
214 | //--------------------------------------------------------------------------- Terme X**3 Z |
215 | c = C(5); |
216 | MULT_A3_B1(c4,c3,c2,c1,c0, ax3, 3.0*ax2*ax0, 3.0*ax1*bx2, bx3, az1,az0); |
217 | a4+= c*c4; a3+= c*c3; a2+= c*c2; a1+= c*c1; a0+= c*c0; |
218 | //--------------------------------------------------------------------------- Terme Y**3 X |
219 | c = C(6); |
220 | MULT_A3_B1(c4,c3,c2,c1,c0, ay3, 3.0*ay2*ay0, 3.0*ay1*by2, by3, ax1,ax0); |
221 | a4+= c*c4; a3+= c*c3; a2+= c*c2; a1+= c*c1; a0+= c*c0; |
222 | //--------------------------------------------------------------------------- Terme Y**3 Z |
223 | c = C(7); |
224 | MULT_A3_B1(c4,c3,c2,c1,c0, ay3, 3.0*ay2*ay0, 3.0*ay1*by2, by3, az1,az0); |
225 | a4+= c*c4; a3+= c*c3; a2+= c*c2; a1+= c*c1; a0+= c*c0; |
226 | //--------------------------------------------------------------------------- Terme Z**3 X |
227 | c = C(8); |
228 | MULT_A3_B1(c4,c3,c2,c1,c0, az3, 3.0*az2*az0, 3.0*az1*bz2, bz3, ax1,ax0); |
229 | a4+= c*c4; a3+= c*c3; a2+= c*c2; a1+= c*c1; a0+= c*c0; |
230 | //--------------------------------------------------------------------------- Terme Z**3 Y |
231 | c = C(9); |
232 | MULT_A3_B1(c4,c3,c2,c1,c0, az3, 3.0*az2*az0, 3.0*az1*bz2, bz3, ay1,ay0); |
233 | a4+= c*c4; a3+= c*c3; a2+= c*c2; a1+= c*c1; a0+= c*c0; |
234 | |
235 | |
236 | //--------------------------------------------------------------------------- Terme X**2 Y**2 |
237 | c = C(10); |
238 | MULT_A2_B2(c4,c3,c2,c1,c0, ax2, 2.0*ax1*ax0, bx2, ay2,2.0*ay1*ay0, by2); |
239 | a4+= c*c4; a3+= c*c3; a2+= c*c2; a1+= c*c1; a0+= c*c0; |
240 | //--------------------------------------------------------------------------- Terme X**2 Z**2 |
241 | c = C(11); |
242 | MULT_A2_B2(c4,c3,c2,c1,c0, ax2, 2.0*ax1*ax0, bx2, az2,2.0*az1*az0, bz2); |
243 | a4+= c*c4; a3+= c*c3; a2+= c*c2; a1+= c*c1; a0+= c*c0; |
244 | //--------------------------------------------------------------------------- Terme Y**2 Z**2 |
245 | c = C(12); |
246 | MULT_A2_B2(c4,c3,c2,c1,c0, ay2, 2.0*ay1*ay0, by2, az2,2.0*az1*az0, bz2); |
247 | a4+= c*c4; a3+= c*c3; a2+= c*c2; a1+= c*c1; a0+= c*c0; |
248 | |
249 | |
250 | //--------------------------------------------------------------------------- Terme X**3 |
251 | c = C(13); |
252 | a3+= c*( ax3 ); |
253 | a2+= c*( 3.0*ax2*ax0 ); |
254 | a1+= c*( 3.0*ax1*bx2 ); |
255 | a0+= c*( bx3 ); |
256 | //--------------------------------------------------------------------------- Terme Y**3 |
257 | c = C(14); |
258 | a3+= c*( ay3 ); |
259 | a2+= c*( 3.0*ay2*ay0 ); |
260 | a1+= c*( 3.0*ay1*by2 ); |
261 | a0+= c*( by3 ); |
262 | //--------------------------------------------------------------------------- Terme Y**3 |
263 | c = C(15); |
264 | a3+= c*( az3 ); |
265 | a2+= c*( 3.0*az2*az0 ); |
266 | a1+= c*( 3.0*az1*bz2 ); |
267 | a0+= c*( bz3 ); |
268 | |
269 | |
270 | //--------------------------------------------------------------------------- Terme X**2 Y |
271 | c = C(16); |
272 | MULT_A2_B1(c3,c2,c1,c0, ax2, 2.0*ax1*ax0, bx2, ay1,ay0); |
273 | a3+= c*c3; a2+= c* c2; a1+= c* c1; a0+= c*c0; |
274 | //--------------------------------------------------------------------------- Terme X**2 Z |
275 | c = C(17); |
276 | MULT_A2_B1(c3,c2,c1,c0, ax2, 2.0*ax1*ax0, bx2, az1,az0); |
277 | a3+= c*c3; a2+= c* c2; a1+= c* c1; a0+= c*c0; |
278 | //--------------------------------------------------------------------------- Terme Y**2 X |
279 | c = C(18); |
280 | MULT_A2_B1(c3,c2,c1,c0, ay2, 2.0*ay1*ay0, by2, ax1,ax0); |
281 | a3+= c*c3; a2+= c* c2; a1+= c* c1; a0+= c*c0; |
282 | //--------------------------------------------------------------------------- Terme Y**2 Z |
283 | c = C(19); |
284 | MULT_A2_B1(c3,c2,c1,c0, ay2, 2.0*ay1*ay0, by2, az1,az0); |
285 | a3+= c*c3; a2+= c* c2; a1+= c* c1; a0+= c*c0; |
286 | //--------------------------------------------------------------------------- Terme Z**2 X |
287 | c = C(20); |
288 | MULT_A2_B1(c3,c2,c1,c0, az2, 2.0*az1*az0, bz2, ax1,ax0); |
289 | a3+= c*c3; a2+= c* c2; a1+= c* c1; a0+= c*c0; |
290 | //--------------------------------------------------------------------------- Terme Z**2 Y |
291 | c = C(21); |
292 | MULT_A2_B1(c3,c2,c1,c0, az2, 2.0*az1*az0, bz2, ay1,ay0); |
293 | a3+= c*c3; a2+= c* c2; a1+= c* c1; a0+= c*c0; |
294 | |
295 | |
296 | //--------------------------------------------------------------------------- Terme X**2 |
297 | c = C(22); |
298 | a2+= c*ax2; |
299 | a1+= c*2.0*ax1*ax0; |
300 | a0+= c*bx2; |
301 | //--------------------------------------------------------------------------- Terme Y**2 |
302 | c = C(23); |
303 | a2+= c*ay2; |
304 | a1+= c*2.0*ay1*ay0; |
305 | a0+= c*by2; |
306 | //--------------------------------------------------------------------------- Terme Z**2 |
307 | c = C(24); |
308 | a2+= c*az2; |
309 | a1+= c*2.0*az1*az0; |
310 | a0+= c*bz2; |
311 | |
312 | |
313 | //--------------------------------------------------------------------------- Terme X Y |
314 | c = C(25); |
315 | a2+= c*(ax1*ay1); |
316 | a1+= c*(ax1*ay0 + ax0*ay1); |
317 | a0+= c*(ax0*ay0); |
318 | //--------------------------------------------------------------------------- Terme X Z |
319 | c = C(26); |
320 | a2+= c*(ax1*az1); |
321 | a1+= c*(ax1*az0 + ax0*az1); |
322 | a0+= c*(ax0*az0); |
323 | //--------------------------------------------------------------------------- Terme Y Z |
324 | c = C(27); |
325 | a2+= c*(ay1*az1); |
326 | a1+= c*(ay1*az0 + ay0*az1); |
327 | a0+= c*(ay0*az0); |
328 | |
329 | //--------------------------------------------------------------------------- Terme X |
330 | c = C(28); |
331 | a1+= c*ax1; |
332 | a0+= c*ax0; |
333 | //--------------------------------------------------------------------------- Terme Y |
334 | c = C(29); |
335 | a1+= c*ay1; |
336 | a0+= c*ay0; |
337 | //--------------------------------------------------------------------------- Terme Z |
338 | c = C(30); |
339 | a1+= c*az1; |
340 | a0+= c*az0; |
341 | |
342 | //--------------------------------------------------------------------------- Terme Constant |
343 | c = C(31); |
344 | a0+=c; |
345 | |
346 | |
347 | |
348 | cout<<"\n ---------- Coefficients Line - Torus : "<<endl; |
349 | cout<<" a0 : "<<a0<<endl; |
350 | cout<<" a1 : "<<a1<<endl; |
351 | cout<<" a2 : "<<a2<<endl; |
352 | cout<<" a3 : "<<a3<<endl; |
353 | cout<<" a4 : "<<a4<<endl; |
354 | |
355 | Standard_Real u,v; |
356 | math_DirectPolynomialRoots mdpr(a4,a3,a2,a1,a0); |
357 | if(mdpr.IsDone()) { |
358 | Standard_Integer nbsolvalid = 0; |
359 | Standard_Integer n = mdpr.NbSolutions(); |
360 | for(Standard_Integer i = 1; i<=n ; i++) { |
361 | Standard_Real t = mdpr.Value(i); |
362 | gp_Pnt PSolL(ax0+ax1*t, ay0+ay1*t, az0+az1*t); |
363 | ElSLib::Parameters(T,PSolL,u,v); |
364 | gp_Pnt PSolT(ElSLib::Value(u,v,T)); |
365 | |
366 | a0 = PSolT.SquareDistance(PSolL); |
367 | if(a0>0.0000000001) { |
368 | cout<<" ------- Erreur : P Ligne < > P Tore "; |
369 | cout<<"Ligne : X:"<<PSolL.X()<<" Y:"<<PSolL.Y()<<" Z:"<<PSolL.Z()<<" l:"<<t<<endl; |
370 | cout<<"Tore : X:"<<PSolL.X()<<" Y:"<<PSolL.Y()<<" Z:"<<PSolL.Z()<<" u:"<<u<<" v:"<<v<<endl; |
371 | } |
372 | else { |
373 | theParam[nbsolvalid] = t; |
374 | theFi[nbsolvalid] = v; |
375 | theTheta[nbsolvalid] = u; |
376 | thePoint[nbsolvalid] = PSolL; |
377 | nbsolvalid++; |
378 | } |
379 | } |
380 | nbpt = nbsolvalid; |
381 | done = Standard_True; |
382 | } |
383 | else { |
384 | nbpt = 0; |
385 | done = Standard_False; |
386 | } |
387 | } |
388 | #endif |
389 | |
390 | |