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