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

Added option to have an arbitrarily oriented cutting plane #392

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions glue_vispy_viewers/volume/shaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def indent(text, prefix):
varying vec4 v_nearpos;
varying vec4 v_farpos;

// A cutting plane defined as ax + by + cz + d = 0 where the vector below is (a, b, c)
uniform vec3 u_cutting_plane_abc;
uniform float u_cutting_plane_d;

// uniforms for lighting. Hard coded until we figure out how to do lights
const vec4 u_ambient = vec4(0.2, 0.4, 0.2, 1.0);
const vec4 u_diffuse = vec4(0.8, 0.2, 0.2, 1.0);
Expand Down Expand Up @@ -121,6 +125,10 @@ def indent(text, prefix):
distance = max(distance, min((-0.5 - v_position.z) / view_ray.z,
(u_shape.z - 0.5 - v_position.z) / view_ray.z));

// Compute distance to cutting plane
float cutting_distance = -(dot(v_position, u_cutting_plane_abc) + u_cutting_plane_d) / dot(view_ray, u_cutting_plane_abc);
distance = max(distance, cutting_distance);

// Now we have the starting position on the front surface
vec3 front = v_position + view_ray * distance;

Expand Down
2 changes: 2 additions & 0 deletions glue_vispy_viewers/volume/viewer_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Vispy3DVolumeViewerState(Vispy3DViewerState):
'which defines the coordinate frame in '
'which the images are shown')

cutting_plane = CallbackProperty(None)

def __init__(self, **kwargs):

super(Vispy3DVolumeViewerState, self).__init__()
Expand Down
7 changes: 7 additions & 0 deletions glue_vispy_viewers/volume/volume_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ def setup_widget_and_callbacks(self):
self.state.add_callback('resolution', self._update_resolution)
self._update_resolution()

self.state.add_callback('cutting_plane', self._update_cutting_plane)
self._update_cutting_plane()

def _update_cutting_plane(self, *args):
self._vispy_widget._multivol.set_cutting_plane(self.state.cutting_plane)
self._vispy_widget.canvas.update()

def _update_clip(self, force=False):
if hasattr(self._vispy_widget, '_multivol'):
if (self.state.clip_data or force):
Expand Down
16 changes: 12 additions & 4 deletions glue_vispy_viewers/volume/volume_visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ def __init__(self, n_volume_max=16, emulate_texture=False, bgcolor='white', reso
self.shared_program['u_clip_min'] = [0, 0, 0]
self.shared_program['u_clip_max'] = [1, 1, 1]

# Set initial cutting plane
self.set_cutting_plane(None)

# Set up texture vertices - note that these variables are required by
# the parent VolumeVisual class.

Expand Down Expand Up @@ -201,6 +204,14 @@ def set_multiply(self, label, label_other):
self.volumes[label]['multiply'] = label_other
self._update_shader()

def set_cutting_plane(self, parameters):
if parameters is None:
self.shared_program['u_cutting_plane_abc'] = [0, 0, 1]
self.shared_program['u_cutting_plane_d'] = -10000.
else:
self.shared_program['u_cutting_plane_abc'] = list(parameters[:3])
self.shared_program['u_cutting_plane_d'] = float(parameters[3])

# The following methods don't require any changes to the shader code, so we
# don't update the shader after setting the OpenGL variables.

Expand Down Expand Up @@ -388,10 +399,7 @@ def draw(self):
if not any(self.enabled):
return
else:
try:
super(MultiVolumeVisual, self).draw()
except Exception:
pass
super(MultiVolumeVisual, self).draw()


MultiVolume = create_visual_node(MultiVolumeVisual)
Loading