Blame view

stim/gl/gl_spider.h 51.5 KB
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
1
2
3
  #ifndef STIM_GL_SPIDER_H
  #define STIM_GL_SPIDER_H
  
98eecaa9   David Mayerich   VS and win32 updates
4
  //#include <GL/glew.h>
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
5
  #include <GL/glut.h>
a9b45efe   Pavel Govyadinov   changes to spider
6
7
8
  #include <cuda.h>
  #include <cuda_gl_interop.h>
  #include <cudaGL.h>
1a456186   Pavel Govyadinov   Added directional...
9
  #include <math.h>
7d3162a2   Pavel Govyadinov   fixed majority of...
10
11
12
13
14
15
16
  #include <stim/gl/gl_texture.h>
  #include <stim/visualization/camera.h>
  #include <stim/gl/error.h>
  #include <stim/math/vector.h>
  #include <stim/math/vec3.h>
  #include <stim/math/rect.h>
  #include <stim/math/matrix.h>
6ada8448   Pavel Govyadinov   Reverted to 40db1...
17
  #include <stim/math/constants.h>
7d3162a2   Pavel Govyadinov   fixed majority of...
18
  #include <stim/cuda/spider_cost.cuh>
7d1d153a   Pavel Govyadinov   fixed the include...
19
  #include <stim/cuda/cudatools/glbind.h>
84eff8b1   Pavel Govyadinov   Merged only the n...
20
  #include <stim/cuda/arraymath.cuh>
c37611a6   Pavel Govyadinov   removed the time ...
21
  #include <stim/cuda/cuda_texture.cuh>
84eff8b1   Pavel Govyadinov   Merged only the n...
22
23
24
  #include <stim/cuda/cudatools.h>
  #include <stim/cuda/ivote.cuh>
  #include <stim/visualization/glObj.h>
385d2447   Pavel Govyadinov   Checkpoint: Conve...
25
  #include <vector>
b50c840e   David Mayerich   eliminated ANN fr...
26
  #include <stack>
84eff8b1   Pavel Govyadinov   Merged only the n...
27
  #include <stim/cuda/branch_detection.cuh>
23af8e36   Pavel Govyadinov   modified spider t...
28
  #include "../../../volume-spider/glnetwork.h"
e45b97ce   Pavel Govyadinov   safety commit, I ...
29
  #include <stim/visualization/cylinder.h>
c0e09133   Pavel Govyadinov   STABLE: made temp...
30
  #include <stim/cuda/testKernel.cuh>
42145f38   Pavel Govyadinov   Fixed the issues ...
31
32
  #include <iostream>
  #include <fstream>
efe7b7cc   Pavel Govyadinov   Added a detailed ...
33
34
35
36
37
  #ifdef TIMING
  	#include <ctime>
  	#include <cstdio>	
  #endif
  
84eff8b1   Pavel Govyadinov   Merged only the n...
38
  #ifdef TESTING
84eff8b1   Pavel Govyadinov   Merged only the n...
39
40
41
  	#include <cstdio>
  	#include <ctime>
  #endif
42145f38   Pavel Govyadinov   Fixed the issues ...
42
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
43
  
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
44
45
46
47
  namespace stim
  {
  
  template<typename T>
34ec8a82   David Mayerich   added comments
48
  class gl_spider // : public virtual gl_texture<T>
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
49
50
  {
  	//doen't use gl_texture really, just needs the GLuint id.
4cefeb6d   Pavel Govyadinov   Changes to the re...
51
  	//doesn't even need the texture iD really.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
52
  	private:
84eff8b1   Pavel Govyadinov   Merged only the n...
53
  
efe7b7cc   Pavel Govyadinov   Added a detailed ...
54
  		#ifdef TIMING
98eecaa9   David Mayerich   VS and win32 updates
55
56
57
58
59
60
61
  			double branch_time;// = 0;
  			double direction_time;// = 0;
  			double position_time;// = 0;
  			double size_time;// = 0;
  			double cost_time;// = 0;
  			double network_time;// = 0;
  			double hit_time;// = 0;
efe7b7cc   Pavel Govyadinov   Added a detailed ...
62
63
  		#endif
  
7d3162a2   Pavel Govyadinov   fixed majority of...
64
  		stim::vec3<float> p;  	//vector designating the position of the spider.
34ec8a82   David Mayerich   added comments
65
  		stim::vec3<float> d;	//normalized direction of travel
7f297fc6   Pavel Govyadinov   added more detail...
66
  		float m;			//size of the spider in tissue space.
34ec8a82   David Mayerich   added comments
67
  
7d3162a2   Pavel Govyadinov   fixed majority of...
68
  		std::vector<stim::vec3<float> > dV;	//A list of all the direction vectors.
34ec8a82   David Mayerich   added comments
69
  		std::vector<stim::vec3<float> > pV;	//A list of all test positions (relative to p)
7f297fc6   Pavel Govyadinov   added more detail...
70
  		std::vector<float> mV;	//A list of all the size vectors.
84eff8b1   Pavel Govyadinov   Merged only the n...
71
  
34ec8a82   David Mayerich   added comments
72
73
  		stim::matrix<float, 4> cT;			//current Transformation matrix (tissue)->(texture)
  		GLuint texID;						//OpenGL ID for the texture to be traced
7f297fc6   Pavel Govyadinov   added more detail...
74
75
  		stim::vec3<float> S;					//Size of a voxel in the volume.
  		stim::vec3<float> R;					//Dimensions of the volume.
84eff8b1   Pavel Govyadinov   Merged only the n...
76
77
78
  
  
  		//GL and Cuda variables
34ec8a82   David Mayerich   added comments
79
80
81
82
83
84
85
86
  		GLuint dList;				//ID of the starting display lists (series of 4)
  									//dList + 0 = direction template rectangles
  									//dList + 1 = position template rectangles
  									//dList + 2 = size template rectangles
  									//dList + 3 = branch detection cylinder around the fiber
  
  		GLuint fboID;				//framebuffer ID for direction templates
  		GLuint texbufferID;			//texture ID for direction templates
7f297fc6   Pavel Govyadinov   added more detail...
87
88
  		GLuint direction_buffID;				//framebuffer ID, position templates
  		GLuint direction_texID;		//texture ID, position templates
34ec8a82   David Mayerich   added comments
89
  
7f297fc6   Pavel Govyadinov   added more detail...
90
91
  		GLuint position_buffID;				//framebuffer ID, position templates
  		GLuint position_texID;		//texture ID, position templates
34ec8a82   David Mayerich   added comments
92
  
7f297fc6   Pavel Govyadinov   added more detail...
93
94
  		GLuint radius_buffID;				//framebuffer ID, radius templates
  		GLuint radius_texID;		//texture ID, radius templates
34ec8a82   David Mayerich   added comments
95
  
7f297fc6   Pavel Govyadinov   added more detail...
96
97
  		GLuint cylinder_buffID;				//framebuffer ID, cylinder (surrounding fiber)
  		GLuint cylinder_texID;		//texture ID, cylinder
1306fd96   Pavel Govyadinov   minor bug fixes i...
98
  
84eff8b1   Pavel Govyadinov   Merged only the n...
99
  		int numSamples;				//The number of templates in the buffer.
035d798f   Pavel Govyadinov   modified the spid...
100
101
102
  		int numSamplesPos;
  		int numSamplesMag;
  
34ec8a82   David Mayerich   added comments
103
104
105
106
  		float length;				//this will be a function of the radius
  		float stepsize;				//this will be a function of the length
  
  		int current_cost;			//variable to store the cost of the current step		
84eff8b1   Pavel Govyadinov   Merged only the n...
107
108
  		
  		//Tracing variables.
34ec8a82   David Mayerich   added comments
109
110
111
  		std::stack< stim::vec3<float> > seeds;		//seed positions
  		std::stack< stim::vec3<float> > seedsvecs;	//seed directions
  		std::stack< float > 		seedsmags;		//seed magnitudes
6b8621f8   Pavel Govyadinov   stable version
112
  
34ec8a82   David Mayerich   added comments
113
114
  		std::vector< stim::vec3<float> > cL;	//centerline up to the current point
  		std::vector< stim::vec3<float> > cD;	//directions up to the current point (debugging)
7f297fc6   Pavel Govyadinov   added more detail...
115
  		std::vector< float > cM;		//radius up to the current point
23af8e36   Pavel Govyadinov   modified spider t...
116
  
34ec8a82   David Mayerich   added comments
117
118
  		stim::glnetwork<float> nt;				//network object holding the currently traced centerlines
  		stim::glObj<float> sk;					//OBJ file storing the network (identical to above)
23af8e36   Pavel Govyadinov   modified spider t...
119
  
34ec8a82   David Mayerich   added comments
120
121
  		//consider replacing with two seed points facing opposite directions
  		stim::vec<float> rev;					//reverse vector
c0e09133   Pavel Govyadinov   STABLE: made temp...
122
  
34ec8a82   David Mayerich   added comments
123
124
125
126
127
128
  		//selection mode - detecting fiber intersections
  		stim::camera camSel;					//camera for selection mode (detecting collisions)
  		stim::vec3<float> ps;					//position for the selection camera
  		stim::vec3<float> ups;					//up direction for the selection camera
  		stim::vec3<float> ds;					//direction for the selection camera
  
7f297fc6   Pavel Govyadinov   added more detail...
129
  		float n_pixels;							//length of the template (in pixels)
c37611a6   Pavel Govyadinov   removed the time ...
130
131
  
  		//cuda texture variables that keep track of the binding.
7f297fc6   Pavel Govyadinov   added more detail...
132
133
134
  		stim::cuda::cuda_texture t_dir;				//cuda_texture object used as an interface between OpenGL and cuda for direction vectors.
  		stim::cuda::cuda_texture t_pos;				//cuda_texture object used as an interface between OpenGL and cuda for position vectors.
  		stim::cuda::cuda_texture t_mag;				//cuda_texture object used as an interface between OpenGL and cuda for size vectors.
84eff8b1   Pavel Govyadinov   Merged only the n...
135
136
  		
  
4166e973   Pavel Govyadinov   Added debug tags ...
137
138
139
140
141
142
143
  		#ifdef DEBUG
  			stringstream name;
  			int iter;
  			int iter_pos;
  			int iter_dir;
  			int iter_siz;
  		#endif
47fe6300   Pavel Govyadinov   fixed an accident...
144
  
6b8621f8   Pavel Govyadinov   stable version
145
146
147
148
  //--------------------------------------------------------------------------//
  //-------------------------------PRIVATE METHODS----------------------------//
  //--------------------------------------------------------------------------//
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
149
150
151
152
153
154
  		/// Method for finding the best scale for the spider.
  		/// changes the x, y, z size of the spider to minimize the cost
  		/// function. 
  		void
  		findOptimalDirection()
  		{
efe7b7cc   Pavel Govyadinov   Added a detailed ...
155
  			#ifdef TIMING
7f297fc6   Pavel Govyadinov   added more detail...
156
  				gpuStartTimer();	//Timer for profiling
efe7b7cc   Pavel Govyadinov   Added a detailed ...
157
  			#endif
ac430567   Pavel Govyadinov   comments comments...
158
159
  			setMatrix();			//create the transformation matrix.
  			glCallList(dList);		//move the templates to p, d, m.
7f297fc6   Pavel Govyadinov   added more detail...
160
  			glFinish();			//flush the pipeline
efe7b7cc   Pavel Govyadinov   Added a detailed ...
161
  			#ifdef TIMING
7f297fc6   Pavel Govyadinov   added more detail...
162
  				direction_time += gpuStopTimer();	//profiling
efe7b7cc   Pavel Govyadinov   Added a detailed ...
163
  			#endif
efe7b7cc   Pavel Govyadinov   Added a detailed ...
164
  
c37611a6   Pavel Govyadinov   removed the time ...
165
  			int best = getCost(t_dir.getTexture(), t_dir.getAuxArray() ,numSamples);		//find min cost.
4166e973   Pavel Govyadinov   Added debug tags ...
166
167
168
169
170
171
  			#ifdef DEBUG
  				name.str("");
  				name << "Final_Cost_Direction_fiber_"<< iter << "_" << iter_dir << ".bmp";	
  				test(t_dir.getTexture(), n_pixels*2.0, numSamples*n_pixels, name.str());
  				iter_dir++;
  			#endif
7f297fc6   Pavel Govyadinov   added more detail...
172
  			stim::vec<float> next(		///calculate the next vector.
c0e09133   Pavel Govyadinov   STABLE: made temp...
173
  			dV[best][0]*S[0]*R[0],
5eeaf941   Pavel Govyadinov   changer to the ba...
174
175
  			dV[best][1]*S[1]*R[1],
  			dV[best][2]*S[2]*R[2],
d4721000   Pavel Govyadinov   changes with debu...
176
  			0);
7f297fc6   Pavel Govyadinov   added more detail...
177
178
179
180
181
  			next = (cT*next).norm();	///transform the next vector into Tissue space.
  			setPosition(	p[0]+next[0]*m/stepsize,
  					p[1]+next[1]*m/stepsize,
  					p[2]+next[2]*m/stepsize);	
  			setDirection(next[0], next[1], next[2]);		//move forward and change direction.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
182
183
  		}
  
6b8621f8   Pavel Govyadinov   stable version
184
185
  		/// Method for finding the best d (direction) for the spider.
  		/// Not sure if necessary since the next p (position) for the spider
8e56a0a7   Pavel Govyadinov   Added the propose...
186
  		/// will be at d * m.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
187
188
189
  		void
  		findOptimalPosition()
  		{
efe7b7cc   Pavel Govyadinov   Added a detailed ...
190
  			#ifdef TIMING
7f297fc6   Pavel Govyadinov   added more detail...
191
  				gpuStartTimer();	//timer for profiling
efe7b7cc   Pavel Govyadinov   Added a detailed ...
192
  			#endif
ac430567   Pavel Govyadinov   comments comments...
193
194
  			setMatrix();			//create the transformation matrix.
  			glCallList(dList+1);		//move the templates to p, d, m.
7f297fc6   Pavel Govyadinov   added more detail...
195
  			glFinish();			//flush the pipeline
c37611a6   Pavel Govyadinov   removed the time ...
196
  //			glFlush();
efe7b7cc   Pavel Govyadinov   Added a detailed ...
197
  			#ifdef TIMING
7f297fc6   Pavel Govyadinov   added more detail...
198
  				position_time += gpuStopTimer();		///timer for profiling
efe7b7cc   Pavel Govyadinov   Added a detailed ...
199
200
  			#endif
  
c37611a6   Pavel Govyadinov   removed the time ...
201
  			int best = getCost(t_pos.getTexture(), t_pos.getAuxArray(), numSamplesPos);		//find min cost.
4166e973   Pavel Govyadinov   Added debug tags ...
202
203
204
205
206
207
  			#ifdef DEBUG
  				name.str("");
  				name << "Final_Cost_Position_" << iter << "_" << iter_pos << ".bmp";	
  				test(t_pos.getTexture(), n_pixels*2.0, numSamplesPos*n_pixels, name.str());
  				iter_pos++;
  			#endif
ac430567   Pavel Govyadinov   comments comments...
208
  			stim::vec<float> next(		//find next position.
3f15dade   Pavel Govyadinov   changed the plane...
209
210
211
212
   				pV[best][0],
  				pV[best][1],
  				pV[best][2],
  				1);
7f297fc6   Pavel Govyadinov   added more detail...
213
  			next = cT*next;			//transform the next position vector into tissue space.
d4721000   Pavel Govyadinov   changes with debu...
214
215
  			setPosition(
  					next[0]*S[0]*R[0],
8e56a0a7   Pavel Govyadinov   Added the propose...
216
  					next[1]*S[1]*R[1],
d4721000   Pavel Govyadinov   changes with debu...
217
  					next[2]*S[2]*R[2]
ac430567   Pavel Govyadinov   comments comments...
218
  				   );			//adjust position.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
219
220
  		}
  	
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
221
222
  		/// Method for finding the best scale for the spider.
  		/// changes the x, y, z size of the spider to minimize the cost
b50c840e   David Mayerich   eliminated ANN fr...
223
  		/// function. 
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
224
225
226
  		void
  		findOptimalScale()
  		{
efe7b7cc   Pavel Govyadinov   Added a detailed ...
227
228
229
  			#ifdef TIMING
  				gpuStartTimer();
  			#endif
ac430567   Pavel Govyadinov   comments comments...
230
231
  			setMatrix();			//create the transformation.
  			glCallList(dList+2);		//move the templates to p, d, m.
7f297fc6   Pavel Govyadinov   added more detail...
232
  			glFinish();			//flush the drawing pipeline.
efe7b7cc   Pavel Govyadinov   Added a detailed ...
233
234
235
  			#ifdef TIMING
  				size_time += gpuStopTimer();
  			#endif
c37611a6   Pavel Govyadinov   removed the time ...
236
  			int best = getCost(t_mag.getTexture(), t_mag.getAuxArray(), numSamplesMag);		//get best cost.
4166e973   Pavel Govyadinov   Added debug tags ...
237
238
239
240
241
242
  			#ifdef DEBUG
  				name.str("");
  				name << "Final_Cost_Size_" << iter << "_" <<  iter_siz << ".bmp";	
  				test(t_mag.getTexture(), n_pixels*2.0, numSamplesMag*n_pixels, name.str());
  				iter_siz++;
  			#endif
7f297fc6   Pavel Govyadinov   added more detail...
243
  			setMagnitude(m*mV[best]);	//adjust the magnitude.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
244
245
  		}
  
6b8621f8   Pavel Govyadinov   stable version
246
  
f31bf86d   Pavel Govyadinov   Added skeleton fu...
247
  
e45b97ce   Pavel Govyadinov   safety commit, I ...
248
  
27194b56   Pavel Govyadinov   major bug fixes, ...
249
250
  		///finds all the branches in the a given fiber.
  		///using LoG method.
e45b97ce   Pavel Govyadinov   safety commit, I ...
251
252
253
  		void
  		branchDetection2(int n = 8, int l_template = 8, int l_square = 8)
  		{
efe7b7cc   Pavel Govyadinov   Added a detailed ...
254
  			#ifdef TIMING
7f297fc6   Pavel Govyadinov   added more detail...
255
  				gpuStartTimer();		///timer for performance analysis
efe7b7cc   Pavel Govyadinov   Added a detailed ...
256
257
  			#endif
  
7f297fc6   Pavel Govyadinov   added more detail...
258
  			if(cL.size() < 4){}		///if the size of the fiber is less then 4 we do nothing.
e45b97ce   Pavel Govyadinov   safety commit, I ...
259
  			else{
7f297fc6   Pavel Govyadinov   added more detail...
260
  			setMatrix(1);			///finds the current transformation matrix
7f297fc6   Pavel Govyadinov   added more detail...
261
  			DrawLongCylinder(n, l_template, l_square);	///Draw the cylinder.
e45b97ce   Pavel Govyadinov   safety commit, I ...
262
  			stim::cylinder<float> cyl(cL, cM);
7f297fc6   Pavel Govyadinov   added more detail...
263
264
265
266
  			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
  			stim::vec3<float> size(S[0]*R[0], S[1]*R[1], S[2]*R[2]);			///the borders of the texture.
  			float pval;									//pvalue associated with the points on the cylinder.
  			if(!result.empty())								///if we have any points
e45b97ce   Pavel Govyadinov   safety commit, I ...
267
  			{
7f297fc6   Pavel Govyadinov   added more detail...
268
  				for(int i = 0; i < result.size(); i++)					///for each point
e45b97ce   Pavel Govyadinov   safety commit, I ...
269
  				{
8c4f5d84   Pavel Govyadinov   fixed the issue w...
270
  					int id = result[i][2];
7f297fc6   Pavel Govyadinov   added more detail...
271
  					if(fmod(result[i][2], id) != 0 && id != 0)			///if the remainer is odd
8c4f5d84   Pavel Govyadinov   fixed the issue w...
272
273
274
  					{
  						
  						pval = ((cyl.getl(id+1)-cyl.getl(id))*
7f297fc6   Pavel Govyadinov   added more detail...
275
  							(fmod(result[i][2], id))+cyl.getl(id))/cyl.getl(cL.size()-1);		///calculate pvalue
8c4f5d84   Pavel Govyadinov   fixed the issue w...
276
  					}
7f297fc6   Pavel Govyadinov   added more detail...
277
  					else if(id == 0)									///if the point is on the edge
8c4f5d84   Pavel Govyadinov   fixed the issue w...
278
  					{
7f297fc6   Pavel Govyadinov   added more detail...
279
  						pval = (cyl.getl(id+1)*result[i][2])/cyl.getl(cL.size()-1);			
8c4f5d84   Pavel Govyadinov   fixed the issue w...
280
281
282
  					}
  					else
  					{
7f297fc6   Pavel Govyadinov   added more detail...
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
8c4f5d84   Pavel Govyadinov   fixed the issue w...
284
  					}
7f297fc6   Pavel Govyadinov   added more detail...
285
286
  					stim::vec3<float> v = cyl.surf(pval, result[i][0]);					///find the coordinates of the point at pval on the surface in tissue space.
  					stim::vec3<float> di = cyl.p(pval);							///find the coord of v in tissue space projected on the centerline.
4166e973   Pavel Govyadinov   Added debug tags ...
287
  					float rad = cyl.r(pval)/2;								///find the radius at the pvalue's location
8c4f5d84   Pavel Govyadinov   fixed the issue w...
288
289
290
  					if(
  					 !(v[0] > size[0] || v[1] > size[1]
  					 || v[2] > size[2] || v[0] < 0
7f297fc6   Pavel Govyadinov   added more detail...
291
  					 || v[1] < 0 || v[2] < 0))								///if the v point is INSIDE the volume
8c4f5d84   Pavel Govyadinov   fixed the issue w...
292
  					{
7f297fc6   Pavel Govyadinov   added more detail...
293
294
295
  						setSeed(v);									///add a seedpoint's position.
  						setSeedVec((v-di).norm());							///add a seedpoints direction
  						setSeedMag(rad);								///add the starting radius.
8c4f5d84   Pavel Govyadinov   fixed the issue w...
296
  					}
e45b97ce   Pavel Govyadinov   safety commit, I ...
297
298
  				}
  			}
e45b97ce   Pavel Govyadinov   safety commit, I ...
299
  			}
efe7b7cc   Pavel Govyadinov   Added a detailed ...
300
  			#ifdef TIMING
7f297fc6   Pavel Govyadinov   added more detail...
301
  				branch_time += gpuStopTimer();									///timer for performance.
efe7b7cc   Pavel Govyadinov   Added a detailed ...
302
  			#endif
e45b97ce   Pavel Govyadinov   safety commit, I ...
303
304
  		}
  
c0e09133   Pavel Govyadinov   STABLE: made temp...
305
306
307
  
  		float uniformRandom()
  		{
7f297fc6   Pavel Govyadinov   added more detail...
308
  			return (  (float)(rand()))/(  (float)(RAND_MAX));							///generates a random number between 0 and 1 using the uniform distribution.
c0e09133   Pavel Govyadinov   STABLE: made temp...
309
310
311
312
313
314
  		}
  
  		float normalRandom()
  		{
  			float u1 = uniformRandom();
  			float u2 = uniformRandom();
7f297fc6   Pavel Govyadinov   added more detail...
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.
c0e09133   Pavel Govyadinov   STABLE: made temp...
316
317
  		}
  
7d3162a2   Pavel Govyadinov   fixed majority of...
318
  		stim::vec3<float> uniformRandVector()
c0e09133   Pavel Govyadinov   STABLE: made temp...
319
  		{
7f297fc6   Pavel Govyadinov   added more detail...
320
  			stim::vec3<float> r(uniformRandom(), uniformRandom(), 1.0);						///generate a random vector using the uniform distribution between 0 and 1.
c0e09133   Pavel Govyadinov   STABLE: made temp...
321
322
323
  			return r;
  		}
  
7d3162a2   Pavel Govyadinov   fixed majority of...
324
  		stim::vec3<float> normalRandVector()
c0e09133   Pavel Govyadinov   STABLE: made temp...
325
  		{
7f297fc6   Pavel Govyadinov   added more detail...
326
  			stim::vec3<float> r(normalRandom(), normalRandom(), 1.0);						///generate a random vector using the normal distribution between 0 and 1.
c0e09133   Pavel Govyadinov   STABLE: made temp...
327
328
329
  			return r;
  		}
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
330
331
332
333
334
  		
  //--------------------------------------------------------------------------//
  //---------------------TEMPLATE CREATION METHODS----------------------------//
  //--------------------------------------------------------------------------//
  
c0e09133   Pavel Govyadinov   STABLE: made temp...
335
336
  
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
337
338
  		///@param solidAngle, the size of the arc to sample.
  		///Method for populating the vector arrays with sampled vectors.
6b8621f8   Pavel Govyadinov   stable version
339
340
341
  		///Objects created are rectangles the with the created directions.
  		///All points are sampled from a texture.
  		///Stored in a display list.
8e56a0a7   Pavel Govyadinov   Added the propose...
342
  		///uses the default d vector <0,0,1>
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
343
  		void
34ec8a82   David Mayerich   added comments
344
  		genDirectionVectors(float solidAngle = stim::PI/2)
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
345
  		{
c0e09133   Pavel Govyadinov   STABLE: made temp...
346
  
8e56a0a7   Pavel Govyadinov   Added the propose...
347
  			//Set up the vectors necessary for Rectangle creation.
7d3162a2   Pavel Govyadinov   fixed majority of...
348
  			stim::vec3<float> Y(1.0,0.0,0.0);		//orthogonal vec.
7f297fc6   Pavel Govyadinov   added more detail...
349
350
351
  			stim::vec3<float> pos(0.0,0.0,0.0);		//center point of a rectangle
  			float mag = 1.0;				//size of the generated rectangle.
  			stim::vec3<float> dir(0.0, 0.0, 1.0);		//normal of the rectangle
22e7d0c5   Pavel Govyadinov   Minor changes in ...
352
  
7f297fc6   Pavel Govyadinov   added more detail...
353
354
  			float PHI[2], Z[2], range;			
  			PHI[0] = solidAngle/2;				///Project the solid angle into spherical coordinates
c0e09133   Pavel Govyadinov   STABLE: made temp...
355
  			PHI[1] = asin(0);
8e56a0a7   Pavel Govyadinov   Added the propose...
356
  			
7f297fc6   Pavel Govyadinov   added more detail...
357
  			Z[0] = cos(PHI[0]);				///Project the z into spherical coordinates
c0e09133   Pavel Govyadinov   STABLE: made temp...
358
359
  			Z[1] = cos(PHI[1]);
  			
7f297fc6   Pavel Govyadinov   added more detail...
360
  			range = Z[0] - Z[1];				///The range the possible values can be.
c0e09133   Pavel Govyadinov   STABLE: made temp...
361
362
  
  			float z, theta, phi;
7f297fc6   Pavel Govyadinov   added more detail...
363
364
  			glNewList(dList, GL_COMPILE); 			///create a display list of all the direction templates.
  			for(int i = 0; i < numSamples; i++)		///for each sample
c0e09133   Pavel Govyadinov   STABLE: made temp...
365
  			{
7f297fc6   Pavel Govyadinov   added more detail...
366
367
368
369
370
371
  				z = uniformRandom()*range + Z[1];	///generate a z coordinate
  				theta = uniformRandom()*stim::TAU;	///generate a theta coordinate
  				phi = acos(z);				///generate a phi from the z.
  				stim::vec3<float> sph(1, theta, phi);	///combine into a vector in spherical coordinates.
  				stim::vec3<float> cart = sph.sph2cart();///convert to cartesian.	
  				dV.push_back(cart);			///save the generated vector for further use.
4166e973   Pavel Govyadinov   Added debug tags ...
372
373
374
  				#ifdef DEBUG
  //					std::cout << cart << std::endl;
  				#endif
7f297fc6   Pavel Govyadinov   added more detail...
375
  				if(cos(Y.dot(cart)) < 0.087)		///make sure that the Y is not parallel to the new vector.
c0e09133   Pavel Govyadinov   STABLE: made temp...
376
377
378
379
  				{
  						Y[0] = 0.0; Y[1] = 1.0;
  				}else{
  					Y[0] = 1.0; Y[1] = 0.0;
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
380
  				}
4166e973   Pavel Govyadinov   Added debug tags ...
381
  
7f297fc6   Pavel Govyadinov   added more detail...
382
                                  hor = stim::rect<float>(mag,		///generate a rectangle with the new vectro as a normal.
c0e09133   Pavel Govyadinov   STABLE: made temp...
383
384
  					 pos, cart,
                         			((Y.cross(cart)).cross(cart)).norm());
4166e973   Pavel Govyadinov   Added debug tags ...
385
386
387
388
  
  				#ifdef DEBUG
  			//		std::cout << hor.n() << std::endl;
  				#endif
7f297fc6   Pavel Govyadinov   added more detail...
389
         				ver = stim::rect<float>(mag,		///generate another rectangle that's perpendicular the first but parallel to the cart vector.
c0e09133   Pavel Govyadinov   STABLE: made temp...
390
391
  					 pos, cart,
                         			hor.n());
7f297fc6   Pavel Govyadinov   added more detail...
392
  				UpdateBuffer(0.0, 0.0+i*n_pixels);	///Put the necessary points into the diplaylist.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
393
  			}
7f297fc6   Pavel Govyadinov   added more detail...
394
  			glEndList();					///finilize the display list.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
395
  		}
2a18be6d   Pavel Govyadinov   New comments and ...
396
  
ac430567   Pavel Govyadinov   comments comments...
397
  		///@param float delta, How much the rectangles vary in position.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
398
  		///Method for populating the buffer with the sampled texture.
ac430567   Pavel Govyadinov   comments comments...
399
400
401
  		///Objects created are rectangles the with the created positions.
  		///All points are sampled from a texture.
  		///Stored in a display list.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
402
  		///uses the default vector <0,0,0>
a9f956be   Pavel Govyadinov   Fixed the cost fu...
403
  		void
4191c034   Pavel Govyadinov   minor:bug fixes. ...
404
  		genPositionVectors(float delta = 0.4)
a9f956be   Pavel Govyadinov   Fixed the cost fu...
405
  		{
8e56a0a7   Pavel Govyadinov   Added the propose...
406
  			//Set up the vectors necessary for Rectangle creation.
7d3162a2   Pavel Govyadinov   fixed majority of...
407
  			stim::vec3<float> Y(1.0,0.0,0.0);		//orthogonal vec.
7f297fc6   Pavel Govyadinov   added more detail...
408
409
410
  			stim::vec3<float> pos(0.0,0.0,0.0);		//center point of a rectangle
  			float mag = 1.0;				///size of each rectangle
  			stim::vec3<float> dir(0.0, 0.0, 1.0);		///normal of the rectangle plane.
8e56a0a7   Pavel Govyadinov   Added the propose...
411
412
  
  			//Set up the variable necessary for vector creation.
7f297fc6   Pavel Govyadinov   added more detail...
413
  			glNewList(dList+1, GL_COMPILE);			///generate a new display list.
4166e973   Pavel Govyadinov   Added debug tags ...
414
415
416
417
418
419
420
421
422
423
  			pV.push_back(pos);
  			hor = stim::rect<float>(mag,                    ///generate a rec     tangle with the new vector as a normal.  
                  	        pos, dir,
  	                       	((Y.cross(d)).cross(d))
  	                        .norm());
                          ver = stim::rect<float>(mag,                    ///generate anoth     er rectangle that's perpendicular the first but parallel to the cart vector.
          	                pos, dir,
  	                        hor.n()); 
                          UpdateBuffer(0.0, 0.0+0*n_pixels);
  			for(int i = 1; i < numSamplesPos; i++)		///for the number of position samples
c0e09133   Pavel Govyadinov   STABLE: made temp...
424
  			{
7f297fc6   Pavel Govyadinov   added more detail...
425
  				stim::vec3<float> temp = uniformRandVector();	///generate a random point on a plane.
4166e973   Pavel Govyadinov   Added debug tags ...
426
  				temp = temp*delta*2.0 - delta;		///scale the point and center it around 0.0
c0e09133   Pavel Govyadinov   STABLE: made temp...
427
  				temp[2] = 0.0;
7f297fc6   Pavel Govyadinov   added more detail...
428
429
  				pV.push_back(temp);				///save the point for further use.
                  		hor = stim::rect<float>(mag,			///generate a rectangle with the new vector as a normal.
c0e09133   Pavel Govyadinov   STABLE: made temp...
430
431
432
  					 temp, dir,
                        			((Y.cross(d)).cross(d))
  					.norm());
7f297fc6   Pavel Govyadinov   added more detail...
433
                  		ver = stim::rect<float>(mag,			///generate another rectangle that's perpendicular the first but parallel to the cart vector.
c0e09133   Pavel Govyadinov   STABLE: made temp...
434
435
  					 temp, dir,
                          		hor.n());
7f297fc6   Pavel Govyadinov   added more detail...
436
  				UpdateBuffer(0.0, 0.0+i*n_pixels);		///sample the necessary points and put them into a display list.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
437
  			}
7f297fc6   Pavel Govyadinov   added more detail...
438
  			glEndList();					///finilize the display list.
4166e973   Pavel Govyadinov   Added debug tags ...
439
440
441
442
  			#ifdef DEBUG
  				for(int i = 0; i < numSamplesPos; i++)
  					std::cout << pV[i] << std::endl;
  			#endif
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
443
444
  		}
  
ac430567   Pavel Govyadinov   comments comments...
445
  		///@param float delta, How much the rectangles are allowed to expand.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
446
  		///Method for populating the buffer with the sampled texture.
ac430567   Pavel Govyadinov   comments comments...
447
448
449
  		///Objects created are rectangles the with the created sizes.
  		///All points are sampled from a texture.
  		///Stored in a display list.
8e56a0a7   Pavel Govyadinov   Added the propose...
450
  		///uses the default m <1,1,0>
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
451
  		void
79a9bf3f   Pavel Govyadinov   new implementatio...
452
  		genMagnitudeVectors(float delta = 0.70)
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
453
454
  		{
  			
8e56a0a7   Pavel Govyadinov   Added the propose...
455
  			//Set up the vectors necessary for Rectangle creation.
7d3162a2   Pavel Govyadinov   fixed majority of...
456
  			stim::vec3<float> Y(1.0, 0.0, 0.0);		//orthogonal vec.
7f297fc6   Pavel Govyadinov   added more detail...
457
458
459
  			stim::vec3<float> pos(0.0, 0.0, 0.0);		//center of the future rect.
  			float mag = 1.0;				///size of the rectangle
  			stim::vec3<float> dir(0.0, 0.0, 1.0);		///normal of the rectangle plane.
8e56a0a7   Pavel Govyadinov   Added the propose...
460
461
  
  			//Set up the variable necessary for vector creation.
7f297fc6   Pavel Govyadinov   added more detail...
462
463
464
  			float min 	= 1.0-delta;			///smallest size
  			float max 	= 1.0+delta;			///largers size.
  			float step	= (max-min)/(numSamplesMag-1);	///the size variation from one rect to the next.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
465
  			float factor;
8e56a0a7   Pavel Govyadinov   Added the propose...
466
  			glNewList(dList+2, GL_COMPILE);
7f297fc6   Pavel Govyadinov   added more detail...
467
  			for(int i = 0; i < numSamplesMag; i++){		///for the number of position samples
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
468
  				//Create linear index
7f297fc6   Pavel Govyadinov   added more detail...
469
470
471
  				factor = (min+step*i)*mag;		///scaling factor
  				mV.push_back(factor);			///save the size factor for further use.
  				hor = stim::rect<float>(factor,		///generate a rectangle with the new vector as a normal.
8e56a0a7   Pavel Govyadinov   Added the propose...
472
473
474
  					 pos, dir, 
         	       				((Y.cross(d)).cross(d))
  					.norm());
7f297fc6   Pavel Govyadinov   added more detail...
475
                 			ver = stim::rect<float>(factor,		///generate another rectangle that's perpendicular the first but parallel to the cart vector.
8e56a0a7   Pavel Govyadinov   Added the propose...
476
477
  					 pos, dir,
                         			hor.n());
7f297fc6   Pavel Govyadinov   added more detail...
478
  				UpdateBuffer(0.0, 0.0+i*n_pixels);	///sample the necessary points and put them into a display list.
8e56a0a7   Pavel Govyadinov   Added the propose...
479
  			CHECK_OPENGL_ERROR
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
480
  			}
7f297fc6   Pavel Govyadinov   added more detail...
481
  			glEndList();					///finilize the displaylist.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
482
  		}
ac430567   Pavel Govyadinov   comments comments...
483
484
485
486
487
  
  		///@param float v_x x-coordinate in buffer-space,
  		///@param float v_y y-coordinate in buffer-space.
  		///Samples the texture space.
   		///places a sample in the provided coordinates of bufferspace.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
488
489
490
  		void
  		UpdateBuffer(float v_x, float v_y)
  		{	
7f297fc6   Pavel Govyadinov   added more detail...
491
492
493
494
495
496
497
498
499
  			stim::vec3<float>p1; 				///first point.
          	        stim::vec3<float>p2; 				///second point.
  	                stim::vec3<float>p3; 				///third point.
                  	stim::vec3<float>p4;				///fourth point.
  			p1 = hor.p(1,1);				///generate the top right point from the horizontal template.
  			p2 = hor.p(1,0);				///generate the bottom right point from the horizonatal template.
  			p3 = hor.p(0,0);				///generate the bottom left point from the horizontal template.
  			p4 = hor.p(0,1);				///generate the top left point from the horizonatal template.
  			glBegin(GL_QUADS);				///generate the Quad from the 4 points.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
500
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
501
502
503
504
  					p1[0],
  					p1[1],
  					p1[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
505
506
  				glVertex2f(v_x,v_y);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
507
508
509
510
  					p2[0],
  					p2[1],
  					p2[2]
  					);
7f297fc6   Pavel Govyadinov   added more detail...
511
  				glVertex2f(v_x+n_pixels, v_y);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
512
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
513
514
515
516
  					p3[0],
  					p3[1],
  					p3[2]
  					);
7f297fc6   Pavel Govyadinov   added more detail...
517
  				glVertex2f(v_x+n_pixels, v_y+n_pixels);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
518
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
519
520
521
  					p4[0],
  					p4[1],
  					p4[2]
a9f956be   Pavel Govyadinov   Fixed the cost fu...
522
  					);
7f297fc6   Pavel Govyadinov   added more detail...
523
524
525
526
527
528
529
530
  				glVertex2f(v_x, v_y+n_pixels);
  			 glEnd();					///finish the quad.
  
  			 p1 = ver.p(1,1);				///generate the top right point from the vertical template.
  			 p2 = ver.p(1,0);                               ///generate the bottom right point from the vertical template.
  			 p3 = ver.p(0,0);                               ///generate the bottom left point from the vertical template.
  			 p4 = ver.p(0,1);                               ///generate the top left point from the vertical template.
  		 	 glBegin(GL_QUADS);				///generate the Quad from the 4 points.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
531
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
532
533
534
535
  					p1[0],
  					p1[1],
  					p1[2]
  					);
7f297fc6   Pavel Govyadinov   added more detail...
536
  				glVertex2f(v_x+n_pixels, v_y);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
537
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
538
539
540
541
  					p2[0],
  					p2[1],
  					p2[2]
  					);
7f297fc6   Pavel Govyadinov   added more detail...
542
  				glVertex2f(v_x+2.0*n_pixels, v_y);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
543
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
544
545
546
547
  					p3[0],
  					p3[1],
  					p3[2]
  					);
7f297fc6   Pavel Govyadinov   added more detail...
548
  				glVertex2f(v_x+2.0*n_pixels, v_y+n_pixels);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
549
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
550
551
552
553
  					p4[0],
  					p4[1],
  					p4[2]
  					);
7f297fc6   Pavel Govyadinov   added more detail...
554
555
  				glVertex2f(v_x+n_pixels, v_y+n_pixels);
  			glEnd(); 					///finish the quad.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
556
  		}
2a18be6d   Pavel Govyadinov   New comments and ...
557
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
558
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
559
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
560
561
562
  //--------------------------------------------------------------------------//
  //--------------------------------GL METHODS--------------------------------//
  //--------------------------------------------------------------------------//
385d2447   Pavel Govyadinov   Checkpoint: Conve...
563
  
ac430567   Pavel Govyadinov   comments comments...
564
565
566
567
  		///@param uint width sets the width of the buffer.
  		///@param uint height sets the height of the buffer.
  		///@param GLuint &textureID gives the texture ID of the texture to be initialized.
  		///@param GLuint &framebufferID gives the buffer ID of the texture to be initialized.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
568
  		///Function for setting up the 2D buffer that stores the samples.
ac430567   Pavel Govyadinov   comments comments...
569
  		///Initiates and sets parameters.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
570
  		void
84eff8b1   Pavel Govyadinov   Merged only the n...
571
572
  		GenerateFBO(unsigned int width, unsigned int height, GLuint &textureID, GLuint &framebufferID)
  		{
7f297fc6   Pavel Govyadinov   added more detail...
573
574
575
  			glDeleteFramebuffers(1, &framebufferID);				///clear the framebuffer.
  			glGenFramebuffers(1, &framebufferID);					///generate a clean buffer.
  			glBindFramebuffer(GL_FRAMEBUFFER, framebufferID);			///bind the new buffer.
6ada8448   Pavel Govyadinov   Reverted to 40db1...
576
  //			int numChannels = 1;
c37611a6   Pavel Govyadinov   removed the time ...
577
  //			unsigned char* texels = new unsigned char[width * height * numChannels];
7f297fc6   Pavel Govyadinov   added more detail...
578
  			glGenTextures(1, &textureID);						///generate a texture that will attach to the buffer.
84eff8b1   Pavel Govyadinov   Merged only the n...
579
  			glBindTexture(GL_TEXTURE_2D, textureID);
ac430567   Pavel Govyadinov   comments comments...
580
581
  
  			//Textures repeat and use linear interpolation, luminance format.
4166e973   Pavel Govyadinov   Added debug tags ...
582
583
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);		///Set up the texture to repeat at edges.
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);		
7f297fc6   Pavel Govyadinov   added more detail...
584
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	///Set up the texture to use Linear interpolation
84eff8b1   Pavel Govyadinov   Merged only the n...
585
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
7f297fc6   Pavel Govyadinov   added more detail...
586
  			glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE,				///Create the texture with no data.
c37611a6   Pavel Govyadinov   removed the time ...
587
588
  				 width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);   
  //			delete[] texels;
7f297fc6   Pavel Govyadinov   added more detail...
589
590
  			glBindFramebuffer(GL_FRAMEBUFFER, 0); 					///Bind the frontbuffer
  			glBindTexture(GL_TEXTURE_2D, 0);					///Unbind the texture.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
591
  		}
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
592
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
593
  
e45b97ce   Pavel Govyadinov   safety commit, I ...
594
  		///IF type == 0
ac430567   Pavel Govyadinov   comments comments...
595
  		///Method for using the gl manipulation to align templates from
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
596
  		///Template space (-0.5 0.5) to Texture space (0.0, 1.0),
8e56a0a7   Pavel Govyadinov   Added the propose...
597
  		///Based on the p of the spider in real space (arbitrary).
e45b97ce   Pavel Govyadinov   safety commit, I ...
598
599
600
601
  
  		///IF type == 1
  		///Method for using the gl manipulation to set up a matrix 
  		///To transform from tissue space into texture space.
ac430567   Pavel Govyadinov   comments comments...
602
  		///All transformation happen in glMatrixMode(GL_TEXTURE).
ac430567   Pavel Govyadinov   comments comments...
603
  		///All transformation happen in glMatrixMode(GL_TEXTURE).
e45b97ce   Pavel Govyadinov   safety commit, I ...
604
  		void setMatrix(int type = 0)
5f81932b   David Mayerich   restored Pavel's ...
605
  		{
e45b97ce   Pavel Govyadinov   safety commit, I ...
606
607
608
609
610
611
  			if(type == 0)
  			{
  				float curTrans[16];			//array to store the matrix values.
  				stim::vec<float> rot = getRotation(d);	//get the rotation parameters for the current direction vector.
  				glMatrixMode(GL_TEXTURE);
  				glLoadIdentity();
d4721000   Pavel Govyadinov   changes with debu...
612
  
e45b97ce   Pavel Govyadinov   safety commit, I ...
613
614
615
616
617
618
619
620
621
  				//Scale by the voxel size and number of slices.
  				glScalef(1.0/S[0]/R[0], 1.0/S[1]/R[1], 1.0/S[2]/R[2]);
  				//translate to the current position of the spider in the texture.
  				glTranslatef(p[0],
  					     p[1],
  					     p[2]);
  				//rotate to the current direction of the spider.
  				glRotatef(rot[0], rot[1], rot[2], rot[3]);
  				//scale to the magnitude of the spider.
7f297fc6   Pavel Govyadinov   added more detail...
622
623
624
  				glScalef(m,
  					 m,
  					 m);
e45b97ce   Pavel Govyadinov   safety commit, I ...
625
626
627
  				//get and store the current transformation matrix for later use.
  				glGetFloatv(GL_TEXTURE_MATRIX, curTrans);
  				cT.set(curTrans);
e45b97ce   Pavel Govyadinov   safety commit, I ...
628
629
630
631
632
633
634
635
636
637
638
639
  				
  				CHECK_OPENGL_ERROR
  				//revert back to default gl mode.
  				glMatrixMode(GL_MODELVIEW);
  			}
  			else if(type == 1)
  			{
  				glMatrixMode(GL_TEXTURE);
  				glLoadIdentity();
  				glScalef(1.0/S[0]/R[0], 1.0/S[1]/R[1], 1.0/S[2]/R[2]);
  				glMatrixMode(GL_MODELVIEW);
  			}
5f81932b   David Mayerich   restored Pavel's ...
640
  		}
84eff8b1   Pavel Govyadinov   Merged only the n...
641
  
ac430567   Pavel Govyadinov   comments comments...
642
643
  		///Method for controling the buffer and texture binding.
  		///Clears the buffer upon binding.
09049866   Pavel Govyadinov   fixed a drawing b...
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
  		void
  		Bind()
  		{
  			glBindFramebuffer(GL_FRAMEBUFFER, direction_buffID);//set up GL buffer		
  			glFramebufferTexture2D(
  				GL_FRAMEBUFFER,
  				GL_COLOR_ATTACHMENT0,
  				GL_TEXTURE_2D,
  				direction_texID,
  				0);
  			glBindFramebuffer(GL_FRAMEBUFFER, direction_buffID);
  			GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
  			glDrawBuffers(1, DrawBuffers);
  			glBindTexture(GL_TEXTURE_2D, direction_texID);
  			glClearColor(1,1,1,1);
  			glClear(GL_COLOR_BUFFER_BIT);
  			glMatrixMode(GL_PROJECTION);
  			glLoadIdentity();
  			glMatrixMode(GL_MODELVIEW);
  			glLoadIdentity();
  			glViewport(0,0,2.0*n_pixels, numSamples*n_pixels);
  			gluOrtho2D(0.0,2.0*n_pixels,0.0,numSamples*n_pixels);
  			glEnable(GL_TEXTURE_3D);
  			glBindTexture(GL_TEXTURE_3D, texID);
  
  			CHECK_OPENGL_ERROR
  		}
  
  		///Method for controling the buffer and texture binding.
  		///Clears the buffer upon binding.
ac430567   Pavel Govyadinov   comments comments...
674
675
676
  		///@param GLuint &textureID, texture to be bound.
  		///@param GLuint &framebufferID, framebuffer used for storage.
  		///@param int nSamples, number of rectanges to create. 
84eff8b1   Pavel Govyadinov   Merged only the n...
677
  		void
e45b97ce   Pavel Govyadinov   safety commit, I ...
678
  		Bind(GLuint &textureID, GLuint &framebufferID, int nSamples, float len = 8.0)
84eff8b1   Pavel Govyadinov   Merged only the n...
679
  		{
c0e09133   Pavel Govyadinov   STABLE: made temp...
680
  			
7f297fc6   Pavel Govyadinov   added more detail...
681
682
  			glBindFramebuffer(GL_FRAMEBUFFER, framebufferID);	///Bind the framebuffer.
  			glFramebufferTexture2D(					///associate it with the texture
84eff8b1   Pavel Govyadinov   Merged only the n...
683
684
685
686
687
  				GL_FRAMEBUFFER,
  				GL_COLOR_ATTACHMENT0,
  				GL_TEXTURE_2D,
  				textureID,
  				0);
7f297fc6   Pavel Govyadinov   added more detail...
688
689
690
691
692
  			glBindFramebuffer(GL_FRAMEBUFFER, framebufferID);	///Bind the framebuffer.
  			GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};		///generate the drawbuffer.
  			glDrawBuffers(1, DrawBuffers);				///set the drawbuffer.
  			glBindTexture(GL_TEXTURE_2D, textureID);		///Bind the texture passed.
  			glMatrixMode(GL_PROJECTION);				///clear out the draw matrices
84eff8b1   Pavel Govyadinov   Merged only the n...
693
694
695
  			glLoadIdentity();
  			glMatrixMode(GL_MODELVIEW);
  			glLoadIdentity();
7f297fc6   Pavel Govyadinov   added more detail...
696
697
698
699
  			glViewport(0,0,2.0*len, nSamples*len);			///set up viewport
  			gluOrtho2D(0.0,2.0*len,0.0,nSamples*len);		///Set up ortho
  			glEnable(GL_TEXTURE_3D);				
  			glBindTexture(GL_TEXTURE_3D, texID);			///bind the main texture (return to original state).
84eff8b1   Pavel Govyadinov   Merged only the n...
700
701
702
  
  			CHECK_OPENGL_ERROR
  		}
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
703
  		
ac430567   Pavel Govyadinov   comments comments...
704
  		///Unbinds all texture resources.
84eff8b1   Pavel Govyadinov   Merged only the n...
705
706
707
708
  		void
  		Unbind()
  		{
  			//Finalize GL_buffer
7f297fc6   Pavel Govyadinov   added more detail...
709
  			glBindTexture(GL_TEXTURE_3D, 0);                      ///Bind the front buffer.
84eff8b1   Pavel Govyadinov   Merged only the n...
710
  			CHECK_OPENGL_ERROR
7f297fc6   Pavel Govyadinov   added more detail...
711
  			glBindTexture(GL_TEXTURE_2D, 0);                      ///Bind the default GL texture.
84eff8b1   Pavel Govyadinov   Merged only the n...
712
  			CHECK_OPENGL_ERROR
7f297fc6   Pavel Govyadinov   added more detail...
713
  			glBindFramebuffer(GL_FRAMEBUFFER, 0);		      ///Bind the defautl framebuffer.
84eff8b1   Pavel Govyadinov   Merged only the n...
714
  			CHECK_OPENGL_ERROR
7f297fc6   Pavel Govyadinov   added more detail...
715
  			glDisable(GL_TEXTURE_3D);			      ///Turn off texturing.
ac430567   Pavel Govyadinov   comments comments...
716
  			CHECK_OPENGL_ERROR
ac430567   Pavel Govyadinov   comments comments...
717
718
  		}
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
719
  		
5f81932b   David Mayerich   restored Pavel's ...
720
  
42145f38   Pavel Govyadinov   Fixed the issues ...
721
722
723
724
725
  
  //--------------------------------------------------------------------------//
  //--------------------------------CUDA METHODS------------------------------//
  //--------------------------------------------------------------------------//
  		
42145f38   Pavel Govyadinov   Fixed the issues ...
726
  
ac430567   Pavel Govyadinov   comments comments...
727
728
729
  		///Entry-point into the cuda code for calculating the cost of a given samples array (in texture form)
  		///finds the minimum cost and sets the current_cost to that value.
  		/// and returns the index of the template with the minimal cost. 
42145f38   Pavel Govyadinov   Fixed the issues ...
730
  		int
c37611a6   Pavel Govyadinov   removed the time ...
731
  		getCost(cudaTextureObject_t tObj, float* result, int n)
035d798f   Pavel Govyadinov   modified the spid...
732
  		{
efe7b7cc   Pavel Govyadinov   Added a detailed ...
733
734
  			#ifdef TIMING
  				gpuStartTimer();
035d798f   Pavel Govyadinov   modified the spid...
735
736
  			#endif
  			stim::vec<int> cost = 
7f297fc6   Pavel Govyadinov   added more detail...
737
  				stim::cuda::get_cost(tObj, result, n, 2*n_pixels, n_pixels);	///call the cuda function with the appropriate texture buffer.
efe7b7cc   Pavel Govyadinov   Added a detailed ...
738
739
  			#ifdef TIMING
  				cost_time += gpuStopTimer();
035d798f   Pavel Govyadinov   modified the spid...
740
  			#endif
7f297fc6   Pavel Govyadinov   added more detail...
741
  			current_cost = cost[1];							///current cost.
035d798f   Pavel Govyadinov   modified the spid...
742
743
  			return cost[0];
  		}
ac430567   Pavel Govyadinov   comments comments...
744
  
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
745
  	public:
ac430567   Pavel Govyadinov   comments comments...
746
747
748
749
750
751
  
  		///ininializes the cuda device and environment.
  		void
  		initCuda()
  		{	
  			stim::cudaSetDevice();
ac430567   Pavel Govyadinov   comments comments...
752
753
754
  		}
  
  		//horizonal rectangle forming the spider.
13c2a7d4   Pavel Govyadinov   some changes to t...
755
  		stim::rect<float> hor;
ac430567   Pavel Govyadinov   comments comments...
756
  		//vectical rectangle forming the spider.
13c2a7d4   Pavel Govyadinov   some changes to t...
757
758
  		stim::rect<float> ver;	
  
efe7b7cc   Pavel Govyadinov   Added a detailed ...
759
  		//Timing variables.
84eff8b1   Pavel Govyadinov   Merged only the n...
760
761
  		#ifdef TESTING
  			std::clock_t start;
84eff8b1   Pavel Govyadinov   Merged only the n...
762
763
  		#endif
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
764
765
766
767
768
  //--------------------------------------------------------------------------//
  //-----------------------------CONSTRUCTORS---------------------------------//
  //--------------------------------------------------------------------------//
  
  
ac430567   Pavel Govyadinov   comments comments...
769
770
  		///@param int samples, the number of samples this spider is going to use.
  		///Best results if samples is can create a perfect root.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
771
  		///Default Constructor
2a18be6d   Pavel Govyadinov   New comments and ...
772
  		gl_spider
3f15dade   Pavel Govyadinov   changed the plane...
773
  		(int samples = 1089, int samplespos = 441,int samplesmag = 144)
2a18be6d   Pavel Govyadinov   New comments and ...
774
  		{
7d3162a2   Pavel Govyadinov   fixed majority of...
775
776
  			p = stim::vec3<float>(0.0, 0.0, 0.0);
  			d = stim::vec3<float>(0.0, 0.0, 1.0);
7f297fc6   Pavel Govyadinov   added more detail...
777
  			m = 1.0;
7d3162a2   Pavel Govyadinov   fixed majority of...
778
779
  			S = stim::vec3<float>(1.0, 1.0, 1.0);
  			R = stim::vec3<float>(1.0, 1.0, 1.0);
2a18be6d   Pavel Govyadinov   New comments and ...
780
  			numSamples = samples;
035d798f   Pavel Govyadinov   modified the spid...
781
782
  			numSamplesPos = samplespos;
  			numSamplesMag = samplesmag;
4166e973   Pavel Govyadinov   Added debug tags ...
783
  		#ifdef DEBUG
47fe6300   Pavel Govyadinov   fixed an accident...
784
  			iter = 0;
4166e973   Pavel Govyadinov   Added debug tags ...
785
786
787
788
  			iter_pos = 0;
  			iter_dir = 0;
  			iter_siz = 0;
  		#endif
4cefeb6d   Pavel Govyadinov   Changes to the re...
789
790
  		}
  
ac430567   Pavel Govyadinov   comments comments...
791
792
793
794
795
796
797
798
799
  		///Position constructor: floats.
  		///@param float pos_x, position x.
  		///@param float pos_y, position y.
  		///@param float pos_z, position z.
  		///@param float dir_x, direction x.
  		///@param float dir_y, direction y.
  		///@param float dir_z, direction z.
  		///@param float mag_x, size of the vector.
  		///@param int 	samples, number of templates this spider is going to use.
13c2a7d4   Pavel Govyadinov   some changes to t...
800
801
  		gl_spider
  		(float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z,
3f15dade   Pavel Govyadinov   changed the plane...
802
  			float mag_x, int numsamples = 1089, int numsamplespos = 441, int numsamplesmag =144)
13c2a7d4   Pavel Govyadinov   some changes to t...
803
  		{
7d3162a2   Pavel Govyadinov   fixed majority of...
804
805
  			p = stim::vec3<float>(pos_x, pos_y, pos_z);
  			d = stim::vec3<float>(dir_x, dir_y, dir_z);
7f297fc6   Pavel Govyadinov   added more detail...
806
  			m = mag_x;
7d3162a2   Pavel Govyadinov   fixed majority of...
807
808
  			S = stim::vec3<float>(1.0,1.0,1.0);
  			R = stim::vec3<float>(1.0,1.0,1.0);
035d798f   Pavel Govyadinov   modified the spid...
809
810
811
  			numSamples = numsamples;
  			numSamplesPos = numsamplespos;
  			numSamplesMag = numsamplesmag;
4166e973   Pavel Govyadinov   Added debug tags ...
812
  		#ifdef DEBUG
47fe6300   Pavel Govyadinov   fixed an accident...
813
  			iter = 0;
4166e973   Pavel Govyadinov   Added debug tags ...
814
815
816
817
  			iter_pos = 0;
  			iter_dir = 0;
  			iter_siz = 0;
  		#endif
385d2447   Pavel Govyadinov   Checkpoint: Conve...
818
  		}
ac430567   Pavel Govyadinov   comments comments...
819
820
821
822
823
824
825
  
  		///Position constructor: vecs of floats.
  		///@param stim::vec<float> pos, position.
  		///@param stim::vec<float> dir, direction.
  		///@param float mag, size of the vector.
  		///@param int 	samples, number of templates this spider is going to use.
  		gl_spider
7d3162a2   Pavel Govyadinov   fixed majority of...
826
  		(stim::vec3<float> pos, stim::vec3<float> dir, float mag, int samples = 1089, int samplesPos = 441, int samplesMag = 144)
ac430567   Pavel Govyadinov   comments comments...
827
828
829
  		{
  			p = pos;
  			d = dir;
7f297fc6   Pavel Govyadinov   added more detail...
830
  			m = mag;
7d3162a2   Pavel Govyadinov   fixed majority of...
831
832
  			S = vec3<float>(1.0,1.0,1.0);
  			R = vec3<float>(1.0,1.0,1.0);
ac430567   Pavel Govyadinov   comments comments...
833
  			numSamples = samples;
035d798f   Pavel Govyadinov   modified the spid...
834
835
  			numSamplesPos = samplesPos;
  			numSamplesMag = samplesMag;
4166e973   Pavel Govyadinov   Added debug tags ...
836
  		#ifdef DEBUG
47fe6300   Pavel Govyadinov   fixed an accident...
837
  			iter = 0;
4166e973   Pavel Govyadinov   Added debug tags ...
838
839
840
841
  			iter_pos = 0;
  			iter_dir = 0;
  			iter_siz = 0;
  		#endif
ac430567   Pavel Govyadinov   comments comments...
842
843
844
  		}
  
  		///destructor	
8e56a0a7   Pavel Govyadinov   Added the propose...
845
846
847
848
  		~gl_spider
  		(void)
  		{
  			Unbind();
7f297fc6   Pavel Govyadinov   added more detail...
849
850
851
852
853
854
855
856
  			glDeleteTextures(1, &direction_texID);
  			glDeleteBuffers(1, &direction_buffID);
  			glDeleteTextures(1, &position_texID);
  			glDeleteBuffers(1, &position_buffID);
  			glDeleteTextures(1, &radius_texID);
  			glDeleteBuffers(1, &radius_buffID);     
  			glDeleteTextures(1, &cylinder_texID);
  			glDeleteBuffers(1, &cylinder_buffID);
8e56a0a7   Pavel Govyadinov   Added the propose...
857
858
  		}
  
ac430567   Pavel Govyadinov   comments comments...
859
  		///@param GLuint id, texture that is going to be sampled.
385d2447   Pavel Govyadinov   Checkpoint: Conve...
860
  		///Attached the spider to the texture with the given GLuint ID.
8e56a0a7   Pavel Govyadinov   Added the propose...
861
  		///Samples in the default d acting as the init method.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
862
  		///Also acts an init.	
a9b45efe   Pavel Govyadinov   changes to spider
863
  		void
2a18be6d   Pavel Govyadinov   New comments and ...
864
  		attachSpider(GLuint id)
a9b45efe   Pavel Govyadinov   changes to spider
865
  		{
98eecaa9   David Mayerich   VS and win32 updates
866
867
868
869
870
871
872
873
874
  #ifdef TIMING
  			branch_time = 0;
  			direction_time = 0;
  			position_time = 0;
  			size_time = 0;
  			cost_time = 0;
  			network_time = 0;
  			hit_time = 0;
  #endif
47fe6300   Pavel Govyadinov   fixed an accident...
875
876
  #ifdef DEBUG
  			iter = 0;
4166e973   Pavel Govyadinov   Added debug tags ...
877
878
879
  			iter_pos = 0;
  			iter_dir = 0;
  			iter_siz = 0;
47fe6300   Pavel Govyadinov   fixed an accident...
880
  #endif
7f297fc6   Pavel Govyadinov   added more detail...
881
882
  			stepsize = 3.0;
  			n_pixels = 16.0;
98eecaa9   David Mayerich   VS and win32 updates
883
  
c0e09133   Pavel Govyadinov   STABLE: made temp...
884
  			srand(100);	
a9b45efe   Pavel Govyadinov   changes to spider
885
  			texID = id;
7f297fc6   Pavel Govyadinov   added more detail...
886
  			GenerateFBO(n_pixels*2, numSamples*n_pixels, direction_texID, direction_buffID); 
c0e09133   Pavel Govyadinov   STABLE: made temp...
887
  			CHECK_OPENGL_ERROR
7f297fc6   Pavel Govyadinov   added more detail...
888
  			GenerateFBO(n_pixels*2, numSamplesPos*n_pixels, position_texID, position_buffID); 
c0e09133   Pavel Govyadinov   STABLE: made temp...
889
  			CHECK_OPENGL_ERROR
7f297fc6   Pavel Govyadinov   added more detail...
890
  			GenerateFBO(n_pixels*2, numSamplesMag*n_pixels, radius_texID, radius_buffID); 
c0e09133   Pavel Govyadinov   STABLE: made temp...
891
  			CHECK_OPENGL_ERROR
7f297fc6   Pavel Govyadinov   added more detail...
892
  			GenerateFBO(16, 216, cylinder_texID, cylinder_buffID); 
c0e09133   Pavel Govyadinov   STABLE: made temp...
893
  			CHECK_OPENGL_ERROR
09049866   Pavel Govyadinov   fixed a drawing b...
894
  			t_dir.MapCudaTexture(direction_texID, GL_TEXTURE_2D);
c37611a6   Pavel Govyadinov   removed the time ...
895
  			t_dir.Alloc(numSamples);
7f297fc6   Pavel Govyadinov   added more detail...
896
  			t_pos.MapCudaTexture(position_texID, GL_TEXTURE_2D);
c37611a6   Pavel Govyadinov   removed the time ...
897
  			t_pos.Alloc(numSamplesPos);
7f297fc6   Pavel Govyadinov   added more detail...
898
  			t_mag.MapCudaTexture(radius_texID, GL_TEXTURE_2D);
c37611a6   Pavel Govyadinov   removed the time ...
899
  			t_mag.Alloc(numSamplesMag);
5eeaf941   Pavel Govyadinov   changer to the ba...
900
  			setMatrix();
db3c28c9   Pavel Govyadinov   Implemented glDis...
901
  			dList = glGenLists(3);
db3c28c9   Pavel Govyadinov   Implemented glDis...
902
  			glListBase(dList);
7f297fc6   Pavel Govyadinov   added more detail...
903
  			Bind(direction_texID, direction_buffID, numSamples, n_pixels);
b50c840e   David Mayerich   eliminated ANN fr...
904
  				genDirectionVectors(5*stim::PI/4);
3f15dade   Pavel Govyadinov   changed the plane...
905
  			Unbind();
7f297fc6   Pavel Govyadinov   added more detail...
906
  			Bind(position_texID, position_buffID, numSamplesPos, n_pixels);
4166e973   Pavel Govyadinov   Added debug tags ...
907
  				genPositionVectors(0.2);
3f15dade   Pavel Govyadinov   changed the plane...
908
  			Unbind();
7f297fc6   Pavel Govyadinov   added more detail...
909
  			Bind(radius_texID, radius_buffID, numSamplesMag, n_pixels);
035d798f   Pavel Govyadinov   modified the spid...
910
  				genMagnitudeVectors();
3f15dade   Pavel Govyadinov   changed the plane...
911
  			Unbind();
7f297fc6   Pavel Govyadinov   added more detail...
912
  			Bind(cylinder_texID, cylinder_buffID, 27);
09049866   Pavel Govyadinov   fixed a drawing b...
913
  //				DrawCylinder();
8e56a0a7   Pavel Govyadinov   Added the propose...
914
  			Unbind();
a39577bf   Pavel Govyadinov   Changes to the sp...
915
  		}
5f81932b   David Mayerich   restored Pavel's ...
916
  		
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
917
918
919
  //--------------------------------------------------------------------------//
  //-----------------------------ACCESS METHODS-------------------------------//
  //--------------------------------------------------------------------------//
8e56a0a7   Pavel Govyadinov   Added the propose...
920
  		///Returns the p vector.
7d3162a2   Pavel Govyadinov   fixed majority of...
921
  		vec3<float>
a39577bf   Pavel Govyadinov   Changes to the sp...
922
  		getPosition()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
923
  		{
8e56a0a7   Pavel Govyadinov   Added the propose...
924
  			return p;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
925
926
  		}
  	
8e56a0a7   Pavel Govyadinov   Added the propose...
927
  		///Returns the d vector.
7d3162a2   Pavel Govyadinov   fixed majority of...
928
  		vec3<float>
a39577bf   Pavel Govyadinov   Changes to the sp...
929
  		getDirection()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
930
  		{
8e56a0a7   Pavel Govyadinov   Added the propose...
931
  			return d;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
932
933
  		}
  
8e56a0a7   Pavel Govyadinov   Added the propose...
934
  		///Returns the m vector.
09049866   Pavel Govyadinov   fixed a drawing b...
935
  		float	
a39577bf   Pavel Govyadinov   Changes to the sp...
936
  		getMagnitude()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
937
  		{
8e56a0a7   Pavel Govyadinov   Added the propose...
938
  			return m;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
939
940
  		}
  	
ac430567   Pavel Govyadinov   comments comments...
941
  		///@param stim::vec<float> pos, the new p.
8e56a0a7   Pavel Govyadinov   Added the propose...
942
  		///Sets the p vector to input vector pos.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
943
  		void
7d3162a2   Pavel Govyadinov   fixed majority of...
944
  		setPosition(stim::vec3<float> pos)
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
945
  		{
8e56a0a7   Pavel Govyadinov   Added the propose...
946
  			p = pos;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
947
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
948
  
ac430567   Pavel Govyadinov   comments comments...
949
950
951
  		///@param float x x-coordinate.
  		///@param float y y-coordinate.
  		///@param float z z-coordinate.
8e56a0a7   Pavel Govyadinov   Added the propose...
952
  		///Sets the p vector to the input float coordinates x,y,z.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
953
954
955
  		void
  		setPosition(float x, float y, float z)
  		{
8e56a0a7   Pavel Govyadinov   Added the propose...
956
957
958
  			p[0] = x;
  			p[1] = y;
  			p[2] = z;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
959
  		}
887a3e49   Pavel Govyadinov   fixed some order ...
960
  		
ac430567   Pavel Govyadinov   comments comments...
961
  		///@param stim::vec<float> dir, the new d.
8e56a0a7   Pavel Govyadinov   Added the propose...
962
  		///Sets the d vector to input vector dir.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
963
  		void
7d3162a2   Pavel Govyadinov   fixed majority of...
964
  		setDirection(stim::vec3<float> dir)
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
965
  		{
8e56a0a7   Pavel Govyadinov   Added the propose...
966
  			d = dir;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
967
968
  		}
  		
ac430567   Pavel Govyadinov   comments comments...
969
970
971
  		///@param stim::vec<float> x x-coordinate.
  		///@param stim::vec<float> y y-coordinate.
  		///@param stim::vec<float> z z-coordinate.
8e56a0a7   Pavel Govyadinov   Added the propose...
972
  		///Sets the d vector to the input float coordinates x,y,z.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
973
974
975
  		void
  		setDirection(float x, float y, float z)
  		{
8e56a0a7   Pavel Govyadinov   Added the propose...
976
977
978
  			d[0] = x;
  			d[1] = y;
  			d[2] = z;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
979
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
980
  			
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
981
  		
ac430567   Pavel Govyadinov   comments comments...
982
  		///@param float mag, size of the sampled region.
8e56a0a7   Pavel Govyadinov   Added the propose...
983
  		///Sets the m vector to the input mag for both templates.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
984
  		void
5f81932b   David Mayerich   restored Pavel's ...
985
  		setMagnitude(float mag)
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
986
  		{
7f297fc6   Pavel Govyadinov   added more detail...
987
  			m = mag;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
988
  		}
5f81932b   David Mayerich   restored Pavel's ...
989
  		
ac430567   Pavel Govyadinov   comments comments...
990
991
992
993
  		///@param float x, voxel size in the x direction.
  		///@param float y, voxel size in the y direction.
  		///@param float z, voxel size in the z direction.
  		///Sets the voxel sizes in each direction. necessary for non-standard data.
8e56a0a7   Pavel Govyadinov   Added the propose...
994
995
996
997
998
999
1000
1001
  		void
  		setDims(float x, float y, float z)
  		{
  			S[0] = x;
  			S[1] = y;
  			S[2] = z;
  		}
  
ac430567   Pavel Govyadinov   comments comments...
1002
1003
1004
  		///@param stim::vec<float> Dims, voxel size.
  		///Sets the voxel sizes in each direction. necessary for non-standard data.
  		void
7d3162a2   Pavel Govyadinov   fixed majority of...
1005
  		setDims(stim::vec3<float> Dims)
ac430567   Pavel Govyadinov   comments comments...
1006
1007
1008
1009
1010
1011
1012
1013
  		{
  			S = Dims;
  		}
  
  		///@param float x, size of the data in the x direction.
  		///@param float y, size of the data in the y direction.
  		///@param float z, size of the data in the z direction.
  		///Sets the data volume sizes in each direction.
b710b044   Pavel Govyadinov   Added more tempor...
1014
  		void
8e56a0a7   Pavel Govyadinov   Added the propose...
1015
  		setSize(float x, float y, float z)
b710b044   Pavel Govyadinov   Added more tempor...
1016
  		{
8e56a0a7   Pavel Govyadinov   Added the propose...
1017
1018
1019
  			R[0] = x;
  			R[1] = y;
  			R[2] = z;
b710b044   Pavel Govyadinov   Added more tempor...
1020
  		}
ac430567   Pavel Govyadinov   comments comments...
1021
1022
1023
1024
  
  		///@param stim::vec<float> Dims, size of the volume.
  		///Sets the data volume sizes in each direction.
  		void
7d3162a2   Pavel Govyadinov   fixed majority of...
1025
  		setSize(stim::vec3<float> Siz)
ac430567   Pavel Govyadinov   comments comments...
1026
1027
1028
  		{
  			S = Siz;
  		}
b710b044   Pavel Govyadinov   Added more tempor...
1029
  		
ac430567   Pavel Govyadinov   comments comments...
1030
1031
1032
1033
  		///@param stim::vec<float> dir, the vector to which we are rotating.
  		///given a vector to align to, finds the required axis and angle for glRotatef.
  		///rotates from 0.0, 0.0, 1.0 to dir.
  		///return is in degrees for use with glRotatef.
5eeaf941   Pavel Govyadinov   changer to the ba...
1034
  		stim::vec<float>
4fa3f483   Pavel Govyadinov   Fixed the memory ...
1035
  		getRotation(stim::vec3<float> dir)
f304d6de   Pavel Govyadinov   added rotation to...
1036
  		{
d4721000   Pavel Govyadinov   changes with debu...
1037
  			stim::vec<float> out(0.0,0.0,0.0,0.0);
4166e973   Pavel Govyadinov   Added debug tags ...
1038
  			stim::vec3<float> from(0.0,0.0,1.0);
b50c840e   David Mayerich   eliminated ANN fr...
1039
  			out[0] = acos(dir.dot(from))*180/stim::PI;
4166e973   Pavel Govyadinov   Added debug tags ...
1040
1041
1042
1043
1044
  			#ifdef DEBUG
  				std::cout << "out is " << out << std::endl;
  				std::cout << "when rotating from " << from << " to " << dir << std::endl;
  			#endif
  			if(out[0] < 0.01){
f304d6de   Pavel Govyadinov   added rotation to...
1045
1046
1047
1048
  				out[0] = 0.0;
  				out[1] = 0.0;
  				out[2] = 0.0;
  				out[3] = 1.0;
f7b84fb2   Pavel Govyadinov   more debug statem...
1049
1050
1051
1052
1053
1054
1055
  			}
  			else if(out[0] < -180.0+0.01)
  			{
  				out[0] = 180.0;
  				out[1] = 1.0;
  				out[2] = 0.0;
  				out[3] = 0.0;
f304d6de   Pavel Govyadinov   added rotation to...
1056
  			} else {
4166e973   Pavel Govyadinov   Added debug tags ...
1057
1058
  				stim::vec3<float> temp(0.0, 0.0, 0.0);;
  				temp = (from.cross(dir)).norm();
f304d6de   Pavel Govyadinov   added rotation to...
1059
1060
1061
1062
  				out[1] = temp[0];
  				out[2] = temp[1];
  				out[3] = temp[2];
  			}
f304d6de   Pavel Govyadinov   added rotation to...
1063
1064
  			return out;
  		}
84eff8b1   Pavel Govyadinov   Merged only the n...
1065
  
ac430567   Pavel Govyadinov   comments comments...
1066
  		///@param stim::vec<float> pos, the position of the seed to be added.
84eff8b1   Pavel Govyadinov   Merged only the n...
1067
1068
1069
  		///Adds a seed to the seed list.
  		///Assumes that the coordinates passes are in tissue space.
  		void
7d3162a2   Pavel Govyadinov   fixed majority of...
1070
  		setSeed(stim::vec3<float> pos)
a9b45efe   Pavel Govyadinov   changes to spider
1071
  		{
84eff8b1   Pavel Govyadinov   Merged only the n...
1072
  			seeds.push(pos);
a9b45efe   Pavel Govyadinov   changes to spider
1073
  		}
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
1074
  
ac430567   Pavel Govyadinov   comments comments...
1075
1076
  		///@param stim::vec<float> dir, the direction of the seed to be added.
  		///Adds a seed to the seed directions list.
79a9bf3f   Pavel Govyadinov   new implementatio...
1077
  		void
7d3162a2   Pavel Govyadinov   fixed majority of...
1078
  		setSeedVec(stim::vec3<float> dir)
59781ee3   Pavel Govyadinov   fixed a stask bug...
1079
1080
1081
1082
  		{
  			seedsvecs.push(dir);
  		}
  
ac430567   Pavel Govyadinov   comments comments...
1083
1084
1085
  		///@param float mag, the size of the seed to be added.
  		///Adds a seed to the seed list.
  		///Assumes that the coordinates passes are in tissue space.
59781ee3   Pavel Govyadinov   fixed a stask bug...
1086
1087
  		void
  		setSeedMag(float mag)
79a9bf3f   Pavel Govyadinov   new implementatio...
1088
  		{
59781ee3   Pavel Govyadinov   fixed a stask bug...
1089
  			seedsmags.push(mag);
84eff8b1   Pavel Govyadinov   Merged only the n...
1090
  		}
79a9bf3f   Pavel Govyadinov   new implementatio...
1091
  
59781ee3   Pavel Govyadinov   fixed a stask bug...
1092
  
ac430567   Pavel Govyadinov   comments comments...
1093
1094
1095
  		///@param float x, x-position of the seed to be added.
  		///@param float y, y-position of the seed to be added.
  		///@param float z, z-position of the seed to be added.
84eff8b1   Pavel Govyadinov   Merged only the n...
1096
1097
1098
1099
1100
  		///Adds a seed to the seed list.
  		///Assumes that the coordinates passes are in tissue space.
  		void
  		setSeed(float x, float y, float z)
  		{
84eff8b1   Pavel Govyadinov   Merged only the n...
1101
1102
1103
  			seeds.push(stim::vec<float>(x, y, z));
  		}
  
ac430567   Pavel Govyadinov   comments comments...
1104
1105
1106
1107
  		///@param float x, x-direction of the seed to be added.
  		///@param float y, y-direction of the seed to be added.
  		///@param float z, z-direction of the seed to be added.
  		///Adds a seed to the seed directions list.
84eff8b1   Pavel Govyadinov   Merged only the n...
1108
1109
1110
  		void
  		setSeedVec(float x, float y, float z)
  		{
84eff8b1   Pavel Govyadinov   Merged only the n...
1111
1112
  			seedsvecs.push(stim::vec<float>(x, y, z));
  		}
ac430567   Pavel Govyadinov   comments comments...
1113
1114
  		
  		///Method to get the top of the seed positions stack.
7d3162a2   Pavel Govyadinov   fixed majority of...
1115
  		stim::vec3<float> 
84eff8b1   Pavel Govyadinov   Merged only the n...
1116
1117
  		getLastSeed()
  		{
7d3162a2   Pavel Govyadinov   fixed majority of...
1118
  			stim::vec3<float> tp = seeds.top();
84eff8b1   Pavel Govyadinov   Merged only the n...
1119
1120
1121
  			return tp;
  		}
  
ac430567   Pavel Govyadinov   comments comments...
1122
  		///Method to get the top of the seed direction stack.
7d3162a2   Pavel Govyadinov   fixed majority of...
1123
  		stim::vec3<float> 
59781ee3   Pavel Govyadinov   fixed a stask bug...
1124
1125
  		getLastSeedVec()
  		{
7d3162a2   Pavel Govyadinov   fixed majority of...
1126
  			stim::vec3<float> tp = seedsvecs.top();
59781ee3   Pavel Govyadinov   fixed a stask bug...
1127
1128
1129
  			return tp;
  		}
  
ac430567   Pavel Govyadinov   comments comments...
1130
  		///Method to get the top of the seed magnitude stack.
59781ee3   Pavel Govyadinov   fixed a stask bug...
1131
1132
1133
1134
1135
1136
1137
  		float
  		getLastSeedMag()
  		{
  			float tp = seedsmags.top();
  			return tp;
  		}
  
ac430567   Pavel Govyadinov   comments comments...
1138
  		///deletes all data associated with the last seed.
59781ee3   Pavel Govyadinov   fixed a stask bug...
1139
1140
1141
1142
1143
  		void
  		popSeed()
  		{
  			seeds.pop();
  			seedsvecs.pop();
6707e8f5   Pavel Govyadinov   handled the case ...
1144
  			seedsmags.pop();
59781ee3   Pavel Govyadinov   fixed a stask bug...
1145
  		}
ac430567   Pavel Govyadinov   comments comments...
1146
1147
  		
  		///returns the seeds position stack.
7d3162a2   Pavel Govyadinov   fixed majority of...
1148
  		std::stack<stim::vec3<float> >
84eff8b1   Pavel Govyadinov   Merged only the n...
1149
1150
1151
1152
1153
  		getSeeds()
  		{
  			return seeds;
  		}
  
efe7b7cc   Pavel Govyadinov   Added a detailed ...
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
  		///sets the number of direction templates.
  		void
  		setNumberDirectionTemplates(int n)
  		{
  			numSamples = n;
  		}
  
  		///sets the number of position templates.
  		void
  		setNumberPositionTemplates(int n)
  		{
  			numSamplesPos = n;
  		}
  
  		///sets the number of position templates.
  		void
  		setNumberSizeTemplates(int n)
  		{
  			numSamplesMag = n;
  		}
  
  		#ifdef TIMING
  		///Returns the timings at the moment the method is called.
  		///In the following order: Branch, Direction, Position, Size, Cost, Network, Hit_detetion.
  		std::vector<double> 
  		getTimings()
  		{
  			std::vector <double> ret;
  			ret.resize(7);
  			ret[0] = branch_time;
  			ret[1] = direction_time;
  			ret[2] = position_time;
  			ret[3] = size_time;
  			ret[4] = cost_time;
  			ret[5] = network_time;
  			ret[6] = hit_time;
  
  			return ret;
  		}
  		#endif
  
ac430567   Pavel Govyadinov   comments comments...
1195
  		///returns true if all seed stacks are empty, else false.
84eff8b1   Pavel Govyadinov   Merged only the n...
1196
1197
1198
  		bool
  		Empty()
  		{
ac430567   Pavel Govyadinov   comments comments...
1199
  			//return (seeds.empty() && seedsvecs.empty() && seedsmags.empty());
1306fd96   Pavel Govyadinov   minor bug fixes i...
1200
  			return (seeds.empty() && seedsvecs.empty());
84eff8b1   Pavel Govyadinov   Merged only the n...
1201
  		}
ac430567   Pavel Govyadinov   comments comments...
1202
1203
1204
  
  		///@param std::string file:file with variables to populate the seed stacks.
  		///Adds a seed to the seed list, including the position, direction and magnitude.
84eff8b1   Pavel Govyadinov   Merged only the n...
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
  		///Assumes that the coordinates passes are in tissue space.
  		void
  		setSeeds(std::string file)
  		{
  			std::ifstream myfile(file.c_str());
  			string line;
  			if(myfile.is_open())
             		{
                     		while (getline(myfile, line))
                     		{
ac430567   Pavel Govyadinov   comments comments...
1215
1216
                             		float x, y, z, u, v, w, m;
                             		myfile >> x >> y >> z >> u >> v >> w >> m;
8761b649   Pavel Govyadinov   moved a lot of th...
1217
  					setSeed(x, y, z);
ac430567   Pavel Govyadinov   comments comments...
1218
1219
  					setSeedVec(u, v, w);
  					setSeedMag(m);
84eff8b1   Pavel Govyadinov   Merged only the n...
1220
1221
1222
1223
1224
                     		}
                     	myfile.close();
  	          	} else {
          	                  std::cerr<<"failed" << std::endl;
  			}
79a9bf3f   Pavel Govyadinov   new implementatio...
1225
1226
  		}
  		
ac430567   Pavel Govyadinov   comments comments...
1227
  		///Saves the network to a file.
79a9bf3f   Pavel Govyadinov   new implementatio...
1228
  		void
84eff8b1   Pavel Govyadinov   Merged only the n...
1229
  		saveNetwork(std::string name)
79a9bf3f   Pavel Govyadinov   new implementatio...
1230
  		{
0311ab81   Pavel Govyadinov   fixed some issues...
1231
  /*			stim::glObj<float> sk;
8761b649   Pavel Govyadinov   moved a lot of th...
1232
1233
1234
  			for(int i = 0; i < nt.sizeE(); i++)
  			{
  				std::vector<stim::vec< float > > cm = nt.getEdgeCenterLineMag(i);
7d3162a2   Pavel Govyadinov   fixed majority of...
1235
                   		std::vector<stim::vec3< float > > ce = nt.getEdgeCenterLine(i);
8761b649   Pavel Govyadinov   moved a lot of th...
1236
1237
1238
1239
1240
1241
1242
1243
  				sk.Begin(stim::OBJ_LINE);
  				for(int j = 0; j < ce.size(); j++)
  				{
  					sk.TexCoord(cm[j][0]);
  					sk.Vertex(ce[j][0], ce[j][1], ce[j][2]);
  				}
  				sk.End();
  			}	
0311ab81   Pavel Govyadinov   fixed some issues...
1244
  */			sk.save(name);
79a9bf3f   Pavel Govyadinov   new implementatio...
1245
  		}
84eff8b1   Pavel Govyadinov   Merged only the n...
1246
  
8761b649   Pavel Govyadinov   moved a lot of th...
1247
  		///Depreciated, but might be reused later()
ac430567   Pavel Govyadinov   comments comments...
1248
  		///returns a COPY of the entire stim::glObj object.
84eff8b1   Pavel Govyadinov   Merged only the n...
1249
1250
1251
  		stim::glObj<float>
  		getNetwork()
  		{
0311ab81   Pavel Govyadinov   fixed some issues...
1252
  			return sk;
84eff8b1   Pavel Govyadinov   Merged only the n...
1253
  		}
ac430567   Pavel Govyadinov   comments comments...
1254
1255
1256
1257
1258
1259
1260
  
  		///returns a COPY of the entire stim::glnetwork object.
  		stim::glnetwork<T>
  		getGLNetwork()
  		{
  			return nt;
  		}
84eff8b1   Pavel Govyadinov   Merged only the n...
1261
1262
1263
1264
1265
1266
  		
  		///Function to get back the framebuffer Object attached to the spider.
  		///For external access.
  		GLuint
  		getFB()
  		{
7f297fc6   Pavel Govyadinov   added more detail...
1267
  			return cylinder_buffID;
84eff8b1   Pavel Govyadinov   Merged only the n...
1268
1269
  		}
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
1270
1271
1272
1273
1274
1275
1276
1277
  //--------------------------------------------------------------------------//
  //-----------------------------TEMPORARY METHODS----------------------------//
  //--------------------------------------------------------------------------//
  
  		///temporary Method necessary for visualization and testing.
  		void
  		Update()
  		{
7d3162a2   Pavel Govyadinov   fixed majority of...
1278
  			vec3<float> Y(1.0,0.0,0.0);
8e56a0a7   Pavel Govyadinov   Added the propose...
1279
  			if(cos(Y.dot(d))< 0.087){
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
1280
  				Y[0] = 0.0; Y[1] = 1.0;}
8e56a0a7   Pavel Govyadinov   Added the propose...
1281
1282
1283
  			hor = stim::rect<float>(m, p, d.norm(),
  				((Y.cross(d)).cross(d)).norm());
  			ver = stim::rect<float>(m, p, d.norm(),
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
1284
1285
1286
1287
  				 hor.n());
  		}
  
  
c4887649   Pavel Govyadinov   fixed a significa...
1288
  		int
13c2a7d4   Pavel Govyadinov   some changes to t...
1289
1290
  		Step()
  		{
4166e973   Pavel Govyadinov   Added debug tags ...
1291
  			#ifdef DEBUG
09049866   Pavel Govyadinov   fixed a drawing b...
1292
  			std::cerr << "Took a step" << std::endl;
4166e973   Pavel Govyadinov   Added debug tags ...
1293
  			#endif
7f297fc6   Pavel Govyadinov   added more detail...
1294
  			Bind(direction_texID, direction_buffID, numSamples, n_pixels);
84eff8b1   Pavel Govyadinov   Merged only the n...
1295
  			CHECK_OPENGL_ERROR
035d798f   Pavel Govyadinov   modified the spid...
1296
  				findOptimalDirection();
3f15dade   Pavel Govyadinov   changed the plane...
1297
  			Unbind();
7f297fc6   Pavel Govyadinov   added more detail...
1298
  			Bind(position_texID, position_buffID, numSamplesPos, n_pixels);
035d798f   Pavel Govyadinov   modified the spid...
1299
  				findOptimalPosition();
3f15dade   Pavel Govyadinov   changed the plane...
1300
  			Unbind();
7f297fc6   Pavel Govyadinov   added more detail...
1301
  			Bind(radius_texID, radius_buffID, numSamplesMag, n_pixels);
035d798f   Pavel Govyadinov   modified the spid...
1302
  				findOptimalScale();
3f15dade   Pavel Govyadinov   changed the plane...
1303
  			Unbind();
84eff8b1   Pavel Govyadinov   Merged only the n...
1304
1305
  			CHECK_OPENGL_ERROR
  
59781ee3   Pavel Govyadinov   fixed a stask bug...
1306
1307
1308
  			return current_cost;
  		}
  
556c4e15   Pavel Govyadinov   Changed the handl...
1309
1310
1311
1312
  
  		void
  		printTransform()
  		{
8e56a0a7   Pavel Govyadinov   Added the propose...
1313
  			std::cout << cT << std::endl;
b710b044   Pavel Govyadinov   Added more tempor...
1314
  		}
a9b45efe   Pavel Govyadinov   changes to spider
1315
  
f31bf86d   Pavel Govyadinov   Added skeleton fu...
1316
1317
1318
1319
1320
1321
  
  //--------------------------------------------------------------------------//
  //-----------------------------EXPERIMENTAL METHODS-------------------------//
  //--------------------------------------------------------------------------//
  
  		void
34ec8a82   David Mayerich   added comments
1322
  		MonteCarloDirectionVectors(int nSamples, float solidAngle = stim::TAU)
efe7b7cc   Pavel Govyadinov   Added a detailed ...
1323
  		{
0311ab81   Pavel Govyadinov   fixed some issues...
1324
1325
1326
  //			float PHI[2];//, Z[2];//, range;
  //			PHI[0] = asin(solidAngle/2);
  //			PHI[1] = asin(0);
efe7b7cc   Pavel Govyadinov   Added a detailed ...
1327
  			
0311ab81   Pavel Govyadinov   fixed some issues...
1328
1329
  //			Z[0] = cos(PHI[0]);
  //			Z[1] = cos(PHI[1]);
efe7b7cc   Pavel Govyadinov   Added a detailed ...
1330
  			
0311ab81   Pavel Govyadinov   fixed some issues...
1331
  //			range = Z[0] - Z[1];
efe7b7cc   Pavel Govyadinov   Added a detailed ...
1332
  
efe7b7cc   Pavel Govyadinov   Added a detailed ...
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
  			std::vector<stim::vec3<float> > vecsUni;
  			for(int i = 0; i < numSamplesPos; i++)
  			{
  				stim::vec3<float> a(uniformRandom()*0.8, uniformRandom()*0.8, 0.0);
  				a[0] = a[0]-0.4;
  				a[1] = a[1]-0.4;
  				vecsUni.push_back(a);
  			}
  
  			stringstream name;
  			for(int i = 0; i < numSamplesPos; i++)
  				name << vecsUni[i].str() << std::endl;
  			
  			std::ofstream outFile;
  			outFile.open("New_Pos_Vectors.txt");
  			outFile << name.str().c_str();
  		}
  		void
f31bf86d   Pavel Govyadinov   Added skeleton fu...
1351
1352
  		DrawCylinder()
  		{	 
f31bf86d   Pavel Govyadinov   Added skeleton fu...
1353
1354
1355
  			 glNewList(dList+3, GL_COMPILE);
  			 float z0 = -0.5; float z1 = 0.5; float r0 = 0.5;
  			 float x,y;
84eff8b1   Pavel Govyadinov   Merged only the n...
1356
  			 float xold = 0.5; float yold = 0.0;
ac430567   Pavel Govyadinov   comments comments...
1357
1358
  			 float step = 360.0/numSamples*32;
  			 //float step = 360.0/8.0;
f31bf86d   Pavel Govyadinov   Added skeleton fu...
1359
1360
  			 glEnable(GL_TEXTURE_3D);
  			 glBindTexture(GL_TEXTURE_3D, texID);
ac430567   Pavel Govyadinov   comments comments...
1361
  			 glBegin(GL_QUAD_STRIP);
84eff8b1   Pavel Govyadinov   Merged only the n...
1362
  			 int j = 0;
f31bf86d   Pavel Govyadinov   Added skeleton fu...
1363
1364
  			 	for(float i = step; i <= 360.0; i += step)
  			 	{
b50c840e   David Mayerich   eliminated ANN fr...
1365
1366
  					 x=r0*cos(i*stim::TAU/360.0);
  					 y=r0*sin(i*stim::TAU/360.0);
f31bf86d   Pavel Govyadinov   Added skeleton fu...
1367
  					 glTexCoord3f(x,y,z0); 
ac430567   Pavel Govyadinov   comments comments...
1368
1369
  					 glVertex2f(0.0, j*6.4+6.4);
  //					 glVertex2f(0.0, j*27.0+27.0);
f31bf86d   Pavel Govyadinov   Added skeleton fu...
1370
  					 glTexCoord3f(x,y,z1); 
ac430567   Pavel Govyadinov   comments comments...
1371
1372
  					 glVertex2f(16.0, j*6.4+6.4);
  //					 glVertex2f(16.0, j*27.0+27.0);
f31bf86d   Pavel Govyadinov   Added skeleton fu...
1373
  					 glTexCoord3f(xold,yold,z1); 
ac430567   Pavel Govyadinov   comments comments...
1374
1375
  					 glVertex2f(16.0, j*6.4); 
  //					 glVertex2f(16.0, j*27.0); 
f31bf86d   Pavel Govyadinov   Added skeleton fu...
1376
  					 glTexCoord3f(xold,yold,z0); 
ac430567   Pavel Govyadinov   comments comments...
1377
1378
  					 glVertex2f(0.0, j*6.4);
  //					 glVertex2f(0.0, j*27.0);
f31bf86d   Pavel Govyadinov   Added skeleton fu...
1379
1380
1381
1382
1383
1384
  					 xold=x;
  					 yold=y;
  					 j++;
  				}
  		      	 glEnd();  
  			 glEndList();
f31bf86d   Pavel Govyadinov   Added skeleton fu...
1385
  		}
e45b97ce   Pavel Govyadinov   safety commit, I ...
1386
  
8c4f5d84   Pavel Govyadinov   fixed the issue w...
1387
  ///need to return the cylinder.
7f297fc6   Pavel Govyadinov   added more detail...
1388
  ///SOMETHING MIGHT BE GOING ON HERE IN GENERATE BUFFER.
e45b97ce   Pavel Govyadinov   safety commit, I ...
1389
1390
1391
1392
  		void
  		DrawLongCylinder(int n = 8, int l_template = 8,int l_square = 8)
  		{
  			int cylLen = cL.size()-1;
7f297fc6   Pavel Govyadinov   added more detail...
1393
1394
  			GenerateFBO(n*l_square, cylLen*l_template, cylinder_texID, cylinder_buffID);
  			Bind(cylinder_texID, cylinder_buffID, cylLen, l_template*l_square/2.0);
e45b97ce   Pavel Govyadinov   safety commit, I ...
1395
  			stim::cylinder<float> cyl(cL, cM);
7d3162a2   Pavel Govyadinov   fixed majority of...
1396
  			std::vector<std::vector<stim::vec3<float> > > p = cyl.getPoints(n);
e45b97ce   Pavel Govyadinov   safety commit, I ...
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
  			for(int i = 0; i < p.size()-1; i++)	///number of circles
  			{
  				for(int j = 0; j < p[0].size()-1; j++)		///points in the circle
  				{
  					glBegin(GL_QUADS);
  						glTexCoord3f(p[i][j][0], p[i][j][1], p[i][j][2]);
  						glVertex2f(j*l_square,  i*(float)l_template); 
  
  						glTexCoord3f(p[i][j+1][0], p[i][j+1][1], p[i][j+1][2]);
  						glVertex2f(j*l_square+l_square, i*(float)l_template); 
  
  						glTexCoord3f(p[i+1][j+1][0], p[i+1][j+1][1], p[i+1][j+1][2]);
  						glVertex2f(j*l_square+l_square, i*(float)l_template+(float)l_template); 
  
  						glTexCoord3f(p[i+1][j][0], p[i+1][j][1], p[i+1][j][2]);
  						glVertex2f(j*l_square,i*(float)l_template+(float)l_template); 
  					glEnd();
  				}
  			}
  			Unbind();
  		}
84eff8b1   Pavel Govyadinov   Merged only the n...
1418
1419
1420
1421
1422
1423
1424
  		
  
  		///@param min_cost the cost value used for tracing
  		///traces out each seedpoint in the seeds queue to completion in both directions.
  		void
  		trace(int min_cost)
  		{	
7d3162a2   Pavel Govyadinov   fixed majority of...
1425
1426
  			stim::vec3<float> curSeed; 
  			stim::vec3<float> curSeedVec;			
8761b649   Pavel Govyadinov   moved a lot of th...
1427
1428
  			float curSeedMag;
  			while(!Empty())
84eff8b1   Pavel Govyadinov   Merged only the n...
1429
1430
1431
  			{
  				//clear the currently traced line and start a new one.
  				cL.clear();
ac430567   Pavel Govyadinov   comments comments...
1432
  				cM.clear();
8761b649   Pavel Govyadinov   moved a lot of th...
1433
1434
1435
1436
  				cD.clear();
  				curSeed = seeds.top();
  				curSeedVec = seedsvecs.top();
  				curSeedMag = seedsmags.top();
59781ee3   Pavel Govyadinov   fixed a stask bug...
1437
1438
  				seeds.pop();
  				seedsvecs.pop();
ac430567   Pavel Govyadinov   comments comments...
1439
  				seedsmags.pop();
84eff8b1   Pavel Govyadinov   Merged only the n...
1440
1441
  				setPosition(curSeed);
  				setDirection(curSeedVec);
8761b649   Pavel Govyadinov   moved a lot of th...
1442
  				setMagnitude(curSeedMag);
4166e973   Pavel Govyadinov   Added debug tags ...
1443
1444
1445
1446
1447
1448
1449
1450
1451
  
  				#ifdef DEBUG
  					std::cout << "The new seed " << curSeed << curSeedVec << curSeedMag << std::endl;
  				#endif
  				
  //			Bind(direction_texID, direction_buffID, numSamples, n_pixels);
  //			CHECK_OPENGL_ERROR
  //				findOptimalDirection();
  //			Unbind();
ec9d82e1   Pavel Govyadinov   finished the debu...
1452
1453
1454
1455
1456
  //THIS IS EXPERIMENTAL
  			Bind(radius_texID, radius_buffID, numSamplesMag, n_pixels);
  				findOptimalScale();
  			Unbind();
  //THIS IS EXPERIMENTAL
0311ab81   Pavel Govyadinov   fixed some issues...
1457
  
8761b649   Pavel Govyadinov   moved a lot of th...
1458
1459
1460
  //				cL.push_back(curSeed);
  //				cM.push_back(curSeedMag);
  //				cD.push_back(curSeedMag);
0311ab81   Pavel Govyadinov   fixed some issues...
1461
  				traceLine(p, m, min_cost);
84eff8b1   Pavel Govyadinov   Merged only the n...
1462
  			}
84eff8b1   Pavel Govyadinov   Merged only the n...
1463
1464
  		}
  
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1465
  		int
7d3162a2   Pavel Govyadinov   fixed majority of...
1466
  		selectObject(stim::vec3<float> loc, stim::vec3<float> dir, float mag) 
84eff8b1   Pavel Govyadinov   Merged only the n...
1467
1468
  		{
  		//Define the varibles and turn on Selection Mode
efe7b7cc   Pavel Govyadinov   Added a detailed ...
1469
1470
1471
1472
  			
  			#ifdef TIMING
  				gpuStartTimer();
  			#endif
84eff8b1   Pavel Govyadinov   Merged only the n...
1473
  
6ada8448   Pavel Govyadinov   Reverted to 40db1...
1474
  //			float s = 3.0;
84eff8b1   Pavel Govyadinov   Merged only the n...
1475
1476
1477
1478
1479
  			GLuint selectBuf[2048];
  			GLint hits;
  			glSelectBuffer(2048, selectBuf);
  			glDisable(GL_CULL_FACE);
  			(void) glRenderMode(GL_SELECT);
84eff8b1   Pavel Govyadinov   Merged only the n...
1480
1481
1482
1483
1484
1485
1486
1487
  		//Init Names stack
  
  			glInitNames();
  			glPushName(1);
  
  			CHECK_OPENGL_ERROR
  		//What would that vessel see in front of it.
  				camSel.setPosition(loc);
6ada8448   Pavel Govyadinov   Reverted to 40db1...
1488
1489
1490
1491
  				camSel.setFocalDistance(mag/stepsize);
  				camSel.LookAt((loc[0]+dir[0]*mag/stepsize),
  					 (loc[1]+dir[1]*mag/stepsize),
  					 (loc[2]+dir[2]*mag/stepsize));
84eff8b1   Pavel Govyadinov   Merged only the n...
1492
1493
1494
1495
1496
1497
  				ps  = camSel.getPosition();
  				ups = camSel.getUp();
  				ds  = camSel.getLookAt();
  				glMatrixMode(GL_PROJECTION);
  				glPushMatrix();
  				glLoadIdentity();
6ada8448   Pavel Govyadinov   Reverted to 40db1...
1498
  				glOrtho(-mag/stepsize/2.0, mag/stepsize/2.0, -mag/stepsize/2.0, mag/stepsize/2.0, 0.0, mag/stepsize/2.0);
84eff8b1   Pavel Govyadinov   Merged only the n...
1499
1500
1501
1502
1503
1504
1505
1506
  				glMatrixMode(GL_MODELVIEW);
  				glPushMatrix();
  				glLoadIdentity();
  				
  				CHECK_OPENGL_ERROR
  				gluLookAt(ps[0], ps[1], ps[2],
  					 ds[0], ds[1], ds[2],
  					 ups[0], ups[1], ups[2]);
23af8e36   Pavel Govyadinov   modified spider t...
1507
  
0311ab81   Pavel Govyadinov   fixed some issues...
1508
1509
  				sk.Render();
  //				nt.Render();								
23af8e36   Pavel Govyadinov   modified spider t...
1510
  
84eff8b1   Pavel Govyadinov   Merged only the n...
1511
  				CHECK_OPENGL_ERROR
23af8e36   Pavel Govyadinov   modified spider t...
1512
1513
  
  
0311ab81   Pavel Govyadinov   fixed some issues...
1514
1515
  				glLoadName((int) sk.numL());
  //				glLoadName(nt.sizeE());
23af8e36   Pavel Govyadinov   modified spider t...
1516
  
0311ab81   Pavel Govyadinov   fixed some issues...
1517
1518
  				sk.RenderLine(cL);
  //				nt.RenderLine(cL);	
23af8e36   Pavel Govyadinov   modified spider t...
1519
  
84eff8b1   Pavel Govyadinov   Merged only the n...
1520
1521
1522
1523
1524
1525
1526
1527
1528
  //				glPopName();
  				glFlush();
  
  				glMatrixMode(GL_PROJECTION);
  				glPopMatrix();
  				glMatrixMode(GL_MODELVIEW);
  				CHECK_OPENGL_ERROR
  				glPopMatrix();
  
59781ee3   Pavel Govyadinov   fixed a stask bug...
1529
  		//	glEnable(GL_CULL_FACE);
84eff8b1   Pavel Govyadinov   Merged only the n...
1530
  			hits = glRenderMode(GL_RENDER);
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1531
  			int found_hits = processHits(hits, selectBuf);
efe7b7cc   Pavel Govyadinov   Added a detailed ...
1532
1533
1534
1535
  			#ifdef TIMING
  				hit_time += gpuStopTimer();
  			#endif
  
84eff8b1   Pavel Govyadinov   Merged only the n...
1536
1537
1538
1539
1540
  			return found_hits;
  		}
  
  		//Given a size of the array (hits) and the memory holding it (buffer)
  		//returns whether a hit tool place or not.
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1541
  		int
84eff8b1   Pavel Govyadinov   Merged only the n...
1542
1543
  		processHits(GLint hits, GLuint buffer[])
  		{
6ada8448   Pavel Govyadinov   Reverted to 40db1...
1544
  			GLuint *ptr;
84eff8b1   Pavel Govyadinov   Merged only the n...
1545
  			ptr = (GLuint *) buffer;
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1546
1547
1548
  			ptr++;
  			ptr++; //Skip the minimum depth value.  
  			ptr++; //Skip the maximum depth value.
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1549
1550
  
  			
84eff8b1   Pavel Govyadinov   Merged only the n...
1551
  			if(hits == 0)
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1552
1553
1554
  			{
  				return -1;
  			}
84eff8b1   Pavel Govyadinov   Merged only the n...
1555
  			else
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1556
  			{
035d798f   Pavel Govyadinov   modified the spid...
1557
  //				printf ("%u ", *ptr);
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1558
1559
1560
1561
1562
1563
1564
1565
  				return *ptr;
  			}
  		}
  
  		void
  		clearCurrent()
  		{
  			cL.clear();
ac430567   Pavel Govyadinov   comments comments...
1566
  			cM.clear();
0311ab81   Pavel Govyadinov   fixed some issues...
1567
  		}
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1568
  	
0311ab81   Pavel Govyadinov   fixed some issues...
1569
  /*
23af8e36   Pavel Govyadinov   modified spider t...
1570
  		void
7d3162a2   Pavel Govyadinov   fixed majority of...
1571
1572
  		addToNetwork(pair<stim::fiber<float>, int> in, stim::vec3<float> spos, 
  				stim::vec<float> smag, stim::vec3<float> sdir)
23af8e36   Pavel Govyadinov   modified spider t...
1573
  		{
efe7b7cc   Pavel Govyadinov   Added a detailed ...
1574
1575
1576
1577
  			#ifdef TIMING
  				 double s = std::clock();
  			#endif
  			
7d3162a2   Pavel Govyadinov   fixed majority of...
1578
                          std::vector<stim::vec3<float> > ce = in.first.centerline();                
23af8e36   Pavel Govyadinov   modified spider t...
1579
                          std::vector<stim::vec<float> > cm = in.first.centerlinemag();
6707e8f5   Pavel Govyadinov   handled the case ...
1580
  			//if the fiber is longer than 2 steps (the number it takes to diverge)
6b8621f8   Pavel Govyadinov   stable version
1581
  			if(ce.size() > 3)
6707e8f5   Pavel Govyadinov   handled the case ...
1582
1583
  			{	
  				//if we did not hit a fiber
23af8e36   Pavel Govyadinov   modified spider t...
1584
1585
  				if(in.second == -1)
  				{
6707e8f5   Pavel Govyadinov   handled the case ...
1586
1587
1588
  					spos[0] = spos[0]-sdir[0]*smag[0]/2.;
  					spos[1] = spos[1]-sdir[1]*smag[0]/2.;
  					spos[2] = spos[2]-sdir[2]*smag[0]/2.;
821409d5   Pavel Govyadinov   added a way to ad...
1589
  					int h = selectObject(spos, -sdir, smag[0]);
6707e8f5   Pavel Govyadinov   handled the case ...
1590
  					//did we start with a fiber?
11c1b46b   Pavel Govyadinov   minor bug fixes a...
1591
  					if(h != -1 && h < nt.sizeE())
821409d5   Pavel Govyadinov   added a way to ad...
1592
1593
1594
  						nt.addEdge(ce, cm, h, -1);
  					else
  						nt.addEdge(ce, cm, -1, -1);
23af8e36   Pavel Govyadinov   modified spider t...
1595
  				}
6707e8f5   Pavel Govyadinov   handled the case ...
1596
  				//if we hit a fiber?
23af8e36   Pavel Govyadinov   modified spider t...
1597
1598
  				else if(in.second != -1)
  				{
6707e8f5   Pavel Govyadinov   handled the case ...
1599
1600
1601
1602
  					nt.addEdge(ce,cm,-1, in.second);
  					spos[0] = spos[0]-sdir[0]*smag[0]/2.;
  					spos[1] = spos[1]-sdir[1]*smag[0]/2.;
  					spos[2] = spos[2]-sdir[2]*smag[0]/2.;
6707e8f5   Pavel Govyadinov   handled the case ...
1603
1604
  					int h = selectObject(spos, -sdir, smag[0]);
  					//did start with a fiber?
11c1b46b   Pavel Govyadinov   minor bug fixes a...
1605
  					if(h != -1 && h < nt.sizeE()){	
ac430567   Pavel Govyadinov   comments comments...
1606
1607
  			//			std::cout << "got here double" << smag.str() << std::endl;
  						nt.addEdge(ce,cm, h, in.second);	
27194b56   Pavel Govyadinov   major bug fixes, ...
1608
  					} else { nt.addEdge(ce,cm, -1, -1);}
23af8e36   Pavel Govyadinov   modified spider t...
1609
1610
  				}
  			}		
efe7b7cc   Pavel Govyadinov   Added a detailed ...
1611
1612
  
  			#ifdef TIMING
6ada8448   Pavel Govyadinov   Reverted to 40db1...
1613
1614
  				double nt = (std::clock() - s) / (double) CLOCKS_PER_SEC;
  				network_time += nt * 1000.0;
efe7b7cc   Pavel Govyadinov   Added a detailed ...
1615
  			#endif
23af8e36   Pavel Govyadinov   modified spider t...
1616
  		}
0311ab81   Pavel Govyadinov   fixed some issues...
1617
1618
  */
  		void
09049866   Pavel Govyadinov   fixed a drawing b...
1619
  		addToNetwork(std::vector<stim::vec3<float> > L, std::vector<float > M)
0311ab81   Pavel Govyadinov   fixed some issues...
1620
1621
1622
1623
1624
1625
  		{
  			if(L.size() > 3)
  			{
  				sk.Begin(stim::OBJ_LINE);
  				for(int i = 0; i < L.size(); i++)
  				{
09049866   Pavel Govyadinov   fixed a drawing b...
1626
  					sk.TexCoord(M[i]);
0311ab81   Pavel Govyadinov   fixed some issues...
1627
1628
1629
  					sk.Vertex(L[i][0], L[i][1], L[i][2]);
  				}
  				sk.End();
23af8e36   Pavel Govyadinov   modified spider t...
1630
  
0311ab81   Pavel Govyadinov   fixed some issues...
1631
  				nt.addEdge(L,M);
4166e973   Pavel Govyadinov   Added debug tags ...
1632
1633
1634
  				#ifdef DEBUG
  					iter++;
  				#endif
0311ab81   Pavel Govyadinov   fixed some issues...
1635
1636
  			}
  		}
6ada8448   Pavel Govyadinov   Reverted to 40db1...
1637
  
23af8e36   Pavel Govyadinov   modified spider t...
1638
  
821409d5   Pavel Govyadinov   added a way to ad...
1639
1640
1641
  		void
  		printSizes()
  		{
035d798f   Pavel Govyadinov   modified the spid...
1642
1643
  			std::cout << nt.sizeE() << " edges " << std::endl;
  			std::cout << nt.sizeV() << " nodes " << std::endl;
821409d5   Pavel Govyadinov   added a way to ad...
1644
1645
  		}
  
0311ab81   Pavel Govyadinov   fixed some issues...
1646
  		void
09049866   Pavel Govyadinov   fixed a drawing b...
1647
  		traceLine(stim::vec3<float> pos, float mag, int min_cost)
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1648
  		{
821409d5   Pavel Govyadinov   added a way to ad...
1649
  			//starting (seed) position and magnitude.
7d3162a2   Pavel Govyadinov   fixed majority of...
1650
  			stim::vec3<float> spos = getPosition();
a36c3850   Pavel Govyadinov   fixed the bug tha...
1651
  			float smag = getMagnitude();
7d3162a2   Pavel Govyadinov   fixed majority of...
1652
  			stim::vec3<float> sdir = getDirection();	
821409d5   Pavel Govyadinov   added a way to ad...
1653
  
47fe6300   Pavel Govyadinov   fixed an accident...
1654
  //			Bind();
8761b649   Pavel Govyadinov   moved a lot of th...
1655
  //			sk.Begin(stim::OBJ_LINE);
23af8e36   Pavel Govyadinov   modified spider t...
1656
1657
  
  
0311ab81   Pavel Govyadinov   fixed some issues...
1658
1659
  			sk.createFromSelf(GL_SELECT);
  //			nt.createFromSelf(GL_SELECT);
23af8e36   Pavel Govyadinov   modified spider t...
1660
  
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1661
1662
1663
1664
1665
1666
1667
1668
  			cL.push_back(pos);
  			cM.push_back(mag);
  
  //			setPosition(pos);
  //			setMagnitude(mag);
  			int h;
  			bool started = false;
  			bool running = true;
7d3162a2   Pavel Govyadinov   fixed majority of...
1669
  			stim::vec3<float> size(S[0]*R[0], S[1]*R[1], S[2]*R[2]);
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1670
1671
1672
1673
1674
  			while(running)
  			{
  				int cost = Step();
  				if (cost > min_cost){
  					running = false;
3e5d3ad3   Pavel Govyadinov   merged the change...
1675
  					branchDetection2();
0311ab81   Pavel Govyadinov   fixed some issues...
1676
  					addToNetwork(cL, cM);
ec9d82e1   Pavel Govyadinov   finished the debu...
1677
  					#ifdef DEBUG
a36c3850   Pavel Govyadinov   fixed the bug tha...
1678
  					std::cerr << "the cost of " << cost << " > " << min_cost << std::endl;
ec9d82e1   Pavel Govyadinov   finished the debu...
1679
  					#endif
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1680
1681
1682
1683
1684
1685
1686
1687
  					break;
  				} else {
  					//Have we found the edge of the map?
  					pos = getPosition();
  					if(pos[0] > size[0] || pos[1] > size[1]
  					 || pos[2] > size[2] || pos[0] < 0
  					 || pos[1] < 0 || pos[2] < 0)
  					{
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1688
  						running = false;
3e5d3ad3   Pavel Govyadinov   merged the change...
1689
  						branchDetection2();
0311ab81   Pavel Govyadinov   fixed some issues...
1690
  						addToNetwork(cL, cM);
ec9d82e1   Pavel Govyadinov   finished the debu...
1691
  						#ifdef DEBUG
a36c3850   Pavel Govyadinov   fixed the bug tha...
1692
  						std::cerr << "I hit and edge" << std::endl;
ec9d82e1   Pavel Govyadinov   finished the debu...
1693
  						#endif
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1694
1695
1696
1697
1698
1699
1700
1701
1702
  						break;
  					}
  					//If this is the first step in the trace,
  					// save the direction
  					//(to be used later to trace the fiber in the opposite direction)
  					if(started == false){
  						rev = -getDirection();
  						started = true;
  					}
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1703
1704
  					//Has the template size gotten unreasonable?
  					mag = getMagnitude();
09049866   Pavel Govyadinov   fixed a drawing b...
1705
  					if(mag > 75 || mag < 1){
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1706
  						running = false;
3e5d3ad3   Pavel Govyadinov   merged the change...
1707
  						branchDetection2();
0311ab81   Pavel Govyadinov   fixed some issues...
1708
  						addToNetwork(cL, cM);
ec9d82e1   Pavel Govyadinov   finished the debu...
1709
  						#ifdef DEBUG
a36c3850   Pavel Govyadinov   fixed the bug tha...
1710
  						std::cerr << "The templates are too big" << std::endl;
ec9d82e1   Pavel Govyadinov   finished the debu...
1711
  						#endif
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1712
1713
1714
1715
  						break;
  					}
  					else
  					{
7f297fc6   Pavel Govyadinov   added more detail...
1716
  						h = selectObject(p, getDirection(), m);
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1717
1718
  						//Have we hit something previously traced?
  						if(h != -1){
ec9d82e1   Pavel Govyadinov   finished the debu...
1719
  							#ifdef DEBUG
a36c3850   Pavel Govyadinov   fixed the bug tha...
1720
  							std::cerr << "I hit the fiber " << h << std::endl;
ec9d82e1   Pavel Govyadinov   finished the debu...
1721
  							#endif
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1722
  							running = false;
3e5d3ad3   Pavel Govyadinov   merged the change...
1723
  							branchDetection2();
0311ab81   Pavel Govyadinov   fixed some issues...
1724
  							addToNetwork(cL, cM);
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1725
1726
1727
  							break;
  						}
  						else {  
7d3162a2   Pavel Govyadinov   fixed majority of...
1728
  							cL.push_back(stim::vec3<float>(p[0], p[1],p[2]));
7f297fc6   Pavel Govyadinov   added more detail...
1729
  							cM.push_back(m);
47fe6300   Pavel Govyadinov   fixed an accident...
1730
  //							Unbind();
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1731
  							CHECK_OPENGL_ERROR
5de3a9c2   Pavel Govyadinov   CHECKPOINT: befo...
1732
1733
1734
1735
  						}
  				 	}
                   		}
           		}
4166e973   Pavel Govyadinov   Added debug tags ...
1736
1737
1738
  				#ifdef DEBUG
  				std::cout << "I broke out!" << std::endl;
  				#endif
84eff8b1   Pavel Govyadinov   Merged only the n...
1739
  		}
4cefeb6d   Pavel Govyadinov   Changes to the re...
1740
  };
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
1741
1742
  }
  #endif