19#ifndef itkTileConfiguration_h
20#define itkTileConfiguration_h
28#include "double-conversion/double-conversion.h"
37template <
unsigned Dimension>
38struct ITK_TEMPLATE_EXPORT
Tile
72template <
unsigned Dimension>
99 return Tiles[linearIndex];
104 Tiles[linearIndex] = tile;
110 size_t linearSize = 1u;
111 for (
unsigned d = 0; d < Dimension; d++)
123 for (
unsigned d = 0; d < Dimension; d++)
125 itkAssertOrThrowMacro(nDIndex[d] <
AxisSizes[d],
126 "Tile index " << nDIndex <<
" exceeds axis size " <<
AxisSizes <<
" at dimension " << d);
127 ind += nDIndex[d] * stride;
138 for (
unsigned d = 0; d < Dimension; d++)
144 itkAssertOrThrowMacro(linearIndex < stride,
145 "Linear tile index " << linearIndex <<
" exceeds total montage size " << stride);
151 TryParse(
const std::string & pathToFile,
unsigned & dimension)
153 std::ifstream tileFile(pathToFile);
156 throw std::runtime_error(
"Could not open for reading: " + pathToFile);
160 if (temp.substr(0, 6) ==
"dim = ")
162 dimension = std::stoul(temp.substr(6));
166 std::string timePointID;
172 Parse(
const std::string & pathToFile)
174 std::ifstream tileFile(pathToFile);
177 throw std::runtime_error(
"Could not open for reading: " + pathToFile);
180 if (line.substr(0, 6) ==
"dim = ")
182 unsigned dim = std::stoul(line.substr(6));
183 if (dim != Dimension)
185 throw std::runtime_error(
"Expected dimension " + std::to_string(Dimension) +
", but got " +
186 std::to_string(dim) +
" from string:\n\n" + line);
195 unsigned initializedDimensions = 0;
197 std::string timePoint;
199 Tiles.push_back(tile);
206 unsigned maxAxis = 0;
207 double maxDiff = tile.
Position[0] -
Tiles.back().Position[0];
208 for (
unsigned d = 1; d < Dimension; d++)
218 if (maxAxis > initializedDimensions)
220 AxisSizes[maxAxis - 1] = cInd[maxAxis - 1] + 1;
221 initializedDimensions = maxAxis;
225 for (
unsigned d = 0; d < maxAxis; d++)
227 itkAssertOrThrowMacro(cInd[d] ==
AxisSizes[d] - 1,
228 "Axis sizes: " <<
AxisSizes <<
" current index: " << cInd
229 <<
". We have reached the end along axis " << maxAxis
230 <<
"\nIndex along axis " << d <<
" is " << cInd[d] <<
", but it should be "
235 for (
unsigned d = 0; d < maxAxis; d++)
242 if (maxAxis < initializedDimensions)
244 itkAssertOrThrowMacro(cInd[maxAxis] <
AxisSizes[maxAxis],
245 "Axis sizes: " <<
AxisSizes <<
", but we reached index " << cInd[maxAxis]
246 <<
". Violation along axis " << maxAxis);
249 Tiles.push_back(tile);
253 for (
unsigned d = 0; d < Dimension; ++d)
259 itkAssertOrThrowMacro(expectedSize ==
Tiles.size(),
260 "Incorrect number of tiles: " <<
Tiles.size() <<
". Expected: " << expectedSize);
264 Write(
const std::string & pathToFile)
266 std::ofstream tileFile(pathToFile);
269 throw std::runtime_error(
"Could not open for writing: " + pathToFile);
272 tileFile <<
"# Tile coordinates are in index space, not physical space\n";
273 tileFile <<
"dim = " << Dimension <<
"\n\n";
275 double_conversion::StringBuilder conversionResult(buffer, 25);
278 for (
SizeValueType linearIndex = 0; linearIndex < totalTiles; linearIndex++)
280 tileFile <<
Tiles[linearIndex].FileName <<
";;(";
282 for (
unsigned d = 0; d < Dimension; d++)
290 tileFile << conversionResult.Finalize();
291 conversionResult.Reset();
293 tileFile <<
')' << std::endl;
298 throw std::runtime_error(
"Writing not successful to: " + pathToFile);
309 while (std::getline(in, temp))
311 if (temp.empty() || temp[0] ==
'#')
315 if (temp.size() == 1 && temp[0] ==
'\r')
319 if (temp[temp.size() - 1] ==
'\r')
321 temp.erase(temp.size() - 1, 1);
329 parseLine(
const std::string line, std::string & timePointID)
332 std::stringstream ss(line);
335 std::getline(ss, temp,
';');
337 std::getline(ss, temp,
';');
338 if (timePointID.empty())
344 itkAssertOrThrowMacro(temp == timePointID,
345 "Only a single time point is supported. " << timePointID <<
" != " << temp);
347 std::getline(ss, temp,
'(');
351 for (
unsigned d = 0; d < Dimension; d++)
353 std::getline(ss, temp,
',');
355 p[d] =
stringConverter.StringToDouble(temp.c_str(), temp.length(), &processed);
363template <
unsigned Dimension>
365 double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK |
366 double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES |
367 double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES,
369 std::numeric_limits<double>::quiet_NaN(),
373template <
unsigned Dimension>
374double_conversion::DoubleToStringConverter TileConfiguration<
375 Dimension>::doubleConverter(double_conversion::DoubleToStringConverter::NO_FLAGS,
nullptr,
nullptr,
'e', 0, 17, 1, 0);
A templated class holding a geometric point in n-Dimensional space.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Represent a n-dimensional size (bounds) of a n-dimensional image.
void Fill(SizeValueType value)
static std::string getNextNonCommentLine(std::istream &in)
void Parse(const std::string &pathToFile)
void SetAxisSizes(TileIndexType axisSizes)
static std::string TryParse(const std::string &pathToFile, unsigned &dimension)
TileND GetTile(vcl_size_t linearIndex) const
vcl_size_t LinearSize() const
TileIndexType LinearIndexToNDIndex(vcl_size_t linearIndex) const
static Tile< Dimension > parseLine(const std::string line, std::string &timePointID)
void SetTile(vcl_size_t linearIndex, TileND tile)
Size< Dimension > TileIndexType
void Write(const std::string &pathToFile)
TileIndexType GetAxisSizes() const
typename Tile< Dimension >::PointType PointType
std::vector< TileND > Tiles
static double_conversion::StringToDoubleConverter stringConverter
vcl_size_t nDIndexToLinearIndex(TileIndexType nDIndex) const
static double_conversion::DoubleToStringConverter doubleConverter
void SetPosition(PointType position)
void SetFileName(std::string fileName)
Point< double, Dimension > PointType
PointType GetPosition() const
std::string GetFileName() const