1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include<GeomAPI_Edge.h>
22 #include<GeomAPI_Pln.h>
23 #include<GeomAPI_Pnt.h>
24 #include<GeomAPI_Circ.h>
25 #include<GeomAPI_Dir.h>
26 #include<GeomAPI_Lin.h>
28 #include <BRepAdaptor_Curve.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <TopoDS_Edge.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRep_Tool.hxx>
35 #include <Geom_Curve.hxx>
36 #include <Geom_Line.hxx>
37 #include <Geom_Circle.hxx>
38 #include <GeomAdaptor_Curve.hxx>
42 #include <GCPnts_AbscissaPoint.hxx>
44 GeomAPI_Edge::GeomAPI_Edge()
46 TopoDS_Edge* anEdge = new TopoDS_Edge;
48 BRep_Builder aBuilder;
49 aBuilder.MakeEdge(*anEdge);
54 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
56 if (!theShape->isNull() && theShape->isEdge()) {
57 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
61 bool GeomAPI_Edge::isLine() 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_Line)))
71 bool GeomAPI_Edge::isCircle() 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 to be equal to the curve period
79 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
85 bool GeomAPI_Edge::isArc() const
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);
90 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
92 // Check the difference of first and last parameters is not equal the curve period
93 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
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()
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_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
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_Lin> GeomAPI_Edge::line()
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);
143 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
145 gp_Pnt aStartPnt = aLine->Value(aFirst);
146 std::shared_ptr<GeomAPI_Pnt> aStart(
147 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
148 gp_Pnt aEndPnt = aLine->Value(aLast);
149 std::shared_ptr<GeomAPI_Pnt> aEnd(
150 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
151 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
154 return std::shared_ptr<GeomAPI_Lin>(); // not circle
158 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
160 if (!theEdge.get() || ! theEdge->isEdge())
162 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
163 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
165 if (aMyShape.IsNull() || aInShape.IsNull())
168 if (aMyShape.ShapeType() != aInShape.ShapeType())
171 double aMyStart, aMyEnd;
172 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
173 double aInStart, aInEnd;
174 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
176 // Check that curves a the same type
177 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
178 GeomAdaptor_Curve aInAdaptor(aInCurve);
179 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
182 // Check that end point parameters are the same
183 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
186 // Check that end points are equal
187 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
188 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
189 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
190 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
192 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
193 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
199 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
201 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
202 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
205 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
207 double aFirst, aLast;
208 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
209 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
214 thePlane->coefficients(A, B, C, D);
215 gp_Pln aPlane(A, B, C, D);
217 bool inPlane = false;
218 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
219 // check start and end points on the plane
220 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
221 gp_Pnt aLastPnt = aCurve->Value(aLast);
222 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
223 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
224 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
225 // check the center on the plane and normals are collinear
226 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
227 gp_Pnt aCenter = aCirc->Location();
228 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
229 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
230 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
232 // three points checking
233 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
234 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
235 gp_Pnt aLastPnt = aCurve->Value(aLast);
236 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
237 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
238 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
243 double GeomAPI_Edge::length() const
245 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
246 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
247 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
248 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
251 bool GeomAPI_Edge::isClosed() const
253 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
256 double aFirst, aLast;
257 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
258 if (aCurve.IsNull() || !aCurve->IsPeriodic())
260 aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
262 return fabs(aFirst - aLast) < 1.e-9;
265 bool GeomAPI_Edge::isDegenerated() const
267 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
268 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
270 return BRep_Tool::Degenerated(TopoDS::Edge(aShape));