Blame view

grids/grid_data.h 1.51 KB
1d08c377   David Mayerich   added grids subdi...
1
2
3
4
5
6
  #ifndef STIM_GRID_DATA_CUH
  #define STIM_GRID_DATA_CUH
  
  #include <vector>
  #include <string>
  #include <sstream>
8b7be670   David Mayerich   implemented savin...
7
  #include <fstream>
1d08c377   David Mayerich   added grids subdi...
8
9
10
11
  
  #include "../cuda/threads.h"
  #include "../cuda/error.h"
  #include "../cuda/devices.h"
8157c392   David Mayerich   added parser and ...
12
  #include "../math/vector.h"
1d08c377   David Mayerich   added grids subdi...
13
14
15
16
17
  
  
  namespace stim{
  
  //This object describes a generic D-dimensional grid containing data of type T
1d08c377   David Mayerich   added grids subdi...
18
19
20
21
22
23
24
  	// data can be loaded in the form of images
  	// data can be saved in the form of binary files
  template<typename T, unsigned int D = 1>
  class grid_data{
  
  protected:
  
8b7be670   David Mayerich   implemented savin...
25
  	stim::vec<unsigned long, D> R;		//elements in each dimension
8157c392   David Mayerich   added parser and ...
26
  	T* ptr;									//pointer to the data (on the GPU or CPU)
8157c392   David Mayerich   added parser and ...
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  
  	//return the total number of values in the binary file
  	unsigned long samples(){
  
  		unsigned long s = 1;
  		for(unsigned int d = 0; d < D; d++)
  			s *= R[d];
  
  		return s;
  
  	}
  
  public:
  
  	//write data to disk
  	void write(std::string filename){
  
8b7be670   David Mayerich   implemented savin...
44
45
  		std::fstream file;
  
8157c392   David Mayerich   added parser and ...
46
47
48
49
  		//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...
50
  		file.write((char *)ptr, samples() * sizeof(T));
8157c392   David Mayerich   added parser and ...
51
52
53
54
  	}
  
  	//load a binary file from disk
  	//		header size is in bytes
8b7be670   David Mayerich   implemented savin...
55
  	void read(std::string filename, stim::vec<unsigned long, D> S, unsigned long header = 0){
8157c392   David Mayerich   added parser and ...
56
57
58
  
  		R = S;	//set the sample resolution
  
8b7be670   David Mayerich   implemented savin...
59
60
  		std::fstream file;
  
8157c392   David Mayerich   added parser and ...
61
62
63
64
65
66
67
  		//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);
  
  		//read the data
8b7be670   David Mayerich   implemented savin...
68
  		file.read((char *)ptr, samples() * sizeof(T));
8157c392   David Mayerich   added parser and ...
69
70
71
  	}
  
  
1d08c377   David Mayerich   added grids subdi...
72
73
74
75
76
77
  
  };
  
  }
  
  
8b7be670   David Mayerich   implemented savin...
78
  #endif