# toy example
curve <- function(t) {
x <- sin(4 * t + sample(300, 1) / 100) + t + sample(seq_along(t)) / length(t) / 10
y <- cos(sin(t) + 5 * t) + sample(seq_along(t)) / length(t) / 10
z <- t * 3
cbind(x, y, z)
}
# 10 lines, each line is represented by nx3 matrix
tracts <- lapply(seq(100, 109), function(n) {
curve(seq_len(n) / 100)
})
# convert to streamline
x <- as_ieegio_streamlines(tracts)
# Display
print(x)
plot(x, col = 1:10)
if(system.file(package = "r3js") != '') {
plot(x, method = "r3js")
}
# Subset the first line (transformed)
coords <- x[[1]]$coords
head(coords)
# Save different formats
tdir <- tempfile()
dir.create(tdir, showWarnings = FALSE, recursive = TRUE)
write_streamlines(x, file.path(tdir, "sample.tck"))
write_streamlines(x, file.path(tdir, "sample.trk"))
write_streamlines(x, file.path(tdir, "sample.trk.gz"))
if (FALSE) {
# Require Python
write_streamlines(x, file.path(tdir, "sample.vtk"))
write_streamlines(x, file.path(tdir, "sample.vtp"))
write_streamlines(x, file.path(tdir, "sample.vtpb"))
}
# Read formats
y <- read_streamlines(file.path(tdir, "sample.trk"))
# Compare x and y
diffs <- mapply(
x = as.vector(x),
y = as.vector(y),
function(x, y) {
range(x$coords - y$coords)
}
)
# Should be floating errors
max(abs(diffs))
unlink(tdir, recursive = TRUE)
Run the code above in your browser using DataLab