Skip to content

Commit

Permalink
reorganized and refactored odf actor
Browse files Browse the repository at this point in the history
  • Loading branch information
tvcastillod committed Aug 26, 2024
1 parent 3a375d9 commit c56920b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 35 deletions.
6 changes: 3 additions & 3 deletions fury/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4025,7 +4025,7 @@ def odf(
centers,
coeffs,
degree=None,
basis_type='descoteaux',
sh_basis='descoteaux',
scales=1.0,
opacity=1.0
):
Expand All @@ -4044,7 +4044,7 @@ def odf(
Index of the highest used band of the spherical harmonics basis. Must
be even, at least 2 and at most 12. If None the degree is set based on
the number of SH coefficients given.
basis_type: str, optional
sh_basis: str, optional
Type of basis (descoteaux, tournier)
'descoteaux' for the default ``descoteaux07`` DYPY basis.
'tournier' for the default ``tournier07` DYPY basis.
Expand Down Expand Up @@ -4108,4 +4108,4 @@ def odf(
total = np.sum(abs(coeffs), axis=1)
coeffs = np.dot(np.diag(1 / total * scales), coeffs) * 1.7

return sh_odf(centers, coeffs, degree, basis_type, scales, opacity)
return sh_odf(centers, coeffs, degree, sh_basis, scales, opacity)
62 changes: 33 additions & 29 deletions fury/actors/odf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from fury.texture.utils import uv_calculations


def sh_odf(centers, coeffs, degree, basis_type, scales, opacity):
def sh_odf(centers, coeffs, degree, sh_basis, scales, opacity):
"""
Visualize one or many ODFs with different features.
Expand All @@ -28,7 +28,7 @@ def sh_odf(centers, coeffs, degree, basis_type, scales, opacity):
ODFs positions.
coeffs : ndarray
2D ODFs array in SH coefficients.
basis_type: str, optional
sh_basis: str, optional
Type of basis (descoteaux, tournier)
'descoteaux' for the default ``descoteaux07`` DYPY basis.
'tournier' for the default ``tournier07` DYPY basis.
Expand Down Expand Up @@ -56,11 +56,26 @@ def sh_odf(centers, coeffs, degree, basis_type, scales, opacity):
big_minmax = np.repeat(minmax, 8, axis=0)
attribute_to_actor(odf_actor, big_minmax, "minmax")

odf_actor_pd = odf_actor.GetMapper().GetInput()

n_glyphs = coeffs.shape[0]
# Coordinates to locate the data of each glyph in the texture.
uv_vals = np.array(uv_calculations(n_glyphs))
num_pnts = uv_vals.shape[0]

# Definition of texture coordinates to be associated with the actor.
t_coords = FloatArray()
t_coords.SetNumberOfComponents(2)
t_coords.SetNumberOfTuples(num_pnts)
[t_coords.SetTuple(i, uv_vals[i]) for i in range(num_pnts)]

set_polydata_tcoords(odf_actor_pd, t_coords)

# The coefficient data is stored in a texture to be passed to the shaders.

# Data is normalized to a range of 0 to 1.
arr = minmax_norm(coeffs)
# Data is turned into values within the RGB color range, and then coverted
# Data is turned into values within the RGB color range, and then converted
# into a vtk image data.
arr *= 255
grid = numpy_to_vtk_image_data(arr.astype(np.uint8))
Expand All @@ -73,21 +88,6 @@ def sh_odf(centers, coeffs, degree, basis_type, scales, opacity):
# Texture is associated with the actor
odf_actor.GetProperty().SetTexture("texture0", texture)

odf_actor_pd = odf_actor.GetMapper().GetInput()

n_glyphs = coeffs.shape[0]
# Coordinates to locate the data of each glyph in the texture.
uv_vals = np.array(uv_calculations(n_glyphs))
num_pnts = uv_vals.shape[0]

# Definition of texture coordinates to be associated with the actor.
t_coords = FloatArray()
t_coords.SetNumberOfComponents(2)
t_coords.SetNumberOfTuples(num_pnts)
[t_coords.SetTuple(i, uv_vals[i]) for i in range(num_pnts)]

set_polydata_tcoords(odf_actor_pd, t_coords)

# The number of coefficients is associated to the order of the SH
odf_actor.GetShaderProperty().GetFragmentCustomUniforms().SetUniformf(
"numCoeffs", ((degree + 1) * (degree + 2)) / 2
Expand Down Expand Up @@ -169,14 +169,19 @@ def sh_odf(centers, coeffs, degree, basis_type, scales, opacity):

coeffs_norm = import_fury_shader(os.path.join("utils", "minmax_norm.glsl"))

eval_sh_list = ''
for i in range(2, degree+1, 2):
eval_sh_composed = ""
for i in range(2, degree + 1, 2): #PUT sh_degree
eval_sh = import_fury_shader(
os.path.join("rt_odfs", basis_type, 'eval_sh_' + str(i) + '.frag'))
os.path.join("rt_odfs", sh_basis, "eval_sh_" + str(i) + ".frag")
)
eval_sh_grad = import_fury_shader(
os.path.join("rt_odfs", basis_type,
'eval_sh_grad_' + str(i) + '.frag'))
eval_sh_list = eval_sh_list + '\n\n' + eval_sh + '\n\n' + eval_sh_grad
os.path.join(
"rt_odfs", sh_basis, "eval_sh_grad_" + str(i) + ".frag"
)
)
eval_sh_composed = compose_shader(
[eval_sh_composed, eval_sh, eval_sh_grad]
)

# Searches a single root of a polynomial within a given interval.
# param out_root The location of the found root.
Expand Down Expand Up @@ -283,7 +288,7 @@ def sh_odf(centers, coeffs, degree, basis_type, scales, opacity):
fs_dec = compose_shader([
def_sh_degree, def_sh_count, def_max_degree,
def_gl_ext_control_flow_attributes, def_no_intersection, def_pis,
fs_vs_vars, coeffs_norm, eval_sh_list, newton_bisection, find_roots,
fs_vs_vars, coeffs_norm, eval_sh_composed, newton_bisection, find_roots,
eval_sh, eval_sh_grad, get_inv_vandermonde, ray_sh_glyph_intersections,
get_sh_glyph_normal, blinn_phong_model, linear_to_srgb, srgb_to_linear,
linear_rgb_to_srgb, srgb_to_linear_rgb, tonemap
Expand Down Expand Up @@ -311,10 +316,9 @@ def sh_odf(centers, coeffs, degree, basis_type, scales, opacity):
float i = 1 / (numCoeffs * 2);
float sh_coeffs[SH_COUNT];
for(int j=0; j<numCoeffs; j++){
sh_coeffs[j] = coeffsNorm(
texture(texture0, vec2(i + j / numCoeffs,
tcoordVCVSOutput.y)).x, 0, 1, minmaxVSOutput.x,
minmaxVSOutput.y);
sh_coeffs[j] = rescale(texture(
texture0, vec2(i + j / numCoeffs, tcoordVCVSOutput.y)).x,
0, 1, minmaxVSOutput.x, minmaxVSOutput.y);
}
"""

Expand Down
6 changes: 3 additions & 3 deletions fury/shaders/utils/minmax_norm.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
float coeffsNorm(float coef, float min, float max, float a, float b)
float rescale(float x, float oldMin, float oldMax, float newMin, float newMax)
{
return (coef - min) * ((b - a) / (max - min)) + a;
}
return (x - oldMin) * (newMax - newMin) / (oldMax - oldMin) + newMin;
}

0 comments on commit c56920b

Please sign in to comment.