Blame view

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