1 // Created on: 2000-11-16
2 // Created by: Peter KURNEV
3 // Copyright (c) 2000-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
22 #include <BOPTools_Tools.ixx>
24 #include <Precision.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopTools_IndexedDataMapOfShapeShape.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Vertex.hxx>
33 #include <TopoDS_Iterator.hxx>
37 #include <gp_Pnt2d.hxx>
39 #include <BRep_Builder.hxx>
40 #include <BRepBuilderAPI_MakeEdge.hxx>
41 #include <BRep_Tool.hxx>
43 #include <BRepAdaptor_Curve.hxx>
44 #include <BRepAdaptor_Surface.hxx>
46 #include <GeomAdaptor_Curve.hxx>
47 #include <Geom_Curve.hxx>
48 #include <Geom_Surface.hxx>
49 #include <GeomAPI_ProjectPointOnSurf.hxx>
50 #include <GeomAbs_CurveType.hxx>
52 #include <BooleanOperations_OnceExplorer.hxx>
54 #include <IntTools_ShrunkRange.hxx>
55 #include <IntTools_Tools.hxx>
56 #include <IntTools_Range.hxx>
60 void CopySrc(const TopoDS_Shape& E,
61 TopTools_IndexedDataMapOfShapeShape& aMapSS,
62 TopoDS_Shape& anEdge);
64 //=======================================================================
65 // function: IsBlockInOnFace
67 //=======================================================================
68 Standard_Boolean BOPTools_Tools::IsBlockInOnFace (const BOPTools_PaveBlock& aPB1,
69 const TopoDS_Face& aF,
70 const Handle(IntTools_Context)& aContext)
72 Standard_Boolean bFlag;
73 Standard_Real f1, l1, ULD, VLD;
78 const IntTools_ShrunkRange& aSR1=aPB1.ShrunkRange();
79 const TopoDS_Edge& aE1=aSR1.Edge();
81 const IntTools_Range& aShrR= aSR1.ShrunkRange();
84 Standard_Real dt=0.0075, k;//dt=0.001, k;
90 BOPTools_Tools::PointOnEdge(aE1, f1, aP11);
92 GeomAPI_ProjectPointOnSurf& aProjector=aContext->ProjPS(aF);
93 aProjector.Perform(aP11);
95 bFlag=aProjector.IsDone();
100 aProjector.LowerDistanceParameters(ULD, VLD);
101 aP2D.SetCoord(ULD, VLD);
103 bFlag=aContext->IsPointInOnFace (aF, aP2D);
110 BOPTools_Tools::PointOnEdge(aE1, l1, aP12);
112 aProjector.Perform(aP12);
114 bFlag=aProjector.IsDone();
119 aProjector.LowerDistanceParameters(ULD, VLD);
120 aP2D.SetCoord(ULD, VLD);
122 bFlag=aContext->IsPointInOnFace (aF, aP2D);
128 // Treatment intemediate
129 Standard_Real m1, aTolF, aTolE, aTol, aDist;
130 m1=IntTools_Tools::IntermediatePoint(f1, l1);
131 BOPTools_Tools::PointOnEdge(aE1, m1, aP12);
133 aProjector.Perform(aP12);
135 bFlag=aProjector.IsDone();
140 aTolE=BRep_Tool::Tolerance(aE1);
141 aTolF=BRep_Tool::Tolerance(aF);
143 aDist=aProjector.LowerDistance();
145 return Standard_False;
148 aProjector.LowerDistanceParameters(ULD, VLD);
149 aP2D.SetCoord(ULD, VLD);
151 bFlag=aContext->IsPointInOnFace (aF, aP2D);
159 //=======================================================================
160 // function: IsBlocksCoinside
162 //=======================================================================
163 Standard_Boolean BOPTools_Tools::IsBlocksCoinside(const BOPTools_PaveBlock& aPB1,
164 const BOPTools_PaveBlock& aPB2)
167 Standard_Real f1, l1, aTol1;
168 const IntTools_ShrunkRange aSR1=aPB1.ShrunkRange();
169 const TopoDS_Edge& aE1=aSR1.Edge();
170 aTol1=BRep_Tool::Tolerance(aE1);
171 aPB1.Parameters(f1, l1);
173 BOPTools_Tools::PointOnEdge(aE1, f1, aP11);
174 BOPTools_Tools::PointOnEdge(aE1, l1, aP12);
176 Standard_Real f2, l2, aTol2;
177 const IntTools_ShrunkRange aSR2=aPB2.ShrunkRange();
178 const TopoDS_Edge& aE2=aSR2.Edge();
179 aTol2=BRep_Tool::Tolerance(aE2);
180 aPB2.Parameters(f2, l2);
182 BOPTools_Tools::PointOnEdge(aE2, f2, aP21);
183 BOPTools_Tools::PointOnEdge(aE2, l2, aP22);
185 Standard_Real aTolSum, d1121, d1122, d1222, d1221;
187 aTolSum=1.05*(aTol1+aTol2);
189 d1121=aP11.Distance(aP21);
191 d1222=aP12.Distance(aP22);
193 return Standard_True;
197 d1122=aP11.Distance(aP22);
199 d1221=aP12.Distance(aP21);
201 return Standard_True;
204 return Standard_False;
207 //=======================================================================
208 // function: UpdateVertex
210 //=======================================================================
211 void BOPTools_Tools::UpdateVertex (const TopoDS_Vertex& aVF,
212 const TopoDS_Vertex& aNewVertex)
214 Standard_Real aTolVF, aTolNewVertex, aDist, aDTol=1.e-12, aNewTol;
216 gp_Pnt aPVF=BRep_Tool::Pnt(aVF);
217 gp_Pnt aPNewVertex=BRep_Tool::Pnt(aNewVertex);
218 aTolVF=BRep_Tool::Tolerance(aVF);
219 aTolNewVertex=BRep_Tool::Tolerance(aNewVertex);
221 aDist=aPVF.Distance(aPNewVertex);
222 aNewTol=aDist+aTolNewVertex;
224 if (aNewTol>aTolVF) {
226 BB.UpdateVertex (aVF, aNewTol+aDTol);
230 //=======================================================================
231 // function: UpdateVertex
233 //=======================================================================
234 void BOPTools_Tools::UpdateVertex (const TopoDS_Edge& aE,
235 const Standard_Real aT,
236 const TopoDS_Vertex& aV)
238 Standard_Real aTolV, aDist, aDTol=1.e-12, aFirst, aLast;
241 gp_Pnt aPv=BRep_Tool::Pnt(aV);
242 aTolV=BRep_Tool::Tolerance(aV);
244 Handle(Geom_Curve) aC3D=BRep_Tool::Curve(aE, aFirst, aLast);
246 aDist=aPv.Distance(aPc);
249 BB.UpdateVertex (aV, aDist+aDTol);
253 //=======================================================================
254 // function: UpdateVertex
256 //=======================================================================
257 void BOPTools_Tools::UpdateVertex (const IntTools_Curve& aC,
258 const Standard_Real aT,
259 const TopoDS_Vertex& aV)
261 Standard_Real aTolV, aDist, aDTol=1.e-12;
264 gp_Pnt aPv=BRep_Tool::Pnt(aV);
265 aTolV=BRep_Tool::Tolerance(aV);
267 Handle(Geom_Curve) aC3D=aC.Curve();
269 aDist=aPv.Distance(aPc);
272 BB.UpdateVertex (aV, aDist+aDTol);
275 //=======================================================================
276 // function: MakeSectEdge
278 //=======================================================================
279 void BOPTools_Tools::MakeSectEdge(const IntTools_Curve& aIC,
280 const TopoDS_Vertex& aV1,
281 const Standard_Real aP1,
282 const TopoDS_Vertex& aV2,
283 const Standard_Real aP2,
284 TopoDS_Edge& aNewEdge)
286 Handle(Geom_Curve) aC=aIC.Curve ();
288 BRepBuilderAPI_MakeEdge aMakeEdge(aC, aV1, aV2, aP1, aP2);
290 const TopoDS_Edge& aE=TopoDS::Edge(aMakeEdge.Shape());
292 // Range must be as it was !
294 aBB.Range (aE, aP1, aP2);
300 //=======================================================================
301 // function: MakeSplitEdge
303 //=======================================================================
304 void BOPTools_Tools::MakeSplitEdge(const TopoDS_Edge& aE,
305 const TopoDS_Vertex& aV1,
306 const Standard_Real aP1,
307 const TopoDS_Vertex& aV2,
308 const Standard_Real aP2,
309 TopoDS_Edge& aNewEdge)
311 Standard_Real f, l, aTol;
312 Handle(Geom_Curve) aC=BRep_Tool::Curve (aE, f, l);
313 aTol=BRep_Tool::Tolerance(aE);
315 // MakeEdge is used for chechking all input data only
316 BRepBuilderAPI_MakeEdge aMakeEdge(aC, aV1, aV2, aP1, aP2);
317 //ZZ const TopoDS_Edge& E1=TopoDS::Edge(aMakeEdge.Shape());
324 BB.Range(E, aP1, aP2);
325 BB.UpdateEdge(E, aTol);
329 //=======================================================================
330 // function: MakeNewVertex
332 //=======================================================================
333 void BOPTools_Tools::MakeNewVertex(const TopoDS_Vertex& aV1,
334 const TopoDS_Vertex& aV2,
335 TopoDS_Vertex& aNewVertex)
337 gp_Pnt aPnt1=BRep_Tool::Pnt(aV1);
338 Standard_Real aTol1=BRep_Tool::Tolerance(aV1);
340 gp_Pnt aPnt2=BRep_Tool::Pnt(aV2);
341 Standard_Real aTol2=BRep_Tool::Tolerance(aV2);
343 Standard_Real aMaxTol, aDist;
345 aDist=aPnt1.Distance(aPnt2);
346 aMaxTol=(aTol1>aTol2)? aTol1 : aTol2;
347 aMaxTol=aMaxTol+0.5*aDist;
349 const gp_XYZ& aXYZ1=aPnt1.XYZ();
350 const gp_XYZ& aXYZ2=aPnt2.XYZ();
351 gp_XYZ aNewXYZ=0.5*(aXYZ1+aXYZ2);
353 gp_Pnt aNewPnt(aNewXYZ);
355 aBB.MakeVertex (aNewVertex, aNewPnt, aMaxTol);
357 //=======================================================================
358 // function: MakeNewVertex
360 //=======================================================================
361 void BOPTools_Tools::MakeNewVertex(const gp_Pnt& aP,
362 const Standard_Real aTol,
363 TopoDS_Vertex& aNewVertex)
366 aBB.MakeVertex (aNewVertex, aP, aTol);
369 //=======================================================================
370 // function: MakeNewVertex
372 //=======================================================================
373 void BOPTools_Tools::MakeNewVertex(const TopoDS_Edge& aE1,
374 const Standard_Real aParm1,
375 const TopoDS_Edge& aE2,
376 const Standard_Real aParm2,
377 TopoDS_Vertex& aNewVertex)
379 Standard_Real aTol1, aTol2, aMaxTol, aDist;
382 PointOnEdge (aE1, aParm1, aPnt1);
383 PointOnEdge (aE2, aParm2, aPnt2);
385 aTol1=BRep_Tool::Tolerance(aE1);
386 aTol2=BRep_Tool::Tolerance(aE2);
388 aDist=aPnt1.Distance(aPnt2);
389 aMaxTol=(aTol1>aTol2)? aTol1 : aTol2;
390 aMaxTol=aMaxTol+0.5*aDist;
392 const gp_XYZ& aXYZ1=aPnt1.XYZ();
393 const gp_XYZ& aXYZ2=aPnt2.XYZ();
394 gp_XYZ aNewXYZ=0.5*(aXYZ1+aXYZ2);
396 gp_Pnt aNewPnt(aNewXYZ);
398 aBB.MakeVertex (aNewVertex, aNewPnt, aMaxTol);
400 //=======================================================================
401 // function: MakeNewVertex
403 //=======================================================================
404 void BOPTools_Tools::MakeNewVertex(const TopoDS_Edge& aE1,
405 const Standard_Real aParm1,
406 const TopoDS_Face& aF1,
407 TopoDS_Vertex& aNewVertex)
409 Standard_Real aTol1, aTol2, aMaxTol, delta=1.e-12;
412 PointOnEdge (aE1, aParm1, aPnt);
414 aTol1=BRep_Tool::Tolerance(aE1);
415 aTol2=BRep_Tool::Tolerance(aF1);
417 //aMaxTol=(aTol1>aTol2)? aTol1 : aTol2;
418 aMaxTol=aTol1+aTol2+delta;
421 aBB.MakeVertex (aNewVertex, aPnt, aMaxTol);
424 //=======================================================================
425 // function: PointOnEdge
427 //=======================================================================
428 void BOPTools_Tools::PointOnEdge(const TopoDS_Edge& aE,
429 const Standard_Real aParm,
433 Handle(Geom_Curve) C1=BRep_Tool::Curve(aE, f, l);
437 //=======================================================================
438 //function : CopySource
440 //=======================================================================
441 void BOPTools_Tools::CopySource(const TopoDS_Shape& aSS,
444 TopTools_IndexedDataMapOfShapeShape aMapSS;
445 CopySrc (aSS, aMapSS, aSD);
448 //=======================================================================
449 //function : MapShapes
451 //=======================================================================
452 void BOPTools_Tools::MapShapes(const TopoDS_Shape& aS,
453 TopTools_IndexedMapOfShape& aM)
456 TopoDS_Iterator anIt;
458 for (; anIt.More(); anIt.Next()) {
459 const TopoDS_Shape& aSx=anIt.Value();
460 BOPTools_Tools::MapShapes(aSx, aM);
464 //=======================================================================
465 //function : CorrectRange
467 //=======================================================================
468 void BOPTools_Tools::CorrectRange(const TopoDS_Edge& aE1,
469 const TopoDS_Edge& aE2,
470 const IntTools_Range& aSR,
471 IntTools_Range& aNewSR)
474 Standard_Real aRes, aTolE1, aTolE2, aTF, aTL, dT;
475 BRepAdaptor_Curve aBC;
476 GeomAbs_CurveType aCT;
482 //modified by NIZNHY-PKV Tue Feb 10 08:47:03 2009f
485 if (aCT==GeomAbs_Line) {
488 //modified by NIZNHY-PKV Tue Feb 10 08:47:06 2009t
490 dT=Precision::PConfusion();
494 //modified by NIZNHY-PKV Tue Feb 10 08:47:39 2009f
499 //modified by NIZNHY-PKV Tue Feb 10 08:47:43 2009t
501 aTolE1=BRep_Tool::Tolerance(aE1);
502 aTolE2=BRep_Tool::Tolerance(aE2);
505 aRes = 2.*(aTolE1 + aTolE2);
507 if (aCT==GeomAbs_BezierCurve ||
508 aCT==GeomAbs_BSplineCurve||
509 aCT==GeomAbs_OtherCurve) {
512 aBC.D1 (aTF, aP, aDer);
515 aBC.D1 (aTL, aP, aDer);
518 Standard_Real aMgn = aDer.Magnitude();
524 aRes = aBC.Resolution(aRes);
526 } // if (aCT==GeomAbs_BezierCurve||...
528 aRes = aBC.Resolution(aRes);
532 aNewSR.SetFirst (aTF+aRes);
535 aNewSR.SetLast (aTL-aRes);
538 if ((aNewSR.Last()-aNewSR.First()) < dT) {
541 //aNewSR=((aNewSR.Last()-aNewSR.First()) < dT) ? aSR : aNewSR;
545 //=======================================================================
546 //function : CorrectRange
548 //=======================================================================
549 void BOPTools_Tools::CorrectRange(const TopoDS_Edge& aE,
550 const TopoDS_Face& aF,
551 const IntTools_Range& aSR,
552 IntTools_Range& aNewSR)
555 Standard_Real aRes, aTolF, aTF, aTL, dT;
556 BRepAdaptor_Curve aBC;
557 GeomAbs_CurveType aCT;
563 dT=Precision::PConfusion();
570 aTolF=BRep_Tool::Tolerance(aF);
575 if (aCT==GeomAbs_BezierCurve ||
576 aCT==GeomAbs_BSplineCurve||
577 aCT==GeomAbs_OtherCurve) {
580 aBC.D1 (aTF, aP, aDer);
583 aBC.D1 (aTL, aP, aDer);
586 Standard_Real aMgn = aDer.Magnitude();
592 aRes = aBC.Resolution(aRes);
594 } // if (aCT==GeomAbs_BezierCurve||...
596 aRes = aBC.Resolution(aRes);
600 aNewSR.SetFirst (aTF+aRes);
603 aNewSR.SetLast (aTL-aRes);
606 if ((aNewSR.Last()-aNewSR.First()) < dT) {
612 //=======================================================================
615 //=======================================================================
616 void CopySrc(const TopoDS_Shape& E,
617 TopTools_IndexedDataMapOfShapeShape& aMapSS,
618 TopoDS_Shape& anEdge)
621 TopAbs_ShapeEnum aTT;
625 if (aMapSS.Contains(E)) {
626 anEdge=aMapSS.ChangeFromKey(E);
627 if (aTT==TopAbs_EDGE)
631 anEdge=E.EmptyCopied();
632 aMapSS.Add(E, anEdge);
635 aR=(Standard_Integer)aTT+1;
637 if (aR>TopAbs_VERTEX) {
641 Standard_Boolean free = anEdge.Free();
642 anEdge.Free(Standard_True);
644 aTT=(TopAbs_ShapeEnum) aR;
645 TopExp_Explorer anExpVertices(E, aTT);
646 for (; anExpVertices.More(); anExpVertices.Next()) {
647 const TopoDS_Shape& V=anExpVertices.Current();
648 TopoDS_Shape aVertex;
650 CopySrc (V, aMapSS, aVertex);
652 aVertex.Orientation(V.Orientation());
653 BB.Add(anEdge, aVertex);