This vignette holds code that was previously included as “demos” in
rgl
. Some of the demos require R to be running; those
remain available via demo(package = "rgl")
.
##########
### 3D HIST EXAMPLE:
##########
################################################################################
##### Required functions 'binplot' and 'hist3d':
.3d<-function(x,y,z,alpha=1,topcol="#ff0000",sidecol="#aaaaaa") {
binplot<- par3d(skipRedraw=TRUE)
save on.exit(par3d(save))
<-c(rep(c(x[1],x[2],x[2],x[1]),3),rep(x[1],4),rep(x[2],4))
x1<-c(rep(0,4),rep(c(0,0,z,z),4))
z1<-c(y[1],y[1],y[2],y[2],rep(y[1],4),rep(y[2],4),rep(c(y[1],y[2],y[2],y[1]),2))
y1<-c(rep(c(x[1],x[1],x[2],x[2]),2),rep(c(x[1],x[2],rep(x[1],3),rep(x[2],3)),2))
x2<-c(rep(c(0,z),4),rep(0,8),rep(z,8) )
z2<-c(rep(y[1],4),rep(y[2],4),rep(c(rep(y[1],3),rep(y[2],3),y[1],y[2]),2) )
y2quads3d(x1,z1,y1,col=rep(sidecol,each=4),alpha=alpha)
quads3d(c(x[1],x[2],x[2],x[1]),rep(z,4),c(y[1],y[1],y[2],y[2]),
col=rep(topcol,each=4),alpha=1)
segments3d(x2,z2,y2,col="#000000")
}
<-function(x,y=NULL,nclass="auto",alpha=1,col="#ff0000",scale=10) {
hist3d<- par3d(skipRedraw=TRUE)
save on.exit(par3d(save))
<- xy.coords(x,y)
xy <- xy$x
x <- xy$y
y <-length(x)
nif (nclass == "auto") nclass<-ceiling(sqrt(nclass.Sturges(x)))
<- seq(min(x),max(x),length=(nclass+1))
breaks.x <- seq(min(y),max(y),length=(nclass+1))
breaks.y <-matrix(0,(nclass),(nclass))
zfor (i in seq_len(nclass)) {
for (j in seq_len(nclass)) {
<- (1/n)*sum(x < breaks.x[i+1] & y < breaks.y[j+1] &
z[i, j] >= breaks.x[i] & y >= breaks.y[j])
x binplot.3d(c(breaks.x[i],breaks.x[i+1]),c(breaks.y[j],breaks.y[j+1]),
*z[i,j],alpha=alpha,topcol=col)
scale
}
}
}################################################################################
open3d()
#> null
#> 15
bg3d(color="gray")
light3d(0, 0)
# Drawing a 'bin' for given coordinates:
binplot.3d(c(-0.5,0.5),c(4.5,5.5),2,alpha=0.6)
# Setting the viewpoint ('theta' and 'phi' have the same meaning as in persp):
view3d(theta=40,phi=40)
##### QUADS FORMING BIN
open3d()
#> null
#> 16
# Defining transparency and colors:
<-0.7; topcol<-"#ff0000"; sidecol<-"#aaaaaa"
alpha
# Setting up coordinates for the quads and adding them to the scene:
<-x<-c(-1,1) ; z<-4; of<-0.3
y<-c(x[1],x[2],x[2],x[1]); x11<-rep(x[1],4); x22<-rep(x[2],4)
x12<-rep(0,4); z0z<-c(0,0,z,z); zzz<-rep(z,4); y11<-rep(y[1],4)
z00<-c(y[1],y[1],y[2],y[2]); y12<-c(y[1],y[2],y[2],y[1]); y22<-rep(y[2],4)
y1122
quads3d(c(x12,x12,x11-of,x12,x22+of,x12),
c(z00-of,rep(z0z,4),zzz+of),
c(y1122,y11-of,y12,y22+of,y12,y1122),
col=rep(c(rep(sidecol,5),topcol),each=4),alpha=c(rep(alpha,5),1))
# Setting up coordinates for the border-lines of the quads and drawing them:
<-c(y[1],y[2],y[1],y[2]); yl2<-c(y[1]-of,y[1]-of)
yl1<-c(rep(x[1],8),rep(x[1]-of,8),rep(c(x[1],x[2]),8),rep(x[2],8),rep(x[2]+of,8))
xl<-c(0,z,0,z,z+of,z+of,-of,-of,0,0,z,z,0,z,0,z,rep(0,4),rep(z,4),rep(-of,4),
zlrep(z+of,4),z+of,z+of,-of,-of,rep(c(0,z),4),0,0,z,z)
<-c(yl2,y[2]+of,y[2]+of,rep(c(y[1],y[2]),4),y[1],y[1],y[2],y[2],yl2,
ylrep(y[2]+of,4),yl2,y[2],y[2],rep(y[1],4),y[2],y[2],yl1,yl2,y[2]+of,
2]+of,y[1],y[1],y[2],y[2],yl1)
y[
lines3d(xl,zl,yl,col="#000000")
view3d(theta=40,phi=40)
##### COMPLETE HISTOGRAM:
open3d()
#> null
#> 17
# Setting the rng to a fixed value:
set.seed(1000)
# Drawing a 3d histogramm of 2500 normaly distributed observations:
hist3d(rnorm(2500),rnorm(2500),alpha=0.4,nclass=7,scale=30)
# Choosing a lightgrey background:
bg3d(col="#cccccc")
view3d(theta=40,phi=40)
# rgl demo: rgl-bivar.r
# author: Daniel Adler
<- function() {
rgl.demo.bivar if (!requireNamespace("MASS", quietly = TRUE))
stop("This demo requires MASS")
# parameters:
<-50; ngrid<-40
n
# generate samples:
set.seed(31415)
<-rnorm(n); y<-rnorm(n)
x
# estimate non-parameteric density surface via kernel smoothing
<- MASS::kde2d(x, y, n=ngrid)
denobj <-denobj$z
den.z
# generate parametric density surface of a bivariate normal distribution
<- denobj$x
xgrid <- denobj$y
ygrid <- dnorm(xgrid)%*%t(dnorm(ygrid))
bi.z
# visualize:
<-20
zscale
# New window
open3d()
# clear scene:
clear3d("all")
# setup env:
bg3d(color="#887777")
light3d()
# Draws the simulated data as spheres on the baseline
spheres3d(x,y,rep(0,n),radius=0.1,color="#CCCCFF")
# Draws non-parametric density
surface3d(xgrid,ygrid,den.z*zscale,color="#FF2222",alpha=0.5)
# Draws parametric density
surface3d(xgrid,ygrid,bi.z*zscale,color="#CCCCFF",front="lines")
}
rgl.demo.bivar()
# RGL-Demo: animal abundance
# Authors: Oleg Nenadic, Daniel Adler
<- function() {
rgl.demo.abundance open3d()
clear3d("all") # remove all shapes, lights, bounding-box, and restore viewpoint
# Setup environment:
bg3d(col="#cccccc") # setup background
light3d() # setup head-light
# Importing animal data (created with wisp)
<-dget(system.file("demodata/region.dat",package="rgl"))
terrain<-dget(system.file("demodata/population.dat",package="rgl"))
pop
# Define colors for terrain
<- range(terrain)
zlim <- terrain.colors(82)
colorlut <- colorlut[9*sqrt(3.6*(terrain-zlim[1])+2)]
col1
# Set color to (water-)blue for regions with zero 'altitude'
==0]<-"#0000FF"
col1[terrain
# Add terrain surface shape (i.e. population density):
surface3d(
1:100,seq(1,60,length=100),terrain,
col=col1,spec="#000000", ambient="#333333", back="lines"
)
# Define colors for simulated populations (males:blue, females:red):
<-pop[,4]
col2==0]<-"#3333ff"
col2[col2==1]<-"#ff3333"
col2[col2
# Add simulated populations as sphere-set shape
spheres3d(
1],
pop[,2],
pop[,cbind( ceiling(pop[,1]),ceiling(pop[,2]*10/6) )]+0.5,
terrain[radius=0.2*pop[,3], col=col2, alpha=(1-(pop[,5])/10 )
)
}rgl.demo.abundance()
# demo: lsystem.r
# author: Daniel Adler
#
# geometry
#
<- function( degree ) {
deg2rad return( degree*pi/180 )
}
<- function( degree ) {
rotZ.m3x3 <- cos(deg2rad(degree))
kc <- sin(deg2rad(degree))
ks return(
matrix(
c(
-ks, 0,
kc, 0,
ks, kc, 0, 0, 1
ncol=3,byrow=TRUE
),
)
)
}
<- function( degree ) {
rotX.m3x3 <- cos(deg2rad(degree))
kc <- sin(deg2rad(degree))
ks return(
matrix(
c(
1, 0, 0,
0, kc, -ks,
0, ks, kc
ncol=3,byrow=TRUE
),
)
)
}
<- function( degree ) {
rotY.m3x3 <- cos(deg2rad(degree))
kc <- sin(deg2rad(degree))
ks return(
matrix(
c(
0, ks,
kc, 0, 1, 0,
-ks, 0, kc
ncol=3,byrow=TRUE
),
)
)
}
<- function( v, degree ) {
rotZ return( rotZ.m3x3(degree) %*% v)
}
<- function( v, degree ) {
rotX return( rotX.m3x3(degree) %*% v)
}
<- function( v, degree ) {
rotY return( rotY.m3x3(degree) %*% v)
}
#
# turtle graphics, rgl implementation:
#
<- function(pos=c(0,0,0),head=0,pitch=90,roll=0,level=0) {
turtle.init clear3d("all")
bg3d(color="gray")
light3d()
return( list(pos=pos,head=head,pitch=pitch,roll=roll,level=level) )
}
<- function(turtle, steps, color) {
turtle.move
<- rotX.m3x3(turtle$pitch) %*% rotY.m3x3(turtle$head) %*% rotZ.m3x3(turtle$roll)
rm
<- as.vector( turtle$pos )
from <- rm %*% c(0,0,-1)
dir <- from + dir * steps
to
<- c( from[1], to[1] )
x <- c( from[2], to[2] )
y <- c( from[3], to[3] )
z lines3d(x,y,z,col=color,size=1.5,alpha=0.5)
$pos <- to
turtlereturn(turtle)
}
<- function(turtle, degree) {
turtle.pitch $pitch <- turtle$pitch + degree
turtlereturn(turtle)
}
<- function(turtle, degree) {
turtle.head $head <- turtle$head + degree
turtlereturn(turtle)
}
<- function(turtle, degree) {
turtle.roll $roll <- turtle$roll + degree
turtlereturn(turtle)
}
#
# l-system general
#
<- function( x )
lsystem.code substitute( x )
<- function( x, grammar, levels=0 ) {
lsystem.gen <- eval( substitute( substitute( REPLACE , grammar ), list(REPLACE=x) ) )
code if (levels)
return( lsystem.gen( code , grammar , levels-1 ) )
else
return( code )
}
#
# l-system plot
#
<- function( expr, level ) {
lsystem.plot <- turtle.init(level=level)
turtle lsystem.eval( expr, turtle )
}
<- function( expr, turtle ) {
lsystem.eval if ( length(expr) == 3 ) {
<- lsystem.eval( expr[[2]], turtle )
turtle <- lsystem.eval( expr[[3]], turtle )
turtle <- lsystem.eval( expr[[1]], turtle )
turtle else if ( length(expr) == 2 ) {
} <- turtle
saved <- lsystem.eval( expr[[1]], turtle )
turtle <- lsystem.eval( expr[[2]], turtle )
turtle <- saved
turtle else if ( length(expr) == 1 ) {
} if ( as.name(expr) == "stem" ) turtle <- turtle.move(turtle, 5, "brown")
else if ( as.name(expr) == "short") turtle <- turtle.move(turtle, 5, "brown")
else if ( as.name(expr) == "leaf" ) {
spheres3d(turtle$pos[1],turtle$pos[2],turtle$pos[3],radius=0.1+turtle$level*0.3,color="green")
sprites3d(turtle$pos[1],turtle$pos[2],turtle$pos[3],radius=0.5+turtle$level*0.3 ,color="green",texture=system.file("textures/particle.png",package="rgl"),textype="alpha",alpha=0.5)
}else if ( as.name(expr) == "roll" ) turtle <- turtle.head(turtle, 60)
else if ( as.name(expr) == "down" ) turtle <- turtle.pitch(turtle,10)
else if ( as.name(expr) == "up" ) turtle <- turtle.pitch(turtle,-10)
else if ( as.name(expr) == "left" ) turtle <- turtle.head(turtle, 1)
else if ( as.name(expr) == "right") turtle <- turtle.head(turtle,-1.5)
else if ( as.name(expr) == "turnleft") turtle <- turtle.head(turtle,20)
else if ( as.name(expr) == "turnright") turtle <- turtle.head(turtle,-20)
else if ( as.name(expr) == "turn") turtle <- turtle.roll(turtle,180)
}return(turtle)
}
#
# example
#
<- function(level=0) {
simple <- list(
grammar stem=lsystem.code(
-(up-stem-leaf)-stem-(down-stem-leaf)-stem-leaf
stem
)
)<- lsystem.gen(lsystem.code(stem), grammar, level )
plant lsystem.plot(plant,level)
}
<- function(level=0) {
rgl.demo.lsystem <- list(
gen stem=lsystem.code(
-left-stem-branch( turnleft-down-short-turnleft-down-stem-leaf)-right-right-stem--branch( turnright-up-short-turnright-up-short-turnright-short-stem-leaf)-left-left-left-stem-branch( turnleft-down-short-turnright-down-stem-leaf )-branch( up-turnright-short-up-turnleft-up-stem-leaf )
stem
)
) <- lsystem.gen(lsystem.code(stem), gen, level )
plant lsystem.plot(plant,level)
}
open3d()
#> null
#> 20
rgl.demo.lsystem(level=1)
#> $pos
#> [,1]
#> [1,] -2.092747e+00
#> [2,] 7.992083e+01
#> [3,] -4.893740e-15
#>
#> $head
#> [1] 5
#>
#> $pitch
#> [1] 90
#>
#> $roll
#> [1] 0
#>
#> $level
#> [1] 1
rglwidget()