-
Notifications
You must be signed in to change notification settings - Fork 5
/
contour.js
162 lines (142 loc) · 3.71 KB
/
contour.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict'
module.exports = extractContour
var ndarray = require('ndarray')
var pool = require('typedarray-pool')
var ndsort = require('ndarray-sort')
var contourAlgorithm = require('./lib/codegen')
function getDimension(cells) {
var numCells = cells.length
var d = 0
for(var i=0; i<numCells; ++i) {
d = Math.max(d, cells[i].length)|0
}
return d-1
}
function getSigns(values, level) {
var numVerts = values.length
var vertexSigns = pool.mallocUint8(numVerts)
for(var i=0; i<numVerts; ++i) {
vertexSigns[i] = (values[i] < level)|0
}
return vertexSigns
}
function getEdges(cells, d) {
var numCells = cells.length
var maxEdges = ((d * (d+1)/2) * numCells)|0
var edges = pool.mallocUint32(maxEdges*2)
var ePtr = 0
for(var i=0; i<numCells; ++i) {
var c = cells[i]
var d = c.length
for(var j=0; j<d; ++j) {
for(var k=0; k<j; ++k) {
var a = c[k]
var b = c[j]
edges[ePtr++] = Math.min(a,b)|0
edges[ePtr++] = Math.max(a,b)|0
}
}
}
var nedges = (ePtr/2)|0
ndsort(ndarray(edges, [nedges,2]))
var ptr = 2
for(var i=2; i<ePtr; i+=2) {
if(edges[i-2] === edges[i] &&
edges[i-1] === edges[i+1]) {
continue
}
edges[ptr++] = edges[i]
edges[ptr++] = edges[i+1]
}
return ndarray(edges, [(ptr/2)|0, 2])
}
function getCrossingWeights(edges, values, signs, level) {
var edata = edges.data
var numEdges = edges.shape[0]
var weights = pool.mallocDouble(numEdges)
var ptr = 0
for(var i=0; i<numEdges; ++i) {
var a = edata[2*i]
var b = edata[2*i+1]
if(signs[a] === signs[b]) {
continue
}
var va = values[a]
var vb = values[b]
edata[2*ptr] = a
edata[2*ptr+1] = b
weights[ptr++] = (vb - level) / (vb - va)
}
edges.shape[0] = ptr
return ndarray(weights, [ptr])
}
function getCascade(edges, numVerts) {
var result = pool.mallocInt32(numVerts*2)
var numEdges = edges.shape[0]
var edata = edges.data
result[0] = 0
var lastV = 0
for(var i=0; i<numEdges; ++i) {
var a = edata[2*i]
if(a !== lastV) {
result[2*lastV+1] = i
while(++lastV < a) {
result[2*lastV] = i
result[2*lastV+1] = i
}
result[2*lastV] = i
}
}
result[2*lastV+1] = numEdges
while(++lastV < numVerts) {
result[2*lastV] = result[2*lastV+1] = numEdges
}
return result
}
function unpackEdges(edges) {
var ne = edges.shape[0]|0
var edata = edges.data
var result = new Array(ne)
for(var i=0; i<ne; ++i) {
result[i] = [edata[2*i], edata[2*i+1]]
}
return result
}
function extractContour(cells, values, level, d) {
level = level||0.0
//If user didn't specify `d`, use brute force scan
if(typeof d === 'undefined') {
d = getDimension(cells)
}
//Count number of cells
var numCells = cells.length
if(numCells === 0 || d < 1) {
return {
cells: [],
vertexIds: [],
vertexWeights: []
}
}
//Read in vertex signs
var vertexSigns = getSigns(values, +level)
//First get 1-skeleton, find all crossings
var edges = getEdges(cells, d)
var weights = getCrossingWeights(edges, values, vertexSigns, +level)
//Build vertex cascade to speed up binary search
var vcascade = getCascade(edges, values.length|0)
//Then construct cells
var faces = contourAlgorithm(d)(cells, edges.data, vcascade, vertexSigns)
//Unpack data into pretty format
var uedges = unpackEdges(edges)
var uweights = [].slice.call(weights.data, 0, weights.shape[0])
//Release data
pool.free(vertexSigns)
pool.free(edges.data)
pool.free(weights.data)
pool.free(vcascade)
return {
cells: faces,
vertexIds: uedges,
vertexWeights: uweights
}
}