Volume.js 13 KB

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