1 // Copyright (C) 2014-2019 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
20 #include<GeomAPI_Curve.h>
21 #include<GeomAPI_Pnt.h>
23 #include <TopoDS_Shape.hxx>
24 #include <Geom_Circle.hxx>
25 #include <Geom_Curve.hxx>
26 #include <Geom_Ellipse.hxx>
27 #include <Geom_Line.hxx>
28 #include <Geom_TrimmedCurve.hxx>
29 #include <GeomAPI_ProjectPointOnCurve.hxx>
30 #include <BRep_Tool.hxx>
31 #include <TopoDS_Edge.hxx>
33 #include <GeomAdaptor_Curve.hxx>
35 #define MY_CURVE (*(implPtr<Handle_Geom_Curve>()))
37 GeomAPI_Curve::GeomAPI_Curve()
38 : GeomAPI_Interface(new Handle_Geom_Curve()), myStart(0), myEnd(1)
42 GeomAPI_Curve::GeomAPI_Curve(const std::shared_ptr<GeomAPI_Shape>& theShape)
43 : GeomAPI_Interface(new Handle_Geom_Curve()) // initially it is null
45 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
46 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
47 if (!anEdge.IsNull()) {
48 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, myStart, myEnd);
49 if (!aCurve.IsNull()) {
50 setImpl(new Handle(Geom_Curve)(aCurve));
55 bool GeomAPI_Curve::isNull() const
57 return MY_CURVE.IsNull() == Standard_True;
60 bool GeomAPI_Curve::isLine() const
62 return !isNull() && MY_CURVE->DynamicType() == STANDARD_TYPE(Geom_Line);
65 bool GeomAPI_Curve::isCircle() const
67 return !isNull() && MY_CURVE->DynamicType() == STANDARD_TYPE(Geom_Circle);
70 bool GeomAPI_Curve::isEllipse() const
72 return !isNull() && MY_CURVE->DynamicType() == STANDARD_TYPE(Geom_Ellipse);
75 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Curve::getPoint(double theParam)
77 GeomAdaptor_Curve aAdaptor(MY_CURVE, myStart, myEnd);
78 gp_Pnt aPnt = aAdaptor.Value(theParam);
79 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
82 bool GeomAPI_Curve::isEqual(const std::shared_ptr<GeomAPI_Curve>& theOther) const
84 return MY_CURVE == theOther->impl<Handle_Geom_Curve>();
87 bool GeomAPI_Curve::isTrimmed() const
89 return !isNull() && MY_CURVE->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve);
92 // unused in the unit tests for now
94 GeomCurvePtr GeomAPI_Curve::basisCurve() const
96 Handle(Geom_Curve) aCurve = MY_CURVE;
97 if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
98 aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
100 GeomCurvePtr aNewCurve(new GeomAPI_Curve);
101 aNewCurve->setImpl(new Handle(Geom_Curve)(aCurve));
107 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Curve::project(
108 const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
110 std::shared_ptr<GeomAPI_Pnt> aResult;
111 if (MY_CURVE.IsNull())
114 const gp_Pnt& aPoint = thePoint->impl<gp_Pnt>();
116 GeomAPI_ProjectPointOnCurve aProj(aPoint, MY_CURVE);
117 Standard_Integer aNbPoint = aProj.NbPoints();
119 gp_Pnt aNearest = aProj.NearestPoint();
120 aResult = GeomPointPtr(new GeomAPI_Pnt(aNearest.X(), aNearest.Y(), aNearest.Z()));
125 // ================================================================================================
127 bool GeomAPI_Curve::Comparator::operator()(const GeomCurvePtr& theCurve1,
128 const GeomCurvePtr& theCurve2) const
130 const Handle(Geom_Curve)& aCurve1 = theCurve1->impl<Handle_Geom_Curve>();
131 const Handle(Geom_Curve)& aCurve2 = theCurve2->impl<Handle_Geom_Curve>();
132 return aCurve1.get() < aCurve2.get();