Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | import {HermiteCurve} from './hermite-curve';
import * as mathf from './mathf';
import {Vector} from './vector';
/**
* A basic catmull-rom class. Useful for creating splines or catmull-rom
* interpolations. They are useful compared to other splines because they are
* guaranteed to go through specified control points.
*
* This particular implementation uses hermite curves from point to point.
* Given a set of points, we calculate the m0, m1 tangents between and
* curve until we are out of points.
*
* TODO (uxder): Using interpolate, create a spline.
* TDOO (uxder): Take on SVG spline interpolate.
*
* ```ts
* let catmullInterpolate = CatmullRom.interpolate(
* [
* new Vector(0, 0),
* new Vector(100, 100),
* new Vector(200, 200),
* new Vector(300, 400),
* ... // Can take any number of vectors.
* ],
* 0.25, // Optional m0 tension
* 0.25 // Optional m1 tensions
* );
*
* let progress = 0.5;
* let x = catmullInterplate(progress).x; // Returns the x value at 0.5 progress.
* let y = catmullInterpolate(progress).y; // Returns the y value at 0.5 progress.
* ```
*
* Or static method
* ```ts
*
* let mypoints = [
* new Vector(0, 0),
* new Vector(100, 100),
* new Vector(200, 200),
* ]
* this.x = CatmullRom.getPoint(
* myPoints,
* 0.5, // Progress
* ).x;
* this.y = CatmullRom.getPoint(
* myPoints,
* 0.5, // Progress
* ).y;
*
* ```
*
*
* @see http://marina.sys.wakayama-u.ac.jp/~tokoi/?date=20150105
* @see https://www.youtube.com/watch?v=w_uU_rPayoQ
* @see https://en.wikibooks.org/wiki/Cg_Programming/Unity/Hermite_Curves
* @see https://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline
* @see http://hadashia.hatenablog.com/entry/2017/12/30/150556
*/
export class CatmullRom {
/**
* Create a function that can be called to apply camtull roll interpolations.
* @param points A point of vectors.
* @param m0Tension
* @param m1Tension
*/
public static interpolate(
points: Array<Vector>,
m0Tension = 0.5,
m1Tension = 0.5
): Function {
return (progress: number) => {
return CatmullRom.getPoint(points, progress, m0Tension, m1Tension);
};
}
/**
* For the given set of vectors, finds the p0 and p1 (start and end)
* vectors that apply to the current progress range and then calculates
* the in-between m0 and m1 tangents and applies the hermite-curve
* and returns a vector of the current catmull-rom interpolation.
* @param points
* @param progress
*/
public static getPoint(
points: Array<Vector>,
progress: number,
m0Tension = 0.5,
m1Tension = 0.5
): Vector {
const pointCount = points.length - 1;
const percentPerVector = 100 / pointCount;
const i = mathf.clamp(
Math.floor(mathf.lerp(0, pointCount, progress)),
pointCount - 1,
0
);
const p_1 = points[i - 1] && points[i - 1].clone();
const p0 = points[i] && points[i].clone();
const p1 = points[i + 1] && points[i + 1].clone();
const p2 = points[i + 2] && points[i + 2].clone();
// Calculate M0 tangent
// M0 = p1 - p_1 / 2
let m0;
if (i > 0) {
m0 = Vector.subtract(p1, p_1).scale(m0Tension);
} else {
m0 = Vector.subtract(p1, p0);
}
// Calculate M1 tangent
// M1 = p2 - p0 / 2
let m1;
if (i < pointCount - 2) {
m1 = Vector.subtract(p2, p0).scale(m1Tension);
} else {
m1 = Vector.subtract(p1, p0);
}
// Calculate the child progress for this curve.
const startProgress = (percentPerVector * i) / 100;
const endProgress = (percentPerVector * (i + 1)) / 100;
const childProgress = mathf.childProgress(
progress,
startProgress,
endProgress
);
return HermiteCurve.getPoint(childProgress, p0, m0, p1, m1);
}
}
|