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:> In S-Plus, persp() returns a value that can be used to transform 3D > locations to 2D, but this sort of thing is not (yet) available in R.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! -- -- David Brahm (brahm at alum.mit.edu) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi David Brahm wrote:> > 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: > > In S-Plus, persp() returns a value that can be used to transform 3D > > locations to 2D, but this sort of thing is not (yet) available in R.Sorry, that is out of date.> 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?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:> 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: > > In S-Plus, persp() returns a value that can be used to transform 3D > > locations to 2D, but this sort of thing is not (yet) available in R. > > 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! >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
David Brahm <brahm at alum.mit.edu> writes:> 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: > > In S-Plus, persp() returns a value that can be used to transform 3D > > locations to 2D, but this sort of thing is not (yet) available in R. > > 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?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)'.> Paul also suggested Uwe Ligges's "scatterplot3d" package, which is very nice > but seems better at producing points than grids.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). -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thanks very much to Paul Murrell, Ben Bolker, and Peter Dalgaard for responding. Sorry I failed to find the 3/15/02 discussion of this same topic. May I suggest that the omission of the returned value from the help page for persp() is a "documentation bug"? Something like this could be added: Value: A 4x4 matrix (call it "pmat"), suitable for transforming 3D coordinates (x,y,z) into list(x,y) values. These can then be used by lines(), points(), etc, to superimpose additional data on the 3D plot. The transformation can be accomplished with the following function: 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,]) } -- -- David Brahm (brahm at alum.mit.edu) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._