1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAPI_Edge.cpp
4 // Created: 24 Jul 2014
5 // Author: Artem ZHIDKOV
7 #include<GeomAPI_Edge.h>
8 #include<GeomAPI_Pln.h>
9 #include<GeomAPI_Pnt.h>
10 #include<GeomAPI_Circ.h>
11 #include<GeomAPI_Dir.h>
12 #include<GeomAPI_Lin.h>
13 #include<GeomAPI_Ax2.h>
14 #include<GeomAPI_Ellipse.h>
16 #include <BRepAdaptor_Curve.hxx>
18 #include <TopoDS_Shape.hxx>
19 #include <TopoDS_Edge.hxx>
21 #include <BRep_Builder.hxx>
22 #include <BRep_Tool.hxx>
23 #include <Geom_Curve.hxx>
24 #include <Geom_Line.hxx>
25 #include <Geom_Circle.hxx>
26 #include <Geom_Ellipse.hxx>
27 #include <GeomAdaptor_Curve.hxx>
30 #include <gp_Elips.hxx>
32 #include <GCPnts_AbscissaPoint.hxx>
34 GeomAPI_Edge::GeomAPI_Edge()
36 TopoDS_Edge* anEdge = new TopoDS_Edge;
38 BRep_Builder aBuilder;
39 aBuilder.MakeEdge(*anEdge);
44 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
46 if (!theShape->isNull() && theShape->isEdge()) {
47 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
51 bool GeomAPI_Edge::isLine() const
53 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
55 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
56 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
61 bool GeomAPI_Edge::isCircle() const
63 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
65 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
66 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
68 // Check the difference of first and last parameters to be equal to the curve period
69 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
75 bool GeomAPI_Edge::isArc() const
77 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
79 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
80 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
82 // Check the difference of first and last parameters is not equal the curve period
83 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
89 bool GeomAPI_Edge::isEllipse() const
91 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
93 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
94 if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
99 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
101 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
102 double aFirst, aLast;
103 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
105 aCurve->D0(aFirst, aPoint);
106 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
109 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
111 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
112 double aFirst, aLast;
113 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
115 aCurve->D0(aLast, aPoint);
116 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
119 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
121 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
122 double aFirst, aLast;
123 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
124 if (!aCurve.IsNull()) {
125 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
126 if (!aCirc.IsNull()) {
127 gp_Pnt aLoc = aCirc->Location();
128 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
129 gp_Dir anAxis = aCirc->Axis().Direction();
130 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
131 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
134 return std::shared_ptr<GeomAPI_Circ>(); // not circle
137 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
139 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
140 double aFirst, aLast;
141 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
142 if (!aCurve.IsNull()) {
143 Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
144 if (!aElips.IsNull()) {
145 gp_Elips aGpElips = aElips->Elips();
146 std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
147 aEllipse->setImpl(new gp_Elips(aGpElips));
151 return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
154 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
156 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
157 double aFirst, aLast;
158 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
160 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
162 gp_Pnt aStartPnt = aLine->Value(aFirst);
163 std::shared_ptr<GeomAPI_Pnt> aStart(
164 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
165 gp_Pnt aEndPnt = aLine->Value(aLast);
166 std::shared_ptr<GeomAPI_Pnt> aEnd(
167 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
168 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
171 return std::shared_ptr<GeomAPI_Lin>(); // not circle
175 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
177 if (!theEdge.get() || ! theEdge->isEdge())
179 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
180 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
182 if (aMyShape.IsNull() || aInShape.IsNull())
185 if (aMyShape.ShapeType() != aInShape.ShapeType())
188 double aMyStart, aMyEnd;
189 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
190 double aInStart, aInEnd;
191 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
193 // Check that curves a the same type
194 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
195 GeomAdaptor_Curve aInAdaptor(aInCurve);
196 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
199 // Check that end point parameters are the same
200 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
203 // Check that end points are equal
204 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
205 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
206 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
207 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
209 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
210 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
216 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
218 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
219 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
222 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
224 double aFirst, aLast;
225 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
226 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
231 thePlane->coefficients(A, B, C, D);
232 gp_Pln aPlane(A, B, C, D);
234 bool inPlane = false;
235 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
236 // check start and end points on the plane
237 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
238 gp_Pnt aLastPnt = aCurve->Value(aLast);
239 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
240 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
241 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
242 // check the center on the plane and normals are collinear
243 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
244 gp_Pnt aCenter = aCirc->Location();
245 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
246 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
247 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
249 // three points checking
250 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
251 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
252 gp_Pnt aLastPnt = aCurve->Value(aLast);
253 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
254 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
255 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
260 double GeomAPI_Edge::length() const
262 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
263 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
264 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
265 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);