File: mcdisplay.js

package info (click to toggle)
mccode 3.5.19%2Bds5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,113,256 kB
  • sloc: ansic: 40,697; python: 25,137; yacc: 8,438; sh: 5,405; javascript: 4,596; lex: 1,632; cpp: 742; perl: 296; lisp: 273; makefile: 226; fortran: 132
file content (702 lines) | stat: -rw-r--r-- 19,975 bytes parent folder | download
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
// transforms vertices by and returns the transformed
//      apoints  -  an array of vertices
//      transform  -  matrix4
var transformPoints = function(apoints, transform)
{
    var geometry = new THREE.Geometry();
    geometry.vertices = apoints;
    geometry.applyMatrix(transform);
    return geometry.vertices.slice();
}
// "main" class which is a collection of scene graph setup routives and data objects
//
var Main = function ()
{
    this.scene;
    this.camera;
    this.renderer;
    this.controls;

    this.compnodes = {};
    this.rootnode = new THREE.Object3D();
    this.bbnode = new THREE.Object3D();
    this.rootnode.add(this.bbnode);

    this.compColors = [0xf80000, 0x00f800, 0x0000f8, 0x00f8f8, 0xf800f8, 0x00f880, 0xf8f800, 0xf88000, 0x80f800, 0x0080f8, 0x800f8, 0xf80080, 0xa8a8a8];
    this.iColor = -1;

    this.rayColor = 0x00ffff;
    this.aCompVertices
    this.rayobj;
    this.raynodes = [];

    this.iRay = -1;

    this.lut = null;
}
// add circle to xy, xz or yz plane
//
Main.prototype.addCircle = function(plane, x, y, z, radius, parent, linecolor)
{
    console.log(plane, x, y, z, radius);
    if (radius == 0)
    {
        return;
    }

    var wrapper = new THREE.Object3D();
    var m = new THREE.Matrix4()
    if (plane == 'xy') { } // identity
    if (plane == 'xz') { m.makeRotationX( Math.PI/2 ); }
    if (plane == 'yz') { m.makeRotationY( Math.PI/2 ); }

    var segments = 48;
    var circleGeometry = new THREE.CircleGeometry( radius, segments );
    circleGeometry.vertices.shift(); // removes center vertex
    var material = new THREE.MeshBasicMaterial( {color: linecolor} );
    material.side = THREE.DoubleSide;

    var circle = new THREE.Line( circleGeometry, material ); // THREE.Mesh results in solid coloring

    circle.applyMatrix(m);

    circle.position.x = x;
    circle.position.y = y;
    circle.position.z = z;

    parent.add( circle );
}
// add sphere
//		radius
//		wseg 	- width segments
//		hseg 	- height segments
Main.prototype.addSphere = function(radius, wseg, hseg)
{
    var geometry = new THREE.SphereGeometry(radius, wseg, hseg);
    var material = new THREE.MeshBasicMaterial( {color: 0xffff00} );
    var sphereMaterial = new THREE.MeshLambertMaterial({ color: 0xCC0000 });
    var sphere = new THREE.Mesh( geometry, sphereMaterial );
    this.scene.add( sphere );
}
// add pointlight
// 		center 	- THREE.Vector3 instance
Main.prototype.addLight = function(center)
{
    // pointlight (required to light up sphereMaterial)
    var pointLight = new THREE.PointLight(0xFFFFFF);
    pointLight.position.x = center.x;
    pointLight.position.y = center.y;
    pointLight.position.z = center.z;
    this.scene.add(pointLight);
}
//  initialize the scene
//
Main.prototype.init = function(campos, invert)
{
    this.scene = new THREE.Scene();

    this.camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 0.001, 130);
    this.camera.position.x = campos.x; // -50;
    this.camera.position.y = campos.y; // 0;
    this.camera.position.z = campos.z; // 50;

    // NOTE: initial camera view direction is along the x axis

    this.renderer = new THREE.WebGLRenderer();
    this.renderer.setSize(window.innerWidth, window.innerHeight);
    if (invert) {
        this.renderer.setClearColor( 0xffffff );
    }

    element = document.getElementById("3dcanvas");
    console.log(element);
    document.getElementById("3dcanvas").appendChild(this.renderer.domElement);

    this.controls = new THREE.OrbitControls(this.camera);
    this.controls.target.x = -campos.x/2; //1;
    this.controls.target.y = 0; //0;
    this.controls.target.z = campos.z; //49;

    this.camera.lookAt(this.controls.target);

    this.addLight(new THREE.Vector3(10, 50, 130))
    this.scene.add(this.rootnode);
}
//  cat camera view according to campos, a Vector3
//
Main.prototype.setCameraView = function(campos)
{
    this.camera.position.x = campos.x;
    this.camera.position.y = campos.y;
    this.camera.position.z = campos.z;

    this.controls.target.x = 0;
    this.controls.target.y = 0;
    this.controls.target.z = campos.z;

    this.camera.lookAt(this.controls.target);
}
//  set a bounding box around the components
//
Main.prototype.setNativeBoundingBox = function()
{
    var box = new THREE.BoxHelper(this.rootnode, 0x808080);
    this.scene.add(box);
}
// add multiline
// 		arrVector3  -  an array of THREE.Vector3 instances
Main.prototype.addMultiLineV3 = function(arrVector3, parent, linecolor)
{
    var multilinematerial = new THREE.LineBasicMaterial({color: linecolor});
    var multilinegeometry = new THREE.Geometry();
    multilinegeometry.vertices = arrVector3;
    var multiline = new THREE.Line(multilinegeometry, multilinematerial);
    parent.add(multiline);
}
// add multiline proxy method
//		points  -  array of single points
Main.prototype.addMultiLine = function(points, parent, linecolor)
{
    vectors = [];
    for (var i = 0; i < points.length / 3; i++)
    {
        points[i];
        v = new THREE.Vector3(points[i*3], points[i*3+1], points[i*3+2]);
        vectors.push(v);
    }
    this.addMultiLineV3(vectors, parent, linecolor);
}
//  adds tiny boxes to each particle ray scatter point
//
Main.prototype.putScatterPoints = function(raynode)
{
    var v;
    var vtx = raynode.vtx;
    for (var i = 0; i < vtx.length; i++)
    {
        var geometry = new THREE.BoxGeometry( 0.007, 0.007, 0.007 );
        v = vtx[i];
        geometry.translate(v.x, v.y, v.z);
        var material = new THREE.MeshBasicMaterial({ color: raynode.lut_color });
        var cube = new THREE.Mesh( geometry, material );

        raynode.annotcubes.add( cube );
    }
}

//  set color lookuptable min/max range
//  color maps: rainbow, cooltowarm, blackbody, grayscale
Main.prototype.setLutRange = function(min, max)
{
    this.lut = new THREE.Lut( "cooltowarm", 512 );
    this.lut.setMin(min);
    this.lut.setMax(max);
    console.log("Lut min value: " + min);
    console.log("Lut max value: " + max);
}
// add a ray node
//
Main.prototype.addRayNode = function(rayObj, vertices, speed)
{
    var lut_color = this.lut.getColor(speed);
    rayObj.lut_color = lut_color;
    console.log("lut_color: " + lut_color);

    var multilinematerial = new THREE.LineBasicMaterial({color: lut_color});
    var multilinegeometry = new THREE.Geometry();
    multilinegeometry.vertices = vertices;
    var multiline = new THREE.Line(multilinegeometry, multilinematerial);
    rayObj.add(multiline);

    rayObj.vtx = vertices; // WARNING: this is a hidden field hacky stuff, but only used in the function putScatterPoints
    annotcubes = new THREE.Object3D();
    rayObj.add(annotcubes);
    rayObj.annotcubes = annotcubes;

    this.raynodes.push(rayObj);
    rayObj.visible = false;
}
// add a component origo
//		compname  -  component name for the reference dict
Main.prototype.addComponent = function(compname)
{
    var comp = new THREE.Object3D();
    this.rootnode.add(comp);
    this.compnodes[compname] = comp;
    return comp;
}
// returns a cyclically new color intended for a component draw node
//
Main.prototype.getNextComponentColor = function()
{
    this.iColor += 1;
    if (this.iColor >= this.compColors.length)
    {
        this.iColor = 0;
    }
    return this.compColors[this.iColor];
}
// show all rays in this.raynodes
//
Main.prototype.showAllRays = function()
{
    for (var i = 0; i < this.raynodes.length; i++)
    {
        node = this.raynodes[i];
        // NOTE: the following two lines should instead hvae been implemented via a function called "update" and set in a proper manor
        if (node.parent == null) { this.putScatterPoints(node); this.rootnode.add(node); };
        this.raynodes[i].visible = true;
    }
}
// remove all rays in this.raynodes from the scene graph
//
Main.prototype.hideAllRays = function()
{
    for (var i = 0; i < this.raynodes.length; i++)
    {
        this.raynodes[i].visible = false;
    }
}
// hide all annotation cubes on all rays
//
Main.prototype.hideCubes = function()
{
    for (var i = 0; i < this.raynodes.length; i++)
    {
        ray = this.raynodes[i];
        ray.annotcubes.visible = false;
    }
}
// show all annotation cubes on all rays
//
Main.prototype.showCubes = function()
{
    for (var i = 0; i < this.raynodes.length; i++)
    {
        ray = this.raynodes[i];
        ray.annotcubes.visible = true;
    }
}
// iterates to attach the next ray in the global ray sequence
//
Main.prototype.showNextRay = function()
{
    if (this.raynodes.length == 0) { return; }

    lastRay = this.iRay;
    this.iRay += 1;
    if (this.iRay >= this.raynodes.length)
    {
        this.iRay = 0;
    }
    newnode = this.raynodes[this.iRay];
    // NOTE: the following two lines should instead hvae been implemented via a function called "update" and set in a proper manor
    if (node.parent == null) { this.putScatterPoints(newnode); this.rootnode.add(newnode); };
    newnode.visible = true;

    this.raynodes[lastRay].visible = false;
}
// iterates to attach the next ray in the global ray sequence
//
Main.prototype.hideRay = function(idx)
{
    if (this.raynodes.length == 0) { return idx; }

    if (idx >= this.raynodes.length | idx < 0)
    {
        throw "idx out of range"
    }
    this.raynodes[idx].visible = false;
}
// iterates to attach the next ray in the global ray sequence
//
Main.prototype.showRay = function(idx)
{
    if (this.raynodes.length == 0) { return idx; }

    if (idx >= this.raynodes.length | idx < 0)
    {
        throw "idx out of range"
    }
    //this.rootnode.add(this.raynodes[idx]);
    node = this.raynodes[idx];
    // NOTE: the following two lines should instead hvae been implemented via a function called "update" and set in a proper maner
    if (node.parent == null) { this.putScatterPoints(node); this.rootnode.add(node); };
    node.visible = true;
}

// loads json data into the scene graph
//      instrdata    -  json instrument definition data
//      particledata  -  json particle ray data
//      main         -  reference to scene wrapper
var TraceLoader = function(instrdata, particledata, main)
{
    this.main = main;
    this.instrdata = instrdata;
    this.particledata = particledata;
}
// synchronously load instrument component draw data
//
TraceLoader.prototype.loadInstr = function()
{
    var main = this.main;

    // utility function
    var drawFunc = function(call, parentnode, color)
    {
        key = call['key'];
        args = call['args'];

        if (key == 'multiline') {
            main.addMultiLine(args, parentnode, color);
        }
        if (key == 'line') {
            main.addMultiLine(args, parentnode, color);
        }
        if (key == 'circle') {
            main.addCircle(args[0], args[1], args[2], args[3], args[4], parentnode, color);
        }
    }

    // INSTRUMENT
    var instr = this.instrdata;
    var instname = instr['name'];
    var abspath = instr['abspath'];

    // COMPONENTS
    var comps = instr['components'];
    var comp;
    var comp_node;
    var compname;
    var m4;
    var acolor;

    for (var i = 0; i < comps.length; i++)
    {
        comp = comps[i];

        compname = comp['name'];
        m4 = comp['m4'];
        comp_node = main.addComponent(compname);
        acolor = main.getNextComponentColor();

        drawcalls = comp['drawcalls']
        var call;
        for (var j = 0; j < drawcalls.length; j++) {
            call = drawcalls[j];

            drawFunc(call, comp_node, acolor);
        }

        comp_matrix = new THREE.Matrix4();
        comp_matrix.set(m4[0], m4[1], m4[2], m4[3], m4[4], m4[5], m4[6], m4[7], m4[8], m4[9], m4[10], m4[11], m4[12], m4[13], m4[14], m4[15]);
        comp_node.applyMatrix(comp_matrix);
    }

    // BOUNDING BOX
    var bb = instr['boundingbox'];
    var drawcalls = bb['drawcalls'];
    var call;
    for (var i = 0; i < drawcalls.length; i++)
    {
        call = drawcalls[i];
        key = call['key'];

        drawFunc(call, main.bbnode, 0x808080);
    }
}
// load particle rays
//
TraceLoader.prototype.loadParticles = function()
{
    var main = this.main;

    // set Lut stuff
    main.setLutRange(this.particledata['vmin'], this.particledata['vmax']);

    // RAYS
    var rays = this.particledata['rays'];
    var ray;
    var aVertices;
    for (var i = 0; i < rays.length; i++) {
        ray = rays[i];

        rayobj = new THREE.Object3D();
        aVertices = [];
        var aCompVertices;
        var compname;
        var speed = ray['speed'];

        var groups =  ray['groups'];
        var group;
        for (var j = 0; j < groups.length; j++) {
            group = groups[j];
            compname = group['compname'];
            aCompVertices = [];

            // PARTICLE STATES
            var events = group['events'];
            var localstate;
            var args;
            for (var k = 0; k < events.length; k++) {
                localstate = events[k];

                args = localstate['args'];
                aCompVertices.push(new THREE.Vector3(args[0], args[1], args[2]));
            }

            // transform these vertices by component matrix and add to vertex container for this ray
            if (main.compnodes[compname])
            {
                aVertices = aVertices.concat(transformPoints(aCompVertices, main.compnodes[compname].matrix));
            }
        }
        // add ray as a multiline
        main.addRayNode(rayobj, aVertices, speed);
    }
}
//  program controller
//      campos_x/y/z  -  determines initial camera position, this is used with --inspect
var Controller = function(campos_x, campos_y, campos_z, box_lst, invert_canvas)
{
    this.camPosInitial = new THREE.Vector3(campos_x, campos_y, campos_z);
    this.invert_canvas = invert_canvas;
    this.box_lst = box_lst; // this would be [x_min, x_max, ...]
    this.main = new Main();
    this.loader = new TraceLoader(MCDATA_instrdata, MCDATA_particledata, this.main);
    this.numRays = MCDATA_particledata["numrays"]
    this.viewmodel = new ViewModel(numRays = this.numRays);
}
Controller.prototype.setUpdateGuiFunc = function(updateGuiFunc)
{
    this.updateGuiFunc = updateGuiFunc;
}
//  main program execution loops are set up here
//
Controller.prototype.run = function()
{
    // init mcdisplay
    this.main.init(this.camPosInitial, this.invert_canvas);

    // execution loops
    var _this = this;
    var renderLoop = function()
    {
    	requestAnimationFrame(renderLoop);
    	_this.main.renderer.render(_this.main.scene, _this.main.camera);
    }
    var busy = false;
    var dataLoop = function()
    {
        // add a single-threaded critical section / lock
        if (busy == true) { console.log("busy"); return; }
        busy = true;

        // show/hide ray nodes
        if (_this.viewmodel.playBack == PlayBack.ALL)
        {
            _this.showAllRays();
            return;
        }
        if (_this.viewmodel.playBack == PlayBack.RUN)
        {
            _this.incSingleRay();
        }
        if (_this.viewmodel.playBack == PlayBack.PAUSE)
        {
            // TODO: use viewmodel.updateVersion to skip this step if needed
            _this.showCurrentRay();
        }
        if (viewmodel.getDisplayMode() == DisplayMode.SINGLE)
        {
            _this.hidePrevRays();
        }

        // set bounding box visible property
        _this.main.bbnode.visible = viewmodel.getShowBoundingBox();

        // set scatter cubes on/off
        if (viewmodel.getShowScatterCubes())
        {
            _this.main.showCubes();
        }
        else
        {
            _this.main.hideCubes();
        }

        setTimeout(dataLoop, 1000/_this.viewmodel.raysPrSec);

        busy = false;
    }
    var updateGuiLoop = function()
    {
        setTimeout(updateGuiLoop, 100);
        _this.updateGuiFunc();
    }

    // initiate timed execution loops
    renderLoop();
    dataLoop();
    updateGuiLoop();

    // load data - possibly heavy
    this.loader.loadInstr();
    this.loader.loadParticles();
    //this.main.setNativeBoundingBox();

    // set pause playback mode now after everything else, in case of zero or one rays
    if (this.numRays <= 1) { this.viewmodel.playBack = PlayBack.PAUSE }
}
Controller.prototype.showAllRays = function()
{
    this.main.showAllRays();
}
Controller.prototype.hidePrevRays = function()
{
    var arrLast = this.viewmodel.shiftNonFirstRayIdxs();
    for (var i = 0; i < arrLast.length; i++)
    {
        this.main.hideRay(arrLast[i]);
    }
}
Controller.prototype.incSingleRay = function()
{
    this.viewmodel.setRayIdx(this.viewmodel.getRayIdx() + 1)
    var retidx = this.main.showRay(this.viewmodel.getRayIdx());
}
Controller.prototype.showCurrentRay = function()
{
    this.main.showRay(this.viewmodel.getRayIdx());
}
Controller.prototype.setViewTop = function()
{
    var box = this.box_lst;
    var x = - 0.01; // must not be zero due to the euler angle rotation lock stuff
    var y = (box[5] - box[4])/2;
    var z = (box[5] - box[4])/2;
    this.main.camera.up = new THREE.Vector3(1, 0, 0);
    this.main.setCameraView(new THREE.Vector3(x, y, z));
    this.main.camera.up = new THREE.Vector3(0, 1, 0);
    // next line just to be safe
    this.main.camera.updateMatrix();
}
Controller.prototype.setViewSide = function()
{
    var box = this.box_lst;
    var x = - (box[5] - box[4])/2;
    var y = 0;
    var z = (box[5] - box[4])/2;
    this.main.setCameraView(new THREE.Vector3(x, y, z));
    // next line just to be safe
    this.main.camera.updateMatrix();
}
Controller.prototype.setViewHome = function()
{
    this.main.setCameraView(this.camPosInitial);
}

//  enum for playback state
//
PlayBack = { RUN : 0, PAUSE : 1, ALL : 3 };

//  displaymode e.g. keep rays animated or display one at the time
//
DisplayMode = { SINGLE : 0, KEEP : 1 }

//  viewmodel for keeping control data free of the gui
//
var ViewModel = function(numRays)
{
    this.playBack = PlayBack.RUN;
    this.displayMode = DisplayMode.SINGLE;

    this.showBoundingbox = true;
    this.showScattCubes = false;

    this.numRays = numRays;
    this.rayIdx = [-1];
    this.raysPrSec = 5;

    this.updateVersion = 0; // incremented on update
}
ViewModel.prototype.getUpdateVersion = function()
{
    return this.updateVersion;
}
ViewModel.prototype.setShowBoundingBox = function(bbOnOff)
{
    if (bbOnOff)
    {
        this.showBoundingbox = true;
    }
    else
    {
        this.showBoundingbox = false;
    }
    this.updateVersion += 1;
}
ViewModel.prototype.getShowBoundingBox = function()
{
    return this.showBoundingbox;
}
ViewModel.prototype.setShowScatterCubes = function(scOnOff)
{
    if (scOnOff)
    {
        this.showScattCubes = true;
    }
    else
    {
        this.showScattCubes = false;
    }
    this.updateVersion += 1;
}
ViewModel.prototype.getShowScatterCubes = function()
{
    return this.showScattCubes;
}
ViewModel.prototype.setRayIdx = function(idx)
{
    // the socalled "js modulus bug" for negative numbers, using ((n % m) + m) % m;
    idx = ((idx % this.numRays) + this.numRays) % this.numRays;
    this.rayIdx.push(idx);
    this.updateVersion += 1;
}
ViewModel.prototype.getRayIdx = function(idx)
{
    return this.rayIdx[this.rayIdx.length-1];
}
ViewModel.prototype.shiftNonFirstRayIdxs = function()
{
    if (this.rayIdx.length <= 1)
    {
        return [];
    }

    var arr = [];
    for (var i=0; i < this.rayIdx.length -1; i++)
    {
        arr.push(this.rayIdx.shift());
    }
    this.updateVersion += 1;
    return arr;
}
ViewModel.prototype.setPlayBack = function(playBack)
{
    this.playBack = playBack;
    this.updateVersion += 1;
}
ViewModel.prototype.getPlayBack = function()
{
    return this.playBack;
}
ViewModel.prototype.setDisplayMode = function(displayMode)
{
    this.displayMode = displayMode;
    this.updateVersion += 1;
}
ViewModel.prototype.getDisplayMode = function()
{
    return this.displayMode;
}