Volume.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. case 'Float32' :
  117. case 'float32' :
  118. case 'float' :
  119. this.data = new Float32Array( arrayBuffer );
  120. break;
  121. case 'Float64' :
  122. case 'float64' :
  123. case 'double' :
  124. this.data = new Float64Array( arrayBuffer );
  125. break;
  126. default :
  127. this.data = new Uint8Array( arrayBuffer );
  128. }
  129. if ( this.data.length !== this.xLength * this.yLength * this.zLength ) {
  130. throw new Error( 'Error in Volume constructor, lengths are not matching arrayBuffer size' );
  131. }
  132. }
  133. /**
  134. * Spacing to apply to the volume from IJK to RAS coordinate system
  135. *
  136. * @type {Array<number>}
  137. */
  138. this.spacing = [ 1, 1, 1 ];
  139. /**
  140. * Offset of the volume in the RAS coordinate system
  141. *
  142. * @type {Array<number>}
  143. */
  144. this.offset = [ 0, 0, 0 ];
  145. /**
  146. * The IJK to RAS matrix.
  147. *
  148. * @type {Martrix3}
  149. */
  150. this.matrix = new Matrix3();
  151. this.matrix.identity();
  152. /**
  153. * The RAS to IJK matrix.
  154. *
  155. * @type {Martrix3}
  156. */
  157. this.inverseMatrix = new Matrix3();
  158. let lowerThreshold = - Infinity;
  159. Object.defineProperty( this, 'lowerThreshold', {
  160. get: function () {
  161. return lowerThreshold;
  162. },
  163. /**
  164. * The voxels with values under this threshold won't appear in the slices.
  165. * If changed, geometryNeedsUpdate is automatically set to true on all the slices associated to this volume.
  166. *
  167. * @name Volume#lowerThreshold
  168. * @type {number}
  169. * @param {number} value
  170. */
  171. set: function ( value ) {
  172. lowerThreshold = value;
  173. this.sliceList.forEach( function ( slice ) {
  174. slice.geometryNeedsUpdate = true;
  175. } );
  176. }
  177. } );
  178. let upperThreshold = Infinity;
  179. Object.defineProperty( this, 'upperThreshold', {
  180. get: function () {
  181. return upperThreshold;
  182. },
  183. /**
  184. * The voxels with values over this threshold won't appear in the slices.
  185. * If changed, geometryNeedsUpdate is automatically set to true on all the slices associated to this volume
  186. *
  187. * @name Volume#upperThreshold
  188. * @type {number}
  189. * @param {number} value
  190. */
  191. set: function ( value ) {
  192. upperThreshold = value;
  193. this.sliceList.forEach( function ( slice ) {
  194. slice.geometryNeedsUpdate = true;
  195. } );
  196. }
  197. } );
  198. /**
  199. * The list of all the slices associated to this volume
  200. *
  201. * @type {Array<VolumeSlice>}
  202. */
  203. this.sliceList = [];
  204. /**
  205. * Whether to use segmentation mode or not.
  206. * It can load 16-bits nrrds correctly.
  207. *
  208. * @type {boolean}
  209. * @default false
  210. */
  211. this.segmentation = false;
  212. /**
  213. * This array holds the dimensions of the volume in the RAS space
  214. *
  215. * @type {Array<number>}
  216. */
  217. this.RASDimensions = [];
  218. }
  219. /**
  220. * Shortcut for data[access(i,j,k)].
  221. *
  222. * @param {number} i - First coordinate.
  223. * @param {number} j - Second coordinate.
  224. * @param {number} k - Third coordinate.
  225. * @returns {number} The value in the data array.
  226. */
  227. getData( i, j, k ) {
  228. return this.data[ k * this.xLength * this.yLength + j * this.xLength + i ];
  229. }
  230. /**
  231. * Compute the index in the data array corresponding to the given coordinates in IJK system.
  232. *
  233. * @param {number} i - First coordinate.
  234. * @param {number} j - Second coordinate.
  235. * @param {number} k - Third coordinate.
  236. * @returns {number} The index.
  237. */
  238. access( i, j, k ) {
  239. return k * this.xLength * this.yLength + j * this.xLength + i;
  240. }
  241. /**
  242. * Retrieve the IJK coordinates of the voxel corresponding of the given index in the data.
  243. *
  244. * @param {number} index - Index of the voxel.
  245. * @returns {Array<number>} The IJK coordinates as `[x,y,z]`.
  246. */
  247. reverseAccess( index ) {
  248. const z = Math.floor( index / ( this.yLength * this.xLength ) );
  249. const y = Math.floor( ( index - z * this.yLength * this.xLength ) / this.xLength );
  250. const x = index - z * this.yLength * this.xLength - y * this.xLength;
  251. return [ x, y, z ];
  252. }
  253. /**
  254. * Apply a function to all the voxels, be careful, the value will be replaced.
  255. *
  256. * @param {Function} functionToMap A function to apply to every voxel, will be called with the following parameters:
  257. * value of the voxel, index of the voxel, the data (TypedArray).
  258. * @param {Object} context - You can specify a context in which call the function, default if this Volume.
  259. * @returns {Volume} A reference to this instance.
  260. */
  261. map( functionToMap, context ) {
  262. const length = this.data.length;
  263. context = context || this;
  264. for ( let i = 0; i < length; i ++ ) {
  265. this.data[ i ] = functionToMap.call( context, this.data[ i ], i, this.data );
  266. }
  267. return this;
  268. }
  269. /**
  270. * Compute the orientation of the slice and returns all the information relative to the geometry such as sliceAccess,
  271. * the plane matrix (orientation and position in RAS coordinate) and the dimensions of the plane in both coordinate system.
  272. *
  273. * @param {('x'|'y'|'z')} axis - The normal axis to the slice.
  274. * @param {number} RASIndex - The index of the slice.
  275. * @returns {Object} An object containing all the useful information on the geometry of the slice.
  276. */
  277. extractPerpendicularPlane( axis, RASIndex ) {
  278. let firstSpacing,
  279. secondSpacing,
  280. positionOffset,
  281. IJKIndex;
  282. const axisInIJK = new Vector3(),
  283. firstDirection = new Vector3(),
  284. secondDirection = new Vector3(),
  285. planeMatrix = ( new Matrix4() ).identity(),
  286. volume = this;
  287. const dimensions = new Vector3( this.xLength, this.yLength, this.zLength );
  288. switch ( axis ) {
  289. case 'x' :
  290. axisInIJK.set( 1, 0, 0 );
  291. firstDirection.set( 0, 0, - 1 );
  292. secondDirection.set( 0, - 1, 0 );
  293. firstSpacing = this.spacing[ this.axisOrder.indexOf( 'z' ) ];
  294. secondSpacing = this.spacing[ this.axisOrder.indexOf( 'y' ) ];
  295. IJKIndex = new Vector3( RASIndex, 0, 0 );
  296. planeMatrix.multiply( ( new Matrix4() ).makeRotationY( Math.PI / 2 ) );
  297. positionOffset = ( volume.RASDimensions[ 0 ] - 1 ) / 2;
  298. planeMatrix.setPosition( new Vector3( RASIndex - positionOffset, 0, 0 ) );
  299. break;
  300. case 'y' :
  301. axisInIJK.set( 0, 1, 0 );
  302. firstDirection.set( 1, 0, 0 );
  303. secondDirection.set( 0, 0, 1 );
  304. firstSpacing = this.spacing[ this.axisOrder.indexOf( 'x' ) ];
  305. secondSpacing = this.spacing[ this.axisOrder.indexOf( 'z' ) ];
  306. IJKIndex = new Vector3( 0, RASIndex, 0 );
  307. planeMatrix.multiply( ( new Matrix4() ).makeRotationX( - Math.PI / 2 ) );
  308. positionOffset = ( volume.RASDimensions[ 1 ] - 1 ) / 2;
  309. planeMatrix.setPosition( new Vector3( 0, RASIndex - positionOffset, 0 ) );
  310. break;
  311. case 'z' :
  312. default :
  313. axisInIJK.set( 0, 0, 1 );
  314. firstDirection.set( 1, 0, 0 );
  315. secondDirection.set( 0, - 1, 0 );
  316. firstSpacing = this.spacing[ this.axisOrder.indexOf( 'x' ) ];
  317. secondSpacing = this.spacing[ this.axisOrder.indexOf( 'y' ) ];
  318. IJKIndex = new Vector3( 0, 0, RASIndex );
  319. positionOffset = ( volume.RASDimensions[ 2 ] - 1 ) / 2;
  320. planeMatrix.setPosition( new Vector3( 0, 0, RASIndex - positionOffset ) );
  321. break;
  322. }
  323. if ( ! this.segmentation ) {
  324. firstDirection.applyMatrix4( volume.inverseMatrix ).normalize();
  325. secondDirection.applyMatrix4( volume.inverseMatrix ).normalize();
  326. axisInIJK.applyMatrix4( volume.inverseMatrix ).normalize();
  327. }
  328. firstDirection.arglet = 'i';
  329. secondDirection.arglet = 'j';
  330. const iLength = Math.floor( Math.abs( firstDirection.dot( dimensions ) ) );
  331. const jLength = Math.floor( Math.abs( secondDirection.dot( dimensions ) ) );
  332. const planeWidth = Math.abs( iLength * firstSpacing );
  333. const planeHeight = Math.abs( jLength * secondSpacing );
  334. IJKIndex = Math.abs( Math.round( IJKIndex.applyMatrix4( volume.inverseMatrix ).dot( axisInIJK ) ) );
  335. const base = [ new Vector3( 1, 0, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ) ];
  336. const iDirection = [ firstDirection, secondDirection, axisInIJK ].find( function ( x ) {
  337. return Math.abs( x.dot( base[ 0 ] ) ) > 0.9;
  338. } );
  339. const jDirection = [ firstDirection, secondDirection, axisInIJK ].find( function ( x ) {
  340. return Math.abs( x.dot( base[ 1 ] ) ) > 0.9;
  341. } );
  342. const kDirection = [ firstDirection, secondDirection, axisInIJK ].find( function ( x ) {
  343. return Math.abs( x.dot( base[ 2 ] ) ) > 0.9;
  344. } );
  345. function sliceAccess( i, j ) {
  346. const si = ( iDirection === axisInIJK ) ? IJKIndex : ( iDirection.arglet === 'i' ? i : j );
  347. const sj = ( jDirection === axisInIJK ) ? IJKIndex : ( jDirection.arglet === 'i' ? i : j );
  348. const sk = ( kDirection === axisInIJK ) ? IJKIndex : ( kDirection.arglet === 'i' ? i : j );
  349. // invert indices if necessary
  350. const accessI = ( iDirection.dot( base[ 0 ] ) > 0 ) ? si : ( volume.xLength - 1 ) - si;
  351. const accessJ = ( jDirection.dot( base[ 1 ] ) > 0 ) ? sj : ( volume.yLength - 1 ) - sj;
  352. const accessK = ( kDirection.dot( base[ 2 ] ) > 0 ) ? sk : ( volume.zLength - 1 ) - sk;
  353. return volume.access( accessI, accessJ, accessK );
  354. }
  355. return {
  356. iLength: iLength,
  357. jLength: jLength,
  358. sliceAccess: sliceAccess,
  359. matrix: planeMatrix,
  360. planeWidth: planeWidth,
  361. planeHeight: planeHeight
  362. };
  363. }
  364. /**
  365. * Returns a slice corresponding to the given axis and index.
  366. * The coordinate are given in the Right Anterior Superior coordinate format.
  367. *
  368. * @param {('x'|'y'|'z')} axis - The normal axis to the slice.
  369. * @param {number} index - The index of the slice.
  370. * @returns {VolumeSlice} The extracted slice.
  371. */
  372. extractSlice( axis, index ) {
  373. const slice = new VolumeSlice( this, index, axis );
  374. this.sliceList.push( slice );
  375. return slice;
  376. }
  377. /**
  378. * Call repaint on all the slices extracted from this volume.
  379. *
  380. * @see {@link VolumeSlice#repaint}
  381. * @returns {Volume} A reference to this volume.
  382. */
  383. repaintAllSlices() {
  384. this.sliceList.forEach( function ( slice ) {
  385. slice.repaint();
  386. } );
  387. return this;
  388. }
  389. /**
  390. * Compute the minimum and the maximum of the data in the volume.
  391. *
  392. * @returns {Array<number>} The min/max data as `[min,max]`.
  393. */
  394. computeMinMax() {
  395. let min = Infinity;
  396. let max = - Infinity;
  397. // buffer the length
  398. const datasize = this.data.length;
  399. let i = 0;
  400. for ( i = 0; i < datasize; i ++ ) {
  401. if ( ! isNaN( this.data[ i ] ) ) {
  402. const value = this.data[ i ];
  403. min = Math.min( min, value );
  404. max = Math.max( max, value );
  405. }
  406. }
  407. this.min = min;
  408. this.max = max;
  409. return [ min, max ];
  410. }
  411. }
  412. export { Volume };
粤ICP备19079148号