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>
37 #include <Geom_Curve.hxx>
38 #include <Geom_Line.hxx>
39 #include <Geom_Circle.hxx>
40 #include <Geom_Ellipse.hxx>
41 #include <Geom_Plane.hxx>
42 #include <GeomAPI_IntCS.hxx>
43 #include <GeomAdaptor_Curve.hxx>
46 #include <gp_Elips.hxx>
49 #include <GCPnts_AbscissaPoint.hxx>
51 GeomAPI_Edge::GeomAPI_Edge()
53 TopoDS_Edge* anEdge = new TopoDS_Edge;
55 BRep_Builder aBuilder;
56 aBuilder.MakeEdge(*anEdge);
61 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
63 if (!theShape->isNull() && theShape->isEdge()) {
64 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
68 bool GeomAPI_Edge::isLine() const
70 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
72 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
73 if (aCurve.IsNull()) // degenerative edge
75 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
80 bool GeomAPI_Edge::isCircle() const
82 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
84 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
85 if (aCurve.IsNull()) // degenerative edge
87 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
89 // Check the difference of first and last parameters to be equal to the curve period
90 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
96 bool GeomAPI_Edge::isArc() const
98 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
100 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
101 if (aCurve.IsNull()) // degenerative edge
103 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
105 // Check the difference of first and last parameters is not equal the curve period
106 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
112 bool GeomAPI_Edge::isEllipse() const
114 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
115 double aFirst, aLast;
116 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
117 if (aCurve.IsNull()) // degenerative edge
119 if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
124 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
126 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
127 double aFirst, aLast;
128 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
130 aCurve->D0(aFirst, aPoint);
131 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
134 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
136 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
137 double aFirst, aLast;
138 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
140 aCurve->D0(aLast, aPoint);
141 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
144 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
146 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
147 double aFirst, aLast;
148 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
149 if (!aCurve.IsNull()) {
150 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
151 if (!aCirc.IsNull()) {
152 gp_Pnt aLoc = aCirc->Location();
153 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
154 gp_Dir anAxis = aCirc->Axis().Direction();
155 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
156 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
159 return std::shared_ptr<GeomAPI_Circ>(); // not circle
162 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
164 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
165 double aFirst, aLast;
166 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
167 if (!aCurve.IsNull()) {
168 Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
169 if (!aElips.IsNull()) {
170 gp_Elips aGpElips = aElips->Elips();
171 std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
172 aEllipse->setImpl(new gp_Elips(aGpElips));
176 return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
179 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
181 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
182 double aFirst, aLast;
183 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
185 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
187 gp_Pnt aStartPnt = aLine->Value(aFirst);
188 std::shared_ptr<GeomAPI_Pnt> aStart(
189 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
190 gp_Pnt aEndPnt = aLine->Value(aLast);
191 std::shared_ptr<GeomAPI_Pnt> aEnd(
192 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
193 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
196 return std::shared_ptr<GeomAPI_Lin>(); // not circle
200 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
202 if (!theEdge.get() || ! theEdge->isEdge())
204 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
205 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
207 if (aMyShape.IsNull() || aInShape.IsNull())
210 if (aMyShape.ShapeType() != aInShape.ShapeType())
213 double aMyStart, aMyEnd;
214 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
215 double aInStart, aInEnd;
216 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
218 // Check that curves a the same type
219 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
220 GeomAdaptor_Curve aInAdaptor(aInCurve);
221 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
224 // Check that end point parameters are the same
225 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
228 // Check that end points are equal
229 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
230 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
231 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
232 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
234 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
235 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
241 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
243 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
244 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
247 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
249 double aFirst, aLast;
250 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
251 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
256 thePlane->coefficients(A, B, C, D);
257 gp_Pln aPlane(A, B, C, D);
259 bool inPlane = false;
260 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
261 // check start and end points on the plane
262 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
263 gp_Pnt aLastPnt = aCurve->Value(aLast);
264 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
265 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
266 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
267 // check the center on the plane and normals are collinear
268 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
269 gp_Pnt aCenter = aCirc->Location();
270 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
271 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
272 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
274 // three points checking
275 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
276 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
277 gp_Pnt aLastPnt = aCurve->Value(aLast);
278 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
279 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
280 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
285 std::list<std::shared_ptr<GeomAPI_Pnt>> GeomAPI_Edge::intersectWithPlane(
286 const std::shared_ptr<GeomAPI_Pln> thePlane) const
288 std::list<GeomPointPtr> aResList;
289 double aFirst, aLast;
290 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
291 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
292 if (!aCurve.IsNull()) {
294 thePlane->coefficients(A, B, C, D);
295 gp_Pln aPln(A, B, C, D);
297 Handle(Geom_Plane) aPlane = new Geom_Plane(aPln);
298 GeomAPI_IntCS aIntersect;
299 aIntersect.Perform(aCurve, aPlane);
300 if (aIntersect.IsDone() && (aIntersect.NbPoints() > 0)) {
302 for (int i = 1; i <= aIntersect.NbPoints(); i++) {
303 aPnt = aIntersect.Point(i);
304 std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
305 aResList.push_back(aPntPtr);
312 double GeomAPI_Edge::length() const
314 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
315 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
316 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
317 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
320 bool GeomAPI_Edge::isClosed() const
322 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
325 double aFirst, aLast;
326 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
327 if (aCurve.IsNull() || !aCurve->IsPeriodic())
329 aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
331 return fabs(aFirst - aLast) < 1.e-9;
334 bool GeomAPI_Edge::isDegenerated() const
336 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
337 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
339 return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
342 void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
344 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
345 TopoDS_Vertex aVFirst, aVLast;
346 TopExp::Vertices(anEdge, aVFirst, aVLast);
347 BRep_Builder().UpdateVertex(aVFirst, theTolerance);
350 void GeomAPI_Edge::setLastPointTolerance(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(aVLast, theTolerance);