molstar
Version:
A comprehensive macromolecular library.
344 lines (343 loc) • 18 kB
JavaScript
/**
* Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { __assign } from "tslib";
import { Visual } from '../visual';
import { Bond, Structure, StructureElement } from '../../mol-model/structure';
import { Geometry } from '../../mol-geo/geometry/geometry';
import { Theme } from '../../mol-theme/theme';
import { createIdentityTransform } from '../../mol-geo/geometry/transform-data';
import { createRenderObject } from '../../mol-gl/render-object';
import { isEveryLoci, EmptyLoci } from '../../mol-model/loci';
import { Interval } from '../../mol-data/int';
import { VisualUpdateState } from '../util';
import { ColorTheme } from '../../mol-theme/color';
import { ValueCell, deepEqual } from '../../mol-util';
import { createSizes } from '../../mol-geo/geometry/size-data';
import { createColors } from '../../mol-geo/geometry/color-data';
import { MarkerAction } from '../../mol-util/marker-action';
import { Mesh } from '../../mol-geo/geometry/mesh/mesh';
import { Cylinders } from '../../mol-geo/geometry/cylinders/cylinders';
import { Lines } from '../../mol-geo/geometry/lines/lines';
import { Text } from '../../mol-geo/geometry/text/text';
import { SizeTheme } from '../../mol-theme/size';
import { DirectVolume } from '../../mol-geo/geometry/direct-volume/direct-volume';
import { createMarkers } from '../../mol-geo/geometry/marker-data';
import { StructureParams, StructureMeshParams, StructureTextParams, StructureDirectVolumeParams, StructureLinesParams, StructureCylindersParams, StructureTextureMeshParams } from './params';
import { TextureMesh } from '../../mol-geo/geometry/texture-mesh/texture-mesh';
import { isPromiseLike } from '../../mol-util/type-helpers';
function createComplexRenderObject(structure, geometry, locationIt, theme, props, materialId) {
var _a = Geometry.getUtils(geometry), createValues = _a.createValues, createRenderableState = _a.createRenderableState;
var transform = createIdentityTransform();
var values = createValues(geometry, transform, locationIt, theme, props);
var state = createRenderableState(props);
return createRenderObject(geometry.kind, values, state, materialId);
}
export function ComplexVisual(builder, materialId) {
var defaultProps = builder.defaultProps, createGeometry = builder.createGeometry, createLocationIterator = builder.createLocationIterator, getLoci = builder.getLoci, eachLocation = builder.eachLocation, setUpdateState = builder.setUpdateState, mustRecreate = builder.mustRecreate, processValues = builder.processValues, dispose = builder.dispose;
var _a = builder.geometryUtils, updateValues = _a.updateValues, updateBoundingSphere = _a.updateBoundingSphere, updateRenderableState = _a.updateRenderableState, createPositionIterator = _a.createPositionIterator;
var updateState = VisualUpdateState.create();
var previousMark = { loci: EmptyLoci, action: MarkerAction.None, status: -1 };
var renderObject;
var newProps;
var newTheme;
var newStructure;
var currentProps = Object.assign({}, defaultProps);
var currentTheme = Theme.createEmpty();
var currentStructure;
var geometry;
var geometryVersion = -1;
var locationIt;
var positionIt;
function prepareUpdate(theme, props, structure) {
if (!structure && !currentStructure) {
throw new Error('missing structure');
}
newProps = Object.assign({}, currentProps, props);
newTheme = theme;
newStructure = structure;
VisualUpdateState.reset(updateState);
if (!renderObject || !currentStructure) {
updateState.createNew = true;
updateState.createGeometry = true;
return;
}
setUpdateState(updateState, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
if (!Structure.areEquivalent(newStructure, currentStructure)) {
updateState.createGeometry = true;
}
if (!Structure.areHierarchiesEqual(newStructure, currentStructure)) {
updateState.updateTransform = true;
updateState.createGeometry = true;
}
if (!ColorTheme.areEqual(theme.color, currentTheme.color)) {
updateState.updateColor = true;
}
if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) {
updateState.createGeometry = true;
}
if (currentStructure.child !== newStructure.child) {
// console.log('new child');
updateState.createGeometry = true;
}
if (newProps.instanceGranularity !== currentProps.instanceGranularity) {
updateState.updateTransform = true;
}
if (updateState.updateSize && !('uSize' in renderObject.values)) {
updateState.createGeometry = true;
}
if (updateState.createGeometry) {
updateState.updateColor = true;
updateState.updateSize = true;
}
}
function update(newGeometry) {
if (updateState.createNew) {
locationIt = createLocationIterator(newStructure);
if (newGeometry) {
renderObject = createComplexRenderObject(newStructure, newGeometry, locationIt, newTheme, newProps, materialId);
positionIt = createPositionIterator(newGeometry, renderObject.values);
}
else {
throw new Error('expected geometry to be given');
}
}
else {
if (!renderObject) {
throw new Error('expected renderObject to be available');
}
if (updateState.updateTransform) {
// console.log('update transform')
locationIt = createLocationIterator(newStructure);
var instanceCount = locationIt.instanceCount, groupCount = locationIt.groupCount;
if (newProps.instanceGranularity) {
createMarkers(instanceCount, 'instance', renderObject.values);
}
else {
createMarkers(instanceCount * groupCount, 'groupInstance', renderObject.values);
}
}
if (updateState.createGeometry) {
if (newGeometry) {
ValueCell.updateIfChanged(renderObject.values.drawCount, Geometry.getDrawCount(newGeometry));
ValueCell.updateIfChanged(renderObject.values.uVertexCount, Geometry.getVertexCount(newGeometry));
ValueCell.updateIfChanged(renderObject.values.uGroupCount, Geometry.getGroupCount(newGeometry));
}
else {
throw new Error('expected geometry to be given');
}
}
if (updateState.updateTransform || updateState.createGeometry) {
updateBoundingSphere(renderObject.values, newGeometry || geometry);
positionIt = createPositionIterator(geometry, renderObject.values);
}
if (updateState.updateSize) {
// not all geometries have size data, so check here
if ('uSize' in renderObject.values) {
createSizes(locationIt, newTheme.size, renderObject.values);
}
}
if (updateState.updateColor) {
createColors(locationIt, positionIt, newTheme.color, renderObject.values);
}
updateValues(renderObject.values, newProps);
updateRenderableState(renderObject.state, newProps);
}
currentProps = newProps;
currentTheme = newTheme;
currentStructure = newStructure;
if (newGeometry) {
geometry = newGeometry;
geometryVersion += 1;
}
}
function lociIsSuperset(loci) {
if (isEveryLoci(loci))
return true;
if (Structure.isLoci(loci) && Structure.areRootsEquivalent(loci.structure, currentStructure))
return true;
if (StructureElement.Loci.is(loci) && Structure.areRootsEquivalent(loci.structure, currentStructure)) {
if (StructureElement.Loci.isWholeStructure(loci))
return true;
}
return false;
}
function eachInstance(loci, structure, apply) {
var changed = false;
if (!StructureElement.Loci.is(loci) && !Bond.isLoci(loci))
return false;
if (!Structure.areEquivalent(loci.structure, structure))
return false;
if (apply(Interval.ofSingleton(0)))
changed = true;
return changed;
}
function lociApply(loci, apply, isMarking) {
if (lociIsSuperset(loci)) {
if (currentProps.instanceGranularity) {
return apply(Interval.ofBounds(0, locationIt.instanceCount));
}
else {
return apply(Interval.ofBounds(0, locationIt.groupCount * locationIt.instanceCount));
}
}
else {
if (currentProps.instanceGranularity) {
return eachInstance(loci, currentStructure, apply);
}
else {
return eachLocation(loci, currentStructure, apply, isMarking);
}
}
}
function finalize(ctx) {
if (renderObject) {
processValues === null || processValues === void 0 ? void 0 : processValues(renderObject.values, geometry, currentProps, currentTheme, ctx.webgl);
}
}
return {
get groupCount() { return locationIt ? locationIt.count : 0; },
get renderObject() { return locationIt && locationIt.count ? renderObject : undefined; },
get geometryVersion() { return geometryVersion; },
createOrUpdate: function (ctx, theme, props, structure) {
if (props === void 0) { props = {}; }
prepareUpdate(theme, props, structure || currentStructure);
if (updateState.createGeometry) {
var newGeometry = createGeometry(ctx, newStructure, newTheme, newProps, geometry);
if (isPromiseLike(newGeometry)) {
return newGeometry.then(function (g) {
update(g);
finalize(ctx);
});
}
update(newGeometry);
}
else {
update();
}
finalize(ctx);
},
getLoci: function (pickingId) {
return renderObject ? getLoci(pickingId, currentStructure, renderObject.id) : EmptyLoci;
},
mark: function (loci, action) {
return Visual.mark(renderObject, loci, action, lociApply, previousMark);
},
setVisibility: function (visible) {
Visual.setVisibility(renderObject, visible);
},
setAlphaFactor: function (alphaFactor) {
Visual.setAlphaFactor(renderObject, alphaFactor);
},
setPickable: function (pickable) {
Visual.setPickable(renderObject, pickable);
},
setColorOnly: function (colorOnly) {
Visual.setColorOnly(renderObject, colorOnly);
},
setTransform: function (matrix, instanceMatrices) {
Visual.setTransform(renderObject, matrix, instanceMatrices);
},
setOverpaint: function (overpaint, webgl) {
var smoothing = { geometry: geometry, props: currentProps, webgl: webgl };
Visual.setOverpaint(renderObject, overpaint, lociApply, true, smoothing);
},
setTransparency: function (transparency, webgl) {
var smoothing = { geometry: geometry, props: currentProps, webgl: webgl };
Visual.setTransparency(renderObject, transparency, lociApply, true, smoothing);
},
setSubstance: function (substance, webgl) {
var smoothing = { geometry: geometry, props: currentProps, webgl: webgl };
Visual.setSubstance(renderObject, substance, lociApply, true, smoothing);
},
setClipping: function (clipping) {
Visual.setClipping(renderObject, clipping, lociApply, true);
},
destroy: function () {
dispose === null || dispose === void 0 ? void 0 : dispose(geometry);
if (renderObject) {
renderObject.state.disposed = true;
renderObject = undefined;
}
},
mustRecreate: mustRecreate
};
}
// mesh
export var ComplexMeshParams = __assign(__assign({}, StructureMeshParams), StructureParams);
export function ComplexMeshVisual(builder, materialId) {
return ComplexVisual(__assign(__assign({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) {
builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
if (!SizeTheme.areEqual(newTheme.size, currentTheme.size))
state.createGeometry = true;
}, geometryUtils: Mesh.Utils }), materialId);
}
// cylinders
export var ComplexCylindersParams = __assign(__assign({}, StructureCylindersParams), StructureParams);
export function ComplexCylindersVisual(builder, materialId) {
return ComplexVisual(__assign(__assign({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) {
builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
if (!SizeTheme.areEqual(newTheme.size, currentTheme.size))
state.updateSize = true;
}, geometryUtils: Cylinders.Utils }), materialId);
}
// lines
export var ComplexLinesParams = __assign(__assign({}, StructureLinesParams), StructureParams);
export function ComplexLinesVisual(builder, materialId) {
return ComplexVisual(__assign(__assign({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) {
builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
if (!SizeTheme.areEqual(newTheme.size, currentTheme.size))
state.updateSize = true;
}, geometryUtils: Lines.Utils }), materialId);
}
// text
export var ComplexTextParams = __assign(__assign({}, StructureTextParams), StructureParams);
export function ComplexTextVisual(builder, materialId) {
return ComplexVisual(__assign(__assign({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) {
builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
if (!SizeTheme.areEqual(newTheme.size, currentTheme.size))
state.updateSize = true;
if (newProps.background !== currentProps.background)
state.createGeometry = true;
if (newProps.backgroundMargin !== currentProps.backgroundMargin)
state.createGeometry = true;
if (newProps.tether !== currentProps.tether)
state.createGeometry = true;
if (newProps.tetherLength !== currentProps.tetherLength)
state.createGeometry = true;
if (newProps.tetherBaseWidth !== currentProps.tetherBaseWidth)
state.createGeometry = true;
if (newProps.attachment !== currentProps.attachment)
state.createGeometry = true;
if (newProps.fontFamily !== currentProps.fontFamily)
state.createGeometry = true;
if (newProps.fontQuality !== currentProps.fontQuality)
state.createGeometry = true;
if (newProps.fontStyle !== currentProps.fontStyle)
state.createGeometry = true;
if (newProps.fontVariant !== currentProps.fontVariant)
state.createGeometry = true;
if (newProps.fontWeight !== currentProps.fontWeight)
state.createGeometry = true;
}, geometryUtils: Text.Utils }), materialId);
}
// direct-volume
export var ComplexDirectVolumeParams = __assign(__assign({}, StructureDirectVolumeParams), StructureParams);
export function ComplexDirectVolumeVisual(builder, materialId) {
return ComplexVisual(__assign(__assign({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) {
builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
if (!SizeTheme.areEqual(newTheme.size, currentTheme.size))
state.createGeometry = true;
}, geometryUtils: DirectVolume.Utils }), materialId);
}
// texture-mesh
export var ComplexTextureMeshParams = __assign(__assign({}, StructureTextureMeshParams), StructureParams);
export function ComplexTextureMeshVisual(builder, materialId) {
return ComplexVisual(__assign(__assign({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) {
builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure);
if (!SizeTheme.areEqual(newTheme.size, currentTheme.size))
state.createGeometry = true;
}, geometryUtils: TextureMesh.Utils }), materialId);
}