units-visual.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /**
  2. * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  7. import { Structure, Unit, StructureElement } from '../../mol-model/structure';
  8. import { RepresentationProps } from '../representation';
  9. import { Visual, VisualContext } from '../visual';
  10. import { Geometry, GeometryUtils } from '../../mol-geo/geometry/geometry';
  11. import { LocationIterator } from '../../mol-geo/util/location-iterator';
  12. import { Theme } from '../../mol-theme/theme';
  13. import { createUnitsTransform, includesUnitKind } from './visual/util/common';
  14. import { createRenderObject, RenderObjectValues, GraphicsRenderObject } from '../../mol-gl/render-object';
  15. import { PickingId } from '../../mol-geo/geometry/picking';
  16. import { Loci, isEveryLoci, EmptyLoci } from '../../mol-model/loci';
  17. import { Interval } from '../../mol-data/int';
  18. import { VisualUpdateState } from '../util';
  19. import { ColorTheme } from '../../mol-theme/color';
  20. import { createMarkers } from '../../mol-geo/geometry/marker-data';
  21. import { MarkerAction } from '../../mol-util/marker-action';
  22. import { ValueCell, deepEqual } from '../../mol-util';
  23. import { createSizes } from '../../mol-geo/geometry/size-data';
  24. import { createColors } from '../../mol-geo/geometry/color-data';
  25. import { Mat4 } from '../../mol-math/linear-algebra';
  26. import { Overpaint } from '../../mol-theme/overpaint';
  27. import { Transparency } from '../../mol-theme/transparency';
  28. import { Mesh } from '../../mol-geo/geometry/mesh/mesh';
  29. import { SizeTheme } from '../../mol-theme/size';
  30. import { Spheres } from '../../mol-geo/geometry/spheres/spheres';
  31. import { Points } from '../../mol-geo/geometry/points/points';
  32. import { Lines } from '../../mol-geo/geometry/lines/lines';
  33. import { Text } from '../../mol-geo/geometry/text/text';
  34. import { DirectVolume } from '../../mol-geo/geometry/direct-volume/direct-volume';
  35. import { TextureMesh } from '../../mol-geo/geometry/texture-mesh/texture-mesh';
  36. import { SizeValues } from '../../mol-gl/renderable/schema';
  37. import { StructureParams, StructureMeshParams, StructureSpheresParams, StructurePointsParams, StructureLinesParams, StructureTextParams, StructureDirectVolumeParams, StructureTextureMeshParams } from './params';
  38. import { Clipping } from '../../mol-theme/clipping';
  39. export type StructureGroup = { structure: Structure, group: Unit.SymmetryGroup }
  40. export interface UnitsVisual<P extends RepresentationProps = {}> extends Visual<StructureGroup, P> { }
  41. function createUnitsRenderObject<G extends Geometry>(group: Unit.SymmetryGroup, geometry: G, locationIt: LocationIterator, theme: Theme, props: PD.Values<Geometry.Params<G>>, materialId: number) {
  42. const { createValues, createRenderableState } = Geometry.getUtils(geometry);
  43. const transform = createUnitsTransform(group);
  44. const values = createValues(geometry, transform, locationIt, theme, props);
  45. const state = createRenderableState(props);
  46. return createRenderObject(geometry.kind, values, state, materialId);
  47. }
  48. interface UnitsVisualBuilder<P extends StructureParams, G extends Geometry> {
  49. defaultProps: PD.Values<P>
  50. createGeometry(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<P>, geometry?: G): Promise<G> | G
  51. createLocationIterator(structureGroup: StructureGroup): LocationIterator
  52. getLoci(pickingId: PickingId, structureGroup: StructureGroup, id: number): Loci
  53. eachLocation(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean, isMarking: boolean): boolean
  54. setUpdateState(state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup): void
  55. }
  56. interface UnitsVisualGeometryBuilder<P extends StructureParams, G extends Geometry> extends UnitsVisualBuilder<P, G> {
  57. geometryUtils: GeometryUtils<G>
  58. }
  59. export function UnitsVisual<G extends Geometry, P extends StructureParams & Geometry.Params<G>>(builder: UnitsVisualGeometryBuilder<P, G>, materialId: number): UnitsVisual<P> {
  60. const { defaultProps, createGeometry, createLocationIterator, getLoci, eachLocation, setUpdateState } = builder;
  61. const { createEmpty: createEmptyGeometry, updateValues, updateBoundingSphere, updateRenderableState } = builder.geometryUtils;
  62. const updateState = VisualUpdateState.create();
  63. let renderObject: GraphicsRenderObject<G['kind']> | undefined;
  64. let newProps: PD.Values<P> = Object.assign({}, defaultProps);
  65. let newTheme: Theme = Theme.createEmpty();
  66. let newStructureGroup: StructureGroup;
  67. let currentProps: PD.Values<P>;
  68. let currentTheme: Theme;
  69. let currentStructureGroup: StructureGroup;
  70. let geometry: G;
  71. let locationIt: LocationIterator;
  72. function prepareUpdate(theme: Theme, props: PD.Values<P>, structureGroup: StructureGroup) {
  73. if (!structureGroup && !currentStructureGroup) {
  74. throw new Error('missing structureGroup');
  75. }
  76. newProps = props;
  77. newTheme = theme;
  78. newStructureGroup = structureGroup;
  79. VisualUpdateState.reset(updateState);
  80. if (!renderObject) {
  81. // console.log('create new - no renderObject');
  82. updateState.createNew = true;
  83. } else if (!currentStructureGroup || !Unit.SymmetryGroup.areInvariantElementsEqual(newStructureGroup.group, currentStructureGroup.group)) {
  84. // console.log('create new - elements not equal');
  85. updateState.createNew = true;
  86. }
  87. if (updateState.createNew) {
  88. updateState.createGeometry = true;
  89. return;
  90. }
  91. setUpdateState(updateState, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  92. if (!Structure.areHierarchiesEqual(currentStructureGroup.structure, newStructureGroup.structure)) {
  93. // console.log('new hierarchy');
  94. updateState.updateTransform = true;
  95. updateState.updateColor = true;
  96. }
  97. if (!ColorTheme.areEqual(newTheme.color, currentTheme.color)) {
  98. // console.log('new colorTheme');
  99. updateState.updateColor = true;
  100. }
  101. if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) {
  102. // console.log('new unitKinds');
  103. updateState.createGeometry = true;
  104. }
  105. if (newStructureGroup.group.transformHash !== currentStructureGroup.group.transformHash) {
  106. // console.log('new transformHash');
  107. if (newStructureGroup.group.units.length !== currentStructureGroup.group.units.length || updateState.updateColor) {
  108. updateState.updateTransform = true;
  109. } else {
  110. updateState.updateMatrix = true;
  111. }
  112. }
  113. // check if the operator or conformation of unit has changed
  114. const newUnit = newStructureGroup.group.units[0];
  115. const currentUnit = currentStructureGroup.group.units[0];
  116. if (!Unit.areOperatorsEqual(newUnit, currentUnit)) {
  117. // console.log('new operators');
  118. updateState.updateTransform = true;
  119. }
  120. if (!Unit.areConformationsEqual(newUnit, currentUnit)) {
  121. // console.log('new conformation');
  122. updateState.createGeometry = true;
  123. }
  124. if (updateState.updateTransform) {
  125. updateState.updateMatrix = true;
  126. }
  127. if (updateState.createGeometry || updateState.updateTransform) {
  128. if (currentStructureGroup.structure.hashCode !== newStructureGroup.structure.hashCode) {
  129. // console.log('new hashCode');
  130. updateState.updateColor = true;
  131. updateState.updateSize = true;
  132. }
  133. }
  134. }
  135. function update(newGeometry?: G) {
  136. if (updateState.createNew) {
  137. locationIt = createLocationIterator(newStructureGroup);
  138. if (newGeometry) {
  139. renderObject = createUnitsRenderObject(newStructureGroup.group, newGeometry, locationIt, newTheme, newProps, materialId);
  140. } else {
  141. throw new Error('expected geometry to be given');
  142. }
  143. } else {
  144. if (!renderObject) {
  145. throw new Error('expected renderObject to be available');
  146. }
  147. if (updateState.updateTransform) {
  148. // console.log('update transform');
  149. locationIt = createLocationIterator(newStructureGroup);
  150. const { instanceCount, groupCount } = locationIt;
  151. createMarkers(instanceCount * groupCount, renderObject.values);
  152. }
  153. if (updateState.updateMatrix) {
  154. // console.log('update matrix');
  155. createUnitsTransform(newStructureGroup.group, renderObject.values);
  156. }
  157. if (updateState.createGeometry) {
  158. // console.log('update geometry');
  159. if (newGeometry) {
  160. ValueCell.updateIfChanged(renderObject.values.drawCount, Geometry.getDrawCount(newGeometry));
  161. } else {
  162. throw new Error('expected geometry to be given');
  163. }
  164. }
  165. if (updateState.updateTransform || updateState.createGeometry) {
  166. // console.log('UnitsVisual.updateBoundingSphere');
  167. updateBoundingSphere(renderObject.values as RenderObjectValues<G['kind']>, newGeometry || geometry);
  168. }
  169. if (updateState.updateSize) {
  170. // not all geometries have size data, so check here
  171. if ('uSize' in renderObject.values) {
  172. // console.log('update size');
  173. createSizes(locationIt, newTheme.size, renderObject.values as SizeValues);
  174. }
  175. }
  176. if (updateState.updateColor) {
  177. // console.log('update color');
  178. createColors(locationIt, newTheme.color, renderObject.values);
  179. }
  180. updateValues(renderObject.values as RenderObjectValues<G['kind']>, newProps);
  181. updateRenderableState(renderObject.state, newProps);
  182. }
  183. currentProps = newProps;
  184. currentTheme = newTheme;
  185. currentStructureGroup = newStructureGroup;
  186. if (newGeometry) geometry = newGeometry;
  187. }
  188. function _createGeometry(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<P>, geometry?: G) {
  189. return includesUnitKind(props.unitKinds, unit)
  190. ? createGeometry(ctx, unit, structure, theme, props, geometry)
  191. : createEmptyGeometry(geometry);
  192. }
  193. function lociIsSuperset(loci: Loci) {
  194. if (isEveryLoci(loci)) return true;
  195. if (Structure.isLoci(loci) && Structure.areRootsEquivalent(loci.structure, currentStructureGroup.structure)) return true;
  196. if (StructureElement.Loci.is(loci) && Structure.areRootsEquivalent(loci.structure, currentStructureGroup.structure)) {
  197. if (StructureElement.Loci.isWholeStructure(loci)) return true;
  198. }
  199. return false;
  200. }
  201. function lociApply(loci: Loci, apply: (interval: Interval) => boolean, isMarking: boolean) {
  202. if (lociIsSuperset(loci)) {
  203. return apply(Interval.ofBounds(0, locationIt.groupCount * locationIt.instanceCount));
  204. } else {
  205. return eachLocation(loci, currentStructureGroup, apply, isMarking);
  206. }
  207. }
  208. return {
  209. get groupCount() { return locationIt ? locationIt.count : 0; },
  210. get renderObject () { return locationIt && locationIt.count ? renderObject : undefined; },
  211. createOrUpdate(ctx: VisualContext, theme: Theme, props: PD.Values<P>, structureGroup?: StructureGroup) {
  212. prepareUpdate(theme, props, structureGroup || currentStructureGroup);
  213. if (updateState.createGeometry) {
  214. const newGeometry = _createGeometry(ctx, newStructureGroup.group.units[0], newStructureGroup.structure, newTheme, newProps, geometry);
  215. return newGeometry instanceof Promise ? newGeometry.then(update) : update(newGeometry as G);
  216. } else {
  217. update();
  218. }
  219. },
  220. getLoci(pickingId: PickingId) {
  221. return renderObject ? getLoci(pickingId, currentStructureGroup, renderObject.id) : EmptyLoci;
  222. },
  223. mark(loci: Loci, action: MarkerAction) {
  224. return Visual.mark(renderObject, loci, action, lociApply);
  225. },
  226. setVisibility(visible: boolean) {
  227. Visual.setVisibility(renderObject, visible);
  228. },
  229. setAlphaFactor(alphaFactor: number) {
  230. Visual.setAlphaFactor(renderObject, alphaFactor);
  231. },
  232. setPickable(pickable: boolean) {
  233. Visual.setPickable(renderObject, pickable);
  234. },
  235. setTransform(matrix?: Mat4, instanceMatrices?: Float32Array | null) {
  236. Visual.setTransform(renderObject, matrix, instanceMatrices);
  237. },
  238. setOverpaint(overpaint: Overpaint) {
  239. Visual.setOverpaint(renderObject, overpaint, lociApply, true);
  240. },
  241. setTransparency(transparency: Transparency) {
  242. Visual.setTransparency(renderObject, transparency, lociApply, true);
  243. },
  244. setClipping(clipping: Clipping) {
  245. Visual.setClipping(renderObject, clipping, lociApply, true);
  246. },
  247. destroy() {
  248. // TODO
  249. renderObject = undefined;
  250. }
  251. };
  252. }
  253. // mesh
  254. export const UnitsMeshParams = { ...StructureMeshParams, ...StructureParams };
  255. export type UnitsMeshParams = typeof UnitsMeshParams
  256. export interface UnitsMeshVisualBuilder<P extends UnitsMeshParams> extends UnitsVisualBuilder<P, Mesh> { }
  257. export function UnitsMeshVisual<P extends UnitsMeshParams>(builder: UnitsMeshVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  258. return UnitsVisual<Mesh, P>({
  259. ...builder,
  260. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  261. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  262. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true;
  263. },
  264. geometryUtils: Mesh.Utils
  265. }, materialId);
  266. }
  267. // spheres
  268. export const UnitsSpheresParams = { ...StructureSpheresParams, ...StructureParams };
  269. export type UnitsSpheresParams = typeof UnitsSpheresParams
  270. export interface UnitsSpheresVisualBuilder<P extends UnitsSpheresParams> extends UnitsVisualBuilder<P, Spheres> { }
  271. export function UnitsSpheresVisual<P extends UnitsSpheresParams>(builder: UnitsSpheresVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  272. return UnitsVisual<Spheres, P>({
  273. ...builder,
  274. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  275. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  276. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  277. },
  278. geometryUtils: Spheres.Utils
  279. }, materialId);
  280. }
  281. // points
  282. export const UnitsPointsParams = { ...StructurePointsParams, ...StructureParams };
  283. export type UnitsPointsParams = typeof UnitsPointsParams
  284. export interface UnitsPointVisualBuilder<P extends UnitsPointsParams> extends UnitsVisualBuilder<P, Points> { }
  285. export function UnitsPointsVisual<P extends UnitsPointsParams>(builder: UnitsPointVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  286. return UnitsVisual<Points, P>({
  287. ...builder,
  288. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  289. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  290. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  291. },
  292. geometryUtils: Points.Utils
  293. }, materialId);
  294. }
  295. // lines
  296. export const UnitsLinesParams = { ...StructureLinesParams, ...StructureParams };
  297. export type UnitsLinesParams = typeof UnitsLinesParams
  298. export interface UnitsLinesVisualBuilder<P extends UnitsLinesParams> extends UnitsVisualBuilder<P, Lines> { }
  299. export function UnitsLinesVisual<P extends UnitsLinesParams>(builder: UnitsLinesVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  300. return UnitsVisual<Lines, P>({
  301. ...builder,
  302. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  303. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  304. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  305. },
  306. geometryUtils: Lines.Utils
  307. }, materialId);
  308. }
  309. // text
  310. export const UnitsTextParams = { ...StructureTextParams, ...StructureParams };
  311. export type UnitsTextParams = typeof UnitsTextParams
  312. export interface UnitsTextVisualBuilder<P extends UnitsTextParams> extends UnitsVisualBuilder<P, Text> { }
  313. export function UnitsTextVisual<P extends UnitsTextParams>(builder: UnitsTextVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  314. return UnitsVisual<Text, P>({
  315. ...builder,
  316. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  317. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  318. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true;
  319. if (newProps.background !== currentProps.background) state.createGeometry = true;
  320. if (newProps.backgroundMargin !== currentProps.backgroundMargin) state.createGeometry = true;
  321. if (newProps.tether !== currentProps.tether) state.createGeometry = true;
  322. if (newProps.tetherLength !== currentProps.tetherLength) state.createGeometry = true;
  323. if (newProps.tetherBaseWidth !== currentProps.tetherBaseWidth) state.createGeometry = true;
  324. if (newProps.attachment !== currentProps.attachment) state.createGeometry = true;
  325. },
  326. geometryUtils: Text.Utils
  327. }, materialId);
  328. }
  329. // direct-volume
  330. export const UnitsDirectVolumeParams = { ...StructureDirectVolumeParams, ...StructureParams };
  331. export type UnitsDirectVolumeParams = typeof UnitsDirectVolumeParams
  332. export interface UnitsDirectVolumeVisualBuilder<P extends UnitsDirectVolumeParams> extends UnitsVisualBuilder<P, DirectVolume> { }
  333. export function UnitsDirectVolumeVisual<P extends UnitsDirectVolumeParams>(builder: UnitsDirectVolumeVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  334. return UnitsVisual<DirectVolume, P>({
  335. ...builder,
  336. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  337. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  338. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true;
  339. },
  340. geometryUtils: DirectVolume.Utils
  341. }, materialId);
  342. }
  343. // texture-mesh
  344. export const UnitsTextureMeshParams = { ...StructureTextureMeshParams, ...StructureParams };
  345. export type UnitsTextureMeshParams = typeof UnitsTextureMeshParams
  346. export interface UnitsTextureMeshVisualBuilder<P extends UnitsTextureMeshParams> extends UnitsVisualBuilder<P, TextureMesh> { }
  347. export function UnitsTextureMeshVisual<P extends UnitsTextureMeshParams>(builder: UnitsTextureMeshVisualBuilder<P>, materialId: number): UnitsVisual<P> {
  348. return UnitsVisual<TextureMesh, P>({
  349. ...builder,
  350. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  351. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup);
  352. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true;
  353. },
  354. geometryUtils: TextureMesh.Utils
  355. }, materialId);
  356. }