Commit ad9345728682e41b55856cb4f4572bba8738096a
Merge branch 'master' of git.stim.ee.uh.edu:codebase/stimlib
Showing
9 changed files
with
685 additions
and
748 deletions
Show diff stats
stim/biomodels/fiber.h renamed to stim/biomodels/centerline.h
1 | -#ifndef STIM_FIBER_H | |
2 | -#define STIM_FIBER_H | |
1 | +#ifndef STIM_CENTERLINE_H | |
2 | +#define STIM_CENTERLINE_H | |
3 | 3 | |
4 | 4 | #include <vector> |
5 | -#include <ANN/ANN.h> | |
5 | +#include <stim/math/vec3.h> | |
6 | +//#include <ANN/ANN.h> | |
6 | 7 | |
7 | 8 | namespace stim{ |
8 | 9 | |
... | ... | @@ -11,22 +12,19 @@ namespace stim{ |
11 | 12 | * class to describe an interconnected (often biological) network. |
12 | 13 | */ |
13 | 14 | template<typename T> |
14 | -class fiber{ | |
15 | +class centerline{ | |
15 | 16 | |
16 | 17 | protected: |
17 | 18 | unsigned int N; //number of points in the fiber |
18 | 19 | double **c; //centerline (array of double pointers) |
19 | - | |
20 | - T* r; // array of fiber radii | |
21 | - ANNkd_tree* kdt; //kd-tree stores all points in the fiber for fast searching | |
20 | +// ANNkd_tree* kdt; //kd-tree stores all points in the fiber for fast searching | |
22 | 21 | |
23 | 22 | /// Initialize an empty fiber |
24 | 23 | void init() |
25 | 24 | { |
26 | - kdt = NULL; | |
27 | - c=NULL; | |
28 | - r=NULL; | |
29 | 25 | N=0; |
26 | + c=NULL; | |
27 | +// kdt = NULL; | |
30 | 28 | } |
31 | 29 | |
32 | 30 | /// Initialize a fiber with N centerline points (all located at [0, 0, 0] with radius 0) |
... | ... | @@ -34,19 +32,17 @@ protected: |
34 | 32 | { |
35 | 33 | |
36 | 34 | N = n; //set the number of points |
37 | - kdt = NULL; | |
35 | +// kdt = NULL; | |
38 | 36 | c = (double**) malloc(sizeof(double*) * N); //allocate the array pointer |
39 | 37 | |
40 | 38 | for(unsigned int i = 0; i < N; i++) //allocate space for each point |
41 | 39 | c[i] = (double*) malloc(sizeof(double) * 3); |
42 | - | |
43 | - r = (T*) malloc(sizeof(T) * N); //allocate space for the radii | |
44 | 40 | } |
45 | 41 | |
46 | 42 | /// Copies an existing fiber to the current fiber |
47 | 43 | |
48 | 44 | /// @param cpy stores the new copy of the fiber |
49 | - void copy( const stim::fiber<T>& cpy ){ | |
45 | + void copy( const stim::centerline<T>& cpy, bool kd = 0){ | |
50 | 46 | |
51 | 47 | ///allocate space for the new fiber |
52 | 48 | init(cpy.N); |
... | ... | @@ -55,20 +51,18 @@ protected: |
55 | 51 | for(unsigned int i = 0; i < N; i++){ |
56 | 52 | for(unsigned int d = 0; d < 3; d++) //for each dimension |
57 | 53 | c[i][d] = cpy.c[i][d]; //copy the coordinate |
58 | - | |
59 | - r[i] = cpy.r[i]; //copy the radius | |
60 | 54 | } |
61 | - | |
62 | - gen_kdtree(); //generate the kd tree for the new fiber | |
55 | +// if(kd) | |
56 | +// gen_kdtree(); //generate the kd tree for the new fiber | |
63 | 57 | } |
64 | 58 | |
65 | 59 | /// generate a KD tree for points on fiber |
66 | - void gen_kdtree() | |
67 | - { | |
68 | - int n_data = N; //create an array of data points | |
69 | - ANNpointArray pts = (ANNpointArray)c; //cast the centerline list to an ANNpointArray | |
70 | - kdt = new ANNkd_tree(pts, n_data, 3); //build a KD tree | |
71 | - } | |
60 | +// void gen_kdtree() | |
61 | +// { | |
62 | +// int n_data = N; //create an array of data points | |
63 | +// ANNpointArray pts = (ANNpointArray)c; //cast the centerline list to an ANNpointArray | |
64 | +// kdt = new ANNkd_tree(pts, n_data, 3); //build a KD tree | |
65 | +// } | |
72 | 66 | |
73 | 67 | /// find distance between two points |
74 | 68 | double dist(double* p0, double* p1){ |
... | ... | @@ -79,7 +73,6 @@ protected: |
79 | 73 | { |
80 | 74 | v = p1[d] - p0[d]; |
81 | 75 | sum +=v * v; |
82 | - | |
83 | 76 | } |
84 | 77 | return sqrt(sum); |
85 | 78 | } |
... | ... | @@ -87,21 +80,18 @@ protected: |
87 | 80 | /// This function retreives the index for the fiber point closest to q |
88 | 81 | |
89 | 82 | /// @param q is a reference point used to find the closest point on the fiber center line |
90 | - unsigned int ann( stim::vec<double> q ){ | |
91 | - | |
92 | - ANNidxArray idx = new ANNidx[1]; //variable used to hold the nearest point | |
93 | - ANNdistArray sq_dist = new ANNdist[1]; //variable used to hold the squared distance to the nearest point | |
94 | - | |
95 | - kdt->annkSearch(q.data(), 1, idx, sq_dist); //search the KD tree for the nearest neighbor | |
96 | - | |
97 | - return *idx; | |
98 | - } | |
83 | +// unsigned int ann( stim::vec<double> q ){ | |
84 | +// ANNidxArray idx = new ANNidx[1]; //variable used to hold the nearest point | |
85 | +// ANNdistArray sq_dist = new ANNdist[1]; //variable used to hold the squared distance to the nearest point | |
86 | +// kdt->annkSearch(q.data(), 1, idx, sq_dist); //search the KD tree for the nearest neighbor | |
87 | +// return *idx; | |
88 | +// } | |
99 | 89 | |
100 | 90 | /// Returns a stim::vec representing the point at index i |
101 | 91 | |
102 | 92 | /// @param i is an index of the desired centerline point |
103 | 93 | stim::vec<T> get_vec(unsigned i){ |
104 | - stim::vec<T> r; | |
94 | + stim::vec3<T> r; | |
105 | 95 | r.resize(3); |
106 | 96 | r[0] = c[i][0]; |
107 | 97 | r[1] = c[i][1]; |
... | ... | @@ -113,25 +103,23 @@ protected: |
113 | 103 | |
114 | 104 | public: |
115 | 105 | |
116 | - fiber(){ | |
106 | + centerline(){ | |
117 | 107 | init(); |
118 | 108 | } |
119 | 109 | |
120 | 110 | /// Copy constructor |
121 | - fiber(const stim::fiber<T> &obj){ | |
122 | - | |
111 | + centerline(const stim::centerline<T> &obj){ | |
123 | 112 | copy(obj); |
124 | - | |
125 | 113 | } |
126 | 114 | |
127 | 115 | //temp constructor for graph visualization |
128 | - fiber(int n) | |
116 | + centerline(int n) | |
129 | 117 | { |
130 | 118 | init(n); |
131 | 119 | } |
132 | 120 | |
133 | 121 | /// Constructor takes a list of stim::vec points, the radius at each point is set to zero |
134 | - fiber(std::vector< stim::vec<T> > p){ | |
122 | + centerline(std::vector< stim::vec<T> > p, bool kd = 0){ | |
135 | 123 | init(p.size()); //initialize the fiber |
136 | 124 | |
137 | 125 | //for each point, set the centerline position and radius |
... | ... | @@ -142,200 +130,77 @@ public: |
142 | 130 | c[i][d] = (double) p[i][d]; |
143 | 131 | |
144 | 132 | //set the radius |
145 | - r[i] = 0; | |
146 | 133 | } |
147 | - | |
148 | 134 | //generate a kd tree |
149 | - gen_kdtree(); | |
135 | +// if(kd) | |
136 | +// gen_kdtree(); | |
150 | 137 | } |
151 | 138 | |
152 | - /// constructor takes a list of points and radii | |
153 | - fiber(std::vector< stim::vec< T > > pos, std::vector< T > radii){ | |
139 | + /// constructor takes a list of points | |
140 | + centerline(std::vector< stim::vec3< T > > pos, bool kd = 0){ | |
154 | 141 | init(pos.size()); //initialize the fiber |
155 | 142 | |
156 | 143 | //for each point, set the centerline position and radius |
157 | 144 | for(unsigned int i = 0; i < N; i++){ |
158 | - | |
159 | 145 | //set the centerline position |
160 | 146 | for(unsigned int d = 0; d < 3; d++) |
161 | 147 | c[i][d] = (double) pos[i][d]; |
162 | - | |
163 | 148 | //set the radius |
164 | - r[i] = radii[i]; | |
165 | 149 | } |
166 | 150 | |
167 | 151 | //generate a kd tree |
168 | - gen_kdtree(); | |
169 | - } | |
170 | - | |
171 | - /// constructor takes an array of points and radii | |
172 | - // this function is used when the radii are represented as a stim::vec, | |
173 | - // since this may be easier when importing OBJs | |
174 | - fiber(std::vector< stim::vec<T> > pos, std::vector< stim::vec<T> > radii){ | |
175 | - | |
176 | - init(pos.size()); | |
177 | - | |
178 | - //for each point, set the position and radius | |
179 | - for(unsigned int i = 0; i < N; i++){ | |
180 | - //at(i) = (double*)malloc(sizeof(double) * 3); | |
181 | - for(unsigned int d = 0; d < 3; d++) | |
182 | - c[i][d] = (double) pos[i][d]; | |
183 | - | |
184 | - r[i] = radii[i][(unsigned int)0]; | |
185 | - } | |
186 | - | |
187 | - gen_kdtree(); | |
152 | + //if(kd) | |
153 | + // gen_kdtree(); | |
188 | 154 | } |
189 | 155 | |
190 | 156 | /// Assignment operation |
191 | - fiber& operator=(const fiber &rhs){ | |
192 | - | |
157 | + centerline& operator=(const centerline &rhs){ | |
193 | 158 | if(this == &rhs) return *this; //test for and handle self-assignment |
194 | - | |
195 | 159 | copy(rhs); |
160 | + return *this; | |
196 | 161 | } |
197 | 162 | |
198 | - /// Calculate the length of the fiber and return it. | |
199 | - double length(){ | |
200 | - | |
201 | - double* p0; | |
202 | - double *p1; | |
203 | - double l = 0; //initialize the length to zero | |
204 | - | |
205 | - //for each point | |
206 | - //typename std::list< point<T> >::iterator i; //create a point iterator | |
207 | - for(unsigned int i = 0; i < N; i++){ //for each point in the fiber | |
208 | - | |
209 | - if(i == 0) //if this is the first point, just store it | |
210 | - p1 = c[0]; | |
211 | - else{ //if this is any other point | |
212 | - p0 = p1; //shift p1->p0 | |
213 | - p1 = c[i]; //set p1 to the new point | |
214 | - l += dist(p0, p1); //add the length of p1 - p0 to the running sum | |
215 | - } | |
216 | - } | |
217 | - | |
218 | - return l; //return the length | |
219 | - } | |
220 | - | |
221 | - /// Calculates the length and average radius of the fiber | |
222 | - | |
223 | - /// @param length is filled with the fiber length | |
224 | - T radius(T& length){ | |
225 | - | |
226 | - double* p0; //temporary variables to store point positions | |
227 | - double* p1; | |
228 | - T r0, r1; //temporary variables to store radii at points | |
229 | - double l; | |
230 | - T r_mean; //temporary variable to store the length and average radius of a fiber segment | |
231 | - double length_sum = 0; //initialize the length to zero | |
232 | - T radius_sum = 0; //initialize the radius sum to zero | |
233 | - | |
234 | - //for each point | |
235 | - //typename std::list< point<T> >::iterator i; //create a point iterator | |
236 | - for(unsigned int i = 0; i < N; i++){ //for each point in the fiber | |
237 | - | |
238 | - if(i == 0){ //if this is the first point, just store it | |
239 | - p1 = c[0]; | |
240 | - r1 = r[0]; | |
241 | - } | |
242 | - else{ //if this is any other point | |
243 | - p0 = p1; //shift p1->p0 and r1->r0 | |
244 | - r0 = r1; | |
245 | - p1 = c[i]; //set p1 to the new point | |
246 | - r1 = r[i]; | |
247 | - | |
248 | - l = dist(p0, p1); //calculate the length of the p0-p1 segment | |
249 | - r_mean = (r0 + r1) / 2; //calculate the average radius of the segment | |
250 | - | |
251 | - radius_sum += r_mean * (T) l; //add the radius scaled by the length to a running sum | |
252 | - length_sum += l; //add the length of p1 - p0 to the running sum | |
253 | - } | |
254 | - } | |
255 | - | |
256 | - length = length_sum; //store the total length | |
257 | - | |
258 | - //if the total length is zero, store a radius of zero | |
259 | - if(length == 0) | |
260 | - return 0; | |
261 | - else | |
262 | - return (T)(radius_sum / length); //return the average radius of the fiber | |
263 | - } | |
264 | - T average_radius() | |
265 | - { | |
266 | - T r_sum = 0.; | |
267 | - for(unsigned int i = 0; i < N; i++) | |
268 | - { | |
269 | - r_sum = r_sum + r[i]; | |
270 | - } | |
271 | - return r_sum/((T) N); | |
272 | - } | |
273 | - | |
274 | - /// Calculates the average radius of the fiber | |
275 | - T radius(){ | |
276 | - T length; | |
277 | - return radius(length); | |
278 | - } | |
279 | - | |
280 | - /// Returns the radius at index idx. | |
281 | - T radius(int idx){ | |
282 | - return r[idx]; | |
283 | - } | |
284 | 163 | |
285 | 164 | /// Return the point on the fiber closest to q |
286 | 165 | /// @param q is the query point used to locate the nearest point on the fiber centerline |
287 | - stim::vec<T> nearest(stim::vec<T> q){ | |
288 | - | |
289 | - stim::vec<double> temp( (double) q[0], (double) q[1], (double) q[2]); | |
290 | - | |
291 | - unsigned int idx = ann(temp); //determine the index of the nearest neighbor | |
292 | - | |
293 | - return stim::vec<T>((T) c[idx][0], (T) c[idx][1], (T) c[idx][2]); //return the nearest centerline point | |
294 | - } | |
166 | +// stim::vec<T> nearest(stim::vec<T> q){ | |
167 | +// | |
168 | +// stim::vec<double> temp( (double) q[0], (double) q[1], (double) q[2]); | |
169 | +// | |
170 | +// unsigned int idx = ann(temp); //determine the index of the nearest neighbor | |
171 | +// | |
172 | +// return stim::vec<T>((T) c[idx][0], (T) c[idx][1], (T) c[idx][2]); //return the nearest centerline point | |
173 | +// } | |
295 | 174 | |
296 | 175 | /// Return the point index on the fiber closest to q |
297 | 176 | /// @param q is the query point used to locate the nearest point on the fiber centerline |
298 | - unsigned int nearest_idx(stim::vec<T> q){ | |
299 | - | |
300 | - stim::vec<double> temp((double) q[0], (double) q[1], (double) q[2]); | |
301 | - | |
302 | - unsigned int idx = ann(temp); //determine the index of the nearest neighbor | |
303 | - | |
304 | - return idx; //return the nearest centerline point index | |
305 | - } | |
177 | +// unsigned int nearest_idx(stim::vec<T> q){ | |
178 | +// | |
179 | +// stim::vec<double> temp((double) q[0], (double) q[1], (double) q[2]); | |
180 | +// | |
181 | +// unsigned int idx = ann(temp); //determine the index of the nearest neighbor | |
182 | +// | |
183 | +// return idx; //return the nearest centerline point index | |
184 | +// } | |
306 | 185 | |
307 | 186 | /// Returns the fiber centerline as an array of stim::vec points |
308 | - std::vector< stim::vec<T> > centerline(){ | |
187 | + std::vector< stim::vec<T> > get_centerline(){ | |
309 | 188 | |
310 | 189 | //create an array of stim vectors |
311 | - std::vector< stim::vec<T> > pts(N); | |
190 | + std::vector< stim::vec3<T> > pts(N); | |
312 | 191 | |
313 | 192 | //cast each point to a stim::vec, keeping only the position information |
314 | 193 | for(unsigned int i = 0; i < N; i++) |
315 | - pts[i] = stim::vec<T>((T) c[i][0], (T) c[i][1], (T) c[i][2]); | |
316 | - | |
317 | - //return the centerline array | |
318 | - return pts; | |
319 | - } | |
320 | - | |
321 | - /// Returns the fiber centerline magnitudes as an array of stim::vec points | |
322 | - std::vector< stim::vec<T> > centerlinemag(){ | |
323 | - | |
324 | - //create an array of stim vectors | |
325 | - std::vector< stim::vec<T> > pts(N); | |
326 | - | |
327 | - //cast each point to a stim::vec, keeping only the position information | |
328 | - for(unsigned int i = 0; i < N; i++) | |
329 | - pts[i] = stim::vec<T>(r[i], r[i]);; | |
194 | + pts[i] = stim::vec3<T>((T) c[i][0], (T) c[i][1], (T) c[i][2]); | |
330 | 195 | |
331 | 196 | //return the centerline array |
332 | 197 | return pts; |
333 | 198 | } |
334 | 199 | |
335 | 200 | /// Split the fiber at the specified index. If the index is an end point, only one fiber is returned |
336 | - std::vector< stim::fiber<T> > split(unsigned int idx){ | |
201 | + std::vector< stim::centerline<T> > split(unsigned int idx){ | |
337 | 202 | |
338 | - std::vector< stim::fiber<T> > fl; //create an array to store up to two fibers | |
203 | + std::vector< stim::centerline<T> > fl; //create an array to store up to two fibers | |
339 | 204 | |
340 | 205 | //if the index is an end point, only the existing fiber is returned |
341 | 206 | if(idx == 0 || idx == N-1){ |
... | ... | @@ -361,14 +226,13 @@ public: |
361 | 226 | for(i = 0; i < N1; i++){ //for each centerline point |
362 | 227 | for(d = 0; d < 3; d++) |
363 | 228 | fl[0].c[i][d] = c[i][d]; //copy each coordinate |
364 | - fl[0].r[i] = r[i]; //copy the corresponding radius | |
365 | 229 | } |
366 | 230 | |
367 | 231 | //second half |
368 | 232 | for(i = 0; i < N2; i++){ |
369 | 233 | for(d = 0; d < 3; d++) |
370 | 234 | fl[1].c[i][d] = c[idx + i][d]; |
371 | - fl[1].r[i] = r[idx + i]; | |
235 | + | |
372 | 236 | } |
373 | 237 | } |
374 | 238 | |
... | ... | @@ -379,7 +243,7 @@ public: |
379 | 243 | /// Calculates the set of fibers resulting from a connection between the current fiber and a fiber f |
380 | 244 | |
381 | 245 | /// @param f is the fiber that will be connected to the current fiber |
382 | - std::vector< stim::fiber<T> > connect( stim::fiber<T> &f, double dist){ | |
246 | +/* std::vector< stim::centerline<T> > connect( stim::centerline<T> &f, double dist){ | |
383 | 247 | |
384 | 248 | double min_dist; |
385 | 249 | unsigned int idx0, idx1; |
... | ... | @@ -393,7 +257,7 @@ public: |
393 | 257 | |
394 | 258 | |
395 | 259 | } |
396 | - | |
260 | +*/ | |
397 | 261 | /// Outputs the fiber as a string |
398 | 262 | std::string str(){ |
399 | 263 | std::stringstream ss; |
... | ... | @@ -405,7 +269,6 @@ public: |
405 | 269 | for(unsigned int d = 0; d < 3; d++){ |
406 | 270 | ss<<c[i][d]<<" "; |
407 | 271 | } |
408 | - ss<<"] r = "<<r[i]<<std::endl; | |
409 | 272 | } |
410 | 273 | |
411 | 274 | return ss.str(); |
... | ... | @@ -428,7 +291,7 @@ public: |
428 | 291 | return get_vec(N-1); |
429 | 292 | } |
430 | 293 | ////resample a fiber in the network |
431 | - stim::fiber<T> resample(T spacing) | |
294 | + stim::centerline<T> resample(T spacing) | |
432 | 295 | { |
433 | 296 | std::cout<<"fiber::resample()"<<std::endl; |
434 | 297 | |
... | ... | @@ -468,7 +331,7 @@ public: |
468 | 331 | newPointList.push_back(fiberPositions[f+1]); |
469 | 332 | } |
470 | 333 | newPointList.push_back(fiberPositions[N-1]); //add the last point on the fiber to the new fiber list |
471 | - fiber newFiber(newPointList); | |
334 | + centerline newFiber(newPointList); | |
472 | 335 | return newFiber; |
473 | 336 | } |
474 | 337 | ... | ... |
stim/cuda/testKernel.cuh
... | ... | @@ -8,7 +8,7 @@ |
8 | 8 | #include <stim/cuda/cudatools/devices.h> |
9 | 9 | #include <stim/cuda/cudatools/threads.h> |
10 | 10 | #include <stim/cuda/cuda_texture.cuh> |
11 | - stim::cuda::cuda_texture tx; //texture object. | |
11 | + | |
12 | 12 | float* print; |
13 | 13 | |
14 | 14 | ///Initialization function, allocates the memory and passes the necessary |
... | ... | @@ -26,16 +26,16 @@ |
26 | 26 | cudaFree(print); ///temporary |
27 | 27 | } |
28 | 28 | |
29 | - __device__ | |
30 | - float templ(int x) | |
31 | - { | |
32 | - if(x < 32/6 || x > 32*5/6 || (x > 32*2/6 && x < 32*4/6)){ | |
33 | - return 1.0; | |
34 | - }else{ | |
35 | - return 0.0; | |
36 | - } | |
37 | - | |
38 | - } | |
29 | + __device__ | |
30 | + float templ(int x, int max_x) | |
31 | + { | |
32 | + if(x < max_x/6 || x > max_x*5/6 || (x > max_x*2/6 && x < max_x*4/6)) | |
33 | + { | |
34 | + return 1.0; | |
35 | + }else{ | |
36 | + return 0.0; | |
37 | + } | |
38 | + } | |
39 | 39 | |
40 | 40 | ///Find the difference of the given set of samples and the template |
41 | 41 | ///using cuda acceleration. |
... | ... | @@ -44,33 +44,24 @@ |
44 | 44 | ///@param float* result --a pointer to the memory that stores the result. |
45 | 45 | __global__ |
46 | 46 | //void get_diff (float *result) |
47 | - void get_diff (cudaTextureObject_t texIn, float *print) | |
47 | + void get_diff (cudaTextureObject_t texIn, float *print, int dx) | |
48 | 48 | { |
49 | 49 | int x = threadIdx.x + blockIdx.x * blockDim.x; |
50 | 50 | int y = threadIdx.y + blockIdx.y * blockDim.y; |
51 | -// int idx = y*64+x; | |
52 | - int idx = y*32+x; | |
51 | + int idx = y*dx+x; | |
53 | 52 | // int idx = y*16+x; |
54 | 53 | |
55 | 54 | float valIn = tex2D<unsigned char>(texIn, x, y); |
56 | - float templa = templ(x); | |
57 | - print[idx] = valIn; ///temporary | |
55 | + float templa = templ(x, 32)*255.0; | |
56 | + print[idx] = abs(valIn-templa); ///temporary | |
58 | 57 | //print[idx] = abs(templa); ///temporary |
59 | 58 | |
60 | 59 | } |
61 | 60 | |
62 | - | |
63 | - ///External access-point to the cuda function | |
64 | - ///@param GLuint texbufferID --GLtexture (most be contained in a framebuffer object) | |
65 | - /// that holds the data that will be handed to cuda. | |
66 | - ///@param GLenum texType --either GL_TEXTURE_1D, GL_TEXTURE_2D or GL_TEXTURE_3D | |
67 | - /// may work with other gl texture types, but untested. | |
68 | - ///@param DIM_Y, the number of samples in the template. | |
69 | - void test(GLint texbufferID, GLenum texType, int x, int y) | |
61 | + void test(cudaTextureObject_t tObj, int x, int y, std::string nam) | |
70 | 62 | { |
71 | 63 | |
72 | 64 | //Bind the Texture in GL and allow access to cuda. |
73 | - tx.MapCudaTexture(texbufferID, texType); | |
74 | 65 | |
75 | 66 | //initialize the return arrays. |
76 | 67 | |
... | ... | @@ -85,44 +76,13 @@ |
85 | 76 | |
86 | 77 | |
87 | 78 | // get_diff <<< blocks, threads >>> (tx.getTexture(), print); |
88 | - get_diff <<< numBlocks, threadsPerBlock >>> (tx.getTexture(), print); | |
89 | - | |
90 | - cudaDeviceSynchronize(); | |
91 | - stringstream name; //for debugging | |
92 | - name << "FromTex.bmp"; | |
93 | - stim::gpu2image<float>(print, name.str(),x,y,0,255); | |
94 | - | |
95 | - tx.UnmapCudaTexture(); | |
96 | - cleanUP(); | |
97 | - } | |
98 | - | |
99 | - void test(GLint texbufferID, GLenum texType, int x, int y, std::string nam) | |
100 | - { | |
101 | - | |
102 | - //Bind the Texture in GL and allow access to cuda. | |
103 | - tx.MapCudaTexture(texbufferID, texType); | |
104 | - | |
105 | - //initialize the return arrays. | |
106 | - | |
107 | - initArray(x,y); | |
108 | - dim3 numBlocks(1, y); | |
109 | - dim3 threadsPerBlock(x, 1); | |
110 | - int max_threads = stim::maxThreadsPerBlock(); | |
111 | - //dim3 threads(max_threads, 1); | |
112 | - //dim3 blocks(x / threads.x + 1, y); | |
113 | - //dim3 numBlocks(2, 2); | |
114 | - //dim3 threadsPerBlock(8, 108); | |
115 | - | |
116 | - | |
117 | -// get_diff <<< blocks, threads >>> (tx.getTexture(), print); | |
118 | - get_diff <<< numBlocks, threadsPerBlock >>> (tx.getTexture(), print); | |
79 | + get_diff <<< numBlocks, threadsPerBlock >>> (tObj, print, x); | |
119 | 80 | |
120 | 81 | cudaDeviceSynchronize(); |
121 | 82 | stringstream name; //for debugging |
122 | 83 | name << nam.c_str(); |
123 | - //stim::gpu2image<float>(print, name.str(),x,y,0,255); | |
84 | + stim::gpu2image<float>(print, name.str(),x,y,0,255); | |
124 | 85 | |
125 | - tx.UnmapCudaTexture(); | |
126 | 86 | cleanUP(); |
127 | 87 | } |
128 | 88 | ... | ... |
stim/gl/gl_spider.h
... | ... | @@ -23,14 +23,11 @@ |
23 | 23 | #include <stim/cuda/ivote.cuh> |
24 | 24 | #include <stim/visualization/glObj.h> |
25 | 25 | #include <vector> |
26 | +#include <stack> | |
26 | 27 | #include <stim/cuda/branch_detection.cuh> |
27 | -#include "../../../volume-spider/fiber.h" | |
28 | 28 | #include "../../../volume-spider/glnetwork.h" |
29 | 29 | #include <stim/visualization/cylinder.h> |
30 | 30 | #include <stim/cuda/testKernel.cuh> |
31 | - | |
32 | -//#include <stim/cuda/testKernel.cuh> | |
33 | - | |
34 | 31 | #include <iostream> |
35 | 32 | #include <fstream> |
36 | 33 | #ifdef TIMING |
... | ... | @@ -39,7 +36,6 @@ |
39 | 36 | #endif |
40 | 37 | |
41 | 38 | #ifdef TESTING |
42 | - #include <iostream> | |
43 | 39 | #include <cstdio> |
44 | 40 | #include <ctime> |
45 | 41 | #endif |
... | ... | @@ -49,7 +45,7 @@ namespace stim |
49 | 45 | { |
50 | 46 | |
51 | 47 | template<typename T> |
52 | -class gl_spider : public virtual gl_texture<T> | |
48 | +class gl_spider // : public virtual gl_texture<T> | |
53 | 49 | { |
54 | 50 | //doen't use gl_texture really, just needs the GLuint id. |
55 | 51 | //doesn't even need the texture iD really. |
... | ... | @@ -65,72 +61,87 @@ class gl_spider : public virtual gl_texture<T> |
65 | 61 | double hit_time;// = 0; |
66 | 62 | #endif |
67 | 63 | |
68 | - // | |
69 | 64 | stim::vec3<float> p; //vector designating the position of the spider. |
70 | - stim::vec3<float> d; //vector designating the orientation of the spider | |
71 | - //always a unit vector. | |
72 | - stim::vec<float> m; //magnitude of the spider vector. | |
73 | - //mag[0] = length. | |
74 | - //mag[1] = width. | |
65 | + stim::vec3<float> d; //normalized direction of travel | |
66 | + float m; //size of the spider in tissue space. | |
67 | + | |
75 | 68 | std::vector<stim::vec3<float> > dV; //A list of all the direction vectors. |
76 | - std::vector<stim::vec3<float> > pV; //A list of all the position vectors. | |
77 | - std::vector<stim::vec3<float> > mV; //A list of all the size vectors. | |
69 | + std::vector<stim::vec3<float> > pV; //A list of all test positions (relative to p) | |
70 | + std::vector<float> mV; //A list of all the size vectors. | |
78 | 71 | |
79 | - stim::matrix<float, 4> cT; //current Transformation matrix | |
80 | - //From tissue space to texture space. | |
81 | - GLuint texID; | |
82 | - stim::vec<float> S; //Size of a voxel in the volume. | |
83 | - stim::vec<float> R; //Dimensions of the volume. | |
72 | + stim::matrix<float, 4> cT; //current Transformation matrix (tissue)->(texture) | |
73 | + GLuint texID; //OpenGL ID for the texture to be traced | |
74 | + stim::vec3<float> S; //Size of a voxel in the volume. | |
75 | + stim::vec3<float> R; //Dimensions of the volume. | |
84 | 76 | |
85 | 77 | |
86 | 78 | //GL and Cuda variables |
87 | - GLuint dList; //displaylist ID | |
88 | - GLuint fboID; //framebuffer ID | |
89 | - GLuint texbufferID; //texbuffer ID, only necessary for | |
90 | - //cuda aspect of the calculation. | |
91 | - GLuint pfboID; //buffer object for position tracking. | |
92 | - GLuint ptexbufferID; //texture object for position tracking. | |
93 | - | |
94 | - GLuint mfboID; //buffer object for magnitude adjustment. | |
95 | - GLuint mtexbufferID; //texture object for magnitude adjustment. | |
96 | - GLuint bfboID; //buffer object for position adjustment. | |
97 | - GLuint btexbufferID; //buffer object for position adjustment. | |
79 | + GLuint dList; //ID of the starting display lists (series of 4) | |
80 | + //dList + 0 = direction template rectangles | |
81 | + //dList + 1 = position template rectangles | |
82 | + //dList + 2 = size template rectangles | |
83 | + //dList + 3 = branch detection cylinder around the fiber | |
84 | + | |
85 | + GLuint fboID; //framebuffer ID for direction templates | |
86 | + GLuint texbufferID; //texture ID for direction templates | |
87 | + GLuint direction_buffID; //framebuffer ID, position templates | |
88 | + GLuint direction_texID; //texture ID, position templates | |
89 | + | |
90 | + GLuint position_buffID; //framebuffer ID, position templates | |
91 | + GLuint position_texID; //texture ID, position templates | |
92 | + | |
93 | + GLuint radius_buffID; //framebuffer ID, radius templates | |
94 | + GLuint radius_texID; //texture ID, radius templates | |
95 | + | |
96 | + GLuint cylinder_buffID; //framebuffer ID, cylinder (surrounding fiber) | |
97 | + GLuint cylinder_texID; //texture ID, cylinder | |
98 | 98 | |
99 | 99 | int numSamples; //The number of templates in the buffer. |
100 | 100 | int numSamplesPos; |
101 | 101 | int numSamplesMag; |
102 | 102 | |
103 | - float stepsize;// = 5.0; //Step size. | |
104 | -// float stepsize = 3.0; //Step size. | |
105 | - int current_cost; //variable to store the cost of the current step. | |
106 | - | |
103 | + float length; //this will be a function of the radius | |
104 | + float stepsize; //this will be a function of the length | |
105 | + | |
106 | + int current_cost; //variable to store the cost of the current step | |
107 | 107 | |
108 | 108 | //Tracing variables. |
109 | - std::stack< stim::vec3<float> > seeds; //seed positions. | |
110 | - std::stack< stim::vec3<float> > seedsvecs; //seed directions. | |
111 | - std::stack< float > seedsmags; //seed magnitudes. | |
109 | + std::stack< stim::vec3<float> > seeds; //seed positions | |
110 | + std::stack< stim::vec3<float> > seedsvecs; //seed directions | |
111 | + std::stack< float > seedsmags; //seed magnitudes | |
112 | + | |
113 | + std::vector< stim::vec3<float> > cL; //centerline up to the current point | |
114 | + std::vector< stim::vec3<float> > cD; //directions up to the current point (debugging) | |
115 | + std::vector< float > cM; //radius up to the current point | |
112 | 116 | |
113 | - std::vector< stim::vec3<float> > cL; //Positions of line currently being traced. | |
114 | - std::vector< stim::vec3<float> > cD; //Direction of line currently being traced. | |
115 | - std::vector< stim::vec<float> > cM; //Magnitude of line currently being traced. | |
117 | + stim::glnetwork<float> nt; //network object holding the currently traced centerlines | |
118 | + stim::glObj<float> sk; //OBJ file storing the network (identical to above) | |
116 | 119 | |
117 | - stim::glnetwork<float> nt; //object for storing the network. | |
120 | + //consider replacing with two seed points facing opposite directions | |
121 | + stim::vec<float> rev; //reverse vector | |
118 | 122 | |
119 | - stim::vec<float> rev; //reverse vector; | |
120 | - stim::camera camSel; | |
121 | - stim::vec3<float> ps; | |
122 | - stim::vec3<float> ups; | |
123 | - stim::vec3<float> ds; | |
123 | + //selection mode - detecting fiber intersections | |
124 | + stim::camera camSel; //camera for selection mode (detecting collisions) | |
125 | + stim::vec3<float> ps; //position for the selection camera | |
126 | + stim::vec3<float> ups; //up direction for the selection camera | |
127 | + stim::vec3<float> ds; //direction for the selection camera | |
124 | 128 | |
125 | - //static const float t_length = 16.0; | |
126 | - float t_length; | |
129 | + float n_pixels; //length of the template (in pixels) | |
127 | 130 | |
128 | 131 | //cuda texture variables that keep track of the binding. |
129 | - stim::cuda::cuda_texture t_dir; | |
130 | - stim::cuda::cuda_texture t_pos; | |
131 | - stim::cuda::cuda_texture t_mag; | |
132 | + stim::cuda::cuda_texture t_dir; //cuda_texture object used as an interface between OpenGL and cuda for direction vectors. | |
133 | + stim::cuda::cuda_texture t_pos; //cuda_texture object used as an interface between OpenGL and cuda for position vectors. | |
134 | + stim::cuda::cuda_texture t_mag; //cuda_texture object used as an interface between OpenGL and cuda for size vectors. | |
132 | 135 | |
133 | 136 | |
137 | + #ifdef DEBUG | |
138 | + stringstream name; | |
139 | + int iter; | |
140 | + int iter_pos; | |
141 | + int iter_dir; | |
142 | + int iter_siz; | |
143 | + #endif | |
144 | + | |
134 | 145 | //--------------------------------------------------------------------------// |
135 | 146 | //-------------------------------PRIVATE METHODS----------------------------// |
136 | 147 | //--------------------------------------------------------------------------// |
... | ... | @@ -142,31 +153,32 @@ class gl_spider : public virtual gl_texture<T> |
142 | 153 | findOptimalDirection() |
143 | 154 | { |
144 | 155 | #ifdef TIMING |
145 | - gpuStartTimer(); | |
156 | + gpuStartTimer(); //Timer for profiling | |
146 | 157 | #endif |
147 | 158 | setMatrix(); //create the transformation matrix. |
148 | 159 | glCallList(dList); //move the templates to p, d, m. |
149 | - glFinish(); | |
150 | -// glFlush(); | |
160 | + glFinish(); //flush the pipeline | |
151 | 161 | #ifdef TIMING |
152 | - direction_time += gpuStopTimer(); | |
153 | - #endif | |
154 | - #ifdef TESTING | |
155 | -// test(texbufferID, GL_TEXTURE_2D,2*t_length,numSamples*t_length, "Final_Cost_Direction.bmp"); | |
162 | + direction_time += gpuStopTimer(); //profiling | |
156 | 163 | #endif |
157 | 164 | |
158 | 165 | int best = getCost(t_dir.getTexture(), t_dir.getAuxArray() ,numSamples); //find min cost. |
159 | - stim::vec<float> next( //find next vector. | |
166 | + #ifdef DEBUG | |
167 | + name.str(""); | |
168 | + name << "Final_Cost_Direction_fiber_"<< iter << "_" << iter_dir << ".bmp"; | |
169 | + test(t_dir.getTexture(), n_pixels*2.0, numSamples*n_pixels, name.str()); | |
170 | + iter_dir++; | |
171 | + #endif | |
172 | + stim::vec<float> next( ///calculate the next vector. | |
160 | 173 | dV[best][0]*S[0]*R[0], |
161 | 174 | dV[best][1]*S[1]*R[1], |
162 | 175 | dV[best][2]*S[2]*R[2], |
163 | 176 | 0); |
164 | - next = (cT*next).norm(); //find next vector. | |
165 | - setPosition( p[0]+next[0]*m[0]/stepsize, | |
166 | - p[1]+next[1]*m[0]/stepsize, | |
167 | - p[2]+next[2]*m[0]/stepsize); | |
168 | - setDirection(next[0], next[1], next[2]); | |
169 | - //move forward and change direction. | |
177 | + next = (cT*next).norm(); ///transform the next vector into Tissue space. | |
178 | + setPosition( p[0]+next[0]*m/stepsize, | |
179 | + p[1]+next[1]*m/stepsize, | |
180 | + p[2]+next[2]*m/stepsize); | |
181 | + setDirection(next[0], next[1], next[2]); //move forward and change direction. | |
170 | 182 | } |
171 | 183 | |
172 | 184 | /// Method for finding the best d (direction) for the spider. |
... | ... | @@ -176,27 +188,29 @@ class gl_spider : public virtual gl_texture<T> |
176 | 188 | findOptimalPosition() |
177 | 189 | { |
178 | 190 | #ifdef TIMING |
179 | - gpuStartTimer(); | |
191 | + gpuStartTimer(); //timer for profiling | |
180 | 192 | #endif |
181 | 193 | setMatrix(); //create the transformation matrix. |
182 | 194 | glCallList(dList+1); //move the templates to p, d, m. |
183 | - glFinish(); | |
195 | + glFinish(); //flush the pipeline | |
184 | 196 | // glFlush(); |
185 | 197 | #ifdef TIMING |
186 | - position_time += gpuStopTimer(); | |
198 | + position_time += gpuStopTimer(); ///timer for profiling | |
187 | 199 | #endif |
188 | 200 | |
189 | - #ifdef TESTING | |
190 | -// test(ptexbufferID, GL_TEXTURE_2D,2*t_length, numSamplesPos*t_length, "Final_Cost_Position.bmp"); | |
191 | - #endif | |
192 | 201 | int best = getCost(t_pos.getTexture(), t_pos.getAuxArray(), numSamplesPos); //find min cost. |
193 | -// std::cerr << best << std::endl; | |
202 | + #ifdef DEBUG | |
203 | + name.str(""); | |
204 | + name << "Final_Cost_Position_" << iter << "_" << iter_pos << ".bmp"; | |
205 | + test(t_pos.getTexture(), n_pixels*2.0, numSamplesPos*n_pixels, name.str()); | |
206 | + iter_pos++; | |
207 | + #endif | |
194 | 208 | stim::vec<float> next( //find next position. |
195 | 209 | pV[best][0], |
196 | 210 | pV[best][1], |
197 | 211 | pV[best][2], |
198 | 212 | 1); |
199 | - next = cT*next; //find next position. | |
213 | + next = cT*next; //transform the next position vector into tissue space. | |
200 | 214 | setPosition( |
201 | 215 | next[0]*S[0]*R[0], |
202 | 216 | next[1]*S[1]*R[1], |
... | ... | @@ -206,7 +220,7 @@ class gl_spider : public virtual gl_texture<T> |
206 | 220 | |
207 | 221 | /// Method for finding the best scale for the spider. |
208 | 222 | /// changes the x, y, z size of the spider to minimize the cost |
209 | - /// function. */ | |
223 | + /// function. | |
210 | 224 | void |
211 | 225 | findOptimalScale() |
212 | 226 | { |
... | ... | @@ -215,64 +229,21 @@ class gl_spider : public virtual gl_texture<T> |
215 | 229 | #endif |
216 | 230 | setMatrix(); //create the transformation. |
217 | 231 | glCallList(dList+2); //move the templates to p, d, m. |
218 | - glFinish(); | |
219 | -// glFlush(); | |
232 | + glFinish(); //flush the drawing pipeline. | |
220 | 233 | #ifdef TIMING |
221 | 234 | size_time += gpuStopTimer(); |
222 | 235 | #endif |
223 | - #ifdef TESTING | |
224 | -// test(mtexbufferID, GL_TEXTURE_2D, 2*t_length, numSamplesMag*t_length, "Final_Cost_Position.bmp"); | |
225 | - #endif | |
226 | 236 | int best = getCost(t_mag.getTexture(), t_mag.getAuxArray(), numSamplesMag); //get best cost. |
227 | - setMagnitude(m[0]*mV[best][0]); //adjust the magnitude. | |
237 | + #ifdef DEBUG | |
238 | + name.str(""); | |
239 | + name << "Final_Cost_Size_" << iter << "_" << iter_siz << ".bmp"; | |
240 | + test(t_mag.getTexture(), n_pixels*2.0, numSamplesMag*n_pixels, name.str()); | |
241 | + iter_siz++; | |
242 | + #endif | |
243 | + setMagnitude(m*mV[best]); //adjust the magnitude. | |
228 | 244 | } |
229 | 245 | |
230 | 246 | |
231 | - ///subject to change. | |
232 | - ///finds branches. | |
233 | - ///depreciated | |
234 | - void | |
235 | - branchDetection() | |
236 | - { | |
237 | - setMatrix(); | |
238 | - glCallList(dList+3); | |
239 | - std::vector< stim::vec<float> > result = find_branch( | |
240 | - btexbufferID, GL_TEXTURE_2D, 16, 216); | |
241 | - stim::vec3<float> size(S[0]*R[0], S[1]*R[1], S[2]*R[2]); | |
242 | - if(!result.empty()) | |
243 | - { | |
244 | - for(int i = 1; i < result.size(); i++) | |
245 | - { | |
246 | - stim::vec<float> cylp( | |
247 | - 0.5 * cos(2*M_PI*(result[i][1])), | |
248 | - 0.5 * sin(2*M_PI*(result[i][1])), | |
249 | - result[i][0]-0.5, | |
250 | - 1.0); | |
251 | - cylp = cT*cylp; | |
252 | - | |
253 | - stim::vec3<float> vec( | |
254 | - cylp[0]*S[0]*R[0], | |
255 | - cylp[1]*S[1]*R[1], | |
256 | - cylp[2]*S[2]*R[2]); | |
257 | - stim::vec3<float> seeddir(-p[0] + cylp[0]*S[0]*R[0], | |
258 | - -p[1] + cylp[1]*S[1]*R[1], | |
259 | - -p[2] + cylp[2]*S[2]*R[2]); | |
260 | - seeddir = seeddir.norm(); | |
261 | - float seedm = m[0]; | |
262 | -// Uncomment for global run | |
263 | - if( | |
264 | - !(vec[0] > size[0] || vec[1] > size[1] | |
265 | - || vec[2] > size[2] || vec[0] < 0 | |
266 | - || vec[1] < 0 || vec[2] < 0)) | |
267 | - { | |
268 | - setSeed(vec); | |
269 | - setSeedVec(seeddir); | |
270 | - setSeedMag(seedm); | |
271 | - } | |
272 | - } | |
273 | - } | |
274 | - | |
275 | - } | |
276 | 247 | |
277 | 248 | |
278 | 249 | ///finds all the branches in the a given fiber. |
... | ... | @@ -281,78 +252,78 @@ class gl_spider : public virtual gl_texture<T> |
281 | 252 | branchDetection2(int n = 8, int l_template = 8, int l_square = 8) |
282 | 253 | { |
283 | 254 | #ifdef TIMING |
284 | - gpuStartTimer(); | |
255 | + gpuStartTimer(); ///timer for performance analysis | |
285 | 256 | #endif |
286 | 257 | |
287 | - if(cL.size() < 4){} | |
258 | + if(cL.size() < 4){} ///if the size of the fiber is less then 4 we do nothing. | |
288 | 259 | else{ |
289 | - setMatrix(1); | |
290 | - DrawLongCylinder(n, l_template, l_square); | |
260 | + setMatrix(1); ///finds the current transformation matrix | |
261 | + DrawLongCylinder(n, l_template, l_square); ///Draw the cylinder. | |
291 | 262 | stim::cylinder<float> cyl(cL, cM); |
292 | - std::vector< stim::vec<float> > result = find_branch(btexbufferID, GL_TEXTURE_2D, n*l_square, (cL.size()-1)*l_template); | |
293 | - stim::vec3<float> size(S[0]*R[0], S[1]*R[1], S[2]*R[2]); | |
294 | - float pval; | |
295 | - if(!result.empty()) | |
263 | + std::vector< stim::vec<float> > result = find_branch(cylinder_texID, GL_TEXTURE_2D, n*l_square, (cL.size()-1)*l_template); ///find all the centers in cuda | |
264 | + stim::vec3<float> size(S[0]*R[0], S[1]*R[1], S[2]*R[2]); ///the borders of the texture. | |
265 | + float pval; //pvalue associated with the points on the cylinder. | |
266 | + if(!result.empty()) ///if we have any points | |
296 | 267 | { |
297 | - for(int i = 0; i < result.size(); i++) | |
268 | + for(int i = 0; i < result.size(); i++) ///for each point | |
298 | 269 | { |
299 | 270 | int id = result[i][2]; |
300 | - if(fmod(result[i][2], id) != 0 && id != 0) | |
271 | + if(fmod(result[i][2], id) != 0 && id != 0) ///if the remainer is odd | |
301 | 272 | { |
302 | 273 | |
303 | 274 | pval = ((cyl.getl(id+1)-cyl.getl(id))* |
304 | - (fmod(result[i][2], id))+cyl.getl(id))/cyl.getl(cL.size()-1); | |
275 | + (fmod(result[i][2], id))+cyl.getl(id))/cyl.getl(cL.size()-1); ///calculate pvalue | |
305 | 276 | } |
306 | - else if(id == 0) | |
277 | + else if(id == 0) ///if the point is on the edge | |
307 | 278 | { |
308 | - pval = (cyl.getl(id+1)*result[i][2])/cyl.getl(cL.size()-1); | |
279 | + pval = (cyl.getl(id+1)*result[i][2])/cyl.getl(cL.size()-1); | |
309 | 280 | } |
310 | 281 | else |
311 | 282 | { |
312 | - pval = (cyl.getl(id)/cyl.getl(cL.size()-1)); | |
283 | + pval = (cyl.getl(id)/cyl.getl(cL.size()-1)); ///if the point is somewhere on the surface of the cylinder other than the edge | |
313 | 284 | } |
314 | - stim::vec3<float> v = cyl.surf(pval, result[i][0]); | |
315 | - stim::vec3<float> di = cyl.p(pval); | |
316 | - float rad = cyl.r(pval); | |
285 | + stim::vec3<float> v = cyl.surf(pval, result[i][0]); ///find the coordinates of the point at pval on the surface in tissue space. | |
286 | + stim::vec3<float> di = cyl.p(pval); ///find the coord of v in tissue space projected on the centerline. | |
287 | + float rad = cyl.r(pval)/2; ///find the radius at the pvalue's location | |
317 | 288 | if( |
318 | 289 | !(v[0] > size[0] || v[1] > size[1] |
319 | 290 | || v[2] > size[2] || v[0] < 0 |
320 | - || v[1] < 0 || v[2] < 0)) | |
291 | + || v[1] < 0 || v[2] < 0)) ///if the v point is INSIDE the volume | |
321 | 292 | { |
322 | - setSeed(v); | |
323 | - setSeedVec((v-di).norm()); | |
324 | - setSeedMag(rad); | |
293 | + setSeed(v); ///add a seedpoint's position. | |
294 | + setSeedVec((v-di).norm()); ///add a seedpoints direction | |
295 | + setSeedMag(rad); ///add the starting radius. | |
325 | 296 | } |
326 | 297 | } |
327 | 298 | } |
328 | 299 | } |
329 | 300 | #ifdef TIMING |
330 | - branch_time += gpuStopTimer(); | |
301 | + branch_time += gpuStopTimer(); ///timer for performance. | |
331 | 302 | #endif |
332 | 303 | } |
333 | 304 | |
334 | 305 | |
335 | 306 | float uniformRandom() |
336 | 307 | { |
337 | - return ( (float)(rand()))/( (float)(RAND_MAX)); | |
308 | + return ( (float)(rand()))/( (float)(RAND_MAX)); ///generates a random number between 0 and 1 using the uniform distribution. | |
338 | 309 | } |
339 | 310 | |
340 | 311 | float normalRandom() |
341 | 312 | { |
342 | 313 | float u1 = uniformRandom(); |
343 | 314 | float u2 = uniformRandom(); |
344 | - return cos(2.0*atan(1.0)*u2)*sqrt(-1.0*log(u1)); | |
315 | + return cos(2.0*atan(1.0)*u2)*sqrt(-1.0*log(u1)); ///generate a random number using the normal distribution between 0 and 1. | |
345 | 316 | } |
346 | 317 | |
347 | 318 | stim::vec3<float> uniformRandVector() |
348 | 319 | { |
349 | - stim::vec3<float> r(uniformRandom(), uniformRandom(), 1.0); | |
320 | + stim::vec3<float> r(uniformRandom(), uniformRandom(), 1.0); ///generate a random vector using the uniform distribution between 0 and 1. | |
350 | 321 | return r; |
351 | 322 | } |
352 | 323 | |
353 | 324 | stim::vec3<float> normalRandVector() |
354 | 325 | { |
355 | - stim::vec3<float> r(normalRandom(), normalRandom(), 1.0); | |
326 | + stim::vec3<float> r(normalRandom(), normalRandom(), 1.0); ///generate a random vector using the normal distribution between 0 and 1. | |
356 | 327 | return r; |
357 | 328 | } |
358 | 329 | |
... | ... | @@ -370,49 +341,57 @@ class gl_spider : public virtual gl_texture<T> |
370 | 341 | ///Stored in a display list. |
371 | 342 | ///uses the default d vector <0,0,1> |
372 | 343 | void |
373 | - genDirectionVectors(float solidAngle = M_PI/2) | |
344 | + genDirectionVectors(float solidAngle = stim::PI/2) | |
374 | 345 | { |
375 | 346 | |
376 | 347 | //Set up the vectors necessary for Rectangle creation. |
377 | 348 | stim::vec3<float> Y(1.0,0.0,0.0); //orthogonal vec. |
378 | - stim::vec3<float> pos(0.0,0.0,0.0); | |
379 | - stim::vec3<float> mag(1.0, 1.0, 1.0); | |
380 | - stim::vec3<float> dir(0.0, 0.0, 1.0); | |
349 | + stim::vec3<float> pos(0.0,0.0,0.0); //center point of a rectangle | |
350 | + float mag = 1.0; //size of the generated rectangle. | |
351 | + stim::vec3<float> dir(0.0, 0.0, 1.0); //normal of the rectangle | |
381 | 352 | |
382 | - float PHI[2], Z[2], range; | |
383 | - PHI[0] = solidAngle/2; | |
353 | + float PHI[2], Z[2], range; | |
354 | + PHI[0] = solidAngle/2; ///Project the solid angle into spherical coordinates | |
384 | 355 | PHI[1] = asin(0); |
385 | 356 | |
386 | - Z[0] = cos(PHI[0]); | |
357 | + Z[0] = cos(PHI[0]); ///Project the z into spherical coordinates | |
387 | 358 | Z[1] = cos(PHI[1]); |
388 | 359 | |
389 | - range = Z[0] - Z[1]; | |
360 | + range = Z[0] - Z[1]; ///The range the possible values can be. | |
390 | 361 | |
391 | 362 | float z, theta, phi; |
392 | - glNewList(dList, GL_COMPILE); | |
393 | - for(int i = 0; i < numSamples; i++) | |
363 | + glNewList(dList, GL_COMPILE); ///create a display list of all the direction templates. | |
364 | + for(int i = 0; i < numSamples; i++) ///for each sample | |
394 | 365 | { |
395 | - z = uniformRandom()*range + Z[1]; | |
396 | - theta = uniformRandom()*2*M_PI; | |
397 | - phi = acos(z); | |
398 | - stim::vec3<float> sph(1, theta, phi); | |
399 | - stim::vec3<float> cart = sph.sph2cart(); | |
400 | - dV.push_back(cart); | |
401 | - if(cos(Y.dot(cart)) < 0.087) | |
366 | + z = uniformRandom()*range + Z[1]; ///generate a z coordinate | |
367 | + theta = uniformRandom()*stim::TAU; ///generate a theta coordinate | |
368 | + phi = acos(z); ///generate a phi from the z. | |
369 | + stim::vec3<float> sph(1, theta, phi); ///combine into a vector in spherical coordinates. | |
370 | + stim::vec3<float> cart = sph.sph2cart();///convert to cartesian. | |
371 | + dV.push_back(cart); ///save the generated vector for further use. | |
372 | + #ifdef DEBUG | |
373 | +// std::cout << cart << std::endl; | |
374 | + #endif | |
375 | + if(cos(Y.dot(cart)) < 0.087) ///make sure that the Y is not parallel to the new vector. | |
402 | 376 | { |
403 | 377 | Y[0] = 0.0; Y[1] = 1.0; |
404 | 378 | }else{ |
405 | 379 | Y[0] = 1.0; Y[1] = 0.0; |
406 | 380 | } |
407 | - hor = stim::rect<float>(mag, | |
381 | + | |
382 | + hor = stim::rect<float>(mag, ///generate a rectangle with the new vectro as a normal. | |
408 | 383 | pos, cart, |
409 | 384 | ((Y.cross(cart)).cross(cart)).norm()); |
410 | - ver = stim::rect<float>(mag, | |
385 | + | |
386 | + #ifdef DEBUG | |
387 | + // std::cout << hor.n() << std::endl; | |
388 | + #endif | |
389 | + ver = stim::rect<float>(mag, ///generate another rectangle that's perpendicular the first but parallel to the cart vector. | |
411 | 390 | pos, cart, |
412 | 391 | hor.n()); |
413 | - UpdateBuffer(0.0, 0.0+i*t_length); | |
392 | + UpdateBuffer(0.0, 0.0+i*n_pixels); ///Put the necessary points into the diplaylist. | |
414 | 393 | } |
415 | - glEndList(); | |
394 | + glEndList(); ///finilize the display list. | |
416 | 395 | } |
417 | 396 | |
418 | 397 | ///@param float delta, How much the rectangles vary in position. |
... | ... | @@ -426,28 +405,45 @@ class gl_spider : public virtual gl_texture<T> |
426 | 405 | { |
427 | 406 | //Set up the vectors necessary for Rectangle creation. |
428 | 407 | stim::vec3<float> Y(1.0,0.0,0.0); //orthogonal vec. |
429 | - stim::vec3<float> pos(0.0,0.0,0.0); | |
430 | - stim::vec3<float> mag(1.0, 1.0, 1.0); | |
431 | - stim::vec3<float> dir(0.0, 0.0, 1.0); | |
408 | + stim::vec3<float> pos(0.0,0.0,0.0); //center point of a rectangle | |
409 | + float mag = 1.0; ///size of each rectangle | |
410 | + stim::vec3<float> dir(0.0, 0.0, 1.0); ///normal of the rectangle plane. | |
432 | 411 | |
433 | 412 | //Set up the variable necessary for vector creation. |
434 | - glNewList(dList+1, GL_COMPILE); | |
435 | - for(int i = 0; i < numSamplesPos; i++) | |
413 | + glNewList(dList+1, GL_COMPILE); ///generate a new display list. | |
414 | + pV.push_back(pos); | |
415 | + hor = stim::rect<float>(mag, ///generate a rec tangle with the new vector as a normal. | |
416 | + pos, dir, | |
417 | + ((Y.cross(d)).cross(d)) | |
418 | + .norm()); | |
419 | + ver = stim::rect<float>(mag, ///generate anoth er rectangle that's perpendicular the first but parallel to the cart vector. | |
420 | + pos, dir, | |
421 | + hor.n()); | |
422 | + ///The first vector is always in the center. | |
423 | + UpdateBuffer(0.0, 0.0+0*n_pixels); | |
424 | + for(int i = 1; i < numSamplesPos; i++) ///for the number of position samples | |
436 | 425 | { |
437 | - stim::vec3<float> temp = uniformRandVector(); | |
438 | - temp = temp*delta*2.0 - delta/2.0; | |
426 | + stim::vec3<float> temp = uniformRandVector(); ///generate a random point on a plane. | |
427 | + temp[0] = temp[0]*delta; | |
428 | + temp[1] = temp[1]*2*stim::PI; | |
429 | + | |
439 | 430 | temp[2] = 0.0; |
440 | - pV.push_back(temp); | |
441 | - hor = stim::rect<float>(mag, | |
431 | + temp = temp.cyl2cart(); | |
432 | + pV.push_back(temp); ///save the point for further use. | |
433 | + hor = stim::rect<float>(mag, ///generate a rectangle with the new vector as a normal. | |
442 | 434 | temp, dir, |
443 | 435 | ((Y.cross(d)).cross(d)) |
444 | 436 | .norm()); |
445 | - ver = stim::rect<float>(mag, | |
437 | + ver = stim::rect<float>(mag, ///generate another rectangle that's perpendicular the first but parallel to the cart vector. | |
446 | 438 | temp, dir, |
447 | 439 | hor.n()); |
448 | - UpdateBuffer(0.0, 0.0+i*t_length); | |
440 | + UpdateBuffer(0.0, 0.0+i*n_pixels); ///sample the necessary points and put them into a display list. | |
449 | 441 | } |
450 | - glEndList(); | |
442 | + glEndList(); ///finilize the display list. | |
443 | + #ifdef DEBUG | |
444 | + for(int i = 0; i < numSamplesPos; i++) | |
445 | + std::cout << pV[i] << std::endl; | |
446 | + #endif | |
451 | 447 | } |
452 | 448 | |
453 | 449 | ///@param float delta, How much the rectangles are allowed to expand. |
... | ... | @@ -462,35 +458,31 @@ class gl_spider : public virtual gl_texture<T> |
462 | 458 | |
463 | 459 | //Set up the vectors necessary for Rectangle creation. |
464 | 460 | stim::vec3<float> Y(1.0, 0.0, 0.0); //orthogonal vec. |
465 | - stim::vec3<float> pos(0.0, 0.0, 0.0); | |
466 | - stim::vec3<float> mag(1.0, 1.0, 1.0); | |
467 | - stim::vec3<float> dir(0.0, 0.0, 1.0); | |
461 | + stim::vec3<float> pos(0.0, 0.0, 0.0); //center of the future rect. | |
462 | + float mag = 1.0; ///size of the rectangle | |
463 | + stim::vec3<float> dir(0.0, 0.0, 1.0); ///normal of the rectangle plane. | |
468 | 464 | |
469 | 465 | //Set up the variable necessary for vector creation. |
470 | - int dim = (sqrt(numSamplesMag)-1)/2; | |
471 | - float min = 1.0-delta; | |
472 | - float max = 1.0+delta; | |
473 | - float step = (max-min)/(numSamplesMag-1); | |
466 | + float min = 1.0-delta; ///smallest size | |
467 | + float max = 1.0+delta; ///largers size. | |
468 | + float step = (max-min)/(numSamplesMag-1); ///the size variation from one rect to the next. | |
474 | 469 | float factor; |
475 | - stim::vec3<float> temp(0.0,0.0,0.0); | |
476 | - | |
477 | 470 | glNewList(dList+2, GL_COMPILE); |
478 | - for(int i = 0; i < numSamplesMag; i++){ | |
471 | + for(int i = 0; i < numSamplesMag; i++){ ///for the number of position samples | |
479 | 472 | //Create linear index |
480 | - factor = (min+step*i)*mag[0]; | |
481 | - temp = factor; | |
482 | - mV.push_back(temp); | |
483 | - hor = stim::rect<float>(temp, | |
473 | + factor = (min+step*i)*mag; ///scaling factor | |
474 | + mV.push_back(factor); ///save the size factor for further use. | |
475 | + hor = stim::rect<float>(factor, ///generate a rectangle with the new vector as a normal. | |
484 | 476 | pos, dir, |
485 | 477 | ((Y.cross(d)).cross(d)) |
486 | 478 | .norm()); |
487 | - ver = stim::rect<float>(temp, | |
479 | + ver = stim::rect<float>(factor, ///generate another rectangle that's perpendicular the first but parallel to the cart vector. | |
488 | 480 | pos, dir, |
489 | 481 | hor.n()); |
490 | - UpdateBuffer(0.0, 0.0+i*t_length); | |
482 | + UpdateBuffer(0.0, 0.0+i*n_pixels); ///sample the necessary points and put them into a display list. | |
491 | 483 | CHECK_OPENGL_ERROR |
492 | 484 | } |
493 | - glEndList(); | |
485 | + glEndList(); ///finilize the displaylist. | |
494 | 486 | } |
495 | 487 | |
496 | 488 | ///@param float v_x x-coordinate in buffer-space, |
... | ... | @@ -500,15 +492,15 @@ class gl_spider : public virtual gl_texture<T> |
500 | 492 | void |
501 | 493 | UpdateBuffer(float v_x, float v_y) |
502 | 494 | { |
503 | - stim::vec3<float>p1; | |
504 | - stim::vec3<float>p2; | |
505 | - stim::vec3<float>p3; | |
506 | - stim::vec3<float>p4; | |
507 | - p1 = hor.p(1,1); | |
508 | - p2 = hor.p(1,0); | |
509 | - p3 = hor.p(0,0); | |
510 | - p4 = hor.p(0,1); | |
511 | - glBegin(GL_QUADS); | |
495 | + stim::vec3<float>p1; ///first point. | |
496 | + stim::vec3<float>p2; ///second point. | |
497 | + stim::vec3<float>p3; ///third point. | |
498 | + stim::vec3<float>p4; ///fourth point. | |
499 | + p1 = hor.p(1,1); ///generate the top right point from the horizontal template. | |
500 | + p2 = hor.p(1,0); ///generate the bottom right point from the horizonatal template. | |
501 | + p3 = hor.p(0,0); ///generate the bottom left point from the horizontal template. | |
502 | + p4 = hor.p(0,1); ///generate the top left point from the horizonatal template. | |
503 | + glBegin(GL_QUADS); ///generate the Quad from the 4 points. | |
512 | 504 | glTexCoord3f( |
513 | 505 | p1[0], |
514 | 506 | p1[1], |
... | ... | @@ -520,51 +512,51 @@ class gl_spider : public virtual gl_texture<T> |
520 | 512 | p2[1], |
521 | 513 | p2[2] |
522 | 514 | ); |
523 | - glVertex2f(v_x+t_length, v_y); | |
515 | + glVertex2f(v_x+n_pixels, v_y); | |
524 | 516 | glTexCoord3f( |
525 | 517 | p3[0], |
526 | 518 | p3[1], |
527 | 519 | p3[2] |
528 | 520 | ); |
529 | - glVertex2f(v_x+t_length, v_y+t_length); | |
521 | + glVertex2f(v_x+n_pixels, v_y+n_pixels); | |
530 | 522 | glTexCoord3f( |
531 | 523 | p4[0], |
532 | 524 | p4[1], |
533 | 525 | p4[2] |
534 | 526 | ); |
535 | - glVertex2f(v_x, v_y+t_length); | |
536 | - glEnd(); | |
537 | - | |
538 | - p1 = ver.p(1,1); | |
539 | - p2 = ver.p(1,0); | |
540 | - p3 = ver.p(0,0); | |
541 | - p4 = ver.p(0,1); | |
542 | - glBegin(GL_QUADS); | |
527 | + glVertex2f(v_x, v_y+n_pixels); | |
528 | + glEnd(); ///finish the quad. | |
529 | + | |
530 | + p1 = ver.p(1,1); ///generate the top right point from the vertical template. | |
531 | + p2 = ver.p(1,0); ///generate the bottom right point from the vertical template. | |
532 | + p3 = ver.p(0,0); ///generate the bottom left point from the vertical template. | |
533 | + p4 = ver.p(0,1); ///generate the top left point from the vertical template. | |
534 | + glBegin(GL_QUADS); ///generate the Quad from the 4 points. | |
543 | 535 | glTexCoord3f( |
544 | 536 | p1[0], |
545 | 537 | p1[1], |
546 | 538 | p1[2] |
547 | 539 | ); |
548 | - glVertex2f(v_x+t_length, v_y); | |
540 | + glVertex2f(v_x+n_pixels, v_y); | |
549 | 541 | glTexCoord3f( |
550 | 542 | p2[0], |
551 | 543 | p2[1], |
552 | 544 | p2[2] |
553 | 545 | ); |
554 | - glVertex2f(v_x+2.0*t_length, v_y); | |
546 | + glVertex2f(v_x+2.0*n_pixels, v_y); | |
555 | 547 | glTexCoord3f( |
556 | 548 | p3[0], |
557 | 549 | p3[1], |
558 | 550 | p3[2] |
559 | 551 | ); |
560 | - glVertex2f(v_x+2.0*t_length, v_y+t_length); | |
552 | + glVertex2f(v_x+2.0*n_pixels, v_y+n_pixels); | |
561 | 553 | glTexCoord3f( |
562 | 554 | p4[0], |
563 | 555 | p4[1], |
564 | 556 | p4[2] |
565 | 557 | ); |
566 | - glVertex2f(v_x+t_length, v_y+t_length); | |
567 | - glEnd(); | |
558 | + glVertex2f(v_x+n_pixels, v_y+n_pixels); | |
559 | + glEnd(); ///finish the quad. | |
568 | 560 | } |
569 | 561 | |
570 | 562 | |
... | ... | @@ -582,50 +574,24 @@ class gl_spider : public virtual gl_texture<T> |
582 | 574 | void |
583 | 575 | GenerateFBO(unsigned int width, unsigned int height, GLuint &textureID, GLuint &framebufferID) |
584 | 576 | { |
585 | - glDeleteFramebuffers(1, &framebufferID); | |
586 | - glGenFramebuffers(1, &framebufferID); | |
587 | - glBindFramebuffer(GL_FRAMEBUFFER, framebufferID); | |
577 | + glDeleteFramebuffers(1, &framebufferID); ///clear the framebuffer. | |
578 | + glGenFramebuffers(1, &framebufferID); ///generate a clean buffer. | |
579 | + glBindFramebuffer(GL_FRAMEBUFFER, framebufferID); ///bind the new buffer. | |
588 | 580 | // int numChannels = 1; |
589 | 581 | // unsigned char* texels = new unsigned char[width * height * numChannels]; |
590 | - glGenTextures(1, &textureID); | |
582 | + glGenTextures(1, &textureID); ///generate a texture that will attach to the buffer. | |
591 | 583 | glBindTexture(GL_TEXTURE_2D, textureID); |
592 | 584 | |
593 | 585 | //Textures repeat and use linear interpolation, luminance format. |
594 | - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | |
595 | - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | |
596 | - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
586 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); ///Set up the texture to repeat at edges. | |
587 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); | |
588 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); ///Set up the texture to use Linear interpolation | |
597 | 589 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
598 | - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, | |
590 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, ///Create the texture with no data. | |
599 | 591 | width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL); |
600 | 592 | // delete[] texels; |
601 | - glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
602 | - glBindTexture(GL_TEXTURE_2D, 0); | |
603 | - } | |
604 | - | |
605 | - ///@param uint width sets the width of the buffer. | |
606 | - ///@param uint height sets the height of the buffer. | |
607 | - ///Function for setting up the 2D buffer that stores the samples. | |
608 | - void | |
609 | - GenerateFBO(unsigned int width, unsigned int height) | |
610 | - { | |
611 | - glGenFramebuffers(1, &fboID); | |
612 | - glBindFramebuffer(GL_FRAMEBUFFER, fboID); | |
613 | -// int numChannels = 1; | |
614 | -// unsigned char* texels = new unsigned char[width * height * numChannels]; | |
615 | - glGenTextures(1, &texbufferID); | |
616 | - glBindTexture(GL_TEXTURE_2D, texbufferID); | |
617 | - | |
618 | - //Textures repeat and use linear interpolation, luminance format. | |
619 | - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); | |
620 | - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); | |
621 | - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
622 | - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
623 | - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, | |
624 | - width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL); | |
625 | -// delete[] texels; | |
626 | - glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
627 | - glBindTexture(GL_TEXTURE_2D, 0); | |
628 | - CHECK_OPENGL_ERROR | |
593 | + glBindFramebuffer(GL_FRAMEBUFFER, 0); ///Bind the frontbuffer | |
594 | + glBindTexture(GL_TEXTURE_2D, 0); ///Unbind the texture. | |
629 | 595 | } |
630 | 596 | |
631 | 597 | |
... | ... | @@ -657,13 +623,12 @@ class gl_spider : public virtual gl_texture<T> |
657 | 623 | //rotate to the current direction of the spider. |
658 | 624 | glRotatef(rot[0], rot[1], rot[2], rot[3]); |
659 | 625 | //scale to the magnitude of the spider. |
660 | - glScalef(m[0], | |
661 | - m[0], | |
662 | - m[0]); | |
626 | + glScalef(m, | |
627 | + m, | |
628 | + m); | |
663 | 629 | //get and store the current transformation matrix for later use. |
664 | 630 | glGetFloatv(GL_TEXTURE_MATRIX, curTrans); |
665 | 631 | cT.set(curTrans); |
666 | - // printTransform(); | |
667 | 632 | |
668 | 633 | CHECK_OPENGL_ERROR |
669 | 634 | //revert back to default gl mode. |
... | ... | @@ -683,31 +648,31 @@ class gl_spider : public virtual gl_texture<T> |
683 | 648 | void |
684 | 649 | Bind() |
685 | 650 | { |
686 | - glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer | |
651 | + glBindFramebuffer(GL_FRAMEBUFFER, direction_buffID);//set up GL buffer | |
687 | 652 | glFramebufferTexture2D( |
688 | 653 | GL_FRAMEBUFFER, |
689 | 654 | GL_COLOR_ATTACHMENT0, |
690 | 655 | GL_TEXTURE_2D, |
691 | - texbufferID, | |
656 | + direction_texID, | |
692 | 657 | 0); |
693 | - glBindFramebuffer(GL_FRAMEBUFFER, fboID); | |
658 | + glBindFramebuffer(GL_FRAMEBUFFER, direction_buffID); | |
694 | 659 | GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; |
695 | 660 | glDrawBuffers(1, DrawBuffers); |
696 | - glBindTexture(GL_TEXTURE_2D, texbufferID); | |
661 | + glBindTexture(GL_TEXTURE_2D, direction_texID); | |
697 | 662 | glClearColor(1,1,1,1); |
698 | 663 | glClear(GL_COLOR_BUFFER_BIT); |
699 | 664 | glMatrixMode(GL_PROJECTION); |
700 | 665 | glLoadIdentity(); |
701 | 666 | glMatrixMode(GL_MODELVIEW); |
702 | 667 | glLoadIdentity(); |
703 | - glViewport(0,0,2.0*t_length, numSamples*t_length); | |
704 | - gluOrtho2D(0.0,2.0*t_length,0.0,numSamples*t_length); | |
668 | + glViewport(0,0,2.0*n_pixels, numSamples*n_pixels); | |
669 | + gluOrtho2D(0.0,2.0*n_pixels,0.0,numSamples*n_pixels); | |
705 | 670 | glEnable(GL_TEXTURE_3D); |
706 | 671 | glBindTexture(GL_TEXTURE_3D, texID); |
707 | 672 | |
708 | 673 | CHECK_OPENGL_ERROR |
709 | 674 | } |
710 | - | |
675 | + | |
711 | 676 | ///Method for controling the buffer and texture binding. |
712 | 677 | ///Clears the buffer upon binding. |
713 | 678 | ///@param GLuint &textureID, texture to be bound. |
... | ... | @@ -717,25 +682,25 @@ class gl_spider : public virtual gl_texture<T> |
717 | 682 | Bind(GLuint &textureID, GLuint &framebufferID, int nSamples, float len = 8.0) |
718 | 683 | { |
719 | 684 | |
720 | - glBindFramebuffer(GL_FRAMEBUFFER, framebufferID);//set up GL buffer | |
721 | - glFramebufferTexture2D( | |
685 | + glBindFramebuffer(GL_FRAMEBUFFER, framebufferID); ///Bind the framebuffer. | |
686 | + glFramebufferTexture2D( ///associate it with the texture | |
722 | 687 | GL_FRAMEBUFFER, |
723 | 688 | GL_COLOR_ATTACHMENT0, |
724 | 689 | GL_TEXTURE_2D, |
725 | 690 | textureID, |
726 | 691 | 0); |
727 | - glBindFramebuffer(GL_FRAMEBUFFER, framebufferID); | |
728 | - GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; | |
729 | - glDrawBuffers(1, DrawBuffers); | |
730 | - glBindTexture(GL_TEXTURE_2D, textureID); | |
731 | - glMatrixMode(GL_PROJECTION); | |
692 | + glBindFramebuffer(GL_FRAMEBUFFER, framebufferID); ///Bind the framebuffer. | |
693 | + GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; ///generate the drawbuffer. | |
694 | + glDrawBuffers(1, DrawBuffers); ///set the drawbuffer. | |
695 | + glBindTexture(GL_TEXTURE_2D, textureID); ///Bind the texture passed. | |
696 | + glMatrixMode(GL_PROJECTION); ///clear out the draw matrices | |
732 | 697 | glLoadIdentity(); |
733 | 698 | glMatrixMode(GL_MODELVIEW); |
734 | 699 | glLoadIdentity(); |
735 | - glViewport(0,0,2.0*len, nSamples*len); | |
736 | - gluOrtho2D(0.0,2.0*len,0.0,nSamples*len); | |
737 | - glEnable(GL_TEXTURE_3D); | |
738 | - glBindTexture(GL_TEXTURE_3D, texID); | |
700 | + glViewport(0,0,2.0*len, nSamples*len); ///set up viewport | |
701 | + gluOrtho2D(0.0,2.0*len,0.0,nSamples*len); ///Set up ortho | |
702 | + glEnable(GL_TEXTURE_3D); | |
703 | + glBindTexture(GL_TEXTURE_3D, texID); ///bind the main texture (return to original state). | |
739 | 704 | |
740 | 705 | CHECK_OPENGL_ERROR |
741 | 706 | } |
... | ... | @@ -745,34 +710,16 @@ class gl_spider : public virtual gl_texture<T> |
745 | 710 | Unbind() |
746 | 711 | { |
747 | 712 | //Finalize GL_buffer |
748 | - glBindTexture(GL_TEXTURE_3D, 0); | |
713 | + glBindTexture(GL_TEXTURE_3D, 0); ///Bind the front buffer. | |
749 | 714 | CHECK_OPENGL_ERROR |
750 | - glBindTexture(GL_TEXTURE_2D, 0); | |
715 | + glBindTexture(GL_TEXTURE_2D, 0); ///Bind the default GL texture. | |
751 | 716 | CHECK_OPENGL_ERROR |
752 | - glBindFramebuffer(GL_FRAMEBUFFER, 0); | |
717 | + glBindFramebuffer(GL_FRAMEBUFFER, 0); ///Bind the defautl framebuffer. | |
753 | 718 | CHECK_OPENGL_ERROR |
754 | - glDisable(GL_TEXTURE_3D); | |
719 | + glDisable(GL_TEXTURE_3D); ///Turn off texturing. | |
755 | 720 | CHECK_OPENGL_ERROR |
756 | 721 | } |
757 | 722 | |
758 | - ///Makes the spider take a step. | |
759 | - ///starting with the current p, d, m, find the next optimal p, d, m. | |
760 | - ///Performs the branch detection on each step. | |
761 | - int | |
762 | - StepP() | |
763 | - { | |
764 | - Bind(); | |
765 | - CHECK_OPENGL_ERROR | |
766 | - findOptimalDirection(); | |
767 | - findOptimalPosition(); | |
768 | - findOptimalScale(); | |
769 | - Unbind(); | |
770 | - Bind(btexbufferID, bfboID, 27); | |
771 | - branchDetection(); | |
772 | - Unbind(); | |
773 | - return current_cost; | |
774 | - } | |
775 | - | |
776 | 723 | |
777 | 724 | |
778 | 725 | |
... | ... | @@ -785,42 +732,17 @@ class gl_spider : public virtual gl_texture<T> |
785 | 732 | ///finds the minimum cost and sets the current_cost to that value. |
786 | 733 | /// and returns the index of the template with the minimal cost. |
787 | 734 | int |
788 | - getCost() | |
789 | - { | |
790 | - stim::vec<int> cost = | |
791 | -// stim::cuda::get_cost(texbufferID, GL_TEXTURE_2D, numSamples); | |
792 | - cudaDeviceSynchronize(); | |
793 | - current_cost = cost[1]; | |
794 | - return cost[0]; | |
795 | - } | |
796 | - | |
797 | -// int | |
798 | -// getCost(GLuint tID, int n) | |
799 | -// { | |
800 | -// #ifdef TIMING | |
801 | -// gpuStartTimer(); | |
802 | -// #endif | |
803 | -// stim::vec<int> cost = | |
804 | -// stim::cuda::get_cost(tID, GL_TEXTURE_2D, n, 2*t_length, t_length); | |
805 | -// #ifdef TIMING | |
806 | -// cost_time += gpuStopTimer(); | |
807 | -// #endif | |
808 | -// current_cost = cost[1]; | |
809 | -// return cost[0]; | |
810 | -// } | |
811 | - | |
812 | - int | |
813 | 735 | getCost(cudaTextureObject_t tObj, float* result, int n) |
814 | 736 | { |
815 | 737 | #ifdef TIMING |
816 | 738 | gpuStartTimer(); |
817 | 739 | #endif |
818 | 740 | stim::vec<int> cost = |
819 | - stim::cuda::get_cost(tObj, result, n, 2*t_length, t_length); | |
741 | + stim::cuda::get_cost(tObj, result, n, 2*n_pixels, n_pixels); ///call the cuda function with the appropriate texture buffer. | |
820 | 742 | #ifdef TIMING |
821 | 743 | cost_time += gpuStopTimer(); |
822 | 744 | #endif |
823 | - current_cost = cost[1]; | |
745 | + current_cost = cost[1]; ///current cost. | |
824 | 746 | return cost[0]; |
825 | 747 | } |
826 | 748 | |
... | ... | @@ -854,17 +776,20 @@ class gl_spider : public virtual gl_texture<T> |
854 | 776 | gl_spider |
855 | 777 | (int samples = 1089, int samplespos = 441,int samplesmag = 144) |
856 | 778 | { |
857 | -// std::cout << "I ran this constructor" << std::endl; | |
858 | 779 | p = stim::vec3<float>(0.0, 0.0, 0.0); |
859 | 780 | d = stim::vec3<float>(0.0, 0.0, 1.0); |
860 | - m = stim::vec<float>(1.0, 1.0); | |
781 | + m = 1.0; | |
861 | 782 | S = stim::vec3<float>(1.0, 1.0, 1.0); |
862 | 783 | R = stim::vec3<float>(1.0, 1.0, 1.0); |
863 | -// std::cout << samples << std::endl; | |
864 | 784 | numSamples = samples; |
865 | -// std::cout << numSamples << std::endl; | |
866 | 785 | numSamplesPos = samplespos; |
867 | 786 | numSamplesMag = samplesmag; |
787 | + #ifdef DEBUG | |
788 | + iter = 0; | |
789 | + iter_pos = 0; | |
790 | + iter_dir = 0; | |
791 | + iter_siz = 0; | |
792 | + #endif | |
868 | 793 | } |
869 | 794 | |
870 | 795 | ///Position constructor: floats. |
... | ... | @@ -882,12 +807,18 @@ class gl_spider : public virtual gl_texture<T> |
882 | 807 | { |
883 | 808 | p = stim::vec3<float>(pos_x, pos_y, pos_z); |
884 | 809 | d = stim::vec3<float>(dir_x, dir_y, dir_z); |
885 | - m = stim::vec<float>(mag_x, mag_x, mag_x); | |
810 | + m = mag_x; | |
886 | 811 | S = stim::vec3<float>(1.0,1.0,1.0); |
887 | 812 | R = stim::vec3<float>(1.0,1.0,1.0); |
888 | 813 | numSamples = numsamples; |
889 | 814 | numSamplesPos = numsamplespos; |
890 | 815 | numSamplesMag = numsamplesmag; |
816 | + #ifdef DEBUG | |
817 | + iter = 0; | |
818 | + iter_pos = 0; | |
819 | + iter_dir = 0; | |
820 | + iter_siz = 0; | |
821 | + #endif | |
891 | 822 | } |
892 | 823 | |
893 | 824 | ///Position constructor: vecs of floats. |
... | ... | @@ -900,12 +831,18 @@ class gl_spider : public virtual gl_texture<T> |
900 | 831 | { |
901 | 832 | p = pos; |
902 | 833 | d = dir; |
903 | - m = vec<float>(mag, mag, mag); | |
834 | + m = mag; | |
904 | 835 | S = vec3<float>(1.0,1.0,1.0); |
905 | 836 | R = vec3<float>(1.0,1.0,1.0); |
906 | 837 | numSamples = samples; |
907 | 838 | numSamplesPos = samplesPos; |
908 | 839 | numSamplesMag = samplesMag; |
840 | + #ifdef DEBUG | |
841 | + iter = 0; | |
842 | + iter_pos = 0; | |
843 | + iter_dir = 0; | |
844 | + iter_siz = 0; | |
845 | + #endif | |
909 | 846 | } |
910 | 847 | |
911 | 848 | ///destructor |
... | ... | @@ -913,14 +850,14 @@ class gl_spider : public virtual gl_texture<T> |
913 | 850 | (void) |
914 | 851 | { |
915 | 852 | Unbind(); |
916 | - glDeleteTextures(1, &texbufferID); | |
917 | - glDeleteBuffers(1, &fboID); | |
918 | - glDeleteTextures(1, &ptexbufferID); | |
919 | - glDeleteBuffers(1, &pfboID); | |
920 | - glDeleteTextures(1, &mtexbufferID); | |
921 | - glDeleteBuffers(1, &mfboID); | |
922 | - glDeleteTextures(1, &btexbufferID); | |
923 | - glDeleteBuffers(1, &bfboID); | |
853 | + glDeleteTextures(1, &direction_texID); | |
854 | + glDeleteBuffers(1, &direction_buffID); | |
855 | + glDeleteTextures(1, &position_texID); | |
856 | + glDeleteBuffers(1, &position_buffID); | |
857 | + glDeleteTextures(1, &radius_texID); | |
858 | + glDeleteBuffers(1, &radius_buffID); | |
859 | + glDeleteTextures(1, &cylinder_texID); | |
860 | + glDeleteBuffers(1, &cylinder_buffID); | |
924 | 861 | } |
925 | 862 | |
926 | 863 | ///@param GLuint id, texture that is going to be sampled. |
... | ... | @@ -939,45 +876,45 @@ class gl_spider : public virtual gl_texture<T> |
939 | 876 | network_time = 0; |
940 | 877 | hit_time = 0; |
941 | 878 | #endif |
942 | - stepsize = 2.5; | |
943 | - t_length = 16.0; | |
879 | +#ifdef DEBUG | |
880 | + iter = 0; | |
881 | + iter_pos = 0; | |
882 | + iter_dir = 0; | |
883 | + iter_siz = 0; | |
884 | +#endif | |
885 | + stepsize = 10.0; | |
886 | + n_pixels = 16.0; | |
944 | 887 | |
945 | 888 | srand(100); |
946 | 889 | texID = id; |
947 | - //GenerateFBO(16, numSamples*8); | |
948 | - GenerateFBO(t_length*2, numSamples*t_length, texbufferID, fboID); | |
949 | - std::cout << numSamples << std::endl; | |
890 | + GenerateFBO(n_pixels*2, numSamples*n_pixels, direction_texID, direction_buffID); | |
950 | 891 | CHECK_OPENGL_ERROR |
951 | - GenerateFBO(t_length*2, numSamplesPos*t_length, ptexbufferID, pfboID); | |
952 | - std::cout << numSamplesPos << std::endl; | |
892 | + GenerateFBO(n_pixels*2, numSamplesPos*n_pixels, position_texID, position_buffID); | |
953 | 893 | CHECK_OPENGL_ERROR |
954 | - GenerateFBO(t_length*2, numSamplesMag*t_length, mtexbufferID, mfboID); | |
955 | - std::cout << numSamplesMag << std::endl; | |
894 | + GenerateFBO(n_pixels*2, numSamplesMag*n_pixels, radius_texID, radius_buffID); | |
956 | 895 | CHECK_OPENGL_ERROR |
957 | - GenerateFBO(16, 216, btexbufferID, bfboID); | |
896 | + GenerateFBO(16, 216, cylinder_texID, cylinder_buffID); | |
958 | 897 | CHECK_OPENGL_ERROR |
959 | - t_dir.MapCudaTexture(texbufferID, GL_TEXTURE_2D); | |
898 | + t_dir.MapCudaTexture(direction_texID, GL_TEXTURE_2D); | |
960 | 899 | t_dir.Alloc(numSamples); |
961 | - t_pos.MapCudaTexture(ptexbufferID, GL_TEXTURE_2D); | |
900 | + t_pos.MapCudaTexture(position_texID, GL_TEXTURE_2D); | |
962 | 901 | t_pos.Alloc(numSamplesPos); |
963 | - t_mag.MapCudaTexture(mtexbufferID, GL_TEXTURE_2D); | |
902 | + t_mag.MapCudaTexture(radius_texID, GL_TEXTURE_2D); | |
964 | 903 | t_mag.Alloc(numSamplesMag); |
965 | -// setDims(0.6, 0.6, 1.0); | |
966 | -// setSize(1024.0, 1024.0, 1024.0); | |
967 | 904 | setMatrix(); |
968 | 905 | dList = glGenLists(3); |
969 | 906 | glListBase(dList); |
970 | - Bind(texbufferID, fboID, numSamples, t_length); | |
971 | - genDirectionVectors(5*M_PI/4); | |
907 | + Bind(direction_texID, direction_buffID, numSamples, n_pixels); | |
908 | + genDirectionVectors(5*stim::PI/4); | |
972 | 909 | Unbind(); |
973 | - Bind(ptexbufferID, pfboID, numSamplesPos, t_length); | |
974 | - genPositionVectors(); | |
910 | + Bind(position_texID, position_buffID, numSamplesPos, n_pixels); | |
911 | + genPositionVectors(0.2); | |
975 | 912 | Unbind(); |
976 | - Bind(mtexbufferID, mfboID, numSamplesMag, t_length); | |
913 | + Bind(radius_texID, radius_buffID, numSamplesMag, n_pixels); | |
977 | 914 | genMagnitudeVectors(); |
978 | 915 | Unbind(); |
979 | - Bind(btexbufferID, bfboID, 27); | |
980 | - DrawCylinder(); | |
916 | + Bind(cylinder_texID, cylinder_buffID, 27); | |
917 | +// DrawCylinder(); | |
981 | 918 | Unbind(); |
982 | 919 | } |
983 | 920 | |
... | ... | @@ -999,7 +936,7 @@ class gl_spider : public virtual gl_texture<T> |
999 | 936 | } |
1000 | 937 | |
1001 | 938 | ///Returns the m vector. |
1002 | - stim::vec<float> | |
939 | + float | |
1003 | 940 | getMagnitude() |
1004 | 941 | { |
1005 | 942 | return m; |
... | ... | @@ -1045,22 +982,13 @@ class gl_spider : public virtual gl_texture<T> |
1045 | 982 | d[2] = z; |
1046 | 983 | } |
1047 | 984 | |
1048 | - ///@param stim::vec<float> dir, the new d. | |
1049 | - ///Sets the m vector to the input vector mag. | |
1050 | - void | |
1051 | - setMagnitude(stim::vec<float> mag) | |
1052 | - { | |
1053 | - m[0] = mag[0]; | |
1054 | - m[1] = mag[0]; | |
1055 | - } | |
1056 | 985 | |
1057 | 986 | ///@param float mag, size of the sampled region. |
1058 | 987 | ///Sets the m vector to the input mag for both templates. |
1059 | 988 | void |
1060 | 989 | setMagnitude(float mag) |
1061 | 990 | { |
1062 | - m[0] = mag; | |
1063 | - m[1] = mag; | |
991 | + m = mag; | |
1064 | 992 | } |
1065 | 993 | |
1066 | 994 | ///@param float x, voxel size in the x direction. |
... | ... | @@ -1111,17 +1039,27 @@ class gl_spider : public virtual gl_texture<T> |
1111 | 1039 | getRotation(stim::vec3<float> dir) |
1112 | 1040 | { |
1113 | 1041 | stim::vec<float> out(0.0,0.0,0.0,0.0); |
1114 | - stim::vec<float> from(0.0,0.0,1.0); | |
1115 | - out[0] = acos(dir.dot(from))*180/M_PI; | |
1116 | - if(out[0] < 1.0){ | |
1042 | + stim::vec3<float> from(0.0,0.0,1.0); | |
1043 | + out[0] = acos(dir.dot(from))*180/stim::PI; | |
1044 | + #ifdef DEBUG | |
1045 | + std::cout << "out is " << out << std::endl; | |
1046 | + std::cout << "when rotating from " << from << " to " << dir << std::endl; | |
1047 | + #endif | |
1048 | + if(out[0] < 0.01){ | |
1117 | 1049 | out[0] = 0.0; |
1118 | 1050 | out[1] = 0.0; |
1119 | 1051 | out[2] = 0.0; |
1120 | 1052 | out[3] = 1.0; |
1053 | + } | |
1054 | + else if(out[0] < -180.0+0.01) | |
1055 | + { | |
1056 | + out[0] = 180.0; | |
1057 | + out[1] = 1.0; | |
1058 | + out[2] = 0.0; | |
1059 | + out[3] = 0.0; | |
1121 |