rheatmaprgl

Colouring brain surface with heat map


library(rgl)
library(brainR)
template <- readNIfTI(system.file("MNI152_T1_2mm_brain.nii.gz",
                                  package = "brainR"), reorient = FALSE)
misc3d::contour3d(template, level = 4500, alpha = .7, draw = T)

With the above code one can can generate a 3D model of the brain. The argument draw = FALSE asks contour3d to compute and return the contour surface as a triangle mesh object without drawing it.

a <- misc3d::contour3d(template, level = 4500, alpha = .7, draw = F)
str(a)
List of 10
 $ v1      : num [1:110433, 1:3] 45 45 46 46 47 47 43 43 44 44 ...
 $ v2      : num [1:110433, 1:3] 44.1 46 46 47 47 ...
 $ v3      : num [1:110433, 1:3] 45 45 45 46 46 ...
 $ color   : chr "white"
 $ color2  : logi NA
 $ fill    : logi TRUE
 $ material: chr "default"
 $ col.mesh: logi NA
 $ alpha   : num 0.7
 $ smooth  : num 0
 - attr(*, "class")= chr "Triangles3D"

I would like to use the external surface of the above object to project heat maps, or say, colouring the surface... I also wonder how to determine certain positions in this model, e.g. EEG channel positions. Is it possible to generate only the surface with a$v1, a$v2 ... using function rgl:::surface3d? Thank you in advance,


Solution

  • I was able to draw the triangle mesh in colors with rgl. Some considerations:

    Hope this will help you.

    
    data<-misc3d::contour3d(template, level = 4500, alpha = .7, draw = F)
    
    points <- rbind(data$v1, data$v2, data$v3)
    points <- points[order(rep(1:(nrow(points)/3),3), rep(1:3, nrow(points)/3)),] 
    
    color_map <-  c("red","blue","green")[cut(points[,1], 3, labels=F)]
    rgl::open3d()
    rgl::triangles3d(points, alpha=.7, color = color_map)
    
    

    Edit Coordinates x, y, z are in the scale of the positions of the array template i.e. point c(5.4, 10.8, 30.1) is related to the level of the array around [5, 10, 30], that is, that point should be around level=4500