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

Spherical volume rendering #64

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f4e9c3b
Adding temp PR for spherical rendering
sochowski Dec 3, 2021
ab78cab
Merged grid_position and spherical_grid_position to switch vertex sha…
sochowski Dec 8, 2021
af7663d
Temporary PR for spherical rendering 2
sochowski Dec 20, 2021
ba3ed48
Update to do transformations in geometry shader
matthewturk Dec 20, 2021
ca6dbfb
add constant color shader
matthewturk Dec 20, 2021
d840d3f
Organized spherical ray-tracing
sochowski Jan 21, 2022
83d793f
Merge remote-tracking branch 'upstream/main' into spherical_refactoring
chrishavlin Aug 2, 2022
4cd83b3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 2, 2022
726aa82
spherical example to its own, resetn cartesian
chrishavlin Aug 2, 2022
3cf8186
experimenting...
chrishavlin Dec 1, 2022
c9a5bff
working? now only converting to cartesian for GLpos, etc.
chrishavlin Dec 1, 2022
065eb6d
add a shell fragment to the example, skipped linting
chrishavlin Dec 1, 2022
ddb69fd
swap the angles in the transformation
chrishavlin Dec 2, 2022
0501c6f
simplify the example
chrishavlin Dec 2, 2022
2cb14a8
cleanup
chrishavlin Dec 2, 2022
ad96eb3
Merge remote-tracking branch 'upstream/main' into spherical_vol_rende…
chrishavlin Dec 2, 2022
fb80cea
bad merge: add back the constant color shader
chrishavlin Dec 2, 2022
65a6abf
more cleaning
chrishavlin Dec 2, 2022
f43d577
move the spherical uniforms to child
chrishavlin Dec 6, 2022
d9a9771
add a bbox command line arg for example
chrishavlin Dec 16, 2022
1ffffb5
Merge remote-tracking branch 'upstream/main' into spherical_vol_rende…
chrishavlin Dec 21, 2022
0474454
in progress: spherical ray intersections
chrishavlin Jan 6, 2023
0031ee8
vectorize normal plane calc, only do it if spherical
chrishavlin Jan 9, 2023
4e4fdec
lingering changes, they look OK
chrishavlin Apr 12, 2023
dbd7240
Merge remote-tracking branch 'upstream/main' into spherical_vol_rende…
chrishavlin Jun 6, 2023
afcef53
add a simple test
chrishavlin Jun 6, 2023
4f73cdd
only rescale for cartesian
chrishavlin Jun 8, 2023
ac9cd00
move shader constants to include
chrishavlin Jun 8, 2023
3cef5b3
Merge remote-tracking branch 'upstream/main' into spherical_vol_rende…
chrishavlin Jun 8, 2023
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# temp files
*.swp
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
50 changes: 50 additions & 0 deletions examples/amr_spherical_volume_rendering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import sys

import numpy as np
import yt

import yt_idv

# yt reminder: phi is the polar angle (0 to 2pi)
# theta is the angle from north (0 to pi)


# coord ordering here will be r, phi, theta

bbox_options = {
"partial": np.array([[0.5, 1.0], [0.0, np.pi / 4], [np.pi / 4, np.pi / 2]]),
"whole": np.array([[0.1, 1.0], [0.0, 2 * np.pi], [0, np.pi]]),
"north_hemi": np.array([[0.1, 1.0], [0.0, 2 * np.pi], [0, 0.5 * np.pi]]),
"south_hemi": np.array([[0.1, 1.0], [0.0, 2 * np.pi], [0.5 * np.pi, np.pi]]),
"ew_hemi": np.array([[0.1, 1.0], [0.0, np.pi], [0.0, np.pi]]),
}


sz = (256, 256, 256)
fake_data = {"density": np.random.random(sz)}

if __name__ == "__main__":
if len(sys.argv) > 1:
bbox_type = sys.argv[1]
else:
bbox_type = "partial"

bbox = bbox_options[bbox_type]

ds = yt.load_uniform_grid(
fake_data,
sz,
bbox=bbox,
nprocs=4096,
geometry=("spherical", ("r", "phi", "theta")),
length_unit="m",
)

rc = yt_idv.render_context(height=800, width=800, gui=True)
dd = ds.all_data()
dd.max_level = 1
sg = rc.add_scene(ds, ("index", "r"), no_ghost=True)
# sg = rc.add_scene(ds, ("index", "theta"), no_ghost=True)
# sg = rc.add_scene(ds, ("index", "phi"), no_ghost=True)
sg.camera.focus = [0.0, 0.0, 0.0]
rc.run()
33 changes: 33 additions & 0 deletions examples/spherical_subset_volume_rendering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import numpy as np
import yt

import yt_idv

# Spherical Test (to line 20)

NDIM = 32

bbox = np.array(
[[0.0, 0.5], [np.pi / 8, 2 * np.pi / 8], [2 * np.pi / 8, 3 * np.pi / 8]]
)

fake_data = {"density": np.random.random((NDIM, NDIM, NDIM))}
ds = yt.load_uniform_grid(
fake_data,
[NDIM, NDIM, NDIM],
bbox=bbox,
geometry="spherical",
)

rc = yt_idv.render_context(height=800, width=800, gui=True)
dd = ds.all_data()
dd.max_level = 1
sg = rc.add_scene(ds, ("index", "r"), no_ghost=True)
sg.camera.focus = [0.0, 0.0, 0.0]
rc.run()

# Cartesian Test (to line 25)
# ds = yt.load_sample("IsolatedGalaxy")
# rc = yt_idv.render_context(height=800, width=800, gui=True)
# sg = rc.add_scene(ds, "density", no_ghost=True)
# rc.run()
2 changes: 1 addition & 1 deletion yt_idv/scene_components/base_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class SceneComponent(traitlets.HasTraits):
# These attributes are
cmap_min = traitlets.CFloat(None, allow_none=True)
cmap_max = traitlets.CFloat(None, allow_none=True)
cmap_log = traitlets.Bool(True)
cmap_log = traitlets.Bool(False)
scale = traitlets.CFloat(1.0)

@traitlets.observe("display_bounds")
Expand Down
9 changes: 9 additions & 0 deletions yt_idv/scene_components/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ def draw(self, scene, program):
GL.glDrawArrays(GL.GL_POINTS, tex_ind * each, each)

def _set_uniforms(self, scene, shader_program):
shader_program._set_uniform(
"is_spherical", self.data.data_source.ds.geometry == "spherical"
)
if self.data.data_source.ds.geometry == "spherical":
axis_id = self.data.data_source.ds.coordinates.axis_id
shader_program._set_uniform("id_theta", axis_id["theta"])
shader_program._set_uniform("id_r", axis_id["r"])
shader_program._set_uniform("id_phi", axis_id["phi"])

shader_program._set_uniform("box_width", self.box_width)
shader_program._set_uniform("sample_factor", self.sample_factor)
shader_program._set_uniform("ds_tex", 0)
Expand Down
49 changes: 49 additions & 0 deletions yt_idv/scene_data/block_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,39 @@ def add_data(self, field, no_ghost=False):
# Every time we change our data source, we wipe all existing ones.
# We now set up our vertices into our current data source.
vert, dx, le, re = [], [], [], []

# spherical-only: SHOULD ONLY DO THIS IF DATASET IS SPHERICAL
# normal vectors and plane constants for min/max phi face
phi_plane_le = []
phi_plane_re = []

axis_id = self.data_source.ds.coordinates.axis_id

def calculate_phi_normal_plane(le_or_re):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we spend enough time in this function that it makes sense to vectorize it? i.e., call if on a list of coordinates and then return an array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vectorizing this is a good idea! and I think will simplify un-tangling the spherical-only part of the calculation from the base cartesian version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# calculates the parameters for a plane parallel to the z-axis and
# passing through the x-y values defined by the polar angle phi
# the result plane defines the boundary on +/-phi faces.
#
# eq of plane:
# X dot N = d where N is a normal vector, d is a constant,
# X is a position vector
# this function returns N and d.
phi = le_or_re[axis_id["phi"]]
theta = le_or_re[axis_id["theta"]]
r = le_or_re[axis_id["r"]]

# prob can/should use a yt func here to ensure consistency....
z = r * np.cos(theta)
r_xy = r * np.sin(theta)
x = r_xy * np.cos(phi)
y = r_xy * np.sin(phi)
pt = np.array([x, y, z])

z_hat = np.array([0, 0, 1])
normal_vec = np.cross(pt, z_hat)
d = np.dot(normal_vec, pt)
return normal_vec, d

self.min_val = +np.inf
self.max_val = -np.inf
if self.scale:
Expand All @@ -64,6 +97,13 @@ def add_data(self, field, no_ghost=False):
dx.append(dds.tolist())
le.append(block.LeftEdge.tolist())
re.append(block.RightEdge.tolist())

normal, const = calculate_phi_normal_plane(le[-1])
phi_plane_le.append(np.array([*normal, const]))

normal, const = calculate_phi_normal_plane(re[-1])
phi_plane_re.append(np.array([*normal, const]))

for (g, node, (sl, _dims, _gi)) in self.data_source.tiles.slice_traverse():
block = node.data
self.blocks_by_grid[g.id - g._id_offset].append((id(block), i))
Expand Down Expand Up @@ -94,6 +134,15 @@ def add_data(self, field, no_ghost=False):
VertexAttribute(name="in_right_edge", data=re)
)

phi_plane_re = np.array(phi_plane_re, dtype="f4")
phi_plane_le = np.array(phi_plane_le, dtype="f4")
self.vertex_array.attributes.append(
VertexAttribute(name="phi_plane_le", data=phi_plane_le)
)
self.vertex_array.attributes.append(
VertexAttribute(name="phi_plane_re", data=phi_plane_re)
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it occurs to me that we should have the cartesian volume rendering as the basic option, and we can/should have spherical as a subclass. Let's not try cramming it all in; @neutrinoceros 's work on having things be visible in index-space coordinates is encouraging me to think of it in both ways.

That's not really specific here though, except that what we may want to do is to have this be a supplemental function that gets called if the component accessing the data is viewing it in spherical coordinates.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya! I agree totally! I just jammed it in here to get things in initially working. Unjamming it and subclassing it relates to your vectorization question I think. If I vectorize that function, it'll be easier to have a sublcass that takes the output from the standard cartesian version to calculate the new spherical-only attributes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

latest push no longer breaks things for cartesian coords (it's now vectorized and only runs if spherical).

I still want to keep thinking on how to better capture the different geometries though. I initially thought it may make sense to have a SphericalBlockCollection, but implementing it wasn't super straightforward because the calculations rely on the left and right edges, which currently only get saved via the vertex_array attribute after casting to "f4". So a standard construction like:

class SphericalBlockCollection(BlockCollection):
        def add_data(self, field, no_ghost=False):
            super().add_data(field, no_ghost=no_ghost)
            
            << --- calculate phi-normal planes from left, right edges --- >>

would require us also saving the full le and re arrays as attributes. Since they're only needed for an intermediate step, it seemed better to just add a geometry check to BlockCollection.add_data(). But i'm open to ideas here!

# Now we set up our textures
self._load_textures()

Expand Down
49 changes: 38 additions & 11 deletions yt_idv/shaders/grid_expand.geom.glsl
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
layout ( points ) in;
layout ( triangle_strip, max_vertices = 14 ) out;

// note: all in/out variables below are always in native coordinates (e.g.,
// spherical or cartesian) except when noted.
flat in vec3 vdx[];
flat in vec3 vleft_edge[];
flat in vec3 vright_edge[];
flat in vec4 vphi_plane_le[];
flat in vec4 vphi_plane_re[];

flat out vec3 dx;
flat out vec3 left_edge;
flat out vec3 right_edge;
flat out mat4 inverse_proj;
flat out mat4 inverse_mvm;
flat out mat4 inverse_pmvm;
flat out mat4 inverse_proj; // always cartesian
flat out mat4 inverse_mvm; // always cartesian
flat out mat4 inverse_pmvm; // always cartesian
out vec4 v_model;

flat in mat4 vinverse_proj[];
flat in mat4 vinverse_mvm[];
flat in mat4 vinverse_pmvm[];
flat in mat4 vinverse_proj[]; // always cartesian
flat in mat4 vinverse_mvm[]; // always cartesian
flat in mat4 vinverse_pmvm[]; // always cartesian
flat in vec4 vv_model[];

flat out ivec3 texture_offset;

flat out vec4 phi_plane_le;
flat out vec4 phi_plane_re;

// https://stackoverflow.com/questions/28375338/cube-using-single-gl-triangle-strip
// suggests that the triangle strip we want for the cube is

Expand All @@ -36,26 +43,46 @@ uniform vec3 arrangement[8] = vec3[](

uniform int aindex[14] = int[](6, 7, 4, 5, 1, 7, 3, 6, 2, 4, 0, 1, 2, 3);

vec3 transform_vec3(vec3 v) {
if (is_spherical) {
// in yt, phi is the polar angle from (0, 2pi), theta is the azimuthal
// angle (0, pi). the id_ values below are uniforms that depend on the
// yt dataset coordinate ordering
return vec3(
v[id_r] * sin(v[id_theta]) * cos(v[id_phi]),
v[id_r] * sin(v[id_theta]) * sin(v[id_phi]),
v[id_r] * cos(v[id_theta])
);
} else {
return v;
}
}

void main() {

vec4 center = gl_in[0].gl_Position;
vec4 center = gl_in[0].gl_Position; // always cartesian

vec3 width = vright_edge[0] - vleft_edge[0];

vec4 newPos;
vec3 current_pos;

for (int i = 0; i < 14; i++) {
newPos = vec4(vleft_edge[0] + width * arrangement[aindex[i]], 1.0);
gl_Position = projection * modelview * newPos;
// walks through each vertex of the triangle strip, emit it. need to
// emit gl_Position in cartesian, but pass native coords out in v_model
current_pos = vec3(vleft_edge[0] + width * arrangement[aindex[i]]);
newPos = vec4(transform_vec3(current_pos), 1.0); // cartesian
gl_Position = projection * modelview * newPos; // cartesian
left_edge = vleft_edge[0];
right_edge = vright_edge[0];
inverse_pmvm = vinverse_pmvm[0];
inverse_proj = vinverse_proj[0];
inverse_mvm = vinverse_mvm[0];
phi_plane_le = vphi_plane_le[0];
phi_plane_re = vphi_plane_re[0];
dx = vdx[0];
v_model = newPos;
v_model = vec4(current_pos, 1.0);
texture_offset = ivec3(0);
EmitVertex();
}

}
44 changes: 39 additions & 5 deletions yt_idv/shaders/grid_position.vert.glsl
Original file line number Diff line number Diff line change
@@ -1,25 +1,59 @@
in vec4 model_vertex; // The location of the vertex in model space
// note: all in/out variables below are always in native coordinates (e.g.,
// spherical or cartesian) except when noted.

in vec4 model_vertex;
in vec3 in_dx;
in vec3 in_left_edge;
in vec3 in_right_edge;

in vec4 phi_plane_le; // first 3 elements are the normal vector, final is constant
in vec4 phi_plane_re;

flat out vec4 vv_model;
flat out mat4 vinverse_proj;
flat out mat4 vinverse_mvm;
flat out mat4 vinverse_pmvm;
flat out mat4 vinverse_proj; // always cartesian
flat out mat4 vinverse_mvm; // always cartesian
flat out mat4 vinverse_pmvm; // always cartesian
flat out vec3 vdx;
flat out vec3 vleft_edge;
flat out vec3 vright_edge;

flat out vec4 vphi_plane_le;
flat out vec4 vphi_plane_re;

vec3 transform_vec3(vec3 v) {
if (is_spherical) {
// in yt, phi is the polar angle from (0, 2pi), theta is the azimuthal
// angle (0, pi). the id_ values below are uniforms that depend on the
// yt dataset coordinate ordering
return vec3(
v[id_r] * sin(v[id_theta]) * cos(v[id_phi]),
v[id_r] * sin(v[id_theta]) * sin(v[id_phi]),
v[id_r] * cos(v[id_theta])
);
} else {
return v;
}
}

vec4 transform_vec4(vec4 v) {
vec3 v3 = transform_vec3(vec3(v));
return vec4(v3[0], v3[1], v3[2], v[3]);
}

void main()
{
// camera uniforms:
// projection, modelview
vv_model = model_vertex;
vinverse_proj = inverse(projection);
// inverse model-view-matrix
vinverse_mvm = inverse(modelview);
vinverse_pmvm = inverse(projection * modelview);
gl_Position = projection * modelview * model_vertex;
gl_Position = projection * modelview * transform_vec4(model_vertex);
vdx = vec3(in_dx);
vleft_edge = vec3(in_left_edge);
vright_edge = vec3(in_right_edge);

vphi_plane_le = vec4(phi_plane_le);
vphi_plane_re = vec4(phi_plane_re);
}
6 changes: 6 additions & 0 deletions yt_idv/shaders/known_uniforms.inc.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ uniform float iso_layers[32];
uniform bool iso_log;
uniform float iso_min;
uniform float iso_max;

// spherical coordinates
uniform bool is_spherical;
uniform int id_theta; // azimuthal angle (0 to pi) index in the yt dataset
uniform int id_r; // radial index in the yt dataset
uniform int id_phi; // polar angle (0 to 2pi) indexi n the yt dataset
Loading