Salome HOME
ef6f7a24ef231f945e3a8480f5d184856c1e6d90
[modules/shaper.git] / src / GeomAPI / GeomAPI_BSpline2d.cpp
1 // Copyright (C) 2019-2021  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   int anAuxPole = 0;
51   if (thePeriodic && thePoles.front()->distance(thePoles.back()) < Precision::Confusion()) {
52     // additionally check the number of poles is greater than needed for th periodic B-spline
53     int aNbPoles = 0;
54     std::list<int>::const_iterator it = theMults.begin();
55     for (++it; it != theMults.end(); ++it)
56       aNbPoles += *it;
57     if ((int)thePoles.size() > aNbPoles)
58       anAuxPole = -1;
59   }
60
61   // collect arrays of poles, weights, knots and multiplicities
62   TColgp_Array1OfPnt2d aPoles(1, (int)thePoles.size() + anAuxPole);
63   TColStd_Array1OfReal aWeights(1, (int)theWeights.size() + anAuxPole);
64   TColStd_Array1OfReal aKnots(1, (int)theKnots.size());
65   TColStd_Array1OfInteger aMults(1, (int)theMults.size());
66
67   int anIndex = 1;
68   for (std::list<GeomPnt2dPtr>::const_iterator aPIt = thePoles.begin();
69        aPIt != thePoles.end() && anIndex <= aPoles.Upper(); ++aPIt, ++anIndex)
70     aPoles.SetValue(anIndex, gp_Pnt2d((*aPIt)->x(), (*aPIt)->y()));
71   anIndex = 1;
72   for (std::list<double>::const_iterator aWIt = theWeights.begin();
73        aWIt != theWeights.end() && anIndex <= aWeights.Upper(); ++aWIt, ++anIndex)
74     aWeights.SetValue(anIndex, *aWIt);
75   anIndex = 1;
76   for (std::list<double>::const_iterator aKIt = theKnots.begin();
77        aKIt != theKnots.end(); ++aKIt, ++anIndex)
78     aKnots.SetValue(anIndex, *aKIt);
79   anIndex = 1;
80   for (std::list<int>::const_iterator aMIt = theMults.begin();
81        aMIt != theMults.end(); ++aMIt, ++anIndex)
82     aMults.SetValue(anIndex, *aMIt);
83
84   Handle(Geom2d_BSplineCurve) aCurve =
85       new Geom2d_BSplineCurve(aPoles, aWeights, aKnots, aMults, theDegree, thePeriodic);
86   return new Handle_Geom2d_BSplineCurve(aCurve);
87 }
88
89 Handle_Geom2d_BSplineCurve* newBSpline2d(
90     const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
91     const std::list<double>& theWeights,
92     const int theDegree,
93     const bool thePeriodic)
94 {
95   std::list<std::shared_ptr<GeomAPI_Pnt2d> > aPoles = thePoles;
96   std::list<double> aWeights = theWeights;
97   int aMult = theDegree + 1;
98   int aNbKnots = (int)thePoles.size() - theDegree + 1;
99   if (thePeriodic) {
100     if (aPoles.front()->distance(aPoles.back()) < Precision::Confusion()) {
101       aPoles.pop_back();
102       aWeights.pop_back();
103     }
104     aMult = 1;
105     aNbKnots = (int)aPoles.size() + 1;
106   }
107
108   if (aNbKnots < 2)
109     return new Handle_Geom2d_BSplineCurve();
110
111   static const double aStartParam = 0.0;
112   static const double aEndParam = 1.0;
113   double aStep = aEndParam / (aNbKnots - 1);
114   int anIndex = 1;
115   std::list<double> aKnots;
116   for (double aKnot = aStartParam; anIndex < aNbKnots; ++anIndex, aKnot += aStep)
117     aKnots.push_back(aKnot);
118   aKnots.push_back(aEndParam);
119
120   std::list<int> aMults(aNbKnots - 2, 1);
121   aMults.push_front(aMult);
122   aMults.push_back(aMult);
123
124   return newBSpline2d(aPoles, aWeights, aKnots, aMults, theDegree, thePeriodic);
125 }
126
127 static Handle_Geom2d_BSplineCurve* newBSpline2d(
128     const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
129     const std::list<double>& theWeights,
130     const bool thePeriodic)
131 {
132   int aDegree = 3;
133   if ((int)thePoles.size() <= aDegree)
134     aDegree = (int)thePoles.size() - 1;
135   if (aDegree <= 0)
136     return new Handle_Geom2d_BSplineCurve();
137   return newBSpline2d(thePoles, theWeights, aDegree, thePeriodic);
138 }
139
140
141 GeomAPI_BSpline2d::GeomAPI_BSpline2d(const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
142                                      const std::list<double>& theWeights,
143                                      const bool thePeriodic)
144   : GeomAPI_Interface(newBSpline2d(thePoles, theWeights, thePeriodic))
145 {
146   if (isNull())
147     throw Standard_ConstructionError("GeomAPI_BSpline2d: Impossible to create B-spline curve");
148 }
149
150 GeomAPI_BSpline2d::GeomAPI_BSpline2d(const int theDegree,
151                                      const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
152                                      const std::list<double>& theWeights,
153                                      const std::list<double>& theKnots,
154                                      const std::list<int>& theMults,
155                                      const bool thePeriodic)
156   : GeomAPI_Interface(newBSpline2d(thePoles, theWeights, theKnots, theMults,
157                                    theDegree, thePeriodic))
158 {
159   if (isNull())
160     throw Standard_ConstructionError("GeomAPI_BSpline2d: Impossible to create B-spline curve");
161 }
162
163 bool GeomAPI_BSpline2d::isNull() const
164 {
165   return MY_BSPLINE.IsNull();
166 }
167
168 int GeomAPI_BSpline2d::degree() const
169 {
170   return MY_BSPLINE->Degree();
171 }
172
173 std::list<double> GeomAPI_BSpline2d::knots() const
174 {
175   const TColStd_Array1OfReal& aBSplKnots = MY_BSPLINE->Knots();
176   return std::list<double>(aBSplKnots.begin(), aBSplKnots.end());
177 }
178
179 std::list<int> GeomAPI_BSpline2d::mults() const
180 {
181   const TColStd_Array1OfInteger& aBSplMults = MY_BSPLINE->Multiplicities();
182   return std::list<int>(aBSplMults.begin(), aBSplMults.end());
183 }
184
185 const bool GeomAPI_BSpline2d::parameter(const std::shared_ptr<GeomAPI_Pnt2d> thePoint,
186                                         const double theTolerance,
187                                         double& theParameter) const
188 {
189   const gp_Pnt2d& aPoint = thePoint->impl<gp_Pnt2d>();
190   bool isOk = GeomLib_Tool::Parameter(MY_BSPLINE, aPoint,
191                                       theTolerance, theParameter) == Standard_True;
192   if (!isOk) {
193     // Sometimes OCCT's Extrema algorithm cannot find the parameter on B-spline curve
194     // (usually, if the point is near the curve extremity).
195     // Workaround: compute distance to each boundary point
196     isOk = true;
197     double aDistPS = aPoint.Distance(MY_BSPLINE->Poles().First());
198     double aDistPE = aPoint.Distance(MY_BSPLINE->Poles().Last());
199     if (aDistPS < aDistPE && aDistPS < theTolerance)
200       theParameter = MY_BSPLINE->Knots().First();
201     else if (aDistPE < aDistPS && aDistPE < theTolerance)
202       theParameter = MY_BSPLINE->Knots().Last();
203     else
204       isOk = false;
205   }
206   return isOk;
207 }
208
209 void GeomAPI_BSpline2d::D0(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
210 {
211   gp_Pnt2d aPnt;
212   MY_BSPLINE->D0(theU, aPnt);
213   thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
214 }
215
216 void GeomAPI_BSpline2d::D1(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
217                                               std::shared_ptr<GeomAPI_XY>& theDerivative)
218 {
219   gp_Pnt2d aPnt;
220   gp_Vec2d aVec;
221   MY_BSPLINE->D1(theU, aPnt, aVec);
222   thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
223   theDerivative.reset(new GeomAPI_XY(aVec.X(), aVec.Y()));
224 }