Browse Source

wip, polygon refactoring

Alexander Rose 6 years ago
parent
commit
6cef11b4d8

+ 11 - 11
src/mol-geo/primitive/box.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
@@ -20,25 +20,25 @@ function createBox(perforated: boolean): Primitive {
     // create sides
     for (let i = 0; i < 4; ++i) {
         const ni = (i + 1) % 4
-        Vec3.set(a, points[i * 2], points[i * 2 + 1], -0.5)
-        Vec3.set(b, points[ni * 2], points[ni * 2 + 1], -0.5)
-        Vec3.set(c, points[ni * 2], points[ni * 2 + 1], 0.5)
-        Vec3.set(d, points[i * 2], points[i * 2 + 1], 0.5)
+        Vec3.set(a, points[i * 3], points[i * 3 + 1], -0.5)
+        Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -0.5)
+        Vec3.set(c, points[ni * 3], points[ni * 3 + 1], 0.5)
+        Vec3.set(d, points[i * 3], points[i * 3 + 1], 0.5)
         builder.add(a, b, c)
         if (!perforated) builder.add(c, d, a)
     }
 
     // create bases
     Vec3.set(a, points[0], points[1], -0.5)
-    Vec3.set(b, points[2], points[3], -0.5)
-    Vec3.set(c, points[4], points[5], -0.5)
-    Vec3.set(d, points[6], points[7], -0.5)
+    Vec3.set(b, points[3], points[4], -0.5)
+    Vec3.set(c, points[6], points[7], -0.5)
+    Vec3.set(d, points[9], points[10], -0.5)
     builder.add(c, b, a)
     if (!perforated) builder.add(a, d, c)
     Vec3.set(a, points[0], points[1], 0.5)
-    Vec3.set(b, points[2], points[3], 0.5)
-    Vec3.set(c, points[4], points[5], 0.5)
-    Vec3.set(d, points[6], points[7], 0.5)
+    Vec3.set(b, points[3], points[4], 0.5)
+    Vec3.set(c, points[6], points[7], 0.5)
+    Vec3.set(d, points[9], points[10], 0.5)
     builder.add(a, b, c)
     if (!perforated) builder.add(c, d, a)
 

+ 8 - 7
src/mol-geo/primitive/polygon.ts

@@ -1,23 +1,24 @@
 /**
- * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
 /**
- * Create points for a polygon:
+ * Create 3d points for a polygon:
  * 3 for a triangle, 4 for a rectangle, 5 for a pentagon, 6 for a hexagon...
  */
 export function polygon(sideCount: number, shift: boolean) {
-    const points = new Float32Array(sideCount * 2)
+    const points = new Float32Array(sideCount * 3)
     const radius = sideCount <= 4 ? Math.sqrt(2) / 2 : 0.6
 
     const offset = shift ? 1 : 0
 
-    for (let i = 0, il = 2 * sideCount; i < il; i += 2) {
-        const c = (i + offset) / sideCount * Math.PI
-        points[i] = Math.cos(c) * radius
-        points[i + 1] = Math.sin(c) * radius
+    for (let i = 0, il = sideCount; i < il; ++i) {
+        const c = (i * 2 + offset) / sideCount * Math.PI
+        points[i * 3] = Math.cos(c) * radius
+        points[i * 3 + 1] = Math.sin(c) * radius
+        points[i * 3 + 2] = 0
     }
     return points
 }

+ 12 - 12
src/mol-geo/primitive/prism.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
@@ -12,11 +12,11 @@ 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 poligonal base of 5 or more points
+ * Create a prism with a polygonal base of 4 or more points
  */
 export function Prism(points: ArrayLike<number>): Primitive {
-    const sideCount = points.length / 2
-    if (sideCount < 4) throw new Error('need at least 5 points to build a prism')
+    const sideCount = points.length / 3
+    if (sideCount < 4) throw new Error('need at least 4 points to build a prism')
 
     const count = 4 * sideCount
     const builder = PrimitiveBuilder(count)
@@ -24,10 +24,10 @@ export function Prism(points: ArrayLike<number>): Primitive {
     // create sides
     for (let i = 0; i < sideCount; ++i) {
         const ni = (i + 1) % sideCount
-        Vec3.set(a, points[i * 2], points[i * 2 + 1], -0.5)
-        Vec3.set(b, points[ni * 2], points[ni * 2 + 1], -0.5)
-        Vec3.set(c, points[ni * 2], points[ni * 2 + 1], 0.5)
-        Vec3.set(d, points[i * 2], points[i * 2 + 1], 0.5)
+        Vec3.set(a, points[i * 3], points[i * 3 + 1], -0.5)
+        Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -0.5)
+        Vec3.set(c, points[ni * 3], points[ni * 3 + 1], 0.5)
+        Vec3.set(d, points[i * 3], points[i * 3 + 1], 0.5)
         builder.add(a, b, c)
         builder.add(c, d, a)
     }
@@ -35,11 +35,11 @@ export function Prism(points: ArrayLike<number>): Primitive {
     // create bases
     for (let i = 0; i < sideCount; ++i) {
         const ni = (i + 1) % sideCount
-        Vec3.set(a, points[i * 2], points[i * 2 + 1], -0.5)
-        Vec3.set(b, points[ni * 2], points[ni * 2 + 1], -0.5)
+        Vec3.set(a, points[i * 3], points[i * 3 + 1], -0.5)
+        Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -0.5)
         builder.add(on, b, a)
-        Vec3.set(a, points[i * 2], points[i * 2 + 1], 0.5)
-        Vec3.set(b, points[ni * 2], points[ni * 2 + 1], 0.5)
+        Vec3.set(a, points[i * 3], points[i * 3 + 1], 0.5)
+        Vec3.set(b, points[ni * 3], points[ni * 3 + 1], 0.5)
         builder.add(a, b, op)
     }
 

+ 14 - 14
src/mol-geo/primitive/pyramid.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
@@ -12,10 +12,10 @@ 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 pyramid with a poligonal base
+ * Create a pyramid with a polygonal base
  */
 export function Pyramid(points: ArrayLike<number>): Primitive {
-    const sideCount = points.length / 2
+    const sideCount = points.length / 3
     const baseCount = sideCount === 3 ? 1 : sideCount === 4 ? 2 : sideCount
     const count = 2 * baseCount + 2 * sideCount
     const builder = PrimitiveBuilder(count)
@@ -23,29 +23,29 @@ export function Pyramid(points: ArrayLike<number>): Primitive {
     // create sides
     for (let i = 0; i < sideCount; ++i) {
         const ni = (i + 1) % sideCount
-        Vec3.set(a, points[i * 2], points[i * 2 + 1], -0.5)
-        Vec3.set(b, points[ni * 2], points[ni * 2 + 1], -0.5)
+        Vec3.set(a, points[i * 3], points[i * 3 + 1], -0.5)
+        Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -0.5)
         builder.add(a, b, op)
     }
 
     // create base
     if (sideCount === 3) {
         Vec3.set(a, points[0], points[1], -0.5)
-        Vec3.set(b, points[2], points[3], -0.5)
-        Vec3.set(c, points[4], points[5], -0.5)
+        Vec3.set(b, points[3], points[4], -0.5)
+        Vec3.set(c, points[6], points[7], -0.5)
         builder.add(c, b, a)
     } else if (sideCount === 4) {
         Vec3.set(a, points[0], points[1], -0.5)
-        Vec3.set(b, points[2], points[3], -0.5)
-        Vec3.set(c, points[4], points[5], -0.5)
-        Vec3.set(d, points[6], points[7], -0.5)
+        Vec3.set(b, points[3], points[4], -0.5)
+        Vec3.set(c, points[6], points[7], -0.5)
+        Vec3.set(d, points[9], points[10], -0.5)
         builder.add(c, b, a)
         builder.add(a, d, c)
     } else {
         for (let i = 0; i < sideCount; ++i) {
             const ni = (i + 1) % sideCount
-            Vec3.set(a, points[i * 2], points[i * 2 + 1], -0.5)
-            Vec3.set(b, points[ni * 2], points[ni * 2 + 1], -0.5)
+            Vec3.set(a, points[i * 3], points[i * 3 + 1], -0.5)
+            Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -0.5)
             builder.add(on, b, a)
         }
     }
@@ -67,8 +67,8 @@ export function PerforatedOctagonalPyramid() {
         const points = polygon(8, true)
         const vertices = new Float32Array(8 * 3 + 6)
         for (let i = 0; i < 8; ++i) {
-            vertices[i * 3] = points[i * 2]
-            vertices[i * 3 + 1] = points[i * 2 + 1]
+            vertices[i * 3] = points[i * 3]
+            vertices[i * 3 + 1] = points[i * 3 + 1]
             vertices[i * 3 + 2] = -0.5
         }
         vertices[8 * 3] = 0

+ 10 - 10
src/mol-geo/primitive/wedge.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
@@ -12,7 +12,7 @@ const a = Vec3.zero(), b = Vec3.zero(), c = Vec3.zero(), d = Vec3.zero()
 const points = polygon(3, false)
 
 /**
- * Create a prism with a poligonal base
+ * Create a prism with a triangular base
  */
 export function createWedge(): Primitive {
     const builder = PrimitiveBuilder(8)
@@ -20,22 +20,22 @@ export function createWedge(): Primitive {
     // create sides
     for (let i = 0; i < 3; ++i) {
         const ni = (i + 1) % 3
-        Vec3.set(a, points[i * 2], points[i * 2 + 1], -0.5)
-        Vec3.set(b, points[ni * 2], points[ni * 2 + 1], -0.5)
-        Vec3.set(c, points[ni * 2], points[ni * 2 + 1], 0.5)
-        Vec3.set(d, points[i * 2], points[i * 2 + 1], 0.5)
+        Vec3.set(a, points[i * 3], points[i * 3 + 1], -0.5)
+        Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -0.5)
+        Vec3.set(c, points[ni * 3], points[ni * 3 + 1], 0.5)
+        Vec3.set(d, points[i * 3], points[i * 3 + 1], 0.5)
         builder.add(a, b, c)
         builder.add(c, d, a)
     }
 
     // create bases
     Vec3.set(a, points[0], points[1], -0.5)
-    Vec3.set(b, points[2], points[3], -0.5)
-    Vec3.set(c, points[4], points[5], -0.5)
+    Vec3.set(b, points[3], points[4], -0.5)
+    Vec3.set(c, points[6], points[7], -0.5)
     builder.add(c, b, a)
     Vec3.set(a, points[0], points[1], 0.5)
-    Vec3.set(b, points[2], points[3], 0.5)
-    Vec3.set(c, points[4], points[5], 0.5)
+    Vec3.set(b, points[3], points[4], 0.5)
+    Vec3.set(c, points[6], points[7], 0.5)
     builder.add(a, b, c)
 
     return builder.getPrimitive()