Skip to content

Commit

Permalink
Merge pull request #92 from PolyPhyHub/feature/3d-discrete-gui-enhanc…
Browse files Browse the repository at this point in the history
…ements

Enhanced 3D_discrete simulation GUI with keyboard navigation for improved user control
  • Loading branch information
pjflux2001 authored May 25, 2024
2 parents d325f9e + e6c8a4e commit acfbd7f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/polyphy/core/discrete3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,22 @@ def __init__(self,
self.hide_UI = not self.hide_UI
if window.event.key in [ti.ui.ESCAPE]:
self.do_quit = True
# Handle keyboard controls
if window.event.key == ti.ui.UP:
camera_polar -= 0.1
camera_polar = max(0.01, min(np.pi - 0.01, camera_polar))
if window.event.key == ti.ui.DOWN:
camera_polar += 0.1
camera_polar = max(0.01, min(np.pi - 0.01, camera_polar))
if window.event.key == ti.ui.LEFT:
camera_azimuth -= 0.1
if window.event.key == ti.ui.RIGHT:
camera_azimuth += 0.1
if window.event.key == ti.ui.PLUS:
camera_distance -= 0.1 * ppConfig.DOMAIN_SIZE_MAX
camera_distance = max(0.85 * ppConfig.DOMAIN_SIZE_MAX, camera_distance)
if window.event.key == ti.ui.MINUS:
camera_distance += 0.1 * ppConfig.DOMAIN_SIZE_MAX

# Handle camera control: rotation
mouse_pos = window.get_cursor_pos()
Expand Down Expand Up @@ -342,6 +358,28 @@ def __init__(self,
ppInternalData.trace_field,
ppInternalData.vis_field)

# Handle 3D rendering
if not batch_mode:
if window.is_pressed(ti.ui.LMB):
x = int(mouse_pos[0] * canvas.get_image().shape[1])
y = int(mouse_pos[1] * canvas.get_image().shape[0])
if (x >= 0) and (y >= 0) and (x < canvas.get_image().shape[1]) and (y < canvas.get_image().shape[0]):
pixels = np.array(canvas.get_image())
rgb = pixels[y, x, :]
lum = np.sqrt(0.299 * rgb[0] ** 2 + 0.587 * rgb[1] ** 2 + 0.114 * rgb[2] ** 2)
Logger.logToStdOut("info", "Luminosity: %.2f" % (lum))

camera_pos = np.array([
camera_distance * np.sin(camera_polar) * np.sin(camera_azimuth),
camera_distance * np.cos(camera_polar),
camera_distance * np.sin(camera_polar) * np.cos(camera_azimuth)
])
camera_dir = -camera_pos / np.linalg.norm(camera_pos)
camera_up = np.array([0.0, 1.0, 0.0])
canvas.set_background_color((0.0, 0.0, 0.0))
scene = ti.ui.Scene()
canvas.scene(scene, camera_pos, camera_dir, camera_up)

if batch_mode is False:
canvas.set_image(ppInternalData.vis_field)
if self.do_screenshot:
Expand Down

0 comments on commit acfbd7f

Please sign in to comment.