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