diff --git a/package-lock.json b/package-lock.json index 9418aee..4a229cf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "curve-interpolator", - "version": "3.1.0-beta.1", + "version": "3.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "curve-interpolator", - "version": "3.1.0-beta.1", + "version": "3.1.0", "license": "MIT", "devDependencies": { "@rollup/plugin-node-resolve": "^15.0.1", diff --git a/package.json b/package.json index c38e2c0..ada7bb4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "private": false, "name": "curve-interpolator", - "version": "3.1.0", + "version": "3.1.1", "description": "Interpolate values on a Cardinal/Catmull-Rom spline curve", "repository": "https://github.com/kjerandp/curve-interpolator", "bugs": { diff --git a/src/core/spline-segment.ts b/src/core/spline-segment.ts index 3db3e1e..f5b70ca 100644 --- a/src/core/spline-segment.ts +++ b/src/core/spline-segment.ts @@ -63,8 +63,9 @@ export function calculateCoefficients(p0: Vector, p1:Vector, p2:Vector, p3:Vecto const a = (2 * v1 - 2 * v2 + u + v); const b = (-3 * v1 + 3 * v2 - 2 * u - v); + const c = u; const d = v1; - coefficientsList[k] = [a, b, u, d]; + coefficientsList[k] = [a, b, c, d]; } return coefficientsList; } diff --git a/src/curve-interpolator.spec.ts b/src/curve-interpolator.spec.ts index e11d6d3..56c7a8f 100644 --- a/src/curve-interpolator.spec.ts +++ b/src/curve-interpolator.spec.ts @@ -92,6 +92,37 @@ describe('curve-interpolator.ts', () => { }); + it('should be able to lookup extrema on curve', () => { + const interp = new CurveInterpolator([[187.7, 113.4], [930, 821.62], [1620, 1330.82], [2310, 1621.24]], { tension: 0.75, alpha: 0 }); + + // expect one solution for the end-points in this data + let actual = interp.getIntersects(187.7, 0, 0) as number[]; + expect(actual.length).to.eq(1); + + actual = interp.getIntersects(2310, 0, 0) as number[]; + expect(actual.length).to.eq(1); + + //from start of curve + actual = interp.getIntersects(187.7, 0, 1) as number[]; + expect(actual).to.not.be.null; + expect(actual[1]).to.be.closeTo(113.4, EPS); + + actual = interp.getIntersects(2310, 0, 1) as number[]; + expect(actual).to.not.be.null; + expect(actual[1]).to.be.closeTo(1621.24, EPS); + + // from end of curve + actual = interp.getIntersects(187.7, 0, -1) as number[]; + expect(actual).to.not.be.null; + expect(actual[1]).to.be.closeTo(113.4, EPS); + + actual = interp.getIntersects(2310, 0, -1) as number[]; + expect(actual).to.not.be.null; + expect(actual[1]).to.be.closeTo(1621.24, EPS); + + + }); + it('should be able to get bounds of curve', () => { const interp = new CurveInterpolator(points, { tension: 0, alpha: 0 }); diff --git a/src/curve-interpolator.ts b/src/curve-interpolator.ts index b410546..4e6c1a9 100644 --- a/src/curve-interpolator.ts +++ b/src/curve-interpolator.ts @@ -507,7 +507,6 @@ export default class CurveInterpolator { else if (max >= 0) ts.sort((a, b) => a - b); for (let j = 0; j < ts.length; j++) { - if (ts[j] === 0 && i > 0) continue; // avoid duplicate (0 would be found as 1 in previous iteration) const nt = (ts[j] + idx) / nPoints; // normalize t solutions.add(nt);