Icosahedron.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 azlmbr.legacy.general as general
  9. import azlmbr.bus as bus
  10. import azlmbr.whitebox.api as api
  11. # usage: pyRunFile path/to/file/icosahedron.py <radius>
  12. # create the faces which will be used in the icosahedron
  13. def create_icosahedron_faces(whiteBoxMesh, radius):
  14. # get coordinates for all the vertices using the internal angles of a icosahedron
  15. # upper side
  16. pos1 = whiteBoxMath.spherical_to_cartesian(0.0, 0.0, radius)
  17. pos2 = whiteBoxMath.spherical_to_cartesian(63.43, 18.0, radius)
  18. pos3 = whiteBoxMath.spherical_to_cartesian(63.43, 90.0, radius)
  19. pos4 = whiteBoxMath.spherical_to_cartesian(63.43, 162.0, radius)
  20. pos5 = whiteBoxMath.spherical_to_cartesian(63.43, 234.0, radius)
  21. pos6 = whiteBoxMath.spherical_to_cartesian(63.43, 306.0, radius)
  22. # lower side
  23. pos7 = whiteBoxMath.spherical_to_cartesian(180.0, 0.0, radius)
  24. pos8 = whiteBoxMath.spherical_to_cartesian(116.57, 52.0, radius)
  25. pos9 = whiteBoxMath.spherical_to_cartesian(116.57, 126.0, radius)
  26. pos10 = whiteBoxMath.spherical_to_cartesian(116.57, 198.0, radius)
  27. pos11 = whiteBoxMath.spherical_to_cartesian(116.57, 270.0, radius)
  28. pos12 = whiteBoxMath.spherical_to_cartesian(116.57, 342.0, radius)
  29. # create vertices from all the coordinates
  30. # upper side
  31. v1 = whiteBoxMesh.AddVertex(pos1)
  32. v2 = whiteBoxMesh.AddVertex(pos2)
  33. v3 = whiteBoxMesh.AddVertex(pos3)
  34. v4 = whiteBoxMesh.AddVertex(pos4)
  35. v5 = whiteBoxMesh.AddVertex(pos5)
  36. v6 = whiteBoxMesh.AddVertex(pos6)
  37. # lower side
  38. v7 = whiteBoxMesh.AddVertex(pos7)
  39. v8 = whiteBoxMesh.AddVertex(pos8)
  40. v9 = whiteBoxMesh.AddVertex(pos9)
  41. v10 = whiteBoxMesh.AddVertex(pos10)
  42. v11 = whiteBoxMesh.AddVertex(pos11)
  43. v12 = whiteBoxMesh.AddVertex(pos12)
  44. # add faces to list
  45. faces = []
  46. # upper side
  47. fvh1 = faces.append(api.util_MakeFaceVertHandles(v1, v2, v3))
  48. fvh2 = faces.append(api.util_MakeFaceVertHandles(v1, v3, v4))
  49. fvh3 = faces.append(api.util_MakeFaceVertHandles(v1, v4, v5))
  50. fvh4 = faces.append(api.util_MakeFaceVertHandles(v1, v5, v6))
  51. fvh5 = faces.append(api.util_MakeFaceVertHandles(v1, v6, v2))
  52. # lower side
  53. fvh6 = faces.append(api.util_MakeFaceVertHandles(v7, v12, v11))
  54. fvh7 = faces.append(api.util_MakeFaceVertHandles(v7, v11, v10))
  55. fvh8 = faces.append(api.util_MakeFaceVertHandles(v7, v10, v9))
  56. fvh9 = faces.append(api.util_MakeFaceVertHandles(v7, v9, v8))
  57. fvh10 = faces.append(api.util_MakeFaceVertHandles(v7, v8, v12))
  58. # middle side
  59. fvh11 = faces.append(api.util_MakeFaceVertHandles(v12, v8, v2))
  60. fvh12 = faces.append(api.util_MakeFaceVertHandles(v8, v9, v3))
  61. fvh13 = faces.append(api.util_MakeFaceVertHandles(v9, v10, v4))
  62. fvh14 = faces.append(api.util_MakeFaceVertHandles(v10, v11, v5))
  63. fvh15 = faces.append(api.util_MakeFaceVertHandles(v11, v12, v6))
  64. fvh16 = faces.append(api.util_MakeFaceVertHandles(v2, v8, v3))
  65. fvh17 = faces.append(api.util_MakeFaceVertHandles(v3, v9, v4))
  66. fvh18 = faces.append(api.util_MakeFaceVertHandles(v4, v10, v5))
  67. fvh19 = faces.append(api.util_MakeFaceVertHandles(v5, v11, v6))
  68. fvh20 = faces.append(api.util_MakeFaceVertHandles(v6, v12, v2))
  69. return faces
  70. def create_icosahedron(whiteBoxMesh, radius=0.6):
  71. # create list of faces to add to polygon
  72. icosahedron_faces = create_icosahedron_faces(whiteBoxMesh, radius)
  73. # add polygons to white box mesh
  74. for face in icosahedron_faces:
  75. whiteBoxMesh.AddPolygon([face])
  76. if __name__ == "__main__":
  77. parser = argparse.ArgumentParser(description='Creates an icosahedron.')
  78. parser.add_argument('radius', nargs='?', default=0.6, type=int, help='radius of the icosahedron')
  79. args = parser.parse_args()
  80. whiteBoxEntity = init.create_white_box_entity("WhiteBox-Icosahedron")
  81. whiteBoxMeshComponent = init.create_white_box_component(whiteBoxEntity)
  82. whiteBoxMesh = init.create_white_box_handle(whiteBoxMeshComponent)
  83. # clear whiteBoxMesh to make a icosahedron from scratch
  84. whiteBoxMesh.Clear()
  85. create_icosahedron(whiteBoxMesh, args.radius)
  86. # update whiteBoxMesh
  87. init.update_white_box(whiteBoxMesh, whiteBoxMeshComponent)