Volume.js 13 KB

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