VolumeSlice.js 6.0 KB

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