1
0

VolumeSlice.js 6.4 KB

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