1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include<GeomAPI_Edge.h>
22 #include<GeomAPI_Pln.h>
23 #include<GeomAPI_Pnt.h>
24 #include<GeomAPI_Circ.h>
25 #include<GeomAPI_Dir.h>
26 #include<GeomAPI_Lin.h>
27 #include<GeomAPI_Ax2.h>
28 #include<GeomAPI_Ellipse.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_Curve.hxx>
40 #include <Geom_Line.hxx>
41 #include <Geom_Circle.hxx>
42 #include <Geom_TrimmedCurve.hxx>
43 #include <Geom_Ellipse.hxx>
44 #include <Geom_Plane.hxx>
45 #include <GeomAPI_IntCS.hxx>
46 #include <GeomAdaptor_Curve.hxx>
49 #include <gp_Elips.hxx>
52 #include <GCPnts_AbscissaPoint.hxx>
54 GeomAPI_Edge::GeomAPI_Edge()
56 TopoDS_Edge* anEdge = new TopoDS_Edge;
58 BRep_Builder aBuilder;
59 aBuilder.MakeEdge(*anEdge);
64 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
66 if (!theShape->isNull() && theShape->isEdge()) {
67 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
71 bool GeomAPI_Edge::isLine() const
73 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
75 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
76 if (aCurve.IsNull()) // degenerative edge
78 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
83 /// extracts a circle curve from the arbitrary curve, returns null is it is different type
84 static Handle(Geom_Circle) circ(const Handle(Geom_Curve) theCurve)
86 Handle(Geom_Circle) aResult = Handle(Geom_Circle)::DownCast(theCurve);
87 if (!aResult.IsNull())
89 // check this may be a trimmed curve that contains circle inside
90 Handle(Geom_TrimmedCurve) aTrimmed = Handle(Geom_TrimmedCurve)::DownCast(theCurve);
91 while(!aTrimmed.IsNull()) {
92 aResult = Handle(Geom_Circle)::DownCast(aTrimmed->BasisCurve());
93 if (!aResult.IsNull())
95 aTrimmed = Handle(Geom_TrimmedCurve)::DownCast(aTrimmed->BasisCurve());
97 return aResult; // null, not circle
100 bool GeomAPI_Edge::isCircle() const
102 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
103 double aFirst, aLast;
104 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
105 if (!circ(aCurve).IsNull()) {
106 // Check the difference of first and last parameters to be equal to the curve period
107 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
113 bool GeomAPI_Edge::isArc() const
115 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
116 double aFirst, aLast;
117 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
118 if (!circ(aCurve).IsNull()) {
119 // Check the difference of first and last parameters is not equal the curve period
120 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
126 bool GeomAPI_Edge::isEllipse() const
128 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
129 double aFirst, aLast;
130 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
131 if (aCurve.IsNull()) // degenerative edge
133 if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
138 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
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);
144 aCurve->D0(aFirst, aPoint);
145 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
148 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
150 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
151 double aFirst, aLast;
152 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
154 aCurve->D0(aLast, aPoint);
155 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
158 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() 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 Handle(Geom_Circle) aCirc = circ(aCurve);
164 if (!aCirc.IsNull()) {
165 gp_Pnt aLoc = aCirc->Location();
166 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
167 gp_Dir anAxis = aCirc->Axis().Direction();
168 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
169 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
171 return std::shared_ptr<GeomAPI_Circ>(); // not circle
174 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
176 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
177 double aFirst, aLast;
178 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
179 if (!aCurve.IsNull()) {
180 Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
181 if (!aElips.IsNull()) {
182 gp_Elips aGpElips = aElips->Elips();
183 std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
184 aEllipse->setImpl(new gp_Elips(aGpElips));
188 return std::shared_ptr<GeomAPI_Ellipse>(); // not ellipse
191 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
193 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
194 double aFirst, aLast;
195 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
197 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
199 gp_Pnt aStartPnt = aLine->Value(aFirst);
200 std::shared_ptr<GeomAPI_Pnt> aStart(
201 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
202 gp_Pnt aEndPnt = aLine->Value(aLast);
203 std::shared_ptr<GeomAPI_Pnt> aEnd(
204 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
205 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
208 return std::shared_ptr<GeomAPI_Lin>(); // not circle
212 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
214 if (!theEdge.get() || ! theEdge->isEdge())
216 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
217 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
219 if (aMyShape.IsNull() || aInShape.IsNull())
222 if (aMyShape.ShapeType() != aInShape.ShapeType())
225 double aMyStart, aMyEnd;
226 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
227 double aInStart, aInEnd;
228 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
230 // Check that end point parameters are the same
231 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
234 // Check that curves a the same type
235 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
236 GeomAdaptor_Curve aInAdaptor(aInCurve);
237 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
240 // Check that end points are equal
241 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
242 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
243 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
244 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
246 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
247 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
254 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
256 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
257 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
260 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
262 double aFirst, aLast;
263 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
264 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
269 thePlane->coefficients(A, B, C, D);
270 gp_Pln aPlane(A, B, C, D);
272 bool inPlane = false;
273 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
274 // check start and end points on the plane
275 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
276 gp_Pnt aLastPnt = aCurve->Value(aLast);
277 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
278 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
280 Handle(Geom_Circle) aCirc = circ(aCurve);
281 if (!aCirc.IsNull()) {
282 gp_Pnt aCenter = aCirc->Location();
283 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
284 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
285 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
287 // three points checking
288 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
289 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
290 gp_Pnt aLastPnt = aCurve->Value(aLast);
291 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
292 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
293 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
300 void GeomAPI_Edge::intersectWithPlane(const std::shared_ptr<GeomAPI_Pln> thePlane,
301 std::list<std::shared_ptr<GeomAPI_Pnt>>& theResult) const
303 double aFirst, aLast;
304 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
305 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
306 if (!aCurve.IsNull()) {
308 thePlane->coefficients(A, B, C, D);
309 gp_Pln aPln(A, B, C, D);
311 Handle(Geom_Plane) aPlane = new Geom_Plane(aPln);
312 GeomAPI_IntCS aIntersect;
313 aIntersect.Perform(aCurve, aPlane);
314 if (aIntersect.IsDone() && (aIntersect.NbPoints() > 0)) {
316 for (int i = 1; i <= aIntersect.NbPoints(); i++) {
317 // check the parameter of intersection in the edge range
318 aIntersect.Parameters(i, A, B, C);
319 if (aCurve->IsPeriodic())
320 C = ElCLib::InPeriod(C, aFirst, aFirst + aCurve->Period());
321 if (C < aFirst - Precision::PConfusion() || C > aLast + Precision::PConfusion())
324 // obtain intersection point
325 aPnt = aIntersect.Point(i);
326 std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
327 theResult.push_back(aPntPtr);
333 double GeomAPI_Edge::length() const
335 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
336 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
337 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
338 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
341 bool GeomAPI_Edge::isClosed() const
343 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
346 double aFirst, aLast;
347 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
348 if (aCurve.IsNull() || !aCurve->IsPeriodic())
350 aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
352 return fabs(aFirst - aLast) < 1.e-9;
355 bool GeomAPI_Edge::isDegenerated() const
357 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
358 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
360 return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
363 void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
365 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
366 TopoDS_Vertex aVFirst, aVLast;
367 TopExp::Vertices(anEdge, aVFirst, aVLast);
368 BRep_Builder().UpdateVertex(aVFirst, theTolerance);
371 void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
373 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
374 TopoDS_Vertex aVFirst, aVLast;
375 TopExp::Vertices(anEdge, aVFirst, aVLast);
376 BRep_Builder().UpdateVertex(aVLast, theTolerance);
379 GeomPointPtr GeomAPI_Edge::middlePoint() const
381 GeomPointPtr aMiddlePoint;
383 const TopoDS_Edge& anEdge = impl<TopoDS_Edge>();
386 double aFirst, aLast;
387 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
391 static const int NB_POINTS = 3;
392 GeomAdaptor_Curve aCurveAdaptor(aCurve, aFirst, aLast);
393 GCPnts_UniformAbscissa anAlgo(aCurveAdaptor, NB_POINTS);
394 if (anAlgo.IsDone()) {
395 gp_Pnt aPnt = aCurveAdaptor.Value(anAlgo.Parameter(2));
396 aMiddlePoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));