1 // Created on: 2000-11-16
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 under
8 // the terms of the GNU Lesser General Public License 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.
17 #include <Bnd_Box.hxx>
18 #include <BRep_Builder.hxx>
19 #include <BRep_Tool.hxx>
20 #include <BRepAdaptor_Curve.hxx>
21 #include <BRepAdaptor_Surface.hxx>
22 #include <Geom2d_Curve.hxx>
23 #include <Geom2d_TrimmedCurve.hxx>
24 #include <Geom_BoundedCurve.hxx>
25 #include <Geom_Curve.hxx>
26 #include <Geom_Geometry.hxx>
27 #include <Geom_Line.hxx>
28 #include <Geom_Surface.hxx>
29 #include <Geom_TrimmedCurve.hxx>
30 #include <GeomAbs_CurveType.hxx>
31 #include <GeomAdaptor_Curve.hxx>
32 #include <GeomAdaptor_Surface.hxx>
33 #include <GeomAPI_ProjectPointOnCurve.hxx>
34 #include <GeomAPI_ProjectPointOnSurf.hxx>
41 #include <gp_Pnt2d.hxx>
42 #include <IntTools_CommonPrt.hxx>
43 #include <IntTools_Curve.hxx>
44 #include <IntTools_FClass2d.hxx>
45 #include <IntTools_Range.hxx>
46 #include <IntTools_SequenceOfCurves.hxx>
47 #include <IntTools_Tools.hxx>
48 #include <Precision.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TopLoc_Location.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Face.hxx>
54 #include <TopoDS_Shape.hxx>
55 #include <TopoDS_Vertex.hxx>
56 #include <TopoDS_Wire.hxx>
57 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
60 void ParabolaTolerance(const Handle(Geom_Curve)& ,
67 //=======================================================================
68 //function : HasInternalEdge
70 //=======================================================================
71 Standard_Boolean IntTools_Tools::HasInternalEdge(const TopoDS_Wire& aW)
73 Standard_Boolean bFlag=Standard_True;
75 TopExp_Explorer anExp(aW, TopAbs_EDGE);
76 for (; anExp.More(); anExp.Next()) {
77 const TopoDS_Edge& aE=TopoDS::Edge(anExp.Current());
78 TopAbs_Orientation anOr=aE.Orientation();
79 if (anOr==TopAbs_INTERNAL) {
86 //=======================================================================
89 //=======================================================================
90 Standard_Boolean IntTools_Tools::IsClosed (const Handle(Geom_Curve)& aC3D)
92 Standard_Boolean bRet;
93 Standard_Real aF, aL, aDist, aPC;
96 Handle (Geom_BoundedCurve) aGBC=
97 Handle (Geom_BoundedCurve)::DownCast(aC3D);
99 return Standard_False;
102 aF=aC3D->FirstParameter();
103 aL=aC3D-> LastParameter();
110 aPC=Precision::Confusion();
112 aDist=aP1.SquareDistance(aP2);
117 //=======================================================================
118 //function : RejectLines
120 //=======================================================================
121 void IntTools_Tools::RejectLines(const IntTools_SequenceOfCurves& aSIn,
122 IntTools_SequenceOfCurves& aSOut)
124 Standard_Integer i, j, aNb;
125 Standard_Boolean bFlag;
126 Handle (Geom_Curve) aC3D;
133 for (i=1; i<=aNb; i++) {
134 const IntTools_Curve& IC=aSIn(i);
137 Handle (Geom_TrimmedCurve) aGTC=
138 Handle (Geom_TrimmedCurve)::DownCast(aC3D);
140 if (!aGTC.IsNull()) {
141 aC3D=aGTC->BasisCurve();
142 IntTools_Curve* pIC=(IntTools_Curve*) &IC;
146 Handle (Geom_Line) aGLine=
147 Handle (Geom_Line)::DownCast(aC3D);
149 if (aGLine.IsNull()) {
151 for (j=1; j<=aNb; j++) {
152 aSOut.Append(aSIn(j));
157 gp_Lin aLin=aGLine->Lin();
158 aD2=aLin.Direction();
165 bFlag=IntTools_Tools::IsDirsCoinside(aD1, aD2);
173 //=======================================================================
174 //function : IsDirsCoinside
176 //=======================================================================
177 Standard_Boolean IntTools_Tools::IsDirsCoinside (const gp_Dir& D1, const gp_Dir& D2)
179 Standard_Boolean bFlag;
180 gp_Pnt P1(D1.X(), D1.Y(), D1.Z());
181 gp_Pnt P2(D2.X(), D2.Y(), D2.Z());
182 Standard_Real dLim=0.0002, d;
184 bFlag= (d<dLim || fabs (2.-d)<dLim);
188 //=======================================================================
189 //function : IsDirsCoinside
191 //=======================================================================
192 Standard_Boolean IntTools_Tools::IsDirsCoinside (const gp_Dir& D1,
194 const Standard_Real dLim)
196 Standard_Boolean bFlag;
199 gp_Pnt P1(D1.X(), D1.Y(), D1.Z());
200 gp_Pnt P2(D2.X(), D2.Y(), D2.Z());
202 bFlag= (d<dLim || fabs (2.-d)<dLim);
205 //=======================================================================
206 //function : SplitCurve
208 //=======================================================================
209 Standard_Integer IntTools_Tools::SplitCurve(const IntTools_Curve& IC,
210 IntTools_SequenceOfCurves& aCvs)
212 Handle (Geom_Curve) aC3D =IC.Curve();
216 Handle (Geom2d_Curve) aC2D1=IC.FirstCurve2d();
217 Handle (Geom2d_Curve) aC2D2=IC.SecondCurve2d();
218 Standard_Boolean bIsClosed;
220 bIsClosed=IntTools_Tools::IsClosed(aC3D);
225 Standard_Real aF, aL, aMid;
228 aF=aC3D->FirstParameter();
229 aL=aC3D->LastParameter();
231 GeomAdaptor_Curve aGAC(aC3D);
232 GeomAbs_CurveType aCT=aGAC.GetType();
233 if (aCT==GeomAbs_BSplineCurve ||
234 aCT==GeomAbs_BezierCurve) {
236 aMid=IntTools_Tools::IntermediatePoint(aF, aL);
239 Handle(Geom_Curve) aC3DNewF, aC3DNewL;
240 aC3DNewF =new Geom_TrimmedCurve (aC3D, aF, aMid);
241 aC3DNewL =new Geom_TrimmedCurve (aC3D, aMid, aL);
244 Handle (Geom2d_Curve) aC2D1F, aC2D1L, aC2D2F, aC2D2L;
246 if(!aC2D1.IsNull()) {
247 aC2D1F=new Geom2d_TrimmedCurve (aC2D1, aF, aMid);
248 aC2D1L=new Geom2d_TrimmedCurve (aC2D1, aMid, aL);
251 if(!aC2D2.IsNull()) {
252 aC2D2F=new Geom2d_TrimmedCurve (aC2D2, aF, aMid);
253 aC2D2L=new Geom2d_TrimmedCurve (aC2D2, aMid, aL);
256 IntTools_Curve aIC1(aC3DNewF, aC2D1F, aC2D2F, IC.Tolerance(), IC.TangentialTolerance());
257 IntTools_Curve aIC2(aC3DNewL, aC2D1L, aC2D2L, IC.Tolerance(), IC.TangentialTolerance());
266 //=======================================================================
267 //function : IntermediatePoint
269 //=======================================================================
270 Standard_Real IntTools_Tools::IntermediatePoint (const Standard_Real aFirst,
271 const Standard_Real aLast)
273 //define parameter division number as 10*e^(-M_PI) = 0.43213918
274 const Standard_Real PAR_T = 0.43213918;
276 aParm=(1.-PAR_T)*aFirst + PAR_T*aLast;
280 //=======================================================================
281 //function : IsVertex
283 //=======================================================================
284 Standard_Boolean IntTools_Tools::IsVertex (const gp_Pnt& aP,
285 const Standard_Real aTolPV,
286 const TopoDS_Vertex& aV)
288 Standard_Boolean bRet;
289 Standard_Real aTolV, aD, dTol;
292 aTolV=BRep_Tool::Tolerance(aV);
294 dTol=Precision::Confusion();
295 aTolV=aTolV+aTolPV+dTol;
297 aPv=BRep_Tool::Pnt(aV);
299 aD=aPv.SquareDistance(aP);
306 //=======================================================================
307 //function : IsVertex
309 //=======================================================================
310 Standard_Boolean IntTools_Tools::IsVertex (const IntTools_CommonPrt& aCmnPrt)
312 Standard_Boolean anIsVertex;
313 Standard_Real aParam;
315 const TopoDS_Edge& aE1=aCmnPrt.Edge1();
316 const IntTools_Range& aR1=aCmnPrt.Range1();
317 aParam=0.5*(aR1.First()+aR1.Last());
318 anIsVertex=IntTools_Tools::IsVertex (aE1, aParam);
321 return Standard_True;
324 const TopoDS_Edge& aE2=aCmnPrt.Edge2();
325 const IntTools_SequenceOfRanges& aRs2=aCmnPrt.Ranges2();
326 const IntTools_Range& aR2=aRs2(1);
327 aParam=0.5*(aR2.First()+aR2.Last());
328 anIsVertex=IntTools_Tools::IsVertex (aE2, aParam);
330 return Standard_True;
332 return Standard_False;
335 //=======================================================================
336 //function : IsVertex
338 //=======================================================================
339 Standard_Boolean IntTools_Tools::IsVertex (const TopoDS_Edge& aE,
340 const TopoDS_Vertex& aV,
341 const Standard_Real t)
343 Standard_Real aTolV, aTolV2, d2;
346 BRepAdaptor_Curve aBAC(aE);
349 aTolV=BRep_Tool::Tolerance(aV);
351 aPv=BRep_Tool::Pnt(aV);
352 d2=aPv.SquareDistance (aPt);
354 return Standard_True;
356 return Standard_False;
358 //=======================================================================
359 //function : IsVertex
361 //=======================================================================
362 Standard_Boolean IntTools_Tools::IsVertex (const TopoDS_Edge& aE,
363 const Standard_Real t)
365 Standard_Real aTolV, aTolV2, d2;
369 BRepAdaptor_Curve aBAC(aE);
372 TopExp_Explorer anExp(aE, TopAbs_VERTEX);
373 for (; anExp.More(); anExp.Next()) {
374 aV=TopoDS::Vertex (anExp.Current());
375 aTolV=BRep_Tool::Tolerance(aV);
378 aPv=BRep_Tool::Pnt(aV);
379 d2=aPv.SquareDistance (aPt);
381 return Standard_True;
384 return Standard_False;
388 //=======================================================================
389 //function : ComputeVV
391 //=======================================================================
392 Standard_Integer IntTools_Tools::ComputeVV(const TopoDS_Vertex& aV1,
393 const TopoDS_Vertex& aV2)
395 Standard_Real aTolV1, aTolV2, aTolSum, d;
398 aTolV1=BRep_Tool::Tolerance(aV1);
399 aTolV2=BRep_Tool::Tolerance(aV2);
400 aTolSum=aTolV1+aTolV2;
402 aP1=BRep_Tool::Pnt(aV1);
403 aP2=BRep_Tool::Pnt(aV2);
404 aTolSum=aTolSum*aTolSum;
405 d=aP1.SquareDistance(aP2);
412 //=======================================================================
413 //function : MakeFaceFromWireAndFace
415 //=======================================================================
416 void IntTools_Tools::MakeFaceFromWireAndFace(const TopoDS_Wire& aW,
417 const TopoDS_Face& aF,
422 aFF.Orientation(TopAbs_FORWARD);
423 aFNew=TopoDS::Face (aFF.EmptyCopied());
428 //=======================================================================
429 //function : ClassifyPointByFace
431 //=======================================================================
432 TopAbs_State IntTools_Tools::ClassifyPointByFace(const TopoDS_Face& aF,
433 const gp_Pnt2d& aP2d)
435 Standard_Real aFaceTolerance;
438 aFaceTolerance=BRep_Tool::Tolerance(aF);
439 IntTools_FClass2d aClass2d(aF, aFaceTolerance);
440 aState=aClass2d.Perform(aP2d);
445 //=======================================================================
446 //function : IsMiddlePointsEqual
448 //=======================================================================
449 Standard_Boolean IntTools_Tools::IsMiddlePointsEqual(const TopoDS_Edge& aE1,
450 const TopoDS_Edge& aE2)
453 Standard_Boolean bRet;
454 Standard_Real f1, l1, m1, f2, l2, m2, aTol1, aTol2, aSumTol, aD2;
457 aTol1=BRep_Tool::Tolerance(aE1);
458 Handle(Geom_Curve) C1=BRep_Tool::Curve(aE1, f1, l1);
462 aTol2=BRep_Tool::Tolerance(aE2);
463 Handle(Geom_Curve) C2=BRep_Tool::Curve(aE2, f2, l2);
468 aSumTol=aSumTol*aSumTol;
469 aD2=aP1.SquareDistance(aP2);
474 //=======================================================================
475 //function : CurveTolerance
477 //=======================================================================
478 Standard_Real IntTools_Tools::CurveTolerance(const Handle(Geom_Curve)& aC3D,
479 const Standard_Real aTolBase)
481 Standard_Real aTolReached, aTf, aTl, aTolMin, aTolMax;
483 aTolReached=aTolBase;
489 Handle(Geom_TrimmedCurve) aCT3D=Handle(Geom_TrimmedCurve)::DownCast(aC3D);
490 if (aCT3D.IsNull()) {
497 aTf=aCT3D->FirstParameter();
498 aTl=aCT3D->LastParameter();
500 GeomAdaptor_Curve aGAC(aCT3D);
501 GeomAbs_CurveType aCType=aGAC.GetType();
503 if (aCType==GeomAbs_Parabola) {
504 Handle(Geom_Curve) aC3DBase=aCT3D->BasisCurve();
505 ParabolaTolerance(aC3DBase, aTf, aTl, aTolBase, aTolMin, aTolMax);
512 #include <Geom_Parabola.hxx>
513 #include <gp_Parab.hxx>
514 #include <BndLib_Add3dCurve.hxx>
515 #include <BRepLib_CheckCurveOnSurface.hxx>
516 //=======================================================================
517 //function : ParabolaTolerance
519 //=======================================================================
520 void ParabolaTolerance(const Handle(Geom_Curve)& aC3D,
521 const Standard_Real aTf,
522 const Standard_Real aTl,
523 const Standard_Real aTol,
524 Standard_Real& aTolMin,
525 Standard_Real& aTolMax)
531 Handle(Geom_Parabola) aGP=Handle(Geom_Parabola)::DownCast(aC3D);
536 Standard_Integer aNbPoints;
537 Standard_Real aFocal, aX1, aX2, aTol1, aTol2;
539 gp_Parab aParab=aGP->Parab();
540 gp_Ax1 aXAxis=aParab.XAxis();
541 Handle(Geom_Line) aGAxis=new Geom_Line(aXAxis);
552 GeomAPI_ProjectPointOnCurve aProj1(aPf, aGAxis);
553 aNbPoints=aProj1.NbPoints();
555 aX1=aProj1.LowerDistanceParameter();
558 aTol1=aTol*sqrt(0.5*aX1/aFocal);
568 GeomAPI_ProjectPointOnCurve aProj2(aPl, aGAxis);
569 aNbPoints=aProj2.NbPoints();
571 aX2=aProj2.LowerDistanceParameter();
575 aTol2=aTol*sqrt(0.5*aX2/aFocal);
581 aTolMax=(aTol1>aTol2) ? aTol1 : aTol2;
582 aTolMin=(aTol1<aTol2) ? aTol1 : aTol2;
584 /////////////////////////////////////////////////////////////////////////
585 //=======================================================================
586 //function : CheckCurve
588 //=======================================================================
589 Standard_Boolean IntTools_Tools::CheckCurve(const IntTools_Curve& theCurve,
592 const Handle(Geom_Curve)& aC3D = theCurve.Curve();
593 Standard_Boolean bValid = !aC3D.IsNull();
598 // Build bounding box for the curve
599 BndLib_Add3dCurve::Add(GeomAdaptor_Curve(aC3D),
600 Max(theCurve.Tolerance(), theCurve.TangentialTolerance()),
603 // Estimate the bounding box of the curve comparing it with the
604 // minimal length for the curve from which the valid edge can be built -
605 // 3*Precision::Confusion():
606 // - 2 vertices with the Precision::Confusion() tolerance;
607 // - plus Precision::Confusion() as the minimal distance between vertices.
608 Standard_Real aTolCmp = 3*Precision::Confusion();
610 // Check the size of the box using the Bnd_Box::IsThin() method
611 // which does not use the gap of the box.
612 bValid = !theBox.IsThin(aTolCmp);
616 //=======================================================================
617 //function : IsOnPave
619 //=======================================================================
620 Standard_Boolean IntTools_Tools::IsOnPave(const Standard_Real aT1,
621 const IntTools_Range& aRange,
622 const Standard_Real aTolerance)
624 Standard_Boolean firstisonpave1, firstisonpave2, bIsOnPave;
626 firstisonpave1 = (Abs(aRange.First() - aT1) < aTolerance);
627 firstisonpave2 = (Abs(aRange.Last() - aT1) < aTolerance);
628 bIsOnPave=(firstisonpave1 || firstisonpave2);
631 //=======================================================================
632 // function: VertexParameters
634 //=======================================================================
635 void IntTools_Tools::VertexParameters(const IntTools_CommonPrt& aCPart,
639 const IntTools_Range& aR1=aCPart.Range1();
640 aT1=0.5*(aR1.First()+aR1.Last());
642 if((aCPart.VertexParameter1() >= aR1.First()) &&
643 (aCPart.VertexParameter1() <= aR1.Last())) {
644 aT1 = aCPart.VertexParameter1();
647 const IntTools_SequenceOfRanges& aRanges2=aCPart.Ranges2();
648 const IntTools_Range& aR2=aRanges2(1);
649 aT2=0.5*(aR2.First()+aR2.Last());
651 if((aCPart.VertexParameter2() >= aR2.First()) &&
652 (aCPart.VertexParameter2() <= aR2.Last())) {
653 aT2 = aCPart.VertexParameter2();
656 //=======================================================================
657 // function: VertexParameter
659 //=======================================================================
660 void IntTools_Tools::VertexParameter(const IntTools_CommonPrt& aCPart,
663 const IntTools_Range& aR=aCPart.Range1();
664 aT=0.5*(aR.First()+aR.Last());
665 if((aCPart.VertexParameter1() >= aR.First()) &&
666 (aCPart.VertexParameter1() <= aR.Last())) {
667 aT = aCPart.VertexParameter1();
670 //=======================================================================
671 // function: IsOnPave1
673 //=======================================================================
674 Standard_Boolean IntTools_Tools::IsOnPave1(const Standard_Real aTR,
675 const IntTools_Range& aCPRange,
676 const Standard_Real aTolerance)
678 Standard_Boolean bIsOnPave;
679 Standard_Real aT1, aT2, dT1, dT2;
681 aT1=aCPRange.First();
683 bIsOnPave=(aTR>=aT1 && aTR<=aT2);
690 bIsOnPave=(dT1<=aTolerance || dT2<=aTolerance);
693 //=======================================================================
694 // function: IsInRange
696 //=======================================================================
697 Standard_Boolean IntTools_Tools::IsInRange(const IntTools_Range& aRRef,
698 const IntTools_Range& aR,
699 const Standard_Real aTolerance)
701 Standard_Boolean bIsIn;
702 Standard_Real aT1, aT2, aTRef1, aTRef2;
705 aRRef.Range(aTRef1, aTRef2);
710 bIsIn = (aT1>=aTRef1 && aT1<=aTRef2) ||
711 (aT2>=aTRef1 && aT2<=aTRef2);
715 //=======================================================================
718 //=======================================================================
719 Standard_Integer IntTools_Tools::SegPln(const gp_Lin& theLin,
720 const Standard_Real theTLin1,
721 const Standard_Real theTLin2,
722 const Standard_Real theTolLin,
723 const gp_Pln& thePln,
724 const Standard_Real theTolPln,
726 Standard_Real& theTP,
727 Standard_Real& theTolP,
728 Standard_Real& theTPmin,
729 Standard_Real& theTPmax)
731 Standard_Integer iRet;
732 Standard_Real aTol, aA, aB, aC, aD, aE, aH, aTP, aDist1, aDist2;
736 aTol=theTolLin+theTolPln;
738 const gp_Ax3& aPosPln=thePln.Position();
739 const gp_Dir& aDirPln=aPosPln.Direction();
740 const gp_Pnt& aLocPln=aPosPln.Location();
742 const gp_Dir& aDirLin=theLin.Direction();
743 const gp_Pnt& aLocLin=theLin.Location();
745 aP1.SetXYZ(aLocLin.XYZ()+theTLin1*aDirLin.XYZ());
746 aDist1=aDirPln.X()*(aP1.X()-aLocPln.X())+
747 aDirPln.Y()*(aP1.Y()-aLocPln.Y())+
748 aDirPln.Z()*(aP1.Z()-aLocPln.Z());
750 aP2.SetXYZ(aLocLin.XYZ()+theTLin2*aDirLin.XYZ());
751 aDist2=aDirPln.X()*(aP2.X()-aLocPln.X())+
752 aDirPln.Y()*(aP2.Y()-aLocPln.Y())+
753 aDirPln.Z()*(aP2.Z()-aLocPln.Z());
755 if (aDist1<aTol && aDist2<aTol){
756 iRet=1; // common block
760 if (aDist1*aDist2 > 0.) {
761 iRet=2; // segment lays on one side to the Plane
765 thePln.Coefficients(aA, aB, aC, aD);
766 aE=aA*aLocLin.X()+aB*aLocLin.Y()+aC*aLocLin.Z()+aD;
767 aH=aA*aDirLin.X()+aB*aDirLin.Y()+aC*aDirLin.Z();
769 if (aTP < theTLin1-aTol || aTP > theTLin2+aTol) {
770 iRet=3; // no intersections due to range of the Line
775 theP.SetXYZ(aLocLin.XYZ()+aTP*aDirLin.XYZ());
777 theTPmin=theTP-theTolPln;
778 theTPmax=theTP+theTolPln;
779 iRet=0; // intersection point
783 //=======================================================================
784 // Function : ComputeTolerance
786 //=======================================================================
787 Standard_Boolean IntTools_Tools::ComputeTolerance
788 (const Handle(Geom_Curve)& theCurve3D,
789 const Handle(Geom2d_Curve)& theCurve2D,
790 const Handle(Geom_Surface)& theSurf,
791 const Standard_Real theFirst,
792 const Standard_Real theLast,
793 Standard_Real& theMaxDist,
794 Standard_Real& theMaxPar,
795 const Standard_Real theTolRange)
797 GeomLib_CheckCurveOnSurface aCS;
799 aCS.Init(theCurve3D, theSurf, theFirst, theLast, theTolRange);
800 aCS.Perform(theCurve2D);
802 return Standard_False;
805 //Obtaining precise result is impossible if we use
806 //numeric methods for solution. Therefore, we must provide
807 //some margin. Otherwise, in the future
808 //(when geometrical properties of the curve will be changed,
809 //e.g. after trimming) we will be able to come
810 //to the more precise minimum point. As result, this curve with the
811 //tolerance computed earlier will become invalid.
812 const Standard_Real anEps = (1.0+1.0e-5);
813 theMaxDist = anEps*aCS.MaxDistance();
814 theMaxPar = aCS.MaxParameter();
816 return Standard_True;
819 //=======================================================================
820 // Function : ComputeIntRange
822 //=======================================================================
823 Standard_Real IntTools_Tools::ComputeIntRange(const Standard_Real theTol1,
824 const Standard_Real theTol2,
825 const Standard_Real theAngle)
829 if (Abs(M_PI_2 - theAngle) < Precision::Angular()) {
833 Standard_Real a1, a2, anAngle;
835 anAngle = (theAngle > M_PI_2) ? (M_PI - theAngle) : theAngle;
836 a1 = theTol1 * tan(M_PI_2 - anAngle);
837 a2 = theTol2 / sin(anAngle);