#ifndef STIM_GRID_H #define STIM_GRID_H #include #include #include #include #include #include "../math/vector.h" namespace stim{ /**This object describes a generic D-dimensional grid containing data of type T. Functions are provided for saving and loading binary data. **/ template class grid{ protected: stim::vec 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; } ///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); } public: ///Default constructor doesn't do anything grid(){} ///Constructor used to specify the grid size as a vector /// @param _R is a vector describing the grid resolution grid( stim::vec _R){ //set the grid resolution R = _R; init(); } ///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 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)); } ///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