VolumeSlice.js 6.1 KB

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