1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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>
30 #include <BRepAdaptor_Curve.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Edge.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRep_Tool.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>
52 #include <gp_Elips.hxx>
55 #include <GCPnts_AbscissaPoint.hxx>
57 GeomAPI_Edge::GeomAPI_Edge()
59 TopoDS_Edge* anEdge = new TopoDS_Edge;
61 BRep_Builder aBuilder;
62 aBuilder.MakeEdge(*anEdge);
67 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
69 if (!theShape->isNull() && theShape->isEdge()) {
70 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
74 void GeomAPI_Edge::vertices(std::shared_ptr<GeomAPI_Vertex>& theStartVertex,
75 std::shared_ptr<GeomAPI_Vertex>& theEndVertex) const
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));
86 static Handle(Geom_Curve) baseCurve(const TopoDS_Edge& theEdge)
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();
97 bool GeomAPI_Edge::isSameGeometry(const std::shared_ptr<GeomAPI_Shape> theShape) const
99 if (!theShape->isEdge())
101 if (isSame(theShape))
104 TopoDS_Edge anOwnEdge = TopoDS::Edge(impl<TopoDS_Shape>());
105 TopoDS_Edge anOtherEdge = TopoDS::Edge(theShape->impl<TopoDS_Shape>());
107 Handle(Geom_Curve) anOwnCurve = baseCurve(anOwnEdge);
108 Handle(Geom_Curve) anOtherCurve = baseCurve(anOtherEdge);
109 GeomAPI_ExtremaCurveCurve anExtrema(anOwnCurve, anOtherCurve);
111 bool isSame = anExtrema.Extrema().IsParallel() &&
112 anExtrema.TotalLowerDistance() < Precision::Confusion();
116 bool GeomAPI_Edge::isLine() const
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
123 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
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)
131 Handle(Geom_Circle) aResult = Handle(Geom_Circle)::DownCast(theCurve);
132 if (!aResult.IsNull())
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())
140 aTrimmed = Handle(Geom_TrimmedCurve)::DownCast(aTrimmed->BasisCurve());
142 return aResult; // null, not circle
145 bool GeomAPI_Edge::isCircle() const
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())
158 bool GeomAPI_Edge::isArc() const
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())
171 bool GeomAPI_Edge::isEllipse() const
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
178 while (aCurve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve)))
179 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
180 return aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse));
183 bool GeomAPI_Edge::isBSpline() const
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
190 while (aCurve->IsKind(STANDARD_TYPE(Geom_TrimmedCurve)))
191 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
192 return aCurve->IsKind(STANDARD_TYPE(Geom_BSplineCurve));
195 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
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);
201 aCurve->D0(aFirst, aPoint);
202 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
205 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
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);
211 aCurve->D0(aLast, aPoint);
212 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
215 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
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()));
228 return std::shared_ptr<GeomAPI_Circ>(); // not circle
231 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
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));
247 return std::shared_ptr<GeomAPI_Ellipse>(); // not ellipse
250 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
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);
256 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
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));
267 return std::shared_ptr<GeomAPI_Lin>(); // not circle
271 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
273 if (!theEdge.get() || ! theEdge->isEdge())
275 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
276 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
278 if (aMyShape.IsNull() || aInShape.IsNull())
281 if (aMyShape.ShapeType() != aInShape.ShapeType())
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);
289 // Check that end point parameters are the same
290 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
293 // Check that curves a the same type
294 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
295 GeomAdaptor_Curve aInAdaptor(aInCurve);
296 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
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);
305 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
306 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
312 void GeomAPI_Edge::setRange(const double& theFirst, const double& theLast)
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);
322 BRep_Builder().Range(anEdge, theFirst, theLast);
325 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
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);
332 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
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);
341 thePlane->coefficients(A, B, C, D);
342 gp_Pln aPlane(A, B, C, D);
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();
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();
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();
372 void GeomAPI_Edge::intersectWithPlane(const std::shared_ptr<GeomAPI_Pln> thePlane,
373 std::list<std::shared_ptr<GeomAPI_Pnt>>& theResult) const
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()) {
380 thePlane->coefficients(A, B, C, D);
381 gp_Pln aPln(A, B, C, D);
382 Handle(Geom_Plane) aPlane = new Geom_Plane(aPln);
384 // intersect the plane with the curve
385 GeomAPI_IntCS aIntersect;
386 aIntersect.Perform(aCurve, aPlane);
387 if (aIntersect.IsDone() && (aIntersect.NbPoints() > 0)) {
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())
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);
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
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);
420 double GeomAPI_Edge::length() const
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);
428 bool GeomAPI_Edge::isClosed() const
430 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
433 double aFirst, aLast;
434 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
435 if (aCurve.IsNull() || !aCurve->IsPeriodic())
437 aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
439 return fabs(aFirst - aLast) < 1.e-9;
442 bool GeomAPI_Edge::isDegenerated() const
444 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
445 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
447 return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
450 void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
452 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
453 TopoDS_Vertex aVFirst, aVLast;
454 TopExp::Vertices(anEdge, aVFirst, aVLast);
455 BRep_Builder().UpdateVertex(aVFirst, theTolerance);
458 void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
460 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
461 TopoDS_Vertex aVFirst, aVLast;
462 TopExp::Vertices(anEdge, aVFirst, aVLast);
463 BRep_Builder().UpdateVertex(aVLast, theTolerance);
466 double GeomAPI_Edge::firstPointTolerance() const
468 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
469 TopoDS_Vertex aVFirst, aVLast;
470 TopExp::Vertices(anEdge, aVFirst, aVLast);
471 return BRep_Tool::Tolerance(aVFirst);
474 double GeomAPI_Edge::lastPointTolerance() const
476 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
477 TopoDS_Vertex aVFirst, aVLast;
478 TopExp::Vertices(anEdge, aVFirst, aVLast);
479 return BRep_Tool::Tolerance(aVLast);
482 GeomPointPtr GeomAPI_Edge::middlePoint() const
484 GeomPointPtr aMiddlePoint;
486 const TopoDS_Edge& anEdge = impl<TopoDS_Edge>();
489 double aFirst, aLast;
490 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
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()));