Salome HOME
Issue #608: Usage of OCCT in interface -- Remove OCCT from *Export interfaces --...
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_SketchBuilder.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_SketchBuilder.cpp
4 // Created:     02 Jun 2014
5 // Author:      Artem ZHIDKOV
6
7 #include <GeomAlgoAPI_SketchBuilder.h>
8 #include <GeomAPI_PlanarEdges.h>
9
10 #include <set>
11
12 #include <gp_Dir.hxx>
13 #include <gp_Pln.hxx>
14
15 #include <BOPAlgo_Builder.hxx>
16 #include <BOPAlgo_Operation.hxx>
17 #include <BOPAlgo_PaveFiller.hxx>
18 #include <BOPTools.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRep_Curve3D.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepAlgoAPI_Cut.hxx>
23 #include <BRepBuilderAPI_MakeFace.hxx>
24 #include <BRepClass_FaceClassifier.hxx>
25 #include <Geom_Curve.hxx>
26 #include <Geom_Plane.hxx>
27 #include <TopExp.hxx>
28 #include <TopExp_Explorer.hxx>
29 #include <TopoDS_Edge.hxx>
30 #include <TopoDS_Face.hxx>
31 #include <TopoDS_Vertex.hxx>
32 #include <TopoDS_Wire.hxx>
33
34 #include <Precision.hxx>
35
36 #include <assert.h>
37
38 #ifndef DBL_MAX
39 #define DBL_MAX 1.7976931348623158e+308 
40 #endif
41
42 const double tolerance = Precision::Confusion();
43 // This value helps to find direction on the boundaries of curve.
44 // It is not significant for lines, but is used for circles to avoid 
45 // wrong directions of movement (when two edges are tangent on the certain vertex)
46 const double shift = acos(1.0 - 3.0 * tolerance);
47
48 /// \brief Search first vertex - the vertex with lowest x coordinate, which is used in 2 edges at least
49 static const TopoDS_Vertex& findStartVertex(const BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE,
50                                            const gp_Dir& theDirX, const gp_Dir& theDirY);
51
52 /// \brief Search the vertex on the sketch candidate to be the next one in the loop
53 static void findNextVertex(const TopoDS_Vertex& theStartVertex,
54                            const BOPCol_IndexedDataMapOfShapeListOfShape& theVertexEdgeMap,
55                            const gp_Dir& theStartDir, const gp_Dir& theNormal,
56                            TopoDS_Vertex& theNextVertex, TopoDS_Edge& theNextEdge,
57                            gp_Dir& theNextDir);
58
59 /// \brief Create planar face using the edges surrounding it
60 static void createFace(const TopoDS_Vertex& theStartVertex,
61                        const std::list<TopoDS_Edge>::iterator& theStartEdge,
62                        const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
63                        const gp_Pln& thePlane, TopoDS_Face& theResFace);
64
65 /// \bief Create planar wire
66 static void createWireList(const TopoDS_Vertex& theStartVertex,
67                            const std::list<TopoDS_Edge>::iterator& theStartEdge,
68                            const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
69                            const std::set<TopoDS_Edge*>& theEdgesInLoops,
70                            std::list<TopoDS_Wire>& theResWires);
71
72 /// \brief Calculate outer tengency on the edge in specified vertex
73 static gp_Dir getOuterEdgeDirection(const TopoDS_Edge& theEdge, const TopoDS_Vertex& theVertex);
74
75 /// \brief Unnecessary edges will be removed from the map.
76 ///        Positions of iterator will be updated
77 static void removeWasteEdges(std::list<TopoDS_Vertex>::iterator& theStartVertex,
78                              std::list<TopoDS_Edge>::iterator& theStartEdge,
79                              const std::list<TopoDS_Vertex>::iterator& theEndOfVertexes,
80                              const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
81                              BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE);
82
83
84 void GeomAlgoAPI_SketchBuilder::createFaces(
85     const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
86     const std::shared_ptr<GeomAPI_Dir>& theDirX,
87     const std::shared_ptr<GeomAPI_Dir>& theNorm,
88     const std::list<std::shared_ptr<GeomAPI_Shape> >& theFeatures,
89     std::list<std::shared_ptr<GeomAPI_Shape> >& theResultFaces,
90     std::list<std::shared_ptr<GeomAPI_Shape> >& theResultWires)
91 {
92   if (theFeatures.empty())
93     return;
94
95   // Create the list of edges with shared vertexes
96   BOPAlgo_Builder aBuilder;
97   BOPAlgo_PaveFiller aPF;
98   TopoDS_Shape aFeaturesCompound;
99
100   // Obtain only edges from the features list
101   std::list<std::shared_ptr<GeomAPI_Shape> > anEdges;
102   std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aFeatIt = theFeatures.begin();
103   for (; aFeatIt != theFeatures.end(); aFeatIt++) {
104     std::shared_ptr<GeomAPI_Shape> aShape(*aFeatIt);
105     const TopoDS_Edge& anEdge = aShape->impl<TopoDS_Edge>();
106     if (anEdge.ShapeType() == TopAbs_EDGE)
107       anEdges.push_back(aShape);
108   }
109
110   if (anEdges.size() == 1) {  // If there is only one feature, BOPAlgo_Builder will decline to work. Need to process it anyway
111     aFeaturesCompound = anEdges.front()->impl<TopoDS_Shape>();
112   } else {
113     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = anEdges.begin();
114     for (; anIt != anEdges.end(); anIt++) {
115       std::shared_ptr<GeomAPI_Shape> aPreview(*anIt);
116       aBuilder.AddArgument(aPreview->impl<TopoDS_Edge>());
117     }
118     aPF.SetArguments(aBuilder.Arguments());
119     aPF.Perform();
120     int aErr = aPF.ErrorStatus();
121     if (aErr)
122       return;
123     aBuilder.PerformWithFiller(aPF);
124     aErr = aBuilder.ErrorStatus();
125     if (aErr)
126       return;
127     aFeaturesCompound = aBuilder.Shape();
128   }
129
130   BOPCol_IndexedDataMapOfShapeListOfShape aMapVE;  // map between vertexes and edges
131   BOPTools::MapShapesAndAncestors(aFeaturesCompound, TopAbs_VERTEX, TopAbs_EDGE, aMapVE);
132   if (aMapVE.IsEmpty())  // in case of not-initialized circle
133     return;
134
135   gp_Dir aDirX = theDirX->impl<gp_Dir>();
136   gp_Dir aNorm = theNorm->impl<gp_Dir>();
137   gp_Dir aDirY = aNorm.Crossed(aDirX);
138
139   gp_Pln aPlane(theOrigin->impl<gp_Pnt>(), aNorm);
140
141   // Set of edges used in loops
142   std::set<TopoDS_Edge*> anEdgesInLoops;
143   // Lists for processed vertexes and edges
144   std::list<TopoDS_Vertex> aProcVertexes;
145   std::list<TopoDS_Edge> aProcEdges;
146
147   // Search the start vertex
148   TopoDS_Vertex aStartVertex = findStartVertex(aMapVE, aDirX, aDirY);
149   aProcVertexes.push_back(aStartVertex);
150
151   TopoDS_Vertex aCurVertex = aStartVertex;
152   gp_Dir aCurDir = aDirY.Reversed();
153   gp_Dir aCurNorm = aNorm.Reversed();
154
155   // Go through the edges and find loops
156   TopoDS_Vertex aNextVertex;
157   TopoDS_Edge aBindingEdge;
158   gp_Dir aNextDir;
159   while (aMapVE.Extent() > 0) {
160     if (aCurVertex.IsNull())
161       return;
162     if (!aProcEdges.empty())
163       aBindingEdge = aProcEdges.back();
164     findNextVertex(aCurVertex, aMapVE, aCurDir, aCurNorm, aNextVertex, aBindingEdge, aNextDir);
165     aCurNorm = aNorm;
166
167     // Try to find next vertex in the list of already processed
168     bool isLoopFound = false;
169     std::list<TopoDS_Vertex>::iterator aVertIter = aProcVertexes.begin();
170     std::list<TopoDS_Edge>::iterator anEdgeIter = aProcEdges.begin();
171     for (; aVertIter != aProcVertexes.end(); aVertIter++) {
172       if (aVertIter->IsSame(aNextVertex)) {
173         isLoopFound = true;
174         break;
175       }
176       if (anEdgeIter != aProcEdges.end())
177         anEdgeIter++;
178     }
179
180     bool isCircleFound = (isLoopFound && anEdgeIter == aProcEdges.end());
181     aProcVertexes.push_back(aNextVertex);
182     aProcEdges.push_back(aBindingEdge);
183
184     if (isLoopFound) {
185       // If the binding edge is a full circle, then it may be added recently. Need to update edge iterator
186       if (isCircleFound) {
187         anEdgeIter = aProcEdges.end();
188         anEdgeIter--;
189       }
190       else if (aVertIter != aProcVertexes.begin()) {
191         // Check the orientation of the loop
192         gp_Dir aCN = getOuterEdgeDirection(*anEdgeIter, *aVertIter);
193         gp_Dir aCP = getOuterEdgeDirection(aProcEdges.back(), *aVertIter);
194         aCN.Reverse();
195         aCP.Reverse();
196         if (aCN.DotCross(aCP, aNorm) < -tolerance) {
197           // The found loop has wrong orientation and may contain sub-loops.
198           // Need to check it once again with another initial direction.
199           aCurVertex = *aVertIter;
200           do {
201             aProcVertexes.pop_back();
202             aProcEdges.pop_back();
203           } while (aCurVertex != aProcVertexes.back());
204           aCurDir = aCN.Reversed();
205           aCurNorm = aNorm.Reversed();
206           continue;
207         }
208       }
209
210       if (!isCircleFound && anEdgeIter != aProcEdges.end() && 
211           anEdgeIter->IsSame(aProcEdges.back())) { // The loop on the same edge taken twice
212         aProcVertexes.pop_back();
213         aProcEdges.pop_back();
214         aCurVertex = aProcVertexes.back();
215         aCurDir = getOuterEdgeDirection(aProcEdges.back(), aCurVertex);
216         aCurNorm = aNorm.Reversed();
217         continue;
218       }
219
220       // When the orientation is correct or the edges looped through
221       // the first element, create new face and remove unnecessary edges.
222       TopoDS_Face aPatch;
223       createFace(*aVertIter, anEdgeIter, aProcEdges.end(), aPlane, aPatch);
224       if (!aPatch.IsNull()) {
225         std::shared_ptr<GeomAPI_Shape> aFace(new GeomAPI_Shape);
226         aFace->setImpl(new TopoDS_Face(aPatch));
227         theResultFaces.push_back(aFace);
228       }
229       // push the edges used in the loop to the map
230       std::list<TopoDS_Edge>::iterator anIter;
231       for (anIter = anEdgeIter; anIter != aProcEdges.end(); anIter++)
232         anEdgesInLoops.insert(&(*anIter));
233       // remove unnecessary edges
234       std::list<TopoDS_Vertex>::iterator aCopyVLoop = aVertIter;
235       std::list<TopoDS_Edge>::iterator aCopyELoop = anEdgeIter;
236       removeWasteEdges(aVertIter, anEdgeIter, aProcVertexes.end(), aProcEdges.end(), aMapVE);
237
238       // revert the list of remaining edges
239       std::list<TopoDS_Vertex> aRemainVertexes;
240       for (; aVertIter != aProcVertexes.end(); aVertIter++)
241         aRemainVertexes.push_front(*aVertIter);
242       std::list<TopoDS_Edge> aRemainEdges;
243       for (; anEdgeIter != aProcEdges.end(); anEdgeIter++)
244         aRemainEdges.push_front(*anEdgeIter);
245       // remove edges and vertexes used in the loop and add remaining ones
246       if (aCopyVLoop != aProcVertexes.begin()) {
247         aVertIter = aCopyVLoop;
248         aVertIter--;
249       } else
250         aVertIter = aProcVertexes.end();
251       aProcVertexes.erase(aCopyVLoop, aProcVertexes.end());
252       aProcVertexes.insert(aProcVertexes.end(), aRemainVertexes.begin(), aRemainVertexes.end());
253       if (aCopyELoop != aProcEdges.begin()) {
254         anEdgeIter = aCopyELoop;
255         anEdgeIter--;
256       } else
257         anEdgeIter = aProcEdges.end();
258       aProcEdges.erase(aCopyELoop, aProcEdges.end());
259       aProcEdges.insert(aProcEdges.end(), aRemainEdges.begin(), aRemainEdges.end());
260
261       if (aVertIter == aProcVertexes.end())
262         aVertIter = aProcVertexes.begin();
263       else
264         aVertIter++;
265       if (anEdgeIter == aProcEdges.end())
266         anEdgeIter = aProcEdges.begin();
267       else
268         anEdgeIter++;
269       aCopyVLoop = aVertIter;
270       aCopyELoop = anEdgeIter;
271
272       if (aVertIter != aProcVertexes.end() && 
273           aMapVE.Contains(*aVertIter) && aMapVE.FindFromKey(*aVertIter).Extent() <= 1)
274         removeWasteEdges(aVertIter, anEdgeIter, aProcVertexes.end(), aProcEdges.end(), aMapVE);
275       if (aCopyVLoop != aVertIter)
276         aProcVertexes.erase(aCopyVLoop, aVertIter);
277       if (aCopyELoop != anEdgeIter)
278         aProcEdges.erase(aCopyELoop, anEdgeIter);
279
280       // Check whether the next vertex already exists
281       if (aVertIter != aProcVertexes.end())
282         aVertIter++;
283       if (aVertIter != aProcVertexes.end() && anEdgeIter != aProcEdges.end()) {
284         aNextVertex = *aVertIter;
285         aNextDir = getOuterEdgeDirection(*anEdgeIter, aNextVertex);
286         aProcVertexes.erase(++aVertIter, aProcVertexes.end());
287         aProcEdges.erase(++anEdgeIter, aProcEdges.end());
288       } else {
289         // Recalculate current vertex and current direction
290         aProcEdges.clear();
291         aProcVertexes.clear();
292         if (aMapVE.Extent() > 0) {
293           aNextVertex = findStartVertex(aMapVE, aDirX, aDirY);
294           aProcVertexes.push_back(aNextVertex);
295         }
296         aNextDir = aDirY.Reversed();
297         aCurNorm = aNorm.Reversed();
298       }
299     }
300
301     // if next vertex connected only to alone edge, this is a part of wire (not a closed loop),
302     // we need to go back through the list of already checked edges to find a branching vertex
303     if (!aMapVE.IsEmpty() && aMapVE.Contains(aNextVertex)
304         && aMapVE.FindFromKey(aNextVertex).Size() == 1) {
305       std::list<TopoDS_Vertex>::reverse_iterator aVRIter = aProcVertexes.rbegin();
306       std::list<TopoDS_Edge>::reverse_iterator aERIter = aProcEdges.rbegin();
307       if (aVRIter != aProcVertexes.rend())
308         aVRIter++;
309       if (aERIter != aProcEdges.rend())
310         aERIter++;
311
312       for (; aERIter != aProcEdges.rend(); aERIter++, aVRIter++)
313         if (aMapVE.FindFromKey(*aVRIter).Size() > 2)
314           break;
315       if (aERIter != aProcEdges.rend()
316           || (aVRIter != aProcVertexes.rend() && aMapVE.FindFromKey(*aVRIter).Size() == 1)) {  // the branching vertex was found or current list of edges is a wire without branches
317         std::list<TopoDS_Edge>::iterator aEIter;
318         TopoDS_Edge aCurEdge;
319         if (aERIter != aProcEdges.rend()) {
320           aEIter = aERIter.base();
321           aCurEdge = *aERIter;
322         } else
323           aEIter = aProcEdges.begin();
324         std::list<TopoDS_Wire> aTail;
325         createWireList(*aVRIter, aEIter, aProcEdges.end(), anEdgesInLoops, aTail);
326         std::list<TopoDS_Wire>::const_iterator aTailIter = aTail.begin();
327         for (; aTailIter != aTail.end(); aTailIter++)
328           if (!aTailIter->IsNull()) {
329             std::shared_ptr<GeomAPI_Shape> aWire(new GeomAPI_Shape);
330             aWire->setImpl(new TopoDS_Shape(*aTailIter));
331             theResultWires.push_back(aWire);
332           }
333         std::list<TopoDS_Vertex>::iterator aVIter = aVRIter.base();
334         std::list<TopoDS_Edge>::iterator aEItCopy = aEIter;
335         removeWasteEdges(--aVIter, aEItCopy, aProcVertexes.end(), aProcEdges.end(), aMapVE);
336
337         aProcEdges.erase(aEIter, aProcEdges.end());
338         aVIter = aVRIter.base();
339         aProcVertexes.erase(aVIter, aProcVertexes.end());
340
341         if (!aProcVertexes.empty()) {
342           aNextVertex = aProcVertexes.back();
343           if (!aCurEdge.IsNull())
344             aNextDir = getOuterEdgeDirection(aCurEdge, aNextVertex);
345         }
346       } else {  // there is no branching vertex in the list of proceeded,
347                 // so we should revert the list and go the opposite way
348         aProcVertexes.reverse();
349         aProcEdges.reverse();
350         aNextVertex = aProcVertexes.back();
351         aNextDir =
352             aProcEdges.empty() ? aDirY : getOuterEdgeDirection(aProcEdges.back(), aNextVertex);
353       }
354     }
355
356     // When all edges of current component of connectivity are processed,
357     // we should repeat procedure for finding initial vertex in the remaining
358     if (!aMapVE.IsEmpty() && !aMapVE.Contains(aNextVertex)) {
359       aProcVertexes.clear();
360       aProcEdges.clear();
361
362       aStartVertex = findStartVertex(aMapVE, aDirX, aDirY);
363       aProcVertexes.push_back(aStartVertex);
364       aNextVertex = aStartVertex;
365       aNextDir = aDirY.Reversed();
366       aCurNorm = aNorm.Reversed();
367     }
368
369     aCurVertex = aNextVertex;
370     aCurDir = aNextDir;
371   }
372
373   if (theResultFaces.size() > 1)
374     fixIntersections(theResultFaces);
375 }
376
377 void GeomAlgoAPI_SketchBuilder::createFaces(const std::shared_ptr<GeomAPI_Pnt>& theOrigin,
378                                             const std::shared_ptr<GeomAPI_Dir>& theDirX,
379                                             const std::shared_ptr<GeomAPI_Dir>& theNorm,
380                                             const std::shared_ptr<GeomAPI_Shape>& theWire,
381                                             std::list<std::shared_ptr<GeomAPI_Shape> >& theResultFaces)
382 {
383   std::list<std::shared_ptr<GeomAPI_Shape> > aFilteredWires;
384   std::shared_ptr<GeomAPI_PlanarEdges> aWire = 
385     std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(theWire);
386   if(aWire) {
387     // Filter wires, return only faces.
388     createFaces(theOrigin, theDirX, theNorm,
389                 aWire->getEdges(), theResultFaces, aFilteredWires);
390
391     return;
392   } else { // it may be only one circle
393     std::shared_ptr<GeomAPI_Edge> anEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(theWire);
394     if (anEdge) {
395       std::list<std::shared_ptr<GeomAPI_Shape> > aList;
396       aList.push_back(anEdge);
397       createFaces(theOrigin, theDirX, theNorm,
398                   aList, theResultFaces, aFilteredWires);
399     }
400   }
401 }
402
403
404 void GeomAlgoAPI_SketchBuilder::fixIntersections(
405     std::list<std::shared_ptr<GeomAPI_Shape> >& theFaces)
406 {
407   BRepClass_FaceClassifier aClassifier;
408
409   std::list<std::shared_ptr<GeomAPI_Shape> >::iterator anIter1 = theFaces.begin();
410   std::list<std::shared_ptr<GeomAPI_Shape> >::iterator anIter2;
411   for (; anIter1 != theFaces.end(); anIter1++) {
412     anIter2 = anIter1;
413     for (++anIter2; anIter2 != theFaces.end(); anIter2++) {
414       const TopoDS_Face& aF1 = (*anIter1)->impl<TopoDS_Face>();
415       assert(aF1.ShapeType() == TopAbs_FACE);  // all items in result list should be faces
416       TopExp_Explorer aVert2((*anIter2)->impl<TopoDS_Shape>(), TopAbs_VERTEX);
417       for (; aVert2.More(); aVert2.Next()) {
418         const TopoDS_Vertex& aV = (const TopoDS_Vertex&)aVert2.Current();
419         aClassifier.Perform(aF1, BRep_Tool::Pnt(aV), tolerance);
420         TopAbs_State aState = aClassifier.State();
421         if (aState != TopAbs_IN && aState != TopAbs_ON)
422           break;
423       }
424       if (aVert2.More()) {  // second shape is not inside first, change the shapes order and repeat comparision
425         const TopoDS_Face& aF2 = (*anIter2)->impl<TopoDS_Face>();
426         assert(aF2.ShapeType() == TopAbs_FACE);  // all items in result list should be faces
427         TopExp_Explorer aVert1((*anIter1)->impl<TopoDS_Shape>(), TopAbs_VERTEX);
428         for (; aVert1.More(); aVert1.Next()) {
429           const TopoDS_Vertex& aV = (const TopoDS_Vertex&)aVert2.Current();
430           aClassifier.Perform(aF2, BRep_Tool::Pnt(aV), tolerance);
431           TopAbs_State aState = aClassifier.State();
432           if (aState != TopAbs_IN && aState != TopAbs_ON)
433             break;
434         }
435         if (!aVert1.More()) {  // first shape should be cut from the second
436           BRepAlgoAPI_Cut aCut((*anIter2)->impl<TopoDS_Shape>(), (*anIter1)->impl<TopoDS_Shape>());
437           aCut.Build();
438           TopExp_Explorer anExp(aCut.Shape(), TopAbs_FACE);
439           bool isFirstFace = true;
440           for (; anExp.More(); anExp.Next()) {
441             if (anExp.Current().ShapeType() != TopAbs_FACE) continue;
442             if (isFirstFace) {
443               (*anIter2)->setImpl(new TopoDS_Shape(anExp.Current()));
444               isFirstFace = false;
445             } else {
446               std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
447               aShape->setImpl(new TopoDS_Shape(anExp.Current()));
448               theFaces.push_back(aShape);
449             }
450           }
451         }
452       } else {  // second shape should be cut from the first
453         BRepAlgoAPI_Cut aCut((*anIter1)->impl<TopoDS_Shape>(), (*anIter2)->impl<TopoDS_Shape>());
454         aCut.Build();
455         TopExp_Explorer anExp(aCut.Shape(), TopAbs_FACE);
456         bool isFirstFace = true;
457         for (; anExp.More(); anExp.Next()) {
458           if (anExp.Current().ShapeType() != TopAbs_FACE) continue;
459           if (isFirstFace) {
460             (*anIter1)->setImpl(new TopoDS_Shape(anExp.Current()));
461             isFirstFace = false;
462           } else {
463             std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
464             aShape->setImpl(new TopoDS_Shape(anExp.Current()));
465             theFaces.push_back(aShape);
466           }
467         }
468       }
469     }
470   }
471 }
472
473 // =================== Auxiliary functions ====================================
474 const TopoDS_Vertex& findStartVertex(const BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE,
475                                     const gp_Dir& theDirX, const gp_Dir& theDirY)
476 {
477   int aStartVertexInd = 1;
478   double aMaxX = -DBL_MAX;
479   double aMaxY = -DBL_MAX;
480   int aNbVert = theMapVE.Extent();
481   for (int i = 1; i <= aNbVert; i++) {
482     const TopoDS_Vertex& aV = (const TopoDS_Vertex&) theMapVE.FindKey(i);
483     const gp_Pnt& aVertPnt = BRep_Tool::Pnt(aV);
484
485     double aX = aVertPnt.XYZ().Dot(theDirX.XYZ());
486     double aY = aVertPnt.XYZ().Dot(theDirY.XYZ());
487     if ((aX > aMaxX || (fabs(aX - aMaxX) < tolerance && aY > aMaxY))
488         && theMapVE.FindFromIndex(i).Extent() > 1) {
489       aMaxX = aX;
490       aMaxY = aY;
491       aStartVertexInd = i;
492     }
493   }
494   return static_cast<const TopoDS_Vertex&>(theMapVE.FindKey(aStartVertexInd));
495 }
496
497 void findNextVertex(const TopoDS_Vertex& theStartVertex,
498                     const BOPCol_IndexedDataMapOfShapeListOfShape& theVertexEdgeMap,
499                     const gp_Dir& theStartDir, const gp_Dir& theNormal, TopoDS_Vertex& theNextVertex,
500                     TopoDS_Edge& theNextEdge, gp_Dir& theNextDir)
501 {
502   theNextVertex = TopoDS_Vertex();
503   const BOPCol_ListOfShape& anEdgesList = theVertexEdgeMap.FindFromKey(theStartVertex);
504   int anEdgesNum = anEdgesList.Extent();
505   BOPCol_ListOfShape::Iterator aEdIter(anEdgesList);
506   double aBestEdgeProj = DBL_MAX;
507   for (; aEdIter.More(); aEdIter.Next()) {
508     const TopoDS_Edge& anEdge = static_cast<const TopoDS_Edge&>(aEdIter.Value());
509     gp_Dir aTang = getOuterEdgeDirection(anEdge, theStartVertex);
510     aTang.Reverse();
511
512     // The projection is normalized in segment (-1, 1),
513     // where (-1, 0] corresponds to the angles (pi/2, 0] between theStartDir and aTang
514     // and [0, 1) corresponds to the angles [0, -pi/2)
515     double aProj = (aTang.Dot(theStartDir) - 1.0) * 0.5;
516     if (anEdgesNum > 1 && fabs(fabs(aProj) - 1) < tolerance)
517       continue;
518     if (theStartDir.DotCross(aTang, theNormal) < tolerance)
519       aProj *= -1.0;
520
521     if (aProj < aBestEdgeProj) {
522       aBestEdgeProj = aProj;
523       theNextEdge = anEdge;
524       TopExp_Explorer aVertExp(theNextEdge, TopAbs_VERTEX);
525       for (; aVertExp.More(); aVertExp.Next())
526         if (!aVertExp.Current().IsSame(theStartVertex)) {
527           theNextVertex = static_cast<const TopoDS_Vertex&>(aVertExp.Current());
528           theNextDir = getOuterEdgeDirection(anEdge, theNextVertex);
529           break;
530         }
531       if (!aVertExp.More()) {  // This edge is a full circle
532         TopoDS_Vertex aV1, aV2;
533         TopExp::Vertices(theNextEdge, aV1, aV2);
534         if (aV1.Orientation() == theStartVertex.Orientation())
535           theNextVertex = aV2;
536         else
537           theNextVertex = aV1;
538         theNextDir = getOuterEdgeDirection(anEdge, theNextVertex);
539       }
540     }
541   }
542
543   // Probably there are two tangent edges. We will take the edge differs from current one
544   if (theNextVertex.IsNull() && anEdgesNum == 2) {
545     BOPCol_ListOfShape::Iterator aEdIter(anEdgesList);
546     if (aEdIter.Value() == theNextEdge)
547       aEdIter.Next();
548     theNextEdge = static_cast<const TopoDS_Edge&>(aEdIter.Value());
549     TopoDS_Vertex aV1, aV2;
550     TopExp::Vertices(theNextEdge, aV1, aV2);
551     theNextVertex = theStartVertex.IsSame(aV1) ? aV2 : aV1;
552     theNextDir = getOuterEdgeDirection(theNextEdge, theNextVertex);
553   }
554 }
555
556 static void addEdgeToWire(const TopoDS_Edge& theEdge, const BRep_Builder& theBuilder,
557                           TopoDS_Shape& theSpliceVertex, TopoDS_Wire& theWire)
558 {
559   TopoDS_Edge anEdge = theEdge;
560   bool isCurVertChanged = false;
561   TopoDS_Shape aCurVertChanged;
562
563   TopExp_Explorer aVertExp(theEdge, TopAbs_VERTEX);
564   for (; aVertExp.More(); aVertExp.Next()) {
565     const TopoDS_Shape& aVertex = aVertExp.Current();
566     if (aVertex.IsSame(theSpliceVertex)
567         && aVertex.Orientation() != theEdge.Orientation()) {  // Current vertex is the last for the edge, so its orientation is wrong, need to revert the edge
568       anEdge.Reverse();
569       break;
570     }
571     if (!aVertex.IsSame(theSpliceVertex)) {
572       aCurVertChanged = aVertex;
573       isCurVertChanged = true;
574     }
575   }
576   theSpliceVertex = isCurVertChanged ? aCurVertChanged : aVertExp.Current();
577
578   theBuilder.Add(theWire, anEdge);
579 }
580
581 void createFace(const TopoDS_Vertex& theStartVertex,
582                 const std::list<TopoDS_Edge>::iterator& theStartEdge,
583                 const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
584                 const gp_Pln& thePlane,
585                 TopoDS_Face& theResFace)
586 {
587   TopoDS_Wire aResWire;
588   BRep_Builder aBuilder;
589   aBuilder.MakeWire(aResWire);
590
591   TopoDS_Vertex aCurVertex = theStartVertex;
592   std::list<TopoDS_Edge>::const_iterator anEdgeIter = theStartEdge;
593   for (; anEdgeIter != theEndOfEdges; anEdgeIter++) {
594     if (!anEdgeIter->IsNull())
595       addEdgeToWire(*anEdgeIter, aBuilder, aCurVertex, aResWire);
596   }
597
598   BRepBuilderAPI_MakeFace aFaceBuilder(thePlane, aResWire);
599   if (aFaceBuilder.Error() == BRepBuilderAPI_FaceDone)
600     theResFace = aFaceBuilder.Face();
601 }
602
603 void createWireList(const TopoDS_Vertex& theStartVertex,
604                     const std::list<TopoDS_Edge>::iterator& theStartEdge,
605                     const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
606                     const std::set<TopoDS_Edge*>& theEdgesInLoops,
607                     std::list<TopoDS_Wire>& theResWires)
608 {
609   BRep_Builder aBuilder;
610   bool needNewWire = true;
611   TopoDS_Vertex aCurVertex = theStartVertex;
612
613   std::list<TopoDS_Edge>::iterator anIter = theStartEdge;
614   while (anIter != theEndOfEdges) {
615     while (anIter != theEndOfEdges && needNewWire && theEdgesInLoops.count(&(*anIter)) != 0) {
616       TopExp_Explorer aVertExp(*anIter, TopAbs_VERTEX);
617       for (; aVertExp.More(); aVertExp.Next())
618         if (!aVertExp.Current().IsSame(aCurVertex)) {
619           aCurVertex = static_cast<const TopoDS_Vertex&>(aVertExp.Current());
620           break;
621         }
622       anIter++;
623     }
624     if (anIter == theEndOfEdges)
625       break;
626
627     if (needNewWire) {  // The new wire should be created
628       TopoDS_Wire aWire;
629       aBuilder.MakeWire(aWire);
630       theResWires.push_back(aWire);
631       needNewWire = false;
632     } else if (theEdgesInLoops.count(&(*anIter)) != 0) {  // There was found the edge already used in loop.
633                                                           // Current wire should be released and new one should started
634       needNewWire = true;
635       continue;
636     }
637
638     addEdgeToWire(*anIter, aBuilder, aCurVertex, theResWires.back());
639     anIter++;
640   }
641 }
642
643 gp_Dir getOuterEdgeDirection(const TopoDS_Edge& theEdge, const TopoDS_Vertex& theVertex)
644 {
645   gp_Pnt aVertexPnt = BRep_Tool::Pnt(theVertex);
646
647   // Convert the edge to the curve to calculate the tangency.
648   // There should be only one curve in the edge.
649   double aFirst, aLast;
650   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(theEdge, aFirst, aLast);
651
652   gp_Pnt aPnt;
653   gp_Vec aTang;
654   // A direction is determined not in the boundary points but in the points with small shift.
655   // It was done to avoid tangency between circle and other edge in the shared vertex.
656   aCurve->D1(aFirst + shift > aLast ? aFirst : aFirst + shift, aPnt, aTang);
657   aCurve->D0(aFirst, aPnt);
658   if (aVertexPnt.IsEqual(aPnt, tolerance))
659     return gp_Dir(aTang.Reversed());
660
661   aCurve->D1(aLast - shift < aFirst ? aLast : aLast - shift, aPnt, aTang);
662   return gp_Dir(aTang);
663 }
664
665 void removeWasteEdges(std::list<TopoDS_Vertex>::iterator& theStartVertex,
666                       std::list<TopoDS_Edge>::iterator& theStartEdge,
667                       const std::list<TopoDS_Vertex>::iterator& theEndOfVertexes,
668                       const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
669                       BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE)
670 {
671   bool isVertStep = true;
672   while (theStartVertex != theEndOfVertexes && theStartEdge != theEndOfEdges) {
673     BOPCol_ListOfShape& aBunch = theMapVE.ChangeFromKey(*theStartVertex);
674     BOPCol_ListOfShape::Iterator anApprEdge(aBunch);
675     for (; anApprEdge.More(); anApprEdge.Next())
676       if (anApprEdge.Value().IsSame(*theStartEdge))
677         break;
678     if (anApprEdge.More())
679       aBunch.Remove(anApprEdge);
680
681     if (isVertStep)
682       theStartVertex++;
683     else {
684       theStartEdge++;
685       // check current vertex to be a branching point
686       // if so, it will be a new starting point to find a loop
687       if (aBunch.Size() > 1)
688         break;
689     }
690     isVertStep = !isVertStep;
691   }
692
693   // The map of vertex-edges may be changed
694   BOPCol_IndexedDataMapOfShapeListOfShape aMapVECopy;
695   BOPCol_IndexedDataMapOfShapeListOfShape::Iterator aMapIter(theMapVE);
696   for (int ind = 1; aMapIter.More(); aMapIter.Next(), ind++)
697     if (!aMapIter.Value().IsEmpty())
698       aMapVECopy.Add(theMapVE.FindKey(ind), aMapIter.Value());
699   theMapVE.Clear();
700   theMapVE.Exchange(aMapVECopy);
701 }
702