From db2dc79e605230afd0c30fc5000d6920059a0137 Mon Sep 17 00:00:00 2001 From: Ian B Date: Sat, 23 Mar 2013 21:20:05 +0100 Subject: [PATCH] Refactor Orthogonalize --- js/id/actions/orthogonalize.js | 65 ++++++++++++++++------------------ 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/js/id/actions/orthogonalize.js b/js/id/actions/orthogonalize.js index 140ad5dce..d04317d78 100644 --- a/js/id/actions/orthogonalize.js +++ b/js/id/actions/orthogonalize.js @@ -6,23 +6,27 @@ iD.actions.Orthogonalize = function(wayId, projection) { var action = function(graph) { var way = graph.entity(wayId), nodes = graph.childNodes(way), - points, best, i, j, score, corner; + corner = {i: 0, dotp: 1}, + points, i, j, score, motions; - corner = {i: 0, dotp: 1}; //corner closest to 90 - - if(nodes.length == 4) { + if (nodes.length === 4) { points = _.uniq(nodes).map(function(n) { return projection(n.loc); }); - } else { - points = nodes.map(function(n) { return projection(n.loc); }); - score = squareness(); - } - - for (i = 0; i < 1000; i++) { - var motions = points.map(stepMap); - if(nodes.length == 4) { + for (i = 0; i < 1000; i++) { + motions = points.map(calcMotion); points[corner.i] = addPoints(points[corner.i],motions[corner.i]); score = corner.dotp; - } else { + if (score < 1.0e-8) { + break; + } + } + graph = graph.replace(graph.entity(nodes[corner.i].id) + .move(projection.invert(points[corner.i]))); + } else { + var best; + points = nodes.map(function(n) { return projection(n.loc); }); + score = squareness(); + for (i = 0; i < 1000; i++) { + motions = points.map(calcMotion); for (j = 0; j < motions.length; j++) { points[j] = addPoints(points[j],motions[j]); } @@ -31,26 +35,19 @@ iD.actions.Orthogonalize = function(wayId, projection) { best = _.clone(points); score = newScore; } + if (score < 1.0e-8) { + break; + } } - if (score < 1.0e-8) { - break; - } - } - - if(nodes.length == 4) { - graph = graph.replace(graph.entity(nodes[corner.i].id) - .move(projection.invert(points[corner.i]))); - } else { points = best; for (i = 0; i < points.length - 1; i++) { graph = graph.replace(graph.entity(nodes[i].id) - .move(projection.invert(points[i]))); + .move(projection.invert(points[i]))); } } - return graph; - function stepMap(b, i, array) { + function calcMotion(b, i, array) { var a = array[(i - 1 + array.length) % array.length], c = array[(i + 1) % array.length], p = subtractPoints(a, b), @@ -62,16 +59,16 @@ iD.actions.Orthogonalize = function(wayId, projection) { var dotp = p[0] * q[0] + p[1] * q[1]; - if(nodes.length == 4) { + // nasty hack to deal with almost-straight segments (angle is closer to 180 than to 90/270). + if (array.length > 3) { + if (dotp < -0.707106781186547) { + dotp += 1.0; + } + } else { if( Math.abs(dotp) < corner.dotp){ corner.i = i; corner.dotp = Math.abs(dotp); } - } else { - // nasty hack to deal with almost-straight segments (angle is closer to 180 than to 90/270). - if (dotp < -0.707106781186547) { - dotp += 1.0; - } } return normalizePoint(addPoints(p, q), 0.1 * dotp * scale); @@ -111,7 +108,7 @@ iD.actions.Orthogonalize = function(wayId, projection) { return [a[0] + b[0], a[1] + b[1]]; } - function normalizePoint(point, thickness) { + function normalizePoint(point, scale) { var vector = [0, 0]; var length = Math.sqrt(point[0] * point[0] + point[1] * point[1]); if (length !== 0) { @@ -119,8 +116,8 @@ iD.actions.Orthogonalize = function(wayId, projection) { vector[1] = point[1] / length; } - vector[0] *= thickness; - vector[1] *= thickness; + vector[0] *= scale; + vector[1] *= scale; return vector; }