Volume.js 13 KB

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