molstar
Version:
A comprehensive macromolecular library.
127 lines (126 loc) • 6.19 kB
JavaScript
/**
* Copyright (c) 2020-2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author Gianluca Tomasello <giagitom@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CylindersBuilder = void 0;
const util_1 = require("../../../mol-data/util");
const cylinders_1 = require("./cylinders");
const linear_algebra_1 = require("../../../mol-math/linear-algebra");
const tmpVecA = (0, linear_algebra_1.Vec3)();
const tmpVecB = (0, linear_algebra_1.Vec3)();
const tmpDir = (0, linear_algebra_1.Vec3)();
// avoiding namespace lookup improved performance in Chrome (Aug 2020)
const caAdd = util_1.ChunkedArray.add;
const caAdd3 = util_1.ChunkedArray.add3;
var CylindersBuilder;
(function (CylindersBuilder) {
function create(initialCount = 2048, chunkSize = 1024, cylinders) {
const groups = util_1.ChunkedArray.create(Float32Array, 1, chunkSize, cylinders ? cylinders.groupBuffer.ref.value : initialCount);
const starts = util_1.ChunkedArray.create(Float32Array, 3, chunkSize, cylinders ? cylinders.startBuffer.ref.value : initialCount);
const ends = util_1.ChunkedArray.create(Float32Array, 3, chunkSize, cylinders ? cylinders.endBuffer.ref.value : initialCount);
const scales = util_1.ChunkedArray.create(Float32Array, 1, chunkSize, cylinders ? cylinders.scaleBuffer.ref.value : initialCount);
const caps = util_1.ChunkedArray.create(Float32Array, 1, chunkSize, cylinders ? cylinders.capBuffer.ref.value : initialCount);
const colorModes = util_1.ChunkedArray.create(Float32Array, 1, chunkSize, cylinders ? cylinders.colorModeBuffer.ref.value : initialCount);
const add = (startX, startY, startZ, endX, endY, endZ, radiusScale, topCap, bottomCap, colorMode, group) => {
for (let i = 0; i < 6; ++i) {
caAdd3(starts, startX, startY, startZ);
caAdd3(ends, endX, endY, endZ);
caAdd(groups, group);
caAdd(scales, radiusScale);
caAdd(caps, (topCap ? 1 : 0) + (bottomCap ? 2 : 0));
caAdd(colorModes, colorMode);
}
};
const addFixedCountDashes = (start, end, segmentCount, radiusScale, topCap, bottomCap, stubCap, interpolate, group) => {
const d = linear_algebra_1.Vec3.distance(start, end);
const isOdd = segmentCount % 2 !== 0;
const s = Math.floor((segmentCount + 1) / 2);
const step = d / (segmentCount + 0.5);
let colorMode = 2.0;
linear_algebra_1.Vec3.setMagnitude(tmpDir, linear_algebra_1.Vec3.sub(tmpDir, end, start), step);
linear_algebra_1.Vec3.copy(tmpVecA, start);
for (let j = 0; j < s; ++j) {
linear_algebra_1.Vec3.add(tmpVecA, tmpVecA, tmpDir);
if (isOdd && j === s - 1) {
linear_algebra_1.Vec3.copy(tmpVecB, end);
if (!stubCap)
bottomCap = false;
}
else {
linear_algebra_1.Vec3.add(tmpVecB, tmpVecA, tmpDir);
}
if (interpolate) {
colorMode = linear_algebra_1.Vec3.distance(start, tmpVecB) / (d * 2);
}
add(tmpVecA[0], tmpVecA[1], tmpVecA[2], tmpVecB[0], tmpVecB[1], tmpVecB[2], radiusScale, topCap, bottomCap, colorMode, group);
linear_algebra_1.Vec3.add(tmpVecA, tmpVecA, tmpDir);
}
};
return {
add,
addFixedCountDashes,
addFixedLengthDashes: (start, end, segmentLength, radiusScale, topCap, bottomCap, interpolate, group) => {
const d = linear_algebra_1.Vec3.distance(start, end);
addFixedCountDashes(start, end, d / segmentLength, radiusScale, topCap, bottomCap, true, interpolate, group);
},
getCylinders: () => {
const cylinderCount = groups.elementCount / 6;
const gb = util_1.ChunkedArray.compact(groups, true);
const sb = util_1.ChunkedArray.compact(starts, true);
const eb = util_1.ChunkedArray.compact(ends, true);
const ab = util_1.ChunkedArray.compact(scales, true);
const cb = util_1.ChunkedArray.compact(caps, true);
const cmb = util_1.ChunkedArray.compact(colorModes, true);
const mb = cylinders && cylinderCount <= cylinders.cylinderCount ? cylinders.mappingBuffer.ref.value : new Float32Array(cylinderCount * 18);
const ib = cylinders && cylinderCount <= cylinders.cylinderCount ? cylinders.indexBuffer.ref.value : new Uint32Array(cylinderCount * 12);
if (!cylinders || cylinderCount > cylinders.cylinderCount)
fillMappingAndIndices(cylinderCount, mb, ib);
return cylinders_1.Cylinders.create(mb, ib, gb, sb, eb, ab, cb, cmb, cylinderCount, cylinders);
}
};
}
CylindersBuilder.create = create;
})(CylindersBuilder || (exports.CylindersBuilder = CylindersBuilder = {}));
function fillMappingAndIndices(n, mb, ib) {
for (let i = 0; i < n; ++i) {
const mo = i * 18;
mb[mo] = -1;
mb[mo + 1] = 1;
mb[mo + 2] = -1;
mb[mo + 3] = -1;
mb[mo + 4] = -1;
mb[mo + 5] = -1;
mb[mo + 6] = 1;
mb[mo + 7] = 1;
mb[mo + 8] = -1;
mb[mo + 9] = 1;
mb[mo + 10] = 1;
mb[mo + 11] = 1;
mb[mo + 12] = 1;
mb[mo + 13] = -1;
mb[mo + 14] = -1;
mb[mo + 15] = 1;
mb[mo + 16] = -1;
mb[mo + 17] = 1;
}
for (let i = 0; i < n; ++i) {
const o = i * 6;
const io = i * 12;
ib[io] = o;
ib[io + 1] = o + 1;
ib[io + 2] = o + 2;
ib[io + 3] = o + 1;
ib[io + 4] = o + 4;
ib[io + 5] = o + 2;
ib[io + 6] = o + 2;
ib[io + 7] = o + 4;
ib[io + 8] = o + 3;
ib[io + 9] = o + 4;
ib[io + 10] = o + 5;
ib[io + 11] = o + 3;
}
}
;