Subversion Repositories gelsvn

Rev

Rev 125 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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