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>
27 #include<GeomAPI_Ax2.h>
28 #include<GeomAPI_Ellipse.h>
30 #include <BRepAdaptor_Curve.hxx>
32 #include <TopoDS_Shape.hxx>
33 #include <TopoDS_Edge.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRep_Tool.hxx>
37 #include <Geom_Curve.hxx>
38 #include <Geom_Line.hxx>
39 #include <Geom_Circle.hxx>
40 #include <Geom_Ellipse.hxx>
41 #include <GeomAdaptor_Curve.hxx>
44 #include <gp_Elips.hxx>
46 #include <GCPnts_AbscissaPoint.hxx>
48 GeomAPI_Edge::GeomAPI_Edge()
50 TopoDS_Edge* anEdge = new TopoDS_Edge;
52 BRep_Builder aBuilder;
53 aBuilder.MakeEdge(*anEdge);
58 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
60 if (!theShape->isNull() && theShape->isEdge()) {
61 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
65 bool GeomAPI_Edge::isLine() const
67 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
69 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
70 if (aCurve.IsNull()) // degenerative edge
72 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
77 bool GeomAPI_Edge::isCircle() const
79 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
81 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
82 if (aCurve.IsNull()) // degenerative edge
84 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
86 // Check the difference of first and last parameters to be equal to the curve period
87 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
93 bool GeomAPI_Edge::isArc() const
95 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
97 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
98 if (aCurve.IsNull()) // degenerative edge
100 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
102 // Check the difference of first and last parameters is not equal the curve period
103 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
109 bool GeomAPI_Edge::isEllipse() const
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);
114 if (aCurve.IsNull()) // degenerative edge
116 if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
121 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
123 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
124 double aFirst, aLast;
125 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
127 aCurve->D0(aFirst, aPoint);
128 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
131 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
133 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
134 double aFirst, aLast;
135 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
137 aCurve->D0(aLast, aPoint);
138 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
141 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
143 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
144 double aFirst, aLast;
145 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
146 if (!aCurve.IsNull()) {
147 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
148 if (!aCirc.IsNull()) {
149 gp_Pnt aLoc = aCirc->Location();
150 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
151 gp_Dir anAxis = aCirc->Axis().Direction();
152 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
153 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
156 return std::shared_ptr<GeomAPI_Circ>(); // not circle
159 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
161 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
162 double aFirst, aLast;
163 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
164 if (!aCurve.IsNull()) {
165 Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
166 if (!aElips.IsNull()) {
167 gp_Elips aGpElips = aElips->Elips();
168 std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
169 aEllipse->setImpl(new gp_Elips(aGpElips));
173 return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
176 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
178 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
179 double aFirst, aLast;
180 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
182 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
184 gp_Pnt aStartPnt = aLine->Value(aFirst);
185 std::shared_ptr<GeomAPI_Pnt> aStart(
186 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
187 gp_Pnt aEndPnt = aLine->Value(aLast);
188 std::shared_ptr<GeomAPI_Pnt> aEnd(
189 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
190 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
193 return std::shared_ptr<GeomAPI_Lin>(); // not circle
197 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
199 if (!theEdge.get() || ! theEdge->isEdge())
201 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
202 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
204 if (aMyShape.IsNull() || aInShape.IsNull())
207 if (aMyShape.ShapeType() != aInShape.ShapeType())
210 double aMyStart, aMyEnd;
211 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
212 double aInStart, aInEnd;
213 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
215 // Check that curves a the same type
216 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
217 GeomAdaptor_Curve aInAdaptor(aInCurve);
218 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
221 // Check that end point parameters are the same
222 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
225 // Check that end points are equal
226 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
227 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
228 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
229 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
231 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
232 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
238 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
240 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
241 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
244 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
246 double aFirst, aLast;
247 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
248 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
253 thePlane->coefficients(A, B, C, D);
254 gp_Pln aPlane(A, B, C, D);
256 bool inPlane = false;
257 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
258 // check start and end points on the plane
259 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
260 gp_Pnt aLastPnt = aCurve->Value(aLast);
261 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
262 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
263 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
264 // check the center on the plane and normals are collinear
265 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
266 gp_Pnt aCenter = aCirc->Location();
267 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
268 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
269 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
271 // three points checking
272 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
273 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
274 gp_Pnt aLastPnt = aCurve->Value(aLast);
275 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
276 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
277 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
282 double GeomAPI_Edge::length() const
284 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
285 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
286 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
287 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);