]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAPI/GeomAPI_Wire.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / GeomAPI / GeomAPI_Wire.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 "GeomAPI_Wire.h"
21 #include "GeomAPI_Pnt.h"
22
23 #include <BRep_Builder.hxx>
24 #include <BRepTools_WireExplorer.hxx>
25 #include <Geom_Line.hxx>
26 #include <Precision.hxx>
27 #include <Standard_Type.hxx>
28 #include <TopoDS.hxx>
29 #include <TopoDS_Wire.hxx>
30
31 //==================================================================================================
32 GeomAPI_Wire::GeomAPI_Wire()
33 {
34   TopoDS_Wire* aWire = new TopoDS_Wire();
35
36   BRep_Builder aBuilder;
37   aBuilder.MakeWire(*aWire);
38
39   this->setImpl(aWire);
40 }
41
42 //==================================================================================================
43 GeomAPI_Wire::GeomAPI_Wire(const std::shared_ptr<GeomAPI_Shape>& theShape)
44 {
45   if (!theShape->isNull() && theShape->isWire()) {
46     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
47   }
48 }
49
50 //==================================================================================================
51 bool GeomAPI_Wire::isClosed() const
52 {
53   return BRep_Tool::IsClosed(impl<TopoDS_Shape>());
54 }
55
56 //==================================================================================================
57 bool GeomAPI_Wire::isPolygon(std::list<GeomPointPtr>& thePoints) const
58 {
59   const TopoDS_Wire& aWire = TopoDS::Wire(impl<TopoDS_Shape>());
60
61   bool isPolygon = true;
62   const Handle(Standard_Type)& aLineType = STANDARD_TYPE(Geom_Line);
63   for (BRepTools_WireExplorer anExp(aWire); anExp.More() && isPolygon; anExp.Next()) {
64     const TopoDS_Edge& anEdge = anExp.Current();
65     double aT1, aT2;
66     Handle(Geom_Curve) aC3D = BRep_Tool::Curve(anEdge, aT1, aT2);
67     if (!aC3D.IsNull() && aC3D->IsKind(aLineType)) {
68       gp_Pnt aCorner = BRep_Tool::Pnt(anExp.CurrentVertex());
69       thePoints.push_back(GeomPointPtr(new GeomAPI_Pnt(aCorner.X(), aCorner.Y(), aCorner.Z())));
70     }
71     else
72       isPolygon = false;
73   }
74
75   if (!isPolygon)
76     thePoints.clear();
77   return isPolygon;
78 }
79
80 //==================================================================================================
81 bool GeomAPI_Wire::isRectangle(std::list<GeomPointPtr>& thePoints) const
82 {
83   thePoints.clear();
84
85   const TopoDS_Wire& aWire = TopoDS::Wire(impl<TopoDS_Shape>());
86   const Handle(Standard_Type)& aLineType = STANDARD_TYPE(Geom_Line);
87
88   gp_XYZ aPrevDir(0, 0, 0);
89
90   for (BRepTools_WireExplorer anExp(aWire); anExp.More(); anExp.Next()) {
91     const TopoDS_Edge& anEdge = anExp.Current();
92     double aT1, aT2;
93     Handle(Geom_Curve) aC3D = BRep_Tool::Curve(anEdge, aT1, aT2);
94     if (!aC3D.IsNull() && aC3D->IsKind(aLineType)) {
95       gp_Pnt aCorner = BRep_Tool::Pnt(anExp.CurrentVertex());
96       thePoints.push_back(GeomPointPtr(new GeomAPI_Pnt(aCorner.X(), aCorner.Y(), aCorner.Z())));
97     }
98     else
99       return false;
100
101     if (thePoints.size() > 4)
102       return false;
103
104     // collect length of the edge
105     gp_Pnt aStart = aC3D->Value(aT1);
106     gp_Pnt aEnd = aC3D->Value(aT2);
107
108     if (aStart.Distance(aEnd) <= Precision::Confusion())
109       return false;
110
111     // check the edge is orthogonal to the previous
112     gp_XYZ aCurDir = (aEnd.XYZ() - aStart.XYZ()).Normalized();
113     if (aPrevDir.Dot(aCurDir) < Precision::Confusion())
114       aPrevDir = aCurDir;
115     else
116       return false;
117   }
118   return true;
119 }
120
121 //==================================================================================================
122 GeomPointPtr GeomAPI_Wire::middlePoint() const
123 {
124   // find middle edge in the wire
125   std::list<GeomShapePtr> aSubs = subShapes(EDGE);
126   size_t aNbSubs = aSubs.size();
127   if (aNbSubs == 0)
128     return GeomPointPtr();
129
130   aNbSubs /= 2;
131   for (; aNbSubs > 0; --aNbSubs)
132     aSubs.pop_front();
133
134   // compute middle point on the middle edge
135   return aSubs.front()->middlePoint();
136 }