Salome HOME
379bdab86b9f396eee35febc5f608727d43d321b
[modules/shaper.git] / src / GeomAPI / GeomAPI_Edge.cpp
1 // Copyright (C) 2014-2019  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
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 #include<GeomAPI_Ax2.h>
27 #include<GeomAPI_Ellipse.h>
28
29 #include <BRepAdaptor_Curve.hxx>
30
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Edge.hxx>
33 #include <TopoDS.hxx>
34 #include <BRep_Builder.hxx>
35 #include <BRep_Tool.hxx>
36 #include <ElCLib.hxx>
37 #include <GCPnts_UniformAbscissa.hxx>
38 #include <Geom_Curve.hxx>
39 #include <Geom_Line.hxx>
40 #include <Geom_Circle.hxx>
41 #include <Geom_TrimmedCurve.hxx>
42 #include <Geom_Ellipse.hxx>
43 #include <Geom_Plane.hxx>
44 #include <GeomAPI_ExtremaCurveSurface.hxx>
45 #include <GeomAPI_IntCS.hxx>
46 #include <GeomAdaptor_Curve.hxx>
47 #include <gp_Ax1.hxx>
48 #include <gp_Pln.hxx>
49 #include <gp_Elips.hxx>
50 #include <TopExp.hxx>
51
52 #include <GCPnts_AbscissaPoint.hxx>
53
54 GeomAPI_Edge::GeomAPI_Edge()
55 {
56   TopoDS_Edge* anEdge = new TopoDS_Edge;
57
58   BRep_Builder aBuilder;
59   aBuilder.MakeEdge(*anEdge);
60
61   setImpl(anEdge);
62 }
63
64 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
65 {
66   if (!theShape->isNull() && theShape->isEdge()) {
67     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
68   }
69 }
70
71 bool GeomAPI_Edge::isLine() 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.IsNull()) // degenerative edge
77     return false;
78   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
79     return true;
80   return false;
81 }
82
83 /// extracts a circle curve from the arbitrary curve, returns null is it is different type
84 static Handle(Geom_Circle) circ(const Handle(Geom_Curve) theCurve)
85 {
86   Handle(Geom_Circle) aResult = Handle(Geom_Circle)::DownCast(theCurve);
87   if (!aResult.IsNull())
88     return aResult;
89   // check this may be a trimmed curve that contains circle inside
90   Handle(Geom_TrimmedCurve) aTrimmed = Handle(Geom_TrimmedCurve)::DownCast(theCurve);
91   while(!aTrimmed.IsNull()) {
92     aResult = Handle(Geom_Circle)::DownCast(aTrimmed->BasisCurve());
93     if (!aResult.IsNull())
94       return aResult;
95     aTrimmed = Handle(Geom_TrimmedCurve)::DownCast(aTrimmed->BasisCurve());
96   }
97   return aResult; // null, not circle
98 }
99
100 bool GeomAPI_Edge::isCircle() const
101 {
102   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
103   double aFirst, aLast;
104   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
105   if (!circ(aCurve).IsNull()) {
106     // Check the difference of first and last parameters to be equal to the curve period
107     if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
108       return true;
109   }
110   return false;
111 }
112
113 bool GeomAPI_Edge::isArc() const
114 {
115   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
116   double aFirst, aLast;
117   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
118   if (!circ(aCurve).IsNull()) {
119     // Check the difference of first and last parameters is not equal the curve period
120     if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
121       return true;
122   }
123   return false;
124 }
125
126 bool GeomAPI_Edge::isEllipse() const
127 {
128   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
129   double aFirst, aLast;
130   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
131   if (aCurve.IsNull()) // degenerative edge
132     return false;
133   if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
134     return true;
135   return false;
136 }
137
138 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
139 {
140   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
141   double aFirst, aLast;
142   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
143   gp_Pnt aPoint;
144   aCurve->D0(aFirst, aPoint);
145   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
146 }
147
148 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
149 {
150   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
151   double aFirst, aLast;
152   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
153   gp_Pnt aPoint;
154   aCurve->D0(aLast, aPoint);
155   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
156 }
157
158 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
159 {
160   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
161   double aFirst, aLast;
162   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
163   Handle(Geom_Circle) aCirc = circ(aCurve);
164   if (!aCirc.IsNull()) {
165     gp_Pnt aLoc = aCirc->Location();
166     std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
167     gp_Dir anAxis = aCirc->Axis().Direction();
168     std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
169     return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
170   }
171   return std::shared_ptr<GeomAPI_Circ>(); // not circle
172 }
173
174 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
175 {
176   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
177   double aFirst, aLast;
178   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
179   if (!aCurve.IsNull()) {
180     Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
181     if (!aElips.IsNull()) {
182       gp_Elips aGpElips = aElips->Elips();
183       std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
184       aEllipse->setImpl(new gp_Elips(aGpElips));
185       return aEllipse;
186     }
187   }
188   return std::shared_ptr<GeomAPI_Ellipse>(); // not ellipse
189 }
190
191 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
192 {
193   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
194   double aFirst, aLast;
195   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
196   if (aCurve) {
197     Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
198     if (aLine) {
199       gp_Pnt aStartPnt = aLine->Value(aFirst);
200       std::shared_ptr<GeomAPI_Pnt> aStart(
201           new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
202       gp_Pnt aEndPnt = aLine->Value(aLast);
203       std::shared_ptr<GeomAPI_Pnt> aEnd(
204           new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
205       return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
206     }
207   }
208   return std::shared_ptr<GeomAPI_Lin>(); // not circle
209 }
210
211
212 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
213 {
214   if (!theEdge.get() || ! theEdge->isEdge())
215     return false;
216   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
217   const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
218
219   if (aMyShape.IsNull() || aInShape.IsNull())
220     return false;
221
222   if (aMyShape.ShapeType() != aInShape.ShapeType())
223     return false;
224
225   double aMyStart, aMyEnd;
226   Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
227   double aInStart, aInEnd;
228   Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
229
230   // Check that end point parameters are the same
231   if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
232     return false;
233
234   // Check that curves a the same type
235   GeomAdaptor_Curve aMyAdaptor(aMyCurve);
236   GeomAdaptor_Curve aInAdaptor(aInCurve);
237   if (aMyAdaptor.GetType() != aInAdaptor.GetType())
238     return false;
239
240   // Check that end points are equal
241   gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
242   gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
243   gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
244   gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
245
246   if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
247     (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
248     return false;
249
250   return true;
251 }
252
253 // LCOV_EXCL_START
254 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
255 {
256   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
257   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
258 }
259
260 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
261 {
262   double aFirst, aLast;
263   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
264   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
265   if (aCurve.IsNull())
266     return false;
267
268   double A, B, C, D;
269   thePlane->coefficients(A, B, C, D);
270   gp_Pln aPlane(A, B, C, D);
271
272   bool inPlane = false;
273   if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
274     // check start and end points on the plane
275     gp_Pnt aFirstPnt = aCurve->Value(aFirst);
276     gp_Pnt aLastPnt = aCurve->Value(aLast);
277     inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
278               aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
279   } else {
280     Handle(Geom_Circle) aCirc = circ(aCurve);
281     if (!aCirc.IsNull()) {
282       gp_Pnt aCenter = aCirc->Location();
283       Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
284       inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
285                 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
286     } else {
287       // three points checking
288       gp_Pnt aFirstPnt = aCurve->Value(aFirst);
289       gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
290       gp_Pnt aLastPnt = aCurve->Value(aLast);
291       inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
292                 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
293                 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
294     }
295   }
296   return inPlane;
297 }
298 // LCOV_EXCL_STOP
299
300 void GeomAPI_Edge::intersectWithPlane(const std::shared_ptr<GeomAPI_Pln> thePlane,
301                                       std::list<std::shared_ptr<GeomAPI_Pnt>>& theResult) const
302 {
303   double aFirst, aLast;
304   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
305   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
306   if (!aCurve.IsNull()) {
307     double A, B, C, D;
308     thePlane->coefficients(A, B, C, D);
309     gp_Pln aPln(A, B, C, D);
310     Handle(Geom_Plane) aPlane = new Geom_Plane(aPln);
311
312     // intersect the plane with the curve
313     GeomAPI_IntCS aIntersect;
314     aIntersect.Perform(aCurve, aPlane);
315     if (aIntersect.IsDone() && (aIntersect.NbPoints() > 0)) {
316       gp_Pnt aPnt;
317       for (int i = 1; i <= aIntersect.NbPoints(); i++) {
318         // check the parameter of intersection in the edge range
319         aIntersect.Parameters(i, A, B, C);
320         if (aCurve->IsPeriodic())
321           C = ElCLib::InPeriod(C, aFirst, aFirst + aCurve->Period());
322         if (C < aFirst - Precision::PConfusion() || C > aLast + Precision::PConfusion())
323           continue;
324
325         // obtain intersection point
326         aPnt = aIntersect.Point(i);
327         std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
328         theResult.push_back(aPntPtr);
329       }
330     }
331     else {
332       // find minimal distance between the plane and the curve
333       GeomAPI_ExtremaCurveSurface anExtrema(aCurve, aPlane);
334       double aTolerance = BRep_Tool::Tolerance(TopoDS::Edge(aShape));
335       if (anExtrema.LowerDistance() < aTolerance) {
336         // distance is lower than tolerance => tangent case
337         gp_Pnt aPntC, aPntS;
338         anExtrema.NearestPoints(aPntC, aPntS);
339         std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPntS.X(), aPntS.Y(), aPntS.Z()));
340         theResult.push_back(aPntPtr);
341       }
342     }
343   }
344 }
345
346 double GeomAPI_Edge::length() const
347 {
348   const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
349   BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
350   Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
351   return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
352 }
353
354 bool GeomAPI_Edge::isClosed() const
355 {
356   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
357   if (aShape.IsNull())
358     return false;
359   double aFirst, aLast;
360   Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
361   if (aCurve.IsNull() || !aCurve->IsPeriodic())
362     return false;
363   aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
364
365   return fabs(aFirst - aLast) < 1.e-9;
366 }
367
368 bool GeomAPI_Edge::isDegenerated() const
369 {
370   const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
371   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
372     return false;
373   return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
374 }
375
376 void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
377 {
378   TopoDS_Edge anEdge = impl<TopoDS_Edge>();
379   TopoDS_Vertex aVFirst, aVLast;
380   TopExp::Vertices(anEdge, aVFirst, aVLast);
381   BRep_Builder().UpdateVertex(aVFirst, theTolerance);
382 }
383
384 void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
385 {
386   TopoDS_Edge anEdge = impl<TopoDS_Edge>();
387   TopoDS_Vertex aVFirst, aVLast;
388   TopExp::Vertices(anEdge, aVFirst, aVLast);
389   BRep_Builder().UpdateVertex(aVLast, theTolerance);
390 }
391
392 GeomPointPtr GeomAPI_Edge::middlePoint() const
393 {
394   GeomPointPtr aMiddlePoint;
395
396   const TopoDS_Edge& anEdge = impl<TopoDS_Edge>();
397   if (anEdge.IsNull())
398     return aMiddlePoint;
399   double aFirst, aLast;
400   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
401   if (aCurve.IsNull())
402     return aMiddlePoint;
403
404   static const int NB_POINTS = 3;
405   GeomAdaptor_Curve aCurveAdaptor(aCurve, aFirst, aLast);
406   GCPnts_UniformAbscissa anAlgo(aCurveAdaptor, NB_POINTS);
407   if (anAlgo.IsDone()) {
408     gp_Pnt aPnt = aCurveAdaptor.Value(anAlgo.Parameter(2));
409     aMiddlePoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
410   }
411   return aMiddlePoint;
412 }