# [R] What does persp() return?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Thu Aug 1 23:44:34 CEST 2002

```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

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```