1 // File: GeomAlgoAPI_SketchBuilder.cpp
2 // Created: 02 Jun 2014
3 // Author: Artem ZHIDKOV
5 #include <GeomAlgoAPI_SketchBuilder.h>
12 #include <BOPAlgo_Builder.hxx>
13 #include <BOPAlgo_Operation.hxx>
14 #include <BOPAlgo_PaveFiller.hxx>
15 #include <BOPTools.hxx>
16 #include <BRep_Builder.hxx>
17 #include <BRep_Curve3D.hxx>
18 #include <BRep_TEdge.hxx>
19 #include <BRep_TVertex.hxx>
20 #include <BRepAlgoAPI_Cut.hxx>
21 #include <BRepBuilderAPI_MakeFace.hxx>
22 #include <BRepClass_FaceClassifier.hxx>
23 #include <Geom_Curve.hxx>
24 #include <Geom_Plane.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TopoDS_Edge.hxx>
28 #include <TopoDS_Face.hxx>
29 #include <TopoDS_Vertex.hxx>
30 #include <TopoDS_Wire.hxx>
32 #include <Precision.hxx>
37 #define DBL_MAX 1.7976931348623158e+308
41 const double tolerance = Precision::Confusion();
42 // This value helps to find direction on the boundaries of curve.
43 // It is not significant for lines, but is used for circles to avoid
44 // wrong directions of movement (when two edges are tangent on the certain vertex)
45 const double shift = acos(1.0 - 3.0 * tolerance);
47 /// \brief Search first vertex - the vertex with lowest x coordinate, which is used in 2 edges at least
48 static const TopoDS_Shape& findStartVertex(
49 const BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE,
50 const gp_Dir& theDirX, const gp_Dir& theDirY);
52 /// \brief Search the vertex on the sketch candidate to be the next one in the loop
53 static void findNextVertex(
54 const TopoDS_Shape& theStartVertex,
55 const BOPCol_IndexedDataMapOfShapeListOfShape& theVertexEdgeMap,
56 const gp_Dir& theStartDir,
57 const gp_Dir& theNormal,
58 TopoDS_Shape& theNextVertex,
59 TopoDS_Shape& theNextEdge,
62 /// \brief Create planar face using the edges surrounding it
63 static void createFace(const TopoDS_Shape& theStartVertex,
64 const std::list<TopoDS_Shape>::iterator& theStartEdge,
65 const std::list<TopoDS_Shape>::iterator& theEndOfEdges,
66 const gp_Pln& thePlane,
67 TopoDS_Face& theResFace);
69 /// \bief Create planar wire
70 static void createWireList(const TopoDS_Shape& theStartVertex,
71 const std::list<TopoDS_Shape>::iterator& theStartEdge,
72 const std::list<TopoDS_Shape>::iterator& theEndOfEdges,
73 const std::set<Handle(TopoDS_TShape)>& theEdgesInLoops,
74 std::list<TopoDS_Wire>& theResWires);
76 /// \brief Calculate outer tengency on the edge in specified vertex
77 static gp_Dir getOuterEdgeDirection(const TopoDS_Shape& theEdge,
78 const TopoDS_Shape& theVertex);
80 /// \brief Unnecessary edges will be removed from the map.
81 /// Positions of iterator will be updated
82 static void removeWasteEdges(
83 std::list<TopoDS_Shape>::iterator& theStartVertex,
84 std::list<TopoDS_Shape>::iterator& theStartEdge,
85 const std::list<TopoDS_Shape>::iterator& theEndOfVertexes,
86 const std::list<TopoDS_Shape>::iterator& theEndOfEdges,
87 BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE);
92 void GeomAlgoAPI_SketchBuilder::createFaces(
93 const boost::shared_ptr<GeomAPI_Pnt>& theOrigin,
94 const boost::shared_ptr<GeomAPI_Dir>& theDirX,
95 const boost::shared_ptr<GeomAPI_Dir>& theDirY,
96 const boost::shared_ptr<GeomAPI_Dir>& theNorm,
97 const std::list< boost::shared_ptr<GeomAPI_Shape> >& theFeatures,
98 std::list< boost::shared_ptr<GeomAPI_Shape> >& theResultFaces,
99 std::list< boost::shared_ptr<GeomAPI_Shape> >& theResultWires)
101 if (theFeatures.empty())
104 // Create the list of edges with shared vertexes
105 BOPAlgo_Builder aBuilder;
106 BOPAlgo_PaveFiller aPF;
107 TopoDS_Shape aFeaturesCompound;
109 // Obtain only edges from the features list
110 std::list< boost::shared_ptr<GeomAPI_Shape> > anEdges;
111 std::list< boost::shared_ptr<GeomAPI_Shape> >::const_iterator aFeatIt = theFeatures.begin();
112 for ( ; aFeatIt != theFeatures.end(); aFeatIt++)
114 boost::shared_ptr<GeomAPI_Shape> aShape(*aFeatIt);
115 const TopoDS_Edge& anEdge = aShape->impl<TopoDS_Edge>();
116 if (anEdge.ShapeType() == TopAbs_EDGE)
117 anEdges.push_back(aShape);
120 if (anEdges.size() == 1)
121 { // If there is only one feature, BOPAlgo_Builder will decline to work. Need to process it anyway
122 aFeaturesCompound = anEdges.front()->impl<TopoDS_Shape>();
126 std::list< boost::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = anEdges.begin();
127 for (; anIt != anEdges.end(); anIt++)
129 boost::shared_ptr<GeomAPI_Shape> aPreview(*anIt);
130 aBuilder.AddArgument(aPreview->impl<TopoDS_Edge>());
132 aPF.SetArguments(aBuilder.Arguments());
134 int aErr = aPF.ErrorStatus();
136 aBuilder.PerformWithFiller(aPF);
137 aErr = aBuilder.ErrorStatus();
139 aFeaturesCompound = aBuilder.Shape();
142 BOPCol_IndexedDataMapOfShapeListOfShape aMapVE; // map between vertexes and edges
143 BOPTools::MapShapesAndAncestors(aFeaturesCompound, TopAbs_VERTEX, TopAbs_EDGE, aMapVE);
144 if (aMapVE.IsEmpty()) // in case of not-initialized circle
147 gp_Dir aDirX = theDirX->impl<gp_Dir>();
148 gp_Dir aDirY = theDirY->impl<gp_Dir>();
149 gp_Dir aNorm = theNorm->impl<gp_Dir>();
151 gp_Pln aPlane(theOrigin->impl<gp_Pnt>(), aNorm);
153 // Set of edges used in loops
154 std::set<Handle(TopoDS_TShape)> anEdgesInLoops;
155 // Lists for processed vertexes and edges
156 std::list<TopoDS_Shape> aProcVertexes;
157 std::list<TopoDS_Shape> aProcEdges;
159 // Search the start vertex
160 TopoDS_Shape aStartVertex = findStartVertex(aMapVE, aDirX, aDirY);
161 aProcVertexes.push_back(aStartVertex);
163 TopoDS_Shape aCurVertex = aStartVertex;
164 gp_Dir aCurDir = aDirY.Reversed();
165 gp_Dir aCurNorm = aNorm.Reversed();
167 // Go through the edges and find loops
168 TopoDS_Shape aNextVertex;
169 TopoDS_Shape aBindingEdge;
171 while (aMapVE.Extent() > 0)
173 if (aCurVertex.IsNull())
175 findNextVertex(aCurVertex, aMapVE, aCurDir, aCurNorm, aNextVertex, aBindingEdge, aNextDir);
178 // Try to find next vertex in the list of already processed
179 std::list<TopoDS_Shape>::iterator aVertIter = aProcVertexes.begin();
180 std::list<TopoDS_Shape>::iterator anEdgeIter = aProcEdges.begin();
181 for ( ; aVertIter != aProcVertexes.end(); aVertIter++)
183 if (aVertIter->TShape() == aNextVertex.TShape())
185 if (anEdgeIter != aProcEdges.end())
189 aProcVertexes.push_back(aNextVertex);
190 aProcEdges.push_back(aBindingEdge);
192 // The loop was found
193 if (aVertIter != aProcVertexes.end())
195 // If the binding edge is a full circle, then the list may be empty before addition. Need to update edge iterator
196 if (aProcEdges.size() == 1)
197 anEdgeIter = aProcEdges.begin();
199 if (aVertIter != aProcVertexes.begin())
201 // Check the orientation of the loop
202 gp_Dir aCN = getOuterEdgeDirection(*anEdgeIter, *aVertIter);
203 gp_Dir aCP = getOuterEdgeDirection(aProcEdges.back(), *aVertIter);
206 if (aCN.DotCross(aCP, aNorm) < -tolerance)
208 // The found loop has wrong orientation and may contain sub-loops.
209 // Need to check it onle again with another initial direction.
210 aCurVertex = *aVertIter;
212 aProcVertexes.pop_back();
213 aProcEdges.pop_back();
214 } while (aCurVertex != aProcVertexes.back());
215 aCurDir = getOuterEdgeDirection(aProcEdges.back(), aCurVertex);
216 aCurNorm = aNorm.Reversed();
221 // When the orientation is correct or the edges looped through
222 // the first element, create new face and remove unnecessary edges.
224 createFace(*aVertIter, anEdgeIter, aProcEdges.end(), aPlane, aPatch);
225 if (!aPatch.IsNull())
227 boost::shared_ptr<GeomAPI_Shape> aFace(new GeomAPI_Shape);
228 aFace->setImpl(new TopoDS_Face(aPatch));
229 theResultFaces.push_back(aFace);
231 // push the edges used in the loop to the map
232 std::list<TopoDS_Shape>::iterator anIter;
233 for (anIter = anEdgeIter; anIter != aProcEdges.end(); anIter++)
234 anEdgesInLoops.insert(anIter->TShape());
235 // remove unnecessary edges
236 std::list<TopoDS_Shape>::iterator aCopyVLoop = aVertIter;
237 std::list<TopoDS_Shape>::iterator aCopyELoop = anEdgeIter;
238 removeWasteEdges(aVertIter, anEdgeIter, aProcVertexes.end(), aProcEdges.end(), aMapVE);
240 // revert the list of remaining edges
241 std::list<TopoDS_Shape> aRemainVertexes;
242 for ( ; aVertIter != aProcVertexes.end(); aVertIter++)
243 aRemainVertexes.push_front(*aVertIter);
244 std::list<TopoDS_Shape> aRemainEdges;
245 for ( ; anEdgeIter != aProcEdges.end(); anEdgeIter++)
246 aRemainEdges.push_front(*anEdgeIter);
247 // remove edges and vertexes used in the loop and add remaining ones
248 aProcVertexes.erase(aCopyVLoop, aProcVertexes.end());
249 aProcVertexes.insert(aProcVertexes.end(), aRemainVertexes.begin(), aRemainVertexes.end());
250 aProcEdges.erase(aCopyELoop, aProcEdges.end());
251 aProcEdges.insert(aProcEdges.end(), aRemainEdges.begin(), aRemainEdges.end());
253 // Recalculate current vertex and current direction
254 if (!aProcVertexes.empty())
256 aNextVertex = aProcVertexes.back();
257 if (!aProcEdges.empty())
258 aNextDir = getOuterEdgeDirection(aProcEdges.back(), aNextVertex);
259 else aNextDir = aDirY;
263 // if next vertex connected only to alone edge, this is a part of wire (not a closed loop),
264 // we need to go back through the list of already checked edges to find a branching vertex
265 if (!aMapVE.IsEmpty() && aMapVE.Contains(aNextVertex) &&
266 aMapVE.FindFromKey(aNextVertex).Size() == 1)
268 std::list<TopoDS_Shape>::reverse_iterator aVRIter = aProcVertexes.rbegin();
269 std::list<TopoDS_Shape>::reverse_iterator aERIter = aProcEdges.rbegin();
270 if (aVRIter != aProcVertexes.rend())
272 if (aERIter != aProcEdges.rend())
275 for ( ; aERIter != aProcEdges.rend(); aERIter++, aVRIter++)
276 if (aMapVE.FindFromKey(*aVRIter).Size() > 2)
278 if (aERIter != aProcEdges.rend() ||
279 (aVRIter != aProcVertexes.rend() && aMapVE.FindFromKey(*aVRIter).Size() == 1))
280 { // the branching vertex was found or current list of edges is a wire without branches
281 std::list<TopoDS_Shape>::iterator aEIter;
282 TopoDS_Shape aCurEdge;
283 if (aERIter != aProcEdges.rend())
285 aEIter = aERIter.base();
289 aEIter = aProcEdges.begin();
290 std::list<TopoDS_Wire> aTail;
291 createWireList(*aVRIter, aEIter, aProcEdges.end(), anEdgesInLoops, aTail);
292 std::list<TopoDS_Wire>::const_iterator aTailIter = aTail.begin();
293 for ( ; aTailIter != aTail.end(); aTailIter++)
294 if (!aTailIter->IsNull())
296 boost::shared_ptr<GeomAPI_Shape> aWire(new GeomAPI_Shape);
297 aWire->setImpl(new TopoDS_Shape(*aTailIter));
298 theResultWires.push_back(aWire);
300 std::list<TopoDS_Shape>::iterator aVIter = aVRIter.base();
301 std::list<TopoDS_Shape>::iterator aEItCopy = aEIter;
302 removeWasteEdges(--aVIter, aEItCopy, aProcVertexes.end(), aProcEdges.end(), aMapVE);
304 aProcEdges.erase(aEIter, aProcEdges.end());
305 aVIter = aVRIter.base();
306 aProcVertexes.erase(aVIter, aProcVertexes.end());
308 if (!aProcVertexes.empty())
310 aNextVertex = aProcVertexes.back();
311 if (!aCurEdge.IsNull())
312 aNextDir = getOuterEdgeDirection(aCurEdge, aNextVertex);
316 { // there is no branching vertex in the list of proceeded,
317 // so we should revert the list and go the opposite way
318 aProcVertexes.reverse();
319 aProcEdges.reverse();
320 aNextVertex = aProcVertexes.back();
321 aNextDir = aProcEdges.empty() ? aDirY :
322 getOuterEdgeDirection(aProcEdges.back(), aNextVertex);
326 // When all edges of current component of connectivity are processed,
327 // we should repeat procedure for finding initial vertex in the remaining
328 if (!aMapVE.IsEmpty() && !aMapVE.Contains(aNextVertex))
330 aProcVertexes.clear();
333 TopoDS_Shape aStartEdge;
334 aStartVertex = findStartVertex(aMapVE, aDirX, aDirY);
335 aProcVertexes.push_back(aStartVertex);
336 aNextVertex = aStartVertex;
337 aNextDir = aDirY.Reversed();
338 aCurNorm = aNorm.Reversed();
341 aCurVertex = aNextVertex;
345 if (theResultFaces.size() > 1)
346 fixIntersections(theResultFaces);
349 void GeomAlgoAPI_SketchBuilder::fixIntersections(
350 std::list< boost::shared_ptr<GeomAPI_Shape> >& theFaces)
352 BRepClass_FaceClassifier aClassifier;
354 std::list< boost::shared_ptr<GeomAPI_Shape> >::iterator anIter1 = theFaces.begin();
355 std::list< boost::shared_ptr<GeomAPI_Shape> >::iterator anIter2;
356 for ( ; anIter1 != theFaces.end(); anIter1++)
359 for (++anIter2; anIter2 != theFaces.end(); anIter2++)
361 const TopoDS_Face& aF1 = (*anIter1)->impl<TopoDS_Face>();
362 assert(aF1.ShapeType() == TopAbs_FACE); // all items in result list should be faces
363 TopExp_Explorer aVert2((*anIter2)->impl<TopoDS_Shape>(), TopAbs_VERTEX);
364 for ( ; aVert2.More(); aVert2.Next())
366 Handle(BRep_TVertex) aV = Handle(BRep_TVertex)::DownCast(aVert2.Current().TShape());
367 aClassifier.Perform(aF1, aV->Pnt(), tolerance);
368 if (aClassifier.State() != TopAbs_IN)
372 { // second shape is not inside first, change the shapes order and repeat comparision
373 const TopoDS_Face& aF2 = (*anIter2)->impl<TopoDS_Face>();
374 assert(aF2.ShapeType() == TopAbs_FACE); // all items in result list should be faces
375 TopExp_Explorer aVert1((*anIter1)->impl<TopoDS_Shape>(), TopAbs_VERTEX);
376 for ( ; aVert1.More(); aVert1.Next())
378 Handle(BRep_TVertex) aV = Handle(BRep_TVertex)::DownCast(aVert1.Current().TShape());
379 aClassifier.Perform(aF2, aV->Pnt(), tolerance);
380 if (aClassifier.State() != TopAbs_IN)
384 { // first shape should be cut from the second
385 BRepAlgoAPI_Cut aCut((*anIter2)->impl<TopoDS_Shape>(),
386 (*anIter1)->impl<TopoDS_Shape>());
388 TopExp_Explorer anExp(aCut.Shape(), TopAbs_FACE);
389 (*anIter2)->setImpl(new TopoDS_Shape(anExp.Current()));
390 for (anExp.Next(); anExp.More(); anExp.Next())
392 boost::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
393 aShape->setImpl(new TopoDS_Shape(anExp.Current()));
394 theFaces.push_back(aShape);
399 { // second shape should be cut from the first
400 BRepAlgoAPI_Cut aCut((*anIter1)->impl<TopoDS_Shape>(),
401 (*anIter2)->impl<TopoDS_Shape>());
403 TopExp_Explorer anExp(aCut.Shape(), TopAbs_FACE);
404 (*anIter1)->setImpl(new TopoDS_Shape(anExp.Current()));
405 for (anExp.Next(); anExp.More(); anExp.Next())
407 boost::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
408 aShape->setImpl(new TopoDS_Shape(anExp.Current()));
409 theFaces.push_back(aShape);
417 // =================== Auxiliary functions ====================================
418 const TopoDS_Shape& findStartVertex(
419 const BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE,
420 const gp_Dir& theDirX, const gp_Dir& theDirY)
422 int aStartVertexInd = 1;
423 double aMaxX = -DBL_MAX;
424 double aMaxY = -DBL_MAX;
425 int aNbVert = theMapVE.Extent();
426 for (int i = 1; i <= aNbVert; i++)
428 const TopoDS_Vertex& aV = (const TopoDS_Vertex&)theMapVE.FindKey(i);
429 const Handle(BRep_TVertex)& aVert = (const Handle(BRep_TVertex)&)aV.TShape();
430 const gp_Pnt& aVertPnt = aVert->Pnt();
432 double aX = aVertPnt.XYZ().Dot(theDirX.XYZ());
433 double aY = aVertPnt.XYZ().Dot(theDirY.XYZ());
434 if ((aX > aMaxX || (fabs(aX - aMaxX) < tolerance && aY > aMaxY)) &&
435 theMapVE.FindFromIndex(i).Extent() > 1)
442 return theMapVE.FindKey(aStartVertexInd);
446 const TopoDS_Shape& theStartVertex,
447 const BOPCol_IndexedDataMapOfShapeListOfShape& theVertexEdgeMap,
448 const gp_Dir& theStartDir,
449 const gp_Dir& theNormal,
450 TopoDS_Shape& theNextVertex,
451 TopoDS_Shape& theNextEdge,
454 const BOPCol_ListOfShape& anEdgesList = theVertexEdgeMap.FindFromKey(theStartVertex);
455 BOPCol_ListOfShape::Iterator aEdIter(anEdgesList);
456 double aBestEdgeProj = DBL_MAX;
457 for ( ; aEdIter.More(); aEdIter.Next())
459 gp_Dir aTang = getOuterEdgeDirection(aEdIter.Value(), theStartVertex);
462 // The projection is normalized in segment (-1, 1),
463 // where (-1, 0] corresponds to the angles (pi/2, 0] between theStartDir and aTang
464 // and [0, 1) corresponds to the angles [0, -pi/2)
465 double aProj = (aTang.Dot(theStartDir) - 1.0) * 0.5;
466 if (fabs(fabs(aProj) - 1) < tolerance)
468 if (theStartDir.DotCross(aTang, theNormal) < tolerance)
471 if (aProj < aBestEdgeProj)
473 aBestEdgeProj = aProj;
474 theNextEdge = aEdIter.Value();
475 TopExp_Explorer aVertExp(theNextEdge, TopAbs_VERTEX);
476 for ( ; aVertExp.More(); aVertExp.Next())
477 if (aVertExp.Current().TShape() != theStartVertex.TShape())
479 theNextVertex = aVertExp.Current();
480 theNextDir = getOuterEdgeDirection(aEdIter.Value(), theNextVertex);
483 if (!aVertExp.More())
484 { // This edge is a full circle
485 TopoDS_Vertex aV1, aV2;
486 TopExp::Vertices(*(const TopoDS_Edge*)(&theNextEdge), aV1, aV2);
487 if (aV1.Orientation() == theStartVertex.Orientation())
491 theNextDir = getOuterEdgeDirection(aEdIter.Value(), theNextVertex);
497 static void addEdgeToWire(const TopoDS_Edge& theEdge,
498 const BRep_Builder& theBuilder,
499 TopoDS_Shape& theSpliceVertex,
500 TopoDS_Wire& theWire)
502 TopoDS_Edge anEdge = theEdge;
503 bool isCurVertChanged = false;
504 TopoDS_Shape aCurVertChanged;
506 TopExp_Explorer aVertExp(theEdge, TopAbs_VERTEX);
507 for ( ; aVertExp.More(); aVertExp.Next())
509 const TopoDS_Shape& aVertex = aVertExp.Current();
510 if (aVertex.TShape() == theSpliceVertex.TShape() &&
511 aVertex.Orientation() != theEdge.Orientation())
512 { // Current vertex is the last for the edge, so its orientation is wrong, need to revert the edge
516 if (aVertex.TShape() != theSpliceVertex.TShape())
518 aCurVertChanged = aVertex;
519 isCurVertChanged = true;
522 theSpliceVertex = isCurVertChanged ? aCurVertChanged : aVertExp.Current();
524 theBuilder.Add(theWire, anEdge);
527 void createFace(const TopoDS_Shape& theStartVertex,
528 const std::list<TopoDS_Shape>::iterator& theStartEdge,
529 const std::list<TopoDS_Shape>::iterator& theEndOfEdges,
530 const gp_Pln& thePlane,
531 TopoDS_Face& theResFace)
533 TopoDS_Wire aResWire;
534 BRep_Builder aBuilder;
535 aBuilder.MakeWire(aResWire);
537 TopoDS_Shape aCurVertex = theStartVertex;
538 std::list<TopoDS_Shape>::const_iterator anEdgeIter = theStartEdge;
539 for ( ; anEdgeIter != theEndOfEdges; anEdgeIter++)
541 TopoDS_Edge anEdge = *((TopoDS_Edge*)(&(*anEdgeIter)));
542 if (!anEdge.IsNull())
543 addEdgeToWire(anEdge, aBuilder, aCurVertex, aResWire);
546 BRepBuilderAPI_MakeFace aFaceBuilder(thePlane, aResWire);
547 if (aFaceBuilder.Error() == BRepBuilderAPI_FaceDone)
548 theResFace = aFaceBuilder.Face();
551 void createWireList(const TopoDS_Shape& theStartVertex,
552 const std::list<TopoDS_Shape>::iterator& theStartEdge,
553 const std::list<TopoDS_Shape>::iterator& theEndOfEdges,
554 const std::set<Handle(TopoDS_TShape)>& theEdgesInLoops,
555 std::list<TopoDS_Wire>& theResWires)
557 BRep_Builder aBuilder;
558 bool needNewWire = true;
559 TopoDS_Shape aCurVertex = theStartVertex;
561 std::list<TopoDS_Shape>::iterator anIter = theStartEdge;
562 while (anIter != theEndOfEdges)
564 while (anIter != theEndOfEdges && needNewWire &&
565 theEdgesInLoops.count(anIter->TShape()) != 0)
567 TopExp_Explorer aVertExp(*anIter, TopAbs_VERTEX);
568 for ( ; aVertExp.More(); aVertExp.Next())
569 if (aVertExp.Current().TShape() != aCurVertex.TShape())
571 aCurVertex = aVertExp.Current();
576 if (anIter == theEndOfEdges)
580 { // The new wire should be created
582 aBuilder.MakeWire(aWire);
583 theResWires.push_back(aWire);
586 else if (theEdgesInLoops.count(anIter->TShape()) != 0)
587 { // There was found the edge already used in loop.
588 // Current wire should be released and new one should started
593 TopoDS_Edge anEdge = *((TopoDS_Edge*)(&(*anIter)));
594 addEdgeToWire(anEdge, aBuilder, aCurVertex, theResWires.back());
600 gp_Dir getOuterEdgeDirection(const TopoDS_Shape& theEdge,
601 const TopoDS_Shape& theVertex)
603 const Handle(BRep_TVertex)& aVertex = (const Handle(BRep_TVertex)&)theVertex.TShape();
604 gp_Pnt aVertexPnt = aVertex->Pnt();
606 const Handle(BRep_TEdge)& anEdge = (const Handle(BRep_TEdge)&)theEdge.TShape();
608 // Convert the edge to the curve to calculate the tangency.
609 // There should be only one curve in the edge.
610 Handle(BRep_Curve3D) aEdCurve =
611 Handle(BRep_Curve3D)::DownCast(anEdge->Curves().First());
612 double aFirst, aLast;
613 aEdCurve->Range(aFirst, aLast);
614 Handle(Geom_Curve) aCurve = aEdCurve->Curve3D();
618 // A direction is determined not in the boundary points but in the points with small shift.
619 // It was done to avoid tangency between circle and other edge in the shared vertex.
620 aCurve->D1(aFirst + shift > aLast ? aFirst : aFirst + shift, aPnt, aTang);
621 aCurve->D0(aFirst, aPnt);
622 if (aVertexPnt.IsEqual(aPnt, tolerance))
623 return gp_Dir(aTang.Reversed());
625 aCurve->D1(aLast - shift < aFirst ? aLast : aLast - shift, aPnt, aTang);
626 return gp_Dir(aTang);
629 void removeWasteEdges(
630 std::list<TopoDS_Shape>::iterator& theStartVertex,
631 std::list<TopoDS_Shape>::iterator& theStartEdge,
632 const std::list<TopoDS_Shape>::iterator& theEndOfVertexes,
633 const std::list<TopoDS_Shape>::iterator& theEndOfEdges,
634 BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE)
636 bool isVertStep = true;
637 while (theStartVertex != theEndOfVertexes && theStartEdge != theEndOfEdges)
639 BOPCol_ListOfShape& aBunch = theMapVE.ChangeFromKey(*theStartVertex);
640 BOPCol_ListOfShape::Iterator anApprEdge(aBunch);
641 for ( ; anApprEdge.More(); anApprEdge.Next())
642 if (anApprEdge.Value() == *theStartEdge)
644 if (anApprEdge.More())
645 aBunch.Remove(anApprEdge);
652 // check current vertex to be a branching point
653 // if so, it will be a new starting point to find a loop
654 if (aBunch.Size() > 1)
657 isVertStep = !isVertStep;
660 // The map of vertex-edges may be changed
661 BOPCol_IndexedDataMapOfShapeListOfShape aMapVECopy;
662 BOPCol_IndexedDataMapOfShapeListOfShape::Iterator aMapIter(theMapVE);
663 for (int ind = 1; aMapIter.More(); aMapIter.Next(), ind++)
664 if (!aMapIter.Value().IsEmpty())
665 aMapVECopy.Add(theMapVE.FindKey(ind), aMapIter.Value());
667 theMapVE.Exchange(aMapVECopy);