UNPKG

pnltri

Version:

Polygon near-linear time triangulation (based on Seidel's algorithm) with ear clip for small cases. Handles degenerate cases of colinear edges and vertices touching edges.

43 lines (42 loc) 7.31 kB
{ "name": "pnltri", "version": "2.1.1", "description": "Polygon near-linear time triangulation (based on Seidel's algorithm) with ear clip for small cases. Handles degenerate cases of colinear edges and vertices touching edges.", "keywords": [ "Seidel", "polygon", "triangulation", "tesselation", "ear clipping", "linear", "geometry" ], "homepage": "https://github.com/jahting/pnltri.js", "bugs": { "url": "https://github.com/jahting/pnltri.js/issues" }, "author": { "name": "Juergen Ahting" }, "main": "./pnltri.js", "repository": { "type": "git", "url": "git://github.com/jahting/pnltri.js.git" }, "license": { "type": "The MIT License", "url": "https://raw.github.com/jahting/pnltri.js/master/LICENSE" }, "engines": { "node": "*" }, "help": { "web": "https://github.com/jahting/pnltri.js/issues" }, "readme": "pnltri.js\r\n=========\r\n\r\n#### (Simple) Polygon Near-Linear Triangulation in JavaScript ####\r\n\r\nThis project implements an algorithm by Raimund Seidel [Sei91] for the triangulation of simple polygons in expected near-linear time with several improvements and extensions.\r\n\r\nThe algorithm handles simple polygons (no crossing edges) with holes. The contour and the holes are specified as vertex sequences.\r\nThe input can contain several polygons and/or holes in any order. There is no need for a specific winding order clockwise (CW) or counter-clockwise (CCW).\r\nRepeating points (zero-length edges), vertices touching other edges and edges touching other (co-linear) edges are allowed.\r\n\r\nAt present crossing edges and vertices coinciding with non-adjacent vertices are NOT allowed for polygons with holes.\r\n\r\nThe output is a list of triangles. Each triangle is a triple of vertex indices. These refer to the order of the vertices in the input sequences, as if those had all been concatenated (numbering starts at 0). No Steiner points are added.\r\nThe output triangles for a specific polygon (with holes) are always the same, but their order changes since there is a random part in the algorithm and output is not sorted. Sorting would take longer than the triangulation itself.\r\n\r\nIn the future it shall handle crossing edges too - along the lines presented by Sigbjorn Vik in [Vik01].\r\n\r\n### Testing ###\r\n\r\nThe project includes unit and functional tests (~1800 assertions) with QUnit: [TestSuite](https://github.com/jahting/pnltri.js/blob/master/test/test_pnltri_js.html)\r\n\r\nIt has been tested with the data available from Three.js examples and issues concerning triangulation, but more test data would be welcome.\r\n\r\n### Performance ###\r\n\r\nThe algorithm handles the polygon edges in a randomised order to reach an expected near-linear running time ( n * log*(n) ). As a consequence the running time varies and a regular nature of polygons is of no \"advantage\".\r\n\r\nThis implementation seems reasonably fast for Javascript. Measurements confirm the near-linear expected running time of the algorithm.\r\n\r\nMeasured on an old Laptop ( Core 2 Duo T7200, 2 GHz ) with Firefox 30.0 ([measured with](https://github.com/jahting/pnltri.js/blob/master/test/triang_pnltri_js.html)):\r\nA square with 1600 square holes (~ 6400 vertices -> ~10k triangles) takes about 140 milliseconds.\r\nA square with 25000 square holes (~ 100k vertices -> ~150k triangles) takes little more than 2 seconds.\r\n\r\n\r\n### Usage ###\r\n\r\nDownload the [minified library](https://raw.github.com/jahting/pnltri.js/master/build/pnltri.min.js) and include it in your html.\r\n\r\n```html\r\n<script src=\"js/pnltri.min.js\"></script>\r\n```\r\n\r\nThis code specifies a polygon with 3 holes. It then creates the Triangulator and calls the algorithm. Finally it displays the result.\r\n\r\n```html\r\n<script type=\"text/javascript\">\r\n\t// define polygon with holes\r\n\tvar example_data = [\r\n\t\t// Contour\r\n\t\t[ { x:0.0, y:0.0 },\t{ x:30.0, y:0.0 }, { x:30.0, y:40.0 }, { x:0.0, y:40.0 } ],\r\n\t\t// Holes\r\n\t\t[ { x: 2.5, y:26.0 },\t{ x: 5.0, y:31.0 }, { x:10.0, y:28.5 } ],\r\n\t\t[ { x:27.5, y:25.0 },\t{ x:25.0, y:30.0 }, { x:20.0, y:27.5 } ],\r\n\t\t[ { x:15.0, y:20.0 },\t{ x:12.5, y:10.0 }, { x:18.0, y:10.0 } ],\r\n\t\t\t\t\t\t];\r\n\r\n\t// triangulate the polygon with holes\r\n\tvar myTriangulator = new PNLTRI.Triangulator();\r\n\tvar triangList = myTriangulator.triangulate_polygon( example_data );\r\n\r\n\t// output the result\r\n\tvar resultStr = ( triangList ) ?\r\n\t\t\ttriangList.map( function(tri) { return \"[ \"+tri.join(\", \")+\" ]\" } ).join(\", \") :\r\n\t\t\t'NO Triangle-List!';\r\n\tresultDiv = document.createElement('div');\r\n\tresultDiv.innerHTML = '<p/>'+resultStr+'<p/>';\r\n\tdocument.body.appendChild( resultDiv );\r\n</script>\r\n```\r\n\r\nIf everything went well you should see this list - probably in a different order.\r\n```html\r\n[ 0, 1, 11 ], [ 0, 4, 3 ], [ 0, 10, 4 ], [ 0, 11, 10 ], [ 1, 2, 7 ], [ 1, 7, 12 ],\r\n[ 1, 12, 11 ], [ 2, 3, 8 ], [ 2, 8, 7 ], [ 3, 4, 5 ], [ 3, 5, 8 ], [ 4, 7, 9 ],\r\n[ 4, 9, 6 ], [ 4, 10, 7 ], [ 5, 6, 8 ], [ 6, 9, 8 ], [ 7, 10, 12 ]\r\n```\r\n\r\n#### Integration into Three.js ####\r\n\r\nIn [Three.js](https://github.com/mrdoob/three.js) replace THREE.Shape.Utils.triangulateShape with:\r\n\r\n```html\r\n\ttriangulateShape: function ( contour, holes ) {\r\n\t\tvar myTriangulator = new PNLTRI.Triangulator();\r\n\t\treturn\tmyTriangulator.triangulate_polygon( [ contour ].concat(holes) );\r\n\t},\r\n```\r\n\r\nThis works with all examples included in Three.js .\r\n\r\nIn Three.js is an example [WebGL / geometry / text2](http://threejs.org/examples/webgl_geometry_text2.html) which uses ```pnltri.min.js```.\r\n\r\n### Change log ###\r\n\r\n[releases](https://github.com/jahting/pnltri.js/releases)\r\n\r\n### References ###\r\n\r\n[Sei91]\r\n\tAUTHOR = \"Raimund Seidel\",\r\n\tTITLE = \"A simple and fast incremental randomized algorithm for computing trapezoidal decompositions and for triangulating polygons\",\r\n\tJOURNAL = \"Computational Geometry Theory & Applications\",\r\n\tPAGES = \"51-64\",\r\n\tYEAR = 1991,\r\n\tNUMBER = 1,\r\n\tVOLUME = 1,\r\n\t[DOI](http://dx.doi.org/10.1016/0925-7721(91)90012-4)\r\n\t[Url](http://www.ime.usp.br/~walterfm/cursos/mac0331/2006/seidel.pdf)\r\n\r\n[FoM84]\r\n\tAUTHOR = \"Alain Fournier, Delfin Y. Montuno\",\r\n\tTITLE = \"Triangulating Simple Polygons and Equivalent Problems\",\r\n\tJOURNAL = \"ACM Transactions on Graphics\",\r\n\tPAGES = \"153-174\",\r\n\tYEAR = 1984,\r\n\tVOLUME = 3\r\n\tNUMBER = 2,\r\n\t[Url](http://dl.acm.org/citation.cfm?doid=357337.357341)\r\n\r\n[NaM95]\r\n\tAUTHOR = \"Atul Narkhede, Dinesh Manocha\",\r\n\tTITLE = \"Fast polygon triangulation based on Seidel's Algorithm\",\r\n\t\t\tImplementation report: UNC-CH, 1994.\r\n\t[Url](http://www.cs.unc.edu/~dm/CODE/GEM/chapter.html)\r\n\r\n[Vik01]\r\n\tAUTHOR = \"Sigbjorn Vik\",\r\n\tTITLE = \"An Implementation of a Near-Linear Polygon Triangulation Algorithm for General Polygons\",\r\n\tYEAR = 2001,\r\n\t[Url](http://sigbjorn.vik.name/projects/Triangulation.pdf)\r\n\r\n\r\n", "readmeFilename": "README.md", "_id": "pnltri@2.1.1", "scripts": {}, "_shasum": "97e61ff498d4bc0ee67028a06990ac30b4dc3d1c", "_from": "../pnltri" }