UNPKG

molstar

Version:

A comprehensive macromolecular library.

173 lines 6.03 kB
/** * 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 { Vec3 } from '../../mol-math/linear-algebra'; import { PrimitiveBuilder } from './primitive'; import { polygon } from './polygon'; var on = Vec3(), op = Vec3(); var a = Vec3(), b = Vec3(), c = Vec3(), d = Vec3(); export var DefaultPrismProps = { height: 1, topCap: true, bottomCap: true, }; /** * Create a prism with a base of 3 or more points */ export function Prism(points, props) { var sideCount = points.length / 3; if (sideCount < 3) throw new Error('need at least 3 points to build a prism'); var _a = __assign(__assign({}, DefaultPrismProps), props), height = _a.height, topCap = _a.topCap, bottomCap = _a.bottomCap; var triangleCount = sideCount * 2; var vertexCount = sideCount * 4; var capCount = (topCap ? 1 : 0) + (bottomCap ? 1 : 0); if (sideCount === 3) { triangleCount += capCount; vertexCount += capCount * 3; } else if (sideCount === 4) { triangleCount += capCount * 2; vertexCount += capCount * 4; } else { triangleCount += capCount * sideCount; vertexCount += capCount * sideCount * 3; } var builder = PrimitiveBuilder(triangleCount, vertexCount); var halfHeight = height * 0.5; Vec3.set(on, 0, 0, -halfHeight); Vec3.set(op, 0, 0, halfHeight); // create sides for (var i = 0; i < sideCount; ++i) { var ni = (i + 1) % sideCount; Vec3.set(a, points[i * 3], points[i * 3 + 1], -halfHeight); Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -halfHeight); Vec3.set(c, points[ni * 3], points[ni * 3 + 1], halfHeight); Vec3.set(d, points[i * 3], points[i * 3 + 1], halfHeight); builder.addQuad(a, b, c, d); } // create bases if (sideCount === 3) { if (topCap) { Vec3.set(a, points[0], points[1], -halfHeight); Vec3.set(b, points[3], points[4], -halfHeight); Vec3.set(c, points[6], points[7], -halfHeight); builder.add(c, b, a); } if (bottomCap) { Vec3.set(a, points[0], points[1], halfHeight); Vec3.set(b, points[3], points[4], halfHeight); Vec3.set(c, points[6], points[7], halfHeight); builder.add(a, b, c); } } else if (sideCount === 4) { if (topCap) { Vec3.set(a, points[0], points[1], -halfHeight); Vec3.set(b, points[3], points[4], -halfHeight); Vec3.set(c, points[6], points[7], -halfHeight); Vec3.set(d, points[9], points[10], -halfHeight); builder.addQuad(d, c, b, a); } if (bottomCap) { Vec3.set(a, points[0], points[1], halfHeight); Vec3.set(b, points[3], points[4], halfHeight); Vec3.set(c, points[6], points[7], halfHeight); Vec3.set(d, points[9], points[10], halfHeight); builder.addQuad(a, b, c, d); } } else { for (var i = 0; i < sideCount; ++i) { var ni = (i + 1) % sideCount; if (topCap) { Vec3.set(a, points[i * 3], points[i * 3 + 1], -halfHeight); Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -halfHeight); builder.add(on, b, a); } if (bottomCap) { Vec3.set(a, points[i * 3], points[i * 3 + 1], halfHeight); Vec3.set(b, points[ni * 3], points[ni * 3 + 1], halfHeight); builder.add(a, b, op); } } } return builder.getPrimitive(); } var diamond; export function DiamondPrism() { if (!diamond) diamond = Prism(polygon(4, false)); return diamond; } var pentagonalPrism; export function PentagonalPrism() { if (!pentagonalPrism) pentagonalPrism = Prism(polygon(5, false)); return pentagonalPrism; } var hexagonalPrism; export function HexagonalPrism() { if (!hexagonalPrism) hexagonalPrism = Prism(polygon(6, false)); return hexagonalPrism; } var shiftedHexagonalPrism; export function ShiftedHexagonalPrism() { if (!shiftedHexagonalPrism) shiftedHexagonalPrism = Prism(polygon(6, true)); return shiftedHexagonalPrism; } var heptagonalPrism; export function HeptagonalPrism() { if (!heptagonalPrism) heptagonalPrism = Prism(polygon(7, false)); return heptagonalPrism; } // /** * Create a prism cage */ export function PrismCage(points, height) { if (height === void 0) { height = 1; } var sideCount = points.length / 3; var vertices = []; var edges = []; var halfHeight = height * 0.5; var offset = 0; // vertices and side edges for (var i = 0; i < sideCount; ++i) { vertices.push(points[i * 3], points[i * 3 + 1], -halfHeight, points[i * 3], points[i * 3 + 1], halfHeight); edges.push(offset, offset + 1); offset += 2; } // bases edges for (var i = 0; i < sideCount; ++i) { var ni = (i + 1) % sideCount; edges.push(i * 2, ni * 2, i * 2 + 1, ni * 2 + 1); } return { vertices: vertices, edges: edges }; } var diamondCage; export function DiamondPrismCage() { if (!diamondCage) diamondCage = PrismCage(polygon(4, false)); return diamondCage; } var pentagonalPrismCage; export function PentagonalPrismCage() { if (!pentagonalPrismCage) pentagonalPrismCage = PrismCage(polygon(5, false)); return pentagonalPrismCage; } var hexagonalPrismCage; export function HexagonalPrismCage() { if (!hexagonalPrismCage) hexagonalPrismCage = PrismCage(polygon(6, false)); return hexagonalPrismCage; } //# sourceMappingURL=prism.js.map