Salome HOME
Issue #2559: Add Polyline feature to Build plugin for 3D polyline creation.
[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 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_WireBuilder::wire(const ListOfShape& theShapes)
35 {
36   TopTools_ListOfShape aListOfEdges;
37
38   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); 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 std::shared_ptr<GeomAPI_Shape>& theWire)
70 {
71   // Collect edges.
72   ListOfShape anEdges;
73
74   for (GeomAPI_ShapeExplorer anExp(theWire, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
75     GeomShapePtr anEdge = anExp.current();
76     anEdges.push_back(anEdge);
77   }
78
79   // Check intersections between edges pair-wise
80   int aNbEdges = (int)anEdges.size();
81   std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anEdgesIt = anEdges.begin();
82   for (int i = 0; anEdgesIt != anEdges.end(); ++anEdgesIt, i++) {
83     std::shared_ptr<GeomAPI_Edge> anEdge1(new GeomAPI_Edge(*anEdgesIt));
84
85     std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator anOtherEdgesIt = std::next(anEdgesIt);
86     for (int j = i + 1; anOtherEdgesIt != anEdges.end(); ++anOtherEdgesIt, j++) {
87       std::shared_ptr<GeomAPI_Edge> anEdge2(new GeomAPI_Edge(*anOtherEdgesIt));
88       GeomShapePtr anInter = anEdge1->intersect(anEdge2);
89       if (!anInter.get()) {
90         continue;
91       }
92
93       bool isOk = false;
94
95       if (anInter->isVertex()) {
96         std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex(anInter));
97         std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
98
99         GeomPointPtr aFirstPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
100                                   anEdge1->firstPoint() : anEdge1->lastPoint();
101         GeomPointPtr aLastPnt1 = anEdge1->orientation() == GeomAPI_Shape::FORWARD ?
102                                  anEdge1->lastPoint() : anEdge1->firstPoint();
103         GeomPointPtr aFirstPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
104                                   anEdge2->firstPoint() : anEdge2->lastPoint();
105         GeomPointPtr aLastPnt2 = anEdge2->orientation() == GeomAPI_Shape::FORWARD ?
106                                  anEdge2->lastPoint() : anEdge2->firstPoint();
107
108         std::shared_ptr<GeomAPI_Pnt> aCommonEndPnt;
109         if (aFirstPnt1->isEqual(aLastPnt2)) {
110           aCommonEndPnt = aFirstPnt1;
111         } else if(aLastPnt1->isEqual(aFirstPnt2)) {
112           aCommonEndPnt = aLastPnt1;
113         }
114
115         isOk = aCommonEndPnt && aPnt->isEqual(aCommonEndPnt);
116       }
117
118       if (!isOk) {
119         return true;
120       }
121     }
122   }
123
124   return false;
125 }