VolumeSlice.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. console.warn( "THREE.VolumeSlice: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * This class has been made to hold a slice of a volume data
  4. * @class
  5. * @author Valentin Demeusy / https://github.com/stity
  6. * @param {THREE.Volume} volume The associated volume
  7. * @param {number} [index=0] The index of the slice
  8. * @param {string} [axis='z'] For now only 'x', 'y' or 'z' but later it will change to a normal vector
  9. * @see THREE.Volume
  10. */
  11. THREE.VolumeSlice = function ( volume, index, axis ) {
  12. var slice = this;
  13. /**
  14. * @member {THREE.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. var canvasMap = new THREE.Texture( this.canvas );
  51. canvasMap.minFilter = THREE.LinearFilter;
  52. canvasMap.wrapS = canvasMap.wrapT = THREE.ClampToEdgeWrapping;
  53. var material = new THREE.MeshBasicMaterial( { map: canvasMap, side: THREE.DoubleSide, transparent: true } );
  54. /**
  55. * @member {THREE.Mesh} mesh The mesh ready to get used in the scene
  56. */
  57. this.mesh = new THREE.Mesh( this.geometry, material );
  58. this.mesh.matrixAutoUpdate = false;
  59. /**
  60. * @member {Boolean} geometryNeedsUpdate If set to true, updateGeometry will be triggered at the next repaint
  61. */
  62. this.geometryNeedsUpdate = true;
  63. this.repaint();
  64. /**
  65. * @member {Number} iLength Width of slice in the original coordinate system, corresponds to the width of the buffer canvas
  66. */
  67. /**
  68. * @member {Number} jLength Height of slice in the original coordinate system, corresponds to the height of the buffer canvas
  69. */
  70. /**
  71. * @member {Function} sliceAccess Function that allow the slice to access right data
  72. * @see THREE.Volume.extractPerpendicularPlane
  73. * @param {Number} i The first coordinate
  74. * @param {Number} j The second coordinate
  75. * @returns {Number} the index corresponding to the voxel in volume.data of the given position in the slice
  76. */
  77. };
  78. THREE.VolumeSlice.prototype = {
  79. constructor: THREE.VolumeSlice,
  80. /**
  81. * @member {Function} repaint Refresh the texture and the geometry if geometryNeedsUpdate is set to true
  82. * @memberof THREE.VolumeSlice
  83. */
  84. repaint: function () {
  85. if ( this.geometryNeedsUpdate ) {
  86. this.updateGeometry();
  87. }
  88. var iLength = this.iLength,
  89. jLength = this.jLength,
  90. sliceAccess = this.sliceAccess,
  91. volume = this.volume,
  92. canvas = this.canvasBuffer,
  93. ctx = this.ctxBuffer;
  94. // get the imageData and pixel array from the canvas
  95. var imgData = ctx.getImageData( 0, 0, iLength, jLength );
  96. var data = imgData.data;
  97. var volumeData = volume.data;
  98. var upperThreshold = volume.upperThreshold;
  99. var lowerThreshold = volume.lowerThreshold;
  100. var windowLow = volume.windowLow;
  101. var windowHigh = volume.windowHigh;
  102. // manipulate some pixel elements
  103. var pixelCount = 0;
  104. if ( volume.dataType === 'label' ) {
  105. //this part is currently useless but will be used when colortables will be handled
  106. for ( var j = 0; j < jLength; j ++ ) {
  107. for ( var i = 0; i < iLength; i ++ ) {
  108. var label = volumeData[ sliceAccess( i, j ) ];
  109. label = label >= this.colorMap.length ? ( label % this.colorMap.length ) + 1 : label;
  110. var color = this.colorMap[ label ];
  111. data[ 4 * pixelCount ] = ( color >> 24 ) & 0xff;
  112. data[ 4 * pixelCount + 1 ] = ( color >> 16 ) & 0xff;
  113. data[ 4 * pixelCount + 2 ] = ( color >> 8 ) & 0xff;
  114. data[ 4 * pixelCount + 3 ] = color & 0xff;
  115. pixelCount ++;
  116. }
  117. }
  118. } else {
  119. for ( var j = 0; j < jLength; j ++ ) {
  120. for ( var i = 0; i < iLength; i ++ ) {
  121. var value = volumeData[ sliceAccess( i, j ) ];
  122. var alpha = 0xff;
  123. //apply threshold
  124. alpha = upperThreshold >= value ? ( lowerThreshold <= value ? alpha : 0 ) : 0;
  125. //apply window level
  126. value = Math.floor( 255 * ( value - windowLow ) / ( windowHigh - windowLow ) );
  127. value = value > 255 ? 255 : ( value < 0 ? 0 : value | 0 );
  128. data[ 4 * pixelCount ] = value;
  129. data[ 4 * pixelCount + 1 ] = value;
  130. data[ 4 * pixelCount + 2 ] = value;
  131. data[ 4 * pixelCount + 3 ] = alpha;
  132. pixelCount ++;
  133. }
  134. }
  135. }
  136. ctx.putImageData( imgData, 0, 0 );
  137. this.ctx.drawImage( canvas, 0, 0, iLength, jLength, 0, 0, this.canvas.width, this.canvas.height );
  138. this.mesh.material.map.needsUpdate = true;
  139. },
  140. /**
  141. * @member {Function} Refresh the geometry according to axis and index
  142. * @see THREE.Volume.extractPerpendicularPlane
  143. * @memberof THREE.VolumeSlice
  144. */
  145. updateGeometry: function () {
  146. var extracted = this.volume.extractPerpendicularPlane( this.axis, this.index );
  147. this.sliceAccess = extracted.sliceAccess;
  148. this.jLength = extracted.jLength;
  149. this.iLength = extracted.iLength;
  150. this.matrix = extracted.matrix;
  151. this.canvas.width = extracted.planeWidth;
  152. this.canvas.height = extracted.planeHeight;
  153. this.canvasBuffer.width = this.iLength;
  154. this.canvasBuffer.height = this.jLength;
  155. this.ctx = this.canvas.getContext( '2d' );
  156. this.ctxBuffer = this.canvasBuffer.getContext( '2d' );
  157. if ( this.geometry ) this.geometry.dispose(); // dispose existing geometry
  158. this.geometry = new THREE.PlaneBufferGeometry( extracted.planeWidth, extracted.planeHeight );
  159. if ( this.mesh ) {
  160. this.mesh.geometry = this.geometry;
  161. //reset mesh matrix
  162. this.mesh.matrix.identity();
  163. this.mesh.applyMatrix4( this.matrix );
  164. }
  165. this.geometryNeedsUpdate = false;
  166. }
  167. };