Salome HOME
676187250f130631f798c5c8129c70a18f0aa672
[modules/shaper.git] / src / GeomAPI / GeomAPI_BSpline.cpp
1 // Copyright (C) 2019-2023  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_BSpline.h>
21 #include <GeomAPI_Pnt.h>
22
23 #include <Geom_BSplineCurve.hxx>
24
25 #include <GeomConvert.hxx>
26
27 #define MY_BSPLINE (*(implPtr<Handle_Geom_BSplineCurve>()))
28
29 GeomAPI_BSpline::GeomAPI_BSpline (const GeomCurvePtr& theCurve)
30 {
31   GeomCurvePtr anUntrimmedCurve = theCurve->basisCurve();
32   Handle(Geom_Curve) aCurve = anUntrimmedCurve->impl<Handle(Geom_Curve)>();
33   Handle(Geom_BSplineCurve) aBSpl = Handle(Geom_BSplineCurve)::DownCast(aCurve);
34   if (aBSpl.IsNull())
35     throw Standard_ConstructionError("GeomAPI_BSpline: Curve is not a B-spline");
36   setImpl(new Handle_Geom_BSplineCurve(aBSpl));
37 }
38
39 int GeomAPI_BSpline::degree() const
40 {
41   return MY_BSPLINE->Degree();
42 }
43
44 std::list<GeomPointPtr> GeomAPI_BSpline::poles() const
45 {
46   const TColgp_Array1OfPnt& aBSplPoles = MY_BSPLINE->Poles();
47
48   std::list<GeomPointPtr> aPoles;
49   for (int anIndex = aBSplPoles.Lower(); anIndex <= aBSplPoles.Upper(); ++anIndex) {
50     const gp_Pnt& aPoint = aBSplPoles.Value(anIndex);
51     aPoles.push_back(GeomPointPtr(new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z())));
52   }
53   return aPoles;
54 }
55
56 std::list<double> GeomAPI_BSpline::weights() const
57 {
58   std::list<double> aWeights;
59   const TColStd_Array1OfReal* aBSplWeights = MY_BSPLINE->Weights();
60   if (aBSplWeights)
61     aWeights.assign(aBSplWeights->begin(), aBSplWeights->end());
62   return aWeights;
63 }
64
65 std::list<double> GeomAPI_BSpline::knots() const
66 {
67   const TColStd_Array1OfReal& aBSplKnots = MY_BSPLINE->Knots();
68   return std::list<double>(aBSplKnots.begin(), aBSplKnots.end());
69 }
70
71 std::list<int> GeomAPI_BSpline::mults() const
72 {
73   const TColStd_Array1OfInteger& aBSplMults = MY_BSPLINE->Multiplicities();
74   return std::list<int>(aBSplMults.begin(), aBSplMults.end());
75 }
76
77 bool GeomAPI_BSpline::isPeriodic() const
78 {
79   return MY_BSPLINE->IsPeriodic();
80 }
81
82 GeomBSplinePtr GeomAPI_BSpline::convertToBSpline (const GeomCurvePtr& theCurve)
83 {
84   GeomCurvePtr anUntrimmedCurve = theCurve->basisCurve();
85   Handle(Geom_Curve) aCurve = anUntrimmedCurve->impl<Handle(Geom_Curve)>();
86   Handle(Geom_BSplineCurve) aBSpl = Handle(Geom_BSplineCurve)::DownCast(aCurve);
87   if (aBSpl.IsNull()) {
88     // convert to b-spline
89     aBSpl = GeomConvert::CurveToBSplineCurve(aCurve);
90   }
91   if (aBSpl.IsNull())
92     throw Standard_ConstructionError("GeomAPI_BSpline: Conversion to B-spline failed");
93   GeomCurvePtr aResCurve (new GeomAPI_Curve());
94   aResCurve->setImpl(new Handle_Geom_BSplineCurve(aBSpl));
95   GeomBSplinePtr aResult (new GeomAPI_BSpline(aResCurve));
96   return aResult;
97 }