When I want to plot polygons using rgl
, for certain coordinate pairs, I get an error:
# polygon data
xyz <- structure(c(5.90000000002328, 6, 6.20000000001164, 5.90000000002328,
12.2999999998137, 12.2999999998137, 12.2999999998137, 12.2999999998137,
7.45599365234375, 7.6009521484375, 7.7039794921875, 7.45599365234375
), dim = 4:3, dimnames = list(c("p1", "p2", "p3", "p1"), c("x",
"y", "Z")))
# plotting
polygon3d(
x = as.numeric(xyz[,1]),
y = as.numeric(xyz[,2]),
z = as.numeric(xyz[,3]), col = "red")
> Error in processOutside(i) : Cannot triangulate polygon
However, for other polygons, it works, e.g. for:
# working polygon
xyz <- structure(c(3.70000000001164, 3, 3.09999999997672, 3.70000000001164,
12.2999999998137, 12, 12, 12.2999999998137, 8.74200439453125,
8.91094970703125, 8.990966796875, 8.74200439453125), dim = 4:3, dimnames = list(
c("p1", "p2", "p3", "p1"), c("x", "y", "Z")))
I thought it might be because of the proximity of the points, but after multiplying the points with 100 or rounding decimal points, I still get an error. Why do I get this error? Does this have to do with too sharp angles?
CodePudding user response:
The docs for polygon3d
say that it uses the coords
argument (default 1:2
) to choose the coordinates to use for triangulating the polygon. Since your y
coordinate is constant, this creates a degenerate triangle and triangulation fails.
To fix this, specify coords = c(1, 3)
to tell it to use x
and z
coordinates instead, i.e.
polygon3d(
x = as.numeric(xyz[,1]),
y = as.numeric(xyz[,2]),
z = as.numeric(xyz[,3]),
coords = c(1,3), col = "red")
A change is in the works to make this choice automatically; you should soon be able to use your original code if you install the development version of rgl
(version 0.109.15 or greater). Use
remotes::install_github("dmurdoch/rgl")
It will require a full set of tools (compilers, etc.) to run.
CodePudding user response:
The rgl
manual mentions that
Polygons must be non-degenerate and quadrilaterals must be entirely in one plane and convex, or the results are undefined.
However, I don't see how yours can be degenerate. I would plot using another method:
xyz <- structure(c(5.90000000002328, 6, 6.20000000001164, 5.90000000002328,
6, 12.2999999998137, 12.2999999998137, 12.2999999998137,
7.45599365234375, 7.6009521484375, 7.7039794921875, 7.45599365234375
), dim = 4:3, dimnames = list(c("p1", "p2", "p3", "p1"), c("x", "y", "Z")))
x <- as.numeric(xyz[,1])
y <- as.numeric(xyz[,2])
z <- as.numeric(xyz[,3])
plot3d(x,y,z)
segments3d(x[1:2],y[1:2],z[1:2],col="blue",lwd=2)
segments3d(x[2:3],y[2:3],z[2:3],col="blue",lwd=2)
segments3d(x[c(1,3)],y[c(1,3)],z[c(1,3)],col="blue",lwd=2)
xyz <- structure(c(3.7, 3, 3.09, 3.7,
12.3, 12, 12, 12.3, 8.74,
8.91, 9, 8.74), dim = 4:3, dimnames = list(
c("p1", "p2", "p3", "p1"), c("x", "y", "Z")))
x <- as.numeric(xyz[,1])
y <- as.numeric(xyz[,2])
z <- as.numeric(xyz[,3])
plot3d(x,y,z)
segments3d(x[1:2],y[1:2],z[1:2],col="red",lwd=2)
segments3d(x[2:3],y[2:3],z[2:3],col="red",lwd=2)
segments3d(x[c(1,3)],y[c(1,3)],z[c(1,3)],col="red",lwd=2)