1 // Created on: 2000-10-26
2 // Created by: Peter KURNEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and / or modify it
8 // under the terms of the GNU Lesser General Public version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <IntTools_EdgeEdge.ixx>
18 #include <Precision.hxx>
20 #include <TColStd_SequenceOfReal.hxx>
22 #include <gp_Circ.hxx>
29 #include <Geom_Curve.hxx>
31 #include <Geom_BSplineCurve.hxx>
32 #include <GeomAdaptor_Curve.hxx>
34 #include <CPnts_AbscissaPoint.hxx>
36 #include <GeomAPI_ProjectPointOnCurve.hxx>
38 #include <Extrema_ExtElC.hxx>
39 #include <Extrema_POnCurv.hxx>
40 #include <Extrema_ExtCC.hxx>
42 #include <TopoDS_Iterator.hxx>
43 #include <BRep_Tool.hxx>
45 #include <IntTools.hxx>
46 #include <IntTools_Range.hxx>
47 #include <IntTools_CArray1OfReal.hxx>
48 #include <IntTools_CommonPrt.hxx>
49 #include <IntTools_SequenceOfRanges.hxx>
50 #include <IntTools_Tools.hxx>
51 #include <IntTools_BeanBeanIntersector.hxx>
54 //=======================================================================
55 //function : IntTools_EdgeEdge::IntTools_EdgeEdge
57 //=======================================================================
58 IntTools_EdgeEdge::IntTools_EdgeEdge()
66 myIsDone=Standard_False;
68 myOrder=Standard_False;
71 myParallel=Standard_False;
74 //=======================================================================
77 //=======================================================================
78 void IntTools_EdgeEdge::SetEdge1(const TopoDS_Edge& anEdge)
82 //=======================================================================
85 //=======================================================================
86 void IntTools_EdgeEdge::SetEdge2(const TopoDS_Edge& anEdge)
91 //=======================================================================
92 //function : SetTolerance1
94 //=======================================================================
95 void IntTools_EdgeEdge::SetTolerance1(const Standard_Real aTol)
99 //=======================================================================
100 //function : SetTolerance2
102 //=======================================================================
103 void IntTools_EdgeEdge::SetTolerance2(const Standard_Real aTol)
108 //=======================================================================
109 //function : SetDiscretize
111 //=======================================================================
112 void IntTools_EdgeEdge::SetDiscretize(const Standard_Integer aDiscret)
116 //=======================================================================
117 //function : SetDeflection
119 //=======================================================================
120 void IntTools_EdgeEdge::SetDeflection(const Standard_Real aDefl)
124 //=======================================================================
125 //function : SetEpsilonT
127 //=======================================================================
128 void IntTools_EdgeEdge::SetEpsilonT(const Standard_Real anEpsT)
132 //=======================================================================
133 //function : SetEpsilonNull
135 //=======================================================================
136 void IntTools_EdgeEdge::SetEpsilonNull(const Standard_Real anEpsNull)
141 //=======================================================================
142 //function : SetRange1
144 //=======================================================================
145 void IntTools_EdgeEdge::SetRange1(const Standard_Real aFirst,
146 const Standard_Real aLast)
148 myRange1.SetFirst (aFirst);
149 myRange1.SetLast (aLast);
151 //=======================================================================
152 //function : SetRange2
154 //=======================================================================
155 void IntTools_EdgeEdge::SetRange2(const Standard_Real aFirst,
156 const Standard_Real aLast)
158 myRange2.SetFirst (aFirst);
159 myRange2.SetLast (aLast);
162 //=======================================================================
163 //function : SetRange1
165 //=======================================================================
166 void IntTools_EdgeEdge::SetRange1(const IntTools_Range& aRange)
168 myRange1.SetFirst (aRange.First());
169 myRange1.SetLast (aRange.Last());
171 //=======================================================================
172 //function : SetRange2
174 //=======================================================================
175 void IntTools_EdgeEdge::SetRange2(const IntTools_Range& aRange)
177 myRange2.SetFirst (aRange.First());
178 myRange2.SetLast (aRange.Last());
181 //=======================================================================
184 //=======================================================================
185 Standard_Boolean IntTools_EdgeEdge::Order()const
189 //=======================================================================
192 //=======================================================================
193 Standard_Boolean IntTools_EdgeEdge::IsDone()const
197 //=======================================================================
198 //function : ErrorStatus
200 //=======================================================================
201 Standard_Integer IntTools_EdgeEdge::ErrorStatus()const
203 return myErrorStatus;
206 //=======================================================================
207 //function : CommonParts
209 //=======================================================================
210 const IntTools_SequenceOfCommonPrts& IntTools_EdgeEdge::CommonParts() const
212 return mySeqOfCommonPrts;
214 //=======================================================================
217 //=======================================================================
218 const IntTools_Range& IntTools_EdgeEdge::Range1() const
222 //=======================================================================
225 //=======================================================================
226 const IntTools_Range& IntTools_EdgeEdge::Range2() const
230 //=======================================================================
233 //=======================================================================
234 void IntTools_EdgeEdge::Perform()
236 Standard_Boolean bIsSameCurves;
237 Standard_Integer i, pri, aNbCommonPrts, aNbRange;
238 Standard_Real aT1, aT2, aPC;
239 IntTools_CommonPrt aCommonPrt;
240 GeomAbs_CurveType aCTFrom, aCTTo;
242 myIsDone=Standard_False;
255 aCTFrom = myCFrom.GetType();
256 aCTTo = myCTo.GetType();
258 if(aCTFrom==GeomAbs_Line && aCTTo==GeomAbs_Line) {
269 bIsSameCurves=IsSameCurves();
271 aCommonPrt.SetType(TopAbs_EDGE);
272 aCommonPrt.SetRange1 (myTminFrom, myTmaxFrom);
273 aCommonPrt.AppendRange2 (myTminTo, myTmaxTo);
274 mySeqOfCommonPrts.Append(aCommonPrt);
275 myIsDone=Standard_True;
279 IntTools_BeanBeanIntersector anIntersector(myCFrom, myCTo, myTolFrom, myTolTo);
280 anIntersector.SetBeanParameters(Standard_True, myTminFrom, myTmaxFrom);
281 anIntersector.SetBeanParameters(Standard_False, myTminTo, myTmaxTo);
283 anIntersector.Perform();
284 if(!anIntersector.IsDone()) {
285 myIsDone = Standard_False;
289 aPC=Precision::PConfusion();
290 aCommonPrt.SetEdge1(myCFrom.Edge());
291 aCommonPrt.SetEdge2(myCTo.Edge());
293 const IntTools_SequenceOfRanges& aSR=anIntersector.Result();
294 aNbRange=aSR.Length();
295 for(i=1; i <=aNbRange; ++i) {
296 const IntTools_Range& aRange =aSR.Value(i);
300 if(IsProjectable(IntTools_Tools::IntermediatePoint(aT1, aT2))) {
301 aCommonPrt.SetRange1(aT1, aT2);
303 if(((aT1 - myTminFrom)<aPC) && ((myTmaxFrom - aT2)<aPC)) {
304 aCommonPrt.SetAllNullFlag(Standard_True);
306 mySeqOfCommonPrts.Append(aCommonPrt);
310 aNbCommonPrts=mySeqOfCommonPrts.Length();
311 for (i=1; i<=aNbCommonPrts; ++i) {
312 IntTools_CommonPrt& aCmnPrt=mySeqOfCommonPrts.ChangeValue(i);
313 pri=FindRangeOnCurve2 (aCmnPrt);
320 // Line Circle's Common Parts treatement
321 if ((aCTFrom==GeomAbs_Line && aCTTo==GeomAbs_Circle) ||
322 (aCTFrom==GeomAbs_Circle && aCTTo==GeomAbs_Line) ||
323 (aCTFrom==GeomAbs_Ellipse && aCTTo==GeomAbs_Ellipse) ||
324 (aCTFrom==GeomAbs_Circle && aCTTo==GeomAbs_Circle)) {
325 for (i=1; i<=aNbCommonPrts; i++) {
326 IntTools_CommonPrt& aCP=mySeqOfCommonPrts(i);
327 TopAbs_ShapeEnum aType=aCP.Type();
328 Standard_Boolean bIsTouch;
329 Standard_Real aTx1, aTx2;
331 if ((aType==TopAbs_EDGE) && !aCommonPrt.AllNullFlag()) {
332 bIsTouch=CheckTouch (aCP, aTx1, aTx2);
334 aCP.SetType(TopAbs_VERTEX);
335 aCP.SetVertexParameter1(aTx1);
336 aCP.SetRange1 (aTx1, aTx1);
337 IntTools_Range& aRange2=(aCP.ChangeRanges2()).ChangeValue(1);
338 aRange2.SetFirst(aTx2);
339 aRange2.SetLast (aTx2);
343 if (aType==TopAbs_VERTEX) {
344 if(aCTFrom==GeomAbs_Line || aCTTo==GeomAbs_Line) {
345 bIsTouch=CheckTouchVertex (aCP, aTx1, aTx2);
347 aCP.SetVertexParameter1(aTx1);
348 aCP.SetRange1 (aTx1, aTx1);
349 IntTools_Range& aRange2=(aCP.ChangeRanges2()).ChangeValue(1);
350 aRange2.SetFirst(aTx2);
351 aRange2.SetLast (aTx2);
365 myIsDone=Standard_True;
368 //=======================================================================
369 //function : CheckData
371 //=======================================================================
372 void IntTools_EdgeEdge::CheckData()
374 if (BRep_Tool::Degenerated(myEdge1)) {
377 if (!BRep_Tool::IsGeometric(myEdge1)) {
380 if (BRep_Tool::Degenerated(myEdge2)) {
383 if (!BRep_Tool::IsGeometric(myEdge2)) {
387 //=======================================================================
390 //=======================================================================
391 void IntTools_EdgeEdge::Prepare()
393 Standard_Real aLE1, aLE2, aT1, aT2, aTol1, aTol2;
394 GeomAdaptor_Curve aGAC;
395 GeomAbs_CurveType aCT1, aCT2;
397 // 1.Prepare Curves' data
398 const Handle(Geom_Curve)& aC1=BRep_Tool::Curve (myEdge1, aT1, aT2);
399 aT1=myRange1.First();
401 aGAC.Load(aC1, myRange1.First(), myRange1.Last());
402 aLE1=CPnts_AbscissaPoint::Length(aGAC, aT1, aT2);
404 const Handle(Geom_Curve)& aC2=BRep_Tool::Curve (myEdge2, aT1, aT2);
405 aT1=myRange2.First();
407 aGAC.Load(aC2, aT1, aT2);
408 aLE2=CPnts_AbscissaPoint::Length(aGAC, aT1, aT2);
410 myOrder=Standard_False;
412 myCFrom.Initialize(myEdge1);
413 myCTo .Initialize(myEdge2);
416 myTminFrom=myRange1.First();
417 myTmaxFrom=myRange1.Last ();
418 myTminTo =myRange2.First();
419 myTmaxTo =myRange2.Last ();
422 myCFrom.Initialize(myEdge2);
423 myCTo .Initialize(myEdge1);
426 myTminFrom=myRange2.First();
427 myTmaxFrom=myRange2.Last ();
428 myTminTo =myRange1.First();
429 myTmaxTo =myRange1.Last ();
431 myOrder=Standard_True; // revesed order
434 // 2.Prepare myCriteria
435 aCT1=myCFrom.GetType();
436 aCT2=myCTo.GetType();
439 if(aCT1==GeomAbs_BSplineCurve|| aCT1==GeomAbs_BezierCurve){
443 if(aCT2==GeomAbs_BSplineCurve|| aCT2==GeomAbs_BezierCurve){
446 myCriteria=aTol1+aTol2;
448 //=======================================================================
449 //function : IsProjectable
451 //=======================================================================
452 Standard_Integer IntTools_EdgeEdge::IsProjectable(const Standard_Real t) const
454 Standard_Integer aNbProj;
458 GeomAPI_ProjectPointOnCurve aProjector;
459 const TopoDS_Edge& aEFrom=myCFrom.Edge();
460 Handle(Geom_Curve)aCurveFrom=BRep_Tool::Curve (aEFrom, f, l);
461 aCurveFrom->D0(t, aPFrom);
463 Handle(Geom_Curve)aCurveTo=BRep_Tool::Curve (myCTo.Edge(), f, l);
464 aProjector.Init(aCurveTo, myTminTo, myTmaxTo);
465 aProjector.Perform(aPFrom);
466 aNbProj=aProjector.NbPoints();
468 if (myCTo.GetType()==GeomAbs_Circle) {
469 gp_Circ aCirc=myCTo.Circle();
470 const gp_Pnt& aCenter=aCirc.Location();
471 if (aCenter.SquareDistance(aPFrom) < 1.e-7) {
478 //=======================================================================
479 //function : DistanceFunction
481 //=======================================================================
482 Standard_Real IntTools_EdgeEdge::DistanceFunction(const Standard_Real t)//const
484 Standard_Real aD, f, l;
485 GeomAPI_ProjectPointOnCurve aProjector;
486 gp_Pnt aPFrom; //ZZ , aPTo;
488 const TopoDS_Edge& aEFrom=myCFrom.Edge();
489 const TopoDS_Edge& aETo =myCTo.Edge();
491 Handle(Geom_Curve)aCurveFrom=BRep_Tool::Curve (aEFrom, f, l);
492 aCurveFrom->D0 (t, aPFrom);
493 Handle(Geom_Curve)aCurveTo=BRep_Tool::Curve (aETo, f, l);
495 if (myCTo.GetType()==GeomAbs_Circle) {
496 gp_Circ aCirc=myCTo.Circle();
497 const gp_Pnt& aCenter=aCirc.Location();
498 const gp_Ax1& anAx1 =aCirc.Axis();
499 const gp_Dir& aDir =anAx1.Direction();
500 gp_Lin aLin(aCenter, aDir);
501 Standard_Real dPFromLin=aLin.Distance(aPFrom);
502 if (dPFromLin < 1.e-7) {
504 aCurveTo->D0 (myTminTo, anAnyPTo);
505 aD=aPFrom.Distance(anAnyPTo);
512 aProjector.Init(aCurveTo, myTminTo, myTmaxTo);
513 aProjector.Perform(aPFrom);
515 Standard_Integer j, aNbPoints;
517 aNbPoints =aProjector.NbPoints();
519 for (j=0; j<=1; j++) {
526 aCurveFrom->D0 (tt, aPFrom);
527 aProjector.Init(aCurveTo, myTminTo, myTmaxTo);
528 aProjector.Perform(aPFrom);
529 aNbPoints=aProjector.NbPoints();
538 // Can't find projection.
544 aD=aProjector.LowerDistance();
550 //=======================================================================
551 //function : DerivativeFunction
553 //=======================================================================
554 Standard_Real IntTools_EdgeEdge::DerivativeFunction(const Standard_Real t2)
556 Standard_Real t1, t3, aD1, aD2, aD3;
557 Standard_Real dt=1.e-7;
559 aD1=DistanceFunction(t1);
561 aD3=DistanceFunction(t3);
568 //=======================================================================
569 //function : FindSimpleRoot
570 //purpose : [private]
571 //=======================================================================
572 Standard_Real IntTools_EdgeEdge::FindSimpleRoot (const Standard_Integer IP,
573 const Standard_Real tA,
574 const Standard_Real tB,
575 const Standard_Real fA)
577 Standard_Real r, a, b, y, x0, s;
581 Standard_Integer step = 1, stepcheck = 1000, steplimit = 100000;
582 Standard_Real value = (IP==1) ? DistanceFunction(0.5*(a+b)) : DerivativeFunction(0.5*(a+b));
588 y=DistanceFunction(x0);
590 y=DerivativeFunction(x0);
592 Standard_Real aMaxAB100 = 100.*Max(a, b);
593 Standard_Real anEps = Epsilon(aMaxAB100);
594 Standard_Real anEpsT = Max(anEps, myEpsT);
595 // if (fabs(b-a) < myEpsT || y==0.) {
596 if (fabs(b-a) < anEpsT || y==0.) {
600 if( step == stepcheck ) {
601 if( Abs(value - y) <= 1.e-9 ) {
610 if( step == steplimit ) {
630 //=======================================================================
631 //function : FindRangeOnCurve2
633 //=======================================================================
634 Standard_Integer IntTools_EdgeEdge::FindRangeOnCurve2(IntTools_CommonPrt& aCommonPrt)
636 Standard_Integer pri;
639 if (aCommonPrt.AllNullFlag()) {
640 aCommonPrt.SetType(TopAbs_EDGE);
641 aCommonPrt.AppendRange2 (myTminTo, myTmaxTo);
645 Standard_Real ttmp, f, l, af1, al1, am1, af2, al2, am2;
646 gp_Pnt aPf1, aPl1, aPm1, aPf2, aPl2, aPm2;
647 GeomAPI_ProjectPointOnCurve aProjector;
649 aCommonPrt.Range1(af1, al1);
652 const TopoDS_Edge& anEdgeTo=myCTo.Edge();
653 Handle(Geom_Curve)aCurveTo=BRep_Tool::Curve (anEdgeTo, f, l);
655 const TopoDS_Edge& anEdgeFrom=myCFrom.Edge();
656 Handle(Geom_Curve)aCurveFrom=BRep_Tool::Curve (anEdgeFrom, f, l);
659 aCurveFrom->D0 (af1, aPf1);
660 pri=GetParameterOnCurve2 (af1, af2);
664 aCurveTo->D0(af2, aPf2);
667 aCurveFrom->D0 (al1, aPl1);
668 pri=GetParameterOnCurve2 (al1, al2);
672 aCurveTo->D0(al2, aPl2);
675 aCurveFrom->D0 (am1, aPm1);
676 pri=GetParameterOnCurve2 (am1, am2);
680 aCurveTo->D0(am2, aPm2);
682 // Reverse C2 points if it is necessary
683 Standard_Boolean reverse = (af2 > al2);
695 if((Abs(af2 - myTminTo) < Precision::PConfusion()) &&
696 (Abs(al2 - myTmaxTo) < Precision::PConfusion())) {
697 aCommonPrt.SetAllNullFlag(Standard_True);
701 Standard_Boolean aVFlag1, aVFlag2, aGeomFlag1, aGeomFlag2;
702 Standard_Real Df2m2, Dm2l2, Df2l2, df2m2, dm2l2, df2l2, df1l1;
703 Standard_Real tV1, tV2;
705 // parametric differences for C2
710 // geometric distances for C2
711 df2m2=aPf2.Distance(aPm2);
712 dm2l2=aPm2.Distance(aPl2);
713 df2l2=aPf2.Distance(aPl2);
715 aVFlag1=(Df2m2<myEpsT && Dm2l2<myEpsT);
716 aVFlag2=(df2m2 < myCriteria && dm2l2 < myCriteria);
719 // Two perpendicular lines => VERTEX
720 if ( aVFlag1 && aVFlag2) {
722 aCommonPrt.SetType(TopAbs_VERTEX);
723 pri=TreatVertexType(am1, am2, aCommonPrt);
727 aCommonPrt.SetVertexParameter2(tV2);
728 aCommonPrt.AppendRange2 (af2, al2);
731 aCommonPrt.SetVertexParameter1(tV1);
732 aCommonPrt.SetRange1 (af1, al1);
737 // geometric distances for C1
738 df1l1=aPf1.Distance(aPl1);
740 // if geometric distances between boundaries is less than myCriteria
742 aGeomFlag1=(df1l1 < myCriteria);
743 aGeomFlag2=(df2l2 < myCriteria);
744 if (aGeomFlag1 && aGeomFlag2) {
745 aCommonPrt.SetType(TopAbs_VERTEX);
748 aCommonPrt.SetVertexParameter2(tV2);
749 aCommonPrt.AppendRange2 (af2, al2);
752 aCommonPrt.SetVertexParameter1(tV1);
753 aCommonPrt.SetRange1 (af1, al1);
758 if (Df2l2 < myEpsT && !aVFlag1) {
759 if (aPf1.Distance(aPl1) < myCriteria && aPf2.Distance(aPl2) < myCriteria) {
762 aCommonPrt.AppendRange2 (af1, al2);
763 aCommonPrt.SetType(TopAbs_EDGE);
768 aProjector.Init(aCurveFrom, myTminFrom, myTmaxFrom);
769 aProjector.Perform(aPm2);
770 Standard_Integer aNbPoints=aProjector.NbPoints();
772 Standard_Real aDD=aProjector.LowerDistance();
773 if (aDD > myCriteria) {
775 aCommonPrt.SetType(TopAbs_EDGE);
776 aCommonPrt.AppendRange2 (myTminTo, af2);
777 aCommonPrt.AppendRange2 (al2, myTmaxTo);
783 aCommonPrt.SetType(TopAbs_EDGE);
784 aCommonPrt.AppendRange2 (myTminTo, af2);
785 aCommonPrt.AppendRange2 (al2, myTmaxTo);
789 IsIntersection (af1, al1);
790 if (!myParallel && !aCommonPrt.AllNullFlag()) {
792 GetParameterOnCurve2 (myPar1, aPar2);
793 aCommonPrt.SetType(TopAbs_VERTEX);
795 Standard_Boolean IsmyPar1 = Standard_True;
797 if(Abs(af1-myTminFrom) < Precision::PConfusion()) {
798 IsmyPar1 = Standard_False;
799 aCommonPrt.SetVertexParameter1(af1);
801 aCommonPrt.SetVertexParameter2(al2);
803 aCommonPrt.SetVertexParameter2(af2);
806 if(Abs(al1-myTmaxFrom) < Precision::PConfusion()) {
807 IsmyPar1 = Standard_False;
808 aCommonPrt.SetVertexParameter1(al1);
811 aCommonPrt.SetVertexParameter2(af2);
813 aCommonPrt.SetVertexParameter2(al2);
816 if(Abs(af2-myTminTo) < Precision::PConfusion()) {
817 IsmyPar1 = Standard_False;
818 aCommonPrt.SetVertexParameter2(af2);
821 aCommonPrt.SetVertexParameter1(al1);
823 aCommonPrt.SetVertexParameter1(af1);
826 if(Abs(al2-myTmaxTo) < Precision::PConfusion()) {
827 IsmyPar1 = Standard_False;
828 aCommonPrt.SetVertexParameter2(al2);
831 aCommonPrt.SetVertexParameter1(af1);
833 aCommonPrt.SetVertexParameter1(al1);
835 // aCommonPrt.SetVertexParameter1(myPar1);
836 // aCommonPrt.SetRange1 (af1, al1);
838 // aCommonPrt.SetVertexParameter2(aPar2);
840 aCommonPrt.SetVertexParameter1(myPar1);
841 aCommonPrt.SetRange1 (af1, al1);
843 aCommonPrt.SetVertexParameter2(aPar2);
845 aCommonPrt.AppendRange2 (af2, al2);
851 aCommonPrt.SetType(TopAbs_EDGE);
852 aCommonPrt.AppendRange2 (af2, al2);
856 //=======================================================================
857 //function : IsIntersection
859 //=======================================================================
860 void IntTools_EdgeEdge::IsIntersection (const Standard_Real ta,
861 const Standard_Real tb)
863 Standard_Integer i, aNb;
865 GeomAbs_CurveType aCT1, aCT2;
866 IntTools_CArray1OfReal anArgs, aFunc;
868 aCT1=myCFrom.GetType();
869 aCT2=myCTo.GetType();
870 if((aCT1==GeomAbs_Line) && (aCT2==GeomAbs_Line)) {
871 const Handle(Geom_Curve)& Curve1=BRep_Tool::Curve (myCFrom.Edge(), t, f);
872 const Handle(Geom_Curve)& Curve2=BRep_Tool::Curve (myCTo.Edge() , t, f);
874 GeomAdaptor_Curve TheCurve1 (Curve1);
875 GeomAdaptor_Curve TheCurve2 (Curve2);
876 Extrema_ExtCC anExtrema (TheCurve1, TheCurve2);
878 if(anExtrema.IsDone() && anExtrema.IsParallel()) {
879 myParallel = Standard_True;
884 if (aCT1==GeomAbs_Circle && aCT2==GeomAbs_Circle) {
885 Standard_Boolean bIsDone, bIsParallel;
886 Standard_Integer aNbExt;
887 Standard_Real aD2, aCriteria2, aT1;
888 gp_Circ aCirc1, aCirc2;
889 Extrema_POnCurv aPC1, aPC2;
891 aCirc1=myCFrom.Circle();
892 aCirc2=myCTo.Circle();
894 Extrema_ExtElC aExtElC(aCirc1, aCirc2);
896 bIsDone=aExtElC.IsDone();
898 bIsParallel=aExtElC.IsParallel();
900 aCriteria2=myCriteria*myCriteria;
901 aNbExt=aExtElC.NbExt();
902 for (i=1; i<=aNbExt; ++i) {
903 aD2=aExtElC.SquareDistance(i);
904 if (aD2<aCriteria2) {
905 aExtElC.Points(i, aPC1, aPC2);
906 aT1=aPC1.Parameter();
907 if (aT1>ta && aT1<tb) {
909 myParallel=Standard_False;
918 // Prepare values of arguments for the interval [ta, tb]
919 IntTools::PrepareArgs (myCFrom, tb, ta, myDiscret, myDeflection, anArgs);
923 for (i=0; i<aNb; i++) {
925 f=DistanceFunction(t);
926 if (fabs(f) < myEpsNull) {
931 FindDerivativeRoot(anArgs, aFunc);
935 //=======================================================================
936 //function : FindDerivativeRoot
938 //=======================================================================
939 void IntTools_EdgeEdge::FindDerivativeRoot(const IntTools_CArray1OfReal& t,
940 const IntTools_CArray1OfReal& f)
942 Standard_Integer i, n, k;
943 Standard_Real tr, anEpsNull;
944 IntTools_CArray1OfReal fd;
945 TColStd_SequenceOfReal aTSeq, aFSeq;
947 anEpsNull=100.*myEpsNull;
949 myParallel=Standard_True;
954 // Table of derivatives
955 fd(0)=(f(1)-f(0))/(t(1)-t(0));
956 if (fabs(fd(0)) < anEpsNull) {
960 for (i=1; i<k; i++) {
961 fd(i)=.5*(f(i+1)-f(i-1))/(t(i)-t(i-1));
962 if (fabs(fd(i)) < anEpsNull) {
966 fd(n-1)=(f(n-1)-f(n-2))/(t(n-1)-t(n-2));
967 if (fabs(fd(n-1)) < anEpsNull) {
971 // Finding the range where the derivatives have different signs
972 // for neighbouring points
973 for (i=1; i<n; i++) {
974 Standard_Real fd1, fd2, t1, t2, fabsfd1, fabsfd2;
975 Standard_Boolean bF1, bF2;
982 bF1=fabsfd1 < myEpsNull;
985 bF2=fabsfd2 < myEpsNull;
989 tr=FindSimpleRoot(2, t1, t2, fd1);
991 myParallel=Standard_False;
998 myParallel=Standard_False;
1005 myParallel=Standard_False;
1012 //=======================================================================
1013 //function : GetParameterOnCurve2
1015 //=======================================================================
1016 Standard_Integer IntTools_EdgeEdge::GetParameterOnCurve2(const Standard_Real aT1,
1017 Standard_Real& aT2) const
1020 Standard_Integer j, found, aNbPoints;
1021 const TopoDS_Edge& anEdgeTo=myCTo.Edge();
1022 const TopoDS_Edge& anEdgeFrom=myCFrom.Edge();
1024 Handle(Geom_Curve)aCurveFrom=BRep_Tool::Curve (anEdgeFrom, f, l);
1025 Handle(Geom_Curve)aCurveTo =BRep_Tool::Curve (anEdgeTo, f, l);
1028 aCurveFrom->D0 (aT1, aP1);
1029 GeomAPI_ProjectPointOnCurve aProjector;
1030 aProjector.Init(aCurveTo, myTminTo, myTmaxTo);
1031 aProjector.Perform(aP1);
1032 aNbPoints=aProjector.NbPoints();
1036 for (j=0; j<=1; j++) {
1042 aCurveFrom->D0 (tt, aP1);
1043 aProjector.Init(aCurveTo, myTminTo, myTmaxTo);
1044 aProjector.Perform(aP1);
1045 aNbPoints=aProjector.NbPoints();
1054 aCurveFrom->D0 (aT1, aP1);
1055 Standard_Real aDistance = RealLast();
1057 for(Standard_Integer pIt=0; pIt < 2; pIt++) {
1058 Standard_Real adist = aDistance;
1060 adist = aP1.Distance(aCurveTo->Value(myTminTo));
1062 adist = aP1.Distance(aCurveTo->Value(myTmaxTo));
1064 if(adist < myCriteria) {
1065 found = Standard_True;
1067 if(adist < aDistance) {
1068 aT2 = (pIt) ? myTminTo : myTmaxTo;
1082 for (j=1; j<=aNbPoints; j++) {
1083 aT2=aProjector.Parameter(j);
1084 f=aProjector.Distance(j);
1087 aT2=aProjector.LowerDistanceParameter();
1088 if (aT2 < myTminTo) {
1091 if (aT2 > myTmaxTo) {
1097 //=======================================================================
1098 //function : TreatVertexType
1100 //=======================================================================
1101 Standard_Integer IntTools_EdgeEdge::TreatVertexType(const Standard_Real am1,
1102 const Standard_Real am2,
1103 IntTools_CommonPrt& aCommonPrt)
1105 Standard_Real f1, l1, f2, l2, Alfa , aPeriod;
1106 gp_Pnt aPm1, aPm2, aP;
1110 const TopoDS_Edge& anEdgeFrom=myCFrom.Edge();
1111 Handle(Geom_Curve)aCurveFrom=BRep_Tool::Curve (anEdgeFrom, f1, l1);
1112 aCurveFrom->D1 (am1, aPm1, aVm1);
1115 const TopoDS_Edge& anEdgeTo=myCTo.Edge();
1116 Handle(Geom_Curve)aCurveTo=BRep_Tool::Curve (anEdgeTo, f2, l2);
1117 aCurveTo->D1 (am2, aPm2, aVm2);
1120 Alfa=aVm1.Angle(aVm2);
1122 if (Alfa < Precision::Angular()) {
1126 Standard_Real sinAlfa, cosAlfa, dd, tf1, tl1, tf2, tl2, aL1, aL2;
1127 Standard_Integer ip;
1132 dd=aPm1.Distance(aPm2);
1134 if (dd>myCriteria) {
1137 aL2=(myTolTo*cosAlfa+myTolFrom)/sinAlfa;
1139 aP.SetXYZ(aPm2.XYZ()-aVm2.XYZ()*aL2);
1140 ip=IntTools::Parameter (aP, aCurveTo, tf2);
1145 if(aP.Distance(aCurveTo->Value(tf2)) > myTolTo)
1149 aP.SetXYZ(aPm2.XYZ()+aVm2.XYZ()*aL2);
1150 ip=IntTools::Parameter (aP, aCurveTo, tl2);
1155 if(aP.Distance(aCurveTo->Value(tl2)) > myTolTo)
1159 if (dd>myCriteria) {
1163 aL1=(myTolFrom*cosAlfa+myTolTo)/sinAlfa;
1165 aP.SetXYZ(aPm1.XYZ()-aVm1.XYZ()*aL1);
1166 ip=IntTools::Parameter (aP, aCurveFrom, tf1);
1171 if(aP.Distance(aCurveFrom->Value(tf1)) > myTolFrom)
1175 aP.SetXYZ(aPm1.XYZ()+aVm1.XYZ()*aL1);
1176 ip=IntTools::Parameter (aP, aCurveFrom, tl1);
1181 if(aP.Distance(aCurveFrom->Value(tl1)) > myTolFrom)
1185 if (aCurveFrom->IsPeriodic()) {
1186 aPeriod=aCurveFrom->Period();
1187 if (tf1<f1 || tf1>l1) {
1190 if (tl1<f1 || tl1>l1) {
1196 aCommonPrt.SetRange1 (tf1, tl1);
1197 aCommonPrt.SetVertexParameter1((tf1 + tl1) * 0.5);
1200 if (aCurveTo->IsPeriodic() && tf2 > tl2) {
1201 // aCurveTo is periodic curve and we pass through 0.
1203 aPeriod=aCurveTo->Period();
1204 aCommonPrt.AppendRange2 (tf2, aPeriod);
1205 aCommonPrt.AppendRange2 (0., tl2);
1206 aCommonPrt.SetVertexParameter2((tf2 + aPeriod) * 0.5);
1222 // 1 - the method Perform() is not invoked
1223 // 2,3,4,5 -the method CheckData() fails
1224 // 6 - PrepareArgs() problems
1225 // 7 - No Projectable ranges
1226 // 8,9 - PrepareArgs() problems occured inside projectable Ranges
1227 // 10 - problems in FindRange2
1228 // 11 - can't fill array aFunc(i) in PrepareArgsFuncArrays:
1229 // possible reason is that no points on myCFrom that could be projected
1234 //=======================================================================
1235 //function : CheckTouchVertex
1236 //purpose : line/Circle refinement
1237 //=======================================================================
1238 Standard_Boolean IntTools_EdgeEdge::CheckTouchVertex (const IntTools_CommonPrt& aCP,
1239 Standard_Real& aTx1,
1240 Standard_Real& aTx2) const
1242 Standard_Boolean bFlag;
1243 Standard_Real aTFR1, aTLR1, aTFR2, aTLR2;
1244 Standard_Real aTL1, aTL2, aTC1, aTC2;
1245 Standard_Real aRC, aDLC, aD2, aC2, aTLx, aTCx;
1246 GeomAbs_CurveType aTFrom;
1249 gp_Pnt aPC, aPLx, aPCx;
1251 bFlag=Standard_False;
1252 aCP.Range1(aTFR1, aTLR1);
1253 (aCP.Ranges2())(1).Range(aTFR2, aTLR2);
1255 aTFrom=myCFrom.GetType();
1261 if (aTFrom==GeomAbs_Circle) {
1262 aCirc=myCFrom.Circle();
1270 aCirc=myCTo.Circle();
1271 aLine=myCFrom.Line();
1274 aPC=aCirc.Location();
1277 aDLC=aLine.Distance(aPC);
1278 if (fabs(aDLC-aRC)>myCriteria) {
1282 aTLx=ElCLib::Parameter(aLine, aPC);
1283 aPLx=ElCLib::Value(aTLx, aLine);
1284 aTCx=ElCLib::Parameter(aCirc, aPLx);
1285 aPCx=ElCLib::Value(aTCx, aCirc);
1286 aD2=aPLx.SquareDistance(aPCx);
1287 aC2=myCriteria*myCriteria;
1292 if (aTLx<aTL1 || aTLx>aTL2) {
1295 if (aTCx<aTC1 || aTCx>aTC2) {
1301 if (aTFrom==GeomAbs_Circle) {
1308 //=======================================================================
1309 //function : CheckTouch
1311 //=======================================================================
1312 Standard_Boolean IntTools_EdgeEdge::CheckTouch(const IntTools_CommonPrt& aCP,
1313 Standard_Real& aTx1,
1314 Standard_Real& aTx2)
1316 Standard_Real aTF1, aTL1, aTF2, aTL2, Tol, af, al,aDist2, aMinDist2;
1317 Standard_Boolean theflag=Standard_False;
1318 Standard_Integer aNbExt, i, iLower;
1320 aCP.Range1(aTF1, aTL1);
1321 (aCP.Ranges2())(1).Range(aTF2, aTL2);
1323 Tol = Precision::PConfusion();
1325 const Handle(Geom_Curve)& Curve1 =BRep_Tool::Curve (myCFrom.Edge(), af, al);
1326 const Handle(Geom_Curve)& Curve2 =BRep_Tool::Curve (myCTo.Edge() , af, al);
1328 GeomAdaptor_Curve TheCurve1 (Curve1, aTF1, aTL1);
1329 GeomAdaptor_Curve TheCurve2 (Curve2, aTF2, aTL2);
1332 Standard_Real aTol1 = TheCurve1.Resolution(myCriteria);
1333 aTol1 = (Tol < aTol1) ? Tol : aTol1;
1335 Standard_Boolean isfirst = (Abs(myTminFrom - aTF1) < aTol1);
1336 Standard_Boolean islast = (Abs(myTmaxFrom - aTL1) < aTol1);
1338 if(!isfirst || !islast) {
1341 GeomAPI_ProjectPointOnCurve aProjector;
1342 aProjector.Init(Curve2, aTF2, aTL2);
1343 aProjector.Perform(Curve1->Value(aTx1));
1345 if(aProjector.NbPoints() > 0)
1346 aTx2 = aProjector.LowerDistanceParameter();
1348 if(Curve1->Value(aTx1).Distance(Curve2->Value(aTF2)) < myCriteria)
1358 GeomAPI_ProjectPointOnCurve aProjector;
1359 aProjector.Init(Curve2, aTF2, aTL2);
1360 aProjector.Perform(Curve1->Value(aTx1));
1361 if(aProjector.NbPoints() > 0)
1362 aTx2 = aProjector.LowerDistanceParameter();
1364 if(Curve1->Value(aTx1).Distance(Curve2->Value(aTL2)) < myCriteria)
1374 Extrema_ExtCC anExtrema (TheCurve1, TheCurve2, aTF1-Tol, aTL1+Tol, aTF2-Tol, aTL2+Tol, Tol, Tol);
1376 if(!anExtrema.IsDone()) {
1379 if (anExtrema.IsParallel()) {
1383 aNbExt=anExtrema.NbExt() ;
1388 Standard_Boolean istouch = Standard_True;
1389 Standard_Integer avalidindex = 0;
1393 for (i=1; i<=aNbExt; ++i) {
1394 aDist2=anExtrema.SquareDistance(i);
1395 if (aDist2 < aMinDist2) {
1400 if(aDist2 < myCriteria * myCriteria) {
1402 Extrema_POnCurv aPOnC1, aPOnC2;
1403 anExtrema.Points(i, aPOnC1, aPOnC2);
1404 Standard_Real aPar1 = aPOnC1.Parameter();
1405 anExtrema.Points(avalidindex, aPOnC1, aPOnC2);
1406 Standard_Real aPar2 = aPOnC1.Parameter();
1408 if(Abs(aPar1 - aPar2) > Precision::PConfusion()) {
1409 istouch = Standard_False;
1416 aDist2=anExtrema.SquareDistance(iLower);
1417 if (aDist2 > myCriteria * myCriteria) {
1421 Extrema_POnCurv aPOnC1, aPOnC2;
1422 anExtrema.Points(iLower, aPOnC1, aPOnC2);
1424 aTx1=aPOnC1.Parameter();
1425 aTx2=aPOnC2.Parameter();
1427 if((myCFrom.GetType() == GeomAbs_Line && myCTo.GetType() == GeomAbs_Circle) ||
1428 (myCFrom.GetType() == GeomAbs_Circle && myCTo.GetType() == GeomAbs_Line))
1430 Standard_Real aRadius;
1431 GeomAbs_CurveType aTFrom;
1434 gp_Pnt aPCenter, aPOnLine;
1436 aTFrom=myCFrom.GetType();
1438 if (aTFrom==GeomAbs_Circle) {
1439 aCirc=myCFrom.Circle();
1441 Curve2->D0(aTx2, aPOnLine);
1445 aCirc=myCTo.Circle();
1446 aLine=myCFrom.Line();
1447 Curve1->D0(aTx1, aPOnLine);
1451 aPCenter=aCirc.Location();
1452 aRadius =aCirc.Radius();
1454 aDist2=aPOnLine.SquareDistance(aPCenter);
1455 aDist2=fabs (sqrt(aDist2)-aRadius);
1457 if (aDist2 < Tol * Tol) {
1462 GeomAPI_ProjectPointOnCurve aProjector;
1463 Standard_Real aMidPar, aMidDist;
1464 aMidPar = (aTF1 + aTL1) * 0.5;
1465 aProjector.Init(Curve2, aTF2, aTL2);
1466 aProjector.Perform(Curve1->Value(aMidPar));
1467 if(aProjector.NbPoints() > 0) {
1468 aMidDist=aProjector.LowerDistance();
1469 if(aMidDist * aMidDist < aDist2 || !istouch) {
1471 aTx2 = aProjector.LowerDistanceParameter();
1475 if (fabs (aTx1-aTF1) < Tol) {
1479 if (fabs (aTx1-aTL1) < Tol) {
1483 if (aTx1 > (aTF1-Tol) && aTx1 < (aTL1+Tol) ) {
1490 //=======================================================================
1491 //function : ComputeLineLine
1493 //=======================================================================
1494 void IntTools_EdgeEdge::ComputeLineLine()
1496 Standard_Boolean IsParallel, IsCoincide;
1497 Standard_Real Tolang2, Tol2;
1498 gp_Pnt P11, P12, P21, P22;
1500 myIsDone = Standard_True;
1502 IsParallel = Standard_False;
1503 IsCoincide = Standard_False;
1504 Tolang2 = Precision::Angular();
1505 Tol2 = myCriteria*myCriteria;
1507 gp_Lin C1 = myCFrom.Line();
1508 gp_Lin C2 = myCTo.Line();
1509 const gp_Dir& D1 = C1.Position().Direction();
1510 const gp_Dir& D2 = C2.Position().Direction();
1511 Standard_Real aCos = D1.Dot(D2);
1515 Ang2 = 2.*(1. - aCos);
1518 Ang2 = 2.*(1. + aCos);
1521 if(Ang2 <= Tolang2) {
1522 IsParallel = Standard_True;
1523 if(C2.SquareDistance(C1.Location()) <= Tol2) {
1524 IsCoincide = Standard_True;
1525 P11 = ElCLib::Value(myTminFrom, C1);
1526 P12 = ElCLib::Value(myTmaxFrom, C1);
1530 //Check coincidence of extremity points;
1531 //Check only shortest line
1532 P11 = ElCLib::Value(myTminFrom, C1);
1533 P12 = ElCLib::Value(myTmaxFrom, C1);
1534 if(C2.SquareDistance(P11) <= Tol2 && C2.SquareDistance(P12) <= Tol2) {
1535 IsCoincide = Standard_True;
1540 Standard_Real t21, t22;
1541 t21 = ElCLib::Parameter(C2, P11);
1542 t22 = ElCLib::Parameter(C2, P12);
1544 if((t21 > myTmaxTo && t22 > myTmaxTo) || (t21 < myTminTo && t22 < myTminTo)) {
1555 IntTools_CommonPrt aCommonPrt;
1556 aCommonPrt.SetEdge1(myCFrom.Edge());
1557 aCommonPrt.SetEdge2(myCTo.Edge());
1558 if(t21 >= myTminTo) {
1559 if(t22 <= myTmaxTo) {
1560 aCommonPrt.SetRange1(myTminFrom, myTmaxFrom);
1561 aCommonPrt.SetAllNullFlag(Standard_True);
1562 aCommonPrt.AppendRange2(t21, t22);
1565 aCommonPrt.SetRange1(myTminFrom, myTmaxFrom - (t22 - myTmaxTo));
1566 aCommonPrt.AppendRange2(t21, myTmaxTo);
1570 aCommonPrt.SetRange1(myTminFrom + (myTminTo - t21), myTmaxFrom);
1571 aCommonPrt.AppendRange2(myTminTo, t22);
1573 aCommonPrt.SetType(TopAbs_EDGE);
1574 mySeqOfCommonPrts.Append(aCommonPrt);
1584 TopoDS_Iterator aIt1, aIt2;
1586 aIt1.Initialize(myEdge1);
1587 for (; aIt1.More(); aIt1.Next()) {
1588 const TopoDS_Shape& aV1=aIt1.Value();
1589 aIt2.Initialize(myEdge2);
1590 for (; aIt2.More(); aIt2.Next()) {
1591 const TopoDS_Shape& aV2=aIt2.Value();
1592 if (aV2.IsSame(aV1)) {
1593 // the two straight lines have commpn vertex
1600 Standard_Real aSin2 = 1. - aCos*aCos;
1601 gp_Pnt O1 = C1.Location();
1602 gp_Pnt O2 = C2.Location();
1603 gp_Vec O1O2 (O1,O2);
1604 Standard_Real U2 = (D1.XYZ()*(O1O2.Dot(D1))-(O1O2.XYZ())).Dot(D2.XYZ());
1606 if(U2 < myTminTo || U2 > myTmaxTo) {
1610 gp_Pnt P2(ElCLib::Value(U2,C2));
1611 Standard_Real U1 = (gp_Vec(O1,P2)).Dot(D1);
1612 if(U1 < myTminFrom || U1 > myTmaxFrom) {
1616 gp_Pnt P1(ElCLib::Value(U1,C1));
1617 Standard_Real d2 = P1.SquareDistance(P2);
1623 IntTools_CommonPrt aCommonPrt;
1624 aCommonPrt.SetEdge1(myCFrom.Edge());
1625 aCommonPrt.SetEdge2(myCTo.Edge());
1626 aCommonPrt.SetRange1(U1 - myCriteria, U1 + myCriteria);
1627 aCommonPrt.AppendRange2(U2 - myCriteria, U2 + myCriteria);
1628 aCommonPrt.SetType(TopAbs_VERTEX);
1629 aCommonPrt.SetVertexParameter1(U1);
1630 aCommonPrt.SetVertexParameter2(U2);
1631 mySeqOfCommonPrts.Append(aCommonPrt);