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