This repository was archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmesh_parameterization.js
230 lines (196 loc) · 7.74 KB
/
mesh_parameterization.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { acos, cot, min, max, sqrt, tan } from "mathjs";
import { CircleGeometry, Vector3 } from "three";
import { inverse, Matrix } from "ml-matrix";
import { BOUNDARY_SHAPES, MOUTH_FIXATIONS, WEIGHT_APPROACHES } from "../constants";
function calculateAngle(vertex1, vertex2) {
// As described at https://www.jwwalker.com/pages/angle-between-vectors.html
const dot12 = vertex1.dot(vertex2);
const dot11 = vertex1.dot(vertex1);
const dot22 = vertex2.dot(vertex2);
return acos(dot12 / sqrt(dot11 * dot22));
}
function findClosestVertex(source, targets) {
let closest = undefined;
let minDistance = Infinity;
targets.forEach((target) => {
const currentDistance = target.distanceTo(source);
if (currentDistance < minDistance) {
closest = target;
minDistance = currentDistance;
}
});
return { closest, minDistance };
}
function shouldPin(vertex, initialBoundaryVertices, isMouthFixated) {
return (
initialBoundaryVertices.has(vertex) &&
// This has should ideally be replaced by a generic solution
(MOUTH_FIXATIONS[isMouthFixated] === MOUTH_FIXATIONS.True ||
!(
vertex.x >= -19 &&
vertex.x <= 27 &&
vertex.y >= -37 &&
vertex.y <= -33 &&
vertex.z >= 4 &&
vertex.z <= 24
))
);
}
export function generateMeshParameterization({
geometry,
weightApproach,
boundaryShape,
isMouthFixated,
logger,
}) {
let startTime, elapsedTime;
startTime = new Date();
logger && logger.log(`Finding boundary vertices...`);
const edgeCounts = new Map();
geometry.faces.forEach((face) => {
const edges = [
[face.a, face.b],
[face.a, face.c],
[face.b, face.c],
];
edges.forEach(([vertexIndex1, vertexIndex2]) => {
let minIndex = min(vertexIndex1, vertexIndex2),
maxIndex = max(vertexIndex1, vertexIndex2),
identifier = `${minIndex},${maxIndex}`;
edgeCounts.set(identifier, (edgeCounts.get(identifier) || 0) + 1);
});
});
const boundaryEdges = new Set();
const initialBoundaryVertices = new Set();
const finalBoundaryVertices = new Set();
edgeCounts.forEach((count, edge) => {
if (count === 1) {
boundaryEdges.add({
vertices: [
geometry.vertices[Number(edge.split(",")[0])],
geometry.vertices[Number(edge.split(",")[1])],
],
});
initialBoundaryVertices.add(geometry.vertices[Number(edge.split(",")[0])]);
initialBoundaryVertices.add(geometry.vertices[Number(edge.split(",")[1])]);
}
});
elapsedTime = new Date() - startTime;
logger && logger.log(`\tdone in ${elapsedTime.toLocaleString()}ms.`);
startTime = new Date();
logger && logger.log(`Calculating W, bx, and by matrices...`);
const length = geometry.vertices.length;
const W = Matrix.zeros(length, length);
const bx = Matrix.zeros(length, 1);
const by = Matrix.zeros(length, 1);
geometry.faces.forEach((face) => {
const edges = [
[face.a, face.b, face.c],
[face.a, face.c, face.b],
[face.b, face.c, face.a],
];
edges.forEach(([vertexIndex1, vertexIndex2, otherVertexIndex]) => {
const vertex1 = geometry.vertices[vertexIndex1],
vertex2 = geometry.vertices[vertexIndex2],
otherVertex = geometry.vertices[otherVertexIndex];
let value;
if (WEIGHT_APPROACHES[weightApproach] === WEIGHT_APPROACHES.Uniform) {
value = 0.5;
} else if (WEIGHT_APPROACHES[weightApproach] === WEIGHT_APPROACHES.Harmonic) {
const angle = calculateAngle(
new Vector3().subVectors(vertex1, otherVertex),
new Vector3().subVectors(vertex2, otherVertex),
);
value = cot(angle) / 2;
} else if (WEIGHT_APPROACHES[weightApproach] === WEIGHT_APPROACHES.MeanValue) {
const angle = calculateAngle(
new Vector3().subVectors(otherVertex, vertex1),
new Vector3().subVectors(vertex2, vertex1),
);
value = tan(angle / 2) / (2 * vertex1.distanceTo(vertex2));
}
if (!shouldPin(vertex1, initialBoundaryVertices, isMouthFixated)) {
const acc = W.get(vertexIndex1, vertexIndex2);
W.set(vertexIndex1, vertexIndex2, acc + value);
}
if (!shouldPin(vertex2, initialBoundaryVertices, isMouthFixated)) {
const acc = W.get(vertexIndex2, vertexIndex1);
W.set(vertexIndex2, vertexIndex1, acc + value);
}
});
});
const circleVertices = new CircleGeometry(75, initialBoundaryVertices.size).vertices;
circleVertices.shift();
geometry.vertices.forEach((vertex, index) => {
if (!shouldPin(vertex, initialBoundaryVertices, isMouthFixated)) {
const row = W.getRow(index);
let value = 0;
row.forEach((cur) => {
value = value - cur;
});
W.set(index, index, value);
} else {
W.set(index, index, 1);
if (
BOUNDARY_SHAPES[boundaryShape] === BOUNDARY_SHAPES.Free ||
(vertex.x >= -19 &&
vertex.x <= 27 &&
vertex.y >= -37 &&
vertex.y <= -33 &&
vertex.z >= 4 &&
vertex.z <= 24)
) {
finalBoundaryVertices.add(new Vector3(vertex.x, vertex.y, 0));
bx.set(index, 0, vertex.x);
by.set(index, 0, vertex.y);
} else if (BOUNDARY_SHAPES[boundaryShape] === BOUNDARY_SHAPES.Circle) {
const { closest } = findClosestVertex(
new Vector3(vertex.x, vertex.y, 0),
circleVertices,
);
finalBoundaryVertices.add(new Vector3(closest.x, closest.y, 0));
bx.set(index, 0, closest.x);
by.set(index, 0, closest.y);
}
}
});
elapsedTime = new Date() - startTime;
logger && logger.log(`\tdone in ${elapsedTime.toLocaleString()}ms.`);
startTime = new Date();
logger && logger.log(`Calculating inverse W matrix...`);
const Wi = inverse(W);
elapsedTime = new Date() - startTime;
logger && logger.log(`\tdone in ${elapsedTime.toLocaleString()}ms.`);
startTime = new Date();
logger && logger.log(`Calculating xx, and xy matrices...`);
const xx = Wi.mmul(bx);
const xy = Wi.mmul(by);
elapsedTime = new Date() - startTime;
logger && logger.log(`\tdone in ${elapsedTime.toLocaleString()}ms.`);
startTime = new Date();
logger && logger.log(`Creating parameterization edges...`);
const allEdges = new Set();
geometry.faces.forEach((face) => {
const edges = [
[face.a, face.b],
[face.a, face.c],
[face.b, face.c],
];
edges.forEach(([vertexIndex1, vertexIndex2]) => {
allEdges.add({
vertices: [
new Vector3(xx.get(vertexIndex1, 0), xy.get(vertexIndex1, 0), 0),
new Vector3(xx.get(vertexIndex2, 0), xy.get(vertexIndex2, 0), 0),
],
});
});
});
elapsedTime = new Date() - startTime;
logger && logger.log(`\tdone in ${elapsedTime.toLocaleString()}ms.`);
return {
allEdges,
boundaryEdges,
initialBoundaryVertices,
finalBoundaryVertices,
};
}