isosurface-mesh.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import { VolumeData } from 'mol-model/volume'
  8. import { VolumeVisual, VolumeRepresentation } from './index';
  9. import { createMeshRenderObject } from 'mol-gl/render-object';
  10. import { Loci, EmptyLoci } from 'mol-model/loci';
  11. import { ParamDefinition as PD } from 'mol-util/param-definition';
  12. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  13. import { computeMarchingCubesMesh } from 'mol-geo/util/marching-cubes/algorithm';
  14. import { LocationIterator } from 'mol-geo/util/location-iterator';
  15. import { createIdentityTransform } from 'mol-geo/geometry/transform-data';
  16. import { createRenderableState, Theme } from 'mol-geo/geometry/geometry';
  17. import { PickingId } from 'mol-geo/geometry/picking';
  18. import { MarkerAction } from 'mol-geo/geometry/marker-data';
  19. import { VisualUpdateState } from 'mol-repr/util';
  20. import { RepresentationContext, VisualContext } from 'mol-repr';
  21. interface VolumeIsosurfaceProps {
  22. isoValueAbsolute: number
  23. }
  24. export async function createVolumeIsosurface(ctx: VisualContext, volume: VolumeData, props: VolumeIsosurfaceProps, mesh?: Mesh) {
  25. ctx.runtime.update({ message: 'Marching cubes...' });
  26. const surface = await computeMarchingCubesMesh({
  27. isoLevel: props.isoValueAbsolute,
  28. scalarField: volume.data
  29. }, mesh).runAsChild(ctx.runtime);
  30. const transform = VolumeData.getGridToCartesianTransform(volume);
  31. ctx.runtime.update({ message: 'Transforming mesh...' });
  32. Mesh.transformImmediate(surface, transform);
  33. Mesh.computeNormalsImmediate(surface)
  34. return surface;
  35. }
  36. export const IsosurfaceParams = {
  37. ...Mesh.Params,
  38. isoValueAbsolute: PD.Range('Iso Value Absolute', '', 0.22, -1, 1, 0.01),
  39. isoValueRelative: PD.Range('Iso Value Relative', '', 2, -10, 10, 0.1),
  40. }
  41. export const DefaultIsosurfaceProps = PD.getDefaultValues(IsosurfaceParams)
  42. export type IsosurfaceProps = typeof DefaultIsosurfaceProps
  43. export function IsosurfaceVisual(): VolumeVisual<IsosurfaceProps> {
  44. return VolumeVisual<IsosurfaceProps>({
  45. defaultProps: DefaultIsosurfaceProps,
  46. createGeometry: createVolumeIsosurface,
  47. getLoci: () => EmptyLoci,
  48. mark: () => false,
  49. setUpdateState: (state: VisualUpdateState, newProps: IsosurfaceProps, currentProps: IsosurfaceProps) => {
  50. if (newProps.isoValueAbsolute !== currentProps.isoValueAbsolute) state.createGeometry = true
  51. },
  52. createRenderObject: async (ctx: VisualContext, geometry: Mesh, locationIt: LocationIterator, theme: Theme, props: IsosurfaceProps) => {
  53. const transform = createIdentityTransform()
  54. const values = await Mesh.createValues(ctx.runtime, geometry, transform, locationIt, theme, props)
  55. const state = createRenderableState(props)
  56. return createMeshRenderObject(values, state)
  57. },
  58. updateValues: Mesh.updateValues
  59. })
  60. }
  61. export function IsosurfaceRepresentation(): VolumeRepresentation<IsosurfaceProps> {
  62. let currentProps: IsosurfaceProps
  63. const volumeRepr = VolumeRepresentation(IsosurfaceVisual)
  64. return {
  65. label: 'Isosurface',
  66. params: IsosurfaceParams,
  67. get renderObjects() {
  68. return [ ...volumeRepr.renderObjects ]
  69. },
  70. get props() {
  71. return { ...volumeRepr.props }
  72. },
  73. createOrUpdate: (ctx: RepresentationContext, props: Partial<IsosurfaceProps> = {}, volume?: VolumeData) => {
  74. currentProps = Object.assign({}, DefaultIsosurfaceProps, currentProps, props)
  75. return volumeRepr.createOrUpdate(ctx, currentProps, volume)
  76. },
  77. getLoci: (pickingId: PickingId) => {
  78. return volumeRepr.getLoci(pickingId)
  79. },
  80. mark: (loci: Loci, action: MarkerAction) => {
  81. return volumeRepr.mark(loci, action)
  82. },
  83. destroy() {
  84. volumeRepr.destroy()
  85. }
  86. }
  87. }