Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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
28 #include <BRepAdaptor_Curve.hxx>
29
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Edge.hxx>
32 #include <TopoDS.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRep_Tool.hxx>
35 #include <Geom_Curve.hxx>
36 #include <Geom_Line.hxx>
37 #include <Geom_Circle.hxx>
38 #include <GeomAdaptor_Curve.hxx>
39 #include <gp_Ax1.hxx>
40 #include <gp_Pln.hxx>
41
42 #include <GCPnts_AbscissaPoint.hxx>
43
44 GeomAPI_Edge::GeomAPI_Edge()
45 {
46   TopoDS_Edge* anEdge = new TopoDS_Edge;
47
48   BRep_Builder aBuilder;
49   aBuilder.MakeEdge(*anEdge);
50
51   setImpl(anEdge);
52 }
53
54 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
55 {
56   if (!theShape->isNull() && theShape->isEdge()) {
57     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
58   }
59 }
60
61 bool GeomAPI_Edge::isLine() 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_Line)))
67     return true;
68   return false;
69 }
70
71 bool GeomAPI_Edge::isCircle() const
72 {
73   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
74   double aFirst, aLast;
75   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
76   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
77   {
78     // Check the difference of first and last parameters to be equal to the curve period
79     if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
80       return true;
81   }
82   return false;
83 }
84
85 bool GeomAPI_Edge::isArc() const
86 {
87   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
88   double aFirst, aLast;
89   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
90   if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
91   {
92     // Check the difference of first and last parameters is not equal the curve period
93     if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
94       return true;
95   }
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()
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) {
125     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
126     if (aCirc) {
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_Lin> GeomAPI_Edge::line()
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) {
143     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
144     if (aLine) {
145       gp_Pnt aStartPnt = aLine->Value(aFirst);
146       std::shared_ptr<GeomAPI_Pnt> aStart(
147           new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
148       gp_Pnt aEndPnt = aLine->Value(aLast);
149       std::shared_ptr<GeomAPI_Pnt> aEnd(
150           new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
151       return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
152     }
153   }
154   return std::shared_ptr<GeomAPI_Lin>(); // not circle
155 }
156
157
158 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
159 {
160   if (!theEdge.get() || ! theEdge->isEdge())
161     return false;
162   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
163   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
164
165   if (aMyShape.IsNull() || aInShape.IsNull())
166     return false;
167
168   if (aMyShape.ShapeType() != aInShape.ShapeType())
169     return false;
170
171   double aMyStart, aMyEnd;
172   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
173   double aInStart, aInEnd;
174   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
175
176   // Check that curves a the same type
177   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
178   GeomAdaptor_Curve aInAdaptor(aInCurve);
179   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
180     return false;
181
182   // Check that end point parameters are the same
183   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
184     return false;
185
186   // Check that end points are equal
187   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
188   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
189   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
190   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
191
192   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
193     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
194     return false;
195
196   return true;
197 }
198
199 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
200 {
201   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
202   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
203 }
204
205 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
206 {
207   double aFirst, aLast;
208   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
209   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
210   if (aCurve.IsNull())
211     return false;
212
213   double A, B, C, D;
214   thePlane->coefficients(A, B, C, D);
215   gp_Pln aPlane(A, B, C, D);
216
217   bool inPlane = false;
218   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
219     // check start and end points on the plane
220     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
221     gp_Pnt aLastPnt = aCurve->Value(aLast);
222     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
223               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
224   } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
225     // check the center on the plane and normals are collinear
226     Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
227     gp_Pnt aCenter = aCirc->Location();
228     Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
229     inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
230               Abs(Abs(aDot) - 1.0) < Precision::Confusion();
231   } else {
232     // three points checking
233     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
234     gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
235     gp_Pnt aLastPnt = aCurve->Value(aLast);
236     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
237               aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
238               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
239   }
240   return inPlane;
241 }
242
243 double GeomAPI_Edge::length() const
244 {
245   const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
246   BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
247   Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
248   return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
249 }