HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_Spline.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_Spline.h ( UT Library, C++)
7  *
8  * COMMENTS: Simple spline class.
9  *
10  * The linear and catmull-rom splines expect a parametric evaluation coordinate
11  * between 0 and 1.
12  */
13 
14 #ifndef __UT_Spline__
15 #define __UT_Spline__
16 
17 #include "UT_API.h"
18 #include "UT_Array.h"
19 #include "UT_BoundingBox.h"
20 #include "UT_Color.h"
21 #include "UT_Matrix4.h"
22 #include "UT_RootFinder.h"
23 #include "UT_Vector3.h"
24 #include "UT_Vector4.h"
25 #include <VM/VM_SSEFunc.h>
26 #include <SYS/SYS_Floor.h>
27 #include <SYS/SYS_Inline.h>
28 #include <SYS/SYS_Math.h>
29 #include <SYS/SYS_Types.h>
30 
31 typedef enum {
32  // These splines all work on uniform keys.
33  UT_SPLINE_CONSTANT, // Constant between keys
34  UT_SPLINE_LINEAR, // Linear interpolation between keys
35  UT_SPLINE_CATMULL_ROM, // Catmull-Rom Cardinal Spline
36  UT_SPLINE_MONOTONECUBIC, // Monotone Cubic Hermite Spline
37 
38  // This interpolation works a little differently. It takes a set of scalar
39  // values, and "fits" the parametric coordinate into the keys. That is, it
40  // performs the binary search to find where the parametric coordinate maps,
41  // then, it performs a linear interpolation between the two nearest key
42  // values to figure out what the coordinate should be.
44 
45  UT_SPLINE_BEZIER, // Bezier Curve
46  UT_SPLINE_BSPLINE, // B-Spline
47  UT_SPLINE_HERMITE, // Hermite Spline
49 
50 UT_API extern const char * UTnameFromSplineBasis(UT_SPLINE_BASIS basis);
52 
53 
55 {
56 public:
57  /// Evaluates an open spline at a given location.
58  /// The given CV list must have 4 elements in it!
59  /// The cvs should be for the current segment, and t in [0, 1]
60  template <typename T>
61  static T evalOpen(const T *cvs, float t)
62  {
63  UT_Matrix4 weightmatrix = getOpenWeights();
64  float t2 = t*t;
65  float t3 = t2*t;
66  UT_Vector4 powers(1, t, t2, t3);
67 
68  UT_Vector4 weights = colVecMult(weightmatrix, powers);
69 
70  T value;
71 
72  value = cvs[0] * weights[0];
73  value += cvs[1] * weights[1];
74  value += cvs[2] * weights[2];
75  value += cvs[3] * weights[3];
76 
77  return value;
78  }
79 
80  /// Evaluates a range of t values in uniform increasing manner.
81  /// The cvs list should have 3 + nseg entries.
82  template <typename T>
83  static void evalRangeOpen(T *results, const T *cvs, float start_t, float step_t, int len_t, int nseg)
84  {
85  int curseg;
86  curseg = SYSfastFloor(start_t);
87  curseg = SYSclamp(curseg, 0, nseg-1);
88  float t = start_t - curseg;
89 
90  for (int i = 0; i < len_t; i++)
91  {
92  results[i] = evalOpen(&cvs[curseg], t);
93  t += step_t;
94  if (t > 1)
95  {
96  while (curseg < nseg-1)
97  {
98  curseg++;
99  t -= 1;
100  if (t <= 1)
101  break;
102  }
103  }
104  }
105  }
106 
107  /// Evaluates a closed spline at the given location. The given
108  /// cv list must have 4 elements. Whether we are end interpolated or
109  /// not depends on which segment this represents. The cvs list
110  /// should be the cvs for the current segment and t in [0, 1]
111  template <typename T>
112  static T evalClosed(const T *cvs, float t, int seg, int nseg, bool deriv = false)
113  {
114  UT_Matrix4 weightmatrix = getClosedWeights(seg, nseg, deriv);
115  float t2 = t*t;
116  float t3 = t2*t;
117  UT_Vector4 powers(1, t, t2, t3);
118 
119  UT_Vector4 weights = colVecMult(weightmatrix, powers);
120 
121  T value;
122 
123  value = cvs[0] * weights[0];
124  value += cvs[1] * weights[1];
125  value += cvs[2] * weights[2];
126  value += cvs[3] * weights[3];
127 
128  return value;
129  }
130 
131  /// Evaluates a range of t values in uniform increasing manner.
132  /// The cvs list should have 3 + nseg entries.
133  template <typename T>
134  static void evalRangeClosed(T *results, const T *cvs, float start_t, float step_t, int len_t, int nseg, bool deriv = false)
135  {
136  int curseg;
137  curseg = SYSfastFloor(start_t);
138  curseg = SYSclamp(curseg, 0, nseg-1);
139  float t = start_t - curseg;
140 
141  for (int i = 0; i < len_t; i++)
142  {
143  results[i] = evalClosed(&cvs[curseg], t, curseg, nseg, deriv);
144  t += step_t;
145  if (t > 1)
146  {
147  while (curseg < nseg-1)
148  {
149  curseg++;
150  t -= 1;
151  if (t <= 1)
152  break;
153  }
154  }
155  }
156  }
157 
158  template <typename T>
159  static T evalSubDStart(const T *cvs, float t)
160  {
161  // First segment is (1 - t + (1/6)t^3)*P0 + (t - (1/3)*t^3)*P1 + ((1/6)t^3)*P2
162  const float onesixth = 0.16666666666666667f;
163  float onesixtht3 = onesixth*t*t*t;
164  float w0 = 1 - t + onesixtht3;
165  float w1 = t - 2*onesixtht3;
166  float w2 = onesixtht3;
167 
168  T value = w0*cvs[0];
169  value += w1*cvs[1];
170  value += w2*cvs[2];
171  return value;
172  }
173 
174  template <typename T>
175  static T evalSubDEnd(const T *cvs, float t)
176  {
177  // Reverse t relative to evalSubDStart
178  t = 1.0f-t;
179 
180  const float onesixth = 0.16666666666666667f;
181  float onesixtht3 = onesixth*t*t*t;
182  float w0 = 1 - t + onesixtht3;
183  float w1 = t - 2*onesixtht3;
184  float w2 = onesixtht3;
185 
186  // Also reverse point order relative to evalSubDStart
187  T value = w0*cvs[2];
188  value += w1*cvs[1];
189  value += w2*cvs[0];
190  return value;
191  }
192 
193  template <float (func)(const float *,float)>
195  UT_BoundingBox &box, const UT_Vector3 *cvs,
196  const UT_Vector3 &a, const UT_Vector3 &b, const UT_Vector3 &c,
197  float rootmin, float rootmax)
198  {
199  // If the value of the quadratic has equal signs at zero
200  // and one, AND the derivative has equal signs at zero and one,
201  // it can't have crossed zero between zero and one, so we
202  // can skip the root find in that case. The other rejection
203  // case of a negative b^2-4ac is already checked by
204  // UT_RootFinder, because it doesn't depend on the range
205  // limits.
206 
207  // a+b+c = value of quadratic at one
208  // (a+b+c)*c > 0 iff signs of values are equal
209  UT_Vector3 abc = a + b + c;
210  abc *= c;
211  // 2a+b = derivative of quadratic at one
212  // (2a+b)*b > 0 iff signs of derivatives are equal
213  UT_Vector3 b2a = a * 2.0F + b;
214  b2a *= b;
215 
216  for (int DIM = 0; DIM < 3; DIM++)
217  {
218  // No chance of crossing zero in case descirbed above
219  // NOTE: The abc == 0 case can be rejected, because we
220  // already did enlargeBounds on both p values.
221  // The abc > 0 && b2a == 0 case can be rejected,
222  // because the peak of the quadratic has the same
223  // sign as the rest, so never crosses zero.
224  if (abc[DIM] >= 0 && b2a[DIM] >= 0)
225  continue;
226 
227  float t1, t2;
228  int nroots = UT_RootFinder::quadratic(a[DIM], b[DIM], c[DIM], t1, t2);
229  if (nroots == 0)
230  continue;
231 
232  float fcvs[4];
233  fcvs[0] = cvs[0][DIM];
234  fcvs[1] = cvs[1][DIM];
235  fcvs[2] = cvs[2][DIM];
236  fcvs[3] = cvs[3][DIM];
237 
238  // Add any minima/maxima to the bounding box
239  if (t1 > rootmin && t1 < rootmax)
240  {
241  float v = func(fcvs, t1);
242  box.vals[DIM][0] = SYSmin(box.vals[DIM][0], v);
243  box.vals[DIM][1] = SYSmax(box.vals[DIM][1], v);
244  }
245  if (nroots == 2 && t2 > rootmin && t2 < rootmax)
246  {
247  float v = func(fcvs, t2);
248  box.vals[DIM][0] = SYSmin(box.vals[DIM][0], v);
249  box.vals[DIM][1] = SYSmax(box.vals[DIM][1], v);
250  }
251  }
252  }
253 
254  /// Enlarges box by any minima/maxima of the cubic curve defined by 4 cvs, that lie between rootmin and rootmax.
255  /// NOTE: This must be defined below so that it doesn't instantiate evalOpen before its specialization below.
256  static inline void enlargeBoundingBoxOpen(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax);
257 
258  /// Enlarges box by any minima/maxima of the cubic curve defined by 3 cvs, that lie between rootmin and rootmax.
259  /// The curve in this case is the start segment of a subdivision curve.
260  static inline void enlargeBoundingBoxSubDStart(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax);
261 
262  /// Enlarges box by any minima/maxima of the cubic curve defined by cvs, that lie between rootmin and rootmax.
263  /// The curve in this case is the end segment of a subdivision curve.
264  static inline void enlargeBoundingBoxSubDEnd(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax);
265 
266  /// Returns the weights for a power-basis evaluation of a segment.
267  /// The t values should be normalized inside the segment.
268  /// The format is (1, t, t^2, t^3), and colVecMult.
269  /// Assumes uniform knots.
271  {
272  return UT_Matrix4( 1/6., -3/6., 3/6., -1/6.,
273  4/6., 0/6., -6/6., 3/6.,
274  1/6., 3/6., 3/6., -3/6.,
275  0/6., 0/6., 0/6., 1/6. );
276  }
278  {
279  return UT_Matrix4( 1/6., 4/6., 1/6., 0/6.,
280  -3/6., 0/6., 3/6., 0/6.,
281  3/6., -6/6., 3/6., 0/6.,
282  -1/6., 3/6., -3/6., 1/6. );
283  }
284 
285  template<typename T>
286  static T evalMatrix(const UT_Matrix4 &basis, const T cvs[4], float t)
287  {
288  float t2 = t*t;
289  UT_Vector4 tpow(1.0f, t, t2, t2*t);
290 
291  UT_Vector4 coeff = colVecMult(basis, tpow);
292 
293  T val = cvs[0]*coeff[0] + cvs[1]*coeff[1] + cvs[2]*coeff[2] + cvs[3]*coeff[3];
294 
295  return val;
296  }
297 
298  /// This function is for evaluating a subdivision curve that is open.
299  /// For simplicitly, the parameter range is [0,1].
300  /// It's implemented in a way that maximizes stability
301  /// and readability, not necessarily performance.
302  template<typename T>
303  static T evalSubDCurve(const T *cvs, float t, int npts, bool deriv=false)
304  {
305  T p0;
306  T diff; // p1-p0
307  T c0; // Average of neighbours of p0, minus p0
308  T c1; // Average of neighbours of p1, minus p1
309 
310  // npts-1 segments, since npts points in whole curve
311  t *= (npts-1);
312 
313  int i = int(t);
314 
315  if (i < 0)
316  i = 0;
317  else if (i > npts-1)
318  i = npts-1;
319 
320  t -= i;
321  p0 = cvs[i];
322  diff = cvs[i+1]-cvs[i];
323 
324  if (i > 0)
325  c0 = 0.5*(cvs[i-1]+cvs[i+1]) - cvs[i];
326  else
327  c0 = 0;
328 
329  if (i < npts-1)
330  c1 = 0.5*(cvs[i]+cvs[i+2]) - cvs[i+1];
331  else
332  c1 = 0;
333 
334  float ti = 1-t;
335  if (!deriv)
336  {
337  float t3 = t*t*t/3;
338  float ti3 = ti*ti*ti/3;
339  // Order of addition should reduce roundoff in common cases.
340  return p0 + (diff*t + (c0*ti3 + c1*t3));
341  }
342  else
343  {
344  float t2 = t*t;
345  float ti2 = ti*ti;
346  // Order of addition should reduce roundoff in common cases.
347  return diff + (c1*t2 - c0*ti2);
348  }
349  }
350 
351  /// Basis for first segment of subd curve. Evaluation is:
352  /// [p[0] p[1] p[2] p[3]] * theSubDFirstBasis * [1 t t^2 t^3]^T
353  /// The last segment can be evaluated as: (NOTE the reversed order and 1-t)
354  /// [p[n-1] p[n-2] p[n-3] p[n-4]] * theSubDFirstBasis * [1 (1-t) (1-t)^2 (1-t)^3]^T
355  /// FYI: The last row is all zero, since it only depends on 3 points.
357 
358  /// Basis for derivative of first segment of subd curve. Evaluation is:
359  /// [p[0] p[1] p[2] p[3]] * theSubDFirstDerivBasis * [1 t t^2 t^3]^T
360  /// The last segment derivative can be evaluated as: (NOTE the reversed order and 1-t)
361  /// [p[n-1] p[n-2] p[n-3] p[n-4]] * theSubDFirstDerivBasis * [1 (1-t) (1-t)^2 t^3]^T
362  /// FYI: The last row is all zero, since it only depends on 3 points.
363  /// The last column is all zero, since the derivative has no cubic component.
365 
366  /// Basis for middle segment of subd curve or uniform, open, cubic NURBS.
367  /// Evaluation is:
368  /// [p[-1] p[0] p[1] p[2]] * theOpenBasis * [1 t t^2 t^3]^T
369  static const UT_Matrix4 theOpenBasis;
370 
371  /// Basis for derivative of middle segment of subd curve or uniform, open, cubic NURBS.
372  /// Evaluation is:
373  /// [p[-1] p[0] p[1] p[2]] * theOpenDerivBasis * [1 t t^2 t^3]^T
374  /// FYI: The last column is all zero, since the derivative has no cubic component.
376 
377  /// Basis for first segment of interpolating curve. Evaluation is:
378  /// [p[0] p[1] p[2] p[3]] * theInterpFirstBasis * [1 t t^2 t^3]^T
379  /// The last segment can be evaluated as: (NOTE the reversed order and 1-t)
380  /// [p[n-1] p[n-2] p[n-3] p[n-4]] * theInterpFirstBasis * [1 (1-t) (1-t)^2 (1-t)^3]^T
381  /// FYI: The last row is all zero, since it only depends on 3 points.
383 
384  /// Basis for middle segment of interpolating curve. Evaluation is:
385  /// [p[-1] p[0] p[1] p[2]] * theInterpBasis * [1 t t^2 t^3]^T
387 
388  /// Basis for Hermite cubic curve using two values (p) and two derivatives (v):
389  /// Evaluation is:
390  /// [p[0] p[1] v[0] v[1]] * theHermiteBasis * [1 t t^2 t^3]^T
392 
393  /// Basis for derivative of Hermite cubic curve using two values (p) and two derivatives (v):
394  /// Evaluation is:
395  /// [p[0] p[1] v[0] v[1]] * theHermiteDerivBasis * [1 t t^2 t^3]^T
396  /// FYI: The last column is all zero, since the derivative has no cubic component.
398 
399  /// Uniform knots with closed end conditions. seg is which segment
400  /// is being evaluates, nseg is the total. nseg should be
401  /// number of vertices minus three as we have cubics.
402  static UT_Matrix4 getClosedWeights(int seg, int nseg, bool deriv = false)
403  {
404  // these matrices come from $GEO/support/computespline.py
405  // which computes the power basis form of end-interpolated
406  // uniform bsplines.
407 
408  if (deriv == false)
409  {
410  if (nseg <= 1)
411  {
412  // Bezier.
413  return UT_Matrix4( 1, -3, 3, -1,
414  0, 3, -6, 3,
415  0, 0, 3, -3,
416  0, 0, 0, 1 );
417  }
418  else if (nseg == 2)
419  {
420  // 0, 0, 0, 1, 2, 2, 2,
421  if (seg == 0)
422  return UT_Matrix4( 1, -3, 3, -1,
423  0, 3, -4.5, 1.75,
424  0, 0, 1.5, -1,
425  0, 0, 0, 0.25 );
426  else
427  return UT_Matrix4( .25, -.75, .75, -0.25,
428  0.5, 0, -1.5, 1,
429  0.25, 0.75, 0.75, -1.75,
430  0, 0, 0, 1 );
431  }
432  else if (nseg == 3)
433  {
434  // 0, 0, 0, 1, 2, 3, 3, 3
435  if (seg == 0)
436  return UT_Matrix4( 1, -3, 3, -1,
437  0, 3, -4.5, 1.75,
438  0, 0, 1.5, -11/12.,
439  0, 0, 0, 1/6.);
440  else if (seg == 1)
441  return UT_Matrix4( .25, -.75, .75, -0.25,
442  7/12., 0.25, -1.25, 7/12.,
443  1/6., 0.5, 0.5, -7/12.,
444  0, 0, 0, 0.25 );
445  else
446  return UT_Matrix4( 1/6., -.5, .5, -1/6.,
447  7/12., -0.25, -1.25, 11/12.,
448  0.25, 0.75, 0.75, -1.75,
449  0, 0, 0, 1 );
450  }
451  else
452  {
453  // Either on an end, or in the middle
454  if (seg >= 2 && seg < nseg-2)
455  return UT_Matrix4( 1/6., -3/6., 3/6., -1/6.,
456  4/6., 0/6., -6/6., 3/6.,
457  1/6., 3/6., 3/6., -3/6.,
458  0/6., 0/6., 0/6., 1/6. );
459  else if (seg == 0)
460  return UT_Matrix4( 1, -3, 3, -1,
461  0, 3, -4.5, 1.75,
462  0, 0, 1.5, -11/12.,
463  0, 0, 0, 1/6. );
464  else if (seg == 1)
465  return UT_Matrix4( 0.25, -0.75, 0.75, -.25,
466  7/12., 0.25, -1.25, 7/12.,
467  1/6., 0.5, 0.5, -0.5,
468  0, 0, 0, 1/6. );
469  else if (seg == nseg-2)
470  return UT_Matrix4( 1/6., -3/6., 3/6., -1/6.,
471  2/3., 0, -1, 0.5,
472  1/6., 0.5, 0.5, -7/12.,
473  0, 0, 0, 0.25 );
474  else // if (seg == nseg-1)
475  return UT_Matrix4( 1/6., -3/6., 3/6., -1/6.,
476  7/12., -.25, -1.25, 11/12.,
477  0.25, 0.75, 0.75, -1.75,
478  0, 0, 0, 1 );
479  }
480  }
481  else
482  {
483  if (nseg <= 1)
484  {
485  // Bezier.
486  return UT_Matrix4( -3, 6, -3, 0,
487  3, -12, 9, 0,
488  0, 6, -9, 0,
489  0, 0, 3, 0 );
490  }
491  else if (nseg == 2)
492  {
493  // 0, 0, 0, 1, 2, 2, 2,
494  if (seg == 0)
495  return UT_Matrix4( -3, 6, -3, 0,
496  3, -9, 5.25, 0,
497  0, 3, -3, 0,
498  0, 0, 0.75, 9 );
499  else
500  return UT_Matrix4( -.75, 1.5, -0.75, 0,
501  0, -3, 3, 0,
502  0.75, 1.5, -5.25, 0,
503  0, 0, 3, 0 );
504  }
505  else if (nseg == 3)
506  {
507  // 0, 0, 0, 1, 2, 3, 3, 3
508  if (seg == 0)
509  return UT_Matrix4( -3, 6, -3, 0,
510  3, -9, 5.25, 0,
511  0, 3, -11/4., 0,
512  0, 0, .5, 0);
513  else if (seg == 1)
514  return UT_Matrix4( -.75, 1.5, -0.75, 0,
515  0.25, -2.5, 7/4., 0,
516  0.5, 1, -7/4., 0,
517  0, 0, 0.75, 0);
518  else
519  return UT_Matrix4( -.5, 1, -0.5, 0,
520  -0.25, -2.5, 11/4., 0,
521  0.75, 1.5, -5.25, 0,
522  0, 0, 3, 0);
523  }
524  else
525  {
526  // Either on an end, or in the middle
527  if (seg >= 2 && seg < nseg-2)
528  return UT_Matrix4( -3/6., 1.0, -0.5, 0,
529  0/6., -2.0, 1.5, 0,
530  3/6., 1.0, -1.5, 0,
531  0/6., 0, 0.5, 0);
532  else if (seg == 0)
533  return UT_Matrix4( -3, 6, -3, 0,
534  3, -9, 5.25, 0,
535  0, 3, -11/4., 0,
536  0, 0, 0.5, 0);
537  else if (seg == 1)
538  return UT_Matrix4( -0.75, 1.5, -.75, 0,
539  0.25, -2.5, 7/4., 0,
540  0.5, 1, -1.5, 0,
541  0, 0, 0.5, 0 );
542  else if (seg == nseg-2)
543  return UT_Matrix4( -3/6., 1, -0.5, 0,
544  0, -2, 1.5, 0,
545  0.5, 1, -7/4., 0,
546  0, 0, 0.75, 0 );
547  else // if (seg == nseg-1)
548  return UT_Matrix4(-3/6., 1, -.5, 0,
549  -.25, -2.5, 11/4., 0,
550  0.75, 1.5, -5.25, 0,
551  0, 0, 3, 0);
552  }
553  }
554  }
555  static UT_Matrix4 getClosedWeightsTranspose(int seg, int nseg, bool deriv = false)
556  {
557  if (deriv == false)
558  {
559  // these matrices come from $GEO/support/computespline.py
560  // which computes the power basis form of end-interpolated
561  // uniform bsplines.
562  if (nseg <= 1)
563  {
564  // Bezier.
565  return UT_Matrix4( 1, 0, 0, 0,
566  -3, 3, 0, 0,
567  3, -6, 3, 0,
568  -1, 3, -3, 1 );
569  }
570  else if (nseg == 2)
571  {
572  // 0, 0, 0, 1, 2, 2, 2,
573  if (seg == 0)
574  return UT_Matrix4( 1, 0, 0, 0,
575  -3, 3, 0, 0,
576  3, -4.5, 1.5, 0,
577  -1, 1.75, -1, 0.25 );
578  else
579  return UT_Matrix4( .25, .5, .25, 0,
580  -.75, 0, .75, 0,
581  0.75, -1.5, 0.75, 0,
582  -0.25, 1, -1.75, 1 );
583  }
584  else if (nseg == 3)
585  {
586  // 0, 0, 0, 1, 2, 3, 3, 3
587  if (seg == 0)
588  return UT_Matrix4( 1, 0, 0, 0,
589  -3, 3, 0, 0,
590  3,-4.5,1.5, 0,
591  -1,1.75,-11/12.,1/6. );
592  else if (seg == 1)
593  return UT_Matrix4( 0.25, 7/12., 1/6., 0,
594  -.75, 0.25, 0.5, 0,
595  .75,-1.25, 0.5, 0,
596  -.25,7/12.,-7/12.,0.25 );
597  else
598  return UT_Matrix4( 1/6., 7/12., 0.25, 0,
599  -.5, -0.25, 0.75, 0,
600  .5, -1.25, 0.75, 0,
601  -1/6.,11/12.,-1.75, 1 );
602 
603  }
604  else
605  {
606  // Either on an end, or in the middle
607  if (seg >= 2 && seg < nseg-2)
608  return UT_Matrix4( 1/6., 4/6., 1/6., 0/6.,
609  -3/6., 0/6., 3/6., 0/6.,
610  3/6., -6/6., 3/6., 0/6.,
611  -1/6., 3/6., -3/6., 1/6. );
612  else if (seg == 0)
613  return UT_Matrix4( 1, 0, 0, 0,
614  -3, 3, 0, 0,
615  3,-4.5,1.5, 0,
616  -1,1.75,-11/12., 1/6. );
617  else if (seg == 1)
618  return UT_Matrix4( 0.25, 7/12., 1/6., 0,
619  -0.75, 0.25, 0.5, 0,
620  0.75,-1.25, 0.5, 0,
621  -0.25,7/12., -0.5, 1/6. );
622  else if (seg == nseg-2)
623  return UT_Matrix4( 1/6., 2/3., 1/6., 0,
624  -3/6., 0, 0.5, 0,
625  3/6., -1, 0.5, 0,
626  -1/6., 0.5,-7/12.,0.25 );
627  else // if (seg == nseg-1)
628  return UT_Matrix4( 1/6., 7/12., 0.25, 0,
629  -3/6., -.25, 0.75, 0,
630  3/6.,-1.25, 0.75, 0,
631  -1/6.,11/12.,-1.75, 1 );
632  }
633  }
634  else
635  {
636  if (nseg <= 1)
637  {
638  // Bezier.
639  return UT_Matrix4(-3, 3, 0, 0,
640  6, -12, 6, 0,
641  -3, 9, -9, 3,
642  0, 0, 0, 0);
643  }
644  else if (nseg == 2)
645  {
646  // 0, 0, 0, 1, 2, 2, 2,
647  if (seg == 0)
648  return UT_Matrix4(-3, 3, 0, 0,
649  6, -9, 3, 0,
650  -3, 5.25, -3, 0.75,
651  0, 0, 0, 0);
652  else
653  return UT_Matrix4(-.75, 0, .75, 0,
654  1.5, -3, 1.5, 0,
655  -0.75, 3, -5.25, 3,
656  0, 0, 0, 0);
657  }
658  else if (nseg == 3)
659  {
660  // 0, 0, 0, 1, 2, 3, 3, 3
661  if (seg == 0)
662  return UT_Matrix4(-3, 3, 0, 0,
663  6, -9, 3, 0,
664  -3, 5.25, -11/4., .5,
665  0, 0, 0, 0);
666  else if (seg == 1)
667  return UT_Matrix4(-.75, 0.25, 0.5, 0,
668  1.5,-2.5, 1, 0,
669  -.75,7/4.,-7/4.,0.75,
670  0, 0, 0, 0);
671  else
672  return UT_Matrix4(-.5, -0.25, 0.75, 0,
673  1, -2.5, 1.5, 0,
674  -.5, 11/4., -5.25, 3,
675  0, 0, 0, 0);
676 
677  }
678  else
679  {
680  // Either on an end, or in the middle
681  if (seg >= 2 && seg < nseg-2)
682  return UT_Matrix4(-3/6., 0/6., 3/6., 0/6.,
683  1, -2, 1, 0,
684  -0.5, 1.5, -1.5, 0.5,
685  0, 0, 0, 0);
686  else if (seg == 0)
687  return UT_Matrix4(-3, 3, 0, 0,
688  6, -9, 3, 0,
689  -3, 5.25, -11/4., .5,
690  0, 0, 0, 0);
691  else if (seg == 1)
692  return UT_Matrix4(-0.75, 0.25, 0.5, 0,
693  1.5, -2.5, 1, 0,
694  -0.75, 7/4., -1.5, .5,
695  0, 0, 0, 0);
696 
697  else if (seg == nseg-2)
698  return UT_Matrix4(-3/6., 0, 0.5, 0,
699  1, -2, 1, 0,
700  -0.5, 1.5, -7/4., 0.75,
701  0, 0, 0, 0);
702  else // if (seg == nseg-1)
703  return UT_Matrix4(-3/6., -.25, 0.75, 0,
704  1, -2.5, 1.5, 0,
705  -.5, 11/4.,-5.25, 3,
706  0, 0, 0, 0);
707  }
708  }
709  }
710 };
711 
712 
713 /// The Linear & Catmull-Rom splines expect a parametric coordinate for
714 /// evaluation between 0 and 1. The Catmull-Rom spline requires additional
715 /// key values at the beginning and end of the spline to evaluate the slopes
716 /// of the Hermite spline properly.
717 ///
718 /// The LinearSolve only works on scalar values. It will compute the
719 /// parametric coordinate associated with the value passed in. This can be
720 /// used to simulate non-uniform keys on the spline.
722 {
723 public:
724  UT_Spline();
725  ~UT_Spline();
726 
727  /// Return the amount of memory owned by this UT_Spline in bytes
728  int64 getMemoryUsage(bool inclusive) const;
729 
730  /// Query the basis or knot length of the spline
731  UT_SPLINE_BASIS getGlobalBasis() const { return myGlobalBasis; }
732  int getVectorSize() const { return myVectorSize; }
733  int getKnotLength() const { return myKnotLength; }
734  fpreal64 getTension() const { return myTension; }
735 
737  { myGlobalBasis = b; }
738 
739  /// Sets the spline's global basis to b, and also set the basis
740  /// of every knot to b.
741  void setAllBases(UT_SPLINE_BASIS b);
742 
743  /// Construction of the spline object. All values are initialized to 0.
744  /// Warning, calling setSize() will clear all existing values.
745  void setSize(int nkeys, int vector_size);
746  /// Cubic splines may have a "tension". The tension defaults to 0.5 which
747  /// results in Catmull-Rom splines.
748  void setTension(fpreal64 t);
749 
750  /// Once the spline has been constructed, the values need to be set.
751  /// It is possible to change values between evaluations.
752  // @{
753  void setValue(int key, const fpreal32 *value, int size);
754  void setValue(int key, const fpreal64 *value, int size);
755  // @}
756 
757  /// Set the basis for the given key index.
758  /// This will also set the global basis.
759  void setBasis(int key, UT_SPLINE_BASIS b);
760 
761  /// Evaluate the spline using the global basis.
762  /// When interp_space is not UT_RGB, then values are treated as UT_RGBA
763  /// and converted into the desired color space before being interpolated.
764  /// The result is always returned as UT_RGBA.
765  ///
766  /// If order == 0, the value is returned.
767  /// If order == 1, the derivative with respect to t is returned.
768  // @{
769  bool evaluate(fpreal t, fpreal32 *result, int size,
770  UT_ColorType interp_space, int order = 0) const;
771  bool evaluate(fpreal t, fpreal64 *result, int size,
772  UT_ColorType interp_space, int order = 0) const;
773  // @}
774 
775  /// Evaluate the spline using multiple basis types depending on t.
776  /// Also unlike evaluate(), evaluateMulti() doesn't require extra keys for
777  /// Catmull-Rom on the ends. It always evaluates using a 0 slope at the
778  /// ends.
779  ///
780  /// If order == 0, the value is returned.
781  /// If order == 1, the derivative with respect to t is returned.
782  // @{
783  bool evaluateMulti(fpreal t, fpreal32 *result, int n,
784  UT_ColorType interp_space,
785  int knot_segment_hint = -1,
786  int order = 0) const;
787  bool evaluateMulti(fpreal t, fpreal64 *result, int n,
788  UT_ColorType interp_space,
789  int knot_segment_hint = -1,
790  int order = 0) const;
791  // @}
792 
793  /// Return _monotone_ cubic Hermite slopes at the current knot given the
794  /// previous and next knots.
795  // @{
796  /// Fritsch-Carlson (local) method.
797  /// It gives relatively good looking C1 curves but might not give a C2
798  /// solution even if it exists.
799  /// 1. Fritsch, F.N., Carlson, R.E., Monotone piecewise cubic interpolant,
800  /// SIAM J. Numer. Anal., 17(1980), 238-246.
801  static fpreal64 getMonotoneSlopeFC(fpreal64 v_cur, fpreal64 t_cur,
802  fpreal64 v_prev, fpreal64 t_prev,
803  fpreal64 v_next, fpreal64 t_next);
804  /// Paul Kupan's method.
805  /// Similar to Fritsch-Carlson method except it gives more visually
806  /// pleasing results when the intervals next to the knot are uneven.
807  /// 2. Kupan, P.A., Monotone Interpolant Built with Slopes Obtained by
808  /// Linear Combination, Studia Universitatis Babes-Bolyai Mathematica,
809  /// 53(2008), 59-66.
810  static fpreal64 getMonotoneSlopePA(fpreal64 v_cur, fpreal64 t_cur,
811  fpreal64 v_prev, fpreal64 t_prev,
812  fpreal64 v_next, fpreal64 t_next);
813  // @}
814 
815  /// Given the position within the two knots and the first knot index
816  /// number, normalize the position from knot-length domain to unit domain.
817  fpreal normalizeParameter(fpreal parm, fpreal t0, fpreal t1,
818  int t0_index) const;
819 
820  // Given parm in [0,1] interval in which CVs are at 'knots' values,
821  // find the parameter t in [0,1] interval in which CVs are evenly spaced.
822  // The returned reparameterized value is calculated in such a way that it
823  // yields smooth curve at CVs (unlike normalizeation that uses linear
824  // interpolation that yields tangent disconinuity at CVs).
825  // If 'parm_knot_segment' is given, this function sets it to the index
826  // of the knot segment into which the parm falls; it can be used as a hint
827  // to the evaluateMulti() method, especially for b-splines that may remap
828  // parameters to an adjacent segment (for continuity at end control points).
829  // If 'order' == 1, the derivative of t with respect to parm is returned.
830  fpreal solveSpline(fpreal parm,
831  const UT_Array<fpreal> &knots,
832  int *parm_knot_segment = nullptr,
833  int order = 0) const;
834 
835  /// Given the keys surrounding a channel segment, evaluate it as cubic
836  /// hermite spline. This function assumes dt > 0.
837  /// kt is [0,1] which maps over the dt time interval.
838  /// The `order`-th derivative with respect to kt is returned.
839  /// In particular, for order == 0 (default), the value is returned.
840  template <typename T>
841  static T evalCubic(T kt, T dt, T iv, T im, T ov, T om, int order = 0)
842  {
843  T x0 = iv;
844  T x1 = im*dt;
845  T x3 = 2*(iv - ov) + (im + om)*dt;
846  T x2 = ov - iv - im*dt - x3;
847  switch (order)
848  {
849  case 0:
850  return x0 + kt*(x1 + kt*(x2 + kt*x3));
851  case 1:
852  return x1 + kt*(2*x2 + kt*3*x3);
853  case 2:
854  return 2*x2 + kt*6*x3;
855  case 3:
856  return 6*x3;
857  default:
858  return 0;
859  }
860  }
861 
862  /// Given the keys surrounding a channel segment, evaluate it as
863  /// a quintic hermite spline. See evalCubic above.
864  template <typename T>
865  static T evalQuintic(T kt, T dt, T iv, T im, T ia, T ov, T om, T oa, int order = 0)
866  {
867  fpreal x0 = iv;
868  fpreal x1 = im * dt;
869  fpreal x2 = (fpreal).5 * ia * dt;
870  fpreal b0 = ov - (x0 + x1 + x2);
871  fpreal b1 = om * dt - (x1 + ia * dt);
872  fpreal b2 = oa * dt - ia * dt;
873  fpreal x5 = (fpreal).5 * (b2 - 6 * b1 + 12 * b0);
874  fpreal x4 = (fpreal).25 * (b2 - 2 * b1 - 10 * x5);
875  fpreal x3 = b0 - x4 - x5;
876 
877  switch (order)
878  {
879  case 0:
880  return x0 + kt*(x1 + kt*(x2 + kt*(x3 + kt*(x4 + kt*x5))));
881  case 1:
882  return x1 + kt*(2*x2 + kt*(3*x3 + kt*(4*x4 + kt*5*x5)));
883  case 2:
884  return 2*x2 + kt*(6*x3 + kt*(12*x4 + kt*20*x5));
885  case 3:
886  return 6*x3 + kt*(24*x4 + kt*60*x5);
887  case 4:
888  return 24*x4 + kt*120*x5;
889  case 5:
890  return 120*x5;
891  default:
892  return 0;
893  }
894  }
895 
896 private:
897  UT_Spline(const UT_Spline &copy); // not implemented yet
898  UT_Spline &operator =(const UT_Spline &copy); // not implemented yet
899 
900 private:
901  void grow(int size);
902  int getInterp(fpreal t, int knots[], fpreal weights[]);
903 
904  int64 getSizeOfValues() const
905  { return myKnotLength*myVectorSize*sizeof(fpreal64); }
906  int64 getSizeOfBases() const
907  { return myKnotLength*sizeof(UT_SPLINE_BASIS); }
908 
909  template <typename T>
910  inline void combineKeys(T *result, int vector_size,
911  fpreal64 weights[], int indices[],
912  int num_indices,
913  UT_ColorType interp_space) const;
914 
915  template <typename T>
916  inline void evalMonotoneCubic(fpreal t, T *result, int n,
917  int order, UT_ColorType interp_space,
918  bool do_multi) const;
919 
920  template <typename T>
921  inline void setValueInternal(int key, const T *value, int size);
922 
923  template <typename T>
924  inline bool evaluateInternal(fpreal t, T *result, int size,
925  UT_ColorType interp_space, int order) const;
926 
927  template <typename T>
928  inline bool evaluateMultiInternal(fpreal t, T *result, int n,
929  UT_ColorType interp_space,
930  int knot_segment_hint, int order) const;
931 
932  fpreal64 *myValues;
933  UT_SPLINE_BASIS *myBases;
934  fpreal64 myTension;
935  int myVectorSize;
936  int myKnotLength;
937  UT_SPLINE_BASIS myGlobalBasis;
938 };
939 
940 #include <VM/VM_SIMD.h>
941 
942 template <>
945 {
946 #if defined(CPU_HAS_SIMD_INSTR)
947  v4uf row1(1/6., 4/6., 1/6., 0/6.);
948  v4uf row2(-3/6., 0/6., 3/6., 0/6.);
949  v4uf row3(3/6., -6/6., 3/6., 0/6.);
950  v4uf row4(-1/6., 3/6., -3/6., 1/6. );
951 
952  v4uf vcvsx(cvs[0].x(), cvs[1].x(), cvs[2].x(), cvs[3].x());
953  v4uf vcvsy(cvs[0].y(), cvs[1].y(), cvs[2].y(), cvs[3].y());
954  v4uf vcvsz(cvs[0].z(), cvs[1].z(), cvs[2].z(), cvs[3].z());
955  v4uf vt(t);
956  v4uf vt2 = vt*vt;
957  v4uf vt3 = vt2*vt;
958 
959  v4uf weights;
960 
961  weights = row1;
962  weights += row2 * vt;
963  weights += row3 * vt2;
964  weights += row4 * vt3;
965 
966  vcvsx *= weights;
967  vcvsx += vcvsx.swizzle<1, 1, 3, 3>();
968  vcvsx += vcvsx.swizzle<2, 2, 2, 2>();
969  vcvsy *= weights;
970  vcvsy += vcvsy.swizzle<1, 1, 3, 3>();
971  vcvsy += vcvsy.swizzle<2, 2, 2, 2>();
972  vcvsz *= weights;
973  vcvsz += vcvsz.swizzle<1, 1, 3, 3>();
974  vcvsz += vcvsz.swizzle<2, 2, 2, 2>();
975 
976  return UT_Vector3( vcvsx[0], vcvsy[0], vcvsz[0] );
977 #else
978  UT_Matrix4 weightmatrix = getOpenWeights();
979  float t2 = t*t;
980  float t3 = t2*t;
981  UT_Vector4 powers(1, t, t2, t3);
982 
983  UT_Vector4 weights = colVecMult(weightmatrix, powers);
984 
986 
987  value = cvs[0] * weights[0];
988  value += cvs[1] * weights[1];
989  value += cvs[2] * weights[2];
990  value += cvs[3] * weights[3];
991 
992  return value;
993 #endif
994 }
995 
996 template <>
997 SYS_FORCE_INLINE float
998 UT_SplineCubic::evalOpen(const float *cvs, float t)
999 {
1000 #if defined(CPU_HAS_SIMD_INSTR)
1001  v4uf row1(1/6., 4/6., 1/6., 0/6.);
1002  v4uf row2(-3/6., 0/6., 3/6., 0/6.);
1003  v4uf row3(3/6., -6/6., 3/6., 0/6.);
1004  v4uf row4(-1/6., 3/6., -3/6., 1/6. );
1005 
1006  v4uf vcvs(cvs);
1007  v4uf vt(t);
1008  v4uf vt2 = vt*vt;
1009  v4uf vt3 = vt2*vt;
1010 
1011  v4uf weights;
1012 
1013  weights = row1;
1014  weights += row2 * vt;
1015  weights += row3 * vt2;
1016  weights += row4 * vt3;
1017 
1018  vcvs *= weights;
1019  vcvs += vcvs.swizzle<1, 1, 3, 3>();
1020  vcvs += vcvs.swizzle<2, 2, 2, 2>();
1021 
1022  return vcvs[0];
1023 #else
1024  UT_Matrix4 weightmatrix = getOpenWeights();
1025  float t2 = t*t;
1026  float t3 = t2*t;
1027  UT_Vector4 powers(1, t, t2, t3);
1028 
1029  UT_Vector4 weights = colVecMult(weightmatrix, powers);
1030 
1031  float value;
1032 
1033  value = cvs[0] * weights[0];
1034  value += cvs[1] * weights[1];
1035  value += cvs[2] * weights[2];
1036  value += cvs[3] * weights[3];
1037 
1038  return value;
1039 #endif
1040 }
1041 
1042 template <>
1043 inline void
1044 UT_SplineCubic::evalRangeOpen(UT_Vector3 *results, const UT_Vector3 *cvs, float start_t, float step_t, int len_t, int nseg)
1045 {
1046  int curseg;
1047  curseg = SYSfastFloor(start_t);
1048  curseg = SYSclamp(curseg, 0, nseg-1);
1049  float t = start_t - curseg;
1050 
1051 #if defined(CPU_HAS_SIMD_INSTR)
1052  v4uf row1(1/6., 4/6., 1/6., 0/6.);
1053  v4uf row2(-3/6., 0/6., 3/6., 0/6.);
1054  v4uf row3(3/6., -6/6., 3/6., 0/6.);
1055  v4uf row4(-1/6., 3/6., -3/6., 1/6. );
1056 
1057  v4uf vcvsx(cvs[curseg].x(), cvs[curseg+1].x(), cvs[curseg+2].x(), cvs[curseg+3].x());
1058  v4uf vcvsy(cvs[curseg].y(), cvs[curseg+1].y(), cvs[curseg+2].y(), cvs[curseg+3].y());
1059  v4uf vcvsz(cvs[curseg].z(), cvs[curseg+1].z(), cvs[curseg+2].z(), cvs[curseg+3].z());
1060 
1061  for (int i = 0; i < len_t; i++)
1062  {
1063  {
1064  v4uf weights;
1065  float t2 = t*t;
1066  float t3 = t2*t;
1067 
1068  weights = row1;
1069  weights += row2 * t;
1070  weights += row3 * t2;
1071  weights += row4 * t3;
1072 
1073  v4uf vx = vcvsx * weights;
1074  vx += vx.swizzle<1, 1, 3, 3>();
1075  vx += vx.swizzle<2, 2, 2, 2>();
1076  v4uf vy = vcvsy * weights;
1077  vy += vy.swizzle<1, 1, 3, 3>();
1078  vy += vy.swizzle<2, 2, 2, 2>();
1079  v4uf vz = vcvsz * weights;
1080  vz += vz.swizzle<1, 1, 3, 3>();
1081  vz += vz.swizzle<2, 2, 2, 2>();
1082  results[i] = UT_Vector3( vx[0], vy[0], vz[0] );
1083  }
1084 
1085  t += step_t;
1086  if (t > 1)
1087  {
1088  while (curseg < nseg-1)
1089  {
1090  curseg++;
1091  t -= 1;
1092  if (t <= 1)
1093  break;
1094  }
1095  if (i < len_t-1)
1096  {
1097  vcvsx = v4uf(cvs[curseg].x(), cvs[curseg+1].x(), cvs[curseg+2].x(), cvs[curseg+3].x());
1098  vcvsy = v4uf(cvs[curseg].y(), cvs[curseg+1].y(), cvs[curseg+2].y(), cvs[curseg+3].y());
1099  vcvsz = v4uf(cvs[curseg].z(), cvs[curseg+1].z(), cvs[curseg+2].z(), cvs[curseg+3].z());
1100  }
1101  }
1102  }
1103 #else
1104  for (int i = 0; i < len_t; i++)
1105  {
1106  results[i] = evalOpen(&cvs[curseg], t);
1107  t += step_t;
1108  if (t > 1)
1109  {
1110  while (curseg < nseg-1)
1111  {
1112  curseg++;
1113  t -= 1;
1114  if (t <= 1)
1115  break;
1116  }
1117  }
1118  }
1119 #endif
1120 }
1121 
1122 template <>
1123 inline void
1124 UT_SplineCubic::evalRangeOpen(float *results, const float *cvs, float start_t, float step_t, int len_t, int nseg)
1125 {
1126  int curseg;
1127  curseg = SYSfastFloor(start_t);
1128  curseg = SYSclamp(curseg, 0, nseg-1);
1129  float t = start_t - curseg;
1130 
1131 #if defined(CPU_HAS_SIMD_INSTR)
1132  v4uf row1(1/6., 4/6., 1/6., 0/6.);
1133  v4uf row2(-3/6., 0/6., 3/6., 0/6.);
1134  v4uf row3(3/6., -6/6., 3/6., 0/6.);
1135  v4uf row4(-1/6., 3/6., -3/6., 1/6. );
1136 
1137  v4uf vcvs(&cvs[curseg]);
1138 
1139  for (int i = 0; i < len_t; i++)
1140  {
1141  {
1142  v4uf weights;
1143  float t2 = t*t;
1144  float t3 = t2*t;
1145 
1146  weights = row1;
1147  weights += row2 * t;
1148  weights += row3 * t2;
1149  weights += row4 * t3;
1150 
1151  v4uf v = vcvs * weights;
1152  v += v.swizzle<1, 1, 3, 3>();
1153  v += v.swizzle<2, 2, 2, 2>();
1154  results[i] = v[0];
1155  }
1156 
1157  t += step_t;
1158  if (t > 1)
1159  {
1160  while (curseg < nseg-1)
1161  {
1162  curseg++;
1163  t -= 1;
1164  if (t <= 1)
1165  break;
1166  }
1167  if (i < len_t-1)
1168  {
1169  vcvs = v4uf(&cvs[curseg]);
1170  }
1171  }
1172  }
1173 #else
1174  for (int i = 0 ; i < len_t; i++)
1175  {
1176  results[i] = evalOpen(&cvs[curseg], t);
1177  t += step_t;
1178  if (t > 1)
1179  {
1180  while (curseg < nseg-1)
1181  {
1182  curseg++;
1183  t -= 1;
1184  if (t <= 1)
1185  break;
1186  }
1187  }
1188  }
1189 #endif
1190 }
1191 
1192 template <>
1194 UT_SplineCubic::evalClosed(const UT_Vector3 *cvs, float t, int seg, int nseg, bool deriv)
1195 {
1196 #if defined(CPU_HAS_SIMD_INSTR)
1197  UT_Matrix4 weightmatrix = getClosedWeightsTranspose(seg, nseg, deriv);
1198 
1199  v4uf row1(weightmatrix.data());
1200  v4uf row2(weightmatrix.data()+4);
1201  v4uf row3(weightmatrix.data()+8);
1202  v4uf row4(weightmatrix.data()+12);
1203 
1204  v4uf vcvsx(cvs[0].x(), cvs[1].x(), cvs[2].x(), cvs[3].x());
1205  v4uf vcvsy(cvs[0].y(), cvs[1].y(), cvs[2].y(), cvs[3].y());
1206  v4uf vcvsz(cvs[0].z(), cvs[1].z(), cvs[2].z(), cvs[3].z());
1207  v4uf vt(t);
1208  v4uf vt2 = vt*vt;
1209  v4uf vt3 = vt2*vt;
1210 
1211  v4uf weights;
1212 
1213  weights = row1;
1214  weights += row2 * vt;
1215  weights += row3 * vt2;
1216  weights += row4 * vt3;
1217 
1218  vcvsx *= weights;
1219  vcvsx += vcvsx.swizzle<1, 1, 3, 3>();
1220  vcvsx += vcvsx.swizzle<2, 2, 2, 2>();
1221  vcvsy *= weights;
1222  vcvsy += vcvsy.swizzle<1, 1, 3, 3>();
1223  vcvsy += vcvsy.swizzle<2, 2, 2, 2>();
1224  vcvsz *= weights;
1225  vcvsz += vcvsz.swizzle<1, 1, 3, 3>();
1226  vcvsz += vcvsz.swizzle<2, 2, 2, 2>();
1227 
1228  return UT_Vector3( vcvsx[0], vcvsy[0], vcvsz[0] );
1229 #else
1230  UT_Matrix4 weightmatrix = getClosedWeights(seg, nseg);
1231  float t2 = t*t;
1232  float t3 = t2*t;
1233  UT_Vector4 powers(1, t, t2, t3);
1234 
1235  UT_Vector4 weights = colVecMult(weightmatrix, powers);
1236 
1237  UT_Vector3 value;
1238 
1239  value = cvs[0] * weights[0];
1240  value += cvs[1] * weights[1];
1241  value += cvs[2] * weights[2];
1242  value += cvs[3] * weights[3];
1243 
1244  return value;
1245 #endif
1246 }
1247 
1248 template <>
1249 SYS_FORCE_INLINE float
1250 UT_SplineCubic::evalClosed(const float *cvs, float t, int seg, int nseg, bool deriv)
1251 {
1252 #if defined(CPU_HAS_SIMD_INSTR)
1253  UT_Matrix4 weightmatrix = getClosedWeightsTranspose(seg, nseg, deriv);
1254 
1255  v4uf row1(weightmatrix.data());
1256  v4uf row2(weightmatrix.data()+4);
1257  v4uf row3(weightmatrix.data()+8);
1258  v4uf row4(weightmatrix.data()+12);
1259 
1260  v4uf vcvs(cvs);
1261  float t2 = t*t;
1262  float t3 = t2*t;
1263 
1264  v4uf weights;
1265 
1266  weights = row1;
1267  weights += row2 * t;
1268  weights += row3 * t2;
1269  weights += row4 * t3;
1270 
1271  vcvs *= weights;
1272 
1273  vcvs += vcvs.swizzle<1, 1, 3, 3>();
1274  vcvs += vcvs.swizzle<2, 2, 2, 2>();
1275 
1276  return vcvs[0];
1277 #else
1278  UT_Matrix4 weightmatrix = getClosedWeights(seg, nseg);
1279  float t2 = t*t;
1280  float t3 = t2*t;
1281  UT_Vector4 powers(1, t, t2, t3);
1282 
1283  UT_Vector4 weights = colVecMult(weightmatrix, powers);
1284 
1285  float value;
1286 
1287  value = cvs[0] * weights[0];
1288  value += cvs[1] * weights[1];
1289  value += cvs[2] * weights[2];
1290  value += cvs[3] * weights[3];
1291 
1292  return value;
1293 #endif
1294 }
1295 
1296 template <>
1297 inline void
1298 UT_SplineCubic::evalRangeClosed(UT_Vector3 *results, const UT_Vector3 *cvs, float start_t, float step_t, int len_t, int nseg, bool deriv)
1299 {
1300  int curseg;
1301  curseg = SYSfastFloor(start_t);
1302  curseg = SYSclamp(curseg, 0, nseg-1);
1303  float t = start_t - curseg;
1304 
1305 #if defined(CPU_HAS_SIMD_INSTR)
1306  UT_Matrix4 weightmatrix = getClosedWeightsTranspose(curseg, nseg, deriv);
1307 
1308  v4uf row1(weightmatrix.data());
1309  v4uf row2(weightmatrix.data()+4);
1310  v4uf row3(weightmatrix.data()+8);
1311  v4uf row4(weightmatrix.data()+12);
1312 
1313  v4uf vcvsx(cvs[curseg].x(), cvs[curseg+1].x(), cvs[curseg+2].x(), cvs[curseg+3].x());
1314  v4uf vcvsy(cvs[curseg].y(), cvs[curseg+1].y(), cvs[curseg+2].y(), cvs[curseg+3].y());
1315  v4uf vcvsz(cvs[curseg].z(), cvs[curseg+1].z(), cvs[curseg+2].z(), cvs[curseg+3].z());
1316 
1317  for (int i = 0; i < len_t; i++)
1318  {
1319  {
1320  v4uf weights;
1321  float t2 = t*t;
1322  float t3 = t2*t;
1323 
1324  weights = row1;
1325  weights += row2 * t;
1326  weights += row3 * t2;
1327  weights += row4 * t3;
1328 
1329  v4uf vx = vcvsx * weights;
1330  vx += vx.swizzle<1, 1, 3, 3>();
1331  vx += vx.swizzle<2, 2, 2, 2>();
1332  v4uf vy = vcvsy * weights;
1333  vy += vy.swizzle<1, 1, 3, 3>();
1334  vy += vy.swizzle<2, 2, 2, 2>();
1335  v4uf vz = vcvsz * weights;
1336  vz += vz.swizzle<1, 1, 3, 3>();
1337  vz += vz.swizzle<2, 2, 2, 2>();
1338  results[i] = UT_Vector3( vx[0], vy[0], vz[0] );
1339  }
1340 
1341  t += step_t;
1342  if (t > 1)
1343  {
1344  while (curseg < nseg-1)
1345  {
1346  curseg++;
1347  t -= 1;
1348  if (t <= 1)
1349  break;
1350  }
1351  if (i < len_t-1)
1352  {
1353  weightmatrix = getClosedWeightsTranspose(curseg, nseg, deriv);
1354 
1355  row1 = v4uf(weightmatrix.data());
1356  row2 = v4uf(weightmatrix.data()+4);
1357  row3 = v4uf(weightmatrix.data()+8);
1358  row4 = v4uf(weightmatrix.data()+12);
1359 
1360  vcvsx = v4uf(cvs[curseg].x(), cvs[curseg+1].x(), cvs[curseg+2].x(), cvs[curseg+3].x());
1361  vcvsy = v4uf(cvs[curseg].y(), cvs[curseg+1].y(), cvs[curseg+2].y(), cvs[curseg+3].y());
1362  vcvsz = v4uf(cvs[curseg].z(), cvs[curseg+1].z(), cvs[curseg+2].z(), cvs[curseg+3].z());
1363  }
1364  }
1365  }
1366 #else
1367  for (int i = 0; i < len_t; i++)
1368  {
1369  results[i] = evalClosed(&cvs[curseg], t, curseg, nseg, deriv);
1370  t += step_t;
1371  if (t > 1)
1372  {
1373  while (curseg < nseg-1)
1374  {
1375  curseg++;
1376  t -= 1;
1377  if (t <= 1)
1378  break;
1379  }
1380  }
1381  }
1382 #endif
1383 }
1384 
1385 template <>
1386 inline void
1387 UT_SplineCubic::evalRangeClosed(float *results, const float *cvs, float start_t, float step_t, int len_t, int nseg, bool deriv)
1388 {
1389  int curseg;
1390  curseg = SYSfastFloor(start_t);
1391  curseg = SYSclamp(curseg, 0, nseg-1);
1392  float t = start_t - curseg;
1393 
1394 #if defined(CPU_HAS_SIMD_INSTR)
1395  UT_Matrix4 weightmatrix = getClosedWeightsTranspose(curseg, nseg, deriv);
1396 
1397  v4uf row1(weightmatrix.data());
1398  v4uf row2(weightmatrix.data()+4);
1399  v4uf row3(weightmatrix.data()+8);
1400  v4uf row4(weightmatrix.data()+12);
1401 
1402  v4uf vcvs(&cvs[curseg]);
1403 
1404  for (int i = 0; i < len_t; i++)
1405  {
1406  {
1407  v4uf weights;
1408  float t2 = t*t;
1409  float t3 = t2*t;
1410 
1411  weights = row1;
1412  weights += row2 * t;
1413  weights += row3 * t2;
1414  weights += row4 * t3;
1415 
1416  v4uf v = vcvs * weights;
1417  v += v.swizzle<1, 1, 3, 3>();
1418  v += v.swizzle<2, 2, 2, 2>();
1419  results[i] = v[0];
1420  }
1421 
1422  t += step_t;
1423  if (t > 1)
1424  {
1425  while (curseg < nseg-1)
1426  {
1427  curseg++;
1428  t -= 1;
1429  if (t <= 1)
1430  break;
1431  }
1432  if (i < len_t-1)
1433  {
1434  weightmatrix = getClosedWeightsTranspose(curseg, nseg, deriv);
1435 
1436  row1 = v4uf(weightmatrix.data());
1437  row2 = v4uf(weightmatrix.data()+4);
1438  row3 = v4uf(weightmatrix.data()+8);
1439  row4 = v4uf(weightmatrix.data()+12);
1440 
1441  vcvs = v4uf(&cvs[curseg]);
1442  }
1443  }
1444  }
1445 #else
1446  for (int i = 0 ; i < len_t; i++)
1447  {
1448  results[i] = evalClosed(&cvs[curseg], t, curseg, nseg, deriv);
1449  t += step_t;
1450  if (t > 1)
1451  {
1452  while (curseg < nseg-1)
1453  {
1454  curseg++;
1455  t -= 1;
1456  if (t <= 1)
1457  break;
1458  }
1459  }
1460  }
1461 #endif
1462 }
1463 
1464 void
1465 UT_SplineCubic::enlargeBoundingBoxOpen(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax)
1466 {
1467  // We need to find any minimum or maximum in each dimension
1468  // to enlarge the bounding box.
1469  // To do this, for each, dimension, we take the derivative
1470  // of the cubic, leaving a quadratic, and find the zeros of it.
1471  // The quadratic is such that its ith derivatives at zero are
1472  // the (i+1)th derivatives of the curve segment at zero.
1473  // a = (1/2) * 3rd derivative of curve segment at zero
1474  UT_Vector3 a = -cvs[0] + cvs[1] * 3.0F + cvs[2] * (-3.0F) + cvs[3];
1475  a *= 0.5F;
1476 
1477  // b = 2nd derivative of curve segment at zero
1478  // (this is equivalent to the 2nd difference)
1479  UT_Vector3 b = cvs[0] + cvs[1] * (-2.0F) + cvs[2];
1480  // c = 1st derivative of curve segment at zero
1481  // (this is equivalent to the central difference)
1482  UT_Vector3 c = cvs[2] - cvs[0];
1483  c *= 0.5F;
1484 
1485  enlargeBoundingBoxCommon<UT_SplineCubic::evalOpen<float> >(box, cvs, a, b, c, rootmin, rootmax);
1486 }
1487 
1488 void
1489 UT_SplineCubic::enlargeBoundingBoxSubDStart(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax)
1490 {
1491  // We need to find any minimum or maximum in each dimension
1492  // to enlarge the bounding box.
1493  // To do this, for each, dimension, we take the derivative
1494  // of the cubic, leaving a quadratic, and find the zeros of it.
1495  // The quadratic is such that its ith derivatives at zero are
1496  // the (i+1)th derivatives of the curve segment at zero.
1497 
1498  // First segment is (1 - t + (1/6)t^3)*P0 + (t - (1/3)*t^3)*P1 + ((1/6)t^3)*P2
1499  // 1st derivative is (-1 + (1/2)t^2)*P0 + (1 - t^2)*P1 + ((1/2)t^2)*P2
1500  // 2nd derivative is (t)*P0 + (-2t)*P1 + (t)*P2
1501  // 3rd derivative is (1)*P0 + (-2)*P1 + (1)*P2
1502 
1503  // a = (1/2) * 3rd derivative of curve segment at zero
1504  UT_Vector3 a = cvs[0] - 2.0f*cvs[1] + cvs[2];
1505  a *= 0.5F;
1506 
1507  // b = 2nd derivative of curve segment at zero
1508  // (this is equivalent to the 2nd difference)
1509  UT_Vector3 b(0,0,0);
1510  // c = 1st derivative of curve segment at zero
1511  // (this is equivalent to the central difference)
1512  UT_Vector3 c = cvs[1] - cvs[0];
1513 
1514  enlargeBoundingBoxCommon<UT_SplineCubic::evalSubDStart<float> >(box, cvs, a, b, c, rootmin, rootmax);
1515 }
1516 
1517 void
1518 UT_SplineCubic::enlargeBoundingBoxSubDEnd(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax)
1519 {
1520  // We need to find any minimum or maximum in each dimension
1521  // to enlarge the bounding box.
1522  // To do this, for each, dimension, we take the derivative
1523  // of the cubic, leaving a quadratic, and find the zeros of it.
1524  // The quadratic is such that its ith derivatives at zero are
1525  // the (i+1)th derivatives of the curve segment at zero.
1526 
1527  // First segment is ((1/6)(1-t)^3)*P0 + ((1-t) - (1/3)*(1-t)^3)*P1 + (1 - (1-t) + (1/6)(1-t)^3)*P2
1528  // 1st derivative is (-(1/2)(1-t)^2)*P0 + (-1 + (1-t)^2)*P1 + (1 - (1/2)(1-t)^2)*P2
1529  // 2nd derivative is (1-t)*P0 + (-2(1-t))*P1 + (1-t)*P2
1530  // 3rd derivative is (-1)*P0 + (2)*P1 + (-1)*P2
1531 
1532  // a = (1/2) * 3rd derivative of curve segment at zero
1533  // b = 2nd derivative of curve segment at zero
1534  // (this is equivalent to the 2nd difference)
1535  UT_Vector3 b = cvs[0] - 2.0f*cvs[1] + cvs[2];
1536  UT_Vector3 a = -0.5f*b;
1537 
1538  // c = 1st derivative of curve segment at zero
1539  // (this is equivalent to the central difference)
1540  UT_Vector3 c = cvs[2] - cvs[0];
1541  c *= 0.5f;
1542 
1543  enlargeBoundingBoxCommon<UT_SplineCubic::evalSubDEnd<float> >(box, cvs, a, b, c, rootmin, rootmax);
1544 }
1545 
1546 #endif
static const UT_Matrix4 theHermiteDerivBasis
Definition: UT_Spline.h:397
static UT_Matrix4 getClosedWeightsTranspose(int seg, int nseg, bool deriv=false)
Definition: UT_Spline.h:555
#define SYSmax(a, b)
Definition: SYS_Math.h:1582
UT_SPLINE_BASIS getGlobalBasis() const
Query the basis or knot length of the spline.
Definition: UT_Spline.h:731
static T evalClosed(const T *cvs, float t, int seg, int nseg, bool deriv=false)
Definition: UT_Spline.h:112
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
const GLdouble * v
Definition: glcorearb.h:837
int getKnotLength() const
Definition: UT_Spline.h:733
GLsizei const GLfloat * value
Definition: glcorearb.h:824
static UT_Matrix4 getOpenWeightsTranspose()
Definition: UT_Spline.h:277
static void evalRangeClosed(T *results, const T *cvs, float start_t, float step_t, int len_t, int nseg, bool deriv=false)
Definition: UT_Spline.h:134
UT_Vector3T< float > UT_Vector3
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
#define UT_API
Definition: UT_API.h:14
void setGlobalBasis(UT_SPLINE_BASIS b)
Definition: UT_Spline.h:736
GLint y
Definition: glcorearb.h:103
**But if you need a result
Definition: thread.h:622
static T evalSubDStart(const T *cvs, float t)
Definition: UT_Spline.h:159
__hostdev__ void setValue(uint32_t offset, bool v)
Definition: NanoVDB.h:5750
UT_SPLINE_BASIS
Definition: UT_Spline.h:31
float fpreal32
Definition: SYS_Types.h:200
static UT_Matrix4 getClosedWeights(int seg, int nseg, bool deriv=false)
Definition: UT_Spline.h:402
static void evalRangeOpen(T *results, const T *cvs, float start_t, float step_t, int len_t, int nseg)
Definition: UT_Spline.h:83
fpreal64 getTension() const
Definition: UT_Spline.h:734
GLdouble GLdouble x2
Definition: glad.h:2349
UT_Matrix4T< float > UT_Matrix4
static const UT_Matrix4 theInterpFirstBasis
Definition: UT_Spline.h:382
double fpreal64
Definition: SYS_Types.h:201
static UT_Matrix4 getOpenWeights()
Definition: UT_Spline.h:270
static const UT_Matrix4 theHermiteBasis
Definition: UT_Spline.h:391
GLdouble n
Definition: glcorearb.h:2008
GLfloat f
Definition: glcorearb.h:1926
int getVectorSize() const
Definition: UT_Spline.h:732
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
UT_Vector3T< T > SYSclamp(const UT_Vector3T< T > &v, const UT_Vector3T< T > &min, const UT_Vector3T< T > &max)
Definition: UT_Vector3.h:1057
Definition: VM_SIMD.h:188
GLdouble GLdouble GLint GLint order
Definition: glad.h:2676
long long int64
Definition: SYS_Types.h:116
static void enlargeBoundingBoxOpen(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax)
Definition: UT_Spline.h:1465
#define SYS_STATIC_FORCE_INLINE
Definition: SYS_Inline.h:48
static const UT_Matrix4 theOpenDerivBasis
Definition: UT_Spline.h:375
static const UT_Matrix4 theSubDFirstBasis
Definition: UT_Spline.h:356
GLuint const GLchar * name
Definition: glcorearb.h:786
static const UT_Matrix4 theOpenBasis
Definition: UT_Spline.h:369
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
GLint GLenum GLint x
Definition: glcorearb.h:409
static T evalCubic(T kt, T dt, T iv, T im, T ov, T om, int order=0)
Definition: UT_Spline.h:841
static T evalQuintic(T kt, T dt, T iv, T im, T ia, T ov, T om, T oa, int order=0)
Definition: UT_Spline.h:865
GLdouble t
Definition: glad.h:2397
static void enlargeBoundingBoxSubDStart(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax)
Definition: UT_Spline.h:1489
static T evalSubDCurve(const T *cvs, float t, int npts, bool deriv=false)
Definition: UT_Spline.h:303
GLsizeiptr size
Definition: glcorearb.h:664
GLenum func
Definition: glcorearb.h:783
UT_ColorType
Definition: UT_Color.h:24
const T * data() const
Return the raw matrix data.
Definition: UT_Matrix4.h:1159
static T evalMatrix(const UT_Matrix4 &basis, const T cvs[4], float t)
Definition: UT_Spline.h:286
static void enlargeBoundingBoxSubDEnd(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax)
Definition: UT_Spline.h:1518
fpreal64 fpreal
Definition: SYS_Types.h:278
static T evalOpen(const T *cvs, float t)
Definition: UT_Spline.h:61
LeafData & operator=(const LeafData &)=delete
UT_Vector3T< T > colVecMult(const UT_Matrix3T< S > &m, const UT_Vector3T< T > &v)
Definition: UT_Matrix3.h:1534
GLuint GLfloat * val
Definition: glcorearb.h:1608
static T evalSubDEnd(const T *cvs, float t)
Definition: UT_Spline.h:175
SYS_STATIC_FORCE_INLINE void enlargeBoundingBoxCommon(UT_BoundingBox &box, const UT_Vector3 *cvs, const UT_Vector3 &a, const UT_Vector3 &b, const UT_Vector3 &c, float rootmin, float rootmax)
Definition: UT_Spline.h:194
SYS_FORCE_INLINE v4uf swizzle() const
Definition: VM_SIMD.h:335
static const UT_Matrix4 theInterpBasis
Definition: UT_Spline.h:386
UT_API UT_SPLINE_BASIS UTsplineBasisFromName(const char *name)
static int quadratic(T a, T b, T c, T &v0, T &v1)
UT_API const char * UTnameFromSplineBasis(UT_SPLINE_BASIS basis)
#define SYSmin(a, b)
Definition: SYS_Math.h:1583
static const UT_Matrix4 theSubDFirstDerivBasis
Definition: UT_Spline.h:364