-
-
-void GeomAlgoAPI_SketchBuilder::fixIntersections(
- std::list<std::shared_ptr<GeomAPI_Shape> >& theFaces)
-{
- BRepClass_FaceClassifier aClassifier;
-
- std::list<std::shared_ptr<GeomAPI_Shape> >::iterator anIter1 = theFaces.begin();
- std::list<std::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()) {
- const TopoDS_Vertex& aV = (const TopoDS_Vertex&)aVert2.Current();
- aClassifier.Perform(aF1, BRep_Tool::Pnt(aV), tolerance);
- TopAbs_State aState = aClassifier.State();
- if (aState != TopAbs_IN && aState != TopAbs_ON)
- 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()) {
- const TopoDS_Vertex& aV = (const TopoDS_Vertex&)aVert2.Current();
- aClassifier.Perform(aF2, BRep_Tool::Pnt(aV), tolerance);
- TopAbs_State aState = aClassifier.State();
- if (aState != TopAbs_IN && aState != TopAbs_ON)
- 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);
- bool isFirstFace = true;
- for (; anExp.More(); anExp.Next()) {
- if (anExp.Current().ShapeType() != TopAbs_FACE) continue;
- if (isFirstFace) {
- (*anIter2)->setImpl(new TopoDS_Shape(anExp.Current()));
- isFirstFace = false;
- } else {
- std::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);
- bool isFirstFace = true;
- for (; anExp.More(); anExp.Next()) {
- if (anExp.Current().ShapeType() != TopAbs_FACE) continue;
- if (isFirstFace) {
- (*anIter1)->setImpl(new TopoDS_Shape(anExp.Current()));
- isFirstFace = false;
- } else {
- std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape);
- aShape->setImpl(new TopoDS_Shape(anExp.Current()));
- theFaces.push_back(aShape);
- }
- }
- }
- }
- }
-}
-
-// =================== Auxiliary functions ====================================
-const TopoDS_Vertex& 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 gp_Pnt& aVertPnt = BRep_Tool::Pnt(aV);
-
- 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 static_cast<const TopoDS_Vertex&>(theMapVE.FindKey(aStartVertexInd));
-}
-
-void findNextVertex(const TopoDS_Vertex& theStartVertex,
- const BOPCol_IndexedDataMapOfShapeListOfShape& theVertexEdgeMap,
- const gp_Dir& theStartDir, const gp_Dir& theNormal, TopoDS_Vertex& theNextVertex,
- TopoDS_Edge& theNextEdge, gp_Dir& theNextDir)
-{
- theNextVertex = TopoDS_Vertex();
- const BOPCol_ListOfShape& anEdgesList = theVertexEdgeMap.FindFromKey(theStartVertex);
- int anEdgesNum = anEdgesList.Extent();
- BOPCol_ListOfShape::Iterator aEdIter(anEdgesList);
- double aBestEdgeProj = DBL_MAX;
- for (; aEdIter.More(); aEdIter.Next()) {
- const TopoDS_Edge& anEdge = static_cast<const TopoDS_Edge&>(aEdIter.Value());
- gp_Dir aTang = getOuterEdgeDirection(anEdge, 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 (anEdgesNum > 1 && fabs(fabs(aProj) - 1) < tolerance)
- continue;
- if (theStartDir.DotCross(aTang, theNormal) < tolerance)
- aProj *= -1.0;
-
- if (aProj < aBestEdgeProj) {
- aBestEdgeProj = aProj;
- theNextEdge = anEdge;
- TopExp_Explorer aVertExp(theNextEdge, TopAbs_VERTEX);
- for (; aVertExp.More(); aVertExp.Next())
- if (!aVertExp.Current().IsSame(theStartVertex)) {
- theNextVertex = static_cast<const TopoDS_Vertex&>(aVertExp.Current());
- theNextDir = getOuterEdgeDirection(anEdge, theNextVertex);
- break;
- }
- if (!aVertExp.More()) { // This edge is a full circle
- TopoDS_Vertex aV1, aV2;
- TopExp::Vertices(theNextEdge, aV1, aV2);
- if (aV1.Orientation() == theStartVertex.Orientation())
- theNextVertex = aV2;
- else
- theNextVertex = aV1;
- theNextDir = getOuterEdgeDirection(anEdge, theNextVertex);
- }
- }
- }
-
- // Probably there are two tangent edges. We will take the edge differs from current one
- if (theNextVertex.IsNull() && anEdgesNum == 2) {
- BOPCol_ListOfShape::Iterator aEdIter(anEdgesList);
- if (aEdIter.Value() == theNextEdge)
- aEdIter.Next();
- theNextEdge = static_cast<const TopoDS_Edge&>(aEdIter.Value());
- TopoDS_Vertex aV1, aV2;
- TopExp::Vertices(theNextEdge, aV1, aV2);
- theNextVertex = theStartVertex.IsSame(aV1) ? aV2 : aV1;
- theNextDir = getOuterEdgeDirection(theNextEdge, 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.IsSame(theSpliceVertex)
- && 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.IsSame(theSpliceVertex)) {
- aCurVertChanged = aVertex;
- isCurVertChanged = true;
- }
- }
- theSpliceVertex = isCurVertChanged ? aCurVertChanged : aVertExp.Current();
-
- theBuilder.Add(theWire, anEdge);
-}
-
-void createFace(const TopoDS_Vertex& theStartVertex,
- const std::list<TopoDS_Edge>::iterator& theStartEdge,
- const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
- const gp_Pln& thePlane,
- TopoDS_Face& theResFace)
-{
- TopoDS_Wire aResWire;
- BRep_Builder aBuilder;
- aBuilder.MakeWire(aResWire);
-
- TopoDS_Vertex aCurVertex = theStartVertex;
- std::list<TopoDS_Edge>::const_iterator anEdgeIter = theStartEdge;
- for (; anEdgeIter != theEndOfEdges; anEdgeIter++) {
- if (!anEdgeIter->IsNull())
- addEdgeToWire(*anEdgeIter, aBuilder, aCurVertex, aResWire);
- }
-
- BRepBuilderAPI_MakeFace aFaceBuilder(thePlane, aResWire);
- if (aFaceBuilder.Error() == BRepBuilderAPI_FaceDone)
- theResFace = aFaceBuilder.Face();
-}
-
-void createWireList(const TopoDS_Vertex& theStartVertex,
- const std::list<TopoDS_Edge>::iterator& theStartEdge,
- const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
- const std::set<TopoDS_Edge*>& theEdgesInLoops,
- std::list<TopoDS_Wire>& theResWires)
-{
- BRep_Builder aBuilder;
- bool needNewWire = true;
- TopoDS_Vertex aCurVertex = theStartVertex;
-
- std::list<TopoDS_Edge>::iterator anIter = theStartEdge;
- while (anIter != theEndOfEdges) {
- while (anIter != theEndOfEdges && needNewWire && theEdgesInLoops.count(&(*anIter)) != 0) {
- TopExp_Explorer aVertExp(*anIter, TopAbs_VERTEX);
- for (; aVertExp.More(); aVertExp.Next())
- if (!aVertExp.Current().IsSame(aCurVertex)) {
- aCurVertex = static_cast<const TopoDS_Vertex&>(aVertExp.Current());
- break;
- }
- anIter++;
- }
- if (anIter == theEndOfEdges)
- break;
-
- if (needNewWire) { // The new wire should be created
- TopoDS_Wire aWire;
- aBuilder.MakeWire(aWire);
- theResWires.push_back(aWire);
- needNewWire = false;
- } else if (theEdgesInLoops.count(&(*anIter)) != 0) { // There was found the edge already used in loop.
- // Current wire should be released and new one should started
- needNewWire = true;
- continue;
- }
-
- addEdgeToWire(*anIter, aBuilder, aCurVertex, theResWires.back());
- anIter++;
- }
-}
-
-gp_Dir getOuterEdgeDirection(const TopoDS_Edge& theEdge, const TopoDS_Vertex& theVertex)
-{
- gp_Pnt aVertexPnt = BRep_Tool::Pnt(theVertex);
-
- // Convert the edge to the curve to calculate the tangency.
- // There should be only one curve in the edge.
- double aFirst, aLast;
- Handle(Geom_Curve) aCurve = BRep_Tool::Curve(theEdge, aFirst, aLast);
-
- gp_Pnt aPnt;
- gp_Vec aTang;
- // A direction is determined not in the boundary points but in the points with small shift.
- // It was done to avoid tangency between circle and other edge in the shared vertex.
- aCurve->D1(aFirst + shift > aLast ? aFirst : aFirst + shift, aPnt, aTang);
- aCurve->D0(aFirst, aPnt);
- if (aVertexPnt.IsEqual(aPnt, tolerance))
- return gp_Dir(aTang.Reversed());
-
- aCurve->D1(aLast - shift < aFirst ? aLast : aLast - shift, aPnt, aTang);
- return gp_Dir(aTang);
-}
-
-void removeWasteEdges(std::list<TopoDS_Vertex>::iterator& theStartVertex,
- std::list<TopoDS_Edge>::iterator& theStartEdge,
- const std::list<TopoDS_Vertex>::iterator& theEndOfVertexes,
- const std::list<TopoDS_Edge>::iterator& theEndOfEdges,
- BOPCol_IndexedDataMapOfShapeListOfShape& theMapVE)
-{
- bool isVertStep = true;
- while (theStartVertex != theEndOfVertexes && theStartEdge != theEndOfEdges) {
- BOPCol_ListOfShape& aBunch = theMapVE.ChangeFromKey(*theStartVertex);
- BOPCol_ListOfShape::Iterator anApprEdge(aBunch);
- for (; anApprEdge.More(); anApprEdge.Next())
- if (anApprEdge.Value().IsSame(*theStartEdge))
- break;
- if (anApprEdge.More())
- aBunch.Remove(anApprEdge);
-
- if (isVertStep)
- theStartVertex++;
- else {
- theStartEdge++;
- // check current vertex to be a branching point
- // if so, it will be a new starting point to find a loop
- if (aBunch.Size() > 1)
- break;
- }
- isVertStep = !isVertStep;
- }
-
- // The map of vertex-edges may be changed
- BOPCol_IndexedDataMapOfShapeListOfShape aMapVECopy;
- BOPCol_IndexedDataMapOfShapeListOfShape::Iterator aMapIter(theMapVE);
- for (int ind = 1; aMapIter.More(); aMapIter.Next(), ind++)
- if (!aMapIter.Value().IsEmpty())
- aMapVECopy.Add(theMapVE.FindKey(ind), aMapIter.Value());
- theMapVE.Clear();
- theMapVE.Exchange(aMapVECopy);
-}
-