TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
splinefunc.h
Go to the documentation of this file.
1 
10 #ifndef G3D_SPLINEFUNC_H
11 #define G3D_SPLINEFUNC_H
12 
13 #include "G3D/platform.h"
14 #include "G3D/debug.h"
15 #include "G3D/Array.h"
16 #include "G3D/g3dmath.h"
17 
18 namespace G3D {
19 
41 template<class XType, class YType>
42 YType linearSpline(double x, const XType* controlX, const YType* controlY, int numControl) {
43  debugAssert(numControl >= 1);
44 
45  // Off the beginning
46  if ((numControl == 1) || (x < controlX[0])) {
47  return controlY[0];
48  }
49 
50  for (int i = 1; i < numControl; ++i) {
51  if (x < controlX[i]) {
52  const double alpha = (double)(controlX[i] - x) / (controlX[i] - controlX[i - 1]);
53  return controlY[i] * (1 - alpha) + controlY[i - 1] * alpha;
54  }
55  }
56 
57  // Off the end
58  return controlY[numControl - 1];
59 }
60 
61 
63 template<class YType> YType cyclicCatmullRomSpline(
64  double t,
65  const YType* controlY,
66  int numPoints) {
67 
68  debugAssert(numPoints >= 3);
69 
70  t = wrap(t, numPoints);
71 
72  // Find the indices of adjacent control points
73  int i = iFloor(t);
74 
75  // Compute the distance from the control point
76  t = t - i;
77 
78  // Shift back one point for correct indexing
79  i += numPoints - 1;
80 
81  // Pick up four control points
82  const YType& P0 = controlY[(i + 0) % numPoints];
83  const YType& P1 = controlY[(i + 1) % numPoints];
84  const YType& P2 = controlY[(i + 2) % numPoints];
85  const YType& P3 = controlY[(i + 3) % numPoints];
86 
87  return 0.5 * ((2 * P1) +
88  (-P0 + P2) * t +
89  (2*P0 - 5*P1 + 4*P2 - P3) * t*t +
90  (-P0 + 3*P1- 3*P2 + P3) * t*t*t);
91 }
92 
106 template<class YType> YType cyclicCatmullRomSpline(
107  double t,
108  const Array<YType>& controlY) {
109 
110  int numPoints = controlY.size();
111  return cyclicCatmullRomSpline(t, controlY.getCArray(), numPoints);
112 }
113 
114 }
115 
116 #endif
117 
118 
YType linearSpline(double x, const XType *controlX, const YType *controlY, int numControl)
Definition: splinefunc.h:42
T * getCArray()
Definition: Array.h:256
Definition: AABox.h:25
Dynamic 1D array tuned for performance.
Definition: Array.h:95
int iFloor(double fValue)
Definition: g3dmath.h:603
float wrap(float t, float lo, float hi)
Definition: g3dmath.h:495
#define debugAssert(exp)
Definition: debugAssert.h:160
int size() const
Definition: Array.h:430
YType cyclicCatmullRomSpline(double t, const YType *controlY, int numPoints)
Definition: splinefunc.h:63
G3D::int16 x
Definition: Vector2int16.h:37