VolumeSlice.js 6.1 KB

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