van Rijn, Peter
2011-May-12 20:47 UTC
[R] Exporting interactive 3D plots with axes and labels
Hi, I have a question about exporting interactive 3D plots. I use the following code to plot a contour of a trivariate normal distribution: library(mvtnorm) library(rgl) library(misc3d) n=25 x=seq(-3,3,length=n) X=cbind(rep(x,each=n**2),rep(rep(x,each=n),n),rep(x,n**2)) p=array(dmvnorm(X,sigma=diag(3)*0.5+0.5),c(n,n,n)) contour3d(p,x,x,x,level=mean(p)) lim=c(-3,3) decorate3d(xlim=lim,ylim=lim,zlim=lim,xlab="Variable X",ylab="Variable Y",zlab="Variable Z") 1. I know thanks to Luke Tierney (www.stat.uiowa.edu/~luke/R/misc3d/misc3d-pdf/misc3d-pdf.pdf) how to export the 3D plot without the axes and labels with: dummy=function(...){ contour3d(p,x,x,x,level=mean(p),scale = FALSE,...) } conts=dummy() saveTrianglesAsOFF <- function(scene, filename = "scene.OFF") { scene <- misc3d:::colorScene(scene) triangles <- misc3d:::canonicalizeAndMergeScene(scene, "color", "color2", "alpha", "col.mesh", "fill", "smooth") ve <- misc3d:::t2ve(triangles) f <- file(filename, open = "w") on.exit(close(f)) write("OFF", f) write(c(ncol(ve$vb), ncol(ve$ib), 3 * ncol(ve$ib)), f, 3) write(ve$vb, f, 3) write(rbind(3, ve$ib - 1), f, 4) invisible(NULL) } saveTrianglesAsOFF(conts,"plot.OFF") However, I would like to have the axes and labels in the export as well. Simply adding the decorate3d to the dummy function does not work, nor does creating a separate dummy. (I do know how to combine multiple exports in Meshlab). Does anyone know how to do this in some way or the other, or is this simply not possible? Thanks, Peter van Rijn [[alternative HTML version deleted]]
Remko Duursma
2011-May-12 23:17 UTC
[R] Exporting interactive 3D plots with axes and labels
Does ?snapshot3d not do what you want? Or what do you mean by 'export' ? remko ------------------------------------------------- Remko Duursma Research Lecturer Hawkesbury Institute for the Environment University of Western Sydney Hawkesbury Campus, Richmond Mobile: +61 (0)422 096908 www.remkoduursma.com On Fri, May 13, 2011 at 6:47 AM, van Rijn, Peter <pvanrijn at ets.org> wrote:> Hi, > > I have a question about exporting interactive 3D plots. I use the following code to plot a contour of a trivariate normal distribution: > > library(mvtnorm) > library(rgl) > library(misc3d) > > n=25 > x=seq(-3,3,length=n) > X=cbind(rep(x,each=n**2),rep(rep(x,each=n),n),rep(x,n**2)) > p=array(dmvnorm(X,sigma=diag(3)*0.5+0.5),c(n,n,n)) > > contour3d(p,x,x,x,level=mean(p)) > lim=c(-3,3) > decorate3d(xlim=lim,ylim=lim,zlim=lim,xlab="Variable X",ylab="Variable Y",zlab="Variable Z") > > 1. ? ? ?I know thanks to Luke Tierney (www.stat.uiowa.edu/~luke/R/misc3d/misc3d-pdf/misc3d-pdf.pdf) how to export the 3D plot without the axes and labels with: > > dummy=function(...){ > ? ? ? ? ? ?contour3d(p,x,x,x,level=mean(p),scale = FALSE,...) > } > > conts=dummy() > > saveTrianglesAsOFF <- function(scene, filename = "scene.OFF") { > ? ? ? ? ? ?scene <- misc3d:::colorScene(scene) > ? ? ? ? ? ?triangles <- misc3d:::canonicalizeAndMergeScene(scene, "color", "color2", > ? ? ? ? ? ?"alpha", "col.mesh", > ? ? ? ? ? ?"fill", "smooth") > ? ? ? ? ? ?ve <- misc3d:::t2ve(triangles) > ? ? ? ? ? ?f <- file(filename, open = "w") > ? ? ? ? ? ?on.exit(close(f)) > ? ? ? ? ? ?write("OFF", f) > ? ? ? ? ? ?write(c(ncol(ve$vb), ncol(ve$ib), 3 * ncol(ve$ib)), f, 3) > ? ? ? ? ? ?write(ve$vb, f, 3) > ? ? ? ? ? ?write(rbind(3, ve$ib - 1), f, 4) > ? ? ? ? ? ?invisible(NULL) > } > > saveTrianglesAsOFF(conts,"plot.OFF") > > However, I would like to have the axes and labels in the export as well. Simply adding the decorate3d to the dummy function does not work, > nor does creating a separate dummy. (I do know how to combine multiple exports in Meshlab). > > Does anyone know how to do this in some way or the other, or is this simply not possible? > > Thanks, > > Peter van Rijn > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Duncan Murdoch
2011-May-13 00:21 UTC
[R] Exporting interactive 3D plots with axes and labels
On 12/05/2011 4:47 PM, van Rijn, Peter wrote:> Hi, > > I have a question about exporting interactive 3D plots. I use the following code to plot a contour of a trivariate normal distribution: > > library(mvtnorm) > library(rgl) > library(misc3d) > > n=25 > x=seq(-3,3,length=n) > X=cbind(rep(x,each=n**2),rep(rep(x,each=n),n),rep(x,n**2)) > p=array(dmvnorm(X,sigma=diag(3)*0.5+0.5),c(n,n,n)) > > contour3d(p,x,x,x,level=mean(p)) > lim=c(-3,3) > decorate3d(xlim=lim,ylim=lim,zlim=lim,xlab="Variable X",ylab="Variable Y",zlab="Variable Z") > > 1. I know thanks to Luke Tierney (www.stat.uiowa.edu/~luke/R/misc3d/misc3d-pdf/misc3d-pdf.pdf) how to export the 3D plot without the axes and labels with: > > dummy=function(...){ > contour3d(p,x,x,x,level=mean(p),scale = FALSE,...) > } > > conts=dummy() > > saveTrianglesAsOFF<- function(scene, filename = "scene.OFF") { > scene<- misc3d:::colorScene(scene) > triangles<- misc3d:::canonicalizeAndMergeScene(scene, "color", "color2", > "alpha", "col.mesh", > "fill", "smooth") > ve<- misc3d:::t2ve(triangles) > f<- file(filename, open = "w") > on.exit(close(f)) > write("OFF", f) > write(c(ncol(ve$vb), ncol(ve$ib), 3 * ncol(ve$ib)), f, 3) > write(ve$vb, f, 3) > write(rbind(3, ve$ib - 1), f, 4) > invisible(NULL) > } > > saveTrianglesAsOFF(conts,"plot.OFF") > > However, I would like to have the axes and labels in the export as well. Simply adding the decorate3d to the dummy function does not work, > nor does creating a separate dummy. (I do know how to combine multiple exports in Meshlab). > > Does anyone know how to do this in some way or the other, or is this simply not possible?It's not currently possible, but it's on my wish list. What you're doing is grabbing the triangles before they get sent to rgl. I suppose you could try to do the same for all the axes and labels, but a better solution (which is what is on the wish list) is to be able to query an rgl scene and get back complete information (or at least geometric information) on every object in it. Then you wouldn't care how the triangles were produced, you'd just have what you need to draw them again. This isn't theoretically difficult, but it's a lot of tedious programming, and I just haven't needed it enough to put my nose to the grindstone and do it. Duncan Murdoch