-
Notifications
You must be signed in to change notification settings - Fork 1
/
dfs_recipes.html
876 lines (780 loc) · 36.6 KB
/
dfs_recipes.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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
<!DOCTYPE html>
<!--
mobius-transforms - Exploring Möbius transformations and implementing the book Indra's Pearls
Copyright (C) 2022 Tim J. Hutton
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
-->
<html>
<head>
<meta charset="UTF-8">
<script src="code/math.js"></script>
<script src="code/recipes.js"></script>
<script src="code/threejs/three.min.js"></script>
<script src="code/threejs/DragControls.js"></script>
<script src="code/threejs/FontLoader.js"></script>
<script src="code/threejs/helvetica.js"></script>
<script src="code/threejs/OrbitControls.js"></script>
<script>
function init() {
computing = false;
need_recompute = false;
recipes = get_recipes();
var recipes_dropdown = document.getElementById('recipes_dropdown');
for( var i = 0; i < recipes.length; i++ ) {
var el = document.createElement("option");
el.textContent = recipes[i].label;
el.value = recipes[i].label;
recipes_dropdown.appendChild(el);
}
readURLParams();
draw_control_points = true;
plot_solution = [ true, false ];
document.getElementById("which_solution_dropdown").selectedIndex = 0;
plot_ifs = false;
plot_dfs = true;
hq_mode = false;
set_quality_parameters( hq_mode );
const loader = new THREE.FontLoader();
loader.load( helvetiker_regular_typeface_json, (loaded_font) => {
font = loaded_font;
init3js();
compute_and_redraw();
});
}
function setURLParams() {
var searchParams = new URLSearchParams();
searchParams.set( 'id', recipes[ iRecipe ].id );
for(var i = 0; i < control_points.length; i++) {
searchParams.set( recipes[ iRecipe ].pt_labels[ i ] + '.x', control_points[i].x.toFixed(6) );
searchParams.set( recipes[ iRecipe ].pt_labels[ i ] + '.y', control_points[i].y.toFixed(6) );
}
const url = window.location.pathname + '?' + searchParams.toString();
debounce( () => history.pushState(null, '', url), 1000 );
return url;
}
var debounce_timer;
function debounce(func, delay) {
clearTimeout( debounce_timer )
debounce_timer = setTimeout( func, delay )
}
function readURLParams() {
var recipes_dropdown = document.getElementById('recipes_dropdown');
var searchParams = new URLSearchParams(window.location.search);
const id = searchParams.get('id');
if( id === null ) {
// no pattern specification found in URL, use defaults
iRecipe = 8;
recipes_dropdown.selectedIndex = iRecipe;
control_points = [];
for(var i = 0; i < recipes[ iRecipe ].control_points.length; i++) {
control_points.push( p2( recipes[ iRecipe ].control_points[i].x, recipes[ iRecipe ].control_points[i].y ) );
}
}
else {
for(var i = 0; i < recipes.length; i++) {
const recipe = recipes[ i ];
if( recipe.id === id ) {
iRecipe = i;
recipes_dropdown.selectedIndex = iRecipe;
control_points = [];
for(var j = 0; j < recipes[ iRecipe ].control_points.length; j++) {
control_points.push( p2( recipes[ iRecipe ].control_points[j].x, recipes[ iRecipe ].control_points[j].y ) );
}
if( typeof(recipe.pt_labels) !== "undefined") {
for( var j = 0; j < recipe.pt_labels.length; j++) {
const xval = searchParams.get( recipe.pt_labels[ j ] + ".x" );
if( xval !== null ) control_points[j].x = parseFloat( xval );
const yval = searchParams.get( recipe.pt_labels[ j ] + ".y" );
if( yval !== null ) control_points[j].y = parseFloat( yval );
}
}
break;
}
}
}
document.getElementById('which_solution_dropdown').disabled = ( recipes[ iRecipe ].num_solutions == 1 );
document.getElementById('transform0_slider').value = 0;
document.getElementById('transform1_slider').value = 0;
}
function init3js() {
const canvas = document.getElementById('canvas');
const white = new THREE.Color( 'rgb(255,255,255)' );
const green = new THREE.Color( 'rgb(0,255,0)' );
const grey = new THREE.Color( 'rgb(100,100,100)' );
const dark_green = new THREE.Color( 'rgb(0,100,0)' );
const black = new THREE.Color( 'rgb(0,0,0)' );
const yellow = new THREE.Color( 'rgb(255,255,0)' );
const orange = new THREE.Color( 'rgb(255,165,0)' );
scene = new THREE.Scene();
scene.background = black;
line_segments = [[],[]]; // [which_solution]
const line_materials = [ new THREE.LineBasicMaterial({ color: grey }), new THREE.LineBasicMaterial({ color: dark_green }) ];
line_geometry = [ new THREE.BufferGeometry(), new THREE.BufferGeometry() ];
line_meshes = [];
for( var i = 0; i < 2; i++ ) {
line_geometry[i].setAttribute( 'position', new THREE.Float32BufferAttribute( line_segments[i], 3 ) );
line_meshes[i] = new THREE.LineSegments( line_geometry[i], line_materials[i] );
scene.add( line_meshes[i] );
}
points = [[],[]]; // [which_solution]
const point_materials = [ new THREE.PointsMaterial({ color: grey }), new THREE.PointsMaterial({ color: dark_green }) ];
point_geometry = [ new THREE.BufferGeometry(), new THREE.BufferGeometry() ];
points_meshes = [];
for( var i = 0; i < 2; i++ ) {
point_geometry[i].setAttribute( 'position', new THREE.Float32BufferAttribute( points[i], 3 ) );
points_meshes[i] = new THREE.Points( point_geometry[i], point_materials[i] );
scene.add( points_meshes[i] );
}
// transformed pattern
transformed_line_segments = [[],[]];
const transformed_line_materials = [ new THREE.LineBasicMaterial({ color: white }), new THREE.LineBasicMaterial({ color: green }) ];
transformed_line = [ new THREE.BufferGeometry(), new THREE.BufferGeometry() ];
for( var i = 0; i < 2; i++ ) {
transformed_line[i].setAttribute( 'position', new THREE.Float32BufferAttribute( transformed_line_segments[i], 3 ) );
const transformed_line_mesh = new THREE.LineSegments( transformed_line[i], transformed_line_materials[i] );
scene.add( transformed_line_mesh );
}
// transformed points
transformed_vertices = [[],[]];
const transformed_point_materials = [ new THREE.PointsMaterial({ color: white }), new THREE.PointsMaterial({ color: green }) ];
transformed_points = [ new THREE.BufferGeometry(), new THREE.BufferGeometry() ];
for( var i = 0; i < 2; i++ ) {
transformed_points[i].setAttribute( 'position', new THREE.Float32BufferAttribute( transformed_vertices[i], 3 ) );
const transformed_points_mesh = new THREE.Points( transformed_points[i], transformed_point_materials[i] );
scene.add( transformed_points_mesh );
}
// show where mouse location gets transformed to
mouse_line_segments = [[],[]]; // [which_solution]
const mouse_line_materials = [ new THREE.LineBasicMaterial({ color: yellow }), new THREE.LineBasicMaterial({ color: orange }) ];
mouse_line_geometry = [ new THREE.BufferGeometry(), new THREE.BufferGeometry() ];
mouse_line_meshes = [];
for( var i = 0; i < 2; i++ ) {
mouse_line_geometry[i].setAttribute( 'position', new THREE.Float32BufferAttribute( mouse_line_segments[i], 3 ) );
mouse_line_meshes[i] = new THREE.LineSegments( mouse_line_geometry[i], mouse_line_materials[i] );
scene.add( mouse_line_meshes[i] );
}
mouse_pos = new THREE.Vector3();
// add axes
axes = new THREE.GridHelper( 100, 100, 0x444444, 0x222222 );
axes.geometry.rotateX( Math.PI / 2 );
axes.renderOrder = -1;
scene.add( axes );
control_point_meshes = [];
camera = new THREE.OrthographicCamera( -3, 3, 3, -3 );
camera.position.set(0, 0, 1);
camera.up.set(0, 1, 0);
camera.lookAt(0, 0, 0);
scene.add( camera );
renderer = new THREE.WebGLRenderer({ antialias: true, canvas: canvas });
orbit_controls = new THREE.OrbitControls( camera, renderer.domElement );
orbit_controls.enableRotate = false;
orbit_controls.mouseButtons.LEFT = THREE.MOUSE.PAN;
orbit_controls.touches.ONE = THREE.TOUCH.PAN;
orbit_controls.addEventListener( 'change', onCameraChange, false );
control_point_meshes = [];
resetControlPointMeshes( scene );
drag_controls = new THREE.DragControls( control_point_meshes, camera, renderer.domElement );
drag_controls.addEventListener( 'dragstart', () => orbit_controls.enabled = false );
drag_controls.addEventListener( 'dragend', () => orbit_controls.enabled = true );
drag_controls.addEventListener( 'drag', onDragControlPoint, false );
canvas.addEventListener( 'mousemove', onMouseMove, false );
canvas.addEventListener( 'touchmove', render, false );
canvas.addEventListener( 'mousedown', render, false );
canvas.addEventListener( 'touchstart', render, false );
canvas.addEventListener( 'mouseup', render, false );
canvas.addEventListener( 'mouseout', render, false );
canvas.addEventListener( 'touchend', render, false );
canvas.addEventListener( 'touchcancel', render, false );
canvas.addEventListener( 'wheel', render, false );
}
function onMouseMove( e ) {
const { left, top, width, height } = e.currentTarget.getBoundingClientRect();
mouse_pos.set(
((e.clientX - left) / width) * 2 - 1,
-((e.clientY - top) / height) * 2 + 1,
(camera.near + camera.far) / (camera.near - camera.far),
);
mouse_pos.unproject(camera);
// show where the mouse location gets transformed to
var top_left = new THREE.Vector3();
top_left.set(-1, 1, (camera.near + camera.far) / (camera.near - camera.far) );
top_left.unproject(camera);
var bottom_right = new THREE.Vector3();
bottom_right.set(1, -1, (camera.near + camera.far) / (camera.near - camera.far) );
bottom_right.unproject(camera);
var pixel_scale = 20.0; // we don't actually have pixels here so this is for illustration
var pixel_width = pixel_scale * (bottom_right.x - top_left.x) / width;
var pixel_height = pixel_scale * (top_left.y - bottom_right.y) / height;
mouse_line_segments = [[],[]];
// define a square around the current mouse location
var quad = [];
var num_quad_points = 10;
var p = new THREE.Vector3(mouse_pos.x - pixel_width / 2.0, mouse_pos.y + pixel_width / 2.0, 0); // start at top-left
quad.push(p);
for(var i=0; i < num_quad_points; i++) { p.x += pixel_width / num_quad_points; quad.push( new THREE.Vector3(p.x, p.y, 0)); } // move right
for(var i=0; i < num_quad_points; i++) { p.y -= pixel_width / num_quad_points; quad.push( new THREE.Vector3(p.x, p.y, 0)); } // move down
for(var i=0; i < num_quad_points; i++) { p.x -= pixel_width / num_quad_points; quad.push( new THREE.Vector3(p.x, p.y, 0)); } // move left
for(var i=0; i < num_quad_points; i++) { p.y += pixel_width / num_quad_points; quad.push( new THREE.Vector3(p.x, p.y, 0)); } // move up
// then for each transform:
for( var which_solution = 0; which_solution < recipes[ iRecipe ].num_solutions; which_solution++ ) {
if(! plot_solution[which_solution] ) { continue; }
// draw the square around the current pixel
for(var i=0; i < quad.length-1; i++) {
mouse_line_segments[which_solution].push(quad[i].x);
mouse_line_segments[which_solution].push(quad[i].y);
mouse_line_segments[which_solution].push(0);
mouse_line_segments[which_solution].push(quad[i+1].x);
mouse_line_segments[which_solution].push(quad[i+1].y);
mouse_line_segments[which_solution].push(0);
}
for( var transform_idx = 0; transform_idx < transforms[which_solution].length; transform_idx++ ) {
var m = transforms[which_solution][transform_idx];
// draw a line from the center of this pixel
var transformed_mouse_pos = mobius_on_point(m, mouse_pos);
mouse_line_segments[which_solution].push(mouse_pos.x);
mouse_line_segments[which_solution].push(mouse_pos.y);
mouse_line_segments[which_solution].push(0);
mouse_line_segments[which_solution].push(transformed_mouse_pos.x);
mouse_line_segments[which_solution].push(transformed_mouse_pos.y);
mouse_line_segments[which_solution].push(0);
// draw a box around the edges of the transformed pixel
for(var i=0; i < quad.length-1; i++) {
var transformed_p = mobius_on_point(m, quad[i]);
mouse_line_segments[which_solution].push(transformed_p.x);
mouse_line_segments[which_solution].push(transformed_p.y);
mouse_line_segments[which_solution].push(0);
var transformed_p = mobius_on_point(m, quad[i+1]);
mouse_line_segments[which_solution].push(transformed_p.x);
mouse_line_segments[which_solution].push(transformed_p.y);
mouse_line_segments[which_solution].push(0);
}
}
}
updateScene();
}
function onDragControlPoint( e ) {
const mesh = e.object;
const iControlPoint = control_point_meshes.indexOf( mesh );
if( iControlPoint == -1 ) { console.log('unknown dragged object'); return; }
control_points[ iControlPoint ].x = mesh.position.x;
control_points[ iControlPoint ].y = mesh.position.y;
if( computing ) { need_recompute = true; }
else { compute_and_redraw(); }
}
function onCameraChange() {
// set size of the control_points depending on the zoom
const scale = 1.0 / camera.zoom;
for(var i = 0; i < control_point_meshes.length; i++) {
control_point_meshes[i].scale.x = scale;
control_point_meshes[i].scale.y = scale;
control_point_meshes[i].scale.z = scale;
}
}
function resetCamera() {
orbit_controls.reset();
}
function resetControlPointMeshes( scene ) {
for( var i = 0; i < control_point_meshes.length; i++) {
scene.remove( control_point_meshes[i] );
}
control_point_meshes.length = 0;
const marker_size = 0.05;
const marker_geometry = new THREE.BoxGeometry( marker_size, marker_size, marker_size );
const marker_material = new THREE.MeshBasicMaterial( { color: 0xffffff } );
const handle_size = 0.5;
const handle_geometry = new THREE.BoxGeometry( handle_size, handle_size, handle_size );
const handle_material = new THREE.MeshBasicMaterial( { color: 0xffffff, transparent:true, opacity: 0.1 } );
for( var i = 0; i < control_points.length; i++) {
var handle_mesh = new THREE.Mesh( handle_geometry, handle_material );
handle_mesh.position.set( control_points[i].x, control_points[i].y, 0.0 );
var marker_mesh = new THREE.Mesh( marker_geometry, marker_material );
marker_mesh.position.set( control_points[i].x, control_points[i].y, 0.0 );
handle_mesh.attach( marker_mesh );
{
const material = new THREE.MeshBasicMaterial({color: 'rgb(255,255,255)'});
const shapes = font.generateShapes( recipes[ iRecipe ].pt_labels[ i ], 0.2 );
const geometry = new THREE.ShapeBufferGeometry( shapes );
const text = new THREE.Mesh( geometry, material );
text.position.x = control_points[i].x + 0.1;
text.position.y = control_points[i].y + 0.1;
text.position.z = 0.0;
handle_mesh.attach( text );
}
scene.add( handle_mesh );
control_point_meshes.push( handle_mesh );
}
}
function render() {
renderer.render( scene, camera );
}
function updateScene() {
for( var i = 0; i < 2; i++ ) {
line_geometry[i].setAttribute( 'position', new THREE.Float32BufferAttribute( line_segments[i], 3 ) );
point_geometry[i].setAttribute( 'position', new THREE.Float32BufferAttribute( points[i], 3 ) );
mouse_line_geometry[i].setAttribute( 'position', new THREE.Float32BufferAttribute( mouse_line_segments[i], 3 ) );
line_geometry[i].computeBoundingSphere()
point_geometry[i].computeBoundingSphere()
mouse_line_geometry[i].computeBoundingSphere()
}
render();
}
function onDrawControlPointsButton(element) {
draw_control_points = !draw_control_points;
element.value = draw_control_points ? "Draw control points: ON" : "Draw control points: OFF";
for( var i = 0; i < control_point_meshes.length; i++ ) {
control_point_meshes[i].visible = draw_control_points;
}
line_meshes[0].visible = draw_control_points;
line_meshes[1].visible = draw_control_points;
points_meshes[0].visible = draw_control_points;
points_meshes[1].visible = draw_control_points;
mouse_line_meshes[0].visible = draw_control_points;
mouse_line_meshes[1].visible = draw_control_points;
if( draw_control_points ) { drag_controls.activate(); } else { drag_controls.deactivate(); }
axes.visible = draw_control_points;
render();
}
function onRecipeChanged(element) {
iRecipe = recipes_dropdown.selectedIndex;
control_points = [];
for(var i = 0; i < recipes[ iRecipe ].control_points.length; i++) {
control_points.push( p2( recipes[ iRecipe ].control_points[i].x, recipes[ iRecipe ].control_points[i].y ) );
}
document.getElementById('which_solution_dropdown').disabled = ( recipes[ iRecipe ].num_solutions == 1 );
plot_solution = [ true, false ];
document.getElementById("which_solution_dropdown").selectedIndex = 0;
document.getElementById('transform0_slider').value = 0;
document.getElementById('transform1_slider').value = 0;
hq_mode = false;
set_quality_parameters( hq_mode );
resetControlPointMeshes( scene );
resetCamera();
compute_and_redraw();
}
function set_quality_parameters(hq_mode) {
if( hq_mode ) {
num_ifs_iterations = 10000000;
max_depth = 100;
closeness_epsilon = 0.003; // if sequential points are closer than this we can stop searching
}
else {
num_ifs_iterations = 100000;
max_depth = 25;
closeness_epsilon = 0.01; // if sequential points are closer than this we can stop searching
}
document.getElementById('epsilon_input').value = closeness_epsilon;
document.getElementById('maximum_depth_input').value = max_depth;
closeness_epsilon2 = closeness_epsilon * closeness_epsilon;
}
function onMaxDepthChanged(element) {
max_depth = element.value;
compute_and_redraw();
}
function onEpsilonChanged(element) {
closeness_epsilon = element.value;
const min_closeness_epsilon = 0.00001;
if( closeness_epsilon < min_closeness_epsilon ) {
closeness_epsilon = min_closeness_epsilon;
element.value = closeness_epsilon;
}
closeness_epsilon2 = closeness_epsilon * closeness_epsilon;
compute_and_redraw();
}
function onWhichSolutionChanged(element) {
plot_solution[0] = element.selectedIndex == 0 || element.selectedIndex == 2;
plot_solution[1] = element.selectedIndex == 1 || element.selectedIndex == 2;
compute_and_redraw();
}
function onSaveAsPNGButton(element) {
takeScreenshot( 2048, 2048 );
}
function dataURIToBlob( dataURI ) {
const binStr = window.atob( dataURI.split( ',' )[1] );
const len = binStr.length;
const arr = new Uint8Array( len );
for ( let i = 0; i < len; i++ ) {
arr[i] = binStr.charCodeAt( i );
}
return new window.Blob( [arr] );
}
function saveDataURI( name, dataURI ) {
const blob = dataURIToBlob( dataURI );
// force download
const link = document.createElement( 'a' );
link.download = name;
link.href = window.URL.createObjectURL( blob );
link.onclick = () => {
window.setTimeout( () => {
window.URL.revokeObjectURL( blob );
link.removeAttribute( 'href' );
}, 500 );
};
link.click();
}
function defaultFileName (ext) {
const str = `dfs_${new Date().toISOString()}${ext}`;
return str.replace(/:/g, '').replace(/T/g, '_').replace(/Z/g, '');
}
function takeScreenshot( width, height ) {
const old_width = canvas.width;
const old_height = canvas.height;
// set camera and renderer to desired screenshot dimension
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
renderer.render( scene, camera, null, false );
const DataURI = renderer.domElement.toDataURL( 'image/png' );
saveDataURI(defaultFileName( '.png' ), DataURI);
// reset to old dimensions
camera.aspect = old_width / old_height;
camera.updateProjectionMatrix();
renderer.setSize( old_width, old_height );
renderer.render( scene, camera, null, false );
}
function compute_and_redraw() {
computing = true;
need_recompute = false;
document.getElementById('rendering_text').textContent = "Computing...";
setTimeout(function() {
const time_start = performance.now();
var [n_pts_plotted, description] = compute();
updateScene();
computeTransformedObjects();
render();
const time_end = performance.now();
const ms_elapsed = time_end - time_start;
if( ms_elapsed > 1000.0 ) {
document.getElementById('rendering_text').textContent = "Last compute took: "+(ms_elapsed/1000.0).toFixed(2)+"s";
}
else {
document.getElementById('rendering_text').textContent = "Last compute took: "+ms_elapsed.toFixed(2)+"ms";
}
document.getElementById('num_points_text').textContent = "Number of points: " + n_pts_plotted;
computing = false;
if( need_recompute ) {
compute_and_redraw();
}
else {
// no more events on the queue
const url = setURLParams();
document.getElementById('description_text').innerHTML = "Permalink: <a href=\"" + url + "\">"
+ description + "</a>";
}
}, 1 );
}
function format_complex( a ) {
return `${ a.x.toFixed(6) } ${ a.y >= 0.0 ? "+" : "-" } ${ Math.abs(a.y).toFixed(6) }i`;
}
function make_generators( which_solution ) {
// construct the four Mobius transformations we will be using
var [transforms, description] = recipes[ iRecipe ].make_generators(which_solution, control_points);
transforms[2] = get_mobius_inverse( transforms[0] );
transforms[3] = get_mobius_inverse( transforms[1] );
return [transforms, description];
}
function duplicate_point( a, recipe ) {
// return a list of the duplicated points according to the duplication method specified in the recipe
var pts = [];
if( !('duplicate' in recipe) ) {
pts.push( a );
}
else if( recipe.duplicate == 'rot180' ) {
pts.push( a );
pts.push( mul(a, -1.0) );
}
else if( recipe.duplicate == 'rot180_and_rep_x_2' ) { // e.g. riley
for( var x_offset = -4; x_offset <= 4; x_offset += 2 ) {
pts.push( add( a, p2( x_offset, 0.0 ) ) );
pts.push( add( mul(a, -1.0), p2( x_offset, 0.0 ) ) );
}
}
else if( recipe.duplicate == 'maskit' ) {
var mu = control_points[0];
for( var x_offset = -4; x_offset <= 4; x_offset += 2 ) {
pts.push( add( a, p2( x_offset, 0.0 ) ) );
pts.push( add( mul( sub( a, mu ), -1.0), p2( x_offset, 0.0 ) ) );
}
}
else {
//console.log('Unknown duplication command:', recipe.duplicate);
pts.push( a );
}
return pts;
}
function mod4( x ) {
while( x < 0 ) { x += 4; }
return x % 4;
}
function last(arr) { return arr[arr.length - 1]; }
function get_repetends( transforms ) {
var [ a, b, A, B ] = transforms;
var gens = [ {t:a, name:'a'}, {t:b, name:'b'}, {t:A, name:'A'}, {t:B, name:'B'} ];
var bABa = get_mobius_composed(b, A, B, a);
var BAba = get_mobius_composed(B, A, b, a);
var ABab = get_mobius_composed(A, B, a, b);
var aBAb = get_mobius_composed(a, B, A, b);
var BabA = get_mobius_composed(B, a, b, A);
var baBA = get_mobius_composed(b, a, B, A);
var abAB = get_mobius_composed(a, b, A, B);
var AbaB = get_mobius_composed(A, b, a, B);
var aaB = get_mobius_composed(a, a, B);
var aBa = get_mobius_composed(a, B, a);
var Baa = get_mobius_composed(B, a, a);
var AAb = get_mobius_composed(A, A, b);
var AbA = get_mobius_composed(A, b, A);
var bAA = get_mobius_composed(b, A, A);
var repetends = [ [ // each in alphabetical order per abAB with the next letter from [1,0,-1] mod 4, ie:
// after a we get: b,a,B
// after B we get: a,B,A
// after A we get: B,A,b
// after b we get: A,b,a
{ p: get_mobius_fixed_points( bABa )[0], label: '|bABa' },
{ p: get_mobius_fixed_points( a )[0], label: '|a' },
//{ p: get_mobius_fixed_points( aBa )[0], label: '|aBa' },
//{ p: get_mobius_fixed_points( Baa )[0], label: '|Baa' },
{ p: get_mobius_fixed_points( BAba )[0], label: '|BAba' },
], [
{ p: get_mobius_fixed_points( ABab )[0], label: '|ABab' },
{ p: get_mobius_fixed_points( b )[0], label: '|b' },
//{ p: get_mobius_fixed_points( AAb )[0], label: '|AAb' },
{ p: get_mobius_fixed_points( aBAb )[0], label: '|aBAb' },
], [
{ p: get_mobius_fixed_points( BabA )[0], label: '|BabA' },
{ p: get_mobius_fixed_points( A )[0], label: '|A' },
//{ p: get_mobius_fixed_points( AbA )[0], label: '|AbA' },
//{ p: get_mobius_fixed_points( bAA )[0], label: '|bAA' },
{ p: get_mobius_fixed_points( baBA )[0], label: '|baBA' },
], [
{ p: get_mobius_fixed_points( abAB )[0], label: '|abAB' },
{ p: get_mobius_fixed_points( B )[0], label: '|B' },
//{ p: get_mobius_fixed_points( aaB )[0], label: '|aaB' },
{ p: get_mobius_fixed_points( AbaB )[0], label: '|AbaB' },
]
];
return [gens, repetends];
}
function dfs_recursive_tree(gens, repetends, which_solution) {
const max_d2 = 1.0;
var duplication_func = p => duplicate_point( p, recipes[ iRecipe ] );
function explore_tree( x, prev, level ) {
var n_pts = 0;
for( var k = prev + 1; k >= prev - 1; k--) {
const iTag = mod4( k );
const y = get_mobius_composed( x, gens[ iTag ].t );
var z = [];
var close_enough = true;
for(var i = 0; i < repetends[iTag].length; i++) {
z.push( mobius_on_point( y, repetends[iTag][i].p ) );
if( i > 0 ) {
const d2 = dist2( z[i], z[i-1] );
if( d2 > closeness_epsilon2 ) { close_enough = false; }
if( d2 > max_d2 && level >= max_depth ) {
// if there are still very long lines at max_depth then we need to abort
return -1;
}
}
}
if( close_enough || level >= max_depth ) {
if( close_enough ) {
// store the line segments
var pts_dup = [];
for( var j = 0; j < z.length; j++ ) {
pts_dup.push( duplication_func( z[j] ) );
}
for( var j = 0; j < pts_dup[0].length; j++ ) {
for( var i = 0; i < pts_dup.length - 1; i++ ) {
line_segments[which_solution].push( pts_dup[i][j].x );
line_segments[which_solution].push( pts_dup[i][j].y );
line_segments[which_solution].push( 0 );
line_segments[which_solution].push( pts_dup[i+1][j].x );
line_segments[which_solution].push( pts_dup[i+1][j].y );
line_segments[which_solution].push( 0 );
}
}
}
else {
// store the points
for( var j = 0; j < z.length; j++ ) {
const pt_dup = duplication_func( z[j] );
for(var i = 0; i < pt_dup.length; i++) {
points[which_solution].push( pt_dup[i].x );
points[which_solution].push( pt_dup[i].y );
points[which_solution].push( 0.0 );
}
}
}
n_pts += z.length;
}
else {
const ret = explore_tree( y, iTag, level + 1 );
if( ret == -1) { return -1; } // the abort signal bubbles up the stack
n_pts += ret;
}
}
return n_pts;
};
var n_pts_plotted = 0;
var start_letters = [0, 3, 2, 1]; // default order: aBAb
if( 'start_letters' in recipes[iRecipe] ) { start_letters = recipes[iRecipe].start_letters; }
for( var iStartLetter = 0; iStartLetter < start_letters.length; iStartLetter++) {
const iTag = start_letters[iStartLetter];
const ret = explore_tree( gens[iTag].t, iTag, 1 );
if( ret == -1 ) {
return 0; // drawing was aborted
}
n_pts_plotted += ret;
}
return n_pts_plotted;
}
function draw_ifs(transforms, which_solution) {
var discard = 20;
var p = p2(0.0,0.0);
for( var i = 0; i < num_ifs_iterations + discard; i++) {
p = mobius_on_point( transforms[Math.floor(Math.random() * transforms.length)], p );
if( i > discard ) {
points[which_solution].push( p.x );
points[which_solution].push( p.y );
points[which_solution].push( 0.0 );
}
}
return num_ifs_iterations;
}
function compute() {
var total_pts_plotted = 0;
points = [[],[]];
line_segments = [[],[]];
transforms = [[],[]];
var description = "";
var dfs_succeeded = [ true, true ];
// make generators for both solutions
for( var which_solution = 0; which_solution < recipes[ iRecipe ].num_solutions; which_solution++ ) {
[transforms[which_solution], description] = make_generators( which_solution );
}
// run DFS
if( plot_dfs ) {
for( var which_solution = 0; which_solution < recipes[ iRecipe ].num_solutions; which_solution++ ) {
if(! plot_solution[which_solution] ) { continue; }
var [gens, repetends] = get_repetends( transforms[which_solution] );
const num_points_plotted = dfs_recursive_tree( gens, repetends, which_solution );
if( num_points_plotted == 0 ) {
// DFS rendering failed, clear the drawing and draw IFS instead
dfs_succeeded[which_solution] = false;
points[which_solution] = [];
line_segments[which_solution] = [];
}
total_pts_plotted += num_points_plotted;
}
}
// run IFS
for( var which_solution = 0; which_solution < recipes[ iRecipe ].num_solutions; which_solution++ ) {
if( plot_solution[which_solution] && ( !dfs_succeeded[which_solution] || plot_ifs ) ) {
total_pts_plotted += draw_ifs( transforms[which_solution], which_solution );
}
}
return [total_pts_plotted, description];
}
function onTransformSliderChanged() {
computeTransformedObjects();
render();
}
function computeTransformedObjects() {
const k0 = document.getElementById('transform0_slider').value / 100.0;
const k1 = document.getElementById('transform1_slider').value / 100.0;
for( var which_solution = 0; which_solution < 2; which_solution++ ) {
transformed_line_segments[which_solution] = [];
transformed_vertices[which_solution] = [];
if( transforms[which_solution].length > 0) {
const m = get_mobius_composed(mobius_power( transforms[which_solution][0], k0 ), mobius_power( transforms[which_solution][1], k1 ) );
for( var i = 0; i < line_segments[which_solution].length; i += 3 ) {
const p = p2( line_segments[which_solution][i], line_segments[which_solution][i+1] );
const transformed_p = mobius_on_point( m, p );
transformed_line_segments[which_solution].push( transformed_p.x );
transformed_line_segments[which_solution].push( transformed_p.y );
transformed_line_segments[which_solution].push( 0.0 );
}
for( var i = 0; i < points[which_solution].length; i += 3 ) {
const p = p2( points[which_solution][i], points[which_solution][i+1] );
const transformed_p = mobius_on_point( m, p );
transformed_vertices[which_solution].push( transformed_p.x );
transformed_vertices[which_solution].push( transformed_p.y );
transformed_vertices[which_solution].push( 0.0 );
}
}
transformed_line[which_solution].setAttribute( 'position', new THREE.Float32BufferAttribute( transformed_line_segments[which_solution], 3 ) );
transformed_line[which_solution].computeBoundingSphere();
transformed_points[which_solution].setAttribute( 'position', new THREE.Float32BufferAttribute( transformed_vertices[which_solution], 3 ) );
transformed_points[which_solution].computeBoundingSphere();
}
render();
}
window.onload = init;
</script>
<noscript>
<p>For full functionality of this site it is necessary to enable JavaScript.
Here are the <a href="http://www.enable-javascript.com/" target="_blank">
instructions how to enable JavaScript in your web browser</a>.
</p></noscript>
<style>
input, select, label, p {
font-size: 20px;
font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
padding-right: 5px;
}
div.labelSlider {
display: table-cell;
vertical-align: middle;
height: 20px;
}
.lblFloat
{
float:left;
}
</style>
</head>
<body>
<table border="0">
<tr>
<td><canvas id="canvas" width="600" height="600">(Canvas drawing not supported by your browser.)</canvas></td>
<td style="text-align:left;vertical-align:top;padding-left:20px;padding-top:0px">
<p>Use touch/mouse to drag the control points and to pan/zoom the view.</p>
<p><label for="recipes_dropdown">Choose a recipe:</label><br><select id="recipes_dropdown" onchange="onRecipeChanged(this)"></select></p>
<p>
<label for="maximum_depth_input">Max search depth:</label>
<input type="number" id="maximum_depth_input" min="1" max="500" value="25" size="4" onchange="onMaxDepthChanged(this)">
</p>
<p><label for="epsilon_input">Epsilon:</label><input type="number" id="epsilon_input" min="0" max="10" value="0.01" step="0.001" size="10" onchange="onEpsilonChanged(this)"></p>
<p><label for="which_solution_dropdown">Which solution to plot:</label><select id="which_solution_dropdown" onchange="onWhichSolutionChanged(this)">
<option>+ve</option>
<option>-ve</option>
<option>both</option>
</select></p>
<p id="num_points_text"></p>
<p id="rendering_text"></p>
<p><input type="button" value="Draw control points: ON" onclick="onDrawControlPointsButton(this)" /></p>
<label for="transform_sliders">Apply transforms:</label>
<div class="labelSlider" id="transform_sliders">
<label for="transform0_slider" class="lblFloat">1:</label>
<input type="range" min="0" max="100" value="0" id="transform0_slider" oninput="onTransformSliderChanged()">
<br>
<label for="transform1_slider" class="lblFloat">2:</label>
<input type="range" min="0" max="100" value="0" id="transform1_slider" oninput="onTransformSliderChanged()">
</div>
<p><input type="button" id="download_png_button" value="Save as PNG" onclick="onSaveAsPNGButton(this)" /></p>
</td></tr></table>
<p id="description_text"></p>
<p>The image shows the limit set of a <a href="https://en.wikipedia.org/wiki/Kleinian_group">Kleinian group</a> of two <a href="index.html">Möbius transformations</a> and their inverses.</p>
<p>The lines are drawn by a search tree defined by the choice of which of the four transforms to apply next. The search continues until the maximum depth is reached or the distance between successive points is less than epsilon. When the algorithm fails (if the pattern is degenerate) we show an <a href="https://en.wikipedia.org/wiki/Iterated_function_system">IFS</a> point cloud for the same limit set.</p>
<p>The yellow boxes show where the current region is transformed to by the two transformations and their inverses.</p>
<hr>
<p>Source code: <a href="https://github.com/timhutton/mobius-transforms">https://github.com/timhutton/mobius-transforms</a></p>
</div>
</html>