r - R 中的高性能 2D OpenGL 图形用于使用 qtpaint (qt) 或 rdyncall (SDL/OpenGL) 包快速显示光栅图像?

标签 r qt opengl graphics sdl

对于我在 R & Rcpp+OpenMP & Shiny 中制作的实时交互式 Mandelbrot 查看器,我正在寻找一种将 1920x1080 矩阵显示为光栅图像的高性能方式,希望能够实现大约。 5-10 fps( calculating the Mandelbrot images themselves now achieves ca. 20-30 fps at moderate zooms ,当然滚动应该很快)。使用 image()带选项 useRaster=TRUE , plot.raster甚至 grid.raster()仍然没有完全削减它,所以我正在寻找更高性能的选项,理想情况下使用 OpenGL 加速。

我注意到有 qt包装包装qtutilsqtpaint http://finzi.psych.upenn.edu/R/library/qtutils/html/sceneDevice.html
您可以在哪里设置参数 opengl=TRUE
http://finzi.psych.upenn.edu/R/library/qtpaint/html/qplotView.html
再次与参数 opengl=TRUEhttp://finzi.psych.upenn.edu/R/library/qtpaint/html/painting.html .

我还注意到应该能够使用 rdyncall 调用 SDL 和 GL/OpenGL 函数。包(从 https://cran.r-project.org/src/contrib/Archive/rdyncall/ 安装,从 https://www.libsdl.org/download-1.2.php 安装 SDL)`,演示可在 http://hg.dyncall.org/pub/dyncall/bindings/file/87fd9f34eaa0/R/rdyncall/demo/00Index 获得,例如http://hg.dyncall.org/pub/dyncall/bindings/file/87fd9f34eaa0/R/rdyncall/demo/randomfield.R )。

我是否正确,使用这些包应该能够使用 opengl 显示 2D 图像光栅加速度?如果是这样,有没有人有任何想法如何做到这一点(我之所以这么问是因为我不是 qtSDL/OpenGL 的专家)?

非 OpenGL 选项的某些时序对于我的应用程序来说太慢了:

# some example data & desired colour mapping of [0-1] ranged data matrix
library(RColorBrewer)
ncol=1080
cols=colorRampPalette(RColorBrewer::brewer.pal(11, "RdYlBu"))(ncol)
colfun=colorRamp(RColorBrewer::brewer.pal(11, "RdYlBu"))
col = rgb(colfun(seq(0,1, length.out = ncol)), max = 255)
mat=matrix(seq(1:1080)/1080,nrow=1920,ncol=1080,byrow=TRUE)
mat2rast = function(mat, col) {
  idx = findInterval(mat, seq(0, 1, length.out = length(col)))
  colors = col[idx]
  rastmat = t(matrix(colors, ncol = ncol(mat), nrow = nrow(mat), byrow = TRUE))
  class(rastmat) = "raster"
  return(rastmat)
}
system.time(mat2rast(mat, col)) # 0.24s

# plot.raster method - one of the best?
par(mar=c(0, 0, 0, 0))
system.time(plot(mat2rast(mat, col), asp=NA)) # 0.26s

# grid graphics - tie with plot.raster?
library(grid)
system.time(grid.raster(mat2rast(mat, col),interpolate=FALSE)) # 0.28s

# base R image()
par(mar=c(0, 0, 0, 0))
system.time(image(mat,axes=FALSE,useRaster=TRUE,col=cols)) # 0.74s # note Y is flipped to compared to 2 options above - but not so important as I can fill matrix the way I want

# magick - browser viewer, so no good....
# library(magick)
# image_read(mat2rast(mat, col))

# imager - doesn't plot in base R graphics device, so this one won't work together with Shiny
# If you wouldn't have to press ESC to return control to R this
# might have some potential though...
library(imager)
display(as.cimg(mat2rast(mat, col)))

# ggplot2 - just for the record...
df=expand.grid(y=1:1080,x=1:1920)
df$z=seq(1,1080)/1080
library(ggplot2)
system.time({q <- qplot(data=df,x=x,y=y,fill=z,geom="raster") + 
                scale_x_continuous(expand = c(0,0)) + 
                scale_y_continuous(expand = c(0,0)) +
                scale_fill_gradientn(colours = cols) + 
                theme_void() + theme(legend.position="none"); print(q)}) # 11s 

最佳答案

根据RGL package introduction , 这是 :

a visualization device system for R, using OpenGL as the rendering backend. An rgl device at its core is a real-time 3D engine written in C++. It provides an interactive viewpoint navigation facility (mouse + wheel support) and an R programming interface.



由于 RGL 是一个实时 3D 引擎,我希望将 RGL 用于 2D 会给你一个快速的显示。

请注意,这是一个旧项目,所以我不确定它是否符合您的要求。

你可以看看this paper并查看一些结果图像 in this gallery

关于r - R 中的高性能 2D OpenGL 图形用于使用 qtpaint (qt) 或 rdyncall (SDL/OpenGL) 包快速显示光栅图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48119360/

相关文章:

C++ 析构函数奇怪的行为

r - 转换为循环应用

r - xgboost R包安装失败

r - 从具有开始和结束位置的向量创建序列

java - 没有 glGet* 调用的 opengl 截锥体剔除

opengl - 关于 glBindTexture 的问题

linux - 通过 ssh 浏览(或下载)R vignettes

linux - Qimage 到 cv::Mat 转换奇怪的行为

qt - 如何找到日期的日期

visual-studio - 如何使用 qmake 创建 vcproj 使其过滤器反射(reflect)目录结构?