-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
284 lines (253 loc) · 7.29 KB
/
functions.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/////////////////////////
// //
// MISC FUNCTIONS //
// //
/////////////////////////
//
// ANGLE FUNCTIONS
//
//
//
function deg_to_rad(deg) {
return deg * Math.PI / 180.0;
}
function rad_to_deg(rad) {
return 180 * rad / Math.PI;
}
function cart_to_polar(x, y, z) {
var ret = new THREE.Vector3();
ret.set(Math.atan2(y, x), Math.atan2(Math.sqrt(x * x + y * y), z), Math.sqrt(x*x + y*y + z*z));
return ret;
}
function polar_to_cart(theta, phi, radius) {
var ret = new THREE.Vector3();
theta = deg_to_rad(90 - theta);
phi = deg_to_rad(phi);
ret.set(radius * Math.sin(phi) * Math.cos(theta),
radius * Math.cos(phi),
radius * Math.sin(phi) * Math.sin(theta)); // Y AND Z ARE SWITCHED
return ret;
}
function angle_between_vectors(a, b) { // CARETESIAN VECTORS
var dot_product = a.x * b.x + a.y * b.y + a.z * b.z;
return rad_to_deg(Math.acos(dot_product));
}
function angle_to_positive(angle) {
if (angle < 0) {
return (360.0 + angle);
} else {
return angle;
}
}
//
//
//
// LOADING FUNCTION
//
//
//
//
function load_ports() {
var x,
loader = new THREE.ColladaLoader();
loader.options.convertUpAxis = true;
//console.log( port_names );
loader.load("TargetChamberPorts.dae", function (collada) {
var dae_large = collada.scene.getDescendants();
for ( x = 0; x < port_names.length; x += 1) {
dae = dae_large[x]; // Geometry
var port_texture = new THREE.ImageUtils.loadTexture('data/textures/' + dae.name.replace('_', '') + '.png'), // LAST HERE. Find descrepency entre port_names[x] and dae.name!!
port_material = new THREE.MeshBasicMaterial({
map: port_texture,
transparent: true,
opacity: 1.0
});
dae.name = port_names[x];
dae.scale.x = dae.scale.y = dae.scale.z = SCALE;
dae.rotation.y = -0.328; // Weird shift in orientation...I think the models were exported incorrectly oriented
dae.updateMatrix(loader.options.convertUpAxis = true);
dae.geometry.computeBoundingBox();
dae.material = port_material;
scene.add(dae);
objects.push(dae);
if (include(leg1_names, port_names[x])) {
leg1_ports.push(dae);
} else if (include(leg2_names, port_names[x])) {
leg2_ports.push(dae);
} else if (include(leg3_names, port_names[x])) {
leg3_ports.push(dae);
}
}
});
}
//
//
//
// TOGGLES
//
//
//
//
function toggle_diagnostic(name) {
//var e = document.getElementById("diag_select");
//var strUser = e.options[e.selectedIndex].value;
//console.log( name );
var i;
for (i = 0; i < diagnostics.length; i += 1) {
//console.log( diagnostics[i].name );
console.log(diagnostics[i].name);
console.log(name);
if (diagnostics[i].name.trim() == name.trim()) {
console.log("MATCH");
var obj = diagnostics[i];
obj.visible ^= true;
break;
}
}
}
function toggle_TC() {
target_chamber.visible ^= true;
}
// Check what array port is in
function include(arr, obj) {
return (arr.indexOf(obj) !== -1);
}
// Turn on and off ports
function toggle_ports(id) {
var i;
switch (id) {
case 0: // LEG 1 (Blue)
for (i = 0; i < leg1_ports.length; i += 1) {
leg1_ports[i].visible ^= true;
}
break;
case 1: // LEG 2 (Green)
for (i = 0; i < leg2_ports.length; i += 1) {
leg2_ports[i].visible ^= true;
}
break;
case 2: // LEG 3 (Red)
for (i = 0; i < leg3_ports.length; i += 1) {
leg3_ports[i].visible ^= true;
}
break;
}
}
function change_opacity() {
var i;
for (i = 0; i < objects.length; i++) {
objects[i].material.opacity < 1 ? objects[i].material.opacity = 1 : objects[i].material.opacity = 0.4;
}
}
function toggle_tools_visibility() {
var e = document.getElementById("tools_div");
if (e.style.display == 'block') {
e.style.display = 'none';
} else {
e.style.display = 'block';
}
}
function choose_obj_by_id(array, id) {
for (var i = 0; i < array.length; i++) {
if (array[i].portID == id) {
return array[i];
}
}
return null;
}
//
//
//
// DRAWING
//
//
//
// Beam port lines needs lots of fixing, but I'm triaging for now and leaving it alone
function draw_beam_port_lines() { // AUTOMATICALLY TOGGLES
if (beam_port_lines.length) {
//console.log(beam_port_lines);
for (var i = 0; i < beam_port_lines.length; i++) {
beam_port_lines[i].visible ^= true;
}
} else {
for (var i = 0; i < beamPorts.length; i++) {
var line = new draw_line_polar(260, parseInt(beamPorts[i].theta), parseInt(beamPorts[i].phi), Math.random() * 0xffffff, 2, true, 10, 3, 0);
line.visible = false;
scene.add(line);
//console.log( line );
beam_port_lines.push(line);
}
}
}
function draw_line(start, end, color, line_width, dashed, dash_size, gap_size) {
var geometry = new THREE.Geometry();
var vert_array = geometry.vertices;
vert_array.push(start, end);
geometry.computeLineDistances();
if (dashed) {
var line_material = new THREE.LineDashedMaterial({
color: color,
dashSize: dash_size,
gapSize: gap_size,
linewidth: line_width
});
} else {
var line_material = new THREE.LineBasicMaterial({
color: color,
linewidth: line_width
});
}
var line = new THREE.Line(geometry, line_material);
//console.log( line );
return line;
}
function draw_line_polar(radius, theta, phi, color, line_width, dashed, dash_size, gap_size, full_line) {
// Takes degrees!
var start = new THREE.Vector3(0, 0, 0);
var end = polar_to_cart(theta, phi, radius);
return draw_line(start, end, color, line_width, dashed, dash_size, gap_size);
}
//
//
//
// MISC
//
//
//
//
//quick and dirty open/close tab method
function TabButton(item) {
var theContentArea = item.id.split("Button")[0] + "Content";
var buttonID = $(item).attr('class').split(" ")[0];
buttonId = buttonID.split("Button")[0];
//alert($(item).attr('rel'));
if ($(item).attr('rel') == "open") {
$('#' + theContentArea).animate({
width: '-500px'
}, '4000');
if (buttonId == "right") {
$(item).animate({
left: '+500px'
}, '4000');
} else {
$(item).animate({
left: '-500px'
}, '4000');
}
$(item).attr('rel', 'close');
} else {
$('#' + theContentArea).animate({
width: '+500px'
}, '4000');
if (buttonId == "right") {
$(item).animate({
left: '0px'
}, '4000');
} else {
$(item).animate({
left: '0px'
}, '4000');
}
$(item).attr('rel', 'open');
}
}