You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
o3de/Gems/WhiteBox/Editor/Scripts/Tetrahedron.py

67 lines
2.5 KiB
Python

"""
Copyright (c) Contributors to the Open 3D Engine Project.
For complete copyright and license terms please see the LICENSE at the root of this distribution.
SPDX-License-Identifier: Apache-2.0 OR MIT
"""
import WhiteBoxMath as whiteBoxMath
import WhiteBoxInit as init
import argparse
import azlmbr.legacy.general as general
import azlmbr.bus as bus
import azlmbr.whitebox.api as api
# usage: pyRunFile path/to/file/tetrahedron.py <radius>
# mathematically, a tetrahedron built with spherical coordinates will not be centered vertically at origin
# we need to calculate how far off it is to vertically center it
def calculate_offset_for_tetrahedron(radius):
h1 = whiteBoxMath.spherical_to_cartesian(0.0, 0.0, radius).z
h2 = whiteBoxMath.spherical_to_cartesian(109.5, 90.0, radius).z
offset = (h1 + h2) * -0.5
return whiteBoxMath.spherical_to_cartesian(0.0, 0.0, offset)
def create_tetrahedron(whiteBoxMesh, radius=0.75):
# get coordinates for all the vertices using the internal angles of a tetrahedron
offset = calculate_offset_for_tetrahedron(radius)
pos1 = whiteBoxMath.spherical_to_cartesian(0.0, 0.0, radius).Add(offset)
pos2 = whiteBoxMath.spherical_to_cartesian(109.5, 90.0, radius).Add(offset)
pos3 = whiteBoxMath.spherical_to_cartesian(109.5, 210.0, radius).Add(offset)
pos4 = whiteBoxMath.spherical_to_cartesian(109.5, 330.0, radius).Add(offset)
# create vertices from all the coordinates
v1 = whiteBoxMesh.AddVertex(pos1)
v2 = whiteBoxMesh.AddVertex(pos2)
v3 = whiteBoxMesh.AddVertex(pos3)
v4 = whiteBoxMesh.AddVertex(pos4)
# add polygons for each set of vertices
fvh1 = whiteBoxMesh.AddTriPolygon(v1, v2, v3)
fvh2 = whiteBoxMesh.AddTriPolygon(v1, v3, v4)
fvh3 = whiteBoxMesh.AddTriPolygon(v1, v4, v2)
fvh4 = whiteBoxMesh.AddTriPolygon(v2, v4, v3)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Creates a tetrahedron.')
parser.add_argument('radius', nargs='?', default=0.75, type=float, help='radius of the tetrahedron')
args = parser.parse_args()
# initialize whiteBoxMesh
whiteBoxEntity = init.create_white_box_entity("WhiteBox-Tetrahedron")
whiteBoxMeshComponent = init.create_white_box_component(whiteBoxEntity)
whiteBoxMesh = init.create_white_box_handle(whiteBoxMeshComponent)
# clear whiteBoxMesh to make a tetrahedron from scratch
whiteBoxMesh.Clear()
create_tetrahedron(whiteBoxMesh, args.radius)
# update whiteBoxMesh
init.update_white_box(whiteBoxMesh, whiteBoxMeshComponent)