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