Commit ad9345728682e41b55856cb4f4572bba8738096a

Authored by David Mayerich
2 parents d11e2fb6 fac43319

Merge branch 'master' of git.stim.ee.uh.edu:codebase/stimlib

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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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&lt;T&gt;
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 1060 } else {
1122   - stim::vec<float> temp(0.0, 0.0, 0.0);;
1123   - stim::vec<float> dir1(dir[0], dir[1], dir[2]);
1124   - temp = (from.cross(dir1)).norm();
  1061 + stim::vec3<float> temp(0.0, 0.0, 0.0);;
  1062 + temp = (from.cross(dir)).norm();
1125 1063 out[1] = temp[0];
1126 1064 out[2] = temp[1];
1127 1065 out[3] = temp[2];
... ... @@ -1152,7 +1090,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1152 1090 void
1153 1091 setSeedMag(float mag)
1154 1092 {
1155   -// std::cout << "sMag: " << mag << std::endl;
1156 1093 seedsmags.push(mag);
1157 1094 }
1158 1095  
... ... @@ -1165,7 +1102,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1165 1102 void
1166 1103 setSeed(float x, float y, float z)
1167 1104 {
1168   -// std::cout << "sPos: " << x << " " << y << " " << z << std::endl;
1169 1105 seeds.push(stim::vec<float>(x, y, z));
1170 1106 }
1171 1107  
... ... @@ -1176,7 +1112,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1176 1112 void
1177 1113 setSeedVec(float x, float y, float z)
1178 1114 {
1179   -// std::cout << "sDir: " << x << " " << y << " " << z << std::endl;
1180 1115 seedsvecs.push(stim::vec<float>(x, y, z));
1181 1116 }
1182 1117  
... ... @@ -1297,7 +1232,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1297 1232 void
1298 1233 saveNetwork(std::string name)
1299 1234 {
1300   - stim::glObj<float> sk;
  1235 +/* stim::glObj<float> sk;
1301 1236 for(int i = 0; i < nt.sizeE(); i++)
1302 1237 {
1303 1238 std::vector<stim::vec< float > > cm = nt.getEdgeCenterLineMag(i);
... ... @@ -1310,7 +1245,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1310 1245 }
1311 1246 sk.End();
1312 1247 }
1313   - sk.save(name);
  1248 +*/ sk.save(name);
1314 1249 }
1315 1250  
1316 1251 ///Depreciated, but might be reused later()
... ... @@ -1318,7 +1253,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1318 1253 stim::glObj<float>
1319 1254 getNetwork()
1320 1255 {
1321   -
  1256 + return sk;
1322 1257 }
1323 1258  
1324 1259 ///returns a COPY of the entire stim::glnetwork object.
... ... @@ -1333,7 +1268,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1333 1268 GLuint
1334 1269 getFB()
1335 1270 {
1336   - return bfboID;
  1271 + return cylinder_buffID;
1337 1272 }
1338 1273  
1339 1274 //--------------------------------------------------------------------------//
... ... @@ -1357,14 +1292,17 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1357 1292 int
1358 1293 Step()
1359 1294 {
1360   - Bind(texbufferID, fboID, numSamples, t_length);
  1295 + #ifdef DEBUG
  1296 + std::cerr << "Took a step" << std::endl;
  1297 + #endif
  1298 + Bind(direction_texID, direction_buffID, numSamples, n_pixels);
1361 1299 CHECK_OPENGL_ERROR
1362 1300 findOptimalDirection();
1363 1301 Unbind();
1364   - Bind(ptexbufferID, pfboID, numSamplesPos, t_length);
  1302 + Bind(position_texID, position_buffID, numSamplesPos, n_pixels);
1365 1303 findOptimalPosition();
1366 1304 Unbind();
1367   - Bind(mtexbufferID, mfboID, numSamplesMag, t_length);
  1305 + Bind(radius_texID, radius_buffID, numSamplesMag, n_pixels);
1368 1306 findOptimalScale();
1369 1307 Unbind();
1370 1308 CHECK_OPENGL_ERROR
... ... @@ -1385,16 +1323,16 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1385 1323 //--------------------------------------------------------------------------//
1386 1324  
1387 1325 void
1388   - MonteCarloDirectionVectors(int nSamples, float solidAngle = 2*M_PI)
  1326 + MonteCarloDirectionVectors(int nSamples, float solidAngle = stim::TAU)
1389 1327 {
1390   - float PHI[2], Z[2], range;
1391   - PHI[0] = asin(solidAngle/2);
1392   - PHI[1] = asin(0);
  1328 +// float PHI[2];//, Z[2];//, range;
  1329 +// PHI[0] = asin(solidAngle/2);
  1330 +// PHI[1] = asin(0);
1393 1331  
1394   - Z[0] = cos(PHI[0]);
1395   - Z[1] = cos(PHI[1]);
  1332 +// Z[0] = cos(PHI[0]);
  1333 +// Z[1] = cos(PHI[1]);
1396 1334  
1397   - range = Z[0] - Z[1];
  1335 +// range = Z[0] - Z[1];
1398 1336  
1399 1337 std::vector<stim::vec3<float> > vecsUni;
1400 1338 for(int i = 0; i < numSamplesPos; i++)
... ... @@ -1428,8 +1366,8 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1428 1366 int j = 0;
1429 1367 for(float i = step; i <= 360.0; i += step)
1430 1368 {
1431   - x=r0*cos(i*2.0*M_PI/360.0);
1432   - y=r0*sin(i*2.0*M_PI/360.0);
  1369 + x=r0*cos(i*stim::TAU/360.0);
  1370 + y=r0*sin(i*stim::TAU/360.0);
1433 1371 glTexCoord3f(x,y,z0);
1434 1372 glVertex2f(0.0, j*6.4+6.4);
1435 1373 // glVertex2f(0.0, j*27.0+27.0);
... ... @@ -1451,12 +1389,13 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1451 1389 }
1452 1390  
1453 1391 ///need to return the cylinder.
  1392 +///SOMETHING MIGHT BE GOING ON HERE IN GENERATE BUFFER.
1454 1393 void
1455 1394 DrawLongCylinder(int n = 8, int l_template = 8,int l_square = 8)
1456 1395 {
1457 1396 int cylLen = cL.size()-1;
1458   - GenerateFBO(n*l_square, cylLen*l_template, btexbufferID, bfboID);
1459   - Bind(btexbufferID, bfboID, cylLen, l_template*l_square/2.0);
  1397 + GenerateFBO(n*l_square, cylLen*l_template, cylinder_texID, cylinder_buffID);
  1398 + Bind(cylinder_texID, cylinder_buffID, cylLen, l_template*l_square/2.0);
1460 1399 stim::cylinder<float> cyl(cL, cM);
1461 1400 std::vector<std::vector<stim::vec3<float> > > p = cyl.getPoints(n);
1462 1401 for(int i = 0; i < p.size()-1; i++) ///number of circles
... ... @@ -1502,14 +1441,28 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1502 1441 seeds.pop();
1503 1442 seedsvecs.pop();
1504 1443 seedsmags.pop();
1505   -// std::cout << "The current seed Vector is " << curSeedVec << std::endl;
1506 1444 setPosition(curSeed);
1507 1445 setDirection(curSeedVec);
1508 1446 setMagnitude(curSeedMag);
  1447 +
  1448 + #ifdef DEBUG
  1449 + std::cout << "The new seed " << curSeed << curSeedVec << curSeedMag << std::endl;
  1450 + #endif
  1451 +
  1452 +// Bind(direction_texID, direction_buffID, numSamples, n_pixels);
  1453 +// CHECK_OPENGL_ERROR
  1454 +// findOptimalDirection();
  1455 +// Unbind();
  1456 +//THIS IS EXPERIMENTAL
  1457 + Bind(radius_texID, radius_buffID, numSamplesMag, n_pixels);
  1458 + findOptimalScale();
  1459 + Unbind();
  1460 +//THIS IS EXPERIMENTAL
  1461 +
1509 1462 // cL.push_back(curSeed);
1510 1463 // cM.push_back(curSeedMag);
1511 1464 // cD.push_back(curSeedMag);
1512   - pair<stim::fiber<float>, int> a = traceLine(p, m, min_cost);
  1465 + traceLine(p, m, min_cost);
1513 1466 }
1514 1467 }
1515 1468  
... ... @@ -1556,17 +1509,17 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1556 1509 ds[0], ds[1], ds[2],
1557 1510 ups[0], ups[1], ups[2]);
1558 1511  
1559   -// sk.Render();
1560   - nt.Render();
  1512 + sk.Render();
  1513 +// nt.Render();
1561 1514  
1562 1515 CHECK_OPENGL_ERROR
1563 1516  
1564 1517  
1565   -// glLoadName((int) sk.numL());
1566   - glLoadName(nt.sizeE());
  1518 + glLoadName((int) sk.numL());
  1519 +// glLoadName(nt.sizeE());
1567 1520  
1568   -// sk.RenderLine(cL);
1569   - nt.RenderLine(cL);
  1521 + sk.RenderLine(cL);
  1522 +// nt.RenderLine(cL);
1570 1523  
1571 1524 // glPopName();
1572 1525 glFlush();
... ... @@ -1615,8 +1568,9 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1615 1568 {
1616 1569 cL.clear();
1617 1570 cM.clear();
1618   - }
  1571 + }
1619 1572  
  1573 +/*
1620 1574 void
1621 1575 addToNetwork(pair<stim::fiber<float>, int> in, stim::vec3<float> spos,
1622 1576 stim::vec<float> smag, stim::vec3<float> sdir)
... ... @@ -1664,13 +1618,26 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1664 1618 network_time += nt * 1000.0;
1665 1619 #endif
1666 1620 }
  1621 +*/
  1622 + void
  1623 + addToNetwork(std::vector<stim::vec3<float> > L, std::vector<float > M)
  1624 + {
  1625 + if(L.size() > 3)
  1626 + {
  1627 + sk.Begin(stim::OBJ_LINE);
  1628 + for(int i = 0; i < L.size(); i++)
  1629 + {
  1630 + sk.TexCoord(M[i]);
  1631 + sk.Vertex(L[i][0], L[i][1], L[i][2]);
  1632 + }
  1633 + sk.End();
1667 1634  
1668   -// void
1669   -// addToNetwork(pair<stim::fiber<float>, int> in, stim::vec3<float> spos,
1670   -// stim::vec<float> smag, stim::vec3<float> sdir)
1671   -// {
1672   -//
1673   -// }
  1635 + nt.addEdge(L,M);
  1636 + #ifdef DEBUG
  1637 + iter++;
  1638 + #endif
  1639 + }
  1640 + }
1674 1641  
1675 1642  
1676 1643 void
... ... @@ -1678,23 +1645,22 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1678 1645 {
1679 1646 std::cout << nt.sizeE() << " edges " << std::endl;
1680 1647 std::cout << nt.sizeV() << " nodes " << std::endl;
1681   -
1682 1648 }
1683 1649  
1684   - std::pair<stim::fiber<float>, int >
1685   - traceLine(stim::vec3<float> pos, stim::vec<float> mag, int min_cost)
  1650 + void
  1651 + traceLine(stim::vec3<float> pos, float mag, int min_cost)
1686 1652 {
1687 1653 //starting (seed) position and magnitude.
1688 1654 stim::vec3<float> spos = getPosition();
1689   - stim::vec<float> smag = getMagnitude();
  1655 + float smag = getMagnitude();
1690 1656 stim::vec3<float> sdir = getDirection();
1691 1657  
1692   - Bind();
  1658 +// Bind();
1693 1659 // sk.Begin(stim::OBJ_LINE);
1694 1660  
1695 1661  
1696   -// sk.createFromSelf(GL_SELECT);
1697   - nt.createFromSelf(GL_SELECT);
  1662 + sk.createFromSelf(GL_SELECT);
  1663 +// nt.createFromSelf(GL_SELECT);
1698 1664  
1699 1665 cL.push_back(pos);
1700 1666 cM.push_back(mag);
... ... @@ -1710,12 +1676,11 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1710 1676 int cost = Step();
1711 1677 if (cost > min_cost){
1712 1678 running = false;
1713   -// sk.End();
1714 1679 branchDetection2();
1715   - pair<stim::fiber<float>, int> a(stim::fiber<float> (cL, cM), -1);
1716   - addToNetwork(a, spos, smag, sdir);
1717   -// std::cout << "the cost of " << cost << " > " << min_cost << std::endl;
1718   - return a;
  1680 + addToNetwork(cL, cM);
  1681 + #ifdef DEBUG
  1682 + std::cerr << "the cost of " << cost << " > " << min_cost << std::endl;
  1683 + #endif
1719 1684 break;
1720 1685 } else {
1721 1686 //Have we found the edge of the map?
... ... @@ -1726,10 +1691,10 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1726 1691 {
1727 1692 running = false;
1728 1693 branchDetection2();
1729   - pair<stim::fiber<float>, int> a(stim::fiber<float> (cL, cM), -1);
1730   - addToNetwork(a, spos, smag, sdir);
1731   -// std::cout << "I hit and edge" << std::endl;
1732   - return a;
  1694 + addToNetwork(cL, cM);
  1695 + #ifdef DEBUG
  1696 + std::cerr << "I hit and edge" << std::endl;
  1697 + #endif
1733 1698 break;
1734 1699 }
1735 1700 //If this is the first step in the trace,
... ... @@ -1741,39 +1706,40 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
1741 1706 }
1742 1707 //Has the template size gotten unreasonable?
1743 1708 mag = getMagnitude();
1744   - if(mag[0] > 75 || mag[0] < 1){
  1709 + if(mag > 75 || mag < 1){
1745 1710 running = false;
1746 1711 branchDetection2();
1747   - pair<stim::fiber<float>, int> a(stim::fiber<float> (cL, cM), -1);
1748   - addToNetwork(a, spos, smag, sdir);
1749   -// std::cout << "The templates are too big" << std::endl;
1750   - return a;
  1712 + addToNetwork(cL, cM);
  1713 + #ifdef DEBUG
  1714 + std::cerr << "The templates are too big" << std::endl;
  1715 + #endif
1751 1716 break;
1752 1717 }
1753 1718 else
1754 1719 {
1755   - h = selectObject(p, getDirection(), m[0]);
  1720 + h = selectObject(p, getDirection(), m);
1756 1721 //Have we hit something previously traced?
1757 1722 if(h != -1){
1758   -// std::cout << "I hit the fiber " << h << std::endl;
  1723 + #ifdef DEBUG
  1724 + std::cerr << "I hit the fiber " << h << std::endl;
  1725 + #endif
1759 1726 running = false;
1760 1727 branchDetection2();
1761   - pair<stim::fiber<float>, int> a(stim::fiber<float> (cL, cM), h);
1762   - addToNetwork(a, spos, smag, sdir);
1763   - return a;
  1728 + addToNetwork(cL, cM);
1764 1729 break;
1765 1730 }
1766 1731 else {
1767 1732 cL.push_back(stim::vec3<float>(p[0], p[1],p[2]));
1768   - cM.push_back(stim::vec<float>(m[0], m[0]));
1769   -// Bind(btexbufferID, bfboID, 27);
1770   - Unbind();
  1733 + cM.push_back(m);
  1734 +// Unbind();
1771 1735 CHECK_OPENGL_ERROR
1772   -
1773 1736 }
1774 1737 }
1775 1738 }
1776 1739 }
  1740 + #ifdef DEBUG
  1741 + std::cout << "I broke out!" << std::endl;
  1742 + #endif
1777 1743 }
1778 1744 };
1779 1745 }
... ...
stim/gl/gl_texture.h
... ... @@ -64,7 +64,7 @@ class gl_texture : public virtual image_stack&lt;T, F&gt;
64 64 case 4:
65 65 return GL_RGBA;
66 66 default:
67   - std::cout<<"Error in stim::gl_texture - unable to guess texture format based on number of channels ("<<R[4]<<")"<<std::endl;
  67 + std::cout<<"Error in stim::gl_texture - unable to guess texture format based on number of channels" << std::endl;
68 68 exit(1);
69 69 }
70 70 }
... ... @@ -236,7 +236,7 @@ class gl_texture : public virtual image_stack&lt;T, F&gt;
236 236 if(texID == 0) generate_texture(); //generate the texture if it doesn't already exist
237 237 else{
238 238 std::cout<<"Texture has already been attached to a context."<<std::endl;
239   - exit(1);
  239 + //exit(1); Why do we need to quit if it's attached. print the message and continue.
240 240 }
241 241 }
242 242  
... ...
stim/math/circle.h
... ... @@ -48,9 +48,9 @@ public:
48 48 CUDA_CALLABLE
49 49 circle(T size, T z_pos = (T)0) : plane<T>()
50 50 {
51   - init();
52 51 center(stim::vec3<T>(0,0,z_pos));
53 52 scale(size);
  53 + init();
54 54 }
55 55  
56 56 ///create a rectangle from a center point, normal
... ... @@ -88,6 +88,7 @@ public:
88 88 {
89 89 init();
90 90 setU(u);
  91 +// U = u;
91 92 center(c);
92 93 normal(n);
93 94 scale(s);
... ... @@ -174,6 +175,13 @@ public:
174 175 return p(x,y);
175 176 }
176 177  
  178 + std::string str() const
  179 + {
  180 + std::stringstream ss;
  181 + ss << "(P=" << P.str() << ", N=" << N.str() << ", U=" << U.str() << ", Y=" << Y.str();
  182 + return ss.str();
  183 + }
  184 +
177 185 };
178 186 }
179 187 #endif
... ...
stim/math/vec3.h
... ... @@ -80,6 +80,15 @@ public:
80 80 return sph;
81 81 }
82 82  
  83 + CUDA_CALLABLE vec3<T> cart2cyl() const{
  84 + vec3<T> cyl;
  85 + cyl.ptr[0] = sqrt(pow(ptr[0],2) + pow(ptr[1],2));
  86 + cyl.ptr[1] = atan(ptr[1]/ptr[0]);
  87 + cyl.ptr[2] = ptr[2];
  88 +
  89 + return cyl;
  90 + }
  91 +
83 92 /// Convert the vector from cartesian to spherical coordinates (r, theta, phi -> x, y, z where theta = [0, 2*pi])
84 93 CUDA_CALLABLE vec3<T> sph2cart() const{
85 94 vec3<T> cart;
... ... @@ -90,6 +99,16 @@ public:
90 99 return cart;
91 100 }
92 101  
  102 + /// Convert the vector from cylindrical to cart coordinates (r, theta, z -> x, y, z where theta = [0, 2*pi])
  103 + CUDA_CALLABLE vec3<T> cyl2cart() const{
  104 + vec3<T> cart;
  105 + cart.ptr[0] = ptr[0] * std::cos(ptr[1]);
  106 + cart.ptr[1] = ptr[0] * std::sin(ptr[1]);
  107 + cart.ptr[2] = ptr[2];
  108 +
  109 + return cart;
  110 + }
  111 +
93 112 /// Computes the normalized vector (where each coordinate is divided by the L2 norm)
94 113 CUDA_CALLABLE vec3<T> norm() const{
95 114 vec3<T> result;
... ...
stim/visualization/cylinder.h
... ... @@ -2,19 +2,27 @@
2 2 #define STIM_CYLINDER_H
3 3 #include <iostream>
4 4 #include <stim/math/circle.h>
5   -#include <stim/math/vec3.h>
  5 +#include <stim/biomodels/centerline.h>
6 6  
7 7  
8 8 namespace stim
9 9 {
10 10 template<typename T>
11 11 class cylinder
  12 + : public centerline<T>
12 13 {
13 14 private:
14 15 stim::circle<T> s; //an arbitrary circle
15   - std::vector<stim::circle<T> > e;
  16 + std::vector<stim::circle<T> > e; //an array of circles that store the centerline
  17 +
  18 + std::vector<stim::vec3<T> > norms;
  19 + std::vector<stim::vec<T> > Us;
16 20 std::vector<stim::vec<T> > mags;
17 21 std::vector< T > L; //length of the cylinder at each position.
  22 +
  23 +
  24 + using stim::centerline<T>::c;
  25 + using stim::centerline<T>::N;
18 26  
19 27 ///default init
20 28 void
... ... @@ -23,6 +31,20 @@ class cylinder
23 31  
24 32 }
25 33  
  34 + ///Calculate the physical length of the fiber at each point in the fiber.
  35 + void
  36 + calculateL()
  37 + {
  38 +/* L.resize(inP.size());
  39 + T temp = (T)0;
  40 + L[0] = 0;
  41 + for(size_t i = 1; i < L.size(); i++)
  42 + {
  43 + temp += (inP[i-1] - inP[i]).len();
  44 + L[i] = temp;
  45 + }
  46 +*/ }
  47 +
26 48 ///inits the cylinder from a list of points (std::vector of stim::vec3 --inP) and radii (inM)
27 49 void
28 50 init(std::vector<stim::vec3<T> > inP, std::vector<stim::vec<T> > inM)
... ... @@ -31,6 +53,10 @@ class cylinder
31 53 stim::vec3<float> v1;
32 54 stim::vec3<float> v2;
33 55 e.resize(inP.size());
  56 +
  57 + norms.resize(inP.size());
  58 + Us.resize(inP.size());
  59 +
34 60 if(inP.size() < 2)
35 61 return;
36 62  
... ... @@ -46,7 +72,9 @@ class cylinder
46 72  
47 73 stim::vec3<T> dr = (inP[1] - inP[0]).norm();
48 74 s = stim::circle<T>(inP[0], inM[0][0], dr, stim::vec3<T>(1,0,0));
  75 + norms[0] = s.N;
49 76 e[0] = s;
  77 + Us[0] = e[0].U;
50 78 for(size_t i = 1; i < inP.size()-1; i++)
51 79 {
52 80 s.center(inP[i]);
... ... @@ -54,16 +82,24 @@ class cylinder
54 82 v2 = (inP[i+1] - inP[i]).norm();
55 83 dr = (v1+v2).norm();
56 84 s.normal(dr);
  85 +
  86 + norms[i] = s.N;
  87 +
57 88 s.scale(inM[i][0]/inM[i-1][0]);
58 89 e[i] = s;
  90 + Us[i] = e[i].U;
59 91 }
60 92  
61 93 int j = inP.size()-1;
62 94 s.center(inP[j]);
63 95 dr = (inP[j] - inP[j-1]).norm();
64 96 s.normal(dr);
  97 +
  98 + norms[j] = s.N;
  99 +
65 100 s.scale(inM[j][0]/inM[j-1][0]);
66 101 e[j] = s;
  102 + Us[j] = e[j].U;
67 103 }
68 104  
69 105 ///returns the direction vector at point idx.
... ... @@ -72,18 +108,42 @@ class cylinder
72 108 {
73 109 if(idx == 0)
74 110 {
75   - return (e[idx+1].P - e[idx].P).norm();
  111 + stim::vec3<T> temp(
  112 + c[idx+1][0]-c[idx][0],
  113 + c[idx+1][1]-c[idx][1],
  114 + c[idx+1][2]-c[idx][2]
  115 + );
  116 +// return (e[idx+1].P - e[idx].P).norm();
  117 + return (temp.norm());
76 118 }
77   - else if(idx == e.size()-1)
  119 + else if(idx == N-1)
78 120 {
79   - return (e[idx].P - e[idx-1].P).norm();
  121 + stim::vec3<T> temp(
  122 + c[idx][0]-c[idx+1][0],
  123 + c[idx][1]-c[idx+1][1],
  124 + c[idx][2]-c[idx+1][2]
  125 + );
  126 + // return (e[idx].P - e[idx-1].P).norm();
  127 + return (temp.norm());
80 128 }
81 129 else
82 130 {
83 131 // return (e[idx+1].P - e[idx].P).norm();
84   - stim::vec3<float> v1 = (e[idx].P-e[idx-1].P).norm();
85   - stim::vec3<float> v2 = (e[idx+1].P-e[idx].P).norm();
86   - return (v1+v2).norm();
  132 +// stim::vec3<float> v1 = (e[idx].P-e[idx-1].P).norm();
  133 + stim::vec3<T> v1(
  134 + c[idx][0]-c[idx-1][0],
  135 + c[idx][1]-c[idx-1][1],
  136 + c[idx][2]-c[idx-1][2]
  137 + );
  138 +
  139 +// stim::vec3<float> v2 = (e[idx+1].P-e[idx].P).norm();
  140 + stim::vec3<T> v2(
  141 + c[idx+1][0]-c[idx][0],
  142 + c[idx+1][1]-c[idx][1],
  143 + c[idx+1][2]-c[idx][2]
  144 + );
  145 +
  146 + return (v1.norm()+v2.norm()).norm();
87 147 }
88 148 // return e[idx].N;
89 149  
... ... @@ -92,14 +152,17 @@ class cylinder
92 152 stim::vec3<T>
93 153 d(T l, int idx)
94 154 {
95   - if(idx == 0 || idx == e.size()-1)
  155 + if(idx == 0 || idx == N-1)
96 156 {
97   - return e[idx].N;
  157 + return norms[idx];
  158 +// return e[idx].N;
98 159 }
99 160 else
100 161 {
  162 +
101 163 T rat = (l-L[idx])/(L[idx+1]-L[idx]);
102   - return( e[idx].N + (e[idx+1].N - e[idx].N)*rat);
  164 + return( norms[idx] + (norms[idx+1] - norms[idx])*rat);
  165 +// return( e[idx].N + (e[idx+1].N - e[idx].N)*rat);
103 166 }
104 167 }
105 168  
... ... @@ -137,6 +200,7 @@ class cylinder
137 200 public:
138 201 ///default constructor
139 202 cylinder()
  203 + // : centerline<T>()
140 204 {
141 205  
142 206 }
... ... @@ -144,15 +208,33 @@ class cylinder
144 208 ///constructor to create a cylinder from a set of points, radii, and the number of sides for the cylinder.
145 209 ///@param inP: Vector of stim vec3 composing the points of the centerline.
146 210 ///@param inM: Vector of stim vecs composing the radii of the centerline.
147   - cylinder(std::vector<stim::vec3<T> > inP, std::vector<stim::vec<T> > inM){
  211 + cylinder(std::vector<stim::vec3<T> > inP, std::vector<stim::vec<T> > inM)
  212 + : centerline<T>(inP)
  213 + {
148 214 init(inP, inM);
149 215 }
150 216  
  217 + ///constructor to create a cylinder from a set of points, radii, and the number of sides for the cylinder.
  218 + ///@param inP: Vector of stim vec3 composing the points of the centerline.
  219 + ///@param inM: Vector of stim vecs composing the radii of the centerline.
  220 + cylinder(std::vector<stim::vec3<T> > inP, std::vector< T > inM)
  221 + : centerline<T>(inP)
  222 + {
  223 + std::vector<stim::vec<T> > temp;
  224 + stim::vec<T> zero(0.0,0.0);
  225 + temp.resize(inM.size(), zero);
  226 + for(int i = 0; i < inM.size(); i++)
  227 + temp[i][0] = inM[i];
  228 + init(inP, temp);
  229 + }
  230 +
151 231  
152 232 ///Constructor defines a cylinder with centerline inP and magnitudes of zero
153 233 ///@param inP: Vector of stim vec3 composing the points of the centerline
154   - cylinder(std::vector< stim::vec3<T> > inP){
155   - std::vector< stim::vec<T> > inM; //create an array of arbitrary magnitudes
  234 + cylinder(std::vector< stim::vec3<T> > inP)
  235 + : centerline<T>(inP)
  236 + {
  237 + std::vector< T > inM; //create an array of arbitrary magnitudes
156 238  
157 239 stim::vec<T> zero;
158 240 zero.push_back(0);
... ... @@ -164,7 +246,7 @@ class cylinder
164 246 ///Returns the number of points on the cylinder centerline
165 247  
166 248 unsigned int size(){
167   - return e.size();
  249 + return N;
168 250 }
169 251  
170 252  
... ... @@ -180,9 +262,23 @@ class cylinder
180 262 }
181 263 T l = pvalue*L[L.size()-1];
182 264 int idx = findIdx(l);
183   - T rat = (l-L[idx])/(L[idx+1]-L[idx]);
184   - return( e[idx].P + (e[idx+1].P-e[idx].P)*rat);
185   - }
  265 + return (p(l,idx));
  266 +/* T rat = (l-L[idx])/(L[idx+1]-L[idx]);
  267 + stim::vec3<T> v1(
  268 + c[idx][0],
  269 + c[idx][1],
  270 + c[idx][2]
  271 + );
  272 +
  273 + stim::vec3<T> v2(
  274 + c[idx+1][0],
  275 + c[idx+1][1],
  276 + c[idx+1][2]
  277 + );
  278 +
  279 +// return( e[idx].P + (e[idx+1].P-e[idx].P)*rat);
  280 + return( v1 + (v2 - v1)*rat);
  281 +*/ }
186 282  
187 283 ///Returns a position vector at the given length into the fiber (based on the pvalue).
188 284 ///Interpolates the radius along the line.
... ... @@ -192,7 +288,19 @@ class cylinder
192 288 p(T l, int idx)
193 289 {
194 290 T rat = (l-L[idx])/(L[idx+1]-L[idx]);
195   - return( e[idx].P + (e[idx+1].P-e[idx].P)*rat);
  291 + stim::vec3<T> v1(
  292 + c[idx][0],
  293 + c[idx][1],
  294 + c[idx][2]
  295 + );
  296 +
  297 + stim::vec3<T> v2(
  298 + c[idx+1][0],
  299 + c[idx+1][1],
  300 + c[idx+1][2]
  301 + );
  302 +// return( e[idx].P + (e[idx+1].P-e[idx].P)*rat);
  303 + return( v1 + (v2-v1)*rat);
196 304 // return(
197 305 // return (pos[idx] + (pos[idx+1]-pos[idx])*((l-L[idx])/(L[idx+1]- L[idx])));
198 306 }
... ... @@ -209,8 +317,11 @@ class cylinder
209 317 }
210 318 T l = pvalue*L[L.size()-1];
211 319 int idx = findIdx(l);
212   - return (e[idx].U.len() + (e[idx+1].U.len() - e[idx].U.len())*((l-L[idx])/(L[idx+1]- L[idx])));
213   - }
  320 + return (r(l,idx));
  321 +/* T rat = (l-L[idx])/(L[idx+1]-L[idx]);
  322 +// return (mags[idx][0] + (mags[idx+1][0]-mags[idx][0])*rat);
  323 + return (e[idx].U.len() + (e[idx+1].U.len() - e[idx].U.len())*rat);
  324 +*/ }
214 325  
215 326 ///Returns a radius at the given length into the fiber (based on the pvalue).
216 327 ///Interpolates the position along the line.
... ... @@ -220,7 +331,23 @@ class cylinder
220 331 r(T l, int idx)
221 332 {
222 333 T rat = (l-L[idx])/(L[idx+1]-L[idx]);
223   - return( e[idx].U.len() + (e[idx+1].U.len() - e[idx].U.len())*rat);
  334 + T v1 = (e[idx].U.len() + (e[idx+1].U.len() - e[idx].U.len())*rat);
  335 + T v3 = (Us[idx].len() + (Us[idx+1].len() - Us[idx].len())*rat);
  336 + T v2 = (mags[idx][0] + (mags[idx+1][0]-mags[idx][0])*rat);
  337 +// std::cout << (float)v1 = (float) v2 << std::endl;
  338 + if(abs(v3 - v1) >= 10e-6)
  339 + {
  340 + std::cout << "-------------------------" << std::endl;
  341 + std::cout << e[idx].str() << std::endl << std::endl;
  342 + std::cout << Us[idx].str() << std::endl;
  343 + std::cout << (float)v1 - (float) v2 << std::endl;
  344 + std::cout << "failed" << std::endl;
  345 + }
  346 +// std::cout << e[idx].U.len() << " " << mags[idx][0] << std::endl;
  347 +// std::cout << v2 << std::endl;
  348 + return(v2);
  349 +// return (mags[idx][0] + (mags[idx+1][0]-mags[idx][0])*rat);
  350 + // (
224 351 }
225 352  
226 353 /// Returns the magnitude at the given index
... ... @@ -233,7 +360,7 @@ class cylinder
233 360 /// Adds a new magnitude value to all points
234 361 /// @param m is the starting value for the new magnitude
235 362 void add_mag(T m = 0){
236   - for(unsigned int p = 0; p < e.size(); p++)
  363 + for(unsigned int p = 0; p < N; p++)
237 364 mags[p].push_back(m);
238 365 }
239 366  
... ... @@ -279,8 +406,8 @@ class cylinder
279 406 getPoints(int sides)
280 407 {
281 408 std::vector<std::vector <vec3<T> > > points;
282   - points.resize(e.size());
283   - for(int i = 0; i < e.size(); i++)
  409 + points.resize(N);
  410 + for(int i = 0; i < N; i++)
284 411 {
285 412 points[i] = e[i].getPoints(sides);
286 413 }
... ... @@ -319,7 +446,7 @@ class cylinder
319 446 T len = L[0]; //allocate space for the segment length
320 447  
321 448 //for every consecutive point in the cylinder
322   - for(unsigned p = 1; p < e.size(); p++){
  449 + for(unsigned p = 1; p < N; p++){
323 450  
324 451 //p1 = pos[p]; //get the position and magnitude for the next point
325 452 m1 = mags[p][m];
... ...
stim/visualization/glObj.h
... ... @@ -5,6 +5,7 @@
5 5 #include <GL/glut.h>
6 6 #include <stim/math/vector.h>
7 7 #include <stim/visualization/obj.h>
  8 +#include <stim/gl/error.h>
8 9  
9 10  
10 11 namespace stim
... ... @@ -35,9 +36,9 @@ private:
35 36 glListBase(dList);
36 37  
37 38 glMatrixMode(GL_PROJECTION);
38   - glLoadIdentity;
  39 + glLoadIdentity();
39 40 glMatrixMode(GL_MODELVIEW);
40   - glLoadIdentity;
  41 + glLoadIdentity();
41 42  
42 43 }
43 44  
... ... @@ -129,7 +130,7 @@ public:
129 130 }
130 131  
131 132 void
132   - RenderLine(std::vector< stim::vec<T> > l)
  133 + RenderLine(std::vector< stim::vec3<T> > l)
133 134 {
134 135 glColor3f(0.5, 1.0, 0.5);
135 136 glLineWidth(3.0);
... ...
stim/visualization/obj.h
... ... @@ -322,13 +322,6 @@ protected:
322 322 return OBJ_INVALID;
323 323 }
324 324  
325   - //private method returns the vertex indices for a line
326   - std::vector<unsigned> get_l_v(unsigned l){
327   -
328   -
329   -
330   - }
331   -
332 325 public:
333 326 /// Constructor loads a Wavefront OBJ file
334 327 obj(std::string filename){
... ...