Skip to content

Commit

Permalink
feat: Added 5 more kernels for the KDE rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoDell committed Jul 27, 2023
1 parent edd06e8 commit 7ecefeb
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 15 deletions.
7 changes: 4 additions & 3 deletions docs/experimental/viz_kde_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def normalize(array : np.array, min : float = 0.0, max : float = 1.0, axis : int
"Can't normalize an array which maximum and minimum value are the same.")


width, height = (800, 800)
width, height = (800, 600)

scene = Scene()
scene.set_camera(position=(-6, 5, -10),
Expand All @@ -49,12 +49,13 @@ def normalize(array : np.array, min : float = 0.0, max : float = 1.0, axis : int
points = np.random.rand(n_points, 3)
points = normalize(points, -5, 5)
sigmas = normalize(np.random.rand(n_points, 1), 0.1, 0.3)
offset = np.array([3.0, 0.0, 0.0])
offset = np.array([0.0, 0.0, 0.0])
points = points + np.tile(offset, points.shape[0]).reshape(points.shape)

effects = EffectManager(manager)

kde_actor = effects.kde(points, sigmas, colormap = "inferno")
kde_actor = effects.kde(points, sigmas, kernel = "exponential", colormap = "inferno")


manager.scene.add(kde_actor)
# effects.remove_effect(kde_actor)
Expand Down
31 changes: 22 additions & 9 deletions fury/actors/effect_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,9 @@ def __init__(self, manager : ShowManager):
def kde(self,

Check warning on line 201 in fury/actors/effect_manager.py

View check run for this annotation

Codecov / codecov/patch

fury/actors/effect_manager.py#L201

Added line #L201 was not covered by tests
points : np.ndarray,
sigmas,
opacity = 1.0,
colormap = "viridis",
kernel : str = "gaussian",
opacity : float = 1.0,
colormap : str = "viridis",
custom_colormap : np.array = None):
"""Actor that displays the Kernel Density Estimation of a given set of points.
Expand All @@ -212,6 +213,15 @@ def kde(self,
Array of points to be displayed.
sigmas : np.ndarray (1, ) or (N, 1)
Array of sigmas to be used in the KDE calculations. Must be one or one for each point.
kernel : str, optional
Kernel to be used for the distribution calculation. The available options are:
* "cosine"
* "epanechnikov"
* "exponential"
* "gaussian"
* "linear"
* "tophat"
opacity : float, optional
Opacity of the actor.
colormap : str, optional.
Expand All @@ -235,7 +245,7 @@ def kde(self,
varying float out_scale;
"""

kde_dec = import_fury_shader(os.path.join("utils", "normal_distribution.glsl"))
kde_dec = import_fury_shader(os.path.join("utils", f"{kernel.lower()}_distribution.glsl"))

Check warning on line 248 in fury/actors/effect_manager.py

View check run for this annotation

Codecov / codecov/patch

fury/actors/effect_manager.py#L248

Added line #L248 was not covered by tests

kde_impl = """

Check warning on line 250 in fury/actors/effect_manager.py

View check run for this annotation

Codecov / codecov/patch

fury/actors/effect_manager.py#L250

Added line #L250 was not covered by tests
float current_kde = kde(normalizedVertexMCVSOutput*out_scale, out_sigma);
Expand Down Expand Up @@ -264,12 +274,12 @@ def kde(self,
vec2 res_factor = vec2(res.y/res.x, 1.0);
vec2 renorm_tex = res_factor*normalizedVertexMCVSOutput.xy*0.5 + 0.5;
float intensity = texture(screenTexture, renorm_tex).r;
color = color_mapping(intensity, colormapTexture);
if(intensity<=0.0){
discard;
}else{
fragOutput0 = vec4(color, u_opacity);
}else{
vec4 final_color = color_mapping(intensity, colormapTexture);
fragOutput0 = vec4(final_color.rgb, u_opacity*final_color.a);
}
"""

Expand Down Expand Up @@ -309,10 +319,13 @@ def kde(self,
bill_bounds = bill.GetBounds()
max_sigma = 2*4.0*np.max(sigmas)

Check warning on line 320 in fury/actors/effect_manager.py

View check run for this annotation

Codecov / codecov/patch

fury/actors/effect_manager.py#L319-L320

Added lines #L319 - L320 were not covered by tests

scale = np.array([[bill_bounds[1] - bill_bounds[0] + center_of_mass[0] + max_sigma,
bill_bounds[3] - bill_bounds[2] + center_of_mass[1] + max_sigma,
0.0]])
actor_scales = np.array([[bill_bounds[1] - bill_bounds[0] + center_of_mass[0] + max_sigma,

Check warning on line 322 in fury/actors/effect_manager.py

View check run for this annotation

Codecov / codecov/patch

fury/actors/effect_manager.py#L322

Added line #L322 was not covered by tests
bill_bounds[3] - bill_bounds[2] + center_of_mass[1] + max_sigma,
0.0]])

scale = np.array([[actor_scales.max(),

Check warning on line 326 in fury/actors/effect_manager.py

View check run for this annotation

Codecov / codecov/patch

fury/actors/effect_manager.py#L326

Added line #L326 was not covered by tests
actor_scales.max(),
0.0]])

# Render to second billboard for color map post-processing.
textured_billboard = billboard(np.array([center_of_mass]), scales=scale, fs_dec=tex_dec, fs_impl=tex_impl)
Expand Down
4 changes: 2 additions & 2 deletions fury/shaders/effects/color_mapping.glsl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
vec3 color_mapping(float intensity, sampler2D colormapTexture){
return texture(colormapTexture, vec2(intensity,0)).rgb;
vec4 color_mapping(float intensity, sampler2D colormapTexture){
return texture(colormapTexture, vec2(intensity,0));
}
12 changes: 12 additions & 0 deletions fury/shaders/utils/cosine_distribution.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This assumes the center of the normal distribution is the center of the screen
#define PI 3.1415926
float kde(vec3 point, float sigma){
float norm = (PI/(4.0*sigma));
return norm*cos(PI*length(point)/(2*sigma))*int(length(point) < sigma);
}


// This requires a center to be passed
// float kde(vec3 point, vec3 center, float sigma){
// return cos(PI*length(center - point)/(2*sigma))*int(length(center - point) < sigma);
// }
11 changes: 11 additions & 0 deletions fury/shaders/utils/epanechnikov_distribution.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This assumes the center of the normal distribution is the center of the screen
float kde(vec3 point, float sigma){
float norm = (3.0/(4.0*sigma));
return norm*(1.0 - (length(point)*length(point))/(sigma*sigma));
}


// This requires a center to be passed
// float kde(vec3 point, vec3 center, float sigma){
// return 1.0 - (length(center - point)*length(center - point))/(sigma*sigma);
// }
11 changes: 11 additions & 0 deletions fury/shaders/utils/exponential_distribution.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This assumes the center of the normal distribution is the center of the screen
#define E 2.7182818
float kde(vec3 point, float sigma){
return exp(-1.0*length(point)/sigma);
}


// This requires a center to be passed
// float kde(vec3 point, vec3 center, float sigma){
// return exp(-1.0*length(center - point)/sigma);
// }
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// This assumes the center of the normal distribution is the center of the screen
#define PI 3.1415926
float kde(vec3 point, float sigma){
return (1/(sigma*sqrt(2.0*PI)))*exp(-1.0*pow(length(point), 2.0)/(2.0*sigma*sigma) );
float norm = (1/(sigma*sqrt(2.0*PI)));
return norm*exp(-1.0*pow(length(point), 2.0)/(2.0*sigma*sigma) );
}


Expand Down
11 changes: 11 additions & 0 deletions fury/shaders/utils/linear_distribution.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This assumes the center of the normal distribution is the center of the screen
float kde(vec3 point, float sigma){
float norm = (1.0/sigma);
return norm*(1.0 - length(point)/sigma)*int(length(point) < sigma);
}


// This requires a center to be passed
// float kde(vec3 point, vec3 center, float sigma){
// return (1.0 - length(center - point)/sigma)*int(length(center - point) < sigma);
// }
11 changes: 11 additions & 0 deletions fury/shaders/utils/tophat_distribution.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This assumes the center of the normal distribution is the center of the screen
float kde(vec3 point, float sigma){
float norm = (1.0/sigma*2.0);
return norm*int(length(point) < sigma);
}


// This requires a center to be passed
// float kde(vec3 point, vec3 center, float sigma){
// return 1.0*int(length(center - point) < sigma);
// }

0 comments on commit 7ecefeb

Please sign in to comment.