Salome HOME
Issue #17347: B-Splines in Sketcher
[modules/shaper.git] / src / GeomAPI / GeomAPI_BSpline2d.cpp
1 // Copyright (C) 2019-2020  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_BSpline2d.h>
21 #include <GeomAPI_Pnt2d.h>
22 #include <GeomAPI_XY.h>
23
24 #include <Geom2d_BSplineCurve.hxx>
25
26 #define MY_BSPLINE (*(implPtr<Handle_Geom2d_BSplineCurve>()))
27
28
29 static Handle_Geom2d_BSplineCurve* newBSpline2d(
30     const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
31     const std::list<double>& theWeights,
32     const std::list<double>& theKnots,
33     const std::list<int>& theMults,
34     const int theDegree,
35     const bool thePeriodic)
36 {
37   // collect arrays of poles, weights, knots and multiplicities
38   TColgp_Array1OfPnt2d aPoles(1, (int)thePoles.size());
39   TColStd_Array1OfReal aWeights(1, (int)theWeights.size());
40   TColStd_Array1OfReal aKnots(1, (int)theKnots.size());
41   TColStd_Array1OfInteger aMults(1, (int)theMults.size());
42
43   int anIndex = 1;
44   for (std::list<GeomPnt2dPtr>::const_iterator aPIt = thePoles.begin();
45        aPIt != thePoles.end(); ++aPIt, ++anIndex)
46     aPoles.SetValue(anIndex, gp_Pnt2d((*aPIt)->x(), (*aPIt)->y()));
47   anIndex = 1;
48   for (std::list<double>::const_iterator aWIt = theWeights.begin();
49        aWIt != theWeights.end(); ++aWIt, ++anIndex)
50     aWeights.SetValue(anIndex, *aWIt);
51   anIndex = 1;
52   for (std::list<double>::const_iterator aKIt = theKnots.begin();
53        aKIt != theKnots.end(); ++aKIt, ++anIndex)
54     aKnots.SetValue(anIndex, *aKIt);
55   anIndex = 1;
56   for (std::list<int>::const_iterator aMIt = theMults.begin();
57        aMIt != theMults.end(); ++aMIt, ++anIndex)
58     aMults.SetValue(anIndex, *aMIt);
59
60   Handle(Geom2d_BSplineCurve) aCurve =
61       new Geom2d_BSplineCurve(aPoles, aWeights, aKnots, aMults, theDegree, thePeriodic);
62   return new Handle_Geom2d_BSplineCurve(aCurve);
63 }
64
65 static Handle_Geom2d_BSplineCurve* newBSpline2d(
66     const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
67     const std::list<double>& theWeights,
68     const int theDegree,
69     const bool thePeriodic)
70 {
71   int aNbKnots = (int)thePoles.size() - theDegree + 1;
72   if (aNbKnots < 2)
73     return new Handle_Geom2d_BSplineCurve();
74
75   static const double aStartParam = 0.0;
76   static const double aEndParam = 1.0;
77   double aStep = aEndParam / (aNbKnots - 1);
78   int anIndex = 1;
79   std::list<double> aKnots;
80   for (double aKnot = aStartParam; anIndex < aNbKnots; ++anIndex, aKnot += aStep)
81     aKnots.push_back(aKnot);
82   aKnots.push_back(aEndParam);
83
84   std::list<int> aMults(aNbKnots - 2, 1);
85   aMults.push_front(theDegree + 1);
86   aMults.push_back(theDegree + 1);
87
88   return newBSpline2d(thePoles, theWeights, aKnots, aMults, theDegree, thePeriodic);
89 }
90
91 static Handle_Geom2d_BSplineCurve* newBSpline2d(
92     const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
93     const std::list<double>& theWeights,
94     const bool thePeriodic)
95 {
96   int aDegree = 3;
97   if ((int)thePoles.size() <= aDegree)
98     aDegree = (int)thePoles.size() - 1;
99   if (aDegree <= 0)
100     return new Handle_Geom2d_BSplineCurve();
101   return newBSpline2d(thePoles, theWeights, aDegree, thePeriodic);
102 }
103
104
105 GeomAPI_BSpline2d::GeomAPI_BSpline2d(const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
106                                      const std::list<double>& theWeights,
107                                      const bool thePeriodic)
108   : GeomAPI_Interface(newBSpline2d(thePoles, theWeights, thePeriodic))
109 {
110   if (isNull())
111     throw Standard_ConstructionError("GeomAPI_BSpline2d: Impossible to create B-spline curve");
112 }
113
114 GeomAPI_BSpline2d::GeomAPI_BSpline2d(const int theDegree,
115                                      const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
116                                      const std::list<double>& theWeights,
117                                      const std::list<double>& theKnots,
118                                      const std::list<int>& theMults,
119                                      const bool thePeriodic)
120   : GeomAPI_Interface(newBSpline2d(thePoles, theWeights, theKnots, theMults,
121                                    theDegree, thePeriodic))
122 {
123   if (isNull())
124     throw Standard_ConstructionError("GeomAPI_BSpline2d: Impossible to create B-spline curve");
125 }
126
127 bool GeomAPI_BSpline2d::isNull() const
128 {
129   return MY_BSPLINE.IsNull();
130 }
131
132 int GeomAPI_BSpline2d::degree() const
133 {
134   return MY_BSPLINE->Degree();
135 }
136
137 std::list<double> GeomAPI_BSpline2d::knots() const
138 {
139   const TColStd_Array1OfReal& aBSplKnots = MY_BSPLINE->Knots();
140   return std::list<double>(aBSplKnots.begin(), aBSplKnots.end());
141 }
142
143 std::list<int> GeomAPI_BSpline2d::mults() const
144 {
145   const TColStd_Array1OfInteger& aBSplMults = MY_BSPLINE->Multiplicities();
146   return std::list<int>(aBSplMults.begin(), aBSplMults.end());
147 }
148
149 void GeomAPI_BSpline2d::D0(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
150 {
151   gp_Pnt2d aPnt;
152   MY_BSPLINE->D0(theU, aPnt);
153   thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
154 }
155
156 void GeomAPI_BSpline2d::D1(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
157                                               std::shared_ptr<GeomAPI_XY>& theDerivative)
158 {
159   gp_Pnt2d aPnt;
160   gp_Vec2d aVec;
161   MY_BSPLINE->D1(theU, aPnt, aVec);
162   thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
163   theDerivative.reset(new GeomAPI_XY(aVec.X(), aVec.Y()));
164 }