Salome HOME
00c1e79d1e91119604b75a4f495e7127820e7f4c
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_WireBuilder.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_WireBuilder.h"
21
22 #include <GeomAPI_Edge.h>
23 #include <GeomAPI_Pnt.h>
24 #include <GeomAPI_Vertex.h>
25 #include <GeomAPI_ShapeExplorer.h>
26
27 #include <BRepBuilderAPI_MakeWire.hxx>
28 #include <TopoDS.hxx>
29 #include <TopoDS_Wire.hxx>
30 #include <TopExp_Explorer.hxx>
31
32 //=================================================================================================
33 GeomShapePtr GeomAlgoAPI_WireBuilder::wire(const ListOfShape& theShapes)
34 {
35   TopTools_ListOfShape aListOfEdges;
36
37   ListOfShape::const_iterator anIt = theShapes.cbegin();
38   for(; anIt != theShapes.cend(); ++anIt) {
39     const TopoDS_Shape& aShape = (*anIt)->impl<TopoDS_Shape>();
40     switch(aShape.ShapeType()) {
41       case TopAbs_EDGE: {
42         aListOfEdges.Append(aShape);
43         break;
44       }
45       case TopAbs_WIRE: {
46         for(TopExp_Explorer anExp(aShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
47           aListOfEdges.Append(anExp.Current());
48         }
49         break;
50       }
51       default: {
52         return GeomShapePtr();
53       }
54     }
55   }
56
57   BRepBuilderAPI_MakeWire aWireBuilder;
58   aWireBuilder.Add(aListOfEdges);
59   if(aWireBuilder.Error() != BRepBuilderAPI_WireDone) {
60     return GeomShapePtr();
61   }
62
63   GeomShapePtr aResultShape(new GeomAPI_Shape());
64   aResultShape->setImpl(new TopoDS_Shape(aWireBuilder.Wire()));
65   return aResultShape;
66 }
67
68 //=================================================================================================
69 bool GeomAlgoAPI_WireBuilder::isSelfIntersected(const GeomShapePtr& theWire)
70 {
71   // Collect edges.
72   ListOfShape anEdges;
73
74   GeomAPI_ShapeExplorer anExp(theWire, GeomAPI_Shape::EDGE);
75   for (; anExp.more(); anExp.next()) {
76     GeomShapePtr anEdge = anExp.current();
77     anEdges.push_back(anEdge);
78   }
79
80   // Check intersections between edges pair-wise
81   int aNbEdges = (int)anEdges.size();
82   std::list<GeomShapePtr>::const_iterator anEdgesIt = anEdges.begin();
83   for (int i = 0; anEdgesIt != anEdges.end(); ++anEdgesIt, i++) {
84     GeomEdgePtr anEdge1(new GeomAPI_Edge(*anEdgesIt));
85
86     std::list<GeomShapePtr>::const_iterator anOtherEdgesIt = std::next(anEdgesIt);
87     for (int j = i + 1; anOtherEdgesIt != anEdges.end(); ++anOtherEdgesIt, j++) {
88       GeomEdgePtr anEdge2(new GeomAPI_Edge(*anOtherEdgesIt));
89       GeomShapePtr anInter = anEdge1->intersect(anEdge2);
90       if (!anInter.get()) {
91         continue;
92       }
93
94       bool isOk = false;
95
96       if (anInter->isVertex()) {
97         GeomVertexPtr aVertex(new GeomAPI_Vertex(anInter));
98         GeomPointPtr aPnt = aVertex->point();
99
100         GeomPointPtr aFirstPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
101                                   anEdge1->firstPoint() : anEdge1->lastPoint();
102         GeomPointPtr aLastPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
103                                  anEdge1->lastPoint() : anEdge1->firstPoint();
104         GeomPointPtr aFirstPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
105                                   anEdge2->firstPoint() : anEdge2->lastPoint();
106         GeomPointPtr aLastPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
107                                  anEdge2->lastPoint() : anEdge2->firstPoint();
108
109         GeomPointPtr aCommonEndPnt;
110         if (aFirstPnt1->isEqual(aLastPnt2)) {
111           aCommonEndPnt = aFirstPnt1;
112         } else if(aLastPnt1->isEqual(aFirstPnt2)) {
113           aCommonEndPnt = aLastPnt1;
114         }
115
116         isOk = aCommonEndPnt && aPnt->isEqual(aCommonEndPnt);
117       }
118
119       if (!isOk) {
120         return true;
121       }
122     }
123   }
124
125   return false;
126 }