VolumeSlice.js 6.4 KB

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