forked from osresearch/plotter-vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
467 lines (393 loc) · 9.39 KB
/
sketch.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* Parse ascii or binary STL file into a list of triangles
* Binary Header (84 bytes):
* 80 byte name
* uint32_t number of triangles
*
* Binary Triangle (50 bytes)
* 3 32-bit float normals
* 9 32-bit float x,y,z tripples
* uint16_t attributes (ignored)
*/
let x_offset;
let y_offset;
let z_scale = 1;
dark_mode = true;
redblue_mode = false;
// blue color suggested by https://mastodon.sdf.org/@elb/105351977660915938
red_color = 0xff0000;
blue_color = 0x14ecfc;
verbose = false;
stl = false;
stl2 = false;
let camera;
let camera2; // for 3D
eye_separation = 2;
redraw = false;
reproject = false;
let vx = 0;
let vy = 0;
let vz = 0;
let last_x = 0;
let last_y = 0;
let move_lookat = false;
let camera_psi = 0;
let camera_theta = 0;
let camera_radius = 100;
let start_time = 0;
let tri_per_sec = 0;
function computeEye()
{
// normalize theta and psi
if (camera_theta < -Math.PI)
camera_theta += 2 * Math.PI;
else
if (camera_theta > +Math.PI)
camera_theta -= 2 * Math.PI;
if (camera_psi < -Math.PI)
camera_psi += 2 * Math.PI;
else
if (camera_psi > +Math.PI)
camera_psi -= 2 * Math.PI;
camera.eye.x = camera_radius * Math.sin(camera_theta) * Math.sin(camera_psi);
camera.eye.y = camera_radius * Math.sin(camera_theta) * Math.cos(camera_psi);
camera.eye.z = camera_radius * Math.cos(camera_theta);
if (camera_theta < 0)
camera.up.z = -1;
else
camera.up.z = +1;
camera.eye.add(camera.lookat);
camera.update_matrix();
// duplicate for 3D (lookat is shared)
// should scale the eye separation based on the radius since
// otherwise it becomes weird at long distances
// in DARK mode, the eye glasses seem to be backwards?
// normally left eye is red, right eye is blue, but
// that messes up with a dark background.
camera2.eye.x = camera_radius * Math.sin(camera_theta) * Math.sin(camera_psi + eye_separation * Math.PI / 180);
camera2.eye.y = camera_radius * Math.sin(camera_theta) * Math.cos(camera_psi + eye_separation * Math.PI / 180);
camera2.eye.z = camera_radius * Math.cos(camera_theta);
// the lookat and up values are shared between the cameras
camera2.eye.add(camera.lookat);
camera2.update_matrix();
}
function loadBytes(file, callback) {
let oReq = new XMLHttpRequest();
oReq.open("GET", file, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(oEvent) {
let arrayBuffer = oReq.response;
if (arrayBuffer && callback) {
callback(arrayBuffer);
}
}
oReq.send(null);
}
function setup()
{
let canvas = createCanvas(windowWidth-10, windowHeight-30); // WEBGL?
x_offset = width/2;
y_offset = height/2;
// Move the canvas so it’s inside our <div id="sketch-holder">.
canvas.parent('sketch-holder');
//createCanvas(1000, 1080); // WEBGL?
background(0);
loadBytes("test.stl", function(d){
stl = new STL(d);
stl2 = new STL(d);
reproject = true;
});
// initial viewport
vx = vy = vz = 0;
camera_theta = 70 * Math.PI / 180;
camera_psi = -150 * Math.PI / 180;
camera_radius = 170;
let eye = createVector(0,camera_radius,0);
let eye2 = createVector(0,camera_radius,0);
let lookat = createVector(0,0,00);
let up = createVector(0,0,1);
let fov = 60;
camera = new Camera(eye,lookat,up,fov);
camera2 = new Camera(eye2,lookat,up,fov);
computeEye();
}
function v3_line(p0,p1)
{
if (verbose)
{
push()
color(255,255,255,40);
stroke(0.1);
text(p0.z.toFixed(2), p0.x, -p0.y);
text(p1.z.toFixed(2), p1.x, -p1.y);
pop();
}
line(p0.x, -p0.y, p1.x, -p1.y);
}
function drawAxis(camera, lookat)
{
// draw an axis marker at the look-at point
const origin = camera.project(lookat)
const xaxis = camera.project(new p5.Vector(5,0,0).add(lookat));
const yaxis = camera.project(new p5.Vector(0,5,0).add(lookat));
const zaxis = camera.project(new p5.Vector(0,0,5).add(lookat));
strokeWeight(5);
if (!xaxis || !yaxis || !zaxis)
{
// draw them anyway, since no good ordering is possible
stroke(255,0,0);
if (xaxis) v3_line(origin, xaxis);
stroke(0,255,0);
if (yaxis) v3_line(origin, yaxis);
stroke(0,0,255);
if (zaxis) v3_line(origin, zaxis);
return;
}
// draw the axis lines in back-to-front order
const xd = xaxis.z;
const yd = yaxis.z;
const zd = zaxis.z;
if (xd > yd && yd > zd)
{
stroke(255,0,0); v3_line(origin, xaxis);
stroke(0,255,0); v3_line(origin, yaxis);
stroke(0,0,255); v3_line(origin, zaxis);
} else
if (xd > zd && zd > yd)
{
stroke(255,0,0); v3_line(origin, xaxis);
stroke(0,0,255); v3_line(origin, zaxis);
stroke(0,255,0); v3_line(origin, yaxis);
} else
if (yd > xd && xd > zd)
{
stroke(0,255,0); v3_line(origin, yaxis);
stroke(255,0,0); v3_line(origin, xaxis);
stroke(0,0,255); v3_line(origin, zaxis);
} else
if (yd > zd && zd > xd)
{
stroke(0,255,0); v3_line(origin, yaxis);
stroke(0,0,255); v3_line(origin, zaxis);
stroke(255,0,0); v3_line(origin, xaxis);
} else
if (zd > xd && xd > yd)
{
stroke(0,0,255); v3_line(origin, zaxis);
stroke(255,0,0); v3_line(origin, xaxis);
stroke(0,255,0); v3_line(origin, yaxis);
} else
if (zd > yd && yd > xd)
{
stroke(0,0,255); v3_line(origin, zaxis);
stroke(0,255,0); v3_line(origin, yaxis);
stroke(255,0,0); v3_line(origin, xaxis);
} else {
// wtf how did we end up here?
}
}
function keyReleased()
{
vx = vy = vz = 0;
move_lookat = false;
}
function keyPressed()
{
console.log(keyCode);
if (keyCode == SHIFT)
move_lookat = true;
if (keyCode == LEFT_ARROW)
vx = -10;
else
if (keyCode == RIGHT_ARROW)
vx = +10;
if (keyCode == UP_ARROW)
vz = -10;
else
if (keyCode == DOWN_ARROW)
vz = +10;
//return false;
}
function cameraView(theta,psi)
{
camera_theta = theta * Math.PI / 180;
camera_psi = psi * Math.PI / 180;
computeEye();
reproject = true;
}
function keyTyped()
{
if (key === 'v')
{
verbose = !verbose;
reproject = true;
}
if (key === '1')
cameraView(90, 0);
if (key === '2')
cameraView(90, 90);
if (key === '3')
cameraView(90, 180);
if (key === '4')
cameraView(90, 270);
if (key === '5')
cameraView(1, 1);
if (key === 't')
{
redblue_mode = !redblue_mode;
reproject = true;
}
}
function mousePressed()
{
last_x = mouseX;
last_y = mouseY;
}
function mouseWheel(event)
{
vz = event.delta * 0.5;
}
function windowResized() {
resizeCanvas(windowWidth-10, windowHeight-30);
camera.width = width;
camera.height = height;
x_offset = width/2;
y_offset = height/2;
reproject = true;
}
function draw()
{
if (!stl)
return;
if (mouseIsPressed && mouseY >= 0)
{
vx = (mouseX - last_x) * 0.5;
vy = (mouseY - last_y) * 0.5;
last_x = mouseX;
last_y = mouseY;
}
if (vx != 0 || vy != 0 || vz != 0)
{
camera_radius += vz;
if (camera_radius <= 0)
camera_radius = 1;
if (move_lookat)
{
camera.lookat.x += vx;
camera.lookat.z += vy;
} else {
camera_psi += vx * 0.01;
camera_theta -= vy * 0.01;
}
computeEye();
reproject = true;
vx = 0;
vy = 0;
vz = 0;
}
// if there are segments left to process, continue to force redraw
if (!stl || !(redraw || reproject))
return;
redraw = false;
if(reproject)
{
reproject = false;
redraw = true;
stl.project(camera);
if (redblue_mode)
stl2.project(camera2);
start_time = performance.now();
tri_per_sec = 0;
}
// they are dragging; do not try to do any additional work
// and only compute the alterntate view if we're in 3D mode
// if there was work done, return true to force another
// pass through the draw loop.
if (!mouseIsPressed)
{
if (redblue_mode)
stl2.do_work(camera2, 200);
stl.do_work(camera, 200);
}
if (dark_mode)
{
background(0);
fill(9);
} else {
background(255);
fill(253);
}
noStroke();
textSize(128);
textAlign(RIGHT, BOTTOM);
text("plotter.vision", width, height);
fill(dark_mode ? 150 : 80);
textSize(12);
textAlign(LEFT, BOTTOM);
text("camera " + int(camera.eye.x) + "," + int(camera.eye.y) + "," + int(camera.eye.z), 10, 30);
text("lookat " + int(camera.lookat.x) + "," + int(camera.lookat.y) + "," + int(camera.lookat.z), 10, 50);
text("theta " + int(camera_theta * 180 / Math.PI), 10, 100);
text(" psi " + int(camera_psi * 180 / Math.PI), 10, 120);
text(" r " + int(camera_radius), 10, 140);
if (stl.segments.length == 0)
{
if (tri_per_sec == 0)
tri_per_sec = int(stl.triangles.length * 1000 / (performance.now() - start_time));
text("tri/s " + tri_per_sec, 10, 180);
}
push();
translate(x_offset, y_offset);
scale(z_scale);
// draw all of our in-processing segments lightly
strokeWeight(1);
if (redblue_mode)
stroke(200,0,200,100);
else
stroke(0,200,0);
for(let s of stl.segments)
v3_line(s.p0, s.p1);
if (verbose)
{
stroke(100,0,0,100);
for(let s of stl.coplanar)
v3_line(s.p0, s.p1);
}
if (stl.segments.length != 0 || (redblue_mode && stl2.segments.length != 0))
{
// if there are in process ones,
// draw an XYZ axis at the lookat
// and keep computing
drawAxis(camera, camera.lookat);
redraw = true;
} else {
// all done, this should be our last pass through
// the draw loop
redraw = false;
}
// Draw all of our visible segments sharply
strokeWeight(1);
if (dark_mode)
stroke(255,255,255);
else
stroke(0,0,0);
if (redblue_mode)
{
stroke(
((blue_color) >> 16) & 0xFF,
((blue_color) >> 8) & 0xFF,
((blue_color) >> 0) & 0xFF,
200
);
for(let s of stl2.visible_segments)
v3_line(s.p0, s.p1);
stroke(
((red_color) >> 16) & 0xFF,
((red_color) >> 8) & 0xFF,
((red_color) >> 0) & 0xFF,
80
);
}
for(let s of stl.visible_segments)
v3_line(s.p0, s.p1);
pop();
}