Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle smooth = FALSE objects by modifying them in convertScene(). #442

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: rgl
Version: 1.3.13
Version: 1.3.14
Title: 3D Visualization Using OpenGL
Authors@R: c(person("Duncan", "Murdoch", role = c("aut", "cre"),
email = "[email protected]"),
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# rgl 1.3.13
# rgl 1.3.14

## Minor changes

Expand All @@ -12,6 +12,8 @@ background could create a leak of a background object
(issue #439). For back compatibility, these cases
still increment the object ID number, but don't
actually create a new object.
* `rglwidget()` displays didn't support objects with
`smooth = FALSE`.

# rgl 1.3.12

Expand Down
61 changes: 60 additions & 1 deletion R/convertScene.R
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ convertScene <- function(x = scene3d(minimal), width = NULL, height = NULL,
}
if (!is.null(obj$material)) # Never use material$color
obj$material$color <- NULL

if (!is.null(obj$material) && !is.null(obj$material$smooth) && !obj$material$smooth)
obj <- nonSmooth(obj)
} else if (obj$type == "subscene") {
obj$par3d$viewport$x <- obj$par3d$viewport$x/fullviewport$width
obj$par3d$viewport$width <- obj$par3d$viewport$width/fullviewport$width
Expand Down Expand Up @@ -502,3 +503,61 @@ convertScene <- function(x = scene3d(minimal), width = NULL, height = NULL,

result
}

# Modify objects that have smooth = FALSE. Convert
# surfaces to triangles, add vertices so that we can
# set all triangles or quads to solid colours.

nonSmooth <- function(obj) {
oldcolors <- obj$colors
ncolors <- NROW(oldcolors)
if (ncolors > 1) {
if (obj$type == "surface") {
dim <- obj$dim
ul <- rep(2:dim[1], dim[2]-1) + dim[1]*rep(0:(dim[2]-2), each=dim[1]-1)
if (obj$flipped)
indices <- c(rbind(ul-1,
ul,
ul-1+dim[1],
ul+dim[1],
ul-1+dim[1],
ul
))
else
indices <- c(rbind(ul,
ul-1,
ul-1+dim[1],
ul-1+dim[1],
ul+dim[1],
ul))
} else
indices <- obj$indices

if (!is.null(indices)) {
obj$vertices <- obj$vertices[indices,]
oldcolors <- obj$colors[indices,]
obj$normals <- obj$normals[indices,]
if (!is.null(obj$texcoords))
obj$texcoords <- obj$texcoords[indices,]
obj$indices <- NULL
}
newcolors <- oldcolors
i <- seq_len(NROW(newcolors))
nverts <- switch(obj$type,
"triangles" = 3,
"quads" = 4,
"surface" = 6)

for (j in seq_len(nverts-1))
newcolors[i %% nverts == j] <- newcolors[i %% nverts == 0]
obj$colors <- newcolors
if (obj$type == "surface") {
obj$type <- "triangles"
obj$dim <- NULL
obj$centers <- (obj$vertices[i %% 3 == 0,] +
obj$vertices[i %% 3 == 1,] +
obj$vertices[i %% 3 == 2,])/3
}
}
obj
}
5 changes: 4 additions & 1 deletion man/axes3d.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ objects added to the scene.
\author{ Duncan Murdoch }

\seealso{Classic graphics functions \code{\link{axis}}, \code{\link{box}},
\code{\link{title}}, \code{\link{mtext}}, and RGL function \code{\link{bbox3d}}.}
\code{\link{title}}, \code{\link{mtext}} are related.
See RGL functions \code{\link{bbox3d}} for drawing the
box around the plot, and
\code{\link{setAxisCallbacks}} for customized axes.}

\examples{
open3d()
Expand Down