Volume.js 13 KB

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