Salome HOME
Issue #2622: Weak naming
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_WireBuilder.cpp
1 // Copyright (C) 2014-2017  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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "GeomAlgoAPI_WireBuilder.h"
22
23 #include <GeomAPI_Edge.h>
24 #include <GeomAPI_Pnt.h>
25 #include <GeomAPI_Vertex.h>
26 #include <GeomAPI_ShapeExplorer.h>
27
28 #include <BRepBuilderAPI_MakeWire.hxx>
29 #include <TopoDS.hxx>
30 #include <TopoDS_Wire.hxx>
31 #include <TopExp_Explorer.hxx>
32
33 //=================================================================================================
34 GeomShapePtr GeomAlgoAPI_WireBuilder::wire(const ListOfShape& theShapes)
35 {
36   TopTools_ListOfShape aListOfEdges;
37
38   ListOfShape::const_iterator anIt = theShapes.cbegin();
39   for(; anIt != theShapes.cend(); ++anIt) {
40     const TopoDS_Shape& aShape = (*anIt)->impl<TopoDS_Shape>();
41     switch(aShape.ShapeType()) {
42       case TopAbs_EDGE: {
43         aListOfEdges.Append(aShape);
44         break;
45       }
46       case TopAbs_WIRE: {
47         for(TopExp_Explorer anExp(aShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
48           aListOfEdges.Append(anExp.Current());
49         }
50         break;
51       }
52       default: {
53         return GeomShapePtr();
54       }
55     }
56   }
57
58   BRepBuilderAPI_MakeWire aWireBuilder;
59   aWireBuilder.Add(aListOfEdges);
60   if(aWireBuilder.Error() != BRepBuilderAPI_WireDone) {
61     return GeomShapePtr();
62   }
63
64   GeomShapePtr aResultShape(new GeomAPI_Shape());
65   aResultShape->setImpl(new TopoDS_Shape(aWireBuilder.Wire()));
66   return aResultShape;
67 }
68
69 //=================================================================================================
70 bool GeomAlgoAPI_WireBuilder::isSelfIntersected(const GeomShapePtr& theWire)
71 {
72   // Collect edges.
73   ListOfShape anEdges;
74
75   GeomAPI_ShapeExplorer anExp(theWire, GeomAPI_Shape::EDGE);
76   for (; anExp.more(); anExp.next()) {
77     GeomShapePtr anEdge = anExp.current();
78     anEdges.push_back(anEdge);
79   }
80
81   // Check intersections between edges pair-wise
82   int aNbEdges = (int)anEdges.size();
83   std::list<GeomShapePtr>::const_iterator anEdgesIt = anEdges.begin();
84   for (int i = 0; anEdgesIt != anEdges.end(); ++anEdgesIt, i++) {
85     GeomEdgePtr anEdge1(new GeomAPI_Edge(*anEdgesIt));
86
87     std::list<GeomShapePtr>::const_iterator anOtherEdgesIt = std::next(anEdgesIt);
88     for (int j = i + 1; anOtherEdgesIt != anEdges.end(); ++anOtherEdgesIt, j++) {
89       GeomEdgePtr anEdge2(new GeomAPI_Edge(*anOtherEdgesIt));
90       GeomShapePtr anInter = anEdge1->intersect(anEdge2);
91       if (!anInter.get()) {
92         continue;
93       }
94
95       bool isOk = false;
96
97       if (anInter->isVertex()) {
98         GeomVertexPtr aVertex(new GeomAPI_Vertex(anInter));
99         GeomPointPtr aPnt = aVertex->point();
100
101         GeomPointPtr aFirstPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
102                                   anEdge1->firstPoint() : anEdge1->lastPoint();
103         GeomPointPtr aLastPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
104                                  anEdge1->lastPoint() : anEdge1->firstPoint();
105         GeomPointPtr aFirstPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
106                                   anEdge2->firstPoint() : anEdge2->lastPoint();
107         GeomPointPtr aLastPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
108                                  anEdge2->lastPoint() : anEdge2->firstPoint();
109
110         GeomPointPtr aCommonEndPnt;
111         if (aFirstPnt1->isEqual(aLastPnt2)) {
112           aCommonEndPnt = aFirstPnt1;
113         } else if(aLastPnt1->isEqual(aFirstPnt2)) {
114           aCommonEndPnt = aLastPnt1;
115         }
116
117         isOk = aCommonEndPnt && aPnt->isEqual(aCommonEndPnt);
118       }
119
120       if (!isOk) {
121         return true;
122       }
123     }
124   }
125
126   return false;
127 }