1 // Created by: Peter KURNEV
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
20 #include <BOPTools_AlgoTools.ixx>
22 #include <Precision.hxx>
25 #include <TopoDS_Vertex.hxx>
26 #include <TopoDS_Edge.hxx>
30 #include <Geom_Curve.hxx>
31 #include <IntTools_Curve.hxx>
32 #include <BRepBuilderAPI_MakeEdge.hxx>
33 #include <BRepAdaptor_Curve.hxx>
34 #include <GeomAbs_CurveType.hxx>
36 #include <BRep_Tool.hxx>
37 #include <BRep_Builder.hxx>
38 #include <TopoDS_Iterator.hxx>
41 void TreatCompound(const TopoDS_Shape& theC1,
42 BOPCol_ListOfShape& theLSX);
44 //=======================================================================
45 // function: UpdateVertex
47 //=======================================================================
48 void BOPTools_AlgoTools::UpdateVertex (const TopoDS_Vertex& aVF,
49 const TopoDS_Vertex& aNewVertex)
51 Standard_Real aTolVF, aTolNewVertex, aDist, aDTol=1.e-12, aNewTol;
53 gp_Pnt aPVF=BRep_Tool::Pnt(aVF);
54 gp_Pnt aPNewVertex=BRep_Tool::Pnt(aNewVertex);
55 aTolVF=BRep_Tool::Tolerance(aVF);
56 aTolNewVertex=BRep_Tool::Tolerance(aNewVertex);
58 aDist=aPVF.Distance(aPNewVertex);
59 aNewTol=aDist+aTolNewVertex;
63 BB.UpdateVertex (aVF, aNewTol+aDTol);
67 //=======================================================================
68 // function: UpdateVertex
70 //=======================================================================
71 void BOPTools_AlgoTools::UpdateVertex (const TopoDS_Edge& aE,
72 const Standard_Real aT,
73 const TopoDS_Vertex& aV)
75 Standard_Real aTolV, aDist, aDTol=1.e-12, aFirst, aLast;
78 gp_Pnt aPv=BRep_Tool::Pnt(aV);
79 aTolV=BRep_Tool::Tolerance(aV);
81 Handle(Geom_Curve) aC3D=BRep_Tool::Curve(aE, aFirst, aLast);
83 aDist=aPv.Distance(aPc);
86 BB.UpdateVertex (aV, aDist+aDTol);
90 //=======================================================================
91 // function: UpdateVertex
93 //=======================================================================
94 void BOPTools_AlgoTools::UpdateVertex (const IntTools_Curve& aC,
95 const Standard_Real aT,
96 const TopoDS_Vertex& aV)
98 Standard_Real aTolV, aDist, aDTol=1.e-12;
101 gp_Pnt aPv=BRep_Tool::Pnt(aV);
102 aTolV=BRep_Tool::Tolerance(aV);
104 Handle(Geom_Curve) aC3D=aC.Curve();
106 aDist=aPv.Distance(aPc);
109 BB.UpdateVertex (aV, aDist+aDTol);
112 //=======================================================================
113 // function: MakeSectEdge
115 //=======================================================================
116 void BOPTools_AlgoTools::MakeSectEdge(const IntTools_Curve& aIC,
117 const TopoDS_Vertex& aV1,
118 const Standard_Real aP1,
119 const TopoDS_Vertex& aV2,
120 const Standard_Real aP2,
121 TopoDS_Edge& aNewEdge)
123 Handle(Geom_Curve) aC=aIC.Curve ();
125 BRepBuilderAPI_MakeEdge aMakeEdge(aC, aV1, aV2, aP1, aP2);
127 const TopoDS_Edge& aE=TopoDS::Edge(aMakeEdge.Shape());
129 // Range must be as it was !
131 aBB.Range (aE, aP1, aP2);
137 //=======================================================================
138 // function: MakeSplitEdge
140 //=======================================================================
141 void BOPTools_AlgoTools::MakeSplitEdge(const TopoDS_Edge& aE,
142 const TopoDS_Vertex& aV1,
143 const Standard_Real aP1,
144 const TopoDS_Vertex& aV2,
145 const Standard_Real aP2,
146 TopoDS_Edge& aNewEdge)
148 Standard_Real f, l, aTol;
149 Handle(Geom_Curve) aC=BRep_Tool::Curve (aE, f, l);
150 aTol=BRep_Tool::Tolerance(aE);
152 // MakeEdge is used for chechking all input data only
153 BRepBuilderAPI_MakeEdge aMakeEdge(aC, aV1, aV2, aP1, aP2);
154 //ZZ const TopoDS_Edge& E1=TopoDS::Edge(aMakeEdge.Shape());
161 BB.Range(E, aP1, aP2);
162 BB.UpdateEdge(E, aTol);
166 //=======================================================================
167 // function: MakeNewVertex
169 //=======================================================================
170 void BOPTools_AlgoTools::MakeNewVertex(const TopoDS_Vertex& aV1,
171 const TopoDS_Vertex& aV2,
172 TopoDS_Vertex& aNewVertex)
174 gp_Pnt aPnt1=BRep_Tool::Pnt(aV1);
175 Standard_Real aTol1=BRep_Tool::Tolerance(aV1);
177 gp_Pnt aPnt2=BRep_Tool::Pnt(aV2);
178 Standard_Real aTol2=BRep_Tool::Tolerance(aV2);
180 Standard_Real aMaxTol, aDist;
182 aDist=aPnt1.Distance(aPnt2);
183 aMaxTol=(aTol1>aTol2)? aTol1 : aTol2;
184 aMaxTol=aMaxTol+0.5*aDist;
186 const gp_XYZ& aXYZ1=aPnt1.XYZ();
187 const gp_XYZ& aXYZ2=aPnt2.XYZ();
188 gp_XYZ aNewXYZ=0.5*(aXYZ1+aXYZ2);
190 gp_Pnt aNewPnt(aNewXYZ);
192 aBB.MakeVertex (aNewVertex, aNewPnt, aMaxTol);
194 //=======================================================================
195 // function: MakeNewVertex
197 //=======================================================================
198 void BOPTools_AlgoTools::MakeNewVertex(const gp_Pnt& aP,
199 const Standard_Real aTol,
200 TopoDS_Vertex& aNewVertex)
203 aBB.MakeVertex (aNewVertex, aP, aTol);
206 //=======================================================================
207 // function: MakeNewVertex
209 //=======================================================================
210 void BOPTools_AlgoTools::MakeNewVertex(const TopoDS_Edge& aE1,
211 const Standard_Real aParm1,
212 const TopoDS_Edge& aE2,
213 const Standard_Real aParm2,
214 TopoDS_Vertex& aNewVertex)
216 Standard_Real aTol1, aTol2, aMaxTol, aDist;
219 PointOnEdge (aE1, aParm1, aPnt1);
220 PointOnEdge (aE2, aParm2, aPnt2);
222 aTol1=BRep_Tool::Tolerance(aE1);
223 aTol2=BRep_Tool::Tolerance(aE2);
225 aDist=aPnt1.Distance(aPnt2);
226 aMaxTol=(aTol1>aTol2)? aTol1 : aTol2;
227 aMaxTol=aMaxTol+0.5*aDist;
229 const gp_XYZ& aXYZ1=aPnt1.XYZ();
230 const gp_XYZ& aXYZ2=aPnt2.XYZ();
231 gp_XYZ aNewXYZ=0.5*(aXYZ1+aXYZ2);
233 gp_Pnt aNewPnt(aNewXYZ);
235 aBB.MakeVertex (aNewVertex, aNewPnt, aMaxTol);
237 //=======================================================================
238 // function: MakeNewVertex
240 //=======================================================================
241 void BOPTools_AlgoTools::MakeNewVertex(const TopoDS_Edge& aE1,
242 const Standard_Real aParm1,
243 const TopoDS_Face& aF1,
244 TopoDS_Vertex& aNewVertex)
246 Standard_Real aTol1, aTol2, aMaxTol, delta=1.e-12;
249 PointOnEdge (aE1, aParm1, aPnt);
251 aTol1=BRep_Tool::Tolerance(aE1);
252 aTol2=BRep_Tool::Tolerance(aF1);
254 //aMaxTol=(aTol1>aTol2)? aTol1 : aTol2;
255 aMaxTol=aTol1+aTol2+delta;
258 aBB.MakeVertex (aNewVertex, aPnt, aMaxTol);
261 //=======================================================================
262 // function: PointOnEdge
264 //=======================================================================
265 void BOPTools_AlgoTools::PointOnEdge(const TopoDS_Edge& aE,
266 const Standard_Real aParm,
270 Handle(Geom_Curve) C1=BRep_Tool::Curve(aE, f, l);
274 //=======================================================================
275 //function : CorrectRange
277 //=======================================================================
278 void BOPTools_AlgoTools::CorrectRange(const TopoDS_Edge& aE1,
279 const TopoDS_Edge& aE2,
280 const IntTools_Range& aSR,
281 IntTools_Range& aNewSR)
284 Standard_Real aRes, aTolE1, aTolE2, aTF, aTL, dT;
285 BRepAdaptor_Curve aBC;
286 GeomAbs_CurveType aCT;
294 if (aCT==GeomAbs_Line) {
298 dT=Precision::PConfusion();
302 aTolE1=BRep_Tool::Tolerance(aE1);
303 aTolE2=BRep_Tool::Tolerance(aE2);
306 aRes = 2.*(aTolE1 + aTolE2);
308 if (aCT==GeomAbs_BezierCurve ||
309 aCT==GeomAbs_BSplineCurve||
310 aCT==GeomAbs_OtherCurve) {
313 aBC.D1 (aTF, aP, aDer);
316 aBC.D1 (aTL, aP, aDer);
319 Standard_Real aMgn = aDer.Magnitude();
325 aRes = aBC.Resolution(aRes);
327 } // if (aCT==GeomAbs_BezierCurve||...
329 aRes = aBC.Resolution(aRes);
333 aNewSR.SetFirst (aTF+aRes);
336 aNewSR.SetLast (aTL-aRes);
339 if ((aNewSR.Last()-aNewSR.First()) < dT) {
342 //aNewSR=((aNewSR.Last()-aNewSR.First()) < dT) ? aSR : aNewSR;
346 //=======================================================================
347 //function : CorrectRange
349 //=======================================================================
350 void BOPTools_AlgoTools::CorrectRange(const TopoDS_Edge& aE,
351 const TopoDS_Face& aF,
352 const IntTools_Range& aSR,
353 IntTools_Range& aNewSR)
356 Standard_Real aRes, aTolF, aTF, aTL, dT;
357 BRepAdaptor_Curve aBC;
358 GeomAbs_CurveType aCT;
364 dT=Precision::PConfusion();
371 aTolF=BRep_Tool::Tolerance(aF);
376 if (aCT==GeomAbs_BezierCurve ||
377 aCT==GeomAbs_BSplineCurve||
378 aCT==GeomAbs_OtherCurve) {
381 aBC.D1 (aTF, aP, aDer);
384 aBC.D1 (aTL, aP, aDer);
387 Standard_Real aMgn = aDer.Magnitude();
393 aRes = aBC.Resolution(aRes);
395 } // if (aCT==GeomAbs_BezierCurve||...
397 aRes = aBC.Resolution(aRes);
401 aNewSR.SetFirst (aTF+aRes);
404 aNewSR.SetLast (aTL-aRes);
407 if ((aNewSR.Last()-aNewSR.First()) < dT) {
412 //=======================================================================
413 //function : Dimension
415 //=======================================================================
416 Standard_Integer BOPTools_AlgoTools::Dimension(const TopoDS_Shape& theS)
418 Standard_Integer i, iRet, iRx0, iRx;
419 TopAbs_ShapeEnum aTS;
420 BOPCol_ListOfShape aLS;
421 BOPCol_ListIteratorOfListOfShape aIt;
423 aTS=theS.ShapeType();
424 if (aTS!=TopAbs_COMPOUND) {
435 case TopAbs_COMPSOLID:
445 TreatCompound(theS, aLS);
447 iRet = -2; //empty compound
451 for (i=0; aIt.More(); aIt.Next()) {
452 const TopoDS_Shape& aSx=aIt.Value();
466 //=======================================================================
467 //function : TreatCompound
469 //=======================================================================
470 void TreatCompound(const TopoDS_Shape& theC1,
471 BOPCol_ListOfShape& theLSX)
473 Standard_Integer aNbC1;
474 TopAbs_ShapeEnum aType;
475 BOPCol_ListOfShape aLC, aLC1;
476 BOPCol_ListIteratorOfListOfShape aIt, aIt1;
477 TopoDS_Iterator aItC;
483 for (; aIt.More(); aIt.Next()) {
484 const TopoDS_Shape& aC=aIt.Value(); //C is compound
487 for (; aItC.More(); aItC.Next()) {
488 const TopoDS_Shape& aS=aItC.Value();
489 aType=aS.ShapeType();
490 if (aType==TopAbs_COMPOUND) {
505 aIt.Initialize(aLC1);
506 for (; aIt.More(); aIt.Next()) {
507 const TopoDS_Shape& aSC=aIt.Value();