0032768: Coding - get rid of unused headers [BopAlgo to BRepBuilderAPI]
[occt.git] / src / BOPTools / BOPTools_AlgoTools_1.cxx
CommitLineData
4e57c75e 1// Created by: Peter KURNEV
973c2be1 2// Copyright (c) 1999-2014 OPEN CASCADE SAS
4e57c75e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
4e57c75e 5//
d5f74e42 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
973c2be1 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.
4e57c75e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
4e57c75e 14
42cf5bc1 15
c26b5a34 16#include <Adaptor3d_CurveOnSurface.hxx>
42cf5bc1 17#include <BOPTools_AlgoTools.hxx>
1155d05a 18#include <BOPTools_Parallel.hxx>
42cf5bc1 19#include <BRep_Builder.hxx>
42cf5bc1 20#include <BRep_GCurve.hxx>
42cf5bc1 21#include <BRep_ListIteratorOfListOfPointRepresentation.hxx>
22#include <BRep_PointRepresentation.hxx>
23#include <BRep_TEdge.hxx>
24#include <BRep_TFace.hxx>
25#include <BRep_Tool.hxx>
26#include <BRep_TVertex.hxx>
24542bc0 27#include <BRepAdaptor_Curve.hxx>
42cf5bc1 28#include <BRepLib_CheckCurveOnSurface.hxx>
c9337e33 29#include <BRepLib_ValidateEdge.hxx>
42cf5bc1 30#include <BRepTools_WireExplorer.hxx>
31#include <Extrema_LocateExtPC.hxx>
32#include <Geom2d_Curve.hxx>
33#include <Geom2dAdaptor.hxx>
34#include <Geom2dAdaptor_Curve.hxx>
42cf5bc1 35#include <Geom2dInt_GInter.hxx>
4e57c75e 36#include <Geom_Curve.hxx>
4e57c75e 37#include <Geom_Plane.hxx>
c26b5a34 38#include <Geom_RectangularTrimmedSurface.hxx>
42cf5bc1 39#include <Geom_Surface.hxx>
40#include <Geom_TrimmedCurve.hxx>
4e57c75e 41#include <GeomAdaptor_Curve.hxx>
4e57c75e 42#include <GeomProjLib.hxx>
24542bc0 43#include <GCPnts_AbscissaPoint.hxx>
42cf5bc1 44#include <gp_Pnt.hxx>
45#include <gp_Pnt2d.hxx>
c26b5a34 46#include <IntRes2d_Domain.hxx>
47#include <IntRes2d_IntersectionPoint.hxx>
0d0481c7 48#include <IntRes2d_IntersectionSegment.hxx>
42cf5bc1 49#include <IntTools_Context.hxx>
1155d05a 50#include <NCollection_Vector.hxx>
42cf5bc1 51#include <ProjLib_ProjectedCurve.hxx>
52#include <TopExp.hxx>
53#include <TopExp_Explorer.hxx>
c26b5a34 54#include <TopLoc_Location.hxx>
c26b5a34 55#include <TopoDS.hxx>
56#include <TopoDS_Edge.hxx>
c26b5a34 57#include <TopoDS_Face.hxx>
4e57c75e 58#include <TopoDS_Iterator.hxx>
42cf5bc1 59#include <TopoDS_Shape.hxx>
42cf5bc1 60#include <TopoDS_Vertex.hxx>
42cf5bc1 61#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
62#include <TopTools_IndexedMapOfShape.hxx>
42cf5bc1 63#include <TopTools_ListOfShape.hxx>
64
0d0481c7 65
4e57c75e 66static
67 void CheckEdge (const TopoDS_Edge& E,
3510db62 68 const Standard_Real aMaxTol,
1155d05a 69 const TopTools_IndexedMapOfShape& aMapToAvoid);
4e57c75e 70static
71 void CorrectEdgeTolerance (const TopoDS_Edge& myShape,
72 const TopoDS_Face& S,
3510db62 73 const Standard_Real aMaxTol,
1155d05a 74 const TopTools_IndexedMapOfShape& aMapToAvoid);
4e57c75e 75
76static
3510db62 77 void CorrectVertexTolerance(const TopoDS_Edge& aE,
1155d05a 78 const TopTools_IndexedMapOfShape& aMapToAvoid);
4e57c75e 79
80static
3510db62 81 void CorrectWires(const TopoDS_Face& aF,
1155d05a 82 const TopTools_IndexedMapOfShape& aMapToAvoid);
4e57c75e 83
c26b5a34 84
85
86static
3510db62 87 void UpdateEdges(const TopoDS_Face& aF,
1155d05a 88 const TopTools_IndexedMapOfShape& aMapToAvoid);
3510db62 89
90static
91 void UpdateShape(const TopoDS_Shape& aS,
92 const Standard_Real aTol,
1155d05a 93 const TopTools_IndexedMapOfShape& aMapToAvoid);
c26b5a34 94
c26b5a34 95//=======================================================================
96//class : BOPTools_CPC
97//purpose :
98//=======================================================================
99class BOPTools_CPC {
100 public:
101 BOPTools_CPC()
3510db62 102 : myMaxTol(1.e-7), mypMapToAvoid(0L) {
c26b5a34 103 }
104 //
105 ~BOPTools_CPC() {
106 }
107 //
108 void SetEdge(const TopoDS_Edge& aE) {
109 myEdge=aE;
110 }
111 //
112 const TopoDS_Edge& Edge()const {
113 return myEdge;
114 }
115 //
116 void SetMaxTol(const Standard_Real aMaxTol) {
117 myMaxTol=aMaxTol;
118 }
119 //
120 Standard_Real MaxTol()const {
121 return myMaxTol;
122 }
123 //
1155d05a 124 void SetMapToAvoid(const TopTools_IndexedMapOfShape& aMapToAvoid) {
3510db62 125 mypMapToAvoid = &aMapToAvoid;
126 }
127 //
c26b5a34 128 void Perform() {
3510db62 129 Standard_ProgramError_Raise_if(!mypMapToAvoid, "mypMapToAvoid is null");
130 CheckEdge(myEdge, myMaxTol, *mypMapToAvoid);
c26b5a34 131 }
132
133 protected:
134 Standard_Real myMaxTol;
135 TopoDS_Edge myEdge;
1155d05a 136 const TopTools_IndexedMapOfShape* mypMapToAvoid;
c26b5a34 137};
138//
139//=======================================================================
1155d05a 140typedef NCollection_Vector<BOPTools_CPC> BOPTools_VectorOfCPC;
fc867b96 141
c26b5a34 142//=======================================================================
143//class : BOPTools_CWT
144//purpose :
145//=======================================================================
146class BOPTools_CWT {
147 public:
3510db62 148 BOPTools_CWT() : mypMapToAvoid(0L) {
c26b5a34 149 }
150 //
151 ~BOPTools_CWT() {
152 }
153 //
154 void SetFace(const TopoDS_Face& aF) {
155 myFace=aF;
156 }
157 //
1155d05a 158 void SetMapToAvoid(const TopTools_IndexedMapOfShape& aMapToAvoid) {
3510db62 159 mypMapToAvoid = &aMapToAvoid;
160 }
161 //
c26b5a34 162 void Perform() {
3510db62 163 Standard_ProgramError_Raise_if(!mypMapToAvoid, "mypMapToAvoid is null");
164 CorrectWires(myFace, *mypMapToAvoid);
c26b5a34 165 }
166 //
167 protected:
168 TopoDS_Face myFace;
1155d05a 169 const TopTools_IndexedMapOfShape* mypMapToAvoid;
c26b5a34 170};
171//=======================================================================
1155d05a 172typedef NCollection_Vector<BOPTools_CWT> BOPTools_VectorOfCWT;
fc867b96 173
c26b5a34 174//=======================================================================
175//class : BOPTools_CDT
176//purpose :
177//=======================================================================
178class BOPTools_CDT {
179 public:
180 BOPTools_CDT()
3510db62 181 : myMaxTol(1.e-7), mypMapToAvoid(0L) {
c26b5a34 182 }
183 //
184 ~BOPTools_CDT() {
185 }
186 //
187 void SetEdge(const TopoDS_Edge& aE) {
188 myEdge=aE;
189 }
190 //
191 void SetFace(const TopoDS_Face& aF) {
192 myFace=aF;
193 }
194 //
195 void SetMaxTol(const Standard_Real aMaxTol) {
196 myMaxTol=aMaxTol;
197 }
198 //
1155d05a 199 void SetMapToAvoid(const TopTools_IndexedMapOfShape& aMapToAvoid) {
3510db62 200 mypMapToAvoid = &aMapToAvoid;
201 }
202 //
c26b5a34 203 void Perform() {
3510db62 204 Standard_ProgramError_Raise_if(!mypMapToAvoid, "mypMapToAvoid is null");
205 CorrectEdgeTolerance (myEdge, myFace, myMaxTol, *mypMapToAvoid);
c26b5a34 206 }
207 //
208 protected:
209 Standard_Real myMaxTol;
210 TopoDS_Edge myEdge;
211 TopoDS_Face myFace;
1155d05a 212 const TopTools_IndexedMapOfShape* mypMapToAvoid;
c26b5a34 213};
214//=======================================================================
1155d05a 215typedef NCollection_Vector<BOPTools_CDT> BOPTools_VectorOfCDT;
fc867b96 216
c26b5a34 217//=======================================================================
218//class : BOPTools_CVT
219//purpose :
220//=======================================================================
221class BOPTools_CVT {
222 public:
3510db62 223 BOPTools_CVT() : mypMapToAvoid(0L) {
c26b5a34 224 }
225 //
226 ~BOPTools_CVT() {
227 }
228 //
229 void SetEdge(const TopoDS_Edge& aE) {
230 myEdge=aE;
231 }
232 //
1155d05a 233 void SetMapToAvoid(const TopTools_IndexedMapOfShape& aMapToAvoid) {
3510db62 234 mypMapToAvoid = &aMapToAvoid;
235 }
236 //
c26b5a34 237 void Perform() {
3510db62 238 Standard_ProgramError_Raise_if(!mypMapToAvoid, "mypMapToAvoid is null");
239 CorrectVertexTolerance(myEdge, *mypMapToAvoid);
c26b5a34 240 }
241 //
242 protected:
243 TopoDS_Edge myEdge;
1155d05a 244 const TopTools_IndexedMapOfShape* mypMapToAvoid;
c26b5a34 245};
246//
247//=======================================================================
1155d05a 248typedef NCollection_Vector<BOPTools_CVT> BOPTools_VectorOfCVT;
fc867b96 249
c26b5a34 250//=======================================================================
251//class : BOPTools_CET
252//purpose :
253//=======================================================================
254class BOPTools_CET {
255 public:
3510db62 256 BOPTools_CET() : mypMapToAvoid(0L) {
c26b5a34 257 }
258 //
259 ~BOPTools_CET() {
260 }
261 //
262 void SetFace(const TopoDS_Face& aF) {
263 myFace=aF;
264 }
265 //
1155d05a 266 void SetMapToAvoid(const TopTools_IndexedMapOfShape& aMapToAvoid) {
3510db62 267 mypMapToAvoid = &aMapToAvoid;
268 }
269 //
c26b5a34 270 void Perform() {
3510db62 271 Standard_ProgramError_Raise_if(!mypMapToAvoid, "mypMapToAvoid is null");
272 UpdateEdges(myFace, *mypMapToAvoid);
c26b5a34 273 }
274 //
275 protected:
276 TopoDS_Face myFace;
1155d05a 277 const TopTools_IndexedMapOfShape* mypMapToAvoid;
c26b5a34 278};
279//=======================================================================
1155d05a 280typedef NCollection_Vector<BOPTools_CET> BOPTools_VectorOfCET;
fc867b96 281
4e57c75e 282//=======================================================================
283// Function : CorrectTolerances
284// purpose :
285//=======================================================================
c26b5a34 286void BOPTools_AlgoTools::CorrectTolerances
287 (const TopoDS_Shape& aShape,
1155d05a 288 const TopTools_IndexedMapOfShape& aMapToAvoid,
c26b5a34 289 const Standard_Real aMaxTol,
290 const Standard_Boolean bRunParallel)
4e57c75e 291{
3510db62 292 BOPTools_AlgoTools::CorrectPointOnCurve(aShape, aMapToAvoid, aMaxTol, bRunParallel);
293 BOPTools_AlgoTools::CorrectCurveOnSurface(aShape, aMapToAvoid, aMaxTol, bRunParallel);
4e57c75e 294}
c26b5a34 295//
4e57c75e 296//=======================================================================
297// Function : CorrectPointOnCurve
298// purpose :
299//=======================================================================
c26b5a34 300void BOPTools_AlgoTools::CorrectPointOnCurve
301 (const TopoDS_Shape& aS,
1155d05a 302 const TopTools_IndexedMapOfShape& aMapToAvoid,
c26b5a34 303 const Standard_Real aMaxTol,
304 const Standard_Boolean bRunParallel)
4e57c75e 305{
c26b5a34 306 TopExp_Explorer aExp;
307 BOPTools_VectorOfCPC aVCPC;
308 //
309 aExp.Init(aS, TopAbs_EDGE);
310 for(; aExp.More(); aExp.Next()) {
311 const TopoDS_Edge& aE=*((TopoDS_Edge*)&aExp.Current());
1155d05a 312 BOPTools_CPC& aCPC=aVCPC.Appended();
c26b5a34 313 aCPC.SetEdge(aE);
314 aCPC.SetMaxTol(aMaxTol);
3510db62 315 aCPC.SetMapToAvoid(aMapToAvoid);
c26b5a34 316 }
317 //
318 //======================================================
fc867b96 319 BOPTools_Parallel::Perform (bRunParallel, aVCPC);
c26b5a34 320 //======================================================
4e57c75e 321}
4e57c75e 322//=======================================================================
323// Function : CorrectCurveOnSurface
324// purpose :
325//=======================================================================
c26b5a34 326void BOPTools_AlgoTools::CorrectCurveOnSurface
327 (const TopoDS_Shape& aS,
1155d05a 328 const TopTools_IndexedMapOfShape& aMapToAvoid,
c26b5a34 329 const Standard_Real aMaxTol,
330 const Standard_Boolean bRunParallel)
4e57c75e 331{
c26b5a34 332 TopExp_Explorer aExpF, aExpE;
333 BOPTools_VectorOfCWT aVCWT;
334 BOPTools_VectorOfCDT aVCDT;
335 //
336 aExpF.Init(aS, TopAbs_FACE);
337 for (; aExpF.More(); aExpF.Next()) {
338 const TopoDS_Face& aF=*((TopoDS_Face*)&aExpF.Current());
4e57c75e 339 //
1155d05a 340 BOPTools_CWT& aCWT=aVCWT.Appended();
c26b5a34 341 aCWT.SetFace(aF);
3510db62 342 aCWT.SetMapToAvoid(aMapToAvoid);
4e57c75e 343 //
c26b5a34 344 aExpE.Init(aF, TopAbs_EDGE);
345 for (; aExpE.More(); aExpE.Next()) {
346 const TopoDS_Edge& aE=*((TopoDS_Edge*)&aExpE.Current());
347 //
1155d05a 348 BOPTools_CDT& aCDT=aVCDT.Appended();
c26b5a34 349 aCDT.SetEdge(aE);
350 aCDT.SetFace(aF);
351 aCDT.SetMaxTol(aMaxTol);
3510db62 352 aCDT.SetMapToAvoid(aMapToAvoid);
4e57c75e 353 }
354 }
c26b5a34 355 //
356 //======================================================
fc867b96 357 BOPTools_Parallel::Perform (bRunParallel, aVCWT);
c26b5a34 358 //======================================================
fc867b96 359 BOPTools_Parallel::Perform (bRunParallel, aVCDT);
c26b5a34 360 //======================================================
361}
362//=======================================================================
363// Function : CorrectShapeTolerances
364// purpose :
365//=======================================================================
366void BOPTools_AlgoTools::CorrectShapeTolerances
367 (const TopoDS_Shape& aShape,
1155d05a 368 const TopTools_IndexedMapOfShape& aMapToAvoid,
c26b5a34 369 const Standard_Boolean bRunParallel)
370{
371 TopExp_Explorer aExp;
372 BOPTools_VectorOfCVT aVCVT;
373 BOPTools_VectorOfCET aVCET;
374 //
375 aExp.Init(aShape, TopAbs_EDGE);
376 for (; aExp.More(); aExp.Next()) {
377 const TopoDS_Edge& aE = *(TopoDS_Edge*)&aExp.Current();
1155d05a 378 BOPTools_CVT& aCVT=aVCVT.Appended();
c26b5a34 379 aCVT.SetEdge(aE);
3510db62 380 aCVT.SetMapToAvoid(aMapToAvoid);
c26b5a34 381 }
382 //
383 //======================================================
fc867b96 384 BOPTools_Parallel::Perform (bRunParallel, aVCVT);
c26b5a34 385 //======================================================
386 //
387 aExp.Init(aShape, TopAbs_FACE);
388 for (; aExp.More(); aExp.Next()) {
389 const TopoDS_Face& aF = *(TopoDS_Face*)&aExp.Current();
1155d05a 390 BOPTools_CET& aCET=aVCET.Appended();
c26b5a34 391 aCET.SetFace(aF);
3510db62 392 aCET.SetMapToAvoid(aMapToAvoid);
c26b5a34 393 }
394 //
395 //======================================================
fc867b96 396 BOPTools_Parallel::Perform (bRunParallel, aVCET);
c26b5a34 397 //======================================================
398}
399//
400//=======================================================================
401// Function : CheckEdge
402// purpose : Correct tolerances for Vertices on Edge
403//=======================================================================
404void CheckEdge (const TopoDS_Edge& Ed,
3510db62 405 const Standard_Real aMaxTol,
1155d05a 406 const TopTools_IndexedMapOfShape& aMapToAvoid)
c26b5a34 407{
98b37659 408 TopoDS_Edge aE = Ed;
c26b5a34 409 aE.Orientation(TopAbs_FORWARD);
98b37659 410 Standard_Real aTolE = BRep_Tool::Tolerance(aE);
c26b5a34 411 //
412 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&aE.TShape());
413 //
98b37659 414 const TopLoc_Location& Eloc = aE.Location();
415
416 TopoDS_Iterator aItS(aE);
c26b5a34 417 for (; aItS.More(); aItS.Next()) {
98b37659 418 const TopoDS_Vertex& aV= TopoDS::Vertex(aItS.Value());
c26b5a34 419 //
420 Handle(BRep_TVertex)& TV=*((Handle(BRep_TVertex)*)&aV.TShape());
421 const gp_Pnt& aPV = TV->Pnt();
422 //
98b37659 423 Standard_Real aTol=BRep_Tool::Tolerance(aV);
c26b5a34 424 aTol=Max(aTol, aTolE);
98b37659 425 Standard_Real dd=0.1*aTol;
c26b5a34 426 aTol*=aTol;
427 //
98b37659 428 BRep_ListIteratorOfListOfCurveRepresentation aItCR(TE->Curves());
c26b5a34 429 while (aItCR.More()) {
430 const Handle(BRep_CurveRepresentation)& aCR = aItCR.Value();
c26b5a34 431 //
432 if (aCR->IsCurve3D()) {
433 const Handle(Geom_Curve)& aC = aCR->Curve3D();
434 if (!aC.IsNull()) {
98b37659 435 TopLoc_Location L = (Eloc * aCR->Location()).Predivided(aV.Location());
436 BRep_ListIteratorOfListOfPointRepresentation aItPR(TV->Points());
c26b5a34 437 while (aItPR.More()) {
438 const Handle(BRep_PointRepresentation)& aPR=aItPR.Value();
439 if (aPR->IsPointOnCurve(aC, L)) {
98b37659 440 gp_Pnt aPC = aC->Value(aPR->Parameter());
c26b5a34 441 aPC.Transform(L.Transformation());
98b37659 442 Standard_Real aD2=aPV.SquareDistance(aPC);
c26b5a34 443 if (aD2 > aTol) {
98b37659 444 Standard_Real aNewTolerance=sqrt(aD2)+dd;
c26b5a34 445 if (aNewTolerance<aMaxTol)
3510db62 446 UpdateShape(aV, aNewTolerance, aMapToAvoid);
c26b5a34 447 }
448 }
449 aItPR.Next();
450 }
451 //
98b37659 452 TopAbs_Orientation aOrV=aV.Orientation();
c26b5a34 453 if (aOrV==TopAbs_FORWARD || aOrV==TopAbs_REVERSED) {
c5f3a425 454 Handle(BRep_GCurve) aGC (Handle(BRep_GCurve)::DownCast (aCR));
98b37659 455 gp_Pnt aPC;
c26b5a34 456 if (aOrV==TopAbs_FORWARD) {
457 aPC=aC->Value(aGC->First());
458 }
459 else {
460 aPC=aC->Value(aGC->Last());
461 }
462 aPC.Transform(L.Transformation());
463 //
98b37659 464 Standard_Real aD2=aPV.SquareDistance(aPC);
c26b5a34 465 if (aD2 > aTol) {
98b37659 466 Standard_Real aNewTolerance=sqrt(aD2)+dd;
c26b5a34 467 if (aNewTolerance<aMaxTol)
3510db62 468 UpdateShape(aV, aNewTolerance, aMapToAvoid);
c26b5a34 469 }
470 }
471 }
472 }
473 aItCR.Next();
474 }// while (itcr.More()) {
475 } // for (; aVExp.More(); aVExp.Next()) {
4e57c75e 476}
0d0481c7 477
24542bc0 478//=======================================================================
479// Function : MapEdgeLength
480// purpose : Compute edge length and cache it in the map
481//=======================================================================
482static Standard_Real MapEdgeLength(const TopoDS_Edge& theEdge,
483 NCollection_DataMap<TopoDS_Shape, Standard_Real>& theMapEdgeLen)
484{
485 const Standard_Real* pLen = theMapEdgeLen.Seek(theEdge);
486 if (!pLen)
487 {
488 Standard_Real aLen = 0.;
489 if (!BRep_Tool::Degenerated(theEdge))
490 {
491 BRepAdaptor_Curve aCurve(theEdge);
492 aLen = GCPnts_AbscissaPoint::Length(aCurve);
493 }
494 pLen = theMapEdgeLen.Bound(theEdge, aLen);
495 }
496 return *pLen;
497}
98b37659 498
499//=======================================================================
500// Function : EdgeData
501// purpose : Structure to store edge data
502//=======================================================================
503namespace {
504 struct EdgeData {
505 const TopoDS_Edge* Edge; // Edge
506 Standard_Real VParameter; // Parameter of the vertex on the edge
c08fd127 507 Standard_Boolean IsClosed; // Closed flag of the edge
98b37659 508 Geom2dAdaptor_Curve GAdaptor; // 2D adaptor for PCurve of the edge on the face
509 Standard_Real First; // First parameter in the range
510 Standard_Real Last; // Last parameter in the rage
511 };
512}
b4109929 513//=======================================================================
514// Function : IntersectCurves2d
515// purpose : Intersect 2d curves of edges
516//=======================================================================
98b37659 517static
518 Standard_Real IntersectCurves2d(const TopoDS_Vertex& theV,
519 const Handle(Geom_Surface)& theS,
520 const EdgeData& theEData1,
521 const EdgeData& theEData2,
522 NCollection_DataMap<TopoDS_Shape, Standard_Real>& theMapEdgeLen)
b4109929 523{
0d0481c7 524 Geom2dInt_GInter anInter;
98b37659 525 // Range of the first edge
526 Standard_Real aT11 = theEData1.First;
527 Standard_Real aT12 = theEData1.Last;
528 // Range of the second edge
529 Standard_Real aT21 = theEData2.First;
530 Standard_Real aT22 = theEData2.Last;
531
532 Standard_Real aMaxDist = 0.;
533 Standard_Real aTol2d = 1.e-10;
b4109929 534 //
98b37659 535 IntRes2d_Domain aDom1(theEData1.GAdaptor.Value(aT11), aT11, aTol2d,
536 theEData1.GAdaptor.Value(aT12), aT12, aTol2d);
537 IntRes2d_Domain aDom2(theEData2.GAdaptor.Value(aT21), aT21, aTol2d,
538 theEData2.GAdaptor.Value(aT22), aT22, aTol2d);
b4109929 539 //
98b37659 540 anInter.Perform(theEData1.GAdaptor, aDom1, theEData2.GAdaptor, aDom2, aTol2d, aTol2d);
24542bc0 541 if (!anInter.IsDone() || (!anInter.NbSegments() && !anInter.NbPoints())) {
0d0481c7 542 return aMaxDist;
543 }
544 //
545 Standard_Real aT1, aT2, aTint1, aTint2, aHalfR1, aHalfR2, aDist;
546 Standard_Integer i, aNb;
547 gp_Pnt aP, aPV;
548 gp_Pnt2d aP2d;
549 NCollection_List<IntRes2d_IntersectionPoint> aLP;
550 NCollection_List<IntRes2d_IntersectionPoint>::Iterator aItLP;
551 //
552 aPV = BRep_Tool::Pnt(theV);
98b37659 553 aT1 = theEData1.VParameter;
554 aT2 = theEData2.VParameter;
0d0481c7 555 //
556 aHalfR1 = (aT12 - aT11) / 2.;
557 aHalfR2 = (aT22 - aT21) / 2.;
558 //
559 aDist = 0.;
560 //
561 aNb = anInter.NbSegments();
562 for (i = 1; i <= aNb; ++i) {
563 const IntRes2d_IntersectionSegment& aSeg = anInter.Segment(i);
564 aLP.Append(aSeg.FirstPoint());
565 aLP.Append(aSeg.LastPoint());
566 }
567 //
568 aNb = anInter.NbPoints();
569 for (i = 1; i <= aNb; ++i) {
570 const IntRes2d_IntersectionPoint& aPnt = anInter.Point(i);
571 aLP.Append(aPnt);
572 }
573 //
24542bc0 574 // evaluate the length of the smallest edge, so that not to return too large distance
98b37659 575 Standard_Real aLen1 = MapEdgeLength(*theEData1.Edge, theMapEdgeLen);
576 Standard_Real aLen2 = MapEdgeLength(*theEData1.Edge, theMapEdgeLen);
24542bc0 577 const Standard_Real MaxEdgePartCoveredByVertex = 0.3;
578 Standard_Real aMaxThresDist = Min(aLen1, aLen2) * MaxEdgePartCoveredByVertex;
579 aMaxThresDist *= aMaxThresDist;
0d0481c7 580 aItLP.Initialize(aLP);
581 for (; aItLP.More(); aItLP.Next()) {
582 const IntRes2d_IntersectionPoint& aPnt = aItLP.Value();
583 //
584 aTint1 = aPnt.ParamOnFirst();
585 aTint2 = aPnt.ParamOnSecond();
586 //
587 if ((aTint1 < aT11 || aTint1 > aT12) ||
588 (aTint2 < aT21 || aTint2 > aT22)) {
589 // out of range;
590 continue;
b4109929 591 }
0d0481c7 592 //
c08fd127 593 if ((!theEData1.IsClosed && Abs (aTint1 - aT1) > aHalfR1) ||
594 (!theEData2.IsClosed && Abs (aTint2 - aT2) > aHalfR2)) {
595 // intersection is on the other end of the edge
0d0481c7 596 continue;
597 }
598 //
599 aP2d = aPnt.Value();
600 theS->D0(aP2d.X(), aP2d.Y(), aP);
601 aDist = aPV.SquareDistance(aP);
24542bc0 602 if (aDist > aMaxDist && aDist < aMaxThresDist) {
0d0481c7 603 aMaxDist = aDist;
b4109929 604 }
605 }
0d0481c7 606 //
607 return aMaxDist;
b4109929 608}
98b37659 609//=======================================================================
610// Function : CorrectWires
611// purpose :
612//=======================================================================
613void CorrectWires(const TopoDS_Face& aFx,
1155d05a 614 const TopTools_IndexedMapOfShape& aMapToAvoid)
98b37659 615{
616 Standard_Integer i, aNbV;
c08fd127 617 Standard_Real aTol, aTol2, aD2, aD2max, aT1, aT2;
98b37659 618 gp_Pnt aP, aPV;
619 gp_Pnt2d aP2D;
620 TopoDS_Face aF;
621 TopTools_IndexedDataMapOfShapeListOfShape aMVE;
622 TopTools_ListIteratorOfListOfShape aIt;
623 //
624 aF=aFx;
625 aF.Orientation(TopAbs_FORWARD);
626 const Handle(Geom_Surface)& aS=BRep_Tool::Surface(aFx);
c08fd127 627
628 TopExp::MapShapesAndUniqueAncestors (aF, TopAbs_VERTEX, TopAbs_EDGE, aMVE, Standard_True);
629
98b37659 630 NCollection_DataMap<TopoDS_Shape, Standard_Real> aMapEdgeLen;
631 aNbV=aMVE.Extent();
632 for (i=1; i<=aNbV; ++i) {
633 const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&aMVE.FindKey(i));
634 aPV=BRep_Tool::Pnt(aV);
635 aTol=BRep_Tool::Tolerance(aV);
636 aTol2=aTol*aTol;
637 //
638 aD2max=-1.;
639 // Save edge's data to avoid its recalculation during intersection of 2d curves
640 NCollection_List<EdgeData> aLEPars;
641 const TopTools_ListOfShape& aLE=aMVE.FindFromIndex(i);
642 aIt.Initialize(aLE);
643 for (; aIt.More(); aIt.Next()) {
644 const TopoDS_Edge& aE=*(TopoDS_Edge*)(&aIt.Value());
645 const Handle(Geom2d_Curve)& aC2D=
646 BRep_Tool::CurveOnSurface(aE, aF, aT1, aT2);
c08fd127 647 Standard_Real aT = BRep_Tool::Parameter (aV, aE);
648 Standard_Boolean isClosed = Standard_False;
649 {
650 TopoDS_Vertex aV1, aV2;
651 TopExp::Vertices (aE, aV1, aV2);
652 isClosed = aV1.IsSame (aV2);
653 }
98b37659 654 //
655 aC2D->D0(aT, aP2D);
656 aS->D0(aP2D.X(), aP2D.Y(), aP);
657 aD2=aPV.SquareDistance(aP);
658 if (aD2>aD2max) {
659 aD2max=aD2;
660 }
c08fd127 661 EdgeData anEData = {&aE, aT, isClosed, Geom2dAdaptor_Curve(aC2D), aT1, aT2};
98b37659 662 aLEPars.Append(anEData);
663 }
664 //
665 //check wires on self interference by intersecting 2d curves of the edges
666 NCollection_List<EdgeData>::Iterator aItE1(aLEPars);
667 for (; aItE1.More(); aItE1.Next()) {
668 const EdgeData& aEData1 = aItE1.Value();
669 const TopoDS_Shape& aE1 = *aEData1.Edge;
670
671 NCollection_List<EdgeData>::Iterator aItE2 = aItE1;
672 for (aItE2.Next(); aItE2.More(); aItE2.Next()) {
673 const EdgeData& aEData2 = aItE2.Value();
674 const TopoDS_Shape& aE2 = *aEData2.Edge;
675
676 if (aE1.IsSame(aE2))
677 continue;
678
679 aD2 = IntersectCurves2d(aV, aS, aEData1, aEData2, aMapEdgeLen);
680 if (aD2 > aD2max) {
681 aD2max = aD2;
682 }
683 }
684 }
685 //
686 if (aD2max>aTol2) {
687 aTol = 1.01 * sqrt(aD2max);
688 UpdateShape(aV, aTol, aMapToAvoid);
689 }
690 }// for (i=1; i<=aNbV; ++i) {
691}
692
4e57c75e 693//=======================================================================
694// Function : CorrectEdgeTolerance
695// purpose : Correct tolerances for Edge
696//=======================================================================
697void CorrectEdgeTolerance (const TopoDS_Edge& myShape,
698 const TopoDS_Face& S,
3510db62 699 const Standard_Real aMaxTol,
1155d05a 700 const TopTools_IndexedMapOfShape& aMapToAvoid)
4e57c75e 701{
702 //
703 // 1. Minimum of conditions to Perform
704 Handle (BRep_CurveRepresentation) myCref;
c22b52d6 705 Handle (Adaptor3d_Curve) myHCurve;
4e57c75e 706
707 myCref.Nullify();
708
709 Handle(BRep_TEdge)& TEx = *((Handle(BRep_TEdge)*)&myShape.TShape());
710 BRep_ListIteratorOfListOfCurveRepresentation itcrx(TEx->Curves());
711 Standard_Boolean Degenerated, SameParameterx, SameRangex;
712
713 Standard_Integer unique = 0;
714
715 Degenerated = TEx->Degenerated();
716 SameParameterx = TEx->SameParameter();
717 SameRangex = TEx->SameRange();
718
719 if (!SameRangex && SameParameterx) {
720 return;
721 }
722
723 Handle(Geom_Curve) C3d;
724 while (itcrx.More()) {
725 const Handle(BRep_CurveRepresentation)& cr = itcrx.Value();
726 if (cr->IsCurve3D()) {
727 unique++;
728 if (myCref.IsNull() && !cr->Curve3D().IsNull()) {
729 myCref = cr;
730 }
731 }
732 itcrx.Next();
733 }
734
735 if (unique==0) {
736 return;//...No3DCurve
737 }
738 if (unique>1) {
739 return;//...Multiple3DCurve;
740 }
741
742 if (myCref.IsNull() && !Degenerated) {
743 itcrx.Initialize(TEx->Curves());
744 while (itcrx.More()) {
745 const Handle(BRep_CurveRepresentation)& cr = itcrx.Value();
746 if (cr->IsCurveOnSurface()) {
747 myCref = cr;
748 break;
749 }
750 itcrx.Next();
751 }
752 }
753
754 else if (!myCref.IsNull() && Degenerated){
755 return ;//...InvalidDegeneratedFlag;
756 }
757
758 if (!myCref.IsNull()) {
c5f3a425 759 Handle(BRep_GCurve) GCref (Handle(BRep_GCurve)::DownCast (myCref));
4e57c75e 760 Standard_Real First,Last;
761 GCref->Range(First,Last);
762 if (Last<=First) {
763 myCref.Nullify();
764 return ;//InvalidRange;
765 }
766
767 else {
768 if (myCref->IsCurve3D()) {
769 Handle(Geom_Curve) C3dx = Handle(Geom_Curve)::DownCast
c26b5a34 770 (myCref->Curve3D()->Transformed
771 (myCref->Location().Transformation()));
4e57c75e 772 GeomAdaptor_Curve GAC3d(C3dx, First, Last);
c22b52d6 773 myHCurve = new GeomAdaptor_Curve(GAC3d);
4e57c75e 774 }
775 else { // curve on surface
776 Handle(Geom_Surface) Sref = myCref->Surface();
c26b5a34 777 Sref = Handle(Geom_Surface)::
778 DownCast(Sref->Transformed(myCref->Location().Transformation()));
4e57c75e 779 const Handle(Geom2d_Curve)& PCref = myCref->PCurve();
c22b52d6 780 Handle(GeomAdaptor_Surface) GAHSref =
781 new GeomAdaptor_Surface(Sref);
782 Handle(Geom2dAdaptor_Curve) GHPCref =
783 new Geom2dAdaptor_Curve(PCref, First, Last);
4e57c75e 784 Adaptor3d_CurveOnSurface ACSref(GHPCref,GAHSref);
c22b52d6 785 myHCurve = new Adaptor3d_CurveOnSurface(ACSref);
4e57c75e 786 }
787 }
788 }
789
790 //===============================================
791 // 2. Tolerances in InContext
792 {
793 if (myCref.IsNull())
794 return;
4e57c75e 795
796 Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&myShape.TShape());
797 Standard_Real Tol = BRep_Tool::Tolerance(TopoDS::Edge(myShape));
798 Standard_Real aNewTol=Tol;
799
800 Standard_Boolean SameParameter = TE->SameParameter();
801 Standard_Boolean SameRange = TE->SameRange();
802 Standard_Real First = myHCurve->FirstParameter();
803 Standard_Real Last = myHCurve->LastParameter();
4e57c75e 804
805 Handle(BRep_TFace)& TF = *((Handle(BRep_TFace)*) &S.TShape());
806 const TopLoc_Location& Floc = S.Location();
807 const TopLoc_Location& TFloc = TF->Location();
808 const Handle(Geom_Surface)& Su = TF->Surface();
809 TopLoc_Location L = (Floc * TFloc).Predivided(myShape.Location());
4e57c75e 810 Standard_Boolean pcurvefound = Standard_False;
811
812 BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
813 while (itcr.More()) {
814 const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
815 if (cr != myCref && cr->IsCurveOnSurface(Su,L)) {
816 pcurvefound = Standard_True;
c5f3a425 817 Handle(BRep_GCurve) GC (Handle(BRep_GCurve)::DownCast (cr));
4e57c75e 818 Standard_Real f,l;
819 GC->Range(f,l);
820 if (SameRange && (f != First || l != Last)) {
d3f26155 821 return ;//BRepCheck_InvalidSameRangeFlag;
4e57c75e 822 }
c26b5a34 823
4e57c75e 824 Handle(Geom_Surface) Sb = cr->Surface();
c26b5a34 825 Sb = Handle(Geom_Surface)::
826 DownCast (Su->Transformed(L.Transformation()));
4e57c75e 827 Handle(Geom2d_Curve) PC = cr->PCurve();
c22b52d6 828 Handle(GeomAdaptor_Surface) GAHS =
829 new GeomAdaptor_Surface(Sb);
830 Handle(Geom2dAdaptor_Curve) GHPC =
831 new Geom2dAdaptor_Curve(PC,f,l);
c9337e33 832 Handle(Adaptor3d_CurveOnSurface) ACS = new Adaptor3d_CurveOnSurface(GHPC,GAHS);
833
834 BRepLib_ValidateEdge aValidateEdge(myHCurve, ACS, SameParameter);
835 aValidateEdge.Process();
836 aValidateEdge.UpdateTolerance(aNewTol);
837 if (aValidateEdge.IsDone() && !aValidateEdge.CheckTolerance(Tol))
838 {
4e57c75e 839 if (aNewTol<aMaxTol) {
c9337e33 840 UpdateShape(myShape, aNewTol, aMapToAvoid);
3510db62 841 CorrectVertexTolerance(myShape, aMapToAvoid);
4e57c75e 842 }
843 }
844
845 if (cr->IsCurveOnClosedSurface()) {
c22b52d6 846 GHPC->Load(cr->PCurve2(),f,l); // same bounds
c9337e33 847 ACS->Load(GHPC, GAHS); // sans doute inutile
848
849 BRepLib_ValidateEdge aValidateEdgeOnClosedSurf(myHCurve, ACS, SameParameter);
850 aValidateEdgeOnClosedSurf.Process();
851 aValidateEdgeOnClosedSurf.UpdateTolerance(aNewTol);
852 if (aValidateEdgeOnClosedSurf.IsDone() && !aValidateEdgeOnClosedSurf.CheckTolerance(Tol))
853 {
4e57c75e 854 if (aNewTol<aMaxTol) {
c9337e33 855 UpdateShape(myShape, aNewTol, aMapToAvoid);
3510db62 856 CorrectVertexTolerance(myShape, aMapToAvoid);
4e57c75e 857 }
858 }
859 }
860 }
861 itcr.Next();
862 }
863
864 if (!pcurvefound) {
865 Handle(Geom_Plane) P;
866 Handle(Standard_Type) styp = Su->DynamicType();
867 if (styp == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
c26b5a34 868 P = Handle(Geom_Plane)::
869 DownCast(Handle(Geom_RectangularTrimmedSurface)::
870 DownCast(Su)->BasisSurface());
4e57c75e 871 }
872 else {
873 P = Handle(Geom_Plane)::DownCast(Su);
874 }
875 if (P.IsNull()) { // not a plane
c26b5a34 876 return;
4e57c75e 877 }
878
879 else {// on fait la projection a la volee, comme BRep_Tool
c26b5a34 880 P = Handle(Geom_Plane)::
881 DownCast(P->Transformed(L.Transformation()));
4e57c75e 882 //on projette Cref sur ce plan
c22b52d6 883 Handle(GeomAdaptor_Surface) GAHS = new GeomAdaptor_Surface(P);
4e57c75e 884
c22b52d6 885 // Dub - Normalement myHCurve est une GeomAdaptor_Curve
886 Handle(GeomAdaptor_Curve) Gac = Handle(GeomAdaptor_Curve)::DownCast(myHCurve);
887 Handle(Geom_Curve) C3dx = Gac->Curve();
c26b5a34 888 Handle(Geom_Curve) ProjOnPlane =
889 GeomProjLib::ProjectOnPlane(new Geom_TrimmedCurve(C3dx,First,Last),
890 P,
891 P->Position().Direction(),
892 Standard_True);
4e57c75e 893
c22b52d6 894 Handle(GeomAdaptor_Curve) aHCurve =
895 new GeomAdaptor_Curve(ProjOnPlane);
4e57c75e 896
897 ProjLib_ProjectedCurve proj(GAHS,aHCurve);
898 Handle(Geom2d_Curve) PC = Geom2dAdaptor::MakeCurve(proj);
c22b52d6 899 Handle(Geom2dAdaptor_Curve) GHPC =
900 new Geom2dAdaptor_Curve(PC,
c26b5a34 901 myHCurve->FirstParameter(),
902 myHCurve->LastParameter());
4e57c75e 903
c9337e33 904 Handle(Adaptor3d_CurveOnSurface) ACS = new Adaptor3d_CurveOnSurface(GHPC,GAHS);
4e57c75e 905
c9337e33 906 BRepLib_ValidateEdge aValidateProjEdge(myHCurve, ACS, Standard_True);
907 aValidateProjEdge.Process();
908 aValidateProjEdge.UpdateTolerance(aNewTol);
909 if (aValidateProjEdge.IsDone() && !aValidateProjEdge.CheckTolerance(Tol))
910 {
4e57c75e 911 if (aNewTol<aMaxTol) {
c9337e33 912 UpdateShape(myShape, aNewTol, aMapToAvoid);
3510db62 913 CorrectVertexTolerance(myShape, aMapToAvoid);
4e57c75e 914 }
915 }
916 }
4e57c75e 917 }//end of if (!pcurvefound) {
918 } // end of 2. Tolerances in InContext
4e57c75e 919}
4e57c75e 920//=======================================================================
921//function : CorrectVertexTolerance
922//purpose :
923//=======================================================================
3510db62 924void CorrectVertexTolerance(const TopoDS_Edge& aE,
1155d05a 925 const TopTools_IndexedMapOfShape& aMapToAvoid)
4e57c75e 926{
4e57c75e 927 Standard_Real aTolE, aTolV;
c26b5a34 928 TopoDS_Iterator aIt;
929 //
4e57c75e 930 aTolE=BRep_Tool::Tolerance(aE);
c26b5a34 931 aIt.Initialize(aE);
932 for(; aIt.More(); aIt.Next()) {
933 const TopoDS_Vertex& aV=*((TopoDS_Vertex*)&aIt.Value());
4e57c75e 934 aTolV=BRep_Tool::Tolerance(aV);
935 if (aTolV<aTolE) {
3510db62 936 UpdateShape(aV, aTolE, aMapToAvoid);
4e57c75e 937 }
938 }
939}
4e57c75e 940//=======================================================================
4e57c75e 941// Function : UpdateEdges
942// purpose :
943//=======================================================================
3510db62 944void UpdateEdges(const TopoDS_Face& aF,
1155d05a 945 const TopTools_IndexedMapOfShape& aMapToAvoid)
4e57c75e 946{
947 Standard_Real aTolF, aTolE, aTolV;
948 TopoDS_Iterator aItF, aItW, aItE;
4e57c75e 949 //
c26b5a34 950 aTolE=aTolF= BRep_Tool::Tolerance(aF);
4e57c75e 951 aItF.Initialize(aF);
952 for (; aItF.More(); aItF.Next()) {
953 const TopoDS_Shape& aS = aItF.Value();
954 if (aS.ShapeType()==TopAbs_WIRE) {
955 aItW.Initialize(aS);
956 for (; aItW.More(); aItW.Next()) {
c26b5a34 957 const TopoDS_Edge& aE=*((TopoDS_Edge*)&aItW.Value());
4e57c75e 958 aTolE = BRep_Tool::Tolerance(aE);
959 if (aTolE < aTolF) {
3510db62 960 UpdateShape(aE, aTolF, aMapToAvoid);
4e57c75e 961 aTolE = aTolF;
962 }
4e57c75e 963 }
964 }
965 else {
c26b5a34 966 const TopoDS_Vertex& aV=*(TopoDS_Vertex*)&aItF.Value();
4e57c75e 967 aTolV = BRep_Tool::Tolerance(aV);
968 if (aTolV < aTolE) {
3510db62 969 UpdateShape(aV, aTolF, aMapToAvoid);
4e57c75e 970 }
971 }
972 }
973}
e98e3990 974//=======================================================================
3510db62 975//function : UpdateShape
976//purpose :
977//=======================================================================
978void UpdateShape(const TopoDS_Shape& aS,
979 const Standard_Real aTol,
1155d05a 980 const TopTools_IndexedMapOfShape& aMapToAvoid)
3510db62 981{
982 if (aMapToAvoid.Contains(aS)) {
983 return;
984 }
985 //
986 TopAbs_ShapeEnum aType;
987 BRep_Builder aBB;
988 //
989 aType=aS.ShapeType();
990 if (aType==TopAbs_EDGE) {
991 const TopoDS_Edge& aE = *((TopoDS_Edge*)&aS);
992 aBB.UpdateEdge(aE, aTol);
993 }
994 else if (aType==TopAbs_VERTEX) {
995 const TopoDS_Vertex& aV = *((TopoDS_Vertex*)&aS);
996 aBB.UpdateVertex(aV, aTol);
997 }
998}
999//=======================================================================
e98e3990 1000// Function : ComputeTolerance
1001// purpose :
1002//=======================================================================
1003Standard_Boolean BOPTools_AlgoTools::ComputeTolerance
1004 (const TopoDS_Face& theFace,
1005 const TopoDS_Edge& theEdge,
1006 Standard_Real& theMaxDist,
1b7ae951 1007 Standard_Real& theMaxPar)
e98e3990 1008{
1b7ae951 1009 BRepLib_CheckCurveOnSurface aCS;
e98e3990 1010 //
1b7ae951 1011 aCS.Init(theEdge, theFace);
1012 aCS.Perform();
1013 if (!aCS.IsDone()) {
1014 return Standard_False;
e98e3990 1015 }
1016 //
1b7ae951 1017 theMaxDist = aCS.MaxDistance();
1018 theMaxPar = aCS.MaxParameter();
c7b59798 1019 //
1b7ae951 1020 return Standard_True;
e98e3990 1021}