From 245c8d431acd2e74bad69b175fdb9451a51fb8b2 Mon Sep 17 00:00:00 2001 From: Rowan Winsemius Date: Wed, 8 Jan 2020 06:19:53 +1100 Subject: [PATCH] Use robust-predicates in signed area (#111) * Use robust-predicates in signed area * Add issue 102 fixture * add more fixtures and update package-lock.json --- package-lock.json | 9 +++-- package.json | 1 + src/signed_area.js | 7 +++- test/fixtures/issue100.geojson | 74 ++++++++++++++++++++++++++++++++++ test/fixtures/issue102.geojson | 29 +++++++++++++ test/fixtures/issue103.geojson | 18 +++++++++ test/fixtures/issue110.geojson | 18 +++++++++ test/fixtures/issue90.geojson | 18 +++++++++ test/fixtures/issue99.geojson | 12 ++++++ 9 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/issue100.geojson create mode 100644 test/fixtures/issue102.geojson create mode 100644 test/fixtures/issue103.geojson create mode 100644 test/fixtures/issue110.geojson create mode 100644 test/fixtures/issue90.geojson create mode 100644 test/fixtures/issue99.geojson diff --git a/package-lock.json b/package-lock.json index 44c6372..8663f90 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2295,7 +2295,6 @@ "version": "0.1.4", "bundled": true, "dev": true, - "optional": true, "requires": { "kind-of": "^3.0.2", "longest": "^1.0.1", @@ -3478,8 +3477,7 @@ "longest": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "loose-envify": { "version": "1.3.1", @@ -5472,6 +5470,11 @@ "glob": "^7.0.5" } }, + "robust-predicates": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/robust-predicates/-/robust-predicates-2.0.4.tgz", + "integrity": "sha512-l4NwboJM74Ilm4VKfbAtFeGq7aEjWL+5kVFcmgFA2MrdnQWx9iE/tUGvxY5HyMI7o/WpSIUFLbC5fbeaHgSCYg==" + }, "rollup": { "version": "0.59.3", "resolved": "https://registry.npmjs.org/rollup/-/rollup-0.59.3.tgz", diff --git a/package.json b/package.json index f3024b0..1f2a043 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "uglify-js": "^3.3.21" }, "dependencies": { + "robust-predicates": "^2.0.4", "splaytree": "^0.1.4", "tinyqueue": "^1.2.0" } diff --git a/src/signed_area.js b/src/signed_area.js index 3db2aae..b738c38 100644 --- a/src/signed_area.js +++ b/src/signed_area.js @@ -1,3 +1,5 @@ +import {orient2d} from 'robust-predicates'; + /** * Signed area of the triangle (p0, p1, p2) * @param {Array.} p0 @@ -6,5 +8,8 @@ * @return {Number} */ export default function signedArea(p0, p1, p2) { - return (p0[0] - p2[0]) * (p1[1] - p2[1]) - (p1[0] - p2[0]) * (p0[1] - p2[1]); + const res = orient2d(p0[0], p0[1], p1[0], p1[1], p2[0], p2[1]); + if (res > 0) return -1; + if (res < 0) return 1; + return 0; } diff --git a/test/fixtures/issue100.geojson b/test/fixtures/issue100.geojson new file mode 100644 index 0000000..102625a --- /dev/null +++ b/test/fixtures/issue100.geojson @@ -0,0 +1,74 @@ +{ + "type": "FeatureCollection", + "features": [{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [[[-12.5,0,0],[-12.259880065917969,-2.438620090484619,0],[-11.548500061035156,-4.7835001945495605,0],[-12.5,0,0]]] + } + },{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [[[-11.548500061035156,4.7835001945495605,0],[-12.259880065917969,2.438620090484619,0],[-12.5,0,0],[-11.548500061035156,4.7835001945495605,0]], +[[-12.5,0,0],[-11.548500061035156,-4.7835001945495605,0],[12.259880065917969,-2.438620090484619,0],[-12.5,0,0]], +[[-11.548500061035156,4.7835001945495605,0],[-12.5,0,0],[12.259880065917969,-2.438620090484619,0],[-11.548500061035156,4.7835001945495605,0]], +[[11.548199653625488,-4.784060001373291,0],[11.548500061035156,-4.7835001945495605,0],[12.259880065917969,-2.438620090484619,0],[12.25918960571289,-2.440279960632324,0],[11.548199653625488,-4.784060001373291,0]], +[[12.25918960571289,-2.440279960632324,0],[12.259880065917969,-2.438620090484619,0],[12.259539604187012,-2.4391300678253174,0],[12.25918960571289,-2.440279960632324,0]], +[[12.259539604187012,-2.4391300678253174,0],[12.259880065917969,-2.438620090484619,0],[12.259650230407715,-2.438770055770874,0],[12.259539604187012,-2.4391300678253174,0]], +[[12.259650230407715,-2.438770055770874,0],[12.259880065917969,-2.438620090484619,0],[12.259679794311523,-2.438659906387329,0],[12.259650230407715,-2.438770055770874,0]], +[[12.259969711303711,2.4377200603485107,0],[12.259880065917969,2.438620090484619,0],[12.259679794311523,2.438659906387329,0],[12.259969711303711,2.4377200603485107,0]], +[[12.259880065917969,2.438620090484619,0],[11.548500061035156,4.7835001945495605,0],[11.548199653625488,4.784060001373291,0],[12.25918960571289,2.440279960632324,0],[12.259880065917969,2.438620090484619,0]], +[[12.259880065917969,2.438620090484619,0],[12.25918960571289,2.440279960632324,0],[12.259539604187012,2.4391300678253174,0],[12.259880065917969,2.438620090484619,0]], +[[12.259679794311523,2.438659906387329,0],[12.259880065917969,2.438620090484619,0],[12.259539604187012,2.4391300678253174,0],[12.259679794311523,2.438659906387329,0]], +[[12.259880065917969,-2.438620090484619,0],[12.259969711303711,-2.4377200603485107,0],[12.259719848632812,-2.4385499954223633,0],[12.259880065917969,-2.438620090484619,0]], +[[-11.548500061035156,-4.7835001945495605,0],[-11.548199653625488,-4.784060001373291,0],[-11.548370361328125,-4.783490180969238,0],[-11.548500061035156,-4.7835001945495605,0]], +[[-11.548500061035156,4.7835001945495605,0],[-11.548379898071289,4.7834601402282715,0],[-11.548199653625488,4.784060001373291,0],[-11.548500061035156,4.7835001945495605,0]], +[[10.83080005645752,-6.12624979019165,0],[11.548199653625488,-4.784060001373291,0],[11.548370361328125,-4.783490180969238,0],[10.830659866333008,-6.12624979019165,0],[10.83080005645752,-6.12624979019165,0]], +[[11.889439582824707,-3.659140110015869,0],[12.25918960571289,-2.440279960632324,0],[11.889439582824707,-3.332900047302246,0],[11.889439582824707,-3.659140110015869,0]], +[[12.5,0,0],[12.259969711303711,2.4377200603485107,0],[12.259679794311523,2.438659906387329,0],[11.141039848327637,2.66117000579834,0],[11.141039848327637,0.4122300148010254,0],[12.5,0,0]], +[[11.548199653625488,4.784060001373291,0],[11.141039848327637,5.545810222625732,0],[11.141039848327637,5.545559883117676,0],[11.548370361328125,4.783490180969238,0],[11.548199653625488,4.784060001373291,0]], +[[11.141039848327637,5.139659881591797,0],[12.25918960571289,2.440279960632324,0],[11.548370361328125,4.783490180969238,0],[11.141039848327637,5.545559883117676,0],[11.141039848327637,5.139659881591797,0]], +[[12.259969711303711,-2.4377200603485107,0],[12.5,0,0],[11.141039848327637,0.4122300148010254,0],[11.141039848327637,-1.9751800298690796,0],[12.259719848632812,-2.4385499954223633,0],[12.259969711303711,-2.4377200603485107,0]], +[[11.548370361328125,-4.783490180969238,0],[11.889439582824707,-3.659140110015869,0],[11.889439582824707,-3.332900047302246,0],[10.732370376586914,-6.12624979019165,0],[10.830659866333008,-6.12624979019165,0],[11.548370361328125,-4.783490180969238,0]], +[[11.141039848327637,-5.139659881591797,0],[12.25918960571289,-2.440279960632324,0],[12.259539604187012,-2.4391300678253174,0],[11.141039848327637,-4.113059997558594,0],[11.141039848327637,-5.139659881591797,0]], +[[11.141039848327637,-4.113059997558594,0],[12.259539604187012,-2.4391300678253174,0],[12.259650230407715,-2.438770055770874,0],[11.141039848327637,-3.186199903488159,0],[11.141039848327637,-4.113059997558594,0]], +[[11.141039848327637,-3.186199903488159,0],[12.259650230407715,-2.438770055770874,0],[12.259679794311523,-2.438659906387329,0],[11.141039848327637,-2.66117000579834,0],[11.141039848327637,-3.186199903488159,0]], +[[-11.548199653625488,-4.784060001373291,0],[-11.141039848327637,-5.545810222625732,0],[-11.141039848327637,-5.545559883117676,0],[-11.548370361328125,-4.7835001945495605,0],[-11.548199653625488,-4.784060001373291,0]], +[[11.141039848327637,-2.66117000579834,0],[12.259679794311523,-2.438659906387329,0],[11.141039848327637,-2.548830032348633,0],[11.141039848327637,-2.66117000579834,0]], +[[11.141039848327637,2.66117000579834,0],[10.830659866333008,2.722909927368164,0],[10.830659866333008,0.506380021572113,0],[11.141039848327637,0.4122300148010254,0],[11.141039848327637,2.66117000579834,0]], +[[-11.141039848327637,5.545810222625732,0],[-11.548199653625488,4.784060001373291,0],[-11.548370361328125,4.7835001945495605,0],[-11.141039848327637,5.545559883117676,0],[-11.141039848327637,5.545810222625732,0]], +[[12.259710311889648,-2.438570022583008,0],[11.141039848327637,-1.9752000570297241,0],[11.141039848327637,-2.0992300510406494,0],[12.259710311889648,-2.438570022583008,0]], +[[10.830659866333008,5.888959884643555,0],[11.141039848327637,5.139659881591797,0],[11.141039848327637,5.545559883117676,0],[10.830659866333008,6.12624979019165,0],[10.830659866333008,5.888959884643555,0]], +[[12.25918960571289,2.440279960632324,0],[11.141039848327637,5.139659881591797,0],[11.141039848327637,4.113059997558594,0],[12.259539604187012,2.4391300678253174,0],[12.25918960571289,2.440279960632324,0]], +[[12.259679794311523,2.438659906387329,0],[12.259539604187012,2.4391300678253174,0],[11.141039848327637,4.113059997558594,0],[11.141039848327637,2.66117000579834,0],[12.259679794311523,2.438659906387329,0]], +[[11.141039848327637,0.4122300148010254,0],[10.830659866333008,0.506380021572113,0],[10.830659866333008,-1.846619963645935,0],[11.141039848327637,-1.9751800298690796,0],[11.141039848327637,0.4122300148010254,0]], +[[10.830659866333008,-5.888959884643555,0],[11.141039848327637,-5.139659881591797,0],[11.141039848327637,-4.113059997558594,0],[10.830659866333008,-4.577569961547852,0],[10.830659866333008,-5.888959884643555,0]], +[[10.830659866333008,-4.577569961547852,0],[11.141039848327637,-4.113059997558594,0],[11.141039848327637,-3.186199903488159,0],[10.830659866333008,-3.393589973449707,0],[10.830659866333008,-4.577569961547852,0]], +[[10.830659866333008,-3.393589973449707,0],[11.141039848327637,-3.186199903488159,0],[11.141039848327637,-2.66117000579834,0],[10.830659866333008,-2.722909927368164,0],[10.830659866333008,-3.393589973449707,0]], +[[-11.141039848327637,-4.743370056152344,0],[-11.548370361328125,-4.783490180969238,0],[-11.141039848327637,-5.5455498695373535,0],[-11.141039848327637,-4.743370056152344,0]], +[[10.830659866333008,2.722909927368164,0],[-6.279160022735596,6.12624979019165,0],[-7.695549964904785,6.12624979019165,0],[10.830659866333008,0.506380021572113,0],[10.830659866333008,2.722909927368164,0]], +[[-10.83080005645752,6.12624979019165,0],[-11.141039848327637,5.545810222625732,0],[-11.141039848327637,5.545559883117676,0],[-10.830659866333008,6.12624979019165,0],[-10.83080005645752,6.12624979019165,0]], +[[-11.141039848327637,5.545559883117676,0],[-11.548370361328125,4.7835001945495605,0],[-11.141039848327637,4.659939765930176,0],[-11.141039848327637,5.545559883117676,0]], +[[11.141039848327637,5.545810222625732,0],[11.078660011291504,5.662519931793213,0],[11.078660011291504,5.6622700691223145,0],[11.141039848327637,5.545559883117676,0],[11.141039848327637,5.545810222625732,0]], +[[10.732370376586914,6.12624979019165,0],[10.830659866333008,5.888959884643555,0],[10.830659866333008,6.12624979019165,0],[10.732370376586914,6.12624979019165,0]], +[[11.141039848327637,5.139659881591797,0],[10.830659866333008,5.888959884643555,0],[10.830659866333008,4.577569961547852,0],[11.141039848327637,4.113059997558594,0],[11.141039848327637,5.139659881591797,0]], +[[11.141039848327637,4.113059997558594,0],[10.830659866333008,4.577569961547852,0],[10.830659866333008,2.722909927368164,0],[11.141039848327637,2.66117000579834,0],[11.141039848327637,4.113059997558594,0]], +[[10.830659866333008,0.506380021572113,0],[-7.695580005645752,6.12624979019165,0],[-8.417630195617676,6.12624979019165,0],[10.830659866333008,-1.846619963645935,0],[10.830659866333008,0.506380021572113,0]], +[[9.795860290527344,-6.12624979019165,0],[10.830659866333008,-4.577569961547852,0],[10.830659866333008,-3.393589973449707,0],[6.740960121154785,-6.12624979019165,0],[9.795860290527344,-6.12624979019165,0]], +[[6.740960121154785,-6.12624979019165,0],[10.830659866333008,-3.393589973449707,0],[10.830659866333008,-2.722909927368164,0],[-6.279160022735596,-6.12624979019165,0],[6.740960121154785,-6.12624979019165,0]], +[[-11.141039848327637,-5.545810222625732,0],[-11.078660011291504,-5.662519931793213,0],[-11.078660011291504,-5.6622700691223145,0],[-11.141039848327637,-5.545559883117676,0],[-11.141039848327637,-5.545810222625732,0]], +[[10.830659866333008,-2.722909927368164,0],[11.141039848327637,-2.66117000579834,0],[11.141039848327637,-2.548830032348633,0],[10.830659866333008,-2.579400062561035,0],[10.830659866333008,-2.722909927368164,0]], +[[11.141039848327637,-1.9752000570297241,0],[10.830659866333008,-1.8466399908065796,0],[10.830659866333008,-2.005079984664917,0],[11.141039848327637,-2.0992300510406494,0],[11.141039848327637,-1.9752000570297241,0]], +[[11.078660011291504,5.662519931793213,0],[10.830789566040039,6.12624979019165,0],[10.830659866333008,6.12624979019165,0],[11.078660011291504,5.6622700691223145,0],[11.078660011291504,5.662519931793213,0]], +[[10.830659866333008,5.888959884643555,0],[10.732370376586914,6.12624979019165,0],[9.795860290527344,6.12624979019165,0],[10.830659866333008,4.577569961547852,0],[10.830659866333008,5.888959884643555,0]], +[[10.830659866333008,4.577569961547852,0],[9.795860290527344,6.12624979019165,0],[-6.278890132904053,6.12624979019165,0],[10.830659866333008,2.722909927368164,0],[10.830659866333008,4.577569961547852,0]], +[[-11.078660011291504,-5.662519931793213,0],[-10.830789566040039,-6.12624979019165,0],[-10.830659866333008,-6.12624979019165,0],[-11.078660011291504,-5.6622700691223145,0],[-11.078660011291504,-5.662519931793213,0]], +[[-10.830659866333008,-4.712800025939941,0],[-11.141039848327637,-4.743370056152344,0],[-11.141039848327637,-5.5455498695373535,0],[-10.830659866333008,-6.126239776611328,0],[-10.830659866333008,-4.712800025939941,0]], +[[10.830659866333008,-1.8466399908065796,0],[-8.41765022277832,6.12624979019165,0],[-10.830659866333008,6.12624979019165,0],[-11.141039848327637,5.545559883117676,0],[-11.141039848327637,4.659939765930176,0],[10.830659866333008,-2.005079984664917,0],[10.830659866333008,-1.8466399908065796,0]], +[[-6.279160022735596,-6.12624979019165,0],[10.830659866333008,-2.722909927368164,0],[10.830659866333008,-2.579400062561035,0],[-10.830659866333008,-4.712800025939941,0],[-10.830659866333008,-6.126239776611328,0],[-6.279160022735596,-6.12624979019165,0]]] + } + }] +} diff --git a/test/fixtures/issue102.geojson b/test/fixtures/issue102.geojson new file mode 100644 index 0000000..4e9b5a4 --- /dev/null +++ b/test/fixtures/issue102.geojson @@ -0,0 +1,29 @@ +{ + "type": "FeatureCollection", + "features": [{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [[34, 119], + [20, 20], + [132, 141], + [34, 119]] + ] + } + },{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [ + [[27.681744389656238, 74.32090675542626], + [25.1362898442017, 56.3209067554263], + [35.67034343592924, 56.32090675542627], + [35.67034343592924, 74.32090675542624], + [27.681744389656238, 74.32090675542626]] + ] + } + }] +} diff --git a/test/fixtures/issue103.geojson b/test/fixtures/issue103.geojson new file mode 100644 index 0000000..b6f46b3 --- /dev/null +++ b/test/fixtures/issue103.geojson @@ -0,0 +1,18 @@ +{ + "type": "FeatureCollection", + "features": [{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [[[-24.820450799231946, 81.60424965743837],[-66.98439939287994, 65.9251157774066],[-67.17837257682962, 65.5743714314538],[-23.564139586577383, 84.46172863817043],[-35.3782801119441, 162.91715665949576],[-37.42674770001716, 159.79423125392117], [-24.820450799231946, 81.60424965743837]]] + } + },{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [[[-23.564139586577383, 84.46172863817043],[-67.17837257682962, 65.5743714314538],[-24.85941638876879, 26.87535085545798],[-23.564139586577383, 84.46172863817043]]] + } + }] +} diff --git a/test/fixtures/issue110.geojson b/test/fixtures/issue110.geojson new file mode 100644 index 0000000..4f09423 --- /dev/null +++ b/test/fixtures/issue110.geojson @@ -0,0 +1,18 @@ +{ + "type": "FeatureCollection", + "features": [{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [[[115,96], [140,206], [120,210], [125,250], [80,300]]] + } + },{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [[[111,228], [129,192], [309,282]]] + } + }] +} diff --git a/test/fixtures/issue90.geojson b/test/fixtures/issue90.geojson new file mode 100644 index 0000000..2592b82 --- /dev/null +++ b/test/fixtures/issue90.geojson @@ -0,0 +1,18 @@ +{ + "type": "FeatureCollection", + "features": [{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [[ [0,0],[10,0],[10,10],[0,10] ]] + } + },{ + "type": "Feature", + "properties": {}, + "geometry": { + "type": "Polygon", + "coordinates": [[ [9,10],[21,9],[21,21],[9,21] ]] + } + }] +} diff --git a/test/fixtures/issue99.geojson b/test/fixtures/issue99.geojson new file mode 100644 index 0000000..fc474b6 --- /dev/null +++ b/test/fixtures/issue99.geojson @@ -0,0 +1,12 @@ +{ + "type": "FeatureCollection", + "features": [{ + "type": "Feature", + "properties": {}, + "geometry": { "type": "Polygon", "coordinates": [[[ -530, -530], [-530, 530], [530, 530], [530, -530], [ -530, -530]]] } +},{ + "type": "Feature", + "properties": {}, + "geometry": { "type": "Polygon", "coordinates": [[[1.2500125250252, -531],[-98, -531],[-98, 531],[1.250012525025, 531],[1.2500125250252, -531]]] } +}] +}