-#include <TopoDS_Face.hxx>
-#include <TopoDS_Vertex.hxx>
-#include <TopoDS_Wire.hxx>
-
-#include <Precision.hxx>
-
-#ifndef DBL_MAX
-#define DBL_MAX 1.7976931348623158e+308
-#endif
-
-
-const double tolerance = Precision::Confusion();
-
-/// \brief Search first vertex - the vertex with lowest x coordinate, which is used in 2 edges at least
-static const TopoDS_Shape& findStartVertex(
- const BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE,
- const gp_Dir& theDirX, const gp_Dir& theDirY);
-
-/// \brief Search the vertex on the sketch candidate to be the next one in the loop
-static void findNextVertex(
- const TopoDS_Shape& theStartVertex,
- const BOPCol_IndexedDataMapOfShapeListOfShape& theVertexEdgeMap,
- const gp_Dir& theStartDir,
- const gp_Dir& theNormal,
- TopoDS_Shape& theNextVertex,
- TopoDS_Shape& theNextEdge,
- gp_Dir& theNextDir);
-
-/// \brief Create planar face using the edges surrounding it
-static void createFace(const TopoDS_Shape& theStartVertex,
- const std::list<TopoDS_Shape>::iterator& theStartEdge,
- const std::list<TopoDS_Shape>::iterator& theEndOfEdges,
- const gp_Pln& thePlane,
- TopoDS_Face& theResFace);
-
-/// \bief Create planar wire
-static void createWireList(const TopoDS_Shape& theStartVertex,
- const std::list<TopoDS_Shape>::iterator& theStartEdge,
- const std::list<TopoDS_Shape>::iterator& theEndOfEdges,
- const std::set<Handle(TopoDS_TShape)>& theEdgesInLoops,
- std::list<TopoDS_Wire>& theResWires);
-
-/// \brief Calculate outer tengency on the edge in specified vertex
-static gp_Dir getOuterEdgeDirection(const TopoDS_Shape& theEdge,
- const TopoDS_Shape& theVertex);
-
-/// \brief Unnecessary edges will be removed from the map.
-/// Positions of iterator will be updated
-static void removeWasteEdges(
- std::list<TopoDS_Shape>::iterator& theStartVertex,
- std::list<TopoDS_Shape>::iterator& theStartEdge,
- const std::list<TopoDS_Shape>::iterator& theEndOfVertexes,
- const std::list<TopoDS_Shape>::iterator& theEndOfEdges,
- BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE);
-
-
-
-
-void GeomAlgoAPI_SketchBuilder::createFaces(
- const boost::shared_ptr<GeomAPI_Pnt>& theOrigin,
- const boost::shared_ptr<GeomAPI_Dir>& theDirX,
- const boost::shared_ptr<GeomAPI_Dir>& theDirY,
- const boost::shared_ptr<GeomAPI_Dir>& theNorm,
- const std::list< boost::shared_ptr<GeomAPI_Shape> >& theFeatures,
- std::list< boost::shared_ptr<GeomAPI_Shape> >& theResultFaces,
- std::list< boost::shared_ptr<GeomAPI_Shape> >& theResultWires)
-{
- // Create the list of edges with shared vertexes
- BOPAlgo_Builder aBuilder;
- BOPAlgo_PaveFiller aPF;
-
- std::list< boost::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theFeatures.begin();
- for (; anIt != theFeatures.end(); anIt++)
- {
- boost::shared_ptr<GeomAPI_Shape> aPreview(*anIt);
- aBuilder.AddArgument(aPreview->impl<TopoDS_Edge>());
- }
- aPF.SetArguments(aBuilder.Arguments());
- aPF.Perform();
- int aErr = aPF.ErrorStatus();
- if (aErr) return ;
- aBuilder.PerformWithFiller(aPF);
- aErr = aBuilder.ErrorStatus();
- if (aErr) return ;
-
- BOPCol_IndexedDataMapOfShapeListOfShape aMapVE; // map between vertexes and edges
- BOPTools::MapShapesAndAncestors(aBuilder.Shape(), TopAbs_VERTEX, TopAbs_EDGE, aMapVE);
-
- gp_Dir aDirX = theDirX->impl<gp_Dir>();
- gp_Dir aDirY = theDirY->impl<gp_Dir>();
- gp_Dir aNorm = theNorm->impl<gp_Dir>();
-
- gp_Pln aPlane(theOrigin->impl<gp_Pnt>(), aNorm);
-
- // Set of edges used in loops
- std::set<Handle(TopoDS_TShape)> anEdgesInLoops;
- // Lists for processed vertexes and edges
- std::list<TopoDS_Shape> aProcVertexes;
- std::list<TopoDS_Shape> aProcEdges;
-
- // Search the start vertex
- TopoDS_Shape aStartVertex = findStartVertex(aMapVE, aDirX, aDirY);
- aProcVertexes.push_back(aStartVertex);
-
- TopoDS_Shape aCurVertex = aStartVertex;
- gp_Dir aCurDir = aDirY;
- gp_Dir aCurNorm = aNorm.Reversed();
-
- // Go through the edges and find loops
- TopoDS_Shape aNextVertex;
- TopoDS_Shape aBindingEdge;
- gp_Dir aNextDir;
- while (aMapVE.Extent() > 0)
- {
- findNextVertex(aCurVertex, aMapVE, aCurDir, aCurNorm, aNextVertex, aBindingEdge, aNextDir);
- aCurNorm = aNorm;
-
- // Try to find next vertex in the list of already processed
- std::list<TopoDS_Shape>::iterator aVertIter = aProcVertexes.begin();
- std::list<TopoDS_Shape>::iterator anEdgeIter = aProcEdges.begin();
- for ( ; aVertIter != aProcVertexes.end(); aVertIter++)
- {
- if (aVertIter->TShape() == aNextVertex.TShape())
- break;
- if (anEdgeIter != aProcEdges.end())
- anEdgeIter++;
- }
-
- aProcVertexes.push_back(aNextVertex);
- aProcEdges.push_back(aBindingEdge);
-
- // The loop was found
- if (aVertIter != aProcVertexes.end())
- {
- if (aVertIter != aProcVertexes.begin())
- {
- // Check the orientation of the loop
- Handle(BRep_TVertex) aVert = Handle(BRep_TVertex)::DownCast(aVertIter->TShape());
- const gp_Pnt& aCurPnt = aVert->Pnt();
- aVert = Handle(BRep_TVertex)::DownCast((++aVertIter)->TShape());
- aVertIter--;
- const gp_Pnt& aNextPnt = aVert->Pnt();
- std::list<TopoDS_Shape>::reverse_iterator anItBeforeLast = aProcVertexes.rbegin();
- aVert = Handle(BRep_TVertex)::DownCast((++anItBeforeLast)->TShape()); // get the vertex before last one, because the last duplicates the start of loop
- const gp_Pnt& aPrevPnt = aVert->Pnt();
- gp_Vec aCN(aCurPnt, aNextPnt);
- gp_Vec aCP(aCurPnt, aPrevPnt);
- if (aCN.DotCross(aCP, gp_Vec(aNorm)) < -tolerance)
- {
- // The found loop has wrong orientation and may contain sub-loops.
- // Need to check it onle again with another initial direction.
- aCurVertex = *aVertIter;
- do {
- aProcVertexes.pop_back();
- aProcEdges.pop_back();
- } while (aCurVertex != aProcVertexes.back());
- aCurDir = getOuterEdgeDirection(aProcEdges.back(), aCurVertex);
- aCurNorm = aNorm.Reversed();
- continue;
- }
- }
-
- // When the orientation is correct or the edges looped through
- // the first element, create new face and remove unnecessary edges.
- TopoDS_Face aPatch;
- createFace(*aVertIter, anEdgeIter, aProcEdges.end(), aPlane, aPatch);
- if (!aPatch.IsNull())
- {
- boost::shared_ptr<GeomAPI_Shape> aFace(new GeomAPI_Shape);
- aFace->setImpl(new TopoDS_Shape(aPatch));
- theResultFaces.push_back(aFace);
- }
- // push the edges used in the loop to the map
- std::list<TopoDS_Shape>::iterator anIter;
- for (anIter = anEdgeIter; anIter != aProcEdges.end(); anIter++)
- anEdgesInLoops.insert(anIter->TShape());
- // remove unnecessary edges
- std::list<TopoDS_Shape>::iterator aCopyVLoop = aVertIter;
- std::list<TopoDS_Shape>::iterator aCopyELoop = anEdgeIter;
- removeWasteEdges(aVertIter, anEdgeIter, aProcVertexes.end(), aProcEdges.end(), aMapVE);
-
- // revert the list of remaining edges
- std::list<TopoDS_Shape> aRemainVertexes;
- for ( ; aVertIter != aProcVertexes.end(); aVertIter++)
- aRemainVertexes.push_front(*aVertIter);
- std::list<TopoDS_Shape> aRemainEdges;
- for ( ; anEdgeIter != aProcEdges.end(); anEdgeIter++)
- aRemainEdges.push_front(*anEdgeIter);
- // remove edges and vertexes used in the loop and add remaining ones
- aProcVertexes.erase(aCopyVLoop, aProcVertexes.end());
- aProcVertexes.insert(aProcVertexes.end(), aRemainVertexes.begin(), aRemainVertexes.end());
- aProcEdges.erase(aCopyELoop, aProcEdges.end());
- aProcEdges.insert(aProcEdges.end(), aRemainEdges.begin(), aRemainEdges.end());
-
- // Recalculate current vertex and current direction
- if (!aProcVertexes.empty())
- {
- aNextVertex = aProcVertexes.back();
- if (!aProcEdges.empty())
- aNextDir = getOuterEdgeDirection(aProcEdges.back(), aNextVertex);
- else aNextDir = -aDirY;
- }
- }
-
- // if next vertex connected only to alone edge, this is a part of wire (not a closed loop),
- // we need to go back through the list of already checked edges to find a branching vertex
- if (!aMapVE.IsEmpty() && aMapVE.Contains(aNextVertex) &&
- aMapVE.FindFromKey(aNextVertex).Size() == 1)
- {
- std::list<TopoDS_Shape>::reverse_iterator aVRIter = aProcVertexes.rbegin();
- std::list<TopoDS_Shape>::reverse_iterator aERIter = aProcEdges.rbegin();
- for (++aERIter, ++aVRIter; aERIter != aProcEdges.rend(); aERIter++, aVRIter++)
- if (aMapVE.FindFromKey(*aVRIter).Size() > 2)
- break;
- if (aERIter != aProcEdges.rend() || aMapVE.FindFromKey(*aVRIter).Size() == 1)
- { // the branching vertex was found or current list of edges is a wire without branches
- std::list<TopoDS_Shape>::iterator aEIter;
- TopoDS_Shape aCurEdge;
- if (aERIter != aProcEdges.rend())
- {
- aEIter = aERIter.base();
- aCurEdge = *aERIter;
- }
- else
- aEIter = aProcEdges.begin();
- std::list<TopoDS_Wire> aTail;
- createWireList(*aVRIter, aEIter, aProcEdges.end(), anEdgesInLoops, aTail);
- std::list<TopoDS_Wire>::const_iterator aTailIter = aTail.begin();
- for ( ; aTailIter != aTail.end(); aTailIter++)
- if (!aTailIter->IsNull())
- {
- boost::shared_ptr<GeomAPI_Shape> aWire(new GeomAPI_Shape);
- aWire->setImpl(new TopoDS_Shape(*aTailIter));
- theResultWires.push_back(aWire);
- }
- std::list<TopoDS_Shape>::iterator aVIter = aVRIter.base();
- std::list<TopoDS_Shape>::iterator aEItCopy = aEIter;
- removeWasteEdges(--aVIter, aEItCopy, aProcVertexes.end(), aProcEdges.end(), aMapVE);
-
- aProcEdges.erase(aEIter, aProcEdges.end());
- aVIter = aVRIter.base();
- aProcVertexes.erase(aVIter, aProcVertexes.end());
-
- if (!aProcVertexes.empty())
- {
- aNextVertex = aProcVertexes.back();
- if (!aCurEdge.IsNull())
- aNextDir = getOuterEdgeDirection(aCurEdge, aNextVertex);
- }
- }
- else
- { // there is no branching vertex in the list of proceeded,
- // so we should revert the list and go the opposite way
- aProcVertexes.reverse();
- aProcEdges.reverse();
- aNextVertex = aProcVertexes.back();
- aNextDir = getOuterEdgeDirection(aProcEdges.back(), aNextVertex);
- }
- }
-
- // When all edges of current component of connectivity are processed,
- // we should repeat procedure for finding initial vertex in the remaining
- if (!aMapVE.IsEmpty() && !aMapVE.Contains(aNextVertex))
- {
- aProcVertexes.clear();
- aProcEdges.clear();
-
- TopoDS_Shape aStartEdge;
- aStartVertex = findStartVertex(aMapVE, aDirX, aDirY);
- aProcVertexes.push_back(aStartVertex);
- findNextVertex(aStartVertex, aMapVE, aDirY, aNorm.Reversed(), aNextVertex, aStartEdge, aNextDir);
- aProcVertexes.push_back(aNextVertex);
- aProcEdges.push_back(aStartEdge);
- }