VolumeSlice.js 6.0 KB

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