topology.js
2.21 KB
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
import { Vector3 } from 'three';
// Build a map from each edge to its vertices and triangles
export const buildEdgeMap = (geometry) => {
const index = geometry.index;
const posAttr = geometry.attributes.position;
const edges = {};
let i = 0;
while (i < index.count / 3) {
const triIndex = i;
const ia = index.getX(triIndex * 3 + 0);
const ib = index.getX(triIndex * 3 + 1);
const ic = index.getX(triIndex * 3 + 2);
for (let edge of [
{ i1: ia, i2: ib },
{ i1: ib, i2: ic },
{ i1: ic, i2: ia }
]) {
const v1 = new Vector3(posAttr.getX(edge.i1), posAttr.getY(edge.i1), posAttr.getZ(edge.i1));
const v2 = new Vector3(posAttr.getX(edge.i2), posAttr.getY(edge.i2), posAttr.getZ(edge.i2));
const key1 = `${v1.x}_${v1.y}_${v1.z}`;
const key2 = `${v2.x}_${v2.y}_${v2.z}`;
// const key1 = edge.i1;
// const key2 = edge.i2;
const key = key1 < key2 ? `${key1}-${key2}` : `${key2}-${key1}`;
const length = v1.distanceTo(v2);
if (edges[key] == undefined) {
edges[key] = {
length: length,
triIndices: [triIndex],
node1: key1 < key2 ? key1 : key2,
node2: key1 < key2 ? key2 : key1
};
} else {
edges[key].triIndices.push(triIndex);
}
}
++i;
}
return edges;
};
// Given a Edge key, convert it to the neibor triangles
export const posKey2Edge = (edgeMaps, key) => {
return edgeMaps[key] ? edgeMaps[key].triIndices : null;
};
export const posVertexToCreateKey = (v1, v2 /*, index1, index2*/) => {
const key1 = `${v1.x}_${v1.y}_${v1.z}`;
const key2 = `${v2.x}_${v2.y}_${v2.z}`;
// const key1 = index1;
// const key2 = index2;
const key = key1 < key2 ? `${key1}-${key2}` : `${key2}-${key1}`;
return key;
};
export const posTri2Indices = (geometry, triIndex) => {
const index = geometry.index;
const ia = index.getX(triIndex * 3 + 0);
const ib = index.getX(triIndex * 3 + 1);
const ic = index.getX(triIndex * 3 + 2);
return [ia, ib, ic];
};