UNPKG

triangulate-contours

Version:
55 lines (47 loc) 1.28 kB
var Tess2 = require('tess2') var xtend = require('xtend') module.exports = function(contours, opt) { opt = opt||{} contours = contours.filter(function(c) { return c.length>0 }) if (contours.length === 0) { return { positions: [], cells: [] } } if (typeof opt.vertexSize !== 'number') opt.vertexSize = contours[0][0].length //flatten for tess2.js contours = contours.map(function(c) { return c.reduce(function(a, b) { return a.concat(b) }) }) // Tesselate var res = Tess2.tesselate(xtend({ contours: contours, windingRule: Tess2.WINDING_ODD, elementType: Tess2.POLYGONS, polySize: 3, vertexSize: 2 }, opt)) var positions = [] for (var i=0; i<res.vertices.length; i+=opt.vertexSize) { var pos = res.vertices.slice(i, i+opt.vertexSize) positions.push(pos) } var cells = [] for (i=0; i<res.elements.length; i+=3) { var a = res.elements[i], b = res.elements[i+1], c = res.elements[i+2] cells.push([a, b, c]) } //return a simplicial complex return { positions: positions, cells: cells } }