-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
211 lines (185 loc) · 5.91 KB
/
index.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
var Heap = require('heap');
var ndarray = require('ndarray');
var vec3 = require('gl-vec3');
var vec4 = require('gl-vec4');
var normals = require('normals').faceNormals;
var ops = require('ndarray-ops');
var solve = require('ndarray-linear-solve');
var removeOrphans = require('remove-orphan-vertices');
var removeDegenerates = require('remove-degenerate-cells');
function vertexError(vertex, quadratic) {
var xformed = new Array(4);
vec4.transformMat4(xformed, vertex, quadratic);
return vec4.dot(vertex, xformed);
};
function optimalPosition(v1, v2) {
var q1 = v1.error;
var q2 = v2.error;
var costMatrix = ndarray(new Float32Array(4 * 4), [4, 4]);
ops.add(costMatrix, q1, q2);
var mat4Cost = Array.from(costMatrix.data);
var optimal = ndarray(new Float32Array(4));
var toInvert = costMatrix;
toInvert.set(0, 3, 0);
toInvert.set(1, 3, 0);
toInvert.set(2, 3, 0);
toInvert.set(3, 3, 1);
var solved = solve(optimal, toInvert, ndarray([0, 0, 0, 1]));
if (!solved) {
var v1Homogenous = Array.from(v1.position);
v1Homogenous.push(1);
var v2Homogenous = Array.from(v2.position);
v2Homogenous.push(1);
var midpoint = vec3.add(new Array(3), v1.position, v2.position);
vec3.scale(midpoint, midpoint, 0.5);
midpoint.push(1);
var v1Error = vertexError(v1Homogenous, mat4Cost);
var v2Error = vertexError(v2Homogenous, mat4Cost);
var midpointError = vertexError(midpoint, mat4Cost);
var minimum = Math.min([v1Error, v2Error, midpointError]);
if (v1Error == minimum) {
optimal = v1Homogenous;
} else if (v2Error == minimum) {
optimal = v2Homogenous;
} else {
optimal = midpoint;
}
} else {
optimal = optimal.data;
}
var error = vertexError(optimal, mat4Cost);
return {vertex: optimal.slice(0, 3), error: error};
};
module.exports = function(cells, positions, faceNormals, threshold = 0) {
cells = removeDegenerates(cells);
if (!faceNormals) {
faceNormals = normals(cells, positions);
}
var n = positions.length;
var vertices = positions.map(function(p, i) {
return {
position: p,
index: i,
pairs: [],
error: ndarray(new Float32Array(4 * 4).fill(0), [4, 4])
}
});
cells.map(function(cell) {
for (var i = 0; i < 2; i++) {
var j = (i + 1) % 3;
var v1 = cell[i];
var v2 = cell[j];
// consistent ordering to prevent double entries
if (v1 < v2) {
vertices[v1].pairs.push(v2);
} else {
vertices[v2].pairs.push(v1);
}
}
});
if (threshold > 0) {
for (var i = 0; i < n; i++) {
for (var j = i - 1; j >= 0; j--) {
if (vec3.distance(cells[i], cells[j]) < threshold) {
if (i < j) {
vertices[i].pairs.push(vertices[j]);
} else {
vertices[j].pairs.push(vertices[i]);
}
}
}
}
}
cells.map(function(cell, cellId) {
var normal = faceNormals[cellId];
// [a, b, c, d] where plane is defined by a*x+by+cz+d=0
// choose the first vertex WLOG
var pointOnTri = positions[cell[0]];
var plane = [normal[0], normal[1], normal[2], -vec3.dot(normal, pointOnTri)];
cell.map(function(vertexId) {
var errorQuadric = ndarray(new Float32Array(4 * 4), [4, 4]);
for (var i = 0; i < 4; i++) {
for (var j = i; j >= 0; j--) {
var value = plane[i] * plane[j];
errorQuadric.set(i, j, value);
if (i != j) {
errorQuadric.set(j, i, value);
}
}
}
var existingQuadric = vertices[vertexId].error;
ops.add(existingQuadric, existingQuadric, errorQuadric);
})
});
var costs = new Heap(function(a, b) {
return a.cost - b.cost;
});
var edges = []
vertices.map(function(v1) {
v1.pairs.map(function(v2Index) {
var v2 = vertices[v2Index];
var optimal = optimalPosition(v1, v2);
var edge = {
pair: [v1.index, v2Index],
cost: optimal.error,
optimalPosition: optimal.vertex
};
costs.push(edge);
// to update costs
edges.push(edge);
});
});
var n = positions.length;
return function(targetCount) {
// deep-copy trick: https://stackoverflow.com/questions/597588/how-do-you-clone-an-array-of-objects-in-javascript
var newCells = JSON.parse(JSON.stringify(cells));
var deletedCount = 0;
while (n - deletedCount > targetCount) {
var leastCost = costs.pop();
var i1 = leastCost.pair[0];
var i2 = leastCost.pair[1];
if (i1 == i2) {
// edge has already been collapsed
continue;
}
vertices[i1].position = leastCost.optimalPosition;
for (var i = newCells.length - 1; i >= 0; i--) {
var cell = newCells[i];
var cellIndex2 = cell.indexOf(i2);
if (cellIndex2 != -1) {
if (cell.indexOf(i1) != -1) {
// Delete cells with zero area, as v1 == v2 now
newCells.splice(i, 1);
}
cell[cellIndex2] = i1;
}
}
var v1 = vertices[i1];
edges.map(function(edge, i) {
var edgeIndex1 = edge.pair.indexOf(i1);
var edgeIndex2 = edge.pair.indexOf(i2);
if (edgeIndex1 != -1 && edgeIndex2 != -1) {
edge.pair[edgeIndex2] = i1;
return;
}
if (edge.pair.indexOf(i1) != -1) {
var optimal = optimalPosition(v1, vertices[edge.pair[(edgeIndex1 + 1) % 2]]);
edge.optimalPosition = optimal.vertex;
edge.cost = optimal.error;
}
if (edge.pair.indexOf(i2) != -1) {
// use v1 as that is the new position of v2
var optimal = optimalPosition(v1, vertices[edge.pair[(edgeIndex2 + 1) % 2]]);
edge.pair[edgeIndex2] = i1;
edge.optimalPosition = optimal.vertex;
edge.cost = optimal.error;
}
});
costs.heapify();
deletedCount++;
}
return removeOrphans(newCells, vertices.map(function(p) {
return p.position
}));
};
}