molstar
Version:
A comprehensive macromolecular library.
170 lines • 8.93 kB
JavaScript
/**
* Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
*/
import { __assign } from "tslib";
import { ValueCell } from '../../../mol-util';
import { Vec3, Vec4 } from '../../../mol-math/linear-algebra';
import { transformPositionArray, createGroupMapping } from '../../util';
import { createColors } from '../color-data';
import { createMarkers } from '../marker-data';
import { createSizes } from '../size-data';
import { LocationIterator, PositionLocation } from '../../util/location-iterator';
import { ParamDefinition as PD } from '../../../mol-util/param-definition';
import { calculateInvariantBoundingSphere, calculateTransformBoundingSphere } from '../../../mol-gl/renderable/util';
import { Sphere3D } from '../../../mol-math/geometry';
import { BaseGeometry } from '../base';
import { createEmptyOverpaint } from '../overpaint-data';
import { createEmptyTransparency } from '../transparency-data';
import { hashFnv32a } from '../../../mol-data/util';
import { createEmptyClipping } from '../clipping-data';
export var Points;
(function (Points) {
function create(centers, groups, pointCount, points) {
return points ?
update(centers, groups, pointCount, points) :
fromArrays(centers, groups, pointCount);
}
Points.create = create;
function createEmpty(points) {
var cb = points ? points.centerBuffer.ref.value : new Float32Array(0);
var gb = points ? points.groupBuffer.ref.value : new Float32Array(0);
return create(cb, gb, 0, points);
}
Points.createEmpty = createEmpty;
function hashCode(points) {
return hashFnv32a([
points.pointCount, points.centerBuffer.ref.version, points.groupBuffer.ref.version,
]);
}
function fromArrays(centers, groups, pointCount) {
var boundingSphere = Sphere3D();
var groupMapping;
var currentHash = -1;
var currentGroup = -1;
var points = {
kind: 'points',
pointCount: pointCount,
centerBuffer: ValueCell.create(centers),
groupBuffer: ValueCell.create(groups),
get boundingSphere() {
var newHash = hashCode(points);
if (newHash !== currentHash) {
var b = calculateInvariantBoundingSphere(points.centerBuffer.ref.value, points.pointCount, 1);
Sphere3D.copy(boundingSphere, b);
currentHash = newHash;
}
return boundingSphere;
},
get groupMapping() {
if (points.groupBuffer.ref.version !== currentGroup) {
groupMapping = createGroupMapping(points.groupBuffer.ref.value, points.pointCount);
currentGroup = points.groupBuffer.ref.version;
}
return groupMapping;
},
setBoundingSphere: function (sphere) {
Sphere3D.copy(boundingSphere, sphere);
currentHash = hashCode(points);
}
};
return points;
}
function update(centers, groups, pointCount, points) {
points.pointCount = pointCount;
ValueCell.update(points.centerBuffer, centers);
ValueCell.update(points.groupBuffer, groups);
return points;
}
function transform(points, t) {
var c = points.centerBuffer.ref.value;
transformPositionArray(t, c, 0, points.pointCount);
ValueCell.update(points.centerBuffer, c);
}
Points.transform = transform;
//
Points.StyleTypes = {
'square': 'Square',
'circle': 'Circle',
'fuzzy': 'Fuzzy',
};
Points.StyleTypeNames = Object.keys(Points.StyleTypes);
Points.Params = __assign(__assign({}, BaseGeometry.Params), { sizeFactor: PD.Numeric(3, { min: 0, max: 10, step: 0.1 }), pointSizeAttenuation: PD.Boolean(false), pointStyle: PD.Select('square', PD.objectToOptions(Points.StyleTypes)) });
Points.Utils = {
Params: Points.Params,
createEmpty: createEmpty,
createValues: createValues,
createValuesSimple: createValuesSimple,
updateValues: updateValues,
updateBoundingSphere: updateBoundingSphere,
createRenderableState: createRenderableState,
updateRenderableState: updateRenderableState,
createPositionIterator: createPositionIterator
};
function createPositionIterator(points, transform) {
var groupCount = points.pointCount;
var instanceCount = transform.instanceCount.ref.value;
var location = PositionLocation();
var p = location.position;
var v = points.centerBuffer.ref.value;
var m = transform.aTransform.ref.value;
var getLocation = function (groupIndex, instanceIndex) {
if (instanceIndex < 0) {
Vec3.fromArray(p, v, groupIndex * 3);
}
else {
Vec3.transformMat4Offset(p, v, m, 0, groupIndex * 3, instanceIndex * 16);
}
return location;
};
return LocationIterator(groupCount, instanceCount, 1, getLocation);
}
function createValues(points, transform, locationIt, theme, props) {
var instanceCount = locationIt.instanceCount, groupCount = locationIt.groupCount;
var positionIt = createPositionIterator(points, transform);
var color = createColors(locationIt, positionIt, theme.color);
var size = createSizes(locationIt, theme.size);
var marker = createMarkers(instanceCount * groupCount);
var overpaint = createEmptyOverpaint();
var transparency = createEmptyTransparency();
var clipping = createEmptyClipping();
var counts = { drawCount: points.pointCount, vertexCount: points.pointCount, groupCount: groupCount, instanceCount: instanceCount };
var invariantBoundingSphere = Sphere3D.clone(points.boundingSphere);
var boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount);
return __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({ aPosition: points.centerBuffer, aGroup: points.groupBuffer, boundingSphere: ValueCell.create(boundingSphere), invariantBoundingSphere: ValueCell.create(invariantBoundingSphere), uInvariantBoundingSphere: ValueCell.create(Vec4.ofSphere(invariantBoundingSphere)) }, color), size), marker), overpaint), transparency), clipping), transform), BaseGeometry.createValues(props, counts)), { uSizeFactor: ValueCell.create(props.sizeFactor), dPointSizeAttenuation: ValueCell.create(props.pointSizeAttenuation), dPointStyle: ValueCell.create(props.pointStyle) });
}
function createValuesSimple(points, props, colorValue, sizeValue, transform) {
var s = BaseGeometry.createSimple(colorValue, sizeValue, transform);
var p = __assign(__assign({}, PD.getDefaultValues(Points.Params)), props);
return createValues(points, s.transform, s.locationIterator, s.theme, p);
}
function updateValues(values, props) {
BaseGeometry.updateValues(values, props);
ValueCell.updateIfChanged(values.uSizeFactor, props.sizeFactor);
ValueCell.updateIfChanged(values.dPointSizeAttenuation, props.pointSizeAttenuation);
ValueCell.updateIfChanged(values.dPointStyle, props.pointStyle);
}
function updateBoundingSphere(values, points) {
var invariantBoundingSphere = Sphere3D.clone(points.boundingSphere);
var boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, values.aTransform.ref.value, values.instanceCount.ref.value);
if (!Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) {
ValueCell.update(values.boundingSphere, boundingSphere);
}
if (!Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) {
ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere);
ValueCell.update(values.uInvariantBoundingSphere, Vec4.fromSphere(values.uInvariantBoundingSphere.ref.value, invariantBoundingSphere));
}
}
function createRenderableState(props) {
var state = BaseGeometry.createRenderableState(props);
updateRenderableState(state, props);
return state;
}
function updateRenderableState(state, props) {
BaseGeometry.updateRenderableState(state, props);
state.opaque = state.opaque && props.pointStyle !== 'fuzzy';
state.writeDepth = state.opaque;
}
})(Points || (Points = {}));
//# sourceMappingURL=points.js.map