-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathray.html
611 lines (567 loc) · 15.7 KB
/
ray.html
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
<!DOCTYPE html>
<html xmlns="www.w3.org/1999/xhtml" xml:lang="en" manifest="appcache">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<title>js raytracer: spheroid</title>
<!-- (c) 2016-2017 by Refurio Anachro, this html file belongs to the pulic domain! -->
<script type="text/javascript">
var trace_depth;
var path_length;
var model;
var w, h; /* set by resize() */
var up, right, look_at; /* set by init_cam() */
/* tracer main loop */
function trace(depth, fold_func, path, ray) {
var normal = model.normal;
var intersect = model.intersect;
for(var i = 0; i < depth; ++i) {
var p = intersect(model, ray);
if(!p) /* no intersection */
break;
var n = normal(model, p);
ray = reflect(ray, p, n);
path = fold_func(path, ray);
}
return path;
}
/* colorization */
var color_func = {};
color_func.jb = function(path) {
var k = path.length;
for(var i = 1; i < k; ++i) {
var x = dot(pos(path[i]), [1,0,0]);
if(x > (1-0.01)) {
var q = i/k; q = 1-q;
return [q, q, 0.25 + 0.75*q];
}
}
return [0,0,0.25]
}
color_func.z = function(path) {
var k = path.length;
var min_d = 1;
for(var i = 2; i < k; ++i) {
var d = Math.sqrt(dot(dir(path[i]), dir(path[1])) + dot(pos(path[i]), pos(path[1])));
if(d < min_d)
min_d = d;
}
var t = 1 - (min_d+1)/2;
t = 1-t; t = t*t; t = 1-t;
return [t, t, t];
}
var black = [0, 0, 0];
var white = [1, 1, 1];
color_func.bw = function(path) {
var k = path.length;
var j = 1;
var min_d = 1;
for(var i = 2; i < k; ++i) {
var d = Math.sqrt(dot(dir(path[i]), dir(path[1])) + dot(pos(path[i]), pos(path[1])));
if(d < min_d) {
min_d = d;
j = i;
}
}
return path[j][0] < 0 ? black : white;
}
function to_spheroid(q) {
var p = [ q[0], q[1]/model.a, q[2] ];
p = scale(1/abs(p), p);
return [ p[0], p[1]*model.a, p[2] ];
}
function frac(x) {
return x - Math.floor(x);
}
function trace1(ray) {
return trace(1, function(_, r) {
return r;
}, null, ray);
}
color_func.lyap = function(path) {
var k = path.length;
var eps = 0.000001;
var p = scale(1-eps, pos(path[1]));
var q = dir(path[1]);
var s = 0;
var t = 0;
for(var i = 2; i < k; ++i) {
var r = trace1(make_ray(p, q));
if(!r)
return [1,0,0];
var dp = sub(pos(r), pos(path[i]));
var dq = sub(dir(r), dir(path[i]));
var pm = abs(dp);
var qm = abs(dq);
p = add(pos(path[i]), scale(eps/pm, dp));
q = add(dir(path[i]), scale(eps/qm, dq));
s += Math.log(pm);
t += Math.log(qm);
}
s = frac(s/k);
t = frac(t/k);
return [s*s, s*t, t*t];
}
function color_add(a, b) {
return [ Math.sqrt((a[0]*a[0] + b[0]*b[0])/2),
Math.sqrt((a[1]*a[1] + b[1]*b[1])/2),
Math.sqrt((a[2]*a[2] + b[2]*b[2])/2) ];
}
var colorf = color_func.jb;
function color(path) {
if(path.length < 2)
return [0.4, 0.4, 0.4];
return colorf(path);
}
/* camera */
function init_cam(angle) {
var a = angle*Math.PI/180;
right = [ 1, 0, 0 ];
up = [ 0, Math.cos(a), Math.sin(a) ];
look_at = cross(right, up);
}
function cam(x, y) {
var p = add(scale(2*x/w - 1, right), scale(2*y/h - 1, up));
return [ p[0], p[1], p[2],
look_at[0], look_at[1], look_at[2] ];
}
function cam_reverse(p) {
return [ (dot(p, right) + 1)*w/2, (dot(p, up) + 1)*h/2 ];
}
/* model related computations */
function make_sphere(r) {
return {
r: r,
intersect: sphere_intersect,
normal: sphere_normal,
};
}
var eps = 0.0001; /* ignore shorter rays */
function sphere_intersect(model, ray) {
var l = dir(ray);
var o = pos(ray);
var r = model.r;
/* ray/sphere intersection formula,
see https://en.wikipedia.org/wiki/Line%E2%80%93sphere_intersection
c := the center of the sphere, [0, 0, 0] in our case.
d = [ -(l·(o-c)) +- sqrt((l·(o-c))² - |l|²(|o-c|² - r²)) ] / |l|²
*/
var dotlo = dot(l, o);
var ll = absq(l);
var z = dotlo*dotlo - ll*(absq(o) - r*r);
if(z < 0)
return null;
z = Math.sqrt(z);
var d1 = -dotlo + z;
var d2 = -dotlo - z;
if(d1 > eps && (d1 < d2 || d2 < eps))
return add(o, scale(d1/ll, l));
if(d2 > eps && (d2 < d1 || d1 < eps))
return add(o, scale(d2/ll, l));
return null; /* no forward intersection */
}
function sphere_normal(model, p) {
return p;
}
function make_spheroid(a) {
var b = 1;
var s = (a < 1) ? a/b : b/a;
var f = 1-s; /* flattening */
var e = Math.sqrt(2*f - f*f); /* eccentricity */
var c = e*a; /* half focal distance */
return {
a: a,
r: b,
intersect: spheroid_intersect,
normal: spheroid_normal,
c: c,
}
}
function spheroid_intersect(model, ray) {
var a = model.a;
var r = [ ray[0], ray[1]/a, ray[2],
ray[3], ray[4]/a, ray[5]];
var p = sphere_intersect(model, r);
if(!p)
return null;
return [ p[0], p[1]*a, p[2] ];
}
function spheroid_normal(model, p) {
var a = model.a;
var c = model.c;
var y = p[1];
var r = Math.sqrt(p[0]*p[0] + p[2]*p[2]);
var q = y / (r+c);
var n = [ p[0], r*q, p[2] ];
return scale(1/abs(n), n);
}
/* sampling algorithms */
function scanline(y) {
var r = [];
for(var x = 0; x < w; ++x) {
var path = init_path_recorder(cam(x, y));
r[x] = color(trace(trace_depth, path_recorder, path, path[0]));
}
put_row(0, y, r);
return y+1 < h;
}
/* ray primitives */
function dir(ray) {
return [ray[3], ray[4], ray[5]];
}
function pos(ray) {
return [ray[0], ray[1], ray[2]];
}
function reflect(ray, p, n) {
var d = dir(ray);
var r = add(d, scale(-2 * dot(d, n), n));
return [p[0], p[1], p[2],
r[0], r[1], r[2]];
}
function make_ray(p, q) {
return [ p[0], p[1], p[2],
q[0], q[1], q[2] ];
}
/* path folding monad */
function init_path_recorder(ray) {
return [ray];
}
function path_recorder(path, ray) {
path.push(ray);
return path;
}
/* vector primitives */
function scale(a, p) {
return [
a*p[0],
a*p[1],
a*p[2],
];
}
function dot(p, q) {
return p[0]*q[0] + p[1]*q[1] + p[2]*q[2];
}
function add(p, q) {
return [
p[0] + q[0],
p[1] + q[1],
p[2] + q[2],
];
}
function sub(a, b) {
return add(a, scale(-1, b));
}
function absq(p) {
return dot(p, p);
}
function abs(p) {
return Math.sqrt(dot(p, p));
}
function cross(p, q) {
return [ p[1]*q[2] - p[2]*q[1],
p[2]*q[0] - p[0]*q[2],
p[0]*q[1] - p[1]*q[0] ];
}
function vec2str(p) {
return "("+
p.map(function(n) {
return n.toPrecision(3);
}).join(" ")
+")";
}
/* graphics driver */
var canvas, ctx;
var pixel, pixel_data;
var row, row_data;
function init_graphics(w, h) {
canvas = get("canvas");
ctx = canvas.getContext("2d");
screenshot = null;
canvas.setAttribute("width", w);
canvas.setAttribute("height", h);
row = ctx.createImageData(w, 1);
row_data = row.data;
for(var i = 0; i < w; ++i)
row_data[4*i + 3] = 255; /* alpha: fully opaque */
}
function put_row(x, y, r) {
for(var i = 0; i < w; ++i) {
var j = 4*i;
row_data[j + 0] = 255*r[i][0];
row_data[j + 1] = 255*r[i][1];
row_data[j + 2] = 255*r[i][2];
}
ctx.putImageData(row, x, y);
}
var screenshot = null;
function capture() {
screenshot = ctx.getImageData(0, 0, w, h);
}
function restore() {
ctx.putImageData(screenshot, 0, 0);
}
function draw_path(path) {
restore();
ctx.beginPath();
p = cam_reverse(pos(path[0]));
ctx.moveTo(p[0], p[1]);
var k = path.length;
for(var i = 1; i < k; ++i) {
p = cam_reverse(pos(path[i]));
ctx.lineTo(p[0], p[1]);
}
ctx.strokeStyle = "rgba(255,255,255,0.5)";
ctx.stroke();
}
/* reading and writing url #arguments */
var arg_list = ["angle", "a", "trace_depth", "path_length", "w", "h", "x", "y", "z", "dx", "dy", "dz"];
function split1(q, s) {
var k = s.search(q);
var j = q.length;
if(k < 0)
return [s, "1"];
return [s.substr(0, k), s.substr(k+j, s.length - k - j)];
}
function explode(u) {
if(u == "")
return [];
var l = u.split("&");
var r = {};
for(i in l) {
var a = split1("=", l[i]).map(decodeURIComponent);
r[a[0]] = a[1];
}
return r;
}
function implode(l) {
var r = [];
for(i in l)
r.push(""+ i +"=" + l[i]);
return r.map(encodeURIComponent).join("&");
}
function read_args() {
var args = explode(window.location.hash.substr(1));
var n = 0;
for(i in args)
if(i in arg_list) {
get(i).value = args[i];
++n;
}
return n;
}
function write_args() {
var args = {};
for(i in arg_list)
args[arg_list[i]] = get(arg_list[i]).value;
var s = implode(args);
s = s.split("%3D").join("="); // improve readability
window.location.hash = "#"+ s;
}
document.onchange = write_args;
/* html dom utils */
function get(n) {
return document.getElementById(n);
}
function get_input(n) {
return get(n).value;
}
function set_class(n, c) {
get(n).setAttribute("class", c);
}
function status(s) {
get("status").innerHTML = s;
}
function enable(n) {
set_class(n +"_button", "");
}
function disable(n) {
set_class(n +"_button", "gray");
}
/* iterate a function asynchronously until it refuses */
var timer = null;
var iterations;
function main_loop(sample_func) {
clearTimeout(timer);
timer = setTimeout(function() {
if(sample_func(iterations++))
main_loop(sample_func);
else {
timer = null;
done();
}
}, 10);
}
function abort_main_loop() {
clearTimeout(timer);
timer = false;
}
/* user interface */
function init() {
if(read_args() > 0) {
resize();
start();
}
}
function start() {
status("tracing...");
disable("start");
enable("stop");
w = get_input("w");
h = get_input("h");
init_graphics(w, h);
trace_depth = get_input("trace_depth");
model = make_spheroid(get_input("a"));
init_cam(get_input("angle"));
colorf = color_func[get_input("color")];
iterations = 0;
main_loop(scanline);
}
function stop() {
status("stopped, please restart");
enable("start");
disable("stop");
abort_main_loop();
}
function dirty() {
status("edited, please restart.");
enable("start");
}
function done() {
status("done.");
disable("stop");
capture();
}
function resize() {
init_graphics(get_input("w"), get_input("h")); // for visual feedback
dirty();
}
function path(r0) {
if(typeof r0 == "undefined")
r0 = [ get_input("x"), get_input("y"), get_input("z"),
get_input("dx"), get_input("dy"), get_input("dz") ];
path_length = get_input("path_length");
draw_path(trace(path_length, path_recorder, init_path_recorder(r0), r0));
}
function canvasPos(ev) {
var n = ev.target
return [ ev.x + window.scrollX - n.offsetLeft, ev.y + window.scrollY - n.offsetTop ];
}
var enable_fast_path = 0;
function canvasClick() {
if(timer)
return stop();
if(!screenshot)
return start();
var p = canvasPos(window.event);
var x = p[0], y = p[1];
if(x >= 0 && x < w && y >= 0 && y < h) {
path(cam(x, y));
++enable_fast_path;
}
}
document.onmouseup = function() {
enable_fast_path = 0;
var p = canvasPos(window.event);
var x = p[0], y = p[1];
if(x >= 0 && x < w && y >= 0 && y < h) {
var r0 = cam(x, y);
get("x").value = r0[0];
get("y").value = r0[1];
get("z").value = r0[2];
get("dx").value = r0[3];
get("dy").value = r0[4];
get("dz").value = r0[5];
}
}
document.onmousemove = function() {
if(!enable_fast_path)
return;
canvasClick();
}
</script>
<style>
#canvas {
border: 1px solid black;
}
button {
border: 0px;
background-color: #dadaff;
}
button.gray {
background-color: #ffffff;
}
div {
max-width: 60ex;
}
span {
display: inline-block;
}
hr {
border: 1px solid #dadada;
}
</style>
</head>
<body>
<form action="#">
<button id="start_button" type="button" onClick="start()">redraw</button>
<button id="stop_button" class="gray" type="button" onClick="stop()" >stop</button>
<input id="w" size="5" onChange="resize()" type="number" value="300" min="50" max="5000"> x
<input id="h" size="5" onChange="resize()" type="number" value="300" min="5" max="5000"><br/>
<canvas id="canvas" width="300" height="300" onMouseDown="canvasClick()"></canvas><br/>
<div id="status">THIS PAGE REQUIRES PERMISSION TO RUN JAVASCRIPT CODE</div>
<hr/>
<input id="angle" onChange="dirty()" size="5" type="number" value="30" min="0" max="90" > camera angle<br/>
<input id="a" onChange="dirty()" size="5" type="number" value="1.1" min="0" max="10" > major half-axis a (b is 1)<br/>
<input id="trace_depth" onChange="dirty()" size="5" type="number" value="15" min="1" max="500"> trace depth<br/>
<select id="color">
<option value="jb" >disc on the right</option>
<option value="z" >min dist to 1st ray</option>
<option value="bw" >left / right</option>
<option value="lyap">Lyapunov exponent</option>
</select><br/>
<br/>
<input id="path_length" onChange="path()" size="5" type="number" value="15" min="1" max="500"> path length<br/>
<span>
<input id="x" onChange="path()" size="7" value="0" min="-1" max="1"> x,<br/>
<input id="y" onChange="path()" size="7" value="0" min="-1" max="1"> y,<br/>
<input id="z" onChange="path()" size="7" value="-1" min="-1" max="1"> z,<br/>
</span>
<span>
<input id="dx" onChange="path()" size="7" value="0" min="-1" max="1"> dx<br/>
<input id="dy" onChange="path()" size="7" value="0" min="-1" max="1"> dy<br/>
<input id="dz" onChange="path()" size="7" value="1" min="-1" max="1"> dz<br/>
</span>
</form>
<hr/>
<div>
You see a raytraced image of a mirrored spheroid. You can save this html file to your disk and view it from there, or edit to change the coloring in really creative ways! <br>
<br>
Read my reflections on spheroids on google+:</div>
<br>
<div><a href="https://plus.google.com/+RefurioAnachro/posts/L6C4Kob2bNe">Ellipsoid: Glossary and coordinate system</a></div>
<div><a href="https://plus.google.com/+RefurioAnachro/posts/Vmrx7GMRe2i">billiards in ellipses, Birkhoff, Poincare, and Poncelet</a></div>
<div><a href="https://plus.google.com/+RefurioAnachro/posts/Q2nDr5phZfQ">The physical ellipse</a></div>
<div><a href="https://plus.google.com/+RefurioAnachro/posts/KbcDG6zbSEw">more thoughts and images</a></div>
<div><a href="https://plus.google.com/+RefurioAnachro/posts/QPdSyPHFYXa">Anatomy of the ellipse</a></div>
<br>
I´ve posted pictures and announcements on g+ here:
<br>
<div><a href="https://plus.google.com/+RefurioAnachro/posts/1HfQkJX9gpV">announcement for this release</a></div>
<div><a href="https://plus.google.com/+RefurioAnachro/posts/WGsyCKFo3K4">1.1 #spheroid Lyapunov</a></div>
<div><a href="https://plus.google.com/+RefurioAnachro/posts/VsxYKz3k78m">Some more pictures</a></div>
<div><a href="https://plus.google.com/+RefurioAnachro/posts/DaeehWHJJkB">js raytracer for spheroids (first release)</a></div>
<br>
See also:</div>
<br>
<div>wikipedia on <a href="https://en.wikipedia.org/wiki/Spheroid">spheroids</a></div>
<br>
<div>Many thanks to the people who have been participating in the fun so far, especially to <a href="https://plus.google.com/108446499540248742090">+Bruce Elliott</a>, <a href="https://plus.google.com/114187364719055671781">+John Valentine</a>, and <a href="https://plus.google.com/117663015413546257905">+John Baez</a>, who has kindly put this topic on one of his idea magnets, his blog <i>Visual Insight</i>: <a href="http://blogs.ams.org/visualinsight/2015/04/15/sphere-in-mirrored-spheroid/">Sphere in Mirrored Spheroid</a> </div>
<hr/>
<div>this html file, and all pictures generated with it belong to the pulic domain.</div>
by <a href="https://plus.google.com/+RefurioAnachro/">Refurio Anachro</a>
<script type="text/javascript">
status("press redraw or click into frame");
init();
</script>
</body>
</html>