Volume.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. * This class had been written to handle the output of the NRRD loader.
  3. * It contains a volume of data and informations about it.
  4. * For now it only handles 3 dimensional data.
  5. * See the webgl_loader_nrrd.html example and the loaderNRRD.js file to see how to use this class.
  6. * @class
  7. * @author Valentin Demeusy / https://github.com/stity
  8. * @param {number} xLength Width of the volume
  9. * @param {number} yLength Length of the volume
  10. * @param {number} zLength Depth of the volume
  11. * @param {string} type The type of data (uint8, uint16, ...)
  12. * @param {ArrayBuffer} arrayBuffer The buffer with volume data
  13. */
  14. THREE.Volume = function( xLength, yLength, zLength, type, arrayBuffer ) {
  15. if ( arguments.length > 0 ) {
  16. /**
  17. * @member {number} xLength Width of the volume in the IJK coordinate system
  18. */
  19. this.xLength = Number( xLength ) || 1;
  20. /**
  21. * @member {number} yLength Height of the volume in the IJK coordinate system
  22. */
  23. this.yLength = Number( yLength ) || 1;
  24. /**
  25. * @member {number} zLength Depth of the volume in the IJK coordinate system
  26. */
  27. this.zLength = Number( zLength ) || 1;
  28. /**
  29. * @member {TypedArray} data Data of the volume
  30. */
  31. switch ( type ) {
  32. case 'Uint8' :
  33. case 'uint8' :
  34. case 'uchar' :
  35. case 'unsigned char' :
  36. case 'uint8_t' :
  37. this.data = new Uint8Array( arrayBuffer );
  38. break;
  39. case 'Int8' :
  40. case 'int8' :
  41. case 'signed char' :
  42. case 'int8_t' :
  43. this.data = new Int8Array( arrayBuffer );
  44. break;
  45. case 'Int16' :
  46. case 'int16' :
  47. case 'short' :
  48. case 'short int' :
  49. case 'signed short' :
  50. case 'signed short int' :
  51. case 'int16_t' :
  52. this.data = new Int16Array( arrayBuffer );
  53. break;
  54. case 'Uint16' :
  55. case 'uint16' :
  56. case 'ushort' :
  57. case 'unsigned short' :
  58. case 'unsigned short int' :
  59. case 'uint16_t' :
  60. this.data = new Uint16Array( arrayBuffer );
  61. break;
  62. case 'Int32' :
  63. case 'int32' :
  64. case 'int' :
  65. case 'signed int' :
  66. case 'int32_t' :
  67. this.data = new Int32Array( arrayBuffer );
  68. break;
  69. case 'Uint32' :
  70. case 'uint32' :
  71. case 'uint' :
  72. case 'unsigned int' :
  73. case 'uint32_t' :
  74. this.data = new Uint32Array( arrayBuffer );
  75. break;
  76. case 'longlong' :
  77. case 'long long' :
  78. case 'long long int' :
  79. case 'signed long long' :
  80. case 'signed long long int' :
  81. case 'int64' :
  82. case 'int64_t' :
  83. case 'ulonglong' :
  84. case 'unsigned long long' :
  85. case 'unsigned long long int' :
  86. case 'uint64' :
  87. case 'uint64_t' :
  88. throw 'Error in THREE.Volume constructor : this type is not supported in JavaScript';
  89. break;
  90. case 'Float32' :
  91. case 'float32' :
  92. case 'float' :
  93. this.data = new Float32Array( arrayBuffer );
  94. break;
  95. case 'Float64' :
  96. case 'float64' :
  97. case 'double' :
  98. this.data = new Float64Array( arrayBuffer );
  99. break;
  100. default :
  101. this.data = new Uint8Array( arrayBuffer );
  102. }
  103. if ( this.data.length !== this.xLength * this.yLength * this.zLength ) {
  104. throw 'Error in THREE.Volume constructor, lengths are not matching arrayBuffer size';
  105. }
  106. }
  107. /**
  108. * @member {Array} spacing Spacing to apply to the volume from IJK to RAS coordinate system
  109. */
  110. this.spacing = [ 1, 1, 1 ];
  111. /**
  112. * @member {Array} offset Offset of the volume in the RAS coordinate system
  113. */
  114. this.offset = [ 0, 0, 0 ];
  115. /**
  116. * @member {THREE.Martrix3} matrix The IJK to RAS matrix
  117. */
  118. this.matrix = new THREE.Matrix3();
  119. this.matrix.identity();
  120. /**
  121. * @member {THREE.Martrix3} inverseMatrix The RAS to IJK matrix
  122. */
  123. /**
  124. * @member {number} lowerThreshold The voxels with values under this threshold won't appear in the slices.
  125. * If changed, geometryNeedsUpdate is automatically set to true on all the slices associated to this volume
  126. */
  127. var lowerThreshold = - Infinity;
  128. Object.defineProperty( this, 'lowerThreshold', {
  129. get : function() {
  130. return lowerThreshold;
  131. },
  132. set : function( value ) {
  133. lowerThreshold = value;
  134. this.sliceList.forEach( function( slice ) {
  135. slice.geometryNeedsUpdate = true
  136. } );
  137. }
  138. } );
  139. /**
  140. * @member {number} upperThreshold The voxels with values over this threshold won't appear in the slices.
  141. * If changed, geometryNeedsUpdate is automatically set to true on all the slices associated to this volume
  142. */
  143. var upperThreshold = Infinity;
  144. Object.defineProperty( this, 'upperThreshold', {
  145. get : function() {
  146. return upperThreshold;
  147. },
  148. set : function( value ) {
  149. upperThreshold = value;
  150. this.sliceList.forEach( function( slice ) {
  151. slice.geometryNeedsUpdate = true;
  152. } );
  153. }
  154. } );
  155. /**
  156. * @member {Array} sliceList The list of all the slices associated to this volume
  157. */
  158. this.sliceList = [];
  159. /**
  160. * @member {Array} RASDimensions This array holds the dimensions of the volume in the RAS space
  161. */
  162. };
  163. THREE.Volume.prototype = {
  164. constructor : THREE.Volume,
  165. /**
  166. * @member {Function} getData Shortcut for data[access(i,j,k)]
  167. * @memberof THREE.Volume
  168. * @param {number} i First coordinate
  169. * @param {number} j Second coordinate
  170. * @param {number} k Third coordinate
  171. * @returns {number} value in the data array
  172. */
  173. getData : function( i, j, k ) {
  174. return this.data[ k * this.xLength * this.yLength + j * this.xLength + i ];
  175. },
  176. /**
  177. * @member {Function} access compute the index in the data array corresponding to the given coordinates in IJK system
  178. * @memberof THREE.Volume
  179. * @param {number} i First coordinate
  180. * @param {number} j Second coordinate
  181. * @param {number} k Third coordinate
  182. * @returns {number} index
  183. */
  184. access : function( i, j, k ) {
  185. return k * this.xLength * this.yLength + j * this.xLength + i;
  186. },
  187. /**
  188. * @member {Function} reverseAccess Retrieve the IJK coordinates of the voxel corresponding of the given index in the data
  189. * @memberof THREE.Volume
  190. * @param {number} index index of the voxel
  191. * @returns {Array} [x,y,z]
  192. */
  193. reverseAccess : function( index ) {
  194. var z = Math.floor( index / ( this.yLength * this.xLength ) );
  195. var y = Math.floor( ( index - z * this.yLength * this.xLength ) / this.xLength );
  196. var x = index - z * this.yLength * this.xLength - y * this.xLength;
  197. return [ x, y, z ];
  198. },
  199. /**
  200. * @member {Function} map Apply a function to all the voxels, be careful, the value will be replaced
  201. * @memberof THREE.Volume
  202. * @param {Function} functionToMap A function to apply to every voxel, will be called with the following parameters :
  203. * value of the voxel
  204. * index of the voxel
  205. * the data (TypedArray)
  206. * @param {Object} context You can specify a context in which call the function, default if this Volume
  207. * @returns {THREE.Volume} this
  208. */
  209. map : function( functionToMap, context ) {
  210. var length = this.data.length;
  211. context = context || this;
  212. for ( var i = 0; i < length; i ++ ) {
  213. this.data[ i ] = functionToMap.call( context, this.data[ i ], i, this.data );
  214. }
  215. return this;
  216. },
  217. /**
  218. * @member {Function} extractPerpendicularPlane Compute the orientation of the slice and returns all the information relative to the geometry such as sliceAccess, the plane matrix (orientation and position in RAS coordinate) and the dimensions of the plane in both coordinate system.
  219. * @memberof THREE.Volume
  220. * @param {string} axis the normal axis to the slice 'x' 'y' or 'z'
  221. * @param {number} index the index of the slice
  222. * @returns {Object} an object containing all the usefull information on the geometry of the slice
  223. */
  224. extractPerpendicularPlane : function( axis, RASIndex ) {
  225. var iLength,
  226. jLength,
  227. sliceAccess,
  228. planeMatrix = ( new THREE.Matrix4() ).identity(),
  229. volume = this,
  230. planeWidth,
  231. planeHeight,
  232. firstSpacing,
  233. secondSpacing,
  234. positionOffset,
  235. IJKIndex;
  236. var axisInIJK = new THREE.Vector3(),
  237. firstDirection = new THREE.Vector3(),
  238. secondDirection = new THREE.Vector3();
  239. var dimensions = new THREE.Vector3( this.xLength, this.yLength, this.zLength );
  240. switch ( axis ) {
  241. case 'x' :
  242. axisInIJK.set( 1, 0, 0 );
  243. firstDirection.set( 0, 0, - 1 );
  244. secondDirection.set( 0, - 1, 0 );
  245. firstSpacing = this.spacing[ 2 ];
  246. secondSpacing = this.spacing[ 1 ];
  247. IJKIndex = new THREE.Vector3( RASIndex, 0, 0 );
  248. planeMatrix.multiply( ( new THREE.Matrix4() ).makeRotationY( Math.PI / 2 ) );
  249. positionOffset = ( volume.RASDimensions[ 0 ] - 1 ) / 2;
  250. planeMatrix.setPosition( new THREE.Vector3( RASIndex - positionOffset, 0, 0 ) );
  251. break;
  252. case 'y' :
  253. axisInIJK.set( 0, 1, 0 );
  254. firstDirection.set( 1, 0, 0 );
  255. secondDirection.set( 0, 0, 1 );
  256. firstSpacing = this.spacing[ 0 ];
  257. secondSpacing = this.spacing[ 2 ];
  258. IJKIndex = new THREE.Vector3( 0, RASIndex, 0 );
  259. planeMatrix.multiply( ( new THREE.Matrix4() ).makeRotationX( - Math.PI / 2 ) );
  260. positionOffset = ( volume.RASDimensions[ 1 ] - 1 ) / 2;
  261. planeMatrix.setPosition( new THREE.Vector3( 0, RASIndex - positionOffset, 0 ) );
  262. break;
  263. case 'z' :
  264. default :
  265. axisInIJK.set( 0, 0, 1 );
  266. firstDirection.set( 1, 0, 0 );
  267. secondDirection.set( 0, - 1, 0 );
  268. firstSpacing = this.spacing[ 0 ];
  269. secondSpacing = this.spacing[ 1 ];
  270. IJKIndex = new THREE.Vector3( 0, 0, RASIndex );
  271. positionOffset = ( volume.RASDimensions[ 2 ] - 1 ) / 2;
  272. planeMatrix.setPosition( new THREE.Vector3( 0, 0, RASIndex - positionOffset ) );
  273. break;
  274. }
  275. firstDirection.applyMatrix4( volume.inverseMatrix ).normalize();
  276. firstDirection.argVar = 'i';
  277. secondDirection.applyMatrix4( volume.inverseMatrix ).normalize();
  278. secondDirection.argVar = 'j';
  279. axisInIJK.applyMatrix4( volume.inverseMatrix ).normalize();
  280. iLength = Math.floor( Math.abs( firstDirection.dot( dimensions ) ) );
  281. jLength = Math.floor( Math.abs( secondDirection.dot( dimensions ) ) );
  282. planeWidth = Math.abs( iLength * firstSpacing );
  283. planeHeight = Math.abs( jLength * secondSpacing );
  284. IJKIndex = Math.abs( Math.round( IJKIndex.applyMatrix4( volume.inverseMatrix ).dot( axisInIJK ) ) );
  285. var base = [ new THREE.Vector3( 1, 0, 0 ), new THREE.Vector3( 0, 1, 0 ), new THREE.Vector3( 0, 0, 1 ) ];
  286. var iDirection = [ firstDirection, secondDirection, axisInIJK ].find( function( x ) {
  287. return Math.abs( x.dot( base[ 0 ] ) ) > 0.9;
  288. } );
  289. var jDirection = [ firstDirection, secondDirection, axisInIJK ].find( function( x ) {
  290. return Math.abs( x.dot( base[ 1 ] ) ) > 0.9;
  291. } );
  292. var kDirection = [ firstDirection, secondDirection, axisInIJK ].find( function( x ) {
  293. return Math.abs( x.dot( base[ 2 ] ) ) > 0.9;
  294. } );
  295. var argumentsWithInversion = [ 'volume.xLength-1-', 'volume.yLength-1-', 'volume.zLength-1-' ];
  296. var arguments = [ 'i', 'j', 'k' ];
  297. var argArray = [ iDirection, jDirection, kDirection ].map( function( direction, n ) {
  298. return ( direction.dot( base[ n ] ) > 0 ? '' : argumentsWithInversion[ n ] ) + ( direction === axisInIJK ? 'IJKIndex' : direction.argVar )
  299. } );
  300. var argString = argArray.join( ',' );
  301. sliceAccess = eval( '(function sliceAccess (i,j) {return volume.access( ' + argString + ');})' );
  302. return {
  303. iLength : iLength,
  304. jLength : jLength,
  305. sliceAccess : sliceAccess,
  306. matrix : planeMatrix,
  307. planeWidth : planeWidth,
  308. planeHeight : planeHeight
  309. }
  310. },
  311. /**
  312. * @member {Function} extractSlice Returns a slice corresponding to the given axis and index
  313. * The coordinate are given in the Right Anterior Superior coordinate format
  314. * @memberof THREE.Volume
  315. * @param {string} axis the normal axis to the slice 'x' 'y' or 'z'
  316. * @param {number} index the index of the slice
  317. * @returns {THREE.VolumeSlice} the extracted slice
  318. */
  319. extractSlice : function( axis, index ) {
  320. var slice = new THREE.VolumeSlice( this, index, axis );
  321. this.sliceList.push( slice );
  322. return slice;
  323. },
  324. /**
  325. * @member {Function} repaintAllSlices Call repaint on all the slices extracted from this volume
  326. * @see THREE.VolumeSlice.repaint
  327. * @memberof THREE.Volume
  328. * @returns {THREE.Volume} this
  329. */
  330. repaintAllSlices : function() {
  331. this.sliceList.forEach( function( slice ) {
  332. slice.repaint();
  333. } );
  334. return this;
  335. },
  336. /**
  337. * @member {Function} computeMinMax Compute the minimum and the maximum of the data in the volume
  338. * @memberof THREE.Volume
  339. * @returns {Array} [min,max]
  340. */
  341. computeMinMax : function() {
  342. var min = Infinity;
  343. var max = - Infinity;
  344. // buffer the length
  345. var datasize = this.data.length;
  346. var i = 0;
  347. for ( i = 0; i < datasize; i ++ ) {
  348. if ( ! isNaN( this.data[ i ] ) ) {
  349. var value = this.data[ i ];
  350. min = Math.min( min, value );
  351. max = Math.max( max, value );
  352. }
  353. }
  354. this.min = min;
  355. this.max = max;
  356. return [ min, max ];
  357. }
  358. };