Skip to content

Commit

Permalink
Fixes issue #30: loop iterating solutions would terminate if t=0 and …
Browse files Browse the repository at this point in the history
…the loop index was 0 to avoid duplicates. This was not considering max < 0, and is not needed anyway, as we use a Set to avoid duplicates.
  • Loading branch information
kjerandp committed Feb 24, 2023
1 parent f7c49f4 commit 3185239
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
3 changes: 2 additions & 1 deletion src/core/spline-segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
31 changes: 31 additions & 0 deletions src/curve-interpolator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand Down
1 change: 0 additions & 1 deletion src/curve-interpolator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 3185239

Please sign in to comment.