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>
47 #include <GCPnts_AbscissaPoint.hxx>
49 GeomAPI_Edge::GeomAPI_Edge()
51 TopoDS_Edge* anEdge = new TopoDS_Edge;
53 BRep_Builder aBuilder;
54 aBuilder.MakeEdge(*anEdge);
59 GeomAPI_Edge::GeomAPI_Edge(const std::shared_ptr<GeomAPI_Shape>& theShape)
61 if (!theShape->isNull() && theShape->isEdge()) {
62 setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
66 bool GeomAPI_Edge::isLine() const
68 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
70 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
71 if (aCurve.IsNull()) // degenerative edge
73 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line)))
78 bool GeomAPI_Edge::isCircle() const
80 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
82 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
83 if (aCurve.IsNull()) // degenerative edge
85 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
87 // Check the difference of first and last parameters to be equal to the curve period
88 if (Abs(aLast - aFirst - aCurve->Period()) < Precision::PConfusion())
94 bool GeomAPI_Edge::isArc() const
96 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
98 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
99 if (aCurve.IsNull()) // degenerative edge
101 if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle)))
103 // Check the difference of first and last parameters is not equal the curve period
104 if (Abs(aLast - aFirst - aCurve->Period()) >= Precision::PConfusion())
110 bool GeomAPI_Edge::isEllipse() const
112 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
113 double aFirst, aLast;
114 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
115 if (aCurve.IsNull()) // degenerative edge
117 if (aCurve->IsKind(STANDARD_TYPE(Geom_Ellipse)))
122 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::firstPoint()
124 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
125 double aFirst, aLast;
126 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
128 aCurve->D0(aFirst, aPoint);
129 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
132 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Edge::lastPoint()
134 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
135 double aFirst, aLast;
136 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
138 aCurve->D0(aLast, aPoint);
139 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
142 std::shared_ptr<GeomAPI_Circ> GeomAPI_Edge::circle() const
144 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
145 double aFirst, aLast;
146 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
147 if (!aCurve.IsNull()) {
148 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
149 if (!aCirc.IsNull()) {
150 gp_Pnt aLoc = aCirc->Location();
151 std::shared_ptr<GeomAPI_Pnt> aCenter(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
152 gp_Dir anAxis = aCirc->Axis().Direction();
153 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(anAxis.X(), anAxis.Y(), anAxis.Z()));
154 return std::shared_ptr<GeomAPI_Circ>(new GeomAPI_Circ(aCenter, aDir, aCirc->Radius()));
157 return std::shared_ptr<GeomAPI_Circ>(); // not circle
160 std::shared_ptr<GeomAPI_Ellipse> GeomAPI_Edge::ellipse() const
162 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
163 double aFirst, aLast;
164 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
165 if (!aCurve.IsNull()) {
166 Handle(Geom_Ellipse) aElips = Handle(Geom_Ellipse)::DownCast(aCurve);
167 if (!aElips.IsNull()) {
168 gp_Elips aGpElips = aElips->Elips();
169 std::shared_ptr<GeomAPI_Ellipse> aEllipse(new GeomAPI_Ellipse());
170 aEllipse->setImpl(new gp_Elips(aGpElips));
174 return std::shared_ptr<GeomAPI_Ellipse>(); // not elipse
177 std::shared_ptr<GeomAPI_Lin> GeomAPI_Edge::line() const
179 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
180 double aFirst, aLast;
181 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
183 Handle(Geom_Line) aLine = Handle(Geom_Line)::DownCast(aCurve);
185 gp_Pnt aStartPnt = aLine->Value(aFirst);
186 std::shared_ptr<GeomAPI_Pnt> aStart(
187 new GeomAPI_Pnt(aStartPnt.X(), aStartPnt.Y(), aStartPnt.Z()));
188 gp_Pnt aEndPnt = aLine->Value(aLast);
189 std::shared_ptr<GeomAPI_Pnt> aEnd(
190 new GeomAPI_Pnt(aEndPnt.X(), aEndPnt.Y(), aEndPnt.Z()));
191 return std::shared_ptr<GeomAPI_Lin>(new GeomAPI_Lin(aStart, aEnd));
194 return std::shared_ptr<GeomAPI_Lin>(); // not circle
198 bool GeomAPI_Edge::isEqual(const std::shared_ptr<GeomAPI_Shape> theEdge) const
200 if (!theEdge.get() || ! theEdge->isEdge())
202 const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
203 const TopoDS_Shape& aInShape = theEdge->impl<TopoDS_Shape>();
205 if (aMyShape.IsNull() || aInShape.IsNull())
208 if (aMyShape.ShapeType() != aInShape.ShapeType())
211 double aMyStart, aMyEnd;
212 Handle(Geom_Curve) aMyCurve = BRep_Tool::Curve(TopoDS::Edge(aMyShape), aMyStart, aMyEnd);
213 double aInStart, aInEnd;
214 Handle(Geom_Curve) aInCurve = BRep_Tool::Curve(TopoDS::Edge(aInShape), aInStart, aInEnd);
216 // Check that curves a the same type
217 GeomAdaptor_Curve aMyAdaptor(aMyCurve);
218 GeomAdaptor_Curve aInAdaptor(aInCurve);
219 if (aMyAdaptor.GetType() != aInAdaptor.GetType())
222 // Check that end point parameters are the same
223 if ((aMyStart != aInStart) || (aMyEnd != aInEnd))
226 // Check that end points are equal
227 gp_Pnt aMyPnt1 = aMyAdaptor.Value(aMyStart);
228 gp_Pnt aMyPnt2 = aMyAdaptor.Value(aMyEnd);
229 gp_Pnt aInPnt1 = aInAdaptor.Value(aInStart);
230 gp_Pnt aInPnt2 = aInAdaptor.Value(aInEnd);
232 if ((!aMyPnt1.IsEqual(aInPnt1, Precision::Confusion())) ||
233 (!aMyPnt2.IsEqual(aInPnt2, Precision::Confusion())))
239 void GeomAPI_Edge::getRange(double& theFirst, double& theLast) const
241 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
242 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, theFirst, theLast);
245 bool GeomAPI_Edge::isInPlane(std::shared_ptr<GeomAPI_Pln> thePlane) const
247 double aFirst, aLast;
248 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
249 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
254 thePlane->coefficients(A, B, C, D);
255 gp_Pln aPlane(A, B, C, D);
257 bool inPlane = false;
258 if (aCurve->IsKind(STANDARD_TYPE(Geom_Line))) {
259 // check start and end points on the plane
260 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
261 gp_Pnt aLastPnt = aCurve->Value(aLast);
262 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
263 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
264 } else if (aCurve->IsKind(STANDARD_TYPE(Geom_Circle))) {
265 // check the center on the plane and normals are collinear
266 Handle(Geom_Circle) aCirc = Handle(Geom_Circle)::DownCast(aCurve);
267 gp_Pnt aCenter = aCirc->Location();
268 Standard_Real aDot = aPlane.Axis().Direction().Dot(aCirc->Axis().Direction());
269 inPlane = aPlane.SquareDistance(aCenter) < Precision::SquareConfusion() &&
270 Abs(Abs(aDot) - 1.0) < Precision::Confusion();
272 // three points checking
273 gp_Pnt aFirstPnt = aCurve->Value(aFirst);
274 gp_Pnt aMidPnt = aCurve->Value((aFirst + aLast) / 2.);
275 gp_Pnt aLastPnt = aCurve->Value(aLast);
276 inPlane = aPlane.SquareDistance(aFirstPnt) < Precision::SquareConfusion() &&
277 aPlane.SquareDistance(aMidPnt) < Precision::SquareConfusion() &&
278 aPlane.SquareDistance(aLastPnt) < Precision::SquareConfusion();
283 double GeomAPI_Edge::length() const
285 const TopoDS_Edge& anEdge = TopoDS::Edge(impl<TopoDS_Shape>());
286 BRepAdaptor_Curve aBRepAdaptor = BRepAdaptor_Curve(anEdge);
287 Adaptor3d_Curve* anAdaptor3d = &aBRepAdaptor;
288 return GCPnts_AbscissaPoint::Length(*anAdaptor3d);
291 bool GeomAPI_Edge::isClosed() const
293 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
296 double aFirst, aLast;
297 Handle(Geom_Curve) aCurve = BRep_Tool::Curve((const TopoDS_Edge&)aShape, aFirst, aLast);
298 if (aCurve.IsNull() || !aCurve->IsPeriodic())
300 aLast += aLast > aFirst ? -aCurve->Period() : aCurve->Period();;
302 return fabs(aFirst - aLast) < 1.e-9;
305 bool GeomAPI_Edge::isDegenerated() const
307 const TopoDS_Shape& aShape = const_cast<GeomAPI_Edge*>(this)->impl<TopoDS_Shape>();
308 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_EDGE)
310 return BRep_Tool::Degenerated(TopoDS::Edge(aShape));
313 void GeomAPI_Edge::setFirstPointTolerance(const double theTolerance)
315 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
316 TopoDS_Vertex aVFirst, aVLast;
317 TopExp::Vertices(anEdge, aVFirst, aVLast);
318 BRep_Builder().UpdateVertex(aVFirst, theTolerance);
321 void GeomAPI_Edge::setLastPointTolerance(const double theTolerance)
323 TopoDS_Edge anEdge = impl<TopoDS_Edge>();
324 TopoDS_Vertex aVFirst, aVLast;
325 TopExp::Vertices(anEdge, aVFirst, aVLast);
326 BRep_Builder().UpdateVertex(aVLast, theTolerance);