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