wedge.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 } from './primitive';
  8. import { polygon } from './polygon'
  9. import { PrismCage } from './prism';
  10. import { Cage } from './cage';
  11. const a = Vec3.zero(), b = Vec3.zero(), c = Vec3.zero(), d = Vec3.zero()
  12. const points = polygon(3, false)
  13. /**
  14. * Create a prism with a triangular base
  15. */
  16. export function createWedge(): Primitive {
  17. const builder = PrimitiveBuilder(8)
  18. // create sides
  19. for (let i = 0; i < 3; ++i) {
  20. const ni = (i + 1) % 3
  21. Vec3.set(a, points[i * 3], points[i * 3 + 1], -0.5)
  22. Vec3.set(b, points[ni * 3], points[ni * 3 + 1], -0.5)
  23. Vec3.set(c, points[ni * 3], points[ni * 3 + 1], 0.5)
  24. Vec3.set(d, points[i * 3], points[i * 3 + 1], 0.5)
  25. builder.add(a, b, c)
  26. builder.add(c, d, a)
  27. }
  28. // create bases
  29. Vec3.set(a, points[0], points[1], -0.5)
  30. Vec3.set(b, points[3], points[4], -0.5)
  31. Vec3.set(c, points[6], points[7], -0.5)
  32. builder.add(c, b, a)
  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. builder.add(a, b, c)
  37. return builder.getPrimitive()
  38. }
  39. let wedge: Primitive
  40. export function Wedge() {
  41. if (!wedge) wedge = createWedge()
  42. return wedge
  43. }
  44. let wedgeCage: Cage
  45. export function WedgeCage() {
  46. if (!wedgeCage) wedgeCage = PrismCage(points)
  47. return wedgeCage
  48. }