Hello,

I've written a short code (below) to write 3D unstructured grid to binary
VTK files from R. Problem : I can only write integers with the command :

data<-as.numeric(c(3.3))
storage.mode(data)<-'integer'
writeBin(data,bfile_celldata,endian="swap")

the function storage.mode(data)<-'long' looks fine, but the VTK file is not
readable.

thanks for any help or comments,

#----- R SCRIPT TO WRITE A CUBE IN VTK FORMAT

cat('# vtk DataFile Version 3.0\n',file="vtk_header")
cat('R Binary Export v3.0 of inversion model\nBINARY\n',file="vtk_header",
append=TRUE)
cat('DATASET UNSTRUCTURED_GRID\n',file="vtk_header", append=TRUE)
#placed here instead of top of vtk_points, since npoints is not known before
cat('POINTS', 8,'int\n',file="vtk_header",append=TRUE)

#write points
writeBin(as.integer(c(0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1,
0, 1, 1, 1, 1, 1)),bfile_points,endian="swap")

#cells header
cat('\nCELLS', 1,9,'\n',file="cells_header")
#write cells
writeBin(as.integer(c(8, 0, 1, 3, 2, 4, 5, 7, 6)),bfile_cells,endian="swap")

#cell types header
cat('\nCELL_TYPES', 1,'\n',file="celltypes_header")
#write cell types
writeBin(as.integer(c(12)),bfile_celltypes,endian="swap")

#cell data header
cat('\nCELL_DATA',1,'\n',file="celldata_header")
cat('SCALARS R float 1','\n',file="celldata_header", append=TRUE)
cat('LOOKUP_TABLE default\n',file="celldata_header", append=TRUE)
#write cell data
data<-as.numeric(c(3.3))
storage.mode(data)<-'integer'
writeBin(data,bfile_celldata,endian="swap")

#close binary connections
close(bfile_points)
close(bfile_cells)
close(bfile_celltypes)
close(bfile_celldata)

#concatenate files to produce VTK
system(paste('cat',"vtk_header","bfile_points","cells_header","bfile_cells","celltypes_header","bfile_celltypes","celldata_header","bfile_celldata",">","testb_unstructured.vtk",sep="
"))

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to