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 public:
723  UT_Spline();
724  ~UT_Spline();
725 
726  /// Return the amount of memory owned by this UT_Spline in bytes
727  int64 getMemoryUsage(bool inclusive) const;
728 
729  /// Query the basis or knot length of the spline
730  UT_SPLINE_BASIS getGlobalBasis() const { return myGlobalBasis; }
731  int getVectorSize() const { return myVectorSize; }
732  int getKnotLength() const { return myKnotLength; }
733  fpreal64 getTension() const { return myTension; }
734 
736  { myGlobalBasis = b; }
737 
738  /// Construction of the spline object. All values are initialized to 0.
739  /// Warning, calling setSize() will clear all existing values.
740  void setSize(int nkeys, int vector_size);
741  /// Cubic splines may have a "tension". The tension defaults to 0.5 which
742  /// results in Catmull-Rom splines.
743  void setTension(fpreal64 t);
744 
745  /// Once the spline has been constructed, the values need to be set.
746  /// It is possible to change values between evaluations.
747  // @{
748  void setValue(int key, const fpreal32 *value, int size);
749  void setValue(int key, const fpreal64 *value, int size);
750  // @}
751 
752  /// Set the basis for the given key index.
753  /// This will also set the global basis.
754  void setBasis(int key, UT_SPLINE_BASIS b);
755 
756  /// Evaluate the spline using the global basis.
757  /// When interp_space is not UT_RGB, then values are treated as UT_RGBA
758  /// and converted into the desired color space before being interpolated.
759  /// The result is always returned as UT_RGBA.
760  ///
761  /// If order == 0, the value is returned.
762  /// If order == 1, the derivative with respect to t is returned.
763  // @{
764  bool evaluate(fpreal t, fpreal32 *result, int size,
765  UT_ColorType interp_space, int order = 0) const;
766  bool evaluate(fpreal t, fpreal64 *result, int size,
767  UT_ColorType interp_space, int order = 0) const;
768  // @}
769 
770  /// Evaluate the spline using multiple basis types depending on t.
771  /// Also unlike evaluate(), evaluateMulti() doesn't require extra keys for
772  /// Catmull-Rom on the ends. It always evaluates using a 0 slope at the
773  /// ends.
774  ///
775  /// If order == 0, the value is returned.
776  /// If order == 1, the derivative with respect to t is returned.
777  // @{
778  bool evaluateMulti(fpreal t, fpreal32 *result, int n,
779  UT_ColorType interp_space,
780  int knot_segment_hint = -1,
781  int order = 0) const;
782  bool evaluateMulti(fpreal t, fpreal64 *result, int n,
783  UT_ColorType interp_space,
784  int knot_segment_hint = -1,
785  int order = 0) const;
786  // @}
787 
788  /// Return _monotone_ cubic Hermite slopes at the current knot given the
789  /// previous and next knots.
790  // @{
791  /// Fritsch-Carlson (local) method.
792  /// It gives relatively good looking C1 curves but might not give a C2
793  /// solution even if it exists.
794  /// 1. Fritsch, F.N., Carlson, R.E., Monotone piecewise cubic interpolant,
795  /// SIAM J. Numer. Anal., 17(1980), 238-246.
796  static fpreal64 getMonotoneSlopeFC(fpreal64 v_cur, fpreal64 t_cur,
797  fpreal64 v_prev, fpreal64 t_prev,
798  fpreal64 v_next, fpreal64 t_next);
799  /// Paul Kupan's method.
800  /// Similar to Fritsch-Carlson method except it gives more visually
801  /// pleasing results when the intervals next to the knot are uneven.
802  /// 2. Kupan, P.A., Monotone Interpolant Built with Slopes Obtained by
803  /// Linear Combination, Studia Universitatis Babes-Bolyai Mathematica,
804  /// 53(2008), 59-66.
805  static fpreal64 getMonotoneSlopePA(fpreal64 v_cur, fpreal64 t_cur,
806  fpreal64 v_prev, fpreal64 t_prev,
807  fpreal64 v_next, fpreal64 t_next);
808  // @}
809 
810  /// Given the position within the two knots and the first knot index
811  /// number, normalize the position from knot-length domain to unit domain.
812  fpreal normalizeParameter(fpreal parm, fpreal t0, fpreal t1,
813  int t0_index) const;
814 
815  // Given parm in [0,1] interval in which CVs are at 'knots' values,
816  // find the parameter t in [0,1] interval in which CVs are evenly spaced.
817  // The returned reparameterized value is calculated in such a way that it
818  // yields smooth curve at CVs (unlike normalizeation that uses linear
819  // interpolation that yields tangent disconinuity at CVs).
820  // If 'parm_knot_segment' is given, this function sets it to the index
821  // of the knot segment into which the parm falls; it can be used as a hint
822  // to the evaluateMulti() method, especially for b-splines that may remap
823  // parameters to an adjacent segment (for continuity at end control points).
824  // If 'order' == 1, the derivative of t with respect to parm is returned.
825  fpreal solveSpline(fpreal parm,
826  const UT_Array<fpreal> &knots,
827  int *parm_knot_segment = nullptr,
828  int order = 0) const;
829 
830  /// Given the keys surrounding a channel segment, evaluate it as cubic
831  /// hermite spline. This function assumes dt > 0.
832  /// kt is [0,1] which maps over the dt time interval.
833  /// The `order`-th derivative with respect to kt is returned.
834  /// In particular, for order == 0 (default), the value is returned.
835  template <typename T>
836  static T evalCubic(T kt, T dt, T iv, T im, T ov, T om, int order = 0)
837  {
838  T x0 = iv;
839  T x1 = im*dt;
840  T x3 = 2*(iv - ov) + (im + om)*dt;
841  T x2 = ov - iv - im*dt - x3;
842  switch (order)
843  {
844  case 0:
845  return x0 + kt*(x1 + kt*(x2 + kt*x3));
846  case 1:
847  return x1 + kt*(2*x2 + kt*3*x3);
848  case 2:
849  return 2*x2 + kt*6*x3;
850  case 3:
851  return 6*x3;
852  default:
853  return 0;
854  }
855  }
856 
857  /// Given the keys surrounding a channel segment, evaluate it as
858  /// a quintic hermite spline. See evalCubic above.
859  template <typename T>
860  static T evalQuintic(T kt, T dt, T iv, T im, T ia, T ov, T om, T oa, int order = 0)
861  {
862  fpreal x0 = iv;
863  fpreal x1 = im * dt;
864  fpreal x2 = (fpreal).5 * ia * dt;
865  fpreal b0 = ov - (x0 + x1 + x2);
866  fpreal b1 = om * dt - (x1 + ia * dt);
867  fpreal b2 = oa * dt - ia * dt;
868  fpreal x5 = (fpreal).5 * (b2 - 6 * b1 + 12 * b0);
869  fpreal x4 = (fpreal).25 * (b2 - 2 * b1 - 10 * x5);
870  fpreal x3 = b0 - x4 - x5;
871 
872  switch (order)
873  {
874  case 0:
875  return x0 + kt*(x1 + kt*(x2 + kt*(x3 + kt*(x4 + kt*x5))));
876  case 1:
877  return x1 + kt*(2*x2 + kt*(3*x3 + kt*(4*x4 + kt*5*x5)));
878  case 2:
879  return 2*x2 + kt*(6*x3 + kt*(12*x4 + kt*20*x5));
880  case 3:
881  return 6*x3 + kt*(24*x4 + kt*60*x5);
882  case 4:
883  return 24*x4 + kt*120*x5;
884  case 5:
885  return 120*x5;
886  default:
887  return 0;
888  }
889  }
890 
891 private:
892  UT_Spline(const UT_Spline &copy); // not implemented yet
893  UT_Spline &operator =(const UT_Spline &copy); // not implemented yet
894 
895 private:
896  void grow(int size);
897  int getInterp(fpreal t, int knots[], fpreal weights[]);
898 
899  int64 getSizeOfValues() const
900  { return myKnotLength*myVectorSize*sizeof(fpreal64); }
901  int64 getSizeOfBases() const
902  { return myKnotLength*sizeof(UT_SPLINE_BASIS); }
903 
904  template <typename T>
905  inline void combineKeys(T *result, int vector_size,
906  fpreal64 weights[], int indices[],
907  int num_indices,
908  UT_ColorType interp_space) const;
909 
910  template <typename T>
911  inline void evalMonotoneCubic(fpreal t, T *result, int n,
912  int order, UT_ColorType interp_space,
913  bool do_multi) const;
914 
915  template <typename T>
916  inline void setValueInternal(int key, const T *value, int size);
917 
918  template <typename T>
919  inline bool evaluateInternal(fpreal t, T *result, int size,
920  UT_ColorType interp_space, int order) const;
921 
922  template <typename T>
923  inline bool evaluateMultiInternal(fpreal t, T *result, int n,
924  UT_ColorType interp_space,
925  int knot_segment_hint, int order) const;
926 
927  fpreal64 *myValues;
928  UT_SPLINE_BASIS *myBases;
929  fpreal64 myTension;
930  int myVectorSize;
931  int myKnotLength;
932  UT_SPLINE_BASIS myGlobalBasis;
933 };
934 
935 #include <VM/VM_SIMD.h>
936 
937 template <>
940 {
941 #if defined(CPU_HAS_SIMD_INSTR)
942  v4uf row1(1/6., 4/6., 1/6., 0/6.);
943  v4uf row2(-3/6., 0/6., 3/6., 0/6.);
944  v4uf row3(3/6., -6/6., 3/6., 0/6.);
945  v4uf row4(-1/6., 3/6., -3/6., 1/6. );
946 
947  v4uf vcvsx(cvs[0].x(), cvs[1].x(), cvs[2].x(), cvs[3].x());
948  v4uf vcvsy(cvs[0].y(), cvs[1].y(), cvs[2].y(), cvs[3].y());
949  v4uf vcvsz(cvs[0].z(), cvs[1].z(), cvs[2].z(), cvs[3].z());
950  v4uf vt(t);
951  v4uf vt2 = vt*vt;
952  v4uf vt3 = vt2*vt;
953 
954  v4uf weights;
955 
956  weights = row1;
957  weights += row2 * vt;
958  weights += row3 * vt2;
959  weights += row4 * vt3;
960 
961  vcvsx *= weights;
962  vcvsx += vcvsx.swizzle<1, 1, 3, 3>();
963  vcvsx += vcvsx.swizzle<2, 2, 2, 2>();
964  vcvsy *= weights;
965  vcvsy += vcvsy.swizzle<1, 1, 3, 3>();
966  vcvsy += vcvsy.swizzle<2, 2, 2, 2>();
967  vcvsz *= weights;
968  vcvsz += vcvsz.swizzle<1, 1, 3, 3>();
969  vcvsz += vcvsz.swizzle<2, 2, 2, 2>();
970 
971  return UT_Vector3( vcvsx[0], vcvsy[0], vcvsz[0] );
972 #else
973  UT_Matrix4 weightmatrix = getOpenWeights();
974  float t2 = t*t;
975  float t3 = t2*t;
976  UT_Vector4 powers(1, t, t2, t3);
977 
978  UT_Vector4 weights = colVecMult(weightmatrix, powers);
979 
981 
982  value = cvs[0] * weights[0];
983  value += cvs[1] * weights[1];
984  value += cvs[2] * weights[2];
985  value += cvs[3] * weights[3];
986 
987  return value;
988 #endif
989 }
990 
991 template <>
992 SYS_FORCE_INLINE float
993 UT_SplineCubic::evalOpen(const float *cvs, float t)
994 {
995 #if defined(CPU_HAS_SIMD_INSTR)
996  v4uf row1(1/6., 4/6., 1/6., 0/6.);
997  v4uf row2(-3/6., 0/6., 3/6., 0/6.);
998  v4uf row3(3/6., -6/6., 3/6., 0/6.);
999  v4uf row4(-1/6., 3/6., -3/6., 1/6. );
1000 
1001  v4uf vcvs(cvs);
1002  v4uf vt(t);
1003  v4uf vt2 = vt*vt;
1004  v4uf vt3 = vt2*vt;
1005 
1006  v4uf weights;
1007 
1008  weights = row1;
1009  weights += row2 * vt;
1010  weights += row3 * vt2;
1011  weights += row4 * vt3;
1012 
1013  vcvs *= weights;
1014  vcvs += vcvs.swizzle<1, 1, 3, 3>();
1015  vcvs += vcvs.swizzle<2, 2, 2, 2>();
1016 
1017  return vcvs[0];
1018 #else
1019  UT_Matrix4 weightmatrix = getOpenWeights();
1020  float t2 = t*t;
1021  float t3 = t2*t;
1022  UT_Vector4 powers(1, t, t2, t3);
1023 
1024  UT_Vector4 weights = colVecMult(weightmatrix, powers);
1025 
1026  float value;
1027 
1028  value = cvs[0] * weights[0];
1029  value += cvs[1] * weights[1];
1030  value += cvs[2] * weights[2];
1031  value += cvs[3] * weights[3];
1032 
1033  return value;
1034 #endif
1035 }
1036 
1037 template <>
1038 inline void
1039 UT_SplineCubic::evalRangeOpen(UT_Vector3 *results, const UT_Vector3 *cvs, float start_t, float step_t, int len_t, int nseg)
1040 {
1041  int curseg;
1042  curseg = SYSfastFloor(start_t);
1043  curseg = SYSclamp(curseg, 0, nseg-1);
1044  float t = start_t - curseg;
1045 
1046 #if defined(CPU_HAS_SIMD_INSTR)
1047  v4uf row1(1/6., 4/6., 1/6., 0/6.);
1048  v4uf row2(-3/6., 0/6., 3/6., 0/6.);
1049  v4uf row3(3/6., -6/6., 3/6., 0/6.);
1050  v4uf row4(-1/6., 3/6., -3/6., 1/6. );
1051 
1052  v4uf vcvsx(cvs[curseg].x(), cvs[curseg+1].x(), cvs[curseg+2].x(), cvs[curseg+3].x());
1053  v4uf vcvsy(cvs[curseg].y(), cvs[curseg+1].y(), cvs[curseg+2].y(), cvs[curseg+3].y());
1054  v4uf vcvsz(cvs[curseg].z(), cvs[curseg+1].z(), cvs[curseg+2].z(), cvs[curseg+3].z());
1055 
1056  for (int i = 0; i < len_t; i++)
1057  {
1058  {
1059  v4uf weights;
1060  float t2 = t*t;
1061  float t3 = t2*t;
1062 
1063  weights = row1;
1064  weights += row2 * t;
1065  weights += row3 * t2;
1066  weights += row4 * t3;
1067 
1068  v4uf vx = vcvsx * weights;
1069  vx += vx.swizzle<1, 1, 3, 3>();
1070  vx += vx.swizzle<2, 2, 2, 2>();
1071  v4uf vy = vcvsy * weights;
1072  vy += vy.swizzle<1, 1, 3, 3>();
1073  vy += vy.swizzle<2, 2, 2, 2>();
1074  v4uf vz = vcvsz * weights;
1075  vz += vz.swizzle<1, 1, 3, 3>();
1076  vz += vz.swizzle<2, 2, 2, 2>();
1077  results[i] = UT_Vector3( vx[0], vy[0], vz[0] );
1078  }
1079 
1080  t += step_t;
1081  if (t > 1)
1082  {
1083  while (curseg < nseg-1)
1084  {
1085  curseg++;
1086  t -= 1;
1087  if (t <= 1)
1088  break;
1089  }
1090  if (i < len_t-1)
1091  {
1092  vcvsx = v4uf(cvs[curseg].x(), cvs[curseg+1].x(), cvs[curseg+2].x(), cvs[curseg+3].x());
1093  vcvsy = v4uf(cvs[curseg].y(), cvs[curseg+1].y(), cvs[curseg+2].y(), cvs[curseg+3].y());
1094  vcvsz = v4uf(cvs[curseg].z(), cvs[curseg+1].z(), cvs[curseg+2].z(), cvs[curseg+3].z());
1095  }
1096  }
1097  }
1098 #else
1099  for (int i = 0; i < len_t; i++)
1100  {
1101  results[i] = evalOpen(&cvs[curseg], t);
1102  t += step_t;
1103  if (t > 1)
1104  {
1105  while (curseg < nseg-1)
1106  {
1107  curseg++;
1108  t -= 1;
1109  if (t <= 1)
1110  break;
1111  }
1112  }
1113  }
1114 #endif
1115 }
1116 
1117 template <>
1118 inline void
1119 UT_SplineCubic::evalRangeOpen(float *results, const float *cvs, float start_t, float step_t, int len_t, int nseg)
1120 {
1121  int curseg;
1122  curseg = SYSfastFloor(start_t);
1123  curseg = SYSclamp(curseg, 0, nseg-1);
1124  float t = start_t - curseg;
1125 
1126 #if defined(CPU_HAS_SIMD_INSTR)
1127  v4uf row1(1/6., 4/6., 1/6., 0/6.);
1128  v4uf row2(-3/6., 0/6., 3/6., 0/6.);
1129  v4uf row3(3/6., -6/6., 3/6., 0/6.);
1130  v4uf row4(-1/6., 3/6., -3/6., 1/6. );
1131 
1132  v4uf vcvs(&cvs[curseg]);
1133 
1134  for (int i = 0; i < len_t; i++)
1135  {
1136  {
1137  v4uf weights;
1138  float t2 = t*t;
1139  float t3 = t2*t;
1140 
1141  weights = row1;
1142  weights += row2 * t;
1143  weights += row3 * t2;
1144  weights += row4 * t3;
1145 
1146  v4uf v = vcvs * weights;
1147  v += v.swizzle<1, 1, 3, 3>();
1148  v += v.swizzle<2, 2, 2, 2>();
1149  results[i] = v[0];
1150  }
1151 
1152  t += step_t;
1153  if (t > 1)
1154  {
1155  while (curseg < nseg-1)
1156  {
1157  curseg++;
1158  t -= 1;
1159  if (t <= 1)
1160  break;
1161  }
1162  if (i < len_t-1)
1163  {
1164  vcvs = v4uf(&cvs[curseg]);
1165  }
1166  }
1167  }
1168 #else
1169  for (int i = 0 ; i < len_t; i++)
1170  {
1171  results[i] = evalOpen(&cvs[curseg], t);
1172  t += step_t;
1173  if (t > 1)
1174  {
1175  while (curseg < nseg-1)
1176  {
1177  curseg++;
1178  t -= 1;
1179  if (t <= 1)
1180  break;
1181  }
1182  }
1183  }
1184 #endif
1185 }
1186 
1187 template <>
1189 UT_SplineCubic::evalClosed(const UT_Vector3 *cvs, float t, int seg, int nseg, bool deriv)
1190 {
1191 #if defined(CPU_HAS_SIMD_INSTR)
1192  UT_Matrix4 weightmatrix = getClosedWeightsTranspose(seg, nseg, deriv);
1193 
1194  v4uf row1(weightmatrix.data());
1195  v4uf row2(weightmatrix.data()+4);
1196  v4uf row3(weightmatrix.data()+8);
1197  v4uf row4(weightmatrix.data()+12);
1198 
1199  v4uf vcvsx(cvs[0].x(), cvs[1].x(), cvs[2].x(), cvs[3].x());
1200  v4uf vcvsy(cvs[0].y(), cvs[1].y(), cvs[2].y(), cvs[3].y());
1201  v4uf vcvsz(cvs[0].z(), cvs[1].z(), cvs[2].z(), cvs[3].z());
1202  v4uf vt(t);
1203  v4uf vt2 = vt*vt;
1204  v4uf vt3 = vt2*vt;
1205 
1206  v4uf weights;
1207 
1208  weights = row1;
1209  weights += row2 * vt;
1210  weights += row3 * vt2;
1211  weights += row4 * vt3;
1212 
1213  vcvsx *= weights;
1214  vcvsx += vcvsx.swizzle<1, 1, 3, 3>();
1215  vcvsx += vcvsx.swizzle<2, 2, 2, 2>();
1216  vcvsy *= weights;
1217  vcvsy += vcvsy.swizzle<1, 1, 3, 3>();
1218  vcvsy += vcvsy.swizzle<2, 2, 2, 2>();
1219  vcvsz *= weights;
1220  vcvsz += vcvsz.swizzle<1, 1, 3, 3>();
1221  vcvsz += vcvsz.swizzle<2, 2, 2, 2>();
1222 
1223  return UT_Vector3( vcvsx[0], vcvsy[0], vcvsz[0] );
1224 #else
1225  UT_Matrix4 weightmatrix = getClosedWeights(seg, nseg);
1226  float t2 = t*t;
1227  float t3 = t2*t;
1228  UT_Vector4 powers(1, t, t2, t3);
1229 
1230  UT_Vector4 weights = colVecMult(weightmatrix, powers);
1231 
1232  UT_Vector3 value;
1233 
1234  value = cvs[0] * weights[0];
1235  value += cvs[1] * weights[1];
1236  value += cvs[2] * weights[2];
1237  value += cvs[3] * weights[3];
1238 
1239  return value;
1240 #endif
1241 }
1242 
1243 template <>
1244 SYS_FORCE_INLINE float
1245 UT_SplineCubic::evalClosed(const float *cvs, float t, int seg, int nseg, bool deriv)
1246 {
1247 #if defined(CPU_HAS_SIMD_INSTR)
1248  UT_Matrix4 weightmatrix = getClosedWeightsTranspose(seg, nseg, deriv);
1249 
1250  v4uf row1(weightmatrix.data());
1251  v4uf row2(weightmatrix.data()+4);
1252  v4uf row3(weightmatrix.data()+8);
1253  v4uf row4(weightmatrix.data()+12);
1254 
1255  v4uf vcvs(cvs);
1256  float t2 = t*t;
1257  float t3 = t2*t;
1258 
1259  v4uf weights;
1260 
1261  weights = row1;
1262  weights += row2 * t;
1263  weights += row3 * t2;
1264  weights += row4 * t3;
1265 
1266  vcvs *= weights;
1267 
1268  vcvs += vcvs.swizzle<1, 1, 3, 3>();
1269  vcvs += vcvs.swizzle<2, 2, 2, 2>();
1270 
1271  return vcvs[0];
1272 #else
1273  UT_Matrix4 weightmatrix = getClosedWeights(seg, nseg);
1274  float t2 = t*t;
1275  float t3 = t2*t;
1276  UT_Vector4 powers(1, t, t2, t3);
1277 
1278  UT_Vector4 weights = colVecMult(weightmatrix, powers);
1279 
1280  float value;
1281 
1282  value = cvs[0] * weights[0];
1283  value += cvs[1] * weights[1];
1284  value += cvs[2] * weights[2];
1285  value += cvs[3] * weights[3];
1286 
1287  return value;
1288 #endif
1289 }
1290 
1291 template <>
1292 inline void
1293 UT_SplineCubic::evalRangeClosed(UT_Vector3 *results, const UT_Vector3 *cvs, float start_t, float step_t, int len_t, int nseg, bool deriv)
1294 {
1295  int curseg;
1296  curseg = SYSfastFloor(start_t);
1297  curseg = SYSclamp(curseg, 0, nseg-1);
1298  float t = start_t - curseg;
1299 
1300 #if defined(CPU_HAS_SIMD_INSTR)
1301  UT_Matrix4 weightmatrix = getClosedWeightsTranspose(curseg, nseg, deriv);
1302 
1303  v4uf row1(weightmatrix.data());
1304  v4uf row2(weightmatrix.data()+4);
1305  v4uf row3(weightmatrix.data()+8);
1306  v4uf row4(weightmatrix.data()+12);
1307 
1308  v4uf vcvsx(cvs[curseg].x(), cvs[curseg+1].x(), cvs[curseg+2].x(), cvs[curseg+3].x());
1309  v4uf vcvsy(cvs[curseg].y(), cvs[curseg+1].y(), cvs[curseg+2].y(), cvs[curseg+3].y());
1310  v4uf vcvsz(cvs[curseg].z(), cvs[curseg+1].z(), cvs[curseg+2].z(), cvs[curseg+3].z());
1311 
1312  for (int i = 0; i < len_t; i++)
1313  {
1314  {
1315  v4uf weights;
1316  float t2 = t*t;
1317  float t3 = t2*t;
1318 
1319  weights = row1;
1320  weights += row2 * t;
1321  weights += row3 * t2;
1322  weights += row4 * t3;
1323 
1324  v4uf vx = vcvsx * weights;
1325  vx += vx.swizzle<1, 1, 3, 3>();
1326  vx += vx.swizzle<2, 2, 2, 2>();
1327  v4uf vy = vcvsy * weights;
1328  vy += vy.swizzle<1, 1, 3, 3>();
1329  vy += vy.swizzle<2, 2, 2, 2>();
1330  v4uf vz = vcvsz * weights;
1331  vz += vz.swizzle<1, 1, 3, 3>();
1332  vz += vz.swizzle<2, 2, 2, 2>();
1333  results[i] = UT_Vector3( vx[0], vy[0], vz[0] );
1334  }
1335 
1336  t += step_t;
1337  if (t > 1)
1338  {
1339  while (curseg < nseg-1)
1340  {
1341  curseg++;
1342  t -= 1;
1343  if (t <= 1)
1344  break;
1345  }
1346  if (i < len_t-1)
1347  {
1348  weightmatrix = getClosedWeightsTranspose(curseg, nseg, deriv);
1349 
1350  row1 = v4uf(weightmatrix.data());
1351  row2 = v4uf(weightmatrix.data()+4);
1352  row3 = v4uf(weightmatrix.data()+8);
1353  row4 = v4uf(weightmatrix.data()+12);
1354 
1355  vcvsx = v4uf(cvs[curseg].x(), cvs[curseg+1].x(), cvs[curseg+2].x(), cvs[curseg+3].x());
1356  vcvsy = v4uf(cvs[curseg].y(), cvs[curseg+1].y(), cvs[curseg+2].y(), cvs[curseg+3].y());
1357  vcvsz = v4uf(cvs[curseg].z(), cvs[curseg+1].z(), cvs[curseg+2].z(), cvs[curseg+3].z());
1358  }
1359  }
1360  }
1361 #else
1362  for (int i = 0; i < len_t; i++)
1363  {
1364  results[i] = evalClosed(&cvs[curseg], t, curseg, nseg, deriv);
1365  t += step_t;
1366  if (t > 1)
1367  {
1368  while (curseg < nseg-1)
1369  {
1370  curseg++;
1371  t -= 1;
1372  if (t <= 1)
1373  break;
1374  }
1375  }
1376  }
1377 #endif
1378 }
1379 
1380 template <>
1381 inline void
1382 UT_SplineCubic::evalRangeClosed(float *results, const float *cvs, float start_t, float step_t, int len_t, int nseg, bool deriv)
1383 {
1384  int curseg;
1385  curseg = SYSfastFloor(start_t);
1386  curseg = SYSclamp(curseg, 0, nseg-1);
1387  float t = start_t - curseg;
1388 
1389 #if defined(CPU_HAS_SIMD_INSTR)
1390  UT_Matrix4 weightmatrix = getClosedWeightsTranspose(curseg, nseg, deriv);
1391 
1392  v4uf row1(weightmatrix.data());
1393  v4uf row2(weightmatrix.data()+4);
1394  v4uf row3(weightmatrix.data()+8);
1395  v4uf row4(weightmatrix.data()+12);
1396 
1397  v4uf vcvs(&cvs[curseg]);
1398 
1399  for (int i = 0; i < len_t; i++)
1400  {
1401  {
1402  v4uf weights;
1403  float t2 = t*t;
1404  float t3 = t2*t;
1405 
1406  weights = row1;
1407  weights += row2 * t;
1408  weights += row3 * t2;
1409  weights += row4 * t3;
1410 
1411  v4uf v = vcvs * weights;
1412  v += v.swizzle<1, 1, 3, 3>();
1413  v += v.swizzle<2, 2, 2, 2>();
1414  results[i] = v[0];
1415  }
1416 
1417  t += step_t;
1418  if (t > 1)
1419  {
1420  while (curseg < nseg-1)
1421  {
1422  curseg++;
1423  t -= 1;
1424  if (t <= 1)
1425  break;
1426  }
1427  if (i < len_t-1)
1428  {
1429  weightmatrix = getClosedWeightsTranspose(curseg, nseg, deriv);
1430 
1431  row1 = v4uf(weightmatrix.data());
1432  row2 = v4uf(weightmatrix.data()+4);
1433  row3 = v4uf(weightmatrix.data()+8);
1434  row4 = v4uf(weightmatrix.data()+12);
1435 
1436  vcvs = v4uf(&cvs[curseg]);
1437  }
1438  }
1439  }
1440 #else
1441  for (int i = 0 ; i < len_t; i++)
1442  {
1443  results[i] = evalClosed(&cvs[curseg], t, curseg, nseg, deriv);
1444  t += step_t;
1445  if (t > 1)
1446  {
1447  while (curseg < nseg-1)
1448  {
1449  curseg++;
1450  t -= 1;
1451  if (t <= 1)
1452  break;
1453  }
1454  }
1455  }
1456 #endif
1457 }
1458 
1459 void
1460 UT_SplineCubic::enlargeBoundingBoxOpen(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax)
1461 {
1462  // We need to find any minimum or maximum in each dimension
1463  // to enlarge the bounding box.
1464  // To do this, for each, dimension, we take the derivative
1465  // of the cubic, leaving a quadratic, and find the zeros of it.
1466  // The quadratic is such that its ith derivatives at zero are
1467  // the (i+1)th derivatives of the curve segment at zero.
1468  // a = (1/2) * 3rd derivative of curve segment at zero
1469  UT_Vector3 a = -cvs[0] + cvs[1] * 3.0F + cvs[2] * (-3.0F) + cvs[3];
1470  a *= 0.5F;
1471 
1472  // b = 2nd derivative of curve segment at zero
1473  // (this is equivalent to the 2nd difference)
1474  UT_Vector3 b = cvs[0] + cvs[1] * (-2.0F) + cvs[2];
1475  // c = 1st derivative of curve segment at zero
1476  // (this is equivalent to the central difference)
1477  UT_Vector3 c = cvs[2] - cvs[0];
1478  c *= 0.5F;
1479 
1480  enlargeBoundingBoxCommon<UT_SplineCubic::evalOpen<float> >(box, cvs, a, b, c, rootmin, rootmax);
1481 }
1482 
1483 void
1484 UT_SplineCubic::enlargeBoundingBoxSubDStart(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax)
1485 {
1486  // We need to find any minimum or maximum in each dimension
1487  // to enlarge the bounding box.
1488  // To do this, for each, dimension, we take the derivative
1489  // of the cubic, leaving a quadratic, and find the zeros of it.
1490  // The quadratic is such that its ith derivatives at zero are
1491  // the (i+1)th derivatives of the curve segment at zero.
1492 
1493  // First segment is (1 - t + (1/6)t^3)*P0 + (t - (1/3)*t^3)*P1 + ((1/6)t^3)*P2
1494  // 1st derivative is (-1 + (1/2)t^2)*P0 + (1 - t^2)*P1 + ((1/2)t^2)*P2
1495  // 2nd derivative is (t)*P0 + (-2t)*P1 + (t)*P2
1496  // 3rd derivative is (1)*P0 + (-2)*P1 + (1)*P2
1497 
1498  // a = (1/2) * 3rd derivative of curve segment at zero
1499  UT_Vector3 a = cvs[0] - 2.0f*cvs[1] + cvs[2];
1500  a *= 0.5F;
1501 
1502  // b = 2nd derivative of curve segment at zero
1503  // (this is equivalent to the 2nd difference)
1504  UT_Vector3 b(0,0,0);
1505  // c = 1st derivative of curve segment at zero
1506  // (this is equivalent to the central difference)
1507  UT_Vector3 c = cvs[1] - cvs[0];
1508 
1509  enlargeBoundingBoxCommon<UT_SplineCubic::evalSubDStart<float> >(box, cvs, a, b, c, rootmin, rootmax);
1510 }
1511 
1512 void
1513 UT_SplineCubic::enlargeBoundingBoxSubDEnd(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax)
1514 {
1515  // We need to find any minimum or maximum in each dimension
1516  // to enlarge the bounding box.
1517  // To do this, for each, dimension, we take the derivative
1518  // of the cubic, leaving a quadratic, and find the zeros of it.
1519  // The quadratic is such that its ith derivatives at zero are
1520  // the (i+1)th derivatives of the curve segment at zero.
1521 
1522  // 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
1523  // 1st derivative is (-(1/2)(1-t)^2)*P0 + (-1 + (1-t)^2)*P1 + (1 - (1/2)(1-t)^2)*P2
1524  // 2nd derivative is (1-t)*P0 + (-2(1-t))*P1 + (1-t)*P2
1525  // 3rd derivative is (-1)*P0 + (2)*P1 + (-1)*P2
1526 
1527  // a = (1/2) * 3rd derivative of curve segment at zero
1528  // b = 2nd derivative of curve segment at zero
1529  // (this is equivalent to the 2nd difference)
1530  UT_Vector3 b = cvs[0] - 2.0f*cvs[1] + cvs[2];
1531  UT_Vector3 a = -0.5f*b;
1532 
1533  // c = 1st derivative of curve segment at zero
1534  // (this is equivalent to the central difference)
1535  UT_Vector3 c = cvs[2] - cvs[0];
1536  c *= 0.5f;
1537 
1538  enlargeBoundingBoxCommon<UT_SplineCubic::evalSubDEnd<float> >(box, cvs, a, b, c, rootmin, rootmax);
1539 }
1540 
1541 #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:1538
UT_SPLINE_BASIS getGlobalBasis() const
Query the basis or knot length of the spline.
Definition: UT_Spline.h:730
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:732
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:735
GLint y
Definition: glcorearb.h:103
**But if you need a result
Definition: thread.h:613
static T evalSubDStart(const T *cvs, float t)
Definition: UT_Spline.h:159
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:733
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:731
#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:1460
#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:836
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:860
GLdouble t
Definition: glad.h:2397
static void enlargeBoundingBoxSubDStart(UT_BoundingBox &box, const UT_Vector3 *cvs, float rootmin, float rootmax)
Definition: UT_Spline.h:1484
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:1163
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:1513
fpreal64 fpreal
Definition: SYS_Types.h:277
static T evalOpen(const T *cvs, float t)
Definition: UT_Spline.h:61
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
Definition: core.h:1131
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:1539
static const UT_Matrix4 theSubDFirstDerivBasis
Definition: UT_Spline.h:364