Tetrahedron.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. import WhiteBoxMath as whiteBoxMath
  7. import WhiteBoxInit as init
  8. import argparse
  9. import azlmbr.legacy.general as general
  10. import azlmbr.bus as bus
  11. import azlmbr.whitebox.api as api
  12. # usage: pyRunFile path/to/file/tetrahedron.py <radius>
  13. # mathematically, a tetrahedron built with spherical coordinates will not be centered vertically at origin
  14. # we need to calculate how far off it is to vertically center it
  15. def calculate_offset_for_tetrahedron(radius):
  16. h1 = whiteBoxMath.spherical_to_cartesian(0.0, 0.0, radius).z
  17. h2 = whiteBoxMath.spherical_to_cartesian(109.5, 90.0, radius).z
  18. offset = (h1 + h2) * -0.5
  19. return whiteBoxMath.spherical_to_cartesian(0.0, 0.0, offset)
  20. def create_tetrahedron(whiteBoxMesh, radius=0.75):
  21. # get coordinates for all the vertices using the internal angles of a tetrahedron
  22. offset = calculate_offset_for_tetrahedron(radius)
  23. pos1 = whiteBoxMath.spherical_to_cartesian(0.0, 0.0, radius).Add(offset)
  24. pos2 = whiteBoxMath.spherical_to_cartesian(109.5, 90.0, radius).Add(offset)
  25. pos3 = whiteBoxMath.spherical_to_cartesian(109.5, 210.0, radius).Add(offset)
  26. pos4 = whiteBoxMath.spherical_to_cartesian(109.5, 330.0, radius).Add(offset)
  27. # create vertices from all the coordinates
  28. v1 = whiteBoxMesh.AddVertex(pos1)
  29. v2 = whiteBoxMesh.AddVertex(pos2)
  30. v3 = whiteBoxMesh.AddVertex(pos3)
  31. v4 = whiteBoxMesh.AddVertex(pos4)
  32. # add polygons for each set of vertices
  33. fvh1 = whiteBoxMesh.AddTriPolygon(v1, v2, v3)
  34. fvh2 = whiteBoxMesh.AddTriPolygon(v1, v3, v4)
  35. fvh3 = whiteBoxMesh.AddTriPolygon(v1, v4, v2)
  36. fvh4 = whiteBoxMesh.AddTriPolygon(v2, v4, v3)
  37. if __name__ == "__main__":
  38. parser = argparse.ArgumentParser(description='Creates a tetrahedron.')
  39. parser.add_argument('radius', nargs='?', default=0.75, type=float, help='radius of the tetrahedron')
  40. args = parser.parse_args()
  41. # initialize whiteBoxMesh
  42. whiteBoxEntity = init.create_white_box_entity("WhiteBox-Tetrahedron")
  43. whiteBoxMeshComponent = init.create_white_box_component(whiteBoxEntity)
  44. whiteBoxMesh = init.create_white_box_handle(whiteBoxMeshComponent)
  45. # clear whiteBoxMesh to make a tetrahedron from scratch
  46. whiteBoxMesh.Clear()
  47. create_tetrahedron(whiteBoxMesh, args.radius)
  48. # update whiteBoxMesh
  49. init.update_white_box(whiteBoxMesh, whiteBoxMeshComponent)