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>
14 #include <BRepAdaptor_Curve.hxx>
16 #include <TopoDS_Shape.hxx>
17 #include <TopoDS_Edge.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRep_Tool.hxx>
21 #include <Geom_Curve.hxx>
22 #include <Geom_Line.hxx>
23 #include <Geom_Circle.hxx>
24 #include <GeomAdaptor_Curve.hxx>
28 #include <GCPnts_AbscissaPoint.hxx>
30 GeomAPI_Edge::GeomAPI_Edge()
32 TopoDS_Edge* anEdge = new TopoDS_Edge;
34 BRep_Builder aBuilder;
35 aBuilder.MakeEdge(*anEdge);
40 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
42 if (!theShape->isNull() && theShape->isEdge()) {
43 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
47 bool GeomAPI_Edge::isLine() const
49 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
51 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
52 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
57 bool GeomAPI_Edge::isCircle() const
59 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
61 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
62 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
64 // Check the difference of first and last parameters to be equal to the curve period
65 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
71 bool GeomAPI_Edge::isArc() 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->IsKind(STANDARD_TYPE(Geom_Circle)))
78 // Check the difference of first and last parameters is not equal the curve period
79 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
85 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
87 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
89 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
91 aCurve->D0(aFirst, aPoint);
92 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
95 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
97 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
99 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
101 aCurve->D0(aLast, aPoint);
102 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
105 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
107 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
108 double aFirst, aLast;
109 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
111 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
113 gp_Pnt aLoc = aCirc->Location();
114 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
115 gp_Dir anAxis = aCirc->Axis().Direction();
116 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
117 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
120 return std::shared_ptr<GeomAPI_Circ>(); // not circle
123 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line()
125 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
126 double aFirst, aLast;
127 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
129 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
131 gp_Pnt aStartPnt = aLine->Value(aFirst);
132 std::shared_ptr<GeomAPI_Pnt> aStart(
133 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
134 gp_Pnt aEndPnt = aLine->Value(aLast);
135 std::shared_ptr<GeomAPI_Pnt> aEnd(
136 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
137 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
140 return std::shared_ptr<GeomAPI_Lin>(); // not circle
144 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
146 if (!theEdge.get() || ! theEdge->isEdge())
148 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
149 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
151 if (aMyShape.IsNull() || aInShape.IsNull())
154 if (aMyShape.ShapeType() != aInShape.ShapeType())
157 double aMyStart, aMyEnd;
158 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
159 double aInStart, aInEnd;
160 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
162 // Check that curves a the same type
163 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
164 GeomAdaptor_Curve aInAdaptor(aInCurve);
165 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
168 // Check that end point parameters are the same
169 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
172 // Check that end points are equal
173 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
174 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
175 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
176 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
178 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
179 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
185 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
187 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
188 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
191 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
193 double aFirst, aLast;
194 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
195 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
200 thePlane->coefficients(A, B, C, D);
201 gp_Pln aPlane(A, B, C, D);
203 bool inPlane = false;
204 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
205 // check start and end points on the plane
206 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
207 gp_Pnt aLastPnt = aCurve->Value(aLast);
208 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
209 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
210 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
211 // check the center on the plane and normals are collinear
212 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
213 gp_Pnt aCenter = aCirc->Location();
214 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
215 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
216 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
218 // three points checking
219 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
220 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
221 gp_Pnt aLastPnt = aCurve->Value(aLast);
222 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
223 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
224 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
229 double GeomAPI_Edge::length() const
231 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
232 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
233 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
234 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);