-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmp5_json_code.html
959 lines (811 loc) · 35.2 KB
/
mp5_json_code.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
<!DOCTYPE html>
<!-- todo: rename: mp5interactive.html -->
<html lang="en">
<head>
<title>MP5 in Action</title>
<style type="text/css" media="screen">
#editor {
position: absolute;
top: 0;
bottom: 0;
right: 0;
/*right: 60%; or 10%*/
left: 0;
z-index: 1000;
}
.mycanvas3d {
/*
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
*/
position: absolute;
right: 0;
z-index: -10;
/*left: 38%;*/
/* background-color: #f00;*/
}
/*
.ace-monokai {
background-color: red !important;
//background-color: #272822;
//color: #F8F8F2
}
*/
/*
.mybg {
background-color: red !important;
}
*/
</style>
</head>
<script>
'use strict';
var S1 = {
"printerSettings": {},
"mp5-version": "0.3",
"root": {
"type": "root",
"children": [{
"type": "Difference",
"children": [{
"type": "cylinder",
"matrix": [35, 0, 0, 0, 0, 35, 0, 0, 0, 0, 9, 0, 0, 0, 0, 1],
"index": 652818
}, {
"type": "cylinder",
"matrix": [8, 0, 0, 0.658, 0,19, 0, 6.215, 0, 0, 12, 0, 0, 0, 0, 1],
"index": 246357
}],
"matrix": [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
}]
}
};
// multi-object
var S2 = {
"printSettings": {},
"mp5-version": "0.3",
"root": {
"type": "root",
"children": [{
"type": "Difference",
"protected": false,
"children": [{
"type": "cylinder",
"displayColor": [0.77, 0.039, 0.17],
"matrix": [35, 0, 0, 0, 0, 35, 0, 0, 0, 0, 9, 0, 0, 0, 0, 1],
"index": 652818
},
{
"type": "Difference",
"children": [{
"type": "cylinder",
"displayColor": [0.81, 0.657, 0.736],
"matrix": [10, 0, 0, 0, 0, 10, 0, 0, 0, 0, 10, 0, 0, 0, 0, 1],
"index": 1272174
},
{
"type": "cylinder",
"displayColor": [0.114, 0.075, 0.63],
"matrix": [10, 0, 0, 0.658, 0, 10, 0, 6.2155, 0, 0, 10, 0, 0, 0, 0, 1],
"index": 2463576
}],
"displayColor": [0.662, 0.45, 0.72],
"matrix": [2.381193, 0, 0, 0.36, 0, 2.3811, 0, 0.560, 0, 0, 2.38119, 6.90, 0, 0, 0, 1],
"index": 413872
}],
"displayColor": [0.55, 0.06, 0.11],
"matrix": [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
"index": 6565922
},
{
"type": "cylinder",
"displayColor": [0.114, 0.075, 0.63],
"matrix": [10, 0, 0, 0, 0, 10, 0, 0, 0, 0, 1, 9, 0, 0, 0, 1],
"index": 2463576
}
]
}
};
var necklace = {"printerSettings":{"PRINTER":"Ultimaker Origin","FILAMENT":"PLA","DEFAULT":0},"mp5-version":"0.4","root":{"type":"root","children":[{"type":"Difference","protected":false,"children":[{"type":"icylinder","displayColor":[0.77,0.039,0.17],"matrix":[35,0,0,0,0,35,0,0,0,0,9,0,0,0,0,1],"index":652818},{"type":"Difference","protected":false,"children":[{"type":"icylinder","displayColor":[0.81,0.657,0.736],"matrix":[10,0,0,0,0,10,0,0,0,0,10,0,0,0,0,1],"index":1272174},{"type":"icylinder","displayColor":[0.114,0.075,0.63],"matrix":[10,0,0,0.658,0,10,0,6.2155,0,0,10,0,0,0,0,1],"index":2463576}],"displayColor":[0.662,0.45,0.72],"matrix":[2.381193,0,0,0.36,0,2.3811,0,0.56,0,0,2.38119,6.9,0,0,0,1],"index":413872}],"displayColor":[0.55,0.06,0.11],"matrix":[1,0,0,0.329,0,1,0,0.156,0,0,1,4.5,0,0,0,1],"index":6565922},{"type":"icylinder","displayColor":[0.114,0.075,0.63],"matrix":[10,3,0,0,0,10,0,0,0,0,1,5.5,0,0,0,1],"index":2463576}]},"createdAt":"2016-12-27T00:16:07.739Z","title":"necklace","description":"My first hand-written MP5","contributors":["wd: sosi9"],"unique_id":"3ef4a9f2-6175-4a9d-b421-720eac674d89","licence":{}}
var asmjscb_example1 =
{
"printerSettings": {
"PRINTER": "Ultimaker Origin",
"FILAMENT": "PLA",
"DEFAULT": 0
},
"mp5-version": "0.4",
"root": {
"type": "root",
"children":
[
{
"type": "asmjscb",
"displayColor": [0.114, 0.075, 0.63],
"param1": 0.5,
//"matrix": [10, 3, 0, 0, 0, 10, 0, 0, 0, 0, 1, 5.5, 0, 0, 0, 1],
"matrix": [10, 0, 0, 0, 0, 10, 0, 0, 0, 0, 10, 0, 0, 0, 0, 1],
"index": 2463576,
"implicit": "_f = (+R)*(+R) - ((_x) *(_x) + _y * _y + _z * _z);",
"gradient": "_gx = -2 * _x; _gy = -2 * _y; _gz = -2 * _z;"
}
]
},
"createdAt": "2016-12-27",
"title": "first asmjscb object!",
"description": "Dynamically specify the function ans ASM JS fast JIT evaluation",
"contributors": ["sohail"],
"unique_id": "3ef4a9f2-6175-4a9d-b421-720eac674d89",
"licence": {}
};
// ****************************************************
const asmjscb_example2 = {
"printerSettings": {
"PRINTER": "Ultimaker Origin",
"FILAMENT": "PLA",
"DEFAULT": 0
},
"mp5-version": "0.4",
"root": {
"type": "root",
"children": [{
"type": "asmjscb",
"displayColor": [0.114, 0.075, 0.63],
"param1": 0.5,
"matrix": [10, 0, 0, 0, 0, 10, 0, 0, 0, 0, 10, 0, 0, 0, 0, 1],
"index": 2463577,
"implicit": "_f = (+param1)*(+param1) - ((_x/0.7) *(_x/0.7) + _y * _y + _z * _z);",
"gradient": "_gx = (-2) * _x /(0.7); _gy = -2 * _y; _gz = -2 * _z;"
},{
"type": "asmjscb",
"displayColor": [0.114, 0.4, 0.0],
"param1": 0.5,
"matrix": [10,0,0,5, 0,10,0,0, 0,0,10,0, 0,0,0,1],
"index": 2463578,
//"implicit": "_f = (+param1)*(+param1) - ((_x) *(_x) + _y * _y + _z * _z);",
//"gradient": "_gx = (-2) * _x ; _gy = -2 * _y; _gz = -2 * _z;"
"implicit": "m=6; _f = (+param1)**m - ((_x) **m + _y **m + _z **m);",
"gradient": "m=6; _gx = (-m) * (_x**(m-1)) ; _gy = -2 *( _y**(m-1)); _gz = -2 * (_z**(m-1));"
},{
"type": "cylinder",
"displayColor": [1, 0.075, 0],
"matrix": [5,0,0,8, 0,5,0,3, 0,0,10,0, 0,0,0,1],
"index": 2463579
}]
},
"createdAt": "2016-12-27",
"title": "asmjs example 2",
"description": "Multiple colours",
"contributors": ["sohail"],
"unique_id": "3ef4a9f2-6175-4a9d-b421-720eac674d89",
"licence": {}
};
//, { "type": "cylinder", "matrix": [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] }
/*
"implicit": "m=4; _f = (+param1)**m - ((_x) **m + _y **m + _z **m);",
"gradient": "m=4;_gx = (-m) * (_x**(m-1)) ; _gy = -2 *( _y**(m-1)); _gz = -2 * (_z**(m-1));"
*/
//var startup_text = JSON.stringify(S1);
//var MOON = '{"printSettings":{},"mp5-version":"0.3","root":{"type":"root","children":[{"type":"Difference","protected":false,"children":[{"type":"cylinder","displayColor":[0.77,0.039,0.17],"matrix":[35,0,0,0,0,35,0,0,0,0,9,0,0,0,0,1],"index":652818},{"type":"Difference","children":[{"type":"cylinder","displayColor":[0.81,0.657,0.736],"matrix":[10,0,0,0,0,10,0,0,0,0,10,0,0,0,0,1],"index":1272174},{"type":"cylinder","displayColor":[0.114,0.075,0.63],"matrix":[10,0,0,0.658,0,10,0,6.2155,0,0,10,0,0,0,0,1],"index":2463576}],"displayColor":[0.662,0.45,0.72],"matrix":[2.381193,0,0,0.36,0,2.3811,0,0.560,0,0,2.38119,6.90,0,0,0,1],"index":413872}],"displayColor":[0.55,0.06,0.11],"matrix":[1,0,0,0.329,0,1,0,0.156,0,0,1,0.0,0,0,0,1],"index":6565922}]}}';
//var startup_text = MOON;
//var screw_mp5 = {"printerSettings":{"d":2},"mp5-version":"0.3","root":{"type":"root","children":[{"type":"screw","matrix":[1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1],"v":[0,2,0],"pitch":0.5,"profile":"sin","delta_ratio":1.5,"end_type":"0","index":9185154}]}};
//var startup_text = JSON.stringify(screw_mp5);
// var startup_text = JSON.stringify(S2);
// var startup_text = JSON.stringify(necklace); // nicest
// var startup_text = JSON.stringify(asmjscb_example1); // very experimental
var startup_text = JSON.stringify(asmjscb_example2); // actually works well. (apart from bad ellipsoid if hard-coded )
</script>
<script type="text/javascript" src="../js-copy/asmjscb.js"></script>
<script>
'use strict';
// compiler !
// to be moved to a common library? (also used by demo7())
function make_fg (expression, grad_expression) {
const prefix_f = `
'use asm'; // "reports validation errors in developer consoles only where relevant."
param1 = +param1;
//var R = +0.5;
var R = +param1;
var x0 = +0.5*0, y0 = +0.5*0, z0 = +0.5*0;
var i = 0|0, i3 = 0|0;
for(i=0|0; i < points|0; ++i) {
i3 = (i|0)*3;
`, suffix_f = `
}
`, prefix_g = `
'use asm';
param1 = +param1;
var R = +param1;
var i = 0|0, i3 = 0|0;
for(i = 0|0; i < points|0; ++i) {
i3 = (i|0)*3;
`, suffix_g = `
}
`;
var table = {
"_x": "(+x[(i3|0)])",
"_y": "(+x[(i3|0) + 1])",
"_z": "(+x[(i3|0) + 2])",
"_f": "f[i]",
"_gx": "(g[(i3|0)])",
"_gy": "(g[(i3|0) + 1])",
"_gz": "(g[(i3|0) + 2])",
//"p1": "(+param1)",
};
var expression_r = expression;
for (var k in table) {
do {
var old_expression_r = expression_r;
expression_r = expression_r.replace(k, table[k]);
} while (old_expression_r !== expression_r);
console.log(expression_r);
}
var grad_expression_r = grad_expression;
for (var k in table) {
//grad_expression_r = grad_expression_r.replace(k, table[k]);
do {
var old_grad_expression_r = grad_expression_r;
grad_expression_r = grad_expression_r.replace(k, table[k]);
} while (old_grad_expression_r !== grad_expression_r);
console.log(grad_expression_r);
}
var f_source_code = prefix_f + expression_r + suffix_f;
var g_source_code = prefix_g + grad_expression_r + suffix_g;
console.debug(f_source_code);
//console.error(prefix_f);
//var func = new Function("_x", "_y", "_z", "_f", "param1","points", f_source_code);
//var gfunc = new Function("_x", "_y", "_z", "_gx", "_gy", "_gz", "param1","points", prefix_g + grad_expression_r + suffix_g);
var func = new Function("x", "f", "points", "param1", f_source_code);
var gfunc = new Function("x", "g", "points", "param1", g_source_code);
console.debug(func);
console.debug(gfunc);
//asmjscb__func_table[95] = func;
//asmjscb__grad_table[95] = gfunc;
return {eval_implicit: func, eval_gradient: gfunc};
// todo: rtest whether asm.js works on this.
// http://asmjs.org/faq.html
// http://asmjs.org/spec/latest/#modules
}
function make_fg94 () {
const obj94_eval_implicit = function (x, f, points, param1) {
param1 = +param1;
var R = +0.5;
var x0 = +0.5*0, y0 = +0.5*0, z0 = +0.5*0;
var i = 0|0, i3 = 0|0;
for(i=0|0; i < points|0; ++i) {
i3 = (i|0)*3; // i3 = ((i|0) << 1) + (i|0);
//f[i] = +10 - Math.sqrt(+x[i3]*(+x[i3]) + +x[i3+1]*(+x[i3+1]) + +x[i3+2]*(+x[i3+2]));
//f[i] = (+R)*(+R) - (+x[i3]*(+x[i3]) + +x[i3+1]*(+x[i3+1]) + +x[i3+2]*(+x[i3+2]));
f[i] = (+R)*(+R) - ((+x[i3] - +x0)*(+x[i3] - +x0) + (+x[i3+1]- +y0)*(+x[i3+1] - +y0) + (+x[i3+2] - +z0)*(+x[i3+2] - +z0));
}
//console.log("x,f: ", x, f);
}
const obj94_eval_gradient = function (x, g, x_len, param1) {
var i = 0|0, i3 = 0|0;
for(i = 0|0; i < x_len|0; ++i) {
i3 = (i|0)*3; //((i|0) << 1) + (i|0);
//g[(i3|0)] = -2 * +x[(i3|0)];
//g[(i3|0) + 1] = -2 * +x[(i3|0) + 1];
//g[(i3|0) + 2] = -2 * +x[(i3|0) + 2];
//var x0 = +0.5*0, y0 = +0.5*0, z0 = +0.5*0;
g[(i3|0)] = -2 * +x[(i3|0)];
g[(i3|0) + 1] = -2 * +x[(i3|0) + 1];
g[(i3|0) + 2] = -2 * +x[(i3|0) + 2];
}
//console.log("x, g: ", x, g);
}
return {eval_implicit: obj94_eval_implicit, eval_gradient: obj94_eval_gradient};
}
// dispatcher
// function js_implcit_callback(){console.log("ASMJS", arguments);}
function js_implcit_callback(id, x_start_ptr, x_len, output_start_ptr, param1) {
'use asm';
// [94, 8463584, 15625, 7537720, 0.009999999776482582]
//console.log("ASMJS", arguments);
//console.log("id:",id, " x_start_ptr:",x_start_ptr, " x_len:", x_len, " output_start_ptr:", output_start_ptr, " param1", param1);
// $0, Module.HEAPF.subarray($1 >> 2, ($1+$2*3)>>2), Module.HEAPF.subarray($3 >> 2, ($3+$2*3)>>2)
x_start_ptr = x_start_ptr | 0;
x_len = x_len | 0;
//x_len = x_len * 2;
output_start_ptr = output_start_ptr | 0;
var x = Module.HEAPF32.subarray(x_start_ptr >> 2, (x_start_ptr>>2) + (x_len|0) * 3);
var f = Module.HEAPF32.subarray(output_start_ptr >> 2, (output_start_ptr >> 2) + (x_len|0));
//console.log("x,f: ", x, f);
id = id | 0;
/*if ((id|0) === (94|0)) {
//obj94_eval_implicit(x, f, x_len, param1);
throw "no longer";
} else*/ {
var fg = asmjscb__func_table[id|0];
fg.eval_implicit(x, f, x_len, param1);
//throw "unknown ASMJS eval_implicit callback id";
}
}
function js_gradient_callback (id, x_start_ptr, x_len, output_start_ptr, param1) {
//console.log("ASMJS GRAD", arguments);
var x = Module.HEAPF32.subarray(x_start_ptr >> 2, (x_start_ptr>>2) + (x_len|0) * 3);
var g = Module.HEAPF32.subarray(output_start_ptr >> 2, (output_start_ptr >> 2) + ((x_len|0) * 3)); // prantheses are necesary
//id = 95;
id = id | 0;
//if (id !== 94|0) throw "unknown ASMJS callback id";
/*
if ((id|0) === (94|0)) {
//obj94_eval_gradient(x, g, x_len, param1);
throw "no longer";
} else*/ {
var fg = asmjscb__func_table[id|0];
fg.eval_gradient(x, g, x_len, param1);
//throw "unknown ASMJS Gradient callback id";
}
}
// hard coded
const fg94 = make_fg94();
// runtime coded!
//var variables = ["R"];
var expression = `_f = (+R)*(+R) - ((_x) *(_x) + _y * _y + _z * _z);`;
var grad_expression = `_gx = -2 * _x; _gy = -2 * _y; _gz = -2 * _z;`;
asmjscb__func_table[94] = make_fg (expression, grad_expression);
/*
(function($0,$1) {var stack = Runtime.stackSave();var str = $0;var ret = 0;
if (str !== null && str !== undefined && str !== 0) {
ret = Runtime.stackAlloc((str.length << 2) + 1);
writeStringToMemory(str, ret);
};$0=(ret);var str = $1;var ret = 0;
if (str !== null && str !== undefined && str !== 0) {
ret = Runtime.stackAlloc((str.length << 2) + 1);
writeStringToMemory(str, ret);
};$1=(ret);var ret = cfunc($0,$1);Runtime.stackRestore(stack);return ret})
*/
</script>
<body style="background-color: #000000;">
<!--
<canvas class="mycanvas3d" width="500" height="300" id="my_canvas"></canvas>
-->
<div id="editor" style="background-color: rgba(1,0,0,0);">
</div>
</body>
<!-- ************************** THREE JS **************************-->
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"> </script>
<!--script type="text/javascript" src="../../../js_iteration_1/three_r79.js"> </script-->
<script type="text/javascript" src="./js-copy/OrbitControls_r79.js"></script>
<!-- ************************** IMPLISOLID CODE **************************-->
<!--script type="module" src="./js-copy/simple_assert.js"></script-->
<script type="text/javascript" src="./js-copy/simple_assert.js"></script>
<script type="text/javascript" src="./js-copy/geometry79.js"></script>
<script type="text/javascript" src="./js-copy/implisolid_main.js"></script>
<script type="text/javascript" src="./js-copy/example_objects.js"></script>
<script type="text/javascript" src="./js-copy/js_utils.js"></script> <!-- stuff used by pointset_utils.js -->
<script type="text/javascript" src="./js-copy/pointset_utils.js"></script>
<script type="text/javascript" src="./js-copy/arrow_utils.js"></script>
<script type="text/javascript" src="./js-copy/example_materials.js"></script>
<script type="text/javascript" src="./js-copy/performance_graphs.js"></script>
<script type="text/javascript" src="./js-copy/misc_props.js"></script>
<script type="text/javascript" src="./js-copy/boundingbox_utils.js"></script>
<!-- ************************** OLD ******************************** - -
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.js"> </script>
< ! - -script type="text/javascript" src="../../../js_iteration_1/three_r79.js"> </script - - >
<script type="text/javascript" src="../../../js_iteration_1/controls/OrbitControls_r79.js"></script>
<script type="text/javascript" src="../../../examples/js-copy/assert.js"></script>
<script type="text/javascript" src="../../../js_iteration_2/geometry79.js"></script>
<script type="text/javascript" src="../../../js_iteration_2/implisolid_main.js"></script>
<script type="text/javascript" src="../../../examples/js-lib/example_objects.js"></script>
<script type="text/javascript" src="../../../js_iteration_2/js-lib/js_utils.js"></script>
<script type="text/javascript" src="../../../js_iteration_2/js-lib/pointset_utils.js"></script>
<script type="text/javascript" src="../../../js_iteration_2/js-lib/arrow_utils.js"></script>
<script type="text/javascript" src="../../../examples/js-lib/example_materials.js"></script>
<script type="text/javascript" src="../../../examples/js-lib/performance_graphs.js"></script>
<script type="text/javascript" src="../../../examples/js-lib/misc_props.js"></script>
<script type="text/javascript" src="../../../examples/js-lib/boundingbox_utils.js"></script>
-->
<!-- ************************** ACE **************************-->
<!--
http://www.jsoneditoronline.org/
src="/ace-builds/src-noconflict/ace.js"
https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js
https://raw.githubusercontent.com/ajaxorg/ace-builds/master/src-min-noconflict/ace.js
-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript" charset="utf-8"></script>
<!--script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-beautify.js" type="text/javascript" charset="utf-8"></script-->
<!--script src="https://raw.githubusercontent.com/beautify-web/js-beautify/master/js/lib/beautify.js" type="text/javascript" charset="utf-8"></script-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.4/beautify.min.js" type="text/javascript" charset="utf-8"></script>
<script>
'use strict';
/*
sets up interface between ace editor and pure logic.
No dependency to ace editor outside `init_ace()`
wait_before_first_call_to_callback is initial_wait_time
*/
/* Why is `build_code_callback` called in two places? Becauseit is caalled once in the startup, nd then in each change. */
function init_ace(build_code_callback, startup_text1, wait_before_first_call_to_callback=1500) {
// see https://ace.c9.io/#nav=howto
/*
a global state to enable and disable change events,
(almost like a mutex, but used one-off).
Enabled after initial_wait_time.
*/
var eglobals = {reactive: false};
var editor = ace.edit("editor");
/* Themes: "ace/theme/monokai", "ace/theme/ace_theme_1", "ace/theme/clouds", "ace/theme/pastel_on_dark", "ace/theme/twilight", */
editor.setTheme("ace/theme/monokai");
/* Modes: "ace/mode/javascript", "ace/mode/javascript" */
editor.getSession().setMode("ace/mode/json");
editor.getSession().on('change', function(e) {
if (!eglobals.reactive) return;
var text = editor.getValue(); // or session.getValue
build_code_callback(text);
return;
/*
console.log("change detected", e);
// e.type, etc
action: "insert"
start: {column:..., row:...},
end: {column:..., row:...},
lines: [","]
*/
});
editor.commands.addCommand({
name: 'myCommand',
bindKey: {win: 'Ctrl-M', mac: 'Command-M'},
exec: function(editor) {
console.log("Comand M :MyCommand");
},
readOnly: true // false if this command should not apply in readOnly mode
});
editor.$blockScrolling = Infinity;
/* startup text */
editor.setValue(`\n\n"Please Wait ..."\n\n`);
setTimeout(function() {
var beautify = ace.require("ace/ext/beautify");
function my_beautify() {
// beautify.beautify(editor.getSession());
var val = editor.getSession().getValue();
//Remove leading spaces
var array = val.split(/\n/);
array[0] = array[0].trim();
val = array.join("\n");
//Actual beautify (prettify)
val = js_beautify(val); // external
//Change current text to formatted text
editor.getSession().setValue(val);
}
editor.setValue(startup_text1);
my_beautify();
eglobals.reactive = true;
// First call to the build_code_callback()
build_code_callback( editor.getValue() );
}, wait_before_first_call_to_callback);
}
</script>
<script>
'use strict';
function reset() {
// reset tables
asmjscb__func_table = {};
}
function build_code(editor_text) {
try {
reset(); //?
var mp5_json = editor_text;
var mp5 = JSON.parse(mp5_json);
//beautify.beautify(editor.session); // loops forever until your OS crashes!
var sons = mp5.root.children;
for( var i = 0; i < sons.length; ++i) {
var shape_dict = sons[i];
var bb = getBoundingBoxForDict(shape_dict, true);
//console.error("bbox", bb); // ****
var shape_json = JSON.stringify(shape_dict);
//console.error("shape_json11",shape_json); // ****
console.log(JSON.parse(shape_json));
var submesh = put_up_there(shape_json, bb, i, shape_dict.matrix, shape_dict.displayColor, simple_MC_only_polygonization_json);
};
} catch(e) {
// console.log('invalid json in editor');
console.log(e.stack)
if (shape_json) {
console.debug('shape_json that caused error:', shape_json);
}
console.error("Exception:", e);
}
}
;
/*
init_ace() is executed here before startup_sequence(),
but it delays the first call to build_code().
*/
init_ace(build_code, startup_text, 1500);
</script>
<!-- ******************************************************************** -->
<script nottype="module">
'use strict';
/** main state of demo */
var globals = {mesh_solid: [], main_mesh: null, /*mesh_wireframe: null, normals: null, arrows: null,*/ chosen_matrial:null, geometry: null, scene:null };
/** Demo's config */
var CONFIG = {CHOSEN_MATERIAL_INDEX: 2, PERSPECTIVE: true, SHOW_WIREFRAME: false, SHOW_SOLID: true, SPEED: 3. / 100, MESH_SCALE: 1.0};
/** setting up the polygonisation task: provides a json for polygoniser as a task description. */
const simple_MC_only_polygonization_json = function(BSH) { return JSON.stringify({
resolution: Math.floor( 30 ),
box: //bbox,
{xmin: -BSH, xmax: +BSH, ymin: -BSH, ymax: +BSH, zmin: -BSH, zmax: +BSH},
ignore_root_matrix: true,
vresampl: {iters: 0, c: 1.0},
projection: {enabled: 0},
qem: {enabled: 0},
subdiv: {enabled: 0},
overall_repeats: 1,
debug: {
enabled_pointsets: 0,
// only_rank
post_subdiv_noise: 0.0,
},
});};
/**
*/
function put_up_there(string_json, bb_arg, i, matrix, color_rgb, polygonization_json_provider) {
// var ignore_root_matrix = false;
//globals.geometry = IMPLICIT.getLiveGeometry_from_json(shape_json, polygonization_json, true, function(){}, ignore_root_matrix);
var shape_json = string_json;
//console.error(shape_json);
//console.error(polygonization_json);
if (!bb_arg) {
var polygonization_json = JSON.stringify(
{"resolution":40,"box":{"xmin":-0.55,"xmax":0.55,"ymin":-0.55,"ymax":0.55,"zmin":-0.55,"zmax":0.55},"ignore_root_matrix":false,"vresampl":{"iters":3,"c":1},"projection":{"enabled":1},"qem":{"enabled":1},"subdiv":{"enabled":1},"overall_repeats":1,"debug":{"enabled_pointsets":0,"post_subdiv_noise":0}}
);
polygonization_json = polygonization_json_provider(0.5);
//var polygonization_json = JSON.stringify(d.polygonization_json);
//var shape_json = string_json;
//console.error(shape_json);
//console.error(polygonization_json);
var box1 = JSON.parse(polygonization_json).box;
var bbox = {min: {x:box1.xmin, y:box1.ymin, z:box1.zmin}, max: {x:box1.xmax, y:box1.ymax, z:box1.zmax}};
} else {
var bbox = bb_arg;
}
// "asmjscb" objects
var shapedict = JSON.parse(shape_json);
if (shapedict.type === "asmjscb") {
var f = shapedict.implicit;
var g = shapedict.gradient;
var p = shapedict.param1;
var idx = shapedict.index;
assert(idx);
var fg = make_fg(f,g);
if (asmjscb__func_table[idx]) {
alert("Two object with the same \"index\": An object with index ="+idx+" is already created. "+JSON.stringify(idx));
}
if (!idx) {
alert("Object must have an \"index\": with a non-zero integer number. "+JSON.stringify(idx));
}
asmjscb__func_table[idx] = fg;
}
CONFIG.implisolid = {default_mc_resolution: 20};
globals.geometry = IMPLICIT.getLiveGeometry(shape_json, bbox, true);
//globals.geometry = THREE.BoxGeometry();
assert(!CONFIG.SHOW_WIREFRAME);
assert(CONFIG.SHOW_SOLID);
if ( i === undefined) i = 0;
while (globals.mesh_solid.length < i+1) {
globals.mesh_solid.push(null);
}
// remove everything
if(globals.mesh_solid[i]) {
//globals.scene.remove( globals.mesh_solid[i] );
globals.main_mesh.remove( globals.mesh_solid[i] );
globals.mesh_solid[i] = null;
//globals.scene.remove( globals.main_mesh );
//globals.main_mesh = null; //never
}
/*
if(globals.main_mesh) {
globals.main_mesh.remove( globals.mesh_solid[i] );
globals.mesh_solid[i] = null;
globals.main_mesh = null;
}
*/
if(!globals.main_mesh) {
globals.main_mesh = new THREE.Object3D();
globals.scene.add(globals.main_mesh);
}
if(CONFIG.SHOW_SOLID){
// console.error(globals.geometry);
var material = look_nice()[CONFIG.CHOSEN_MATERIAL_INDEX];
var mesh = new THREE.Mesh( globals.geometry, material );
if (color_rgb) {
var alpha = color_rgb[3];
material.color.setRGB(color_rgb[0], color_rgb[1], color_rgb[2]);
if (alpha !== undefined) {
mesh.transparent = true;
mesh.opacity = alpha;
}
}
mesh.position.set( 0, 0, 0 );
mesh.scale.set( CONFIG.MESH_SCALE, CONFIG.MESH_SCALE, CONFIG.MESH_SCALE );
//globals.scene.add( mesh );
//globals.scene.add( globals.main_mesh );
globals.main_mesh.add(mesh);
globals.mesh_solid[i] = mesh;
console.error("OK", globals.mesh_solid[i]);
var submesh = globals.mesh_solid[i];
submesh.matrixAutoUpdate=false;
var m = matrix;
submesh.matrix.set(m[0],m[1],m[2],m[3],m[4],m[5],m[6],m[7],m[8],m[9],m[10],m[11],m[12],m[13],m[14],m[15]);
console.error(m, submesh.matrix.elements);
}
}
function meat() {
'use strict';
/*
var global_time = 0.2 * 5;
var d = provide_input(global_time, 0, {}, globals);
var shape_json = d.shape_json;
put_up_there(shape_json);
*/
}
function init_3js_scene(IMPLICIT, globals, THREE) {
'use strict';
var scene = new THREE.Scene();
globals.scene = scene;
// var render_canvas = document.getElementById('my_canvas');
/*var WIDTH = render_canvas.innerWidth,
HEIGHT = render_canvas.innerHeight;
*/
//assert(WIDTH);
//assert(HEIGHT);
//desired_width, desired_height
globals.desired = {WIDTH:380, HEIGHT:310};
if (CONFIG.PERSPECTIVE) {
globals.camera = new THREE.PerspectiveCamera( 75, globals.desired.WIDTH / globals.desired.HEIGHT, 1, 10000 ); // ( fov, aspect, near, far )
//globals.camera.position.set();
} else {
// Use Orthogaphic camera
var span_per_pixel = 1.0/12.0;
var x0 = WIDTH/2.0*span_per_pixel;
var y0 = HEIGHT/2.0*span_per_pixel;
globals.camera = new THREE.OrthographicCamera( -x0, +x0, -y0, +y0, 1, 10000 ); // ( left, right, top, bottom, near, far )
}
var GRID_CELL_SIZE = 1.0;
//globals.camera.position.set(0, 2, 40*GRID_CELL_SIZE); // tele
globals.camera.position.set(0, 2, 21*GRID_CELL_SIZE); // tele
//globals.camera.position.set(0, 0, 3*GRID_CELL_SIZE); // close up
//var grid = grid_bed(GRID_CELL_SIZE);
//globals.scene.add( grid );
/*
globals.sizeListener = function(w,h){
globals.camera.aspect = w/ h;
};
*/
//var materials_list = look_nice();
//globals.chosen_matrial = materials_list[CONFIG.CHOSEN_MATERIAL_INDEX];
//var grid = grid_bed(GRID_CELL_SIZE);
//globals.scene.add( grid );
var dirLight = new THREE.DirectionalLight(0xffffff, 1);
dirLight.position.set(100, 100, 50);
globals.scene.add(dirLight);
var dirLight2 = new THREE.DirectionalLight(0x00ffff, 1);
dirLight2.position.set(-100, -100, -50);
globals.scene.add(dirLight2);
var renderer = new THREE.WebGLRenderer( { antialias: true/*, canvas: render_canvas*/ } );
//renderer.setSize ( 300, 300, true );
//renderer.setSize (desired_width, desired_height );
renderer.setSize ( globals.desired.WIDTH, globals.desired.HEIGHT, true);
globals.renderer = renderer;
document.body.appendChild( globals.renderer.domElement );
//var cnvs = document.querySelector('canvas');
var cnvs = globals.renderer.domElement;
//cnvs.classList.append("mycanvas3d");
cnvs.className = "mycanvas3d";
/*
globals.renderer.domElement.addEventListener('resize',function(e){
globals.sizeListener(e.width, e.height);
}, true);
*/
var cameraControls = new THREE.OrbitControls( globals.camera, renderer.domElement );
cameraControls.rotateSpeed = 1.5;
cameraControls.zoomSpeed = 1.2;
cameraControls.panSpeed = 0.8;
cameraControls.enableZoom = true;
cameraControls.enablePan = true;
cameraControls.enableDamping = true;
cameraControls.dampingFactor = 0.6; //0.9; // 1.0; //motile: 0.3;
cameraControls.target.set( 0, 0, 0 );
cameraControls.addEventListener( 'change', function () {console.log("CC");} );
globals.cameraControls = cameraControls;
}
/** updates the globals state `globals`.
* globals is a prograam staate, i.e.e contains references, to meshes, scene objects, etc) not just parameters.
* Whatever changes during the animation.
*/
function per_frame_logic(globals) {
'use strict';
/*
for (var i = 0; i < globals.mesh_solid.length; ++i) {
//assert(globals.mesh_solid[i]);
if(globals.mesh_solid[i]) {
globals.mesh_solid[i].rotation.x += 0.01*CONFIG.SPEED;
globals.mesh_solid[i].rotation.y += 0.1*CONFIG.SPEED;
//globals.mesh_solid[i].rotation.set(10,10,10);
//console.error("rotate");
}
}
*/
if (globals.main_mesh){
globals.main_mesh.rotation.x += 0.02*CONFIG.SPEED;
globals.main_mesh.rotation.y += 0.3*CONFIG.SPEED;
}
}
/** Renders a frame, also triggers (drives) the next frame as a chain (of infitnite callbacks). */
function animate_a_frame() {
'use strict';
per_frame_logic(globals);
globals.renderer.render( globals.scene, globals.camera );
// console.log(globals.camera, globals.mesh_solid);
requestAnimationFrame( animate_a_frame );
}
/** Global variables */
var IMPLICIT;
var PS_UTILS;
var assert;
function startup_sequence() {
'use strict';
console.log("ready.");
assert = my_assert;
try{
assert = require('./js-copy/simple_assert.js').assert;
} catch(err) {
console.log('require does not work in browser.')
function assert3(cond, message) {
if (!cond) {
message = message || "Assertion failed for unspecified reason";
console.error(message);
console.error(message.stack);
throw new Exception("assert ", message);
}
}
assert = assert3;
}
/*
//import {assert3} from './js-copy/simple_assert.js';
*/
IMPLICIT = _on_cpp_loaded(Module); // from implisolid_main.js
PS_UTILS = new PS_UTILS_CLASS(IMPLICIT);
/*
//if(delta_t === undefined)
var delta_t = 0.2 * 5;
// Subjective time!
global_time += delta_t;
*/
/*
var global_time = 0.2 * 5;
var d = provide_input(global_time, 0, {}, globals);
var shape_json = d.shape_json;
var polygonization_json = JSON.stringify(d.polygonization_json);*/
//IMPLICIT.use_II = 0;
//IMPLICIT.use_III = 0;
//IMPLICIT.use_II_qem = 0;
init_3js_scene(IMPLICIT, globals, THREE);
animate_a_frame();
meat();
/* Now it waits until the initial_wait_time of `build_code` in `init_ace()` */
};
</script>
<script>
'use strict';
var startup_sequence;
var Module;
if (!Module)
Module =
{
preRun:[ ],
onRuntimeInitialized: startup_sequence,
// //memoryInitializerPrefixURL: "../../../build/",
// memoryInitializerPrefixURL: "../js-copy/",
//locateFile: (path, scriptDirectory) => "../js/",
locateFile: (path, scriptDirectory) => "./js-copy/",
};
</script>
<script type="text/javascript" src="./js-copy/mcc2.compiled.js"></script>
<!-- script type="text/javascript" src="../../../build/mcc2.compiled.js"></script -->
<!--script type="text/javascript" src="https://beta.wedesign.live/mcc2.compiled.js"></script-->
</html>