1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
15 #include <Bnd_Box.hxx>
16 #include <BndLib_Add3dCurve.hxx>
17 #include <BndLib_AddSurface.hxx>
18 #include <BRep_Tool.hxx>
19 #include <BRepAdaptor_Curve.hxx>
20 #include <BRepAdaptor_HCurve.hxx>
21 #include <BRepAdaptor_HSurface.hxx>
22 #include <BRepAdaptor_Surface.hxx>
25 #include <Extrema_ExtCS.hxx>
26 #include <Extrema_ExtPS.hxx>
27 #include <Extrema_GenExtCS.hxx>
28 #include <Extrema_GenLocateExtPS.hxx>
29 #include <Extrema_POnCurv.hxx>
30 #include <Extrema_POnSurf.hxx>
31 #include <Geom_BSplineSurface.hxx>
32 #include <Geom_Curve.hxx>
33 #include <Geom_Surface.hxx>
34 #include <GeomAdaptor_Curve.hxx>
35 #include <GeomAdaptor_Surface.hxx>
36 #include <GeomAPI_ProjectPointOnCurve.hxx>
37 #include <GeomInt.hxx>
38 #include <IntAna_QuadQuadGeo.hxx>
39 #include <IntCurveSurface_HInter.hxx>
40 #include <IntCurveSurface_IntersectionPoint.hxx>
41 #include <IntCurveSurface_IntersectionSegment.hxx>
42 #include <IntTools.hxx>
43 #include <IntTools_BeanFaceIntersector.hxx>
44 #include <IntTools_Context.hxx>
45 #include <IntTools_CurveRangeLocalizeData.hxx>
46 #include <IntTools_CurveRangeSample.hxx>
47 #include <IntTools_CArray1OfReal.hxx>
48 #include <IntTools_ListIteratorOfListOfBox.hxx>
49 #include <IntTools_ListIteratorOfListOfCurveRangeSample.hxx>
50 #include <IntTools_ListIteratorOfListOfSurfaceRangeSample.hxx>
51 #include <IntTools_ListOfBox.hxx>
52 #include <IntTools_ListOfCurveRangeSample.hxx>
53 #include <IntTools_ListOfSurfaceRangeSample.hxx>
54 #include <IntTools_MapIteratorOfMapOfCurveSample.hxx>
55 #include <IntTools_Root.hxx>
56 #include <IntTools_SurfaceRangeLocalizeData.hxx>
57 #include <IntTools_SurfaceRangeSample.hxx>
58 #include <IntTools_Tools.hxx>
59 #include <Precision.hxx>
60 #include <TColgp_Array1OfPnt2d.hxx>
61 #include <TColStd_Array1OfBoolean.hxx>
62 #include <TColStd_Array1OfReal.hxx>
63 #include <TColStd_ListIteratorOfListOfInteger.hxx>
64 #include <TColStd_ListOfInteger.hxx>
65 #include <TopoDS_Edge.hxx>
66 #include <TopoDS_Face.hxx>
68 static Standard_Boolean SetEmptyResultRange(const Standard_Real theParameter,
69 IntTools_MarkedRangeSet& theMarkedRange);
73 static Bnd_Box GetSurfaceBox
74 (const Handle(Geom_BSplineSurface) &theSurf,
75 const Standard_Real theFirstU,
76 const Standard_Real theLastU,
77 const Standard_Real theFirstV,
78 const Standard_Real theLastV,
79 const Standard_Real theTolerance,
80 IntTools_SurfaceRangeLocalizeData &theSurfaceData);
82 static void ComputeGridPoints
83 (const Handle(Geom_BSplineSurface) &theSurf,
84 const Standard_Real theFirstU,
85 const Standard_Real theLastU,
86 const Standard_Real theFirstV,
87 const Standard_Real theLastV,
88 const Standard_Real theTolerance,
89 IntTools_SurfaceRangeLocalizeData &theSurfaceData);
91 static void BuildBox(const Handle(Geom_BSplineSurface) &theSurf,
92 const Standard_Real theFirstU,
93 const Standard_Real theLastU,
94 const Standard_Real theFirstV,
95 const Standard_Real theLastV,
96 IntTools_SurfaceRangeLocalizeData &theSurfaceData,
99 static void MergeSolutions(const IntTools_ListOfCurveRangeSample& theListCurveRange,
100 const IntTools_ListOfSurfaceRangeSample& theListSurfaceRange,
101 IntTools_ListOfCurveRangeSample& theListCurveRangeSort,
102 IntTools_ListOfSurfaceRangeSample& theListSurfaceRangeSort);
104 static void CheckSampling(const IntTools_CurveRangeSample& theCurveRange,
105 const IntTools_SurfaceRangeSample& theSurfaceRange,
106 const IntTools_CurveRangeLocalizeData& theCurveData,
107 const IntTools_SurfaceRangeLocalizeData& theSurfaceData,
108 const Standard_Real DiffC,
109 const Standard_Real DiffU,
110 const Standard_Real DiffV,
111 Standard_Boolean& bAllowSamplingC,
112 Standard_Boolean& bAllowSamplingU,
113 Standard_Boolean& bAllowSamplingV);
115 // ==================================================================================
116 // function: IntTools_BeanFaceIntersector
118 // ==================================================================================
119 IntTools_BeanFaceIntersector::IntTools_BeanFaceIntersector() :
120 myFirstParameter(0.),
128 myIsDone(Standard_False)
130 myCriteria = Precision::Confusion();
131 myCurveResolution = Precision::PConfusion();
135 // ==================================================================================
136 // function: IntTools_BeanFaceIntersector
138 // ==================================================================================
139 IntTools_BeanFaceIntersector::IntTools_BeanFaceIntersector(const TopoDS_Edge& theEdge,
140 const TopoDS_Face& theFace) :
141 myFirstParameter(0.),
149 myIsDone(Standard_False)
151 Init(theEdge, theFace);
154 // ==================================================================================
155 // function: IntTools_BeanFaceIntersector
157 // ==================================================================================
158 IntTools_BeanFaceIntersector::IntTools_BeanFaceIntersector(const BRepAdaptor_Curve& theCurve,
159 const BRepAdaptor_Surface& theSurface,
160 const Standard_Real theBeanTolerance,
161 const Standard_Real theFaceTolerance) :
162 myFirstParameter(0.),
168 myIsDone(Standard_False)
170 Init(theCurve, theSurface, theBeanTolerance, theFaceTolerance);
173 // ==================================================================================
174 // function: IntTools_BeanFaceIntersector
176 // ==================================================================================
177 IntTools_BeanFaceIntersector::IntTools_BeanFaceIntersector(const BRepAdaptor_Curve& theCurve,
178 const BRepAdaptor_Surface& theSurface,
179 const Standard_Real theFirstParOnCurve,
180 const Standard_Real theLastParOnCurve,
181 const Standard_Real theUMinParameter,
182 const Standard_Real theUMaxParameter,
183 const Standard_Real theVMinParameter,
184 const Standard_Real theVMaxParameter,
185 const Standard_Real theBeanTolerance,
186 const Standard_Real theFaceTolerance) :
187 myFirstParameter(theFirstParOnCurve),
188 myLastParameter(theLastParOnCurve),
189 myUMinParameter(theUMinParameter),
190 myUMaxParameter(theUMaxParameter),
191 myVMinParameter(theVMinParameter),
192 myVMaxParameter(theVMaxParameter),
193 myBeanTolerance(theBeanTolerance),
194 myFaceTolerance(theFaceTolerance),
195 myIsDone(Standard_False)
199 myCriteria = myBeanTolerance + myFaceTolerance;
200 myCurveResolution = myCurve.Resolution(myCriteria);
202 mySurface = theSurface;
203 myTrsfSurface = Handle(Geom_Surface)::DownCast(mySurface.Surface().Surface()->Transformed(mySurface.Trsf()));
206 // ==================================================================================
209 // ==================================================================================
210 void IntTools_BeanFaceIntersector::Init(const TopoDS_Edge& theEdge,
211 const TopoDS_Face& theFace)
213 if (myContext.IsNull()) {
214 myContext = new IntTools_Context;
217 myCurve.Initialize(theEdge);
218 mySurface = myContext->SurfaceAdaptor(theFace);
219 myTrsfSurface = Handle(Geom_Surface)::DownCast(mySurface.Surface().Surface()->Transformed(mySurface.Trsf()));
220 myBeanTolerance = BRep_Tool::Tolerance(theEdge);
221 myFaceTolerance = BRep_Tool::Tolerance(theFace);
223 myCriteria = myBeanTolerance + myFaceTolerance + Precision::Confusion();
224 myCurveResolution = myCurve.Resolution(myCriteria);
226 SetSurfaceParameters(mySurface.FirstUParameter(), mySurface.LastUParameter(),
227 mySurface.FirstVParameter(), mySurface.LastVParameter());
231 // ==================================================================================
234 // ==================================================================================
235 void IntTools_BeanFaceIntersector::Init(const BRepAdaptor_Curve& theCurve,
236 const BRepAdaptor_Surface& theSurface,
237 const Standard_Real theBeanTolerance,
238 const Standard_Real theFaceTolerance)
241 mySurface = theSurface;
242 myTrsfSurface = Handle(Geom_Surface)::DownCast(mySurface.Surface().Surface()->Transformed(mySurface.Trsf()));
243 myBeanTolerance = theBeanTolerance;
244 myFaceTolerance = theFaceTolerance;
246 myCriteria = myBeanTolerance + myFaceTolerance;
247 myCurveResolution = myCurve.Resolution(myCriteria);
249 SetSurfaceParameters(mySurface.FirstUParameter(), mySurface.LastUParameter(),
250 mySurface.FirstVParameter(), mySurface.LastVParameter());
254 // ==================================================================================
257 // ==================================================================================
258 void IntTools_BeanFaceIntersector::Init(const BRepAdaptor_Curve& theCurve,
259 const BRepAdaptor_Surface& theSurface,
260 const Standard_Real theFirstParOnCurve,
261 const Standard_Real theLastParOnCurve,
262 const Standard_Real theUMinParameter,
263 const Standard_Real theUMaxParameter,
264 const Standard_Real theVMinParameter,
265 const Standard_Real theVMaxParameter,
266 const Standard_Real theBeanTolerance,
267 const Standard_Real theFaceTolerance)
269 Init(theCurve, theSurface, theBeanTolerance, theFaceTolerance);
270 SetBeanParameters(theFirstParOnCurve, theLastParOnCurve);
271 SetSurfaceParameters(theUMinParameter, theUMaxParameter, theVMinParameter, theVMaxParameter);
274 // ==================================================================================
275 // function: SetContext
277 // ==================================================================================
278 void IntTools_BeanFaceIntersector::SetContext(const Handle(IntTools_Context)& theContext)
280 myContext = theContext;
282 // ==================================================================================
285 // ==================================================================================
286 const Handle(IntTools_Context)& IntTools_BeanFaceIntersector::Context()const
291 // ==================================================================================
292 // function: SetBeanParameters
294 // ==================================================================================
295 void IntTools_BeanFaceIntersector::SetBeanParameters(const Standard_Real theFirstParOnCurve,
296 const Standard_Real theLastParOnCurve)
298 myFirstParameter = theFirstParOnCurve;
299 myLastParameter = theLastParOnCurve;
302 // ==================================================================================
303 // function: SetSurfaceParameters
305 // ==================================================================================
306 void IntTools_BeanFaceIntersector::SetSurfaceParameters(const Standard_Real theUMinParameter,
307 const Standard_Real theUMaxParameter,
308 const Standard_Real theVMinParameter,
309 const Standard_Real theVMaxParameter)
311 myUMinParameter = theUMinParameter;
312 myUMaxParameter = theUMaxParameter;
313 myVMinParameter = theVMinParameter;
314 myVMaxParameter = theVMaxParameter;
317 // ==================================================================================
320 // ==================================================================================
321 void IntTools_BeanFaceIntersector::Perform()
323 myIsDone = Standard_False;
326 if (myContext.IsNull())
328 myContext=new IntTools_Context;
331 // Fast computation of Line/Plane case
332 if (myCurve.GetType() == GeomAbs_Line &&
333 mySurface.GetType() == GeomAbs_Plane)
339 // Fast check on coincidence for analytic cases
340 if (FastComputeAnalytic())
342 // no further computation is necessary
343 myIsDone = Standard_True;
347 // Initialization of the range manager
348 myRangeManager.SetBoundaries(myFirstParameter, myLastParameter, 0);
351 Standard_Boolean isCoincide = TestComputeCoinside();
354 myResults.Append(IntTools_Range(myFirstParameter, myLastParameter));
355 myIsDone = Standard_True;
359 // Perform intersection
361 // try to find localized solution
362 Standard_Boolean bLocalize = (!Precision::IsInfinite(myUMinParameter) &&
363 !Precision::IsInfinite(myUMaxParameter) &&
364 !Precision::IsInfinite(myVMinParameter) &&
365 !Precision::IsInfinite(myVMaxParameter));
366 bLocalize = bLocalize && (mySurface.GetType() == GeomAbs_BezierSurface ||
367 mySurface.GetType() == GeomAbs_OtherSurface ||
368 (mySurface.GetType() == GeomAbs_BSplineSurface &&
369 (mySurface.UDegree() > 2 || mySurface.VDegree() > 2) &&
370 (mySurface.NbUKnots() > 2 && mySurface.NbVKnots() > 2)));
372 Standard_Boolean isLocalized = bLocalize && ComputeLocalized();
374 // Perform real intersection
377 ComputeAroundExactIntersection();
379 ComputeUsingExtremum();
381 ComputeNearRangeBoundaries();
384 myIsDone = Standard_True;
386 // Treatment of the results
387 for (Standard_Integer i = 1; i <= myRangeManager.Length(); i++)
389 if (myRangeManager.Flag(i) != 2)
392 IntTools_Range aRange = myRangeManager.Range(i);
393 Standard_Integer iLastRange = myResults.Length();
396 IntTools_Range& aLastRange = myResults.ChangeValue(iLastRange);
397 if (Abs(aRange.First() - aLastRange.Last()) > Precision::PConfusion())
399 myResults.Append(aRange);
403 aLastRange.SetLast(aRange.Last());
407 myResults.Append(aRange);
411 // ==================================================================================
414 // ==================================================================================
415 const IntTools_SequenceOfRanges& IntTools_BeanFaceIntersector::Result() const
420 // ==================================================================================
423 // ==================================================================================
424 void IntTools_BeanFaceIntersector::Result(IntTools_SequenceOfRanges& theResults) const
426 theResults = myResults;
429 // ==================================================================================
430 // function: Distance
432 // ==================================================================================
433 Standard_Real IntTools_BeanFaceIntersector::Distance(const Standard_Real theArg)
435 gp_Pnt aPoint = myCurve.Value(theArg);
437 GeomAPI_ProjectPointOnSurf& aProjector = myContext->ProjPS(mySurface.Face());
438 aProjector.Perform(aPoint);
440 if(aProjector.IsDone() && aProjector.NbPoints() > 0) {
441 return aProjector.LowerDistance();
444 Standard_Real aDistance = RealLast();
446 for(Standard_Integer i=0; i < 4; i++) {
447 Standard_Real anIsoParameter = (i==0) ? myUMinParameter : ((i==1) ? myUMaxParameter : ((i==2) ? myVMinParameter : myVMaxParameter));
448 Standard_Real aMinParameter = (i < 2) ? myVMinParameter : myUMinParameter;
449 Standard_Real aMaxParameter = (i < 2) ? myVMaxParameter : myUMaxParameter;
450 Standard_Real aMidParameter = (aMinParameter + aMaxParameter) * 0.5;
451 gp_Pnt aPointMin = (i < 2) ? mySurface.Value(anIsoParameter, aMinParameter) : mySurface.Value(aMinParameter, anIsoParameter);
452 gp_Pnt aPointMax = (i < 2) ? mySurface.Value(anIsoParameter, aMaxParameter) : mySurface.Value(aMaxParameter, anIsoParameter);
453 gp_Pnt aPointMid = (i < 2) ? mySurface.Value(anIsoParameter, aMidParameter) : mySurface.Value(aMidParameter, anIsoParameter);
455 Standard_Boolean useMinMaxPoints = Standard_True;
456 Standard_Boolean computeisoline = Standard_True;
458 if(aPointMin.IsEqual(aPointMax, myCriteria) &&
459 aPointMin.IsEqual(aPointMid, myCriteria) &&
460 aPointMax.IsEqual(aPointMid, myCriteria)) {
461 computeisoline = Standard_False;
465 Handle(Geom_Curve) aCurve = (i < 2) ? myTrsfSurface->UIso(anIsoParameter) : myTrsfSurface->VIso(anIsoParameter);
466 GeomAPI_ProjectPointOnCurve aProjectorOnCurve(aPoint, aCurve, aMinParameter, aMaxParameter);
468 if(aProjectorOnCurve.NbPoints() > 0) {
469 useMinMaxPoints = Standard_False;
471 if(aDistance > aProjectorOnCurve.LowerDistance())
472 aDistance = aProjectorOnCurve.LowerDistance();
476 if(useMinMaxPoints) {
477 Standard_Real aPPDistance = aPoint.Distance(aPointMin);
478 aDistance = (aPPDistance < aDistance) ? aPPDistance : aDistance;
479 aPPDistance = aPoint.Distance(aPointMax);
480 aDistance = (aPPDistance < aDistance) ? aPPDistance : aDistance;
487 // ==================================================================================
488 // function: Distance
490 // ==================================================================================
491 Standard_Real IntTools_BeanFaceIntersector::Distance(const Standard_Real theArg,
492 Standard_Real& theUParameter,
493 Standard_Real& theVParameter)
495 gp_Pnt aPoint = myCurve.Value(theArg);
497 theUParameter = myUMinParameter;
498 theVParameter = myVMinParameter;
500 Standard_Real aDistance = RealLast();
501 Standard_Boolean projectionfound = Standard_False;
503 GeomAPI_ProjectPointOnSurf& aProjector = myContext->ProjPS(mySurface.Face());
504 aProjector.Perform(aPoint);
506 if(aProjector.IsDone() && aProjector.NbPoints() > 0) {
507 aProjector.LowerDistanceParameters(theUParameter, theVParameter);
508 aDistance = aProjector.LowerDistance();
509 projectionfound = Standard_True;
512 if(!projectionfound) {
514 for(Standard_Integer i = 0; i < 4; i++) {
515 Standard_Real anIsoParameter = (i==0) ? myUMinParameter : ((i==1) ? myUMaxParameter : ((i==2) ? myVMinParameter : myVMaxParameter));
516 Standard_Real aMinParameter = (i < 2) ? myVMinParameter : myUMinParameter;
517 Standard_Real aMaxParameter = (i < 2) ? myVMaxParameter : myUMaxParameter;
518 Standard_Real aMidParameter = (aMinParameter + aMaxParameter) * 0.5;
519 gp_Pnt aPointMin = (i < 2) ? mySurface.Value(anIsoParameter, aMinParameter) : mySurface.Value(aMinParameter, anIsoParameter);
520 gp_Pnt aPointMax = (i < 2) ? mySurface.Value(anIsoParameter, aMaxParameter) : mySurface.Value(aMaxParameter, anIsoParameter);
521 gp_Pnt aPointMid = (i < 2) ? mySurface.Value(anIsoParameter, aMidParameter) : mySurface.Value(aMidParameter, anIsoParameter);
523 Standard_Boolean useMinMaxPoints = Standard_True;
524 Standard_Boolean computeisoline = Standard_True;
526 if(aPointMin.IsEqual(aPointMax, myCriteria) &&
527 aPointMin.IsEqual(aPointMid, myCriteria) &&
528 aPointMax.IsEqual(aPointMid, myCriteria)) {
529 computeisoline = Standard_False;
533 Handle(Geom_Curve) aCurve = (i < 2) ? myTrsfSurface->UIso(anIsoParameter) : myTrsfSurface->VIso(anIsoParameter);
534 GeomAPI_ProjectPointOnCurve aProjectorOnCurve(aPoint, aCurve, aMinParameter, aMaxParameter);
536 if(aProjectorOnCurve.NbPoints() > 0) {
537 useMinMaxPoints = Standard_False;
539 if(aDistance > aProjectorOnCurve.LowerDistance()) {
540 theUParameter = (i<=1) ? anIsoParameter : aProjectorOnCurve.LowerDistanceParameter();
541 theVParameter = (i>=2) ? anIsoParameter : aProjectorOnCurve.LowerDistanceParameter();
542 aDistance = aProjectorOnCurve.LowerDistance();
547 if(useMinMaxPoints) {
548 Standard_Real aPPDistance = aPoint.Distance(aPointMin);
550 if(aPPDistance < aDistance) {
551 theUParameter = (i<=1) ? anIsoParameter : aMinParameter;
552 theVParameter = (i>=2) ? anIsoParameter : aMinParameter;
553 aDistance = aPPDistance;
555 aPPDistance = aPoint.Distance(aPointMax);
557 if(aPPDistance < aDistance) {
558 theUParameter = (i<=1) ? anIsoParameter : aMaxParameter;
559 theVParameter = (i>=2) ? anIsoParameter : aMaxParameter;
560 aDistance = aPPDistance;
565 theUParameter = (myUMinParameter > theUParameter) ? myUMinParameter : theUParameter;
566 theUParameter = (myUMaxParameter < theUParameter) ? myUMaxParameter : theUParameter;
567 theVParameter = (myVMinParameter > theVParameter) ? myVMinParameter : theVParameter;
568 theVParameter = (myVMaxParameter < theVParameter) ? myVMaxParameter : theVParameter;
573 // ==================================================================================
574 // function: ComputeAroundExactIntersection
576 // ==================================================================================
577 void IntTools_BeanFaceIntersector::ComputeAroundExactIntersection()
579 IntCurveSurface_HInter anExactIntersector;
581 Handle(BRepAdaptor_HCurve) aCurve = new BRepAdaptor_HCurve(myCurve);
582 Handle(BRepAdaptor_HSurface) aSurface = new BRepAdaptor_HSurface(mySurface);
584 anExactIntersector.Perform(aCurve, aSurface);
586 if(anExactIntersector.IsDone()) {
587 Standard_Integer i = 0;
589 for(i = 1; i <= anExactIntersector.NbPoints(); i++) {
590 const IntCurveSurface_IntersectionPoint& aPoint = anExactIntersector.Point(i);
592 if((aPoint.W() >= myFirstParameter) && (aPoint.W() <= myLastParameter)) {
593 Standard_Boolean UIsNotValid = ((myUMinParameter > aPoint.U()) || (aPoint.U() > myUMaxParameter));
594 Standard_Boolean VIsNotValid = ((myVMinParameter > aPoint.V()) || (aPoint.V() > myVMaxParameter));
595 Standard_Boolean solutionIsValid = !UIsNotValid && !VIsNotValid;
596 Standard_Real U = aPoint.U();
597 Standard_Real V = aPoint.V();
599 if(UIsNotValid || VIsNotValid) {
600 Standard_Boolean bUCorrected = Standard_True;
603 bUCorrected = Standard_False;
604 solutionIsValid = Standard_False;
606 if(mySurface.IsUPeriodic()) {
607 Standard_Real aNewU, aUPeriod, aEps, du;
609 aUPeriod = mySurface.UPeriod();
610 aEps = Epsilon(aUPeriod);
612 GeomInt::AdjustPeriodic(U, myUMinParameter, myUMaxParameter,
613 aUPeriod, aNewU, du, aEps);
614 solutionIsValid = Standard_True;
615 bUCorrected = Standard_True;
619 // if(solutionIsValid && VIsNotValid) {
620 if(bUCorrected && VIsNotValid) {
621 solutionIsValid = Standard_False;
623 if(mySurface.IsVPeriodic()) {
624 Standard_Real aNewV, aVPeriod, aEps, dv;
626 aVPeriod = mySurface.VPeriod();
627 aEps = Epsilon(aVPeriod);
629 GeomInt::AdjustPeriodic(V, myVMinParameter, myVMaxParameter,
630 aVPeriod, aNewV, dv, aEps);
631 solutionIsValid = Standard_True;
640 Standard_Integer aNbRanges = myRangeManager.Length();
642 ComputeRangeFromStartPoint(Standard_False, aPoint.W(), U, V);
643 ComputeRangeFromStartPoint(Standard_True, aPoint.W(), U, V);
645 if(aNbRanges == myRangeManager.Length()) {
646 SetEmptyResultRange(aPoint.W(), myRangeManager);
647 } // end if(aNbRanges == myRangeManager.Length())
651 for(i = 1; i <= anExactIntersector.NbSegments(); i++) {
652 const IntCurveSurface_IntersectionSegment& aSegment = anExactIntersector.Segment(i);
653 IntCurveSurface_IntersectionPoint aPoint1, aPoint2;
654 aSegment.Values(aPoint1, aPoint2);
656 Standard_Real aFirstParameter = (aPoint1.W() < myFirstParameter) ? myFirstParameter : aPoint1.W();
657 Standard_Real aLastParameter = (myLastParameter < aPoint2.W()) ? myLastParameter : aPoint2.W();
659 myRangeManager.InsertRange(aFirstParameter, aLastParameter, 2);
661 ComputeRangeFromStartPoint(Standard_False, aPoint1.W(), aPoint1.U(), aPoint1.V());
662 ComputeRangeFromStartPoint(Standard_True, aPoint2.W(), aPoint2.U(), aPoint2.V());
667 // ==================================================================================
668 // function: FastComputeExactIntersection
670 // ==================================================================================
671 Standard_Boolean IntTools_BeanFaceIntersector::FastComputeAnalytic()
673 GeomAbs_CurveType aCT = myCurve.GetType();
674 if (aCT == GeomAbs_BezierCurve ||
675 aCT == GeomAbs_BSplineCurve ||
676 aCT == GeomAbs_OffsetCurve ||
677 aCT == GeomAbs_OtherCurve)
679 // not supported type
680 return Standard_False;
683 Standard_Boolean isCoincide = Standard_False;
684 Standard_Boolean hasIntersection = Standard_True;
686 GeomAbs_SurfaceType aST = mySurface.GetType();
688 // Plane - Circle/Ellipse/Hyperbola/Parabola
689 if (aST == GeomAbs_Plane)
691 gp_Pln surfPlane = mySurface.Plane();
698 aDir = myCurve.Circle().Axis().Direction();
699 aPLoc = myCurve.Circle().Location();
702 case GeomAbs_Ellipse:
704 aDir = myCurve.Ellipse().Axis().Direction();
705 aPLoc = myCurve.Ellipse().Location();
708 case GeomAbs_Hyperbola:
710 aDir = myCurve.Hyperbola().Axis().Direction();
711 aPLoc = myCurve.Hyperbola().Location();
714 case GeomAbs_Parabola:
716 aDir = myCurve.Parabola().Axis().Direction();
717 aPLoc = myCurve.Parabola().Location();
721 return Standard_False;
724 Standard_Real anAngle = aDir.Angle(surfPlane.Axis().Direction());
725 if (anAngle > Precision::Angular())
726 return Standard_False;
728 hasIntersection = Standard_False;
730 Standard_Real aDist = surfPlane.Distance(aPLoc);
731 isCoincide = aDist < myCriteria;
734 // Cylinder - Line/Circle
735 else if (aST == GeomAbs_Cylinder)
737 gp_Cylinder aCylinder = mySurface.Cylinder();
738 const gp_Ax1& aCylAxis = aCylinder.Axis();
739 const gp_Dir& aCylDir = aCylAxis.Direction();
740 Standard_Real aCylRadius = aCylinder.Radius();
742 if (aCT == GeomAbs_Line)
744 gp_Lin aLin = myCurve.Line();
745 if (!aLin.Direction().IsParallel(aCylDir, Precision::Angular()))
746 return Standard_False;
748 hasIntersection = Standard_False;
750 Standard_Real aDist = Abs(aLin.Distance(aCylAxis.Location()) - aCylRadius);
751 isCoincide = (aDist < myCriteria);
754 else if (aCT == GeomAbs_Circle)
756 gp_Circ aCircle = myCurve.Circle();
758 Standard_Real anAngle = aCylDir.Angle(aCircle.Axis().Direction());
759 if (anAngle > Precision::Angular())
760 return Standard_False;
762 Standard_Real aDistLoc = gp_Lin(aCylAxis).Distance(aCircle.Location());
763 Standard_Real aDist = aDistLoc + Abs(aCircle.Radius() - aCylRadius);
764 isCoincide = (aDist < myCriteria);
767 hasIntersection = (aDistLoc - (aCircle.Radius() + aCylRadius)) < myCriteria &&
768 (Abs(aCircle.Radius() - aCylRadius) - aDistLoc) < myCriteria;
773 else if (aST == GeomAbs_Sphere)
775 gp_Sphere aSph = mySurface.Sphere();
776 gp_Pnt aSphLoc = aSph.Location();
777 if (aCT == GeomAbs_Line)
779 gp_Lin aLin = myCurve.Line();
780 Standard_Real aDist = aLin.Distance(aSphLoc) - aSph.Radius();
781 hasIntersection = aDist < myCriteria;
785 // Check intermediate point
788 myResults.Append(IntTools_Range(myFirstParameter, myLastParameter));
791 return isCoincide || !hasIntersection;
794 // ==================================================================================
795 // function: ComputeLinePlane
797 // ==================================================================================
798 void IntTools_BeanFaceIntersector::ComputeLinePlane()
800 Standard_Real Tolang = 1.e-9;
801 gp_Pln P = mySurface.Plane();
802 gp_Lin L = myCurve.Line();
804 myIsDone = Standard_True;
806 Standard_Real A,B,C,D;
807 Standard_Real Al,Bl,Cl;
808 Standard_Real Dis,Direc;
810 P.Coefficients(A,B,C,D);
811 gp_Pnt Orig(L.Location());
812 L.Direction().Coord(Al,Bl,Cl);
814 Direc=A*Al+B*Bl+C*Cl;
815 Dis = A*Orig.X() + B*Orig.Y() + C*Orig.Z() + D;
817 Standard_Boolean parallel = Standard_False, inplane = Standard_False;
818 if (Abs(Direc) < Tolang) {
819 parallel= Standard_True;
820 if (Abs(Dis) < myCriteria) {
821 inplane=Standard_True;
824 inplane=Standard_False;
828 gp_Pnt p1 = ElCLib::Value(myFirstParameter, L);
829 gp_Pnt p2 = ElCLib::Value(myLastParameter, L);
830 Standard_Real d1 = A*p1.X() + B*p1.Y() + C*p1.Z() + D;
832 Standard_Real d2 = A*p2.X() + B*p2.Y() + C*p2.Z() + D;
834 if(d1 <= myCriteria && d2 <= myCriteria) {
835 inplane=Standard_True;
840 IntTools_Range aRange(myFirstParameter, myLastParameter);
841 myResults.Append(aRange);
849 Standard_Real t = - Dis/Direc;
850 if(t < myFirstParameter || t > myLastParameter) {
854 gp_Pnt pint(Orig.X()+t*Al, Orig.Y()+t*Bl, Orig.Z()+t*Cl);
857 ElSLib::Parameters(P, pint, u, v);
858 if(myUMinParameter > u || u > myUMaxParameter || myVMinParameter > v || v > myVMaxParameter) {
862 // compute correct range on the edge
863 Standard_Real anAngle, aDt;
866 aDL = L.Position().Direction();
867 aDP = P.Position().Direction();
868 anAngle = Abs(M_PI_2 - aDL.Angle(aDP));
870 aDt = IntTools_Tools::ComputeIntRange
871 (myBeanTolerance, myFaceTolerance, anAngle);
873 Standard_Real t1 = Max(myFirstParameter, t - aDt);
874 Standard_Real t2 = Min(myLastParameter, t + aDt);
875 IntTools_Range aRange(t1, t2);
876 myResults.Append(aRange);
882 // ==================================================================================
883 // function: ComputeUsingExtremum
885 // ==================================================================================
886 void IntTools_BeanFaceIntersector::ComputeUsingExtremum()
888 Standard_Real Tol, af, al;
889 Tol = Precision::PConfusion();
890 Handle(Geom_Curve) aCurve = BRep_Tool::Curve (myCurve.Edge(), af, al);
891 GeomAdaptor_Surface aGASurface (myTrsfSurface,
897 for(Standard_Integer i = 1; i <= myRangeManager.Length(); i++) {
899 if(myRangeManager.Flag(i) > 0)
902 IntTools_Range aParamRange = myRangeManager.Range(i);
903 Standard_Real anarg1 = aParamRange.First();
904 Standard_Real anarg2 = aParamRange.Last();
906 if(anarg2 - anarg1 < Precision::PConfusion()) {
908 if (((i > 1) && (myRangeManager.Flag(i - 1) == 2)) ||
909 ((i < myRangeManager.Length()) && (myRangeManager.Flag(i + 1) == 2))) {
910 myRangeManager.SetFlag(i, 1);
915 GeomAdaptor_Curve aGACurve(aCurve, anarg1, anarg2);
916 Extrema_ExtCS theExtCS(aGACurve, aGASurface, Tol, Tol);
917 myExtrema = theExtCS;
919 if(myExtrema.IsDone() && (myExtrema.NbExt() || myExtrema.IsParallel())) {
920 Standard_Integer anOldNbRanges = myRangeManager.Length();
922 if (myExtrema.IsParallel()) {
924 if(myExtrema.SquareDistance(1) < myCriteria * myCriteria) {
925 Standard_Real U1, V1, U2, V2;
926 Standard_Real adistance1 = Distance(anarg1, U1, V1);
927 Standard_Real adistance2 = Distance(anarg2, U2, V2);
928 Standard_Boolean validdistance1 = (adistance1 < myCriteria);
929 Standard_Boolean validdistance2 = (adistance2 < myCriteria);
931 if (validdistance1 && validdistance2) {
932 myRangeManager.InsertRange(anarg1, anarg2, 2);
937 ComputeRangeFromStartPoint(Standard_True, anarg1, U1, V1);
941 ComputeRangeFromStartPoint(Standard_False, anarg2, U2, V2);
944 Standard_Real a = anarg1;
945 Standard_Real b = anarg2;
946 Standard_Real da = adistance1;
947 Standard_Real db = adistance2;
948 Standard_Real asolution = a;
949 Standard_Boolean found = Standard_False;
951 while(((b - a) > myCurveResolution) && !found) {
952 asolution = (a+b)*0.5;
953 Standard_Real adist = Distance(asolution, U1, V1);
955 if(adist < myCriteria) {
956 found = Standard_True;
971 ComputeRangeFromStartPoint(Standard_False, asolution, U1, V1);
972 ComputeRangeFromStartPoint(Standard_True, asolution, U1, V1);
975 myRangeManager.SetFlag(i, 1);
982 myRangeManager.SetFlag(i, 1);
986 Standard_Boolean solutionfound = Standard_False;
988 for(Standard_Integer j = 1 ; j <= myExtrema.NbExt(); j++) {
990 if(myExtrema.SquareDistance(j) < myCriteria * myCriteria) {
993 myExtrema.Points(j, p1, p2);
997 Standard_Integer aNbRanges = myRangeManager.Length();
998 ComputeRangeFromStartPoint(Standard_False, p1.Parameter(), U, V);
999 ComputeRangeFromStartPoint(Standard_True, p1.Parameter(), U, V);
1000 solutionfound = Standard_True;
1002 if(aNbRanges == myRangeManager.Length()) {
1003 SetEmptyResultRange(p1.Parameter(), myRangeManager);
1008 if(!solutionfound) {
1009 myRangeManager.SetFlag(i, 1);
1012 Standard_Integer adifference = myRangeManager.Length() - anOldNbRanges;
1014 if(adifference > 0) {
1017 } // end if(myExtrema.IsDone() && (myExtrema.NbExt() || myExtrema.IsParallel()))
1021 // ==================================================================================
1022 // function: ComputeNearRangeBoundaries
1024 // ==================================================================================
1025 void IntTools_BeanFaceIntersector::ComputeNearRangeBoundaries()
1027 Standard_Real U = myUMinParameter;
1028 Standard_Real V = myVMinParameter;
1030 for(Standard_Integer i = 1; i <= myRangeManager.Length(); i++) {
1032 if(myRangeManager.Flag(i) > 0)
1035 if((i > 1) && (myRangeManager.Flag(i-1) > 0))
1038 IntTools_Range aParamRange = myRangeManager.Range(i);
1040 if(Distance(aParamRange.First(), U, V) < myCriteria) {
1041 Standard_Integer aNbRanges = myRangeManager.Length();
1044 ComputeRangeFromStartPoint(Standard_False, aParamRange.First(), U, V, i-1);
1046 ComputeRangeFromStartPoint(Standard_True, aParamRange.First(), U, V, i + (myRangeManager.Length() - aNbRanges));
1048 if(aNbRanges == myRangeManager.Length()) {
1049 SetEmptyResultRange(aParamRange.First(), myRangeManager);
1054 if(myRangeManager.Flag(myRangeManager.Length()) == 0) {
1055 IntTools_Range aParamRange = myRangeManager.Range(myRangeManager.Length());
1057 if(Distance(aParamRange.Last(), U, V) < myCriteria) {
1058 Standard_Integer aNbRanges = myRangeManager.Length();
1060 ComputeRangeFromStartPoint(Standard_False, aParamRange.Last(), U, V, myRangeManager.Length());
1062 if(aNbRanges == myRangeManager.Length()) {
1063 SetEmptyResultRange(aParamRange.Last(), myRangeManager);
1069 // ==================================================================================
1070 // function: ComputeRangeFromStartPoint
1071 // purpose: Compute range using start point according to parameter theParameter,
1072 // increasing parameter on curve if ToIncreaseParameter == Standard_True or
1073 // decreasing parameter on curve if ToIncreaseParameter == Standard_False
1074 // ==================================================================================
1075 void IntTools_BeanFaceIntersector::ComputeRangeFromStartPoint(const Standard_Boolean ToIncreaseParameter,
1076 const Standard_Real theParameter,
1077 const Standard_Real theUParameter,
1078 const Standard_Real theVParameter)
1080 Standard_Integer aFoundIndex = myRangeManager.GetIndex(theParameter, ToIncreaseParameter);
1082 if(aFoundIndex == 0) {
1086 ComputeRangeFromStartPoint(ToIncreaseParameter, theParameter, theUParameter, theVParameter, aFoundIndex);
1089 // ==================================================================================
1090 // function: ComputeRangeFromStartPoint
1091 // purpose: Compute range using start point according to parameter theParameter,
1092 // increasing parameter on curve if ToIncreaseParameter == Standard_True or
1093 // decreasing parameter on curve if ToIncreaseParameter == Standard_False.
1094 // theIndex indicate that theParameter belong the range number theIndex.
1095 // ==================================================================================
1096 void IntTools_BeanFaceIntersector::ComputeRangeFromStartPoint(const Standard_Boolean ToIncreaseParameter,
1097 const Standard_Real theParameter,
1098 const Standard_Real theUParameter,
1099 const Standard_Real theVParameter,
1100 const Standard_Integer theIndex)
1102 if(myRangeManager.Flag(theIndex) > 0)
1105 Standard_Integer aValidIndex = theIndex;
1107 Standard_Real aMinDelta = myCurveResolution * 0.5;
1108 Standard_Real aDeltaRestrictor = myLastParameter - myFirstParameter;
1110 if(aMinDelta > aDeltaRestrictor)
1111 aMinDelta = aDeltaRestrictor * 0.5;
1113 Standard_Real tenOfMinDelta = aMinDelta * 10.;
1114 Standard_Real aDelta = myCurveResolution;
1116 Standard_Real aCurPar = (ToIncreaseParameter) ? (theParameter + aDelta) : (theParameter - aDelta);
1117 Standard_Real aPrevPar = theParameter;
1118 IntTools_Range aCurrentRange = myRangeManager.Range(aValidIndex);
1120 Standard_Boolean BoundaryCondition = (ToIncreaseParameter) ? (aCurPar > aCurrentRange.Last()) : (aCurPar < aCurrentRange.First());
1122 if(BoundaryCondition) {
1123 aCurPar = (ToIncreaseParameter) ? aCurrentRange.Last() : aCurrentRange.First();
1124 BoundaryCondition = Standard_False;
1127 Standard_Integer loopcounter = 0; // neccesary as infinite loop restricter
1128 Standard_Real U = theUParameter;
1129 Standard_Real V = theVParameter;
1130 Standard_Boolean anotherSolutionFound = Standard_False;
1132 Standard_Boolean isboundaryindex = Standard_False;
1133 Standard_Boolean isvalidindex = Standard_True;
1135 while((aDelta >= aMinDelta) && (loopcounter <= 10)) {
1136 Standard_Boolean pointfound = Standard_False;
1139 gp_Pnt aPoint = myCurve.Value(aCurPar);
1140 Extrema_GenLocateExtPS anExtrema(mySurface, 1.e-10, 1.e-10);
1141 anExtrema.Perform(aPoint, U, V);
1143 if(anExtrema.IsDone()) {
1144 if(anExtrema.SquareDistance() < myCriteria * myCriteria) {
1145 Extrema_POnSurf aPOnSurf = anExtrema.Point();
1146 aPOnSurf.Parameter(U, V);
1147 pointfound = Standard_True;
1151 pointfound = (Distance(aCurPar) < myCriteria);
1156 anotherSolutionFound = Standard_True;
1158 if(BoundaryCondition && (isboundaryindex || !isvalidindex))
1162 aDeltaRestrictor = aDelta;
1165 // if point found decide to increase aDelta using derivative of distance function
1168 aDelta = (pointfound) ? (aDelta * 2.) : (aDelta * 0.5);
1169 aDelta = (aDelta < aDeltaRestrictor) ? aDelta : aDeltaRestrictor;
1171 aCurPar = (ToIncreaseParameter) ? (aPrevPar + aDelta) : (aPrevPar - aDelta);
1174 // prevent infinite loop when (aPrevPar +/- aDelta) == aPrevPar == 0.
1177 if( aCurPar == aPrevPar )
1180 BoundaryCondition = (ToIncreaseParameter) ? (aCurPar > aCurrentRange.Last()) : (aCurPar < aCurrentRange.First());
1182 isboundaryindex = Standard_False;
1183 isvalidindex = Standard_True;
1185 if(BoundaryCondition) {
1186 isboundaryindex = ((!ToIncreaseParameter && (aValidIndex == 1)) ||
1187 (ToIncreaseParameter && (aValidIndex == myRangeManager.Length())));
1189 if(!isboundaryindex) {
1192 Standard_Integer aFlag = (ToIncreaseParameter) ? myRangeManager.Flag(aValidIndex + 1) : myRangeManager.Flag(aValidIndex - 1);
1195 aValidIndex = (ToIncreaseParameter) ? (aValidIndex + 1) : (aValidIndex - 1);
1196 aCurrentRange = myRangeManager.Range(aValidIndex);
1198 if((ToIncreaseParameter && (aCurPar > aCurrentRange.Last())) ||
1199 (!ToIncreaseParameter && (aCurPar < aCurrentRange.First()))) {
1200 aCurPar = (aCurrentRange.First() + aCurrentRange.Last()) * 0.5;
1205 isvalidindex = Standard_False;
1206 aCurPar = (ToIncreaseParameter) ? aCurrentRange.Last() : aCurrentRange.First();
1211 aCurPar = (ToIncreaseParameter) ? aCurrentRange.Last() : aCurrentRange.First();
1214 if(aDelta < tenOfMinDelta) {
1220 } // if(BoundaryCondition)
1223 if(anotherSolutionFound) {
1224 if(ToIncreaseParameter)
1225 myRangeManager.InsertRange(theParameter, aPrevPar, 2);
1227 myRangeManager.InsertRange(aPrevPar, theParameter, 2);
1231 // ---------------------------------------------------------------------------------
1232 // static function: SetEmptyResultRange
1234 // ---------------------------------------------------------------------------------
1235 static Standard_Boolean SetEmptyResultRange(const Standard_Real theParameter,
1236 IntTools_MarkedRangeSet& theMarkedRange) {
1238 const TColStd_SequenceOfInteger& anIndices = theMarkedRange.GetIndices(theParameter);
1239 Standard_Boolean add = (anIndices.Length() > 0);
1241 for(Standard_Integer k = 1; k <= anIndices.Length(); k++) {
1242 if(theMarkedRange.Flag(anIndices(k)) == 2) {
1243 add = Standard_False;
1249 theMarkedRange.InsertRange(theParameter, theParameter, 2);
1255 // ---------------------------------------------------------------------------------
1256 // static function: TestCoinside
1258 // ---------------------------------------------------------------------------------
1259 // static Standard_Boolean TestClose(const Extrema_ExtPS & theExt,
1260 // const Standard_Real theDist)
1262 // Standard_Boolean close = Standard_False;
1263 // if(!theExt.IsDone() || theExt.NbExt() == 0)
1266 // Standard_Integer ie;
1267 // for(ie = 1; ie <= theExt.NbExt(); ie++) {
1268 // Standard_Real dist = theExt.Value(ie);
1269 // if(dist <= theDist) {
1270 // close = Standard_True;
1278 // Standard_Boolean TestCoinside(const BRepAdaptor_Curve& theCurve,
1279 // const BRepAdaptor_Surface& theSurface)
1281 // Standard_Real cfp = theCurve.FirstParameter(), clp = theCurve.LastParameter();
1282 // Standard_Real cdp = fabs(clp - cfp) / 23.;
1284 // Standard_Integer i = 0;
1285 // Standard_Real tolE = theCurve.Tolerance(), tolF = theSurface.Tolerance();
1286 // Standard_Real tolT = tolE + tolF, tolU = 1.e-9, tolV = 1.e-9;
1289 // theCurve.D0(cfp,aP);
1290 // Extrema_ExtPS eps(aP,theSurface,tolU,tolV);
1292 // if(!TestClose(eps,tolT))
1293 // return Standard_False;
1295 // theCurve.D0(clp,aP);
1298 // if(!TestClose(eps,tolT))
1299 // return Standard_False;
1301 // Standard_Boolean close = Standard_True;
1303 // for(i = 1; i <= 22; i++) {
1304 // theCurve.D0((cfp+((Standard_Real)i)*cdp),aP);
1306 // if(!TestClose(eps,tolT)) {
1307 // close = Standard_False;
1314 // ======================================================================================================================
1315 // function: LocalizeSolutions
1317 // ======================================================================================================================
1318 Standard_Boolean IntTools_BeanFaceIntersector::LocalizeSolutions(const IntTools_CurveRangeSample& theCurveRange,
1319 const Bnd_Box& theBoxCurve,
1320 const IntTools_SurfaceRangeSample& theSurfaceRange,
1321 const Bnd_Box& theBoxSurface,
1322 IntTools_CurveRangeLocalizeData& theCurveData,
1323 IntTools_SurfaceRangeLocalizeData& theSurfaceData,
1324 IntTools_ListOfCurveRangeSample& theListCurveRange,
1325 IntTools_ListOfSurfaceRangeSample& theListSurfaceRange)
1327 Standard_Integer tIt = 0, uIt = 0, vIt = 0;
1330 IntTools_CurveRangeSample aRootRangeC(0);
1331 aRootRangeC.SetDepth(0);
1332 IntTools_SurfaceRangeSample aRootRangeS(0, 0, 0, 0);
1334 Bnd_Box aMainBoxC = theBoxCurve;
1335 Bnd_Box aMainBoxS = theBoxSurface;
1336 Standard_Boolean bMainBoxFoundS = Standard_False;
1337 Standard_Boolean bMainBoxFoundC = Standard_False;
1339 IntTools_ListOfCurveRangeSample aListCurveRangeFound;
1340 IntTools_ListOfSurfaceRangeSample aListSurfaceRangeFound;
1343 IntTools_Range aRangeC = theCurveRange.GetRange(myFirstParameter, myLastParameter, theCurveData.GetNbSample());
1344 Standard_Real localdiffC = (aRangeC.Last() - aRangeC.First()) / theCurveData.GetNbSample();
1346 Standard_Real aCurPar = aRangeC.First();
1347 Standard_Real aPrevPar = aRangeC.First();
1348 Standard_Integer aCurIndexInit = theCurveRange.GetRangeIndexDeeper(theCurveData.GetNbSample());
1351 TColStd_ListOfInteger aListCToAvoid;
1352 Standard_Boolean bGlobalCheckDone = Standard_False;
1357 Standard_Integer aCurIndexU = theSurfaceRange.GetRangeIndexUDeeper(theSurfaceData.GetNbSampleU());
1359 Standard_Integer aCurIndexVInit = theSurfaceRange.GetRangeIndexVDeeper(theSurfaceData.GetNbSampleV());
1360 IntTools_Range aRangeV = theSurfaceRange.GetRangeV(myVMinParameter, myVMaxParameter, theSurfaceData.GetNbSampleV());
1363 IntTools_Range aRangeU = theSurfaceRange.GetRangeU(myUMinParameter, myUMaxParameter, theSurfaceData.GetNbSampleU());
1364 Standard_Real aCurParU = aRangeU.First();
1365 Standard_Real aLocalDiffU = (aRangeU.Last() - aRangeU.First()) / theSurfaceData.GetNbSampleU();
1367 Standard_Real aPrevParU = aCurParU;
1368 Standard_Real aLocalDiffV = (aRangeV.Last() - aRangeV.First()) / theSurfaceData.GetNbSampleV();
1371 // ranges check.begin
1372 Standard_Boolean bAllowSamplingC = Standard_True;
1373 Standard_Boolean bAllowSamplingU = Standard_True;
1374 Standard_Boolean bAllowSamplingV = Standard_True;
1377 CheckSampling(theCurveRange, theSurfaceRange, theCurveData, theSurfaceData,
1378 localdiffC, aLocalDiffU, aLocalDiffV,
1379 bAllowSamplingC, bAllowSamplingU, bAllowSamplingV);
1382 if(!bAllowSamplingC && !bAllowSamplingU && !bAllowSamplingV) {
1383 theListCurveRange.Append(theCurveRange);
1384 theListSurfaceRange.Append(theSurfaceRange);
1385 return Standard_True;
1389 // init template. begin
1390 IntTools_CurveRangeSample aNewRangeCTemplate;
1392 if(!bAllowSamplingC) {
1393 aNewRangeCTemplate = theCurveRange;
1394 aCurIndexInit = theCurveRange.GetRangeIndex();
1395 localdiffC = (aRangeC.Last() - aRangeC.First());
1398 aNewRangeCTemplate.SetDepth(theCurveRange.GetDepth() + 1);
1399 aNewRangeCTemplate.SetRangeIndex(aCurIndexInit);
1402 IntTools_SurfaceRangeSample aNewRangeSTemplate = theSurfaceRange;
1404 if(bAllowSamplingU) {
1405 aNewRangeSTemplate.SetDepthU(theSurfaceRange.GetDepthU() + 1);
1408 aCurIndexU = aNewRangeSTemplate.GetIndexU();
1409 aLocalDiffU = aRangeU.Last() - aRangeU.First();
1412 if(bAllowSamplingV) {
1413 aNewRangeSTemplate.SetDepthV(theSurfaceRange.GetDepthV() + 1);
1416 aCurIndexVInit = theSurfaceRange.GetIndexV();
1417 aLocalDiffV = aRangeV.Last() - aRangeV.First();
1419 // init template. end
1422 Standard_Boolean bHasOut = Standard_False;
1423 const Standard_Integer nbU = (bAllowSamplingU) ? theSurfaceData.GetNbSampleU() : 1;
1424 const Standard_Integer nbV = (bAllowSamplingV) ? theSurfaceData.GetNbSampleV() : 1;
1425 const Standard_Integer nbC = (bAllowSamplingC) ? theCurveData.GetNbSample() : 1;
1427 for(uIt = 1; uIt <= nbU; uIt++, aCurIndexU++, aPrevParU = aCurParU) {
1428 aCurParU += aLocalDiffU;
1431 Standard_Real aCurParV = aRangeV.First();
1432 Standard_Real aPrevParV = aCurParV;
1433 Standard_Integer aCurIndexV = aCurIndexVInit;
1435 Standard_Boolean bHasOutV = Standard_False;
1438 for(vIt = 1; vIt <= nbV; vIt++, aCurIndexV++, aPrevParV = aCurParV) {
1440 aCurParV += aLocalDiffV;
1445 IntTools_SurfaceRangeSample aNewRangeS = aNewRangeSTemplate;
1447 if(bAllowSamplingU) {
1448 aNewRangeS.SetIndexU(aCurIndexU);
1451 if(bAllowSamplingV) {
1452 aNewRangeS.SetIndexV(aCurIndexV);
1455 if(theSurfaceData.IsRangeOut(aNewRangeS)) {
1456 bHasOutV = Standard_True;
1464 if(!theSurfaceData.FindBox(aNewRangeS, aBoxS)) {
1466 if(mySurface.GetType() == GeomAbs_BSplineSurface) {
1467 // if(Standard_False ) {
1468 Handle(Geom_BSplineSurface) aSurfBspl = Handle(Geom_BSplineSurface)::DownCast(myTrsfSurface);
1469 aBoxS = GetSurfaceBox(aSurfBspl, aPrevParU, aCurParU, aPrevParV, aCurParV, myCriteria, theSurfaceData);
1472 BndLib_AddSurface::Add(mySurface, aPrevParU, aCurParU, aPrevParV, aCurParV, myCriteria, aBoxS);
1474 // Bnd_Box aMainBoxC;
1476 if(!bMainBoxFoundC && theCurveData.FindBox(aRootRangeC, aMainBoxC)) {
1477 bMainBoxFoundC = Standard_True;
1480 if(aBoxS.IsOut(aMainBoxC)) {
1481 theSurfaceData.AddOutRange(aNewRangeS);
1482 bHasOutV = Standard_True;
1486 theSurfaceData.AddBox(aNewRangeS, aBoxS);
1489 if(aBoxS.IsOut(theBoxCurve)) {
1490 bHasOutV = Standard_True;
1494 IntTools_ListOfBox aListOfBox;
1495 TColStd_ListOfInteger aListOfIndex;
1497 Standard_Boolean bHasOutC = Standard_False;
1498 Standard_Integer aCurIndex = aCurIndexInit;
1500 // ////////////////////////////
1501 aCurPar = aRangeC.First();
1502 aPrevPar = aRangeC.First();
1503 IntTools_CurveRangeSample aCurRangeC = aNewRangeCTemplate;
1505 for (tIt = 1; tIt <= nbC; tIt++, aCurIndex++, aPrevPar = aCurPar) {
1507 aCurPar += localdiffC;
1509 // ignore already computed. begin
1510 Standard_Boolean bFound = Standard_False;
1511 TColStd_ListIteratorOfListOfInteger anItToAvoid(aListCToAvoid);
1513 for(; anItToAvoid.More(); anItToAvoid.Next()) {
1514 if(tIt == anItToAvoid.Value()) {
1515 bFound = Standard_True;
1521 if(bAllowSamplingC) {
1522 aCurRangeC.SetRangeIndex(aCurIndex);
1524 bFound = theCurveData.IsRangeOut(aCurRangeC);
1528 bHasOutC = Standard_True;
1531 // ignore already computed. end
1536 if(!theCurveData.FindBox(aCurRangeC, aBoxC)) {
1537 BndLib_Add3dCurve::Add(myCurve, aPrevPar, aCurPar, myCriteria, aBoxC);
1539 // Bnd_Box aMainBoxS;
1541 if(!bMainBoxFoundS && theSurfaceData.FindBox(aRootRangeS, aMainBoxS)) {
1542 bMainBoxFoundS = Standard_True;
1544 if(aBoxC.IsOut(aMainBoxS)) {
1545 theCurveData.AddOutRange(aCurRangeC);
1546 bHasOutC = Standard_True;
1550 theCurveData.AddBox(aCurRangeC, aBoxC);
1553 if(!bGlobalCheckDone && aBoxC.IsOut(theBoxSurface)) {
1554 aListCToAvoid.Append(tIt);
1555 bHasOutC = Standard_True;
1559 if(aBoxC.IsOut(aBoxS)) {
1560 bHasOutV = Standard_True;
1561 bHasOutC = Standard_True;
1566 aListOfIndex.Append(tIt);
1567 aListOfBox.Append(aBoxC);
1568 } // end for(tIt...)
1570 bGlobalCheckDone = Standard_True;
1573 bHasOutV = Standard_True;
1579 IntTools_CurveRangeSample aNewRangeC = aNewRangeCTemplate;
1581 aCurIndex = aCurIndexInit;
1582 TColStd_ListIteratorOfListOfInteger anItI(aListOfIndex);
1583 IntTools_ListIteratorOfListOfBox anItBox(aListOfBox);
1584 Standard_Boolean bUseOldC = Standard_False;
1585 Standard_Boolean bUseOldS = Standard_False;
1586 Standard_Boolean bCheckSize = !bHasOutC;
1588 for(; anItI.More() && anItBox.More(); anItI.Next(), anItBox.Next()) {
1589 aCurIndex = aCurIndexInit + anItI.Value() - 1;
1591 bUseOldS = Standard_False;
1593 if(bAllowSamplingC) {
1594 aNewRangeC.SetRangeIndex(aCurIndex);
1600 if((theCurveRange.GetDepth() == 0) ||
1601 (theSurfaceRange.GetDepthU() == 0) ||
1602 (theSurfaceRange.GetDepthV() == 0)) {
1603 bHasOutC = Standard_True;
1604 bHasOutV = Standard_True;
1606 else if((theCurveRange.GetDepth() < 4) &&
1607 (theSurfaceRange.GetDepthU() < 4) &&
1608 (theSurfaceRange.GetDepthV() < 4)) {
1609 Bnd_Box aBoxC = anItBox.Value();
1611 if(!aBoxC.IsWhole() && !aBoxS.IsWhole()) {
1612 Standard_Real aDiagC = aBoxC.SquareExtent();
1613 Standard_Real aDiagS = aBoxS.SquareExtent();
1615 if(aDiagC < aDiagS) {
1616 if((aDiagC * 10.) < aDiagS) {
1617 bUseOldC = Standard_True;
1618 bHasOutC = Standard_True;
1619 bHasOutV = Standard_True;
1624 if((aDiagS * 10.) < aDiagC) {
1625 bUseOldS = Standard_True;
1626 bHasOutC = Standard_True;
1627 bHasOutV = Standard_True;
1636 aListCurveRangeFound.Append(aNewRangeC);
1637 aListSurfaceRangeFound.Append(aNewRangeS);
1641 // if(bUseOldS || bAllowSamplingU || bAllowSamplingV) {
1642 // theSurfaceData.AddBox(aNewRangeS, aBoxS);
1645 if(bUseOldS && aNewRangeC.IsEqual(theCurveRange)) {
1646 return Standard_False;
1649 if(!LocalizeSolutions(aNewRangeC, anItBox.Value(),
1650 ((bUseOldS) ? theSurfaceRange : aNewRangeS),
1651 ((bUseOldS) ? theBoxSurface : aBoxS),
1652 theCurveData, theSurfaceData,
1653 theListCurveRange, theListSurfaceRange))
1654 return Standard_False;
1658 aListOfIndex.Clear();
1662 // theSurfaceData.AddBox(aNewRangeS, aBoxS);
1664 if(bUseOldC && bAllowSamplingC && (bAllowSamplingU || bAllowSamplingV)) {
1665 if(!LocalizeSolutions(theCurveRange, theBoxCurve,
1667 theCurveData, theSurfaceData,
1668 theListCurveRange, theListSurfaceRange))
1669 return Standard_False;
1672 } // end for (vIt...)
1675 bHasOut = Standard_True;
1680 theListCurveRange.Append(theCurveRange);
1681 theListSurfaceRange.Append(theSurfaceRange);
1684 IntTools_ListIteratorOfListOfCurveRangeSample anIt1(aListCurveRangeFound);
1685 IntTools_ListIteratorOfListOfSurfaceRangeSample anIt2(aListSurfaceRangeFound);
1687 for(; anIt1.More() && anIt2.More(); anIt1.Next(), anIt2.Next()) {
1688 theListCurveRange.Append(anIt1.Value());
1689 theListSurfaceRange.Append(anIt2.Value());
1692 return Standard_True;
1696 // ======================================================================================================================
1697 // function: ComputeLocalized
1699 // ======================================================================================================================
1700 Standard_Boolean IntTools_BeanFaceIntersector::ComputeLocalized() {
1701 Standard_Real Tol = Precision::PConfusion();
1703 IntTools_SurfaceRangeSample aSurfaceRange(0, 0, 0, 0);
1704 Standard_Real dMinU = 10. * Precision::PConfusion();
1705 Standard_Real dMinV = dMinU;
1706 IntTools_SurfaceRangeLocalizeData aSurfaceDataInit(3, 3, dMinU, dMinV);
1707 IntTools_SurfaceRangeLocalizeData& aSurfaceData = myContext->SurfaceData(mySurface.Face());
1708 aSurfaceData.RemoveRangeOutAll();
1709 aSurfaceData.ClearGrid();
1712 Standard_Boolean bFBoxFound = aSurfaceData.FindBox(aSurfaceRange, FBox);
1714 if(mySurface.GetType() == GeomAbs_BSplineSurface) {
1715 Handle(Geom_BSplineSurface) aSurfBspl = Handle(Geom_BSplineSurface)::DownCast(myTrsfSurface);
1717 ComputeGridPoints(aSurfBspl, myUMinParameter, myUMaxParameter,
1718 myVMinParameter, myVMaxParameter, myFaceTolerance,
1722 FBox = GetSurfaceBox(aSurfBspl, myUMinParameter, myUMaxParameter,
1723 myVMinParameter, myVMaxParameter, myCriteria,
1725 aSurfaceData.AddBox(aSurfaceRange, FBox);
1728 } else if(!bFBoxFound) {
1730 BndLib_AddSurface::Add(mySurface, myUMinParameter, myUMaxParameter, myVMinParameter, myVMaxParameter, myFaceTolerance, FBox);
1731 aSurfaceData.AddBox(aSurfaceRange, FBox);
1736 BndLib_Add3dCurve::Add(myCurve.Trim(myFirstParameter, myLastParameter, Precision::PConfusion())->Curve(), myBeanTolerance, EBox);
1738 if(EBox.IsOut(FBox)) {
1739 for(Standard_Integer i = 1; i <= myRangeManager.Length(); i++) {
1740 myRangeManager.SetFlag(i, 1);
1742 aSurfaceData.ClearGrid();
1744 return Standard_True;
1747 IntTools_ListOfCurveRangeSample aListCurveRange;
1748 IntTools_ListOfSurfaceRangeSample aListSurfaceRange;
1750 IntTools_CurveRangeSample aCurveRange(0);
1751 aCurveRange.SetDepth(0);
1752 Standard_Integer nbSampleC = 3;
1753 Standard_Integer nbSampleU = aSurfaceData.GetNbSampleU();
1754 Standard_Integer nbSampleV = aSurfaceData.GetNbSampleV();
1755 Standard_Real dMinC = 10. * myCurveResolution;
1756 IntTools_ListOfCurveRangeSample aListOut;
1759 Standard_Boolean bAllowSamplingC = Standard_True;
1760 Standard_Boolean bAllowSamplingU = Standard_True;
1761 Standard_Boolean bAllowSamplingV = Standard_True;
1762 IntTools_CurveRangeLocalizeData aCurveDataTmp(nbSampleC, dMinC);
1763 IntTools_SurfaceRangeLocalizeData aSurfaceDataTmp(nbSampleU, nbSampleV, dMinU, dMinV);
1765 CheckSampling(aCurveRange, aSurfaceRange, aCurveDataTmp, aSurfaceDataTmp,
1766 myLastParameter - myFirstParameter,
1767 myUMaxParameter - myUMinParameter,
1768 myVMaxParameter - myVMinParameter,
1769 bAllowSamplingC, bAllowSamplingU, bAllowSamplingV);
1773 IntTools_CurveRangeLocalizeData aCurveData(nbSampleC, dMinC);
1775 aCurveData.AddBox(aCurveRange, EBox);
1777 if(!LocalizeSolutions(aCurveRange, EBox, aSurfaceRange, FBox,
1778 aCurveData, aSurfaceData,
1779 aListCurveRange, aListSurfaceRange)) {
1780 aSurfaceData.ClearGrid();
1782 return Standard_False;
1785 IntTools_ListOfCurveRangeSample aListCurveRangeSort;
1786 IntTools_ListOfSurfaceRangeSample aListSurfaceRangeSort;
1788 MergeSolutions(aListCurveRange, aListSurfaceRange, aListCurveRangeSort, aListSurfaceRangeSort);
1790 IntTools_ListIteratorOfListOfCurveRangeSample anItC(aListCurveRangeSort);
1791 IntTools_ListIteratorOfListOfSurfaceRangeSample anItS(aListSurfaceRangeSort);
1792 IntTools_SurfaceRangeSample aRangeSPrev;
1794 Extrema_GenExtCS anExtremaGen;
1796 for(; anItC.More() && anItS.More(); anItC.Next(), anItS.Next()) {
1798 IntTools_Range aRangeC(myFirstParameter, myLastParameter);
1801 aRangeC = anItC.Value().GetRange(myFirstParameter, myLastParameter, nbSampleC);
1803 IntTools_Range aRangeU(myUMinParameter, myUMaxParameter);
1806 aRangeU = anItS.Value().GetRangeU(myUMinParameter, myUMaxParameter, nbSampleU);
1808 IntTools_Range aRangeV(myVMinParameter, myVMaxParameter);
1811 aRangeV = anItS.Value().GetRangeV(myVMinParameter, myVMaxParameter, nbSampleV);
1813 Standard_Real anarg1 = aRangeC.First(), anarg2 = aRangeC.Last();
1815 Standard_Boolean bFound = Standard_False;
1817 Standard_Integer nMinIndex = myRangeManager.Length();
1818 Standard_Integer nMaxIndex = -1;
1819 const TColStd_SequenceOfInteger& anInds1 = myRangeManager.GetIndices(anarg1);
1820 Standard_Integer indIt = 1;
1822 for(indIt = 1 ; indIt <= anInds1.Length(); indIt++) {
1823 Standard_Integer nIndex = anInds1.Value(indIt);
1824 nMinIndex = (nMinIndex > nIndex) ? nIndex : nMinIndex;
1825 nMaxIndex = (nMaxIndex < nIndex) ? nIndex : nMaxIndex;
1828 for(indIt = nMinIndex ; indIt <= nMaxIndex; indIt++) {
1829 if(myRangeManager.Flag(indIt) == 2) {
1830 bFound = Standard_True;
1837 nMinIndex = (nMaxIndex >= 0) ? nMaxIndex : nMinIndex;
1838 const TColStd_SequenceOfInteger& anInds2 = myRangeManager.GetIndices(anarg2);
1840 for(indIt = 1 ; indIt <= anInds2.Length(); indIt++) {
1841 Standard_Integer nIndex = anInds2.Value(indIt);
1842 nMinIndex = (nMinIndex > nIndex) ? nIndex : nMinIndex;
1843 nMaxIndex = (nMaxIndex < nIndex) ? nIndex : nMaxIndex;
1846 for(indIt = nMinIndex ; indIt <= nMaxIndex; indIt++) {
1847 if(myRangeManager.Flag(indIt) == 2) {
1848 bFound = Standard_True;
1856 Standard_Real parUF = aRangeU.First(), parUL = aRangeU.Last();
1857 Standard_Real parVF = aRangeV.First(), parVL = aRangeV.Last();
1859 if(aRangeSPrev.IsEqual(anItS.Value())) {
1860 anExtremaGen.Perform(myCurve, 10, anarg1, anarg2, Tol);
1863 anExtremaGen.Initialize(mySurface, 10, 10, parUF, parUL, parVF, parVL, Tol);
1864 anExtremaGen.Perform(myCurve, 10, anarg1, anarg2, Tol);
1867 if(anExtremaGen.IsDone() && (anExtremaGen.NbExt() > 0)) {
1869 for(Standard_Integer j = 1 ; j <= anExtremaGen.NbExt(); j++) {
1871 if(anExtremaGen.SquareDistance(j) < myCriteria * myCriteria) {
1875 p1 = anExtremaGen.PointOnCurve(j);
1876 p2 = anExtremaGen.PointOnSurface(j);
1877 Standard_Real U, V, T;
1881 if (myCurve.IsPeriodic())
1882 T = ElCLib::InPeriod(T, anarg1, anarg1 + myCurve.Period());
1883 if (mySurface.IsUPeriodic())
1884 U = ElCLib::InPeriod(U, parUF, parUF + mySurface.UPeriod());
1885 if (mySurface.IsVPeriodic())
1886 V = ElCLib::InPeriod(V, parVF, parVF + mySurface.VPeriod());
1888 //To avoid occasional going out of boundaries because of numerical
1890 if(U < myUMinParameter) U = myUMinParameter;
1891 if(U > myUMaxParameter) U = myUMaxParameter;
1892 if(V < myVMinParameter) V = myVMinParameter;
1893 if(V > myVMaxParameter) V = myVMaxParameter;
1895 Standard_Integer aNbRanges = myRangeManager.Length();
1896 ComputeRangeFromStartPoint(Standard_False, T, U, V);
1897 ComputeRangeFromStartPoint(Standard_True, T, U, V);
1899 if(aNbRanges == myRangeManager.Length()) {
1900 SetEmptyResultRange(T, myRangeManager);
1906 myRangeManager.InsertRange(anarg1, anarg2, 0);
1909 aRangeSPrev = anItS.Value();
1913 aCurveData.ListRangeOut(aListOut);
1917 if(bAllowSamplingC) {
1918 IntTools_ListIteratorOfListOfCurveRangeSample anItC(aListOut);
1920 for(; anItC.More(); anItC.Next()) {
1921 IntTools_Range aRangeC =anItC.Value().GetRange(myFirstParameter, myLastParameter, nbSampleC);
1922 myRangeManager.InsertRange(aRangeC.First(), aRangeC.Last(), 1);
1925 ComputeNearRangeBoundaries();
1927 aSurfaceData.ClearGrid();
1929 return Standard_True;
1932 // ======================================================================================================================
1933 // function: TestComputeCoinside
1935 // ======================================================================================================================
1936 Standard_Boolean IntTools_BeanFaceIntersector::TestComputeCoinside()
1938 Standard_Real cfp = myFirstParameter, clp = myLastParameter;
1939 const Standard_Integer nbSeg = 23;
1940 Standard_Real cdp = (clp - cfp) / (Standard_Real )nbSeg;
1942 Standard_Integer i = 0;
1946 if(Distance(cfp, U, V) > myCriteria)
1947 return Standard_False;
1950 ComputeRangeFromStartPoint(Standard_True, cfp, U, V);
1953 Standard_Integer aFoundIndex = myRangeManager.GetIndex(clp, Standard_False );
1955 if(aFoundIndex != 0) {
1956 if(myRangeManager.Flag(aFoundIndex) == 2)
1957 return Standard_True;
1960 if(Distance(clp, U, V) > myCriteria)
1961 return Standard_False;
1964 ComputeRangeFromStartPoint(Standard_False, clp, U, V);
1967 for(i = 1; i < nbSeg; i++) {
1968 Standard_Real aPar = (cfp+((Standard_Real)i)*cdp);
1970 if(Distance(aPar, U, V) > myCriteria)
1971 return Standard_False;
1973 Standard_Integer aNbRanges = myRangeManager.Length();
1974 ComputeRangeFromStartPoint(Standard_False, aPar, U, V);
1975 ComputeRangeFromStartPoint(Standard_True, aPar, U, V);
1977 if(aNbRanges == myRangeManager.Length()) {
1978 SetEmptyResultRange(aPar, myRangeManager);
1982 return Standard_True;
1985 // Modified by skv - Wed Nov 2 15:21:11 2005 Optimization Begin
1986 // ---------------------------------------------------------------------------------
1987 // static function: GetSurfaceBox
1989 // ---------------------------------------------------------------------------------
1990 Bnd_Box GetSurfaceBox(const Handle(Geom_BSplineSurface) &theSurf,
1991 const Standard_Real theFirstU,
1992 const Standard_Real theLastU,
1993 const Standard_Real theFirstV,
1994 const Standard_Real theLastV,
1995 const Standard_Real theTolerance,
1996 IntTools_SurfaceRangeLocalizeData &theSurfaceData)
2000 BuildBox(theSurf, theFirstU, theLastU, theFirstV, theLastV,
2001 theSurfaceData, aTotalBox);
2003 aTotalBox.Enlarge(theTolerance);
2008 // ---------------------------------------------------------------------------------
2009 // static function: ComputeGridPoints
2011 // ---------------------------------------------------------------------------------
2012 void ComputeGridPoints
2013 (const Handle(Geom_BSplineSurface) &theSurf,
2014 const Standard_Real theFirstU,
2015 const Standard_Real theLastU,
2016 const Standard_Real theFirstV,
2017 const Standard_Real theLastV,
2018 const Standard_Real theTolerance,
2019 IntTools_SurfaceRangeLocalizeData &theSurfaceData)
2024 Standard_Integer aNbSamples[2] = { theSurf->UDegree(),
2025 theSurf->VDegree() };
2026 Standard_Integer aNbKnots[2] = { theSurf->NbUKnots(),
2027 theSurf->NbVKnots() };
2028 TColStd_Array1OfReal aKnotsU(1, aNbKnots[0]);
2029 TColStd_Array1OfReal aKnotsV(1, aNbKnots[1]);
2031 theSurf->UKnots(aKnotsU);
2032 theSurf->VKnots(aKnotsV);
2034 Standard_Integer iLmI;
2035 Standard_Integer iMin[2] = { -1, -1 };
2036 Standard_Integer iMax[2] = { -1, -1 };
2037 Standard_Integer aNbGridPnts[2];
2038 Standard_Real aFPar[2] = { theFirstU, theFirstV};
2039 Standard_Real aLPar[2] = { theLastU, theLastV};
2040 Standard_Real aFpTol[2] = { aFPar[0] + theTolerance,
2041 aFPar[1] + theTolerance };
2042 Standard_Real aFmTol[2] = { aFPar[0] - theTolerance,
2043 aFPar[1] - theTolerance };
2044 Standard_Real aLpTol[2] = { aLPar[0] + theTolerance,
2045 aLPar[1] + theTolerance };
2046 Standard_Real aLmTol[2] = { aLPar[0] - theTolerance,
2047 aLPar[1] - theTolerance };
2050 // Compute number of U and V grid points.
2051 for (j = 0; j < 2; j++) {
2052 const TColStd_Array1OfReal &aKnots = (j == 0) ? aKnotsU : aKnotsV;
2054 for (i = 1; i <= aNbKnots[j] && (iMin[j] == -1 || iMax[j] == -1); i++) {
2055 if (iMin[j] == -1 && aFpTol[j] < aKnots.Value(i))
2058 iLmI = aNbKnots[j] - i + 1;
2060 if (iMax[j] == -1 && aLmTol[j] > aKnots.Value(iLmI))
2064 // If indices are not found, return.
2065 //if (iMin[j] == -1 || iMax[j] == -1)
2071 iMax[j] = aNbKnots[j];
2076 if (iMax[j] > aNbKnots[j])
2077 iMax[j] = aNbKnots[j];
2079 if (iMax[j] < iMin[j])
2082 if (iMax[j] == iMin[j]) {
2087 if (iMax[j] > aNbKnots[j])
2088 iMax[j] = aNbKnots[j];
2092 aNbGridPnts[j] = (iMax[j] - iMin[j])*aNbSamples[j] + 1;
2094 // Setting the number of grid points.
2096 theSurfaceData.SetRangeUGrid(aNbGridPnts[j]);
2098 theSurfaceData.SetRangeVGrid(aNbGridPnts[j]);
2100 // Setting the first and last parameters.
2101 Standard_Integer iAbs = 1;
2102 Standard_Real aMinPar;
2103 Standard_Real aMaxPar = (j == 0) ? theLastU : theLastV;
2105 for (i = iMin[j]; i < iMax[j]; i++) {
2106 // Get the first parameter.
2109 if (aFmTol[j] > aKnots.Value(iMin[j]))
2112 aMinPar = aKnots.Value(iMin[j]);
2114 aMinPar = aKnots.Value(i);
2117 // Get the last parameter.
2118 if (i == iMax[j] - 1) {
2120 if (aLpTol[j] < aKnots.Value(iMax[j]))
2123 aMaxPar = aKnots.Value(iMax[j]);
2125 aMaxPar = aKnots.Value(i + 1);
2128 // Compute grid parameters.
2129 Standard_Real aDelta = (aMaxPar - aMinPar)/aNbSamples[j];
2131 for (k = 0; k < aNbSamples[j]; k++, aMinPar += aDelta) {
2133 theSurfaceData.SetUParam(iAbs++, aMinPar);
2135 theSurfaceData.SetVParam(iAbs++, aMinPar);
2139 // Add the last parameter
2141 theSurfaceData.SetUParam(iAbs++, aMaxPar);
2143 theSurfaceData.SetVParam(iAbs++, aMaxPar);
2146 // Compute of grid points.
2148 Standard_Real aParU;
2149 Standard_Real aParV;
2151 for (i = 1; i <= aNbGridPnts[0]; i++) {
2152 aParU = theSurfaceData.GetUParam(i);
2154 for (j = 1; j <= aNbGridPnts[1]; j++) {
2155 aParV = theSurfaceData.GetVParam(j);
2157 theSurf->D0(aParU, aParV, aPnt);
2158 theSurfaceData.SetGridPoint(i, j, aPnt);
2162 // Compute deflection.
2163 Standard_Real aDef = 0.;
2164 // Standard_Real aDefLin;
2165 // Standard_Real aParMid;
2166 // Standard_Real aParConst;
2167 // Standard_Real aDistPP;
2172 // // Compute DU deflection.
2173 // for (i = 1; i < aNbGridPnts[0]; i++) {
2174 // aParMid = 0.5*(theSurfaceData.GetUParam(i + 1) +
2175 // theSurfaceData.GetUParam(i));
2177 // for (j = 1; j <= aNbGridPnts[1]; j++) {
2178 // const gp_Pnt &thePnt1 = theSurfaceData.GetGridPoint(i, j);
2179 // const gp_Pnt &thePnt2 = theSurfaceData.GetGridPoint(i + 1, j);
2181 // aVec.SetXYZ(thePnt2.XYZ().Subtracted(thePnt1.XYZ()));
2182 // aDistPP = aVec.Magnitude();
2184 // if (aDistPP > theTolerance) {
2185 // // Computation of a distance of a middle point from the line P1 - P2.
2186 // aParConst = theSurfaceData.GetVParam(j);
2187 // theSurf->D0(aParMid, aParConst, aPntMid);
2188 // aCoord = aPntMid.XYZ();
2189 // aCoord.Subtract(thePnt1.XYZ());
2190 // aCoord.Cross (aVec.XYZ());
2191 // aCoord.Divide(aDistPP);
2192 // aDefLin = aCoord.Modulus();
2194 // if (aDefLin > aDef)
2200 // // Compute DV deflection.
2201 // for (j = 1; j < aNbGridPnts[1]; j++) {
2202 // aParMid = 0.5*(theSurfaceData.GetVParam(j + 1) +
2203 // theSurfaceData.GetVParam(j));
2205 // for (i = 1; i <= aNbGridPnts[0]; i++) {
2206 // const gp_Pnt &thePnt1 = theSurfaceData.GetGridPoint(i, j);
2207 // const gp_Pnt &thePnt2 = theSurfaceData.GetGridPoint(i, j + 1);
2209 // aVec.SetXYZ(thePnt2.XYZ().Subtracted(thePnt1.XYZ()));
2210 // aDistPP = aVec.Magnitude();
2212 // if (aDistPP > theTolerance) {
2213 // // Computation of a distance of a middle point from the line P1 - P2.
2214 // aParConst = theSurfaceData.GetUParam(i);
2215 // theSurf->D0(aParConst, aParMid, aPntMid);
2216 // aCoord = aPntMid.XYZ();
2217 // aCoord.Subtract(thePnt1.XYZ());
2218 // aCoord.Cross (aVec.XYZ());
2219 // aCoord.Divide(aDistPP);
2220 // aDefLin = aCoord.Modulus();
2222 // if (aDefLin > aDef)
2228 if (theTolerance > aDef)
2229 aDef = theTolerance;
2232 theSurfaceData.SetGridDeflection(aDef);
2235 // ---------------------------------------------------------------------------------
2236 // static function: BuildBox
2237 // purpose: Compute bounding box.
2238 // ---------------------------------------------------------------------------------
2239 void BuildBox(const Handle(Geom_BSplineSurface) &theSurf,
2240 const Standard_Real theFirstU,
2241 const Standard_Real theLastU,
2242 const Standard_Real theFirstV,
2243 const Standard_Real theLastV,
2244 IntTools_SurfaceRangeLocalizeData &theSurfaceData,
2249 Standard_Integer aNbUPnts;
2250 Standard_Integer aNbVPnts;
2251 Standard_Real aParam;
2254 theSurfaceData.SetFrame(theFirstU, theLastU, theFirstV, theLastV);
2255 aNbUPnts = theSurfaceData.GetNBUPointsInFrame();
2256 aNbVPnts = theSurfaceData.GetNBVPointsInFrame();
2258 // Add corner points.
2259 theSurf->D0(theFirstU, theFirstV, aPnt);
2261 theSurf->D0(theLastU, theFirstV, aPnt);
2263 theSurf->D0(theFirstU, theLastV, aPnt);
2265 theSurf->D0(theLastU, theLastV, aPnt);
2268 for (i = 1; i <= aNbUPnts; i++) {
2269 // Add top and bottom points.
2270 aParam = theSurfaceData.GetUParamInFrame(i);
2271 theSurf->D0(aParam, theFirstV, aPnt);
2273 theSurf->D0(aParam, theLastV, aPnt);
2276 // Add internal points.
2277 for (j = 1; j <= aNbVPnts; j++) {
2278 const gp_Pnt &aGridPnt = theSurfaceData.GetPointInFrame(i, j);
2280 theBox.Add(aGridPnt);
2284 // Add left and right points.
2285 for (j = 1; j <= aNbVPnts; j++) {
2286 aParam = theSurfaceData.GetVParamInFrame(j);
2287 theSurf->D0(theFirstU, aParam, aPnt);
2289 theSurf->D0(theLastU, aParam, aPnt);
2293 theBox.Enlarge(theSurfaceData.GetGridDeflection());
2295 // Modified by skv - Wed Nov 2 15:21:11 2005 Optimization End
2298 // ---------------------------------------------------------------------------------
2299 // static function: MergeSolutions
2301 // ---------------------------------------------------------------------------------
2302 static void MergeSolutions(const IntTools_ListOfCurveRangeSample& theListCurveRange,
2303 const IntTools_ListOfSurfaceRangeSample& theListSurfaceRange,
2304 IntTools_ListOfCurveRangeSample& theListCurveRangeSort,
2305 IntTools_ListOfSurfaceRangeSample& theListSurfaceRangeSort) {
2307 IntTools_ListIteratorOfListOfCurveRangeSample anItC2;
2308 IntTools_ListIteratorOfListOfSurfaceRangeSample anItS1(theListSurfaceRange), anItS2;
2309 IntTools_MapOfSurfaceSample aMapToAvoid;
2311 for(; anItS1.More(); anItS1.Next()) {
2312 const IntTools_SurfaceRangeSample& aRangeS = anItS1.Value();
2314 if(aMapToAvoid.Contains(aRangeS))
2316 aMapToAvoid.Add(aRangeS);
2318 anItC2.Initialize(theListCurveRange);
2319 anItS2.Initialize(theListSurfaceRange);
2321 for(; anItS2.More() && anItC2.More(); anItS2.Next(), anItC2.Next()) {
2322 if(aRangeS.IsEqual(anItS2.Value())) {
2323 theListCurveRangeSort.Append(anItC2.Value());
2324 theListSurfaceRangeSort.Append(anItS2.Value());
2330 // ---------------------------------------------------------------------------------
2331 // static function: CheckSampling
2333 // ---------------------------------------------------------------------------------
2334 static void CheckSampling(const IntTools_CurveRangeSample& theCurveRange,
2335 const IntTools_SurfaceRangeSample& theSurfaceRange,
2336 const IntTools_CurveRangeLocalizeData& theCurveData,
2337 const IntTools_SurfaceRangeLocalizeData& theSurfaceData,
2338 const Standard_Real DiffC,
2339 const Standard_Real DiffU,
2340 const Standard_Real DiffV,
2341 Standard_Boolean& bAllowSamplingC,
2342 Standard_Boolean& bAllowSamplingU,
2343 Standard_Boolean& bAllowSamplingV) {
2345 const Standard_Real dLimit = 1000;
2346 bAllowSamplingC = Standard_True;
2347 bAllowSamplingU = Standard_True;
2348 bAllowSamplingV = Standard_True;
2351 if((pow((Standard_Real)theCurveData.GetNbSample(), (Standard_Real )(theCurveRange.GetDepth() + 1)) > dLimit) ||
2352 ((DiffC / theCurveData.GetNbSample()) < theCurveData.GetMinRange())) {
2353 bAllowSamplingC = Standard_False;
2356 if((pow((Standard_Real )theSurfaceData.GetNbSampleU(), (Standard_Real )(theSurfaceRange.GetDepthU() + 1)) > dLimit) ||
2357 ((DiffU / theSurfaceData.GetNbSampleU()) < theSurfaceData.GetMinRangeU())) {
2358 bAllowSamplingU = Standard_False;
2362 if((pow((Standard_Real )theSurfaceData.GetNbSampleV(), (Standard_Real )(theSurfaceRange.GetDepthV() + 1)) > dLimit) ||
2363 ((DiffV / theSurfaceData.GetNbSampleV()) < theSurfaceData.GetMinRangeV())) {
2364 bAllowSamplingV = Standard_False;