Subversion Repositories gelsvn

Rev

Rev 595 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
595 jab 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
7
/**
8
 * @file save_raw.h
9
 * @brief Save a voxel grid.
10
 */
11
 
443 jab 12
#ifndef __GEOMETRY_SAVE_RAW_H
13
#define __GEOMETRY_SAVE_RAW_H
61 jab 14
 
15
#include <iostream>
16
#include <iomanip>
17
#include <fstream>
18
#include "GridAlgorithm.h"
19
 
20
namespace Geometry 
21
{
22
	template<class G>
23
		class VolSaver
24
		{
25
		public:
26
			typedef typename G::DataType DataType;
27
 
28
		private:
29
			std::ofstream of;
30
			const float min_val, max_val;
31
			const float diff;
32
			float old;
33
		public:
34
 
35
 			VolSaver(const std::string& name, float _min_val, float _max_val): 
36
				of(name.c_str(), std::ios::binary),
37
				min_val(_min_val), 
38
				max_val(_max_val),
39
				diff(max_val - min_val) {}
40
 
41
			void operator()(const CGLA::Vec3i& pi, const float& vox_val)
42
				{
43
					float scaled = (vox_val-min_val) / diff;
44
					float clamped = 255.0f *(CGLA::s_min(1.0f,CGLA::s_max(0.0f,scaled)));
45
					unsigned char x = static_cast<unsigned char>(clamped);
46
					of.write((char*) &x, 1);
47
				}	
48
		};
49
 
50
	template<class G>
51
		class VolSaverAscii
52
		{
53
		public:
54
			typedef typename G::DataType DataType;
55
 
56
		private:
57
			std::ofstream of;
58
			const float min_val, max_val;
59
			float old;
60
		public:
61
 
62
 			VolSaverAscii(const std::string& name, float _min_val, float _max_val): 
63
				of(name.c_str()),
64
				min_val(_min_val), 
65
				max_val(_max_val) {} 
66
 
67
			void operator()(const CGLA::Vec3i& pi, const float& vox_val)
68
				{
69
					if(vox_val > min_val && vox_val < max_val)
70
						of << pi[0] << " " << pi[1] << " " << pi[2]
125 jab 71
							 << " " << vox_val << std::endl;
61 jab 72
				}	
73
		};
74
 
75
 
76
	template<class G>
77
		class VolSaverFloat
78
		{
79
		public:
80
			typedef typename G::DataType DataType;
81
 
82
		private:
83
			std::ofstream of;
84
		public:
85
 
86
			VolSaverFloat(const std::string& name): 
87
				of(name.c_str(), std::ios::binary) {}
88
 
89
			void operator()(const CGLA::Vec3i& pi, const float& vox_val)
90
				{
91
					of.write((char*) &vox_val, sizeof(float));
92
				}	
93
		};
94
 
95
	template<class G>
96
		void save_raw_float(const std::string& name, G& grid)
97
		{
98
			VolSaverFloat<G> vs(name);
99
			for_each_voxel_ordered_const(grid, vs);	
100
		}
101
 
102
	template<class G>
103
		void save_raw_byte(const std::string& name, G& grid,
104
											 const typename G::DataType& min_val,
105
											 const typename G::DataType& max_val)
106
		{
107
			VolSaver<G> vs(name, min_val, max_val);
108
			for_each_voxel_ordered_const(grid, vs);	
109
		}
110
 
111
	template<class G>
112
		void save_raw_ascii(const std::string& name, G& grid,
113
												const typename G::DataType& min_val,
114
												const typename G::DataType& max_val)
115
		{
116
			VolSaverAscii<G> vs(name, min_val, max_val);
117
			for_each_voxel_ordered_const(grid, vs);	
118
		}
119
 
120
 
121
}
122
 
123
#endif