这是 cur3d 的正确行为吗?代码如下所示。
% R
> source("ex.R")
> func1()
> func2()
单击 func1 窗口 3 次,然后单击 func2 窗口 3 次。结果是:
func1: 2
func1: 2
func1: 2
func2: 2
func2: 2
func2: 2
代码如下:
library(rgl)
func1 <- function() {
func1.dev.id <- open3d()
func1.scene.id <- newSubscene3d()
begin <- function(x, y) {
cat("func1:", cur3d(), "\n")
}
update <- function(x, y) {
cat("func1:", cur3d(), "\n")
}
useSubscene3d(func1.scene.id)
rgl.setMouseCallbacks(1, begin, update)
}
func2 <- function() {
func2.dev.id <- open3d()
func2.scene.id <- newSubscene3d()
begin <- function(x, y) {
cat("func2:", cur3d(), "\n")
}
update <- function(x, y) {
cat("func2:", cur3d(), "\n")
}
useSubscene3d(func2.scene.id)
rgl.setMouseCallbacks(1, begin, update)
}
我想知道这是否是正确的行为。
@Stibu 在评论中给出了解释:“
cur3d()
返回当前窗口的设备号,这是最后一个启动的窗口。如果运行适当的函数,rgl
将绘制到这个窗口。 cur3d()
不会返回您单击的窗口的设备号,因为单击不会更改当前窗口的内容。所以这确实是预期的行为。”
要获得您期望的行为,您应该在
begin
事件(对应于此处的鼠标按下事件)中显式更改当前设备:
library(rgl)
func1 <- function() {
func1.dev.id <- open3d()
func1.scene.id <- newSubscene3d()
begin <- function(x, y) {
set3d(func1.dev.id, silent = TRUE)
cat("func1:", cur3d(), "\n")
}
update <- function(x, y) {
cat("func1:", cur3d(), "\n")
}
useSubscene3d(func1.scene.id)
rgl.setMouseCallbacks(1, begin, update)
}
func2 <- function() {
func2.dev.id <- open3d()
func2.scene.id <- newSubscene3d()
begin <- function(x, y) {
set3d(func2.dev.id, silent = TRUE)
cat("func2:", cur3d(), "\n")
}
update <- function(x, y) {
cat("func2:", cur3d(), "\n")
}
useSubscene3d(func2.scene.id)
rgl.setMouseCallbacks(1, begin, update)
}
根据您打算在实际代码中执行的操作,您可能还想将
useSubscene3d(...)
添加到 begin
函数中。在本示例中没有必要:每个设备都有一个关联的当前子场景。
在我的系统(Mac)上,第一次单击窗口会选择该窗口,只有第二次单击才会发送到内容。因此,要在不同的
rgl
窗口上调用您的函数,我需要单击它两次。