また、カメラのオイラー角、座標、視野角、画像サイズからモデルビュー行列・プロジェクション行列を作る関数は以下の通りです(R言語)
# camera intrinsic matrix
projection_mat <- function(fov_x_degree, w, h, near = -1, far = 1
, cx = w/2, cy = h/2){
fov_x <- fov_x_degree * pi /180
fov_y <- fov_x * h / w
fx <- 1/(tan(fov_x/2))
fy <- 1/(tan(fov_y/2))
matrix(c(fx , 0, (w-2*cx)/w, 0,
0, fy, -(h-2*cy)/h, 0,
0, 0, -(far+near)/(far-near), -2*far*near/(far-near),
0, 0, -1, 0),
ncol = 4,
byrow = F) %>%
as.vector() %>%
reticulate::np_array(dtype = "float32")
}
# camera extrinsic parameter
modelview_mat <- function(pan_degree, tilt_degree, roll_degree, t_x, t_y, t_z){
y <- (360 - pan_degree) * pi / 180
x <- (tilt_degree) * pi / 180
z <- (roll_degree) * pi / 180
rmat_z <- c(cos(z), -sin(z), 0, 0,
sin(z), cos(z), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1) %>% matrix(ncol = 4, byrow = T)
rmat_x <- c(1, 0, 0, 0,
0, cos(x), -sin(x), 0,
0, sin(x), cos(x), 0,
0, 0, 0, 1) %>% matrix(ncol = 4, byrow = T)
rmat_y <- c(cos(y), 0, sin(y), 0,
0, 1, 0, 0,
-sin(y), 0, cos(y), 0,
0, 0, 0, 1) %>%matrix(ncol = 4, byrow = T)
rmat <- rmat_x %*% rmat_z %*% rmat_y
tmat <- matrix(c(1, 0, 0, -t_x,
0, 1, 0, -t_z,
0, 0, 1, -t_y,
0, 0, 0, 1), byrow = T, ncol = 4)
rmat %*% tmat %>%
as.vector() %>%
reticulate::np_array(dtype = "float32")
}