Skip to content

Commit

Permalink
Utils organization (#3624)
Browse files Browse the repository at this point in the history
* Util organization

* Renaming utils

* Updating example scripts

* utils not needed in workflow_example
  • Loading branch information
ifranda authored Jun 26, 2024
1 parent cd0cd91 commit 755464c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 8 deletions.
7 changes: 4 additions & 3 deletions apps/pythonapi/examples/annotation_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#
# %%
import example_utils
from vapor import session, renderer, dataset, camera, utils
from vapor import session, renderer, dataset, camera
from vapor.utils import histogram

ses = session.Session()
data = example_utils.OpenExampleDataset(ses)
Expand Down Expand Up @@ -59,14 +60,14 @@
# ## MatPlotLib Histograms
#
# %%
utils.ShowMatPlotLibHistogram(ses, ren)
histogram.ShowMatPlotLibHistogram(ses, ren)

# %% [md]
# ---
# You can customize the histograms as you would `matplotlib.pyplot.hist`
#
# %%
plt = utils.GetMatPlotLibHistogram(ses, ren, color ="red")
plt = histogram.GetMatPlotLibHistogram(ses, ren, color ="red")
plt.xlabel("X-Axis")
plt.ylabel("Y-Axis")
plt.title("Title\n", fontweight="bold")
Expand Down
7 changes: 4 additions & 3 deletions apps/pythonapi/examples/transfer_function_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#
# %%
import example_utils
from vapor import session, renderer, dataset, camera, utils, transferfunction
from vapor import session, renderer, dataset, camera, transferfunction
from vapor.utils import histogram

ses = session.Session()
data = example_utils.OpenExampleDataset(ses)
Expand All @@ -20,7 +21,7 @@
# Before we adjust the opacity map of the TF, we get a histogram to help us determine what we want to hide.
#
# %%
utils.ShowMatPlotLibHistogram(ses, ren)
histogram.ShowMatPlotLibHistogram(ses, ren)

# %% [md]
#
Expand All @@ -35,7 +36,7 @@
# We can get the matplotlib histogram plot and add our opacity map to it to compare.
#
# %%
plt = utils.GetMatPlotLibHistogram(ses, ren)
plt = histogram.GetMatPlotLibHistogram(ses, ren)
plt.plot(*zip(*opacities))
plt.show()

Expand Down
2 changes: 1 addition & 1 deletion apps/pythonapi/examples/workflow_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#
# %%
import example_utils
from vapor import session, renderer, dataset, camera, utils
from vapor import session, renderer, dataset, camera

# %%
ses = session.Session()
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .session import *
from ..session import *
import matplotlib.pyplot as plt
import numpy as np
import functools
Expand Down
92 changes: 92 additions & 0 deletions apps/pythonapi/vapor/utils/keyframing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from ..session import Session
from ..animation import Animation

def animate_camera_keyframes(session_paths, steps = None):
"""
Given a list of file paths to sessions with different camera angles, returns an
animation that performs keyframing with linear interpolation between those camera angles.
"""
# Default for steps
if steps == None:
steps = [30] * (len(session_paths) - 1)

# Parameter checks
if len(session_paths) - len(steps) != 1:
raise TypeError(f"With {len(session_paths)} keyframes given, 'steps' should be " +
f"length {len(session_paths) - 1} (currently {len(steps)})")

# Load key frames as sessions
key_frames = []
for path in session_paths:
ses = Session()
ses.Load(path)
key_frames.append(ses)

# Visualization will use renderers from first session in list. Other sessions are only for camera angles
primary_session = key_frames[0]
anim = Animation(primary_session)
cam = primary_session.GetCamera()
total_frames = sum(steps)

# Interpolate camera information between each key frame
n = 0
for i in range(len(key_frames) - 1):
start = key_frames[i]
end = key_frames[i+1]
frames = steps[i]
# Get starting information
cam1 = start.GetCamera()
dir1 = cam1.GetDirection()
pos1 = cam1.GetPosition()
up1 = cam1.GetUp()

# Get ending information
cam2 = end.GetCamera()
dir2 = cam2.GetDirection()
pos2 = cam2.GetPosition()
up2 = cam2.GetUp()

# Difference between camera positions on each axis
dPositionX = (pos2[0] - pos1[0])
dPositionY = (pos2[1] - pos1[1])
dPositionZ = (pos2[2] - pos1[2])

# Difference between camera direction vectors on each axis
dDirectionX = (dir2[0] - dir1[0])
dDirectionY = (dir2[1] - dir1[1])
dDirectionZ = (dir2[2] - dir1[2])

# Difference between camera up vectors on each axis
dUpX = (up2[0] - up1[0])
dUpY = (up2[1] - up1[1])
dUpZ = (up2[2] - up1[2])

# Linear interpolation between start and end
for j in range(frames):
position = [
pos1[0]+dPositionX*j/frames,
pos1[1]+dPositionY*j/frames,
pos1[2]+dPositionZ*j/frames
]
cam.SetPosition( position )

direction = [
dir1[0]+dDirectionX*j/frames,
dir1[1]+dDirectionY*j/frames,
dir1[2]+dDirectionZ*j/frames
]
cam.SetDirection( direction )

up = [
up1[0]+dUpX*j/frames,
up1[1]+dUpY*j/frames,
up1[2]+dUpZ*j/frames
]
cam.SetUp( up )
anim.CaptureFrame()

# Print status
print(f"Rendering Animation [{'#'*round((j+n)*40/total_frames)}{' '*round(40-((j+n)*40/total_frames))}] {(j+1+n)*100/total_frames:.0f}%", end="\r")
n += steps[i]
return anim

0 comments on commit 755464c

Please sign in to comment.