Commit 983b730befdf0a375d4713fba07be7ff9fef6e0b

Authored by David Mayerich
1 parent 278c622b

texture updates

stim/gl/gl_texture.h
1 1 #ifndef STIM_GL_TEXTURE_H
2 2 #define STIM_GL_TEXTURE_H
3 3  
4   -
5   -
6   -
7   -/*
8   -includes not necessary (yet)
9   -
10   -#include <iterator>
11   -#include <algorithm>
12   -
13   -
14   -*/
15   -
16 4 #include <math.h>
17 5 #include <iostream>
18 6 #include <vector>
19 7 #include "../grids/image_stack.h"
20 8 #include <GL/glut.h>
21   -//#include <GL/glext.h>
22   -#include "./error.h"
  9 +#include <GL/glext.h>
  10 +#include <stim/gl/error.h>
23 11 namespace stim{
24 12  
25 13 /*
... ... @@ -30,22 +18,8 @@ class gl_texture
30 18 template<typename T>
31 19 class gl_texture : public virtual image_stack<T>
32 20 {
33   - private:
34   - /// Sets the internal texture_type, based on the data
35   - /// size. Either 3D, 2D, 1D textures.
36   -
37   - void
38   - setTextureType()
39   - {
40   - if (R[3] > 1)
41   - texture_type = GL_TEXTURE_3D;
42   - else if (R[3] == 1 && R[2] == 0)
43   - texture_type = GL_TEXTURE_1D;
44   - else if (R[3] == 1)
45   - texture_type = GL_TEXTURE_2D;
46   - }
47 21 protected:
48   - std::string path;
  22 + //std::string path;
49 23 GLuint texID; //OpenGL object
50 24 GLenum texture_type; //1D, 2D, 3D
51 25 GLint interpType;
... ... @@ -57,43 +31,56 @@ class gl_texture : public virtual image_stack&lt;T&gt;
57 31 using image_stack<T>::ptr;
58 32 using image_stack<T>::samples;
59 33  
  34 + /// Sets the internal texture_type, based on the data dimensions
  35 + void setTextureType(){
  36 + if (R[3] > 1) //if the third dimension is greater than 1
  37 + texture_type = GL_TEXTURE_3D; //this is a 3D texture
  38 + else if (R[2] > 1) //if the second dimension is greater than 1
  39 + texture_type = GL_TEXTURE_2D; //this is a 2D texture
  40 + else if (R[1] > 1) //if the dimension value is greater than 1
  41 + texture_type = GL_TEXTURE_1D; //this is a 1D texture
  42 + }
  43 +
  44 + //initializes important variables
  45 + void init() {
  46 + texID = 0; //initialize texture ID to zero, default if OpenGL returns an error
  47 + memset(R, 0, sizeof)
  48 + }
  49 +
60 50 public:
61 51  
62 52 ///default constructor
63   - gl_texture()
64   - {
  53 + gl_texture() : image_stack<T>() {
65 54  
66 55 }
67 56  
68 57 ///@param string path to the directory with the image files.
69 58 ///Creates an instance of the gl_texture object with a path to the data.
70 59  
71   - gl_texture(std::string file_path)
72   - {
73   - path = file_path;
74   - image_stack<T>::load_images(path.append("/*.jpg"));
  60 + gl_texture(std::string file_mask){
  61 + image_stack<T>::load_images(file_mask);
75 62 setTextureType();
76 63 }
77 64  
78 65 ///returns the dimentions of the data in the x, y, z directions.
79   - vec<int>
80   - getSize()
81   - {
  66 + vec<int> getSize(){
82 67 stim::vec<int> size(R[1], R[2], R[3]);
83 68 return size;
84 69 }
85 70  
  71 + void getSize(size_t& x, size_t& y, size_t& z) {
  72 + x = R[0]; y = R[1]; z = R[2];
  73 + }
  74 +
86 75 ///@param GLint interp --GL_LINEAR, GL_NEAREST...
87 76 ///@param GLint twrap --GL_REPEAR, GL_CLAMP_TO_EDGE...
88 77 ///@param GLenum dataType --GL_UNSIGNED_BYTE, GL_FLOAT16...
89 78 ///@param GLenum dataFormat--GL_LUMINANCE, GL_RGB...
90 79 /// Texture paramenters.
91   - void
92   - setTexParam(GLint interp = GL_LINEAR,
93   - GLint twrap = GL_CLAMP_TO_EDGE,
94   - GLenum dataType = GL_UNSIGNED_BYTE,
95   - GLenum dataFormat = GL_LUMINANCE)
96   - {
  80 + void setTexParam(GLint interp = GL_LINEAR,
  81 + GLint twrap = GL_CLAMP_TO_EDGE,
  82 + GLenum dataType = GL_UNSIGNED_BYTE,
  83 + GLenum dataFormat = GL_LUMINANCE){
97 84 interpType = interp;
98 85 texWrap = twrap;
99 86 type = dataType;
... ... @@ -104,45 +91,34 @@ class gl_texture : public virtual image_stack&lt;T&gt;
104 91 ///@param y size of the voxel in y direction
105 92 ///@param z size of the voxel in z direction
106 93 /// Sets the dimenstions of the voxels.
107   - void
108   - setDims(float x, float y, float z)
109   - {
110   - S[1] = x;
111   - S[2] = y;
112   - S[3] = z;
  94 + void setSpacing(float sx, float sy, float sz){
  95 + S[1] = sx;
  96 + S[2] = sy;
  97 + S[3] = sz;
113 98 }
114 99  
115 100 ///Returns a stim::vec that contains the x, y, z sizes of the voxel.
116   - vec<float>
117   - getDims()
118   - {
  101 + vec<float> getDims(){
119 102 vec<float> dims(S[1], S[2], S[3]);
120 103 return dims;
121 104 }
122 105  
123   - ///@param file_Path location of the directory with the files
  106 + ///@param file_mask specifies the file(s) to be loaded
124 107 /// Sets the path and calls the loader on that path.
125   - void
126   - setPath(std::string file_path)
127   - {
128   - path = file_path;
129   - image_stack<T>::load_images(path.append("/*.jpg"));
  108 + void load(std::string file_mask){
  109 + image_stack<T>::load_images(file_mask);
130 110 setTextureType();
131 111 }
132 112  
133 113 /// Returns an std::string path associated with an instance of the gl_texture class.
134   - std::string
135   - getPath()
136   - {
137   - return path;
138   - }
  114 + //std::string getPath()
  115 + //{
  116 + // return path;
  117 + //}
139 118  
140 119 /// Returns the GLuint id of the texture created by/associated with the
141   - /// instance of the gl_texture class.
142   -
143   - GLuint
144   - getTexture()
145   - {
  120 + /// instance of the gl_texture class.
  121 + GLuint getTexture(){
146 122 return texID;
147 123 }
148 124  
... ... @@ -152,61 +128,24 @@ class gl_texture : public virtual image_stack&lt;T&gt;
152 128 //TO DO:::add methods for handling the cases of T
153 129 // and convert them to GL equivalent.
154 130 // i.e. an overloaded function that handles paramenter conversion.
155   - void
156   - createTexture()
157   - {
158   - glPixelStorei(GL_UNPACK_ALIGNMENT,1);
  131 + void createTexture(){
  132 + glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
159 133 glGenTextures(1, &texID);
160 134 glBindTexture(texture_type, texID);
161   - glTexParameteri(texture_type,
162   - GL_TEXTURE_MIN_FILTER,
163   - interpType);
164   - glTexParameteri(texture_type,
165   - GL_TEXTURE_MAG_FILTER,
166   - interpType);
167   - switch(texture_type)
168   - {
  135 + glTexParameteri(texture_type, GL_TEXTURE_MIN_FILTER, interpType);
  136 + glTexParameteri(texture_type, GL_TEXTURE_MAG_FILTER, interpType);
  137 + switch(texture_type){
169 138 case GL_TEXTURE_3D:
170   - glTexParameteri(texture_type,
171   - GL_TEXTURE_WRAP_S,texWrap);
172   - // GL_REPEAT);
173   - // GL_CLAMP_TO_EDGE);
174   - glTexParameteri(texture_type,
175   - GL_TEXTURE_WRAP_T,texWrap);
176   - // GL_REPEAT);
177   - // GL_CLAMP_TO_EDGE);
178   - glTexParameteri(texture_type,
179   - GL_TEXTURE_WRAP_R,texWrap);
180   - // GL_REPEAT);
181   - // GL_CLAMP_TO_EDGE);
182   - glTexImage3D(texture_type,
183   - 0,
184   - // GL_RGB16,
185   - 1,
186   - R[1],
187   - R[2],
188   - R[3],
189   - 0,
190   - format,
191   - type,
192   - ptr);
193   - //GL_UNSIGNED_BYTE can be TYPES, convert to GL equivalents
  139 + glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, texWrap);
  140 + glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, texWrap);
  141 + glTexParameteri(texture_type, GL_TEXTURE_WRAP_R, texWrap);
  142 + glTexImage3D(texture_type, 0, 1, R[1], R[2], R[3], 0, format, type, ptr);
194 143 glPixelStorei(GL_PACK_ALIGNMENT,1);
195 144 break;
196 145 case GL_TEXTURE_2D:
197   - glTexParameteri(texture_type,
198   - GL_TEXTURE_WRAP_S, texWrap);
199   - glTexParameteri(texture_type,
200   - GL_TEXTURE_WRAP_T, texWrap);
201   - glTexImage2D(texture_type,
202   - 0,
203   - 1,
204   - R[1],
205   - R[2],
206   - 0,
207   - format,
208   - type,
209   - ptr);
  146 + glTexParameteri(texture_type, GL_TEXTURE_WRAP_S, texWrap);
  147 + glTexParameteri(texture_type, GL_TEXTURE_WRAP_T, texWrap);
  148 + glTexImage2D(texture_type, 0, 1, R[1], R[2], 0, format, type, ptr);
210 149 break;
211 150 }
212 151 }
... ...
stim/grids/grid.h
... ... @@ -15,78 +15,80 @@ namespace stim{
15 15 Functions are provided for saving and loading binary data.
16 16  
17 17 **/
18   -template<typename T, unsigned int D = 1>
  18 +template<typename T, unsigned int D = 1, typename F = float>
19 19 class grid{
20 20  
21 21 protected:
22 22  
23   - stim::vec<unsigned long> R; //elements in each dimension
24   - stim::vec<float> S;
  23 + size_t R[D]; //elements in each dimension
  24 + F S[D]; //spacing between element samples
25 25 T* ptr; //pointer to the data (on the GPU or CPU)
26 26  
27 27 ///Return the total number of values in the binary file
28   - unsigned long samples(){
29   -
30   - unsigned long s = 1;
31   - for(unsigned int d = 0; d < D; d++)
  28 + size_t samples(){
  29 + size_t s = 1;
  30 + for(size_t d = 0; d < D; d++)
32 31 s *= R[d];
33   -
34 32 return s;
35 33 }
36 34  
37 35 ///Initializes a grid by allocating the necessary memory and setting all values to zero
38   - void init(){
39   -
40   - //calculate the total number of values
41   - unsigned long S = samples();
42   -
43   - //allocate memory to store the grid
44   - ptr = (T*)malloc(sizeof(T) * S);
45   -
46   - //initialize the memory to zero
47   - memset(ptr, 0, sizeof(T) * S);
48   -
  36 + void init(){
  37 + size_t N = samples(); //calculate the total number of values
  38 + ptr = (T*)calloc(sizeof(T) * N); //allocate memory to store the grid
49 39 }
50 40  
51 41 public:
52 42  
53 43 ///Default constructor doesn't do anything
54 44 grid(){
55   - ptr = NULL; //set the pointer to NULL so that we know nothing is allocated
  45 + memset(R, 0, sizeof(size_t) * D); //initialize the grid dimensions to zero
  46 + memset(S, 0, sizeof(F) * D); //initialize the grid size to zero
  47 + ptr = NULL; //set the data pointer to NULL
56 48 }
57 49  
58 50 ///Constructor used to specify the grid size as a vector
59 51  
60 52 /// @param _R is a vector describing the grid resolution
61   - grid( stim::vec<unsigned long> _R){
62   -
63   - //set the grid resolution
64   - R = _R;
65   -
  53 + grid( stim::vec<size_t> _R){
  54 + for (size_t d = 0; d < D; d++)
  55 + R[d] = _R[d];
66 56 init();
67 57 }
68 58  
69 59 void
70   - setDim(stim::vec<float> s)
71   - {
72   - S = s;
  60 + setDim(stim::vec<float> s){
  61 + for(size_t d = 0; d < D; d++)
  62 + S[d] = s[d];
73 63 }
74 64  
75 65 ///Constructor used to specify the grid size as a set of parameters
76   -
77 66 /// @param X0... is a list of values describing the grid size along each dimension
78   - grid( unsigned long X0, ...){
79   -
80   - R[0] = X0;
81   -
82   - va_list ap;
83   - va_start(ap, X0);
84   - for(unsigned int d = 1; d<D; d++)
85   - R[d] = va_arg(ap, unsigned long);
  67 + grid( size_t X0, ...){
  68 + R[0] = X0; //set the grid size of the first dimension
  69 + va_list ap; //get a variable list
  70 + va_start(ap, X0); //start the variable list at the first element
  71 + for(size_t d = 1; d<D; d++) //for each additional element
  72 + R[d] = va_arg(ap, size_t); //read the value from the variable list as a size_t
86 73 va_end(ap);
  74 + init(); //initialize the grid
  75 + }
87 76  
88   - init();
  77 + ///Set the spacing between grid sample points
  78 + /// @param X0... is a list of values describing the grid sample spacing
  79 + void spacing(F X0, ...) {
  80 + S[0] = X0; //set the grid size of the first dimension
  81 + va_list ap; //get a variable list
  82 + va_start(ap, X0); //start the variable list at the first element
  83 + for (size_t d = 1; d<D; d++) //for each additional element
  84 + S[d] = va_arg(ap, size_t); //read the value from the variable list as a size_t
  85 + va_end(ap);
  86 + init(); //initialize the grid
  87 + }
89 88  
  89 + /// Get the sample spacing for the given dimension
  90 + F get_spacing(size_t d) {
  91 + return S[d];
90 92 }
91 93  
92 94 ///Writes the binary data to disk
... ... @@ -94,13 +96,9 @@ public:
94 96 /// @param filename is the name of the binary file to be written
95 97 void write(std::string filename){
96 98  
97   - std::fstream file;
98   -
99   - //open the file as binary for reading
100   - file.open(filename.c_str(), std::ios::out | std::ios::binary);
101   -
102   - //write file to disk
103   - file.write((char *)ptr, samples() * sizeof(T));
  99 + std::fstream file;
  100 + file.open(filename.c_str(), std::ios::out | std::ios::binary); //open the file as binary for reading
  101 + file.write((char *)ptr, samples() * sizeof(T)); //write file to disk
104 102 }
105 103  
106 104 ///Loads a binary file from disk
... ... @@ -108,44 +106,33 @@ public:
108 106 /// @param filename is the name of the file containing the binary data
109 107 /// @param S is the size of the binary file along each dimension
110 108 /// @param header is the size of the header in bytes
111   - void read(std::string filename, stim::vec<unsigned long> S, unsigned long header = 0){
112   -
113   - R = S; //set the sample resolution
114   -
115   - //allocate space for the data
116   - init();
117   -
118   - std::fstream file;
119   -
120   - //open the file as binary for writing
121   - file.open(filename.c_str(), std::ios::in | std::ios::binary);
122   -
123   - //seek past the header
124   - file.seekg(header, std::ios::beg);
125   -
126   -
127   - //read the data
128   - file.read((char *)ptr, samples() * sizeof(T));
  109 + void read(std::string filename, stim::vec<size_t> X, unsigned long header = 0){
  110 + for(size_t d = 0; d < D; d++)
  111 + R[d] = X[d]; //set the sample resolution
  112 + init(); //allocate space for the data
  113 + std::fstream file;
  114 + file.open(filename.c_str(), std::ios::in | std::ios::binary); //open the file as binary for writing
  115 + file.seekg(header, std::ios::beg); //seek past the header
  116 + file.read((char *)ptr, samples() * sizeof(T)); //read the data
129 117 }
130 118  
131 119 ///Gets a single value from the grid given a set of coordinates
132   -
133 120 /// @param x0... is a list of coordinates specifying the desired value
134 121 T get(unsigned long x0, ...){
135 122  
136   - va_list ap;
  123 + va_list ap; //create a variable list
137 124  
138   - unsigned long F = 1;
139   - unsigned long p = x0;
  125 + unsigned long F = 1; //initialize the dimension size to 1
  126 + unsigned long idx = x0;
140 127  
141   - va_start(ap, x0);
142   - for(unsigned int d = 1; d<D; d++){
143   - F *= R[d-1];
144   - p += va_arg(ap, unsigned int) * F;
  128 + va_start(ap, x0); //start a variable list
  129 + for(unsigned int d = 1; d<D; d++){ //for each dimension
  130 + F *= R[d-1]; //get the size of the first dimension
  131 + idx += va_arg(ap, unsigned int) * F; //increment the index
145 132 }
146 133 va_end(ap);
147 134  
148   - return ptr[p];
  135 + return ptr[idx]; //access the appropriate element and return the value
149 136 }
150 137  
151 138 ///Sets a value in the grid
... ... @@ -153,20 +140,17 @@ public:
153 140 /// @param value is the grid point value
154 141 /// @x0... is the coordinate of the value to be set
155 142 void set(T value, unsigned long x0, ...){
156   -
157   - va_list ap;
158   -
159   - unsigned long F = 1;
160   - unsigned long p = x0;
161   -
162   - va_start(ap, x0);
163   - for(unsigned int d = 1; d<D; d++){
164   - F *= R[d-1];
165   - p += va_arg(ap, unsigned int) * F;
  143 + va_list ap; //create a variable list
  144 + unsigned long F = 1; //initialize the dimension counter to 1
  145 + unsigned long idx = x0; //initialize the index to the first variable
  146 +
  147 + va_start(ap, x0); //start the variable list
  148 + for(unsigned int d = 1; d<D; d++){ //for each dimension
  149 + F *= R[d - 1];
  150 + idx += va_arg(ap, unsigned int) * F; //update the index
166 151 }
167 152 va_end(ap);
168   -
169   - ptr[p] = value;
  153 + ptr[idx] = value; //set the value at the indexed location
170 154 }
171 155  
172 156  
... ... @@ -179,9 +163,7 @@ public:
179 163 for(unsigned int d = 0; d<D; d++){
180 164 if(d!=0) result<<", ";
181 165 result<<R[d];
182   -
183 166 }
184   -
185 167 result<<"]"<<std::endl;
186 168  
187 169 //calculate the number of values to output
... ...
stim/grids/image_stack.h
... ... @@ -8,9 +8,8 @@
8 8  
9 9 namespace stim{
10 10  
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   -**/
  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.
14 13 template<typename T>
15 14 class image_stack : public virtual stim::grid<T, 4>{
16 15  
... ... @@ -24,20 +23,17 @@ protected:
24 23 using stim::grid<T, 4>::read;
25 24  
26 25 public:
  26 + //default constructor
  27 + image_stack() : grid<T, 4>() {
27 28  
28   - ///Load an image stack based on a file mask. Images are loaded in alphanumeric order.
  29 + }
29 30  
  31 + ///Load an image stack based on a file mask. Images are loaded in alphanumeric order
30 32 /// @param file_mask is the mask describing images to be loaded
31 33 void load_images(std::string file_mask){
32 34  
33 35 stim::filename file_path(file_mask);
34 36  
35   - //if the file path is relative, update it with the current working directory
36   -// if(file_path.is_relative()){
37   -// stim::filename wd = stim::filename::cwd();
38   -// file_path = wd.get_relative(file_mask);
39   -// }
40   -
41 37 //get the list of files
42 38 std::vector<stim::filename> file_list = file_path.get_list();
43 39  
... ... @@ -50,7 +46,6 @@ public:
50 46 std::cout << file_list[i].str() << std::endl;
51 47  
52 48 //load the first image and set all of the image_stack properties
53   -// std::cout<<"File to Load: "<<file_list[0].str()<<std::endl;
54 49 stim::image<T> I(file_list[0].str());
55 50  
56 51 //set the image resolution and number of channels
... ... @@ -63,37 +58,25 @@ public:
63 58 ptr = (T*)malloc(sizeof(T) * samples());
64 59  
65 60 //load and copy each image into the grid
66   - for(unsigned int i = 0; i<R[3]; i++){
67   -
68   -// std::cout<<"File to Load: "<<file_list[i].str()<<std::endl;
69   - //load the image
70   - stim::image<T> I(file_list[i].str());
71   -
72   - //retrieve the interlaced data from the image - store it in the grid
73   - I.get_interleaved_rgb(&ptr[ i * R[0] * R[1] * R[2] ]);
74   -
  61 + for(unsigned int i = 0; i<R[3]; i++){
  62 + stim::image<T> I(file_list[i].str()); //load the image
  63 + I.get_interleaved_rgb(&ptr[ i * R[0] * R[1] * R[2] ]); //retrieve the interlaced data from the image - store it in the grid
75 64 }
76 65 }
77 66  
78 67 ///Inserts image I into slot i.
79 68 /// @param stim::image<T> I; image to insert.
80 69 /// @int I, where to place the image.
81   - void insert_image(stim::image<T> I, int i)
82   - {
  70 + void insert_image(stim::image<T> I, int i){
83 71 I.get_interleaved_rgb(&ptr[i *R[0] *R[1] *R[2] ]);
84 72 }
85 73  
86 74 ///Saves a single page to an image file
87 75 /// @param file_name is the name of the image file to be created
88 76 /// @param i is the page to be saved
89   - void save_image(std::string file_name, unsigned int i){
90   -
91   - //create an image
92   - stim::image<T> I;
93   -
94   - //retrieve the interlaced data from the image - store it in the grid
95   - I.set_interleaved_rgb(&ptr[ i * R[0] * R[1] * R[2] ], R[1], R[2], R[0]);
96   -
  77 + void save_image(std::string file_name, unsigned int i){
  78 + stim::image<T> I; //create an image
  79 + I.set_interleaved_rgb(&ptr[ i * R[0] * R[1] * R[2] ], R[1], R[2], R[0]); //retrieve the interlaced data from the image - store it in the grid
97 80 I.save(file_name);
98 81 }
99 82  
... ... @@ -133,12 +116,6 @@ public:
133 116  
134 117 stim::filename file_path(file_mask);
135 118  
136   - //if the file path is relative, update it with the current working directory
137   -// if(file_path.is_relative()){
138   -// stim::filename wd = stim::filename::cwd();
139   -// file_path = wd.get_relative(file_mask);
140   -// }
141   -
142 119 //create a list of file names
143 120 std::vector<std::string> file_list = stim::wildcards::increment(file_path.str(), 0, R[3]-1, 1);
144 121  
... ...
stim/visualization/camera.h
... ... @@ -186,6 +186,7 @@ public:
186 186 d = vec3<float>(0, 0, 1);
187 187 up = vec3<float>(0, 1, 0);
188 188 focus = 1;
  189 + fov = 60;
189 190  
190 191 }
191 192  
... ...