curve-interpolator
Advanced tools
Comparing version
@@ -224,2 +224,16 @@ 'use strict'; | ||
var selectRootValue = function selectRootValue(roots) { | ||
var rootMatch = roots.filter(function (r) { | ||
return r.real >= 0 && r.real < 1.0001 && r.imag === 0; | ||
}); | ||
if (rootMatch.length === 1) { | ||
return rootMatch[0].real; | ||
} else if (rootMatch.length > 1) { | ||
return rootMatch.reduce(function (t, r) { | ||
return Math.max(r.real, t); | ||
}, 0); | ||
} | ||
return undefined; | ||
}; | ||
var classCallCheck = function (instance, Constructor) { | ||
@@ -260,2 +274,8 @@ if (!(instance instanceof Constructor)) { | ||
this.cache = { | ||
xLookup: {}, | ||
yLookup: {}, | ||
arcLengths: undefined | ||
}; | ||
// get extent and cache ordering | ||
@@ -282,9 +302,12 @@ this.sortedX = points.map(function (p, i) { | ||
// normalize points | ||
// normalize and add points to cache | ||
this.convertionFactor = Math.sqrt((this.dx * this.dx + this.dy * this.dy) / 2); | ||
this.points = points.map(function (p) { | ||
return { | ||
var np = { | ||
x: normalizeValue(p.x, _this.dx, _this.minX), | ||
y: normalizeValue(p.y, _this.dy, _this.minY) | ||
}; | ||
_this.cache.xLookup[np.x] = np.y; | ||
_this.cache.yLookup[np.y] = np.x; | ||
return np; | ||
}); | ||
@@ -318,7 +341,7 @@ } | ||
if (this.cacheArcLengths && this.cacheArcLengths.length === divisions + 1) { | ||
return this.cacheArcLengths; | ||
if (this.cache.arcLengths && this.cache.arcLengths.length === divisions + 1) { | ||
return this.cache.arcLengths; | ||
} | ||
var cache = []; | ||
var _cache = []; | ||
var current = void 0, | ||
@@ -329,3 +352,3 @@ last = this._getPoint(0); | ||
cache.push(0); | ||
_cache.push(0); | ||
@@ -335,9 +358,9 @@ for (p = 1; p <= divisions; p++) { | ||
sum += getDistance(current, last); | ||
cache.push(sum); | ||
_cache.push(sum); | ||
last = current; | ||
} | ||
this.cacheArcLengths = cache; | ||
this.cache.arcLengths = _cache; | ||
return cache; // { sums: cache, sum: sum }; Sum is in the last element. | ||
return _cache; // { sums: cache, sum: sum }; Sum is in the last element. | ||
} | ||
@@ -450,2 +473,5 @@ }, { | ||
var nx = isNormalized ? x : normalizeValue(x, this.dx, this.minX); | ||
if (this.cache.xLookup[nx] !== undefined) { | ||
return this.denormalizeY(this.cache.xLookup[nx]); | ||
} | ||
var cp = determineControlPointsFrom(this.points, nx, function (p) { | ||
@@ -456,9 +482,7 @@ return p.x; | ||
var roots = getCubicRoots(coeff.a, coeff.b, coeff.c, coeff.d); | ||
var rootMatch = roots.filter(function (r) { | ||
return r.real >= 0 && r.real < 1 && r.imag === 0; | ||
}); | ||
if (rootMatch.length === 1) { | ||
var t = rootMatch[0].real; | ||
var t = selectRootValue(roots); | ||
if (t !== undefined) { | ||
var y = getPointOnCurve(t, cp.p0.y, cp.p1.y, cp.p2.y, cp.p3.y, this.tension); | ||
return denormalizeValue(y, this.dy, this.minY); | ||
this.cache.xLookup[nx] = y; | ||
return this.denormalizeY(y); | ||
} | ||
@@ -473,2 +497,5 @@ throw new Error('Unable to solve for x = ' + x); | ||
var ny = isNormalized ? y : normalizeValue(y, this.dy, this.minY); | ||
if (this.cache.yLookup[ny] !== undefined) { | ||
return this.denormalizeX(this.cache.yLookup[ny]); | ||
} | ||
var cp = determineControlPointsFrom(this.points, ny, function (p) { | ||
@@ -479,9 +506,7 @@ return p.y; | ||
var roots = getCubicRoots(coeff.a, coeff.b, coeff.c, coeff.d); | ||
var rootMatch = roots.filter(function (r) { | ||
return r.real >= 0 && r.real < 1.00001 && r.imag === 0; | ||
}); | ||
if (rootMatch.length === 1) { | ||
var t = Math.min(rootMatch[0].real, 1.0); | ||
var t = selectRootValue(roots); | ||
if (t !== undefined) { | ||
var x = getPointOnCurve(t, cp.p0.x, cp.p1.x, cp.p2.x, cp.p3.x, this.tension); | ||
return denormalizeValue(x, this.dx, this.minX); | ||
this.cache.yLookup[ny] = x; | ||
return this.denormalizeX(x); | ||
} | ||
@@ -488,0 +513,0 @@ throw new Error('Unable to solve for y = ' + y); |
@@ -222,2 +222,16 @@ var EPS = Math.pow(2, -52); | ||
var selectRootValue = function selectRootValue(roots) { | ||
var rootMatch = roots.filter(function (r) { | ||
return r.real >= 0 && r.real < 1.0001 && r.imag === 0; | ||
}); | ||
if (rootMatch.length === 1) { | ||
return rootMatch[0].real; | ||
} else if (rootMatch.length > 1) { | ||
return rootMatch.reduce(function (t, r) { | ||
return Math.max(r.real, t); | ||
}, 0); | ||
} | ||
return undefined; | ||
}; | ||
var classCallCheck = function (instance, Constructor) { | ||
@@ -258,2 +272,8 @@ if (!(instance instanceof Constructor)) { | ||
this.cache = { | ||
xLookup: {}, | ||
yLookup: {}, | ||
arcLengths: undefined | ||
}; | ||
// get extent and cache ordering | ||
@@ -280,9 +300,12 @@ this.sortedX = points.map(function (p, i) { | ||
// normalize points | ||
// normalize and add points to cache | ||
this.convertionFactor = Math.sqrt((this.dx * this.dx + this.dy * this.dy) / 2); | ||
this.points = points.map(function (p) { | ||
return { | ||
var np = { | ||
x: normalizeValue(p.x, _this.dx, _this.minX), | ||
y: normalizeValue(p.y, _this.dy, _this.minY) | ||
}; | ||
_this.cache.xLookup[np.x] = np.y; | ||
_this.cache.yLookup[np.y] = np.x; | ||
return np; | ||
}); | ||
@@ -316,7 +339,7 @@ } | ||
if (this.cacheArcLengths && this.cacheArcLengths.length === divisions + 1) { | ||
return this.cacheArcLengths; | ||
if (this.cache.arcLengths && this.cache.arcLengths.length === divisions + 1) { | ||
return this.cache.arcLengths; | ||
} | ||
var cache = []; | ||
var _cache = []; | ||
var current = void 0, | ||
@@ -327,3 +350,3 @@ last = this._getPoint(0); | ||
cache.push(0); | ||
_cache.push(0); | ||
@@ -333,9 +356,9 @@ for (p = 1; p <= divisions; p++) { | ||
sum += getDistance(current, last); | ||
cache.push(sum); | ||
_cache.push(sum); | ||
last = current; | ||
} | ||
this.cacheArcLengths = cache; | ||
this.cache.arcLengths = _cache; | ||
return cache; // { sums: cache, sum: sum }; Sum is in the last element. | ||
return _cache; // { sums: cache, sum: sum }; Sum is in the last element. | ||
} | ||
@@ -448,2 +471,5 @@ }, { | ||
var nx = isNormalized ? x : normalizeValue(x, this.dx, this.minX); | ||
if (this.cache.xLookup[nx] !== undefined) { | ||
return this.denormalizeY(this.cache.xLookup[nx]); | ||
} | ||
var cp = determineControlPointsFrom(this.points, nx, function (p) { | ||
@@ -454,9 +480,7 @@ return p.x; | ||
var roots = getCubicRoots(coeff.a, coeff.b, coeff.c, coeff.d); | ||
var rootMatch = roots.filter(function (r) { | ||
return r.real >= 0 && r.real < 1 && r.imag === 0; | ||
}); | ||
if (rootMatch.length === 1) { | ||
var t = rootMatch[0].real; | ||
var t = selectRootValue(roots); | ||
if (t !== undefined) { | ||
var y = getPointOnCurve(t, cp.p0.y, cp.p1.y, cp.p2.y, cp.p3.y, this.tension); | ||
return denormalizeValue(y, this.dy, this.minY); | ||
this.cache.xLookup[nx] = y; | ||
return this.denormalizeY(y); | ||
} | ||
@@ -471,2 +495,5 @@ throw new Error('Unable to solve for x = ' + x); | ||
var ny = isNormalized ? y : normalizeValue(y, this.dy, this.minY); | ||
if (this.cache.yLookup[ny] !== undefined) { | ||
return this.denormalizeX(this.cache.yLookup[ny]); | ||
} | ||
var cp = determineControlPointsFrom(this.points, ny, function (p) { | ||
@@ -477,9 +504,7 @@ return p.y; | ||
var roots = getCubicRoots(coeff.a, coeff.b, coeff.c, coeff.d); | ||
var rootMatch = roots.filter(function (r) { | ||
return r.real >= 0 && r.real < 1.00001 && r.imag === 0; | ||
}); | ||
if (rootMatch.length === 1) { | ||
var t = Math.min(rootMatch[0].real, 1.0); | ||
var t = selectRootValue(roots); | ||
if (t !== undefined) { | ||
var x = getPointOnCurve(t, cp.p0.x, cp.p1.x, cp.p2.x, cp.p3.x, this.tension); | ||
return denormalizeValue(x, this.dx, this.minX); | ||
this.cache.yLookup[ny] = x; | ||
return this.denormalizeX(x); | ||
} | ||
@@ -486,0 +511,0 @@ throw new Error('Unable to solve for y = ' + y); |
@@ -228,2 +228,16 @@ (function (global, factory) { | ||
var selectRootValue = function selectRootValue(roots) { | ||
var rootMatch = roots.filter(function (r) { | ||
return r.real >= 0 && r.real < 1.0001 && r.imag === 0; | ||
}); | ||
if (rootMatch.length === 1) { | ||
return rootMatch[0].real; | ||
} else if (rootMatch.length > 1) { | ||
return rootMatch.reduce(function (t, r) { | ||
return Math.max(r.real, t); | ||
}, 0); | ||
} | ||
return undefined; | ||
}; | ||
var classCallCheck = function (instance, Constructor) { | ||
@@ -264,2 +278,8 @@ if (!(instance instanceof Constructor)) { | ||
this.cache = { | ||
xLookup: {}, | ||
yLookup: {}, | ||
arcLengths: undefined | ||
}; | ||
// get extent and cache ordering | ||
@@ -286,9 +306,12 @@ this.sortedX = points.map(function (p, i) { | ||
// normalize points | ||
// normalize and add points to cache | ||
this.convertionFactor = Math.sqrt((this.dx * this.dx + this.dy * this.dy) / 2); | ||
this.points = points.map(function (p) { | ||
return { | ||
var np = { | ||
x: normalizeValue(p.x, _this.dx, _this.minX), | ||
y: normalizeValue(p.y, _this.dy, _this.minY) | ||
}; | ||
_this.cache.xLookup[np.x] = np.y; | ||
_this.cache.yLookup[np.y] = np.x; | ||
return np; | ||
}); | ||
@@ -322,7 +345,7 @@ } | ||
if (this.cacheArcLengths && this.cacheArcLengths.length === divisions + 1) { | ||
return this.cacheArcLengths; | ||
if (this.cache.arcLengths && this.cache.arcLengths.length === divisions + 1) { | ||
return this.cache.arcLengths; | ||
} | ||
var cache = []; | ||
var _cache = []; | ||
var current = void 0, | ||
@@ -333,3 +356,3 @@ last = this._getPoint(0); | ||
cache.push(0); | ||
_cache.push(0); | ||
@@ -339,9 +362,9 @@ for (p = 1; p <= divisions; p++) { | ||
sum += getDistance(current, last); | ||
cache.push(sum); | ||
_cache.push(sum); | ||
last = current; | ||
} | ||
this.cacheArcLengths = cache; | ||
this.cache.arcLengths = _cache; | ||
return cache; // { sums: cache, sum: sum }; Sum is in the last element. | ||
return _cache; // { sums: cache, sum: sum }; Sum is in the last element. | ||
} | ||
@@ -454,2 +477,5 @@ }, { | ||
var nx = isNormalized ? x : normalizeValue(x, this.dx, this.minX); | ||
if (this.cache.xLookup[nx] !== undefined) { | ||
return this.denormalizeY(this.cache.xLookup[nx]); | ||
} | ||
var cp = determineControlPointsFrom(this.points, nx, function (p) { | ||
@@ -460,9 +486,7 @@ return p.x; | ||
var roots = getCubicRoots(coeff.a, coeff.b, coeff.c, coeff.d); | ||
var rootMatch = roots.filter(function (r) { | ||
return r.real >= 0 && r.real < 1 && r.imag === 0; | ||
}); | ||
if (rootMatch.length === 1) { | ||
var t = rootMatch[0].real; | ||
var t = selectRootValue(roots); | ||
if (t !== undefined) { | ||
var y = getPointOnCurve(t, cp.p0.y, cp.p1.y, cp.p2.y, cp.p3.y, this.tension); | ||
return denormalizeValue(y, this.dy, this.minY); | ||
this.cache.xLookup[nx] = y; | ||
return this.denormalizeY(y); | ||
} | ||
@@ -477,2 +501,5 @@ throw new Error('Unable to solve for x = ' + x); | ||
var ny = isNormalized ? y : normalizeValue(y, this.dy, this.minY); | ||
if (this.cache.yLookup[ny] !== undefined) { | ||
return this.denormalizeX(this.cache.yLookup[ny]); | ||
} | ||
var cp = determineControlPointsFrom(this.points, ny, function (p) { | ||
@@ -483,9 +510,7 @@ return p.y; | ||
var roots = getCubicRoots(coeff.a, coeff.b, coeff.c, coeff.d); | ||
var rootMatch = roots.filter(function (r) { | ||
return r.real >= 0 && r.real < 1.00001 && r.imag === 0; | ||
}); | ||
if (rootMatch.length === 1) { | ||
var t = Math.min(rootMatch[0].real, 1.0); | ||
var t = selectRootValue(roots); | ||
if (t !== undefined) { | ||
var x = getPointOnCurve(t, cp.p0.x, cp.p1.x, cp.p2.x, cp.p3.x, this.tension); | ||
return denormalizeValue(x, this.dx, this.minX); | ||
this.cache.yLookup[ny] = x; | ||
return this.denormalizeX(x); | ||
} | ||
@@ -492,0 +517,0 @@ throw new Error('Unable to solve for y = ' + y); |
{ | ||
"private": false, | ||
"name": "curve-interpolator", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Interpolate values on a Cardinal/Catmull-Rom spline curve", | ||
@@ -6,0 +6,0 @@ "repository": "https://github.com/kjerandp/curve-interpolator", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
121331
5.16%1411
5.14%