VolumeSlice.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import {
  2. ClampToEdgeWrapping,
  3. DoubleSide,
  4. LinearFilter,
  5. Mesh,
  6. MeshBasicMaterial,
  7. PlaneGeometry,
  8. Texture
  9. } from 'three';
  10. /**
  11. * This class has been made to hold a slice of a volume data
  12. * @class
  13. * @param {Volume} volume The associated volume
  14. * @param {number} [index=0] The index of the slice
  15. * @param {string} [axis='z'] For now only 'x', 'y' or 'z' but later it will change to a normal vector
  16. * @see Volume
  17. */
  18. class VolumeSlice {
  19. constructor( volume, index, axis ) {
  20. const slice = this;
  21. /**
  22. * @member {Volume} volume The associated volume
  23. */
  24. this.volume = volume;
  25. /**
  26. * @member {Number} index The index of the slice, if changed, will automatically call updateGeometry at the next repaint
  27. */
  28. index = index || 0;
  29. Object.defineProperty( this, 'index', {
  30. get: function () {
  31. return index;
  32. },
  33. set: function ( value ) {
  34. index = value;
  35. slice.geometryNeedsUpdate = true;
  36. return index;
  37. }
  38. } );
  39. /**
  40. * @member {String} axis The normal axis
  41. */
  42. this.axis = axis || 'z';
  43. /**
  44. * @member {HTMLCanvasElement} canvas The final canvas used for the texture
  45. */
  46. /**
  47. * @member {CanvasRenderingContext2D} ctx Context of the canvas
  48. */
  49. this.canvas = document.createElement( 'canvas' );
  50. /**
  51. * @member {HTMLCanvasElement} canvasBuffer The intermediary canvas used to paint the data
  52. */
  53. /**
  54. * @member {CanvasRenderingContext2D} ctxBuffer Context of the canvas buffer
  55. */
  56. this.canvasBuffer = document.createElement( 'canvas' );
  57. this.updateGeometry();
  58. const canvasMap = new Texture( this.canvas );
  59. canvasMap.minFilter = LinearFilter;
  60. canvasMap.wrapS = canvasMap.wrapT = ClampToEdgeWrapping;
  61. const material = new MeshBasicMaterial( { map: canvasMap, side: DoubleSide, transparent: true } );
  62. /**
  63. * @member {Mesh} mesh The mesh ready to get used in the scene
  64. */
  65. this.mesh = new Mesh( this.geometry, material );
  66. this.mesh.matrixAutoUpdate = false;
  67. /**
  68. * @member {Boolean} geometryNeedsUpdate If set to true, updateGeometry will be triggered at the next repaint
  69. */
  70. this.geometryNeedsUpdate = true;
  71. this.repaint();
  72. /**
  73. * @member {Number} iLength Width of slice in the original coordinate system, corresponds to the width of the buffer canvas
  74. */
  75. /**
  76. * @member {Number} jLength Height of slice in the original coordinate system, corresponds to the height of the buffer canvas
  77. */
  78. /**
  79. * @member {Function} sliceAccess Function that allow the slice to access right data
  80. * @see Volume.extractPerpendicularPlane
  81. * @param {Number} i The first coordinate
  82. * @param {Number} j The second coordinate
  83. * @returns {Number} the index corresponding to the voxel in volume.data of the given position in the slice
  84. */
  85. }
  86. /**
  87. * @member {Function} repaint Refresh the texture and the geometry if geometryNeedsUpdate is set to true
  88. * @memberof VolumeSlice
  89. */
  90. repaint() {
  91. if ( this.geometryNeedsUpdate ) {
  92. this.updateGeometry();
  93. }
  94. const iLength = this.iLength,
  95. jLength = this.jLength,
  96. sliceAccess = this.sliceAccess,
  97. volume = this.volume,
  98. canvas = this.canvasBuffer,
  99. ctx = this.ctxBuffer;
  100. // get the imageData and pixel array from the canvas
  101. const imgData = ctx.getImageData( 0, 0, iLength, jLength );
  102. const data = imgData.data;
  103. const volumeData = volume.data;
  104. const upperThreshold = volume.upperThreshold;
  105. const lowerThreshold = volume.lowerThreshold;
  106. const windowLow = volume.windowLow;
  107. const windowHigh = volume.windowHigh;
  108. // manipulate some pixel elements
  109. let pixelCount = 0;
  110. if ( volume.dataType === 'label' ) {
  111. //this part is currently useless but will be used when colortables will be handled
  112. for ( let j = 0; j < jLength; j ++ ) {
  113. for ( let i = 0; i < iLength; i ++ ) {
  114. let label = volumeData[ sliceAccess( i, j ) ];
  115. label = label >= this.colorMap.length ? ( label % this.colorMap.length ) + 1 : label;
  116. const color = this.colorMap[ label ];
  117. data[ 4 * pixelCount ] = ( color >> 24 ) & 0xff;
  118. data[ 4 * pixelCount + 1 ] = ( color >> 16 ) & 0xff;
  119. data[ 4 * pixelCount + 2 ] = ( color >> 8 ) & 0xff;
  120. data[ 4 * pixelCount + 3 ] = color & 0xff;
  121. pixelCount ++;
  122. }
  123. }
  124. } else {
  125. for ( let j = 0; j < jLength; j ++ ) {
  126. for ( let i = 0; i < iLength; i ++ ) {
  127. let value = volumeData[ sliceAccess( i, j ) ];
  128. let alpha = 0xff;
  129. //apply threshold
  130. alpha = upperThreshold >= value ? ( lowerThreshold <= value ? alpha : 0 ) : 0;
  131. //apply window level
  132. value = Math.floor( 255 * ( value - windowLow ) / ( windowHigh - windowLow ) );
  133. value = value > 255 ? 255 : ( value < 0 ? 0 : value | 0 );
  134. data[ 4 * pixelCount ] = value;
  135. data[ 4 * pixelCount + 1 ] = value;
  136. data[ 4 * pixelCount + 2 ] = value;
  137. data[ 4 * pixelCount + 3 ] = alpha;
  138. pixelCount ++;
  139. }
  140. }
  141. }
  142. ctx.putImageData( imgData, 0, 0 );
  143. this.ctx.drawImage( canvas, 0, 0, iLength, jLength, 0, 0, this.canvas.width, this.canvas.height );
  144. this.mesh.material.map.needsUpdate = true;
  145. }
  146. /**
  147. * @member {Function} Refresh the geometry according to axis and index
  148. * @see Volume.extractPerpendicularPlane
  149. * @memberof VolumeSlice
  150. */
  151. updateGeometry() {
  152. const extracted = this.volume.extractPerpendicularPlane( this.axis, this.index );
  153. this.sliceAccess = extracted.sliceAccess;
  154. this.jLength = extracted.jLength;
  155. this.iLength = extracted.iLength;
  156. this.matrix = extracted.matrix;
  157. this.canvas.width = extracted.planeWidth;
  158. this.canvas.height = extracted.planeHeight;
  159. this.canvasBuffer.width = this.iLength;
  160. this.canvasBuffer.height = this.jLength;
  161. this.ctx = this.canvas.getContext( '2d' );
  162. this.ctxBuffer = this.canvasBuffer.getContext( '2d' );
  163. if ( this.geometry ) this.geometry.dispose(); // dispose existing geometry
  164. this.geometry = new PlaneGeometry( extracted.planeWidth, extracted.planeHeight );
  165. if ( this.mesh ) {
  166. this.mesh.geometry = this.geometry;
  167. //reset mesh matrix
  168. this.mesh.matrix.identity();
  169. this.mesh.applyMatrix4( this.matrix );
  170. }
  171. this.geometryNeedsUpdate = false;
  172. }
  173. }
  174. export { VolumeSlice };