grid_data.h 1.51 KB
#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