Salome HOME
ca5e1891432361b2f2ce0d026dbfa41eb4fd25b7
[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   int aNbKnots = (int)thePoles.size() - theDegree + 1;
36   if (aNbKnots < 2)
37     return new Handle_Geom2d_BSplineCurve();
38
39   // collect arrays of poles, weights, knots and multiplicities
40   TColgp_Array1OfPnt2d aPoles(1, (int)thePoles.size());
41   TColStd_Array1OfReal aWeights(1, (int)theWeights.size());
42   TColStd_Array1OfReal aKnots(1, aNbKnots);
43   TColStd_Array1OfInteger aMults(1, aNbKnots);
44
45   int anIndex = 1;
46   for (std::list<GeomPnt2dPtr>::const_iterator aPIt = thePoles.begin();
47        aPIt != thePoles.end(); ++aPIt, ++anIndex)
48     aPoles.SetValue(anIndex, gp_Pnt2d((*aPIt)->x(), (*aPIt)->y()));
49   anIndex = 1;
50   for (std::list<double>::const_iterator aWIt = theWeights.begin();
51        aWIt != theWeights.end(); ++aWIt, ++anIndex)
52     aWeights.SetValue(anIndex, *aWIt);
53   anIndex = 1;
54   static const double aStartParam = 0.0;
55   static const double aEndParam = 1.0;
56   double aStep = aEndParam / (aNbKnots - 1);
57   for (double aKnot = aStartParam; anIndex < aNbKnots; ++anIndex, aKnot += aStep)
58     aKnots.SetValue(anIndex, aKnot);
59   aKnots.ChangeLast() = aEndParam;
60   anIndex = 1;
61   aMults.SetValue(anIndex, theDegree + 1);
62   for (++anIndex; anIndex < aNbKnots; ++anIndex)
63     aMults.SetValue(anIndex, 1);
64   aMults.SetValue(aNbKnots, theDegree + 1);
65
66   Handle(Geom2d_BSplineCurve) aCurve =
67     new Geom2d_BSplineCurve(aPoles, aWeights, aKnots, aMults, theDegree, thePeriodic);
68   return new Handle_Geom2d_BSplineCurve(aCurve);
69 }
70
71 static Handle_Geom2d_BSplineCurve* newBSpline2d(
72     const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
73     const std::list<double>& theWeights,
74     const bool thePeriodic)
75 {
76   int aDegree = 3;
77   if ((int)thePoles.size() <= aDegree)
78     aDegree = (int)thePoles.size() - 1;
79   if (aDegree <= 0)
80     return new Handle_Geom2d_BSplineCurve();
81   return newBSpline2d(thePoles, theWeights, aDegree, thePeriodic);
82 }
83
84
85 GeomAPI_BSpline2d::GeomAPI_BSpline2d(const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
86                                      const std::list<double>& theWeights,
87                                      const bool thePeriodic)
88   : GeomAPI_Interface(newBSpline2d(thePoles, theWeights, thePeriodic))
89 {
90   if (isNull())
91     throw Standard_ConstructionError("GeomAPI_BSpline2d: Impossible to create B-spline curve");
92 }
93
94 GeomAPI_BSpline2d::GeomAPI_BSpline2d(const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
95                                      const std::list<double>& theWeights,
96                                      const int theDegree,
97                                      const bool thePeriodic)
98   : GeomAPI_Interface(newBSpline2d(thePoles, theWeights, theDegree, thePeriodic))
99 {
100   if (isNull())
101     throw Standard_ConstructionError("GeomAPI_BSpline2d: Impossible to create B-spline curve");
102 }
103
104 bool GeomAPI_BSpline2d::isNull() const
105 {
106   return MY_BSPLINE.IsNull();
107 }
108
109 int GeomAPI_BSpline2d::degree() const
110 {
111   return MY_BSPLINE->Degree();
112 }
113
114 void GeomAPI_BSpline2d::D0(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
115 {
116   gp_Pnt2d aPnt;
117   MY_BSPLINE->D0(theU, aPnt);
118   thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
119 }
120
121 void GeomAPI_BSpline2d::D1(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
122                                               std::shared_ptr<GeomAPI_XY>& theDerivative)
123 {
124   gp_Pnt2d aPnt;
125   gp_Vec2d aVec;
126   MY_BSPLINE->D1(theU, aPnt, aVec);
127   thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
128   theDerivative.reset(new GeomAPI_XY(aVec.X(), aVec.Y()));
129 }