molstar
Version:
A comprehensive macromolecular library.
123 lines (122 loc) • 4.79 kB
JavaScript
/**
* Copyright (c) 2019-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { arrayEqual, UUID } from '../../../mol-util';
import { Column } from '../../../mol-data/db';
//
export { Time };
function Time(value, unit) {
return { value: value, unit: unit };
}
//
export { Coordinates };
var Coordinates;
(function (Coordinates) {
function create(frames, deltaTime, timeOffset) {
var hasCell = !!frames[0].cell;
var hasVelocities = !!frames[0].velocities;
var hasForces = !!frames[0].forces;
return {
id: UUID.create22(),
frames: frames,
hasCell: hasCell,
hasVelocities: hasVelocities,
hasForces: hasForces,
deltaTime: deltaTime,
timeOffset: timeOffset,
};
}
Coordinates.create = create;
/**
* Only use ordering if it's not identity.
*/
function getAtomicConformation(frame, atomId, ordering) {
var x = frame.x, y = frame.y, z = frame.z;
if (frame.xyzOrdering.frozen) {
if (ordering) {
if (frame.xyzOrdering.isIdentity) {
// simple list reordering
x = getOrderedCoords(x, ordering);
y = getOrderedCoords(y, ordering);
z = getOrderedCoords(z, ordering);
}
else if (!arrayEqual(frame.xyzOrdering.index, ordering)) {
x = getSourceOrderedCoords(x, frame.xyzOrdering.index, ordering);
y = getSourceOrderedCoords(y, frame.xyzOrdering.index, ordering);
z = getSourceOrderedCoords(z, frame.xyzOrdering.index, ordering);
}
}
else if (!frame.xyzOrdering.isIdentity) {
x = getInvertedCoords(x, frame.xyzOrdering.index);
y = getInvertedCoords(y, frame.xyzOrdering.index);
z = getInvertedCoords(z, frame.xyzOrdering.index);
}
}
else if (ordering) {
if (frame.xyzOrdering.isIdentity) {
frame.xyzOrdering.isIdentity = false;
frame.xyzOrdering.index = ordering;
reorderCoordsInPlace(x, ordering);
reorderCoordsInPlace(y, ordering);
reorderCoordsInPlace(z, ordering);
}
else {
// is current ordering is not the same as requested?
// => copy the conformations into a new array
if (!arrayEqual(frame.xyzOrdering.index, ordering)) {
x = getSourceOrderedCoords(x, frame.xyzOrdering.index, ordering);
y = getSourceOrderedCoords(y, frame.xyzOrdering.index, ordering);
z = getSourceOrderedCoords(z, frame.xyzOrdering.index, ordering);
}
}
}
// once the conformation has been accessed at least once, freeze it.
// => any other request to the frame with different ordering will result in a copy.
frame.xyzOrdering.frozen = true;
return {
id: UUID.create22(),
atomId: atomId,
occupancy: Column.ofConst(1, frame.elementCount, Column.Schema.int),
B_iso_or_equiv: Column.ofConst(0, frame.elementCount, Column.Schema.float),
xyzDefined: true,
x: x,
y: y,
z: z,
};
}
Coordinates.getAtomicConformation = getAtomicConformation;
var _reorderBuffer = [0.123];
function reorderCoordsInPlace(xs, index) {
var buffer = _reorderBuffer;
for (var i = 0, _i = xs.length; i < _i; i++) {
buffer[i] = xs[index[i]];
}
for (var i = 0, _i = xs.length; i < _i; i++) {
xs[i] = buffer[i];
}
}
function getSourceOrderedCoords(xs, srcIndex, index) {
var ret = new Float32Array(xs.length);
for (var i = 0, _i = xs.length; i < _i; i++) {
ret[i] = xs[srcIndex[index[i]]];
}
return ret;
}
function getOrderedCoords(xs, index) {
var ret = new Float32Array(xs.length);
for (var i = 0, _i = xs.length; i < _i; i++) {
ret[i] = xs[index[i]];
}
return ret;
}
function getInvertedCoords(xs, index) {
var ret = new Float32Array(xs.length);
for (var i = 0, _i = xs.length; i < _i; i++) {
ret[index[i]] = xs[i];
}
return ret;
}
})(Coordinates || (Coordinates = {}));