ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkTileConfiguration.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19#ifndef itkTileConfiguration_h
20#define itkTileConfiguration_h
21
22#include <string>
23#include <vector>
24
25#include "itkPoint.h"
26#include "itkSize.h"
27
28#include "double-conversion/double-conversion.h"
29
30#include <cassert>
31#include <fstream>
32#include <limits>
33#include <sstream>
34
35namespace itk
36{
37template <unsigned Dimension>
38struct ITK_TEMPLATE_EXPORT Tile
39{
41
42 PointType Position; // x, y... coordinates
43
44 std::string FileName;
45
46 /* Primarily for access via SWIG wrapping */
49 {
50 return Position;
51 }
52 void
54 {
55 Position = position;
56 }
57
58 /* Primarily for access via SWIG wrapping */
59 std::string
61 {
62 return FileName;
63 }
64 void
65 SetFileName(std::string fileName)
66 {
67 FileName = fileName;
68 }
69};
70
71
72template <unsigned Dimension>
73struct ITK_TEMPLATE_EXPORT TileConfiguration
74{
78
80
81 std::vector<TileND> Tiles;
82
83 /* Primarily for access via SWIG wrapping */
86 {
87 return AxisSizes;
88 }
89 void
91 {
92 AxisSizes = axisSizes;
93 }
94
95 /* Primarily for access via SWIG wrapping */
96 TileND
97 GetTile(size_t linearIndex) const
98 {
99 return Tiles[linearIndex];
100 }
101 void
102 SetTile(size_t linearIndex, TileND tile)
103 {
104 Tiles[linearIndex] = tile;
105 }
106
107 size_t
109 {
110 size_t linearSize = 1u;
111 for (unsigned d = 0; d < Dimension; d++)
112 {
113 linearSize *= AxisSizes[d];
114 }
115 return linearSize;
116 }
117
118 size_t
120 {
121 size_t ind = 0;
122 SizeValueType stride = 1u;
123 for (unsigned d = 0; d < Dimension; d++)
124 {
125 itkAssertOrThrowMacro(nDIndex[d] < AxisSizes[d],
126 "Tile index " << nDIndex << " exceeds axis size " << AxisSizes << " at dimension " << d);
127 ind += nDIndex[d] * stride;
128 stride *= AxisSizes[d];
129 }
130 return ind;
131 }
132
133 TileIndexType
134 LinearIndexToNDIndex(size_t linearIndex) const
135 {
136 TileIndexType ind;
137 SizeValueType stride = 1u;
138 for (unsigned d = 0; d < Dimension; d++)
139 {
140 stride *= AxisSizes[d];
141 ind[d] = linearIndex % AxisSizes[d];
142 linearIndex /= AxisSizes[d];
143 }
144 itkAssertOrThrowMacro(linearIndex < stride,
145 "Linear tile index " << linearIndex << " exceeds total montage size " << stride);
146 return ind;
147 }
148
149 // tries parsing the file, return first file name and set dimension
150 static std::string
151 TryParse(const std::string & pathToFile, unsigned & dimension)
152 {
153 std::ifstream tileFile(pathToFile);
154 if (!tileFile)
155 {
156 throw std::runtime_error("Could not open for reading: " + pathToFile);
157 }
158
159 std::string temp = getNextNonCommentLine(tileFile);
160 if (temp.substr(0, 6) == "dim = ")
161 {
162 dimension = std::stoul(temp.substr(6));
163 temp = TileConfiguration<Dimension>::getNextNonCommentLine(tileFile); // get next line
164 }
165
166 std::string timePointID;
167 Tile<Dimension> tile = parseLine(temp, timePointID);
168 return tile.FileName;
169 }
170
171 void
172 Parse(const std::string & pathToFile)
173 {
174 std::ifstream tileFile(pathToFile);
175 if (!tileFile)
176 {
177 throw std::runtime_error("Could not open for reading: " + pathToFile);
178 }
179 std::string line = getNextNonCommentLine(tileFile);
180 if (line.substr(0, 6) == "dim = ")
181 {
182 unsigned dim = std::stoul(line.substr(6));
183 if (dim != Dimension)
184 {
185 throw std::runtime_error("Expected dimension " + std::to_string(Dimension) + ", but got " +
186 std::to_string(dim) + " from string:\n\n" + line);
187 }
188 line = TileConfiguration<Dimension>::getNextNonCommentLine(tileFile); // get next line
189 }
190
191 AxisSizes.Fill(1);
192 Tiles.clear();
193 TileIndexType cInd;
194 cInd.Fill(0);
195 unsigned initializedDimensions = 0; // no dimension has been initialized
196
197 std::string timePoint;
198 itk::Tile<Dimension> tile = parseLine(line, timePoint);
199 Tiles.push_back(tile);
200 line = getNextNonCommentLine(tileFile);
201
202 while (tileFile)
203 {
204 tile = parseLine(line, timePoint);
205 // determine dominant axis change
206 unsigned maxAxis = 0; // (0=x, 1=y, 2=z etc)
207 double maxDiff = tile.Position[0] - Tiles.back().Position[0];
208 for (unsigned d = 1; d < Dimension; d++)
209 {
210 double diff = tile.Position[d] - Tiles.back().Position[d];
211 if (diff > maxDiff)
212 {
213 maxDiff = diff;
214 maxAxis = d;
215 }
216 }
217
218 if (maxAxis > initializedDimensions) // we now know the size along this dimension
219 {
220 AxisSizes[maxAxis - 1] = cInd[maxAxis - 1] + 1;
221 initializedDimensions = maxAxis;
222 }
223
224 // check consistency with previously established size
225 for (unsigned d = 0; d < maxAxis; d++)
226 {
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 "
231 << AxisSizes[d] - 1);
232 }
233
234 // update current tile index
235 for (unsigned d = 0; d < maxAxis; d++)
236 {
237 cInd[d] = 0;
238 }
239 ++cInd[maxAxis];
240
241
242 if (maxAxis < initializedDimensions) // check bounds, if bounds are established
243 {
244 itkAssertOrThrowMacro(cInd[maxAxis] < AxisSizes[maxAxis],
245 "Axis sizes: " << AxisSizes << ", but we reached index " << cInd[maxAxis]
246 << ". Violation along axis " << maxAxis);
247 }
248
249 Tiles.push_back(tile);
250 line = getNextNonCommentLine(tileFile);
251 }
252
253 for (unsigned d = 0; d < Dimension; ++d)
254 {
255 AxisSizes[d] = cInd[d] + 1;
256 }
257
258 size_t expectedSize = this->LinearSize();
259 itkAssertOrThrowMacro(expectedSize == Tiles.size(),
260 "Incorrect number of tiles: " << Tiles.size() << ". Expected: " << expectedSize);
261 }
262
263 void
264 Write(const std::string & pathToFile)
265 {
266 std::ofstream tileFile(pathToFile);
267 if (!tileFile)
268 {
269 throw std::runtime_error("Could not open for writing: " + pathToFile);
270 }
271
272 tileFile << "# Tile coordinates are in index space, not physical space\n";
273 tileFile << "dim = " << Dimension << "\n\n";
274 char buffer[25];
275 double_conversion::StringBuilder conversionResult(buffer, 25);
276
277 size_t totalTiles = this->LinearSize();
278 for (SizeValueType linearIndex = 0; linearIndex < totalTiles; linearIndex++)
279 {
280 tileFile << Tiles[linearIndex].FileName << ";;(";
281
282 for (unsigned d = 0; d < Dimension; d++)
283 {
284 if (d > 0)
285 {
286 tileFile << ", ";
287 }
288
289 doubleConverter.ToShortest(Tiles[linearIndex].Position[d], &conversionResult);
290 tileFile << conversionResult.Finalize();
291 conversionResult.Reset();
292 }
293 tileFile << ')' << std::endl;
294 }
295
296 if (!tileFile)
297 {
298 throw std::runtime_error("Writing not successful to: " + pathToFile);
299 }
300 }
301
302 static double_conversion::StringToDoubleConverter stringConverter;
303 static double_conversion::DoubleToStringConverter doubleConverter;
304
305 static std::string
306 getNextNonCommentLine(std::istream & in)
307 {
308 std::string temp;
309 while (std::getline(in, temp))
310 {
311 if (temp.empty() || temp[0] == '#')
312 {
313 continue; // this is either an empty line or a comment
314 }
315 if (temp.size() == 1 && temp[0] == '\r')
316 {
317 continue; // empty line ending in CRLF
318 }
319 if (temp[temp.size() - 1] == '\r')
320 {
321 temp.erase(temp.size() - 1, 1);
322 }
323 break; // temp has interesting content
324 }
325 return temp;
326 }
327
328 static Tile<Dimension>
329 parseLine(const std::string line, std::string & timePointID)
330 {
332 std::stringstream ss(line);
333 std::string temp;
334
335 std::getline(ss, temp, ';');
336 tile.FileName = temp;
337 std::getline(ss, temp, ';');
338 if (timePointID.empty())
339 {
340 timePointID = temp;
341 }
342 else
343 {
344 itkAssertOrThrowMacro(temp == timePointID,
345 "Only a single time point is supported. " << timePointID << " != " << temp);
346 }
347 std::getline(ss, temp, '(');
348
349 using PointTypelocal = itk::Point<double, Dimension>;
350 PointTypelocal p;
351 for (unsigned d = 0; d < Dimension; d++)
352 {
353 std::getline(ss, temp, ',');
354 int processed = 0;
355 p[d] = stringConverter.StringToDouble(temp.c_str(), temp.length(), &processed);
356 }
357 tile.Position = p;
358
359 return tile;
360 }
361};
362
363template <unsigned Dimension>
364double_conversion::StringToDoubleConverter TileConfiguration<Dimension>::stringConverter(
365 double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK |
366 double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES |
367 double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES,
368 0.0,
369 std::numeric_limits<double>::quiet_NaN(),
370 nullptr,
371 nullptr);
372
373template <unsigned Dimension>
374double_conversion::DoubleToStringConverter TileConfiguration<
375 Dimension>::doubleConverter(double_conversion::DoubleToStringConverter::NO_FLAGS, nullptr, nullptr, 'e', 0, 17, 1, 0);
376
377} // namespace itk
378
379#endif // itkTileConfiguration_h
A templated class holding a geometric point in n-Dimensional space.
Definition itkPoint.h:54
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
unsigned long SizeValueType
Definition itkIntTypes.h:82
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition itkSize.h:70
void Fill(SizeValueType value)
Definition itkSize.h:209
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
std::string FileName
PointType GetPosition() const
std::string GetFileName() const