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 if (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 Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
238 if (!aElips.IsNull()) {
239 gp_Elips aGpElips = aElips->Elips();
240 std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
241 aEllipse->setImpl(new gp_Elips(aGpElips));
245 return std::shared_ptr<GeomAPI_Ellipse>(); // not ellipse
248 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
250 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
251 double aFirst, aLast;
252 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
254 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
256 gp_Pnt aStartPnt = aLine->Value(aFirst);
257 std::shared_ptr<GeomAPI_Pnt> aStart(
258 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
259 gp_Pnt aEndPnt = aLine->Value(aLast);
260 std::shared_ptr<GeomAPI_Pnt> aEnd(
261 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
262 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
265 return std::shared_ptr<GeomAPI_Lin>(); // not circle
269 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
271 if (!theEdge.get() || ! theEdge->isEdge())
273 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
274 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
276 if (aMyShape.IsNull() || aInShape.IsNull())
279 if (aMyShape.ShapeType() != aInShape.ShapeType())
282 double aMyStart, aMyEnd;
283 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
284 double aInStart, aInEnd;
285 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
287 // Check that end point parameters are the same
288 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
291 // Check that curves a the same type
292 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
293 GeomAdaptor_Curve aInAdaptor(aInCurve);
294 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
297 // Check that end points are equal
298 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
299 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
300 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
301 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
303 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
304 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
310 void GeomAPI_Edge::setRange(const double& theFirst, const double& theLast)
312 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
313 double aFirst, aLast;
314 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
315 double aTolerance = BRep_Tool::Tolerance(anEdge);
316 if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve)) {
317 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
318 BRep_Builder().UpdateEdge(anEdge, aCurve, aTolerance);
320 BRep_Builder().Range(anEdge, theFirst, theLast);
323 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
325 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
326 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
330 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
332 double aFirst, aLast;
333 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
334 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
339 thePlane->coefficients(A, B, C, D);
340 gp_Pln aPlane(A, B, C, D);
342 bool inPlane = false;
343 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
344 // check start and end points on the plane
345 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
346 gp_Pnt aLastPnt = aCurve->Value(aLast);
347 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
348 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
350 Handle(Geom_Circle) aCirc = circ(aCurve);
351 if (!aCirc.IsNull()) {
352 gp_Pnt aCenter = aCirc->Location();
353 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
354 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
355 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
357 // three points checking
358 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
359 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
360 gp_Pnt aLastPnt = aCurve->Value(aLast);
361 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
362 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
363 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
370 void GeomAPI_Edge::intersectWithPlane(const std::shared_ptr<GeomAPI_Pln> thePlane,
371 std::list<std::shared_ptr<GeomAPI_Pnt>>& theResult) const
373 double aFirst, aLast;
374 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
375 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
376 if (!aCurve.IsNull()) {
378 thePlane->coefficients(A, B, C, D);
379 gp_Pln aPln(A, B, C, D);
380 Handle(Geom_Plane) aPlane = new Geom_Plane(aPln);
382 // intersect the plane with the curve
383 GeomAPI_IntCS aIntersect;
384 aIntersect.Perform(aCurve, aPlane);
385 if (aIntersect.IsDone() && (aIntersect.NbPoints() > 0)) {
387 for (int i = 1; i <= aIntersect.NbPoints(); i++) {
388 // check the parameter of intersection in the edge range
389 aIntersect.Parameters(i, A, B, C);
390 if (aCurve->IsPeriodic())
391 C = ElCLib::InPeriod(C, aFirst, aFirst + aCurve->Period());
392 if (C < aFirst - Precision::PConfusion() || C > aLast + Precision::PConfusion())
395 // obtain intersection point
396 aPnt = aIntersect.Point(i);
397 std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
398 theResult.push_back(aPntPtr);
402 // find minimal distance between the plane and the curve
403 GeomAPI_ExtremaCurveSurface anExtrema(aCurve, aPlane);
404 double aTolerance = BRep_Tool::Tolerance(TopoDS::Edge(aShape));
405 if (anExtrema.NbExtrema() > 0 &&
406 !anExtrema.Extrema().IsParallel() &&
407 anExtrema.LowerDistance() < aTolerance) {
408 // distance is lower than tolerance => tangent case
410 anExtrema.NearestPoints(aPntC, aPntS);
411 std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPntS.X(), aPntS.Y(), aPntS.Z()));
412 theResult.push_back(aPntPtr);
418 double GeomAPI_Edge::length() const
420 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
421 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
422 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
423 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
426 bool GeomAPI_Edge::isClosed() const
428 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
431 double aFirst, aLast;
432 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
433 if (aCurve.IsNull() || !aCurve->IsPeriodic())
435 aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
437 return fabs(aFirst - aLast) < 1.e-9;
440 bool GeomAPI_Edge::isDegenerated() const
442 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
443 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
445 return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
448 void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
450 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
451 TopoDS_Vertex aVFirst, aVLast;
452 TopExp::Vertices(anEdge, aVFirst, aVLast);
453 BRep_Builder().UpdateVertex(aVFirst, theTolerance);
456 void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
458 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
459 TopoDS_Vertex aVFirst, aVLast;
460 TopExp::Vertices(anEdge, aVFirst, aVLast);
461 BRep_Builder().UpdateVertex(aVLast, theTolerance);
464 GeomPointPtr GeomAPI_Edge::middlePoint() const
466 GeomPointPtr aMiddlePoint;
468 const TopoDS_Edge& anEdge = impl<TopoDS_Edge>();
471 double aFirst, aLast;
472 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
476 static const int NB_POINTS = 3;
477 GeomAdaptor_Curve aCurveAdaptor(aCurve, aFirst, aLast);
478 GCPnts_UniformAbscissa anAlgo(aCurveAdaptor, NB_POINTS);
479 if (anAlgo.IsDone()) {
480 gp_Pnt aPnt = aCurveAdaptor.Value(anAlgo.Parameter(2));
481 aMiddlePoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));