Salome HOME
9927c2ae9cc3bb899fcb53611073f05d3d67b436
[modules/shaper.git] / src / GeomAPI / GeomAPI_Edge.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<GeomAPI_Edge.h>
22 #include<GeomAPI_Pln.h>
23 #include<GeomAPI_Pnt.h>
24 #include<GeomAPI_Circ.h>
25 #include<GeomAPI_Dir.h>
26 #include<GeomAPI_Lin.h>
27 #include<GeomAPI_Ax2.h>
28 #include<GeomAPI_Ellipse.h>
29
30 #include <BRepAdaptor_Curve.hxx>
31
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Edge.hxx>
34 #include <TopoDS.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRep_Tool.hxx>
37 #include <Geom_Curve.hxx>
38 #include <Geom_Line.hxx>
39 #include <Geom_Circle.hxx>
40 #include <Geom_Ellipse.hxx>
41 #include <GeomAdaptor_Curve.hxx>
42 #include <gp_Ax1.hxx>
43 #include <gp_Pln.hxx>
44 #include <gp_Elips.hxx>
45
46 #include <GCPnts_AbscissaPoint.hxx>
47
48 GeomAPI_Edge::GeomAPI_Edge()
49 {
50   TopoDS_Edge* anEdge = new TopoDS_Edge;
51
52   BRep_Builder aBuilder;
53   aBuilder.MakeEdge(*anEdge);
54
55   setImpl(anEdge);
56 }
57
58 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
59 {
60   if (!theShape->isNull() && theShape->isEdge()) {
61     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
62   }
63 }
64
65 bool GeomAPI_Edge::isLine() const
66 {
67   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
68   double aFirst, aLast;
69   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
70   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
71     return true;
72   return false;
73 }
74
75 bool GeomAPI_Edge::isCircle() const
76 {
77   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
78   double aFirst, aLast;
79   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
80   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
81   {
82     // Check the difference of first and last parameters to be equal to the curve period
83     if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
84       return true;
85   }
86   return false;
87 }
88
89 bool GeomAPI_Edge::isArc() const
90 {
91   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
92   double aFirst, aLast;
93   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
94   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
95   {
96     // Check the difference of first and last parameters is not equal the curve period
97     if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
98       return true;
99   }
100   return false;
101 }
102
103 bool GeomAPI_Edge::isEllipse() const
104 {
105   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
106   double aFirst, aLast;
107   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
108   if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
109     return true;
110   return false;
111 }
112
113 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
114 {
115   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
116   double aFirst, aLast;
117   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
118   gp_Pnt aPoint;
119   aCurve->D0(aFirst, aPoint);
120   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
121 }
122
123 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
124 {
125   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
126   double aFirst, aLast;
127   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
128   gp_Pnt aPoint;
129   aCurve->D0(aLast, aPoint);
130   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
131 }
132
133 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
134 {
135   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
136   double aFirst, aLast;
137   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
138   if (!aCurve.IsNull()) {
139     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
140     if (!aCirc.IsNull()) {
141       gp_Pnt aLoc = aCirc->Location();
142       std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
143       gp_Dir anAxis = aCirc->Axis().Direction();
144       std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
145       return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
146     }
147   }
148   return std::shared_ptr<GeomAPI_Circ>(); // not circle
149 }
150
151 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
152 {
153   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
154   double aFirst, aLast;
155   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
156   if (!aCurve.IsNull()) {
157     Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
158     if (!aElips.IsNull()) {
159       gp_Elips aGpElips = aElips->Elips();
160       std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
161       aEllipse->setImpl(new gp_Elips(aGpElips));
162       return aEllipse;
163     }
164   }
165   return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
166 }
167
168 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
169 {
170   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
171   double aFirst, aLast;
172   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
173   if (aCurve) {
174     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
175     if (aLine) {
176       gp_Pnt aStartPnt = aLine->Value(aFirst);
177       std::shared_ptr<GeomAPI_Pnt> aStart(
178           new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
179       gp_Pnt aEndPnt = aLine->Value(aLast);
180       std::shared_ptr<GeomAPI_Pnt> aEnd(
181           new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
182       return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
183     }
184   }
185   return std::shared_ptr<GeomAPI_Lin>(); // not circle
186 }
187
188
189 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
190 {
191   if (!theEdge.get() || ! theEdge->isEdge())
192     return false;
193   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
194   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
195
196   if (aMyShape.IsNull() || aInShape.IsNull())
197     return false;
198
199   if (aMyShape.ShapeType() != aInShape.ShapeType())
200     return false;
201
202   double aMyStart, aMyEnd;
203   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
204   double aInStart, aInEnd;
205   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
206
207   // Check that curves a the same type
208   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
209   GeomAdaptor_Curve aInAdaptor(aInCurve);
210   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
211     return false;
212
213   // Check that end point parameters are the same
214   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
215     return false;
216
217   // Check that end points are equal
218   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
219   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
220   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
221   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
222
223   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
224     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
225     return false;
226
227   return true;
228 }
229
230 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
231 {
232   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
233   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
234 }
235
236 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
237 {
238   double aFirst, aLast;
239   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
240   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
241   if (aCurve.IsNull())
242     return false;
243
244   double A, B, C, D;
245   thePlane->coefficients(A, B, C, D);
246   gp_Pln aPlane(A, B, C, D);
247
248   bool inPlane = false;
249   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
250     // check start and end points on the plane
251     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
252     gp_Pnt aLastPnt = aCurve->Value(aLast);
253     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
254               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
255   } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
256     // check the center on the plane and normals are collinear
257     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
258     gp_Pnt aCenter = aCirc->Location();
259     Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
260     inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
261               Abs(Abs(aDot) - 1.0) < Precision::Confusion();
262   } else {
263     // three points checking
264     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
265     gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
266     gp_Pnt aLastPnt = aCurve->Value(aLast);
267     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
268               aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
269               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
270   }
271   return inPlane;
272 }
273
274 double GeomAPI_Edge::length() const
275 {
276   const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
277   BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
278   Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
279   return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
280 }