Volume.js 13 KB

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