Skip to content

Instantly share code, notes, and snippets.

@muschellij2
Created October 30, 2018 03:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save muschellij2/6fb7081c355e5a005f65fc64489a7d31 to your computer and use it in GitHub Desktop.
Save muschellij2/6fb7081c355e5a005f65fc64489a7d31 to your computer and use it in GitHub Desktop.
Example of 3D Image using RGL and misc3d
---
title: "RGL Example"
author: "John Muschelli"
date: "10/29/2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(rgl)
library(misc3d)
library(neurobase)
if (!requireNamespace("aal")) {
devtools::install_github("muschellij2/aal")
} else {
library(aal)
}
if (!requireNamespace("MNITemplate")) {
devtools::install_github("jfortin1/MNITemplate")
} else {
library(MNITemplate)
}
img = aal_image()
template = readMNI(res = "2mm")
cut <- 4500
dtemp <- dim(template)
labs = aal_get_labels()
motor = labs$index[grep("Motor", labs$name)]
mask = remake_img(vec = img %in% motor, img = img)
```
```{r}
### this would be the ``activation'' or surface you want to render
contour3d(template, x=1:dtemp[1], y=1:dtemp[2], z=1:dtemp[3], level = cut, alpha = 0.1, draw = TRUE)
contour3d(mask, level = c(0.5), alpha = c(0.5), add = TRUE, color=c("red") )
### add text
text3d(x=dtemp[1]/2, y=dtemp[2]/2, z = dtemp[3]*0.98, text="Top")
text3d(x=-0.98, y=dtemp[2]/2, z = dtemp[3]/2, text="Right")
rglwidget()
````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment