File: persp3d.Rd

package info (click to toggle)
rgl 0.80-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,508 kB
  • ctags: 10,065
  • sloc: ansic: 27,581; cpp: 14,088; sh: 3,062; makefile: 103
file content (137 lines) | stat: -rw-r--r-- 5,096 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
\name{persp3d}
\alias{persp3d}
\alias{persp3d.default}
\title{ Surface plots }
\description{
  This function draws plots of surfaces over the
  x-y plane. \code{persp3d} is a generic function.}
\usage{
persp3d(x, \dots)

\method{persp3d}{default}(x = seq(0, 1, len = nrow(z)), y = seq(0, 1, len = ncol(z)), z,
    xlim = range(x, na.rm = TRUE), ylim = range(y, na.rm = TRUE), zlim = range(z, na.rm = TRUE),
    xlab = NULL, ylab = NULL, zlab = NULL, add = FALSE, aspect = !add, ...)
}
\arguments{
  \item{x, y}{locations of grid lines at which the values in \code{z} are
    measured.  These may be given as vectors or matrices.
    If vectors, they must be in ascending order.   Either one or both may matrices.
    If \code{x} is a \code{list},
    its components \code{x$x} and \code{x$y} are used for \code{x}
    and \code{y}, respectively.}
  \item{z}{a matrix containing the values to be plotted.  
    Note that \code{x} can be used instead of \code{z} for
    convenience.}
  \item{xlim, ylim, zlim}{x-, y-  and z-limits.  The plot is produced
    so that the rectangular volume defined by these limits is visible.}
  \item{xlab, ylab, zlab}{titles for the axes.  N.B. These must be
    character strings; expressions are not accepted.  Numbers will be
    coerced to character strings.}
  \item{add}{whether to add the points to an existing plot.}    
  \item{aspect}{either a logical indicating whether to adjust the aspect ratio, or a new ratio}
    
  \item{\dots}{additional material parameters to be passed to \code{\link{surface3d}}
  and \code{\link{decorate3d}}.}
}
\details{
  This is similar to \code{\link[graphics]{persp}} with user interaction.  See \code{\link{plot3d}}
  for more general details.
  
  One difference from \code{\link[graphics]{persp}} is that colors are specified on each
  vertex, rather than on each facet of the surface.  To emulate the \code{\link[graphics]{persp}}
  color handling, you need to do the following.  First, convert the color vector to 
  an \code{(nx-1)} by \code{(ny-1)} matrix; then add an extra row before row 1, 
  and an extra column after the last column, to convert it to \code{nx} by \code{ny}.
  (These extra colors will not be used).  For example, 
  \code{col <- rbind(0, cbind(matrix(col, nx-1, ny-1), 0))}.
  Finally, call \code{persp3d} with material property \code{smooth = FALSE}.
  
  If the \code{x} or \code{y} argument is a matrix, then it must be of the same
  dimension as \code{z}, and the values in the matrix will be used for the corresponding
  coordinates. This is used to plot shapes such as spheres or cylinders
  where z is not a function of x and y.  See the fourth and fifth examples below.
}
\value{
  This function is called for the side effect of drawing the plot.  A vector 
  of shape IDs is returned.
}
\author{Duncan Murdoch}
\seealso{\code{\link{plot3d}}, \code{\link{persp}}. The \code{\link[emdbook]{curve3d}}
function in the \code{emdbook} package draws surface plots of functions.}
\examples{

# (1) The Obligatory Mathematical surface.
#     Rotated sinc function.

x <- seq(-10, 10, length= 30)
y <- x
f <- function(x,y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }
z <- outer(x, y, f)
z[is.na(z)] <- 1
open3d()
bg3d("white")
material3d(col="black")
persp3d(x, y, z, aspect=c(1, 1, 0.5), col = "lightblue",
        xlab = "X", ylab = "Y", zlab = "Sinc( r )")

# (2) Add to existing persp plot:

xE <- c(-10,10); xy <- expand.grid(xE, xE)
points3d(xy[,1], xy[,2], 6, col = "red", size = 3)
lines3d(x, y=10, z= 6 + sin(x), col = "green")

phi <- seq(0, 2*pi, len = 201)
r1 <- 7.725 # radius of 2nd maximum
xr <- r1 * cos(phi)
yr <- r1 * sin(phi)
lines3d(xr,yr, f(xr,yr), col = "pink", size = 2)

# (3) Visualizing a simple DEM model

z <- 2 * volcano        # Exaggerate the relief
x <- 10 * (1:nrow(z))   # 10 meter spacing (S to N)
y <- 10 * (1:ncol(z))   # 10 meter spacing (E to W)

open3d()
bg3d("slategray")
material3d(col="black")
persp3d(x, y, z, col = "green3", aspect="iso",
      axes = FALSE, box = FALSE)

# (4) A cylindrical plot

z <- matrix(seq(0, 1, len=50), 50, 50)
theta <- t(z)
r <- 1 + exp( -pmin( (z - theta)^2, (z - theta - 1)^2, (z - theta + 1)^2 )/0.01 )
x <- r*cos(theta*2*pi)
y <- r*sin(theta*2*pi)

open3d()
persp3d(x, y, z, col="red")

# (5) A globe

lat <- matrix(seq(90,-90, len=50)*pi/180, 50, 50, byrow=TRUE)
long <- matrix(seq(-180, 180, len=50)*pi/180, 50, 50)

r <- 6378.1 # radius of Earth in km
x <- r*cos(lat)*cos(long)
y <- r*cos(lat)*sin(long)
z <- r*sin(lat)

open3d()
persp3d(x, y, z, col="white", 
       texture=system.file("textures/worldsmall.png",package="rgl"), 
       specular="black", axes=FALSE, box=FALSE, xlab="", ylab="", zlab="",
       normal_x=x, normal_y=y, normal_z=z)
play3d(spin3d(axis=c(0,0,1), rpm=8), duration=5)

\dontrun{
# This looks much better, but is slow because the texture is very big
persp3d(x, y, z, col="white", 
       texture=system.file("textures/world.png",package="rgl"), 
       specular="black", axes=FALSE, box=FALSE, xlab="", ylab="", zlab="",
       normal_x=x, normal_y=y, normal_z=z)
}
}
\keyword{ dynamic }