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_Ellipse.hxx>
43 #include <Geom_Plane.hxx>
44 #include <GeomAPI_IntCS.hxx>
45 #include <GeomAdaptor_Curve.hxx>
48 #include <gp_Elips.hxx>
51 #include <GCPnts_AbscissaPoint.hxx>
53 GeomAPI_Edge::GeomAPI_Edge()
55 TopoDS_Edge* anEdge = new TopoDS_Edge;
57 BRep_Builder aBuilder;
58 aBuilder.MakeEdge(*anEdge);
63 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
65 if (!theShape->isNull() && theShape->isEdge()) {
66 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
70 bool GeomAPI_Edge::isLine() const
72 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
74 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
75 if (aCurve.IsNull()) // degenerative edge
77 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
82 bool GeomAPI_Edge::isCircle() const
84 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
86 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
87 if (aCurve.IsNull()) // degenerative edge
89 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
91 // Check the difference of first and last parameters to be equal to the curve period
92 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
98 bool GeomAPI_Edge::isArc() const
100 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
101 double aFirst, aLast;
102 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
103 if (aCurve.IsNull()) // degenerative edge
105 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
107 // Check the difference of first and last parameters is not equal the curve period
108 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
114 bool GeomAPI_Edge::isEllipse() const
116 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
117 double aFirst, aLast;
118 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
119 if (aCurve.IsNull()) // degenerative edge
121 if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
126 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
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);
132 aCurve->D0(aFirst, aPoint);
133 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
136 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
138 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
139 double aFirst, aLast;
140 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
142 aCurve->D0(aLast, aPoint);
143 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
146 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
148 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
149 double aFirst, aLast;
150 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
151 if (!aCurve.IsNull()) {
152 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
153 if (!aCirc.IsNull()) {
154 gp_Pnt aLoc = aCirc->Location();
155 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
156 gp_Dir anAxis = aCirc->Axis().Direction();
157 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
158 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
161 return std::shared_ptr<GeomAPI_Circ>(); // not circle
164 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
166 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
167 double aFirst, aLast;
168 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
169 if (!aCurve.IsNull()) {
170 Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
171 if (!aElips.IsNull()) {
172 gp_Elips aGpElips = aElips->Elips();
173 std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
174 aEllipse->setImpl(new gp_Elips(aGpElips));
178 return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
181 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
183 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
184 double aFirst, aLast;
185 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
187 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
189 gp_Pnt aStartPnt = aLine->Value(aFirst);
190 std::shared_ptr<GeomAPI_Pnt> aStart(
191 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
192 gp_Pnt aEndPnt = aLine->Value(aLast);
193 std::shared_ptr<GeomAPI_Pnt> aEnd(
194 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
195 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
198 return std::shared_ptr<GeomAPI_Lin>(); // not circle
202 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
204 if (!theEdge.get() || ! theEdge->isEdge())
206 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
207 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
209 if (aMyShape.IsNull() || aInShape.IsNull())
212 if (aMyShape.ShapeType() != aInShape.ShapeType())
215 double aMyStart, aMyEnd;
216 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
217 double aInStart, aInEnd;
218 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
220 // Check that curves a the same type
221 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
222 GeomAdaptor_Curve aInAdaptor(aInCurve);
223 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
226 // Check that end point parameters are the same
227 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
230 // Check that end points are equal
231 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
232 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
233 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
234 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
236 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
237 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
243 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
245 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
246 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
249 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
251 double aFirst, aLast;
252 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
253 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
258 thePlane->coefficients(A, B, C, D);
259 gp_Pln aPlane(A, B, C, D);
261 bool inPlane = false;
262 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
263 // check start and end points on the plane
264 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
265 gp_Pnt aLastPnt = aCurve->Value(aLast);
266 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
267 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
268 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
269 // check the center on the plane and normals are collinear
270 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
271 gp_Pnt aCenter = aCirc->Location();
272 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
273 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
274 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
276 // three points checking
277 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
278 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
279 gp_Pnt aLastPnt = aCurve->Value(aLast);
280 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
281 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
282 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
287 void GeomAPI_Edge::intersectWithPlane(const std::shared_ptr<GeomAPI_Pln> thePlane,
288 std::list<std::shared_ptr<GeomAPI_Pnt>>& theResult) const
290 double aFirst, aLast;
291 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
292 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
293 if (!aCurve.IsNull()) {
295 thePlane->coefficients(A, B, C, D);
296 gp_Pln aPln(A, B, C, D);
298 Handle(Geom_Plane) aPlane = new Geom_Plane(aPln);
299 GeomAPI_IntCS aIntersect;
300 aIntersect.Perform(aCurve, aPlane);
301 if (aIntersect.IsDone() && (aIntersect.NbPoints() > 0)) {
303 for (int i = 1; i <= aIntersect.NbPoints(); i++) {
304 // check the parameter of intersection in the edge range
305 aIntersect.Parameters(i, A, B, C);
306 if (aCurve->IsPeriodic())
307 C = ElCLib::InPeriod(C, aFirst, aFirst + aCurve->Period());
308 if (C < aFirst - Precision::PConfusion() || C > aLast + Precision::PConfusion())
311 // obtain intersection point
312 aPnt = aIntersect.Point(i);
313 std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
314 theResult.push_back(aPntPtr);
320 double GeomAPI_Edge::length() const
322 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
323 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
324 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
325 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
328 bool GeomAPI_Edge::isClosed() const
330 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
333 double aFirst, aLast;
334 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
335 if (aCurve.IsNull() || !aCurve->IsPeriodic())
337 aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
339 return fabs(aFirst - aLast) < 1.e-9;
342 bool GeomAPI_Edge::isDegenerated() const
344 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
345 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
347 return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
350 void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
352 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
353 TopoDS_Vertex aVFirst, aVLast;
354 TopExp::Vertices(anEdge, aVFirst, aVLast);
355 BRep_Builder().UpdateVertex(aVFirst, theTolerance);
358 void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
360 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
361 TopoDS_Vertex aVFirst, aVLast;
362 TopExp::Vertices(anEdge, aVFirst, aVLast);
363 BRep_Builder().UpdateVertex(aVLast, theTolerance);
366 GeomPointPtr GeomAPI_Edge::middlePoint() const
368 GeomPointPtr aMiddlePoint;
370 const TopoDS_Edge& anEdge = impl<TopoDS_Edge>();
373 double aFirst, aLast;
374 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
378 static const int NB_POINTS = 3;
379 GeomAdaptor_Curve aCurveAdaptor(aCurve, aFirst, aLast);
380 GCPnts_UniformAbscissa anAlgo(aCurveAdaptor, NB_POINTS);
381 if (anAlgo.IsDone()) {
382 gp_Pnt aPnt = aCurveAdaptor.Value(anAlgo.Parameter(2));
383 aMiddlePoint = GeomPointPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));