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