triangulate-polyline
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "triangulate-polyline", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Triangulates a complex polygon", | ||
@@ -13,3 +13,5 @@ "main": "tripolyline.js", | ||
}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"tape": "^3.0.1" | ||
}, | ||
"scripts": { | ||
@@ -16,0 +18,0 @@ "test": "echo \"Error: no test specified\" && exit 1" |
"use strict" | ||
module.exports = triangulatePolyline | ||
module.exports = triangulateLoop | ||
@@ -14,12 +14,3 @@ var poly2tri = require("poly2tri") | ||
function tryTriangulation(ctx) { | ||
try { | ||
ctx.triangulate() | ||
} catch(e) { | ||
return false | ||
} | ||
return true | ||
} | ||
function triangulatePolyline(loops, positions) { | ||
function triangulatePolyline(loops, positions, perturb) { | ||
//Converts a loop into poly2tri format | ||
@@ -29,3 +20,6 @@ function convertLoop(loop) { | ||
var p = positions[v] | ||
return new PointWrapper(p[0], p[1], v) | ||
return new PointWrapper( | ||
p[0] + (0.5-Math.random())*perturb*(1.0+Math.abs(p[0])), | ||
p[1] + (0.5-Math.random())*perturb*(1.0+Math.abs(p[1])), | ||
v) | ||
}) | ||
@@ -64,5 +58,3 @@ } | ||
//Triangulate | ||
if(!tryTriangulation(ctx)) { | ||
return [] | ||
} | ||
ctx.triangulate() | ||
var triangles = ctx.getTriangles() | ||
@@ -78,2 +70,17 @@ | ||
}) | ||
} | ||
//poly2tri is non robust, so we run it in a loop | ||
//If it fails, then we perturb the vertices by increasing amounts | ||
//until it works. (Stupid) | ||
function triangulateLoop(loops, positions) { | ||
var p = 0.0 | ||
for(var i=0; i<10; ++i) { | ||
try { | ||
return triangulatePolyline(loops, positions, p) | ||
}catch(e) { | ||
} | ||
p = (p+1e-8)*2 | ||
} | ||
return [] | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5953
7
112
1