17#ifndef __dwi_tractography_file_h__
18#define __dwi_tractography_file_h__
37 namespace Tractography
41 template <
class ValueType>
50 template <
class ValueType>
61 template <
class ValueType =
float>
69 open (file,
"tracks", properties);
72 weights = load_vector<ValueType> (opt[0][0]);
85 if (std::isinf (p[0])) {
96 if (std::isnan (p[0])) {
104 WARN (
"Streamline weights file contains less entries (" +
str(
weights.size()) +
") than .tck file; "
105 "ceasing reading of streamline data");
128 using __ReaderBase__::in;
129 using __ReaderBase__::dtype;
130 using __ReaderBase__::current_index;
132 Eigen::Matrix<ValueType, Eigen::Dynamic, 1>
weights;
138 using namespace ByteOrder;
143 in.read ((
char*) p,
sizeof (p));
144 return { ValueType(
LE(p[0])), ValueType(
LE(p[1])), ValueType(
LE(p[2])) };
149 in.read ((
char*) p,
sizeof (p));
150 return { ValueType(
BE(p[0])), ValueType(
BE(p[1])), ValueType(
BE(p[2])) };
155 in.read ((
char*) p,
sizeof (p));
156 return { ValueType(
LE(p[0])), ValueType(
LE(p[1])), ValueType(
LE(p[2])) };
161 in.read ((
char*) p,
sizeof (p));
162 return { ValueType(
BE(p[0])), ValueType(
BE(p[1])), ValueType(
BE(p[2])) };
176 if (
size_t(
weights.size()) > current_index) {
177 WARN (
"Streamline weights file contains more entries (" +
str(
weights.size()) +
") than .tck file (" +
str(current_index) +
")");
203 template <
class ValueType =
float>
207 using __WriterBase__<ValueType>::count;
208 using __WriterBase__<ValueType>::total_count;
210 using __WriterBase__<ValueType>::dtype;
212 using __WriterBase__<ValueType>::verify_stream;
213 using __WriterBase__<ValueType>::update_counts;
214 using __WriterBase__<ValueType>::open_success;
220 __WriterBase__<ValueType> (file) {
223 throw Exception (
"output track files must use the .tck suffix");
227 out.
open (
name, std::ios::out | std::ios::binary | std::ios::trunc);
229 throw Exception (
e,
"Unable to create output track file");
232 const_cast<Properties&
> (properties).set_timestamp();
233 const_cast<Properties&
> (properties).set_version_info();
234 const_cast<Properties&
> (properties).update_command_history();
236 create (out, properties,
"tracks");
241 out.write (
reinterpret_cast<char*
> (&x[0]),
sizeof (x));
243 throw Exception (
"error writing tracks file \"" +
name +
"\": " + strerror (errno));
255 for (
size_t n = 0; n < tck.size(); ++n) {
256 assert (tck[n].allFinite());
261 commit (buffer, tck.size()+1);
275 throw Exception (
"Cannot change output streamline weights file path");
292 using namespace ByteOrder;
293 if (dtype.is_little_endian())
294 dest = {
LE(src[0]),
LE(src[1]),
LE(src[2]) };
296 dest = {
BE(src[0]),
BE(src[1]),
BE(src[2]) };
304 throw Exception (
"error writing streamline weights file \"" +
weights_name +
"\": " + strerror (errno));
312 if (num_points == 0 || !open_success)
318 File::OFStream out (
name, std::ios::in | std::ios::out | std::ios::binary | std::ios::ate);
319 out.write (
reinterpret_cast<const char* const
> (data+1),
sizeof (
vector_type) * num_points);
322 out.seekp (prev_barrier_addr, out.beg);
323 out.write (
reinterpret_cast<const char* const
> (data),
sizeof(
vector_type));
351 template <
typename ValueType =
float>
355 using __WriterBase__<ValueType>::count;
356 using __WriterBase__<ValueType>::total_count;
374 Writer (
const std::string& file,
const Properties& properties,
size_t default_buffer_capacity = 16777216) :
392 for (
const auto& i : tck) {
393 assert (i.allFinite());
void set_index(const size_t i)
A class to read streamlines data.
bool operator()(Streamline< ValueType > &tck)
fetch next track from file
Eigen::Matrix< ValueType, 3, 1 > get_next_point()
takes care of byte ordering issues
void check_excess_weights()
Check that the weights file does not contain excess entries.
Eigen::Matrix< ValueType, Eigen::Dynamic, 1 > weights
Reader(const std::string &file, Properties &properties)
open the file for reading and load header into properties
Reader(const Reader &)=delete
virtual ~ReaderInterface()
virtual bool operator()(Streamline< ValueType > &)=0
class to handle writing tracks to file, with RAM buffer
Writer(const Writer &W)=delete
~Writer()
commits any remaining data to file
typename WriterUnbuffered< ValueType >::vector_type vector_type
Writer(const std::string &file, const Properties &properties, size_t default_buffer_capacity=16777216)
create new RAM-buffered track file with specified properties
const size_t buffer_capacity
std::string weights_buffer
bool operator()(const Streamline< ValueType > &tck)
append track to file
std::unique_ptr< vector_type[]> buffer
void add_point(const vector_type &p)
add point to buffer and increment buffer_size accordingly
virtual ~WriterInterface()
virtual bool operator()(const Streamline< ValueType > &)=0
class to handle unbuffered writing of tracks to file
WriterUnbuffered(const std::string &file, const Properties &properties)
create a new track file with the specified properties
void format_point(const vector_type &src, vector_type &dest)
perform per-point byte-swapping if required
void set_weights_path(const std::string &path)
set the path to the track weights
void commit(vector_type *data, size_t num_points)
write track point data to file
WriterUnbuffered(const WriterUnbuffered &)=delete
copy construction explicitly disabled
vector_type delimiter() const
indicates end of track and start of new track
Eigen::Matrix< ValueType, 3, 1 > vector_type
vector_type barrier() const
indicates end of data
void write_weights(const std::string &contents)
write track weights data to file
bool operator()(const Streamline< ValueType > &tck)
append track to file
static constexpr uint8_t Float64LE
static constexpr uint8_t Float32BE
static constexpr uint8_t Float32LE
static constexpr uint8_t Float64BE
open output files for writing, checking for pre-existing file if necessary
void open(const std::string &path, const std::ios_base::openmode mode=std::ios_base::out|std::ios_base::binary)
#define NON_POD_VLA(name, type, num)
const vector< ParsedOption > get_options(const std::string &name)
return all command-line options matching name
void check_overwrite(const std::string &name)
ValueType BE(ValueType v)
ValueType LE(ValueType v)
std::unique_ptr< ImageIO::Base > create(Header &H)
bool has_suffix(const std::string &name, const std::string &suffix)
std::string str(const T &value, int precision=0)
constexpr default_type Inf
constexpr default_type NaN