00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef MLN_IO_OFF_SAVE_HH
00028 # define MLN_IO_OFF_SAVE_HH
00029
00035
00036 # include <cstdlib>
00037
00038 # include <iostream>
00039 # include <fstream>
00040 # include <sstream>
00041
00042 # include <string>
00043
00044 # include <mln/core/alias/complex_image.hh>
00045 # include <mln/core/image/complex_neighborhoods.hh>
00046 # include <mln/core/image/complex_neighborhood_piter.hh>
00047
00048
00049 namespace mln
00050 {
00051
00052 namespace io
00053 {
00054
00055 namespace off
00056 {
00057
00065 void save(const bin_2complex_image3df& ima,
00066 const std::string& filename);
00067
00075 void save(const int_u8_2complex_image3df& ima,
00076 const std::string& filename);
00077
00086 void save(const float_2complex_image3df& ima,
00087 const std::string& filename);
00088
00096 void save(const rgb8_2complex_image3df& ima,
00097 const std::string& filename);
00098
00099
00100 namespace internal
00101 {
00102
00103 template <typename I, typename E>
00104 struct off_saver : public Object<E>
00105 {
00107 typedef mln_value(I) value;
00108
00110 static const unsigned D = 2;
00111
00113 off_saver();
00114
00116 void operator()(const I& ima, const std::string& filename) const;
00117 };
00118
00119
00120 struct bin_off_saver
00121 : public off_saver< bin_2complex_image3df, bin_off_saver >
00122 {
00126 void write_face_data(std::ostream& ostr, const value& v) const;
00127 };
00128
00129 struct int_u8_off_saver
00130 : public off_saver< int_u8_2complex_image3df, int_u8_off_saver >
00131 {
00133 void write_face_data(std::ostream& ostr, const value& v) const;
00134 };
00135
00136
00137
00138
00139
00140 struct float_off_saver
00141 : public off_saver< float_2complex_image3df, float_off_saver >
00142 {
00144 void write_face_data(std::ostream& ostr, const value& v) const;
00145 };
00146
00147
00148 struct rgb8_off_saver
00149 : public off_saver< rgb8_2complex_image3df, rgb8_off_saver >
00150 {
00152 void write_face_data(std::ostream& ostr, const value& v) const;
00153 };
00154
00155 }
00156
00157
00158
00159 # ifndef MLN_INCLUDE_ONLY
00160
00161
00162
00163
00164
00165 inline
00166 void
00167 save(const bin_2complex_image3df& ima, const std::string& filename)
00168 {
00169 trace::entering("mln::io::off::save");
00170 internal::bin_off_saver()(ima, filename);
00171 trace::exiting("mln::io::off::save");
00172 }
00173
00174 inline
00175 void
00176 save(const int_u8_2complex_image3df& ima, const std::string& filename)
00177 {
00178 trace::entering("mln::io::off::save");
00179 internal::int_u8_off_saver()(ima, filename);
00180 trace::exiting("mln::io::off::save");
00181 }
00182
00183 inline
00184 void
00185 save(const float_2complex_image3df& ima, const std::string& filename)
00186 {
00187 trace::entering("mln::io::off::save");
00188 internal::float_off_saver()(ima, filename);
00189 trace::exiting("mln::io::off::save");
00190 }
00191
00192 inline
00193 void
00194 save(const rgb8_2complex_image3df& ima, const std::string& filename)
00195 {
00196 trace::entering("mln::io::off::save");
00197 internal::rgb8_off_saver()(ima, filename);
00198 trace::exiting("mln::io::off::save");
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 namespace internal
00211 {
00212
00213 template <typename I, typename E>
00214 off_saver<I, E>::off_saver()
00215 {
00216
00217 void (E::*m1)(std::ostream&, const value&) const =
00218 &E::write_face_data;
00219 m1 = 0;
00220 }
00221
00222
00223 template <typename I, typename E>
00224 void
00225 off_saver<I, E>::operator()(const I& ima,
00226 const std::string& filename) const
00227 {
00228 const std::string me = "mln::io::off::save";
00229
00230 std::ofstream ostr(filename.c_str());
00231 if (!ostr)
00232 {
00233 std::cerr << me << ": `" << filename << "' invalid file."
00234 << std::endl;
00235
00236
00237
00238
00239
00240 std::exit(1);
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252 ostr << "OFF" << std::endl;
00253
00254
00255 ostr << "# Generated by Milena 1.0 http://olena.lrde.epita.fr\n"
00256 << "# EPITA Research and Development Laboratory (LRDE)"
00257 << std::endl;
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 ostr << ima.domain().cplx().template nfaces_of_static_dim<0>() << ' '
00271 << ima.domain().cplx().template nfaces_of_static_dim<2>() << ' '
00272 << ima.domain().cplx().template nfaces_of_static_dim<1>()
00273 << std::endl;
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283 typedef mln_geom(I) G;
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 p_n_faces_fwd_piter<D, G> v(ima.domain(), 0);
00294 for_all(v)
00295 {
00296 mln_invariant(v.to_site().size() == 1);
00297 ostr << v.to_site().front()[0] << ' '
00298 << v.to_site().front()[1] << ' '
00299 << v.to_site().front()[2] << std::endl;
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 p_n_faces_fwd_piter<D, G> f(ima.domain(), 2);
00313
00314 typedef complex_m_face_neighborhood<D, G> nbh_t;
00315
00316
00317 nbh_t nbh;
00318 mln_fwd_niter(nbh_t) u(nbh, f);
00319
00320
00321
00322 u.iter().set_m(0);
00323
00324
00325
00326 for_all(f)
00327 {
00328 unsigned nvertices = 0;
00329 std::ostringstream vertices;
00330 for_all(u)
00331 {
00332
00333 vertices << ' ' << u.unproxy_().face().face_id();
00334 ++nvertices;
00335 }
00336 ostr << nvertices << vertices.str();
00337
00338 exact(this)->write_face_data(ostr, ima(f));
00339 ostr << std::endl;
00340 }
00341
00342 ostr.close();
00343 }
00344
00345
00346
00347
00348
00388 inline
00389 void
00390 bin_off_saver::write_face_data(std::ostream& ,
00391 const value& ) const
00392 {
00393
00394 }
00395
00396 inline
00397 void
00398 int_u8_off_saver::write_face_data(std::ostream& ostr,
00399 const value& v) const
00400 {
00401
00402
00403
00404
00405
00406
00407
00408
00409 ostr << ' ' << v << ' ' << v << ' ' << v
00410 << ' ' << 1.0f << std::endl;
00411 }
00412
00413
00414
00415 inline
00416 void
00417 float_off_saver::write_face_data(std::ostream& ostr,
00418 const value& v) const
00419 {
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429 mln_assertion(0.0f <= v);
00430 mln_assertion(v <= 1.0f);
00431 ostr << ' ' << v << ' ' << v << ' ' << v
00432 << ' ' << 1.0f;
00433 }
00434
00435 inline
00436 void
00437 rgb8_off_saver::write_face_data(std::ostream& ostr,
00438 const value& v) const
00439 {
00440
00441
00442
00443
00444
00445 ostr << ' ' << v.red() << ' ' << v.green() << ' ' << v.blue()
00446 << ' ' << 1.0f;
00447 }
00448
00449
00450 }
00451
00452
00453 # endif // ! MLN_INCLUDE_ONLY
00454
00455 }
00456
00457 }
00458
00459 }
00460
00461
00462 #endif // ! MLN_IO_OFF_SAVE_HH