-
Notifications
You must be signed in to change notification settings - Fork 0
/
structures.js
148 lines (118 loc) · 3.11 KB
/
structures.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
function Node(x, y){
this.x = x;
this.y = y;
this.label = ++labels;
this.connect = function(node){
edges.add(this, node);
}
this.disconnect = function(node){
edges.remove(this, node);
}
this.getEdge = function(node){
return edges.get(this, node);
}
}
function Triangle(nodea, nodeb, nodec){
this.a = nodea;
this.b = nodeb;
this.c = nodec;
this.boundary = false;
this.updateCenter = function(){
var circ = circumCenter(this);
this.center = {x: circ[0], y: circ[1], r: circ[2]};
}
this.updateCenter();
this.hasEdge = function(a, b){
var rtn = false;
if(this.a === a || this.b === a || this.c === a){
if(this.a === b || this.b === b || this.c === b){
rtn = true;
}
} return rtn;
}
this.getOppositePoint = function(a, b){
if(this.a !== a && this.a !== b){
return this.a;
}
else if(this.b !== a && this.b !== b){
return this.b;
}
else if(this.c !== a && this.c !== b){
return this.c;
}
else{ return null; }
}
}
function EdgeTable(){
this.edges = {};
this.add = function(a, b){
var key = a.label <= b.label ? a.label+' '+b.label : b.label+' '+a.label;
this.edges[key] = {a: a, b: b};
}
this.get = function(a, b){
var key = a.label <= b.label ? a.label+' '+b.label : b.label+' '+a.label;
return this.edges[key];
}
this.remove = function(a, b){
var key = a.label <= b.label ? a.label+' '+b.label : b.label+' '+a.label;
delete this.edges[key];
}
}
//can lookup triangles or pairs by edge
function TriangleTable(){
this.triangles = {};
this.edgetriangles = {};
//hashes triangle and all edge combinations
this.add = function(t){
var key = t.a.label+','+t.b.label+','+t.c.label;
this.triangles[key] = t;
var edgekeys = [
t.a.label+','+t.b.label, t.b.label+','+t.c.label, t.a.label+','+t.c.label,
t.b.label+','+t.a.label, t.c.label+','+t.b.label, t.c.label+','+t.a.label
];
for (var i = 0; i < edgekeys.length; i++) {
if(this.edgetriangles[edgekeys[i]] === undefined){
this.edgetriangles[edgekeys[i]] = []; }
}
for (var i = 0; i < edgekeys.length; i++){
if(this.edgetriangles[edgekeys[i]].length < 2)
this.edgetriangles[edgekeys[i]].push(t);
}
}
//returns triangle or triangle array depending on arguments
this.get = function(t, b){
if(!b){
var key = t.a.label+','+t.b.label+','+t.c.label;
return this.triangles[key];
}else{
var key = t.label+','+b.label;
return this.edgetriangles[key];
}
}
//returns neighbor opposite ab
this.getNeighbor = function(t, a, b){
var key = a.label+','+b.label;
var a = this.edgetriangles[key];
for (var i = a.length-1; i >= 0; i--) {
if(a[i] !== t){
return a[i];
}
}
}
//unhashes triangle
this.remove = function(t){
var key = t.a.label+','+t.b.label+','+t.c.label;
delete this.triangles[key];
var edgekeys = [
t.a.label+','+t.b.label, t.b.label+','+t.c.label, t.a.label+','+t.c.label,
t.b.label+','+t.a.label, t.c.label+','+t.b.label, t.c.label+','+t.a.label
];
for (var i = 0; i < edgekeys.length; i++) {
if(this.edgetriangles[edgekeys[i]])
for (var j = this.edgetriangles[edgekeys[i]].length; j >= 0; j--) {
if(this.edgetriangles[edgekeys[i]][j] === t){
this.edgetriangles[edgekeys[i]].splice(j, 1); break;}
}
}
}
}