Skip to content

Commit

Permalink
add unit test for VTK mesh reading and writing, strip names when reading
Browse files Browse the repository at this point in the history
  • Loading branch information
dfsp-spirit committed Apr 9, 2020
1 parent 9daf5c9 commit a68df6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions R/read_fs_surface.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-write-fs-surface.R
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})

0 comments on commit a68df6b

Please sign in to comment.