【问题标题】:Performant 2D OpenGL graphics in R for fast display of raster image using qtpaint (qt) or rdyncall (SDL/OpenGL) packages?R 中的高性能 2D OpenGL 图形,用于使用 qtpaint (qt) 或 rdyncall (SDL/OpenGL) 包快速显示光栅图像?
【发布时间】:2018-06-15 14:32:24
【问题描述】:

对于我在 R & Rcpp+OpenMP & Shiny 中制作的实时交互式 Mandelbrot 查看器,我正在寻找一种将 1920x1080 矩阵显示为光栅图像的高性能方式,希望能够实现 ca. 5-10 fps(calculating the Mandelbrot images themselves now achieves ca. 20-30 fps at moderate zooms,当然滚动应该很快)。将image() 与选项useRaster=TRUEplot.raster 甚至grid.raster() 一起使用仍然不能完全解决问题,因此我正在寻找性能更高的选项,最好使用OpenGL 加速。

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

而且我还注意到应该能够使用 rdyncall 包(从 https://cran.r-project.org/src/contrib/Archive/rdyncall/ 安装和从 https://www.libsdl.org/download-1.2.php 安装 SDL)调用 SDL 和 GL/OpenGL 函数,可在 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 加速显示二维图像光栅?如果是这样,有没有人有任何想法如何做到这一点(我问是因为我不是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 

【问题讨论】:

  • 尝试使用 quadmesh 包将光栅转换为有效的 rgl 形式。
  • 您可以设置 z = 0。我并不是说它可以解决您的问题!我不知道解决方案,也很想找到一个。使用 raster 或 GDAL 提供特定于当前窗口的详细程度是我见过的最好的,但很难将这些部分串在一起
  • 顺便说一句,我不认为你在这里驾驶魔法,没有转换为那种格式(当然情节不会隐含地这样做??)
  • 不,我一直希望在这里得到答案,但不幸的是,到目前为止我还没有得到答案......

标签: r qt opengl graphics sdl


【解决方案1】:

根据RGL package introduction,是:

R 的可视化设备系统,使用 OpenGL 作为渲染后端。 rgl 设备的核心是一个用 C++ 编写的实时 3D 引擎。它提供了一个交互式视点导航工具(鼠标+滚轮支持)和一个 R 编程接口。

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

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

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

【讨论】:

  • 我尝试了 RGL 库,但它只适用于 3D OpenGL,而不是 2D,我在这里需要它......所以恐怕这行不通(我之前尝试过那个选项).. .
  • 我认为该 rdyncall 包是可行的方法,但恐怕我需要一些帮助才能使用它,因为我不是 OpenGL 专家...
  • 您是否尝试在 (XY) 平面/网格上绘制 3D 数据并将 Z 轴上的相机(位置和方向)朝向 XY 网格固定?
  • 是的,我试过了,但我的数据不是 3D,所以效率不高 - 有非常有效的 OpenGL 方法用于纯 2D 图形,应该在这里使用...
  • @derhass 好吧,至少 RGL 包中使用的 OpenGL 的一小部分不足以满足我的要求,但请随时证明我错了......直接使用 SDL 和 OpenGL另一方面,使用 rdyncall 应该可以工作(查看上面链接的随机字段的演示),但我还没有弄清楚如何让它为我的特定问题工作......
猜你喜欢
  • 2010-12-06
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多