Skip to content

Instantly share code, notes, and snippets.

@veltman
Last active February 20, 2017 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veltman/2f1a7e4fc4fb0b85e66ab3f744a13ada to your computer and use it in GitHub Desktop.
Save veltman/2f1a7e4fc4fb0b85e66ab3f744a13ada to your computer and use it in GitHub Desktop.
Beier-Neely
// Core math for computing displacement of a pixel per Beier-Neely algorithm:
// http://graphics.cs.cmu.edu/courses/15-463/2004_fall/www/Papers/beier-neely.pdf
function computeDisplacement(p1, q1, p2, q2, x2) {
const len1 = distanceBetween(p1, q1),
len2 = distanceBetween(p2, q2),
u = dot(diff(x2, p2), diff(q2, p2)) / (len2 * len2),
v = dot(diff(x2, p2), perpendicular(diff(q2, p2))) / len2,
A = 10,
B = 1,
P = 0.1;
let dist = v,
weight;
const x1 = add(
add(
p1,
diff(q1, p1).map(function(d) {
return d * u;
})
),
perpendicular(diff(q1, p1)).map(function(d) {
return d * v / len1;
})
function computeDisplacement(p1, q1, p2, q2, x2) {
const len1 = distanceBetween(p1, q1),
len2 = distanceBetween(p2, q2),
u = dot(diff(x2, p2), diff(q2, p2)) / (len2 * len2),
v = dot(diff(x2, p2), perpendicular(diff(q2, p2))) / len2,
A = 10,
B = 1,
P = 0.1;
let dist = v,
weight;
const x1 = add(
add(
p1,
diff(q1, p1).map(function(d) {
return d * u;
})
),
perpendicular(diff(q1, p1)).map(function(d) {
return d * v / len1;
})
);
if (u < 0) {
dist = distanceBetween(x2, p2);
} else if (u > 1) {
dist = distanceBetween(x2, q2);
}
return {
weight: Math.pow(Math.pow(len2, P) / (A + dist), B),
displacement: diff(x1, x2)
};
}
function dot(a, b) {
return a[0] * b[0] + a[1] * b[1];
}
function diff(a, b) {
return [a[0] - b[0], a[1] - b[1]];
}
function add(a, b) {
return [a[0] + b[0], a[1] + b[1]];
}
function perpendicular(a) {
return [-a[1], a[0]];
}
function distanceBetween(a, b) {
var dx = b[0] - a[0],
dy = b[1] - a[1];
return Math.sqrt(dx * dx + dy * dy);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment