1 // Created by: Eugeny MALTCHIKOV
2 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
16 #include <Bnd_Box.hxx>
17 #include <BndLib_Add3dCurve.hxx>
18 #include <BOPCol_MapOfInteger.hxx>
19 #include <BRep_Tool.hxx>
20 #include <BRepAdaptor_Curve.hxx>
22 #include <Geom_BezierCurve.hxx>
23 #include <Geom_BSplineCurve.hxx>
24 #include <Geom_Line.hxx>
25 #include <Geom_Circle.hxx>
26 #include <Geom_Curve.hxx>
27 #include <Geom_Ellipse.hxx>
28 #include <Geom_OffsetCurve.hxx>
29 #include <GeomAPI_ProjectPointOnCurve.hxx>
32 #include <IntTools_CommonPrt.hxx>
33 #include <IntTools_EdgeEdge.hxx>
34 #include <IntTools_Range.hxx>
35 #include <IntTools_Tools.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Iterator.hxx>
40 void BndBuildBox(const BRepAdaptor_Curve& theBAC,
41 const Standard_Real aT1,
42 const Standard_Real aT2,
43 const Standard_Real theTol,
46 Standard_Real PointBoxDistance(const Bnd_Box& aB,
49 Standard_Integer SplitRangeOnSegments(const Standard_Real aT1,
50 const Standard_Real aT2,
51 const Standard_Real theResolution,
52 const Standard_Integer theNbSeg,
53 IntTools_SequenceOfRanges& theSegments);
55 Standard_Integer DistPC(const Standard_Real aT1,
56 const Handle(Geom_Curve)& theC1,
57 const Standard_Real theCriteria,
58 GeomAPI_ProjectPointOnCurve& theProjector,
61 const Standard_Integer iC = 1);
63 Standard_Integer DistPC(const Standard_Real aT1,
64 const Handle(Geom_Curve)& theC1,
65 const Standard_Real theCriteria,
66 GeomAPI_ProjectPointOnCurve& theProjector,
70 Standard_Real& aT1max,
71 Standard_Real& aT2max,
72 const Standard_Integer iC = 1);
74 Standard_Integer FindDistPC(const Standard_Real aT1A,
75 const Standard_Real aT1B,
76 const Handle(Geom_Curve)& theC1,
77 const Standard_Real theCriteria,
78 const Standard_Real theEps,
79 GeomAPI_ProjectPointOnCurve& theProjector,
81 Standard_Real& aT1max,
82 Standard_Real& aT2max,
83 const Standard_Boolean bMaxDist = Standard_True);
85 Standard_Real ResolutionCoeff(const BRepAdaptor_Curve& theBAC,
86 const IntTools_Range& theRange);
88 Standard_Real Resolution(const Handle(Geom_Curve)& theCurve,
89 const GeomAbs_CurveType theCurveType,
90 const Standard_Real theResCoeff,
91 const Standard_Real theR3D);
93 Standard_Real CurveDeflection(const BRepAdaptor_Curve& theBAC,
94 const IntTools_Range& theRange);
96 Standard_Integer IsClosed(const Handle(Geom_Curve)& theCurve,
97 const Standard_Real aT1,
98 const Standard_Real aT2,
99 const Standard_Real theTol,
100 const Standard_Real theRes);
102 Standard_Integer TypeToInteger(const GeomAbs_CurveType theCType);
104 //=======================================================================
107 //=======================================================================
108 void IntTools_EdgeEdge::Prepare()
110 GeomAbs_CurveType aCT1, aCT2;
111 Standard_Integer iCT1, iCT2;
113 myCurve1.Initialize(myEdge1);
114 myCurve2.Initialize(myEdge2);
116 if (myRange1.First() == 0. && myRange1.Last() == 0.) {
117 myRange1.SetFirst(myCurve1.FirstParameter());
118 myRange1.SetLast (myCurve1.LastParameter());
121 if (myRange2.First() == 0. && myRange2.Last() == 0.) {
122 myRange2.SetFirst(myCurve2.FirstParameter());
123 myRange2.SetLast (myCurve2.LastParameter());
126 aCT1 = myCurve1.GetType();
127 aCT2 = myCurve2.GetType();
129 iCT1 = TypeToInteger(aCT1);
130 iCT2 = TypeToInteger(aCT2);
135 Standard_Real aC1, aC2;
137 aC2 = CurveDeflection(myCurve2, myRange2);
138 aC1 = (aC2 > Precision::Confusion()) ?
139 CurveDeflection(myCurve1, myRange1) : 1.;
148 TopoDS_Edge tmpE = myEdge1;
152 BRepAdaptor_Curve tmpC = myCurve1;
156 IntTools_Range tmpR = myRange1;
160 mySwap = Standard_True;
163 Standard_Real aTolAdd = Precision::Confusion() / 2.;
164 myTol1 = myCurve1.Tolerance() + aTolAdd;
165 myTol2 = myCurve2.Tolerance() + aTolAdd;
166 myTol = myTol1 + myTol2;
168 if (iCT1 != 0 || iCT2 != 0) {
169 Standard_Real f, l, aTM;
171 myGeom1 = BRep_Tool::Curve(myEdge1, f, l);
172 myGeom2 = BRep_Tool::Curve(myEdge2, f, l);
174 myResCoeff1 = ResolutionCoeff(myCurve1, myRange1);
175 myResCoeff2 = ResolutionCoeff(myCurve2, myRange2);
177 myRes1 = Resolution(myCurve1.Curve().Curve(), myCurve1.GetType(), myResCoeff1, myTol1);
178 myRes2 = Resolution(myCurve2.Curve().Curve(), myCurve2.GetType(), myResCoeff2, myTol2);
181 aTM = Max(fabs(myRange1.First()), fabs(myRange1.Last()));
183 myPTol1 = 5.e-16 * aTM;
187 aTM = Max(fabs(myRange2.First()), fabs(myRange2.Last()));
189 myPTol2 = 5.e-16 * aTM;
194 //=======================================================================
197 //=======================================================================
198 void IntTools_EdgeEdge::Perform()
209 //3.1. Check Line/Line case
210 if (myCurve1.GetType() == GeomAbs_Line &&
211 myCurve2.GetType() == GeomAbs_Line) {
216 if (myQuickCoincidenceCheck) {
217 if (IsCoincident()) {
218 Standard_Real aT11, aT12, aT21, aT22;
220 myRange1.Range(aT11, aT12);
221 myRange2.Range(aT21, aT22);
222 AddSolution(aT11, aT12, aT21, aT22, TopAbs_EDGE);
227 IntTools_SequenceOfRanges aRanges1, aRanges2;
229 //3.2. Find ranges containig solutions
230 Standard_Boolean bSplit2;
231 FindSolutions(aRanges1, aRanges2, bSplit2);
233 //4. Merge solutions and save common parts
234 MergeSolutions(aRanges1, aRanges2, bSplit2);
237 //=======================================================================
238 //function : IsCoincident
240 //=======================================================================
241 Standard_Boolean IntTools_EdgeEdge::IsCoincident()
243 Standard_Integer i, iCnt, aNbSeg, aNbP2;
244 Standard_Real dT, aT1, aCoeff, aTresh, aD;
245 Standard_Real aT11, aT12, aT21, aT22;
246 GeomAPI_ProjectPointOnCurve aProjPC;
251 myRange1.Range(aT11, aT12);
252 myRange2.Range(aT21, aT22);
254 aProjPC.Init(myGeom2, aT21, aT22);
256 dT=(aT12-aT11)/aNbSeg;
259 for(i=0; i <= aNbSeg; ++i) {
261 myGeom1->D0(aT1, aP1);
263 aProjPC.Perform(aP1);
264 aNbP2=aProjPC.NbPoints();
269 aD=aProjPC.LowerDistance();
275 aCoeff=(Standard_Real)iCnt/((Standard_Real)aNbSeg+1);
276 return aCoeff > aTresh;
278 //=======================================================================
279 //function : FindSolutions
281 //=======================================================================
282 void IntTools_EdgeEdge::FindSolutions(IntTools_SequenceOfRanges& theRanges1,
283 IntTools_SequenceOfRanges& theRanges2,
284 Standard_Boolean& bSplit2)
286 Standard_Boolean bIsClosed2;
287 Standard_Real aT11, aT12, aT21, aT22;
290 bSplit2 = Standard_False;
291 myRange1.Range(aT11, aT12);
292 myRange2.Range(aT21, aT22);
294 bIsClosed2 = IsClosed(myGeom2, aT21, aT22, myTol2, myRes2);
298 BndBuildBox(myCurve1, aT11, aT12, myTol1, aB1);
300 gp_Pnt aP = myGeom2->Value(aT21);
301 bIsClosed2 = !aB1.IsOut(aP);
305 BndBuildBox(myCurve2, aT21, aT22, myTol2, aB2);
306 FindSolutions(myRange1, myRange2, aB2, theRanges1, theRanges2);
310 if (!CheckCoincidence(aT11, aT12, aT21, aT22, myTol, myRes1)) {
311 theRanges1.Append(myRange1);
312 theRanges2.Append(myRange2);
316 Standard_Integer i, j, aNb1, aNb2;
317 IntTools_SequenceOfRanges aSegments1, aSegments2;
319 aNb1 = IsClosed(myGeom1, aT11, aT12, myTol1, myRes1) ? 2 : 1;
322 aNb1 = SplitRangeOnSegments(aT11, aT12, myRes1, aNb1, aSegments1);
323 aNb2 = SplitRangeOnSegments(aT21, aT22, myRes2, aNb2, aSegments2);
325 for (i = 1; i <= aNb1; ++i) {
326 const IntTools_Range& aR1 = aSegments1(i);
327 for (j = 1; j <= aNb2; ++j) {
328 const IntTools_Range& aR2 = aSegments2(j);
329 BndBuildBox(myCurve2, aR2.First(), aR2.Last(), myTol2, aB2);
330 FindSolutions(aR1, aR2, aB2, theRanges1, theRanges2);
337 //=======================================================================
338 //function : FindSolutions
340 //=======================================================================
341 void IntTools_EdgeEdge::FindSolutions(const IntTools_Range& theR1,
342 const IntTools_Range& theR2,
343 const Bnd_Box& theBox2,
344 IntTools_SequenceOfRanges& theRanges1,
345 IntTools_SequenceOfRanges& theRanges2)
347 Standard_Boolean bOut, bStop, bThin;
348 Standard_Real aT11, aT12, aT21, aT22;
349 Standard_Real aTB11, aTB12, aTB21, aTB22;
350 Standard_Real aSmallStep1, aSmallStep2;
351 Standard_Integer iCom;
354 theR1.Range(aT11, aT12);
355 theR2.Range(aT21, aT22);
359 bThin = Standard_False;
360 bStop = Standard_False;
369 //1. Build box for first edge and find parameters
370 // of the second one in that box
371 BndBuildBox(myCurve1, aT11, aT12, myTol1, aB1);
372 bOut = aB1.IsOut(aB2);
377 bThin = ((aT12 - aT11) < myRes1) ||
378 (aB1.IsXThin(myTol) && aB1.IsYThin(myTol) && aB1.IsZThin(myTol));
380 bOut = !FindParameters(myCurve2, aTB21, aTB22, myTol2, myRes2, myPTol2,
381 myResCoeff2, aB1, aT21, aT22);
386 //2. Build box for second edge and find parameters
387 // of the first one in that box
388 BndBuildBox(myCurve2, aT21, aT22, myTol2, aB2);
389 bOut = aB1.IsOut(aB2);
394 bThin = ((aT22 - aT21) < myRes2) ||
395 (aB2.IsXThin(myTol) && aB2.IsYThin(myTol) && aB2.IsZThin(myTol));
397 bOut = !FindParameters(myCurve1, aTB11, aTB12, myTol1, myRes1, myPTol1,
398 myResCoeff1, aB2, aT11, aT12);
404 //3. Check if it makes sense to continue
405 aSmallStep1 = (aTB12 - aTB11) / 250.;
406 aSmallStep2 = (aTB22 - aTB21) / 250.;
408 if (aSmallStep1 < myRes1) {
409 aSmallStep1 = myRes1;
411 if (aSmallStep2 < myRes2) {
412 aSmallStep2 = myRes2;
415 if (((aT11 - aTB11) < aSmallStep1) && ((aTB12 - aT12) < aSmallStep1) &&
416 ((aT21 - aTB21) < aSmallStep2) && ((aTB22 - aT22) < aSmallStep2)) {
417 bStop = Standard_True;
428 //check curves for coincidence on the ranges
429 iCom = CheckCoincidence(aT11, aT12, aT21, aT22, myTol, myRes1);
431 bThin = Standard_True;
437 //check intermediate points
438 Standard_Boolean bSol;
441 GeomAPI_ProjectPointOnCurve aProjPC;
443 aT1 = (aT11 + aT12) * .5;
444 myGeom1->D0(aT1, aP1);
446 aProjPC.Init(myGeom2, aT21, aT22);
447 aProjPC.Perform(aP1);
449 if (aProjPC.NbPoints()) {
450 bSol = aProjPC.LowerDistance() <= myTol;
456 aT2 = (aT21 + aT22) * .5;
457 myGeom2->D0(aT2, aP2);
459 bSol = aP1.IsEqual(aP2, myTol);
467 IntTools_Range aR1(aT11, aT12), aR2(aT21, aT22);
469 theRanges1.Append(aR1);
470 theRanges2.Append(aR2);
474 if (!IsIntersection(aT11, aT12, aT21, aT22)) {
478 //split ranges on segments and repeat
479 Standard_Integer i, aNb1;
480 IntTools_SequenceOfRanges aSegments1;
482 IntTools_Range aR2(aT21, aT22);
483 BndBuildBox(myCurve2, aT21, aT22, myTol2, aB2);
485 aNb1 = SplitRangeOnSegments(aT11, aT12, myRes1, 3, aSegments1);
486 for (i = 1; i <= aNb1; ++i) {
487 const IntTools_Range& aR1 = aSegments1(i);
488 FindSolutions(aR1, aR2, aB2, theRanges1, theRanges2);
492 //=======================================================================
493 //function : FindParameters
495 //=======================================================================
496 Standard_Boolean IntTools_EdgeEdge::FindParameters(const BRepAdaptor_Curve& theBAC,
497 const Standard_Real aT1,
498 const Standard_Real aT2,
499 const Standard_Real theTol,
500 const Standard_Real theRes,
501 const Standard_Real thePTol,
502 const Standard_Real theResCoeff,
503 const Bnd_Box& theCBox,
507 Standard_Boolean bRet;
508 Standard_Integer aC, i, k;
509 Standard_Real aCf, aDiff, aDt, aT, aTB, aTOut, aTIn;
510 Standard_Real aDist, aDistP, aDistTol;
514 bRet = Standard_False;
515 aCf = 0.6180339887498948482045868343656;// =0.5*(1.+sqrt(5.))/2.;
520 aCBx.Enlarge(theTol);
522 const Handle(Geom_Curve)& aCurve = theBAC.Curve().Curve();
523 const GeomAbs_CurveType aCurveType = theBAC.GetType();
525 for (i = 0; i < 2; ++i) {
526 aTB = !i ? aT1 : aT2;
527 aT = !i ? aT2 : aTB1;
529 bRet = Standard_False;
531 //looking for the point on the edge which is in the box;
532 while (aC*(aT-aTB) >= 0) {
534 aDist = PointBoxDistance(theCBox, aP);
535 if (aDist > theTol) {
536 if (fabs(aDist - aDistP) < aDistTol) {
537 aDt = Resolution(aCurve, aCurveType, theResCoeff, (++k)*aDist);
540 aDt = Resolution(aCurve, aCurveType, theResCoeff, aDist);
544 bRet = Standard_True;
552 //edge is out of the box;
563 //one point IN, one point OUT; looking for the bounding point;
565 aTOut = aTB - aC*aDt;
566 aDiff = aTIn - aTOut;
567 while (fabs(aDiff) > thePTol) {
568 aTB = aTOut + aDiff*aCf;
570 if (aCBx.IsOut(aP)) {
575 aDiff = aTIn - aTOut;
587 //=======================================================================
588 //function : MergeSolutions
590 //=======================================================================
591 void IntTools_EdgeEdge::MergeSolutions(const IntTools_SequenceOfRanges& theRanges1,
592 const IntTools_SequenceOfRanges& theRanges2,
593 const Standard_Boolean bSplit2)
595 Standard_Integer aNbCP = theRanges1.Length();
600 IntTools_Range aRi1, aRi2, aRj1, aRj2;
601 Standard_Boolean bCond;
602 Standard_Integer i, j;
603 TopAbs_ShapeEnum aType;
604 Standard_Real aT11, aT12, aT21, aT22;
605 Standard_Real aTi11, aTi12, aTi21, aTi22;
606 Standard_Real aTj11, aTj12, aTj21, aTj22;
607 Standard_Real aRes1, aRes2, dTR1, dTR2;
608 BOPCol_MapOfInteger aMI;
610 aRes1 = Resolution(myCurve1.Curve().Curve(),
611 myCurve1.GetType(), myResCoeff1, myTol);
612 aRes2 = Resolution(myCurve2.Curve().Curve(),
613 myCurve2.GetType(), myResCoeff2, myTol);
615 myRange1.Range(aT11, aT12);
616 myRange2.Range(aT21, aT22);
619 aType = TopAbs_VERTEX;
621 for (i = 1; i <= aNbCP;) {
622 if (aMI.Contains(i)) {
627 aRi1 = theRanges1(i);
628 aRi2 = theRanges2(i);
630 aRi1.Range(aTi11, aTi12);
631 aRi2.Range(aTi21, aTi22);
635 for (j = i+1; j <= aNbCP; ++j) {
636 if (aMI.Contains(j)) {
640 aRj1 = theRanges1(j);
641 aRj2 = theRanges2(j);
643 aRj1.Range(aTj11, aTj12);
644 aRj2.Range(aTj21, aTj22);
646 bCond = (fabs(aTi12 - aTj11) < dTR1) ||
647 (bSplit2 && (fabs(aTj12 - aTi11) < dTR1));
648 if (bCond && bSplit2) {
649 bCond = (fabs((Max(aTi22, aTj22) - Min(aTi21, aTj21)) -
650 ((aTi22 - aTi21) + (aTj22 - aTj21))) < dTR2);
654 aTi11 = Min(aTi11, aTj11);
655 aTi12 = Max(aTi12, aTj12);
656 aTi21 = Min(aTi21, aTj21);
657 aTi22 = Max(aTi22, aTj22);
666 if (((fabs(aT11 - aTi11) < myRes1) && (fabs(aT12 - aTi12) < myRes1)) ||
667 ((fabs(aT21 - aTi21) < myRes2) && (fabs(aT22 - aTi22) < myRes2))) {
669 myCommonParts.Clear();
672 AddSolution(aTi11, aTi12, aTi21, aTi22, aType);
673 if (aType == TopAbs_EDGE) {
683 //=======================================================================
684 //function : AddSolution
686 //=======================================================================
687 void IntTools_EdgeEdge::AddSolution(const Standard_Real aT11,
688 const Standard_Real aT12,
689 const Standard_Real aT21,
690 const Standard_Real aT22,
691 const TopAbs_ShapeEnum theType)
693 IntTools_CommonPrt aCPart;
695 aCPart.SetType(theType);
697 aCPart.SetEdge1(myEdge1);
698 aCPart.SetEdge2(myEdge2);
699 aCPart.SetRange1(aT11, aT12);
700 aCPart.AppendRange2(aT21, aT22);
702 aCPart.SetEdge1(myEdge2);
703 aCPart.SetEdge2(myEdge1);
704 aCPart.SetRange1(aT21, aT22);
705 aCPart.AppendRange2(aT11, aT12);
708 if (theType == TopAbs_VERTEX) {
709 Standard_Real aT1, aT2;
711 FindBestSolution(aT11, aT12, aT21, aT22, aT1, aT2);
714 aCPart.SetVertexParameter1(aT1);
715 aCPart.SetVertexParameter2(aT2);
717 aCPart.SetVertexParameter1(aT2);
718 aCPart.SetVertexParameter2(aT1);
721 myCommonParts.Append(aCPart);
724 //=======================================================================
725 //function : FindBestSolution
727 //=======================================================================
728 void IntTools_EdgeEdge::FindBestSolution(const Standard_Real aT11,
729 const Standard_Real aT12,
730 const Standard_Real aT21,
731 const Standard_Real aT22,
735 Standard_Integer i, aNbS, iErr;
736 Standard_Real aDMin, aD, aRes1, aSolCriteria, aTouchCriteria;
737 Standard_Real aT1A, aT1B, aT1Min, aT2Min;
738 Standard_Real aT1Im, aT2Im, aT1Touch;
739 GeomAPI_ProjectPointOnCurve aProjPC;
740 IntTools_SequenceOfRanges aRanges;
741 Standard_Boolean bTouch;
743 aDMin = Precision::Infinite();
744 aSolCriteria = 5.e-16;
745 aTouchCriteria = 5.e-13;
746 bTouch = Standard_False;
749 aRes1 = Resolution(myCurve1.Curve().Curve(),
750 myCurve1.GetType(), myResCoeff1, myTol);
752 aNbS = SplitRangeOnSegments(aT11, aT12, 3*aRes1, aNbS, aRanges);
754 aProjPC.Init(myGeom2, aT21, aT22);
756 aT1 = (aT11 + aT12) * 0.5;
757 iErr = DistPC(aT1, myGeom1, aSolCriteria, aProjPC, aD, aT2, -1);
759 aT2 = (aT21 + aT22) * 0.5;
765 for (i = 1; i <= aNbS; ++i) {
766 const IntTools_Range& aR1 = aRanges(i);
767 aR1.Range(aT1A, aT1B);
770 iErr = FindDistPC(aT1A, aT1B, myGeom1, aSolCriteria, myPTol1,
771 aProjPC, aD, aT1Min, aT2Min, Standard_False);
779 if (aD < aTouchCriteria) {
781 aT1A = (aT1Touch + aT1Min) * 0.5;
782 iErr = DistPC(aT1A, myGeom1, aTouchCriteria,
783 aProjPC, aD, aT2Min, -1);
784 if (aD > aTouchCriteria) {
792 bTouch = Standard_True;
799 //=======================================================================
800 //function : ComputeLineLine
802 //=======================================================================
803 void IntTools_EdgeEdge::ComputeLineLine()
805 Standard_Boolean IsParallel, IsCoincide;
806 Standard_Real aSin, aCos, aAng, aTol;
807 Standard_Real aT1, aT2, aT11, aT12, aT21, aT22;
811 IntTools_CommonPrt aCommonPrt;
813 IsParallel = Standard_False;
814 IsCoincide = Standard_False;
816 aL1 = myCurve1.Line();
817 aL2 = myCurve2.Line();
818 aD1 = aL1.Position().Direction();
819 aD2 = aL2.Position().Direction();
820 myRange1.Range(aT11, aT12);
821 myRange2.Range(aT21, aT22);
823 aCommonPrt.SetEdge1(myEdge1);
824 aCommonPrt.SetEdge2(myEdge2);
827 aAng = (aCos >= 0.) ? 2.*(1. - aCos) : 2.*(1. + aCos);
829 if(aAng <= Precision::Angular()) {
830 IsParallel = Standard_True;
831 if(aL1.SquareDistance(aL2.Location()) <= aTol) {
832 IsCoincide = Standard_True;
833 aP11 = ElCLib::Value(aT11, aL1);
834 aP12 = ElCLib::Value(aT12, aL1);
838 aP11 = ElCLib::Value(aT11, aL1);
839 aP12 = ElCLib::Value(aT12, aL1);
840 if(aL2.SquareDistance(aP11) <= aTol && aL2.SquareDistance(aP12) <= aTol) {
841 IsCoincide = Standard_True;
846 Standard_Real t21, t22;
848 t21 = ElCLib::Parameter(aL2, aP11);
849 t22 = ElCLib::Parameter(aL2, aP12);
850 if((t21 > aT22 && t22 > aT22) || (t21 < aT21 && t22 < aT21)) {
863 aCommonPrt.SetRange1(aT11, aT12);
864 aCommonPrt.SetAllNullFlag(Standard_True);
865 aCommonPrt.AppendRange2(t21, t22);
868 aCommonPrt.SetRange1(aT11, aT12 - (t22 - aT22));
869 aCommonPrt.AppendRange2(t21, aT22);
873 aCommonPrt.SetRange1(aT11 + (aT21 - t21), aT12);
874 aCommonPrt.AppendRange2(aT21, t22);
876 aCommonPrt.SetType(TopAbs_EDGE);
877 myCommonParts.Append(aCommonPrt);
886 TopoDS_Iterator aIt1, aIt2;
887 aIt1.Initialize(myEdge1);
888 for (; aIt1.More(); aIt1.Next()) {
889 const TopoDS_Shape& aV1 = aIt1.Value();
890 aIt2.Initialize(myEdge2);
891 for (; aIt2.More(); aIt2.Next()) {
892 const TopoDS_Shape& aV2 = aIt2.Value();
893 if (aV2.IsSame(aV1)) {
900 aSin = 1. - aCos*aCos;
901 gp_Pnt O1 = aL1.Location();
902 gp_Pnt O2 = aL2.Location();
903 gp_Vec O1O2 (O1, O2);
905 aT2 = (aD1.XYZ()*(O1O2.Dot(aD1))-(O1O2.XYZ())).Dot(aD2.XYZ());
908 if(aT2 < aT21 || aT2 > aT22) {
912 gp_Pnt aP2(ElCLib::Value(aT2, aL2));
913 aT1 = (gp_Vec(O1, aP2)).Dot(aD1);
915 if(aT1 < aT11 || aT1 > aT12) {
919 gp_Pnt aP1(ElCLib::Value(aT1, aL1));
920 Standard_Real aDist = aP1.SquareDistance(aP2);
926 // compute correct range on the edges
927 Standard_Real anAngle, aDt1, aDt2;
929 anAngle = aD1.Angle(aD2);
931 aDt1 = IntTools_Tools::ComputeIntRange(myTol1, myTol2, anAngle);
932 aDt2 = IntTools_Tools::ComputeIntRange(myTol2, myTol1, anAngle);
934 aCommonPrt.SetRange1(aT1 - aDt1, aT1 + aDt1);
935 aCommonPrt.AppendRange2(aT2 - aDt2, aT2 + aDt2);
936 aCommonPrt.SetType(TopAbs_VERTEX);
937 aCommonPrt.SetVertexParameter1(aT1);
938 aCommonPrt.SetVertexParameter2(aT2);
939 myCommonParts.Append(aCommonPrt);
942 //=======================================================================
943 //function : IsIntersection
945 //=======================================================================
946 Standard_Boolean IntTools_EdgeEdge::IsIntersection(const Standard_Real aT11,
947 const Standard_Real aT12,
948 const Standard_Real aT21,
949 const Standard_Real aT22)
951 Standard_Boolean bRet;
952 gp_Pnt aP11, aP12, aP21, aP22;
953 gp_Vec aV11, aV12, aV21, aV22;
954 Standard_Real aD11_21, aD11_22, aD12_21, aD12_22, aCriteria, aCoef;
955 Standard_Boolean bSmall_11_21, bSmall_11_22, bSmall_12_21, bSmall_12_22;
957 bRet = Standard_True;
959 if (((aT12 - aT11) > aCoef*myRes1) && ((aT22 - aT21) > aCoef*myRes2)) {
962 Standard_Real aTRMin = Min((aT12 - aT11)/myRes1, (aT22 - aT21)/myRes2);
963 aCoef = aTRMin / 100.;
968 aCriteria = aCoef * myTol;
969 aCriteria *= aCriteria;
971 myGeom1->D1(aT11, aP11, aV11);
972 myGeom1->D1(aT12, aP12, aV12);
973 myGeom2->D1(aT21, aP21, aV21);
974 myGeom2->D1(aT22, aP22, aV22);
976 aD11_21 = aP11.SquareDistance(aP21);
977 aD11_22 = aP11.SquareDistance(aP22);
978 aD12_21 = aP12.SquareDistance(aP21);
979 aD12_22 = aP12.SquareDistance(aP22);
981 bSmall_11_21 = aD11_21 < aCriteria;
982 bSmall_11_22 = aD11_22 < aCriteria;
983 bSmall_12_21 = aD12_21 < aCriteria;
984 bSmall_12_22 = aD12_22 < aCriteria;
986 if ((bSmall_11_21 && bSmall_12_22) ||
987 (bSmall_11_22 && bSmall_12_21)) {
992 Standard_Real anAngleCriteria;
993 Standard_Real anAngle1, anAngle2;
995 anAngleCriteria = 5.e-3;
996 if (bSmall_11_21 && bSmall_12_22) {
997 anAngle1 = aV11.Angle(aV21);
998 anAngle2 = aV12.Angle(aV22);
1000 anAngle1 = aV11.Angle(aV22);
1001 anAngle2 = aV12.Angle(aV21);
1004 if (((anAngle1 < anAngleCriteria) || ((M_PI - anAngle1) < anAngleCriteria)) ||
1005 ((anAngle2 < anAngleCriteria) || ((M_PI - anAngle2) < anAngleCriteria))) {
1006 GeomAPI_ProjectPointOnCurve aProjPC;
1007 Standard_Integer iErr;
1008 Standard_Real aD, aT1Min, aT2Min;
1010 aD = Precision::Infinite();
1011 aProjPC.Init(myGeom2, aT21, aT22);
1012 iErr = FindDistPC(aT11, aT12, myGeom1, myTol, myRes1,
1013 aProjPC, aD, aT1Min, aT2Min, Standard_False);
1020 //=======================================================================
1021 //function : CheckCoincidence
1023 //=======================================================================
1024 Standard_Integer IntTools_EdgeEdge::CheckCoincidence(const Standard_Real aT11,
1025 const Standard_Real aT12,
1026 const Standard_Real aT21,
1027 const Standard_Real aT22,
1028 const Standard_Real theCriteria,
1029 const Standard_Real theCurveRes1)
1031 Standard_Integer iErr, aNb, aNb1, i;
1032 Standard_Real aT1A, aT1B, aT1max, aT2max, aDmax;
1033 GeomAPI_ProjectPointOnCurve aProjPC;
1034 IntTools_SequenceOfRanges aRanges;
1038 aProjPC.Init(myGeom2, aT21, aT22);
1040 // 1. Express evaluation
1041 aNb = 10; // Number of intervals on the curve #1
1042 aNb1 = SplitRangeOnSegments(aT11, aT12, theCurveRes1, aNb, aRanges);
1043 for (i = 1; i < aNb1; ++i) {
1044 const IntTools_Range& aR1 = aRanges(i);
1045 aR1.Range(aT1A, aT1B);
1047 iErr = DistPC(aT1B, myGeom1, theCriteria, aProjPC, aDmax, aT2max);
1053 // if the ranges in aRanges are less than theCurveRes1,
1054 // there is no need to do step 2 (deep evaluation)
1059 // 2. Deep evaluation
1060 for (i = 2; i < aNb1; ++i) {
1061 const IntTools_Range& aR1 = aRanges(i);
1062 aR1.Range(aT1A, aT1B);
1064 iErr = FindDistPC(aT1A, aT1B, myGeom1, theCriteria, theCurveRes1,
1065 aProjPC, aDmax, aT1max, aT2max);
1071 // iErr == 0 - the patches are coincided
1072 // iErr == 1 - a point from aC1 can not be projected on aC2
1073 // iErr == 2 - the distance is too big
1077 //=======================================================================
1078 //function : FindDistPC
1080 //=======================================================================
1081 Standard_Integer FindDistPC(const Standard_Real aT1A,
1082 const Standard_Real aT1B,
1083 const Handle(Geom_Curve)& theC1,
1084 const Standard_Real theCriteria,
1085 const Standard_Real theEps,
1086 GeomAPI_ProjectPointOnCurve& theProjPC,
1087 Standard_Real& aDmax,
1088 Standard_Real& aT1max,
1089 Standard_Real& aT2max,
1090 const Standard_Boolean bMaxDist)
1092 Standard_Integer iErr, iC;
1093 Standard_Real aGS, aXP, aA, aB, aXL, aYP, aYL, aT2P, aT2L;
1095 iC = bMaxDist ? 1 : -1;
1097 aT1max = aT2max = 0.; // silence GCC warning
1099 aGS = 0.6180339887498948482045868343656;// =0.5*(1.+sqrt(5.))-1.;
1104 iErr = DistPC(aA, theC1, theCriteria, theProjPC,
1105 aYP, aT2P, aDmax, aT1max, aT2max, iC);
1110 iErr = DistPC(aB, theC1, theCriteria, theProjPC,
1111 aYL, aT2L, aDmax, aT1max, aT2max, iC);
1116 aXP = aA + (aB - aA)*aGS;
1117 aXL = aB - (aB - aA)*aGS;
1119 iErr = DistPC(aXP, theC1, theCriteria, theProjPC,
1120 aYP, aT2P, aDmax, aT1max, aT2max, iC);
1125 iErr = DistPC(aXL, theC1, theCriteria, theProjPC,
1126 aYL, aT2L, aDmax, aT1max, aT2max, iC);
1132 if (iC*(aYP - aYL) > 0) {
1136 aXP = aA + (aB - aA)*aGS;
1137 iErr = DistPC(aXP, theC1, theCriteria, theProjPC,
1138 aYP, aT2P, aDmax, aT1max, aT2max, iC);
1144 aXL = aB - (aB - aA)*aGS;
1145 iErr = DistPC(aXL, theC1, theCriteria, theProjPC,
1146 aYL, aT2L, aDmax, aT1max, aT2max, iC);
1150 if ((iErr == 2) && !bMaxDist) {
1151 aXP = (aA + aB) * 0.5;
1152 DistPC(aXP, theC1, theCriteria, theProjPC,
1153 aYP, aT2P, aDmax, aT1max, aT2max, iC);
1158 if ((aB - aA) < theEps) {
1165 //=======================================================================
1168 //=======================================================================
1169 Standard_Integer DistPC(const Standard_Real aT1,
1170 const Handle(Geom_Curve)& theC1,
1171 const Standard_Real theCriteria,
1172 GeomAPI_ProjectPointOnCurve& theProjPC,
1175 Standard_Real& aDmax,
1176 Standard_Real& aT1max,
1177 Standard_Real& aT2max,
1178 const Standard_Integer iC)
1180 Standard_Integer iErr;
1182 iErr = DistPC(aT1, theC1, theCriteria, theProjPC, aD, aT2, iC);
1187 if (iC*(aD - aDmax) > 0) {
1195 //=======================================================================
1198 //=======================================================================
1199 Standard_Integer DistPC(const Standard_Real aT1,
1200 const Handle(Geom_Curve)& theC1,
1201 const Standard_Real theCriteria,
1202 GeomAPI_ProjectPointOnCurve& theProjPC,
1205 const Standard_Integer iC)
1207 Standard_Integer iErr, aNbP2;
1211 theC1->D0(aT1, aP1);
1213 theProjPC.Perform(aP1);
1214 aNbP2 = theProjPC.NbPoints();
1216 iErr = 1;// the point from aC1 can not be projected on aC2
1220 aD = theProjPC.LowerDistance();
1221 aT2 = theProjPC.LowerDistanceParameter();
1222 if (iC*(aD - theCriteria) > 0) {
1223 iErr = 2;// the distance is too big or small
1229 //=======================================================================
1230 //function : SplitRangeOnSegments
1232 //=======================================================================
1233 Standard_Integer SplitRangeOnSegments(const Standard_Real aT1,
1234 const Standard_Real aT2,
1235 const Standard_Real theResolution,
1236 const Standard_Integer theNbSeg,
1237 IntTools_SequenceOfRanges& theSegments)
1239 Standard_Real aDiff = aT2 - aT1;
1240 if (aDiff < theResolution || theNbSeg == 1) {
1241 theSegments.Append(IntTools_Range(aT1, aT2));
1245 Standard_Real aDt, aT1x, aT2x, aSeg;
1246 Standard_Integer aNbSegments, i;
1248 aNbSegments = theNbSeg;
1249 aDt = aDiff / aNbSegments;
1250 if (aDt < theResolution) {
1251 aSeg = aDiff / theResolution;
1252 aNbSegments = Standard_Integer(aSeg) + 1;
1253 aDt = aDiff / aNbSegments;
1257 for (i = 1; i < aNbSegments; ++i) {
1260 IntTools_Range aR(aT1x, aT2x);
1261 theSegments.Append(aR);
1266 IntTools_Range aR(aT1x, aT2);
1267 theSegments.Append(aR);
1272 //=======================================================================
1273 //function : BndBuildBox
1275 //=======================================================================
1276 void BndBuildBox(const BRepAdaptor_Curve& theBAC,
1277 const Standard_Real aT1,
1278 const Standard_Real aT2,
1279 const Standard_Real theTol,
1283 BndLib_Add3dCurve::Add(theBAC, aT1, aT2, theTol, aB);
1287 //=======================================================================
1288 //function : PointBoxDistance
1290 //=======================================================================
1291 Standard_Real PointBoxDistance(const Bnd_Box& aB,
1294 Standard_Real aPCoord[3];
1295 Standard_Real aBMinCoord[3], aBMaxCoord[3];
1296 Standard_Real aDist, aR1, aR2;
1299 aP.Coord(aPCoord[0], aPCoord[1], aPCoord[2]);
1300 aB.Get(aBMinCoord[0], aBMinCoord[1], aBMinCoord[2],
1301 aBMaxCoord[0], aBMaxCoord[1], aBMaxCoord[2]);
1304 for (i = 0; i < 3; ++i) {
1305 aR1 = aBMinCoord[i] - aPCoord[i];
1311 aR2 = aPCoord[i] - aBMaxCoord[i];
1317 aDist = Sqrt(aDist);
1321 //=======================================================================
1322 //function : TypeToInteger
1324 //=======================================================================
1325 Standard_Integer TypeToInteger(const GeomAbs_CurveType theCType)
1327 Standard_Integer iRet;
1333 case GeomAbs_Hyperbola:
1334 case GeomAbs_Parabola:
1337 case GeomAbs_Circle:
1338 case GeomAbs_Ellipse:
1341 case GeomAbs_BezierCurve:
1342 case GeomAbs_BSplineCurve:
1352 //=======================================================================
1353 //function : ResolutionCoeff
1355 //=======================================================================
1356 Standard_Real ResolutionCoeff(const BRepAdaptor_Curve& theBAC,
1357 const IntTools_Range& theRange)
1359 Standard_Real aResCoeff = 0.;
1361 const Handle(Geom_Curve)& aCurve = theBAC.Curve().Curve();
1362 const GeomAbs_CurveType aCurveType = theBAC.GetType();
1364 switch (aCurveType) {
1365 case GeomAbs_Circle :
1366 aResCoeff = 1. / (2 * Handle(Geom_Circle)::DownCast (aCurve)->Circ().Radius());
1368 case GeomAbs_Ellipse :
1369 aResCoeff = 1. / Handle(Geom_Ellipse)::DownCast (aCurve)->MajorRadius();
1371 case GeomAbs_OffsetCurve : {
1372 const Handle(Geom_OffsetCurve)& anOffsetCurve = Handle(Geom_OffsetCurve)::DownCast(aCurve);
1373 const Handle(Geom_Curve)& aBasisCurve = anOffsetCurve->BasisCurve();
1374 const GeomAbs_CurveType aBCType = GeomAdaptor_Curve(aBasisCurve).GetType();
1375 if (aBCType == GeomAbs_Line) {
1378 else if (aBCType == GeomAbs_Circle) {
1379 aResCoeff = 1. / (2 * (anOffsetCurve->Offset() +
1380 Handle(Geom_Circle)::DownCast (aBasisCurve)->Circ().Radius()));
1383 else if (aBCType == GeomAbs_Ellipse) {
1384 aResCoeff = 1. / (anOffsetCurve->Offset() +
1385 Handle(Geom_Ellipse)::DownCast (aBasisCurve)->MajorRadius());
1389 case GeomAbs_Hyperbola :
1390 case GeomAbs_Parabola :
1391 case GeomAbs_OtherCurve :{
1392 Standard_Real k, kMin, aDist, aDt, aT1, aT2, aT;
1393 Standard_Integer aNbP, i;
1397 theRange.Range(aT1, aT2);
1398 aDt = (aT2 - aT1) / aNbP;
1402 theBAC.D0(aT1, aP1);
1403 for (i = 1; i <= aNbP; ++i) {
1406 aDist = aP1.Distance(aP2);
1424 //=======================================================================
1425 //function : Resolution
1427 //=======================================================================
1428 Standard_Real Resolution(const Handle(Geom_Curve)& theCurve,
1429 const GeomAbs_CurveType theCurveType,
1430 const Standard_Real theResCoeff,
1431 const Standard_Real theR3D)
1435 switch (theCurveType) {
1439 case GeomAbs_Circle: {
1440 Standard_Real aDt = theResCoeff * theR3D;
1441 aRes = (aDt <= 1.) ? 2*ASin(aDt) : 2*M_PI;
1444 case GeomAbs_BezierCurve:
1445 Handle(Geom_BezierCurve)::DownCast (theCurve)->Resolution(theR3D, aRes);
1447 case GeomAbs_BSplineCurve:
1448 Handle(Geom_BSplineCurve)::DownCast (theCurve)->Resolution(theR3D, aRes);
1450 case GeomAbs_OffsetCurve: {
1451 const Handle(Geom_Curve)& aBasisCurve =
1452 Handle(Geom_OffsetCurve)::DownCast(theCurve)->BasisCurve();
1453 const GeomAbs_CurveType aBCType = GeomAdaptor_Curve(aBasisCurve).GetType();
1454 if (aBCType == GeomAbs_Line) {
1458 else if (aBCType == GeomAbs_Circle) {
1459 Standard_Real aDt = theResCoeff * theR3D;
1460 aRes = (aDt <= 1.) ? 2*ASin(aDt) : 2*M_PI;
1465 aRes = theResCoeff * theR3D;
1472 //=======================================================================
1473 //function : CurveDeflection
1475 //=======================================================================
1476 Standard_Real CurveDeflection(const BRepAdaptor_Curve& theBAC,
1477 const IntTools_Range& theRange)
1479 Standard_Real aDt, aT, aT1, aT2, aDefl;
1480 Standard_Integer i, aNbP;
1486 theRange.Range(aT1, aT2);
1487 aDt = (aT2 - aT1) / aNbP;
1490 theBAC.D1(aT1, aP, aV1);
1491 for (i = 1; i <= aNbP; ++i) {
1493 theBAC.D1(aT, aP, aV2);
1494 if (aV1.Magnitude() > gp::Resolution() &&
1495 aV2.Magnitude() > gp::Resolution()) {
1496 gp_Dir aD1(aV1), aD2(aV2);
1497 aDefl += aD1.Angle(aD2);
1505 //=======================================================================
1506 //function : IsClosed
1508 //=======================================================================
1509 Standard_Integer IsClosed(const Handle(Geom_Curve)& theCurve,
1510 const Standard_Real aT1,
1511 const Standard_Real aT2,
1512 const Standard_Real theTol,
1513 const Standard_Real theRes)
1515 Standard_Boolean bClosed;
1519 bClosed = Standard_False;
1520 if (Abs(aT1 - aT2) < theRes) {
1524 theCurve->D0(aT1, aP1);
1525 theCurve->D0(aT2, aP2);
1527 aD = aP1.Distance(aP2);
1528 bClosed = aD < theTol;