1

When using rgl, I am trying to get smooth lines drawn in 3D space. At certain view angles, the lines join smoothly together; at other view angles, they look completely disjointed.

For example, using 3dlines to draw the following:

x = c(-32.76755,-32.84534,-32.90371,-32.94217,-32.96003,-32.95662,-33.04784,-33.03502,-32.98490,-32.86505,-32.79418,-32.59470,-32.52399,-32.44813,-32.23378,-32.18913)
y = c(13.15037,12.38736,11.65954,10.9627,10.29022,9.6338,8.67939,7.70395,7.04928,6.05184,5.37749,4.35355,3.64888,2.92609,1.86025,1.11037)
z = c(1.54869,2.22778,2.94911,3.70456,4.48397,5.27592,5.11489,4.94259,5.70322,5.48567,6.18623,5.90521,6.53304,7.12764,6.74987,7.29049)

with a line width of 10:

lines3d(x=x, y=y, z=z, lwd=10)

at this angle, it looks ok:

par3d(userMatrix = matrix(data = c(0.7643722, -0.0511867, -0.6427402, 0, -0.5819589, 0.3744012, -0.7219055, 0, 0.2775946, 0.9258529, 0.2563933, 0, 0, 0, 0, 1), nrow=4, ncol=4))

Correct line join

but here, there is an issue with the joining between the lines:

par3d(userMatrix = matrix(data = c(0.85497350, -0.02970354, -0.51782036, 0, -0.4698080, 0.3786791, -0.7974223, 0, 0.2197740, 0.9250512, 0.3098056, 0, 0, 0, 0, 1), nrow=4, ncol=4))

Incorrect line joins

Is this just a limitation of rgl, or is there a setting I'm missing here?

2 Answers2

1

This is a deeper issue with OpenGL: OpenGL GL_LINES endpoints not joining

Base-R has a graphical parameter that adds rounded ends to line segments. It doesn't exist for rgl, but adding points at the intersection is a (hacky) way to fill in the gaps ...

lines3d(x=x, y=y, z=z, lwd=10)
points3d(x=x, y=y, z=z, size=10, point_antialias = TRUE)

point_antialias = TRUE is supposed to make points display as circles rather than squares, which would be prettier, but is device-dependent; doesn't have any effect on the machine I'm on at the moment, so if you look closely you can see the corners of the square. Still, I think it's an improvement.

enter image description here

(I didn't use your exact par3d() ...)

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
1

As @BenBolker said in his answer, this is an issue with line drawing in the old OpenGL pipeline that rgl uses in R. If you export the scene to WebGL by calling rglwidget(), it uses more modern OpenGL shaders, and looks a lot better.

A workaround within R is to draw cylinders instead of lines. These will be slower to display, but modern graphics hardware is fast enough that you probably won't notice any difference.

For example,

line <- cylinder3d(cbind(x, y, z), radius = 0.1)
shade3d(line, col = "black", lit = FALSE)

screen shot

The width varies a little along the line, but I find that less distracting than the breaks at the corners.

user2554330
  • 37,248
  • 4
  • 43
  • 90