"use strict" module.exports = simplifyPolygon var orient = require("robust-orientation") var sc = require("simplicial-complex") function errorWeight(base, a, b) { var area = Math.abs(orient(base, a, b)) var perim = Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1]-b[1], 2)) return area / perim } function simplifyPolygon(cells, positions, minArea) { var n = positions.length var nc = cells.length var inv = new Array(n) var outv = new Array(n) var weights = new Array(n) var dead = new Array(n) //Initialize tables for(var i=0; i> 1 } return (i >> 1) - 1 } //Bubble element i down the heap function heapDown(i) { var w = heapWeight(i) while(true) { var tw = w var left = 2*i + 1 var right = 2*(i + 1) var next = i if(left < heapCount) { var lw = heapWeight(left) if(lw < tw) { next = left tw = lw } } if(right < heapCount) { var rw = heapWeight(right) if(rw < tw) { next = right } } if(next === i) { return i } heapSwap(i, next) i = next } } //Bubbles element i up the heap function heapUp(i) { var w = heapWeight(i) while(i > 0) { var parent = heapParent(i) if(parent >= 0) { var pw = heapWeight(parent) if(w < pw) { heapSwap(i, parent) i = parent continue } } return i } } //Pop minimum element function heapPop() { if(heapCount > 0) { var head = heap[0] heapSwap(0, heapCount-1) heapCount -= 1 heapDown(0) return head } return -1 } //Update heap item i function heapUpdate(i, w) { var a = heap[i] if(weights[a] === w) { return i } weights[a] = -Infinity heapUp(i) heapPop() weights[a] = w heapCount += 1 return heapUp(heapCount-1) } //Kills a vertex (assume vertex already removed from heap) function kill(i) { if(dead[i]) { return } //Kill vertex dead[i] = true //Fixup topology var s = inv[i] var t = outv[i] if(inv[t] >= 0) { inv[t] = s } if(outv[s] >= 0) { outv[s] = t } //Update weights on s and t if(index[s] >= 0) { heapUpdate(index[s], computeWeight(s)) } if(index[t] >= 0) { heapUpdate(index[t], computeWeight(t)) } } //Initialize weights and heap var heap = [] var index = new Array(n) for(var i=0; i>1; i>=0; --i) { heapDown(i) } //Kill vertices while(true) { var hmin = heapPop() if((hmin < 0) || (weights[hmin] > minArea)) { break } kill(hmin) } //Build collapsed vertex table var npositions = [] for(var i=0; i= 0 && tout >= 0 && tin !== tout) { var cin = index[tin] var cout = index[tout] if(cin !== cout) { ncells.push([ cin, cout ]) } } }) //Normalize result sc.unique(sc.normalize(ncells)) //Return final list of cells return { positions: npositions, edges: ncells } }