triangulate-polyline
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "triangulate-polyline", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Triangulates a complex polygon", | ||
@@ -11,3 +11,3 @@ "main": "tripolyline.js", | ||
"dependencies": { | ||
"poly2tri": "1.3.3" | ||
"cdt2d": "^1.0.0" | ||
}, | ||
@@ -14,0 +14,0 @@ "devDependencies": { |
@@ -5,79 +5,16 @@ "use strict" | ||
var poly2tri = require("poly2tri") | ||
var cdt2d = require('cdt2d') | ||
//poly2tri uses "cute" x/y accessors for point members | ||
function PointWrapper(x, y, idx) { | ||
this.x = x | ||
this.y = y | ||
this.idx = idx | ||
} | ||
function triangulatePolyline(loops, positions, perturb) { | ||
//Converts a loop into poly2tri format | ||
function convertLoop(loop) { | ||
return loop.map(function(v) { | ||
var p = positions[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) | ||
}) | ||
} | ||
//Find outermost loop | ||
var numLoops = loops.length | ||
var outerLoop = 0 | ||
var outerPoint = positions[loops[0][0]] | ||
for(var i=0; i<numLoops; ++i) { | ||
function triangulateLoop(loops, positions) { | ||
var edges = [] | ||
for(var i=0; i<loops.length; ++i) { | ||
var loop = loops[i] | ||
var n = loop.length | ||
for(var j=0; j<n; ++j) { | ||
var p = positions[loop[j]] | ||
var d = p[1] - outerPoint[1] | ||
if(d === 0) { | ||
d = p[0] - outerPoint[0] | ||
} | ||
if(d < 0) { | ||
outerPoint = p | ||
outerLoop = i | ||
} | ||
for(var j=0; j<loop.length; ++j) { | ||
edges.push([loop[j], loop[(j+1)%loop.length]]) | ||
} | ||
} | ||
//Pass geometry to poly2tri | ||
var ctx = new poly2tri.SweepContext(convertLoop(loops[outerLoop])) | ||
for(var i=0; i<numLoops; ++i) { | ||
if(i === outerLoop) { | ||
continue | ||
} | ||
ctx.addHole(convertLoop(loops[i])) | ||
} | ||
//Triangulate | ||
ctx.triangulate() | ||
var triangles = ctx.getTriangles() | ||
//Unbox triangles from poly2tri format to list of indices | ||
return triangles.map(function(tri) { | ||
return [ | ||
tri.getPoint(0).idx, | ||
tri.getPoint(1).idx, | ||
tri.getPoint(2).idx | ||
] | ||
return cdt2d(positions, edges, { | ||
delaunay: false, | ||
exterior: false | ||
}) | ||
} | ||
//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
4447
56
+ Addedcdt2d@^1.0.0
+ Addedbinary-search-bounds@2.0.5(transitive)
+ Addedcdt2d@1.0.0(transitive)
+ Addedrobust-in-sphere@1.2.1(transitive)
+ Addedrobust-orientation@1.2.1(transitive)
+ Addedrobust-scale@1.0.2(transitive)
+ Addedrobust-subtract@1.0.0(transitive)
+ Addedrobust-sum@1.0.0(transitive)
+ Addedtwo-product@1.0.2(transitive)
+ Addedtwo-sum@1.0.0(transitive)
- Removedpoly2tri@1.3.3
- Removedpoly2tri@1.3.3(transitive)