Volume.js 13 KB

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