Search code examples
rrgl

Creating polygons with sharp angles does not work


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?


Solution

  • 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.