소스 검색

prism and pyramid cage

Alexander Rose 6 년 전
부모
커밋
260dfb41db
4개의 변경된 파일110개의 추가작업 그리고 4개의 파일을 삭제
  1. 56 1
      src/mol-geo/primitive/prism.ts
  2. 38 2
      src/mol-geo/primitive/pyramid.ts
  3. 8 0
      src/mol-geo/primitive/wedge.ts
  4. 8 1
      src/tests/browser/render-mesh.ts

+ 56 - 1
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
 }

+ 38 - 2
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
 }

+ 8 - 0
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
 }

+ 8 - 1
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)