pyramid.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Vec3 } from 'mol-math/linear-algebra'
  7. import { Primitive, PrimitiveBuilder, createPrimitive } from './primitive';
  8. import { polygon } from './polygon'
  9. const on = Vec3.create(0, 0, -0.5), op = Vec3.create(0, 0, 0.5)
  10. const a = Vec3.zero(), b = Vec3.zero(), c = Vec3.zero(), d = Vec3.zero()
  11. /**
  12. * Create a pyramid with a polygonal base
  13. */
  14. export function Pyramid(points: ArrayLike<number>): Primitive {
  15. const sideCount = points.length / 3
  16. const baseCount = sideCount === 3 ? 1 : sideCount === 4 ? 2 : sideCount
  17. const count = 2 * baseCount + 2 * sideCount
  18. const builder = PrimitiveBuilder(count)
  19. // create sides
  20. for (let i = 0; i < sideCount; ++i) {
  21. const ni = (i + 1) % sideCount
  22. Vec3.set(a, points[i * 3], points[i * 3 + 1], -0.5)
  23. Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -0.5)
  24. builder.add(a, b, op)
  25. }
  26. // create base
  27. if (sideCount === 3) {
  28. Vec3.set(a, points[0], points[1], -0.5)
  29. Vec3.set(b, points[3], points[4], -0.5)
  30. Vec3.set(c, points[6], points[7], -0.5)
  31. builder.add(c, b, a)
  32. } else if (sideCount === 4) {
  33. Vec3.set(a, points[0], points[1], -0.5)
  34. Vec3.set(b, points[3], points[4], -0.5)
  35. Vec3.set(c, points[6], points[7], -0.5)
  36. Vec3.set(d, points[9], points[10], -0.5)
  37. builder.add(c, b, a)
  38. builder.add(a, d, c)
  39. } else {
  40. for (let i = 0; i < sideCount; ++i) {
  41. const ni = (i + 1) % sideCount
  42. Vec3.set(a, points[i * 3], points[i * 3 + 1], -0.5)
  43. Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -0.5)
  44. builder.add(on, b, a)
  45. }
  46. }
  47. return builder.getPrimitive()
  48. }
  49. let octagonalPyramid: Primitive
  50. export function OctagonalPyramid() {
  51. if (!octagonalPyramid) octagonalPyramid = Pyramid(polygon(8, true))
  52. return octagonalPyramid
  53. }
  54. //
  55. let perforatedOctagonalPyramid: Primitive
  56. export function PerforatedOctagonalPyramid() {
  57. if (!perforatedOctagonalPyramid) {
  58. const points = polygon(8, true)
  59. const vertices = new Float32Array(8 * 3 + 6)
  60. for (let i = 0; i < 8; ++i) {
  61. vertices[i * 3] = points[i * 3]
  62. vertices[i * 3 + 1] = points[i * 3 + 1]
  63. vertices[i * 3 + 2] = -0.5
  64. }
  65. vertices[8 * 3] = 0
  66. vertices[8 * 3 + 1] = 0
  67. vertices[8 * 3 + 2] = -0.5
  68. vertices[8 * 3 + 3] = 0
  69. vertices[8 * 3 + 4] = 0
  70. vertices[8 * 3 + 5] = 0.5
  71. const indices: ReadonlyArray<number> = [
  72. 0, 1, 8, 1, 2, 8, 4, 5, 8, 5, 6, 8,
  73. 2, 3, 9, 3, 4, 9, 6, 7, 9, 7, 0, 9
  74. ];
  75. perforatedOctagonalPyramid = createPrimitive(vertices, indices)
  76. }
  77. return perforatedOctagonalPyramid
  78. }