1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <BOPTools_AlgoTools.ixx>
17 #include <Precision.hxx>
20 #include <TopoDS_Vertex.hxx>
21 #include <TopoDS_Edge.hxx>
25 #include <Geom_Curve.hxx>
26 #include <IntTools_Curve.hxx>
27 #include <BRepBuilderAPI_MakeEdge.hxx>
28 #include <BRepAdaptor_Curve.hxx>
29 #include <GeomAbs_CurveType.hxx>
31 #include <BRep_Tool.hxx>
32 #include <BRep_Builder.hxx>
33 #include <TopoDS_Iterator.hxx>
36 void TreatCompound(const TopoDS_Shape& theC1,
37 BOPCol_ListOfShape& theLSX);
39 //=======================================================================
40 // function: UpdateVertex
42 //=======================================================================
43 void BOPTools_AlgoTools::UpdateVertex (const TopoDS_Vertex& aVF,
44 const TopoDS_Vertex& aNewVertex)
46 Standard_Real aTolVF, aTolNewVertex, aDist, aDTol=1.e-12, aNewTol;
48 gp_Pnt aPVF=BRep_Tool::Pnt(aVF);
49 gp_Pnt aPNewVertex=BRep_Tool::Pnt(aNewVertex);
50 aTolVF=BRep_Tool::Tolerance(aVF);
51 aTolNewVertex=BRep_Tool::Tolerance(aNewVertex);
53 aDist=aPVF.Distance(aPNewVertex);
54 aNewTol=aDist+aTolNewVertex;
58 BB.UpdateVertex (aVF, aNewTol+aDTol);
62 //=======================================================================
63 // function: UpdateVertex
65 //=======================================================================
66 void BOPTools_AlgoTools::UpdateVertex (const TopoDS_Edge& aE,
67 const Standard_Real aT,
68 const TopoDS_Vertex& aV)
70 Standard_Real aTolV, aDist, aDTol=1.e-12, aFirst, aLast;
73 gp_Pnt aPv=BRep_Tool::Pnt(aV);
74 aTolV=BRep_Tool::Tolerance(aV);
76 Handle(Geom_Curve) aC3D=BRep_Tool::Curve(aE, aFirst, aLast);
78 aDist=aPv.Distance(aPc);
81 BB.UpdateVertex (aV, aDist+aDTol);
85 //=======================================================================
86 // function: UpdateVertex
88 //=======================================================================
89 void BOPTools_AlgoTools::UpdateVertex (const IntTools_Curve& aC,
90 const Standard_Real aT,
91 const TopoDS_Vertex& aV)
93 Standard_Real aTolV, aDist, aDTol=1.e-12;
96 gp_Pnt aPv=BRep_Tool::Pnt(aV);
97 aTolV=BRep_Tool::Tolerance(aV);
99 Handle(Geom_Curve) aC3D=aC.Curve();
101 aDist=aPv.Distance(aPc);
104 BB.UpdateVertex (aV, aDist+aDTol);
107 //=======================================================================
108 // function: MakeSectEdge
110 //=======================================================================
111 void BOPTools_AlgoTools::MakeSectEdge(const IntTools_Curve& aIC,
112 const TopoDS_Vertex& aV1,
113 const Standard_Real aP1,
114 const TopoDS_Vertex& aV2,
115 const Standard_Real aP2,
116 TopoDS_Edge& aNewEdge)
118 Handle(Geom_Curve) aC=aIC.Curve ();
120 BRepBuilderAPI_MakeEdge aMakeEdge(aC, aV1, aV2, aP1, aP2);
122 const TopoDS_Edge& aE=TopoDS::Edge(aMakeEdge.Shape());
124 // Range must be as it was !
126 aBB.Range (aE, aP1, aP2);
132 //=======================================================================
133 // function: MakeSplitEdge
135 //=======================================================================
136 void BOPTools_AlgoTools::MakeSplitEdge(const TopoDS_Edge& aE,
137 const TopoDS_Vertex& aV1,
138 const Standard_Real aP1,
139 const TopoDS_Vertex& aV2,
140 const Standard_Real aP2,
141 TopoDS_Edge& aNewEdge)
143 Standard_Real f, l, aTol;
144 Handle(Geom_Curve) aC=BRep_Tool::Curve (aE, f, l);
145 aTol=BRep_Tool::Tolerance(aE);
147 // MakeEdge is used for chechking all input data only
148 BRepBuilderAPI_MakeEdge aMakeEdge(aC, aV1, aV2, aP1, aP2);
149 //ZZ const TopoDS_Edge& E1=TopoDS::Edge(aMakeEdge.Shape());
156 BB.Range(E, aP1, aP2);
157 BB.UpdateEdge(E, aTol);
161 //=======================================================================
162 // function: MakeNewVertex
164 //=======================================================================
165 void BOPTools_AlgoTools::MakeNewVertex(const TopoDS_Vertex& aV1,
166 const TopoDS_Vertex& aV2,
167 TopoDS_Vertex& aNewVertex)
169 gp_Pnt aPnt1=BRep_Tool::Pnt(aV1);
170 Standard_Real aTol1=BRep_Tool::Tolerance(aV1);
172 gp_Pnt aPnt2=BRep_Tool::Pnt(aV2);
173 Standard_Real aTol2=BRep_Tool::Tolerance(aV2);
175 Standard_Real aMaxTol, aDist;
177 aDist=aPnt1.Distance(aPnt2);
178 aMaxTol=(aTol1>aTol2)? aTol1 : aTol2;
179 aMaxTol=aMaxTol+0.5*aDist;
181 const gp_XYZ& aXYZ1=aPnt1.XYZ();
182 const gp_XYZ& aXYZ2=aPnt2.XYZ();
183 gp_XYZ aNewXYZ=0.5*(aXYZ1+aXYZ2);
185 gp_Pnt aNewPnt(aNewXYZ);
187 aBB.MakeVertex (aNewVertex, aNewPnt, aMaxTol);
189 //=======================================================================
190 // function: MakeNewVertex
192 //=======================================================================
193 void BOPTools_AlgoTools::MakeNewVertex(const gp_Pnt& aP,
194 const Standard_Real aTol,
195 TopoDS_Vertex& aNewVertex)
198 aBB.MakeVertex (aNewVertex, aP, aTol);
201 //=======================================================================
202 // function: MakeNewVertex
204 //=======================================================================
205 void BOPTools_AlgoTools::MakeNewVertex(const TopoDS_Edge& aE1,
206 const Standard_Real aParm1,
207 const TopoDS_Edge& aE2,
208 const Standard_Real aParm2,
209 TopoDS_Vertex& aNewVertex)
211 Standard_Real aTol1, aTol2, aMaxTol, aDist;
214 PointOnEdge (aE1, aParm1, aPnt1);
215 PointOnEdge (aE2, aParm2, aPnt2);
217 aTol1=BRep_Tool::Tolerance(aE1);
218 aTol2=BRep_Tool::Tolerance(aE2);
220 aDist=aPnt1.Distance(aPnt2);
221 aMaxTol=(aTol1>aTol2)? aTol1 : aTol2;
222 aMaxTol=aMaxTol+0.5*aDist;
224 const gp_XYZ& aXYZ1=aPnt1.XYZ();
225 const gp_XYZ& aXYZ2=aPnt2.XYZ();
226 gp_XYZ aNewXYZ=0.5*(aXYZ1+aXYZ2);
228 gp_Pnt aNewPnt(aNewXYZ);
230 aBB.MakeVertex (aNewVertex, aNewPnt, aMaxTol);
232 //=======================================================================
233 // function: MakeNewVertex
235 //=======================================================================
236 void BOPTools_AlgoTools::MakeNewVertex(const TopoDS_Edge& aE1,
237 const Standard_Real aParm1,
238 const TopoDS_Face& aF1,
239 TopoDS_Vertex& aNewVertex)
241 Standard_Real aTol1, aTol2, aMaxTol, delta=1.e-12;
244 PointOnEdge (aE1, aParm1, aPnt);
246 aTol1=BRep_Tool::Tolerance(aE1);
247 aTol2=BRep_Tool::Tolerance(aF1);
249 //aMaxTol=(aTol1>aTol2)? aTol1 : aTol2;
250 aMaxTol=aTol1+aTol2+delta;
253 aBB.MakeVertex (aNewVertex, aPnt, aMaxTol);
256 //=======================================================================
257 // function: PointOnEdge
259 //=======================================================================
260 void BOPTools_AlgoTools::PointOnEdge(const TopoDS_Edge& aE,
261 const Standard_Real aParm,
265 Handle(Geom_Curve) C1=BRep_Tool::Curve(aE, f, l);
269 //=======================================================================
270 //function : CorrectRange
272 //=======================================================================
273 void BOPTools_AlgoTools::CorrectRange(const TopoDS_Edge& aE1,
274 const TopoDS_Edge& aE2,
275 const IntTools_Range& aSR,
276 IntTools_Range& aNewSR)
279 Standard_Real aRes, aTolE1, aTolE2, aTF, aTL, dT;
280 BRepAdaptor_Curve aBC;
281 GeomAbs_CurveType aCT;
289 if (aCT==GeomAbs_Line) {
293 dT=Precision::PConfusion();
297 aTolE1=BRep_Tool::Tolerance(aE1);
298 aTolE2=BRep_Tool::Tolerance(aE2);
301 aRes = 2.*(aTolE1 + aTolE2);
303 if (aCT==GeomAbs_BezierCurve ||
304 aCT==GeomAbs_BSplineCurve||
305 aCT==GeomAbs_OtherCurve) {
308 aBC.D1 (aTF, aP, aDer);
311 aBC.D1 (aTL, aP, aDer);
314 Standard_Real aMgn = aDer.Magnitude();
320 aRes = aBC.Resolution(aRes);
322 } // if (aCT==GeomAbs_BezierCurve||...
324 aRes = aBC.Resolution(aRes);
328 aNewSR.SetFirst (aTF+aRes);
331 aNewSR.SetLast (aTL-aRes);
334 if ((aNewSR.Last()-aNewSR.First()) < dT) {
337 //aNewSR=((aNewSR.Last()-aNewSR.First()) < dT) ? aSR : aNewSR;
341 //=======================================================================
342 //function : CorrectRange
344 //=======================================================================
345 void BOPTools_AlgoTools::CorrectRange(const TopoDS_Edge& aE,
346 const TopoDS_Face& aF,
347 const IntTools_Range& aSR,
348 IntTools_Range& aNewSR)
351 Standard_Real aRes, aTolF, aTF, aTL, dT;
352 BRepAdaptor_Curve aBC;
353 GeomAbs_CurveType aCT;
359 dT=Precision::PConfusion();
366 aTolF=BRep_Tool::Tolerance(aF);
371 if (aCT==GeomAbs_BezierCurve ||
372 aCT==GeomAbs_BSplineCurve||
373 aCT==GeomAbs_OtherCurve) {
376 aBC.D1 (aTF, aP, aDer);
379 aBC.D1 (aTL, aP, aDer);
382 Standard_Real aMgn = aDer.Magnitude();
388 aRes = aBC.Resolution(aRes);
390 } // if (aCT==GeomAbs_BezierCurve||...
392 aRes = aBC.Resolution(aRes);
396 aNewSR.SetFirst (aTF+aRes);
399 aNewSR.SetLast (aTL-aRes);
402 if ((aNewSR.Last()-aNewSR.First()) < dT) {
407 //=======================================================================
408 //function : Dimension
410 //=======================================================================
411 Standard_Integer BOPTools_AlgoTools::Dimension(const TopoDS_Shape& theS)
413 Standard_Integer i, iRet, iRx0 = 0, iRx = 0;
414 TopAbs_ShapeEnum aTS;
415 BOPCol_ListOfShape aLS;
416 BOPCol_ListIteratorOfListOfShape aIt;
418 aTS=theS.ShapeType();
419 if (aTS!=TopAbs_COMPOUND) {
430 case TopAbs_COMPSOLID:
440 TreatCompound(theS, aLS);
442 iRet = -2; //empty compound
446 for (i=0; aIt.More(); aIt.Next()) {
447 const TopoDS_Shape& aSx=aIt.Value();
461 //=======================================================================
462 //function : TreatCompound
464 //=======================================================================
465 void TreatCompound(const TopoDS_Shape& theC1,
466 BOPCol_ListOfShape& theLSX)
468 Standard_Integer aNbC1;
469 TopAbs_ShapeEnum aType;
470 BOPCol_ListOfShape aLC, aLC1;
471 BOPCol_ListIteratorOfListOfShape aIt, aIt1;
472 TopoDS_Iterator aItC;
478 for (; aIt.More(); aIt.Next()) {
479 const TopoDS_Shape& aC=aIt.Value(); //C is compound
482 for (; aItC.More(); aItC.Next()) {
483 const TopoDS_Shape& aS=aItC.Value();
484 aType=aS.ShapeType();
485 if (aType==TopAbs_COMPOUND) {
500 aIt.Initialize(aLC1);
501 for (; aIt.More(); aIt.Next()) {
502 const TopoDS_Shape& aSC=aIt.Value();