Commit ae40737672c54c90c23e60e5224dc023cbe7c1f6

Authored by David Mayerich
1 parent 26045f19

added support for variable length arguments in grid.h

Showing 2 changed files with 129 additions and 17 deletions   Show diff stats
stim/grids/grid.h
... ... @@ -5,18 +5,16 @@
5 5 #include <string>
6 6 #include <sstream>
7 7 #include <fstream>
  8 +#include <cstdarg>
8 9  
9   -#include "../cuda/threads.h"
10   -#include "../cuda/error.h"
11   -#include "../cuda/devices.h"
12 10 #include "../math/vector.h"
13 11  
14   -
15 12 namespace stim{
16 13  
17   -//This object describes a generic D-dimensional grid containing data of type T
18   - // data can be loaded in the form of images
19   - // data can be saved in the form of binary files
  14 +/**This object describes a generic D-dimensional grid containing data of type T.
  15 + Functions are provided for saving and loading binary data.
  16 +
  17 +**/
20 18 template<typename T, unsigned int D = 1>
21 19 class grid{
22 20  
... ... @@ -25,7 +23,7 @@ protected:
25 23 stim::vec<unsigned long, D> R; //elements in each dimension
26 24 T* ptr; //pointer to the data (on the GPU or CPU)
27 25  
28   - //return the total number of values in the binary file
  26 + ///Return the total number of values in the binary file
29 27 unsigned long samples(){
30 28  
31 29 unsigned long s = 1;
... ... @@ -33,12 +31,55 @@ protected:
33 31 s *= R[d];
34 32  
35 33 return s;
  34 + }
  35 +
  36 + ///Initializes a grid by allocating the necessary memory and setting all values to zero
  37 + void init(){
  38 +
  39 + //calculate the total number of values
  40 + unsigned long S = samples();
  41 +
  42 + //allocate memory to store the grid
  43 + ptr = (T*)malloc(sizeof(T) * S);
  44 +
  45 + //initialize the memory to zero
  46 + memset(ptr, 0, sizeof(T) * S);
36 47  
37 48 }
38 49  
39 50 public:
40 51  
41   - //write data to disk
  52 + ///Constructor used to specify the grid size as a vector
  53 +
  54 + /// @param _R is a vector describing the grid resolution
  55 + grid( stim::vec<unsigned long, D> _R){
  56 +
  57 + //set the grid resolution
  58 + R = _R;
  59 +
  60 + init();
  61 + }
  62 +
  63 + ///Constructor used to specify the grid size as a set of parameters
  64 +
  65 + /// @param X0... is a list of values describing the grid size along each dimension
  66 + grid( unsigned long X0, ...){
  67 +
  68 + R[0] = X0;
  69 +
  70 + va_list ap;
  71 + va_start(ap, X0);
  72 + for(unsigned int d = 1; d<D; d++)
  73 + R[d] = va_arg(ap, unsigned long);
  74 + va_end(ap);
  75 +
  76 + init();
  77 +
  78 + }
  79 +
  80 + ///Writes the binary data to disk
  81 +
  82 + /// @param filename is the name of the binary file to be written
42 83 void write(std::string filename){
43 84  
44 85 std::fstream file;
... ... @@ -50,8 +91,11 @@ public:
50 91 file.write((char *)ptr, samples() * sizeof(T));
51 92 }
52 93  
53   - //load a binary file from disk
54   - // header size is in bytes
  94 + ///Loads a binary file from disk
  95 +
  96 + /// @param filename is the name of the file containing the binary data
  97 + /// @param S is the size of the binary file along each dimension
  98 + /// @param header is the size of the header in bytes
55 99 void read(std::string filename, stim::vec<unsigned long, D> S, unsigned long header = 0){
56 100  
57 101 R = S; //set the sample resolution
... ... @@ -68,8 +112,69 @@ public:
68 112 file.read((char *)ptr, samples() * sizeof(T));
69 113 }
70 114  
  115 + ///Gets a single value from the grid given a set of coordinates
  116 +
  117 + /// @param x0... is a list of coordinates specifying the desired value
  118 + T get(unsigned long x0, ...){
  119 +
  120 + va_list ap;
  121 +
  122 + unsigned long F = 1;
  123 + unsigned long p = x0;
  124 +
  125 + va_start(ap, x0);
  126 + for(unsigned int d = 1; d<D; d++){
  127 + F *= R[d-1];
  128 + p += va_arg(ap, unsigned int) * F;
  129 + }
  130 + va_end(ap);
  131 +
  132 + return ptr[p];
  133 + }
  134 +
  135 + ///Sets a value in the grid
  136 +
  137 + /// @param value is the grid point value
  138 + /// @x0... is the coordinate of the value to be set
  139 + void set(T value, unsigned long x0, ...){
  140 +
  141 + va_list ap;
  142 +
  143 + unsigned long F = 1;
  144 + unsigned long p = x0;
  145 +
  146 + va_start(ap, x0);
  147 + for(unsigned int d = 1; d<D; d++){
  148 + F *= R[d-1];
  149 + p += va_arg(ap, unsigned int) * F;
  150 + }
  151 + va_end(ap);
  152 +
  153 + ptr[p] = value;
  154 + }
  155 +
  156 + ///Outputs grid data as a string
  157 + std::string str(){
71 158  
  159 + std::stringstream result;
72 160  
  161 + result<<"stim::grid structure of size [";
  162 + for(unsigned int d = 0; d<D; d++){
  163 + if(d!=0) result<<", ";
  164 + result<<R[d];
  165 + }
  166 + result<<"]"<<std::endl;
  167 +
  168 + //calculate the number of values to output
  169 + unsigned long nV = min(R[0], (unsigned long)10);
  170 +
  171 + for(unsigned long v = 0; v<nV; v++){
  172 + result<<ptr[v];
  173 + if(v != nV-1) result<<", ";
  174 + }
  175 +
  176 + return result.str();
  177 + }
73 178 };
74 179  
75 180 }
... ...
stim/grids/image_stack.h
... ... @@ -8,8 +8,9 @@
8 8  
9 9 namespace stim{
10 10  
11   -//this creates a class that can be used to load 3D grid data from stacks of images
12   -// The class uses a 4D grid object, where the first dimension is color
  11 +/**This class is used to load 3D grid data from stacks of images
  12 + The class uses a 4D grid object, where the first dimension is a color value.
  13 +**/
13 14 template<typename T>
14 15 class image_stack : public virtual stim::grid<T, 4>{
15 16  
... ... @@ -22,6 +23,9 @@ protected:
22 23  
23 24 public:
24 25  
  26 + ///Load an image stack based on a file mask. Images are loaded in alphanumeric order.
  27 +
  28 + /// @param file_mask is the mask describing images to be loaded
25 29 void load_images(std::string file_mask){
26 30  
27 31 stim::filename file_path(file_mask);
... ... @@ -65,6 +69,10 @@ public:
65 69 }
66 70 }
67 71  
  72 + ///Saves a single page to an image file
  73 +
  74 + /// @param file_name is the name of the image file to be created
  75 + /// @param i is the page to be saved
68 76 void save_image(std::string file_name, unsigned int i){
69 77  
70 78 //create an image
... ... @@ -76,6 +84,9 @@ public:
76 84 I.save(file_name);
77 85 }
78 86  
  87 + ///Saves the entire stack to a set of images
  88 +
  89 + /// @param file_mask is the mask describing how the file names will be saved (ex. image????.bmp)
79 90 void save_images(std::string file_mask){
80 91  
81 92 stim::filename file_path(file_mask);
... ... @@ -93,10 +104,6 @@ public:
93 104 save_image(file_list[i], i);
94 105 }
95 106  
96   -
97   -
98   -
99   -
100 107 };
101 108  
102 109  
... ...