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.IsNull() || aInShape.IsNull())
144 if (aMyShape.ShapeType() != aInShape.ShapeType())
147 double aMyStart, aMyEnd;
148 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
149 double aInStart, aInEnd;
150 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
152 // Check that curves a the same type
153 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
154 GeomAdaptor_Curve aInAdaptor(aInCurve);
155 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
158 // Check that end point parameters are the same
159 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
162 // Check that end points are equal
163 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
164 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
165 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
166 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
168 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
169 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
175 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
177 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
178 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
181 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
183 double aFirst, aLast;
184 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
185 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
188 thePlane->coefficients(A, B, C, D);
189 gp_Pln aPlane(A, B, C, D);
191 bool inPlane = false;
192 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
193 // check start and end points on the plane
194 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
195 gp_Pnt aLastPnt = aCurve->Value(aLast);
196 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
197 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
198 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
199 // check the center on the plane and normals are collinear
200 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
201 gp_Pnt aCenter = aCirc->Location();
202 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
203 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
204 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
206 // three points checking
207 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
208 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
209 gp_Pnt aLastPnt = aCurve->Value(aLast);
210 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
211 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
212 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();