Skip to content

Commit

Permalink
Examples: Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdoob committed Jun 17, 2021
1 parent 4e0669a commit 2f6358d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 87 deletions.
66 changes: 21 additions & 45 deletions examples/js/loaders/NRRDLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,80 +359,56 @@
volume.windowLow = min;
volume.windowHigh = max; // get the image dimensions


// get the image dimensions
volume.dimensions = [ headerObject.sizes[ 0 ], headerObject.sizes[ 1 ], headerObject.sizes[ 2 ] ];
volume.xLength = volume.dimensions[ 0 ];
volume.yLength = volume.dimensions[ 1 ];
volume.zLength = volume.dimensions[ 2 ];
volume.zLength = volume.dimensions[ 2 ]; // Identify axis order in the space-directions matrix from the header if possible.

// Identify axis order in the space-directions matrix from the header if possible.
if (headerObject.vectors) {
const xIndex = headerObject.vectors.findIndex(vector => vector[0] !== 0);
const yIndex = headerObject.vectors.findIndex(vector => vector[1] !== 0);
const zIndex = headerObject.vectors.findIndex(vector => vector[2] !== 0);
if ( headerObject.vectors ) {

const xIndex = headerObject.vectors.findIndex( vector => vector[ 0 ] !== 0 );
const yIndex = headerObject.vectors.findIndex( vector => vector[ 1 ] !== 0 );
const zIndex = headerObject.vectors.findIndex( vector => vector[ 2 ] !== 0 );
const axisOrder = [];
axisOrder[xIndex] = 'x';
axisOrder[yIndex] = 'y';
axisOrder[zIndex] = 'z';
axisOrder[ xIndex ] = 'x';
axisOrder[ yIndex ] = 'y';
axisOrder[ zIndex ] = 'z';
volume.axisOrder = axisOrder;
}
else {
volume.axisOrder = ['x', 'y', 'z'];
}

// spacing
} else {

volume.axisOrder = [ 'x', 'y', 'z' ];

} // spacing


const spacingX = new THREE.Vector3().fromArray( headerObject.vectors[ 0 ] ).length();
const spacingY = new THREE.Vector3().fromArray( headerObject.vectors[ 1 ] ).length();
const spacingZ = new THREE.Vector3().fromArray( headerObject.vectors[ 2 ] ).length();
volume.spacing = [ spacingX, spacingY, spacingZ ];
volume.spacing = [ spacingX, spacingY, spacingZ ]; // Create IJKtoRAS matrix

// Create IJKtoRAS matrix
volume.matrix = new THREE.Matrix4();

const transitionMatrix = new THREE.Matrix4();

if ( headerObject.space === 'left-posterior-superior' ) {

transitionMatrix.set(
- 1, 0, 0, 0,
0, -1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 );
transitionMatrix.set( - 1, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 );

} else if ( headerObject.space === 'left-anterior-superior' ) {

transitionMatrix.set(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1 );
transitionMatrix.set( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, - 1, 0, 0, 0, 0, 1 );

}


if ( ! headerObject.vectors ) {

volume.matrix.set(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 );
volume.matrix.set( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 );

} else {

const v = headerObject.vectors;

const ijk_to_transition = ( new THREE.Matrix4() ).set(
v[ 0 ][ 0 ], v[ 1 ][ 0 ], v[ 2 ][ 0 ], 0,
v[ 0 ][ 1 ], v[ 1 ][ 1 ], v[ 2 ][ 1 ], 0,
v[ 0 ][ 2 ], v[ 1 ][ 2 ], v[ 2 ][ 2 ], 0,
0, 0, 0, 1
)

const transition_to_ras = (new THREE.Matrix4()).multiplyMatrices( ijk_to_transition, transitionMatrix );

const ijk_to_transition = new THREE.Matrix4().set( v[ 0 ][ 0 ], v[ 1 ][ 0 ], v[ 2 ][ 0 ], 0, v[ 0 ][ 1 ], v[ 1 ][ 1 ], v[ 2 ][ 1 ], 0, v[ 0 ][ 2 ], v[ 1 ][ 2 ], v[ 2 ][ 2 ], 0, 0, 0, 0, 1 );
const transition_to_ras = new THREE.Matrix4().multiplyMatrices( ijk_to_transition, transitionMatrix );
volume.matrix = transition_to_ras;

}
Expand Down
22 changes: 10 additions & 12 deletions examples/js/misc/Volume.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @param {ArrayBuffer} arrayBuffer The buffer with volume data
*/

var Volume = function ( xLength, yLength, zLength, type, arrayBuffer ) {
function Volume( xLength, yLength, zLength, type, arrayBuffer ) {

if ( arguments.length > 0 ) {

Expand All @@ -31,13 +31,11 @@
*/

this.zLength = Number( zLength ) || 1;

/**
* @member {Array<string>} The order of the Axis dictated by the NRRD header
*/
* @member {Array<string>} The order of the Axis dictated by the NRRD header
*/

this.axisOrder = [ 'x', 'y', 'z' ];

/**
* @member {TypedArray} data Data of the volume
*/
Expand Down Expand Up @@ -209,7 +207,7 @@
* @member {Array} RASDimensions This array holds the dimensions of the volume in the RAS space
*/

};
}

Volume.prototype = {
constructor: Volume,
Expand Down Expand Up @@ -313,8 +311,8 @@
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') ];
firstSpacing = this.spacing[ this.axisOrder.indexOf( 'z' ) ];
secondSpacing = this.spacing[ this.axisOrder.indexOf( 'y' ) ];
IJKIndex = new THREE.Vector3( RASIndex, 0, 0 );
planeMatrix.multiply( new THREE.Matrix4().makeRotationY( Math.PI / 2 ) );
positionOffset = ( volume.RASDimensions[ 0 ] - 1 ) / 2;
Expand All @@ -325,8 +323,8 @@
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') ];
firstSpacing = this.spacing[ this.axisOrder.indexOf( 'x' ) ];
secondSpacing = this.spacing[ this.axisOrder.indexOf( 'z' ) ];
IJKIndex = new THREE.Vector3( 0, RASIndex, 0 );
planeMatrix.multiply( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
positionOffset = ( volume.RASDimensions[ 1 ] - 1 ) / 2;
Expand All @@ -338,8 +336,8 @@
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') ];
firstSpacing = this.spacing[ this.axisOrder.indexOf( 'x' ) ];
secondSpacing = this.spacing[ this.axisOrder.indexOf( 'y' ) ];
IJKIndex = new THREE.Vector3( 0, 0, RASIndex );
positionOffset = ( volume.RASDimensions[ 2 ] - 1 ) / 2;
planeMatrix.setPosition( new THREE.Vector3( 0, 0, RASIndex - positionOffset ) );
Expand Down
4 changes: 2 additions & 2 deletions examples/js/misc/VolumeSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @see Volume
*/

var VolumeSlice = function ( volume, index, axis ) {
function VolumeSlice( volume, index, axis ) {

var slice = this;
/**
Expand Down Expand Up @@ -96,7 +96,7 @@
* @returns {Number} the index corresponding to the voxel in volume.data of the given position in the slice
*/

};
}

VolumeSlice.prototype = {
constructor: VolumeSlice,
Expand Down
41 changes: 23 additions & 18 deletions examples/jsm/loaders/NRRDLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,22 @@ class NRRDLoader extends Loader {
volume.zLength = volume.dimensions[ 2 ];

// Identify axis order in the space-directions matrix from the header if possible.
if (headerObject.vectors) {
const xIndex = headerObject.vectors.findIndex(vector => vector[0] !== 0);
const yIndex = headerObject.vectors.findIndex(vector => vector[1] !== 0);
const zIndex = headerObject.vectors.findIndex(vector => vector[2] !== 0);
if ( headerObject.vectors ) {

const xIndex = headerObject.vectors.findIndex( vector => vector[ 0 ] !== 0 );
const yIndex = headerObject.vectors.findIndex( vector => vector[ 1 ] !== 0 );
const zIndex = headerObject.vectors.findIndex( vector => vector[ 2 ] !== 0 );

const axisOrder = [];
axisOrder[xIndex] = 'x';
axisOrder[yIndex] = 'y';
axisOrder[zIndex] = 'z';
axisOrder[ xIndex ] = 'x';
axisOrder[ yIndex ] = 'y';
axisOrder[ zIndex ] = 'z';
volume.axisOrder = axisOrder;
}
else {
volume.axisOrder = ['x', 'y', 'z'];

} else {

volume.axisOrder = [ 'x', 'y', 'z' ];

}

// spacing
Expand All @@ -396,17 +399,19 @@ class NRRDLoader extends Loader {

transitionMatrix.set(
- 1, 0, 0, 0,
0, -1, 0, 0,
0, - 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 );
0, 0, 0, 1
);

} else if ( headerObject.space === 'left-anterior-superior' ) {

transitionMatrix.set(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1 );
0, 0, - 1, 0,
0, 0, 0, 1
);

}

Expand All @@ -423,22 +428,22 @@ class NRRDLoader extends Loader {

const v = headerObject.vectors;

const ijk_to_transition = ( new Matrix4() ).set(
const ijk_to_transition = new Matrix4().set(
v[ 0 ][ 0 ], v[ 1 ][ 0 ], v[ 2 ][ 0 ], 0,
v[ 0 ][ 1 ], v[ 1 ][ 1 ], v[ 2 ][ 1 ], 0,
v[ 0 ][ 2 ], v[ 1 ][ 2 ], v[ 2 ][ 2 ], 0,
0, 0, 0, 1
)
);

const transition_to_ras = (new Matrix4()).multiplyMatrices( ijk_to_transition, transitionMatrix );
const transition_to_ras = new Matrix4().multiplyMatrices( ijk_to_transition, transitionMatrix );

volume.matrix = transition_to_ras;

}

volume.inverseMatrix = new Matrix4();
volume.inverseMatrix.copy( volume.matrix ).invert();
volume.RASDimensions = ( new Vector3( volume.xLength, volume.yLength, volume.zLength ) ).applyMatrix4( volume.matrix ).round().toArray().map( Math.abs );
volume.RASDimensions = new Vector3( volume.xLength, volume.yLength, volume.zLength ).applyMatrix4( volume.matrix ).round().toArray().map( Math.abs );

// .. and set the default threshold
// only if the threshold was not already set
Expand Down
16 changes: 8 additions & 8 deletions examples/jsm/misc/Volume.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { VolumeSlice } from '../misc/VolumeSlice.js';
* @param {string} type The type of data (uint8, uint16, ...)
* @param {ArrayBuffer} arrayBuffer The buffer with volume data
*/
var Volume = function ( xLength, yLength, zLength, type, arrayBuffer ) {
function Volume( xLength, yLength, zLength, type, arrayBuffer ) {

if ( arguments.length > 0 ) {

Expand Down Expand Up @@ -196,7 +196,7 @@ var Volume = function ( xLength, yLength, zLength, type, arrayBuffer ) {
* @member {Array} RASDimensions This array holds the dimensions of the volume in the RAS space
*/

};
}

Volume.prototype = {

Expand Down Expand Up @@ -304,8 +304,8 @@ Volume.prototype = {
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') ];
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 ) );
Expand All @@ -316,8 +316,8 @@ Volume.prototype = {
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') ];
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 ) );
Expand All @@ -329,8 +329,8 @@ Volume.prototype = {
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') ];
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;
Expand Down
4 changes: 2 additions & 2 deletions examples/jsm/misc/VolumeSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
* @param {string} [axis='z'] For now only 'x', 'y' or 'z' but later it will change to a normal vector
* @see Volume
*/
var VolumeSlice = function ( volume, index, axis ) {
function VolumeSlice( volume, index, axis ) {

var slice = this;
/**
Expand Down Expand Up @@ -95,7 +95,7 @@ var VolumeSlice = function ( volume, index, axis ) {
*/


};
}

VolumeSlice.prototype = {

Expand Down

0 comments on commit 2f6358d

Please sign in to comment.