UNPKG

molstar

Version:

A comprehensive macromolecular library.

186 lines 7.46 kB
"use strict"; /** * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.HexagonalPrismCage = exports.PentagonalPrismCage = exports.DiamondPrismCage = exports.PrismCage = exports.HeptagonalPrism = exports.ShiftedHexagonalPrism = exports.HexagonalPrism = exports.PentagonalPrism = exports.DiamondPrism = exports.Prism = exports.DefaultPrismProps = void 0; var tslib_1 = require("tslib"); var linear_algebra_1 = require("../../mol-math/linear-algebra"); var primitive_1 = require("./primitive"); var polygon_1 = require("./polygon"); var on = (0, linear_algebra_1.Vec3)(), op = (0, linear_algebra_1.Vec3)(); var a = (0, linear_algebra_1.Vec3)(), b = (0, linear_algebra_1.Vec3)(), c = (0, linear_algebra_1.Vec3)(), d = (0, linear_algebra_1.Vec3)(); exports.DefaultPrismProps = { height: 1, topCap: true, bottomCap: true, }; /** * Create a prism with a base of 3 or more points */ 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 = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, exports.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 = (0, primitive_1.PrimitiveBuilder)(triangleCount, vertexCount); var halfHeight = height * 0.5; linear_algebra_1.Vec3.set(on, 0, 0, -halfHeight); linear_algebra_1.Vec3.set(op, 0, 0, halfHeight); // create sides for (var i = 0; i < sideCount; ++i) { var ni = (i + 1) % sideCount; linear_algebra_1.Vec3.set(a, points[i * 3], points[i * 3 + 1], -halfHeight); linear_algebra_1.Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -halfHeight); linear_algebra_1.Vec3.set(c, points[ni * 3], points[ni * 3 + 1], halfHeight); linear_algebra_1.Vec3.set(d, points[i * 3], points[i * 3 + 1], halfHeight); builder.addQuad(a, b, c, d); } // create bases if (sideCount === 3) { if (topCap) { linear_algebra_1.Vec3.set(a, points[0], points[1], -halfHeight); linear_algebra_1.Vec3.set(b, points[3], points[4], -halfHeight); linear_algebra_1.Vec3.set(c, points[6], points[7], -halfHeight); builder.add(c, b, a); } if (bottomCap) { linear_algebra_1.Vec3.set(a, points[0], points[1], halfHeight); linear_algebra_1.Vec3.set(b, points[3], points[4], halfHeight); linear_algebra_1.Vec3.set(c, points[6], points[7], halfHeight); builder.add(a, b, c); } } else if (sideCount === 4) { if (topCap) { linear_algebra_1.Vec3.set(a, points[0], points[1], -halfHeight); linear_algebra_1.Vec3.set(b, points[3], points[4], -halfHeight); linear_algebra_1.Vec3.set(c, points[6], points[7], -halfHeight); linear_algebra_1.Vec3.set(d, points[9], points[10], -halfHeight); builder.addQuad(d, c, b, a); } if (bottomCap) { linear_algebra_1.Vec3.set(a, points[0], points[1], halfHeight); linear_algebra_1.Vec3.set(b, points[3], points[4], halfHeight); linear_algebra_1.Vec3.set(c, points[6], points[7], halfHeight); linear_algebra_1.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) { linear_algebra_1.Vec3.set(a, points[i * 3], points[i * 3 + 1], -halfHeight); linear_algebra_1.Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -halfHeight); builder.add(on, b, a); } if (bottomCap) { linear_algebra_1.Vec3.set(a, points[i * 3], points[i * 3 + 1], halfHeight); linear_algebra_1.Vec3.set(b, points[ni * 3], points[ni * 3 + 1], halfHeight); builder.add(a, b, op); } } } return builder.getPrimitive(); } exports.Prism = Prism; var diamond; function DiamondPrism() { if (!diamond) diamond = Prism((0, polygon_1.polygon)(4, false)); return diamond; } exports.DiamondPrism = DiamondPrism; var pentagonalPrism; function PentagonalPrism() { if (!pentagonalPrism) pentagonalPrism = Prism((0, polygon_1.polygon)(5, false)); return pentagonalPrism; } exports.PentagonalPrism = PentagonalPrism; var hexagonalPrism; function HexagonalPrism() { if (!hexagonalPrism) hexagonalPrism = Prism((0, polygon_1.polygon)(6, false)); return hexagonalPrism; } exports.HexagonalPrism = HexagonalPrism; var shiftedHexagonalPrism; function ShiftedHexagonalPrism() { if (!shiftedHexagonalPrism) shiftedHexagonalPrism = Prism((0, polygon_1.polygon)(6, true)); return shiftedHexagonalPrism; } exports.ShiftedHexagonalPrism = ShiftedHexagonalPrism; var heptagonalPrism; function HeptagonalPrism() { if (!heptagonalPrism) heptagonalPrism = Prism((0, polygon_1.polygon)(7, false)); return heptagonalPrism; } exports.HeptagonalPrism = HeptagonalPrism; // /** * Create a prism cage */ 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 }; } exports.PrismCage = PrismCage; var diamondCage; function DiamondPrismCage() { if (!diamondCage) diamondCage = PrismCage((0, polygon_1.polygon)(4, false)); return diamondCage; } exports.DiamondPrismCage = DiamondPrismCage; var pentagonalPrismCage; function PentagonalPrismCage() { if (!pentagonalPrismCage) pentagonalPrismCage = PrismCage((0, polygon_1.polygon)(5, false)); return pentagonalPrismCage; } exports.PentagonalPrismCage = PentagonalPrismCage; var hexagonalPrismCage; function HexagonalPrismCage() { if (!hexagonalPrismCage) hexagonalPrismCage = PrismCage((0, polygon_1.polygon)(6, false)); return hexagonalPrismCage; } exports.HexagonalPrismCage = HexagonalPrismCage; //# sourceMappingURL=prism.js.map