2

Assume a matrix m of integer values:

m <- matrix(sample(1:10, 100, replace = TRUE), nrow = 10)

Given a colour palette that maps those values from 1 to 10 to some colours, how to show matrix m as a heatmap in R with OpenGL graphics, e.g. using the rgl package? (Preferably in the most efficient way.)

user2554330
  • 37,248
  • 4
  • 43
  • 90
Ramiro Magno
  • 3,085
  • 15
  • 30
  • I think that calling SDL and OpenGL directly using rdyncall could be most efficient to do this, but haven't figured out myself exactly how to do this - looking for an answer to this question myself too: https://stackoverflow.com/questions/48119360/performant-2d-opengl-graphics-in-r-for-fast-display-of-raster-image-using-rdynca. Out of the standard R functions provided to display an image raster, grid.raster in library(grid) is the fastest that I know of... – Tom Wenseleers Dec 19 '22 at 14:55
  • I've learned recently that `nativeRaster` can be really fast. The `{nara}` package uses it: https://github.com/coolbutuseless/nara. – Ramiro Magno Dec 20 '22 at 16:07
  • Ha thanks - hadn't seen that one! With that one I can get a real-time smooth Mandelbrot zoomer - cool! – Tom Wenseleers Dec 20 '22 at 20:58
  • Did you also find an efficient way to do the colour mapping? If so, you might want to post an answer below to your original question! I am sure many others would find it useful! – Tom Wenseleers Dec 20 '22 at 21:00

1 Answers1

1

The very thorough answer here suggests this may not be what you want; you might want to try the solution below against the other solutions benchmarked there. Nonetheless:

Set up data and colour map

set.seed(101)
library(viridisLite)
vv <- viridis(10)
m <- matrix(sample(1:10, 100, replace = TRUE), nrow = 10)

Draw the picture:

library(rgl)
view3d(theta=0, phi=0)  ## head-on view
par3d(zoom=0.7)         ## (almost) fill window
surface3d(x = 1:10, y = 1:10, z = matrix(0, 10,10), 
          color = vv[m],
          smooth=FALSE, lit=FALSE  ## turn off smoothing/lights
 )

You may need to use pop3d() between surfaces to clear the previous surface ...

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Unfortunately this solution is very slow compared to just using grid.raster. Looking at the demo of rdyncall, which allows you to call OpenGL & SDL directly, there should be faster ways still though using OpenGL acceleration, https://dyncall.org/demos/soulsalicious/index.html, but haven't figured that out yet. That demo https://dyncall.org/demos/soulsalicious/soulsalicious.tar.gz shows how to display PNG images etc, so shouldn't be too far for what we would need... – Tom Wenseleers Dec 19 '22 at 14:22
  • But still no answer to my 4 year old question https://stackoverflow.com/questions/48119360/performant-2d-opengl-graphics-in-r-for-fast-display-of-raster-image-using-rdynca. Maybe I should ask ChatGPT for some help? – Tom Wenseleers Dec 19 '22 at 14:22
  • Sorry, this is just what I know how to do. Someone has a recent deleted answers under your old question, don't know why they deleted it (haven't tried it for myself) ... ? – Ben Bolker Dec 19 '22 at 14:35
  • No worries! How can I see that deleted answer? Or is it only above a certain reputation that you can see deleted answers? Curious to see the code. ChatGPT got me somewhat one the way to an answer, but still needs a bit of debugging... – Tom Wenseleers Dec 19 '22 at 14:48
  • 1
    Just found that nativeRaster of the {nara} package is very fast: github.com/coolbutuseless/nara. So that's currently the fastest option I know about! He even programmed Pacman with it in R! https://github.com/coolbutuseless/pacman See also my answer here https://stackoverflow.com/questions/48119360/performant-2d-opengl-graphics-in-r-for-fast-display-of-raster-image-using-rdynca – Tom Wenseleers Dec 20 '22 at 21:07
  • Here is a short demo I just made of that {nara} package, using it for real-time Mandelbrot zooms, cf. function zoom() in https://github.com/tomwenseleers/mandelExplorer – Tom Wenseleers Dec 22 '22 at 18:26
  • So I posted an SDL/OpenGL solution here: https://stackoverflow.com/questions/48119360/performant-2d-sdl-or-opengl-graphics-in-r-for-fast-display-of-raster-image-using/76472961?noredirect=1#comment134865427_76472961. Still some scope to do the colour mapping also in OpenGL though - right now I am doing that beforehand... – Tom Wenseleers Jun 28 '23 at 15:11