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 <Geom_Curve.hxx>
39 #include <Geom_Line.hxx>
40 #include <Geom_Circle.hxx>
41 #include <Geom_Ellipse.hxx>
42 #include <Geom_Plane.hxx>
43 #include <GeomAPI_IntCS.hxx>
44 #include <GeomAdaptor_Curve.hxx>
47 #include <gp_Elips.hxx>
50 #include <GCPnts_AbscissaPoint.hxx>
52 GeomAPI_Edge::GeomAPI_Edge()
54 TopoDS_Edge* anEdge = new TopoDS_Edge;
56 BRep_Builder aBuilder;
57 aBuilder.MakeEdge(*anEdge);
62 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
64 if (!theShape->isNull() && theShape->isEdge()) {
65 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
69 bool GeomAPI_Edge::isLine() const
71 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
73 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
74 if (aCurve.IsNull()) // degenerative edge
76 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
81 bool GeomAPI_Edge::isCircle() const
83 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
85 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
86 if (aCurve.IsNull()) // degenerative edge
88 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
90 // Check the difference of first and last parameters to be equal to the curve period
91 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
97 bool GeomAPI_Edge::isArc() const
99 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
100 double aFirst, aLast;
101 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
102 if (aCurve.IsNull()) // degenerative edge
104 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
106 // Check the difference of first and last parameters is not equal the curve period
107 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
113 bool GeomAPI_Edge::isEllipse() 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 (aCurve.IsNull()) // degenerative edge
120 if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
125 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
127 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
128 double aFirst, aLast;
129 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
131 aCurve->D0(aFirst, aPoint);
132 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
135 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
137 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
138 double aFirst, aLast;
139 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
141 aCurve->D0(aLast, aPoint);
142 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
145 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() 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 (!aCurve.IsNull()) {
151 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
152 if (!aCirc.IsNull()) {
153 gp_Pnt aLoc = aCirc->Location();
154 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
155 gp_Dir anAxis = aCirc->Axis().Direction();
156 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
157 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
160 return std::shared_ptr<GeomAPI_Circ>(); // not circle
163 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
165 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
166 double aFirst, aLast;
167 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
168 if (!aCurve.IsNull()) {
169 Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
170 if (!aElips.IsNull()) {
171 gp_Elips aGpElips = aElips->Elips();
172 std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
173 aEllipse->setImpl(new gp_Elips(aGpElips));
177 return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
180 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
182 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
183 double aFirst, aLast;
184 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
186 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
188 gp_Pnt aStartPnt = aLine->Value(aFirst);
189 std::shared_ptr<GeomAPI_Pnt> aStart(
190 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
191 gp_Pnt aEndPnt = aLine->Value(aLast);
192 std::shared_ptr<GeomAPI_Pnt> aEnd(
193 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
194 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
197 return std::shared_ptr<GeomAPI_Lin>(); // not circle
201 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
203 if (!theEdge.get() || ! theEdge->isEdge())
205 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
206 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
208 if (aMyShape.IsNull() || aInShape.IsNull())
211 if (aMyShape.ShapeType() != aInShape.ShapeType())
214 double aMyStart, aMyEnd;
215 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
216 double aInStart, aInEnd;
217 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
219 // Check that curves a the same type
220 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
221 GeomAdaptor_Curve aInAdaptor(aInCurve);
222 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
225 // Check that end point parameters are the same
226 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
229 // Check that end points are equal
230 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
231 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
232 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
233 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
235 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
236 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
242 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
244 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
245 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
248 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
250 double aFirst, aLast;
251 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
252 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
257 thePlane->coefficients(A, B, C, D);
258 gp_Pln aPlane(A, B, C, D);
260 bool inPlane = false;
261 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
262 // check start and end points on the plane
263 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
264 gp_Pnt aLastPnt = aCurve->Value(aLast);
265 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
266 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
267 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
268 // check the center on the plane and normals are collinear
269 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
270 gp_Pnt aCenter = aCirc->Location();
271 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
272 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
273 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
275 // three points checking
276 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
277 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
278 gp_Pnt aLastPnt = aCurve->Value(aLast);
279 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
280 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
281 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
286 void GeomAPI_Edge::intersectWithPlane(const std::shared_ptr<GeomAPI_Pln> thePlane,
287 std::list<std::shared_ptr<GeomAPI_Pnt>>& theResult) const
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 // check the parameter of intersection in the edge range
304 aIntersect.Parameters(i, A, B, C);
305 if (aCurve->IsPeriodic())
306 C = ElCLib::InPeriod(C, aFirst, aFirst + aCurve->Period());
307 if (C < aFirst - Precision::PConfusion() || C > aLast + Precision::PConfusion())
310 // obtain intersection point
311 aPnt = aIntersect.Point(i);
312 std::shared_ptr<GeomAPI_Pnt> aPntPtr(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
313 theResult.push_back(aPntPtr);
319 double GeomAPI_Edge::length() const
321 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
322 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
323 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
324 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
327 bool GeomAPI_Edge::isClosed() const
329 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
332 double aFirst, aLast;
333 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
334 if (aCurve.IsNull() || !aCurve->IsPeriodic())
336 aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
338 return fabs(aFirst - aLast) < 1.e-9;
341 bool GeomAPI_Edge::isDegenerated() const
343 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
344 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
346 return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
349 void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
351 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
352 TopoDS_Vertex aVFirst, aVLast;
353 TopExp::Vertices(anEdge, aVFirst, aVLast);
354 BRep_Builder().UpdateVertex(aVFirst, theTolerance);
357 void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
359 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
360 TopoDS_Vertex aVFirst, aVLast;
361 TopExp::Vertices(anEdge, aVFirst, aVLast);
362 BRep_Builder().UpdateVertex(aVLast, theTolerance);