1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
19 #include <IntTools_BeanFaceIntersector.ixx>
21 #include <IntTools_Root.hxx>
22 #include <Precision.hxx>
23 #include <Extrema_POnCurv.hxx>
24 #include <Extrema_POnSurf.hxx>
25 #include <BRep_Tool.hxx>
26 #include <Geom_Surface.hxx>
27 #include <TColStd_Array1OfReal.hxx>
28 #include <TColStd_Array1OfBoolean.hxx>
29 #include <TColStd_ListOfInteger.hxx>
30 #include <TColStd_ListIteratorOfListOfInteger.hxx>
31 #include <IntTools_EdgeFace.hxx>
32 #include <IntTools_ListOfCurveRangeSample.hxx>
33 #include <IntTools_ListOfSurfaceRangeSample.hxx>
34 #include <IntTools_ListOfBox.hxx>
35 #include <IntTools_ListIteratorOfListOfBox.hxx>
36 #include <IntTools_ListIteratorOfListOfCurveRangeSample.hxx>
37 #include <IntTools_ListIteratorOfListOfSurfaceRangeSample.hxx>
38 #include <IntTools_MapIteratorOfMapOfCurveSample.hxx>
39 #include <TColgp_Array1OfPnt2d.hxx>
41 #include <Geom_Curve.hxx>
42 #include <Geom_Surface.hxx>
43 #include <Geom_BSplineSurface.hxx>
44 #include <GeomAdaptor_Curve.hxx>
45 #include <GeomAdaptor_Surface.hxx>
46 #include <Extrema_ExtCS.hxx>
47 #include <Extrema_ExtPS.hxx>
48 #include <IntTools.hxx>
49 #include <BOPInt_Context.hxx>
50 #include <IntTools_Tools.hxx>
51 #include <GeomAPI_ProjectPointOnCurve.hxx>
52 #include <IntCurveSurface_HInter.hxx>
53 #include <IntCurveSurface_IntersectionPoint.hxx>
54 #include <IntCurveSurface_IntersectionSegment.hxx>
55 #include <IntAna_QuadQuadGeo.hxx>
56 #include <BRepAdaptor_HCurve.hxx>
57 #include <BRepAdaptor_HSurface.hxx>
58 #include <Extrema_GenLocateExtPS.hxx>
59 #include <Extrema_GenExtCS.hxx>
60 #include <Bnd_Box.hxx>
61 #include <BndLib_AddSurface.hxx>
62 #include <BndLib_Add3dCurve.hxx>
65 #include <BOPTools_AlgoTools.hxx>
67 static Standard_Boolean AdjustPeriodic(const Standard_Real U,
68 const Standard_Real UFirst,
69 const Standard_Real ULast,
70 const Standard_Real Period,
71 Standard_Real& UResult);
73 static Standard_Boolean SetEmptyResultRange(const Standard_Real theParameter,
74 IntTools_MarkedRangeSet& theMarkedRange);
76 // static Standard_Boolean TestCoinside(const BRepAdaptor_Curve& theCurve,
77 // const BRepAdaptor_Surface& theSurface);
79 // Modified by skv - Wed Nov 2 15:21:11 2005 Optimization Begin
80 static Bnd_Box GetSurfaceBox
81 (const Handle(Geom_BSplineSurface) &theSurf,
82 const Standard_Real theFirstU,
83 const Standard_Real theLastU,
84 const Standard_Real theFirstV,
85 const Standard_Real theLastV,
86 const Standard_Real theTolerance,
87 IntTools_SurfaceRangeLocalizeData &theSurfaceData);
89 static void ComputeGridPoints
90 (const Handle(Geom_BSplineSurface) &theSurf,
91 const Standard_Real theFirstU,
92 const Standard_Real theLastU,
93 const Standard_Real theFirstV,
94 const Standard_Real theLastV,
95 const Standard_Real theTolerance,
96 IntTools_SurfaceRangeLocalizeData &theSurfaceData);
98 static void BuildBox(const Handle(Geom_BSplineSurface) &theSurf,
99 const Standard_Real theFirstU,
100 const Standard_Real theLastU,
101 const Standard_Real theFirstV,
102 const Standard_Real theLastV,
103 IntTools_SurfaceRangeLocalizeData &theSurfaceData,
105 // Modified by skv - Wed Nov 2 15:21:11 2005 Optimization End
107 static void MergeSolutions(const IntTools_ListOfCurveRangeSample& theListCurveRange,
108 const IntTools_ListOfSurfaceRangeSample& theListSurfaceRange,
109 IntTools_ListOfCurveRangeSample& theListCurveRangeSort,
110 IntTools_ListOfSurfaceRangeSample& theListSurfaceRangeSort);
112 static void CheckSampling(const IntTools_CurveRangeSample& theCurveRange,
113 const IntTools_SurfaceRangeSample& theSurfaceRange,
114 const IntTools_CurveRangeLocalizeData& theCurveData,
115 const IntTools_SurfaceRangeLocalizeData& theSurfaceData,
116 const Standard_Real DiffC,
117 const Standard_Real DiffU,
118 const Standard_Real DiffV,
119 Standard_Boolean& bAllowSamplingC,
120 Standard_Boolean& bAllowSamplingU,
121 Standard_Boolean& bAllowSamplingV);
122 // ==================================================================================
123 // function: IntTools_BeanFaceIntersector
125 // ==================================================================================
126 IntTools_BeanFaceIntersector::IntTools_BeanFaceIntersector() :
127 myFirstParameter(0.),
136 myIsDone(Standard_False)
138 myCriteria = Precision::Confusion();
139 myCurveResolution = Precision::PConfusion();
143 // ==================================================================================
144 // function: IntTools_BeanFaceIntersector
146 // ==================================================================================
147 IntTools_BeanFaceIntersector::IntTools_BeanFaceIntersector(const TopoDS_Edge& theEdge,
148 const TopoDS_Face& theFace) :
149 myFirstParameter(0.),
158 myIsDone(Standard_False)
160 Init(theEdge, theFace);
163 // ==================================================================================
164 // function: IntTools_BeanFaceIntersector
166 // ==================================================================================
167 IntTools_BeanFaceIntersector::IntTools_BeanFaceIntersector(const BRepAdaptor_Curve& theCurve,
168 const BRepAdaptor_Surface& theSurface,
169 const Standard_Real theBeanTolerance,
170 const Standard_Real theFaceTolerance) :
171 myFirstParameter(0.),
178 myIsDone(Standard_False)
180 Init(theCurve, theSurface, theBeanTolerance, theFaceTolerance);
183 // ==================================================================================
184 // function: IntTools_BeanFaceIntersector
186 // ==================================================================================
187 IntTools_BeanFaceIntersector::IntTools_BeanFaceIntersector(const BRepAdaptor_Curve& theCurve,
188 const BRepAdaptor_Surface& theSurface,
189 const Standard_Real theFirstParOnCurve,
190 const Standard_Real theLastParOnCurve,
191 const Standard_Real theUMinParameter,
192 const Standard_Real theUMaxParameter,
193 const Standard_Real theVMinParameter,
194 const Standard_Real theVMaxParameter,
195 const Standard_Real theBeanTolerance,
196 const Standard_Real theFaceTolerance) :
197 myFirstParameter(theFirstParOnCurve),
198 myLastParameter(theLastParOnCurve),
199 myUMinParameter(theUMinParameter),
200 myUMaxParameter(theUMaxParameter),
201 myVMinParameter(theVMinParameter),
202 myVMaxParameter(theVMaxParameter),
203 myBeanTolerance(theBeanTolerance),
204 myFaceTolerance(theFaceTolerance),
206 myIsDone(Standard_False)
210 myCriteria = myBeanTolerance + myFaceTolerance;
211 myCurveResolution = myCurve.Resolution(myCriteria);
213 mySurface = theSurface;
214 myTrsfSurface = Handle(Geom_Surface)::DownCast(mySurface.Surface().Surface()->Transformed(mySurface.Trsf()));
217 // ==================================================================================
220 // ==================================================================================
221 void IntTools_BeanFaceIntersector::Init(const TopoDS_Edge& theEdge,
222 const TopoDS_Face& theFace)
224 myCurve.Initialize(theEdge);
225 mySurface.Initialize(theFace);
226 myTrsfSurface = Handle(Geom_Surface)::DownCast(mySurface.Surface().Surface()->Transformed(mySurface.Trsf()));
227 myBeanTolerance = BRep_Tool::Tolerance(theEdge);
228 myFaceTolerance = BRep_Tool::Tolerance(theFace);
230 myCriteria = myBeanTolerance + myFaceTolerance;
231 myCurveResolution = myCurve.Resolution(myCriteria);
233 SetSurfaceParameters(mySurface.FirstUParameter(), mySurface.LastUParameter(),
234 mySurface.FirstVParameter(), mySurface.LastVParameter());
238 // ==================================================================================
241 // ==================================================================================
242 void IntTools_BeanFaceIntersector::Init(const BRepAdaptor_Curve& theCurve,
243 const BRepAdaptor_Surface& theSurface,
244 const Standard_Real theBeanTolerance,
245 const Standard_Real theFaceTolerance)
248 mySurface = theSurface;
249 myTrsfSurface = Handle(Geom_Surface)::DownCast(mySurface.Surface().Surface()->Transformed(mySurface.Trsf()));
250 myBeanTolerance = theBeanTolerance;
251 myFaceTolerance = theFaceTolerance;
253 myCriteria = myBeanTolerance + myFaceTolerance;
254 myCurveResolution = myCurve.Resolution(myCriteria);
256 SetSurfaceParameters(mySurface.FirstUParameter(), mySurface.LastUParameter(),
257 mySurface.FirstVParameter(), mySurface.LastVParameter());
261 // ==================================================================================
264 // ==================================================================================
265 void IntTools_BeanFaceIntersector::Init(const BRepAdaptor_Curve& theCurve,
266 const BRepAdaptor_Surface& theSurface,
267 const Standard_Real theFirstParOnCurve,
268 const Standard_Real theLastParOnCurve,
269 const Standard_Real theUMinParameter,
270 const Standard_Real theUMaxParameter,
271 const Standard_Real theVMinParameter,
272 const Standard_Real theVMaxParameter,
273 const Standard_Real theBeanTolerance,
274 const Standard_Real theFaceTolerance)
276 Init(theCurve, theSurface, theBeanTolerance, theFaceTolerance);
277 SetBeanParameters(theFirstParOnCurve, theLastParOnCurve);
278 SetSurfaceParameters(theUMinParameter, theUMaxParameter, theVMinParameter, theVMaxParameter);
281 // ==================================================================================
282 // function: SetContext
284 // ==================================================================================
285 void IntTools_BeanFaceIntersector::SetContext(const Handle(BOPInt_Context)& theContext)
287 myContext = theContext;
289 // ==================================================================================
292 // ==================================================================================
293 const Handle(BOPInt_Context)& IntTools_BeanFaceIntersector::Context()const
298 // ==================================================================================
299 // function: SetBeanParameters
301 // ==================================================================================
302 void IntTools_BeanFaceIntersector::SetBeanParameters(const Standard_Real theFirstParOnCurve,
303 const Standard_Real theLastParOnCurve)
305 myFirstParameter = theFirstParOnCurve;
306 myLastParameter = theLastParOnCurve;
309 // ==================================================================================
310 // function: SetSurfaceParameters
312 // ==================================================================================
313 void IntTools_BeanFaceIntersector::SetSurfaceParameters(const Standard_Real theUMinParameter,
314 const Standard_Real theUMaxParameter,
315 const Standard_Real theVMinParameter,
316 const Standard_Real theVMaxParameter)
318 myUMinParameter = theUMinParameter;
319 myUMaxParameter = theUMaxParameter;
320 myVMinParameter = theVMinParameter;
321 myVMaxParameter = theVMaxParameter;
324 // ==================================================================================
327 // ==================================================================================
328 void IntTools_BeanFaceIntersector::Perform()
330 myIsDone = Standard_False;
332 Standard_Integer bRet;
333 Standard_Integer aDiscretization = 30;
334 Standard_Real aRelativeDeflection = 0.01;
335 myDeflection = aRelativeDeflection;
337 if (myContext.IsNull()) {
338 myContext=new BOPInt_Context;
341 if(myCurve.GetType()==GeomAbs_Line && mySurface.GetType()==GeomAbs_Plane) {
346 if(myCurve.GetType()==GeomAbs_Line) {
348 myDeflection = Precision::Confusion();
351 if(myCurve.GetType()==GeomAbs_Circle) {
352 aDiscretization = 23;
353 Standard_Real R = myCurve.Circle().Radius();
354 myDeflection = aRelativeDeflection * R;
356 if(myCurve.GetType() == GeomAbs_Ellipse) {
357 aDiscretization = 23;
358 Standard_Real R = myCurve.Ellipse().MajorRadius();
359 myDeflection = 2 * aRelativeDeflection * R;
362 // modified by NIZHNY-MKK Wed Oct 19 12:15:21 2005
363 Standard_Boolean bLocalize = Standard_False;
365 if(((mySurface.GetType() == GeomAbs_BSplineSurface) &&
366 ((mySurface.UDegree() > 2) || (mySurface.VDegree() > 2)) &&
367 //modified by NIZNHY-PKV Wed Feb 25 15:02:00 2009f
368 //((mySurface.NbUKnots() > 2) || (mySurface.NbVKnots() > 2))) ||
369 ((mySurface.NbUKnots() > 2) && (mySurface.NbVKnots() > 2))) ||
370 //modified by NIZNHY-PKV Wed Feb 25 15:02:13 2009t
371 (mySurface.GetType() == GeomAbs_BezierSurface) ||
372 (mySurface.GetType() == GeomAbs_OtherSurface)) {
373 bLocalize = Standard_True;
377 if(Precision::IsInfinite(myUMinParameter) ||
378 Precision::IsInfinite(myUMaxParameter) ||
379 Precision::IsInfinite(myVMinParameter) ||
380 Precision::IsInfinite(myVMaxParameter))
381 bLocalize = Standard_False;
383 Standard_Boolean bSuccessLocalize = Standard_False;
386 myRangeManager.SetBoundaries(myFirstParameter, myLastParameter, 0);
387 Standard_Boolean coinside = TestComputeCoinside();
390 bSuccessLocalize = ComputeLocalized();
393 if(!bLocalize || !bSuccessLocalize) {
394 // modified by NIZHNY-MKK Wed Oct 19 12:15:26 2005.END
396 IntTools_CArray1OfReal aParams;
398 if(IntTools::PrepareArgs(myCurve,
407 myRangeManager.SetRanges(aParams, 0);
409 if(myRangeManager.Length()==0) {
413 bRet=FastComputeExactIntersection();
415 IntTools_Range aRange(myFirstParameter, myLastParameter);
416 myResults.Append(aRange);
417 myIsDone = Standard_True;
420 //modified by NIZHNY-EMV Fri Apr 20 09:38:08 2012
421 else if (bRet == 2) {
422 myIsDone = Standard_True;
425 //modified by NIZHNY-EMV Fri Apr 20 09:38:10 2012
428 // Standard_Boolean coinside = TestCoinside(myCurve,mySurface);
429 Standard_Boolean coinside = TestComputeCoinside();
431 // myRangeManager.InsertRange(myFirstParameter, myLastParameter, 2);
435 ComputeAroundExactIntersection();
437 ComputeUsingExtremum();
439 ComputeNearRangeBoundaries();
443 myIsDone = Standard_True;
445 for(Standard_Integer i = 1; i <= myRangeManager.Length(); i++) {
447 if(myRangeManager.Flag(i) == 2) {
448 IntTools_Range aRange = myRangeManager.Range(i);
450 if(myResults.Length() > 0) {
451 const IntTools_Range& aLastRange = myResults.Last();
453 if(Abs(aRange.First() - aLastRange.Last()) > Precision::PConfusion()) {
454 myResults.Append(aRange);
457 myResults.ChangeValue(myResults.Length()).SetLast(aRange.Last());
461 myResults.Append(aRange);
467 // ==================================================================================
470 // ==================================================================================
471 const IntTools_SequenceOfRanges& IntTools_BeanFaceIntersector::Result() const
476 // ==================================================================================
479 // ==================================================================================
480 void IntTools_BeanFaceIntersector::Result(IntTools_SequenceOfRanges& theResults) const
482 theResults = myResults;
485 // ==================================================================================
486 // function: Distance
488 // ==================================================================================
489 Standard_Real IntTools_BeanFaceIntersector::Distance(const Standard_Real theArg)
491 gp_Pnt aPoint = myCurve.Value(theArg);
493 GeomAPI_ProjectPointOnSurf& aProjector = myContext->ProjPS(mySurface.Face());
494 aProjector.Perform(aPoint);
496 if(aProjector.IsDone() && aProjector.NbPoints() > 0) {
497 return aProjector.LowerDistance();
500 Standard_Real aDistance = RealLast();
502 for(Standard_Integer i=0; i < 4; i++) {
503 Standard_Real anIsoParameter = (i==0) ? myUMinParameter : ((i==1) ? myUMaxParameter : ((i==2) ? myVMinParameter : myVMaxParameter));
504 Standard_Real aMinParameter = (i < 2) ? myVMinParameter : myUMinParameter;
505 Standard_Real aMaxParameter = (i < 2) ? myVMaxParameter : myUMaxParameter;
506 Standard_Real aMidParameter = (aMinParameter + aMaxParameter) * 0.5;
507 gp_Pnt aPointMin = (i < 2) ? mySurface.Value(anIsoParameter, aMinParameter) : mySurface.Value(aMinParameter, anIsoParameter);
508 gp_Pnt aPointMax = (i < 2) ? mySurface.Value(anIsoParameter, aMaxParameter) : mySurface.Value(aMaxParameter, anIsoParameter);
509 gp_Pnt aPointMid = (i < 2) ? mySurface.Value(anIsoParameter, aMidParameter) : mySurface.Value(aMidParameter, anIsoParameter);
511 Standard_Boolean useMinMaxPoints = Standard_True;
512 Standard_Boolean computeisoline = Standard_True;
514 if(aPointMin.IsEqual(aPointMax, myCriteria) &&
515 aPointMin.IsEqual(aPointMid, myCriteria) &&
516 aPointMax.IsEqual(aPointMid, myCriteria)) {
517 computeisoline = Standard_False;
521 Handle(Geom_Curve) aCurve = (i < 2) ? myTrsfSurface->UIso(anIsoParameter) : myTrsfSurface->VIso(anIsoParameter);
522 GeomAPI_ProjectPointOnCurve aProjectorOnCurve(aPoint, aCurve, aMinParameter, aMaxParameter);
524 if(aProjectorOnCurve.NbPoints() > 0) {
525 useMinMaxPoints = Standard_False;
527 if(aDistance > aProjectorOnCurve.LowerDistance())
528 aDistance = aProjectorOnCurve.LowerDistance();
532 if(useMinMaxPoints) {
533 Standard_Real aPPDistance = aPoint.Distance(aPointMin);
534 aDistance = (aPPDistance < aDistance) ? aPPDistance : aDistance;
535 aPPDistance = aPoint.Distance(aPointMax);
536 aDistance = (aPPDistance < aDistance) ? aPPDistance : aDistance;
543 // ==================================================================================
544 // function: Distance
546 // ==================================================================================
547 Standard_Real IntTools_BeanFaceIntersector::Distance(const Standard_Real theArg,
548 Standard_Real& theUParameter,
549 Standard_Real& theVParameter)
551 gp_Pnt aPoint = myCurve.Value(theArg);
553 theUParameter = myUMinParameter;
554 theVParameter = myVMinParameter;
556 Standard_Real aDistance = RealLast();
557 Standard_Boolean projectionfound = Standard_False;
559 GeomAPI_ProjectPointOnSurf& aProjector = myContext->ProjPS(mySurface.Face());
560 aProjector.Perform(aPoint);
562 if(aProjector.IsDone() && aProjector.NbPoints() > 0) {
563 aProjector.LowerDistanceParameters(theUParameter, theVParameter);
564 aDistance = aProjector.LowerDistance();
565 projectionfound = Standard_True;
568 if(!projectionfound) {
570 for(Standard_Integer i = 0; i < 4; i++) {
571 Standard_Real anIsoParameter = (i==0) ? myUMinParameter : ((i==1) ? myUMaxParameter : ((i==2) ? myVMinParameter : myVMaxParameter));
572 Standard_Real aMinParameter = (i < 2) ? myVMinParameter : myUMinParameter;
573 Standard_Real aMaxParameter = (i < 2) ? myVMaxParameter : myUMaxParameter;
574 Standard_Real aMidParameter = (aMinParameter + aMaxParameter) * 0.5;
575 gp_Pnt aPointMin = (i < 2) ? mySurface.Value(anIsoParameter, aMinParameter) : mySurface.Value(aMinParameter, anIsoParameter);
576 gp_Pnt aPointMax = (i < 2) ? mySurface.Value(anIsoParameter, aMaxParameter) : mySurface.Value(aMaxParameter, anIsoParameter);
577 gp_Pnt aPointMid = (i < 2) ? mySurface.Value(anIsoParameter, aMidParameter) : mySurface.Value(aMidParameter, anIsoParameter);
579 Standard_Boolean useMinMaxPoints = Standard_True;
580 Standard_Boolean computeisoline = Standard_True;
582 if(aPointMin.IsEqual(aPointMax, myCriteria) &&
583 aPointMin.IsEqual(aPointMid, myCriteria) &&
584 aPointMax.IsEqual(aPointMid, myCriteria)) {
585 computeisoline = Standard_False;
589 Handle(Geom_Curve) aCurve = (i < 2) ? myTrsfSurface->UIso(anIsoParameter) : myTrsfSurface->VIso(anIsoParameter);
590 GeomAPI_ProjectPointOnCurve aProjectorOnCurve(aPoint, aCurve, aMinParameter, aMaxParameter);
592 if(aProjectorOnCurve.NbPoints() > 0) {
593 useMinMaxPoints = Standard_False;
595 if(aDistance > aProjectorOnCurve.LowerDistance()) {
596 theUParameter = (i<=1) ? anIsoParameter : aProjectorOnCurve.LowerDistanceParameter();
597 theVParameter = (i>=2) ? anIsoParameter : aProjectorOnCurve.LowerDistanceParameter();
598 aDistance = aProjectorOnCurve.LowerDistance();
603 if(useMinMaxPoints) {
604 Standard_Real aPPDistance = aPoint.Distance(aPointMin);
606 if(aPPDistance < aDistance) {
607 theUParameter = (i<=1) ? anIsoParameter : aMinParameter;
608 theVParameter = (i>=2) ? anIsoParameter : aMinParameter;
609 aDistance = aPPDistance;
611 aPPDistance = aPoint.Distance(aPointMax);
613 if(aPPDistance < aDistance) {
614 theUParameter = (i<=1) ? anIsoParameter : aMaxParameter;
615 theVParameter = (i>=2) ? anIsoParameter : aMaxParameter;
616 aDistance = aPPDistance;
621 theUParameter = (myUMinParameter > theUParameter) ? myUMinParameter : theUParameter;
622 theUParameter = (myUMaxParameter < theUParameter) ? myUMaxParameter : theUParameter;
623 theVParameter = (myVMinParameter > theVParameter) ? myVMinParameter : theVParameter;
624 theVParameter = (myVMaxParameter < theVParameter) ? myVMaxParameter : theVParameter;
629 // ==================================================================================
630 // function: ComputeAroundExactIntersection
632 // ==================================================================================
633 void IntTools_BeanFaceIntersector::ComputeAroundExactIntersection()
635 IntCurveSurface_HInter anExactIntersector;
637 Handle(BRepAdaptor_HCurve) aCurve = new BRepAdaptor_HCurve(myCurve);
638 Handle(BRepAdaptor_HSurface) aSurface = new BRepAdaptor_HSurface(mySurface);
640 anExactIntersector.Perform(aCurve, aSurface);
642 if(anExactIntersector.IsDone()) {
643 Standard_Integer i = 0;
645 for(i = 1; i <= anExactIntersector.NbPoints(); i++) {
646 const IntCurveSurface_IntersectionPoint& aPoint = anExactIntersector.Point(i);
648 if((aPoint.W() >= myFirstParameter) && (aPoint.W() <= myLastParameter)) {
649 Standard_Boolean UIsNotValid = ((myUMinParameter > aPoint.U()) || (aPoint.U() > myUMaxParameter));
650 Standard_Boolean VIsNotValid = ((myVMinParameter > aPoint.V()) || (aPoint.V() > myVMaxParameter));
651 Standard_Boolean solutionIsValid = !UIsNotValid && !VIsNotValid;
652 Standard_Real U = aPoint.U();
653 Standard_Real V = aPoint.V();
655 if(UIsNotValid || VIsNotValid) {
656 // modified by NIZHNY-MKK Thu Jun 17 12:50:39 2004
657 Standard_Boolean bUCorrected = Standard_True;
660 // modified by NIZHNY-MKK Thu Jun 17 12:50:37 2004
661 bUCorrected = Standard_False;
662 solutionIsValid = Standard_False;
664 if(mySurface.IsUPeriodic()) {
665 Standard_Real aNewU = U;
667 if(AdjustPeriodic(U, myUMinParameter, myUMaxParameter, mySurface.UPeriod(), aNewU)) {
668 solutionIsValid = Standard_True;
669 // modified by NIZHNY-MKK Thu Jun 17 12:51:01 2004
670 bUCorrected = Standard_True;
675 // modified by NIZHNY-MKK Thu Jun 17 12:51:03 2004
676 // if(solutionIsValid && VIsNotValid) {
677 if(bUCorrected && VIsNotValid) {
678 solutionIsValid = Standard_False;
680 if(mySurface.IsVPeriodic()) {
681 Standard_Real aNewV = V;
683 if(AdjustPeriodic(V, myVMinParameter, myVMaxParameter, mySurface.VPeriod(), aNewV)) {
684 solutionIsValid = Standard_True;
694 Standard_Integer aNbRanges = myRangeManager.Length();
696 ComputeRangeFromStartPoint(Standard_False, aPoint.W(), U, V);
697 ComputeRangeFromStartPoint(Standard_True, aPoint.W(), U, V);
699 if(aNbRanges == myRangeManager.Length()) {
700 SetEmptyResultRange(aPoint.W(), myRangeManager);
701 } // end if(aNbRanges == myRangeManager.Length())
705 for(i = 1; i <= anExactIntersector.NbSegments(); i++) {
706 const IntCurveSurface_IntersectionSegment& aSegment = anExactIntersector.Segment(i);
707 IntCurveSurface_IntersectionPoint aPoint1, aPoint2;
708 aSegment.Values(aPoint1, aPoint2);
710 Standard_Real aFirstParameter = (aPoint1.W() < myFirstParameter) ? myFirstParameter : aPoint1.W();
711 Standard_Real aLastParameter = (myLastParameter < aPoint2.W()) ? myLastParameter : aPoint2.W();
713 myRangeManager.InsertRange(aFirstParameter, aLastParameter, 2);
715 ComputeRangeFromStartPoint(Standard_False, aPoint1.W(), aPoint1.U(), aPoint1.V());
716 ComputeRangeFromStartPoint(Standard_True, aPoint2.W(), aPoint2.U(), aPoint2.V());
721 // ==================================================================================
722 // function: FastComputeExactIntersection
724 // ==================================================================================
725 Standard_Integer IntTools_BeanFaceIntersector::FastComputeExactIntersection()
727 Standard_Integer aresult;
728 GeomAbs_CurveType aCT;
729 GeomAbs_SurfaceType aST;
732 aCT=myCurve.GetType();
733 aST=mySurface.GetType();
735 if((aCT==GeomAbs_BezierCurve) ||
736 (aCT==GeomAbs_BSplineCurve) ||
737 (aCT==GeomAbs_OtherCurve)) {
741 if(aST==GeomAbs_Plane) {
742 gp_Pln surfPlane = mySurface.Plane();
744 if(aCT==GeomAbs_Line) {
745 if((surfPlane.Distance(myCurve.Value(myFirstParameter)) < myCriteria) &&
746 (surfPlane.Distance(myCurve.Value(myLastParameter)) < myCriteria)) {
754 case GeomAbs_Circle: {
755 aDir = myCurve.Circle().Axis().Direction();
758 case GeomAbs_Ellipse: {
759 aDir = myCurve.Ellipse().Axis().Direction();
762 case GeomAbs_Hyperbola: {
763 aDir = myCurve.Hyperbola().Axis().Direction();
766 case GeomAbs_Parabola: {
767 aDir = myCurve.Parabola().Axis().Direction();
775 Standard_Real anAngle = aDir.Angle(surfPlane.Axis().Direction());
777 if(anAngle < Precision::Angular()) {
778 Standard_Boolean insertRange = Standard_False;
781 case GeomAbs_Circle: {
782 Standard_Real adist =
783 surfPlane.Distance(myCurve.Circle().Location()) +
784 myCurve.Circle().Radius() * Precision::Angular();
786 if(adist < myCriteria) {
787 insertRange = Standard_True;
791 case GeomAbs_Ellipse: {
792 Standard_Real adist =
793 surfPlane.Distance(myCurve.Ellipse().Location()) +
794 myCurve.Ellipse().MajorRadius() * Precision::Angular();
796 if(adist < myCriteria) {
797 insertRange = Standard_True;
801 case GeomAbs_Hyperbola:
802 case GeomAbs_Parabola: {
803 Standard_Real aMaxPar =
804 (Abs(myFirstParameter) > Abs(myLastParameter)) ?
805 Abs(myFirstParameter) : Abs(myLastParameter);
807 gp_Pnt aLoc = (aCT == GeomAbs_Parabola) ?
808 myCurve.Parabola().Location() :
809 myCurve.Hyperbola().Location();
810 Standard_Real adist = aLoc.Distance(myCurve.Value(aMaxPar));
811 adist = surfPlane.Distance(aLoc) + adist * Precision::Angular();
813 if(adist < myCriteria) {
814 insertRange = Standard_True;
826 }//if(anAngle < Precision::Angular()) {
828 }// if(aST==GeomAbs_Plane) {
830 if(aCT==GeomAbs_Circle) {
831 gp_Circ aCircle = myCurve.Circle();
833 if(aST==GeomAbs_Cylinder) {
834 gp_Cylinder aCylinder = mySurface.Cylinder();
835 gp_Dir aDir1(aCylinder.Axis().Direction());
836 gp_Dir aDir2(aCircle.Axis().Direction());
837 Standard_Real anAngle = aDir1.Angle(aDir2);
839 if(anAngle < Precision::Angular()) {
840 gp_Pnt aLoc = aCircle.Location();
841 gp_Lin anCylAxis(aCylinder.Axis());
842 Standard_Real alocdist = anCylAxis.Distance(aLoc);
843 Standard_Real adist = alocdist;
844 Standard_Real adiff = aCircle.Radius() - aCylinder.Radius();
847 if(adist < myCriteria) {
848 Standard_Real acylradius = aCylinder.Radius();
849 Standard_Real atmpvalue = aCircle.Radius() * sin(Precision::Angular());
850 Standard_Real aprojectedradius = atmpvalue;
852 sqrt((aCircle.Radius() * aCircle.Radius())
853 - (aprojectedradius * aprojectedradius));
854 adiff = aprojectedradius - acylradius;
855 adist = alocdist + Abs(adiff);
857 if(adist < myCriteria) { // Abs is important function here
862 }// if(aST==GeomAbs_Cylinder)
864 if(aST==GeomAbs_Sphere) {
865 gp_Pln aCirclePln(aCircle.Location(), aCircle.Axis().Direction());
866 IntAna_QuadQuadGeo anInter(aCirclePln, mySurface.Sphere());
868 if(anInter.IsDone()) {
869 if(anInter.TypeInter() == IntAna_Circle) {
870 gp_Circ aCircleToCompare = anInter.Circle(1);
871 Standard_Real adist =
872 aCircleToCompare.Location().Distance(aCircle.Location());
873 Standard_Real adiff = aCircle.Radius() - aCircleToCompare.Radius();
876 if(adist < myCriteria) {
881 }// if(aST==GeomAbs_Sphere) {
882 }// if(aCT==GeomAbs_Circle) {
884 //modified by NIZNHY-PKV Thu Mar 01 11:54:04 2012f
885 if(aST==GeomAbs_Cylinder) {
889 aCyl=mySurface.Cylinder();
891 const gp_Ax1& aAx1C=aCyl.Axis();
892 const gp_Dir& aDirC=aAx1C.Direction();
894 if(aCT==GeomAbs_Line) {
895 Standard_Real aCos, aAng2, aTolang2;
900 const gp_Dir& aDirL=aLin.Direction();
902 aCos=aDirC.Dot(aDirL);
904 aAng2 = 2.*(1. - aCos);
907 aAng2 = 2.*(1. + aCos);
910 if(aAng2<=aTolang2) {// IsParallel = Standard_True;
911 Standard_Boolean bFlag;
917 aPL[0]=myCurve.Value(myFirstParameter);
918 aPL[1]=myCurve.Value(myLastParameter);
920 for (i=0; i<2; ++i) {
921 aD=aLC.Distance(aPL[i]);
923 bFlag=(aD > myCriteria);
933 }//if(aCT==GeomAbs_Line) {
935 //modified by NIZNHY-PKV Thu Mar 01 11:54:06 2012t
937 //modified by NIZHNY-EMV Fri May 17 09:48:49 2013
939 IntTools_Range aRange(myFirstParameter, myLastParameter);
940 const TopoDS_Face& aF = mySurface.Face();
941 const TopoDS_Edge& aE = myCurve.Edge();
943 if (BOPTools_AlgoTools::IsBlockInOnFace(aRange, aF, aE, myContext)) {
944 myRangeManager.InsertRange(aRange, 2);
949 //modified by NIZHNY-EMV Fri May 17 09:48:53 2013
954 // ==================================================================================
955 // function: ComputeLinePlane
957 // ==================================================================================
958 void IntTools_BeanFaceIntersector::ComputeLinePlane()
960 Standard_Real Tolang = 1.e-9;
961 gp_Pln P = mySurface.Plane();
962 gp_Lin L = myCurve.Line();
964 myIsDone = Standard_True;
966 Standard_Real A,B,C,D;
967 Standard_Real Al,Bl,Cl;
968 Standard_Real Dis,Direc;
970 P.Coefficients(A,B,C,D);
971 gp_Pnt Orig(L.Location());
972 L.Direction().Coord(Al,Bl,Cl);
974 Direc=A*Al+B*Bl+C*Cl;
975 Dis = A*Orig.X() + B*Orig.Y() + C*Orig.Z() + D;
977 Standard_Boolean parallel = Standard_False, inplane = Standard_False;
978 if (Abs(Direc) < Tolang) {
979 parallel= Standard_True;
980 if (Abs(Dis) < myCriteria) {
981 inplane=Standard_True;
984 inplane=Standard_False;
988 gp_Pnt p1 = ElCLib::Value(myFirstParameter, L);
989 gp_Pnt p2 = ElCLib::Value(myLastParameter, L);
990 Standard_Real d1 = A*p1.X() + B*p1.Y() + C*p1.Z() + D;
992 Standard_Real d2 = A*p2.X() + B*p2.Y() + C*p2.Z() + D;
994 if(d1 <= myCriteria && d2 <= myCriteria) {
995 inplane=Standard_True;
1000 IntTools_Range aRange(myFirstParameter, myLastParameter);
1001 myResults.Append(aRange);
1009 Standard_Real t = - Dis/Direc;
1010 if(t < myFirstParameter || t > myLastParameter) {
1014 gp_Pnt pint(Orig.X()+t*Al, Orig.Y()+t*Bl, Orig.Z()+t*Cl);
1017 ElSLib::Parameters(P, pint, u, v);
1018 if(myUMinParameter > u || u > myUMaxParameter || myVMinParameter > v || v > myVMaxParameter) {
1022 Standard_Real t1 = Max(myFirstParameter, t-myCriteria);
1023 Standard_Real t2 = Min(myLastParameter, t+myCriteria);
1024 IntTools_Range aRange(t1, t2);
1025 myResults.Append(aRange);
1032 // ==================================================================================
1033 // function: ComputeUsingExtremum
1035 // ==================================================================================
1036 void IntTools_BeanFaceIntersector::ComputeUsingExtremum()
1038 Standard_Real Tol, af, al;
1039 Tol = Precision::PConfusion();
1040 Handle(Geom_Curve) aCurve = BRep_Tool::Curve (myCurve.Edge(), af, al);
1041 GeomAdaptor_Surface aGASurface (myTrsfSurface,
1048 BndLib_AddSurface::Add(mySurface, 0., FBox);
1049 FBox.Enlarge(myFaceTolerance);
1051 for(Standard_Integer i = 1; i <= myRangeManager.Length(); i++) {
1053 if(myRangeManager.Flag(i) > 0)
1056 IntTools_Range aParamRange = myRangeManager.Range(i);
1057 Standard_Real anarg1 = aParamRange.First();
1058 Standard_Real anarg2 = aParamRange.Last();
1060 if(anarg2 - anarg1 < Precision::PConfusion()) {
1062 if(((i > 1) && (myRangeManager.Flag(i-1) == 2)) ||
1063 ((i < myRangeManager.Length()) && (myRangeManager.Flag(i+1) == 2))) {
1064 myRangeManager.SetFlag(i, 1);
1069 // check bounding boxes
1071 EBox.Add(myCurve.Value(anarg1));
1072 EBox.Add(myCurve.Value(anarg2));
1073 EBox.Enlarge(myBeanTolerance + myDeflection);
1075 if(EBox.IsOut(FBox)) {
1076 myRangeManager.SetFlag(i, 1);
1080 GeomAdaptor_Curve aGACurve(aCurve, anarg1, anarg2);
1081 Extrema_ExtCS theExtCS(aGACurve, aGASurface, Tol, Tol);
1082 myExtrema = theExtCS;
1084 if(myExtrema.IsDone() && (myExtrema.NbExt() || myExtrema.IsParallel())) {
1085 Standard_Integer anOldNbRanges = myRangeManager.Length();
1087 if (myExtrema.IsParallel()) {
1089 if(myExtrema.SquareDistance(1) < myCriteria * myCriteria) {
1090 Standard_Real U1, V1, U2, V2;
1091 Standard_Real adistance1 = Distance(anarg1, U1, V1);
1092 Standard_Real adistance2 = Distance(anarg2, U2, V2);
1093 Standard_Boolean validdistance1 = (adistance1 < myCriteria);
1094 Standard_Boolean validdistance2 = (adistance2 < myCriteria);
1096 if (validdistance1 && validdistance2) {
1097 myRangeManager.InsertRange(anarg1, anarg2, 2);
1101 if(validdistance1) {
1102 ComputeRangeFromStartPoint(Standard_True, anarg1, U1, V1);
1105 if(validdistance2) {
1106 ComputeRangeFromStartPoint(Standard_False, anarg2, U2, V2);
1109 Standard_Real a = anarg1;
1110 Standard_Real b = anarg2;
1111 Standard_Real da = adistance1;
1112 Standard_Real db = adistance2;
1113 Standard_Real asolution = a;
1114 Standard_Boolean found = Standard_False;
1116 while(((b - a) > myCurveResolution) && !found) {
1117 asolution = (a+b)*0.5;
1118 Standard_Real adist = Distance(asolution, U1, V1);
1120 if(adist < myCriteria) {
1121 found = Standard_True;
1136 ComputeRangeFromStartPoint(Standard_False, asolution, U1, V1);
1137 ComputeRangeFromStartPoint(Standard_True, asolution, U1, V1);
1140 myRangeManager.SetFlag(i, 1);
1147 myRangeManager.SetFlag(i, 1);
1151 Standard_Boolean solutionfound = Standard_False;
1153 for(Standard_Integer j = 1 ; j <= myExtrema.NbExt(); j++) {
1155 if(myExtrema.SquareDistance(j) < myCriteria * myCriteria) {
1158 myExtrema.Points(j, p1, p2);
1162 Standard_Integer aNbRanges = myRangeManager.Length();
1163 ComputeRangeFromStartPoint(Standard_False, p1.Parameter(), U, V);
1164 ComputeRangeFromStartPoint(Standard_True, p1.Parameter(), U, V);
1165 solutionfound = Standard_True;
1167 if(aNbRanges == myRangeManager.Length()) {
1168 SetEmptyResultRange(p1.Parameter(), myRangeManager);
1173 if(!solutionfound) {
1174 myRangeManager.SetFlag(i, 1);
1177 Standard_Integer adifference = myRangeManager.Length() - anOldNbRanges;
1179 if(adifference > 0) {
1182 } // end if(myExtrema.IsDone() && (myExtrema.NbExt() || myExtrema.IsParallel()))
1186 // ==================================================================================
1187 // function: ComputeNearRangeBoundaries
1189 // ==================================================================================
1190 void IntTools_BeanFaceIntersector::ComputeNearRangeBoundaries()
1192 Standard_Real U = myUMinParameter;
1193 Standard_Real V = myVMinParameter;
1195 for(Standard_Integer i = 1; i <= myRangeManager.Length(); i++) {
1197 if(myRangeManager.Flag(i) > 0)
1200 if((i > 1) && (myRangeManager.Flag(i-1) > 0))
1203 IntTools_Range aParamRange = myRangeManager.Range(i);
1205 if(Distance(aParamRange.First(), U, V) < myCriteria) {
1206 Standard_Integer aNbRanges = myRangeManager.Length();
1209 ComputeRangeFromStartPoint(Standard_False, aParamRange.First(), U, V, i-1);
1211 ComputeRangeFromStartPoint(Standard_True, aParamRange.First(), U, V, i + (myRangeManager.Length() - aNbRanges));
1213 if(aNbRanges == myRangeManager.Length()) {
1214 SetEmptyResultRange(aParamRange.First(), myRangeManager);
1219 if(myRangeManager.Flag(myRangeManager.Length()) == 0) {
1220 IntTools_Range aParamRange = myRangeManager.Range(myRangeManager.Length());
1222 if(Distance(aParamRange.Last(), U, V) < myCriteria) {
1223 Standard_Integer aNbRanges = myRangeManager.Length();
1225 ComputeRangeFromStartPoint(Standard_False, aParamRange.Last(), U, V, myRangeManager.Length());
1227 if(aNbRanges == myRangeManager.Length()) {
1228 SetEmptyResultRange(aParamRange.Last(), myRangeManager);
1234 // ==================================================================================
1235 // function: ComputeRangeFromStartPoint
1236 // purpose: Compute range using start point according to parameter theParameter,
1237 // increasing parameter on curve if ToIncreaseParameter == Standard_True or
1238 // decreasing parameter on curve if ToIncreaseParameter == Standard_False
1239 // ==================================================================================
1240 void IntTools_BeanFaceIntersector::ComputeRangeFromStartPoint(const Standard_Boolean ToIncreaseParameter,
1241 const Standard_Real theParameter,
1242 const Standard_Real theUParameter,
1243 const Standard_Real theVParameter)
1245 Standard_Integer aFoundIndex = myRangeManager.GetIndex(theParameter, ToIncreaseParameter);
1247 if(aFoundIndex == 0) {
1251 ComputeRangeFromStartPoint(ToIncreaseParameter, theParameter, theUParameter, theVParameter, aFoundIndex);
1254 // ==================================================================================
1255 // function: ComputeRangeFromStartPoint
1256 // purpose: Compute range using start point according to parameter theParameter,
1257 // increasing parameter on curve if ToIncreaseParameter == Standard_True or
1258 // decreasing parameter on curve if ToIncreaseParameter == Standard_False.
1259 // theIndex indicate that theParameter belong the range number theIndex.
1260 // ==================================================================================
1261 void IntTools_BeanFaceIntersector::ComputeRangeFromStartPoint(const Standard_Boolean ToIncreaseParameter,
1262 const Standard_Real theParameter,
1263 const Standard_Real theUParameter,
1264 const Standard_Real theVParameter,
1265 const Standard_Integer theIndex)
1267 if(myRangeManager.Flag(theIndex) > 0)
1270 Standard_Integer aValidIndex = theIndex;
1272 Standard_Real aMinDelta = myCurveResolution * 0.5;
1273 Standard_Real aDeltaRestrictor = myLastParameter - myFirstParameter;
1275 if(aMinDelta > aDeltaRestrictor)
1276 aMinDelta = aDeltaRestrictor * 0.5;
1278 Standard_Real tenOfMinDelta = aMinDelta * 10.;
1279 Standard_Real aDelta = myCurveResolution;
1281 Standard_Real aCurPar = (ToIncreaseParameter) ? (theParameter + aDelta) : (theParameter - aDelta);
1282 Standard_Real aPrevPar = theParameter;
1283 IntTools_Range aCurrentRange = myRangeManager.Range(aValidIndex);
1285 Standard_Boolean BoundaryCondition = (ToIncreaseParameter) ? (aCurPar > aCurrentRange.Last()) : (aCurPar < aCurrentRange.First());
1287 if(BoundaryCondition) {
1288 aCurPar = (ToIncreaseParameter) ? aCurrentRange.Last() : aCurrentRange.First();
1289 BoundaryCondition = Standard_False;
1292 Standard_Integer loopcounter = 0; // neccesary as infinite loop restricter
1293 Standard_Real U = theUParameter;
1294 Standard_Real V = theVParameter;
1295 Standard_Boolean anotherSolutionFound = Standard_False;
1297 Standard_Boolean isboundaryindex = Standard_False;
1298 Standard_Boolean isvalidindex = Standard_True;
1300 while((aDelta >= aMinDelta) && (loopcounter <= 10)) {
1301 Standard_Boolean pointfound = Standard_False;
1304 gp_Pnt aPoint = myCurve.Value(aCurPar);
1305 Extrema_GenLocateExtPS anExtrema(aPoint, mySurface, U, V, 1.e-10, 1.e-10);
1307 if(anExtrema.IsDone()) {
1308 if(anExtrema.SquareDistance() < myCriteria * myCriteria) {
1309 Extrema_POnSurf aPOnSurf = anExtrema.Point();
1310 aPOnSurf.Parameter(U, V);
1311 pointfound = Standard_True;
1315 pointfound = (Distance(aCurPar) < myCriteria);
1320 anotherSolutionFound = Standard_True;
1322 if(BoundaryCondition && (isboundaryindex || !isvalidindex))
1326 aDeltaRestrictor = aDelta;
1329 // if point found decide to increase aDelta using derivative of distance function
1332 aDelta = (pointfound) ? (aDelta * 2.) : (aDelta * 0.5);
1333 aDelta = (aDelta < aDeltaRestrictor) ? aDelta : aDeltaRestrictor;
1335 aCurPar = (ToIncreaseParameter) ? (aPrevPar + aDelta) : (aPrevPar - aDelta);
1338 // prevent infinite loop when (aPrevPar +/- aDelta) == aPrevPar == 0.
1341 if( aCurPar == aPrevPar )
1344 BoundaryCondition = (ToIncreaseParameter) ? (aCurPar > aCurrentRange.Last()) : (aCurPar < aCurrentRange.First());
1346 isboundaryindex = Standard_False;
1347 isvalidindex = Standard_True;
1349 if(BoundaryCondition) {
1350 isboundaryindex = ((!ToIncreaseParameter && (aValidIndex == 1)) ||
1351 (ToIncreaseParameter && (aValidIndex == myRangeManager.Length())));
1353 if(!isboundaryindex) {
1356 Standard_Integer aFlag = (ToIncreaseParameter) ? myRangeManager.Flag(aValidIndex + 1) : myRangeManager.Flag(aValidIndex - 1);
1359 aValidIndex = (ToIncreaseParameter) ? (aValidIndex + 1) : (aValidIndex - 1);
1360 aCurrentRange = myRangeManager.Range(aValidIndex);
1362 if((ToIncreaseParameter && (aCurPar > aCurrentRange.Last())) ||
1363 (!ToIncreaseParameter && (aCurPar < aCurrentRange.First()))) {
1364 aCurPar = (aCurrentRange.First() + aCurrentRange.Last()) * 0.5;
1369 isvalidindex = Standard_False;
1370 aCurPar = (ToIncreaseParameter) ? aCurrentRange.Last() : aCurrentRange.First();
1375 aCurPar = (ToIncreaseParameter) ? aCurrentRange.Last() : aCurrentRange.First();
1378 if(aDelta < tenOfMinDelta) {
1384 } // if(BoundaryCondition)
1387 if(anotherSolutionFound) {
1388 if(ToIncreaseParameter)
1389 myRangeManager.InsertRange(theParameter, aPrevPar, 2);
1391 myRangeManager.InsertRange(aPrevPar, theParameter, 2);
1395 // ---------------------------------------------------------------------------------
1396 // static function: AdjustPeriodic
1398 // ---------------------------------------------------------------------------------
1399 static Standard_Boolean AdjustPeriodic(const Standard_Real U,
1400 const Standard_Real UFirst,
1401 const Standard_Real ULast,
1402 const Standard_Real Period,
1403 Standard_Real& UResult) {
1405 Standard_Real u = U;
1406 Standard_Real Eps = Epsilon(Period);
1407 while (Eps < (UFirst-u)) u += Period;
1408 while (Eps > (ULast -u)) u -= Period;
1410 return Standard_False;
1413 return Standard_True;
1416 // ---------------------------------------------------------------------------------
1417 // static function: SetEmptyResultRange
1419 // ---------------------------------------------------------------------------------
1420 static Standard_Boolean SetEmptyResultRange(const Standard_Real theParameter,
1421 IntTools_MarkedRangeSet& theMarkedRange) {
1423 const TColStd_SequenceOfInteger& anIndices = theMarkedRange.GetIndices(theParameter);
1424 Standard_Boolean add = (anIndices.Length() > 0);
1426 for(Standard_Integer k = 1; k <= anIndices.Length(); k++) {
1427 if(theMarkedRange.Flag(anIndices(k)) == 2) {
1428 add = Standard_False;
1434 theMarkedRange.InsertRange(theParameter, theParameter, 2);
1440 // ---------------------------------------------------------------------------------
1441 // static function: TestCoinside
1443 // ---------------------------------------------------------------------------------
1444 // static Standard_Boolean TestClose(const Extrema_ExtPS & theExt,
1445 // const Standard_Real theDist)
1447 // Standard_Boolean close = Standard_False;
1448 // if(!theExt.IsDone() || theExt.NbExt() == 0)
1451 // Standard_Integer ie;
1452 // for(ie = 1; ie <= theExt.NbExt(); ie++) {
1453 // Standard_Real dist = theExt.Value(ie);
1454 // if(dist <= theDist) {
1455 // close = Standard_True;
1463 // Standard_Boolean TestCoinside(const BRepAdaptor_Curve& theCurve,
1464 // const BRepAdaptor_Surface& theSurface)
1466 // Standard_Real cfp = theCurve.FirstParameter(), clp = theCurve.LastParameter();
1467 // Standard_Real cdp = fabs(clp - cfp) / 23.;
1469 // Standard_Integer i = 0;
1470 // Standard_Real tolE = theCurve.Tolerance(), tolF = theSurface.Tolerance();
1471 // Standard_Real tolT = tolE + tolF, tolU = 1.e-9, tolV = 1.e-9;
1474 // theCurve.D0(cfp,aP);
1475 // Extrema_ExtPS eps(aP,theSurface,tolU,tolV);
1477 // if(!TestClose(eps,tolT))
1478 // return Standard_False;
1480 // theCurve.D0(clp,aP);
1483 // if(!TestClose(eps,tolT))
1484 // return Standard_False;
1486 // Standard_Boolean close = Standard_True;
1488 // for(i = 1; i <= 22; i++) {
1489 // theCurve.D0((cfp+((Standard_Real)i)*cdp),aP);
1491 // if(!TestClose(eps,tolT)) {
1492 // close = Standard_False;
1499 // ======================================================================================================================
1500 // function: LocalizeSolutions
1502 // ======================================================================================================================
1503 Standard_Boolean IntTools_BeanFaceIntersector::LocalizeSolutions(const IntTools_CurveRangeSample& theCurveRange,
1504 const Bnd_Box& theBoxCurve,
1505 const IntTools_SurfaceRangeSample& theSurfaceRange,
1506 const Bnd_Box& theBoxSurface,
1507 IntTools_CurveRangeLocalizeData& theCurveData,
1508 IntTools_SurfaceRangeLocalizeData& theSurfaceData,
1509 IntTools_ListOfCurveRangeSample& theListCurveRange,
1510 IntTools_ListOfSurfaceRangeSample& theListSurfaceRange)
1512 Standard_Integer tIt = 0, uIt = 0, vIt = 0;
1515 IntTools_CurveRangeSample aRootRangeC(0);
1516 aRootRangeC.SetDepth(0);
1517 IntTools_SurfaceRangeSample aRootRangeS(0, 0, 0, 0);
1519 Bnd_Box aMainBoxC = theBoxCurve;
1520 Bnd_Box aMainBoxS = theBoxSurface;
1521 Standard_Boolean bMainBoxFoundS = Standard_False;
1522 Standard_Boolean bMainBoxFoundC = Standard_False;
1524 IntTools_ListOfCurveRangeSample aListCurveRangeFound;
1525 IntTools_ListOfSurfaceRangeSample aListSurfaceRangeFound;
1528 IntTools_Range aRangeC = theCurveRange.GetRange(myFirstParameter, myLastParameter, theCurveData.GetNbSample());
1529 Standard_Real localdiffC = (aRangeC.Last() - aRangeC.First()) / theCurveData.GetNbSample();
1531 Standard_Real aCurPar = aRangeC.First();
1532 Standard_Real aPrevPar = aRangeC.First();
1533 Standard_Integer aCurIndexInit = theCurveRange.GetRangeIndexDeeper(theCurveData.GetNbSample());
1536 TColStd_ListOfInteger aListCToAvoid;
1537 Standard_Boolean bGlobalCheckDone = Standard_False;
1542 Standard_Integer aCurIndexU = theSurfaceRange.GetRangeIndexUDeeper(theSurfaceData.GetNbSampleU());
1544 Standard_Integer aCurIndexVInit = theSurfaceRange.GetRangeIndexVDeeper(theSurfaceData.GetNbSampleV());
1545 IntTools_Range aRangeV = theSurfaceRange.GetRangeV(myVMinParameter, myVMaxParameter, theSurfaceData.GetNbSampleV());
1548 IntTools_Range aRangeU = theSurfaceRange.GetRangeU(myUMinParameter, myUMaxParameter, theSurfaceData.GetNbSampleU());
1549 Standard_Real aCurParU = aRangeU.First();
1550 Standard_Real aLocalDiffU = (aRangeU.Last() - aRangeU.First()) / theSurfaceData.GetNbSampleU();
1552 Standard_Real aPrevParU = aCurParU;
1553 Standard_Real aLocalDiffV = (aRangeV.Last() - aRangeV.First()) / theSurfaceData.GetNbSampleV();
1556 // ranges check.begin
1557 Standard_Boolean bAllowSamplingC = Standard_True;
1558 Standard_Boolean bAllowSamplingU = Standard_True;
1559 Standard_Boolean bAllowSamplingV = Standard_True;
1562 CheckSampling(theCurveRange, theSurfaceRange, theCurveData, theSurfaceData,
1563 localdiffC, aLocalDiffU, aLocalDiffV,
1564 bAllowSamplingC, bAllowSamplingU, bAllowSamplingV);
1567 if(!bAllowSamplingC && !bAllowSamplingU && !bAllowSamplingV) {
1568 theListCurveRange.Append(theCurveRange);
1569 theListSurfaceRange.Append(theSurfaceRange);
1570 return Standard_True;
1574 // init template. begin
1575 IntTools_CurveRangeSample aNewRangeCTemplate;
1577 if(!bAllowSamplingC) {
1578 aNewRangeCTemplate = theCurveRange;
1579 aCurIndexInit = theCurveRange.GetRangeIndex();
1580 localdiffC = (aRangeC.Last() - aRangeC.First());
1583 aNewRangeCTemplate.SetDepth(theCurveRange.GetDepth() + 1);
1584 aNewRangeCTemplate.SetRangeIndex(aCurIndexInit);
1587 IntTools_SurfaceRangeSample aNewRangeSTemplate = theSurfaceRange;
1589 if(bAllowSamplingU) {
1590 aNewRangeSTemplate.SetDepthU(theSurfaceRange.GetDepthU() + 1);
1593 aCurIndexU = aNewRangeSTemplate.GetIndexU();
1594 aLocalDiffU = aRangeU.Last() - aRangeU.First();
1597 if(bAllowSamplingV) {
1598 aNewRangeSTemplate.SetDepthV(theSurfaceRange.GetDepthV() + 1);
1601 aCurIndexVInit = theSurfaceRange.GetIndexV();
1602 aLocalDiffV = aRangeV.Last() - aRangeV.First();
1604 // init template. end
1607 Standard_Boolean bHasOut = Standard_False;
1608 const Standard_Integer nbU = (bAllowSamplingU) ? theSurfaceData.GetNbSampleU() : 1;
1609 const Standard_Integer nbV = (bAllowSamplingV) ? theSurfaceData.GetNbSampleV() : 1;
1610 const Standard_Integer nbC = (bAllowSamplingC) ? theCurveData.GetNbSample() : 1;
1612 for(uIt = 1; uIt <= nbU; uIt++, aCurIndexU++, aPrevParU = aCurParU) {
1613 aCurParU += aLocalDiffU;
1616 Standard_Real aCurParV = aRangeV.First();
1617 Standard_Real aPrevParV = aCurParV;
1618 Standard_Integer aCurIndexV = aCurIndexVInit;
1620 Standard_Boolean bHasOutV = Standard_False;
1623 for(vIt = 1; vIt <= nbV; vIt++, aCurIndexV++, aPrevParV = aCurParV) {
1625 aCurParV += aLocalDiffV;
1630 IntTools_SurfaceRangeSample aNewRangeS = aNewRangeSTemplate;
1632 if(bAllowSamplingU) {
1633 aNewRangeS.SetIndexU(aCurIndexU);
1636 if(bAllowSamplingV) {
1637 aNewRangeS.SetIndexV(aCurIndexV);
1640 if(theSurfaceData.IsRangeOut(aNewRangeS)) {
1641 bHasOutV = Standard_True;
1649 if(!theSurfaceData.FindBox(aNewRangeS, aBoxS)) {
1651 if(mySurface.GetType() == GeomAbs_BSplineSurface) {
1652 // if(Standard_False ) {
1653 Handle(Geom_BSplineSurface) aSurfBspl = Handle(Geom_BSplineSurface)::DownCast(myTrsfSurface);
1654 aBoxS = GetSurfaceBox(aSurfBspl, aPrevParU, aCurParU, aPrevParV, aCurParV, myCriteria, theSurfaceData);
1657 BndLib_AddSurface::Add(mySurface, aPrevParU, aCurParU, aPrevParV, aCurParV, myCriteria, aBoxS);
1659 // Bnd_Box aMainBoxC;
1661 if(!bMainBoxFoundC && theCurveData.FindBox(aRootRangeC, aMainBoxC)) {
1662 bMainBoxFoundC = Standard_True;
1665 if(aBoxS.IsOut(aMainBoxC)) {
1666 theSurfaceData.AddOutRange(aNewRangeS);
1667 bHasOutV = Standard_True;
1671 theSurfaceData.AddBox(aNewRangeS, aBoxS);
1674 if(aBoxS.IsOut(theBoxCurve)) {
1675 bHasOutV = Standard_True;
1679 IntTools_ListOfBox aListOfBox;
1680 TColStd_ListOfInteger aListOfIndex;
1682 Standard_Boolean bHasOutC = Standard_False;
1683 Standard_Integer aCurIndex = aCurIndexInit;
1685 // ////////////////////////////
1686 aCurPar = aRangeC.First();
1687 aPrevPar = aRangeC.First();
1688 IntTools_CurveRangeSample aCurRangeC = aNewRangeCTemplate;
1690 for (tIt = 1; tIt <= nbC; tIt++, aCurIndex++, aPrevPar = aCurPar) {
1692 aCurPar += localdiffC;
1694 // ignore already computed. begin
1695 Standard_Boolean bFound = Standard_False;
1696 TColStd_ListIteratorOfListOfInteger anItToAvoid(aListCToAvoid);
1698 for(; anItToAvoid.More(); anItToAvoid.Next()) {
1699 if(tIt == anItToAvoid.Value()) {
1700 bFound = Standard_True;
1706 if(bAllowSamplingC) {
1707 aCurRangeC.SetRangeIndex(aCurIndex);
1709 bFound = theCurveData.IsRangeOut(aCurRangeC);
1713 bHasOutC = Standard_True;
1716 // ignore already computed. end
1721 if(!theCurveData.FindBox(aCurRangeC, aBoxC)) {
1722 BndLib_Add3dCurve::Add(myCurve, aPrevPar, aCurPar, myCriteria, aBoxC);
1724 // Bnd_Box aMainBoxS;
1726 if(!bMainBoxFoundS && theSurfaceData.FindBox(aRootRangeS, aMainBoxS)) {
1727 bMainBoxFoundS = Standard_True;
1729 if(aBoxC.IsOut(aMainBoxS)) {
1730 theCurveData.AddOutRange(aCurRangeC);
1731 bHasOutC = Standard_True;
1735 theCurveData.AddBox(aCurRangeC, aBoxC);
1738 if(!bGlobalCheckDone && aBoxC.IsOut(theBoxSurface)) {
1739 aListCToAvoid.Append(tIt);
1740 bHasOutC = Standard_True;
1744 if(aBoxC.IsOut(aBoxS)) {
1745 bHasOutV = Standard_True;
1746 bHasOutC = Standard_True;
1751 aListOfIndex.Append(tIt);
1752 aListOfBox.Append(aBoxC);
1753 } // end for(tIt...)
1755 bGlobalCheckDone = Standard_True;
1758 bHasOutV = Standard_True;
1764 IntTools_CurveRangeSample aNewRangeC = aNewRangeCTemplate;
1766 aCurIndex = aCurIndexInit;
1767 TColStd_ListIteratorOfListOfInteger anItI(aListOfIndex);
1768 IntTools_ListIteratorOfListOfBox anItBox(aListOfBox);
1769 Standard_Boolean bUseOldC = Standard_False;
1770 Standard_Boolean bUseOldS = Standard_False;
1771 Standard_Boolean bCheckSize = !bHasOutC;
1773 for(; anItI.More() && anItBox.More(); anItI.Next(), anItBox.Next()) {
1774 aCurIndex = aCurIndexInit + anItI.Value() - 1;
1776 bUseOldS = Standard_False;
1778 if(bAllowSamplingC) {
1779 aNewRangeC.SetRangeIndex(aCurIndex);
1785 if((theCurveRange.GetDepth() == 0) ||
1786 (theSurfaceRange.GetDepthU() == 0) ||
1787 (theSurfaceRange.GetDepthV() == 0)) {
1788 bHasOutC = Standard_True;
1789 bHasOutV = Standard_True;
1791 else if((theCurveRange.GetDepth() < 4) &&
1792 (theSurfaceRange.GetDepthU() < 4) &&
1793 (theSurfaceRange.GetDepthV() < 4)) {
1794 Bnd_Box aBoxC = anItBox.Value();
1796 if(!aBoxC.IsWhole() && !aBoxS.IsWhole()) {
1797 Standard_Real aDiagC = aBoxC.SquareExtent();
1798 Standard_Real aDiagS = aBoxS.SquareExtent();
1800 if(aDiagC < aDiagS) {
1801 if((aDiagC * 10.) < aDiagS) {
1802 bUseOldC = Standard_True;
1803 bHasOutC = Standard_True;
1804 bHasOutV = Standard_True;
1809 if((aDiagS * 10.) < aDiagC) {
1810 bUseOldS = Standard_True;
1811 bHasOutC = Standard_True;
1812 bHasOutV = Standard_True;
1821 aListCurveRangeFound.Append(aNewRangeC);
1822 aListSurfaceRangeFound.Append(aNewRangeS);
1826 // if(bUseOldS || bAllowSamplingU || bAllowSamplingV) {
1827 // theSurfaceData.AddBox(aNewRangeS, aBoxS);
1830 if(bUseOldS && aNewRangeC.IsEqual(theCurveRange)) {
1831 return Standard_False;
1834 if(!LocalizeSolutions(aNewRangeC, anItBox.Value(),
1835 ((bUseOldS) ? theSurfaceRange : aNewRangeS),
1836 ((bUseOldS) ? theBoxSurface : aBoxS),
1837 theCurveData, theSurfaceData,
1838 theListCurveRange, theListSurfaceRange))
1839 return Standard_False;
1843 aListOfIndex.Clear();
1847 // theSurfaceData.AddBox(aNewRangeS, aBoxS);
1849 if(bUseOldC && bAllowSamplingC && (bAllowSamplingU || bAllowSamplingV)) {
1850 if(!LocalizeSolutions(theCurveRange, theBoxCurve,
1852 theCurveData, theSurfaceData,
1853 theListCurveRange, theListSurfaceRange))
1854 return Standard_False;
1857 } // end for (vIt...)
1860 bHasOut = Standard_True;
1865 theListCurveRange.Append(theCurveRange);
1866 theListSurfaceRange.Append(theSurfaceRange);
1869 IntTools_ListIteratorOfListOfCurveRangeSample anIt1(aListCurveRangeFound);
1870 IntTools_ListIteratorOfListOfSurfaceRangeSample anIt2(aListSurfaceRangeFound);
1872 for(; anIt1.More() && anIt2.More(); anIt1.Next(), anIt2.Next()) {
1873 theListCurveRange.Append(anIt1.Value());
1874 theListSurfaceRange.Append(anIt2.Value());
1877 return Standard_True;
1881 // ======================================================================================================================
1882 // function: ComputeLocalized
1884 // ======================================================================================================================
1885 Standard_Boolean IntTools_BeanFaceIntersector::ComputeLocalized() {
1886 Standard_Real Tol = Precision::PConfusion();
1888 IntTools_SurfaceRangeSample aSurfaceRange(0, 0, 0, 0);
1889 Standard_Real dMinU = 10. * Precision::PConfusion();
1890 Standard_Real dMinV = dMinU;
1891 IntTools_SurfaceRangeLocalizeData aSurfaceDataInit(3, 3, dMinU, dMinV);
1892 IntTools_SurfaceRangeLocalizeData& aSurfaceData = myContext->SurfaceData(mySurface.Face());
1893 aSurfaceData.RemoveRangeOutAll();
1894 aSurfaceData.ClearGrid();
1897 Standard_Boolean bFBoxFound = aSurfaceData.FindBox(aSurfaceRange, FBox);
1899 if(mySurface.GetType() == GeomAbs_BSplineSurface) {
1900 Handle(Geom_BSplineSurface) aSurfBspl = Handle(Geom_BSplineSurface)::DownCast(myTrsfSurface);
1902 ComputeGridPoints(aSurfBspl, myUMinParameter, myUMaxParameter,
1903 myVMinParameter, myVMaxParameter, myCriteria,
1907 FBox = GetSurfaceBox(aSurfBspl, myUMinParameter, myUMaxParameter,
1908 myVMinParameter, myVMaxParameter, myCriteria,
1910 aSurfaceData.AddBox(aSurfaceRange, FBox);
1913 } else if(!bFBoxFound) {
1915 BndLib_AddSurface::Add(mySurface, myUMinParameter, myUMaxParameter, myVMinParameter, myVMaxParameter, myFaceTolerance, FBox);
1916 aSurfaceData.AddBox(aSurfaceRange, FBox);
1921 BndLib_Add3dCurve::Add(myCurve.Trim(myFirstParameter, myLastParameter, Precision::PConfusion())->Curve(), myBeanTolerance, EBox);
1923 if(EBox.IsOut(FBox)) {
1924 for(Standard_Integer i = 1; i <= myRangeManager.Length(); i++) {
1925 myRangeManager.SetFlag(i, 1);
1927 aSurfaceData.ClearGrid();
1929 return Standard_True;
1932 IntTools_ListOfCurveRangeSample aListCurveRange;
1933 IntTools_ListOfSurfaceRangeSample aListSurfaceRange;
1935 IntTools_CurveRangeSample aCurveRange(0);
1936 aCurveRange.SetDepth(0);
1937 Standard_Integer nbSampleC = 3;
1938 Standard_Integer nbSampleU = aSurfaceData.GetNbSampleU();
1939 Standard_Integer nbSampleV = aSurfaceData.GetNbSampleV();
1940 Standard_Real dMinC = 10. * myCurveResolution;
1941 IntTools_ListOfCurveRangeSample aListOut;
1944 Standard_Boolean bAllowSamplingC = Standard_True;
1945 Standard_Boolean bAllowSamplingU = Standard_True;
1946 Standard_Boolean bAllowSamplingV = Standard_True;
1947 IntTools_CurveRangeLocalizeData aCurveDataTmp(nbSampleC, dMinC);
1948 IntTools_SurfaceRangeLocalizeData aSurfaceDataTmp(nbSampleU, nbSampleV, dMinU, dMinV);
1950 CheckSampling(aCurveRange, aSurfaceRange, aCurveDataTmp, aSurfaceDataTmp,
1951 myLastParameter - myFirstParameter,
1952 myUMaxParameter - myUMinParameter,
1953 myVMaxParameter - myVMinParameter,
1954 bAllowSamplingC, bAllowSamplingU, bAllowSamplingV);
1958 IntTools_CurveRangeLocalizeData aCurveData(nbSampleC, dMinC);
1960 aCurveData.AddBox(aCurveRange, EBox);
1962 if(!LocalizeSolutions(aCurveRange, EBox, aSurfaceRange, FBox,
1963 aCurveData, aSurfaceData,
1964 aListCurveRange, aListSurfaceRange)) {
1965 aSurfaceData.ClearGrid();
1967 return Standard_False;
1970 IntTools_ListOfCurveRangeSample aListCurveRangeSort;
1971 IntTools_ListOfSurfaceRangeSample aListSurfaceRangeSort;
1973 MergeSolutions(aListCurveRange, aListSurfaceRange, aListCurveRangeSort, aListSurfaceRangeSort);
1975 IntTools_ListIteratorOfListOfCurveRangeSample anItC(aListCurveRangeSort);
1976 IntTools_ListIteratorOfListOfSurfaceRangeSample anItS(aListSurfaceRangeSort);
1977 IntTools_SurfaceRangeSample aRangeSPrev;
1979 Extrema_GenExtCS anExtremaGen;
1981 for(; anItC.More() && anItS.More(); anItC.Next(), anItS.Next()) {
1983 IntTools_Range aRangeC(myFirstParameter, myLastParameter);
1986 aRangeC = anItC.Value().GetRange(myFirstParameter, myLastParameter, nbSampleC);
1988 IntTools_Range aRangeU(myUMinParameter, myUMaxParameter);
1991 aRangeU = anItS.Value().GetRangeU(myUMinParameter, myUMaxParameter, nbSampleU);
1993 IntTools_Range aRangeV(myVMinParameter, myVMaxParameter);
1996 aRangeV = anItS.Value().GetRangeV(myVMinParameter, myVMaxParameter, nbSampleV);
1998 Standard_Real anarg1 = aRangeC.First(), anarg2 = aRangeC.Last();
2000 Standard_Boolean bFound = Standard_False;
2002 Standard_Integer nMinIndex = myRangeManager.Length();
2003 Standard_Integer nMaxIndex = -1;
2004 const TColStd_SequenceOfInteger& anInds1 = myRangeManager.GetIndices(anarg1);
2005 Standard_Integer indIt = 1;
2007 for(indIt = 1 ; indIt <= anInds1.Length(); indIt++) {
2008 Standard_Integer nIndex = anInds1.Value(indIt);
2009 nMinIndex = (nMinIndex > nIndex) ? nIndex : nMinIndex;
2010 nMaxIndex = (nMaxIndex < nIndex) ? nIndex : nMaxIndex;
2013 for(indIt = nMinIndex ; indIt <= nMaxIndex; indIt++) {
2014 if(myRangeManager.Flag(indIt) == 2) {
2015 bFound = Standard_True;
2022 nMinIndex = (nMaxIndex >= 0) ? nMaxIndex : nMinIndex;
2023 const TColStd_SequenceOfInteger& anInds2 = myRangeManager.GetIndices(anarg2);
2025 for(indIt = 1 ; indIt <= anInds2.Length(); indIt++) {
2026 Standard_Integer nIndex = anInds2.Value(indIt);
2027 nMinIndex = (nMinIndex > nIndex) ? nIndex : nMinIndex;
2028 nMaxIndex = (nMaxIndex < nIndex) ? nIndex : nMaxIndex;
2031 for(indIt = nMinIndex ; indIt <= nMaxIndex; indIt++) {
2032 if(myRangeManager.Flag(indIt) == 2) {
2033 bFound = Standard_True;
2041 Standard_Real parUF = aRangeU.First(), parUL = aRangeU.Last();
2042 Standard_Real parVF = aRangeV.First(), parVL = aRangeV.Last();
2044 if(aRangeSPrev.IsEqual(anItS.Value())) {
2045 anExtremaGen.Perform(myCurve, 10, anarg1, anarg2, Tol);
2048 anExtremaGen.Initialize(mySurface, 10, 10, parUF, parUL, parVF, parVL, Tol);
2049 anExtremaGen.Perform(myCurve, 10, anarg1, anarg2, Tol);
2052 if(anExtremaGen.IsDone() && (anExtremaGen.NbExt() > 0)) {
2054 for(Standard_Integer j = 1 ; j <= anExtremaGen.NbExt(); j++) {
2056 if(anExtremaGen.SquareDistance(j) < myCriteria * myCriteria) {
2060 p1 = anExtremaGen.PointOnCurve(j);
2061 p2 = anExtremaGen.PointOnSurface(j);
2062 Standard_Real U, V, T;
2066 if (myCurve.IsPeriodic())
2067 T = ElCLib::InPeriod(T, anarg1, anarg1 + myCurve.Period());
2068 if (mySurface.IsUPeriodic())
2069 U = ElCLib::InPeriod(U, parUF, parUF + mySurface.UPeriod());
2070 if (mySurface.IsVPeriodic())
2071 V = ElCLib::InPeriod(V, parVF, parVF + mySurface.VPeriod());
2073 //To avoid occasional going out of boundaries because of numerical
2075 if(U < myUMinParameter) U = myUMinParameter;
2076 if(U > myUMaxParameter) U = myUMaxParameter;
2077 if(V < myVMinParameter) V = myVMinParameter;
2078 if(V > myVMaxParameter) V = myVMaxParameter;
2080 Standard_Integer aNbRanges = myRangeManager.Length();
2081 ComputeRangeFromStartPoint(Standard_False, T, U, V);
2082 ComputeRangeFromStartPoint(Standard_True, T, U, V);
2084 if(aNbRanges == myRangeManager.Length()) {
2085 SetEmptyResultRange(T, myRangeManager);
2091 myRangeManager.InsertRange(anarg1, anarg2, 0);
2094 aRangeSPrev = anItS.Value();
2098 aCurveData.ListRangeOut(aListOut);
2102 if(bAllowSamplingC) {
2103 IntTools_ListIteratorOfListOfCurveRangeSample anItC(aListOut);
2105 for(; anItC.More(); anItC.Next()) {
2106 IntTools_Range aRangeC =anItC.Value().GetRange(myFirstParameter, myLastParameter, nbSampleC);
2107 myRangeManager.InsertRange(aRangeC.First(), aRangeC.Last(), 1);
2110 ComputeNearRangeBoundaries();
2112 aSurfaceData.ClearGrid();
2114 return Standard_True;
2117 // ======================================================================================================================
2118 // function: TestComputeCoinside
2120 // ======================================================================================================================
2121 Standard_Boolean IntTools_BeanFaceIntersector::TestComputeCoinside()
2123 Standard_Real cfp = myFirstParameter, clp = myLastParameter;
2124 const Standard_Integer nbSeg = 23;
2125 Standard_Real cdp = (clp - cfp) / (Standard_Real )nbSeg;
2127 Standard_Integer i = 0;
2131 if(Distance(cfp, U, V) > myCriteria)
2132 return Standard_False;
2135 ComputeRangeFromStartPoint(Standard_True, cfp, U, V);
2138 Standard_Integer aFoundIndex = myRangeManager.GetIndex(clp, Standard_False );
2140 if(aFoundIndex != 0) {
2141 if(myRangeManager.Flag(aFoundIndex) == 2)
2142 return Standard_True;
2145 if(Distance(clp, U, V) > myCriteria)
2146 return Standard_False;
2149 ComputeRangeFromStartPoint(Standard_False, clp, U, V);
2152 for(i = 1; i < nbSeg; i++) {
2153 Standard_Real aPar = (cfp+((Standard_Real)i)*cdp);
2155 if(Distance(aPar, U, V) > myCriteria)
2156 return Standard_False;
2158 Standard_Integer aNbRanges = myRangeManager.Length();
2159 ComputeRangeFromStartPoint(Standard_False, aPar, U, V);
2160 ComputeRangeFromStartPoint(Standard_True, aPar, U, V);
2162 if(aNbRanges == myRangeManager.Length()) {
2163 SetEmptyResultRange(aPar, myRangeManager);
2167 return Standard_True;
2170 // Modified by skv - Wed Nov 2 15:21:11 2005 Optimization Begin
2171 // ---------------------------------------------------------------------------------
2172 // static function: GetSurfaceBox
2174 // ---------------------------------------------------------------------------------
2175 Bnd_Box GetSurfaceBox(const Handle(Geom_BSplineSurface) &theSurf,
2176 const Standard_Real theFirstU,
2177 const Standard_Real theLastU,
2178 const Standard_Real theFirstV,
2179 const Standard_Real theLastV,
2180 const Standard_Real theTolerance,
2181 IntTools_SurfaceRangeLocalizeData &theSurfaceData)
2185 BuildBox(theSurf, theFirstU, theLastU, theFirstV, theLastV,
2186 theSurfaceData, aTotalBox);
2188 aTotalBox.Enlarge(theTolerance);
2193 // ---------------------------------------------------------------------------------
2194 // static function: ComputeGridPoints
2196 // ---------------------------------------------------------------------------------
2197 void ComputeGridPoints
2198 (const Handle(Geom_BSplineSurface) &theSurf,
2199 const Standard_Real theFirstU,
2200 const Standard_Real theLastU,
2201 const Standard_Real theFirstV,
2202 const Standard_Real theLastV,
2203 const Standard_Real theTolerance,
2204 IntTools_SurfaceRangeLocalizeData &theSurfaceData)
2209 Standard_Integer aNbSamples[2] = { theSurf->UDegree(),
2210 theSurf->VDegree() };
2211 Standard_Integer aNbKnots[2] = { theSurf->NbUKnots(),
2212 theSurf->NbVKnots() };
2213 TColStd_Array1OfReal aKnotsU(1, aNbKnots[0]);
2214 TColStd_Array1OfReal aKnotsV(1, aNbKnots[1]);
2216 theSurf->UKnots(aKnotsU);
2217 theSurf->VKnots(aKnotsV);
2219 Standard_Integer iLmI;
2220 Standard_Integer iMin[2] = { -1, -1 };
2221 Standard_Integer iMax[2] = { -1, -1 };
2222 Standard_Integer aNbGridPnts[2];
2223 Standard_Real aFPar[2] = { theFirstU, theFirstV};
2224 Standard_Real aLPar[2] = { theLastU, theLastV};
2225 Standard_Real aFpTol[2] = { aFPar[0] + theTolerance,
2226 aFPar[1] + theTolerance };
2227 Standard_Real aFmTol[2] = { aFPar[0] - theTolerance,
2228 aFPar[1] - theTolerance };
2229 Standard_Real aLpTol[2] = { aLPar[0] + theTolerance,
2230 aLPar[1] + theTolerance };
2231 Standard_Real aLmTol[2] = { aLPar[0] - theTolerance,
2232 aLPar[1] - theTolerance };
2235 // Compute number of U and V grid points.
2236 for (j = 0; j < 2; j++) {
2237 const TColStd_Array1OfReal &aKnots = (j == 0) ? aKnotsU : aKnotsV;
2239 for (i = 1; i <= aNbKnots[j] && (iMin[j] == -1 || iMax[j] == -1); i++) {
2240 if (iMin[j] == -1 && aFpTol[j] < aKnots.Value(i))
2243 iLmI = aNbKnots[j] - i + 1;
2245 if (iMax[j] == -1 && aLmTol[j] > aKnots.Value(iLmI))
2249 // If indices are not found, return.
2250 //if (iMin[j] == -1 || iMax[j] == -1)
2256 iMax[j] = aNbKnots[j];
2261 if (iMax[j] > aNbKnots[j])
2262 iMax[j] = aNbKnots[j];
2264 if (iMax[j] < iMin[j])
2267 if (iMax[j] == iMin[j]) {
2272 if (iMax[j] > aNbKnots[j])
2273 iMax[j] = aNbKnots[j];
2277 aNbGridPnts[j] = (iMax[j] - iMin[j])*aNbSamples[j] + 1;
2279 // Setting the number of grid points.
2281 theSurfaceData.SetRangeUGrid(aNbGridPnts[j]);
2283 theSurfaceData.SetRangeVGrid(aNbGridPnts[j]);
2285 // Setting the first and last parameters.
2286 Standard_Integer iAbs = 1;
2287 Standard_Real aMinPar;
2288 Standard_Real aMaxPar = (j == 0) ? theLastU : theLastV;
2290 for (i = iMin[j]; i < iMax[j]; i++) {
2291 // Get the first parameter.
2294 if (aFmTol[j] > aKnots.Value(iMin[j]))
2297 aMinPar = aKnots.Value(iMin[j]);
2299 aMinPar = aKnots.Value(i);
2302 // Get the last parameter.
2303 if (i == iMax[j] - 1) {
2305 if (aLpTol[j] < aKnots.Value(iMax[j]))
2308 aMaxPar = aKnots.Value(iMax[j]);
2310 aMaxPar = aKnots.Value(i + 1);
2313 // Compute grid parameters.
2314 Standard_Real aDelta = (aMaxPar - aMinPar)/aNbSamples[j];
2316 for (k = 0; k < aNbSamples[j]; k++, aMinPar += aDelta) {
2318 theSurfaceData.SetUParam(iAbs++, aMinPar);
2320 theSurfaceData.SetVParam(iAbs++, aMinPar);
2324 // Add the last parameter
2326 theSurfaceData.SetUParam(iAbs++, aMaxPar);
2328 theSurfaceData.SetVParam(iAbs++, aMaxPar);
2331 // Compute of grid points.
2333 Standard_Real aParU;
2334 Standard_Real aParV;
2336 for (i = 1; i <= aNbGridPnts[0]; i++) {
2337 aParU = theSurfaceData.GetUParam(i);
2339 for (j = 1; j <= aNbGridPnts[1]; j++) {
2340 aParV = theSurfaceData.GetVParam(j);
2342 theSurf->D0(aParU, aParV, aPnt);
2343 theSurfaceData.SetGridPoint(i, j, aPnt);
2347 // Compute deflection.
2348 Standard_Real aDef = 0.;
2349 // Standard_Real aDefLin;
2350 // Standard_Real aParMid;
2351 // Standard_Real aParConst;
2352 // Standard_Real aDistPP;
2357 // // Compute DU deflection.
2358 // for (i = 1; i < aNbGridPnts[0]; i++) {
2359 // aParMid = 0.5*(theSurfaceData.GetUParam(i + 1) +
2360 // theSurfaceData.GetUParam(i));
2362 // for (j = 1; j <= aNbGridPnts[1]; j++) {
2363 // const gp_Pnt &thePnt1 = theSurfaceData.GetGridPoint(i, j);
2364 // const gp_Pnt &thePnt2 = theSurfaceData.GetGridPoint(i + 1, j);
2366 // aVec.SetXYZ(thePnt2.XYZ().Subtracted(thePnt1.XYZ()));
2367 // aDistPP = aVec.Magnitude();
2369 // if (aDistPP > theTolerance) {
2370 // // Computation of a distance of a middle point from the line P1 - P2.
2371 // aParConst = theSurfaceData.GetVParam(j);
2372 // theSurf->D0(aParMid, aParConst, aPntMid);
2373 // aCoord = aPntMid.XYZ();
2374 // aCoord.Subtract(thePnt1.XYZ());
2375 // aCoord.Cross (aVec.XYZ());
2376 // aCoord.Divide(aDistPP);
2377 // aDefLin = aCoord.Modulus();
2379 // if (aDefLin > aDef)
2385 // // Compute DV deflection.
2386 // for (j = 1; j < aNbGridPnts[1]; j++) {
2387 // aParMid = 0.5*(theSurfaceData.GetVParam(j + 1) +
2388 // theSurfaceData.GetVParam(j));
2390 // for (i = 1; i <= aNbGridPnts[0]; i++) {
2391 // const gp_Pnt &thePnt1 = theSurfaceData.GetGridPoint(i, j);
2392 // const gp_Pnt &thePnt2 = theSurfaceData.GetGridPoint(i, j + 1);
2394 // aVec.SetXYZ(thePnt2.XYZ().Subtracted(thePnt1.XYZ()));
2395 // aDistPP = aVec.Magnitude();
2397 // if (aDistPP > theTolerance) {
2398 // // Computation of a distance of a middle point from the line P1 - P2.
2399 // aParConst = theSurfaceData.GetUParam(i);
2400 // theSurf->D0(aParConst, aParMid, aPntMid);
2401 // aCoord = aPntMid.XYZ();
2402 // aCoord.Subtract(thePnt1.XYZ());
2403 // aCoord.Cross (aVec.XYZ());
2404 // aCoord.Divide(aDistPP);
2405 // aDefLin = aCoord.Modulus();
2407 // if (aDefLin > aDef)
2413 if (theTolerance > aDef)
2414 aDef = theTolerance;
2417 theSurfaceData.SetGridDeflection(aDef);
2420 // ---------------------------------------------------------------------------------
2421 // static function: BuildBox
2422 // purpose: Compute bounding box.
2423 // ---------------------------------------------------------------------------------
2424 void BuildBox(const Handle(Geom_BSplineSurface) &theSurf,
2425 const Standard_Real theFirstU,
2426 const Standard_Real theLastU,
2427 const Standard_Real theFirstV,
2428 const Standard_Real theLastV,
2429 IntTools_SurfaceRangeLocalizeData &theSurfaceData,
2434 Standard_Integer aNbUPnts;
2435 Standard_Integer aNbVPnts;
2436 Standard_Real aParam;
2439 theSurfaceData.SetFrame(theFirstU, theLastU, theFirstV, theLastV);
2440 aNbUPnts = theSurfaceData.GetNBUPointsInFrame();
2441 aNbVPnts = theSurfaceData.GetNBVPointsInFrame();
2443 // Add corner points.
2444 theSurf->D0(theFirstU, theFirstV, aPnt);
2446 theSurf->D0(theLastU, theFirstV, aPnt);
2448 theSurf->D0(theFirstU, theLastV, aPnt);
2450 theSurf->D0(theLastU, theLastV, aPnt);
2453 for (i = 1; i <= aNbUPnts; i++) {
2454 // Add top and bottom points.
2455 aParam = theSurfaceData.GetUParamInFrame(i);
2456 theSurf->D0(aParam, theFirstV, aPnt);
2458 theSurf->D0(aParam, theLastV, aPnt);
2461 // Add internal points.
2462 for (j = 1; j <= aNbVPnts; j++) {
2463 const gp_Pnt &aGridPnt = theSurfaceData.GetPointInFrame(i, j);
2465 theBox.Add(aGridPnt);
2469 // Add left and right points.
2470 for (j = 1; j <= aNbVPnts; j++) {
2471 aParam = theSurfaceData.GetVParamInFrame(j);
2472 theSurf->D0(theFirstU, aParam, aPnt);
2474 theSurf->D0(theLastU, aParam, aPnt);
2478 theBox.Enlarge(theSurfaceData.GetGridDeflection());
2480 // Modified by skv - Wed Nov 2 15:21:11 2005 Optimization End
2483 // ---------------------------------------------------------------------------------
2484 // static function: MergeSolutions
2486 // ---------------------------------------------------------------------------------
2487 static void MergeSolutions(const IntTools_ListOfCurveRangeSample& theListCurveRange,
2488 const IntTools_ListOfSurfaceRangeSample& theListSurfaceRange,
2489 IntTools_ListOfCurveRangeSample& theListCurveRangeSort,
2490 IntTools_ListOfSurfaceRangeSample& theListSurfaceRangeSort) {
2492 IntTools_ListIteratorOfListOfCurveRangeSample anItC2;
2493 IntTools_ListIteratorOfListOfSurfaceRangeSample anItS1(theListSurfaceRange), anItS2;
2494 IntTools_MapOfSurfaceSample aMapToAvoid;
2496 for(; anItS1.More(); anItS1.Next()) {
2497 const IntTools_SurfaceRangeSample& aRangeS = anItS1.Value();
2499 if(aMapToAvoid.Contains(aRangeS))
2501 aMapToAvoid.Add(aRangeS);
2503 anItC2.Initialize(theListCurveRange);
2504 anItS2.Initialize(theListSurfaceRange);
2506 for(; anItS2.More() && anItC2.More(); anItS2.Next(), anItC2.Next()) {
2507 if(aRangeS.IsEqual(anItS2.Value())) {
2508 theListCurveRangeSort.Append(anItC2.Value());
2509 theListSurfaceRangeSort.Append(anItS2.Value());
2515 // ---------------------------------------------------------------------------------
2516 // static function: CheckSampling
2518 // ---------------------------------------------------------------------------------
2519 static void CheckSampling(const IntTools_CurveRangeSample& theCurveRange,
2520 const IntTools_SurfaceRangeSample& theSurfaceRange,
2521 const IntTools_CurveRangeLocalizeData& theCurveData,
2522 const IntTools_SurfaceRangeLocalizeData& theSurfaceData,
2523 const Standard_Real DiffC,
2524 const Standard_Real DiffU,
2525 const Standard_Real DiffV,
2526 Standard_Boolean& bAllowSamplingC,
2527 Standard_Boolean& bAllowSamplingU,
2528 Standard_Boolean& bAllowSamplingV) {
2530 const Standard_Real dLimit = 1000;
2531 bAllowSamplingC = Standard_True;
2532 bAllowSamplingU = Standard_True;
2533 bAllowSamplingV = Standard_True;
2536 if((pow((Standard_Real)theCurveData.GetNbSample(), (Standard_Real )(theCurveRange.GetDepth() + 1)) > dLimit) ||
2537 ((DiffC / theCurveData.GetNbSample()) < theCurveData.GetMinRange())) {
2538 bAllowSamplingC = Standard_False;
2541 if((pow((Standard_Real )theSurfaceData.GetNbSampleU(), (Standard_Real )(theSurfaceRange.GetDepthU() + 1)) > dLimit) ||
2542 ((DiffU / theSurfaceData.GetNbSampleU()) < theSurfaceData.GetMinRangeU())) {
2543 bAllowSamplingU = Standard_False;
2547 if((pow((Standard_Real )theSurfaceData.GetNbSampleV(), (Standard_Real )(theSurfaceRange.GetDepthV() + 1)) > dLimit) ||
2548 ((DiffV / theSurfaceData.GetNbSampleV()) < theSurfaceData.GetMinRangeV())) {
2549 bAllowSamplingV = Standard_False;