Volume.js 13 KB

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