Blame view

stim/grids/grid.h 3.96 KB
ad4a30c5   David Mayerich   changed grid_data...
1
2
  #ifndef STIM_GRID_H
  #define STIM_GRID_H
1d08c377   David Mayerich   added grids subdi...
3
4
5
6
  
  #include <vector>
  #include <string>
  #include <sstream>
8b7be670   David Mayerich   implemented savin...
7
  #include <fstream>
945ee13c   Laila Saadatifard   the get_list func...
8
  #include <cstdarg> 
1d08c377   David Mayerich   added grids subdi...
9
  
cce7daf9   Pavel Govyadinov   added glObj.h to ...
10
  #include <stim/math/vector.h>
1d08c377   David Mayerich   added grids subdi...
11
  
1d08c377   David Mayerich   added grids subdi...
12
13
  namespace stim{
  
ae407376   David Mayerich   added support for...
14
15
16
17
  /**This object describes a generic D-dimensional grid containing data of type T.
  	Functions are provided for saving and loading binary data.
  
  **/
1d08c377   David Mayerich   added grids subdi...
18
  template<typename T, unsigned int D = 1>
ad4a30c5   David Mayerich   changed grid_data...
19
  class grid{
1d08c377   David Mayerich   added grids subdi...
20
21
22
  
  protected:
  
5eeaf941   Pavel Govyadinov   changer to the ba...
23
24
  	stim::vec<unsigned long> R;		//elements in each dimension
  	stim::vec<float> S;
8157c392   David Mayerich   added parser and ...
25
  	T* ptr;									//pointer to the data (on the GPU or CPU)
8157c392   David Mayerich   added parser and ...
26
  
ae407376   David Mayerich   added support for...
27
  	///Return the total number of values in the binary file
8157c392   David Mayerich   added parser and ...
28
29
30
31
32
33
34
  	unsigned long samples(){
  
  		unsigned long s = 1;
  		for(unsigned int d = 0; d < D; d++)
  			s *= R[d];
  
  		return s;
ae407376   David Mayerich   added support for...
35
36
37
38
39
40
41
42
43
44
45
46
47
  	}
  
  	///Initializes a grid by allocating the necessary memory and setting all values to zero
  	void init(){
  
  		//calculate the total number of values
  		unsigned long S = samples();
  
  		//allocate memory to store the grid
  		ptr = (T*)malloc(sizeof(T) * S);
  
  		//initialize the memory to zero
  		memset(ptr, 0, sizeof(T) * S);
8157c392   David Mayerich   added parser and ...
48
49
50
51
52
  
  	}
  
  public:
  
72bc805b   David Mayerich   added a default c...
53
  	///Default constructor doesn't do anything
945ee13c   Laila Saadatifard   the get_list func...
54
55
56
  	grid(){
  		ptr = NULL;			//set the pointer to NULL so that we know nothing is allocated
  	}
ae407376   David Mayerich   added support for...
57
  
72bc805b   David Mayerich   added a default c...
58
59
  	///Constructor used to specify the grid size as a vector
  	
ae407376   David Mayerich   added support for...
60
  	/// @param _R is a vector describing the grid resolution
5eeaf941   Pavel Govyadinov   changer to the ba...
61
  	grid( stim::vec<unsigned long> _R){
ae407376   David Mayerich   added support for...
62
63
64
65
66
67
68
  		
  		//set the grid resolution
  		R = _R;
  
  		init();		
  	}
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
69
  	void
5eeaf941   Pavel Govyadinov   changer to the ba...
70
  	setDim(stim::vec<float> s)
385d2447   Pavel Govyadinov   Checkpoint: Conve...
71
72
73
74
  	{
  		S = s;
  	}
  
ae407376   David Mayerich   added support for...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  	///Constructor used to specify the grid size as a set of parameters
  
  	/// @param X0... is a list of values describing the grid size along each dimension
  	grid( unsigned long X0, ...){
  
  		R[0] = X0;
  
  		va_list ap;
  		va_start(ap, X0);
  		for(unsigned int d = 1; d<D; d++)
  			R[d] = va_arg(ap, unsigned long);
  		va_end(ap);
  
  		init();
  
  	}
  
  	///Writes the binary data to disk
  
  	/// @param filename is the name of the binary file to be written
8157c392   David Mayerich   added parser and ...
95
96
  	void write(std::string filename){
  
8b7be670   David Mayerich   implemented savin...
97
98
  		std::fstream file;
  
8157c392   David Mayerich   added parser and ...
99
100
101
102
  		//open the file as binary for reading
  		file.open(filename.c_str(), std::ios::out | std::ios::binary);
  
  		//write file to disk
8b7be670   David Mayerich   implemented savin...
103
  		file.write((char *)ptr, samples() * sizeof(T));
8157c392   David Mayerich   added parser and ...
104
105
  	}
  
ae407376   David Mayerich   added support for...
106
107
108
109
110
  	///Loads a binary file from disk
  
  	/// @param filename is the name of the file containing the binary data
  	/// @param S is the size of the binary file along each dimension
  	/// @param header is the size of the header in bytes
5eeaf941   Pavel Govyadinov   changer to the ba...
111
  	void read(std::string filename, stim::vec<unsigned long> S, unsigned long header = 0){
8157c392   David Mayerich   added parser and ...
112
113
114
  
  		R = S;	//set the sample resolution
  
945ee13c   Laila Saadatifard   the get_list func...
115
116
117
  		//allocate space for the data
  		init();
  
8b7be670   David Mayerich   implemented savin...
118
119
  		std::fstream file;
  
8157c392   David Mayerich   added parser and ...
120
121
122
123
124
125
  		//open the file as binary for writing
  		file.open(filename.c_str(), std::ios::in | std::ios::binary);
  
  		//seek past the header
  		file.seekg(header, std::ios::beg);
  
945ee13c   Laila Saadatifard   the get_list func...
126
  
8157c392   David Mayerich   added parser and ...
127
  		//read the data
8b7be670   David Mayerich   implemented savin...
128
  		file.read((char *)ptr, samples() * sizeof(T));
8157c392   David Mayerich   added parser and ...
129
130
  	}
  
ae407376   David Mayerich   added support for...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  	///Gets a single value from the grid given a set of coordinates
  
  	/// @param x0... is a list of coordinates specifying the desired value
  	T get(unsigned long x0, ...){
  
  		va_list ap;
  		
  		unsigned long F = 1;
  		unsigned long p = x0;
  
  		va_start(ap, x0);
  		for(unsigned int d = 1; d<D; d++){
  			F *= R[d-1];
  			p += va_arg(ap, unsigned int) * F;
  		}
  		va_end(ap);
  
  		return ptr[p];
  	}
  
  	///Sets a value in the grid
  
  	/// @param value is the grid point value
  	/// @x0... is the coordinate of the value to be set
  	void set(T value, unsigned long x0, ...){
  
  		va_list ap;
  		
  		unsigned long F = 1;
  		unsigned long p = x0;
  
  		va_start(ap, x0);
  		for(unsigned int d = 1; d<D; d++){
  			F *= R[d-1];
  			p += va_arg(ap, unsigned int) * F;
  		}
  		va_end(ap);
  
  		ptr[p] = value;
  	}
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
172
  
ae407376   David Mayerich   added support for...
173
174
  	///Outputs grid data as a string
  	std::string str(){
8157c392   David Mayerich   added parser and ...
175
  
ae407376   David Mayerich   added support for...
176
  		std::stringstream result;
1d08c377   David Mayerich   added grids subdi...
177
  
ae407376   David Mayerich   added support for...
178
179
180
181
  		result<<"stim::grid structure of size [";
  		for(unsigned int d = 0; d<D; d++){
  			if(d!=0) result<<", ";
  			result<<R[d];
945ee13c   Laila Saadatifard   the get_list func...
182
  
ae407376   David Mayerich   added support for...
183
  		}
945ee13c   Laila Saadatifard   the get_list func...
184
  
ae407376   David Mayerich   added support for...
185
186
187
  		result<<"]"<<std::endl;
  
  		//calculate the number of values to output
5eeaf941   Pavel Govyadinov   changer to the ba...
188
  		unsigned long nV = min((unsigned long long)R[0], (unsigned long long)10);
ae407376   David Mayerich   added support for...
189
190
191
192
193
194
195
196
  
  		for(unsigned long v = 0; v<nV; v++){
  			result<<ptr[v];
  			if(v != nV-1) result<<", ";
  		}
  
  		return result.str();
  	}
1d08c377   David Mayerich   added grids subdi...
197
198
199
200
201
  };
  
  }
  
  
8b7be670   David Mayerich   implemented savin...
202
  #endif