Forum: help
Monitor Forum | | RE: Decorating shapes in rgl [ Reply ] By: Lars Relund on 2020-03-10 13:48 | [forum:47983] |
|
I found out how to add lines # A plane using lines plot3d(pts, xlim = c(-1,5), ylim = c(-1,5), zlim = c(-1,10), type = 'n') normal <- c(1,1,1) offset <- -3 lines <- 100 limits <- rgl::par3d()$bbox m <- c(limits[1], limits[3], limits[5]) M <- c(limits[2], limits[4], limits[6]) # do a mesh x <- seq(m[1], M[1], length.out = lines) y <- seq(m[2], M[2], length.out = lines) f <- function(x,y){-(normal[1]/normal[3])*x - (normal[2]/normal[3])*y - offset/normal[3]} z <- outer(x,y,f) z[z < m[3] | z > M[3]] <- NA persp3d(x, y, z, back = 'lines', front = 'lines', add = TRUE) # A polygon using lines lines <- 50 plot3d(pts, xlim = c(-1,2), ylim = c(-1,2), zlim = c(-1,2), type = 'n') pts <- data.frame(x = c(1,0,0,0.4), y = c(0,1,0,0.3), z = c(0,0,1,0.3)) polygon3d(pts, col = "white", polygon_offset = 1) #' pts <- data.frame(x = c(1,0,0), y = c(0,1,0), z = c(0,0,1)) x <- pts[,1] y <- pts[,2] z <- pts[,3] x0 <- seq(min(pts$x), max(pts$x), length.out = lines) y0 <- seq(min(pts$y), max(pts$y), length.out = lines) xy <- expand.grid(x0, y0) xy[sp::point.in.polygon(xy[,1], xy[,2], pts$x, pts$y) <= 0,] <- NA z0 <- predict(lm(z ~ x + y), newdata = data.frame(x=xy[,1], y=xy[,2])) persp3d(x0, y0, z0, back = 'lines', front = 'lines', add = TRUE) However, can you give an example on how to add texture to a plane? |
|
| RE: Decorating shapes in rgl [ Reply ] By: Duncan Murdoch on 2020-03-09 19:35 | [forum:47460] |
| Sure, why not? | |
| RE: Decorating shapes in rgl [ Reply ] By: Lars Relund on 2020-03-09 19:07 | [forum:47459] |
| Cool. Would it be possible to draw lines / mesh inside the polygon too? | |
| RE: Decorating shapes in rgl [ Reply ] By: Duncan Murdoch on 2020-03-06 13:28 | [forum:47457] |
|
Here's some code showing both approaches: library(rgl) pts<-matrix(c(1,0,0,1,2,1,1,3,2,4,1,4), ncol = 3, byrow = TRUE) x <- pts[,1] y <- pts[,2] z <- pts[,3] z1 <- predict(lm(z ~ x + y)) pts <- cbind(x, y, z1) filename <- tempfile(fileext = ".png") png(filename = filename) plot(1, 1, pch = 16, cex = 10, axes=FALSE, xlab="", ylab="") dev.off() open3d() poly <- polygon3d(pts, plot = FALSE) poly$texcoords <- rbind(x, y)*5 shade3d(poly, col = "white", specular = "black", texture = filename) decorate3d() x0 <- seq(min(x), max(x), by = 0.1) y0 <- seq(min(y), max(y), by = 0.1) xy <- expand.grid(x0, y0) xy <- xy[sp::point.in.polygon(xy[,1], xy[,2], x, y) > 0,] xyz <- cbind(xy, predict(lm(z ~ x + y), newdata = data.frame(x=xy[,1], y=xy[,2]))) open3d() polygon3d(pts, col = "white", polygon_offset = 1) points3d(xyz) |
|
| RE: Decorating shapes in rgl [ Reply ] By: Duncan Murdoch on 2020-03-06 10:51 | [forum:47456] |
|
That's not a valid polygon -- it's not planar. You can make it planar by fitting z to x + y using lm(), e.g. x <- pts[,1] y <- pts[,2] z <- pts[,3] z1 <- predict(lm(z ~ x+y)) Then you could use a texture to fill in the surface, but I don't think polygon3d supports textures very well. You'll need to figure out how to specify the texture correctly. Alternatively, just set up a grid of values in x and y, select those that lie in the polygon, predict the z value for each, and plot points. |
|
| Decorating shapes in rgl [ Reply ] By: Lars Relund on 2020-03-06 10:07 | [forum:47455] |
|
Assume the following polygon pts<-matrix(c(1,0,0,1,2,1,1,3,2,4,1,4), ncol = 3, byrow = TRUE) open3d() polygon3d(x = pts[,1], y = pts[,2], z = pts[,3]) axes3d() Is there a way to decorate the surface of the polygon with e.g. dots? I guess one way is to use a texture of a png file with a dot. |
|

