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