Blame view

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