molstar
Version:
A comprehensive macromolecular library.
190 lines • 10.5 kB
JavaScript
/**
* Copyright (c) 2018-2020 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 { LinesBuilder } from './lines-builder';
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 Lines;
(function (Lines) {
function create(mappings, indices, groups, starts, ends, lineCount, lines) {
return lines ?
update(mappings, indices, groups, starts, ends, lineCount, lines) :
fromArrays(mappings, indices, groups, starts, ends, lineCount);
}
Lines.create = create;
function createEmpty(lines) {
var mb = lines ? lines.mappingBuffer.ref.value : new Float32Array(0);
var ib = lines ? lines.indexBuffer.ref.value : new Uint32Array(0);
var gb = lines ? lines.groupBuffer.ref.value : new Float32Array(0);
var sb = lines ? lines.startBuffer.ref.value : new Float32Array(0);
var eb = lines ? lines.endBuffer.ref.value : new Float32Array(0);
return create(mb, ib, gb, sb, eb, 0, lines);
}
Lines.createEmpty = createEmpty;
function fromMesh(mesh, lines) {
var vb = mesh.vertexBuffer.ref.value;
var ib = mesh.indexBuffer.ref.value;
var gb = mesh.groupBuffer.ref.value;
var builder = LinesBuilder.create(mesh.triangleCount * 3, mesh.triangleCount / 10, lines);
// TODO avoid duplicate lines
for (var i = 0, il = mesh.triangleCount * 3; i < il; i += 3) {
var i0 = ib[i], i1 = ib[i + 1], i2 = ib[i + 2];
var x0 = vb[i0 * 3], y0 = vb[i0 * 3 + 1], z0 = vb[i0 * 3 + 2];
var x1 = vb[i1 * 3], y1 = vb[i1 * 3 + 1], z1 = vb[i1 * 3 + 2];
var x2 = vb[i2 * 3], y2 = vb[i2 * 3 + 1], z2 = vb[i2 * 3 + 2];
builder.add(x0, y0, z0, x1, y1, z1, gb[i0]);
builder.add(x0, y0, z0, x2, y2, z2, gb[i0]);
builder.add(x1, y1, z1, x2, y2, z2, gb[i1]);
}
return builder.getLines();
}
Lines.fromMesh = fromMesh;
function hashCode(lines) {
return hashFnv32a([
lines.lineCount, lines.mappingBuffer.ref.version, lines.indexBuffer.ref.version,
lines.groupBuffer.ref.version, lines.startBuffer.ref.version, lines.endBuffer.ref.version
]);
}
function fromArrays(mappings, indices, groups, starts, ends, lineCount) {
var boundingSphere = Sphere3D();
var groupMapping;
var currentHash = -1;
var currentGroup = -1;
var lines = {
kind: 'lines',
lineCount: lineCount,
mappingBuffer: ValueCell.create(mappings),
indexBuffer: ValueCell.create(indices),
groupBuffer: ValueCell.create(groups),
startBuffer: ValueCell.create(starts),
endBuffer: ValueCell.create(ends),
get boundingSphere() {
var newHash = hashCode(lines);
if (newHash !== currentHash) {
var s = calculateInvariantBoundingSphere(lines.startBuffer.ref.value, lines.lineCount * 4, 4);
var e = calculateInvariantBoundingSphere(lines.endBuffer.ref.value, lines.lineCount * 4, 4);
Sphere3D.expandBySphere(boundingSphere, s, e);
currentHash = newHash;
}
return boundingSphere;
},
get groupMapping() {
if (lines.groupBuffer.ref.version !== currentGroup) {
groupMapping = createGroupMapping(lines.groupBuffer.ref.value, lines.lineCount, 4);
currentGroup = lines.groupBuffer.ref.version;
}
return groupMapping;
},
setBoundingSphere: function (sphere) {
Sphere3D.copy(boundingSphere, sphere);
currentHash = hashCode(lines);
}
};
return lines;
}
function update(mappings, indices, groups, starts, ends, lineCount, lines) {
if (lineCount > lines.lineCount) {
ValueCell.update(lines.mappingBuffer, mappings);
ValueCell.update(lines.indexBuffer, indices);
}
lines.lineCount = lineCount;
ValueCell.update(lines.groupBuffer, groups);
ValueCell.update(lines.startBuffer, starts);
ValueCell.update(lines.endBuffer, ends);
return lines;
}
function transform(lines, t) {
var start = lines.startBuffer.ref.value;
transformPositionArray(t, start, 0, lines.lineCount * 4);
ValueCell.update(lines.startBuffer, start);
var end = lines.endBuffer.ref.value;
transformPositionArray(t, end, 0, lines.lineCount * 4);
ValueCell.update(lines.endBuffer, end);
}
Lines.transform = transform;
//
Lines.Params = __assign(__assign({}, BaseGeometry.Params), { sizeFactor: PD.Numeric(3, { min: 0, max: 10, step: 0.1 }), lineSizeAttenuation: PD.Boolean(false) });
Lines.Utils = {
Params: Lines.Params,
createEmpty: createEmpty,
createValues: createValues,
createValuesSimple: createValuesSimple,
updateValues: updateValues,
updateBoundingSphere: updateBoundingSphere,
createRenderableState: BaseGeometry.createRenderableState,
updateRenderableState: BaseGeometry.updateRenderableState,
createPositionIterator: createPositionIterator
};
function createPositionIterator(lines, transform) {
var groupCount = lines.lineCount * 4;
var instanceCount = transform.instanceCount.ref.value;
var location = PositionLocation();
var p = location.position;
var s = lines.startBuffer.ref.value;
var e = lines.endBuffer.ref.value;
var m = transform.aTransform.ref.value;
var getLocation = function (groupIndex, instanceIndex) {
var v = groupIndex % 4 === 0 ? s : e;
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, 2, getLocation);
}
function createValues(lines, transform, locationIt, theme, props) {
var instanceCount = locationIt.instanceCount, groupCount = locationIt.groupCount;
var positionIt = createPositionIterator(lines, 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: lines.lineCount * 2 * 3, vertexCount: lines.lineCount * 4, groupCount: groupCount, instanceCount: instanceCount };
var invariantBoundingSphere = Sphere3D.clone(lines.boundingSphere);
var boundingSphere = calculateTransformBoundingSphere(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount);
return __assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign(__assign({ aMapping: lines.mappingBuffer, aGroup: lines.groupBuffer, aStart: lines.startBuffer, aEnd: lines.endBuffer, elements: lines.indexBuffer, 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), dLineSizeAttenuation: ValueCell.create(props.lineSizeAttenuation), dDoubleSided: ValueCell.create(true), dFlipSided: ValueCell.create(false) });
}
function createValuesSimple(lines, props, colorValue, sizeValue, transform) {
var s = BaseGeometry.createSimple(colorValue, sizeValue, transform);
var p = __assign(__assign({}, PD.getDefaultValues(Lines.Params)), props);
return createValues(lines, s.transform, s.locationIterator, s.theme, p);
}
function updateValues(values, props) {
BaseGeometry.updateValues(values, props);
ValueCell.updateIfChanged(values.uSizeFactor, props.sizeFactor);
ValueCell.updateIfChanged(values.dLineSizeAttenuation, props.lineSizeAttenuation);
}
function updateBoundingSphere(values, lines) {
var invariantBoundingSphere = Sphere3D.clone(lines.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));
}
}
})(Lines || (Lines = {}));
//# sourceMappingURL=lines.js.map