From a68df6bb302f2cfae45cd87ff48e375f32477d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Sch=C3=A4fer?= Date: Thu, 9 Apr 2020 19:42:02 +0200 Subject: [PATCH] add unit test for VTK mesh reading and writing, strip names when reading --- R/read_fs_surface.R | 8 ++++---- tests/testthat/test-write-fs-surface.R | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/R/read_fs_surface.R b/R/read_fs_surface.R index a47880f..e498c3d 100644 --- a/R/read_fs_surface.R +++ b/R/read_fs_surface.R @@ -19,8 +19,8 @@ read.fs.surface.asc <- function(filepath) { faces_df = read.table(filepath, skip=2L + num_verts, col.names = c('vertex1', 'vertex2', 'vertex3', 'value'), colClasses = c("integer", "integer", "integer", "numeric"), nrows=num_faces); ret_list = list(); - ret_list$vertices = data.matrix(vertices_df[1:3]); - ret_list$faces = data.matrix(faces_df[1:3]) + 1L; # the +1 is because the surface should use R indices (one-based) + ret_list$vertices = unname(data.matrix(vertices_df[1:3])); + ret_list$faces = unname(data.matrix(faces_df[1:3])) + 1L; # the +1 is because the surface should use R indices (one-based) class(ret_list) = c("fs.surface", class(ret_list)); if(nrow(ret_list$vertices) != num_verts) { @@ -92,8 +92,8 @@ read.fs.surface.vtk <- function(filepath) { ret_list = list(); - ret_list$vertices = data.matrix(vertices_df[1:3]); - ret_list$faces = data.matrix(faces_df[2:4]) + 1L; # the +1 is because the surface should use R indices (one-based) + ret_list$vertices = unname(data.matrix(vertices_df[1:3])); + ret_list$faces = unname(data.matrix(faces_df[2:4])) + 1L; # the +1 is because the surface should use R indices (one-based) class(ret_list) = c("fs.surface", class(ret_list)); return(ret_list); diff --git a/tests/testthat/test-write-fs-surface.R b/tests/testthat/test-write-fs-surface.R index 1c3cfd4..6a748f6 100644 --- a/tests/testthat/test-write-fs-surface.R +++ b/tests/testthat/test-write-fs-surface.R @@ -74,3 +74,18 @@ test_that("One can read, write and re-read triangular surface data", { expect_equal(surf$vertices, surf_re$vertices); expect_equal(surf$faces, surf_re$faces); }) + + +test_that("Surface files in VTK format can be read and written", { + + surface_file = system.file("extdata", "lh.tinysurface", package = "freesurferformats", mustWork = TRUE); + surf = read.fs.surface(surface_file); + + tmp_vtk_file = tempfile(fileext=".vtk"); + write.fs.surface.vtk(tmp_vtk_file, surf$vertices, surf$faces); + + surf_re = read.fs.surface(tmp_vtk_file); + expect_equal(surf$vertices, surf_re$vertices); + expect_equal(surf$faces, surf_re$faces); +}) +