direct-volume.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { hashFnv32a } from '../../../mol-data/util';
  7. import { LocationIterator, PositionLocation } from '../../../mol-geo/util/location-iterator';
  8. import { RenderableState } from '../../../mol-gl/renderable';
  9. import { DirectVolumeValues } from '../../../mol-gl/renderable/direct-volume';
  10. import { calculateTransformBoundingSphere } from '../../../mol-gl/renderable/util';
  11. import { createNullTexture, Texture } from '../../../mol-gl/webgl/texture';
  12. import { Box3D, Sphere3D } from '../../../mol-math/geometry';
  13. import { Mat4, Vec2, Vec3, Vec4 } from '../../../mol-math/linear-algebra';
  14. import { Theme } from '../../../mol-theme/theme';
  15. import { ValueCell } from '../../../mol-util';
  16. import { Color } from '../../../mol-util/color';
  17. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  18. import { Box } from '../../primitive/box';
  19. import { BaseGeometry } from '../base';
  20. import { createColors } from '../color-data';
  21. import { GeometryUtils } from '../geometry';
  22. import { createMarkers } from '../marker-data';
  23. import { createEmptyOverpaint } from '../overpaint-data';
  24. import { TransformData } from '../transform-data';
  25. import { createEmptyTransparency } from '../transparency-data';
  26. import { createTransferFunctionTexture, getControlPointsFromVec2Array } from './transfer-function';
  27. import { createEmptyClipping } from '../clipping-data';
  28. import { Grid } from '../../../mol-model/volume';
  29. import { createEmptySubstance } from '../substance-data';
  30. const VolumeBox = Box();
  31. export interface DirectVolume {
  32. readonly kind: 'direct-volume',
  33. readonly gridTexture: ValueCell<Texture>
  34. readonly gridTextureDim: ValueCell<Vec3>
  35. readonly gridDimension: ValueCell<Vec3>
  36. readonly gridStats: ValueCell<Vec4> // [min, max, mean, sigma]
  37. readonly bboxSize: ValueCell<Vec3>
  38. readonly bboxMin: ValueCell<Vec3>
  39. readonly bboxMax: ValueCell<Vec3>
  40. readonly transform: ValueCell<Mat4>
  41. readonly cellDim: ValueCell<Vec3>
  42. readonly unitToCartn: ValueCell<Mat4>
  43. readonly cartnToUnit: ValueCell<Mat4>
  44. readonly packedGroup: ValueCell<boolean>
  45. readonly axisOrder: ValueCell<Vec3>
  46. /** Bounding sphere of the volume */
  47. readonly boundingSphere: Sphere3D
  48. setBoundingSphere(boundingSphere: Sphere3D): void
  49. }
  50. export namespace DirectVolume {
  51. export function create(bbox: Box3D, gridDimension: Vec3, transform: Mat4, unitToCartn: Mat4, cellDim: Vec3, texture: Texture, stats: Grid['stats'], packedGroup: boolean, axisOrder: Vec3, directVolume?: DirectVolume): DirectVolume {
  52. return directVolume ?
  53. update(bbox, gridDimension, transform, unitToCartn, cellDim, texture, stats, packedGroup, axisOrder, directVolume) :
  54. fromData(bbox, gridDimension, transform, unitToCartn, cellDim, texture, stats, packedGroup, axisOrder);
  55. }
  56. function hashCode(directVolume: DirectVolume) {
  57. return hashFnv32a([
  58. directVolume.bboxSize.ref.version, directVolume.gridDimension.ref.version,
  59. directVolume.gridTexture.ref.version, directVolume.transform.ref.version,
  60. directVolume.gridStats.ref.version
  61. ]);
  62. }
  63. function fromData(bbox: Box3D, gridDimension: Vec3, transform: Mat4, unitToCartn: Mat4, cellDim: Vec3, texture: Texture, stats: Grid['stats'], packedGroup: boolean, axisOrder: Vec3): DirectVolume {
  64. const boundingSphere = Sphere3D();
  65. let currentHash = -1;
  66. const width = texture.getWidth();
  67. const height = texture.getHeight();
  68. const depth = texture.getDepth();
  69. const directVolume = {
  70. kind: 'direct-volume' as const,
  71. gridDimension: ValueCell.create(gridDimension),
  72. gridTexture: ValueCell.create(texture),
  73. gridTextureDim: ValueCell.create(Vec3.create(width, height, depth)),
  74. gridStats: ValueCell.create(Vec4.create(stats.min, stats.max, stats.mean, stats.sigma)),
  75. bboxMin: ValueCell.create(bbox.min),
  76. bboxMax: ValueCell.create(bbox.max),
  77. bboxSize: ValueCell.create(Vec3.sub(Vec3(), bbox.max, bbox.min)),
  78. transform: ValueCell.create(transform),
  79. cellDim: ValueCell.create(cellDim),
  80. unitToCartn: ValueCell.create(unitToCartn),
  81. cartnToUnit: ValueCell.create(Mat4.invert(Mat4(), unitToCartn)),
  82. get boundingSphere() {
  83. const newHash = hashCode(directVolume);
  84. if (newHash !== currentHash) {
  85. const b = getBoundingSphere(directVolume.gridDimension.ref.value, directVolume.transform.ref.value);
  86. Sphere3D.copy(boundingSphere, b);
  87. currentHash = newHash;
  88. }
  89. return boundingSphere;
  90. },
  91. packedGroup: ValueCell.create(packedGroup),
  92. axisOrder: ValueCell.create(axisOrder),
  93. setBoundingSphere(sphere: Sphere3D) {
  94. Sphere3D.copy(boundingSphere, sphere);
  95. currentHash = hashCode(directVolume);
  96. }
  97. };
  98. return directVolume;
  99. }
  100. function update(bbox: Box3D, gridDimension: Vec3, transform: Mat4, unitToCartn: Mat4, cellDim: Vec3, texture: Texture, stats: Grid['stats'], packedGroup: boolean, axisOrder: Vec3, directVolume: DirectVolume): DirectVolume {
  101. const width = texture.getWidth();
  102. const height = texture.getHeight();
  103. const depth = texture.getDepth();
  104. ValueCell.update(directVolume.gridDimension, gridDimension);
  105. ValueCell.update(directVolume.gridTexture, texture);
  106. ValueCell.update(directVolume.gridTextureDim, Vec3.set(directVolume.gridTextureDim.ref.value, width, height, depth));
  107. ValueCell.update(directVolume.gridStats, Vec4.set(directVolume.gridStats.ref.value, stats.min, stats.max, stats.mean, stats.sigma));
  108. ValueCell.update(directVolume.bboxMin, bbox.min);
  109. ValueCell.update(directVolume.bboxMax, bbox.max);
  110. ValueCell.update(directVolume.bboxSize, Vec3.sub(directVolume.bboxSize.ref.value, bbox.max, bbox.min));
  111. ValueCell.update(directVolume.transform, transform);
  112. ValueCell.update(directVolume.cellDim, cellDim);
  113. ValueCell.update(directVolume.unitToCartn, unitToCartn);
  114. ValueCell.update(directVolume.cartnToUnit, Mat4.invert(Mat4(), unitToCartn));
  115. ValueCell.updateIfChanged(directVolume.packedGroup, packedGroup);
  116. ValueCell.updateIfChanged(directVolume.axisOrder, Vec3.fromArray(directVolume.axisOrder.ref.value, axisOrder, 0));
  117. return directVolume;
  118. }
  119. export function createEmpty(directVolume?: DirectVolume): DirectVolume {
  120. const bbox = Box3D();
  121. const gridDimension = Vec3();
  122. const transform = Mat4.identity();
  123. const unitToCartn = Mat4.identity();
  124. const cellDim = Vec3();
  125. const texture = createNullTexture();
  126. const stats = Grid.One.stats;
  127. const packedGroup = false;
  128. const axisOrder = Vec3.create(0, 1, 2);
  129. return create(bbox, gridDimension, transform, unitToCartn, cellDim, texture, stats, packedGroup, axisOrder, directVolume);
  130. }
  131. export const Params = {
  132. ...BaseGeometry.Params,
  133. ignoreLight: PD.Boolean(false, BaseGeometry.ShadingCategory),
  134. xrayShaded: PD.Boolean(false, BaseGeometry.ShadingCategory),
  135. controlPoints: PD.LineGraph([
  136. Vec2.create(0.19, 0.0), Vec2.create(0.2, 0.05), Vec2.create(0.25, 0.05), Vec2.create(0.26, 0.0),
  137. Vec2.create(0.79, 0.0), Vec2.create(0.8, 0.05), Vec2.create(0.85, 0.05), Vec2.create(0.86, 0.0),
  138. ], { isEssential: true }),
  139. stepsPerCell: PD.Numeric(3, { min: 1, max: 10, step: 1 }),
  140. jumpLength: PD.Numeric(0, { min: 0, max: 20, step: 0.1 }),
  141. };
  142. export type Params = typeof Params
  143. export const Utils: GeometryUtils<DirectVolume, Params> = {
  144. Params,
  145. createEmpty,
  146. createValues,
  147. createValuesSimple,
  148. updateValues,
  149. updateBoundingSphere,
  150. createRenderableState,
  151. updateRenderableState,
  152. createPositionIterator
  153. };
  154. function createPositionIterator(directVolume: DirectVolume, transform: TransformData): LocationIterator {
  155. const t = directVolume.transform.ref.value;
  156. const [x, y, z] = directVolume.gridDimension.ref.value;
  157. const groupCount = x * y * z;
  158. const instanceCount = transform.instanceCount.ref.value;
  159. const location = PositionLocation();
  160. const p = location.position;
  161. const m = transform.aTransform.ref.value;
  162. const getLocation = (groupIndex: number, instanceIndex: number) => {
  163. const k = Math.floor(groupIndex / z);
  164. p[0] = Math.floor(k / y);
  165. p[1] = k % y;
  166. p[2] = groupIndex % z;
  167. Vec3.transformMat4(p, p, t);
  168. if (instanceIndex >= 0) {
  169. Vec3.transformMat4Offset(p, p, m, 0, 0, instanceIndex * 16);
  170. }
  171. return location;
  172. };
  173. return LocationIterator(groupCount, instanceCount, 1, getLocation);
  174. }
  175. function getMaxSteps(gridDim: Vec3, stepsPerCell: number) {
  176. return Math.ceil(Vec3.magnitude(gridDim) * stepsPerCell);
  177. }
  178. function getStepScale(cellDim: Vec3, stepsPerCell: number) {
  179. return Math.min(...cellDim) * (1 / stepsPerCell);
  180. }
  181. function getTransferScale(stepsPerCell: number) {
  182. return (1 / stepsPerCell);
  183. }
  184. function createValues(directVolume: DirectVolume, transform: TransformData, locationIt: LocationIterator, theme: Theme, props: PD.Values<Params>): DirectVolumeValues {
  185. const { gridTexture, gridTextureDim, gridStats } = directVolume;
  186. const { bboxSize, bboxMin, bboxMax, gridDimension, transform: gridTransform } = directVolume;
  187. const { instanceCount, groupCount } = locationIt;
  188. const positionIt = Utils.createPositionIterator(directVolume, transform);
  189. const color = createColors(locationIt, positionIt, theme.color);
  190. const marker = createMarkers(instanceCount * groupCount);
  191. const overpaint = createEmptyOverpaint();
  192. const transparency = createEmptyTransparency();
  193. const material = createEmptySubstance();
  194. const clipping = createEmptyClipping();
  195. const [x, y, z] = gridDimension.ref.value;
  196. const counts = { drawCount: VolumeBox.indices.length, vertexCount: x * y * z, groupCount, instanceCount };
  197. const invariantBoundingSphere = Sphere3D.clone(directVolume.boundingSphere);
  198. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount);
  199. const controlPoints = getControlPointsFromVec2Array(props.controlPoints);
  200. const transferTex = createTransferFunctionTexture(controlPoints);
  201. return {
  202. dGeometryType: ValueCell.create('directVolume'),
  203. ...color,
  204. ...marker,
  205. ...overpaint,
  206. ...transparency,
  207. ...material,
  208. ...clipping,
  209. ...transform,
  210. ...BaseGeometry.createValues(props, counts),
  211. aPosition: ValueCell.create(VolumeBox.vertices as Float32Array),
  212. elements: ValueCell.create(VolumeBox.indices as Uint32Array),
  213. boundingSphere: ValueCell.create(boundingSphere),
  214. invariantBoundingSphere: ValueCell.create(invariantBoundingSphere),
  215. uInvariantBoundingSphere: ValueCell.create(Vec4.ofSphere(invariantBoundingSphere)),
  216. uBboxMin: bboxMin,
  217. uBboxMax: bboxMax,
  218. uBboxSize: bboxSize,
  219. uMaxSteps: ValueCell.create(getMaxSteps(gridDimension.ref.value, props.stepsPerCell)),
  220. uStepScale: ValueCell.create(getStepScale(directVolume.cellDim.ref.value, props.stepsPerCell)),
  221. uJumpLength: ValueCell.create(props.jumpLength),
  222. uTransform: gridTransform,
  223. uGridDim: gridDimension,
  224. tTransferTex: transferTex,
  225. uTransferScale: ValueCell.create(getTransferScale(props.stepsPerCell)),
  226. dGridTexType: ValueCell.create(gridTexture.ref.value.getDepth() > 0 ? '3d' : '2d'),
  227. uGridTexDim: gridTextureDim,
  228. tGridTex: gridTexture,
  229. uGridStats: gridStats,
  230. uCellDim: directVolume.cellDim,
  231. uCartnToUnit: directVolume.cartnToUnit,
  232. uUnitToCartn: directVolume.unitToCartn,
  233. dPackedGroup: directVolume.packedGroup,
  234. dAxisOrder: ValueCell.create(directVolume.axisOrder.ref.value.join('')),
  235. dIgnoreLight: ValueCell.create(props.ignoreLight),
  236. dXrayShaded: ValueCell.create(props.xrayShaded),
  237. };
  238. }
  239. function createValuesSimple(directVolume: DirectVolume, props: Partial<PD.Values<Params>>, colorValue: Color, sizeValue: number, transform?: TransformData) {
  240. const s = BaseGeometry.createSimple(colorValue, sizeValue, transform);
  241. const p = { ...PD.getDefaultValues(Params), ...props };
  242. return createValues(directVolume, s.transform, s.locationIterator, s.theme, p);
  243. }
  244. function updateValues(values: DirectVolumeValues, props: PD.Values<Params>) {
  245. BaseGeometry.updateValues(values, props);
  246. ValueCell.updateIfChanged(values.dIgnoreLight, props.ignoreLight);
  247. ValueCell.updateIfChanged(values.dXrayShaded, props.xrayShaded);
  248. const controlPoints = getControlPointsFromVec2Array(props.controlPoints);
  249. createTransferFunctionTexture(controlPoints, values.tTransferTex);
  250. ValueCell.updateIfChanged(values.uMaxSteps, getMaxSteps(values.uGridDim.ref.value, props.stepsPerCell));
  251. ValueCell.updateIfChanged(values.uStepScale, getStepScale(values.uCellDim.ref.value, props.stepsPerCell));
  252. ValueCell.updateIfChanged(values.uTransferScale, getTransferScale(props.stepsPerCell));
  253. ValueCell.updateIfChanged(values.uJumpLength, props.jumpLength);
  254. }
  255. function updateBoundingSphere(values: DirectVolumeValues, directVolume: DirectVolume) {
  256. const invariantBoundingSphere = Sphere3D.clone(directVolume.boundingSphere);
  257. const boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, values.aTransform.ref.value, values.instanceCount.ref.value);
  258. if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
  259. ValueCell.update(values.boundingSphere, boundingSphere);
  260. }
  261. if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
  262. ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere);
  263. ValueCell.update(values.uInvariantBoundingSphere, Vec4.fromSphere(values.uInvariantBoundingSphere.ref.value, invariantBoundingSphere));
  264. }
  265. }
  266. function createRenderableState(props: PD.Values<Params>): RenderableState {
  267. const state = BaseGeometry.createRenderableState(props);
  268. state.opaque = false;
  269. state.writeDepth = false;
  270. return state;
  271. }
  272. function updateRenderableState(state: RenderableState, props: PD.Values<Params>) {
  273. BaseGeometry.updateRenderableState(state, props);
  274. state.opaque = false;
  275. state.writeDepth = false;
  276. }
  277. }
  278. //
  279. function getBoundingSphere(gridDimension: Vec3, gridTransform: Mat4) {
  280. return Sphere3D.fromDimensionsAndTransform(Sphere3D(), gridDimension, gridTransform);
  281. }