Volume.js 13 KB

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