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->IsKind(STANDARD_TYPE(Geom_Line)))
75 bool GeomAPI_Edge::isCircle() const
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);
80 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
82 // Check the difference of first and last parameters to be equal to the curve period
83 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
89 bool GeomAPI_Edge::isArc() const
91 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
93 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
94 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
96 // Check the difference of first and last parameters is not equal the curve period
97 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
103 bool GeomAPI_Edge::isEllipse() const
105 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
106 double aFirst, aLast;
107 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
108 if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
113 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
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 aCurve->D0(aFirst, aPoint);
120 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
123 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
125 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
126 double aFirst, aLast;
127 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
129 aCurve->D0(aLast, aPoint);
130 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
133 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
135 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
136 double aFirst, aLast;
137 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
138 if (!aCurve.IsNull()) {
139 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
140 if (!aCirc.IsNull()) {
141 gp_Pnt aLoc = aCirc->Location();
142 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
143 gp_Dir anAxis = aCirc->Axis().Direction();
144 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
145 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
148 return std::shared_ptr<GeomAPI_Circ>(); // not circle
151 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
153 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
154 double aFirst, aLast;
155 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
156 if (!aCurve.IsNull()) {
157 Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
158 if (!aElips.IsNull()) {
159 gp_Elips aGpElips = aElips->Elips();
160 std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
161 aEllipse->setImpl(new gp_Elips(aGpElips));
165 return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
168 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
170 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
171 double aFirst, aLast;
172 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
174 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
176 gp_Pnt aStartPnt = aLine->Value(aFirst);
177 std::shared_ptr<GeomAPI_Pnt> aStart(
178 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
179 gp_Pnt aEndPnt = aLine->Value(aLast);
180 std::shared_ptr<GeomAPI_Pnt> aEnd(
181 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
182 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
185 return std::shared_ptr<GeomAPI_Lin>(); // not circle
189 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
191 if (!theEdge.get() || ! theEdge->isEdge())
193 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
194 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
196 if (aMyShape.IsNull() || aInShape.IsNull())
199 if (aMyShape.ShapeType() != aInShape.ShapeType())
202 double aMyStart, aMyEnd;
203 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
204 double aInStart, aInEnd;
205 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
207 // Check that curves a the same type
208 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
209 GeomAdaptor_Curve aInAdaptor(aInCurve);
210 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
213 // Check that end point parameters are the same
214 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
217 // Check that end points are equal
218 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
219 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
220 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
221 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
223 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
224 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
230 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
232 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
233 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
236 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
238 double aFirst, aLast;
239 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
240 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
245 thePlane->coefficients(A, B, C, D);
246 gp_Pln aPlane(A, B, C, D);
248 bool inPlane = false;
249 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
250 // check start and end points on the plane
251 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
252 gp_Pnt aLastPnt = aCurve->Value(aLast);
253 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
254 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
255 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
256 // check the center on the plane and normals are collinear
257 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
258 gp_Pnt aCenter = aCirc->Location();
259 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
260 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
261 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
263 // three points checking
264 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
265 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
266 gp_Pnt aLastPnt = aCurve->Value(aLast);
267 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
268 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
269 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
274 double GeomAPI_Edge::length() const
276 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
277 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
278 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
279 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);