Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Textured planes for slices, cutting planes and projections #14

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
71777ff
adding planes as textured objects
chrishavlin Apr 27, 2021
cb2bb67
transformation messed up, but planes are plotting
chrishavlin Apr 27, 2021
54b090c
check for baseplane instance so arbitrary planes work
chrishavlin Apr 27, 2021
6f2a167
silly image data on a plane
chrishavlin Apr 27, 2021
33984a9
more interesting silly image data slice
chrishavlin Apr 27, 2021
6969518
axis-normal planes seem to be working...
chrishavlin Apr 28, 2021
33e76c7
rename some variables for clarity
chrishavlin Apr 28, 2021
d4f5a6d
add colormap, log options to planes
chrishavlin Apr 28, 2021
7e166e6
simplifying cmap, log selection for plane
chrishavlin Apr 28, 2021
d9c4931
cleaning, improved vert shader projection, cutting plane works
chrishavlin Apr 29, 2021
fc74b3d
more sensible translation matrix, updated examples
chrishavlin Apr 30, 2021
daddce5
linting
chrishavlin May 4, 2021
10261f0
renaming, docstringing.
chrishavlin May 4, 2021
1eb4283
properly scaling by domain_width
chrishavlin May 4, 2021
35701a3
isort fixes
chrishavlin May 4, 2021
0731471
black fix
chrishavlin May 4, 2021
2a9a22b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 24, 2022
388bf00
remove unused import
chrishavlin Oct 25, 2022
66fa9d6
found the problem
chrishavlin Oct 26, 2022
dbc8f94
fixes
chrishavlin Oct 26, 2022
91a7999
blend the planes
chrishavlin Oct 26, 2022
a892165
use yt Orientation to handle basis vectors
chrishavlin Oct 26, 2022
c69155e
cleanup, update projection
chrishavlin Oct 26, 2022
390e1b8
more cleanup
chrishavlin Oct 26, 2022
8a4935e
render planes on both faces!!
chrishavlin Oct 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions examples/amr_volume_rendering_with_planes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# example showing how to add a static cutting plane to an amr rendering
import numpy as np
import yt
from unyt import unyt_quantity

import yt_idv
from yt_idv.scene_components.plane import Plane # NOQA
from yt_idv.scene_data.plane import PlaneData # NOQA

ds = yt.load_sample("IsolatedGalaxy")

# add the volume rendering to the scene
rc = yt_idv.render_context(height=800, width=800, gui=True)
sg = rc.add_scene(ds, "density", no_ghost=True)

field = ("index", "z")

# add some planes (these are independent objects, not linked to the volume rendering)

# slice
slc = ds.slice(0, 0.5)
slc_data = PlaneData(data_source=slc)
slc_data.add_data(field, 1.0, (400, 400))
slc_render = Plane(data=slc_data, cmap_log=True)
rc.scene.data_objects.append(slc_data)
rc.scene.components.append(slc_render)

# a second slice, off-center
slc = ds.slice(1, 0.5)
slc_data = PlaneData(data_source=slc)
slc_data.add_data(
field,
unyt_quantity(400, "kpc"),
(400, 400),
center=np.array([0.65, slc.coord, 0.5]),
)
slc_render = Plane(data=slc_data, cmap_log=True)
rc.scene.data_objects.append(slc_data)
rc.scene.components.append(slc_render)

# another slice, lower width
slc = ds.slice(2, 0.5)
slc_data = PlaneData(data_source=slc)
slc_data.add_data(
field,
unyt_quantity(400, "kpc"),
(400, 400),
)
slc_render = Plane(data=slc_data, cmap_log=True)
rc.scene.data_objects.append(slc_data)
rc.scene.components.append(slc_render)

# cutting plane
normal = np.array([1.0, 1.0, 0.0], dtype="float64")
center = ds.domain_center.to("code_length").value
cut = ds.cutting(normal, center)
cut_data = PlaneData(data_source=cut)
cut_data.add_data(field, 1.0, (400, 400))
cut_render = Plane(data=cut_data, cmap_log=True)
rc.scene.data_objects.append(cut_data)
rc.scene.components.append(cut_render)

# projection
proj = ds.proj(field, 0)
proj_data = PlaneData(data_source=proj)
proj_data.add_data(field, 1.0, (400, 400))
proj_render = Plane(data=proj_data, cmap_log=True)
rc.scene.data_objects.append(proj_data)
rc.scene.components.append(proj_render)

rc.run()
37 changes: 37 additions & 0 deletions examples/plane_textures_arbitrary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# plots arbitrary data on a plane

import numpy as np

from yt_idv import render_context
from yt_idv.cameras.trackball_camera import TrackballCamera
from yt_idv.scene_components.plane import Plane
from yt_idv.scene_data.plane import BasePlane
from yt_idv.scene_graph import SceneGraph

# create an empty scene with a camera
rc = render_context(height=800, width=800, gui=True)
c = TrackballCamera(position=[3.5, 3.5, 3.5], focus=[0.0, 0.0, 0.0])
rc.scene = SceneGraph(camera=c)

# create some arbitrary 2d data
x, y = np.meshgrid(np.linspace(0, 1, 200), np.linspace(0, 1, 300))
dist = np.sqrt((x - 0.5) ** 2 + (y - 0.5) ** 2)
test_data = np.exp(-((dist / 0.25) ** 2))

# create a plane for our 2d data, add the data
image_plane = BasePlane(
normal=np.array([1.0, 0.0, 0.0]),
center=np.array([0.0, 0.0, 0.0]),
data=test_data,
width=1,
height=1,
)
image_plane.north_vec = np.array([0.0, 0.0, 1.0])
image_plane.add_data()

# add the rendering object, data to the scene
plane_render = Plane(data=image_plane, cmap_log=False)
rc.scene.data_objects.append(image_plane)
rc.scene.components.append(plane_render)

rc.run()
70 changes: 70 additions & 0 deletions examples/plane_textures_arbitrary_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import numpy as np
import requests
import yt
from PIL import Image

import yt_idv
from yt_idv.scene_annotations.grid_outlines import GridOutlines # NOQA
from yt_idv.scene_components.plane import Plane
from yt_idv.scene_data.grid_positions import GridPositions # NOQA
from yt_idv.scene_data.plane import BasePlane

# create a volume rendering
ds = yt.load("IsolatedGalaxy/galaxy0030/galaxy0030")
rc = yt_idv.render_context(height=800, width=800, gui=True)
sg = rc.add_scene(ds, "density", no_ghost=True)

# pull in an image file, convert to greyscale and ignore resulting alpha channel
im_url = "https://raw.githubusercontent.com/yt-project/website/master/img/yt_logo.png"
im = np.array(Image.open(requests.get(im_url, stream=True).raw).convert("LA"))[:, :, 0]


# create a plane for our 2d data, add the data
image_plane = BasePlane(
normal=np.array([1.0, 1.0, 1.0]),
center=np.array([0.5, 0.5, 0.5]),
data=im,
width=0.5,
height=0.5,
)
# image_plane.east_vec = np.array([0.0, 1.0, 0.0])
# image_plane.north_vec = np.array([0.0, 0.0, 1.0])
image_plane.add_data()

# add the rendering object, data to the scene
plane_render = Plane(data=image_plane, cmap_log=False)
rc.scene.data_objects.append(image_plane)
rc.scene.components.append(plane_render)

# for norm in [
# np.array([1.0, 0.0, 0.0]),
# np.array([0.0, 1.0, 0.0]),
# np.array([0.0, 0.0, 1.0])
# ]:
#
# # create a plane for our 2d data, add the data
# image_plane = BasePlane(
# normal=norm,
# center=np.array([.5, .5, .5]),
# data=im,
# width=.5,
# height=.5,
# )
# # image_plane.east_vec = np.array([0.0, 1.0, 0.0])
# # image_plane.north_vec = np.array([0.0, 0.0, 1.0])
# image_plane.add_data()
#
# # add the rendering object, data to the scene
# plane_render = Plane(data=image_plane, cmap_log=False)
# rc.scene.data_objects.append(image_plane)
# rc.scene.components.append(plane_render)

# add grids
grids = ds.index.grids.tolist()
gp = GridPositions(grid_list=grids)
rc.scene.data_objects.append(gp)
go = GridOutlines(data=gp)
rc.scene.components.append(go)


rc.run()
35 changes: 35 additions & 0 deletions examples/plane_textures_cutting_plane.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import numpy as np
import yt

from yt_idv import render_context
from yt_idv.cameras.trackball_camera import TrackballCamera
from yt_idv.scene_annotations.grid_outlines import GridOutlines # NOQA
from yt_idv.scene_components.plane import Plane # NOQA
from yt_idv.scene_data.grid_positions import GridPositions # NOQA
from yt_idv.scene_data.plane import PlaneData # NOQA
from yt_idv.scene_graph import SceneGraph

rc = render_context(height=800, width=800, gui=True)
c = TrackballCamera(position=[3.5, 3.5, 3.5], focus=[0.0, 0.0, 0.0])
rc.scene = SceneGraph(camera=c)

ds = yt.load("Enzo_64/RD0005/RedshiftOutput0005")

normal = np.array([1.0, 1.0, 0.0], dtype="float64")
center = ds.domain_center.to("code_length").value
cut = ds.cutting(normal, center)
cut_data = PlaneData(data_source=cut)
cut_data.add_data(("enzo", "Density"), 1.0, (400, 400))
cut_render = Plane(data=cut_data, cmap_log=True)

rc.scene.data_objects.append(cut_data)
rc.scene.components.append(cut_render)

# add grids
grids = ds.index.grids.tolist()
gp = GridPositions(grid_list=grids)
rc.scene.data_objects.append(gp)
go = GridOutlines(data=gp)
rc.scene.components.append(go)

rc.run()
58 changes: 58 additions & 0 deletions examples/plane_textures_multiple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import numpy as np
import yt

from yt_idv import render_context
from yt_idv.cameras.trackball_camera import TrackballCamera
from yt_idv.scene_components.plane import Plane # NOQA
from yt_idv.scene_data.plane import PlaneData # NOQA
from yt_idv.scene_graph import SceneGraph

rc = render_context(height=800, width=800, gui=True)
c = TrackballCamera(position=[3.5, 3.5, 3.5], focus=[0.0, 0.0, 0.0])
rc.scene = SceneGraph(camera=c)

ds = yt.load("Enzo_64/RD0005/RedshiftOutput0005")

# add a projection
proj = ds.proj(("enzo", "Density"), 1.0)
proj_data = PlaneData(data_source=proj)
proj_data.add_data(("enzo", "Density"), 1.0, (400, 400))
proj_render = Plane(data=proj_data, cmap_log=True)
rc.scene.data_objects.append(proj_data)
rc.scene.components.append(proj_render)

# add another projection
proj_2 = ds.proj(("enzo", "Density"), 0.0)
proj_data_2 = PlaneData(data_source=proj_2)
proj_data_2.add_data(("enzo", "Density"), 1.0, (400, 400))
proj_render_2 = Plane(data=proj_data_2, cmap_log=True)
rc.scene.data_objects.append(proj_data_2)
rc.scene.components.append(proj_render_2)

# add another projection, shifted to other axis
proj_3 = ds.proj(("enzo", "Density"), 1.0)
proj_data_3 = PlaneData(data_source=proj_3)
proj_data_3.add_data(("enzo", "Density"), 1.0, (400, 400), translate=-1.0)
proj_render_3 = Plane(data=proj_data_3, cmap_log=True)
rc.scene.data_objects.append(proj_data_3)
rc.scene.components.append(proj_render_3)

# add an axis-normal slice
slc = ds.slice(0, 0.5)
slice_data = PlaneData(data_source=slc)
slice_data.add_data(("enzo", "Density"), 1.0, (400, 400))
slice_render = Plane(data=slice_data, cmap_log=True)
rc.scene.data_objects.append(slice_data)
rc.scene.components.append(slice_render)

# add a cutting plane
normal = np.array([1.0, 1.0, 0.0], dtype="float64")
center = ds.domain_center.to("code_length").value
cut = ds.cutting(normal, center)
cut_data = PlaneData(data_source=cut)
cut_data.add_data(("enzo", "Density"), 1.0, (400, 400))
cut_render = Plane(data=cut_data, cmap_log=True)
rc.scene.data_objects.append(cut_data)
rc.scene.components.append(cut_render)

rc.run()
33 changes: 33 additions & 0 deletions examples/plane_textures_projection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import yt

from yt_idv import render_context
from yt_idv.cameras.trackball_camera import TrackballCamera
from yt_idv.scene_annotations.grid_outlines import GridOutlines # NOQA
from yt_idv.scene_components.plane import Plane # NOQA
from yt_idv.scene_data.grid_positions import GridPositions # NOQA
from yt_idv.scene_data.plane import PlaneData # NOQA
from yt_idv.scene_graph import SceneGraph

rc = render_context(height=800, width=800, gui=True)
c = TrackballCamera(position=[3.5, 3.5, 3.5], focus=[0.0, 0.0, 0.0])
rc.scene = SceneGraph(camera=c)

ds = yt.load("Enzo_64/RD0005/RedshiftOutput0005")

for proj_axis in [0.0, 1.0, 2.0]:
proj = ds.proj(("enzo", "Density"), proj_axis)
proj_data = PlaneData(data_source=proj)
proj_data.add_data(("enzo", "Density"), 1.0, (400, 400))
proj_render = Plane(data=proj_data, cmap_log=True)

rc.scene.data_objects.append(proj_data)
rc.scene.components.append(proj_render)

# add grids
grids = ds.index.grids.tolist()
gp = GridPositions(grid_list=grids)
rc.scene.data_objects.append(gp)
go = GridOutlines(data=gp)
rc.scene.components.append(go)

rc.run()
62 changes: 62 additions & 0 deletions examples/plane_textures_slice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import numpy as np
import yt
from unyt import unyt_quantity

from yt_idv import render_context
from yt_idv.cameras.trackball_camera import TrackballCamera
from yt_idv.scene_annotations.grid_outlines import GridOutlines # NOQA
from yt_idv.scene_components.plane import Plane # NOQA
from yt_idv.scene_data.grid_positions import GridPositions # NOQA
from yt_idv.scene_data.plane import PlaneData # NOQA
from yt_idv.scene_graph import SceneGraph

rc = render_context(height=800, width=800, gui=True)
c = TrackballCamera(position=[3.5, 3.5, 3.5], focus=[0.0, 0.0, 0.0])
rc.scene = SceneGraph(camera=c)

ds = yt.load("Enzo_64/RD0005/RedshiftOutput0005")

for slice_axis in [0, 1]:
slc = ds.slice(slice_axis, 0.5)
slice_data = PlaneData(data_source=slc)
slice_data.add_data(("enzo", "Density"), 1.0, (400, 400))
slice_render = Plane(data=slice_data, cmap_log=True)

rc.scene.data_objects.append(slice_data)
rc.scene.components.append(slice_render)

# another slice covering a smaller distance
slc = ds.slice(2, 0.5)
slice_data = PlaneData(data_source=slc)
slice_data.add_data(
("enzo", "Density"),
unyt_quantity(45, "Mpc"),
(400, 400),
height=unyt_quantity(45, "Mpc"),
)
slice_render = Plane(data=slice_data, cmap_log=True)
rc.scene.data_objects.append(slice_data)
rc.scene.components.append(slice_render)

# another small slice, at a different center
slc = ds.slice(2, 0.25)
slice_data = PlaneData(data_source=slc)
slice_data.add_data(
("enzo", "Density"),
unyt_quantity(45, "Mpc"),
(400, 400),
height=unyt_quantity(45, "Mpc"),
center=np.array([0.75, 0.75, 0.25]),
)
slice_render = Plane(data=slice_data, cmap_log=True)
rc.scene.data_objects.append(slice_data)
rc.scene.components.append(slice_render)

# add grids
grids = ds.index.grids.tolist()
gp = GridPositions(grid_list=grids)
rc.scene.data_objects.append(gp)
go = GridOutlines(data=gp)
rc.scene.components.append(go)

rc.run()
3 changes: 3 additions & 0 deletions yt_idv/scene_components/base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,6 @@ def _reset_cmap_bounds(self):
else:
print(f"Computed new cmap values {self.cmap_min} - {self.cmap_max}")
self._cmap_bounds_invalid = False


_cmaps = ["arbre", "viridis", "magma", "doom"]
Loading