Volume.js 13 KB

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