From 587e5cbe14e0b180e0fbb837ab25dffb0c55d81b Mon Sep 17 00:00:00 2001 From: dom1n1k Date: Mon, 15 Feb 2016 02:31:24 +0300 Subject: [PATCH 1/3] Numeric parameter quality (0..1) instead boolean highestQuality --- simplify.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/simplify.js b/simplify.js index f451078..a6fb3aa 100644 --- a/simplify.js +++ b/simplify.js @@ -100,13 +100,19 @@ function simplifyDouglasPeucker(points, sqTolerance) { } // both algorithms combined for awesome performance -function simplify(points, tolerance, highestQuality) { +function simplify(points, tolerance, quality) { if (points.length <= 2) return points; var sqTolerance = tolerance !== undefined ? tolerance * tolerance : 1; - points = highestQuality ? points : simplifyRadialDist(points, sqTolerance); + quality = quality !== undefined ? +quality : 0; + quality = quality < 0 ? 0 : (quality > 1 ? 1 : quality); + if (quality < 1) { + var sqToleranceFactor = (1 - quality) * (1 - quality); + points = simplifyRadialDist(points, sqTolerance * sqToleranceFactor); + } + points = simplifyDouglasPeucker(points, sqTolerance); return points; From c4ffefef61ee45f19588782afae9ce8257383178 Mon Sep 17 00:00:00 2001 From: dom1n1k Date: Wed, 17 Feb 2016 01:59:41 +0300 Subject: [PATCH 2/3] add tests for numeric quality --- test/test.js | 81 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 12 deletions(-) diff --git a/test/test.js b/test/test.js index 2850850..294b18e 100644 --- a/test/test.js +++ b/test/test.js @@ -26,24 +26,51 @@ var points = [ {x:847.16,y:458.44},{x:851.38,y:462.79},{x:853.97,y:471.15},{x:866.36,y:480.77} ]; -var simplified = [ - {x:224.55,y:250.15},{x:267.76,y:213.81},{x:296.91,y:155.64},{x:330.33,y:137.57}, - {x:409.52,y:141.14},{x:439.60,y:119.74},{x:486.51,y:106.75},{x:529.57,y:127.86}, - {x:539.27,y:147.24},{x:617.74,y:159.86},{x:629.55,y:194.60},{x:671.55,y:222.55}, - {x:727.81,y:213.36},{x:739.94,y:204.77},{x:769.98,y:208.42},{x:779.60,y:216.87}, - {x:800.24,y:214.62},{x:820.77,y:236.17},{x:859.88,y:255.49},{x:865.21,y:268.53}, - {x:857.95,y:280.30},{x:867.79,y:306.17},{x:859.87,y:311.37},{x:854.54,y:335.40}, - {x:860.92,y:343.00},{x:849.84,y:359.59},{x:854.56,y:365.53},{x:844.09,y:371.89}, - {x:839.57,y:390.40},{x:848.40,y:407.55},{x:839.51,y:432.76},{x:853.97,y:471.15}, - {x:866.36,y:480.77} -]; +var simplified = { + // quality == 0 + 0: [ + {x:224.55,y:250.15},{x:267.76,y:213.81},{x:296.91,y:155.64},{x:330.33,y:137.57}, + {x:409.52,y:141.14},{x:439.60,y:119.74},{x:486.51,y:106.75},{x:529.57,y:127.86}, + {x:539.27,y:147.24},{x:617.74,y:159.86},{x:629.55,y:194.60},{x:671.55,y:222.55}, + {x:727.81,y:213.36},{x:739.94,y:204.77},{x:769.98,y:208.42},{x:779.60,y:216.87}, + {x:800.24,y:214.62},{x:820.77,y:236.17},{x:859.88,y:255.49},{x:865.21,y:268.53}, + {x:857.95,y:280.30},{x:867.79,y:306.17},{x:859.87,y:311.37},{x:854.54,y:335.40}, + {x:860.92,y:343.00},{x:849.84,y:359.59},{x:854.56,y:365.53},{x:844.09,y:371.89}, + {x:839.57,y:390.40},{x:848.40,y:407.55},{x:839.51,y:432.76},{x:853.97,y:471.15}, + {x:866.36,y:480.77} + ], + // quality == 0.5 (1 different point) + 0.5: [ + {x:224.55,y:250.15},{x:267.76,y:213.81},{x:296.91,y:155.64},{x:330.33,y:137.57}, + {x:409.52,y:141.14},{x:439.60,y:119.74},{x:486.51,y:106.75},{x:529.57,y:127.86}, + {x:539.27,y:147.24},{x:617.74,y:159.86},{x:629.55,y:194.60},{x:671.55,y:222.55}, + {x:727.81,y:213.36},{x:739.94,y:204.77},{x:769.98,y:208.42},{x:784.20,y:218.16}/*!*/, + {x:800.24,y:214.62},{x:820.77,y:236.17},{x:859.88,y:255.49},{x:865.21,y:268.53}, + {x:857.95,y:280.30},{x:867.79,y:306.17},{x:859.87,y:311.37},{x:854.54,y:335.40}, + {x:860.92,y:343.00},{x:849.84,y:359.59},{x:854.56,y:365.53},{x:844.09,y:371.89}, + {x:839.57,y:390.40},{x:848.40,y:407.55},{x:839.51,y:432.76},{x:853.97,y:471.15}, + {x:866.36,y:480.77} + ], + // quality == 1 (2 different points) + 1: [ + {x:224.55,y:250.15},{x:267.76,y:213.81},{x:296.91,y:155.64},{x:330.33,y:137.57}, + {x:409.52,y:141.14},{x:439.60,y:119.74},{x:486.51,y:106.75},{x:529.57,y:127.86}, + {x:539.27,y:147.24},{x:617.74,y:159.86},{x:629.55,y:194.60},{x:671.55,y:222.55}, + {x:727.81,y:213.36},{x:739.94,y:204.77},{x:769.98,y:208.42},{x:784.20,y:218.16}/*!*/, + {x:800.24,y:214.62},{x:820.77,y:236.17},{x:859.88,y:255.49},{x:865.21,y:268.53}, + {x:857.95,y:280.30},{x:867.79,y:306.17},{x:858.29,y:314.94}/*!*/,{x:854.54,y:335.40}, + {x:860.92,y:343.00},{x:849.84,y:359.59},{x:854.56,y:365.53},{x:844.09,y:371.89}, + {x:839.57,y:390.40},{x:848.40,y:407.55},{x:839.51,y:432.76},{x:853.97,y:471.15}, + {x:866.36,y:480.77} + ] +}; var simplify = require('../simplify'), t = require('tape'); t('simplifies points correctly with the given tolerance', function (t) { var result = simplify(points, 5); - t.same(result, simplified); + t.same(result, simplified[0]); t.end(); }); @@ -58,3 +85,33 @@ t('just return the points if it has no points', function(t){ t.same(result, []); t.end(); }); + +t('quality == 0', function(t){ + var result = simplify(points, 5, 0); + t.same(result, simplified[0]); + t.end(); +}); + +t('quality == false', function(t){ + var result = simplify(points, 5, false); + t.same(result, simplified[0]); + t.end(); +}); + +t('quality == 1', function(t){ + var result = simplify(points, 5, 1); + t.same(result, simplified[1]); + t.end(); +}); + +t('quality == true', function(t){ + var result = simplify(points, 5, 1); + t.same(result, simplified[1]); + t.end(); +}); + +t('quality == 0.5', function(t){ + var result = simplify(points, 5, 0.5); + t.same(result, simplified[0.5]); + t.end(); +}); From a98c6b95b68989db78564ada7a6a4f3b4a071ddc Mon Sep 17 00:00:00 2001 From: dom1n1k Date: Wed, 17 Feb 2016 02:10:26 +0300 Subject: [PATCH 3/3] small fix --- test/test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 294b18e..68331aa 100644 --- a/test/test.js +++ b/test/test.js @@ -105,7 +105,7 @@ t('quality == 1', function(t){ }); t('quality == true', function(t){ - var result = simplify(points, 5, 1); + var result = simplify(points, 5, true); t.same(result, simplified[1]); t.end(); });