1 // Copyright (c) 2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <BSplSLib_Cache.hxx>
15 #include <BSplSLib.hxx>
17 #include <NCollection_LocalArray.hxx>
19 #include <TColgp_HArray2OfPnt.hxx>
20 #include <TColStd_HArray1OfInteger.hxx>
21 #include <TColStd_HArray1OfReal.hxx>
22 #include <TColStd_HArray2OfReal.hxx>
24 IMPLEMENT_STANDARD_HANDLE(BSplSLib_Cache, Standard_Transient)
25 IMPLEMENT_STANDARD_RTTIEXT(BSplSLib_Cache, Standard_Transient)
27 //! Converts handle of array of Standard_Real into the pointer to Standard_Real
28 static Standard_Real* ConvertArray(const Handle_TColStd_HArray2OfReal& theHArray)
30 const TColStd_Array2OfReal& anArray = theHArray->Array2();
31 return (Standard_Real*) &(anArray(anArray.LowerRow(), anArray.LowerCol()));
35 BSplSLib_Cache::BSplSLib_Cache()
37 myPolesWeights.Nullify();
38 myIsRational = Standard_False;
39 mySpanStart[0] = mySpanStart[1] = 0.0;
40 mySpanLength[0] = mySpanLength[1] = 0.0;
41 mySpanIndex[0] = mySpanIndex[1] = 0;
42 myDegree[0] = myDegree[1] = 0;
43 myFlatKnots[0].Nullify();
44 myFlatKnots[1].Nullify();
47 BSplSLib_Cache::BSplSLib_Cache(const Standard_Integer& theDegreeU,
48 const Standard_Boolean& thePeriodicU,
49 const TColStd_Array1OfReal& theFlatKnotsU,
50 const Standard_Integer& theDegreeV,
51 const Standard_Boolean& thePeriodicV,
52 const TColStd_Array1OfReal& theFlatKnotsV,
53 const TColgp_Array2OfPnt& thePoles,
54 const TColStd_Array2OfReal& theWeights)
56 Standard_Real aU = theFlatKnotsU.Value(theFlatKnotsU.Lower() + theDegreeU);
57 Standard_Real aV = theFlatKnotsV.Value(theFlatKnotsV.Lower() + theDegreeV);
60 theDegreeU, thePeriodicU, theFlatKnotsU,
61 theDegreeV, thePeriodicV, theFlatKnotsV,
62 thePoles, theWeights);
66 Standard_Boolean BSplSLib_Cache::IsCacheValid(Standard_Real theParameterU,
67 Standard_Real theParameterV) const
69 Standard_Real aNewU = theParameterU;
70 Standard_Real aNewV = theParameterV;
71 if (!myFlatKnots[0].IsNull())
72 PeriodicNormalization(myDegree[0], myFlatKnots[0]->Array1(), aNewU);
73 if (!myFlatKnots[1].IsNull())
74 PeriodicNormalization(myDegree[1], myFlatKnots[1]->Array1(), aNewV);
76 Standard_Real aDelta0 = aNewU - mySpanStart[0];
77 Standard_Real aDelta1 = aNewV - mySpanStart[1];
78 return (aDelta0 >= -mySpanLength[0] && (aDelta0 < mySpanLength[0] || mySpanIndex[0] == mySpanIndexMax[0]) &&
79 aDelta1 >= -mySpanLength[1] && (aDelta1 < mySpanLength[1] || mySpanIndex[1] == mySpanIndexMax[1]));
82 void BSplSLib_Cache::PeriodicNormalization(const Standard_Integer& theDegree,
83 const TColStd_Array1OfReal& theFlatKnots,
84 Standard_Real& theParameter) const
86 Standard_Real aPeriod = theFlatKnots.Value(theFlatKnots.Upper() - theDegree) -
87 theFlatKnots.Value(theDegree + 1) ;
88 if (theParameter < theFlatKnots.Value(theDegree + 1))
90 Standard_Real aScale = IntegerPart(
91 (theFlatKnots.Value(theDegree + 1) - theParameter) / aPeriod);
92 theParameter += aPeriod * (aScale + 1.0);
94 if (theParameter > theFlatKnots.Value(theFlatKnots.Upper() - theDegree))
96 Standard_Real aScale = IntegerPart(
97 (theParameter - theFlatKnots.Value(theFlatKnots.Upper() - theDegree)) / aPeriod);
98 theParameter -= aPeriod * (aScale + 1.0);
103 void BSplSLib_Cache::BuildCache(const Standard_Real& theParameterU,
104 const Standard_Real& theParameterV,
105 const Standard_Integer& theDegreeU,
106 const Standard_Boolean& thePeriodicU,
107 const TColStd_Array1OfReal& theFlatKnotsU,
108 const Standard_Integer& theDegreeV,
109 const Standard_Boolean& thePeriodicV,
110 const TColStd_Array1OfReal& theFlatKnotsV,
111 const TColgp_Array2OfPnt& thePoles,
112 const TColStd_Array2OfReal& theWeights)
114 // Normalize the parameters for periodical B-splines
115 Standard_Real aNewParamU = theParameterU;
118 PeriodicNormalization(theDegreeU, theFlatKnotsU, aNewParamU);
119 myFlatKnots[0] = new TColStd_HArray1OfReal(1, theFlatKnotsU.Length());
120 myFlatKnots[0]->ChangeArray1() = theFlatKnotsU;
122 else if (!myFlatKnots[0].IsNull()) // Periodical curve became non-periodical
123 myFlatKnots[0].Nullify();
125 Standard_Real aNewParamV = theParameterV;
128 PeriodicNormalization(theDegreeV, theFlatKnotsV, aNewParamV);
129 myFlatKnots[1] = new TColStd_HArray1OfReal(1, theFlatKnotsV.Length());
130 myFlatKnots[1]->ChangeArray1() = theFlatKnotsV;
132 else if (!myFlatKnots[1].IsNull()) // Periodical curve became non-periodical
133 myFlatKnots[1].Nullify();
135 Standard_Integer aMinDegree = Min(theDegreeU, theDegreeV);
136 Standard_Integer aMaxDegree = Max(theDegreeU, theDegreeV);
138 // Change the size of cached data if needed
139 myIsRational = (&theWeights != NULL);
140 Standard_Integer aPWColNumber = myIsRational ? 4 : 3;
141 if (theDegreeU > myDegree[0] || theDegreeV > myDegree[1])
142 myPolesWeights = new TColStd_HArray2OfReal(1, aMaxDegree + 1, 1, aPWColNumber * (aMinDegree + 1));
144 myDegree[0] = theDegreeU;
145 myDegree[1] = theDegreeV;
146 mySpanIndex[0] = mySpanIndex[1] = 0;
147 BSplCLib::LocateParameter(theDegreeU, theFlatKnotsU, BSplCLib::NoMults(), aNewParamU,
148 thePeriodicU, mySpanIndex[0], aNewParamU);
149 BSplCLib::LocateParameter(theDegreeV, theFlatKnotsV, BSplCLib::NoMults(), aNewParamV,
150 thePeriodicV, mySpanIndex[1], aNewParamV);
151 mySpanLength[0] = (theFlatKnotsU.Value(mySpanIndex[0] + 1) - theFlatKnotsU.Value(mySpanIndex[0])) * 0.5;
152 mySpanStart[0] = theFlatKnotsU.Value(mySpanIndex[0]) + mySpanLength[0];
153 mySpanLength[1] = (theFlatKnotsV.Value(mySpanIndex[1] + 1) - theFlatKnotsV.Value(mySpanIndex[1])) * 0.5;
154 mySpanStart[1] = theFlatKnotsV.Value(mySpanIndex[1]) + mySpanLength[1];
155 mySpanIndexMax[0] = theFlatKnotsU.Length() - 1 - theDegreeU;
156 mySpanIndexMax[1] = theFlatKnotsV.Length() - 1 - theDegreeV;
158 // Calculate new cache data
159 BSplSLib::BuildCache(mySpanStart[0], mySpanStart[1],
160 mySpanLength[0], mySpanLength[1],
161 thePeriodicU, thePeriodicV,
162 theDegreeU, theDegreeV,
163 mySpanIndex[0], mySpanIndex[1],
164 theFlatKnotsU, theFlatKnotsV,
165 thePoles, theWeights, myPolesWeights->ChangeArray2());
169 void BSplSLib_Cache::D0(const Standard_Real& theU,
170 const Standard_Real& theV,
171 gp_Pnt& thePoint) const
173 Standard_Real aNewU = theU;
174 Standard_Real aNewV = theV;
175 if (!myFlatKnots[0].IsNull()) // B-spline is U-periodical
176 PeriodicNormalization(myDegree[0], myFlatKnots[0]->Array1(), aNewU);
177 aNewU = (aNewU - mySpanStart[0]) / mySpanLength[0];
178 if (!myFlatKnots[1].IsNull()) // B-spline is V-periodical
179 PeriodicNormalization(myDegree[1], myFlatKnots[1]->Array1(), aNewV);
180 aNewV = (aNewV - mySpanStart[1]) / mySpanLength[1];
182 Standard_Real* aPolesArray = ConvertArray(myPolesWeights);
183 Standard_Real aPoint[4];
185 Standard_Integer aDimension = myIsRational ? 4 : 3;
186 Standard_Integer aCacheCols = myPolesWeights->RowLength();
187 Standard_Integer aMinMaxDegree[2] = {Min(myDegree[0], myDegree[1]),
188 Max(myDegree[0], myDegree[1])};
189 Standard_Real aParameters[2];
190 if (myDegree[0] > myDegree[1])
192 aParameters[0] = aNewV;
193 aParameters[1] = aNewU;
197 aParameters[0] = aNewU;
198 aParameters[1] = aNewV;
201 NCollection_LocalArray<Standard_Real> aTransientCoeffs(aCacheCols); // array for intermediate results
203 // Calculate intermediate value of cached polynomial along columns
204 PLib::NoDerivativeEvalPolynomial(aParameters[1], aMinMaxDegree[1],
205 aCacheCols, aMinMaxDegree[1] * aCacheCols,
206 aPolesArray[0], aTransientCoeffs[0]);
208 // Calculate total value
209 PLib::NoDerivativeEvalPolynomial(aParameters[0], aMinMaxDegree[0],
210 aDimension, aDimension * aMinMaxDegree[0],
211 aTransientCoeffs[0], aPoint[0]);
213 thePoint.SetCoord(aPoint[0], aPoint[1], aPoint[2]);
215 thePoint.ChangeCoord().Divide(aPoint[3]);
219 void BSplSLib_Cache::D1(const Standard_Real& theU,
220 const Standard_Real& theV,
223 gp_Vec& theTangentV) const
225 Standard_Real aNewU = theU;
226 Standard_Real aNewV = theV;
227 Standard_Real anInvU = 1.0 / mySpanLength[0];
228 Standard_Real anInvV = 1.0 / mySpanLength[1];
229 if (!myFlatKnots[0].IsNull()) // B-spline is U-periodical
230 PeriodicNormalization(myDegree[0], myFlatKnots[0]->Array1(), aNewU);
231 aNewU = (aNewU - mySpanStart[0]) * anInvU;
232 if (!myFlatKnots[1].IsNull()) // B-spline is V-periodical
233 PeriodicNormalization(myDegree[1], myFlatKnots[1]->Array1(), aNewV);
234 aNewV = (aNewV - mySpanStart[1]) * anInvV;
236 Standard_Real* aPolesArray = ConvertArray(myPolesWeights);
237 Standard_Real aPntDeriv[16]; // result storage (point and derivative coordinates)
238 for (Standard_Integer i = 0; i< 16; i++) aPntDeriv[i] = 0.0;
240 Standard_Integer aDimension = myIsRational ? 4 : 3;
241 Standard_Integer aCacheCols = myPolesWeights->RowLength();
242 Standard_Integer aMinMaxDegree[2] = {Min(myDegree[0], myDegree[1]),
243 Max(myDegree[0], myDegree[1])};
245 Standard_Real aParameters[2];
246 if (myDegree[0] > myDegree[1])
248 aParameters[0] = aNewV;
249 aParameters[1] = aNewU;
253 aParameters[0] = aNewU;
254 aParameters[1] = aNewV;
257 NCollection_LocalArray<Standard_Real> aTransientCoeffs(aCacheCols<<1); // array for intermediate results
259 // Calculate intermediate values and derivatives of bivariate polynomial along variable with maximal degree
260 PLib::EvalPolynomial(aParameters[1], 1, aMinMaxDegree[1], aCacheCols, aPolesArray[0], aTransientCoeffs[0]);
262 // Calculate a point on surface and a derivative along variable with minimal degree
263 PLib::EvalPolynomial(aParameters[0], 1, aMinMaxDegree[0], aDimension, aTransientCoeffs[0], aPntDeriv[0]);
265 // Calculate derivative along variable with maximal degree
266 PLib::NoDerivativeEvalPolynomial(aParameters[0], aMinMaxDegree[0], aDimension,
267 aMinMaxDegree[0] * aDimension, aTransientCoeffs[aCacheCols],
268 aPntDeriv[aDimension<<1]);
270 Standard_Real* aResult = aPntDeriv;
271 Standard_Real aTempStorage[12];
272 if (myIsRational) // calculate derivatives divided by weight's derivatives
274 BSplSLib::RationalDerivative(1, 1, 1, 1, aPntDeriv[0], aTempStorage[0]);
275 aResult = aTempStorage;
279 thePoint.SetCoord(aResult[0], aResult[1], aResult[2]);
280 if (myDegree[0] > myDegree[1])
282 theTangentV.SetCoord(aResult[aDimension], aResult[aDimension + 1], aResult[aDimension + 2]);
283 Standard_Integer aShift = aDimension<<1;
284 theTangentU.SetCoord(aResult[aShift], aResult[aShift + 1], aResult[aShift + 2]);
288 theTangentU.SetCoord(aResult[aDimension], aResult[aDimension + 1], aResult[aDimension + 2]);
289 Standard_Integer aShift = aDimension<<1;
290 theTangentV.SetCoord(aResult[aShift], aResult[aShift + 1], aResult[aShift + 2]);
292 theTangentU.Multiply(anInvU);
293 theTangentV.Multiply(anInvV);
297 void BSplSLib_Cache::D2(const Standard_Real& theU,
298 const Standard_Real& theV,
302 gp_Vec& theCurvatureU,
303 gp_Vec& theCurvatureV,
304 gp_Vec& theCurvatureUV) const
306 Standard_Real aNewU = theU;
307 Standard_Real aNewV = theV;
308 Standard_Real anInvU = 1.0 / mySpanLength[0];
309 Standard_Real anInvV = 1.0 / mySpanLength[1];
310 if (!myFlatKnots[0].IsNull()) // B-spline is U-periodical
311 PeriodicNormalization(myDegree[0], myFlatKnots[0]->Array1(), aNewU);
312 aNewU = (aNewU - mySpanStart[0]) * anInvU;
313 if (!myFlatKnots[1].IsNull()) // B-spline is V-periodical
314 PeriodicNormalization(myDegree[1], myFlatKnots[1]->Array1(), aNewV);
315 aNewV = (aNewV - mySpanStart[1]) * anInvV;
317 Standard_Real* aPolesArray = ConvertArray(myPolesWeights);
318 Standard_Real aPntDeriv[36]; // result storage (point and derivative coordinates)
319 for (Standard_Integer i = 0; i < 36; i++) aPntDeriv[i] = 0.0;
321 Standard_Integer aDimension = myIsRational ? 4 : 3;
322 Standard_Integer aCacheCols = myPolesWeights->RowLength();
323 Standard_Integer aMinMaxDegree[2] = {Min(myDegree[0], myDegree[1]),
324 Max(myDegree[0], myDegree[1])};
326 Standard_Real aParameters[2];
327 if (myDegree[0] > myDegree[1])
329 aParameters[0] = aNewV;
330 aParameters[1] = aNewU;
334 aParameters[0] = aNewU;
335 aParameters[1] = aNewV;
338 NCollection_LocalArray<Standard_Real> aTransientCoeffs(3 * aCacheCols); // array for intermediate results
339 // Calculating derivative to be evaluate and
340 // nulling transient coefficients when max or min derivative is less than 2
341 Standard_Integer aMinMaxDeriv[2] = {Min(2, aMinMaxDegree[0]),
342 Min(2, aMinMaxDegree[1])};
343 for (Standard_Integer i = aMinMaxDeriv[1] + 1; i < 3; i++)
345 Standard_Integer index = i * aCacheCols;
346 for (Standard_Integer j = 0; j < aCacheCols; j++)
347 aTransientCoeffs[index++] = 0.0;
350 // Calculate intermediate values and derivatives of bivariate polynomial along variable with maximal degree
351 PLib::EvalPolynomial(aParameters[1], aMinMaxDeriv[1], aMinMaxDegree[1],
352 aCacheCols, aPolesArray[0], aTransientCoeffs[0]);
354 // Calculate a point on surface and a derivatives along variable with minimal degree
355 PLib::EvalPolynomial(aParameters[0], aMinMaxDeriv[0], aMinMaxDegree[0],
356 aDimension, aTransientCoeffs[0], aPntDeriv[0]);
358 // Calculate derivative along variable with maximal degree and mixed derivative
359 PLib::EvalPolynomial(aParameters[0], 1, aMinMaxDegree[0], aDimension,
360 aTransientCoeffs[aCacheCols], aPntDeriv[3 * aDimension]);
362 // Calculate second derivative along variable with maximal degree
363 PLib::NoDerivativeEvalPolynomial(aParameters[0], aMinMaxDegree[0], aDimension,
364 aMinMaxDegree[0] * aDimension, aTransientCoeffs[aCacheCols<<1],
365 aPntDeriv[6 * aDimension]);
367 Standard_Real* aResult = aPntDeriv;
368 Standard_Real aTempStorage[36];
369 if (myIsRational) // calculate derivatives divided by weight's derivatives
371 BSplSLib::RationalDerivative(2, 2, 2, 2, aPntDeriv[0], aTempStorage[0]);
372 aResult = aTempStorage;
376 thePoint.SetCoord(aResult[0], aResult[1], aResult[2]);
377 if (myDegree[0] > myDegree[1])
379 theTangentV.SetCoord(aResult[aDimension], aResult[aDimension + 1], aResult[aDimension + 2]);
380 Standard_Integer aShift = aDimension<<1;
381 theCurvatureV.SetCoord(aResult[aShift], aResult[aShift + 1], aResult[aShift + 2]);
382 aShift += aDimension;
383 theTangentU.SetCoord(aResult[aShift], aResult[aShift + 1], aResult[aShift + 2]);
384 aShift += aDimension;
385 theCurvatureUV.SetCoord(aResult[aShift], aResult[aShift + 1], aResult[aShift + 2]);
386 aShift += (aDimension << 1);
387 theCurvatureU.SetCoord(aResult[aShift], aResult[aShift + 1], aResult[aShift + 2]);
391 theTangentU.SetCoord(aResult[aDimension], aResult[aDimension + 1], aResult[aDimension + 2]);
392 Standard_Integer aShift = aDimension<<1;
393 theCurvatureU.SetCoord(aResult[aShift], aResult[aShift + 1], aResult[aShift + 2]);
394 aShift += aDimension;
395 theTangentV.SetCoord(aResult[aShift], aResult[aShift + 1], aResult[aShift + 2]);
396 aShift += aDimension;
397 theCurvatureUV.SetCoord(aResult[aShift], aResult[aShift + 1], aResult[aShift + 2]);
398 aShift += (aDimension << 1);
399 theCurvatureV.SetCoord(aResult[aShift], aResult[aShift + 1], aResult[aShift + 2]);
401 theTangentU.Multiply(anInvU);
402 theTangentV.Multiply(anInvV);
403 theCurvatureU.Multiply(anInvU * anInvU);
404 theCurvatureV.Multiply(anInvV * anInvV);
405 theCurvatureUV.Multiply(anInvU * anInvV);