35 #ifndef __VMML__T3_CONVERTER__HPP__
36 #define __VMML__T3_CONVERTER__HPP__
38 #include <vmmlib/tensor3.hpp>
42 template<
size_t I1,
size_t I2,
size_t I3,
typename T =
float >
50 template<
typename T_convert >
51 static void convert_raw(
const std::string& dir_,
const std::string& in_filename_,
const std::string& out_filename_);
54 static void remove_uct_cylinder(
const std::string& dir_,
55 const std::string& in_filename_,
56 const std::string& out_filename_,
58 const size_t header_size_,
59 const size_t radius_offset_,
62 static void export_to(
const t3_t& input_, std::vector< T >& output_);
63 static void import_from(
const std::vector< T >& input_,
t3_t& output_);
64 static void write_to_raw(
const t3_t& data_,
const std::string& dir_,
const std::string& filename_);
65 static void read_from_raw(
t3_t& data_,
const std::string& dir_,
const std::string& filename_);
66 static void write_datfile(
const std::string& dir_,
const std::string& filename_);
67 static void write_to_csv(
const t3_t& data_,
const std::string& dir_,
const std::string& filename_);
68 static void remove_normals_from_raw(
const t3_t& data_,
const std::string& dir_,
const std::string& filename_);
69 static double rmse_from_files(
const std::string& dir_,
const std::string& filename_a_,
const std::string& filename_b_ );
71 template<
typename TT >
72 static void quantize_to(
const std::string& dir_,
73 const std::string& in_filename_,
const std::string& out_filename_,
74 const T& min_value_,
const T& max_value_);
79 static void concat_path(
const std::string& dir_,
const std::string& filename_, std::string& path_);
85 #define VMML_TEMPLATE_STRING template< size_t I1, size_t I2, size_t I3, typename T >
86 #define VMML_TEMPLATE_CLASSNAME t3_converter< I1, I2, I3, T >
89 template<
typename TT >
91 VMML_TEMPLATE_CLASSNAME::quantize_to(
const std::string& dir_,
92 const std::string& in_filename_,
const std::string& out_filename_,
93 const T& min_value_,
const T& max_value_) {
94 std::string path_in_raw =
"";
95 std::string path_out_raw =
"";
96 concat_path(dir_, in_filename_, path_in_raw);
97 concat_path(dir_, out_filename_, path_out_raw);
99 std::ofstream outfile;
100 outfile.open(path_out_raw.c_str());
102 std::ifstream infile;
103 infile.open(path_in_raw.c_str(), std::ios::in);
105 if (infile.is_open() && outfile.is_open()) {
106 double max_tt_range = double((std::numeric_limits< TT >::max)());
107 double min_tt_range = double((std::numeric_limits< TT >::min)());
108 double tt_range = max_tt_range - min_tt_range;
109 double t_range = max_value_ - min_value_;
116 size_t len_in =
sizeof (T);
117 size_t len_out =
sizeof (TT);
118 char* data =
new char[ len_in ];
120 for (
size_t i3 = 0; i3 < I3; ++i3) {
121 for (
size_t i1 = 0; i1 < I1; ++i1) {
122 for (
size_t i2 = 0; i2 < I2; ++i2) {
124 infile.read(data, len_in);
125 in_value = (T*)&(data[0]);
128 if (std::numeric_limits<TT>::is_signed) {
129 out_value = TT((std::min)((std::max)(min_tt_range,
double((*in_value * tt_range / t_range) + 0.5)), max_tt_range));
131 out_value = TT((std::min)((std::max)(min_tt_range,
double(((*in_value - min_value_) * tt_range / t_range) + 0.5)), max_tt_range));
135 outfile.write((
char*) &(out_value), len_out);
145 std::cout <<
"no file open" << std::endl;
151 VMML_TEMPLATE_CLASSNAME::concat_path(
const std::string& dir_,
const std::string& filename_, std::string& path_) {
152 int dir_length = dir_.size() - 1;
153 int last_separator = dir_.find_last_of(
"/");
155 if (last_separator < dir_length) {
158 path_.append(filename_);
161 if( filename_.find(
"raw", filename_.size() - 3) == std::string::npos )
169 template<
typename T_convert >
171 VMML_TEMPLATE_CLASSNAME::convert_raw(
const std::string& dir_,
const std::string& in_filename_,
const std::string& out_filename_) {
172 std::string path_in_raw =
"";
173 std::string path_out_raw =
"";
174 concat_path(dir_, in_filename_, path_in_raw);
175 concat_path(dir_, out_filename_, path_out_raw);
177 std::ofstream outfile;
178 outfile.open(path_out_raw.c_str());
180 std::ifstream infile;
181 infile.open(path_in_raw.c_str(), std::ios::in);
183 if (infile.is_open() && outfile.is_open()) {
186 size_t len_in =
sizeof (T);
187 size_t len_out =
sizeof (T_convert);
188 char* data =
new char[ len_in ];
190 for (
size_t i3 = 0; i3 < I3; ++i3) {
191 for (
size_t i1 = 0; i1 < I1; ++i1) {
192 for (
size_t i2 = 0; i2 < I2; ++i2) {
193 infile.read(data, len_in);
194 in_value = (T*)&(data[0]);
195 out_value =
static_cast<T_convert
> (*in_value);
196 outfile.write((
char*) &(out_value), len_out);
206 std::cout <<
"no file open" << std::endl;
212 VMML_TEMPLATE_CLASSNAME::remove_uct_cylinder(
const std::string& dir_,
213 const std::string& in_filename_,
214 const std::string& out_filename_,
215 const double& sigma_,
216 const size_t header_size_,
217 const size_t radius_offset_,
219 std::string path_in_raw =
"";
220 std::string path_out_raw =
"";
221 concat_path(dir_, in_filename_, path_in_raw);
222 concat_path(dir_, out_filename_, path_out_raw);
224 std::ofstream outfile;
225 outfile.open(path_out_raw.c_str());
227 std::ifstream infile;
228 infile.open(path_in_raw.c_str(), std::ios::in);
235 double radius = (I1 - 1.0) / 2.0 - radius_offset_;
239 double fill_value = 0;
241 if (infile.is_open() && outfile.is_open()) {
244 size_t len_val =
sizeof (T);
245 char* data =
new char[ len_val ];
248 infile.read(data, header_size_);
251 for (
size_t i3 = 0; i3 < I3; ++i3) {
252 for (
size_t i1 = 0; i1 < I1; ++i1) {
253 k1 = i1 - (I1 - 1.0) / 2.0;
254 for (
size_t i2 = 0; i2 < I2; ++i2) {
255 infile.read(data, len_val);
256 in_value = (T*)&(data[0]);
257 fill_value =
static_cast<T
> (*in_value);
260 k2 = i2 - (I2 - 1.0) / 2.0;
261 length = k1 * k1 + k2*k2;
262 if (length >= radius) {
264 fill_value /= RAND_MAX;
265 fill_value *= sigma_;
268 out_value =
static_cast<T
> (fill_value);
269 outfile.write((
char*) &(out_value), len_val);
279 std::cout <<
"no file open" << std::endl;
285 VMML_TEMPLATE_CLASSNAME::export_to(
const t3_t& input_, std::vector< T >& output_) {
287 const_iterator it = input_.begin(),
288 it_end = input_.end();
289 for (; it != it_end; ++it) {
290 output_.push_back(*it);
296 VMML_TEMPLATE_CLASSNAME::import_from(
const std::vector< T >& input_, t3_t& output_) {
298 size_t input_size = input_.size();
300 iterator it = output_.begin(),
301 it_end = output_.end();
302 for (; it != it_end; ++it, ++i) {
304 *it =
static_cast<T
> (0);
312 VMML_TEMPLATE_CLASSNAME::remove_normals_from_raw(
const t3_t& data_,
const std::string& dir_,
const std::string& filename_) {
313 int dir_length = dir_.size() - 1;
314 int last_separator = dir_.find_last_of(
"/");
315 std::string path = dir_;
316 if (last_separator < dir_length) {
319 path.append(filename_);
322 size_t max_file_len = 2147483648u -
sizeof (T);
323 size_t len_data =
sizeof (T) * data_.SIZE;
324 size_t len_value = sizeof (T) * 4;
326 char* data =
new char[ len_data ];
327 std::ifstream infile;
328 infile.open(path.c_str(), std::ios::in);
330 if (infile.is_open()) {
331 tensor3_iterator<T> it = data_.begin(),
332 it_end = data_.end();
335 while (len_data > 0) {
336 len_read = (len_data % max_file_len) > 0 ? len_data % max_file_len : len_data;
337 len_data -= len_read;
338 infile.read(data, len_read);
340 T* T_ptr = (T*)&(data[0]);
341 for (; (it != it_end) && (len_read > 0); ++it, len_read -= len_value) {
354 std::cout <<
"no file open" << std::endl;
357 std::cout <<
"converted normals" << std::endl;
359 std::string filename =
"";
360 filename = filename_.substr(0, filename_.size() - 6);
363 write_datfile(
".", filename);
364 write_to_raw(
".", filename);
369 VMML_TEMPLATE_CLASSNAME::read_from_raw(t3_t& data_,
const std::string& dir_,
const std::string& filename_) {
370 int dir_length = dir_.size() - 1;
371 int last_separator = dir_.find_last_of(
"/");
372 std::string path = dir_;
373 if (last_separator < dir_length) {
376 path.append(filename_);
378 size_t max_file_len = 2147483648u -
sizeof (T);
379 size_t len_data =
sizeof (T) * data_.SIZE;
381 char* data =
new char[ len_data ];
382 std::ifstream infile;
383 infile.open(path.c_str(), std::ios::in);
385 if (infile.is_open()) {
386 tensor3_iterator<t3_t> it = data_.begin(),
387 it_end = data_.end();
389 while (len_data > 0) {
390 len_read = (len_data % max_file_len) > 0 ? len_data % max_file_len : len_data;
391 len_data -= len_read;
392 infile.read(data, len_read);
394 T* T_ptr = (T*)&(data[0]);
395 for (; (it != it_end) && (len_read > 0); ++it, len_read -=
sizeof (T)) {
404 std::cout <<
"no file open" << std::endl;
411 VMML_TEMPLATE_CLASSNAME::write_to_raw(
const t3_t& data_,
const std::string& dir_,
const std::string& filename_) {
412 int dir_length = dir_.size() - 1;
413 int last_separator = dir_.find_last_of(
"/");
414 std::string path = dir_;
415 if (last_separator < dir_length) {
418 path.append(filename_);
420 if (filename_.find(
"raw", filename_.size() - 3) == std::string::npos) {
424 std::string path_raw = path;
426 std::ofstream outfile;
427 outfile.open(path_raw.c_str());
428 if (outfile.is_open()) {
429 size_t len_slice =
sizeof (T) * I1 * I2;
430 for (
size_t index = 0; index < I3; ++index) {
431 outfile.write((
char*) &(data_.get_frontal_slice_fwd(index)), len_slice);
436 std::cout <<
"no file open" << std::endl;
442 VMML_TEMPLATE_CLASSNAME::write_datfile(
const std::string& dir_,
const std::string& filename_) {
443 int dir_length = dir_.size() - 1;
444 int last_separator = dir_.find_last_of(
"/");
445 std::string path = dir_;
446 if (last_separator < dir_length) {
450 std::string filename = filename_;
451 const size_t pos = filename_.size();
452 if ((filename_.find(
".raw", pos) + 4 == pos) ||
453 (filename_.find(
".dat", pos) + 4 == pos))
455 filename = filename_.substr(0, filename_.size() - 4);
457 path.append(filename);
459 if (filename_.find(
"dat", filename_.size() - 3) == std::string::npos) {
464 std::string path_dat = path;
466 const char* format = (
sizeof (T) == 2) ?
"USHORT" :
"UCHAR";
468 FILE* datfile = fopen(path_dat.c_str(),
"w");
469 fprintf(datfile,
"ObjectFileName:\t%s.raw\n", filename.c_str());
470 fprintf(datfile,
"TaggedFileName:\t---\nResolution:\t%i %i %i\n",
int(I1),
int(I2),
int(I3));
471 fprintf(datfile,
"SliceThickness:\t1.0 1.0 1.0\n");
472 fprintf(datfile,
"Format:\t%s\nNbrTags:\t0\n", format);
473 fprintf(datfile,
"ObjectType:\tTEXTURE_VOLUME_OBJECT\nObjectModel:\tI\nGridType:\tEQUIDISTANT\n");
474 fprintf(datfile,
"Modality:\tunknown\nTimeStep:\t0\n");
480 VMML_TEMPLATE_CLASSNAME::write_to_csv(
const t3_t& data_,
const std::string& dir_,
const std::string& filename_) {
481 int dir_length = dir_.size() - 1;
482 int last_separator = dir_.find_last_of(
"/");
483 std::string path = dir_;
484 if (last_separator < dir_length) {
487 path.append(filename_);
489 if (filename_.find(
"csv", filename_.size() - 3) == std::string::npos)
495 std::ofstream outfile;
496 outfile.open(path.c_str());
497 if (outfile.is_open()) {
499 for (
size_t i = 0; i < I3; ++i) {
500 outfile << data_.get_frontal_slice_fwd(i) << std::endl;
505 std::cout <<
"no file open" << std::endl;
512 VMML_TEMPLATE_CLASSNAME::rmse_from_files(
const std::string& dir_,
513 const std::string& filename_a_,
const std::string& filename_b_ ) {
514 std::string path_a_raw =
"";
515 std::string path_b_raw =
"";
516 concat_path( dir_, filename_a_, path_a_raw );
517 concat_path( dir_, filename_b_, path_b_raw );
520 afile.open( path_a_raw.c_str(), std::ios::in);
523 bfile.open( path_b_raw.c_str(), std::ios::in);
525 double mse_val = 0.0f;
526 double mse_val_avg = 0.0f;
527 double mse_val_i3 = 0.0f;
530 if (afile.is_open() && bfile.is_open()) {
536 size_t value_len =
sizeof (T);
537 char* data_a =
new char[ value_len ];
538 char* data_b =
new char[ value_len ];
540 for (
size_t i3 = 0; i3 < I3; ++i3) {
542 for (
size_t i1 = 0; i1 < I1; ++i1) {
543 for (
size_t i2 = 0; i2 < I2; ++i2) {
544 afile.read( data_a, value_len);
545 a_value = (T*)&(data_a[0]);
546 a_value_f =
static_cast<double> (*a_value);
548 bfile.read( data_b, value_len);
549 b_value = (T*)&(data_b[0]);
550 b_value_f =
static_cast<double> (*b_value);
552 diff = fabs(b_value_f - a_value_f);
557 mse_val_avg = mse_val;
558 mse_val_avg /= double(I1);
559 mse_val_avg /= double(I2);
560 mse_val_i3 += mse_val_avg;
570 std::cout <<
"no file open" << std::endl;
574 mse_val_i3 /= double(I3);
575 return sqrt(mse_val_i3);
579 #undef VMML_TEMPLATE_STRING
580 #undef VMML_TEMPLATE_CLASSNAME