1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
20 #include <Extrema_ExtPElS.ixx>
21 #include <StdFail_NotDone.hxx>
22 #include <Standard_OutOfRange.hxx>
23 #include <Standard_NotImplemented.hxx>
25 //=============================================================================
27 Extrema_ExtPElS::Extrema_ExtPElS () { myDone = Standard_False; }
28 //=============================================================================
30 Extrema_ExtPElS::Extrema_ExtPElS (const gp_Pnt& P,
32 const Standard_Real Tol)
37 /*-----------------------------------------------------------------------------
39 Find 2 extreme distances between point P and cylinder S.
42 Let Pp be the projection of P in plane XOY of the cylinder;
43 2 cases are considered:
44 1- distance(Pp,O) < Tol:
45 There are infinite solutions; IsDone() = Standard_False.
46 2- distance(Pp,O) > Tol:
48 U1 = angle(OX,OPp) with 0 < U1 < 2.*M_PI
49 U2 = U1 + M_PI with 0 < U2 < 2.*M_PI;
50 then (U1,V) corresponds to the min distance.
51 and (U2,V) corresponds to the max distance.
52 -----------------------------------------------------------------------------*/
54 void Extrema_ExtPElS::Perform(const gp_Pnt& P,
56 const Standard_Real Tol)
58 myDone = Standard_False;
61 // Projection of point P in plane XOY of the cylinder ...
62 gp_Ax3 Pos = S.Position();
63 gp_Pnt O = Pos.Location();
64 gp_Vec OZ (Pos.Direction());
65 Standard_Real V = gp_Vec(O,P).Dot(OZ);
66 gp_Pnt Pp = P.Translated(OZ.Multiplied(-V));
68 // Calculation of extrema
70 if (OPp.Magnitude() < Tol) { return; }
71 gp_Vec myZ = Pos.XDirection()^Pos.YDirection();
72 Standard_Real U1 = gp_Vec(Pos.XDirection()).AngleWithRef(OPp,myZ); //-M_PI<U1<M_PI
73 Standard_Real U2 = U1 + M_PI;
74 if (U1 < 0.) { U1 += 2. * M_PI; }
77 Ps = ElSLib::Value(U1,V,S);
78 mySqDist[0] = Ps.SquareDistance(P);
79 myPoint[0] = Extrema_POnSurf(U1,V,Ps);
80 Ps = ElSLib::Value(U2,V,S);
81 mySqDist[1] = Ps.SquareDistance(P);
82 myPoint[1] = Extrema_POnSurf(U2,V,Ps);
85 myDone = Standard_True;
87 //=============================================================================
89 Extrema_ExtPElS::Extrema_ExtPElS (const gp_Pnt& P,
91 const Standard_Real Tol)
95 /*-----------------------------------------------------------------------------
97 Find 2 extreme distances between point P and cone S.
100 Let M the top of the cone.
101 2 cases are considered:
102 1- distance(P,M) < Tol:
103 there is a minimum in M.
104 2- distance(P,M) > Tol:
105 Let Pp the projection of P in the plane XOY of the cone;
106 2 cases are considered:
107 1- distance(Pp,O) < Tol:
108 There is an infinite number of solutions; IsDone() = Standard_False.
109 2- distance(Pp,O) > Tol:
110 There exist 2 extrema:
111 Let Vm = value of v for point M,
112 Vp = value of v for point P,
113 U1 = angle(OX,OPp) if Vp > Vm )
114 -angle(OX,OPp) otherwise ) with 0. < U1 < 2*M_PI,
115 U2 = U1 + M_PI with 0. < U2 < 2*M_PI;
116 We are in plane PpOZ.
117 Let A the angle of the cone,
118 B = angle(MP,MO) with 0. < B < M_PI,
120 V1 = (L * cos(B-A)) + Vm,
121 V2 = (L * cos(B+A)) + Vm;
122 then (U1,V1) and (U2,V2) correspond to min distances.
123 -----------------------------------------------------------------------------*/
125 void Extrema_ExtPElS::Perform(const gp_Pnt& P,
127 const Standard_Real Tol)
129 myDone = Standard_False;
133 gp_Ax3 Pos = S.Position();
134 gp_Pnt O = Pos.Location();
135 Standard_Real A = S.SemiAngle();
136 gp_Vec OZ (Pos.Direction());
137 gp_Vec myZ = Pos.XDirection()^Pos.YDirection();
140 Standard_Real L2 = MP.SquareMagnitude();
141 Standard_Real Vm = -(S.RefRadius() / Sin(A));
143 // Case when P is mixed with S ...
144 if (L2 < Tol * Tol) {
146 myPoint[0] = Extrema_POnSurf(0.,Vm,M);
148 myDone = Standard_True;
152 if (M.SquareDistance(O)<Tol * Tol)
154 if( A<0) DirZ.Multiplied(-1.);
158 // Projection of P in the reference plane of the cone ...
159 Standard_Real Zp = gp_Vec(O, P).Dot(OZ);
161 gp_Pnt Pp = P.Translated(OZ.Multiplied(-Zp));
163 if (OPp.SquareMagnitude() < Tol * Tol) return;
164 Standard_Real B, U1, V1, U2, V2;
165 Standard_Boolean Same = DirZ.Dot(MP) >= 0.0;
166 U1 = gp_Vec(Pos.XDirection()).AngleWithRef(OPp,myZ); //-M_PI<U1<M_PI
168 if (!Same) { U1 += M_PI; }
170 if (U1 < 0.) { U1 += 2. * M_PI; }
171 if (U2 > 2.*M_PI) { U2 -= 2. * M_PI; }
174 Standard_Real L = sqrt(L2);
184 Standard_Real Sense = OZ.Dot(gp_Dir(DirZ));
185 V1 *= Sense; V2 *= Sense;
189 Ps = ElSLib::Value(U1,V1,S);
190 mySqDist[0] = Ps.SquareDistance(P);
191 myPoint[0] = Extrema_POnSurf(U1,V1,Ps);
192 Ps = ElSLib::Value(U2,V2,S);
193 mySqDist[1] = Ps.SquareDistance(P);
194 myPoint[1] = Extrema_POnSurf(U2,V2,Ps);
197 myDone = Standard_True;
199 //=============================================================================
201 Extrema_ExtPElS::Extrema_ExtPElS (const gp_Pnt& P,
203 const Standard_Real Tol)
207 /*-----------------------------------------------------------------------------
209 Find 2 extreme distances between point P and sphere S.
212 Let O be the origin of the sphere.
213 2 cases are considered:
214 1- distance(P,O) < Tol:
215 There is an infinite number of solutions; IsDone() = Standard_False
216 2- distance(P,O) > Tol:
217 Let Pp be the projection of point P in the plane XOY of the sphere;
218 2 cases are considered:
219 1- distance(Pp,O) < Tol:
220 2 solutions are: (0,-M_PI/2.) and (0.,M_PI/2.)
221 2- distance(Pp,O) > Tol:
222 Let U1 = angle(OX,OPp) with 0. < U1 < 2.*M_PI,
223 U2 = U1 + M_PI avec 0. < U2 < 2*M_PI,
224 V1 = angle(OPp,OP) with -M_PI/2. < V1 < M_PI/2. ,
225 then (U1, V1) corresponds to the min distance
226 and (U2,-V1) corresponds to the max distance.
227 -----------------------------------------------------------------------------*/
229 void Extrema_ExtPElS::Perform(const gp_Pnt& P,
231 const Standard_Real Tol)
233 myDone = Standard_False;
236 gp_Ax3 Pos = S.Position();
237 gp_Vec OP (Pos.Location(),P);
239 // Case when P is mixed with O ...
240 if (OP.SquareMagnitude() < Tol * Tol) { return; }
242 // Projection if P in plane XOY of the sphere ...
243 gp_Pnt O = Pos.Location();
244 gp_Vec OZ (Pos.Direction());
245 Standard_Real Zp = OP.Dot(OZ);
246 gp_Pnt Pp = P.Translated(OZ.Multiplied(-Zp));
248 // Calculation of extrema ...
250 Standard_Real U1, U2, V;
251 if (OPp.SquareMagnitude() < Tol * Tol) {
254 if (Zp < 0.) { V = -M_PI / 2.; }
255 else { V = M_PI / 2.; }
258 gp_Vec myZ = Pos.XDirection()^Pos.YDirection();
259 U1 = gp_Vec(Pos.XDirection()).AngleWithRef(OPp,myZ);
261 if (U1 < 0.) { U1 += 2. * M_PI; }
263 if (Zp < 0.) { V = -V; }
267 Ps = ElSLib::Value(U1,V,S);
268 mySqDist[0] = Ps.SquareDistance(P);
269 myPoint[0] = Extrema_POnSurf(U1,V,Ps);
270 Ps = ElSLib::Value(U2,-V,S);
271 mySqDist[1] = Ps.SquareDistance(P);
272 myPoint[1] = Extrema_POnSurf(U2,-V,Ps);
275 myDone = Standard_True;
277 //=============================================================================
279 Extrema_ExtPElS::Extrema_ExtPElS (const gp_Pnt& P,
281 const Standard_Real Tol)
285 /*-----------------------------------------------------------------------------
287 Find 2 extreme distances between point P and torus S.
290 Let Pp be the projection of point P in plane XOY of the torus;
291 2 cases are consideres:
292 1- distance(Pp,O) < Tol:
293 There is an infinite number of solutions; IsDone() = Standard_False.
294 2- distance(Pp,O) > Tol:
295 One is located in plane PpOZ;
296 Let V1 = angle(OX,OPp) with 0. < V1 < 2.*M_PI,
297 V2 = V1 + M_PI with 0. < V2 < 2.*M_PI,
298 O1 and O2 centers of circles (O1 on coord. posit.)
301 then (U1,V1) corresponds to the min distance
302 and (U2,V2) corresponds to the max distance.
303 -----------------------------------------------------------------------------*/
304 void Extrema_ExtPElS::Perform(const gp_Pnt& P,
306 const Standard_Real Tol)
308 myDone = Standard_False;
311 // Projection of P in plane XOY ...
312 gp_Ax3 Pos = S.Position();
313 gp_Pnt O = Pos.Location();
314 gp_Vec OZ (Pos.Direction());
315 gp_Pnt Pp = P.Translated(OZ.Multiplied(-(gp_Vec(O,P).Dot(Pos.Direction()))));
317 // Calculation of extrema ...
319 Standard_Real R2 = OPp.SquareMagnitude();
320 if (R2 < Tol * Tol) { return; }
322 gp_Vec myZ = Pos.XDirection()^Pos.YDirection();
323 Standard_Real U1 = gp_Vec(Pos.XDirection()).AngleWithRef(OPp,myZ);
324 Standard_Real U2 = U1 + M_PI;
325 if (U1 < 0.) { U1 += 2. * M_PI; }
326 Standard_Real R = sqrt(R2);
327 gp_Vec OO1 = OPp.Divided(R).Multiplied(S.MajorRadius());
328 gp_Vec OO2 = OO1.Multiplied(-1.);
329 gp_Pnt O1 = O.Translated(OO1);
330 gp_Pnt O2 = O.Translated(OO2);
332 if(O1.SquareDistance(P) < Tol) { return; }
333 if(O2.SquareDistance(P) < Tol) { return; }
335 Standard_Real V1 = OO1.AngleWithRef(gp_Vec(O1,P),OO1.Crossed(OZ));
336 Standard_Real V2 = OO2.AngleWithRef(gp_Vec(P,O2),OO2.Crossed(OZ));
337 if (V1 < 0.) { V1 += 2. * M_PI; }
338 if (V2 < 0.) { V2 += 2. * M_PI; }
341 Ps = ElSLib::Value(U1,V1,S);
342 mySqDist[0] = Ps.SquareDistance(P);
343 myPoint[0] = Extrema_POnSurf(U1,V1,Ps);
345 Ps = ElSLib::Value(U1,V1+M_PI,S);
346 mySqDist[1] = Ps.SquareDistance(P);
347 myPoint[1] = Extrema_POnSurf(U1,V1+M_PI,Ps);
349 Ps = ElSLib::Value(U2,V2,S);
350 mySqDist[2] = Ps.SquareDistance(P);
351 myPoint[2] = Extrema_POnSurf(U2,V2,Ps);
353 Ps = ElSLib::Value(U2,V2+M_PI,S);
354 mySqDist[3] = Ps.SquareDistance(P);
355 myPoint[3] = Extrema_POnSurf(U2,V2+M_PI,Ps);
358 myDone = Standard_True;
362 Extrema_ExtPElS::Extrema_ExtPElS (const gp_Pnt& P,
364 const Standard_Real Tol)
369 void Extrema_ExtPElS::Perform (const gp_Pnt& P,
371 // const Standard_Real Tol)
372 const Standard_Real )
374 myDone = Standard_False;
377 // Projection of point P in plane XOY of the cylinder ...
378 gp_Pnt O = S.Location();
379 gp_Vec OZ (S.Axis().Direction());
380 Standard_Real U, V = gp_Vec(O,P).Dot(OZ);
381 gp_Pnt Pp = P.Translated(OZ.Multiplied(-V));
383 ElSLib::Parameters(S, P, U, V);
384 mySqDist[0] = Pp.SquareDistance(P);
385 myPoint[0] = Extrema_POnSurf(U,V,Pp);
387 myDone = Standard_True;
391 //=============================================================================
393 Standard_Boolean Extrema_ExtPElS::IsDone () const { return myDone; }
394 //=============================================================================
396 Standard_Integer Extrema_ExtPElS::NbExt () const
398 if (!IsDone()) { StdFail_NotDone::Raise(); }
401 //=============================================================================
403 Standard_Real Extrema_ExtPElS::SquareDistance (const Standard_Integer N) const
405 if (!IsDone()) { StdFail_NotDone::Raise(); }
406 if ((N < 1) || (N > myNbExt)) { Standard_OutOfRange::Raise(); }
407 return mySqDist[N-1];
409 //=============================================================================
411 const Extrema_POnSurf& Extrema_ExtPElS::Point (const Standard_Integer N) const
413 if (!IsDone()) { StdFail_NotDone::Raise(); }
414 if ((N < 1) || (N > myNbExt)) { Standard_OutOfRange::Raise(); }
417 //=============================================================================