Blame view

stim/grids/grid.h 6.3 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.
  
  **/
983b730b   David Mayerich   texture updates
18
  template<typename T, unsigned int D = 1, typename F = float>
ad4a30c5   David Mayerich   changed grid_data...
19
  class grid{
1d08c377   David Mayerich   added grids subdi...
20
21
22
  
  protected:
  
983b730b   David Mayerich   texture updates
23
24
  	size_t R[D];							//elements in each dimension
  	F S[D];									//spacing between element samples
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
  
26da82c8   David Mayerich   finalized stim::g...
27
  	
ae407376   David Mayerich   added support for...
28
29
  
  	///Initializes a grid by allocating the necessary memory and setting all values to zero
983b730b   David Mayerich   texture updates
30
  	void init(){		
26da82c8   David Mayerich   finalized stim::g...
31
32
33
34
35
36
37
38
39
  		ptr = NULL;										//initialize the data pointer to NULL
  		memset(R, 0, sizeof(size_t) * D);				//set the resolution to zero
  		for(size_t d = 0; d < D; d++) S[d] = (F)1.0;	//initialize the spacing to unity
  	}
  
  	void alloc(){
  		if(ptr != NULL) free(ptr);						//if memory has already been allocated, free it
  		size_t N = samples();							//calculate the total number of values		
  		ptr = (T*)calloc(sizeof(T), N);					//allocate memory to store the grid
8157c392   David Mayerich   added parser and ...
40
41
42
43
  	}
  
  public:
  
72bc805b   David Mayerich   added a default c...
44
  	///Default constructor doesn't do anything
945ee13c   Laila Saadatifard   the get_list func...
45
  	grid(){
26da82c8   David Mayerich   finalized stim::g...
46
  		init();
945ee13c   Laila Saadatifard   the get_list func...
47
  	}
ae407376   David Mayerich   added support for...
48
  
72bc805b   David Mayerich   added a default c...
49
50
  	///Constructor used to specify the grid size as a vector
  	
ae407376   David Mayerich   added support for...
51
  	/// @param _R is a vector describing the grid resolution
983b730b   David Mayerich   texture updates
52
53
54
  	grid( stim::vec<size_t> _R){
  		for (size_t d = 0; d < D; d++)
  			R[d] = _R[d];
ae407376   David Mayerich   added support for...
55
56
57
  		init();		
  	}
  
26da82c8   David Mayerich   finalized stim::g...
58
59
60
61
62
63
64
65
66
67
68
69
70
  	///Return the total number of values in the binary file
  	size_t samples(){
  		size_t s = 1;
  		for(size_t d = 0; d < D; d++)
  			s *= R[d];
  		return s;
  	}
  
  	///Return the number of bytes in the binary file
  	size_t bytes(){
  		return samples() * sizeof(T);
  	}
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
71
  	void
983b730b   David Mayerich   texture updates
72
73
74
  	setDim(stim::vec<float> s){
  		for(size_t d = 0; d < D; d++)
  			S[d] = s[d];
385d2447   Pavel Govyadinov   Checkpoint: Conve...
75
76
  	}
  
ae407376   David Mayerich   added support for...
77
  	///Constructor used to specify the grid size as a set of parameters
ae407376   David Mayerich   added support for...
78
  	/// @param X0... is a list of values describing the grid size along each dimension
26da82c8   David Mayerich   finalized stim::g...
79
  	/*grid( size_t X0, ...){
983b730b   David Mayerich   texture updates
80
81
82
83
84
  		R[0] = X0;									//set the grid size of the first dimension
  		va_list ap;									//get a variable list
  		va_start(ap, X0);							//start the variable list at the first element
  		for(size_t d = 1; d<D; d++)					//for each additional element
  			R[d] = va_arg(ap, size_t);				//read the value from the variable list as a size_t
ae407376   David Mayerich   added support for...
85
  		va_end(ap);
983b730b   David Mayerich   texture updates
86
  		init();										//initialize the grid
26da82c8   David Mayerich   finalized stim::g...
87
  	}*/
ae407376   David Mayerich   added support for...
88
  
983b730b   David Mayerich   texture updates
89
90
  	///Set the spacing between grid sample points
  	/// @param X0... is a list of values describing the grid sample spacing
26da82c8   David Mayerich   finalized stim::g...
91
  	/*void spacing(F X0, ...) {
983b730b   David Mayerich   texture updates
92
93
94
95
  		S[0] = X0;											//set the grid size of the first dimension
  		va_list ap;											//get a variable list
  		va_start(ap, X0);									//start the variable list at the first element
  		for (size_t d = 1; d<D; d++)						//for each additional element
26da82c8   David Mayerich   finalized stim::g...
96
  			S[d] = va_arg(ap, F);						//read the value from the variable list as a size_t
983b730b   David Mayerich   texture updates
97
  		va_end(ap);
26da82c8   David Mayerich   finalized stim::g...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  	}*/
  
  	/// Set the spacing between grid sample points for the specified dimension
  	void spacing(size_t d, F sp){
  		if(d < D) S[d] = sp;
  		else{
  			std::cout<<"error in stim::grid::spacing() - insufficient dimensions"<<std::endl;
  			exit(1);
  		}
  	}
  
  	/// Return the spacing for a given dimension
  	F spacing(size_t d){
  		if(d < D) return S[d];
  		else{
  			std::cout<<"error in stim::grid::spacing() - insufficient dimensions"<<std::endl;
  			exit(1);
  		}
983b730b   David Mayerich   texture updates
116
  	}
ae407376   David Mayerich   added support for...
117
  
983b730b   David Mayerich   texture updates
118
119
120
  	/// Get the sample spacing for the given dimension
  	F get_spacing(size_t d) {
  		return S[d];
ae407376   David Mayerich   added support for...
121
122
  	}
  
26da82c8   David Mayerich   finalized stim::g...
123
124
125
126
127
128
129
130
131
132
  	/// Get the size of the grid along the specified dimension
  	F size(size_t d){
  		return (F)R[d] * S[d];
  	}
  
  	/// Return the number of samples
  	size_t samples(size_t d){
  		return R[d];
  	}
  
ae407376   David Mayerich   added support for...
133
134
135
  	///Writes the binary data to disk
  
  	/// @param filename is the name of the binary file to be written
8157c392   David Mayerich   added parser and ...
136
137
  	void write(std::string filename){
  
983b730b   David Mayerich   texture updates
138
139
140
  		std::fstream file;		
  		file.open(filename.c_str(), std::ios::out | std::ios::binary);		//open the file as binary for reading		
  		file.write((char *)ptr, samples() * sizeof(T));						//write file to disk
8157c392   David Mayerich   added parser and ...
141
142
  	}
  
ae407376   David Mayerich   added support for...
143
144
145
146
147
  	///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
983b730b   David Mayerich   texture updates
148
149
150
151
152
153
154
155
  	void read(std::string filename, stim::vec<size_t> X, unsigned long header = 0){
  		for(size_t d = 0; d < D; d++)
  			R[d] = X[d];																//set the sample resolution		
  		init();																//allocate space for the data
  		std::fstream file;		
  		file.open(filename.c_str(), std::ios::in | std::ios::binary);		//open the file as binary for writing		
  		file.seekg(header, std::ios::beg);									//seek past the header		
  		file.read((char *)ptr, samples() * sizeof(T));						//read the data
8157c392   David Mayerich   added parser and ...
156
157
  	}
  
ae407376   David Mayerich   added support for...
158
  	///Gets a single value from the grid given a set of coordinates
ae407376   David Mayerich   added support for...
159
  	/// @param x0... is a list of coordinates specifying the desired value
26da82c8   David Mayerich   finalized stim::g...
160
  	/*T get(unsigned long x0, ...){
ae407376   David Mayerich   added support for...
161
  
983b730b   David Mayerich   texture updates
162
  		va_list ap;									//create a variable list
ae407376   David Mayerich   added support for...
163
  		
983b730b   David Mayerich   texture updates
164
165
  		unsigned long F = 1;						//initialize the dimension size to 1
  		unsigned long idx = x0;
ae407376   David Mayerich   added support for...
166
  
983b730b   David Mayerich   texture updates
167
168
169
170
  		va_start(ap, x0);							//start a variable list
  		for(unsigned int d = 1; d<D; d++){			//for each dimension
  			F *= R[d-1];							//get the size of the first dimension
  			idx += va_arg(ap, unsigned int) * F;	//increment the index
ae407376   David Mayerich   added support for...
171
172
173
  		}
  		va_end(ap);
  
983b730b   David Mayerich   texture updates
174
  		return ptr[idx];							//access the appropriate element and return the value
26da82c8   David Mayerich   finalized stim::g...
175
  	}*/
ae407376   David Mayerich   added support for...
176
177
178
179
180
  
  	///Sets a value in the grid
  
  	/// @param value is the grid point value
  	/// @x0... is the coordinate of the value to be set
26da82c8   David Mayerich   finalized stim::g...
181
  	/*void set(T value, unsigned long x0, ...){
983b730b   David Mayerich   texture updates
182
183
184
185
186
187
188
189
  		va_list ap;									//create a variable list		
  		unsigned long F = 1;						//initialize the dimension counter to 1
  		unsigned long idx = x0;						//initialize the index to the first variable
  
  		va_start(ap, x0);							//start the variable list
  		for(unsigned int d = 1; d<D; d++){			//for each dimension
  			F *= R[d - 1];
  			idx += va_arg(ap, unsigned int) * F;	//update the index
ae407376   David Mayerich   added support for...
190
191
  		}
  		va_end(ap);
983b730b   David Mayerich   texture updates
192
  		ptr[idx] = value;							//set the value at the indexed location
26da82c8   David Mayerich   finalized stim::g...
193
  	}*/
ae407376   David Mayerich   added support for...
194
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
195
  
ae407376   David Mayerich   added support for...
196
197
  	///Outputs grid data as a string
  	std::string str(){
8157c392   David Mayerich   added parser and ...
198
  
ae407376   David Mayerich   added support for...
199
  		std::stringstream result;
1d08c377   David Mayerich   added grids subdi...
200
  
ae407376   David Mayerich   added support for...
201
202
203
204
  		result<<"stim::grid structure of size [";
  		for(unsigned int d = 0; d<D; d++){
  			if(d!=0) result<<", ";
  			result<<R[d];
ae407376   David Mayerich   added support for...
205
  		}
ae407376   David Mayerich   added support for...
206
207
208
  		result<<"]"<<std::endl;
  
  		//calculate the number of values to output
256b431c   david   fixed Linux compi...
209
  		unsigned long nV = std::min((unsigned long long)R[0], (unsigned long long)10);
ae407376   David Mayerich   added support for...
210
211
212
213
214
215
216
217
  
  		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...
218
219
220
221
222
  };
  
  }
  
  
8b7be670   David Mayerich   implemented savin...
223
  #endif