1 // Created by: Peter KURNEV
2 // Copyright (c) 2010-2014 OPEN CASCADE SAS
3 // Copyright (c) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
4 // Copyright (c) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, CEDRAT,
5 // EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 // This file is part of Open CASCADE Technology software library.
9 // This library is free software; you can redistribute it and/or modify it under
10 // the terms of the GNU Lesser General Public License version 2.1 as published
11 // by the Free Software Foundation, with special exception defined in the file
12 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
13 // distribution for complete text of the license and disclaimer of any warranty.
15 // Alternatively, this file may be used under the terms of Open CASCADE
16 // commercial license or contractual agreement.
18 #include <BOPTools_AlgoTools.ixx>
20 #include <Precision.hxx>
24 #include <gp_Pnt2d.hxx>
25 #include <gp_Cylinder.hxx>
26 #include <gp_Cone.hxx>
27 #include <gp_Sphere.hxx>
28 #include <gp_Torus.hxx>
31 #include <Geom2d_Curve.hxx>
32 #include <Geom_Surface.hxx>
33 #include <Geom_Plane.hxx>
34 #include <Geom_TrimmedCurve.hxx>
35 #include <Geom_Curve.hxx>
36 #include <GeomAPI_ProjectPointOnSurf.hxx>
37 #include <Geom2dInt_Geom2dCurveTool.hxx>
39 #include <TopAbs_Orientation.hxx>
41 #include <TopoDS_Compound.hxx>
42 #include <TopoDS_CompSolid.hxx>
43 #include <TopoDS_Solid.hxx>
44 #include <TopoDS_Shell.hxx>
45 #include <TopoDS_Wire.hxx>
47 #include <BRep_Builder.hxx>
48 #include <BRep_Tool.hxx>
49 #include <BRepLib.hxx>
50 #include <BRepAdaptor_Curve2d.hxx>
51 #include <BRepAdaptor_Surface.hxx>
52 #include <BRepClass3d_SolidClassifier.hxx>
54 #include <TopExp_Explorer.hxx>
56 #include <IntTools_Tools.hxx>
58 #include <BOPTools.hxx>
59 #include <BOPTools_CoupleOfShape.hxx>
60 #include <BOPTools_ListOfCoupleOfShape.hxx>
61 #include <BOPTools_AlgoTools2D.hxx>
62 #include <BOPTools_AlgoTools3D.hxx>
64 #include <BOPCol_IndexedMapOfShape.hxx>
65 #include <BOPCol_MapOfShape.hxx>
67 #include <IntTools_ShrunkRange.hxx>
68 #include <Precision.hxx>
72 Standard_Real AngleWithRef(const gp_Dir& theD1,
74 const gp_Dir& theDRef);
77 Standard_Boolean FindFacePairs (const TopoDS_Edge& theE,
78 const BOPCol_ListOfShape& thLF,
79 BOPTools_ListOfCoupleOfShape& theLCFF,
80 Handle(IntTools_Context)& theContext);
82 TopAbs_Orientation Orientation(const TopoDS_Edge& anE,
83 const TopoDS_Face& aF);
86 void GetFaceDir(const TopoDS_Edge& aE,
87 const TopoDS_Face& aF,
89 const Standard_Real aT,
93 Handle(IntTools_Context)& theContext,
94 GeomAPI_ProjectPointOnSurf& aProjPL,
95 const Standard_Real aDt);
97 Standard_Boolean FindPointInFace(const TopoDS_Face& aF,
101 Handle(IntTools_Context)& theContext,
102 GeomAPI_ProjectPointOnSurf& aProjPL,
103 const Standard_Real aDt,
104 const Standard_Real aTolE);
106 Standard_Real MinStep3D(const TopoDS_Edge& theE1,
107 const TopoDS_Face& theF1,
108 const BOPTools_ListOfCoupleOfShape& theLCS,
111 //=======================================================================
112 // function: MakeConnexityBlocks
114 //=======================================================================
115 void BOPTools_AlgoTools::MakeConnexityBlocks
116 (const TopoDS_Shape& theS,
117 const TopAbs_ShapeEnum theType1,
118 const TopAbs_ShapeEnum theType2,
119 BOPCol_ListOfShape& theLCB)
121 Standard_Integer aNbF, aNbAdd, aNbAdd1, i;
125 TopExp_Explorer aExp;
126 BOPCol_MapOfShape aMP;
127 BOPCol_IndexedMapOfShape aMCB, aMAdd, aMAdd1;
128 BOPCol_IndexedDataMapOfShapeListOfShape aMEF;
129 BOPCol_ListIteratorOfListOfShape aItLF;
132 BOPTools::MapShapesAndAncestors(theS, theType1, theType2, aMEF);
135 aIt.Initialize(theS);
136 for (; aIt.More(); aIt.Next()) {
137 const TopoDS_Shape& aF1=aIt.Value();
138 if (aMP.Contains(aF1)) {
149 aNbAdd = aMAdd.Extent();
150 for (i=1; i<=aNbAdd; ++i) {
151 const TopoDS_Shape& aF=aMAdd(i);
153 aExp.Init(aF, theType1);
154 for (; aExp.More(); aExp.Next()) {
155 const TopoDS_Shape& aE=aExp.Current();
157 const BOPCol_ListOfShape& aLF=aMEF.FindFromKey(aE);
158 aItLF.Initialize(aLF);
159 for (; aItLF.More(); aItLF.Next()) {
160 const TopoDS_Shape& aFx=aItLF.Value();
161 if (aFx.IsSame(aF)) {
164 if (aMCB.Contains(aFx)) {
169 }//for (; aExp.More(); aExp.Next()){
171 }// for (i=1; i<=aNbAdd; ++i) {
173 aNbAdd1=aMAdd1.Extent();
175 break;// ->make new CB from aMCB
179 for (i=1; i<=aNbAdd1; ++i) {
180 const TopoDS_Shape& aFAdd = aMAdd1(i);
187 aBB.MakeCompound(aC);
189 for (i=1; i<=aNbF; ++i) {
190 const TopoDS_Shape& aF=aMCB(i);
196 }// for (; aIt.More(); aIt.Next())
198 //=======================================================================
199 // function: OrientFacesOnShell
201 //=======================================================================
202 void BOPTools_AlgoTools::OrientFacesOnShell (TopoDS_Shape& aShell)
204 Standard_Boolean bIsProcessed1, bIsProcessed2;
205 Standard_Integer i, aNbE, aNbF, j;
206 TopAbs_Orientation anOrE1, anOrE2;
207 TopoDS_Face aF1x, aF2x;
208 TopoDS_Shape aShellNew;
209 BOPCol_IndexedDataMapOfShapeListOfShape aEFMap;
210 BOPCol_IndexedMapOfShape aProcessedFaces;
213 BOPTools_AlgoTools::MakeContainer(TopAbs_SHELL, aShellNew);
215 BOPTools::MapShapesAndAncestors(aShell,
216 TopAbs_EDGE, TopAbs_FACE,
218 aNbE=aEFMap.Extent();
220 // One seam edge in aEFMap contains 2 equivalent faces.
221 for (i=1; i<=aNbE; ++i) {
222 BOPCol_ListOfShape& aLF=aEFMap.ChangeFromIndex(i);
225 BOPCol_ListOfShape aLFTmp;
226 BOPCol_IndexedMapOfShape aFM;
228 BOPCol_ListIteratorOfListOfShape anIt(aLF);
229 for (; anIt.More(); anIt.Next()) {
230 const TopoDS_Shape& aF=anIt.Value();
231 if (!aFM.Contains(aF)) {
242 for (i=1; i<=aNbE; ++i) {
243 const TopoDS_Edge& aE=(*(TopoDS_Edge*)(&aEFMap.FindKey(i)));
244 if (BRep_Tool::Degenerated(aE)) {
248 const BOPCol_ListOfShape& aLF=aEFMap.FindFromIndex(i);
254 TopoDS_Face& aF1=(*(TopoDS_Face*)(&aLF.First()));
255 TopoDS_Face& aF2=(*(TopoDS_Face*)(&aLF.Last()));
257 bIsProcessed1=aProcessedFaces.Contains(aF1);
258 bIsProcessed2=aProcessedFaces.Contains(aF2);
259 if (bIsProcessed1 && bIsProcessed2) {
263 if (!bIsProcessed1 && !bIsProcessed2) {
264 aProcessedFaces.Add(aF1);
265 aBB.Add(aShellNew, aF1);
266 bIsProcessed1=!bIsProcessed1;
271 j=aProcessedFaces.FindIndex(aF1);
272 aF1x=(*(TopoDS_Face*)(&aProcessedFaces.FindKey(j)));
277 j=aProcessedFaces.FindIndex(aF2);
278 aF2x=(*(TopoDS_Face*)(&aProcessedFaces.FindKey(j)));
281 anOrE1=Orientation(aE, aF1x);
282 anOrE2=Orientation(aE, aF2x);
284 if (bIsProcessed1 && !bIsProcessed2) {
285 if (anOrE1==anOrE2) {
286 if (!BRep_Tool::IsClosed(aE, aF1) &&
287 !BRep_Tool::IsClosed(aE, aF2)) {
291 aProcessedFaces.Add(aF2);
292 aBB.Add(aShellNew, aF2);
294 else if (!bIsProcessed1 && bIsProcessed2) {
295 if (anOrE1==anOrE2) {
296 if (!BRep_Tool::IsClosed(aE, aF1) &&
297 !BRep_Tool::IsClosed(aE, aF2)) {
301 aProcessedFaces.Add(aF1);
302 aBB.Add(aShellNew, aF1);
307 for (i=1; i<=aNbE; ++i) {
308 const TopoDS_Edge& aE=(*(TopoDS_Edge*)(&aEFMap.FindKey(i)));
309 if (BRep_Tool::Degenerated(aE)) {
313 const BOPCol_ListOfShape& aLF=aEFMap.FindFromIndex(i);
316 BOPCol_ListIteratorOfListOfShape anIt(aLF);
317 for(; anIt.More(); anIt.Next()) {
318 const TopoDS_Face& aF=(*(TopoDS_Face*)(&anIt.Value()));
319 if (!aProcessedFaces.Contains(aF)) {
320 aProcessedFaces.Add(aF);
321 aBB.Add(aShellNew, aF);
328 //=======================================================================
329 //function : Orientation
331 //=======================================================================
332 TopAbs_Orientation Orientation(const TopoDS_Edge& anE,
333 const TopoDS_Face& aF)
335 TopAbs_Orientation anOr=TopAbs_INTERNAL;
337 TopExp_Explorer anExp;
338 anExp.Init(aF, TopAbs_EDGE);
339 for (; anExp.More(); anExp.Next()) {
340 const TopoDS_Edge& anEF1=(*(TopoDS_Edge*)(&anExp.Current()));
341 if (anEF1.IsSame(anE)) {
342 anOr=anEF1.Orientation();
348 //=======================================================================
349 // function: MakeConnexityBlock.
351 //=======================================================================
352 void BOPTools_AlgoTools::MakeConnexityBlock
353 (BOPCol_ListOfShape& theLFIn,
354 BOPCol_IndexedMapOfShape& theMEAvoid,
355 BOPCol_ListOfShape& theLCB,
356 const Handle(NCollection_BaseAllocator)& theAllocator)
358 Standard_Integer aNbF, aNbAdd1, aNbAdd, i;
359 TopExp_Explorer aExp;
360 BOPCol_ListIteratorOfListOfShape aIt;
362 BOPCol_IndexedMapOfShape aMCB(100, theAllocator);
363 BOPCol_IndexedMapOfShape aMAdd(100, theAllocator);
364 BOPCol_IndexedMapOfShape aMAdd1(100, theAllocator);
365 BOPCol_IndexedDataMapOfShapeListOfShape aMEF(100, theAllocator);
368 aNbF=theLFIn.Extent();
369 aIt.Initialize(theLFIn);
370 for (; aIt.More(); aIt.Next()) {
371 const TopoDS_Shape& aF=aIt.Value();
372 BOPTools::MapShapesAndAncestors(aF, TopAbs_EDGE, TopAbs_FACE, aMEF);
376 const TopoDS_Shape& aF1=theLFIn.First();
381 aNbAdd = aMAdd.Extent();
382 for (i=1; i<=aNbAdd; ++i) {
383 const TopoDS_Shape& aF=aMAdd(i);
386 aExp.Init(aF, TopAbs_EDGE);
387 for (; aExp.More(); aExp.Next()) {
388 const TopoDS_Shape& aE=aExp.Current();
389 if (theMEAvoid.Contains(aE)){
393 const BOPCol_ListOfShape& aLF=aMEF.FindFromKey(aE);
395 for (; aIt.More(); aIt.Next()) {
396 const TopoDS_Shape& aFx=aIt.Value();
397 if (aFx.IsSame(aF)) {
400 if (aMCB.Contains(aFx)) {
405 }//for (; aExp.More(); aExp.Next()){
407 }// for (i=1; i<=aNbAdd; ++i) {
409 aNbAdd1=aMAdd1.Extent();
415 for (i=1; i<=aNbAdd1; ++i) {
416 const TopoDS_Shape& aFAdd=aMAdd1(i);
424 for (i=1; i<=aNbF; ++i) {
425 const TopoDS_Shape& aF=aMCB(i);
429 //=======================================================================
430 // function: ComputeStateByOnePoint
432 //=======================================================================
433 TopAbs_State BOPTools_AlgoTools::ComputeStateByOnePoint
434 (const TopoDS_Shape& theS,
435 const TopoDS_Solid& theRef,
436 const Standard_Real theTol,
437 Handle(IntTools_Context)& theContext)
440 TopAbs_ShapeEnum aType;
442 aState=TopAbs_UNKNOWN;
443 aType=theS.ShapeType();
444 if (aType==TopAbs_VERTEX) {
445 const TopoDS_Vertex& aV=(*(TopoDS_Vertex*)(&theS));
446 aState=BOPTools_AlgoTools::ComputeState(aV, theRef, theTol, theContext);
448 else if (aType==TopAbs_EDGE) {
449 const TopoDS_Edge& aE=(*(TopoDS_Edge*)(&theS));
450 aState=BOPTools_AlgoTools::ComputeState(aE, theRef, theTol, theContext);
455 //=======================================================================
456 // function: ComputeState
458 //=======================================================================
459 TopAbs_State BOPTools_AlgoTools::ComputeState
460 (const TopoDS_Face& theF,
461 const TopoDS_Solid& theRef,
462 const Standard_Real theTol,
463 BOPCol_IndexedMapOfShape& theBounds,
464 Handle(IntTools_Context)& theContext)
467 TopExp_Explorer aExp;
472 aState=TopAbs_UNKNOWN;
474 aExp.Init(theF, TopAbs_EDGE);
475 for (; aExp.More(); aExp.Next()) {
476 const TopoDS_Edge& aSE=(*(TopoDS_Edge*)(&aExp.Current()));
477 if (BRep_Tool::Degenerated(aSE)) {
481 if (!theBounds.Contains(aSE)) {
482 const TopoDS_Edge& aE=(*(TopoDS_Edge*)(&aSE));
483 aState=BOPTools_AlgoTools::ComputeState(aE, theRef, theTol,
488 aE1=(*(TopoDS_Edge*)(&aSE));
491 // !!<- process edges that are all on theRef
493 BOPTools_AlgoTools3D::PointNearEdge(aE1, theF,
494 aP2D, aP3D, theContext);
495 aState=BOPTools_AlgoTools::ComputeState(aP3D, theRef, theTol,
501 //=======================================================================
502 // function: ComputeState
504 //=======================================================================
505 TopAbs_State BOPTools_AlgoTools::ComputeState
506 (const TopoDS_Vertex& theV,
507 const TopoDS_Solid& theRef,
508 const Standard_Real theTol,
509 Handle(IntTools_Context)& theContext)
514 aP3D=BRep_Tool::Pnt(theV);
515 aState=BOPTools_AlgoTools::ComputeState(aP3D, theRef, theTol,
519 //=======================================================================
520 // function: ComputeState
522 //=======================================================================
523 TopAbs_State BOPTools_AlgoTools::ComputeState
524 (const TopoDS_Edge& theE,
525 const TopoDS_Solid& theRef,
526 const Standard_Real theTol,
527 Handle(IntTools_Context)& theContext)
529 Standard_Real aT1, aT2, aT = 0.;
531 Handle(Geom_Curve) aC3D;
534 aC3D = BRep_Tool::Curve(theE, aT1, aT2);
537 //it means that we are in degenerated edge
538 const TopoDS_Vertex& aV = TopExp::FirstVertex(theE);
540 return TopAbs_UNKNOWN;
542 aP3D=BRep_Tool::Pnt(aV);
545 Standard_Boolean bF2Inf, bL2Inf;
546 Standard_Real dT=10.;
548 bF2Inf = Precision::IsNegativeInfinite(aT1);
549 bL2Inf = Precision::IsPositiveInfinite(aT2);
551 if (bF2Inf && !bL2Inf) {
554 else if (!bF2Inf && bL2Inf) {
557 else if (bF2Inf && bL2Inf) {
561 aT=IntTools_Tools::IntermediatePoint(aT1, aT2);
566 aState=BOPTools_AlgoTools::ComputeState(aP3D, theRef, theTol,
571 //=======================================================================
572 // function: ComputeState
574 //=======================================================================
575 TopAbs_State BOPTools_AlgoTools::ComputeState
577 const TopoDS_Solid& theRef,
578 const Standard_Real theTol,
579 Handle(IntTools_Context)& theContext)
583 BRepClass3d_SolidClassifier& aSC=theContext->SolidClassifier(theRef);
584 aSC.Perform(theP, theTol);
590 //=======================================================================
591 //function : IsInternalFace
593 //=======================================================================
594 Standard_Integer BOPTools_AlgoTools::IsInternalFace
595 (const TopoDS_Face& theFace,
596 const TopoDS_Solid& theSolid,
597 BOPCol_IndexedDataMapOfShapeListOfShape& theMEF,
598 const Standard_Real theTol,
599 Handle(IntTools_Context)& theContext)
601 Standard_Boolean bDegenerated;
602 Standard_Integer aNbF, iRet, iFound;
603 TopAbs_Orientation aOr;
605 TopExp_Explorer aExp;
606 BOPCol_ListIteratorOfListOfShape aItF;
608 // For all invoked functions: [::IsInternalFace(...)]
609 // the returned value iRet means:
610 // iRet=0; - state is not IN
611 // iRet=1; - state is IN
612 // iRet=2; - state can not be found by the method of angles
614 // For this function the returned value iRet means:
615 // iRet=0; - state is not IN
616 // iRet=1; - state is IN
619 // 1 Try to find an edge from theFace in theMEF
621 aExp.Init(theFace, TopAbs_EDGE);
622 for(; aExp.More(); aExp.Next()) {
623 const TopoDS_Edge& aE=(*(TopoDS_Edge*)(&aExp.Current()));
624 if (!theMEF.Contains(aE)) {
630 aOr=aE.Orientation();
631 if (aOr==TopAbs_INTERNAL) {
634 bDegenerated=BRep_Tool::Degenerated(aE);
639 BOPCol_ListOfShape& aLF=theMEF.ChangeFromKey(aE);
642 return iRet; // it can not be so
646 // aE is internal edge on aLF.First()
647 const TopoDS_Face& aF1=(*(TopoDS_Face*)(&aLF.First()));
648 BOPTools_AlgoTools::GetEdgeOnFace(aE, aF1, aE1);
649 if (aE1.Orientation()!=TopAbs_INTERNAL) {
654 iRet=BOPTools_AlgoTools::IsInternalFace(theFace, aE, aF1, aF1,
660 const TopoDS_Face& aF1=(*(TopoDS_Face*)(&aLF.First()));
661 const TopoDS_Face& aF2=(*(TopoDS_Face*)(&aLF.Last()));
663 if (aF2.IsSame(aF1) && BRep_Tool::IsClosed(aE, aF1)) {
664 // treat as it was for 1 face
665 iRet=BOPTools_AlgoTools::IsInternalFace(theFace, aE, aF1, aF2,
673 return iRet; // it can not be so
675 else { // aNbF=2,4,6,8,...
676 iRet=BOPTools_AlgoTools::IsInternalFace(theFace, aE, aLF,
680 }//for(; aExp.More(); aExp.Next()) {
683 // the face has no shared edges with the solid
691 //========================================
692 // 2. Classify face using classifier
695 BOPCol_IndexedMapOfShape aBounds;
697 BOPTools::MapShapes(theSolid, TopAbs_EDGE, aBounds);
699 aState=BOPTools_AlgoTools::ComputeState(theFace, theSolid,
700 theTol, aBounds, theContext);
702 iRet=(aState==TopAbs_IN)? 1 : 0;
706 //=======================================================================
707 //function : IsInternalFace
709 //=======================================================================
710 Standard_Integer BOPTools_AlgoTools::IsInternalFace
711 (const TopoDS_Face& theFace,
712 const TopoDS_Edge& theEdge,
713 BOPCol_ListOfShape& theLF,
714 Handle(IntTools_Context)& theContext)
716 Standard_Integer aNbF, iRet;
722 const TopoDS_Face& aF1=(*(TopoDS_Face*)(&theLF.First()));
723 const TopoDS_Face& aF2=(*(TopoDS_Face*)(&theLF.Last()));
724 iRet=BOPTools_AlgoTools::IsInternalFace(theFace, theEdge, aF1, aF2,
730 BOPTools_ListOfCoupleOfShape aLCFF;
731 BOPTools_ListIteratorOfListOfCoupleOfShape aIt;
733 FindFacePairs(theEdge, theLF, aLCFF, theContext);
735 aIt.Initialize(aLCFF);
736 for (; aIt.More(); aIt.Next()) {
737 BOPTools_CoupleOfShape& aCSFF=aIt.ChangeValue();
739 const TopoDS_Face& aF1=(*(TopoDS_Face*)(&aCSFF.Shape1()));
740 const TopoDS_Face& aF2=(*(TopoDS_Face*)(&aCSFF.Shape2()));
741 iRet=BOPTools_AlgoTools::IsInternalFace(theFace, theEdge, aF1, aF2,
750 //=======================================================================
751 //function : IsInternalFace
753 //=======================================================================
754 Standard_Integer BOPTools_AlgoTools::IsInternalFace
755 (const TopoDS_Face& theFace,
756 const TopoDS_Edge& theEdge,
757 const TopoDS_Face& theFace1,
758 const TopoDS_Face& theFace2,
759 Handle(IntTools_Context)& theContext)
761 Standard_Boolean bRet;
762 Standard_Integer iRet;
763 TopoDS_Edge aE1, aE2;
765 BOPTools_ListOfCoupleOfShape theLCSOff;
766 BOPTools_CoupleOfShape aCS1, aCS2;
768 BOPTools_AlgoTools::GetEdgeOnFace(theEdge, theFace1, aE1);
769 if (aE1.Orientation()==TopAbs_INTERNAL) {
771 aE1.Orientation(TopAbs_FORWARD);
772 aE2.Orientation(TopAbs_REVERSED);
774 else if (theFace1==theFace2) {
776 aE1.Orientation(TopAbs_FORWARD);
777 aE2.Orientation(TopAbs_REVERSED);
780 BOPTools_AlgoTools::GetEdgeOnFace(theEdge, theFace2, aE2);
783 aCS1.SetShape1(theEdge);
784 aCS1.SetShape2(theFace);
785 theLCSOff.Append(aCS1);
788 aCS2.SetShape2(theFace2);
789 theLCSOff.Append(aCS2);
791 bRet=GetFaceOff(aE1, theFace1, theLCSOff, aFOff, theContext);
793 iRet=0; // theFace is not internal
794 if (theFace.IsEqual(aFOff)) {
795 // theFace is internal
798 // theFace seems to be internal
804 //=======================================================================
805 //function : GetFaceOff
807 //=======================================================================
808 Standard_Boolean BOPTools_AlgoTools::GetFaceOff
809 (const TopoDS_Edge& theE1,
810 const TopoDS_Face& theF1,
811 BOPTools_ListOfCoupleOfShape& theLCSOff,
812 TopoDS_Face& theFOff,
813 Handle(IntTools_Context)& theContext)
815 Standard_Boolean bRet;
816 Standard_Real aT, aT1, aT2, aAngle, aTwoPI, aAngleMin, aDt3D;
817 Standard_Real aUmin, aUsup, aVmin, aVsup, aPA;
818 gp_Pnt aPn1, aPn2, aPx;
819 gp_Dir aDN1, aDN2, aDBF, aDBF2, aDTF;
821 TopAbs_Orientation aOr;
822 Handle(Geom_Curve)aC3D;
823 Handle(Geom_Plane) aPL;
824 BOPTools_ListIteratorOfListOfCoupleOfShape aIt;
825 GeomAPI_ProjectPointOnSurf aProjPL;
827 aPA=Precision::Angular();
830 aC3D =BRep_Tool::Curve(theE1, aT1, aT2);
831 aT=BOPTools_AlgoTools2D::IntermediatePoint(aT1, aT2);
834 BOPTools_AlgoTools2D::EdgeTangent(theE1, aT, aVTgt);
835 gp_Dir aDTgt(aVTgt), aDTgt2;
836 aOr = theE1.Orientation();
838 aPL = new Geom_Plane(aPx, aDTgt);
839 aPL->Bounds(aUmin, aUsup, aVmin, aVsup);
840 aProjPL.Init(aPL, aUmin, aUsup, aVmin, aVsup);
842 aDt3D = MinStep3D(theE1, theF1, theLCSOff, aPx);
843 GetFaceDir(theE1, theF1, aPx, aT, aDTgt, aDN1, aDBF, theContext,
849 aIt.Initialize(theLCSOff);
850 for (; aIt.More(); aIt.Next()) {
851 const BOPTools_CoupleOfShape& aCS=aIt.Value();
852 const TopoDS_Edge& aE2=(*(TopoDS_Edge*)(&aCS.Shape1()));
853 const TopoDS_Face& aF2=(*(TopoDS_Face*)(&aCS.Shape2()));
855 aDTgt2 = (aE2.Orientation()==aOr) ? aDTgt : aDTgt.Reversed();
856 GetFaceDir(aE2, aF2, aPx, aT, aDTgt2, aDN2, aDBF2, theContext,
859 aAngle=AngleWithRef(aDBF, aDBF2, aDTF);
862 aAngle=aTwoPI+aAngle;
869 else if (aF2.IsSame(theF1)) {
874 if (fabs(aAngle-aAngleMin)<aPA) {
875 // the minimal angle can not be found
879 if (aAngle<aAngleMin){
883 else if (aAngle==aAngleMin) {
884 // the minimal angle can not be found
890 //=======================================================================
891 //function : GetEdgeOff
893 //=======================================================================
894 Standard_Boolean BOPTools_AlgoTools::GetEdgeOff(const TopoDS_Edge& theE1,
895 const TopoDS_Face& theF2,
898 Standard_Boolean bFound;
899 TopAbs_Orientation aOr1, aOr1C, aOr2;
900 TopExp_Explorer anExp;
902 bFound=Standard_False;
903 aOr1=theE1.Orientation();
904 aOr1C=TopAbs::Reverse(aOr1);
906 anExp.Init(theF2, TopAbs_EDGE);
907 for (; anExp.More(); anExp.Next()) {
908 const TopoDS_Edge& aEF2=(*(TopoDS_Edge*)(&anExp.Current()));
909 if (aEF2.IsSame(theE1)) {
910 aOr2=aEF2.Orientation();
921 //=======================================================================
922 //function : AreFacesSameDomain
924 //=======================================================================
925 Standard_Boolean BOPTools_AlgoTools::AreFacesSameDomain
926 (const TopoDS_Face& theF1,
927 const TopoDS_Face& theF2,
928 Handle(IntTools_Context)& theContext)
930 Standard_Boolean bFlag;
931 Standard_Integer iErr;
932 Standard_Real aTolF1, aTolF2, aTol;
935 TopoDS_Face aF1, aF2;
937 TopExp_Explorer aExp;
939 bFlag=Standard_False;
942 aF1.Orientation(TopAbs_FORWARD);
944 aF2.Orientation(TopAbs_FORWARD);
946 aTolF1=BRep_Tool::Tolerance(aF1);
948 aExp.Init(aF1, TopAbs_EDGE);
949 for (; aExp.More(); aExp.Next()) {
950 aE1=(*(TopoDS_Edge*)(&aExp.Current()));
951 if (!BRep_Tool::Degenerated(aE1)) {
952 Standard_Real aTolE = BRep_Tool::Tolerance(aE1);
953 aTolF1 = (aTolE > aTolF1) ? aTolE : aTolF1;
957 aTolF2=BRep_Tool::Tolerance(aF2);
960 iErr = BOPTools_AlgoTools3D::PointInFace(aF1, aP, aP2D,
963 bFlag=theContext->IsValidPointForFace(aP, aF2, aTol);
969 //=======================================================================
970 //function : CheckSameGeom
972 //=======================================================================
973 Standard_Boolean BOPTools_AlgoTools::CheckSameGeom
974 (const TopoDS_Face& theF1,
975 const TopoDS_Face& theF2,
976 Handle(IntTools_Context)& theContext)
978 Standard_Boolean bRet;
979 Standard_Real aTolF1, aTolF2, aTol;
982 TopExp_Explorer aExp;
985 aExp.Init(theF1, TopAbs_EDGE);
986 for (; aExp.More(); aExp.Next()) {
987 const TopoDS_Edge& aE=(*(TopoDS_Edge*)(&aExp.Current()));
988 if (!BRep_Tool::Degenerated(aE)) {
989 aTolF1=BRep_Tool::Tolerance(theF1);
990 aTolF2=BRep_Tool::Tolerance(theF2);
992 BOPTools_AlgoTools3D::PointNearEdge(aE, theF1, aP2D, aP, theContext);
993 bRet=theContext->IsValidPointForFace(aP, theF2, aTol);
999 //=======================================================================
1002 //=======================================================================
1003 Standard_Integer BOPTools_AlgoTools::Sense (const TopoDS_Face& theF1,
1004 const TopoDS_Face& theF2)
1006 Standard_Integer iSense=0;
1007 gp_Dir aDNF1, aDNF2;
1008 TopoDS_Edge aE1, aE2;
1009 TopExp_Explorer aExp;
1011 aExp.Init(theF1, TopAbs_EDGE);
1012 for (; aExp.More(); aExp.Next()) {
1013 aE1=(*(TopoDS_Edge*)(&aExp.Current()));
1014 if (!BRep_Tool::Degenerated(aE1)) {
1015 if (!BRep_Tool::IsClosed(aE1, theF1)) {
1021 aExp.Init(theF2, TopAbs_EDGE);
1022 for (; aExp.More(); aExp.Next()) {
1023 aE2=(*(TopoDS_Edge*)(&aExp.Current()));
1024 if (!BRep_Tool::Degenerated(aE2)) {
1025 if (!BRep_Tool::IsClosed(aE2, theF2)) {
1026 if (aE2.IsSame(aE1)) {
1038 BOPTools_AlgoTools3D::GetNormalToFaceOnEdge(aE1, theF1, aDNF1);
1039 BOPTools_AlgoTools3D::GetNormalToFaceOnEdge(aE2, theF2, aDNF2);
1041 iSense=BOPTools_AlgoTools3D::SenseFlag(aDNF1, aDNF2);
1045 //=======================================================================
1046 // function: IsSplitToReverse
1048 //=======================================================================
1049 Standard_Boolean BOPTools_AlgoTools::IsSplitToReverse
1050 (const TopoDS_Shape& theSp,
1051 const TopoDS_Shape& theSr,
1052 Handle(IntTools_Context)& theContext)
1054 Standard_Boolean bRet;
1055 TopAbs_ShapeEnum aType;
1057 bRet=Standard_False;
1059 aType=theSp.ShapeType();
1062 const TopoDS_Edge& aESp=(*(TopoDS_Edge*)(&theSp));
1063 const TopoDS_Edge& aESr=(*(TopoDS_Edge*)(&theSr));
1064 bRet=BOPTools_AlgoTools::IsSplitToReverse(aESp, aESr, theContext);
1069 const TopoDS_Face& aFSp=(*(TopoDS_Face*)(&theSp));
1070 const TopoDS_Face& aFSr=(*(TopoDS_Face*)(&theSr));
1071 bRet=BOPTools_AlgoTools::IsSplitToReverse(aFSp, aFSr, theContext);
1080 //=======================================================================
1081 //function :IsSplitToReverse
1083 //=======================================================================
1084 Standard_Boolean BOPTools_AlgoTools::IsSplitToReverse
1085 (const TopoDS_Face& theFSp,
1086 const TopoDS_Face& theFSr,
1087 Handle(IntTools_Context)& theContext)
1089 Standard_Boolean bRet, bFound, bInFace;
1090 Standard_Real aT1, aT2, aT, aU, aV, aScPr;
1091 gp_Pnt aPFSp, aPFSr;
1094 Handle(Geom_Surface) aSr, aSp;
1095 TopAbs_Orientation aOrSr, aOrSp;
1096 TopExp_Explorer anExp;
1099 bRet=Standard_False;
1101 aSr=BRep_Tool::Surface(theFSr);
1102 aSp=BRep_Tool::Surface(theFSp);
1104 aOrSr=theFSr.Orientation();
1105 aOrSp=theFSp.Orientation();
1106 bRet=(aOrSr!=aOrSp);
1110 bFound=Standard_False;
1111 anExp.Init(theFSp, TopAbs_EDGE);
1112 for (; anExp.More(); anExp.Next()) {
1113 aESp=(*(TopoDS_Edge*)(&anExp.Current()));
1114 if (!BRep_Tool::Degenerated(aESp)) {
1115 if (!BRep_Tool::IsClosed(aESp, theFSp)) {
1122 Standard_Boolean bFlag;
1123 Standard_Integer iErr;
1126 iErr=BOPTools_AlgoTools3D::PointInFace(theFSp, aPFSp, aP2DFSp,
1132 aP2DFSp.Coord(aU, aV);
1133 bFlag=BOPTools_AlgoTools3D::GetNormalToSurface(aSp, aU, aV, aDNFSp);
1138 if (theFSp.Orientation()==TopAbs_REVERSED){
1143 BRep_Tool::Range(aESp, aT1, aT2);
1144 aT=BOPTools_AlgoTools2D::IntermediatePoint(aT1, aT2);
1145 BOPTools_AlgoTools3D::GetApproxNormalToFaceOnEdge(aESp, theFSp, aT,
1150 // Parts of theContext->ComputeVS(..)
1151 GeomAPI_ProjectPointOnSurf& aProjector=theContext->ProjPS(theFSr);
1152 aProjector.Perform(aPFSp);
1153 if (!aProjector.IsDone()) {
1157 aProjector.LowerDistanceParameters(aU, aV);
1158 gp_Pnt2d aP2D(aU, aV);
1159 bInFace=theContext->IsPointInOnFace (theFSr, aP2D);
1164 aSr->D1(aU, aV, aPFSr, aD1U, aD1V);
1167 gp_Dir aDNFSr=aDD1U^aDD1V;
1168 if (theFSr.Orientation()==TopAbs_REVERSED){
1172 aScPr=aDNFSp*aDNFSr;
1177 //=======================================================================
1178 //function :IsSplitToReverse
1180 //=======================================================================
1181 Standard_Boolean BOPTools_AlgoTools::IsSplitToReverse
1182 (const TopoDS_Edge& aEF1,
1183 const TopoDS_Edge& aEF2,
1184 Handle(IntTools_Context)& theContext)
1186 Standard_Boolean bRet, bIsDegenerated;
1188 bRet=Standard_False;
1189 bIsDegenerated=(BRep_Tool::Degenerated(aEF1) ||
1190 BRep_Tool::Degenerated(aEF2));
1191 if (bIsDegenerated) {
1196 TopAbs_Orientation aOrE, aOrSp;
1197 Handle(Geom_Curve)aC1, aC2;
1199 aC2=BRep_Tool::Curve(aEF2, a, b);
1200 aC1=BRep_Tool::Curve(aEF1, a, b);
1203 aOrE=aEF2.Orientation();
1204 aOrSp=aEF1.Orientation();
1209 Standard_Real aT1, aT2, aScPr;
1213 aT1=BOPTools_AlgoTools2D::IntermediatePoint(a, b);
1215 BOPTools_AlgoTools2D::EdgeTangent(aEF1, aT1, aV1);
1218 theContext->ProjectPointOnEdge(aP, aEF2, aT2);
1220 BOPTools_AlgoTools2D::EdgeTangent(aEF2, aT2, aV2);
1229 //=======================================================================
1232 //=======================================================================
1233 Standard_Boolean BOPTools_AlgoTools::IsHole(const TopoDS_Shape& aW,
1234 const TopoDS_Shape& aFace)
1236 Standard_Boolean bIsHole;
1237 Standard_Integer i, aNbS;
1238 Standard_Real aT1, aT2, aS;
1239 Standard_Real aU1, aU, dU;
1240 Standard_Real aX1, aY1, aX0, aY0;
1241 TopAbs_Orientation aOr;
1243 gp_Pnt2d aP2D0, aP2D1;
1244 Handle(Geom2d_Curve) aC2D;
1245 TopoDS_Face aF, aFF;
1246 TopoDS_Iterator aItW;
1248 bIsHole=Standard_False;
1250 aF=(*(TopoDS_Face *)(&aFace));
1252 aFF.Orientation(TopAbs_FORWARD);
1255 aItW.Initialize(aW);
1256 for (; aItW.More(); aItW.Next()) {
1257 const TopoDS_Edge& aE=(*(TopoDS_Edge *)(&aItW.Value()));
1258 aOr=aE.Orientation();
1259 if (!(aOr==TopAbs_FORWARD ||
1260 aOr==TopAbs_REVERSED)) {
1264 aC2D=BRep_Tool::CurveOnSurface(aE, aFF, aT1, aT2);
1265 if (aC2D.IsNull()) {
1269 BRepAdaptor_Curve2d aBAC2D(aE, aFF);
1270 aNbS=Geom2dInt_Geom2dCurveTool::NbSamples(aBAC2D);
1275 dU=(aT2-aT1)/(Standard_Real)(aNbS-1);
1278 if (aOr==TopAbs_REVERSED) {
1284 aBAC2D.D0(aU, aP2D0);
1285 for(i=2; i<=aNbS; i++) {
1287 aBAC2D.D0(aU, aP2D1);
1288 aP2D0.Coord(aX0, aY0);
1289 aP2D1.Coord(aX1, aY1);
1291 aS=aS+(aY0+aY1)*(aX1-aX0);
1295 }//for (; aItW.More(); aItW.Next()) {
1300 //=======================================================================
1301 // function: MakeContainer
1303 //=======================================================================
1304 void BOPTools_AlgoTools::MakeContainer(const TopAbs_ShapeEnum theType,
1310 case TopAbs_COMPOUND:{
1312 aBB.MakeCompound(aC);
1317 case TopAbs_COMPSOLID:{
1318 TopoDS_CompSolid aCS;
1319 aBB.MakeCompSolid(aCS);
1325 TopoDS_Solid aSolid;
1326 aBB.MakeSolid(aSolid);
1333 TopoDS_Shell aShell;
1334 aBB.MakeShell(aShell);
1341 aBB.MakeWire(aWire);
1350 //=======================================================================
1351 // function: MakePCurve
1353 //=======================================================================
1354 void BOPTools_AlgoTools::MakePCurve(const TopoDS_Edge& aE,
1355 const TopoDS_Face& aF1,
1356 const TopoDS_Face& aF2,
1357 const IntTools_Curve& aIC,
1358 const Standard_Boolean bPC1,
1359 const Standard_Boolean bPC2)
1363 Standard_Real aTolE, aT1, aT2, aOutFirst, aOutLast, aOutTol;
1364 Handle(Geom2d_Curve) aC2D, aC2DA, aC2Dx1;
1367 Standard_Boolean bPC;
1369 aTolE=BRep_Tool::Tolerance(aE);
1371 const Handle(Geom_Curve)& aC3DE=BRep_Tool::Curve(aE, aT1, aT2);
1372 Handle(Geom_TrimmedCurve)aC3DETrim=
1373 new Geom_TrimmedCurve(aC3DE, aT1, aT2);
1375 for (i=0; i<2; ++i) {
1376 bPC = !i ? bPC1 : bPC2;
1383 aC2Dx1=aIC.FirstCurve2d();
1387 aC2Dx1=aIC.SecondCurve2d();
1390 aFFWD.Orientation(TopAbs_FORWARD);
1393 if (aC2D.IsNull()) {
1394 BOPTools_AlgoTools2D::BuildPCurveForEdgeOnFace(aE, aFFWD);
1395 BOPTools_AlgoTools2D::CurveOnSurface(aE, aFFWD, aC2D,
1396 aOutFirst, aOutLast,
1400 if (aC3DE->IsPeriodic()) {
1401 BOPTools_AlgoTools2D::AdjustPCurveOnFace(aFFWD, aT1, aT2, aC2D,
1405 BOPTools_AlgoTools2D::AdjustPCurveOnFace(aFFWD, aC3DETrim, aC2D,
1409 aBB.UpdateEdge(aE, aC2DA, aFFWD, aTolE);
1410 //BRepLib::SameParameter(aE);
1412 BRepLib::SameParameter(aE);
1414 //=======================================================================
1415 // function: MakeEdge
1417 //=======================================================================
1418 void BOPTools_AlgoTools::MakeEdge(const IntTools_Curve& theIC,
1419 const TopoDS_Vertex& theV1,
1420 const Standard_Real theT1,
1421 const TopoDS_Vertex& theV2,
1422 const Standard_Real theT2,
1423 const Standard_Real theTolR3D,
1426 Standard_Real aTolV;
1429 BOPTools_AlgoTools::MakeSectEdge (theIC, theV1, theT1, theV2, theT2,
1432 aBB.UpdateEdge(theE, theTolR3D);
1434 aTolV=BRep_Tool::Tolerance(theV1);
1435 if (aTolV<theTolR3D) {
1436 aBB.UpdateVertex(theV1, theTolR3D);
1439 aTolV=BRep_Tool::Tolerance(theV2);
1440 if (aTolV<theTolR3D) {
1441 aBB.UpdateVertex(theV2, theTolR3D);
1444 //=======================================================================
1445 // function: ComputeVV
1447 //=======================================================================
1448 Standard_Integer BOPTools_AlgoTools::ComputeVV(const TopoDS_Vertex& aV1,
1450 const Standard_Real aTolP2)
1452 Standard_Real aTolV1, aTolSum, aTolSum2, aD2;
1455 aTolV1=BRep_Tool::Tolerance(aV1);
1457 aTolSum=aTolV1+aTolP2;
1458 aTolSum2=aTolSum*aTolSum;
1460 aP1=BRep_Tool::Pnt(aV1);
1462 aD2=aP1.SquareDistance(aP2);
1468 //=======================================================================
1469 // function: ComputeVV
1471 //=======================================================================
1472 Standard_Integer BOPTools_AlgoTools::ComputeVV(const TopoDS_Vertex& aV1,
1473 const TopoDS_Vertex& aV2)
1475 Standard_Real aTolV1, aTolV2, aTolSum, aTolSum2, aD2;
1478 aTolV1=BRep_Tool::Tolerance(aV1);
1479 aTolV2=BRep_Tool::Tolerance(aV2);
1480 aTolSum=aTolV1+aTolV2;
1481 aTolSum2=aTolSum*aTolSum;
1483 aP1=BRep_Tool::Pnt(aV1);
1484 aP2=BRep_Tool::Pnt(aV2);
1486 aD2=aP1.SquareDistance(aP2);
1492 //=======================================================================
1493 // function: MakeVertex
1495 //=======================================================================
1496 void BOPTools_AlgoTools::MakeVertex(BOPCol_ListOfShape& aLV,
1497 TopoDS_Vertex& aVnew)
1499 Standard_Integer aNb;
1507 aVnew=*((TopoDS_Vertex*)(&aLV.First()));
1512 Standard_Integer m, n;
1513 Standard_Real aR[2], dR, aD, aEps;
1514 TopoDS_Vertex aV[2];
1519 for (m=0; m<aNb; ++m) {
1521 *((TopoDS_Vertex*)(&aLV.First())):
1522 *((TopoDS_Vertex*)(&aLV.Last()));
1523 aP[m]=BRep_Tool::Pnt(aV[m]);
1524 aR[m]=BRep_Tool::Tolerance(aV[m]);
1534 dR=aR[m]-aR[n]; // dR >= 0.
1535 gp_Vec aVD(aP[m], aP[n]);
1538 if (aD<=dR || aD<aEps) {
1539 aBB.MakeVertex (aVnew, aP[m], aR[m]);
1546 aRr=0.5*(aR[m]+aR[n]+aD);
1547 aXYZr=0.5*(aP[m].XYZ()+aP[n].XYZ()-aVD.XYZ()*(dR/aD));
1550 aBB.MakeVertex (aVnew, aPr, aRr);
1553 }// else if (aNb==2) {
1555 else { // if (aNb>2)
1556 Standard_Real aTi, aDi, aDmax;
1558 gp_XYZ aXYZ(0.,0.,0.), aXYZi;
1559 BOPCol_ListIteratorOfListOfShape aIt;
1561 aIt.Initialize(aLV);
1562 for (; aIt.More(); aIt.Next()) {
1563 TopoDS_Vertex& aVi=*((TopoDS_Vertex*)(&aIt.Value()));
1564 aPi=BRep_Tool::Pnt(aVi);
1569 aXYZ.Divide((Standard_Real)aNb);
1573 aIt.Initialize(aLV);
1574 for (; aIt.More(); aIt.Next()) {
1575 TopoDS_Vertex& aVi=*((TopoDS_Vertex*)(&aIt.Value()));
1576 aPi=BRep_Tool::Pnt(aVi);
1577 aTi=BRep_Tool::Tolerance(aVi);
1578 aDi=aP.SquareDistance(aPi);
1587 aBB.MakeVertex (aVnew, aP, aDmax);
1590 //=======================================================================
1591 //function : GetEdgeOnFace
1593 //=======================================================================
1594 Standard_Boolean BOPTools_AlgoTools::GetEdgeOnFace
1595 (const TopoDS_Edge& theE1,
1596 const TopoDS_Face& theF2,
1599 Standard_Boolean bFound;
1600 TopoDS_Iterator aItF, aItW;
1602 bFound=Standard_False;
1604 aItF.Initialize(theF2);
1605 for (; aItF.More(); aItF.Next()) {
1606 const TopoDS_Shape& aW=aItF.Value();
1607 aItW.Initialize(aW);
1608 for (; aItW.More(); aItW.Next()) {
1609 const TopoDS_Shape& aE=aItW.Value();
1610 if (aE.IsSame(theE1)) {
1611 theE2=(*(TopoDS_Edge*)(&aE));
1619 //=======================================================================
1620 //function : FindFacePairs
1622 //=======================================================================
1623 Standard_Boolean FindFacePairs (const TopoDS_Edge& theE,
1624 const BOPCol_ListOfShape& thLF,
1625 BOPTools_ListOfCoupleOfShape& theLCFF,
1626 Handle(IntTools_Context)& theContext)
1628 Standard_Boolean bFound;
1629 Standard_Integer i, aNbCEF;
1630 TopAbs_Orientation aOr, aOrC = TopAbs_FORWARD;
1631 BOPCol_MapOfShape aMFP;
1632 TopoDS_Face aF1, aF2;
1633 TopoDS_Edge aEL, aE1;
1634 BOPCol_ListIteratorOfListOfShape aItLF;
1635 BOPTools_CoupleOfShape aCEF, aCFF;
1636 BOPTools_ListOfCoupleOfShape aLCEF, aLCEFx;
1637 BOPTools_ListIteratorOfListOfCoupleOfShape aIt;
1639 bFound=Standard_True;
1642 aItLF.Initialize(thLF);
1643 for (; aItLF.More(); aItLF.Next()) {
1644 const TopoDS_Face& aFL=(*(TopoDS_Face*)(&aItLF.Value()));
1646 bFound=BOPTools_AlgoTools::GetEdgeOnFace(theE, aFL, aEL);
1648 return bFound; // it can not be so
1651 aCEF.SetShape1(aEL);
1652 aCEF.SetShape2(aFL);
1656 aNbCEF=aLCEF.Extent();
1661 aIt.Initialize(aLCEF);
1662 for (i=0; aIt.More(); aIt.Next(), ++i) {
1663 const BOPTools_CoupleOfShape& aCSx=aIt.Value();
1664 const TopoDS_Shape& aEx=aCSx.Shape1();
1665 const TopoDS_Shape& aFx=aCSx.Shape2();
1667 aOr=aEx.Orientation();
1670 aOrC=TopAbs::Reverse(aOr);
1671 aE1=(*(TopoDS_Edge*)(&aEx));
1672 aF1=(*(TopoDS_Face*)(&aFx));
1678 aLCEFx.Append(aCSx);
1684 BOPTools_AlgoTools::GetFaceOff(aE1, aF1, aLCEFx, aF2, theContext);
1686 aCFF.SetShape1(aF1);
1687 aCFF.SetShape2(aF2);
1688 theLCFF.Append(aCFF);
1697 aIt.Initialize(aLCEFx);
1698 for (; aIt.More(); aIt.Next()) {
1699 const BOPTools_CoupleOfShape& aCSx=aIt.Value();
1700 const TopoDS_Shape& aFx=aCSx.Shape2();
1701 if (!aMFP.Contains(aFx)) {
1706 aNbCEF=aLCEF.Extent();
1711 //=======================================================================
1712 //function : AngleWithRef
1714 //=======================================================================
1715 Standard_Real AngleWithRef(const gp_Dir& theD1,
1716 const gp_Dir& theD2,
1717 const gp_Dir& theDRef)
1719 Standard_Real aCosinus, aSinus, aBeta, aHalfPI, aScPr;
1724 const gp_XYZ& aXYZ1=theD1.XYZ();
1725 const gp_XYZ& aXYZ2=theD2.XYZ();
1726 aXYZ=aXYZ1.Crossed(aXYZ2);
1727 aSinus=aXYZ.Modulus();
1728 aCosinus=theD1*theD2;
1732 aBeta=aHalfPI*(1.-aCosinus);
1735 aBeta=2.*M_PI-aHalfPI*(3.+aCosinus);
1738 aScPr=aXYZ.Dot(theDRef.XYZ());
1744 //=======================================================================
1745 // function: IsBlockInOnFace
1747 //=======================================================================
1748 Standard_Boolean BOPTools_AlgoTools::IsBlockInOnFace
1749 (const IntTools_Range& aShrR,
1750 const TopoDS_Face& aF,
1751 const TopoDS_Edge& aE1,
1752 Handle(IntTools_Context)& aContext)
1754 Standard_Boolean bFlag;
1755 Standard_Real f1, l1, ULD, VLD;
1759 aShrR.Range(f1, l1);
1760 Standard_Real dt=0.0075, k;//dt=0.001, k;
1766 BOPTools_AlgoTools::PointOnEdge(aE1, f1, aP11);
1768 GeomAPI_ProjectPointOnSurf& aProjector=aContext->ProjPS(aF);
1769 aProjector.Perform(aP11);
1771 bFlag=aProjector.IsDone();
1776 aProjector.LowerDistanceParameters(ULD, VLD);
1777 aP2D.SetCoord(ULD, VLD);
1779 bFlag=aContext->IsPointInOnFace (aF, aP2D);
1786 BOPTools_AlgoTools::PointOnEdge(aE1, l1, aP12);
1788 aProjector.Perform(aP12);
1790 bFlag=aProjector.IsDone();
1795 aProjector.LowerDistanceParameters(ULD, VLD);
1796 aP2D.SetCoord(ULD, VLD);
1798 bFlag=aContext->IsPointInOnFace (aF, aP2D);
1804 // Treatment intemediate
1805 Standard_Real m1, aTolF, aTolE, aTol, aDist;
1806 m1=IntTools_Tools::IntermediatePoint(f1, l1);
1807 BOPTools_AlgoTools::PointOnEdge(aE1, m1, aP12);
1809 aProjector.Perform(aP12);
1811 bFlag=aProjector.IsDone();
1816 aTolE=BRep_Tool::Tolerance(aE1);
1817 aTolF=BRep_Tool::Tolerance(aF);
1819 aDist=aProjector.LowerDistance();
1821 return Standard_False;
1824 aProjector.LowerDistanceParameters(ULD, VLD);
1825 aP2D.SetCoord(ULD, VLD);
1827 bFlag=aContext->IsPointInOnFace (aF, aP2D);
1835 //=======================================================================
1836 //function : IsMicroEdge
1838 //=======================================================================
1839 Standard_Boolean BOPTools_AlgoTools::IsMicroEdge
1840 (const TopoDS_Edge& aE,
1841 const Handle(IntTools_Context)& aCtx)
1843 Standard_Boolean bRet;
1844 Standard_Integer iErr;
1845 Standard_Real aT1, aT2, aTmp;
1846 Handle(Geom_Curve) aC3D;
1847 TopoDS_Vertex aV1, aV2;
1849 bRet=(BRep_Tool::Degenerated(aE) ||
1850 !BRep_Tool::IsGeometric(aE));
1855 aC3D=BRep_Tool::Curve(aE, aT1, aT2);
1856 TopExp::Vertices(aE, aV1, aV2);
1857 aT1=BRep_Tool::Parameter(aV1, aE);
1858 aT2=BRep_Tool::Parameter(aV2, aE);
1865 IntTools_ShrunkRange aSR;
1866 aSR.SetContext(aCtx);
1867 aSR.SetData(aE, aT1, aT2, aV1, aV2);
1869 iErr=aSR.ErrorStatus();
1875 //=======================================================================
1876 //function : GetFaceDir
1877 //purpose : Get binormal direction for the face in the point aP
1878 //=======================================================================
1879 void GetFaceDir(const TopoDS_Edge& aE,
1880 const TopoDS_Face& aF,
1882 const Standard_Real aT,
1883 const gp_Dir& aDTgt,
1886 Handle(IntTools_Context)& theContext,
1887 GeomAPI_ProjectPointOnSurf& aProjPL,
1888 const Standard_Real aDt)
1890 Standard_Real aTolE;
1893 BOPTools_AlgoTools3D::GetNormalToFaceOnEdge(aE, aF, aT, aDN);
1894 if (aF.Orientation()==TopAbs_REVERSED){
1898 aTolE=BRep_Tool::Tolerance(aE);
1901 if (!FindPointInFace(aF, aP, aDB, aPx, theContext, aProjPL, aDt, aTolE)) {
1902 BOPTools_AlgoTools3D::GetApproxNormalToFaceOnEdge(aE, aF, aT, aPx,
1904 aProjPL.Perform(aPx);
1905 aPx = aProjPL.NearestPoint();
1906 gp_Vec aVec(aP, aPx);
1907 aDB.SetXYZ(aVec.XYZ());
1910 //=======================================================================
1911 //function : FindPointInFace
1912 //purpose : Find a point in the face in direction of <aDB>
1913 //=======================================================================
1914 Standard_Boolean FindPointInFace(const TopoDS_Face& aF,
1918 Handle(IntTools_Context)& theContext,
1919 GeomAPI_ProjectPointOnSurf& aProjPL,
1920 const Standard_Real aDt,
1921 const Standard_Real aTolE)
1923 Standard_Integer aNbItMax;
1924 Standard_Real aDist, aDTol, aPM, anEps;
1925 Standard_Boolean bRet;
1928 aDTol = Precision::Angular();
1929 aPM = aP.XYZ().Modulus();
1931 aDTol = 5.e-16 * aPM;
1933 bRet = Standard_False;
1935 anEps = Precision::SquareConfusion();
1937 GeomAPI_ProjectPointOnSurf& aProj=theContext->ProjPS(aF);
1941 if (!aProj.IsDone()) {
1944 aPS=aProj.NearestPoint();
1945 aProjPL.Perform(aPS);
1946 aPS=aProjPL.NearestPoint();
1948 aPS.SetXYZ(aPS.XYZ()+2.*aTolE*aDB.XYZ());
1950 if (!aProj.IsDone()) {
1953 aPS=aProj.NearestPoint();
1954 aProjPL.Perform(aPS);
1955 aPS=aProjPL.NearestPoint();
1958 aP1.SetXYZ(aPS.XYZ()+aDt*aDB.XYZ());
1961 if (!aProj.IsDone()) {
1964 aPOut = aProj.NearestPoint();
1965 aDist = aProj.LowerDistance();
1967 aProjPL.Perform(aPOut);
1968 aPOut = aProjPL.NearestPoint();
1970 gp_Vec aV(aPS, aPOut);
1971 if (aV.SquareMagnitude() < anEps) {
1974 aDB.SetXYZ(aV.XYZ());
1975 } while (aDist > aDTol && --aNbItMax);
1977 bRet = aDist < aDTol;
1980 //=======================================================================
1981 //function : MinStep3D
1983 //=======================================================================
1984 Standard_Real MinStep3D(const TopoDS_Edge& theE1,
1985 const TopoDS_Face& theF1,
1986 const BOPTools_ListOfCoupleOfShape& theLCS,
1989 Standard_Real aDt, aTolE, aTolF, aDtMax, aDtMin, aR;
1990 BOPTools_CoupleOfShape aCS1;
1991 BOPTools_ListOfCoupleOfShape aLCS;
1992 BOPTools_ListIteratorOfListOfCoupleOfShape aIt;
1993 BRepAdaptor_Surface aBAS;
1996 aCS1.SetShape1(theE1);
1997 aCS1.SetShape2(theF1);
2000 aTolE = BRep_Tool::Tolerance(theE1);
2004 aIt.Initialize(aLCS);
2005 for (; aIt.More(); aIt.Next()) {
2006 const BOPTools_CoupleOfShape& aCS = aIt.Value();
2007 const TopoDS_Face& aF = (*(TopoDS_Face*)(&aCS.Shape2()));
2009 aTolF = BRep_Tool::Tolerance(aF);
2010 aDt = 2*(aTolE + aTolF);
2013 aBAS.Initialize(aF, Standard_False);
2014 GeomAbs_SurfaceType aSType = aBAS.GetType();
2016 case GeomAbs_Cylinder: {
2017 aR = aBAS.Cylinder().Radius();
2020 case GeomAbs_Cone: {
2021 gp_Lin aL(aBAS.Cone().Axis());
2022 aR = aL.Distance(aP);
2025 case GeomAbs_Sphere: {
2026 aDtMin = Max(aDtMin, 5.e-4);
2027 aR = aBAS.Sphere().Radius();
2030 case GeomAbs_Torus: {
2031 aR = aBAS.Torus().MajorRadius();
2035 aDtMin = Max(aDtMin, 5.e-4);
2040 Standard_Real d = 10*Precision::PConfusion();
2041 aDtMin = Max(aDtMin, sqrt(d*d + 2*d*aR));
2049 if (aDtMax < aDtMin) {
2055 //=======================================================================
2056 //function : IsOpenShell
2058 //=======================================================================
2059 Standard_Boolean BOPTools_AlgoTools::IsOpenShell(const TopoDS_Shell& aSh)
2061 Standard_Boolean bRet;
2062 Standard_Integer i, aNbE, aNbF;
2063 TopAbs_Orientation aOrF;
2064 BOPCol_IndexedDataMapOfShapeListOfShape aMEF;
2065 BOPCol_ListIteratorOfListOfShape aItLS;
2067 bRet=Standard_False;
2069 BOPTools::MapShapesAndAncestors(aSh, TopAbs_EDGE, TopAbs_FACE, aMEF);
2072 for (i=1; i<=aNbE; ++i) {
2073 const TopoDS_Edge& aE=*((TopoDS_Edge*)&aMEF.FindKey(i));
2074 if (BRep_Tool::Degenerated(aE)) {
2079 const BOPCol_ListOfShape& aLF=aMEF(i);
2080 aItLS.Initialize(aLF);
2081 for (; aItLS.More(); aItLS.Next()) {
2082 const TopoDS_Shape& aF=aItLS.Value();
2083 aOrF=aF.Orientation();
2084 if (aOrF==TopAbs_INTERNAL || aOrF==TopAbs_EXTERNAL) {
2098 //=======================================================================
2099 //function : IsInvertedSolid
2101 //=======================================================================
2102 Standard_Boolean BOPTools_AlgoTools::IsInvertedSolid
2103 (const TopoDS_Solid& aSolid)
2105 Standard_Real aTolS;
2106 TopAbs_State aState;
2107 BRepClass3d_SolidClassifier aSC(aSolid);
2110 aSC.PerformInfinitePoint(aTolS);
2112 return (aState==TopAbs_IN);