Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / GeomAPI / GeomAPI_Curve.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include<GeomAPI_Curve.h>
21 #include<GeomAPI_Pnt.h>
22
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>
32 #include <TopoDS.hxx>
33 #include <GeomAdaptor_Curve.hxx>
34
35 #define MY_CURVE (*(implPtr<Handle_Geom_Curve>()))
36
37 GeomAPI_Curve::GeomAPI_Curve()
38     : GeomAPI_Interface(new Handle_Geom_Curve()),
39       myStart(-Precision::Infinite()),
40       myEnd(Precision::Infinite())
41 {
42 }
43
44 GeomAPI_Curve::GeomAPI_Curve(const std::shared_ptr<GeomAPI_Shape>& theShape)
45   : GeomAPI_Interface(new Handle_Geom_Curve()) // initially it is null
46 {
47   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
48   TopoDS_Edge anEdge = TopoDS::Edge(aShape);
49   if (!anEdge.IsNull()) {
50     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, myStart, myEnd);
51     if (!aCurve.IsNull()) {
52       if (!BRep_Tool::IsClosed(anEdge))
53         aCurve = new Geom_TrimmedCurve(aCurve, myStart, myEnd);
54       setImpl(new Handle(Geom_Curve)(aCurve));
55     }
56   }
57 }
58
59 bool GeomAPI_Curve::isNull() const
60 {
61   return MY_CURVE.IsNull() == Standard_True;
62 }
63
64 static bool isCurveType(const Handle(Geom_Curve)& theCurve, const Handle(Standard_Type)& theType)
65 {
66   if (theCurve.IsNull())
67     return false;
68   Handle(Geom_Curve) aCurve = theCurve;
69   if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
70     aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
71   return aCurve->DynamicType() == theType;
72 }
73
74 bool GeomAPI_Curve::isLine() const
75 {
76   return isCurveType(MY_CURVE, STANDARD_TYPE(Geom_Line));
77 }
78
79 bool GeomAPI_Curve::isCircle() const
80 {
81   return isCurveType(MY_CURVE, STANDARD_TYPE(Geom_Circle));
82 }
83
84 bool GeomAPI_Curve::isEllipse() const
85 {
86   return isCurveType(MY_CURVE, STANDARD_TYPE(Geom_Ellipse));
87 }
88
89 double GeomAPI_Curve::startParam()
90 {
91   if (Precision::IsInfinite(myStart)) {
92     if (isTrimmed()) {
93       myStart = Handle(Geom_TrimmedCurve)::DownCast(MY_CURVE)->FirstParameter();
94     }
95     else if (MY_CURVE->IsClosed() && MY_CURVE->IsPeriodic())
96       myStart = 0.0;
97   }
98   return myStart;
99 }
100
101 double GeomAPI_Curve::endParam()
102 {
103   if (Precision::IsInfinite(myEnd)) {
104     if (isTrimmed()) {
105       myEnd = Handle(Geom_TrimmedCurve)::DownCast(MY_CURVE)->LastParameter();
106     }
107     else if (MY_CURVE->IsClosed() && MY_CURVE->IsPeriodic())
108       myEnd = MY_CURVE->Period();
109   }
110   return myEnd;
111 }
112
113 std::shared_ptr<GeomAPI_Pnt> GeomAPI_Curve::getPoint(double theParam)
114 {
115   GeomAdaptor_Curve aAdaptor(MY_CURVE, myStart, myEnd);
116   gp_Pnt aPnt = aAdaptor.Value(theParam);
117   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aPnt.X(), aPnt.Y(), aPnt.Z()));
118 }
119
120 bool GeomAPI_Curve::isEqual(const std::shared_ptr<GeomAPI_Curve>& theOther) const
121 {
122   return MY_CURVE == theOther->impl<Handle_Geom_Curve>();
123 }
124
125 bool GeomAPI_Curve::isTrimmed() const
126 {
127   return !isNull() && MY_CURVE->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve);
128 }
129
130 // unused in the unit tests for now
131 // LCOV_EXCL_START
132 GeomCurvePtr GeomAPI_Curve::basisCurve() const
133 {
134   Handle(Geom_Curve) aCurve = MY_CURVE;
135   if (aCurve->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve))
136     aCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve)->BasisCurve();
137
138   GeomCurvePtr aNewCurve(new GeomAPI_Curve);
139   aNewCurve->setImpl(new Handle(Geom_Curve)(aCurve));
140   return aNewCurve;
141 }
142 // LCOV_EXCL_STOP
143
144
145 const std::shared_ptr<GeomAPI_Pnt> GeomAPI_Curve::project(
146     const std::shared_ptr<GeomAPI_Pnt>& thePoint) const
147 {
148   std::shared_ptr<GeomAPI_Pnt> aResult;
149   if (MY_CURVE.IsNull())
150     return aResult;
151
152   const gp_Pnt& aPoint = thePoint->impl<gp_Pnt>();
153
154   GeomAPI_ProjectPointOnCurve aProj(aPoint, MY_CURVE);
155   Standard_Integer aNbPoint = aProj.NbPoints();
156   if (aNbPoint > 0) {
157     gp_Pnt aNearest = aProj.NearestPoint();
158     aResult = GeomPointPtr(new GeomAPI_Pnt(aNearest.X(), aNearest.Y(), aNearest.Z()));
159   }
160   return aResult;
161 }
162
163 // ================================================================================================
164
165 bool GeomAPI_Curve::Comparator::operator()(const GeomCurvePtr& theCurve1,
166                                            const GeomCurvePtr& theCurve2) const
167 {
168   const Handle(Geom_Curve)& aCurve1 = theCurve1->impl<Handle_Geom_Curve>();
169   const Handle(Geom_Curve)& aCurve2 = theCurve2->impl<Handle_Geom_Curve>();
170   return aCurve1.get() < aCurve2.get();
171 }