1 // Copyright (C) 2019-2020 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <GeomAPI_BSpline2d.h>
21 #include <GeomAPI_Pnt2d.h>
22 #include <GeomAPI_XY.h>
24 #include <Geom2d_BSplineCurve.hxx>
25 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
26 #include <GeomLib_Tool.hxx>
27 #include <Precision.hxx>
29 #define MY_BSPLINE (*(implPtr<Handle_Geom2d_BSplineCurve>()))
32 static Handle_Geom2d_BSplineCurve* newBSpline2d(
33 const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
34 const std::list<double>& theWeights,
36 const bool thePeriodic);
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,
45 const bool thePeriodic)
47 if (theKnots.empty() || theMults.empty())
48 return newBSpline2d(thePoles, theWeights, theDegree, thePeriodic);
51 if (thePeriodic && thePoles.front()->distance(thePoles.back()) < Precision::Confusion())
54 // collect arrays of poles, weights, knots and multiplicities
55 TColgp_Array1OfPnt2d aPoles(1, (int)thePoles.size() + anAuxPole);
56 TColStd_Array1OfReal aWeights(1, (int)theWeights.size() + anAuxPole);
57 TColStd_Array1OfReal aKnots(1, (int)theKnots.size());
58 TColStd_Array1OfInteger aMults(1, (int)theMults.size());
61 for (std::list<GeomPnt2dPtr>::const_iterator aPIt = thePoles.begin();
62 aPIt != thePoles.end() && anIndex <= aPoles.Upper(); ++aPIt, ++anIndex)
63 aPoles.SetValue(anIndex, gp_Pnt2d((*aPIt)->x(), (*aPIt)->y()));
65 for (std::list<double>::const_iterator aWIt = theWeights.begin();
66 aWIt != theWeights.end() && anIndex <= aWeights.Upper(); ++aWIt, ++anIndex)
67 aWeights.SetValue(anIndex, *aWIt);
69 for (std::list<double>::const_iterator aKIt = theKnots.begin();
70 aKIt != theKnots.end(); ++aKIt, ++anIndex)
71 aKnots.SetValue(anIndex, *aKIt);
73 for (std::list<int>::const_iterator aMIt = theMults.begin();
74 aMIt != theMults.end(); ++aMIt, ++anIndex)
75 aMults.SetValue(anIndex, *aMIt);
77 Handle(Geom2d_BSplineCurve) aCurve =
78 new Geom2d_BSplineCurve(aPoles, aWeights, aKnots, aMults, theDegree, thePeriodic);
79 return new Handle_Geom2d_BSplineCurve(aCurve);
82 Handle_Geom2d_BSplineCurve* newBSpline2d(
83 const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
84 const std::list<double>& theWeights,
86 const bool thePeriodic)
88 std::list<std::shared_ptr<GeomAPI_Pnt2d> > aPoles = thePoles;
89 std::list<double> aWeights = theWeights;
90 int aMult = theDegree + 1;
91 int aNbKnots = (int)thePoles.size() - theDegree + 1;
93 if (aPoles.front()->distance(aPoles.back()) < Precision::Confusion()) {
98 aNbKnots = (int)aPoles.size() + 1;
102 return new Handle_Geom2d_BSplineCurve();
104 static const double aStartParam = 0.0;
105 static const double aEndParam = 1.0;
106 double aStep = aEndParam / (aNbKnots - 1);
108 std::list<double> aKnots;
109 for (double aKnot = aStartParam; anIndex < aNbKnots; ++anIndex, aKnot += aStep)
110 aKnots.push_back(aKnot);
111 aKnots.push_back(aEndParam);
113 std::list<int> aMults(aNbKnots - 2, 1);
114 aMults.push_front(aMult);
115 aMults.push_back(aMult);
117 return newBSpline2d(aPoles, aWeights, aKnots, aMults, theDegree, thePeriodic);
120 static Handle_Geom2d_BSplineCurve* newBSpline2d(
121 const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
122 const std::list<double>& theWeights,
123 const bool thePeriodic)
126 if ((int)thePoles.size() <= aDegree)
127 aDegree = (int)thePoles.size() - 1;
129 return new Handle_Geom2d_BSplineCurve();
130 return newBSpline2d(thePoles, theWeights, aDegree, thePeriodic);
134 GeomAPI_BSpline2d::GeomAPI_BSpline2d(const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
135 const std::list<double>& theWeights,
136 const bool thePeriodic)
137 : GeomAPI_Interface(newBSpline2d(thePoles, theWeights, thePeriodic))
140 throw Standard_ConstructionError("GeomAPI_BSpline2d: Impossible to create B-spline curve");
143 GeomAPI_BSpline2d::GeomAPI_BSpline2d(const int theDegree,
144 const std::list<std::shared_ptr<GeomAPI_Pnt2d> >& thePoles,
145 const std::list<double>& theWeights,
146 const std::list<double>& theKnots,
147 const std::list<int>& theMults,
148 const bool thePeriodic)
149 : GeomAPI_Interface(newBSpline2d(thePoles, theWeights, theKnots, theMults,
150 theDegree, thePeriodic))
153 throw Standard_ConstructionError("GeomAPI_BSpline2d: Impossible to create B-spline curve");
156 bool GeomAPI_BSpline2d::isNull() const
158 return MY_BSPLINE.IsNull();
161 int GeomAPI_BSpline2d::degree() const
163 return MY_BSPLINE->Degree();
166 std::list<double> GeomAPI_BSpline2d::knots() const
168 const TColStd_Array1OfReal& aBSplKnots = MY_BSPLINE->Knots();
169 return std::list<double>(aBSplKnots.begin(), aBSplKnots.end());
172 std::list<int> GeomAPI_BSpline2d::mults() const
174 const TColStd_Array1OfInteger& aBSplMults = MY_BSPLINE->Multiplicities();
175 return std::list<int>(aBSplMults.begin(), aBSplMults.end());
178 const bool GeomAPI_BSpline2d::parameter(const std::shared_ptr<GeomAPI_Pnt2d> thePoint,
179 const double theTolerance,
180 double& theParameter) const
182 return GeomLib_Tool::Parameter(MY_BSPLINE, thePoint->impl<gp_Pnt2d>(),
183 theTolerance, theParameter) == Standard_True;
186 void GeomAPI_BSpline2d::D0(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
189 MY_BSPLINE->D0(theU, aPnt);
190 thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
193 void GeomAPI_BSpline2d::D1(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint,
194 std::shared_ptr<GeomAPI_XY>& theDerivative)
198 MY_BSPLINE->D1(theU, aPnt, aVec);
199 thePoint.reset(new GeomAPI_Pnt2d(aPnt.X(), aPnt.Y()));
200 theDerivative.reset(new GeomAPI_XY(aVec.X(), aVec.Y()));