123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- import {
- Matrix3,
- Matrix4,
- Vector3
- } from '../../../build/three.module.js';
- import { VolumeSlice } from '../misc/VolumeSlice.js';
- function Volume( xLength, yLength, zLength, type, arrayBuffer ) {
- if ( arguments.length > 0 ) {
-
- this.xLength = Number( xLength ) || 1;
-
- this.yLength = Number( yLength ) || 1;
-
- this.zLength = Number( zLength ) || 1;
-
- this.axisOrder = [ 'x', 'y', 'z' ];
-
- switch ( type ) {
- case 'Uint8' :
- case 'uint8' :
- case 'uchar' :
- case 'unsigned char' :
- case 'uint8_t' :
- this.data = new Uint8Array( arrayBuffer );
- break;
- case 'Int8' :
- case 'int8' :
- case 'signed char' :
- case 'int8_t' :
- this.data = new Int8Array( arrayBuffer );
- break;
- case 'Int16' :
- case 'int16' :
- case 'short' :
- case 'short int' :
- case 'signed short' :
- case 'signed short int' :
- case 'int16_t' :
- this.data = new Int16Array( arrayBuffer );
- break;
- case 'Uint16' :
- case 'uint16' :
- case 'ushort' :
- case 'unsigned short' :
- case 'unsigned short int' :
- case 'uint16_t' :
- this.data = new Uint16Array( arrayBuffer );
- break;
- case 'Int32' :
- case 'int32' :
- case 'int' :
- case 'signed int' :
- case 'int32_t' :
- this.data = new Int32Array( arrayBuffer );
- break;
- case 'Uint32' :
- case 'uint32' :
- case 'uint' :
- case 'unsigned int' :
- case 'uint32_t' :
- this.data = new Uint32Array( arrayBuffer );
- break;
- case 'longlong' :
- case 'long long' :
- case 'long long int' :
- case 'signed long long' :
- case 'signed long long int' :
- case 'int64' :
- case 'int64_t' :
- case 'ulonglong' :
- case 'unsigned long long' :
- case 'unsigned long long int' :
- case 'uint64' :
- case 'uint64_t' :
- throw 'Error in Volume constructor : this type is not supported in JavaScript';
- break;
- case 'Float32' :
- case 'float32' :
- case 'float' :
- this.data = new Float32Array( arrayBuffer );
- break;
- case 'Float64' :
- case 'float64' :
- case 'double' :
- this.data = new Float64Array( arrayBuffer );
- break;
- default :
- this.data = new Uint8Array( arrayBuffer );
- }
- if ( this.data.length !== this.xLength * this.yLength * this.zLength ) {
- throw 'Error in Volume constructor, lengths are not matching arrayBuffer size';
- }
- }
-
- this.spacing = [ 1, 1, 1 ];
-
- this.offset = [ 0, 0, 0 ];
-
- this.matrix = new Matrix3();
- this.matrix.identity();
-
-
- var lowerThreshold = - Infinity;
- Object.defineProperty( this, 'lowerThreshold', {
- get: function () {
- return lowerThreshold;
- },
- set: function ( value ) {
- lowerThreshold = value;
- this.sliceList.forEach( function ( slice ) {
- slice.geometryNeedsUpdate = true;
- } );
- }
- } );
-
- var upperThreshold = Infinity;
- Object.defineProperty( this, 'upperThreshold', {
- get: function () {
- return upperThreshold;
- },
- set: function ( value ) {
- upperThreshold = value;
- this.sliceList.forEach( function ( slice ) {
- slice.geometryNeedsUpdate = true;
- } );
- }
- } );
-
- this.sliceList = [];
-
- }
- Volume.prototype = {
- constructor: Volume,
- /**
- * @member {Function} getData Shortcut for data[access(i,j,k)]
- * @memberof Volume
- * @param {number} i First coordinate
- * @param {number} j Second coordinate
- * @param {number} k Third coordinate
- * @returns {number} value in the data array
- */
- getData: function ( i, j, k ) {
- return this.data[ k * this.xLength * this.yLength + j * this.xLength + i ];
- },
- /**
- * @member {Function} access compute the index in the data array corresponding to the given coordinates in IJK system
- * @memberof Volume
- * @param {number} i First coordinate
- * @param {number} j Second coordinate
- * @param {number} k Third coordinate
- * @returns {number} index
- */
- access: function ( i, j, k ) {
- return k * this.xLength * this.yLength + j * this.xLength + i;
- },
- /**
- * @member {Function} reverseAccess Retrieve the IJK coordinates of the voxel corresponding of the given index in the data
- * @memberof Volume
- * @param {number} index index of the voxel
- * @returns {Array} [x,y,z]
- */
- reverseAccess: function ( index ) {
- var z = Math.floor( index / ( this.yLength * this.xLength ) );
- var y = Math.floor( ( index - z * this.yLength * this.xLength ) / this.xLength );
- var x = index - z * this.yLength * this.xLength - y * this.xLength;
- return [ x, y, z ];
- },
-
- map: function ( functionToMap, context ) {
- var length = this.data.length;
- context = context || this;
- for ( var i = 0; i < length; i ++ ) {
- this.data[ i ] = functionToMap.call( context, this.data[ i ], i, this.data );
- }
- return this;
- },
-
- extractPerpendicularPlane: function ( axis, RASIndex ) {
- var iLength,
- jLength,
- sliceAccess,
- planeMatrix = ( new Matrix4() ).identity(),
- volume = this,
- planeWidth,
- planeHeight,
- firstSpacing,
- secondSpacing,
- positionOffset,
- IJKIndex;
- var axisInIJK = new Vector3(),
- firstDirection = new Vector3(),
- secondDirection = new Vector3();
- var dimensions = new Vector3( this.xLength, this.yLength, this.zLength );
- switch ( axis ) {
- case 'x' :
- axisInIJK.set( 1, 0, 0 );
- firstDirection.set( 0, 0, - 1 );
- secondDirection.set( 0, - 1, 0 );
- firstSpacing = this.spacing[ this.axisOrder.indexOf( 'z' ) ];
- secondSpacing = this.spacing[ this.axisOrder.indexOf( 'y' ) ];
- IJKIndex = new Vector3( RASIndex, 0, 0 );
- planeMatrix.multiply( ( new Matrix4() ).makeRotationY( Math.PI / 2 ) );
- positionOffset = ( volume.RASDimensions[ 0 ] - 1 ) / 2;
- planeMatrix.setPosition( new Vector3( RASIndex - positionOffset, 0, 0 ) );
- break;
- case 'y' :
- axisInIJK.set( 0, 1, 0 );
- firstDirection.set( 1, 0, 0 );
- secondDirection.set( 0, 0, 1 );
- firstSpacing = this.spacing[ this.axisOrder.indexOf( 'x' ) ];
- secondSpacing = this.spacing[ this.axisOrder.indexOf( 'z' ) ];
- IJKIndex = new Vector3( 0, RASIndex, 0 );
- planeMatrix.multiply( ( new Matrix4() ).makeRotationX( - Math.PI / 2 ) );
- positionOffset = ( volume.RASDimensions[ 1 ] - 1 ) / 2;
- planeMatrix.setPosition( new Vector3( 0, RASIndex - positionOffset, 0 ) );
- break;
- case 'z' :
- default :
- axisInIJK.set( 0, 0, 1 );
- firstDirection.set( 1, 0, 0 );
- secondDirection.set( 0, - 1, 0 );
- firstSpacing = this.spacing[ this.axisOrder.indexOf( 'x' ) ];
- secondSpacing = this.spacing[ this.axisOrder.indexOf( 'y' ) ];
- IJKIndex = new Vector3( 0, 0, RASIndex );
- positionOffset = ( volume.RASDimensions[ 2 ] - 1 ) / 2;
- planeMatrix.setPosition( new Vector3( 0, 0, RASIndex - positionOffset ) );
- break;
- }
- firstDirection.applyMatrix4( volume.inverseMatrix ).normalize();
- firstDirection.argVar = 'i';
- secondDirection.applyMatrix4( volume.inverseMatrix ).normalize();
- secondDirection.argVar = 'j';
- axisInIJK.applyMatrix4( volume.inverseMatrix ).normalize();
- iLength = Math.floor( Math.abs( firstDirection.dot( dimensions ) ) );
- jLength = Math.floor( Math.abs( secondDirection.dot( dimensions ) ) );
- planeWidth = Math.abs( iLength * firstSpacing );
- planeHeight = Math.abs( jLength * secondSpacing );
- IJKIndex = Math.abs( Math.round( IJKIndex.applyMatrix4( volume.inverseMatrix ).dot( axisInIJK ) ) );
- var base = [ new Vector3( 1, 0, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ) ];
- var iDirection = [ firstDirection, secondDirection, axisInIJK ].find( function ( x ) {
- return Math.abs( x.dot( base[ 0 ] ) ) > 0.9;
- } );
- var jDirection = [ firstDirection, secondDirection, axisInIJK ].find( function ( x ) {
- return Math.abs( x.dot( base[ 1 ] ) ) > 0.9;
- } );
- var kDirection = [ firstDirection, secondDirection, axisInIJK ].find( function ( x ) {
- return Math.abs( x.dot( base[ 2 ] ) ) > 0.9;
- } );
- sliceAccess = function ( i, j ) {
- var accessI, accessJ, accessK;
- var si = ( iDirection === axisInIJK ) ? IJKIndex : ( iDirection.argVar === 'i' ? i : j );
- var sj = ( jDirection === axisInIJK ) ? IJKIndex : ( jDirection.argVar === 'i' ? i : j );
- var sk = ( kDirection === axisInIJK ) ? IJKIndex : ( kDirection.argVar === 'i' ? i : j );
-
- var accessI = ( iDirection.dot( base[ 0 ] ) > 0 ) ? si : ( volume.xLength - 1 ) - si;
- var accessJ = ( jDirection.dot( base[ 1 ] ) > 0 ) ? sj : ( volume.yLength - 1 ) - sj;
- var accessK = ( kDirection.dot( base[ 2 ] ) > 0 ) ? sk : ( volume.zLength - 1 ) - sk;
- return volume.access( accessI, accessJ, accessK );
- };
- return {
- iLength: iLength,
- jLength: jLength,
- sliceAccess: sliceAccess,
- matrix: planeMatrix,
- planeWidth: planeWidth,
- planeHeight: planeHeight
- };
- },
-
- extractSlice: function ( axis, index ) {
- var slice = new VolumeSlice( this, index, axis );
- this.sliceList.push( slice );
- return slice;
- },
-
- repaintAllSlices: function () {
- this.sliceList.forEach( function ( slice ) {
- slice.repaint();
- } );
- return this;
- },
-
- computeMinMax: function () {
- var min = Infinity;
- var max = - Infinity;
-
- var datasize = this.data.length;
- var i = 0;
- for ( i = 0; i < datasize; i ++ ) {
- if ( ! isNaN( this.data[ i ] ) ) {
- var value = this.data[ i ];
- min = Math.min( min, value );
- max = Math.max( max, value );
- }
- }
- this.min = min;
- this.max = max;
- return [ min, max ];
- }
- };
- export { Volume };
|