Subversion Repositories gelsvn

Rev

Rev 443 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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