-
Notifications
You must be signed in to change notification settings - Fork 0
/
mesh.js
108 lines (87 loc) · 2.04 KB
/
mesh.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
"use strict";
class Face {
constructor(v0, v1, v2) {
this.v0 = v0;
this.v1 = v1;
this.v2 = v2;
}
}
class Edge {
constructor(v0, v1) {
this.v0 = v0;
this.v1 = v1;
}
equal(v0, v1) {
return (this.v0==v0 && this.v1==v1) || (this.v0==v1 && this.v1==v0);
}
}
class Mesh {
constructor() {
this.vertices = [];
this.faces = [];
this.boundaryEdges = [];
}
build(positions, indices) {
this.preallocate(positions, indices);
for (let i=0; i<this.vertices.length; i++) {
let p = positions[i];
this.vertices[i] = new Vector(p.x, p.y, p.z);
}
for (let i=0; i<this.faces.length; i++) {
let v0 = indices[3*i+0];
let v1 = indices[3*i+1];
let v2 = indices[3*i+2];
this.faces[i] = new Face(v0,v1,v2);
}
}
preallocate(positions, indices) {
let nVertices = positions.length;
let nFaces = indices.length / 3;
// clear arrays
this.vertices.length = 0;
this.faces.length = 0;
// allocate space
this.vertices = new Array(nVertices);
this.faces = new Array(nFaces);
}
// count number of tris containing (a,b) as an edge
countAdjacentTri(e) {
let count = 0;
for (let i=0; i<this.faces.length; i++) {
let f = this.faces[i];
let v0 = f.v0;
let v1 = f.v1;
let v2 = f.v2;
if (e.equal(v0,v1)) count++;
if (e.equal(v1,v2)) count++;
if (e.equal(v2,v0)) count++;
}
return count;
}
computeBoundaryEdges() {
// visit each edge
for (let i=0; i<this.faces.length; i++) {
let f = this.faces[i];
let v0 = f.v0;
let v1 = f.v1;
let v2 = f.v2;
// edge1
let e1 = new Edge(v0,v1);
let count = this.countAdjacentTri(e1);
if (count == 1)
this.boundaryEdges.push(e1);
// edge2
let e2 = new Edge(v1,v2);
count = this.countAdjacentTri(e2);
if (count == 1)
this.boundaryEdges.push(e2);
// edge3
let e3 = new Edge(v2,v0);
count = this.countAdjacentTri(e3);
if (count == 1)
this.boundaryEdges.push(e3);
}
}
orderBoundaryEdges() {
}
}