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