-
- // 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_Face(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();
- if (aVRIter != aProcVertexes.rend())
- aVRIter++;
- if (aERIter != aProcEdges.rend())
- aERIter++;
-
- for ( ; aERIter != aProcEdges.rend(); aERIter++, aVRIter++)
- if (aMapVE.FindFromKey(*aVRIter).Size() > 2)
- break;
- if (aERIter != aProcEdges.rend() ||
- (aVRIter != aProcVertexes.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 = aProcEdges.empty() ? aDirY :
- 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);
- aNextVertex = aStartVertex;
- aNextDir = aDirY.Reversed();
- aCurNorm = aNorm.Reversed();
- }
-
- aCurVertex = aNextVertex;
- aCurDir = aNextDir;
- }
-
- if (theResultFaces.size() > 1)
- fixIntersections(theResultFaces);
-}
-
-void GeomAlgoAPI_SketchBuilder::fixIntersections(
- std::list< boost::shared_ptr<GeomAPI_Shape> >& theFaces)
-{
- BRepClass_FaceClassifier aClassifier;
-
- std::list< boost::shared_ptr<GeomAPI_Shape> >::iterator anIter1 = theFaces.begin();
- std::list< boost::shared_ptr<GeomAPI_Shape> >::iterator anIter2;
- for ( ; anIter1 != theFaces.end(); anIter1++)
- {
- anIter2 = anIter1;
- for (++anIter2; anIter2 != theFaces.end(); anIter2++)
- {
- const TopoDS_Face& aF1 = (*anIter1)->impl<TopoDS_Face>();
- assert(aF1.ShapeType() == TopAbs_FACE); // all items in result list should be faces
- TopExp_Explorer aVert2((*anIter2)->impl<TopoDS_Shape>(), TopAbs_VERTEX);
- for ( ; aVert2.More(); aVert2.Next())
- {
- Handle(BRep_TVertex) aV = Handle(BRep_TVertex)::DownCast(aVert2.Current().TShape());
- aClassifier.Perform(aF1, aV->Pnt(), tolerance);
- if (aClassifier.State() != TopAbs_IN)
- break;
- }
- if (aVert2.More())
- { // second shape is not inside first, change the shapes order and repeat comparision
- const TopoDS_Face& aF2 = (*anIter2)->impl<TopoDS_Face>();
- assert(aF2.ShapeType() == TopAbs_FACE); // all items in result list should be faces
- TopExp_Explorer aVert1((*anIter1)->impl<TopoDS_Shape>(), TopAbs_VERTEX);
- for ( ; aVert1.More(); aVert1.Next())
- {
- Handle(BRep_TVertex) aV = Handle(BRep_TVertex)::DownCast(aVert1.Current().TShape());
- aClassifier.Perform(aF2, aV->Pnt(), tolerance);
- if (aClassifier.State() != TopAbs_IN)
- break;
- }
- if (!aVert1.More())
- { // first shape should be cut from the second
- BRepAlgoAPI_Cut aCut((*anIter2)->impl<TopoDS_Shape>(),
- (*anIter1)->impl<TopoDS_Shape>());
- aCut.Build();
- TopExp_Explorer anExp(aCut.Shape(), TopAbs_FACE);
- (*anIter2)->setImpl(new TopoDS_Shape(anExp.Current()));
- for (anExp.Next(); anExp.More(); anExp.Next())
- {
- boost::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
- aShape->setImpl(new TopoDS_Shape(anExp.Current()));
- theFaces.push_back(aShape);
- }
- }
- }
- else
- { // second shape should be cut from the first
- BRepAlgoAPI_Cut aCut((*anIter1)->impl<TopoDS_Shape>(),
- (*anIter2)->impl<TopoDS_Shape>());
- aCut.Build();
- TopExp_Explorer anExp(aCut.Shape(), TopAbs_FACE);
- (*anIter1)->setImpl(new TopoDS_Shape(anExp.Current()));
- for (anExp.Next(); anExp.More(); anExp.Next())
- {
- boost::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
- aShape->setImpl(new TopoDS_Shape(anExp.Current()));
- theFaces.push_back(aShape);
- }
- }
- }
- }
-}
-
-
-// =================== Auxiliary functions ====================================
-const TopoDS_Shape& findStartVertex(
- const BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE,
- const gp_Dir& theDirX, const gp_Dir& theDirY)
-{
- int aStartVertexInd = 1;
- double aMaxX = -DBL_MAX;
- double aMaxY = -DBL_MAX;
- int aNbVert = theMapVE.Extent();
- for (int i = 1; i <= aNbVert; i++)
- {
- const TopoDS_Vertex& aV = (const TopoDS_Vertex&)theMapVE.FindKey(i);
- const Handle(BRep_TVertex)& aVert = (const Handle(BRep_TVertex)&)aV.TShape();
- const gp_Pnt& aVertPnt = aVert->Pnt();
-
- double aX = aVertPnt.XYZ().Dot(theDirX.XYZ());
- double aY = aVertPnt.XYZ().Dot(theDirY.XYZ());
- if ((aX > aMaxX || (fabs(aX - aMaxX) < tolerance && aY > aMaxY)) &&
- theMapVE.FindFromIndex(i).Extent() > 1)
- {
- aMaxX = aX;
- aMaxY = aY;
- aStartVertexInd = i;
- }
- }
- return theMapVE.FindKey(aStartVertexInd);
-}
-
-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)
-{
- const BOPCol_ListOfShape& anEdgesList = theVertexEdgeMap.FindFromKey(theStartVertex);
- BOPCol_ListOfShape::Iterator aEdIter(anEdgesList);
- double aBestEdgeProj = DBL_MAX;
- for ( ; aEdIter.More(); aEdIter.Next())
- {
- gp_Dir aTang = getOuterEdgeDirection(aEdIter.Value(), theStartVertex);
- aTang.Reverse();
-
- // The projection is normalized in segment (-1, 1),
- // where (-1, 0] corresponds to the angles (pi/2, 0] between theStartDir and aTang
- // and [0, 1) corresponds to the angles [0, -pi/2)
- double aProj = (aTang.Dot(theStartDir) - 1.0) * 0.5;
- if (fabs(fabs(aProj) - 1) < tolerance)
- continue;
- if (theStartDir.DotCross(aTang, theNormal) < tolerance)
- aProj *= -1.0;
-
- if (aProj < aBestEdgeProj)
- {
- aBestEdgeProj = aProj;
- theNextEdge = aEdIter.Value();
- TopExp_Explorer aVertExp(theNextEdge, TopAbs_VERTEX);
- for ( ; aVertExp.More(); aVertExp.Next())
- if (aVertExp.Current().TShape() != theStartVertex.TShape())
- {
- theNextVertex = aVertExp.Current();
- theNextDir = getOuterEdgeDirection(aEdIter.Value(), theNextVertex);
- break;
- }
- if (!aVertExp.More())
- { // This edge is a full circle
- TopoDS_Vertex aV1, aV2;
- TopExp::Vertices(*(const TopoDS_Edge*)(&theNextEdge), aV1, aV2);
- if (aV1.Orientation() == theStartVertex.Orientation())
- theNextVertex = aV2;
- else
- theNextVertex = aV1;
- theNextDir = getOuterEdgeDirection(aEdIter.Value(), theNextVertex);
- }
- }
- }
-}
-
-static void addEdgeToWire(const TopoDS_Edge& theEdge,
- const BRep_Builder& theBuilder,
- TopoDS_Shape& theSpliceVertex,
- TopoDS_Wire& theWire)
-{
- TopoDS_Edge anEdge = theEdge;
- bool isCurVertChanged = false;
- TopoDS_Shape aCurVertChanged;
-
- TopExp_Explorer aVertExp(theEdge, TopAbs_VERTEX);
- for ( ; aVertExp.More(); aVertExp.Next())
- {
- const TopoDS_Shape& aVertex = aVertExp.Current();
- if (aVertex.TShape() == theSpliceVertex.TShape() &&
- aVertex.Orientation() != theEdge.Orientation())
- { // Current vertex is the last for the edge, so its orientation is wrong, need to revert the edge
- anEdge.Reverse();
- break;
- }
- if (aVertex.TShape() != theSpliceVertex.TShape())
- {
- aCurVertChanged = aVertex;
- isCurVertChanged = true;
- }
- }
- theSpliceVertex = isCurVertChanged ? aCurVertChanged : aVertExp.Current();
-
- theBuilder.Add(theWire, anEdge);
-}
-
-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)
-{
- TopoDS_Wire aResWire;
- BRep_Builder aBuilder;
- aBuilder.MakeWire(aResWire);
-
- TopoDS_Shape aCurVertex = theStartVertex;
- std::list<TopoDS_Shape>::const_iterator anEdgeIter = theStartEdge;
- for ( ; anEdgeIter != theEndOfEdges; anEdgeIter++)
- {
- TopoDS_Edge anEdge = *((TopoDS_Edge*)(&(*anEdgeIter)));
- if (!anEdge.IsNull())
- addEdgeToWire(anEdge, aBuilder, aCurVertex, aResWire);
- }
-
- BRepBuilderAPI_MakeFace aFaceBuilder(thePlane, aResWire);
- if (aFaceBuilder.Error() == BRepBuilderAPI_FaceDone)
- theResFace = aFaceBuilder.Face();
-}
-
-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)
-{
- BRep_Builder aBuilder;
- bool needNewWire = true;
- TopoDS_Shape aCurVertex = theStartVertex;
-
- std::list<TopoDS_Shape>::iterator anIter = theStartEdge;
- while (anIter != theEndOfEdges)
- {
- while (anIter != theEndOfEdges && needNewWire &&
- theEdgesInLoops.count(anIter->TShape()) != 0)
- {
- TopExp_Explorer aVertExp(*anIter, TopAbs_VERTEX);
- for ( ; aVertExp.More(); aVertExp.Next())
- if (aVertExp.Current().TShape() != aCurVertex.TShape())
- {
- aCurVertex = aVertExp.Current();
- break;
- }
- anIter++;