17 public static class CatmullRom {
27 Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3,
28 bool centripetal =
true)
31 CalculateCatmullRomParameterization(p0, p1, p2, p3, out v1, out v2,
41 private static void CalculateCatmullRomParameterization(
42 Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3,
43 out Vector3 v1, out Vector3 v2,
44 bool centripetal =
true)
55 var dt0 = Mathf.Pow((p0 - p1).sqrMagnitude, 0.25f);
56 var dt1 = Mathf.Pow((p1 - p2).sqrMagnitude, 0.25f);
57 var dt2 = Mathf.Pow((p2 - p3).sqrMagnitude, 0.25f);
60 if (dt1 < 1e-4f) dt1 = 1.0f;
61 if (dt0 < 1e-4f) dt0 = dt1;
62 if (dt2 < 1e-4f) dt2 = dt1;
65 v1 = (p1 - p0) / dt0 - (p2 - p0) / (dt0 + dt1) + (p2 - p1) / dt1;
66 v2 = (p2 - p1) / dt1 - (p3 - p1) / (dt1 + dt2) + (p3 - p2) / dt2;
79 Quaternion q0, Quaternion q1, Quaternion q2, Quaternion q3,
80 bool centripetal =
true)
83 CalculateCatmullRomParameterization(q0, q1, q2, q3, out aV1, out aV2, centripetal);
93 private static void CalculateCatmullRomParameterization(Quaternion q0, Quaternion q1,
94 Quaternion q2, Quaternion q3,
97 bool centripetal =
true) {
103 aV1 = Mathq.Log(
Quaternion.Inverse(q0) * q2)/2f;
104 aV2 = Mathq.Log(
Quaternion.Inverse(q1) * q3)/2f;
114 var dt0 = Mathf.Pow(Mathq.Log(
Quaternion.Inverse(q1) * q0).sqrMagnitude, 0.25f);
115 var dt1 = Mathf.Pow(Mathq.Log(
Quaternion.Inverse(q2) * q1).sqrMagnitude, 0.25f);
116 var dt2 = Mathf.Pow(Mathq.Log(
Quaternion.Inverse(q3) * q2).sqrMagnitude, 0.25f);
119 if (dt1 < 1e-4f) dt1 = 1.0f;
120 if (dt0 < 1e-4f) dt0 = dt1;
121 if (dt2 < 1e-4f) dt2 = dt1;
126 aV1 = Mathq.Log(
Quaternion.Inverse(q0) * q1) / dt0
127 - Mathq.Log(
Quaternion.Inverse(q0) * q2) / (dt0 + dt1)
128 + Mathq.Log(
Quaternion.Inverse(q1) * q2) / dt1;
129 aV2 = Mathq.Log(
Quaternion.Inverse(q1) * q2) / dt1
130 - Mathq.Log(
Quaternion.Inverse(q1) * q3) / (dt1 + dt2)
131 + Mathq.Log(
Quaternion.Inverse(q2) * q3) / dt2;
141 CalculateCatmullRomParameterization(p0.position, p1.position,
142 p2.position, p3.position,
144 CalculateCatmullRomParameterization(p0.rotation, p1.rotation,
145 p2.rotation, p3.rotation,
148 return new HermitePoseSpline(p1, p2,
new Movement(v1, aV1),
new Movement(v2, aV2));
151 #region Archived code -- legacy code relies on this. It is incorrect though!
163 public static void InterpolatePoints(Vector3[] fourPositions,
float[] timeValues, ref Vector3[] outPoints) {
164 InterpolatePoints(fourPositions, timeValues, ref outPoints, numPoints: outPoints.Length);
176 public static void InterpolatePoints(Vector3[] fourPositions,
float[] timeValues,
177 ref Vector3[] outPoints,
int numPoints) {
178 for (
int i = 0; i < numPoints; i++) {
179 outPoints[i] = Interpolate(fourPositions, timeValues, i * (1F / (numPoints - 1)));
185 public static Vector3 Interpolate(Vector3[] P,
float[] T,
float t) {
186 Vector3 L01 = P[0] * (T[1] - t) / (T[1] - T[0]) + P[1] * (t - T[0]) / (T[1] - T[0]);
187 Vector3 L12 = P[1] * (T[2] - t) / (T[2] - T[1]) + P[2] * (t - T[1]) / (T[2] - T[1]);
188 Vector3 L23 = P[2] * (T[3] - t) / (T[3] - T[2]) + P[3] * (t - T[2]) / (T[3] - T[2]);
189 Vector3 L012 = L01 * (T[2] - t) / (T[2] - T[0]) + L12 * (t - T[0]) / (T[2] - T[0]);
190 Vector3 L123 = L12 * (T[3] - t) / (T[3] - T[1]) + L23 * (t - T[1]) / (T[3] - T[1]);
191 Vector3 C12 = L012 * (T[2] - t) / (T[2] - T[1]) + L123 * (t - T[1]) / (T[2] - T[1]);
Represents a spline for poses – positions and rotations – that travel from one position and rotation ...
Represents a spline for the rotation of a rigid body from one orientation in space to another over a ...
Represents a spline that travels from one point in space to another over a specified time frame....