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 <TopoDS_Shape.hxx>
15 #include <TopoDS_Edge.hxx>
17 #include <BRep_Builder.hxx>
18 #include <BRep_Tool.hxx>
19 #include <Geom_Curve.hxx>
20 #include <Geom_Line.hxx>
21 #include <Geom_Circle.hxx>
22 #include <GeomAdaptor_Curve.hxx>
26 GeomAPI_Edge::GeomAPI_Edge()
28 TopoDS_Edge* anEdge = new TopoDS_Edge;
30 BRep_Builder aBuilder;
31 aBuilder.MakeEdge(*anEdge);
36 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
38 if (!theShape->isNull() && theShape->isEdge()) {
39 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
43 bool GeomAPI_Edge::isLine() const
45 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
47 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
48 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
53 bool GeomAPI_Edge::isCircle() const
55 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
57 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
58 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
60 // Check the difference of first and last parameters to be equal to the curve period
61 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
67 bool GeomAPI_Edge::isArc() const
69 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
71 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
72 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
74 // Check the difference of first and last parameters is not equal the curve period
75 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
81 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
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);
87 aCurve->D0(aFirst, aPoint);
88 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
91 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
93 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
95 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
97 aCurve->D0(aLast, aPoint);
98 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
101 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
103 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
104 double aFirst, aLast;
105 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
107 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
109 gp_Pnt aLoc = aCirc->Location();
110 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
111 gp_Dir anAxis = aCirc->Axis().Direction();
112 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
113 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
116 return std::shared_ptr<GeomAPI_Circ>(); // not circle
119 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line()
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);
125 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
127 gp_Pnt aStartPnt = aLine->Value(aFirst);
128 std::shared_ptr<GeomAPI_Pnt> aStart(
129 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
130 gp_Pnt aEndPnt = aLine->Value(aLast);
131 std::shared_ptr<GeomAPI_Pnt> aEnd(
132 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
133 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
136 return std::shared_ptr<GeomAPI_Lin>(); // not circle
140 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
142 if (!theEdge.get() || ! theEdge->isEdge())
144 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
145 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
147 if (aMyShape.IsNull() || aInShape.IsNull())
150 if (aMyShape.ShapeType() != aInShape.ShapeType())
153 double aMyStart, aMyEnd;
154 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
155 double aInStart, aInEnd;
156 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
158 // Check that curves a the same type
159 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
160 GeomAdaptor_Curve aInAdaptor(aInCurve);
161 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
164 // Check that end point parameters are the same
165 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
168 // Check that end points are equal
169 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
170 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
171 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
172 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
174 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
175 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
181 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
183 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
184 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
187 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
189 double aFirst, aLast;
190 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
191 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
194 thePlane->coefficients(A, B, C, D);
195 gp_Pln aPlane(A, B, C, D);
197 bool inPlane = false;
198 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
199 // check start and end points on the plane
200 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
201 gp_Pnt aLastPnt = aCurve->Value(aLast);
202 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
203 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
204 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
205 // check the center on the plane and normals are collinear
206 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
207 gp_Pnt aCenter = aCirc->Location();
208 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
209 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
210 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
212 // three points checking
213 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
214 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
215 gp_Pnt aLastPnt = aCurve->Value(aLast);
216 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
217 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
218 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();