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_Tool.hxx>
18 #include <Geom_Curve.hxx>
19 #include <Geom_Line.hxx>
20 #include <Geom_Circle.hxx>
21 #include <GeomAdaptor_Curve.hxx>
25 GeomAPI_Edge::GeomAPI_Edge()
30 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
32 if (!theShape->isNull() && theShape->isEdge()) {
33 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
37 bool GeomAPI_Edge::isLine() const
39 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
41 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
42 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
47 bool GeomAPI_Edge::isCircle() 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_Circle)))
54 // Check the difference of first and last parameters to be equal to the curve period
55 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
61 bool GeomAPI_Edge::isArc() 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 is not equal the curve period
69 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
75 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
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);
81 aCurve->D0(aFirst, aPoint);
82 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
85 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
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(aLast, aPoint);
92 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
95 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
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 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
103 gp_Pnt aLoc = aCirc->Location();
104 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
105 gp_Dir anAxis = aCirc->Axis().Direction();
106 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
107 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
110 return std::shared_ptr<GeomAPI_Circ>(); // not circle
113 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line()
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);
119 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
121 gp_Pnt aStartPnt = aLine->Value(aFirst);
122 std::shared_ptr<GeomAPI_Pnt> aStart(
123 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
124 gp_Pnt aEndPnt = aLine->Value(aLast);
125 std::shared_ptr<GeomAPI_Pnt> aEnd(
126 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
127 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
130 return std::shared_ptr<GeomAPI_Lin>(); // not circle
134 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
136 if (!theEdge.get() || ! theEdge->isEdge())
138 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
139 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
141 if (aMyShape.ShapeType() != aInShape.ShapeType())
144 double aMyStart, aMyEnd;
145 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
146 double aInStart, aInEnd;
147 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
149 // Check that curves a the same type
150 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
151 GeomAdaptor_Curve aInAdaptor(aInCurve);
152 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
155 // Check that end point parameters are the same
156 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
159 // Check that end points are equal
160 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
161 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
162 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
163 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
165 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
166 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
172 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
174 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
175 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
178 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
180 double aFirst, aLast;
181 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
182 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
185 thePlane->coefficients(A, B, C, D);
186 gp_Pln aPlane(A, B, C, D);
188 bool inPlane = false;
189 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
190 // check start and end points on the plane
191 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
192 gp_Pnt aLastPnt = aCurve->Value(aLast);
193 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
194 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
195 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
196 // check the center on the plane and normals are collinear
197 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
198 gp_Pnt aCenter = aCirc->Location();
199 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
200 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
201 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
203 // three points checking
204 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
205 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
206 gp_Pnt aLastPnt = aCurve->Value(aLast);
207 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
208 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
209 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();