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);
257 IntTools_Curve aIC1(aC3DNewF, aC2D1F, aC2D2F);
258 IntTools_Curve aIC2(aC3DNewL, aC2D1L, aC2D2L);
267 //=======================================================================
268 //function : IntermediatePoint
270 //=======================================================================
271 Standard_Real IntTools_Tools::IntermediatePoint (const Standard_Real aFirst,
272 const Standard_Real aLast)
274 //define parameter division number as 10*e^(-M_PI) = 0.43213918
275 const Standard_Real PAR_T = 0.43213918;
277 aParm=(1.-PAR_T)*aFirst + PAR_T*aLast;
281 //=======================================================================
282 //function : IsVertex
284 //=======================================================================
285 Standard_Boolean IntTools_Tools::IsVertex (const gp_Pnt& aP,
286 const Standard_Real aTolPV,
287 const TopoDS_Vertex& aV)
289 Standard_Boolean bRet;
290 Standard_Real aTolV, aD, dTol;
293 aTolV=BRep_Tool::Tolerance(aV);
295 dTol=Precision::Confusion();
296 aTolV=aTolV+aTolPV+dTol;
298 aPv=BRep_Tool::Pnt(aV);
300 aD=aPv.SquareDistance(aP);
307 //=======================================================================
308 //function : IsVertex
310 //=======================================================================
311 Standard_Boolean IntTools_Tools::IsVertex (const IntTools_CommonPrt& aCmnPrt)
313 Standard_Boolean anIsVertex;
314 Standard_Real aParam;
316 const TopoDS_Edge& aE1=aCmnPrt.Edge1();
317 const IntTools_Range& aR1=aCmnPrt.Range1();
318 aParam=0.5*(aR1.First()+aR1.Last());
319 anIsVertex=IntTools_Tools::IsVertex (aE1, aParam);
322 return Standard_True;
325 const TopoDS_Edge& aE2=aCmnPrt.Edge2();
326 const IntTools_SequenceOfRanges& aRs2=aCmnPrt.Ranges2();
327 const IntTools_Range& aR2=aRs2(1);
328 aParam=0.5*(aR2.First()+aR2.Last());
329 anIsVertex=IntTools_Tools::IsVertex (aE2, aParam);
331 return Standard_True;
333 return Standard_False;
336 //=======================================================================
337 //function : IsVertex
339 //=======================================================================
340 Standard_Boolean IntTools_Tools::IsVertex (const TopoDS_Edge& aE,
341 const TopoDS_Vertex& aV,
342 const Standard_Real t)
344 Standard_Real aTolV, aTolV2, d2;
347 BRepAdaptor_Curve aBAC(aE);
350 aTolV=BRep_Tool::Tolerance(aV);
352 aPv=BRep_Tool::Pnt(aV);
353 d2=aPv.SquareDistance (aPt);
355 return Standard_True;
357 return Standard_False;
359 //=======================================================================
360 //function : IsVertex
362 //=======================================================================
363 Standard_Boolean IntTools_Tools::IsVertex (const TopoDS_Edge& aE,
364 const Standard_Real t)
366 Standard_Real aTolV, aTolV2, d2;
370 BRepAdaptor_Curve aBAC(aE);
373 TopExp_Explorer anExp(aE, TopAbs_VERTEX);
374 for (; anExp.More(); anExp.Next()) {
375 aV=TopoDS::Vertex (anExp.Current());
376 aTolV=BRep_Tool::Tolerance(aV);
379 aPv=BRep_Tool::Pnt(aV);
380 d2=aPv.SquareDistance (aPt);
382 return Standard_True;
385 return Standard_False;
389 //=======================================================================
390 //function : ComputeVV
392 //=======================================================================
393 Standard_Integer IntTools_Tools::ComputeVV(const TopoDS_Vertex& aV1,
394 const TopoDS_Vertex& aV2)
396 Standard_Real aTolV1, aTolV2, aTolSum, d;
399 aTolV1=BRep_Tool::Tolerance(aV1);
400 aTolV2=BRep_Tool::Tolerance(aV2);
401 aTolSum=aTolV1+aTolV2;
403 aP1=BRep_Tool::Pnt(aV1);
404 aP2=BRep_Tool::Pnt(aV2);
405 aTolSum=aTolSum*aTolSum;
406 d=aP1.SquareDistance(aP2);
413 //=======================================================================
414 //function : MakeFaceFromWireAndFace
416 //=======================================================================
417 void IntTools_Tools::MakeFaceFromWireAndFace(const TopoDS_Wire& aW,
418 const TopoDS_Face& aF,
423 aFF.Orientation(TopAbs_FORWARD);
424 aFNew=TopoDS::Face (aFF.EmptyCopied());
429 //=======================================================================
430 //function : ClassifyPointByFace
432 //=======================================================================
433 TopAbs_State IntTools_Tools::ClassifyPointByFace(const TopoDS_Face& aF,
434 const gp_Pnt2d& aP2d)
436 Standard_Real aFaceTolerance;
439 aFaceTolerance=BRep_Tool::Tolerance(aF);
440 IntTools_FClass2d aClass2d(aF, aFaceTolerance);
441 aState=aClass2d.Perform(aP2d);
446 //=======================================================================
447 //function : IsMiddlePointsEqual
449 //=======================================================================
450 Standard_Boolean IntTools_Tools::IsMiddlePointsEqual(const TopoDS_Edge& aE1,
451 const TopoDS_Edge& aE2)
454 Standard_Boolean bRet;
455 Standard_Real f1, l1, m1, f2, l2, m2, aTol1, aTol2, aSumTol, aD2;
458 aTol1=BRep_Tool::Tolerance(aE1);
459 Handle(Geom_Curve) C1=BRep_Tool::Curve(aE1, f1, l1);
463 aTol2=BRep_Tool::Tolerance(aE2);
464 Handle(Geom_Curve) C2=BRep_Tool::Curve(aE2, f2, l2);
469 aSumTol=aSumTol*aSumTol;
470 aD2=aP1.SquareDistance(aP2);
475 //=======================================================================
476 //function : CurveTolerance
478 //=======================================================================
479 Standard_Real IntTools_Tools::CurveTolerance(const Handle(Geom_Curve)& aC3D,
480 const Standard_Real aTolBase)
482 Standard_Real aTolReached, aTf, aTl, aTolMin, aTolMax;
484 aTolReached=aTolBase;
490 Handle(Geom_TrimmedCurve) aCT3D=Handle(Geom_TrimmedCurve)::DownCast(aC3D);
491 if (aCT3D.IsNull()) {
498 aTf=aCT3D->FirstParameter();
499 aTl=aCT3D->LastParameter();
501 GeomAdaptor_Curve aGAC(aCT3D);
502 GeomAbs_CurveType aCType=aGAC.GetType();
504 if (aCType==GeomAbs_Parabola) {
505 Handle(Geom_Curve) aC3DBase=aCT3D->BasisCurve();
506 ParabolaTolerance(aC3DBase, aTf, aTl, aTolBase, aTolMin, aTolMax);
513 #include <Geom_Parabola.hxx>
514 #include <gp_Parab.hxx>
515 #include <BndLib_Add3dCurve.hxx>
516 #include <BRepLib_CheckCurveOnSurface.hxx>
517 //=======================================================================
518 //function : ParabolaTolerance
520 //=======================================================================
521 void ParabolaTolerance(const Handle(Geom_Curve)& aC3D,
522 const Standard_Real aTf,
523 const Standard_Real aTl,
524 const Standard_Real aTol,
525 Standard_Real& aTolMin,
526 Standard_Real& aTolMax)
532 Handle(Geom_Parabola) aGP=Handle(Geom_Parabola)::DownCast(aC3D);
537 Standard_Integer aNbPoints;
538 Standard_Real aFocal, aX1, aX2, aTol1, aTol2;
540 gp_Parab aParab=aGP->Parab();
541 gp_Ax1 aXAxis=aParab.XAxis();
542 Handle(Geom_Line) aGAxis=new Geom_Line(aXAxis);
553 GeomAPI_ProjectPointOnCurve aProj1(aPf, aGAxis);
554 aNbPoints=aProj1.NbPoints();
556 aX1=aProj1.LowerDistanceParameter();
559 aTol1=aTol*sqrt(0.5*aX1/aFocal);
569 GeomAPI_ProjectPointOnCurve aProj2(aPl, aGAxis);
570 aNbPoints=aProj2.NbPoints();
572 aX2=aProj2.LowerDistanceParameter();
576 aTol2=aTol*sqrt(0.5*aX2/aFocal);
582 aTolMax=(aTol1>aTol2) ? aTol1 : aTol2;
583 aTolMin=(aTol1<aTol2) ? aTol1 : aTol2;
585 /////////////////////////////////////////////////////////////////////////
586 //=======================================================================
587 //function : CheckCurve
589 //=======================================================================
590 Standard_Boolean IntTools_Tools::CheckCurve(const Handle (Geom_Curve)& aC3D,
591 const Standard_Real aTolR3D,
594 Standard_Boolean bRet;
595 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax, dX, dY, dZ;
596 Standard_Real dS, aTol;
597 GeomAdaptor_Curve aGAC;
600 BndLib_Add3dCurve::Add(aGAC, aTolR3D, aBox);
602 aBox.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
608 bRet=(dX>aTol || dY>aTol || dZ>aTol);
612 //=======================================================================
613 //function : IsOnPave
615 //=======================================================================
616 Standard_Boolean IntTools_Tools::IsOnPave(const Standard_Real aT1,
617 const IntTools_Range& aRange,
618 const Standard_Real aTolerance)
620 Standard_Boolean firstisonpave1, firstisonpave2, bIsOnPave;
622 firstisonpave1 = (Abs(aRange.First() - aT1) < aTolerance);
623 firstisonpave2 = (Abs(aRange.Last() - aT1) < aTolerance);
624 bIsOnPave=(firstisonpave1 || firstisonpave2);
627 //=======================================================================
628 // function: VertexParameters
630 //=======================================================================
631 void IntTools_Tools::VertexParameters(const IntTools_CommonPrt& aCPart,
635 const IntTools_Range& aR1=aCPart.Range1();
636 aT1=0.5*(aR1.First()+aR1.Last());
638 if((aCPart.VertexParameter1() >= aR1.First()) &&
639 (aCPart.VertexParameter1() <= aR1.Last())) {
640 aT1 = aCPart.VertexParameter1();
643 const IntTools_SequenceOfRanges& aRanges2=aCPart.Ranges2();
644 const IntTools_Range& aR2=aRanges2(1);
645 aT2=0.5*(aR2.First()+aR2.Last());
647 if((aCPart.VertexParameter2() >= aR2.First()) &&
648 (aCPart.VertexParameter2() <= aR2.Last())) {
649 aT2 = aCPart.VertexParameter2();
652 //=======================================================================
653 // function: VertexParameter
655 //=======================================================================
656 void IntTools_Tools::VertexParameter(const IntTools_CommonPrt& aCPart,
659 const IntTools_Range& aR=aCPart.Range1();
660 aT=0.5*(aR.First()+aR.Last());
661 if((aCPart.VertexParameter1() >= aR.First()) &&
662 (aCPart.VertexParameter1() <= aR.Last())) {
663 aT = aCPart.VertexParameter1();
666 //=======================================================================
667 // function: IsOnPave1
669 //=======================================================================
670 Standard_Boolean IntTools_Tools::IsOnPave1(const Standard_Real aTR,
671 const IntTools_Range& aCPRange,
672 const Standard_Real aTolerance)
674 Standard_Boolean bIsOnPave;
675 Standard_Real aT1, aT2, dT1, dT2;
677 aT1=aCPRange.First();
679 bIsOnPave=(aTR>=aT1 && aTR<=aT1);
686 bIsOnPave=(dT1<=aTolerance || dT2<=aTolerance);
689 //=======================================================================
690 // function: IsInRange
692 //=======================================================================
693 Standard_Boolean IntTools_Tools::IsInRange(const IntTools_Range& aRRef,
694 const IntTools_Range& aR,
695 const Standard_Real aTolerance)
697 Standard_Boolean bIsIn;
698 Standard_Real aT1, aT2, aTRef1, aTRef2;
701 aRRef.Range(aTRef1, aTRef2);
706 bIsIn = (aT1>=aTRef1 && aT1<=aTRef2) ||
707 (aT2>=aTRef1 && aT2<=aTRef2);
711 //=======================================================================
714 //=======================================================================
715 Standard_Integer IntTools_Tools::SegPln(const gp_Lin& theLin,
716 const Standard_Real theTLin1,
717 const Standard_Real theTLin2,
718 const Standard_Real theTolLin,
719 const gp_Pln& thePln,
720 const Standard_Real theTolPln,
722 Standard_Real& theTP,
723 Standard_Real& theTolP,
724 Standard_Real& theTPmin,
725 Standard_Real& theTPmax)
727 Standard_Integer iRet;
728 Standard_Real aTol, aA, aB, aC, aD, aE, aH, aTP, aDist1, aDist2;
732 aTol=theTolLin+theTolPln;
734 const gp_Ax3& aPosPln=thePln.Position();
735 const gp_Dir& aDirPln=aPosPln.Direction();
736 const gp_Pnt& aLocPln=aPosPln.Location();
738 const gp_Dir& aDirLin=theLin.Direction();
739 const gp_Pnt& aLocLin=theLin.Location();
741 aP1.SetXYZ(aLocLin.XYZ()+theTLin1*aDirLin.XYZ());
742 aDist1=aDirPln.X()*(aP1.X()-aLocPln.X())+
743 aDirPln.Y()*(aP1.Y()-aLocPln.Y())+
744 aDirPln.Z()*(aP1.Z()-aLocPln.Z());
746 aP2.SetXYZ(aLocLin.XYZ()+theTLin2*aDirLin.XYZ());
747 aDist2=aDirPln.X()*(aP2.X()-aLocPln.X())+
748 aDirPln.Y()*(aP2.Y()-aLocPln.Y())+
749 aDirPln.Z()*(aP2.Z()-aLocPln.Z());
751 if (aDist1<aTol && aDist2<aTol){
752 iRet=1; // common block
756 if (aDist1*aDist2 > 0.) {
757 iRet=2; // segment lays on one side to the Plane
761 thePln.Coefficients(aA, aB, aC, aD);
762 aE=aA*aLocLin.X()+aB*aLocLin.Y()+aC*aLocLin.Z()+aD;
763 aH=aA*aDirLin.X()+aB*aDirLin.Y()+aC*aDirLin.Z();
765 if (aTP < theTLin1-aTol || aTP > theTLin2+aTol) {
766 iRet=3; // no intersections due to range of the Line
771 theP.SetXYZ(aLocLin.XYZ()+aTP*aDirLin.XYZ());
773 theTPmin=theTP-theTolPln;
774 theTPmax=theTP+theTolPln;
775 iRet=0; // intersection point
779 //=======================================================================
780 // Function : ComputeTolerance
782 //=======================================================================
783 Standard_Boolean IntTools_Tools::ComputeTolerance
784 (const Handle(Geom_Curve)& theCurve3D,
785 const Handle(Geom2d_Curve)& theCurve2D,
786 const Handle(Geom_Surface)& theSurf,
787 const Standard_Real theFirst,
788 const Standard_Real theLast,
789 Standard_Real& theMaxDist,
790 Standard_Real& theMaxPar)
792 GeomLib_CheckCurveOnSurface aCS;
794 aCS.Init(theCurve3D, theSurf, theFirst, theLast);
795 aCS.Perform(theCurve2D);
797 return Standard_False;
800 theMaxDist = aCS.MaxDistance();
801 theMaxPar = aCS.MaxParameter();
803 return Standard_True;