[R] misc3d Contour export to vtk

Duncan Murdoch murdoch.duncan at gmail.com
Wed Mar 20 18:03:19 CET 2013


On 09/03/2013 7:45 PM, John Muschelli wrote:
> So I wanted to export a contour3d object into VTK and it worked well
> enough, except the top of the ball (the contour) was missing.  Example
> below, or gist here:
> https://gist.github.com/muschellij2/5126544

I don't know VTK, but perhaps some other software could convert one of 
rgl's output formats into that format.  rgl can output OBJ, PLY, and STL 
formats, and one of those might suit your needs.  See ?rgl::writeOBJ, etc.

Duncan Murdoch

>
> rm(list=ls())
>
> require(misc3d)
>
> f <- function(x, y, z) x^2+y^2+z^2
>
> x <- seq(-2,2,len=20)
>
> ball <- contour3d(f,4,x,x,x, draw=FALSE)
>
> drawScene.rgl(ball)
>
> ### get the triangles
>
> allids <- rgl.ids()
>
> id <- allids$id[allids$type == "triangles"]
>
>
> filename="test.vtk"
>
>
>   f <- file(filename, open = "w")
>
>    start <- c("# vtk DataFile Version 3.0", "3D Plot data", "ASCII")
>
>    n3 <- rgl.attrib.count(id, "vertices")
>
>    verts <- rgl.attrib(id, "vertices")
>
>    norms <- rgl.attrib(id, "normals")
>
>    cols <- rgl.attrib(id, "colors")
>
>    if (n3 %% 3 != 0) stop("verts not divisible by 3")
>
>    n <- n3/3
>
>    start <- c(start, "DATASET POLYDATA", paste("POINTS ", n3, " float", sep=
> ""))
>
>    cat(start, sep="\n", file=f)
>
>    dat <- matrix(t(verts), ncol=9, byrow=TRUE)
>
> ## write the points
>
>    cat(sprintf("%f %f %f %f %f %f %f %f %f\n", dat[,1], dat[,2], dat[,3],
>
>                dat[,4], dat[,5], dat[,6],
>
>                dat[,7], dat[,8], dat[,9]), file = f, sep="")
>
>    mat <- matrix(0:(n3-1), ncol = 3, byrow=TRUE)
>
>    print(n)
>
> ### connect the triangles
>
>    cat(sprintf("\nPOLYGONS %d %d\n", n, n3), file = f, sep="")
>
>    cat(sprintf("3 %d %d %d\n", mat[,1], mat[, 2], mat[, 3]), file = f, sep=""
> )
>
> close(f)
>
>
> Any help would be appreciated (especially if someone has already done this).
>
> I know I'm using some old legacy formats for vtk, but I wanted to export to
> XTK since I have some sample JS to do some things in there.
>
>
> Thanks
>
> John
>
> 	[[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.



More information about the R-help mailing list