From 260dfb41dbdcece573f81fcae281078afd6d7e84 Mon Sep 17 00:00:00 2001 From: Alexander Rose <alexander.rose@weirdbyte.de> Date: Sun, 10 Mar 2019 17:12:20 -0700 Subject: [PATCH] prism and pyramid cage --- src/mol-geo/primitive/prism.ts | 57 +++++++++++++++++++++++++++++++- src/mol-geo/primitive/pyramid.ts | 40 ++++++++++++++++++++-- src/mol-geo/primitive/wedge.ts | 8 +++++ src/tests/browser/render-mesh.ts | 9 ++++- 4 files changed, 110 insertions(+), 4 deletions(-) diff --git a/src/mol-geo/primitive/prism.ts b/src/mol-geo/primitive/prism.ts index 8279e8916..d848f2d61 100644 --- a/src/mol-geo/primitive/prism.ts +++ b/src/mol-geo/primitive/prism.ts @@ -7,12 +7,13 @@ import { Vec3 } from 'mol-math/linear-algebra' import { Primitive, PrimitiveBuilder } from './primitive'; import { polygon } from './polygon' +import { Cage } from './cage'; const on = Vec3.create(0, 0, -0.5), op = Vec3.create(0, 0, 0.5) const a = Vec3.zero(), b = Vec3.zero(), c = Vec3.zero(), d = Vec3.zero() /** - * Create a prism with a polygonal base of 4 or more points + * Create a prism with a base of 4 or more points */ export function Prism(points: ArrayLike<number>): Primitive { const sideCount = points.length / 3 @@ -62,4 +63,58 @@ let hexagonalPrism: Primitive export function HexagonalPrism() { if (!hexagonalPrism) hexagonalPrism = Prism(polygon(6, true)) return hexagonalPrism +} + +// + +/** + * Create a prism cage + */ +export function PrismCage(points: ArrayLike<number>): Cage { + const sideCount = points.length / 3 + + // const count = 4 * sideCount + const vertices: number[] = [] + const edges: number[] = [] + + let offset = 0 + + // vertices and side edges + for (let i = 0; i < sideCount; ++i) { + vertices.push( + points[i * 3], points[i * 3 + 1], -0.5, + points[i * 3], points[i * 3 + 1], 0.5 + ) + edges.push(offset, offset + 1) + offset += 2 + } + + // bases edges + for (let i = 0; i < sideCount; ++i) { + const ni = (i + 1) % sideCount + edges.push( + i * 2, ni * 2, + i * 2 + 1, ni * 2 + 1 + ) + } + + return { vertices, edges } +} + +let diamondCage: Cage +export function DiamondPrismCage() { + if (!diamondCage) diamondCage = PrismCage(polygon(4, false)) + return diamondCage +} + +let pentagonalPrismCage: Cage +export function PentagonalPrismCage() { + if (!pentagonalPrismCage) pentagonalPrismCage = PrismCage(polygon(5, false)) + return pentagonalPrismCage +} + +let hexagonalPrismCage: Cage +export function HexagonalPrismCage() { + if (!hexagonalPrismCage) hexagonalPrismCage = PrismCage(polygon(6, true)) + return hexagonalPrismCage } \ No newline at end of file diff --git a/src/mol-geo/primitive/pyramid.ts b/src/mol-geo/primitive/pyramid.ts index 829f25975..242dcbae5 100644 --- a/src/mol-geo/primitive/pyramid.ts +++ b/src/mol-geo/primitive/pyramid.ts @@ -7,6 +7,7 @@ import { Vec3 } from 'mol-math/linear-algebra' import { Primitive, PrimitiveBuilder, createPrimitive } from './primitive'; import { polygon } from './polygon' +import { Cage } from './cage'; const on = Vec3.create(0, 0, -0.5), op = Vec3.create(0, 0, 0.5) const a = Vec3.zero(), b = Vec3.zero(), c = Vec3.zero(), d = Vec3.zero() @@ -59,8 +60,6 @@ export function OctagonalPyramid() { return octagonalPyramid } -// - let perforatedOctagonalPyramid: Primitive export function PerforatedOctagonalPyramid() { if (!perforatedOctagonalPyramid) { @@ -84,4 +83,41 @@ export function PerforatedOctagonalPyramid() { perforatedOctagonalPyramid = createPrimitive(vertices, indices) } return perforatedOctagonalPyramid +} + +// + +/** + * Create a prism cage + */ +export function PyramidCage(points: ArrayLike<number>): Cage { + const sideCount = points.length / 3 + + // const count = 4 * sideCount + const vertices: number[] = [] + const edges: number[] = [] + + let offset = 1 + vertices.push(op[0], op[1], op[2]) + + // vertices and side edges + for (let i = 0; i < sideCount; ++i) { + vertices.push(points[i * 3], points[i * 3 + 1], -0.5) + edges.push(0, offset) + offset += 1 + } + + // bases edges + for (let i = 0; i < sideCount; ++i) { + const ni = (i + 1) % sideCount + edges.push(i + 1, ni + 1) + } + + return { vertices, edges } +} + +let octagonalPyramidCage: Cage +export function OctagonalPyramidCage() { + if (!octagonalPyramidCage) octagonalPyramidCage = PyramidCage(polygon(8, true)) + return octagonalPyramidCage } \ No newline at end of file diff --git a/src/mol-geo/primitive/wedge.ts b/src/mol-geo/primitive/wedge.ts index 1d1a6b0b0..a39125f4f 100644 --- a/src/mol-geo/primitive/wedge.ts +++ b/src/mol-geo/primitive/wedge.ts @@ -7,6 +7,8 @@ import { Vec3 } from 'mol-math/linear-algebra' import { Primitive, PrimitiveBuilder } from './primitive'; import { polygon } from './polygon' +import { PrismCage } from './prism'; +import { Cage } from './cage'; const a = Vec3.zero(), b = Vec3.zero(), c = Vec3.zero(), d = Vec3.zero() const points = polygon(3, false) @@ -45,4 +47,10 @@ let wedge: Primitive export function Wedge() { if (!wedge) wedge = createWedge() return wedge +} + +let wedgeCage: Cage +export function WedgeCage() { + if (!wedgeCage) wedgeCage = PrismCage(points) + return wedgeCage } \ No newline at end of file diff --git a/src/tests/browser/render-mesh.ts b/src/tests/browser/render-mesh.ts index 6ee9ff1e2..4c63a2eb9 100644 --- a/src/tests/browser/render-mesh.ts +++ b/src/tests/browser/render-mesh.ts @@ -13,6 +13,7 @@ import { Representation } from 'mol-repr/representation'; import { Color } from 'mol-util/color'; import { createRenderObject } from 'mol-gl/render-object'; import { SpikedBall } from 'mol-geo/primitive/spiked-ball'; +import { HexagonalPrismCage } from 'mol-geo/primitive/prism'; const parent = document.getElementById('app')! parent.style.width = '100%' @@ -28,8 +29,14 @@ canvas3d.animate() function meshRepr() { const builderState = MeshBuilder.createState() + const t = Mat4.identity() - MeshBuilder.addPrimitive(builderState, t, SpikedBall(3)) + MeshBuilder.addCage(builderState, t, HexagonalPrismCage(), 0.005, 2) + + const t2 = Mat4.identity() + Mat4.scaleUniformly(t2, t2, 0.1) + MeshBuilder.addPrimitive(builderState, t2, SpikedBall(3)) + const mesh = MeshBuilder.getMesh(builderState) const values = Mesh.Utils.createValuesSimple(mesh, {}, Color(0xFF0000), 1) -- GitLab