forked from timhutton/zomes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
990 lines (887 loc) · 33 KB
/
index.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
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
<!DOCTYPE html>
<html>
<head>
<title>Zomes</title>
<meta charset="utf-8">
<style>
.slidecontainer {
width: 100%; /* Width of the outside container */
}
/* The slider itself */
.slider {
-webkit-appearance: none; /* Override default CSS styles */
appearance: none;
width: 100%; /* Full-width */
height: 25px; /* Specified height */
background: #d3d3d3; /* Grey background */
outline: none; /* Remove outline */
opacity: 0.7; /* Set transparency (for mouse-over effects on hover) */
-webkit-transition: .2s; /* 0.2 seconds transition on hover */
transition: opacity .2s;
}
/* Mouse-over effects */
.slider:hover {
opacity: 1; /* Fully shown on mouse-over */
/* causes strange bug on edge where the slider grows when the mouse cursor hovers */
}
/* The slider handle (use -webkit- (Chrome, Opera, Safari, Edge) and -moz- (Firefox) to override default look) */
.slider::-webkit-slider-thumb {
-webkit-appearance: none; /* Override default look */
appearance: none;
width: 25px; /* Set a specific slider handle width */
height: 25px; /* Slider handle height */
background: #4CAF50; /* Green background */
cursor: pointer; /* Cursor on hover */
}
.slider::-moz-range-thumb {
width: 25px; /* Set a specific slider handle width */
height: 25px; /* Slider handle height */
background: #4CAF50; /* Green background */
cursor: pointer; /* Cursor on hover */
}
p {
margin-bottom: 0;
}
</style>
<script>
function p3( x, y, z ) { return { x:x, y:y, z:z }; }
function p2( x, y ) { return { x:x, y:y, z:0 }; }
function add( a, b ) { return p3( a.x + b.x, a.y + b.y, a.z + b.z ); }
function sub( a, b ) { return p3( a.x - b.x, a.y - b.y, a.z - b.z ); }
function mul( a, f ) { return p3( a.x * f, a.y * f, a.z * f ); }
function dot( a, b ) { return a.x * b.x + a.y * b.y + a.z * b.z; }
function len2( a ) { return dot( a, a ); }
function len( a ) { return Math.sqrt( len2( a ) ); }
function dist( a, b ) { return len( sub( a, b ) ); }
function normalize( a ) { return mul( a, 1 / len( a ) ); }
function cross( a, b ) { return p3( a.y * b.z - a.z * b.y, a.z*b.x - a.x * b.z, a.x * b.y - a.y * b.x ); }
function mid( a, b ) { return mul( add( a, b ), 0.5 ); }
function angle( p1, p2, p3 ) { var a = dist(p2,p3); var b = dist(p1,p2); var c = dist(p1,p3); return Math.acos( ( c*c - a*a - b*b ) / ( -2*a*b ) ); }
function rot2d( v, theta ) { var s = Math.sin(theta); var c = Math.cos(theta); return p2( v.x*c - v.y*s, v.x*s + v.y*c ); }
function deg2rad(deg) { return deg * Math.PI / 180; }
function rad2deg(rad) { return 180.0 * rad / Math.PI; }
function plane_normal(a,b,c) { return cross( normalize( sub( b, a ) ), normalize( sub( c, a ) ) ); }
function intersect_line_and_plane(a,v,p,n) { return add( a, mul( v, dot( sub(p,a), n ) / dot( v, n ) ) ); }
function clear() {
verts = [];
verts2d = [];
shadow_verts2d = [];
edges = [];
faces = [];
angles = [];
planar_pieces = [];
}
function getMousePos( evt ) {
var rect = evt.target.getBoundingClientRect();
return p2( evt.clientX - rect.left, evt.clientY - rect.top );
}
function onMouseMove( evt ) {
var pos = getMousePos( evt );
var canvas = evt.target;
view_height = (2*( canvas.height / 2.0 - pos.y ) / canvas.height).toFixed(3)-0.001;
view_slider.value = view_height;
document.getElementById("view_label").innerHTML = "View height: "+view_height;
repositionCamera();
}
function onMouseDown( evt ) {
var pos = getMousePos( evt );
isSpinning = !isSpinning;
}
function repositionCamera() {
var look_at = p3(0,0,0);
var d = 40;
var vd = Math.max( ground_level, d * view_height );
var hd = Math.sqrt( d*d - vd*vd );
camera.p = p3( hd*Math.cos(camera_theta), hd*Math.sin(camera_theta), vd );
camera.z = normalize( sub( look_at, camera.p ) );
var up = p3(0,0,1);
camera.x = normalize( cross( camera.z, up ) );
camera.y = normalize( cross( camera.x, camera.z ) );
}
function pointInRect( p, rect ) {
return p.x > rect.x && p.x < ( rect.x + rect.width ) &&
p.y > rect.y && p.y < ( rect.y + rect.height );
}
function add_vert( p ) {
verts.push( p );
return verts.length-1;
}
function make_edge(i,j) {
edges.push( [i,j] );
}
function make_face3(i,j,k) {
faces.push( [i,j,k] );
make_edge(i,j);
make_edge(j,k);
make_edge(k,i);
}
function make_face4(i,j,k,m) {
faces.push( [i,j,k,m] );
make_edge(i,j);
make_edge(j,k);
make_edge(k,m);
make_edge(m,i);
}
function make_face5(i,j,k,m,n) {
faces.push( [i,j,k,m,n] );
make_edge(i,j);
make_edge(j,k);
make_edge(k,m);
make_edge(m,n);
make_edge(n,i);
}
function make_zome() {
clear();
// TODO: if K is vertical coord of 4th point, add new parameter to control its horizontal position
pieces = "From top to bottom: <ol>";
world_scale = scale * 0.001;
var M = N-1; // number of rows of faces
var cos_theta = Math.cos(deg2rad(theta));
var sin_theta = Math.sin(deg2rad(theta));
var zome_height = H * world_scale;
var total_polar_height = zome_height / D;
var tip_Z = zome_height / 2;
var kite_ratio_sum = (K==1) ? (M+1) : (1-Math.pow(K,M+1)) / (1-K);
var top_edge_length = total_polar_height / ( kite_ratio_sum * cos_theta );
ground_level = tip_Z - zome_height;
var add_overlaps = false;
// add the top vert
var zome_tip = p3( 0, 0, tip_Z );
add_vert( zome_tip );
// add the top ring of vertices
var ring1_radius = top_edge_length * sin_theta;
var ring1_height = top_edge_length * cos_theta;
for(var i=0; i<N; i++) {
var a = i*2*Math.PI / N;
var p = p3( ring1_radius * Math.sin(a), ring1_radius * Math.cos(a), tip_Z-ring1_height );
add_vert( p ); // we assume that this is above ground level
}
// add the faces
var closest_dist_to_ground = Number.MAX_VALUE;
var hit_ground = false;
var ground_level_with_tolerance = ground_level - 0.0001;
var triangles = [];
for(var r=1;r<=M && !hit_ground;r++) {
for(var i=0;i<N;i++) {
var ia = (r-1)*N+1+i;
var ib = Math.max(0,(r-2)*N+1+(i+1)%N);
var ic = (r-1)*N+1+(i+1)%N;
var a = verts[ ia ];
var b = verts[ ib ];
var c = verts[ ic ];
var d = add(b,mul(sub(mid(a,c),b),1+K)); // make a kite (rhombus if K=1)
var top_angle = rad2deg( angle( a, b, c ) );
var top_strut_length = dist( a, b ) / world_scale;
if( d.z >= ground_level_with_tolerance ) {
var id = add_vert( d );
make_face4( ia, ib, ic, id );
if(i==0) {
var width = dist( a, c ) / world_scale;
if( K===1 ) {
var height = dist( b, d ) / world_scale;
pieces += "<li>"+N+" diamonds with width "+width.toFixed(0)+" mm and height "+height.toFixed(0)+" mm. Top angle: " + top_angle.toFixed(0)+" degrees. Strut length: "+top_strut_length.toFixed(1)+" mm. </li>";
}
else {
var top_height = dist( b, mid( a, c ) ) / world_scale;
var bottom_height = dist( d, mid( a, c ) ) / world_scale;
pieces += "<li>"+N+" kites with width "+width.toFixed(0)+" mm and top half height "+top_height.toFixed(0)+" mm and bottom half height "+bottom_height.toFixed(0)+" mm. Top angle: " + top_angle.toFixed(0)+" degrees. Top strut length: "+top_strut_length.toFixed(1)+" mm. </li>";
}
if(!add_overlaps) { planar_pieces.push( [a,b,c,d] ); }
}
if(Math.abs(d.z-ground_level) < 0.0001) {
// deal with case where this ring is exactly on the ground
hit_ground = true;
triangles.push(id);
triangles.push(id);
triangles.push(ic);
}
}
else {
var u = (ground_level - a.z ) / (d.z - a.z);
var e = add(a,mul(sub(d,a),u));
var f = add(c,mul(sub(d,c),u));
var ie = add_vert( e );
var i_f = add_vert( f );
make_face5( ia, ib, ic, i_f, ie );
if(i==0) {
var width = dist( a, c ) / world_scale;
var base = dist( e, f ) / world_scale;
var top_height = dist( b, mid( a, c ) ) / world_scale;
var bottom_height = dist( d, mid( a, c ) ) / world_scale;
var reduced_bottom_height = dist( mid( e, f ), mid( a, c ) ) / world_scale;
var shape = (K===1)?"diamonds":"kites";
pieces += "<li>"+N+" truncated "+shape+" with width "+width.toFixed(0)+" mm and top half height "+top_height.toFixed(0)+" mm and full bottom half height "+bottom_height.toFixed(0)+" mm reduced to "+reduced_bottom_height.toFixed(0)+" mm, leaving base of "+base.toFixed(0)+" mm. Top angle: " + top_angle.toFixed(0)+" degrees. Top strut length: "+top_strut_length.toFixed(1)+" mm. </li>";
if(!add_overlaps) { planar_pieces.push( [a,b,c,f,e] ); }
}
hit_ground = true;
triangles.push(ie);
triangles.push(i_f);
triangles.push(ic);
}
// keep track of the nearest cut value that would intersect a kite/diamond tip
var dist_to_ground = Math.abs( d.z - ground_level );
if( dist_to_ground < closest_dist_to_ground ) {
closest_dist_to_ground = dist_to_ground;
cut_suggested = ( tip_Z - d.z ) / total_polar_height;
}
}
}
if(add_overlaps) {
var base_center = p3( 0, 0, ground_level );
var overlap_mm = 30;
var overlap = overlap_mm * world_scale;
var n_faces = faces.length;
for(var iFace = 0; iFace < n_faces; iFace++ ) {
var face = faces[iFace];
if( face.length < 4) { continue; }
if( face.length == 4 ) {
// extend diamond/kite with overlap on lower edges
var ia = face[0];
var ib = face[1];
var ic = face[2];
var id = face[3];
var a = verts[ ia ];
var b = verts[ ib ];
var c = verts[ ic ];
var d = verts[ id ];
var extension_length = overlap / Math.cos( angle( d, a, b ) - Math.PI/2 ); // (assume angles same on each side)
var ae = mul( sub( a, b ), extension_length / dist( a, b ) );
var cg = mul( sub( c, b ), extension_length / dist( c, b ) );
var df = add( ae, cg );
var e = add( a, ae );
var f = add( d, df );
var g = add( c, cg );
// avoid intersection from same-ring neighbors
var e2 = intersect_line_and_plane( e, sub(f,e), a, plane_normal(a, zome_tip, base_center) );
var g2 = intersect_line_and_plane( g, sub(f,g), c, plane_normal(c, zome_tip, base_center) );
var ie2 = add_vert( e2 );
var i_f = add_vert( f );
var ig2 = add_vert( g2 );
make_face4( ie2, ia, id, i_f );
make_face4( id, ic, ig2, i_f );
if( (iFace % N) == 0 ) {
planar_pieces.push( [ a, b, c, g2, f, e2 ] );
}
}
else if( face.length == 5 ) {
// extend truncated diamond/kite with overlap on lower edges
var ia = face[0];
var ib = face[1];
var ic = face[2];
var id = face[3];
var ie = face[4];
var a = verts[ ia ];
var b = verts[ ib ];
var c = verts[ ic ];
var d = verts[ id ];
var e = verts[ ie ];
var extension_length = overlap / Math.cos( angle( e, a, b ) - Math.PI/2 ); // (assume angles same on each side)
var af = mul( sub( a, b ), extension_length / dist(a, b) );
var cj = mul( sub( c, b ), extension_length / dist(c, b) );
var f = add( a, af );
var j = add( c, cj );
// avoid intersection from same-ring neighbors
var f2 = intersect_line_and_plane( f, sub(e,a), a, plane_normal(a, zome_tip, base_center) );
var j2 = intersect_line_and_plane( j, sub(d,c), c, plane_normal(c, zome_tip, base_center) );
var if2 = add_vert( f2 );
var ij2 = add_vert( j2 );
// avoid intersection with ground place
var g = intersect_line_and_plane( f, sub(e,a), e, p3(0,0,1) );
var h = intersect_line_and_plane( j, sub(d,c), d, p3(0,0,1) );
var ig = add_vert( g );
var ih = add_vert( h );
make_face4( if2, ia, ie, ig );
make_face4( id, ic, ij2, ih );
if( (iFace % N) == 0 ) {
planar_pieces.push( [ a, b, c, j2, h, g, f2 ] );
}
}
}
}
// add the bottom row of triangles as needed
if(triangles.length>0) {
for(var i=0;i<triangles.length;i+=3) {
var ia = triangles[(i+1)%triangles.length];
var ib = triangles[(i+2)%triangles.length];
var ic = triangles[(i+3)%triangles.length];
if( ia < verts.length && ib < verts.length && ic < verts.length ) {
make_face3( ia, ib, ic );
if(i==0) {
var a = verts[ ia ];
var b = verts[ ib ];
var c = verts[ ic ];
var top_angle = rad2deg( angle( a, b, c ) );
var top_strut_length = dist( a, b ) / world_scale;
var width = dist( a, c ) / world_scale;
var height = dist( b, mid( a, c ) ) / world_scale;
pieces += "<li>"+N+" isosceles triangles with base "+width.toFixed(0)+" mm and height "+height.toFixed(0)+" mm. Top angle: " + top_angle.toFixed(0)+" degrees. Top strut length: "+top_strut_length.toFixed(1)+" mm. </li>";
planar_pieces.push( [ a, b, c ] );
}
}
}
}
pieces += "</ol>";
layout_planar_pieces();
var widest_diameter = 0;
for(var iVert = 0; iVert < verts.length; iVert++) {
var v = verts[ iVert ];
var diameter = 2 * dist( v, p3( 0, 0, v.z ) ) / world_scale;
if( diameter > widest_diameter ) {
widest_diameter = diameter;
}
}
var base_diameter = 2 * dist( verts[verts.length-1], p3(0,0,ground_level) ) / world_scale;
var strut_length = dist( verts[0], verts[1] ) / world_scale;
document.getElementById("base_diameter_label").innerHTML = "Base diameter: "+base_diameter.toFixed(0)+" mm";
document.getElementById("widest_diameter_label").innerHTML = "Widest diameter: "+widest_diameter.toFixed(0)+" mm";
document.getElementById("strut_length_label").innerHTML = "Strut length: "+((K==1)?(strut_length.toFixed(0)+" mm"):"varies");
document.getElementById("pieces_label").innerHTML = pieces;
}
function layout_planar_pieces() {
var grid_side = Math.ceil( Math.sqrt( planar_pieces.length ) );
for( var iPiece = 0; iPiece < planar_pieces.length; iPiece++) {
var piece = planar_pieces[iPiece];
// verify planarity
var n = plane_normal( piece[0], piece[1], piece[2] );
for( var iVert=1; iVert < piece.length; iVert++) {
var d = Math.abs(dot(sub(piece[iVert],piece[0]),n));
if( d > 0.0000001) {
console.log('Warning: non-planar piece:',d);
}
}
// reconstruct in z=0
var origin = piece[1]; // (the top of each diamond/kite)
var px = normalize( sub( piece[0], origin ) );
var py = normalize( sub( sub( piece[2], origin ), mul( px, dot( sub( piece[2], origin ), px ) ) ) );
var top_angle = angle( piece[0], piece[1], piece[2] );
for( var iVert = 0; iVert < piece.length; iVert++) {
piece[iVert] = rot2d( p2( dot( sub( piece[iVert], origin ), px ), dot( sub( piece[iVert], origin ), py ) ), Math.PI/2-top_angle/2 );
}
}
// compute the max width and height of the 2D pieces, in mm
var max_width = -Number.MAX_VALUE;
var max_height = -Number.MAX_VALUE;
for( var iPiece = 0; iPiece < planar_pieces.length; iPiece++) {
var piece = planar_pieces[iPiece];
var bounds = { minX: Number.MAX_VALUE, minY: Number.MAX_VALUE, maxX: -Number.MAX_VALUE, maxY: -Number.MAX_VALUE };
for( var iVert = 0; iVert < piece.length; iVert++) {
var v = piece[iVert];
var x = v.x;
var y = v.y;
if( x < bounds.minX) { bounds.minX = x; }
if( y < bounds.minY) { bounds.minY = y; }
if( x > bounds.maxX) { bounds.maxX = x; }
if( y > bounds.maxY) { bounds.maxY = y; }
}
bounds.width = bounds.maxX - bounds.minX;
bounds.height = bounds.maxY - bounds.minY;
if( bounds.width > max_width ) { max_width = bounds.width; }
if( bounds.height > max_height ) { max_height = bounds.height; }
}
var x_sep = 1.05 * max_width / world_scale;
var y_sep = 1.05 * max_height / world_scale;
for( var iPiece = 0; iPiece < planar_pieces.length; iPiece++) {
var piece = planar_pieces[iPiece];
// lay out in grid
var row = Math.floor( iPiece / grid_side );
var col = iPiece % grid_side;
var offset = p2( col*x_sep, row*y_sep );
for(var iVert = 0; iVert < piece.length; iVert++) {
piece[iVert] = p2( piece[iVert].x / world_scale + offset.x, piece[iVert].y / world_scale + offset.y );
}
}
}
function init() {
var canvas = document.getElementById('canvas');
camera_theta = Math.PI / 2;
view_height = 0.5;
point_size = 2;
camera = { p:p3(0,3,-6),
x:p3(1,0,0),
y:p3(0,1,0),
z:p3(0,0,1),
f:canvas.height,
pp:p2(canvas.width/2,canvas.height/2)
};
ground_level = 0;
repositionCamera();
var scale_slider = document.getElementById("scale_slider");
scale_slider.updatefromvalue = function() {
scale = parseFloat(this.value);
document.getElementById("scale_label").innerHTML = "Render scale: "+scale;
}
scale_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
scale_slider.updatefromvalue();
var point_slider = document.getElementById("point_slider");
point_slider.updatefromvalue = function() {
point_size = this.value;
document.getElementById("point_label").innerHTML = "Point size: "+this.value;
}
point_slider.oninput = function() {
this.updatefromvalue();
}
var view_slider = document.getElementById("view_slider");
view_slider.updatefromvalue = function() {
view_height = parseFloat(this.value);
document.getElementById("view_label").innerHTML = "View height: "+view_height;
}
view_slider.oninput = function() {
this.updatefromvalue();
repositionCamera();
}
//view_slider.updatefromvalue();
var H_slider = document.getElementById("height_slider");
H_slider.updatefromvalue = function() {
H = parseFloat(this.value);
document.getElementById("height_label").innerHTML = "Height: "+H+" mm";
}
H_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
H_slider.updatefromvalue();
var N_slider = document.getElementById("N_slider");
N_slider.updatefromvalue = function() {
N = parseFloat(this.value);
document.getElementById("N_label").innerHTML = "N: "+N;
}
N_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
N_slider.updatefromvalue();
var theta_slider = document.getElementById("theta_slider");
theta_slider.updatefromvalue = function() {
theta = parseFloat(this.value);
document.getElementById("theta_label").innerHTML = "Angle of top struts to vertical: "+theta+" degrees";
}
theta_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
theta_slider.updatefromvalue();
var D_slider = document.getElementById("D_slider");
D_slider.updatefromvalue = function() {
D = parseFloat(this.value);
document.getElementById("D_label").innerHTML = "Cut proportion: "+D.toFixed(3);
}
D_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
D_slider.updatefromvalue();
var kite_slider = document.getElementById("kite_slider");
kite_slider.updatefromvalue = function() {
K = parseFloat(this.value);
document.getElementById("kite_label").innerHTML = "Kite ratio: "+K.toFixed(3);
document.getElementById("diamond_button").disabled = (K===1);
}
kite_slider.oninput = function() {
this.updatefromvalue();
make_zome();
}
kite_slider.updatefromvalue();
make_zome();
canvas.addEventListener( 'mousemove', onMouseMove, false );
canvas.addEventListener( 'touchmove', onMouseMove, false );
canvas.addEventListener( 'touchstart', onMouseDown, false );
canvas.addEventListener( 'mousedown', onMouseDown, false );
isSpinning = true;
animate();
}
function camera_projection( p, camera ) {
var ray = sub( p, camera.p ); // the ray from camera center to point
var cp = p3( dot( camera.x, ray ), dot( camera.y, ray ), dot( camera.z, ray ) ); // into camera space
return p3( cp.x * camera.f / cp.z + camera.pp.x,
canvas.height - ( cp.y * camera.f / cp.z + camera.pp.y ),
cp.z );
}
function point( ctx, p ) {
ctx.beginPath();
ctx.arc( p.x, p.y, point_size, 0, 2.0 * Math.PI );
ctx.fill();
}
function redraw() {
drawMesh();
drawNet();
}
function drawMesh() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
// project the mesh onto the screen
for( var iVert = 0; iVert < verts.length; ++iVert ) {
verts2d[ iVert ] = camera_projection( verts[ iVert ], camera );
}
// also project the verts onto the ground
var sun = p3( -50, 50, 70 );
var shadow_verts = [];
for( var iVert = 0; iVert < verts.length; ++iVert ) {
var vert = verts[iVert];
var u = (ground_level - sun.z) / (vert.z - sun.z);
var p = add( sun, mul( sub( vert, sun ), u ) );
shadow_verts2d[ iVert ] = camera_projection( p, camera );
}
ctx.strokeStyle = "rgb(200,200,200)";
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeRect(0, 0, canvas.width, canvas.height);
// draw the faces
ctx.fillStyle = "rgba(200,210,255,0.2)";
for( var iFace = 0; iFace < faces.length; ++iFace ) {
var iVert = faces[iFace][0];
if( iVert < verts2d.length ) {
var a = verts2d[ iVert ];
ctx.beginPath();
ctx.moveTo( a.x, a.y );
for( var i = 1; i < faces[iFace].length; ++i ) {
var iVert = faces[iFace][i];
if( iVert < verts2d.length ) {
a = verts2d[ iVert ];
ctx.lineTo( a.x, a.y );
}
else {
console.log('Invalid vert index:',iVert,'in face',iFace);
}
}
ctx.fill();
}
}
// draw the shadow edges
ctx.strokeStyle = "rgb(240,240,240)";
ctx.beginPath();
for( var i = 0; i < edges.length; ++i ) {
var iVertA = edges[i][0];
var iVertB = edges[i][1];
if( iVertA < shadow_verts2d.length && iVertB < shadow_verts2d.length ) {
var a = shadow_verts2d[ iVertA ];
var b = shadow_verts2d[ iVertB ];
ctx.moveTo( a.x, a.y );
ctx.lineTo( b.x, b.y );
}
}
ctx.stroke();
// draw the edges
ctx.strokeStyle = "rgb(200,200,200)";
ctx.beginPath();
for( var i = 0; i < edges.length; ++i ) {
var iVertA = edges[i][0];
var iVertB = edges[i][1];
var iVertB = edges[i][1];
if( iVertA < verts2d.length && iVertB < verts2d.length ) {
var a = verts2d[ iVertA ];
var b = verts2d[ iVertB ];
ctx.moveTo( a.x, a.y );
ctx.lineTo( b.x, b.y );
}
}
ctx.stroke();
// draw the verts
ctx.fillStyle = "rgb(0,0,0)";
for( var i = 0; i < verts2d.length; ++i ) {
point( ctx, verts2d[i], "" );
}
// draw a dot in the center of the base
ctx.beginPath();
var p = camera_projection( p3( 0, 0, ground_level ), camera );
ctx.fillRect(p.x,p.y,1,1);
ctx.fill();
}
function drawNet() {
var canvas = document.getElementById('canvas2');
var ctx = canvas.getContext('2d');
ctx.strokeStyle = "rgb(100,100,100)";
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeRect(0, 0, canvas.width, canvas.height);
var bounds = getBounds(planar_pieces);
ctx.fillStyle = "rgba(200,210,255,0.2)";
ctx.beginPath();
var scale = 0.9 * Math.min( canvas.width / bounds.width, canvas.height / bounds.height );
var offset = p2( -bounds.minX * scale + bounds.width*0.05*scale, -bounds.minY * scale + bounds.height*0.05*scale );
for( var iPiece = 0; iPiece < planar_pieces.length; iPiece++ ) {
var piece = planar_pieces[ iPiece ];
ctx.moveTo( scale*piece[0].x+offset.x, scale*piece[0].y+offset.y );
for(var iVert = 1; iVert < piece.length; iVert++) {
ctx.lineTo( scale*piece[iVert].x+offset.x, scale*piece[iVert].y+offset.y );
}
ctx.lineTo( scale*piece[0].x+offset.x, scale*piece[0].y+offset.y );
}
ctx.stroke();
ctx.fill();
}
var speed = Math.PI/180/10;//clock angular-speed at 60fps
function animate() {
if( isSpinning ) {
// rotate round
camera_theta += speed;
repositionCamera();
}
redraw();
requestAnimationFrame( animate );
}
function download(filename, type, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:'+type+';charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function getParamsDesc() {
var desc = "# Height = " + H.toFixed(1) + " mm\n";
desc += "# N = " + N.toFixed(0) + " (number of shapes in each ring)\n";
desc += "# theta = " + theta.toFixed(2) + " degrees (angle of top struts to vertical)\n";
desc += "# D = " + D.toFixed(4) + " (cut as proportion of pole-to-pole distance)\n";
desc += "# K = " + K.toFixed(5) + " (kite ratio, lower half to upper half)\n";
desc += "# " + document.getElementById("pieces_label").textContent + "\n";
return desc;
}
function downloadOBJ() {
var obj = "# OBJ produced by https://timhutton.github.io/zomes/\n";
obj += "# Units: mm\n";
obj += getParamsDesc();
for( var iVert = 0; iVert < verts.length; iVert++ ) {
var v = mul( add( verts[ iVert ], p3( 0, 0, -ground_level ) ), 1 / world_scale );
obj += "v " + v.x + " " + v.y + " " + v.z + "\n";
}
for( var iFace = 0; iFace < faces.length; ++iFace ) {
obj += "f";
for( var iFaceVert = 0; iFaceVert < faces[iFace].length; iFaceVert++ ) {
var iVert = faces[iFace][iFaceVert];
obj += " " + (iVert+1); // (OBJ format uses 1-based indices)
}
obj += "\n";
}
download("zome_3D.obj", "application/object", obj);
}
function downloadOBJ_2d() {
var obj = "# OBJ produced by https://timhutton.github.io/zomes/\n";
obj += "# Units: mm\n";
obj += getParamsDesc();
var faces = "";
var iGlobalVert = 1;
for( var iPiece = 0; iPiece < planar_pieces.length; iPiece++) {
var piece = planar_pieces[iPiece];
faces += "f";
for( var iVert = 0; iVert < piece.length; iVert++) {
var v = piece[iVert];
obj += "v " + v.x + " " + -v.y + " " + v.z + "\n";
faces += " " + iGlobalVert;
iGlobalVert++;
}
faces += "\n";
}
obj += faces;
download("zome_2D_pieces.obj", "application/object", obj);
}
function getBounds(planar_pieces) {
var bounds = { minX: Number.MAX_VALUE, minY: Number.MAX_VALUE, maxX: -Number.MAX_VALUE, maxY: -Number.MAX_VALUE };
for( var iPiece = 0; iPiece < planar_pieces.length; iPiece++) {
var piece = planar_pieces[iPiece];
for( var iVert = 0; iVert < piece.length; iVert++) {
var v = piece[iVert];
if( v.x < bounds.minX) { bounds.minX = v.x; }
if( v.y < bounds.minY) { bounds.minY = v.y; }
if( v.x > bounds.maxX) { bounds.maxX = v.x; }
if( v.y > bounds.maxY) { bounds.maxY = v.y; }
}
}
bounds.width = bounds.maxX - bounds.minX;
bounds.height = bounds.maxY - bounds.minY;
return bounds;
}
function downloadSVG_2d() {
var svg = "<!-- SVG produced by https://timhutton.github.io/zomes/ \n";
svg += " Units: mm\n";
svg += getParamsDesc() + " --\>\n";
var bounds = getBounds(planar_pieces);
svg += "<svg version='1.1' xmlns='http://www.w3.org/2000/svg' width='"+bounds.width.toFixed(1)+"mm' height='"+bounds.height.toFixed(1)+"mm' viewBox='"
+ bounds.minX.toFixed(1) + " " + bounds.minY.toFixed(1) + " " + bounds.width.toFixed(1) + " " + bounds.height.toFixed(1) + "'>\n";
svg += " <g stroke='black' stroke-width='1' fill='none'>\n";
for( var iPiece = 0; iPiece < planar_pieces.length; iPiece++) {
var piece = planar_pieces[iPiece];
svg += " <path d='M ";
for( var iVert = 0; iVert < piece.length; iVert++) {
var v = piece[iVert];
svg += v.x + " " + v.y + " ";
if(iVert < piece.length-1) {
svg += "L ";
}
}
svg += "Z' />\n";
}
svg += " </g>\n";
svg += "</svg>\n";
download("zome_2D_pieces.svg", "image/svg+xml", svg);
}
function downloadPDF_2d() {
// TODO: allow user to choose page size
var supported_page_sizes = [ {name:'A4 landscape', width:297, height:210, orientation:'landscape' } ];
var i_page_size = 0;
var margin = { left:10, right:10, top:10, bottom:10 };
var fit_to_one_page = false;
var page = supported_page_sizes[i_page_size];
var drawable = { width:page.width-margin.left-margin.right, height:page.height-margin.top-margin.bottom };
var doc = new jsPDF({
orientation: page.orientation,
unit: 'mm',
format: [page.width, page.height]
})
var bounds = getBounds(planar_pieces);
if( fit_to_one_page ) {
var scale = Math.min( drawable.width / bounds.width, drawable.height / bounds.height );
for( var iPiece = 0; iPiece < planar_pieces.length; iPiece++) {
var piece = planar_pieces[iPiece];
for( var iVert = 0; iVert < piece.length; iVert++) {
var v = piece[iVert];
var v2 = piece[(iVert+1)%piece.length];
doc.line( (v.x-bounds.minX)*scale + margin.left, (v.y-bounds.minY)*scale + margin.top,
(v2.x-bounds.minX)*scale + margin.left, (v2.y-bounds.minY)*scale + margin.top );
}
}
}
else {
// keep the actual size, split the drawing over as many pages as needed, no overlap
var x_pages = Math.ceil( bounds.width / drawable.width );
var y_pages = Math.ceil( bounds.height / drawable.height );
for( var y = 0; y < y_pages; y++) {
for( var x = 0; x < x_pages; x++ ) {
// show which bit to cut
doc.setLineWidth(0.01);
doc.rect(margin.left,margin.top,drawable.width,drawable.height);
// draw the lines (only those within the page will appear)
doc.setLineWidth(0.5);
doc.text('Row '+(y+1).toFixed(0)+' Column '+(x+1).toFixed(0),margin.left+5, margin.top+10);
var left = bounds.minX + drawable.width * x;
var top = bounds.minY + drawable.height * y;
for( var iPiece = 0; iPiece < planar_pieces.length; iPiece++) {
var piece = planar_pieces[iPiece];
for( var iVert = 0; iVert < piece.length; iVert++) {
var v = piece[iVert];
var v2 = piece[(iVert+1)%piece.length];
doc.line( margin.left + v.x - left, margin.top + v.y - top,
margin.left + v2.x - left, margin.top + v2.y - top );
}
}
if( x < x_pages-1 || y < y_pages-1 ) {
doc.addPage();
}
}
}
}
doc.save('zome_2D_pieces.pdf')
}
function setToDiamond() {
var kite_slider = document.getElementById("kite_slider");
kite_slider.value = 1;
kite_slider.oninput();
}
function snapCutToNearest() {
var D_slider = document.getElementById("D_slider");
D_slider.value = cut_suggested;
D_slider.oninput();
}
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>
</head>
<body>
<table border="0">
<tr>
<td valign="top" >
<canvas id="canvas" width="800" height="600">(Canvas drawing not supported by your browser.)</canvas>
</td>
<td width="300px" valign="top" align="left" style="padding: 0px 0px 0px 30px;">
<p id="scale_label">Render scale:</p>
<div class="slidecontainer"><input type="range" min="0.5" max="200" value="17" step="0.01" class="slider" id="scale_slider"></div>
<p id="point_label">Point size:</p>
<div class="slidecontainer"><input type="range" min="0" max="4" value="2" step="0.1" class="slider" id="point_slider"></div>
<p id="view_label">View height:</p>
<div class="slidecontainer"><input type="range" min="-0.999" max="0.999" value="0.5" step="0.001" class="slider" id="view_slider"></div>
<p id="height_label">Height:</p>
<div class="slidecontainer"><input type="range" min="100" max="10000" value="1000" step="5" class="slider" id="height_slider"></div>
<p id="N_label">N:</p>
<div class="slidecontainer"><input type="range" min="3" max="25" value="12" step="1" class="slider" id="N_slider"></div>
<p id="theta_label">Angle of top struts to vertical:</p>
<div class="slidecontainer"><input type="range" min="15" max="80" value="58.6" step="0.1" class="slider" id="theta_slider"></div>
<p>
<div id="D_label" style="float:left">Cut proportion:</div>
<button style="float:right" onclick="snapCutToNearest();" id="snap_cut_button">Snap to nearest</button>
</p>
<div class="slidecontainer"><input type="range" min="0.1" max="1" value="0.5252" step="any" class="slider" id="D_slider"></div>
<p>
<div id="kite_label" style="float:left">Kite ratio:</div>
<button style="float:right" onclick="setToDiamond();" id="diamond_button">Set to diamond</button>
</p>
<div class="slidecontainer"><input type="range" min="0.8" max="1.2" value="1.033" step="0.001" class="slider" id="kite_slider"></div>
<p id="base_diameter_label">Base diameter:</p>
<p id="widest_diameter_label">Widest diameter:</p>
<p id="strut_length_label">Strut length:</p>
<button onclick="downloadOBJ();">Download 3D scene as OBJ</button>
</td>
</tr>
<tr>
<td valign="top" >
<canvas id="canvas2" width="800" height="600">(Canvas drawing not supported by your browser.)</canvas>
</td>
<td valign="top" align="left" style="padding: 0px 0px 0px 30px;">
<p id="pieces_label">From top to bottom:</p>
<button onclick="downloadOBJ_2d();">Download 2D layout as OBJ</button>
<button onclick="downloadSVG_2d();">Download 2D layout as SVG</button>
<button onclick="dynamically();">Download 2D layout as PDF</button>
</td>
</tr>
</table>
<p>
Source code: <a href="https://github.com/timhutton/zomes">https://github.com/timhutton/zomes</a>
</p>
<script>
var loaded = false;
let myScript = document.createElement("script");
myScript.setAttribute("src", "https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.4.1/jspdf.min.js");
function dynamically(){
if (loaded) {
downloadPDF_2d();
} else {
document.body.appendChild(myScript);
}
}
myScript.addEventListener("load", scriptLoaded, false);
function scriptLoaded() {
loaded=true;
downloadPDF_2d();
}
</script>
<!-- jsPDF for PDF output: https://github.com/MrRio/jsPDF
var req = new XMLHttpRequest();
// report progress events
req.addEventListener("progress", function(event) {
if (event.lengthComputable) {
var percentComplete = event.loaded / event.total;
// ...
} else {
// Unable to compute progress information since the total size is unknown
}
}, false);
// load responseText into a new script element
req.addEventListener("load", function(event) {
var e = event.target;
var s = document.createElement("script");
s.innerHTML = e.responseText;
// or: s[s.innerText!=undefined?"innerText":"textContent"] = e.responseText
document.documentElement.appendChild(s);
s.addEventListener("load", function() {
// this runs after the new script has been executed...
});
}, false);
req.open("GET", "foo.js");
req.send();
-->
</body>
</html>