1 // Created on: 1995-07-18
2 // Created by: Modelistation
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
22 // Modified by skv - Thu Jul 7 12:29:34 2005 OCC9134
24 #include <Extrema_ExtCS.ixx>
25 #include <Extrema_GenExtCS.hxx>
26 #include <StdFail_NotDone.hxx>
27 #include <Standard_NotImplemented.hxx>
28 #include <StdFail_InfiniteSolutions.hxx>
29 #include <Precision.hxx>
30 #include <GeomAbs_CurveType.hxx>
33 #include <gp_Cylinder.hxx>
34 #include <gp_Cone.hxx>
35 #include <gp_Sphere.hxx>
36 #include <gp_Torus.hxx>
40 #include <Extrema_ExtPElC.hxx>
41 #include <Bnd_Box.hxx>
42 #include <BndLib_AddSurface.hxx>
43 #include <Extrema_ExtPElS.hxx>
44 #include <TColStd_Array1OfReal.hxx>
45 #include <Extrema_ExtPS.hxx>
47 Extrema_ExtCS::Extrema_ExtCS()
49 myDone = Standard_False;
52 Extrema_ExtCS::Extrema_ExtCS(const Adaptor3d_Curve& C,
53 const Adaptor3d_Surface& S,
54 const Standard_Real TolC,
55 const Standard_Real TolS)
58 Initialize(S, S.FirstUParameter(), S.LastUParameter(),
59 S.FirstVParameter(), S.LastVParameter(),
61 Perform(C, C.FirstParameter(), C.LastParameter());
64 Extrema_ExtCS::Extrema_ExtCS(const Adaptor3d_Curve& C,
65 const Adaptor3d_Surface& S,
66 const Standard_Real UCinf,
67 const Standard_Real UCsup,
68 const Standard_Real Uinf,
69 const Standard_Real Usup,
70 const Standard_Real Vinf,
71 const Standard_Real Vsup,
72 const Standard_Real TolC,
73 const Standard_Real TolS)
76 Initialize(S, Uinf, Usup, Vinf, Vsup, TolC, TolS);
77 Perform(C, UCinf, UCsup);
81 void Extrema_ExtCS::Initialize(const Adaptor3d_Surface& S,
82 const Standard_Real Uinf,
83 const Standard_Real Usup,
84 const Standard_Real Vinf,
85 const Standard_Real Vsup,
86 const Standard_Real TolC,
87 const Standard_Real TolS)
89 myS = (Adaptor3d_SurfacePtr)&S;
90 myIsPar = Standard_False;
97 myStype = myS->GetType();
101 void Extrema_ExtCS::Perform(const Adaptor3d_Curve& C,
102 const Standard_Real Uinf,
103 const Standard_Real Usup)
110 Standard_Integer i, j;
111 Standard_Integer NbT, NbU, NbV;
112 NbT = NbU = NbV = 10;
113 GeomAbs_CurveType myCtype = C.GetType();
123 myExtElCS.Perform(C.Line(), myS->Sphere());
125 case GeomAbs_Cylinder:
126 myExtElCS.Perform(C.Line(), myS->Cylinder());
129 myExtElCS.Perform(C.Line(), myS->Plane());
130 if (myExtElCS.IsParallel()) break;
134 case GeomAbs_BezierSurface:
135 case GeomAbs_BSplineSurface:
136 case GeomAbs_SurfaceOfRevolution:
137 case GeomAbs_SurfaceOfExtrusion:
138 case GeomAbs_OtherSurface:
140 Standard_Real cfirst = myucinf, clast = myucsup;
141 Standard_Real ufirst = myS->FirstUParameter(), ulast = myS->LastUParameter(),
142 vfirst = myS->FirstVParameter(), vlast = myS->LastVParameter();
144 if(Precision::IsInfinite(Abs(cfirst)) || Precision::IsInfinite(Abs(clast))) {
147 BndLib_AddSurface::Add(*myS, ufirst, ulast, vfirst, vlast, Precision::Confusion(), aSurfBox);
148 Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
149 aSurfBox.Get(xmin, ymin, zmin, xmax, ymax, zmax);
150 Standard_Real tmin = Precision::Infinite(), tmax = -tmin;
151 gp_Lin aLin = C.Line();
154 if(!( Precision::IsInfinite(Abs(xmin)) || Precision::IsInfinite(Abs(xmax)) ||
155 Precision::IsInfinite(Abs(ymin)) || Precision::IsInfinite(Abs(ymax)) ||
156 Precision::IsInfinite(Abs(zmin)) || Precision::IsInfinite(Abs(zmax))) ) {
158 Extrema_ExtPElC anExt;
159 Extrema_POnCurv aPntOnLin;
160 Standard_Real aParOnLin;
161 Standard_Real lim = Precision::Infinite();
162 gp_Pnt aLimPntArray[8];
164 aLimPntArray[0].SetCoord(xmin, ymin, zmin);
165 aLimPntArray[1].SetCoord(xmax, ymin, zmin);
166 aLimPntArray[2].SetCoord(xmin, ymax, zmin);
167 aLimPntArray[3].SetCoord(xmax, ymax, zmin);
168 aLimPntArray[4].SetCoord(xmin, ymin, zmax);
169 aLimPntArray[5].SetCoord(xmax, ymin, zmax);
170 aLimPntArray[6].SetCoord(xmin, ymax, zmax);
171 aLimPntArray[7].SetCoord(xmax, ymax, zmax);
173 for(i = 0; i <= 7; i++) {
174 anExt.Perform(aLimPntArray[i], aLin, Precision::Confusion(), -lim, lim);
175 aPntOnLin = anExt.Point(1);
176 aParOnLin = aPntOnLin.Parameter();
177 tmin = Min(aParOnLin, tmin);
178 tmax = Max(aParOnLin, tmax);
188 cfirst = Max(cfirst, tmin);
189 clast = Min(clast, tmax);
195 Extrema_GenExtCS Ext(C, *myS, NbT, NbU, NbV, cfirst, clast, ufirst, ulast,
196 vfirst, vlast, mytolC, mytolS);
198 myDone = Ext.IsDone();
200 Standard_Integer NbExt = Ext.NbExt();
204 for (i = 1; i <= NbExt; i++) {
205 PC = Ext.PointOnCurve(i);
206 PS = Ext.PointOnSurface(i);
209 AddSolution(C, T, U, V, PC.Value(), PS.Value(), Ext.SquareDistance(i));
222 // Modified by skv - Thu Jul 7 12:29:34 2005 OCC9134 Begin
225 if(myStype == GeomAbs_Cylinder) {
226 myExtElCS.Perform(C.Circle(), myS->Cylinder());
230 case GeomAbs_Hyperbola:
232 if(myCtype == GeomAbs_Hyperbola && myStype == GeomAbs_Plane) {
233 // Modified by skv - Thu Jul 7 12:29:34 2005 OCC9134 End
234 myExtElCS.Perform(C.Hyperbola(), myS->Plane());
240 Extrema_GenExtCS Ext;
241 Ext.Initialize(*myS, NbU, NbV, mytolS);
242 if(myCtype == GeomAbs_Hyperbola) {
243 Standard_Real tmin = Max(-20., C.FirstParameter());
244 Standard_Real tmax = Min(20., C.LastParameter());
245 Ext.Perform(C, NbT, tmin, tmax, mytolC); // to avoid overflow
248 if(myCtype == GeomAbs_Circle && NbT < 13) {
251 Ext.Perform(C, NbT, mytolC);
254 myDone = Ext.IsDone();
256 Standard_Integer NbExt = Ext.NbExt();
260 for (i = 1; i <= NbExt; i++) {
261 PC = Ext.PointOnCurve(i);
262 PS = Ext.PointOnSurface(i);
265 AddSolution(C, T, U, V, PC.Value(), PS.Value(), Ext.SquareDistance(i));
269 Standard_Integer SolNumber = mySqDist.Length();
270 Standard_Address CopyC = (Standard_Address)&C;
271 Adaptor3d_Curve& aC = *(Adaptor3d_Curve*)CopyC;
272 Standard_Integer NbIntervals = aC.NbIntervals(GeomAbs_C1);
273 TColStd_Array1OfReal SharpPoints(1, NbIntervals+1);
274 aC.Intervals(SharpPoints, GeomAbs_C1);
275 for (i = 1; i <= SharpPoints.Upper(); i++)
278 gp_Pnt aPnt = C.Value(T);
279 Extrema_ExtPS ProjPS(aPnt, *myS, mytolS, mytolS);
280 if (!ProjPS.IsDone())
282 Standard_Integer NbProj = ProjPS.NbExt(), jmin = 0;
283 Standard_Real MinSqDist = RealLast();
284 for (j = 1; j <= NbProj; j++)
286 Standard_Real aSqDist = ProjPS.SquareDistance(j);
287 if (aSqDist < MinSqDist)
295 ProjPS.Point(jmin).Parameter(U,V);
296 AddSolution(C, T, U, V,
297 aPnt, ProjPS.Point(jmin).Value(), MinSqDist);
300 //Cut sharp solutions to keep only minimum and maximum
301 Standard_Integer imin = SolNumber + 1, imax = mySqDist.Length();
302 for (i = SolNumber + 1; i <= mySqDist.Length(); i++)
304 if (mySqDist(i) < mySqDist(imin))
306 if (mySqDist(i) > mySqDist(imax))
309 if (mySqDist.Length() > SolNumber + 2)
311 Standard_Real MinSqDist = mySqDist(imin);
312 Standard_Real MaxSqDist = mySqDist(imax);
313 Extrema_POnCurv MinPC = myPOnC(imin);
314 Extrema_POnCurv MaxPC = myPOnC(imax);
315 Extrema_POnSurf MinPS = myPOnS(imin);
316 Extrema_POnSurf MaxPS = myPOnS(imax);
318 mySqDist.Remove(SolNumber + 1, mySqDist.Length());
319 myPOnC.Remove(SolNumber + 1, myPOnC.Length());
320 myPOnS.Remove(SolNumber + 1, myPOnS.Length());
322 mySqDist.Append(MinSqDist);
323 myPOnC.Append(MinPC);
324 myPOnS.Append(MinPS);
325 mySqDist.Append(MaxSqDist);
326 myPOnC.Append(MaxPC);
327 myPOnS.Append(MaxPS);
335 myDone = myExtElCS.IsDone();
337 myIsPar = myExtElCS.IsParallel();
339 mySqDist.Append(myExtElCS.SquareDistance(1));
342 Standard_Integer NbExt = myExtElCS.NbExt();
344 for (i = 1; i <= NbExt; i++) {
347 myExtElCS.Points(i, PC, PS);
348 Standard_Real Ucurve = PC.Parameter();
350 AddSolution(C, Ucurve, U, V, PC.Value(), PS.Value(), myExtElCS.SquareDistance(i));
358 Standard_Boolean Extrema_ExtCS::IsDone() const
363 Standard_Boolean Extrema_ExtCS::IsParallel() const
369 Standard_Real Extrema_ExtCS::SquareDistance(const Standard_Integer N) const
371 if(!myDone) StdFail_NotDone::Raise();
372 if (myIsPar && N != 1) StdFail_InfiniteSolutions::Raise();
373 if ((N < 1) || (N > mySqDist.Length())) Standard_OutOfRange::Raise();
374 return mySqDist.Value(N);
378 Standard_Integer Extrema_ExtCS::NbExt() const
380 if(!myDone) StdFail_NotDone::Raise();
381 return mySqDist.Length();
386 void Extrema_ExtCS::Points(const Standard_Integer N,
388 Extrema_POnSurf& P2) const
390 if(!myDone) StdFail_NotDone::Raise();
391 P1 = myPOnC.Value(N);
392 P2 = myPOnS.Value(N);
395 Standard_Boolean Extrema_ExtCS::AddSolution(const Adaptor3d_Curve& theCurve,
396 const Standard_Real aT,
397 const Standard_Real aU,
398 const Standard_Real aV,
399 const gp_Pnt& PointOnCurve,
400 const gp_Pnt& PointOnSurf,
401 const Standard_Real SquareDist)
403 Standard_Boolean Added = Standard_False;
405 Standard_Real T = aT, U = aU, V = aV;
407 if (theCurve.IsPeriodic())
408 T = ElCLib::InPeriod(T, myucinf, myucinf + theCurve.Period());
409 if (myS->IsUPeriodic())
410 U = ElCLib::InPeriod(U, myuinf, myuinf + myS->UPeriod());
411 if (myS->IsVPeriodic())
412 V = ElCLib::InPeriod(V, myvinf, myvinf + myS->VPeriod());
416 if ((myucinf-T) <= mytolC && (T-myucsup) <= mytolC &&
417 (myuinf-U) <= mytolS && (U-myusup) <= mytolS &&
418 (myvinf-V) <= mytolS && (V-myvsup) <= mytolS)
420 Standard_Boolean IsNewSolution = Standard_True;
421 for (Standard_Integer j = 1; j <= mySqDist.Length(); j++)
425 Standard_Real Tj = aPC.Parameter();
426 Standard_Real Uj, Vj;
427 aPS.Parameter(Uj, Vj);
428 if (Abs(T - Tj) <= mytolC &&
429 Abs(U - Uj) <= mytolS &&
430 Abs(V - Vj) <= mytolS)
432 IsNewSolution = Standard_False;
438 mySqDist.Append(SquareDist);
439 aPC.SetValues(T, PointOnCurve);
441 myPOnS.Append(Extrema_POnSurf(U, V, PointOnSurf));
442 Added = Standard_True;