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