Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/write mesh fixes #514

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion R/neuron-io.R
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,14 @@ is.swc<-function(f, TrustSuffix=TRUE) {
#'
#' # write out "myneuron.am" in Amira hxlineset format
#' write.neuron(Cell07PNs[[1]], format = 'hxlineset', file='myneuron')
#'
#' # write a mesh in Stanford ply
#' write.neuron(MBL.surf, file = 'MBL.surf.ply')
#' # ... or Wavefront obj format
#' write.neuron(MBL.surf, file = 'MBL.surf.obj')
#' # specify the format directly if not evident from file suffix
#' # not recommended though as will probably just cause trouble when reading
#' write.neuron(MBL.surf, file = 'MBL.surf', format='obj')
#' }
write.neuron<-function(n, file=NULL, dir=NULL, format=NULL, ext=NULL,
Force=FALSE, MakeDir=TRUE, metadata=NULL, ...){
Expand Down Expand Up @@ -891,7 +899,14 @@ write.neurons<-function(nl, dir, format=NULL, subdir=NULL,
INDICES=names(nl), files=NULL,
include.data.frame=FALSE,
metadata=FALSE,
Force=FALSE, cl=NULL, ...){
Force=FALSE, cl=NULL, ...) {
if(!inherits(nl, 'neuronlist')) {
# check if this looks like some of the other kinds of object we might
# write by accident
single_object_classes <- c("mesh3d", "neuron", "dotprops", "hxsurf")
if(any(single_object_classes %in% class(nl)))
stop("Please use `write.neuron` to write a single neuron/mesh object!")
}
if(grepl("\\.zip", dir)) {
zip_file=dir
# check if file exists (and we want to overwrite)
Expand Down
10 changes: 9 additions & 1 deletion R/neuron-mesh.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ write.neuron.mesh <- function(x, file, format=c("ply", "obj"), ...) {
if(!inherits(x, 'mesh3d')) {
x=tryCatch(as.mesh3d(x), error=function(e) stop("Unable to convert x to mesh3d object! Only neuron meshes can be written in ",format," format!"))
}
if(format=="ply")
if(format=="ply") {
if(!isTRUE(tools::file_ext(file)=="ply")) {
# ply format must end in .ply
origfile=file
file=tempfile(fileext = '.ply')
on.exit(file.copy(file, origfile, overwrite = T), add = T)
on.exit(unlink(file), add = T)
}
Rvcg::vcgPlyWrite(x, filename=file, writeCol = F, writeNormals = F, ...)
}
else if(format=="obj")
Rvcg::vcgObjWrite(x, filename=file, writeNormals=F, ...)
else stop("Unknown format")
Expand Down
8 changes: 8 additions & 0 deletions man/write.neuron.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions tests/testthat/test-neuron-mesh.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ test_that("read/write works", {
md5.2=tools::md5sum(ff2)
expect_equal(md5.1, md5.2)

# error if we try to write one object with write.neurons
expect_error(write.neurons(bl[[1]], dir=td, format='ply'))

expect_is(bl2 <- read.neurons(td, format='ply'), 'neuronlist')
expect_equal(sbl <- summary(bl), summary(bl2))

expect_is(sbl, 'data.frame')
expect_known_value(sbl, file = 'testdata/summary_bl.rds')

expect_error(write.neurons(Cell07PNs[1:3], format = 'ply'))

write.neuron(MBL.surf, file=file.path(td, "MBL.surf.ply"), format = 'ply')
expect_equal(read.neuron(file.path(td, "MBL.surf.ply")), as.mesh3d(MBL.surf), tolerance = 1e-6)
})

skip_if_not_installed('readobj')
Expand Down
Loading