forked from eriksson/X
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
1322 lines (1046 loc) · 40 KB
/
parser.js
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
991
992
993
994
995
996
997
998
999
1000
/*
*
* xxxxxxx xxxxxxx
* x:::::x x:::::x
* x:::::x x:::::x
* x:::::xx:::::x
* x::::::::::x
* x::::::::x
* x::::::::x
* x::::::::::x
* x:::::xx:::::x
* x:::::x x:::::x
* x:::::x x:::::x
* THE xxxxxxx xxxxxxx TOOLKIT
*
* http://www.goXTK.com
*
* Copyright (c) 2012 The X Toolkit Developers <[email protected]>
*
* The X Toolkit (XTK) is licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
*
*
*
* CREDITS:
*
* - the endianness handling was inspired by
* Ilmari Heikkinen's DataStream.js (https://github.com/kig/DataStream.js)
* THANKS!!
*
*/
// provides
goog.provide('X.parser');
// requires
goog.require('X.base');
goog.require('X.event');
goog.require('X.texture');
goog.require('X.triplets');
goog.require('goog.vec.Vec4');
goog.require('goog.vec.Vec3');
goog.require('goog.vec.Mat4');
/**
* Create a parser for binary or ascii data.
*
* @constructor
* @extends X.base
*/
X.parser = function() {
//
// call the standard constructor of X.base
goog.base(this);
//
// class attributes
/**
* @inheritDoc
* @const
*/
this._classname = 'parser';
/**
* The data.
*
* @type {?ArrayBuffer}
* @protected
*/
this._data = null;
/**
* The pointer to the current byte.
*
* @type {!number}
* @protected
*/
this._dataPointer = 0;
/**
* The native endianness flag. Based on
* https://github.com/kig/DataStream.js/blob/master/DataStream.js
*
* @type {!boolean}
* @protected
*/
this._nativeLittleEndian = new Int8Array(new Int16Array([ 1 ]).buffer)[0] > 0;
/**
* The data-specific endianness flag.
*
* @type {!boolean}
* @protected
*/
this._littleEndian = true;
/**
* The min value of the last parsing attempt.
*
* @type {!number}
* @protected
*/
this._lastMin = -Infinity;
/**
* The max value of the last parsing attempt.
*
* @type {!number}
* @protected
*/
this._lastMax = Infinity;
};
// inherit from X.base
goog.inherits(X.parser, X.base);
/**
* Parse data and configure the given object. When complete, a
* X.parser.ModifiedEvent is fired.
*
* @param {!X.base}
* container A container which holds the loaded data. This can be an
* X.object as well.
* @param {!X.object}
* object The object to configure.
* @param {!ArrayBuffer}
* data The data to parse.
* @param {*}
* flag An additional flag.
* @throws {Error}
* An exception if something goes wrong.
*/
X.parser.prototype.parse = function(container, object, data, flag) {
throw new Error('The function parse() should be overloaded.');
};
//
// PARSE FUNCTIONS
//
//
/**
* Get the min and max values of an array.
*
* @param {Array|Uint8Array|Uint16Array|Uint32Array|null}
* data The data array to analyze.
* @return {!Array} An array with length 2 containing the [min, max] values.
*/
X.parser.prototype.arrayMinMax = function(data) {
var _min = Infinity;
var _max = -Infinity;
// buffer the length
var _datasize = data.length;
var i = 0;
for (i = 0; i < _datasize; i++) {
if(!isNaN(data[i])) {
var _value = data[i];
_min = Math.min(_min, _value);
_max = Math.max(_max, _value);
}
}
return [ _min, _max ];
};
/**
* Create a string from a bunch of UChars. This replaces a
* String.fromCharCode.apply call and therefor supports more platforms (like the
* Android stock browser).
*
* @param {!Array|Uint8Array}
* array The Uint8Array.
* @param {?number=}
* start The start position. If undefined, use the whole array.
* @param {?number=}
* end The end position. If undefined, use the whole array.
* @return {string} The created string.
*/
X.parser.prototype.parseChars = function(array, start, end) {
// without borders, use the whole array
if (start === undefined) {
start = 0;
}
if (end === undefined) {
end = array.length;
}
var _output = '';
// create and append the chars
var i = 0;
for (i = start; i < end; ++i) {
_output += String.fromCharCode(array[i]);
}
return _output;
};
/**
* Jump to a position in the byte stream.
*
* @param {!number}
* position The new offset.
*/
X.parser.prototype.jumpTo = function(position) {
this._dataPointer = position;
};
/**
* Scan binary data relative to the internal position in the byte stream.
*
* @param {!string}
* type The data type to scan, f.e.
* 'uchar','schar','ushort','sshort','uint','sint','float'
* @param {!number=}
* chunks The number of chunks to scan. By default, 1.
*/
X.parser.prototype.scan = function(type, chunks) {
if (!goog.isDefAndNotNull(chunks)) {
chunks = 1;
}
var _chunkSize = 1;
var _array_type = Uint8Array;
switch (type) {
// 1 byte data types
case 'uchar':
break;
case 'schar':
_array_type = Int8Array;
break;
// 2 byte data types
case 'ushort':
_array_type = Uint16Array;
_chunkSize = 2;
break;
case 'sshort':
_array_type = Int16Array;
_chunkSize = 2;
break;
// 4 byte data types
case 'uint':
_array_type = Uint32Array;
_chunkSize = 4;
break;
case 'sint':
_array_type = Int32Array;
_chunkSize = 4;
break;
case 'float':
_array_type = Float32Array;
_chunkSize = 4;
break;
case 'complex':
_array_type = Float64Array;
_chunkSize = 8;
break;
case 'double':
_array_type = Float64Array;
_chunkSize = 8;
break;
}
// increase the data pointer in-place
var _bytes = new _array_type(this._data.slice(this._dataPointer,
this._dataPointer += chunks * _chunkSize));
// if required, flip the endianness of the bytes
if (this._nativeLittleEndian != this._littleEndian) {
// we need to flip here since the format doesn't match the native endianness
_bytes = this.flipEndianness(_bytes, _chunkSize);
}
if (chunks == 1) {
// if only one chunk was requested, just return one value
return _bytes[0];
}
// return the byte array
return _bytes;
};
/**
* Flips typed array endianness in-place. Based on
* https://github.com/kig/DataStream.js/blob/master/DataStream.js.
*
* @param {!Object}
* array Typed array to flip.
* @param {!number}
* chunkSize The size of each element.
* @return {!Object} The converted typed array.
*/
X.parser.prototype.flipEndianness = function(array, chunkSize) {
var u8 = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
for ( var i = 0; i < array.byteLength; i += chunkSize) {
for ( var j = i + chunkSize - 1, k = i; j > k; j--, k++) {
var tmp = u8[k];
u8[k] = u8[j];
u8[j] = tmp;
}
}
return array;
};
/**
* Compute RAS bonding box fron IJK dimensions.
*
* @param {!Float32Array} IJKToRAS The IJK to RAS transformation.
* @param {!Array} MRIdim The IJK dimensions.
*
* @return The RAS bounding box.
* @static
*/
X.parser.computeRASBBox = function(IJKToRAS, MRIdim){
var _rasBB = [Number.MAX_VALUE, -Number.MAX_VALUE,
Number.MAX_VALUE, -Number.MAX_VALUE,
Number.MAX_VALUE, -Number.MAX_VALUE];
var ijkTarget = goog.vec.Vec4.createFloat32FromValues(0, 0, 0, 1);
var rasResult = goog.vec.Vec4.createFloat32();
goog.vec.Mat4.multVec4(IJKToRAS, ijkTarget, rasResult);
_rasBB[0] = rasResult[0] < _rasBB[0] ? rasResult[0] : _rasBB[0];
_rasBB[1] = rasResult[0] > _rasBB[1] ? rasResult[0] : _rasBB[1];
_rasBB[2] = rasResult[1] < _rasBB[2] ? rasResult[1] : _rasBB[2];
_rasBB[3] = rasResult[1] > _rasBB[3] ? rasResult[1] : _rasBB[3];
_rasBB[4] = rasResult[2] < _rasBB[4] ? rasResult[2] : _rasBB[4];
_rasBB[5] = rasResult[2] > _rasBB[5] ? rasResult[2] : _rasBB[5];
ijkTarget = goog.vec.Vec4.createFloat32FromValues(0, 0, MRIdim[2]-1, 1);
goog.vec.Mat4.multVec4(IJKToRAS, ijkTarget, rasResult);
_rasBB[0] = rasResult[0] < _rasBB[0] ? rasResult[0] : _rasBB[0];
_rasBB[1] = rasResult[0] > _rasBB[1] ? rasResult[0] : _rasBB[1];
_rasBB[2] = rasResult[1] < _rasBB[2] ? rasResult[1] : _rasBB[2];
_rasBB[3] = rasResult[1] > _rasBB[3] ? rasResult[1] : _rasBB[3];
_rasBB[4] = rasResult[2] < _rasBB[4] ? rasResult[2] : _rasBB[4];
_rasBB[5] = rasResult[2] > _rasBB[5] ? rasResult[2] : _rasBB[5];
ijkTarget = goog.vec.Vec4.createFloat32FromValues(0, MRIdim[1]-1, 0, 1);
goog.vec.Mat4.multVec4(IJKToRAS, ijkTarget, rasResult);
_rasBB[0] = rasResult[0] < _rasBB[0] ? rasResult[0] : _rasBB[0];
_rasBB[1] = rasResult[0] > _rasBB[1] ? rasResult[0] : _rasBB[1];
_rasBB[2] = rasResult[1] < _rasBB[2] ? rasResult[1] : _rasBB[2];
_rasBB[3] = rasResult[1] > _rasBB[3] ? rasResult[1] : _rasBB[3];
_rasBB[4] = rasResult[2] < _rasBB[4] ? rasResult[2] : _rasBB[4];
_rasBB[5] = rasResult[2] > _rasBB[5] ? rasResult[2] : _rasBB[5];
ijkTarget = goog.vec.Vec4.createFloat32FromValues(MRIdim[0]-1, 0, 0, 1);
goog.vec.Mat4.multVec4(IJKToRAS, ijkTarget, rasResult);
_rasBB[0] = rasResult[0] < _rasBB[0] ? rasResult[0] : _rasBB[0];
_rasBB[1] = rasResult[0] > _rasBB[1] ? rasResult[0] : _rasBB[1];
_rasBB[2] = rasResult[1] < _rasBB[2] ? rasResult[1] : _rasBB[2];
_rasBB[3] = rasResult[1] > _rasBB[3] ? rasResult[1] : _rasBB[3];
_rasBB[4] = rasResult[2] < _rasBB[4] ? rasResult[2] : _rasBB[4];
_rasBB[5] = rasResult[2] > _rasBB[5] ? rasResult[2] : _rasBB[5];
ijkTarget = goog.vec.Vec4.createFloat32FromValues(MRIdim[0]-1, MRIdim[1]-1, 0, 1);
goog.vec.Mat4.multVec4(IJKToRAS, ijkTarget, rasResult);
_rasBB[0] = rasResult[0] < _rasBB[0] ? rasResult[0] : _rasBB[0];
_rasBB[1] = rasResult[0] > _rasBB[1] ? rasResult[0] : _rasBB[1];
_rasBB[2] = rasResult[1] < _rasBB[2] ? rasResult[1] : _rasBB[2];
_rasBB[3] = rasResult[1] > _rasBB[3] ? rasResult[1] : _rasBB[3];
_rasBB[4] = rasResult[2] < _rasBB[4] ? rasResult[2] : _rasBB[4];
_rasBB[5] = rasResult[2] > _rasBB[5] ? rasResult[2] : _rasBB[5];
ijkTarget = goog.vec.Vec4.createFloat32FromValues(MRIdim[0]-1, 0, MRIdim[2]-1, 1);
goog.vec.Mat4.multVec4(IJKToRAS, ijkTarget, rasResult);
_rasBB[0] = rasResult[0] < _rasBB[0] ? rasResult[0] : _rasBB[0];
_rasBB[1] = rasResult[0] > _rasBB[1] ? rasResult[0] : _rasBB[1];
_rasBB[2] = rasResult[1] < _rasBB[2] ? rasResult[1] : _rasBB[2];
_rasBB[3] = rasResult[1] > _rasBB[3] ? rasResult[1] : _rasBB[3];
_rasBB[4] = rasResult[2] < _rasBB[4] ? rasResult[2] : _rasBB[4];
_rasBB[5] = rasResult[2] > _rasBB[5] ? rasResult[2] : _rasBB[5];
ijkTarget = goog.vec.Vec4.createFloat32FromValues(0, MRIdim[1]-1, MRIdim[2]-1, 1);
goog.vec.Mat4.multVec4(IJKToRAS, ijkTarget, rasResult);
_rasBB[0] = rasResult[0] < _rasBB[0] ? rasResult[0] : _rasBB[0];
_rasBB[1] = rasResult[0] > _rasBB[1] ? rasResult[0] : _rasBB[1];
_rasBB[2] = rasResult[1] < _rasBB[2] ? rasResult[1] : _rasBB[2];
_rasBB[3] = rasResult[1] > _rasBB[3] ? rasResult[1] : _rasBB[3];
_rasBB[4] = rasResult[2] < _rasBB[4] ? rasResult[2] : _rasBB[4];
_rasBB[5] = rasResult[2] > _rasBB[5] ? rasResult[2] : _rasBB[5];
ijkTarget = goog.vec.Vec4.createFloat32FromValues(MRIdim[0]-1, MRIdim[1]-1, MRIdim[2]-1, 1);
goog.vec.Mat4.multVec4(IJKToRAS, ijkTarget, rasResult);
_rasBB[0] = rasResult[0] < _rasBB[0] ? rasResult[0] : _rasBB[0];
_rasBB[1] = rasResult[0] > _rasBB[1] ? rasResult[0] : _rasBB[1];
_rasBB[2] = rasResult[1] < _rasBB[2] ? rasResult[1] : _rasBB[2];
_rasBB[3] = rasResult[1] > _rasBB[3] ? rasResult[1] : _rasBB[3];
_rasBB[4] = rasResult[2] < _rasBB[4] ? rasResult[2] : _rasBB[4];
_rasBB[5] = rasResult[2] > _rasBB[5] ? rasResult[2] : _rasBB[5];
return _rasBB;
}
/**
* Create the IJK volume.
*
* @param {!Float32Array} _data The target Bounding Box.
* @param {!Array} _dims The line origin.
* @param {!number} _max The maximum intensity value.
*
* @return The IJK volume and the IJK 'normalized' volume.
* @static
*/
X.parser.createIJKVolume = function(_data, _dims, _max, _min){
// initiate variables
// allocate images
var _image = new Array(_dims[2]);
var _nb_pix_per_slice = _dims[1] * _dims[0];
var _pix_value = 0;
var _i = 0;
var _j = 0;
var _k = 0;
var _data_pointer = 0;
for (_k = 0; _k < _dims[2]; _k++) {
// get current slice
var _current_k = _data.subarray(_k * (_nb_pix_per_slice), (_k + 1)
* _nb_pix_per_slice);
// initiate data pointer
_data_pointer = 0;
// allocate images
_image[_k] = new Array(_dims[1]);
for (_j = 0; _j < _dims[1]; _j++) {
// allocate images
_image[_k][_j] = new _data.constructor(_dims[0]);
for (_i = 0; _i < _dims[0]; _i++) {
_pix_value = _current_k[_data_pointer];
_image[_k][_j][_i] = _pix_value;
_data_pointer++;
}
}
}
return _image;
};
/**
* Compute intersection between line and a bounding box
*
* @param {!Array} _bbox The target Bounding Box.
* @param {!Float32Array} _sliceOrigin The line origin.
* @param {!Float32Array} _sliceNormal The line normal.
*
* @return The intersection points and the 'non-intersection' points.
* @static
*/
X.parser.intersectionBBoxLine = function(_bbox, _sliceOrigin, _sliceNormal){
var _solutionsIn = new Array();
var _solutionsOut = new Array();
// xmin, xmax, ymin, ymax, zmin, zmax
for(var _i = 0; _i < 6; _i++) {
var _i2 = Math.floor(_i/2);
var _i3 = (_i2 + 1)%3;
var _i4 = (_i2 + 2)%3;
var _j1 = (2 + (2*_i2))%6;
var _j2 = (4 + (2*_i2))%6;
var _dir = _i2;
var _sol0 = _bbox[_i];
var _invN1 = 1/_sliceNormal[_i2];
var _t = (_sol0 - _sliceOrigin[_i2])*_invN1;
// if _t infinity, we are //
if(_t != Infinity && _t != -Infinity) {
var _sol1 = _sliceOrigin[_i3] + _sliceNormal[_i3]*_t;
var _sol2 = _sliceOrigin[_i4] + _sliceNormal[_i4]*_t;
// in range?
if( (_sol1 >= _bbox[_j1] && _sol1 <= _bbox[_j1+1]) &&
(_sol2 >= _bbox[_j2] && _sol2 <= _bbox[_j2+1])) {
var _sol = new Array();
_sol[_i2] = _bbox[_i];
_sol[_i3] = _sol1;
_sol[_i4] = _sol2;
_solutionsIn.push(_sol);
}
else {
var _sol = new Array();
_sol[_i2] = _bbox[_i];
_sol[_i3] = _sol1;
_sol[_i4] = _sol2;
_solutionsOut.push(_sol);
}
}
}
return [_solutionsIn, _solutionsOut];
};
/**
* Compute intersection between plane and a bounding box
*
* @param {!Array} _bbox The target Bounding Box.
* @param {!Float32Array} _sliceOrigin The plane origin.
* @param {!Float32Array} _sliceNormal The plane normal.
*
* @return The intersection points and the 'non-intersection' points.
* @static
*/
X.parser.intersectionBBoxPlane = function(_bbox, _sliceOrigin, _sliceNormal){
var _solutionsIn = new Array();
var _solutionsOut = new Array();
// xmin, xmax, ymin, ymax, zmin, zmax
for(var _i = 0; _i < 6; _i++) {
//
var _i2 = Math.floor(_i/2);
var _i3 = (_i2 + 1)%3;
var _i4 = (_i2 + 2)%3;
var _j3 = (4 + (2*_i2))%6;
for(var _j = 0; _j < 2; _j++) {
var _j2 = (2 + _j + (2*_i2))%6;
var _solution = (-(
_sliceNormal[_i2]*(_bbox[_i] - _sliceOrigin[_i2])
+
_sliceNormal[_i3]*(_bbox[_j2] - _sliceOrigin[_i3])
)
/
_sliceNormal[_i4]
)
+
_sliceOrigin[_i4]
;
if((_solution >= _bbox[_j3] && _solution <= _bbox[_j3+1])
||
(_solution <= _bbox[_j3] && _solution >= _bbox[_j3+1])) {
var _sol = new Array();
_sol[_i2] = _bbox[_i];
_sol[_i3] = _bbox[_j2];
_sol[_i4] = _solution;
_solutionsIn.push(_sol);
}
else{
var _sol = new Array();
_sol[_i2] = _bbox[_i];
_sol[_i3] = _bbox[_j2];
_sol[_i4] = _solution;
_solutionsOut.push(_sol);
}
}
}
return [_solutionsIn, _solutionsOut];
};
/**
* Get XYToRAS transform and its inverse.
*
* @param {!Float32Array} _sliceNormal The slice normal.
* @param {!Float32Array} _XYNormal The XY normal.
*
* @return The XY to RAS transform and its inverse.
* @static
*/
X.parser.xyrasTransform = function(_sliceNormal, _XYNormal){
var _RASToXY = goog.vec.Mat4.createFloat32Identity();
// no rotation needed if we are in the z plane already
if(!goog.vec.Vec3.equals(_sliceNormal,_XYNormal)) {
var _cp = _sliceNormal[2];
var _teta = Math.acos(_cp);
var _r = goog.vec.Vec3.createFloat32();
goog.vec.Vec3.cross(_sliceNormal, _XYNormal, _r);
goog.vec.Vec3.normalize(_r, _r);
var a = Math.cos(_teta/2);
var b = Math.sin(_teta/2)*_r[0];
var c = Math.sin(_teta/2)*_r[1];
var d = Math.sin(_teta/2)*_r[2];
goog.vec.Mat4.setRowValues(_RASToXY,
0,
(a*a+b*b-c*c-d*d),
2*(b*c-a*d),
2*(b*d+a*c),
0
);
goog.vec.Mat4.setRowValues(_RASToXY,
1,
2*(b*c+a*d),
(a*a+c*c-b*b-d*d),
2*(c*d-a*b),
0
);
goog.vec.Mat4.setRowValues(_RASToXY,
2,
2*(b*d-a*c ),
2*(c*d+a*b),
(a*a+d*d-c*c-b*b),
0
);
var sin0 = _sliceNormal[1];
var cos0 = _sliceNormal[0];
var rotM = goog.vec.Mat4.createFloat32FromValues(cos0, -sin0, 0, 0, sin0, cos0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
goog.vec.Mat4.multMat(rotM, _RASToXY, _RASToXY);
}
var _XYToRAS = goog.vec.Mat4.createFloat32();
goog.vec.Mat4.invert(_RASToXY, _XYToRAS);
return [_RASToXY, _XYToRAS];
};
/**
* Get bounding box given a point cloud.
*
* @param {!Array} _solutionsXY The slice origin in RAS space.
*
* @return The bounding box.
* @static
*/
X.parser.xyBBox = function(_solutionsXY){
var _xyBBox = [Number.MAX_VALUE, -Number.MAX_VALUE,
Number.MAX_VALUE, -Number.MAX_VALUE,
Number.MAX_VALUE, -Number.MAX_VALUE];
var i = 0;
for (i = 0; i < _solutionsXY.length; ++i) {
if(_solutionsXY[i][0] < _xyBBox[0]) {
_xyBBox[0] = _solutionsXY[i][0];
}
if(_solutionsXY[i][0] > _xyBBox[1]) {
_xyBBox[1] = _solutionsXY[i][0];
}
if(_solutionsXY[i][1] < _xyBBox[2]) {
_xyBBox[2] = _solutionsXY[i][1];
}
if(_solutionsXY[i][1] > _xyBBox[3]) {
_xyBBox[3] = _solutionsXY[i][1];
}
if(_solutionsXY[i][2] < _xyBBox[4]) {
_xyBBox[4] = _solutionsXY[i][2];
}
if(_solutionsXY[i][2] > _xyBBox[5]) {
_xyBBox[5] = _solutionsXY[i][2];
}
}
return _xyBBox;
};
/**
* Perform the actual reslicing
*
* @param {!Float32Array} _sliceOrigin The slice origin in RAS space.
* @param {!Float32Array} _sliceNormal The slice normal direction.
* @param {!Array} _color The slice border color.
* @param {!Array} _bbox The volume bounding box.
* @param {!Array} _IJKVolume The IJK volume.
* @param {!X.object} object The X.volume.
* @param {!boolean} hasLabelMap Volume has labelmap attached.
* @param {goog.structs.Map} colorTable Associated color table.
*
* @return The target slice.
* @static
*/
X.parser.reslice2 = function(_sliceOrigin, _sliceXYSpacing, _sliceNormal, _color, _bbox, _IJKVolume, object, hasLabelMap, colorTable){
var sliceXY = new X.slice();
// normalize slice normal (just in case)
goog.vec.Vec3.normalize(_sliceNormal, _sliceNormal);
// ------------------------------------------
// GET INTERSECTION BOUNDING BOX/PLANE
// ------------------------------------------
//_bbox is only this slice bounding box
var _solutions = X.parser.intersectionBBoxPlane(_bbox,_sliceOrigin, _sliceNormal);
var _solutionsIn = _solutions[0];
// ------------------------------------------
// MOVE TO 2D SPACE
// ------------------------------------------
var _XYNormal = goog.vec.Vec3.createFloat32FromValues(0, 0, 1);
var _XYRASTransform = X.parser.xyrasTransform(_sliceNormal, _XYNormal);
var _RASToXY = _XYRASTransform[0];
var _XYToRAS = _XYRASTransform[1];
// Apply transform to each point!
var _solutionsXY = new Array();
for (var i = 0; i < _solutionsIn.length; ++i) {
var _rasIntersection = goog.vec.Vec4.createFloat32FromValues(_solutionsIn[i][0], _solutionsIn[i][1], _solutionsIn[i][2], 1);
var _xyIntersection = goog.vec.Vec4.createFloat32();
goog.vec.Mat4.multVec4(_RASToXY, _rasIntersection, _xyIntersection);
_solutionsXY.push([_xyIntersection[0], _xyIntersection[1], _xyIntersection[2]]);
}
// right
var _right = goog.vec.Vec3.createFloat32FromValues(1, 0, 0);
var _rright = goog.vec.Vec3.createFloat32();
goog.vec.Mat4.multVec3(_XYToRAS, _right, _rright);
// up
var _up = goog.vec.Vec3.createFloat32FromValues(0, 1, 0);
var _rup = goog.vec.Vec3.createFloat32();
goog.vec.Mat4.multVec3(_XYToRAS, _up, _rup);
// get XY bounding box!
var _xyBBox = X.parser.xyBBox(_solutionsXY);
var _xyCenter = goog.vec.Vec4.createFloat32FromValues(_xyBBox[0] + (_xyBBox[1] - _xyBBox[0])/2,_xyBBox[2] + (_xyBBox[3] - _xyBBox[2])/2, _xyBBox[4] + (_xyBBox[5] - _xyBBox[4])/2,0);
var _RASCenter = goog.vec.Vec4.createFloat32();
goog.vec.Mat4.multMat(_XYToRAS,_xyCenter, _RASCenter);
var _wmin = Math.floor(_xyBBox[0]);
var _wmax = Math.ceil(_xyBBox[1]);
// window.console.log(_xyBBox);
// var _wmin = _xyBBox[0];
// var _wmax = _xyBBox[1];
// if the slice only has to intersections with the volume BBox
// (can happens if the slice is right on the edge of the volume)
if(_wmin == _wmax){
_wmax++;
}
var _swidth = _wmax - _wmin;
var _hmin = Math.floor(_xyBBox[2]);
var _hmax = Math.ceil(_xyBBox[3]);
// var _hmin = _xyBBox[2];
// var _hmax = _xyBBox[3];
// if the slice only has to intersections with the volume BBox
// (can happens if the slice is right on the edge of the volume)
if(_hmin == _hmax){
_hmax++;
}
var _sheight = _hmax - _hmin;
var _resX = _sliceXYSpacing[0];
var _resY = _sliceXYSpacing[1];
// not sure why?
var _epsilon = 0.0000001;
// How many pixels are we expecting the raw data
var _cswidth = Math.ceil(_swidth/_resX);
var _csheight = Math.ceil(_sheight/_resY);
var _csize = _cswidth*_csheight;
var textureSize = 4 * _csize;
var textureForCurrentSlice = new Uint8Array(textureSize);
var pixelTexture = new X.texture();
pixelTexture._rawDataWidth = _cswidth;
pixelTexture._rawDataHeight = _csheight;
var _indexIJK = goog.vec.Vec4.createFloat32();
var _indexXY = goog.vec.Vec4.createFloat32FromValues(0, 0, _xyBBox[4], 1);
var _XYToIJK = goog.vec.Mat4.createFloat32();
goog.vec.Mat4.multMat(object._RASToIJK,_XYToRAS, _XYToIJK);
var _he = _hmax - _epsilon;
var _we = _wmax - _epsilon;
var _p = 0;
var _iWidth = 0;
var _iHeight = 0;
var j = _hmin;
var i = _wmin;
for (j = _hmin; j <= _he; j+=_resY) {
_iHeight++;
_iWidth = 0;
_indexXY[1] = j;
i = _wmin;
for (i = _wmin; i <= _we; i+=_resX) {
_iWidth++;
//
_indexXY[0] = i;
// convert to RAS
// convert to IJK
goog.vec.Mat4.multVec4(_XYToIJK, _indexXY, _indexIJK);
// get value if there is a match, trnasparent if no match!
var textureStartIndex = _p * 4;
var _k = Math.floor(_indexIJK[2]);
var _j = Math.floor(_indexIJK[1]);
var _i = Math.floor(_indexIJK[0]);
if( (0 <= _i) && (_i < object._dimensions[0] ) &&
(0 <= _j) && (_j < object._dimensions[1] ) &&
(0 <= _k) && (_k < object._dimensions[2] )) {
// map to 0 if necessary
var pixval = _IJKVolume[_k][_j][_i];
if (colorTable) {
// color table!
var lookupValue = colorTable.get(pixval);
// check for out of range and use the last label value in this case
var pixelValue_r = pixelValue_g = pixelValue_b = lookupValue[1] || 0;
var pixelValue_a = lookupValue[4];
}
else {
// normalization should not happen here, only in the shaders/canvas??
var pixelValue_r = pixelValue_g = pixelValue_b = 255 * ((pixval - object._min )/ (object._max - object._min));
var pixelValue_a = 255;
}
textureForCurrentSlice[textureStartIndex] = pixelValue_r;
textureForCurrentSlice[++textureStartIndex] = pixelValue_g;
textureForCurrentSlice[++textureStartIndex] = pixelValue_b;
textureForCurrentSlice[++textureStartIndex] = pixelValue_a;
}
else {
textureForCurrentSlice[textureStartIndex] = 0;
textureForCurrentSlice[++textureStartIndex] = 0;
textureForCurrentSlice[++textureStartIndex] = 0;
textureForCurrentSlice[++textureStartIndex] = 0;
}
_p++;
}
}
// setup slice texture
pixelTexture._rawData = textureForCurrentSlice;
sliceXY._texture = pixelTexture;
// setup slice spacial information
sliceXY._xyBBox = _xyBBox;
sliceXY._xyIBox = _solutionsXY;
sliceXY._XYToRAS = _XYToRAS;
sliceXY._XYToIJK = _XYToIJK;
sliceXY._RASToXY = _RASToXY;
sliceXY._sliceOrigin = _sliceOrigin;
sliceXY._hmin = _hmin;
sliceXY._hmax = _hmax;
sliceXY._wmin = _wmin;
sliceXY._wmax = _wmax;
sliceXY._iWidth = _iWidth;
sliceXY._iHeight = _iHeight;
sliceXY._widthSpacing = _resX;
sliceXY._width = _swidth;
sliceXY._heightSpacing = _resY;
sliceXY._height = _sheight;
sliceXY._center = [_RASCenter[0], _RASCenter[1], _RASCenter[2]];
// ADD SPACING OFFSET to center so it matches meshes/tracts perfectly
sliceXY._front = [_sliceNormal[0], _sliceNormal[1], _sliceNormal[2]];
sliceXY._right= [_rright[0], _rright[1], _rright[2]];
sliceXY._up = [_rup[0], _rup[1], _rup[2]];
// more styling
sliceXY._visible = false;
sliceXY._volume = /** @type {X.volume} */(object);
// for labelmaps, don't create the borders since this would create them 2x
// hasLabelMap == true means we are the volume
// hasLabelMap == false means we are the labelmap
if (goog.isDefAndNotNull(object._volume) && !hasLabelMap) {
sliceXY._borders = false;
}
else{
sliceXY._borders = true;
}
sliceXY._borderColor = _color;
// create slice
sliceXY.create_();
// update visibility (has to be done after slice creation)
sliceXY._visible = false;
return sliceXY;
};
/**
* Setup basic information for given slice orientation
*
* @param {!number} _index The slice orientation index: 0=SAGITTAL, 1=CORONAL, 2=AXIAL.
* @param {!Float32Array} _sliceOrigin The slice origin in RAS space.
* @param {!Float32Array} _sliceNormal The slice normal direction.
* @param {!X.object} object The X.volume.
*/
X.parser.prototype.updateSliceInfo = function(_index, _sliceOrigin, _sliceNormal, object){
// ------------------------------------------