27 #ifndef MLN_IO_VTK_SAVE_HH
28 # define MLN_IO_VTK_SAVE_HH
44 # include <mln/version.hh>
45 # include <mln/core/alias/complex_image.hh>
46 # include <mln/core/image/complex_neighborhoods.hh>
47 # include <mln/core/image/complex_neighborhood_piter.hh>
65 void save(
const bin_2complex_image3df& ima,
74 void save(
const int_u8_2complex_image3df& ima,
82 void save(
const unsigned_2complex_image3df& ima,
91 void save(
const float_2complex_image3df& ima,
99 void save(
const rgb8_2complex_image3df& ima,
106 template <
typename I,
typename E>
107 struct vtk_saver :
public Object<E>
113 typedef mln_value(I)
value;
116 static const unsigned D = 2;
122 void operator()(
const I& ima,
const std::string&
filename)
const;
127 void write_scalar_data(std::ostream& ostr,
const image& ima,
128 const std::string& data_type)
const;
133 :
public vtk_saver< bin_2complex_image3df, bin_vtk_saver >
136 void write_face_data(std::ostream& ostr,
const image& ima)
const;
140 struct int_u8_vtk_saver
141 :
public vtk_saver< int_u8_2complex_image3df, int_u8_vtk_saver >
144 void write_face_data(std::ostream& ostr,
const image& ima)
const;
148 struct unsigned_vtk_saver
149 :
public vtk_saver< unsigned_2complex_image3df, unsigned_vtk_saver >
152 void write_face_data(std::ostream& ostr,
const image& ima)
const;
156 struct float_vtk_saver
157 :
public vtk_saver< float_2complex_image3df, float_vtk_saver >
160 void write_face_data(std::ostream& ostr,
const image& ima)
const;
164 struct rgb8_vtk_saver
165 :
public vtk_saver< rgb8_2complex_image3df, rgb8_vtk_saver >
168 void write_face_data(std::ostream& ostr,
const image& ima)
const;
175 # ifndef MLN_INCLUDE_ONLY
184 mln_trace(
"mln::io::vtk::save");
185 internal::bin_vtk_saver()(ima,
filename);
191 mln_trace(
"mln::io::vtk::save");
192 internal::int_u8_vtk_saver()(ima,
filename);
198 mln_trace(
"mln::io::vtk::save");
199 internal::unsigned_vtk_saver()(ima,
filename);
203 save(
const float_2complex_image3df& ima,
const std::string&
filename)
205 mln_trace(
"mln::io::vtk::save");
206 internal::float_vtk_saver()(ima,
filename);
210 save(
const rgb8_2complex_image3df& ima,
const std::string&
filename)
212 mln_trace(
"mln::io::vtk::save");
213 internal::rgb8_vtk_saver()(ima,
filename);
228 template <
typename I,
typename E>
229 vtk_saver<I, E>::vtk_saver()
232 void (E::*m1)(std::ostream&,
const I&)
const =
238 template <
typename I,
typename E>
240 vtk_saver<I, E>::operator()(
const I& ima,
243 const std::string me =
"mln::io::vtk::save";
245 std::ofstream ostr(filename.c_str());
248 std::cerr << me <<
": `" << filename <<
"' invalid file."
275 ostr <<
"# vtk DataFile Version 2.0" << std::endl;
282 ostr <<
"Generated by " OLN_PACKAGE_STRING
" (" OLN_PACKAGE_URL
")"
289 ostr <<
"ASCII" << std::endl;
310 ostr <<
"DATASET POLYDATA" << std::endl << std::endl;
316 typedef mln_geom(I) G;
334 << ima.domain().cplx().template nfaces_of_static_dim<0>()
335 << "
float" << std::endl;
337 p_n_faces_fwd_piter<D, G> v(ima.domain(), 0);
340 mln_invariant(v.to_site().size() == 1);
341 ostr << v.to_site().front()[0] <<
' '
342 << v.to_site().front()[1] <<
' '
343 << v.to_site().front()[2] << std::endl;
371 unsigned nvertices = 0;
377 ostr <<
"VERTICES " << nvertices <<
' '
383 << nvertices * 2 << std::endl;
386 ostr << "1 " << v.unproxy_().face().face_id() << std::endl;
396 p_n_faces_fwd_piter<D, G> e(ima.domain(), 1);
402 ostr <<
"LINES " << nedges <<
' '
408 << nedges * 3 << std::endl;
411 typedef complex_lower_neighborhood<D, G> adj_vertices_nbh_t;
412 adj_vertices_nbh_t adj_vertices_nbh;
413 mln_niter(adj_vertices_nbh_t) adj_v(adj_vertices_nbh, e);
422 ostr <<
" " << adj_v.unproxy_().face().face_id();
434 unsigned npolygons = 0;
435 p_n_faces_fwd_piter<D, G>
p(ima.domain(), 2);
443 typedef complex_m_face_neighborhood<D, G> nbh_t;
445 mln_fwd_niter(nbh_t) u(nbh,
p);
453 unsigned polygons_size = 0;
457 unsigned nvertices = 0;
467 polygons_size += 1 + nvertices;
469 ostr <<
"POLYGONS " << npolygons <<
' ' << polygons_size
477 unsigned nvertices = 0;
478 std::ostringstream vertices;
483 vertices <<
' ' << u.unproxy_().face().face_id();
486 ostr << nvertices << vertices.str() << std::endl;
505 unsigned nfaces = nvertices + nedges + npolygons;
511 ostr <<
"CELL_DATA " << nfaces << std::endl;
512 exact(
this)->write_face_data(ostr, ima);
565 template <
typename I,
typename E>
567 vtk_saver<I, E>::write_scalar_data(std::ostream& ostr,
569 const std::string& data_type)
const
571 ostr <<
"SCALARS values " << data_type << std::endl
572 <<
"LOOKUP_TABLE default" << std::endl;
574 mln_fwd_piter(image)
p(ima.domain());
576 ostr << ima(
p) << std::endl;
581 bin_vtk_saver::write_face_data(std::ostream& ostr,
582 const image& ima)
const
584 write_scalar_data(ostr, ima,
"bit");
588 int_u8_vtk_saver::write_face_data(std::ostream& ostr,
589 const image& ima)
const
591 write_scalar_data(ostr, ima,
"unsigned_char");
595 unsigned_vtk_saver::write_face_data(std::ostream& ostr,
596 const image& ima)
const
598 write_scalar_data(ostr, ima,
"unsigned_int");
602 float_vtk_saver::write_face_data(std::ostream& ostr,
603 const image& ima)
const
605 write_scalar_data(ostr, ima,
"float");
622 rgb8_vtk_saver::write_face_data(std::ostream& ostr,
623 const image& ima)
const
625 ostr <<
"COLOR_SCALARS values 4" << std::endl;
627 mln_fwd_piter_(image)
p(ima.domain());
630 ostr <<
float(ima(
p).red()) / mln_max(
value::red_t) << ' '
631 <<
float(ima(
p).green()) / mln_max(
value::green_t) << ' '
632 <<
float(ima(
p).blue()) / mln_max(
value::blue_t) << ' '
640 # endif // ! MLN_INCLUDE_ONLY
649 #endif // ! MLN_IO_VTK_SAVE_HH