Skip to content

What does persp() return?

4 messages · David Brahm, Paul Murrell, Ben Bolker +1 more

#
I want to plot some 3D points on top of the grid produced by persp().

On 2/22/01, Paul Murrell <paul at stat.auckland.ac.nz> wrote in R-help:
But persp() does return something (in R-1.5.1): a 4x4 matrix which in the C
code is called the "viewing transformation matrix", VT.  Can VT be used to plot
additional points?  If not, is such a thing in the works?

Paul also suggested Uwe Ligges's "scatterplot3d" package, which is very nice
but seems better at producing points than grids.

Thanks!
#
Hi
David Brahm wrote:
Sorry, that is out of date.
Yes.  Try searching Chen Huashan's R-help archives
(http://www.baidao.net/r/archives/forum_show.cgi) for the subject
"persp(): add second plane"

Paul
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
I seem to recall this coming up before (I'm on a slow link so will not 
check the mailing list archives), but here is a bit of hackery for adding 
points and lines (etc.) to a persp() plot.  Of course, it doesn't do 
hidden line removal ...

## matrix multiply c(3dv,1) by transformation matrix:
## plot v[0]/v[3], v[1]/v[3]

x <- seq(-10, 10, length = 50)
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
par(bg = "white")
trans3d <- function(x,y,z,pmat) {
  tmat <- t((cbind(x,y,z,1)%*% pmat))
  list(x=tmat[1,]/tmat[4,],y=tmat[2,]/tmat[4,])
}

pmat <- persp(x, y, z, theta = 30, phi = 30, expand = 0.5, 
              col = "lightblue", xlab = "X", ylab = "Y", zlab = "Z",
              ticktype="detailed")
m <- 1e-5
points(trans3d(m,m,f(m,m),pmat),pch=16)
z2 <- sapply(1:length(x),function(n)f(x[n],y[n]))
lines(trans3d(x,y,z2,pmat),col="red",lwd=2)
lines(trans3d(c(-10,10,10,-10,-10),
              c(-10,-10,10,10,-10),
              c(2,2,8,8,2),pmat),col="blue")
On Thu, 1 Aug 2002, David Brahm wrote:

            

  
    
#
David Brahm  <brahm at alum.mit.edu> writes:
Yes, that's what it is for. Classic computational graphics stuff:

I used to be much better at it, but the basic idea is that you
representing 3D points in 4D "homogeneous coordinates", (x,y,z,t) with
t != 0 and so that the usual cartesian coordinates (X,Y,Z) = (x,y,z)/t
(obviously, t is not unique). The main trick is that affine
transformations become linear in this system, e.g. a translation is
obtained by multiplying with

 1 0 0 a
 0 1 0 b
 0 0 1 c
 0 0 0 1

Rotations and reflections and scaling are easily accomplished with
suitable matrices as well. This is why "3D accelerated" CPUs and
graphics processors contain highly optimized 4x4 matrix multipliers,
btw.

I don't know exactly how we do the perspective viewing transformations
(you have the source too...), but one way is to rotate and scale the
scene so that the eye is at zero and looking in the Z direction
through a a projection plane at Z=1; then the coordinates of the
projected points are simply (u/w,v/w) where (u,v,w,s)' = T(x,y,z,1)'.
Yes. Somewhere in dreamspace lies the possibility of interfacing to a
"real" 3D rendering system (vtk is an option I look at once in a while).