{"name":"sweepline-intersections","version":"1.2.0","description":"A module to check if a polygon self-intersects using a sweepline algorithm","main":"dist/sweeplineIntersections.js","module":"dist/sweeplineIntersections.esm.js","unpkg":"dist/sweeplineIntersections.min.js","jsdelivr":"dist/sweeplineIntersections.min.js","scripts":{"bench":"npm run build && node test/benchmark.js","build":"rollup -c","build:debug":"cross-env webpack --config debug/webpack.config.js --mode production","debug":"cross-env webpack-dev-server --config debug/webpack.config.js --mode development --open --hot","test":"ava --verbose"},"ava":{"files":["test/test.e2e.js","test/*.spec.js"],"require":["esm"]},"author":"","license":"MIT","dependencies":{"tinyqueue":"^2.0.0"},"devDependencies":{"2d-polygon-self-intersections":"^1.3.1","@rollup/plugin-commonjs":"^11.1.0","@rollup/plugin-node-resolve":"^7.1.3","@rollup/plugin-strip":"^1.3.2","ava":"^1.0.1","benchmark":"^2.1.4","bentley-ottmann-intersections":"0.0.1","cross-env":"^5.2.0","css-loader":"^2.1.0","eslint":"^5.12.0","eslint-config-mourner":"^3.0.0","esm":"^3.0.84","file-loader":"^3.0.1","geojson-polygon-self-intersections":"^1.2.0","glob":"^7.1.3","json-loader":"^0.5.7","leaflet":"^1.4.0","load-json-file":"^5.3.0","nyc":"^13.1.0","rollup":"^2.7.6","rollup-plugin-terser":"^4.0.2","vue":"^2.5.22","vue-loader":"^15.6.2","vue-template-compiler":"^2.5.22","webpack":"^4.29.0","webpack-cli":"^3.2.1","webpack-dev-server":"^3.1.14"},"repository":{"type":"git","url":"git+https://github.com/rowanwins/sweepline-intersections.git"},"bugs":{"url":"https://github.com/rowanwins/sweepline-intersections/issues"},"gitHead":"a9a224bbd1fb2fc4b74fac06965cab54b0735e3b","homepage":"https://github.com/rowanwins/sweepline-intersections#readme","_id":"sweepline-intersections@1.2.0","_nodeVersion":"14.14.0","_npmVersion":"6.14.8","dist":{"integrity":"sha512-cHTjVvdna12iNXOvZV81mBJh8x5zpJmQLsdiWgK89rKIMyC/z7DO86FJ1/zuenX2c5EvIPAtNXWPOhu/MylToQ==","shasum":"4cbdd29087b2c6b3b79a4951fa8a4b9ac44205d9","tarball":"http://registry-npm.ywwl.com/sweepline-intersections/download/sweepline-intersections-1.2.0.tgz","fileCount":7,"unpackedSize":10268,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgJ416CRA9TVsSAnZWagAA7FgP/2CcKmsBGa+P9YdVvS4z\nb+wyA7tD3K86jWVC2qSEowBloCk+KKPXSPQwZluMDrLKV6EHOPYJgpypQbst\nlRrwSYAEctpFB4wnVHteiX2aMRUiNxx6uj78pQLVTZNv4whTKtoMUqUCs9VE\n7Mg2BrhQn4qLO5D9lxyH3kMBXO8q7eXE6bjmPJp4Axw4l0XmKGlNFDSLqI1G\nHyaLhKNe1qUAJAxp3BayNXhdwNmnw1Qj0fLXRD1KSOgqCyGArXkSAL6UiYQn\npqSDdluk36ib/3wXCXGRzsPten8/hBznugbVhiG3HcpwvtAx4Mzov+ppHj1p\nnrMsycDK+wTCv312ajOvRFDIvoqMnEdDXJ3RJAOyhY/QZ/sYbM2lF2hgsxlI\nlFB8KruqPUwscU7s4o3MdVqYniUe7qhZVOLFJbX17NNNi8kK4D3kkdOcXiaG\nuDQuG2a4M/HAjCizo9jwKA4fGlZ5O4KXBjzts/9ple64Yzsc5nLbPZOehqAt\nUVG4msgqiP05sdgbkuj+yV0HrE34cDpMdJSW7oYvFDyeythpZCxbWq69XA3I\nYREnBfvhaMVc9y8kAOhEALwGjkptbL4keQj5DgK9FG5sPPd7PSGlA5mCsDTR\nICAu1h3jsV7Jo+VtnTJv5JGeB5dxr2KiOB1w6zrn3BntjbX/fyfZwghgrDsC\n/ZIm\r\n=2Ke5\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDhwuq6GxG6xC1vosv6kZhxIOrqFLqHF7R1DzIxcIw4VAiEAoxxdyn+iKf7aFXzENXyfuf/clVkF14YooFpRgBrEiXw="}],"size":4401,"noattachment":false,"key":"/sweepline-intersections/-/sweepline-intersections-1.2.0.tgz"},"_npmUser":{"name":"rowanwins","email":"rowanwins@yahoo.com.au"},"directories":{},"maintainers":[{"name":"rowanwins","email":"rowanwins@yahoo.com.au"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/sweepline-intersections_1.2.0_1613204857535_0.6099324715679806"},"_hasShrinkwrap":false,"readme":"# sweepline-intersections\nA small and fast module using a sweepline algorithm to detect intersections between polygons and/or polylines.\n\n## Documentation\n\n### Install\n````\nnpm install sweepline-intersections\n````\n\n### Basic Use\nValid inputs: Geojson `Feature` or `Geometry` including `Polygon`, `LineString`, `MultiPolygon`, `MultiLineString`, as well as `FeatureCollection`.\n\nReturns an array of intersection points eg, [[x1, y1], [x2, y2]] \n\n````js\n    const findIntersections = require('sweepline-intersections')\n\n    const box = {type: 'Polygon', coordinates: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]}\n    const intersections = findIntersections(box)\n    // returns an array of self-intersection points\n````\n\nAlso accepts an optional boolean argument second which when set to true means the module won't detect self-intersections and will only report intersections between different features. This defaults to false.\neg \n````js\n    const findIntersections = require('sweepline-intersections')\n    const intersectionsBetweenFeature = findIntersections(featureCollection, true)\n    // returns an array of intersection points between features\n````\n\n### Complex Use\nThis library also provide a class-based approach which is helpful if you want to check multiple geometries against a single geometry. This allows you to save the state of the initial event queue with the primary geometry.\n\n````js\n    import SweeplineIntersectionsClass from 'sweepline-intersections/dist/SweeplineIntersectionsClass'\n\n    // create the base instance\n    const sl = new SweeplineIntersectionsClass()\n    // populate the event queue with your primary geometry\n    sl.addData(largeGeoJson)\n    // clone the event queue in the original state so you can reuse it\n    const origQueue = sl.cloneEventQueue()\n\n    // now you can iterate through some other set of features saving\n    // the overhead of having to populate the complete queue multiple times\n    someOtherFeatureCollection.features.forEach(feature => {\n        // add another feature to test against your original data\n        sl.addData(feature, origQueue)\n        // check if those two features intersect\n        // add an optional boolean argument to ignore self-intersections \n        const intersectionPoints = sl.getIntersections(true)\n    })\n````\n\n#### API\n`new SweeplineIntersectionsClass()` - creates a new instance\n\n`.addData(geojson, existingQueue)` - add geojson to the event queue. The second argument for an `existingQueue` is optional, and takes a queue generated from `.cloneEventQueue()`\n\n`.cloneEventQueue()` - clones the state of the existing event queue that's been populated with geojson. Returns a queue that you can pass to the `addData` method\n\n`.getIntersections(ignoreSelfIntersections)` - Checks for segment intersections. Accepts an optional boolean argument to ignore self intersections are only report intersections between features.\n\n\n## Benchmarks\nTested against \n- bentley-ottmann-intersections - https://www.npmjs.com/package/bentley-ottmann-intersections\n- gpsi - https://www.npmjs.com/package/geojson-polygon-self-intersections\n- isects - https://www.npmjs.com/package/2d-polygon-self-intersections\n````\n// Switzerland (~700 vertices)\n// gpsi x 37.05 ops/sec ±1.77% (49 runs sampled)\n// bentleyOttmann x 2,010 ops/sec ±1.52% (89 runs sampled)\n// sweepline x 2,621 ops/sec ±0.29% (95 runs sampled)\n// isects x 14.29 ops/sec ±2.16% (40 runs sampled)\n// - Fastest is sweepline (this library)\n\n// Simple Case (6 vertices)\n// gpsi x 246,512 ops/sec ±1.23% (90 runs sampled)\n// bentleyOttmann x 546,326 ops/sec ±0.66% (92 runs sampled)\n// sweepline x 1,157,425 ops/sec ±1.04% (94 runs sampled)\n// - Fastest is sweepline (this library)\n\n// Chile - Vertical geometry (17,000 vertices)\n// bentleyOttmann x 50.22 ops/sec ±1.75% (65 runs sampled)\n// sweepline x 35.64 ops/sec ±1.20% (62 runs sampled)\n// - Fastest is bentleyOttmann (although it doesn't find intersection)\n````\n\n## Contributing\n- For a live dev server run `npm run debug`. \n  - The geometry being tested can be modified in `debug/src/App.vue`\n- There are a couple of test suites\n  - `npm run test` runs all tests\n  - `npm run test:e2e` does a general test that the correct number of self-intersections are found in the `test/fixtures` folder\n  - `npm run test:unit` is unit style tests to make sure functions & methods do the right thing\n    - these need some love\n\n\n## Algorithm notes\nThe basic concept of this algorithm is based on a sweepline. Where this algorithm differs from the [bentley-ottmann algorithm](https://en.wikipedia.org/wiki/Bentley%E2%80%93Ottmann_algorithm) is that there is no use of a tree data structure to store the segments. The reason for the modification is because if you are dealing with polygons or polylines (rather than a random group of line segments) there is a reasonable assumption that there are going to be very few segments that lie on the same x plane.\n\nRemoving the tree structure greatly simplifies the code. The tree structure is replaced with a priority queue of segments which is sorted by the x vertex of the right endpoint of the segments. A priority queue is already used to sort the vertices which means only 1 data structure is required.\n\nThe package size of this module is 3kb compared to my implementation of the bentley-ottmann algorithm which is 16kb while performance is typically faster than bentley-ottmann. \n\nBentley-ottman only outperforms this library when there are several thousands vertices, however I'm also less confident in the results of my bentley-ottman lib as it occassionally misses intersections and is much harder to write tests for due to the more complex logic.\n\n\n### Algorithm Steps\n- Vertices are entered into a priority queue sorted from left to right\n- An empty priority queue is created to store segments encountered\n- An item is removed from the priority queue\n    - If the vertex is the left endpoint of a segment, we test it against every other segment in the segment queue for intersections with any intersection recorded. We then add the vertex (and it's associated right endpoint) to the segment queue.\n    - When we encounter a right endpoint we remove the first item from the segment queue.\n\nEach pair of segments are only tested once. And only segments that overlap on the x plane are tested against each other.\n","publish_time":1613204857812,"_cnpm_publish_time":1613204857812,"dist-tags":{"latest":"2.0.1"}}