Comparing version 1.2.0 to 1.3.0
59
index.js
/* | ||
*/ | ||
exports.Transform = require('./lib/Transform'); | ||
exports.estimateT = require('./lib/estimateT'); | ||
exports.estimateS = require('./lib/estimateS'); | ||
exports.estimateR = require('./lib/estimateR'); | ||
exports.estimateTS = require('./lib/estimateTS'); | ||
exports.estimateTR = require('./lib/estimateTR'); | ||
exports.estimateSR = require('./lib/estimateSR'); | ||
exports.estimateTSR = require('./lib/estimateTSR'); | ||
exports.version = require('./lib/version'); | ||
exports.Transform = require('./lib/Transform') | ||
exports.estimateI = require('./lib/estimateI') | ||
exports.estimateT = require('./lib/estimateT') | ||
exports.estimateS = require('./lib/estimateS') | ||
exports.estimateR = require('./lib/estimateR') | ||
exports.estimateTS = require('./lib/estimateTS') | ||
exports.estimateTR = require('./lib/estimateTR') | ||
exports.estimateSR = require('./lib/estimateSR') | ||
exports.estimateTSR = require('./lib/estimateTSR') | ||
exports.version = require('./lib/version') | ||
@@ -28,11 +29,11 @@ exports.create = function (scale, rotation, tx, ty) { | ||
if (typeof scale !== 'number') { scale = 1; } | ||
if (typeof rotation !== 'number') { rotation = 0; } | ||
if (typeof tx !== 'number') { tx = 0; } | ||
if (typeof ty !== 'number') { ty = 0; } | ||
if (typeof scale !== 'number') { scale = 1 } | ||
if (typeof rotation !== 'number') { rotation = 0 } | ||
if (typeof tx !== 'number') { tx = 0 } | ||
if (typeof ty !== 'number') { ty = 0 } | ||
var s = scale * Math.cos(rotation); | ||
var r = scale * Math.sin(rotation); | ||
return new exports.Transform(s, r, tx, ty); | ||
}; | ||
var s = scale * Math.cos(rotation) | ||
var r = scale * Math.sin(rotation) | ||
return new exports.Transform(s, r, tx, ty) | ||
} | ||
@@ -50,8 +51,8 @@ exports.createFromArray = function (arr) { | ||
var s = arr[0]; | ||
var r = arr[1]; | ||
var tx = arr[2]; | ||
var ty = arr[3]; | ||
return new exports.Transform(s, r, tx, ty); | ||
}; | ||
var s = arr[0] | ||
var r = arr[1] | ||
var tx = arr[2] | ||
var ty = arr[3] | ||
return new exports.Transform(s, r, tx, ty) | ||
} | ||
@@ -61,3 +62,4 @@ exports.estimate = function (type, domain, range, pivot) { | ||
// type | ||
// string. One of the following: 'T', 'S', 'R', 'TS', 'TR', 'SR', 'TSR' | ||
// string. One of the following: | ||
// 'I', 'T', 'S', 'R', 'TS', 'TR', 'SR', 'TSR' | ||
// domain | ||
@@ -69,7 +71,8 @@ // array of 2d arrays | ||
// optional 2d array, does nothing for translation estimators | ||
var name = 'estimate' + type.toUpperCase(); | ||
// | ||
var name = 'estimate' + type.toUpperCase() | ||
if (exports.hasOwnProperty(name)) { | ||
return exports[name](domain, range, pivot); | ||
return exports[name](domain, range, pivot) | ||
} // else | ||
throw new Error('Unknown estimator type: ' + type); | ||
}; | ||
throw new Error('Unknown estimator type: ' + type) | ||
} |
@@ -1,31 +0,31 @@ | ||
var Transform = require('./Transform'); | ||
var Transform = require('./Transform') | ||
module.exports = function (domain, range, pivot) { | ||
var i, N, D, a0, b0, a, b, c, d, ac, ad, bc, bd, shat, rhat, tx, ty; | ||
var i, N, D, a0, b0, a, b, c, d, ac, ad, bc, bd, p, q, shat, rhat, tx, ty | ||
N = Math.min(domain.length, range.length); | ||
ac = ad = bc = bd = 0; | ||
N = Math.min(domain.length, range.length) | ||
ac = ad = bc = bd = 0 | ||
if (typeof pivot === 'undefined') { | ||
a0 = b0 = 0; | ||
a0 = b0 = 0 | ||
} else { | ||
a0 = pivot[0]; | ||
b0 = pivot[1]; | ||
a0 = pivot[0] | ||
b0 = pivot[1] | ||
} | ||
for (i = 0; i < N; i += 1) { | ||
a = domain[i][0] - a0; | ||
b = domain[i][1] - b0; | ||
c = range[i][0] - a0; | ||
d = range[i][1] - b0; | ||
ac += a * c; | ||
ad += a * d; | ||
bc += b * c; | ||
bd += b * d; | ||
a = domain[i][0] - a0 | ||
b = domain[i][1] - b0 | ||
c = range[i][0] - a0 | ||
d = range[i][1] - b0 | ||
ac += a * c | ||
ad += a * d | ||
bc += b * c | ||
bd += b * d | ||
} | ||
p = ac + bd; | ||
q = ad - bc; | ||
p = ac + bd | ||
q = ad - bc | ||
D = Math.sqrt(p * p + q * q); | ||
D = Math.sqrt(p * p + q * q) | ||
@@ -40,11 +40,11 @@ if (D === 0) { | ||
// Assume identity transform to be the best guess | ||
return Transform.IDENTITY; | ||
return Transform.IDENTITY | ||
} | ||
shat = p / D; | ||
rhat = q / D; | ||
tx = a0 - a0 * shat + b0 * rhat; | ||
ty = b0 - a0 * rhat - b0 * shat; | ||
shat = p / D | ||
rhat = q / D | ||
tx = a0 - a0 * shat + b0 * rhat | ||
ty = b0 - a0 * rhat - b0 * shat | ||
return new Transform(shat, rhat, tx, ty); | ||
}; | ||
return new Transform(shat, rhat, tx, ty) | ||
} |
@@ -1,28 +0,28 @@ | ||
var Transform = require('./Transform'); | ||
var Transform = require('./Transform') | ||
module.exports = function (domain, range, pivot) { | ||
var i, N, D, a0, b0, a, b, c, d, ac, bd, aa, bb, shat, tx, ty; | ||
var i, N, D, a0, b0, a, b, c, d, ac, bd, aa, bb, shat, tx, ty | ||
N = Math.min(domain.length, range.length); | ||
ac = bd = aa = bb = 0; | ||
N = Math.min(domain.length, range.length) | ||
ac = bd = aa = bb = 0 | ||
if (typeof pivot === 'undefined') { | ||
a0 = b0 = 0; | ||
a0 = b0 = 0 | ||
} else { | ||
a0 = pivot[0]; | ||
b0 = pivot[1]; | ||
a0 = pivot[0] | ||
b0 = pivot[1] | ||
} | ||
for (i = 0; i < N; i += 1) { | ||
a = domain[i][0] - a0; | ||
b = domain[i][1] - b0; | ||
c = range[i][0] - a0; | ||
d = range[i][1] - b0; | ||
ac += a * c; | ||
bd += b * d; | ||
aa += a * a; | ||
bb += b * b; | ||
a = domain[i][0] - a0 | ||
b = domain[i][1] - b0 | ||
c = range[i][0] - a0 | ||
d = range[i][1] - b0 | ||
ac += a * c | ||
bd += b * d | ||
aa += a * a | ||
bb += b * b | ||
} | ||
D = aa + bb; | ||
D = aa + bb | ||
@@ -34,3 +34,3 @@ if (D === 0) { | ||
// Assume identity transform to be the best guess | ||
return Transform.IDENTITY; | ||
return Transform.IDENTITY | ||
} | ||
@@ -40,7 +40,7 @@ | ||
// and rotation => limit to zero | ||
shat = Math.max(0, (ac + bd) / D); | ||
tx = (1 - shat) * a0; | ||
ty = (1 - shat) * b0; | ||
shat = Math.max(0, (ac + bd) / D) | ||
tx = (1 - shat) * a0 | ||
ty = (1 - shat) * b0 | ||
return new Transform(shat, 0, tx, ty); | ||
}; | ||
return new Transform(shat, 0, tx, ty) | ||
} |
@@ -1,2 +0,2 @@ | ||
var Transform = require('./Transform'); | ||
var Transform = require('./Transform') | ||
@@ -20,37 +20,37 @@ module.exports = function (domain, range, pivot) { | ||
// | ||
var X, Y, N, s, r, tx, ty; | ||
var X, Y, N, s, r, tx, ty | ||
// Optional pivot | ||
if (typeof pivot === 'undefined') { | ||
pivot = [0, 0]; | ||
pivot = [0, 0] | ||
} | ||
// Alias | ||
X = domain; | ||
Y = range; | ||
X = domain | ||
Y = range | ||
// Allow arrays of different length but | ||
// ignore the extra points. | ||
N = Math.min(X.length, Y.length); | ||
N = Math.min(X.length, Y.length) | ||
var v = pivot[0]; | ||
var w = pivot[1]; | ||
var v = pivot[0] | ||
var w = pivot[1] | ||
var i, a, b, c, d; | ||
var a2, b2; | ||
a2 = b2 = 0; | ||
var ac, bd, bc, ad; | ||
ac = bd = bc = ad = 0; | ||
var i, a, b, c, d | ||
var a2, b2 | ||
a2 = b2 = 0 | ||
var ac, bd, bc, ad | ||
ac = bd = bc = ad = 0 | ||
for (i = 0; i < N; i += 1) { | ||
a = X[i][0] - v; | ||
b = X[i][1] - w; | ||
c = Y[i][0] - v; | ||
d = Y[i][1] - w; | ||
a2 += a * a; | ||
b2 += b * b; | ||
ac += a * c; | ||
bd += b * d; | ||
bc += b * c; | ||
ad += a * d; | ||
a = X[i][0] - v | ||
b = X[i][1] - w | ||
c = Y[i][0] - v | ||
d = Y[i][1] - w | ||
a2 += a * a | ||
b2 += b * b | ||
ac += a * c | ||
bd += b * d | ||
bc += b * c | ||
ad += a * d | ||
} | ||
@@ -62,5 +62,5 @@ | ||
// there is no domain points. | ||
var den = a2 + b2; | ||
var den = a2 + b2 | ||
var eps = 0.00000001; | ||
var eps = 0.00000001 | ||
if (Math.abs(den) < eps) { | ||
@@ -70,12 +70,12 @@ // The domain points are under the pivot or there is no domain points. | ||
// the domain points under the pivot if there is some. | ||
return new Transform(1, 0, 0, 0); | ||
return new Transform(1, 0, 0, 0) | ||
} | ||
// Estimators | ||
s = (ac + bd) / den; | ||
r = (-bc + ad) / den; | ||
tx = w * r - v * s + v; | ||
ty = -v * r - w * s + w; | ||
s = (ac + bd) / den | ||
r = (-bc + ad) / den | ||
tx = w * r - v * s + v | ||
ty = -v * r - w * s + w | ||
return new Transform(s, r, tx, ty); | ||
}; | ||
return new Transform(s, r, tx, ty) | ||
} |
@@ -1,25 +0,25 @@ | ||
var Transform = require('./Transform'); | ||
var Transform = require('./Transform') | ||
module.exports = function (domain, range) { | ||
var i, N, a1, b1, c1, d1, txhat, tyhat; | ||
var i, N, a1, b1, c1, d1, txhat, tyhat | ||
N = Math.min(domain.length, range.length); | ||
a1 = b1 = c1 = d1 = 0; | ||
N = Math.min(domain.length, range.length) | ||
a1 = b1 = c1 = d1 = 0 | ||
if (N < 1) { | ||
// Assume identity transform be the best guess | ||
return Transform.IDENTITY; | ||
return Transform.IDENTITY | ||
} | ||
for (i = 0; i < N; i += 1) { | ||
a1 += domain[i][0]; | ||
b1 += domain[i][1]; | ||
c1 += range[i][0]; | ||
d1 += range[i][1]; | ||
a1 += domain[i][0] | ||
b1 += domain[i][1] | ||
c1 += range[i][0] | ||
d1 += range[i][1] | ||
} | ||
txhat = (c1 - a1) / N; | ||
tyhat = (d1 - b1) / N; | ||
txhat = (c1 - a1) / N | ||
tyhat = (d1 - b1) / N | ||
return new Transform(1, 0, txhat, tyhat); | ||
}; | ||
return new Transform(1, 0, txhat, tyhat) | ||
} |
@@ -1,2 +0,2 @@ | ||
var Transform = require('./Transform'); | ||
var Transform = require('./Transform') | ||
@@ -11,30 +11,30 @@ module.exports = function (domain, range) { | ||
// Alias | ||
var X = domain; | ||
var Y = range; | ||
var X = domain | ||
var Y = range | ||
// Allow arrays of different length but | ||
// ignore the extra points. | ||
var N = Math.min(X.length, Y.length); | ||
var N = Math.min(X.length, Y.length) | ||
var i, a, b, c, d, a1, b1, c1, d1, ac, ad, bc, bd; | ||
a1 = b1 = c1 = d1 = ac = ad = bc = bd = 0; | ||
var i, a, b, c, d, a1, b1, c1, d1, ac, ad, bc, bd | ||
a1 = b1 = c1 = d1 = ac = ad = bc = bd = 0 | ||
for (i = 0; i < N; i += 1) { | ||
a = X[i][0]; | ||
b = X[i][1]; | ||
c = Y[i][0]; | ||
d = Y[i][1]; | ||
a1 += a; | ||
b1 += b; | ||
c1 += c; | ||
d1 += d; | ||
ac += a * c; | ||
ad += a * d; | ||
bc += b * c; | ||
bd += b * d; | ||
a = X[i][0] | ||
b = X[i][1] | ||
c = Y[i][0] | ||
d = Y[i][1] | ||
a1 += a | ||
b1 += b | ||
c1 += c | ||
d1 += d | ||
ac += a * c | ||
ad += a * d | ||
bc += b * c | ||
bd += b * d | ||
} | ||
// Denominator. | ||
var v = N * (ad - bc) - a1 * d1 + b1 * c1; | ||
var w = N * (ac + bd) - a1 * c1 - b1 * d1; | ||
var D = Math.sqrt(v * v + w * w); | ||
var v = N * (ad - bc) - a1 * d1 + b1 * c1 | ||
var w = N * (ac + bd) - a1 * c1 - b1 * d1 | ||
var D = Math.sqrt(v * v + w * w) | ||
@@ -44,3 +44,3 @@ if (D === 0) { | ||
if (N === 0) { | ||
return new Transform(1, 0, 0, 0); | ||
return new Transform(1, 0, 0, 0) | ||
} // else | ||
@@ -50,12 +50,12 @@ // D === 0 <=> undecidable | ||
// Here a, b represents the mean of domain points. | ||
return new Transform(1, 0, (c1 - a1) / N, (d1 - b1) / N); | ||
return new Transform(1, 0, (c1 - a1) / N, (d1 - b1) / N) | ||
} | ||
// Estimators | ||
var shat = w / D; | ||
var rhat = v / D; | ||
var txhat = (-a1 * shat + b1 * rhat + c1) / N; | ||
var tyhat = (-a1 * rhat - b1 * shat + d1) / N; | ||
var shat = w / D | ||
var rhat = v / D | ||
var txhat = (-a1 * shat + b1 * rhat + c1) / N | ||
var tyhat = (-a1 * rhat - b1 * shat + d1) / N | ||
return new Transform(shat, rhat, txhat, tyhat); | ||
}; | ||
return new Transform(shat, rhat, txhat, tyhat) | ||
} |
@@ -1,2 +0,2 @@ | ||
var Transform = require('./Transform'); | ||
var Transform = require('./Transform') | ||
@@ -11,33 +11,33 @@ module.exports = function (domain, range) { | ||
// Alias | ||
var X = domain; | ||
var Y = range; | ||
var X = domain | ||
var Y = range | ||
// Allow arrays of different length but | ||
// ignore the extra points. | ||
var N = Math.min(X.length, Y.length); | ||
var N = Math.min(X.length, Y.length) | ||
var i, a, b, c, d, a1, b1, c1, d1, a2, b2, ac, bd; | ||
a1 = b1 = c1 = d1 = a2 = b2 = ac = bd = 0; | ||
var i, a, b, c, d, a1, b1, c1, d1, a2, b2, ac, bd | ||
a1 = b1 = c1 = d1 = a2 = b2 = ac = bd = 0 | ||
for (i = 0; i < N; i += 1) { | ||
a = X[i][0]; | ||
b = X[i][1]; | ||
c = Y[i][0]; | ||
d = Y[i][1]; | ||
a1 += a; | ||
b1 += b; | ||
c1 += c; | ||
d1 += d; | ||
a2 += a * a; | ||
b2 += b * b; | ||
ac += a * c; | ||
bd += b * d; | ||
a = X[i][0] | ||
b = X[i][1] | ||
c = Y[i][0] | ||
d = Y[i][1] | ||
a1 += a | ||
b1 += b | ||
c1 += c | ||
d1 += d | ||
a2 += a * a | ||
b2 += b * b | ||
ac += a * c | ||
bd += b * d | ||
} | ||
// Denominator. | ||
var N2 = N * N; | ||
var a12 = a1 * a1; | ||
var b12 = b1 * b1; | ||
var p = a2 + b2; | ||
var q = ac + bd; | ||
var D = N2 * p - N * (a12 + b12); | ||
var N2 = N * N | ||
var a12 = a1 * a1 | ||
var b12 = b1 * b1 | ||
var p = a2 + b2 | ||
var q = ac + bd | ||
var D = N2 * p - N * (a12 + b12) | ||
@@ -47,3 +47,3 @@ if (D === 0) { | ||
if (N === 0) { | ||
return new Transform(1, 0, 0, 0); | ||
return new Transform(1, 0, 0, 0) | ||
} // else | ||
@@ -53,11 +53,11 @@ // D === 0 <=> all the domain points are the same | ||
// Here a, b represents the mean of domain points. | ||
return new Transform(1, 0, (c1 / N) - a, (d1 / N) - b); | ||
return new Transform(1, 0, (c1 / N) - a, (d1 / N) - b) | ||
} | ||
// Estimators | ||
var shat = (N2 * q - N * (a1 * c1 + b1 * d1)) / D; | ||
var txhat = (-N * a1 * q + N * c1 * p - b12 * c1 + a1 * b1 * d1) / D; | ||
var tyhat = (-N * b1 * q + N * d1 * p - a12 * d1 + a1 * b1 * c1) / D; | ||
var shat = (N2 * q - N * (a1 * c1 + b1 * d1)) / D | ||
var txhat = (-N * a1 * q + N * c1 * p - b12 * c1 + a1 * b1 * d1) / D | ||
var tyhat = (-N * b1 * q + N * d1 * p - a12 * d1 + a1 * b1 * c1) / D | ||
return new Transform(shat, 0, txhat, tyhat); | ||
}; | ||
return new Transform(shat, 0, txhat, tyhat) | ||
} |
@@ -1,2 +0,2 @@ | ||
var Transform = require('./Transform'); | ||
var Transform = require('./Transform') | ||
@@ -9,11 +9,11 @@ module.exports = function (domain, range) { | ||
// array of [x, y] 2D arrays | ||
var X, Y, N, s, r, tx, ty; | ||
var X, Y, N, s, r, tx, ty | ||
// Alias | ||
X = domain; | ||
Y = range; | ||
X = domain | ||
Y = range | ||
// Allow arrays of different length but | ||
// ignore the extra points. | ||
N = Math.min(X.length, Y.length); | ||
N = Math.min(X.length, Y.length) | ||
@@ -23,31 +23,31 @@ // If length is zero, no estimation can be done. We choose the indentity | ||
if (N === 0) { | ||
return new Transform(1, 0, 0, 0); | ||
return new Transform(1, 0, 0, 0) | ||
} // else | ||
var i, a, b, c, d; | ||
var a1 = 0; | ||
var b1 = 0; | ||
var c1 = 0; | ||
var d1 = 0; | ||
var a2 = 0; | ||
var b2 = 0; | ||
var ad = 0; | ||
var bc = 0; | ||
var ac = 0; | ||
var bd = 0; | ||
var i, a, b, c, d | ||
var a1 = 0 | ||
var b1 = 0 | ||
var c1 = 0 | ||
var d1 = 0 | ||
var a2 = 0 | ||
var b2 = 0 | ||
var ad = 0 | ||
var bc = 0 | ||
var ac = 0 | ||
var bd = 0 | ||
for (i = 0; i < N; i += 1) { | ||
a = X[i][0]; | ||
b = X[i][1]; | ||
c = Y[i][0]; | ||
d = Y[i][1]; | ||
a1 += a; | ||
b1 += b; | ||
c1 += c; | ||
d1 += d; | ||
a2 += a * a; | ||
b2 += b * b; | ||
ad += a * d; | ||
bc += b * c; | ||
ac += a * c; | ||
bd += b * d; | ||
a = X[i][0] | ||
b = X[i][1] | ||
c = Y[i][0] | ||
d = Y[i][1] | ||
a1 += a | ||
b1 += b | ||
c1 += c | ||
d1 += d | ||
a2 += a * a | ||
b2 += b * b | ||
ad += a * d | ||
bc += b * c | ||
ac += a * c | ||
bd += b * d | ||
} | ||
@@ -58,5 +58,5 @@ | ||
// In other words, iff all the domain points are the same or there is only one domain point. | ||
var den = N * a2 + N * b2 - a1 * a1 - b1 * b1; | ||
var den = N * a2 + N * b2 - a1 * a1 - b1 * b1 | ||
var eps = 0.00000001; | ||
var eps = 0.00000001 | ||
if (-eps < den && den < eps) { | ||
@@ -66,12 +66,12 @@ // The domain points are the same. | ||
// Here a, b represents the mean of domain points. | ||
return new Transform(1, 0, (c1 / N) - a, (d1 / N) - b); | ||
return new Transform(1, 0, (c1 / N) - a, (d1 / N) - b) | ||
} | ||
// Estimators | ||
s = (N * (ac + bd) - a1 * c1 - b1 * d1) / den; | ||
r = (N * (ad - bc) + b1 * c1 - a1 * d1) / den; | ||
tx = (-a1 * (ac + bd) + b1 * (ad - bc) + a2 * c1 + b2 * c1) / den; | ||
ty = (-b1 * (ac + bd) - a1 * (ad - bc) + a2 * d1 + b2 * d1) / den; | ||
s = (N * (ac + bd) - a1 * c1 - b1 * d1) / den | ||
r = (N * (ad - bc) + b1 * c1 - a1 * d1) / den | ||
tx = (-a1 * (ac + bd) + b1 * (ad - bc) + a2 * c1 + b2 * c1) / den | ||
ty = (-b1 * (ac + bd) - a1 * (ad - bc) + a2 * d1 + b2 * d1) / den | ||
return new Transform(s, r, tx, ty); | ||
}; | ||
return new Transform(s, r, tx, ty) | ||
} |
var Transform = function (s, r, tx, ty) { | ||
// Public, to allow user access | ||
this.s = s; | ||
this.r = r; | ||
this.tx = tx; | ||
this.ty = ty; | ||
this.s = s | ||
this.r = r | ||
this.tx = tx | ||
this.ty = ty | ||
this.equals = function (t) { | ||
return (s === t.s && r === t.r && tx === t.tx && ty === t.ty); | ||
}; | ||
return (s === t.s && r === t.r && tx === t.tx && ty === t.ty) | ||
} | ||
@@ -24,19 +23,19 @@ this.getMatrix = function () { | ||
// [ 0 0 1 ] [ - - - ] | ||
return { a: s, b: r, c: -r, d: s, e: tx, f: ty }; | ||
}; | ||
return { a: s, b: r, c: -r, d: s, e: tx, f: ty } | ||
} | ||
this.getRotation = function () { | ||
// in rads | ||
return Math.atan2(r, s); | ||
}; | ||
return Math.atan2(r, s) | ||
} | ||
this.getScale = function () { | ||
// scale multiplier | ||
return Math.sqrt(r * r + s * s); | ||
}; | ||
return Math.sqrt(r * r + s * s) | ||
} | ||
this.getTranslation = function () { | ||
// Current translation as a point. | ||
return [tx, ty]; | ||
}; | ||
return [tx, ty] | ||
} | ||
@@ -48,6 +47,5 @@ this.toArray = function () { | ||
// serialization and deserialization to and from JSON possible. | ||
return [s, r, tx, ty]; | ||
}; | ||
return [s, r, tx, ty] | ||
} | ||
// Methods that return new points | ||
@@ -61,13 +59,13 @@ | ||
// Single point | ||
return [s * p[0] - r * p[1] + tx, r * p[0] + s * p[1] + ty]; | ||
return [s * p[0] - r * p[1] + tx, r * p[0] + s * p[1] + ty] | ||
} // else | ||
var i, c = []; | ||
var i | ||
var c = [] | ||
for (i = 0; i < p.length; i += 1) { | ||
c.push([s * p[i][0] - r * p[i][1] + tx, r * p[i][0] + s * p[i][1] + ty]); | ||
c.push([s * p[i][0] - r * p[i][1] + tx, r * p[i][0] + s * p[i][1] + ty]) | ||
} | ||
return c; | ||
}; | ||
return c | ||
} | ||
// Methods that return new Transformations | ||
@@ -78,19 +76,19 @@ | ||
// See note 2015-10-26-16-30 | ||
var det = s * s + r * r; | ||
var det = s * s + r * r | ||
// Test if singular transformation. These might occur when all the range | ||
// points are the same, forcing the scale to drop to zero. | ||
var eps = 0.00000001; | ||
var eps = 0.00000001 | ||
if (Math.abs(det) < eps) { | ||
throw new Error('Singular transformations cannot be inversed.'); | ||
throw new Error('Singular transformations cannot be inversed.') | ||
} | ||
var shat = s / det; | ||
var rhat = -r / det; | ||
var txhat = (-s * tx - r * ty) / det; | ||
var tyhat = ( r * tx - s * ty) / det; | ||
return new Transform(shat, rhat, txhat, tyhat); | ||
}; | ||
var shat = s / det | ||
var rhat = -r / det | ||
var txhat = (-s * tx - r * ty) / det | ||
var tyhat = (r * tx - s * ty) / det | ||
return new Transform(shat, rhat, txhat, tyhat) | ||
} | ||
this.translateBy = function (dx, dy) { | ||
return new Transform(s, r, tx + dx, ty + dy); | ||
}; | ||
return new Transform(s, r, tx + dx, ty + dy) | ||
} | ||
@@ -102,12 +100,12 @@ this.scaleBy = function (multiplier, pivot) { | ||
// optional, a [x, y] point | ||
var m, x, y; | ||
m = multiplier; // alias | ||
var m, x, y | ||
m = multiplier // alias | ||
if (typeof pivot === 'undefined') { | ||
x = y = 0; | ||
x = y = 0 | ||
} else { | ||
x = pivot[0]; | ||
y = pivot[1]; | ||
x = pivot[0] | ||
y = pivot[1] | ||
} | ||
return new Transform(m * s, m * r, m * tx + (1-m) * x, m * ty + (1-m) * y); | ||
}; | ||
return new Transform(m * s, m * r, m * tx + (1 - m) * x, m * ty + (1 - m) * y) | ||
} | ||
@@ -120,19 +118,19 @@ this.rotateBy = function (radians, pivot) { | ||
// optional, a [x, y] point | ||
var co, si, x, y, shat, rhat, txhat, tyhat; | ||
co = Math.cos(radians); | ||
si = Math.sin(radians); | ||
var co, si, x, y, shat, rhat, txhat, tyhat | ||
co = Math.cos(radians) | ||
si = Math.sin(radians) | ||
if (typeof pivot === 'undefined') { | ||
x = y = 0; | ||
x = y = 0 | ||
} else { | ||
x = pivot[0]; | ||
y = pivot[1]; | ||
x = pivot[0] | ||
y = pivot[1] | ||
} | ||
shat = s * co - r * si; | ||
rhat = s * si + r * co; | ||
txhat = (tx - x) * co - (ty - y) * si + x; | ||
tyhat = (tx - x) * si + (ty - y) * co + y; | ||
return new Transform(shat, rhat, txhat, tyhat); | ||
}; | ||
shat = s * co - r * si | ||
rhat = s * si + r * co | ||
txhat = (tx - x) * co - (ty - y) * si + x | ||
tyhat = (tx - x) * si + (ty - y) * co + y | ||
return new Transform(shat, rhat, txhat, tyhat) | ||
} | ||
this.multiplyRight = | ||
this.multiplyBy = function (transform) { | ||
@@ -147,13 +145,17 @@ // Multiply this transformation matrix A | ||
// 0 0 1 0 0 1 | ||
var t = transform; // alias | ||
var shat = s * t.s - r * t.r; | ||
var rhat = s * t.r + r * t.s; | ||
var txhat = s * t.tx - r * t.ty + tx; | ||
var tyhat = r * t.tx + s * t.ty + ty; | ||
return new Transform(shat, rhat, txhat, tyhat); | ||
}; | ||
}; | ||
var t = transform // alias | ||
var shat = s * t.s - r * t.r | ||
var rhat = s * t.r + r * t.s | ||
var txhat = s * t.tx - r * t.ty + tx | ||
var tyhat = r * t.tx + s * t.ty + ty | ||
return new Transform(shat, rhat, txhat, tyhat) | ||
} | ||
} | ||
Transform.IDENTITY = new Transform(1, 0, 0, 0); | ||
Transform.IDENTITY = new Transform(1, 0, 0, 0) | ||
Transform.R90 = new Transform(0, 1, 0, 0) | ||
Transform.R180 = new Transform(-1, 0, 0, 0) | ||
Transform.R270 = new Transform(0, -1, 0, 0) | ||
Transform.X2 = new Transform(2, 0, 0, 0) | ||
module.exports = Transform; | ||
module.exports = Transform |
@@ -1,1 +0,2 @@ | ||
module.exports = '1.2.0'; | ||
// generated by genversion | ||
module.exports = '1.3.0' |
{ | ||
"name": "nudged", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Affine transformation estimator e.g. for multi-touch gestures and calibration", | ||
@@ -31,10 +31,11 @@ "keywords": [ | ||
"devDependencies": { | ||
"jshint": "^2.8.0", | ||
"mocha": "^3.1.2", | ||
"should": "^11.1.1", | ||
"loadimages": "^0.2.2", | ||
"browserify": "^13.1.1", | ||
"browserify": "^15.0.0", | ||
"component-emitter": "^1.2.0", | ||
"genversion": "^2.0.1", | ||
"hammerjs": "^2.0.4", | ||
"component-emitter": "^1.2.0", | ||
"lodash": "^4.17.2" | ||
"loadimages": "^1.0.0", | ||
"lodash": "^4.17.2", | ||
"mocha": "^4.1.0", | ||
"should": "^13.2.0", | ||
"standard": "^10.0.3" | ||
}, | ||
@@ -46,16 +47,13 @@ "scripts": { | ||
"test:unit": "mocha test/index.test.js", | ||
"test:lint": "jshint index.js lib/*.js test/*.js", | ||
"test:nudged-editor": "jshint examples/nudged-editor/src/*.js", | ||
"build:nudged-editor": "npm run test:nudged-editor && browserify examples/nudged-editor/src/index.js -o examples/nudged-editor/app.js", | ||
"test:nudged-map": "jshint examples/nudged-map/src/*.js", | ||
"build:nudged-map": "npm run test:nudged-map && browserify examples/nudged-map/src/index.js -o examples/nudged-map/app.js", | ||
"test:nudged-gesture": "jshint examples/nudged-gesture/src/*.js", | ||
"build:nudged-gesture": "npm run test:nudged-gesture && browserify examples/nudged-gesture/src/index.js -o examples/nudged-gesture/app.js", | ||
"test:typical-gesture": "jshint examples/typical-gesture/src/*.js", | ||
"build:typical-gesture": "npm run test:typical-gesture && browserify examples/typical-gesture/src/index.js -o examples/typical-gesture/app.js", | ||
"test:lint": "standard index.js 'lib/**/*.js' 'test/**/*.js'", | ||
"test:lint:fix": "standard --fix index.js 'lib/**/*.js' 'test/**/*.js'", | ||
"gv": "genversion lib/version.js", | ||
"test:nudged-editor": "standard --fix 'examples/nudged-editor/lib/*.js'", | ||
"build:nudged-editor": "npm run test:nudged-editor && browserify examples/nudged-editor/lib/index.js -o examples/nudged-editor/dist/app.js", | ||
"test:nudged-map": "standard --fix 'examples/nudged-map/lib/*.js'", | ||
"build:nudged-map": "npm run test:nudged-map && browserify examples/nudged-map/lib/index.js -o examples/nudged-map/dist/app.js", | ||
"test:nudged-gesture": "standard --fix 'examples/nudged-gesture/lib/*.js'", | ||
"build:nudged-gesture": "npm run test:nudged-gesture && browserify examples/nudged-gesture/lib/index.js -o examples/nudged-gesture/dist/app.js", | ||
"test:typical-gesture": "standard --fix 'examples/typical-gesture/lib/*.js'", | ||
"build:typical-gesture": "npm run test:typical-gesture && browserify examples/typical-gesture/lib/index.js -o examples/typical-gesture/dist/app.js", | ||
"build:examples": "npm run build:nudged-editor && npm run build:nudged-gesture && npm run build:typical-gesture && npm run build:nudged-map", | ||
@@ -62,0 +60,0 @@ "build:standalone": "browserify index.js --standalone nudged -o nudged.js" |
@@ -1,2 +0,2 @@ | ||
# nudged<sup>1.2.0</sup> | ||
# nudged | ||
@@ -170,6 +170,6 @@ [![NPM Version](https://img.shields.io/npm/v/nudged.svg)](https://www.npmjs.com/package/nudged) | ||
Compute an optimal affine transformation from the *domain* to *range* points. The *type* of transformation is any combination of translation `T`, scaling `S`, and rotation `R`, in this order. The transformations without translation allow an optional fixed *pivot* point. | ||
Compute an optimal affine transformation from the *domain* to *range* points. The *type* of transformation is any combination of translation `T`, scaling `S`, and rotation `R`, in this order. A special type `I` returns always the identity transformation. Transformations without translation (`S`, `R`, `SR`) allow an optional fixed *pivot* point. | ||
**Parameters:** | ||
- *type*: string, freedom of the transformation. Types available: `'T'`, `'S'`, `'R'`, `'TS'`, `'TR'`, `'SR'`, `'TSR'` | ||
- *type*: string, freedom of the transformation. Types available: `'I'`, `'T'`, `'S'`, `'R'`, `'TS'`, `'TR'`, `'SR'`, `'TSR'` | ||
- *domain*: array of [x,y] points | ||
@@ -185,2 +185,3 @@ - *range*: array of [x,y] points | ||
- `nudged.estimateI()` | ||
- `nudged.estimateT(domain, range)` | ||
@@ -194,3 +195,13 @@ - `nudged.estimateS(domain, range, pivot)` | ||
**Example:** | ||
> var domain = [[0,0], [2,0], [ 1,2]] | ||
> var range = [[1,1], [1,3], [-1,2]] | ||
> var tr = nudged.estimate('SR', domain, range) | ||
> tr.getScale() | ||
1.242259 | ||
> tr.getRotation() | ||
1.107148 | ||
### nudged.version | ||
@@ -220,9 +231,22 @@ | ||
### nudged.Transform.IDENTITY | ||
A default instance of `nudged.Transform` that represents the identity transformation i.e. transformation without an effect. You can use it in building new transformations: | ||
A default instance of `nudged.Transform` that represents the identity transformation `new Transform(1, 0, 0, 0)` i.e. transformation without an effect. You can use it in building new transformations: | ||
> var trans = nudged.Transform.IDENTITY.scaleBy(0.6).rotateBy(0.3); | ||
### nudged.Transform.R90 .R180 .R270 .X2 | ||
Following prebuilt `Transform` instances are available: | ||
- `R90`: clockwise 90 degree rotation. Equal to `new Transform(0, 1, 0, 0)`. | ||
- `R180`: 180 degree rotation. Equal to `new Transform(-1, 0, 0, 0)`. | ||
- `R270`: counterclockwise 90 degree rotation. Equal to `new Transform(0, -1, 0, 0)`. | ||
- `X2`: scale up by the factor of two. Equal to `new Transform(2, 0, 0, 0)`. | ||
**Example:** | ||
> nudged.Transform.X2.getScale() | ||
2 | ||
### nudged.Transform#s, #r, #tx, #ty | ||
@@ -325,3 +349,3 @@ | ||
### nudged.Transform#multiplyBy(tr) | ||
### nudged.Transform#multiplyBy(tr) alias #multiplyRight(tr) | ||
@@ -352,3 +376,4 @@ **Parameter** `tr` is an instance of `nudged.Transform`. | ||
- Bump version and run tests. | ||
- Bump version in package.json, `npm run gv`, and run tests. | ||
- Build examples `npm run build:examples` | ||
- Create release branch. See [tutorial](https://www.atlassian.com/git/tutorials/comparing-workflows/gitflow-workflow). | ||
@@ -360,3 +385,3 @@ - `$ git checkout -b release-7.7.7 development` | ||
- Commit: `$ git commit -a -m "Clean release 7.7.7"` | ||
- Merge (see the tut above): | ||
- Merge (see the tutorial link above): | ||
- `$ git checkout master` | ||
@@ -363,0 +388,0 @@ - `$ git merge release-7.7.7` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
409
0
35363
9
15
535