-
Notifications
You must be signed in to change notification settings - Fork 5
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
base: main
Are you sure you want to change the base?
Changes from 22 commits
f4e9c3b
ab78cab
af7663d
ba3ed48
ca6dbfb
d840d3f
83d793f
4cd83b3
726aa82
3cf8186
c9a5bff
065eb6d
ddb69fd
0501c6f
2cb14a8
ad96eb3
fb80cea
65a6abf
f43d577
d9a9771
1ffffb5
0474454
0031ee8
4e4fdec
dbd7240
afcef53
4f73cdd
ac9cd00
3cef5b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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] | ||
|
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() |
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
# 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: | ||
|
@@ -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)) | ||
|
@@ -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) | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 |
||
# Now we set up our textures | ||
self._load_textures() | ||
|
||
|
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); | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is vectorized now! see https://github.com/yt-project/yt_idv/pull/64/files#diff-7d5c480ce667d255f45fb76e3578809a9840831967712edc900858d049cc5517