Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric parameter quality (0..1) instead boolean highestQuality #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions simplify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
81 changes: 69 additions & 12 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand All @@ -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, true);
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();
});