molstar
Version:
A comprehensive macromolecular library.
81 lines (80 loc) • 3.74 kB
JavaScript
/**
* Copyright (c) 2018-2020 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 { Vec3 } from '../../../../mol-math/linear-algebra';
import { ChunkedArray } from '../../../../mol-data/util';
// avoiding namespace lookup improved performance in Chrome (Aug 2020)
var v3fromArray = Vec3.fromArray;
var v3magnitude = Vec3.magnitude;
var v3sub = Vec3.sub;
var v3add = Vec3.add;
var v3scale = Vec3.scale;
var v3negate = Vec3.negate;
var v3copy = Vec3.copy;
var v3cross = Vec3.cross;
var caAdd3 = ChunkedArray.add3;
var caAdd = ChunkedArray.add;
var tA = Vec3();
var tB = Vec3();
var tV = Vec3();
var horizontalVector = Vec3();
var verticalVector = Vec3();
var normalOffset = Vec3();
var positionVector = Vec3();
var normalVector = Vec3();
var torsionVector = Vec3();
/** set arrowHeight = 0 for no arrow */
export function addRibbon(state, controlPoints, normalVectors, binormalVectors, linearSegments, widthValues, heightValues, arrowHeight) {
var currentGroup = state.currentGroup, vertices = state.vertices, normals = state.normals, indices = state.indices, groups = state.groups;
var vertexCount = vertices.elementCount;
var offsetLength = 0;
if (arrowHeight > 0) {
v3fromArray(tA, controlPoints, 0);
v3fromArray(tB, controlPoints, linearSegments * 3);
offsetLength = arrowHeight / v3magnitude(v3sub(tV, tB, tA));
}
for (var i = 0; i <= linearSegments; ++i) {
var width = widthValues[i];
var height = heightValues[i];
var actualHeight = arrowHeight === 0 ? height : arrowHeight * (1 - i / linearSegments);
var i3 = i * 3;
v3fromArray(verticalVector, normalVectors, i3);
v3scale(verticalVector, verticalVector, actualHeight);
v3fromArray(horizontalVector, binormalVectors, i3);
v3scale(horizontalVector, horizontalVector, width);
if (arrowHeight > 0) {
v3fromArray(tA, normalVectors, i3);
v3fromArray(tB, binormalVectors, i3);
v3scale(normalOffset, v3cross(normalOffset, tA, tB), offsetLength);
}
v3fromArray(positionVector, controlPoints, i3);
v3fromArray(normalVector, normalVectors, i3);
v3fromArray(torsionVector, binormalVectors, i3);
v3add(tA, positionVector, verticalVector);
v3negate(tB, torsionVector);
caAdd3(vertices, tA[0], tA[1], tA[2]);
caAdd3(normals, tB[0], tB[1], tB[2]);
v3sub(tA, positionVector, verticalVector);
caAdd3(vertices, tA[0], tA[1], tA[2]);
caAdd3(normals, tB[0], tB[1], tB[2]);
v3add(tA, positionVector, verticalVector);
v3copy(tB, torsionVector);
caAdd3(vertices, tA[0], tA[1], tA[2]);
caAdd3(normals, tB[0], tB[1], tB[2]);
v3sub(tA, positionVector, verticalVector);
caAdd3(vertices, tA[0], tA[1], tA[2]);
caAdd3(normals, tB[0], tB[1], tB[2]);
}
for (var i = 0; i < linearSegments; ++i) {
caAdd3(indices, vertexCount + i * 4, vertexCount + (i + 1) * 4 + 1, vertexCount + i * 4 + 1);
caAdd3(indices, vertexCount + i * 4, vertexCount + (i + 1) * 4, vertexCount + (i + 1) * 4 + 1);
caAdd3(indices, vertexCount + i * 4 + 2 + 1, vertexCount + (i + 1) * 4 + 2 + 1, vertexCount + i * 4 + 2);
caAdd3(indices, vertexCount + i * 4 + 2, vertexCount + (i + 1) * 4 + 2 + 1, vertexCount + (i + 1) * 4 + 2);
}
var addedVertexCount = (linearSegments + 1) * 4;
for (var i = 0, il = addedVertexCount; i < il; ++i)
caAdd(groups, currentGroup);
}