Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into Dev_2.8.0
[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.IsNull()) // degenerative edge
71     return false;
72   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
73     return true;
74   return false;
75 }
76
77 bool GeomAPI_Edge::isCircle() const
78 {
79   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
80   double aFirst, aLast;
81   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
82   if (aCurve.IsNull()) // degenerative edge
83     return false;
84   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
85   {
86     // Check the difference of first and last parameters to be equal to the curve period
87     if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
88       return true;
89   }
90   return false;
91 }
92
93 bool GeomAPI_Edge::isArc() const
94 {
95   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
96   double aFirst, aLast;
97   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
98   if (aCurve.IsNull()) // degenerative edge
99     return false;
100   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
101   {
102     // Check the difference of first and last parameters is not equal the curve period
103     if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
104       return true;
105   }
106   return false;
107 }
108
109 bool GeomAPI_Edge::isEllipse() const
110 {
111   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
112   double aFirst, aLast;
113   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
114   if (aCurve.IsNull()) // degenerative edge
115     return false;
116   if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
117     return true;
118   return false;
119 }
120
121 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
122 {
123   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
124   double aFirst, aLast;
125   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
126   gp_Pnt aPoint;
127   aCurve->D0(aFirst, aPoint);
128   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
129 }
130
131 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
132 {
133   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
134   double aFirst, aLast;
135   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
136   gp_Pnt aPoint;
137   aCurve->D0(aLast, aPoint);
138   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
139 }
140
141 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
142 {
143   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
144   double aFirst, aLast;
145   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
146   if (!aCurve.IsNull()) {
147     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
148     if (!aCirc.IsNull()) {
149       gp_Pnt aLoc = aCirc->Location();
150       std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
151       gp_Dir anAxis = aCirc->Axis().Direction();
152       std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
153       return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
154     }
155   }
156   return std::shared_ptr<GeomAPI_Circ>(); // not circle
157 }
158
159 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
160 {
161   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
162   double aFirst, aLast;
163   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
164   if (!aCurve.IsNull()) {
165     Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
166     if (!aElips.IsNull()) {
167       gp_Elips aGpElips = aElips->Elips();
168       std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
169       aEllipse->setImpl(new gp_Elips(aGpElips));
170       return aEllipse;
171     }
172   }
173   return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
174 }
175
176 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
177 {
178   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
179   double aFirst, aLast;
180   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
181   if (aCurve) {
182     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
183     if (aLine) {
184       gp_Pnt aStartPnt = aLine->Value(aFirst);
185       std::shared_ptr<GeomAPI_Pnt> aStart(
186           new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
187       gp_Pnt aEndPnt = aLine->Value(aLast);
188       std::shared_ptr<GeomAPI_Pnt> aEnd(
189           new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
190       return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
191     }
192   }
193   return std::shared_ptr<GeomAPI_Lin>(); // not circle
194 }
195
196
197 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
198 {
199   if (!theEdge.get() || ! theEdge->isEdge())
200     return false;
201   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
202   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
203
204   if (aMyShape.IsNull() || aInShape.IsNull())
205     return false;
206
207   if (aMyShape.ShapeType() != aInShape.ShapeType())
208     return false;
209
210   double aMyStart, aMyEnd;
211   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
212   double aInStart, aInEnd;
213   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
214
215   // Check that curves a the same type
216   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
217   GeomAdaptor_Curve aInAdaptor(aInCurve);
218   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
219     return false;
220
221   // Check that end point parameters are the same
222   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
223     return false;
224
225   // Check that end points are equal
226   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
227   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
228   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
229   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
230
231   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
232     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
233     return false;
234
235   return true;
236 }
237
238 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
239 {
240   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
241   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
242 }
243
244 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
245 {
246   double aFirst, aLast;
247   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
248   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
249   if (aCurve.IsNull())
250     return false;
251
252   double A, B, C, D;
253   thePlane->coefficients(A, B, C, D);
254   gp_Pln aPlane(A, B, C, D);
255
256   bool inPlane = false;
257   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
258     // check start and end points on the plane
259     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
260     gp_Pnt aLastPnt = aCurve->Value(aLast);
261     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
262               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
263   } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
264     // check the center on the plane and normals are collinear
265     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
266     gp_Pnt aCenter = aCirc->Location();
267     Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
268     inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
269               Abs(Abs(aDot) - 1.0) < Precision::Confusion();
270   } else {
271     // three points checking
272     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
273     gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
274     gp_Pnt aLastPnt = aCurve->Value(aLast);
275     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
276               aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
277               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
278   }
279   return inPlane;
280 }
281
282 double GeomAPI_Edge::length() const
283 {
284   const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
285   BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
286   Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
287   return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
288 }
289
290 bool GeomAPI_Edge::isClosed() const
291 {
292   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
293   if (aShape.IsNull())
294     return false;
295   double aFirst, aLast;
296   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
297   if (aCurve.IsNull() || !aCurve->IsPeriodic())
298     return false;
299   aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
300
301   return fabs(aFirst - aLast) < 1.e-9;
302 }
303
304 bool GeomAPI_Edge::isDegenerated() const
305 {
306   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
307   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
308     return false;
309   return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
310 }