45 #include <boost/filesystem.hpp>
48 #include <Core/ITKCommon/common.hxx>
49 #include <Core/ITKCommon/the_utils.hxx>
51 #include <Core/Utils/Exception.h>
52 #include <Core/Utils/Log.h>
54 namespace bfs=boost::filesystem;
59 template <
typename TImage = image_t>
65 stos_t(
const bfs::path & fn_s0,
66 const bfs::path & fn_s1,
67 const bfs::path & fn_mask_s0,
68 const bfs::path & fn_mask_s1,
69 const bool & flipped_0,
70 const bool & flipped_1,
73 const unsigned int & shrink_factor,
74 const base_transform_t * t01) : t01_(t01)
79 fn_mask_[0] = fn_mask_s0;
80 fn_mask_[1] = fn_mask_s1;
82 flipped_[0] = flipped_0;
83 flipped_[1] = flipped_1;
85 sp_[0] = s0->GetSpacing();
86 sp_[1] = s1->GetSpacing();
88 sz_[0] = s0->GetLargestPossibleRegion().GetSize();
89 sz_[1] = s1->GetLargestPossibleRegion().GetSize();
91 sp_[0][0] /= shrink_factor;
92 sp_[0][1] /= shrink_factor;
93 sp_[1][0] /= shrink_factor;
94 sp_[1][1] /= shrink_factor;
96 sz_[0][0] *= shrink_factor;
97 sz_[0][1] *= shrink_factor;
98 sz_[1][0] *= shrink_factor;
99 sz_[1][1] *= shrink_factor;
102 stos_t(
const bfs::path & fn_s0,
103 const bfs::path & fn_s1,
104 const bfs::path & fn_mask_s0,
105 const bfs::path & fn_mask_s1,
106 const bool & flipped_0,
107 const bool & flipped_1,
108 const double spacing_x0,
109 const double spacing_y0,
110 const double spacing_x1,
111 const double spacing_y1,
112 const unsigned int size_x0,
113 const unsigned int size_y0,
114 const unsigned int size_x1,
115 const unsigned int size_y1,
116 const base_transform_t * t01) : t01_(t01)
121 fn_mask_[0] = fn_mask_s0;
122 fn_mask_[1] = fn_mask_s1;
124 flipped_[0] = flipped_0;
125 flipped_[1] = flipped_1;
127 sp_[0][0] = spacing_x0;
128 sp_[0][1] = spacing_y0;
129 sp_[1][0] = spacing_x1;
130 sp_[1][1] = spacing_y1;
138 stos_t(
const bfs::path & fn_load,
139 const bfs::path & override_slice0_path =
"",
140 const bfs::path & override_slice1_path =
"")
142 if (! load(fn_load, override_slice0_path, override_slice1_path) )
144 std::ostringstream oss;
145 oss <<
"Loading stos file " << fn_load <<
" (with slice path overrides "
146 << override_slice0_path <<
" and " << override_slice1_path <<
") failed.";
147 CORE_THROW_EXCEPTION(oss.str());
152 bool load(
const bfs::path & fn_load,
153 const bfs::path & slice0_path =
"",
154 const bfs::path & slice1_path =
"",
155 const bool blab =
false)
158 the::open_utf8(si, fn_load.string().c_str(), std::ios::in);
161 std::ostringstream oss;
162 oss <<
"Could not open " << fn_load <<
" for reading, skipping....";
163 CORE_LOG_WARNING(oss.str());
168 if (blab) std::cout <<
"stos_t loading " << fn_load_.string() << std::endl;
170 bool ok = load(si, slice0_path, slice1_path);
176 bool load(std::istream & si,
177 const bfs::path & override_slice0_path =
"",
178 const bfs::path & override_slice1_path =
"")
181 std::getline(si, f[0]);
182 std::getline(si, f[1]);
185 bfs::path corrected_path[2];
186 corrected_path[0] = override_slice0_path;
187 corrected_path[1] = override_slice1_path;
190 for (
int i = 0; i < 2; i++)
192 if (! corrected_path[i].empty() )
194 bfs::path mosaicPath(f[i]);
195 bfs::path p = corrected_path[i] / mosaicPath.filename();
196 if ( bfs::exists(p) )
202 std::ostringstream oss;
203 oss <<
"Overridden file path " << p <<
" does not exist. Ignoring path override.";
204 CORE_LOG_WARNING(oss.str());
211 if ( bfs::exists(f[i]) )
217 std::ostringstream oss;
218 oss <<
"File path " << f[i] <<
" does not exist.";
219 CORE_LOG_ERROR(oss.str());
227 std::getline(si, line);
228 std::istringstream iss(line);
234 std::getline(si, line);
235 std::istringstream iss(line);
241 std::getline(si, line);
242 std::istringstream iss(line);
243 iss >> sp_[0][0] >> sp_[0][1] >> sz_[0][0] >> sz_[0][1];
248 std::getline(si, line);
249 std::istringstream iss(line);
250 iss >> sp_[1][0] >> sp_[1][1] >> sz_[1][0] >> sz_[1][1];
254 itk::TransformBase::Pointer tmp = load_transform(si);
256 t01_ =
dynamic_cast<base_transform_t *
>(tmp.GetPointer());
257 bool ok = t01_.GetPointer() != NULL;
265 std::string optional;
266 std::getline(si, optional);
267 if (optional ==
"two_user_supplied_masks:")
270 std::getline(si, tmp);
271 if ( bfs::exists(bfs::path(tmp)) )
276 std::getline(si, tmp);
277 if ( bfs::exists(bfs::path(tmp)) )
287 bool save(
const bfs::path & fn_save,
const bool blab =
false)
const
290 the::open_utf8(so, fn_save.string().c_str(), std::ios::out);
293 std::ostringstream oss;
294 oss <<
"Could not open " << fn_save <<
" for writing, skipping....";
295 CORE_LOG_WARNING(oss.str());
298 if (blab) std::cout <<
"saving " << fn_save << std::endl;
309 void save(std::ostream & so)
const
311 std::ios::fmtflags old_flags = so.setf(std::ios::scientific);
312 int old_precision = so.precision();
315 so << fn_[0].string() << std::endl
316 << fn_[1].string() << std::endl
317 << flipped_[0] << std::endl
318 << flipped_[1] << std::endl
321 <<
static_cast<int>(sz_[0][0]) <<
'\t'
322 << static_cast<int>(sz_[0][1]) << std::endl
325 <<
static_cast<int>(sz_[1][0]) <<
'\t'
326 << static_cast<int>(sz_[1][1]) << std::endl;
328 save_transform(so, t01_);
331 if (!fn_mask_[0].empty() && !fn_mask_[1].empty())
333 so <<
"two_user_supplied_masks:" << std::endl
334 << fn_mask_[0].string() << std::endl
335 << fn_mask_[1].string() << std::endl;
339 so.precision(old_precision);
343 inline pnt2d_t tile_min(
unsigned int )
const
344 {
return pnt2d(0, 0); }
346 inline pnt2d_t tile_max(
unsigned int i)
const
348 return pnt2d(sp_[i][0] * static_cast<double>(sz_[i][0]),
349 sp_[i][1] * static_cast<double>(sz_[i][1]));
352 inline pnt2d_t tile_center(
unsigned int i)
const
354 pnt2d_t min = tile_min(i);
355 pnt2d_t max = tile_max(i);
356 return min + (max - min) * 0.5;
360 mutable bfs::path fn_load_;
362 bfs::path image_dirs_[2];
363 bfs::path fn_mask_[2];
365 typename TImage::SpacingType sp_[2];
366 typename TImage::RegionType::SizeType sz_[2];
367 base_transform_t::ConstPointer t01_;
373 template <
typename TImage>
374 inline static std::ostream &
375 operator << (std::ostream & so, const stos_t<TImage> & stos)
384 template <
typename TImage>
386 save_stos(
const bfs::path & fn_save,
387 const bfs::path & fn_s0,
388 const bfs::path & fn_s1,
389 const bfs::path & fn_mask_s0,
390 const bfs::path & fn_mask_s1,
391 const bool & flipped_0,
392 const bool & flipped_1,
395 const unsigned int & shrink_factor,
396 const base_transform_t * transform,
397 const bool blab =
false)
409 return stos.save(fn_save, blab);