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_Tool.hxx>
19 #include <BRepAlgoAPI_Cut.hxx>
20 #include <BRepBuilderAPI_MakeFace.hxx>
21 #include <BRepClass_FaceClassifier.hxx>
22 #include <Geom_Curve.hxx>
23 #include <Geom_Plane.hxx>
25 #include <TopExp_Explorer.hxx>
26 #include <TopoDS_Edge.hxx>
27 #include <TopoDS_Face.hxx>
28 #include <TopoDS_Vertex.hxx>
29 #include <TopoDS_Wire.hxx>
31 #include <Precision.hxx>
36 #define DBL_MAX 1.7976931348623158e+308
39 const double tolerance = Precision::Confusion();
40 // This value helps to find direction on the boundaries of curve.
41 // It is not significant for lines, but is used for circles to avoid
42 // wrong directions of movement (when two edges are tangent on the certain vertex)
43 const double shift = acos(1.0 - 3.0 * tolerance);
45 /// \brief Search first vertex - the vertex with lowest x coordinate, which is used in 2 edges at least
46 static const TopoDS_Vertex& findStartVertex(const BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE,
47 const gp_Dir& theDirX, const gp_Dir& theDirY);
49 /// \brief Search the vertex on the sketch candidate to be the next one in the loop
50 static void findNextVertex(const TopoDS_Vertex& theStartVertex,
51 const BOPCol_IndexedDataMapOfShapeListOfShape& theVertexEdgeMap,
52 const gp_Dir& theStartDir, const gp_Dir& theNormal,
53 TopoDS_Vertex& theNextVertex, TopoDS_Edge& theNextEdge,
56 /// \brief Create planar face using the edges surrounding it
57 static void createFace(const TopoDS_Vertex& theStartVertex,
58 const std::list<TopoDS_Edge>::iterator& theStartEdge,
59 const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
60 const gp_Pln& thePlane, TopoDS_Face& theResFace);
62 /// \bief Create planar wire
63 static void createWireList(const TopoDS_Vertex& theStartVertex,
64 const std::list<TopoDS_Edge>::iterator& theStartEdge,
65 const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
66 const std::set<TopoDS_Edge*>& theEdgesInLoops,
67 std::list<TopoDS_Wire>& theResWires);
69 /// \brief Calculate outer tengency on the edge in specified vertex
70 static gp_Dir getOuterEdgeDirection(const TopoDS_Edge& theEdge, const TopoDS_Vertex& theVertex);
72 /// \brief Unnecessary edges will be removed from the map.
73 /// Positions of iterator will be updated
74 static void removeWasteEdges(std::list<TopoDS_Vertex>::iterator& theStartVertex,
75 std::list<TopoDS_Edge>::iterator& theStartEdge,
76 const std::list<TopoDS_Vertex>::iterator& theEndOfVertexes,
77 const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
78 BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE);
81 void GeomAlgoAPI_SketchBuilder::createFaces(
82 const boost::shared_ptr<GeomAPI_Pnt>& theOrigin, const boost::shared_ptr<GeomAPI_Dir>& theDirX,
83 const boost::shared_ptr<GeomAPI_Dir>& theDirY, const boost::shared_ptr<GeomAPI_Dir>& theNorm,
84 const std::list<boost::shared_ptr<GeomAPI_Shape> >& theFeatures,
85 std::list<boost::shared_ptr<GeomAPI_Shape> >& theResultFaces,
86 std::list<boost::shared_ptr<GeomAPI_Shape> >& theResultWires)
88 if (theFeatures.empty())
91 // Create the list of edges with shared vertexes
92 BOPAlgo_Builder aBuilder;
93 BOPAlgo_PaveFiller aPF;
94 TopoDS_Shape aFeaturesCompound;
96 // Obtain only edges from the features list
97 std::list<boost::shared_ptr<GeomAPI_Shape> > anEdges;
98 std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator aFeatIt = theFeatures.begin();
99 for (; aFeatIt != theFeatures.end(); aFeatIt++) {
100 boost::shared_ptr<GeomAPI_Shape> aShape(*aFeatIt);
101 const TopoDS_Edge& anEdge = aShape->impl<TopoDS_Edge>();
102 if (anEdge.ShapeType() == TopAbs_EDGE)
103 anEdges.push_back(aShape);
106 if (anEdges.size() == 1) { // If there is only one feature, BOPAlgo_Builder will decline to work. Need to process it anyway
107 aFeaturesCompound = anEdges.front()->impl<TopoDS_Shape>();
109 std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = anEdges.begin();
110 for (; anIt != anEdges.end(); anIt++) {
111 boost::shared_ptr<GeomAPI_Shape> aPreview(*anIt);
112 aBuilder.AddArgument(aPreview->impl<TopoDS_Edge>());
114 aPF.SetArguments(aBuilder.Arguments());
116 int aErr = aPF.ErrorStatus();
119 aBuilder.PerformWithFiller(aPF);
120 aErr = aBuilder.ErrorStatus();
123 aFeaturesCompound = aBuilder.Shape();
126 BOPCol_IndexedDataMapOfShapeListOfShape aMapVE; // map between vertexes and edges
127 BOPTools::MapShapesAndAncestors(aFeaturesCompound, TopAbs_VERTEX, TopAbs_EDGE, aMapVE);
128 if (aMapVE.IsEmpty()) // in case of not-initialized circle
131 gp_Dir aDirX = theDirX->impl<gp_Dir>();
132 gp_Dir aDirY = theDirY->impl<gp_Dir>();
133 gp_Dir aNorm = theNorm->impl<gp_Dir>();
135 gp_Pln aPlane(theOrigin->impl<gp_Pnt>(), aNorm);
137 // Set of edges used in loops
138 std::set<TopoDS_Edge*> anEdgesInLoops;
139 // Lists for processed vertexes and edges
140 std::list<TopoDS_Vertex> aProcVertexes;
141 std::list<TopoDS_Edge> aProcEdges;
143 // Search the start vertex
144 TopoDS_Vertex aStartVertex = findStartVertex(aMapVE, aDirX, aDirY);
145 aProcVertexes.push_back(aStartVertex);
147 TopoDS_Vertex aCurVertex = aStartVertex;
148 gp_Dir aCurDir = aDirY.Reversed();
149 gp_Dir aCurNorm = aNorm.Reversed();
151 // Go through the edges and find loops
152 TopoDS_Vertex aNextVertex;
153 TopoDS_Edge aBindingEdge;
155 while (aMapVE.Extent() > 0) {
156 if (aCurVertex.IsNull())
158 if (!aProcEdges.empty())
159 aBindingEdge = aProcEdges.back();
160 findNextVertex(aCurVertex, aMapVE, aCurDir, aCurNorm, aNextVertex, aBindingEdge, aNextDir);
163 // Try to find next vertex in the list of already processed
164 bool isLoopFound = false;
165 std::list<TopoDS_Vertex>::iterator aVertIter = aProcVertexes.begin();
166 std::list<TopoDS_Edge>::iterator anEdgeIter = aProcEdges.begin();
167 for (; aVertIter != aProcVertexes.end(); aVertIter++) {
168 if (aVertIter->IsSame(aNextVertex)) {
172 if (anEdgeIter != aProcEdges.end())
176 bool isCircleFound = (isLoopFound && anEdgeIter == aProcEdges.end());
177 aProcVertexes.push_back(aNextVertex);
178 aProcEdges.push_back(aBindingEdge);
181 // If the binding edge is a full circle, then it may be added recently. Need to update edge iterator
183 anEdgeIter = aProcEdges.end();
186 else if (aVertIter != aProcVertexes.begin()) {
187 // Check the orientation of the loop
188 gp_Dir aCN = getOuterEdgeDirection(*anEdgeIter, *aVertIter);
189 gp_Dir aCP = getOuterEdgeDirection(aProcEdges.back(), *aVertIter);
192 if (aCN.DotCross(aCP, aNorm) < -tolerance) {
193 // The found loop has wrong orientation and may contain sub-loops.
194 // Need to check it once again with another initial direction.
195 aCurVertex = *aVertIter;
197 aProcVertexes.pop_back();
198 aProcEdges.pop_back();
199 } while (aCurVertex != aProcVertexes.back());
200 aCurDir = aCN.Reversed();
201 aCurNorm = aNorm.Reversed();
206 if (!isCircleFound && anEdgeIter != aProcEdges.end() &&
207 anEdgeIter->IsSame(aProcEdges.back())) { // The loop on the same edge taken twice
208 aProcVertexes.pop_back();
209 aProcEdges.pop_back();
210 aCurVertex = aProcVertexes.back();
211 aCurDir = getOuterEdgeDirection(aProcEdges.back(), aCurVertex);
212 aCurNorm = aNorm.Reversed();
216 // When the orientation is correct or the edges looped through
217 // the first element, create new face and remove unnecessary edges.
219 createFace(*aVertIter, anEdgeIter, aProcEdges.end(), aPlane, aPatch);
220 if (!aPatch.IsNull()) {
221 boost::shared_ptr<GeomAPI_Shape> aFace(new GeomAPI_Shape);
222 aFace->setImpl(new TopoDS_Face(aPatch));
223 theResultFaces.push_back(aFace);
225 // push the edges used in the loop to the map
226 std::list<TopoDS_Edge>::iterator anIter;
227 for (anIter = anEdgeIter; anIter != aProcEdges.end(); anIter++)
228 anEdgesInLoops.insert(&(*anIter));
229 // remove unnecessary edges
230 std::list<TopoDS_Vertex>::iterator aCopyVLoop = aVertIter;
231 std::list<TopoDS_Edge>::iterator aCopyELoop = anEdgeIter;
232 removeWasteEdges(aVertIter, anEdgeIter, aProcVertexes.end(), aProcEdges.end(), aMapVE);
234 // revert the list of remaining edges
235 std::list<TopoDS_Vertex> aRemainVertexes;
236 for (; aVertIter != aProcVertexes.end(); aVertIter++)
237 aRemainVertexes.push_front(*aVertIter);
238 std::list<TopoDS_Edge> aRemainEdges;
239 for (; anEdgeIter != aProcEdges.end(); anEdgeIter++)
240 aRemainEdges.push_front(*anEdgeIter);
241 // remove edges and vertexes used in the loop and add remaining ones
242 if (aCopyVLoop != aProcVertexes.begin()) {
243 aVertIter = aCopyVLoop;
246 aVertIter = aProcVertexes.end();
247 aProcVertexes.erase(aCopyVLoop, aProcVertexes.end());
248 aProcVertexes.insert(aProcVertexes.end(), aRemainVertexes.begin(), aRemainVertexes.end());
249 if (aCopyELoop != aProcEdges.begin()) {
250 anEdgeIter = aCopyELoop;
253 anEdgeIter = aProcEdges.end();
254 aProcEdges.erase(aCopyELoop, aProcEdges.end());
255 aProcEdges.insert(aProcEdges.end(), aRemainEdges.begin(), aRemainEdges.end());
257 if (aVertIter == aProcVertexes.end())
258 aVertIter = aProcVertexes.begin();
261 if (anEdgeIter == aProcEdges.end())
262 anEdgeIter = aProcEdges.begin();
265 aCopyVLoop = aVertIter;
266 aCopyELoop = anEdgeIter;
268 if (aVertIter != aProcVertexes.end() &&
269 aMapVE.Contains(*aVertIter) && aMapVE.FindFromKey(*aVertIter).Extent() <= 1)
270 removeWasteEdges(aVertIter, anEdgeIter, aProcVertexes.end(), aProcEdges.end(), aMapVE);
271 if (aCopyVLoop != aVertIter)
272 aProcVertexes.erase(aCopyVLoop, aVertIter);
273 if (aCopyELoop != anEdgeIter)
274 aProcEdges.erase(aCopyELoop, anEdgeIter);
276 // Check whether the next vertex already exists
277 if (aVertIter != aProcVertexes.end())
279 if (aVertIter != aProcVertexes.end() && anEdgeIter != aProcEdges.end()) {
280 aNextVertex = *aVertIter;
281 aNextDir = getOuterEdgeDirection(*anEdgeIter, aNextVertex);
282 aProcVertexes.erase(++aVertIter, aProcVertexes.end());
283 aProcEdges.erase(++anEdgeIter, aProcEdges.end());
285 // Recalculate current vertex and current direction
287 aProcVertexes.clear();
288 if (aMapVE.Extent() > 0) {
289 aNextVertex = findStartVertex(aMapVE, aDirX, aDirY);
290 aProcVertexes.push_back(aNextVertex);
292 aNextDir = aDirY.Reversed();
293 aCurNorm = aNorm.Reversed();
297 // if next vertex connected only to alone edge, this is a part of wire (not a closed loop),
298 // we need to go back through the list of already checked edges to find a branching vertex
299 if (!aMapVE.IsEmpty() && aMapVE.Contains(aNextVertex)
300 && aMapVE.FindFromKey(aNextVertex).Size() == 1) {
301 std::list<TopoDS_Vertex>::reverse_iterator aVRIter = aProcVertexes.rbegin();
302 std::list<TopoDS_Edge>::reverse_iterator aERIter = aProcEdges.rbegin();
303 if (aVRIter != aProcVertexes.rend())
305 if (aERIter != aProcEdges.rend())
308 for (; aERIter != aProcEdges.rend(); aERIter++, aVRIter++)
309 if (aMapVE.FindFromKey(*aVRIter).Size() > 2)
311 if (aERIter != aProcEdges.rend()
312 || (aVRIter != aProcVertexes.rend() && aMapVE.FindFromKey(*aVRIter).Size() == 1)) { // the branching vertex was found or current list of edges is a wire without branches
313 std::list<TopoDS_Edge>::iterator aEIter;
314 TopoDS_Edge aCurEdge;
315 if (aERIter != aProcEdges.rend()) {
316 aEIter = aERIter.base();
319 aEIter = aProcEdges.begin();
320 std::list<TopoDS_Wire> aTail;
321 createWireList(*aVRIter, aEIter, aProcEdges.end(), anEdgesInLoops, aTail);
322 std::list<TopoDS_Wire>::const_iterator aTailIter = aTail.begin();
323 for (; aTailIter != aTail.end(); aTailIter++)
324 if (!aTailIter->IsNull()) {
325 boost::shared_ptr<GeomAPI_Shape> aWire(new GeomAPI_Shape);
326 aWire->setImpl(new TopoDS_Shape(*aTailIter));
327 theResultWires.push_back(aWire);
329 std::list<TopoDS_Vertex>::iterator aVIter = aVRIter.base();
330 std::list<TopoDS_Edge>::iterator aEItCopy = aEIter;
331 removeWasteEdges(--aVIter, aEItCopy, aProcVertexes.end(), aProcEdges.end(), aMapVE);
333 aProcEdges.erase(aEIter, aProcEdges.end());
334 aVIter = aVRIter.base();
335 aProcVertexes.erase(aVIter, aProcVertexes.end());
337 if (!aProcVertexes.empty()) {
338 aNextVertex = aProcVertexes.back();
339 if (!aCurEdge.IsNull())
340 aNextDir = getOuterEdgeDirection(aCurEdge, aNextVertex);
342 } else { // there is no branching vertex in the list of proceeded,
343 // so we should revert the list and go the opposite way
344 aProcVertexes.reverse();
345 aProcEdges.reverse();
346 aNextVertex = aProcVertexes.back();
348 aProcEdges.empty() ? aDirY : getOuterEdgeDirection(aProcEdges.back(), aNextVertex);
352 // When all edges of current component of connectivity are processed,
353 // we should repeat procedure for finding initial vertex in the remaining
354 if (!aMapVE.IsEmpty() && !aMapVE.Contains(aNextVertex)) {
355 aProcVertexes.clear();
358 aStartVertex = findStartVertex(aMapVE, aDirX, aDirY);
359 aProcVertexes.push_back(aStartVertex);
360 aNextVertex = aStartVertex;
361 aNextDir = aDirY.Reversed();
362 aCurNorm = aNorm.Reversed();
365 aCurVertex = aNextVertex;
369 if (theResultFaces.size() > 1)
370 fixIntersections(theResultFaces);
373 void GeomAlgoAPI_SketchBuilder::fixIntersections(
374 std::list<boost::shared_ptr<GeomAPI_Shape> >& theFaces)
376 BRepClass_FaceClassifier aClassifier;
378 std::list<boost::shared_ptr<GeomAPI_Shape> >::iterator anIter1 = theFaces.begin();
379 std::list<boost::shared_ptr<GeomAPI_Shape> >::iterator anIter2;
380 for (; anIter1 != theFaces.end(); anIter1++) {
382 for (++anIter2; anIter2 != theFaces.end(); anIter2++) {
383 const TopoDS_Face& aF1 = (*anIter1)->impl<TopoDS_Face>();
384 assert(aF1.ShapeType() == TopAbs_FACE); // all items in result list should be faces
385 TopExp_Explorer aVert2((*anIter2)->impl<TopoDS_Shape>(), TopAbs_VERTEX);
386 for (; aVert2.More(); aVert2.Next()) {
387 const TopoDS_Vertex& aV = (const TopoDS_Vertex&)aVert2.Current();
388 aClassifier.Perform(aF1, BRep_Tool::Pnt(aV), tolerance);
389 TopAbs_State aState = aClassifier.State();
390 if (aState != TopAbs_IN && aState != TopAbs_ON)
393 if (aVert2.More()) { // second shape is not inside first, change the shapes order and repeat comparision
394 const TopoDS_Face& aF2 = (*anIter2)->impl<TopoDS_Face>();
395 assert(aF2.ShapeType() == TopAbs_FACE); // all items in result list should be faces
396 TopExp_Explorer aVert1((*anIter1)->impl<TopoDS_Shape>(), TopAbs_VERTEX);
397 for (; aVert1.More(); aVert1.Next()) {
398 const TopoDS_Vertex& aV = (const TopoDS_Vertex&)aVert2.Current();
399 aClassifier.Perform(aF2, BRep_Tool::Pnt(aV), tolerance);
400 TopAbs_State aState = aClassifier.State();
401 if (aState != TopAbs_IN && aState != TopAbs_ON)
404 if (!aVert1.More()) { // first shape should be cut from the second
405 BRepAlgoAPI_Cut aCut((*anIter2)->impl<TopoDS_Shape>(), (*anIter1)->impl<TopoDS_Shape>());
407 TopExp_Explorer anExp(aCut.Shape(), TopAbs_FACE);
408 bool isFirstFace = true;
409 for (; anExp.More(); anExp.Next()) {
410 if (anExp.Current().ShapeType() != TopAbs_FACE) continue;
412 (*anIter2)->setImpl(new TopoDS_Shape(anExp.Current()));
415 boost::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
416 aShape->setImpl(new TopoDS_Shape(anExp.Current()));
417 theFaces.push_back(aShape);
421 } else { // second shape should be cut from the first
422 BRepAlgoAPI_Cut aCut((*anIter1)->impl<TopoDS_Shape>(), (*anIter2)->impl<TopoDS_Shape>());
424 TopExp_Explorer anExp(aCut.Shape(), TopAbs_FACE);
425 bool isFirstFace = true;
426 for (; anExp.More(); anExp.Next()) {
427 if (anExp.Current().ShapeType() != TopAbs_FACE) continue;
429 (*anIter1)->setImpl(new TopoDS_Shape(anExp.Current()));
432 boost::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
433 aShape->setImpl(new TopoDS_Shape(anExp.Current()));
434 theFaces.push_back(aShape);
442 // =================== Auxiliary functions ====================================
443 const TopoDS_Vertex& findStartVertex(const BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE,
444 const gp_Dir& theDirX, const gp_Dir& theDirY)
446 int aStartVertexInd = 1;
447 double aMaxX = -DBL_MAX;
448 double aMaxY = -DBL_MAX;
449 int aNbVert = theMapVE.Extent();
450 for (int i = 1; i <= aNbVert; i++) {
451 const TopoDS_Vertex& aV = (const TopoDS_Vertex&) theMapVE.FindKey(i);
452 const gp_Pnt& aVertPnt = BRep_Tool::Pnt(aV);
454 double aX = aVertPnt.XYZ().Dot(theDirX.XYZ());
455 double aY = aVertPnt.XYZ().Dot(theDirY.XYZ());
456 if ((aX > aMaxX || (fabs(aX - aMaxX) < tolerance && aY > aMaxY))
457 && theMapVE.FindFromIndex(i).Extent() > 1) {
463 return static_cast<const TopoDS_Vertex&>(theMapVE.FindKey(aStartVertexInd));
466 void findNextVertex(const TopoDS_Vertex& theStartVertex,
467 const BOPCol_IndexedDataMapOfShapeListOfShape& theVertexEdgeMap,
468 const gp_Dir& theStartDir, const gp_Dir& theNormal, TopoDS_Vertex& theNextVertex,
469 TopoDS_Edge& theNextEdge, gp_Dir& theNextDir)
471 theNextVertex = TopoDS_Vertex();
472 const BOPCol_ListOfShape& anEdgesList = theVertexEdgeMap.FindFromKey(theStartVertex);
473 int anEdgesNum = anEdgesList.Extent();
474 BOPCol_ListOfShape::Iterator aEdIter(anEdgesList);
475 double aBestEdgeProj = DBL_MAX;
476 for (; aEdIter.More(); aEdIter.Next()) {
477 const TopoDS_Edge& anEdge = static_cast<const TopoDS_Edge&>(aEdIter.Value());
478 gp_Dir aTang = getOuterEdgeDirection(anEdge, theStartVertex);
481 // The projection is normalized in segment (-1, 1),
482 // where (-1, 0] corresponds to the angles (pi/2, 0] between theStartDir and aTang
483 // and [0, 1) corresponds to the angles [0, -pi/2)
484 double aProj = (aTang.Dot(theStartDir) - 1.0) * 0.5;
485 if (anEdgesNum > 1 && fabs(fabs(aProj) - 1) < tolerance)
487 if (theStartDir.DotCross(aTang, theNormal) < tolerance)
490 if (aProj < aBestEdgeProj) {
491 aBestEdgeProj = aProj;
492 theNextEdge = anEdge;
493 TopExp_Explorer aVertExp(theNextEdge, TopAbs_VERTEX);
494 for (; aVertExp.More(); aVertExp.Next())
495 if (!aVertExp.Current().IsSame(theStartVertex)) {
496 theNextVertex = static_cast<const TopoDS_Vertex&>(aVertExp.Current());
497 theNextDir = getOuterEdgeDirection(anEdge, theNextVertex);
500 if (!aVertExp.More()) { // This edge is a full circle
501 TopoDS_Vertex aV1, aV2;
502 TopExp::Vertices(theNextEdge, aV1, aV2);
503 if (aV1.Orientation() == theStartVertex.Orientation())
507 theNextDir = getOuterEdgeDirection(anEdge, theNextVertex);
512 // Probably there are two tangent edges. We will take the edge differs from current one
513 if (theNextVertex.IsNull() && anEdgesNum == 2) {
514 BOPCol_ListOfShape::Iterator aEdIter(anEdgesList);
515 if (aEdIter.Value() == theNextEdge)
517 theNextEdge = static_cast<const TopoDS_Edge&>(aEdIter.Value());
518 TopoDS_Vertex aV1, aV2;
519 TopExp::Vertices(theNextEdge, aV1, aV2);
520 theNextVertex = theStartVertex.IsSame(aV1) ? aV2 : aV1;
521 theNextDir = getOuterEdgeDirection(theNextEdge, theNextVertex);
525 static void addEdgeToWire(const TopoDS_Edge& theEdge, const BRep_Builder& theBuilder,
526 TopoDS_Shape& theSpliceVertex, TopoDS_Wire& theWire)
528 TopoDS_Edge anEdge = theEdge;
529 bool isCurVertChanged = false;
530 TopoDS_Shape aCurVertChanged;
532 TopExp_Explorer aVertExp(theEdge, TopAbs_VERTEX);
533 for (; aVertExp.More(); aVertExp.Next()) {
534 const TopoDS_Shape& aVertex = aVertExp.Current();
535 if (aVertex.IsSame(theSpliceVertex)
536 && aVertex.Orientation() != theEdge.Orientation()) { // Current vertex is the last for the edge, so its orientation is wrong, need to revert the edge
540 if (!aVertex.IsSame(theSpliceVertex)) {
541 aCurVertChanged = aVertex;
542 isCurVertChanged = true;
545 theSpliceVertex = isCurVertChanged ? aCurVertChanged : aVertExp.Current();
547 theBuilder.Add(theWire, anEdge);
550 void createFace(const TopoDS_Vertex& theStartVertex,
551 const std::list<TopoDS_Edge>::iterator& theStartEdge,
552 const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
553 const gp_Pln& thePlane,
554 TopoDS_Face& theResFace)
556 TopoDS_Wire aResWire;
557 BRep_Builder aBuilder;
558 aBuilder.MakeWire(aResWire);
560 TopoDS_Vertex aCurVertex = theStartVertex;
561 std::list<TopoDS_Edge>::const_iterator anEdgeIter = theStartEdge;
562 for (; anEdgeIter != theEndOfEdges; anEdgeIter++) {
563 if (!anEdgeIter->IsNull())
564 addEdgeToWire(*anEdgeIter, aBuilder, aCurVertex, aResWire);
567 BRepBuilderAPI_MakeFace aFaceBuilder(thePlane, aResWire);
568 if (aFaceBuilder.Error() == BRepBuilderAPI_FaceDone)
569 theResFace = aFaceBuilder.Face();
572 void createWireList(const TopoDS_Vertex& theStartVertex,
573 const std::list<TopoDS_Edge>::iterator& theStartEdge,
574 const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
575 const std::set<TopoDS_Edge*>& theEdgesInLoops,
576 std::list<TopoDS_Wire>& theResWires)
578 BRep_Builder aBuilder;
579 bool needNewWire = true;
580 TopoDS_Vertex aCurVertex = theStartVertex;
582 std::list<TopoDS_Edge>::iterator anIter = theStartEdge;
583 while (anIter != theEndOfEdges) {
584 while (anIter != theEndOfEdges && needNewWire && theEdgesInLoops.count(&(*anIter)) != 0) {
585 TopExp_Explorer aVertExp(*anIter, TopAbs_VERTEX);
586 for (; aVertExp.More(); aVertExp.Next())
587 if (!aVertExp.Current().IsSame(aCurVertex)) {
588 aCurVertex = static_cast<const TopoDS_Vertex&>(aVertExp.Current());
593 if (anIter == theEndOfEdges)
596 if (needNewWire) { // The new wire should be created
598 aBuilder.MakeWire(aWire);
599 theResWires.push_back(aWire);
601 } else if (theEdgesInLoops.count(&(*anIter)) != 0) { // There was found the edge already used in loop.
602 // Current wire should be released and new one should started
607 addEdgeToWire(*anIter, aBuilder, aCurVertex, theResWires.back());
612 gp_Dir getOuterEdgeDirection(const TopoDS_Edge& theEdge, const TopoDS_Vertex& theVertex)
614 gp_Pnt aVertexPnt = BRep_Tool::Pnt(theVertex);
616 // Convert the edge to the curve to calculate the tangency.
617 // There should be only one curve in the edge.
618 double aFirst, aLast;
619 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(theEdge, aFirst, aLast);
623 // A direction is determined not in the boundary points but in the points with small shift.
624 // It was done to avoid tangency between circle and other edge in the shared vertex.
625 aCurve->D1(aFirst + shift > aLast ? aFirst : aFirst + shift, aPnt, aTang);
626 aCurve->D0(aFirst, aPnt);
627 if (aVertexPnt.IsEqual(aPnt, tolerance))
628 return gp_Dir(aTang.Reversed());
630 aCurve->D1(aLast - shift < aFirst ? aLast : aLast - shift, aPnt, aTang);
631 return gp_Dir(aTang);
634 void removeWasteEdges(std::list<TopoDS_Vertex>::iterator& theStartVertex,
635 std::list<TopoDS_Edge>::iterator& theStartEdge,
636 const std::list<TopoDS_Vertex>::iterator& theEndOfVertexes,
637 const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
638 BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE)
640 bool isVertStep = true;
641 while (theStartVertex != theEndOfVertexes && theStartEdge != theEndOfEdges) {
642 BOPCol_ListOfShape& aBunch = theMapVE.ChangeFromKey(*theStartVertex);
643 BOPCol_ListOfShape::Iterator anApprEdge(aBunch);
644 for (; anApprEdge.More(); anApprEdge.Next())
645 if (anApprEdge.Value().IsSame(*theStartEdge))
647 if (anApprEdge.More())
648 aBunch.Remove(anApprEdge);
654 // check current vertex to be a branching point
655 // if so, it will be a new starting point to find a loop
656 if (aBunch.Size() > 1)
659 isVertStep = !isVertStep;
662 // The map of vertex-edges may be changed
663 BOPCol_IndexedDataMapOfShapeListOfShape aMapVECopy;
664 BOPCol_IndexedDataMapOfShapeListOfShape::Iterator aMapIter(theMapVE);
665 for (int ind = 1; aMapIter.More(); aMapIter.Next(), ind++)
666 if (!aMapIter.Value().IsEmpty())
667 aMapVECopy.Add(theMapVE.FindKey(ind), aMapIter.Value());
669 theMapVE.Exchange(aMapVECopy);