#ifndef STIM_GRID_H #define STIM_GRID_H #include #include #include #include #include #include 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: size_t R[D]; //elements in each dimension F S[D]; //spacing between element samples T* ptr; //pointer to the data (on the GPU or CPU) ///Initializes a grid by allocating the necessary memory and setting all values to zero void init(){ 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 } public: ///Default constructor doesn't do anything grid(){ init(); } ///Constructor used to specify the grid size as a vector /// @param _R is a vector describing the grid resolution grid( stim::vec _R){ for (size_t d = 0; d < D; d++) R[d] = _R[d]; init(); } ///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); } void setDim(stim::vec s){ for(size_t d = 0; d < D; d++) S[d] = s[d]; } ///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( size_t X0, ...){ 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 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 } ///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; //create a variable list unsigned long F = 1; //initialize the dimension size to 1 unsigned long idx = x0; va_start(ap, x0); //start a variable list for(unsigned int d = 1; d