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 email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include<GeomAPI_Edge.h>
21 #include<GeomAPI_Pln.h>
22 #include<GeomAPI_Pnt.h>
23 #include<GeomAPI_Circ.h>
24 #include<GeomAPI_Dir.h>
25 #include<GeomAPI_Lin.h>
27 #include <BRepAdaptor_Curve.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Edge.hxx>
32 #include <BRep_Builder.hxx>
33 #include <BRep_Tool.hxx>
34 #include <Geom_Curve.hxx>
35 #include <Geom_Line.hxx>
36 #include <Geom_Circle.hxx>
37 #include <GeomAdaptor_Curve.hxx>
41 #include <GCPnts_AbscissaPoint.hxx>
43 GeomAPI_Edge::GeomAPI_Edge()
45 TopoDS_Edge* anEdge = new TopoDS_Edge;
47 BRep_Builder aBuilder;
48 aBuilder.MakeEdge(*anEdge);
53 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
55 if (!theShape->isNull() && theShape->isEdge()) {
56 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
60 bool GeomAPI_Edge::isLine() const
62 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
64 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
65 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
70 bool GeomAPI_Edge::isCircle() const
72 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
74 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
75 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
77 // Check the difference of first and last parameters to be equal to the curve period
78 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
84 bool GeomAPI_Edge::isArc() const
86 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
88 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
89 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
91 // Check the difference of first and last parameters is not equal the curve period
92 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
98 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
100 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
101 double aFirst, aLast;
102 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
104 aCurve->D0(aFirst, aPoint);
105 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
108 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
110 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
111 double aFirst, aLast;
112 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
114 aCurve->D0(aLast, aPoint);
115 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
118 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle()
120 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
121 double aFirst, aLast;
122 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
124 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
126 gp_Pnt aLoc = aCirc->Location();
127 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
128 gp_Dir anAxis = aCirc->Axis().Direction();
129 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
130 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
133 return std::shared_ptr<GeomAPI_Circ>(); // not circle
136 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line()
138 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
139 double aFirst, aLast;
140 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
142 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
144 gp_Pnt aStartPnt = aLine->Value(aFirst);
145 std::shared_ptr<GeomAPI_Pnt> aStart(
146 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
147 gp_Pnt aEndPnt = aLine->Value(aLast);
148 std::shared_ptr<GeomAPI_Pnt> aEnd(
149 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
150 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
153 return std::shared_ptr<GeomAPI_Lin>(); // not circle
157 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
159 if (!theEdge.get() || ! theEdge->isEdge())
161 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
162 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
164 if (aMyShape.IsNull() || aInShape.IsNull())
167 if (aMyShape.ShapeType() != aInShape.ShapeType())
170 double aMyStart, aMyEnd;
171 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
172 double aInStart, aInEnd;
173 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
175 // Check that curves a the same type
176 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
177 GeomAdaptor_Curve aInAdaptor(aInCurve);
178 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
181 // Check that end point parameters are the same
182 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
185 // Check that end points are equal
186 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
187 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
188 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
189 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
191 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
192 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
198 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
200 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
201 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
204 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
206 double aFirst, aLast;
207 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
208 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
213 thePlane->coefficients(A, B, C, D);
214 gp_Pln aPlane(A, B, C, D);
216 bool inPlane = false;
217 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
218 // check start and end points on the plane
219 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
220 gp_Pnt aLastPnt = aCurve->Value(aLast);
221 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
222 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
223 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
224 // check the center on the plane and normals are collinear
225 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
226 gp_Pnt aCenter = aCirc->Location();
227 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
228 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
229 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
231 // three points checking
232 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
233 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
234 gp_Pnt aLastPnt = aCurve->Value(aLast);
235 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
236 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
237 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
242 double GeomAPI_Edge::length() const
244 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
245 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
246 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
247 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);