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