1 // Created on: 1995-07-18
2 // Created by: Modelistation
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
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.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
17 // Modified by skv - Thu Jul 7 12:29:34 2005 OCC9134
19 #include <Adaptor3d_Curve.hxx>
20 #include <Adaptor3d_Surface.hxx>
21 #include <Bnd_Box.hxx>
22 #include <BndLib_AddSurface.hxx>
25 #include <Extrema_ExtCS.hxx>
26 #include <Extrema_ExtPS.hxx>
27 #include <Extrema_GenExtCS.hxx>
28 #include <Extrema_POnCurv.hxx>
29 #include <Extrema_POnSurf.hxx>
30 #include <GeomAbs_CurveType.hxx>
31 #include <gp_Cone.hxx>
32 #include <gp_Cylinder.hxx>
36 #include <gp_Sphere.hxx>
37 #include <gp_Torus.hxx>
38 #include <Precision.hxx>
39 #include <Standard_OutOfRange.hxx>
40 #include <StdFail_NotDone.hxx>
41 #include <TColStd_Array1OfReal.hxx>
42 #include <Extrema_ExtPS.hxx>
44 Extrema_ExtCS::Extrema_ExtCS()
46 myDone(Standard_False),
47 myIsPar(Standard_False),
56 myStype(GeomAbs_OtherSurface)
60 Extrema_ExtCS::Extrema_ExtCS(const Adaptor3d_Curve& C,
61 const Adaptor3d_Surface& S,
62 const Standard_Real TolC,
63 const Standard_Real TolS)
66 Initialize(S, S.FirstUParameter(), S.LastUParameter(),
67 S.FirstVParameter(), S.LastVParameter(),
69 Perform(C, C.FirstParameter(), C.LastParameter());
72 Extrema_ExtCS::Extrema_ExtCS(const Adaptor3d_Curve& C,
73 const Adaptor3d_Surface& S,
74 const Standard_Real UCinf,
75 const Standard_Real UCsup,
76 const Standard_Real Uinf,
77 const Standard_Real Usup,
78 const Standard_Real Vinf,
79 const Standard_Real Vsup,
80 const Standard_Real TolC,
81 const Standard_Real TolS)
84 Initialize(S, Uinf, Usup, Vinf, Vsup, TolC, TolS);
85 Perform(C, UCinf, UCsup);
89 void Extrema_ExtCS::Initialize(const Adaptor3d_Surface& S,
90 const Standard_Real Uinf,
91 const Standard_Real Usup,
92 const Standard_Real Vinf,
93 const Standard_Real Vsup,
94 const Standard_Real TolC,
95 const Standard_Real TolS)
97 myS = (Adaptor3d_SurfacePtr)&S;
98 myIsPar = Standard_False;
105 myStype = myS->GetType();
109 void Extrema_ExtCS::Perform(const Adaptor3d_Curve& C,
110 const Standard_Real Uinf,
111 const Standard_Real Usup)
118 Standard_Integer i, j;
119 Standard_Integer NbT, NbU, NbV;
120 NbT = 12; NbU = NbV = 10;
121 GeomAbs_CurveType myCtype = C.GetType();
123 myDone = Standard_False;
124 // Try analytic computation of extrema
125 Standard_Boolean isComputeAnalytic = Standard_True;
134 myExtElCS.Perform(C.Line(), myS->Sphere());
136 case GeomAbs_Cylinder:
137 myExtElCS.Perform(C.Line(), myS->Cylinder());
140 myExtElCS.Perform(C.Line(), myS->Plane());
141 if (myExtElCS.IsParallel()) break;
146 case GeomAbs_BezierSurface:
147 case GeomAbs_BSplineSurface:
148 case GeomAbs_SurfaceOfRevolution:
149 case GeomAbs_SurfaceOfExtrusion:
150 case GeomAbs_OffsetSurface:
151 case GeomAbs_OtherSurface:
153 Standard_Real cfirst = myucinf, clast = myucsup;
154 Standard_Real ufirst = myS->FirstUParameter(), ulast = myS->LastUParameter(),
155 vfirst = myS->FirstVParameter(), vlast = myS->LastVParameter();
157 if (!(Precision::IsInfinite(ufirst) || Precision::IsInfinite(ulast) ||
158 Precision::IsInfinite(vfirst) || Precision::IsInfinite(vlast)))
160 Standard_Real tmin = Precision::Infinite(), tmax = -tmin;
161 Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
163 BndLib_AddSurface::Add(*myS, ufirst, ulast, vfirst, vlast, Precision::Confusion(), aSurfBox);
164 aSurfBox.Get(xmin, ymin, zmin, xmax, ymax, zmax);
165 gp_Lin aLin = C.Line();
166 Standard_Real aParOnLin;
167 gp_Pnt aLimPntArray[8];
169 aLimPntArray[0].SetCoord(xmin, ymin, zmin);
170 aLimPntArray[1].SetCoord(xmax, ymin, zmin);
171 aLimPntArray[2].SetCoord(xmin, ymax, zmin);
172 aLimPntArray[3].SetCoord(xmax, ymax, zmin);
173 aLimPntArray[4].SetCoord(xmin, ymin, zmax);
174 aLimPntArray[5].SetCoord(xmax, ymin, zmax);
175 aLimPntArray[6].SetCoord(xmin, ymax, zmax);
176 aLimPntArray[7].SetCoord(xmax, ymax, zmax);
178 for (i = 0; i <= 7; i++) {
179 aParOnLin = ElCLib::Parameter(aLin, aLimPntArray[i]);
180 tmin = Min(aParOnLin, tmin);
181 tmax = Max(aParOnLin, tmax);
183 cfirst = Max(cfirst, tmin);
184 clast = Min(clast, tmax);
187 if (myS->IsUPeriodic())
189 if (myS->IsVPeriodic())
192 if (clast - cfirst <= Precision::Confusion())
194 Standard_Real aCPar = (cfirst + clast) / 2.;
195 gp_Pnt aPm = C.Value(aCPar);
196 Extrema_ExtPS anExtPS(aPm, *myS, ufirst, ulast,
197 vfirst, vlast, mytolS, mytolS, Extrema_ExtFlag_MIN);
198 myDone = anExtPS.IsDone();
200 Standard_Integer NbExt = anExtPS.NbExt();
201 Standard_Real T = aCPar, U, V;
204 for (i = 1; i <= NbExt; i++) {
205 PS = anExtPS.Point(i);
207 AddSolution(C, T, U, V, PC.Value(), PS.Value(), anExtPS.SquareDistance(i));
213 Extrema_GenExtCS Ext(C, *myS, NbT, NbU, NbV, cfirst, clast, ufirst, ulast,
214 vfirst, vlast, mytolC, mytolS);
216 myDone = Ext.IsDone();
218 Standard_Integer NbExt = Ext.NbExt();
222 for (i = 1; i <= NbExt; i++) {
223 PC = Ext.PointOnCurve(i);
224 PS = Ext.PointOnSurface(i);
227 AddSolution(C, T, U, V, PC.Value(), PS.Value(), Ext.SquareDistance(i));
235 // Modified by skv - Thu Jul 7 12:29:34 2005 OCC9134 Begin
238 if(myStype == GeomAbs_Cylinder) {
239 myExtElCS.Perform(C.Circle(), myS->Cylinder());
242 else if(myStype == GeomAbs_Plane)
244 myExtElCS.Perform(C.Circle(), myS->Plane());
247 else if (myStype == GeomAbs_Sphere)
249 myExtElCS.Perform(C.Circle(), myS->Sphere());
254 case GeomAbs_Hyperbola:
256 if(myCtype == GeomAbs_Hyperbola && myStype == GeomAbs_Plane) {
257 // Modified by skv - Thu Jul 7 12:29:34 2005 OCC9134 End
258 myExtElCS.Perform(C.Hyperbola(), myS->Plane());
265 isComputeAnalytic = Standard_False;
270 if (isComputeAnalytic)
272 if (myExtElCS.IsDone())
274 myDone = Standard_True;
275 myIsPar = myExtElCS.IsParallel();
278 mySqDist.Append(myExtElCS.SquareDistance(1));
282 Standard_Integer NbExt = myExtElCS.NbExt();
283 for (i = 1; i <= NbExt; i++)
287 myExtElCS.Points(i, PC, PS);
288 Standard_Real Ucurve = PC.Parameter();
291 AddSolution(C, Ucurve, U, V, PC.Value(), PS.Value(), myExtElCS.SquareDistance(i));
294 if (mySqDist.Length() == 0 && NbExt > 0)
296 // Analytical extrema seem to be out of curve/surface boundaries.
297 // Try extremity points of curve.
298 gp_Pnt aPOnC[2], aPOnS[2];
299 Standard_Real aT[2] = { myucinf, myucsup }, U[2], V[2];
300 Standard_Real aDist[2] = { -1, -1 };
301 for (i = 0; i < 2; ++i)
303 if (Precision::IsInfinite(aT[i]))
306 aPOnC[i] = C.Value(aT[i]);
311 ElSLib::Parameters(myS->Plane(), aPOnC[i], U[i], V[i]);
312 aPOnS[i] = ElSLib::Value(U[i], V[i], myS->Plane());
317 ElSLib::Parameters(myS->Sphere(), aPOnC[i], U[i], V[i]);
318 aPOnS[i] = ElSLib::Value(U[i], V[i], myS->Sphere());
321 case GeomAbs_Cylinder:
323 ElSLib::Parameters(myS->Cylinder(), aPOnC[i], U[i], V[i]);
324 aPOnS[i] = ElSLib::Value(U[i], V[i], myS->Cylinder());
329 ElSLib::Parameters(myS->Torus(), aPOnC[i], U[i], V[i]);
330 aPOnS[i] = ElSLib::Value(U[i], V[i], myS->Torus());
335 ElSLib::Parameters(myS->Cone(), aPOnC[i], U[i], V[i]);
336 aPOnS[i] = ElSLib::Value(U[i], V[i], myS->Cone());
343 aDist[i] = aPOnC[i].SquareDistance(aPOnS[i]);
346 Standard_Boolean bAdd[2] = {Standard_False, Standard_False};
348 // Choose solution to add
349 if (aDist[0] >= 0. && aDist[1] >= 0.)
351 Standard_Real aDiff = aDist[0] - aDist[1];
352 // Both computed -> take only minimal
353 if (Abs(aDiff) < Precision::Confusion())
355 bAdd[0] = bAdd[1] = Standard_True;
358 bAdd[0] = Standard_True;
361 bAdd[1] = Standard_True;
363 else if (aDist[0] >= 0.)
365 bAdd[0] = Standard_True;
366 else if (aDist[1] >= 0.)
368 bAdd[1] = Standard_True;
370 for (i = 0; i < 2; ++i)
373 AddSolution(C, aT[i], U[i], V[i], aPOnC[i], aPOnS[i], aDist[i]);
381 // Elementary extrema is not done, try generic solution
382 Extrema_GenExtCS Ext;
383 Ext.Initialize(*myS, NbU, NbV, mytolS);
384 if (myCtype == GeomAbs_Hyperbola) {
385 Standard_Real tmin = Max(-20., C.FirstParameter());
386 Standard_Real tmax = Min(20., C.LastParameter());
387 Ext.Perform(C, NbT, tmin, tmax, mytolC); // to avoid overflow
390 if ((myCtype == GeomAbs_Circle && NbT < 13) ||
391 (myCtype == GeomAbs_BSplineCurve && NbT < 13))
395 Ext.Perform(C, NbT, mytolC);
398 myDone = Ext.IsDone();
400 Standard_Integer NbExt = Ext.NbExt();
401 Standard_Real T, U, V;
404 for (i = 1; i <= NbExt; i++) {
405 PC = Ext.PointOnCurve(i);
406 PS = Ext.PointOnSurface(i);
409 AddSolution(C, T, U, V, PC.Value(), PS.Value(), Ext.SquareDistance(i));
413 Standard_Integer SolNumber = mySqDist.Length();
414 Standard_Address CopyC = (Standard_Address)&C;
415 Adaptor3d_Curve& aC = *(Adaptor3d_Curve*)CopyC;
416 Standard_Integer NbIntervals = aC.NbIntervals(GeomAbs_C1);
417 TColStd_Array1OfReal SharpPoints(1, NbIntervals + 1);
418 aC.Intervals(SharpPoints, GeomAbs_C1);
420 Extrema_ExtPS aProjPS;
421 aProjPS.Initialize(*myS,
422 myS->FirstUParameter(),
423 myS->LastUParameter(),
424 myS->FirstVParameter(),
425 myS->LastVParameter(),
429 for (i = 2; i < SharpPoints.Upper(); ++i)
432 gp_Pnt aPnt = C.Value(T);
433 aProjPS.Perform(aPnt);
434 if (!aProjPS.IsDone())
436 Standard_Integer NbProj = aProjPS.NbExt(), jmin = 0;
437 Standard_Real MinSqDist = RealLast();
438 for (j = 1; j <= NbProj; j++)
440 Standard_Real aSqDist = aProjPS.SquareDistance(j);
441 if (aSqDist < MinSqDist)
449 aProjPS.Point(jmin).Parameter(U, V);
450 AddSolution(C, T, U, V,
451 aPnt, aProjPS.Point(jmin).Value(), MinSqDist);
454 //Cut sharp solutions to keep only minimum and maximum
455 Standard_Integer imin = SolNumber + 1, imax = mySqDist.Length();
456 for (i = SolNumber + 1; i <= mySqDist.Length(); i++)
458 if (mySqDist(i) < mySqDist(imin))
460 if (mySqDist(i) > mySqDist(imax))
463 if (mySqDist.Length() > SolNumber + 2)
465 Standard_Real MinSqDist = mySqDist(imin);
466 Standard_Real MaxSqDist = mySqDist(imax);
467 Extrema_POnCurv MinPC = myPOnC(imin);
468 Extrema_POnCurv MaxPC = myPOnC(imax);
469 Extrema_POnSurf MinPS = myPOnS(imin);
470 Extrema_POnSurf MaxPS = myPOnS(imax);
472 mySqDist.Remove(SolNumber + 1, mySqDist.Length());
473 myPOnC.Remove(SolNumber + 1, myPOnC.Length());
474 myPOnS.Remove(SolNumber + 1, myPOnS.Length());
476 mySqDist.Append(MinSqDist);
477 myPOnC.Append(MinPC);
478 myPOnS.Append(MinPS);
479 mySqDist.Append(MaxSqDist);
480 myPOnC.Append(MaxPC);
481 myPOnS.Append(MaxPS);
487 Standard_Boolean Extrema_ExtCS::IsDone() const
492 Standard_Boolean Extrema_ExtCS::IsParallel() const
496 throw StdFail_NotDone();
503 Standard_Real Extrema_ExtCS::SquareDistance(const Standard_Integer N) const
505 if (N < 1 || N > NbExt())
507 throw Standard_OutOfRange();
510 return mySqDist.Value(N);
514 Standard_Integer Extrema_ExtCS::NbExt() const
518 throw StdFail_NotDone();
521 return mySqDist.Length();
526 void Extrema_ExtCS::Points(const Standard_Integer N,
528 Extrema_POnSurf& P2) const
530 if (N < 1 || N > NbExt())
532 throw Standard_OutOfRange();
535 P1 = myPOnC.Value(N);
536 P2 = myPOnS.Value(N);
539 Standard_Boolean Extrema_ExtCS::AddSolution(const Adaptor3d_Curve& theCurve,
540 const Standard_Real aT,
541 const Standard_Real aU,
542 const Standard_Real aV,
543 const gp_Pnt& PointOnCurve,
544 const gp_Pnt& PointOnSurf,
545 const Standard_Real SquareDist)
547 Standard_Boolean Added = Standard_False;
549 Standard_Real T = aT, U = aU, V = aV;
551 if (theCurve.IsPeriodic())
552 T = ElCLib::InPeriod(T, myucinf, myucinf + theCurve.Period());
553 if (myS->IsUPeriodic())
554 U = ElCLib::InPeriod(U, myuinf, myuinf + myS->UPeriod());
555 if (myS->IsVPeriodic())
556 V = ElCLib::InPeriod(V, myvinf, myvinf + myS->VPeriod());
560 if ((myucinf-T) <= mytolC && (T-myucsup) <= mytolC &&
561 (myuinf-U) <= mytolS && (U-myusup) <= mytolS &&
562 (myvinf-V) <= mytolS && (V-myvsup) <= mytolS)
564 Standard_Boolean IsNewSolution = Standard_True;
565 for (Standard_Integer j = 1; j <= mySqDist.Length(); j++)
569 Standard_Real Tj = aPC.Parameter();
570 Standard_Real Uj, Vj;
571 aPS.Parameter(Uj, Vj);
572 if (Abs(T - Tj) <= mytolC &&
573 Abs(U - Uj) <= mytolS &&
574 Abs(V - Vj) <= mytolS)
576 IsNewSolution = Standard_False;
582 mySqDist.Append(SquareDist);
583 aPC.SetValues(T, PointOnCurve);
585 myPOnS.Append(Extrema_POnSurf(U, V, PointOnSurf));
586 Added = Standard_True;