Skip to content

Instantly share code, notes, and snippets.

@dfsp-spirit
Last active September 7, 2021 20:08
Show Gist options
  • Save dfsp-spirit/d63a6b56f2c11c92086a81911138b453 to your computer and use it in GitHub Desktop.
Save dfsp-spirit/d63a6b56f2c11c92086a81911138b453 to your computer and use it in GitHub Desktop.
Modyfying meshes in fsbrain's interactive view using a pre-render hook
# This currently requires fsbrain dev, >= 0.5.0.
#
# You will need to install with:
#
# devtools::install_github("dfsp-spirit/fsbrain", ref="geodesic");
#
# until 0.5.0 is available on CRAN.
#
library(fsbrain)
# We use thickness on fsaverage as demo data
sjd = fsaverage.path();
sj = "fsaverage";
# Visualize once with standard settings:
vis.subject.morph.native(sjd, sj, "thickness", views="si");
# Register a pre-render hook for interactive view that can do whatever you want.
# The hook function gets called with the list of coloredmeshes that will be rendered afterwards
# and must return the modified meshes.
# The `mesh` field of each coloredmesh in the list contains the mesh as an `rgl::tmesh3d` instance.
hook = function(cm){ for(mesh_idx in seq(length(cm))) { cat(sprintf("Rotating mesh %d.\n", mesh_idx)); cm[[mesh_idx]]$mesh = rgl::rotate3d(cm[[mesh_idx]]$mesh, 0.5, 1, 0, 1); }; return(cm); }
options('fsbrain.callback_hook_interactive_setup_mesh_before_render' = hook);
# Visualize again, this time the hook gets called and the mesh will be rotated:
vis.subject.morph.native(sjd, sj, "thickness", views="si");
# You can reset to defaults if you do not want the hook anymore:
options('fsbrain.callback_hook_interactive_setup_mesh_before_render' = NULL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment