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