Blame view

stim/gl/gl_spider.h 18 KB
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
1
2
3
  #ifndef STIM_GL_SPIDER_H
  #define STIM_GL_SPIDER_H
  
a9b45efe   Pavel Govyadinov   changes to spider
4
  #include <GL/glew.h>
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
5
  #include <GL/glut.h>
a9b45efe   Pavel Govyadinov   changes to spider
6
7
8
  #include <cuda.h>
  #include <cuda_gl_interop.h>
  #include <cudaGL.h>
1a456186   Pavel Govyadinov   Added directional...
9
  #include <math.h>
42145f38   Pavel Govyadinov   Fixed the issues ...
10
  #include "../gl/gl_texture.h"
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
11
12
13
  #include "../visualization/camera.h"
  #include "./error.h"
  #include "../math/vector.h"
4cefeb6d   Pavel Govyadinov   Changes to the re...
14
  #include "../math/rect.h"
a9b45efe   Pavel Govyadinov   changes to spider
15
16
  #include "../cuda/cost.h"
  #include "../cuda/glbind.h"
385d2447   Pavel Govyadinov   Checkpoint: Conve...
17
  #include <vector>
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
18
  
42145f38   Pavel Govyadinov   Fixed the issues ...
19
20
21
  #include <iostream>
  #include <fstream>
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
22
23
24
25
26
  
  
  /* Technically since gl_spider inherits from gl_texture, we could
  	call the init with a path to an image stack, and upload
  	the images while creating the spider (calling init) */
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
27
28
29
30
31
32
33
  namespace stim
  {
  
  template<typename T>
  class gl_spider : public virtual gl_texture<T>
  {
  	//doen't use gl_texture really, just needs the GLuint id.
4cefeb6d   Pavel Govyadinov   Changes to the re...
34
  	//doesn't even need the texture iD really.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
35
  	private:
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
36
37
38
39
  		stim::vec<float> position;  	//vector designating the position of the spider.
  		stim::vec<float> direction;	//vector designating the orientation of the spider
  						//always a unit vector.
  		stim::vec<float> magnitude;	//magnitude of the direction vector.
4cefeb6d   Pavel Govyadinov   Changes to the re...
40
41
  						//mag[0] = length.
  						//mag[1] = width.
385d2447   Pavel Govyadinov   Checkpoint: Conve...
42
43
44
  		std::vector<stim::vec<float> > dirVectors;
  		std::vector<stim::vec<float> > posVectors;
  		std::vector<stim::vec<float> > magVectors;
f304d6de   Pavel Govyadinov   added rotation to...
45
  		double currentTransform[16];
4cefeb6d   Pavel Govyadinov   Changes to the re...
46
  		using gl_texture<T>::texID;
385d2447   Pavel Govyadinov   Checkpoint: Conve...
47
  		using gl_texture<T>::S;
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
48
  		using gl_texture<T>::R;
a9b45efe   Pavel Govyadinov   changes to spider
49
  		cudaArray* c_Array;
a9b45efe   Pavel Govyadinov   changes to spider
50
  		cudaGraphicsResource_t resource;
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
51
  
a9b45efe   Pavel Govyadinov   changes to spider
52
53
  		GLuint fboID;
  		GLuint texbufferID;
2a18be6d   Pavel Govyadinov   New comments and ...
54
55
  		int iter;		//temporary for testing
  		int numSamples;
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
  
  		/// 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()
  		{
  			genTemplate(dirVectors, 0);
  			int best = getCost();
  			
  			
  		}
  
  		/// Method for finding the best direction for the spider.
  		/// Not sure if necessary since the next position for the spider
  		/// will be at direction * magnitude.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
72
73
74
  		void
  		findOptimalPosition()
  		{
887a3e49   Pavel Govyadinov   fixed some order ...
75
  			
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
76
  			genTemplate(magVectors, 1);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
77
  			int best = getCost();
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
78
79
  		}
  	
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
80
81
82
  		/// Method for finding the best scale for the spider.
  		/// changes the x, y, z size of the spider to minimize the cost
  		/// function. */
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
83
84
85
  		void
  		findOptimalScale()
  		{
2a18be6d   Pavel Govyadinov   New comments and ...
86
  			genTemplate(magVectors, 2);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
87
  			int best = getCost();
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
88
89
  		}
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
90
91
  
  		
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
92
  		void
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
93
  		Optimize()
385d2447   Pavel Govyadinov   Checkpoint: Conve...
94
  		{
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
95
  			/*find the optimum direction and scale */ 
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
96
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
97
  
a9b45efe   Pavel Govyadinov   changes to spider
98
  		
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
99
100
101
102
103
104
105
106
107
  		
  		
  //--------------------------------------------------------------------------//
  //---------------------TEMPLATE CREATION METHODS----------------------------//
  //--------------------------------------------------------------------------//
  
  		///@param solidAngle, the size of the arc to sample.
  		///Method for populating the vector arrays with sampled vectors.
  		///uses the default direction vector <0,0,1>
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
108
  		void
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
109
  		genDirectionVectors(float solidAngle = M_PI/2)
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
110
  		{
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  			
  			vec<float> d_s = direction.cart2sph().norm();
  			vec<float> temp;
  			int dim = (sqrt(numSamples)-1)/2;
  			float p0  	= M_PI/3;
  			float dt  	= solidAngle/(2.0 * (dim + 1));
  			float dp  	= p0/(2.0*(dim + 1));
  	
  			for(int i = -dim; i <= dim; i++){
  				for(int j = -dim; j <= dim; j++){
  					//Create linear index
  					
  					temp[0] = d_s[0]; 			//rotate vector
  					temp[1] = d_s[1]+dt*i;
  					temp[2] = d_s[2]+dp*j;
  					
  					temp = (temp.sph2cart()).norm();	//back to cart
  					dirVectors.push_back(temp);
  				}
  			}
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
131
  		}
2a18be6d   Pavel Govyadinov   New comments and ...
132
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
133
134
135
  		///@param solidAngle, the size of the arc to sample.
  		///Method for populating the buffer with the sampled texture.
  		///uses the default vector <0,0,0>
a9f956be   Pavel Govyadinov   Fixed the cost fu...
136
  		void
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
137
  		genPositionVectors(float delta = 0.2)
a9f956be   Pavel Govyadinov   Fixed the cost fu...
138
  		{
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
  			
  			vec<float> temp;
  			int dim = (sqrt(numSamples)-1)/2;
  			stim::rect<float> samplingPlane =
  				 stim::rect<float>(magnitude[0]*delta, position, direction);
  			float step = 1.0/(dim);
  			//Loop over the samples, keeping the original position sample
  			//in the center of the resulting texture.
  
  			int idx;
  			for(int i = -dim; i <= dim; i++){
  				for(int j = -dim; j <= dim; j++){
  					//Create linear index
  
  					temp = samplingPlane.p(
  							0.5+step*i,
  								 0.5+step*j
  										);
  					posVectors.push_back(temp);
  				}
  			}
  		}
  
  		///@param solidAngle, the size of the arc to sample.
  		///Method for populating the buffer with the sampled texture.
  		///uses the default magnitude <1,1,0>
  		void
  		genMagnitudeVectors(float delta = 0.5)
  		{
  			
  			int dim = (sqrt(numSamples)-1)/2;
  			float min 	= 1.0-delta;
  			float max 	= 1.0+delta;
  			float step	= (max-min)/(numSamples-1);
  			float factor;
  			vec<float> temp;
  
  			for(int i = 0; i < numSamples; i++){
  				//Create linear index
  				factor = (min+step*i)*magnitude[0];
  				temp[0] = factor;
  				temp[1] = factor;
  				magVectors.push_back(temp);	
  			}
  		}
  		///@param vector of stim::vec in that stores all of the samplable vectors.
  		///@param type, one of three operations, 0 for Direction vectors
  		///	1 for Position, 2 for Magnitude.
  		///Function for filling the buffer up with the data based on the vectors
  		///Each vector represents a two rectangular templates.
  		///Loops through all of the vectors and transfers rect. associated with it
  		///Into buffer-space.
  		void
  		genTemplate(std::vector<stim::vec<float> > in, int type)
  		{
  			float x = 0.0;
  			vec<float> Y(1.0,0.0,0.0);
  			vec<float> pos(0.0,0.0,0.0);
  			vec<float> mag(1.0, 1.0, 1.0);
                          switch(type) {
  				case 0:          //Direction
  					Bind();
  					positionTemplate();   ///this can be placed anywhere
  							      ///as long as the GL_TEXTURE
  							      ///is not cleared after this call.
  
  					for(int i = 0; i < in.size(); i++)
  					{
  					 	if(cos(Y.dot(in[i]))< 0.087){                                                                             Y[0] = 0.0; Y[1] = 1.0;}
  						else{Y[0] = 1.0; Y[1] = 0.0;}
  
                                           	hor = stim::rect<float>(mag,
  							 pos, in[i],
                          				((Y.cross(in[i])).cross(in[i])).norm());
                				 	ver = stim::rect<float>(mag,
  							 pos, in[i],
                          				hor.n());
  						UpdateBuffer(x, x+i*10.0);
  					 }
  					 //getSample();
  					 Unbind();
                 				 break;
          			case 1:		 //Position
  					Bind();	
  					positionTemplate();
  					if(cos(Y.dot(direction))< 0.087){                                                                             Y[0] = 0.0; Y[1] = 1.0;}
  					else{Y[0] = 1.0; Y[1] = 0.0;}
  
  					for(int i = 0; i < in.size(); i++)
  					{
                  				hor = stim::rect<float>(magnitude,
  							 in[i], direction,
                        					((Y.cross(direction)).cross(direction))
  							.norm());
                  				ver = stim::rect<float>(magnitude,
  							 in[i], direction,
                          				hor.n());
  						UpdateBuffer(x, x+i*10.0);
  					 }
  					Unbind();
                  			 break;
          			case 2:		  //Scale
  					Bind();	
  					positionTemplate();
  					if(cos(Y.dot(direction))< 0.087){                                                                             Y[0] = 0.0; Y[1] = 1.0;}
  					else{Y[0] = 1.0; Y[1] = 0.0;}
  
                  			for(int i = 0; i < in.size(); i++)
  					{
  						hor = stim::rect<float>(in[i],
  							 position, direction,
                        					((Y.cross(direction)).cross(direction))
  							.norm());
                  				ver = stim::rect<float>(in[i],
  							 position, direction,
                          				hor.n());
  						UpdateBuffer(x, x+i*10.0);
  					}
  					Unbind();
  					break;
          			default:
                  			 std::cout << "unknown case have been passed"
                           			<< std::endl;
  					 break;
  			}               
  		Unbind();
  		CHECK_OPENGL_ERROR
a9f956be   Pavel Govyadinov   Fixed the cost fu...
266
  		}
2a18be6d   Pavel Govyadinov   New comments and ...
267
268
269
270
  		///@param v_x x-coordinate in buffer-space,
  		///@param v_y y-coordinate in buffer-space.
  		///Samples the texturespace and places a sample in the provided coordinates
  		///of bufferspace.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
271
272
273
  		void
  		UpdateBuffer(float v_x, float v_y)
  		{	
a9f956be   Pavel Govyadinov   Fixed the cost fu...
274
275
276
277
278
  			float len = 10.0;
  			stim::vec<float>p1; 
          	        stim::vec<float>p2; 
  	                stim::vec<float>p3; 
                  	stim::vec<float>p4;	
a9f956be   Pavel Govyadinov   Fixed the cost fu...
279
  			p1 = hor.p(1,1);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
280
  			p2 = hor.p(1,0);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
281
  			p3 = hor.p(0,0);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
282
  			p4 = hor.p(0,1);
5e7c7581   Pavel Govyadinov   Debugging build f...
283
  			glBegin(GL_QUADS);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
284
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
285
286
287
288
  					p1[0],
  					p1[1],
  					p1[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
289
290
  				glVertex2f(v_x,v_y);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
291
292
293
294
  					p2[0],
  					p2[1],
  					p2[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
295
296
  				glVertex2f(v_x+len, v_y);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
297
298
299
300
  					p3[0],
  					p3[1],
  					p3[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
301
302
  				glVertex2f(v_x+len, v_y+len);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
303
304
305
  					p4[0],
  					p4[1],
  					p4[2]
a9f956be   Pavel Govyadinov   Fixed the cost fu...
306
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
307
308
  				glVertex2f(v_x, v_y+len);
  			 glEnd();
5e7c7581   Pavel Govyadinov   Debugging build f...
309
  
a9f956be   Pavel Govyadinov   Fixed the cost fu...
310
  			 p1 = ver.p(1,1);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
311
  			 p2 = ver.p(1,0);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
312
  			 p3 = ver.p(0,0);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
313
  			 p4 = ver.p(0,1);
5e7c7581   Pavel Govyadinov   Debugging build f...
314
  		 	 glBegin(GL_QUADS);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
315
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
316
317
318
319
  					p1[0],
  					p1[1],
  					p1[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
320
321
  				glVertex2f(v_x+len, v_y);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
322
323
324
325
  					p2[0],
  					p2[1],
  					p2[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
326
327
  				glVertex2f(v_x+2*len, v_y);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
328
329
330
331
  					p3[0],
  					p3[1],
  					p3[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
332
333
  				glVertex2f(v_x+2*len, v_y+len);
  				glTexCoord3f(
a9f956be   Pavel Govyadinov   Fixed the cost fu...
334
335
336
337
  					p4[0],
  					p4[1],
  					p4[2]
  					);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
338
  				glVertex2f(v_x+len, v_y+len);
5e7c7581   Pavel Govyadinov   Debugging build f...
339
340
  			glEnd(); 
  			CHECK_OPENGL_ERROR
a9f956be   Pavel Govyadinov   Fixed the cost fu...
341
  		}
2a18be6d   Pavel Govyadinov   New comments and ...
342
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
343
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
344
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
345
346
347
  //--------------------------------------------------------------------------//
  //--------------------------------GL METHODS--------------------------------//
  //--------------------------------------------------------------------------//
385d2447   Pavel Govyadinov   Checkpoint: Conve...
348
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  		///@param width sets the width of the buffer.
  		///@param height sets the height of the buffer.
  		///Function for setting up the 2D buffer that stores the samples.
  		void
  		GenerateFBO(unsigned int width, unsigned int height)
  		{
  			glGenFramebuffers(1, &fboID);
  			glBindFramebuffer(GL_FRAMEBUFFER, fboID);
  			int numChannels = 1;
  			unsigned char* texels = new unsigned char[width * height * numChannels];
  			glGenTextures(1, &texbufferID);
  			glBindTexture(GL_TEXTURE_2D, texbufferID);
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  			glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE,
  				 width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texels);   
  			delete[] texels;
  			glBindFramebuffer(GL_FRAMEBUFFER, 0); 
  			glBindTexture(GL_TEXTURE_2D, 0);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
370
  		}
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
371
  
2a18be6d   Pavel Govyadinov   New comments and ...
372
373
  		///Method for controling the buffer and texture binding in order to properly
  		///do the render to texture.
a9f956be   Pavel Govyadinov   Fixed the cost fu...
374
  		void
2a18be6d   Pavel Govyadinov   New comments and ...
375
  		Bind()
a9f956be   Pavel Govyadinov   Fixed the cost fu...
376
  		{
5f81932b   David Mayerich   restored Pavel's ...
377
  			float len = 10.0;
a9f956be   Pavel Govyadinov   Fixed the cost fu...
378
379
380
381
382
383
384
385
386
387
388
  			glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer		
  			glFramebufferTexture2D(
  				GL_FRAMEBUFFER,
  				GL_COLOR_ATTACHMENT0,
  				GL_TEXTURE_2D,
  				texbufferID,
  				0);
  			glBindFramebuffer(GL_FRAMEBUFFER, fboID);
  			GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
  			glDrawBuffers(1, DrawBuffers);
  			glBindTexture(GL_TEXTURE_2D, texbufferID);
5e7c7581   Pavel Govyadinov   Debugging build f...
389
  			glClearColor(1,1,1,1);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
390
  			glClear(GL_COLOR_BUFFER_BIT);
a9f956be   Pavel Govyadinov   Fixed the cost fu...
391
392
393
394
395
396
397
398
  			glMatrixMode(GL_PROJECTION);
  			glLoadIdentity();
  			glMatrixMode(GL_MODELVIEW);
  			glLoadIdentity();
  			glViewport(0,0,2.0*len, numSamples*len);
  			gluOrtho2D(0.0,2.0*len,0.0,numSamples*len);
  			glEnable(GL_TEXTURE_3D);
  			glBindTexture(GL_TEXTURE_3D, texID);
957a4248   Pavel Govyadinov   renamed getSample...
399
  
5e7c7581   Pavel Govyadinov   Debugging build f...
400
  			CHECK_OPENGL_ERROR
5f81932b   David Mayerich   restored Pavel's ...
401
402
  		}
  		
2a18be6d   Pavel Govyadinov   New comments and ...
403
  		///Method for Unbinding all of the texture resources
5f81932b   David Mayerich   restored Pavel's ...
404
405
406
407
408
409
410
411
412
  		void
  		Unbind()
  		{
  			//Finalize GL_buffer
  			glBindTexture(GL_TEXTURE_3D, 0);                      
  			glDisable(GL_TEXTURE_3D);
  			glBindFramebuffer(GL_FRAMEBUFFER,0);
  			glBindTexture(GL_TEXTURE_2D, 0);
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
413
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
414
415
416
417
  		///Method for using the gl manipulation to alighn templates from
  		///Template space (-0.5 0.5) to Texture space (0.0, 1.0),
  		///Based on the position of the spider in real space (arbitrary).
  		void positionTemplate()
5f81932b   David Mayerich   restored Pavel's ...
418
  		{
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
419
420
421
  			stim::vec<float, 4> rot = getRotation(direction);
  			glMatrixMode(GL_TEXTURE);
  			glLoadIdentity();
5f81932b   David Mayerich   restored Pavel's ...
422
  			
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
423
424
425
426
427
428
429
  			glTranslatef(position[0]/512/S[1],
  				     position[1]/512/S[2],
  				     position[2]/426/S[3]);
  			glScalef(magnitude[0]/512/S[1],
  				 magnitude[1]/512/S[2],
  				 magnitude[0]/426/S[3]);
  			glRotatef(rot[0], rot[1], rot[2], rot[3]);
5f81932b   David Mayerich   restored Pavel's ...
430
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
431
432
433
434
435
436
437
438
439
440
  /*
  			glTranslatef(position[0],
  				     position[1],
  				     position[2]);
  			glScalef(magnitude[0],
  				 magnitude[1],
  				 magnitude[0]);
  			glRotatef(rot[0], rot[1], rot[2], rot[3]);
  			glScalef(1/512/0.6, 1/512/0.6, 1/426/2.0); //this does not work
  */			
5f81932b   David Mayerich   restored Pavel's ...
441
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
442
443
444
445
446
447
448
449
450
  			glGetDoublev(GL_TEXTURE_MATRIX, currentTransform);
  			int c = 0;
  			for(int i = 0; i < 16; i++){
  				if(c<3){
  					std::cerr << "[" << currentTransform[i] << "]" << " ";
  					c++;
  				} else {
  					std::cerr << "[" << currentTransform[i] << "]" << "\n";
  					c = 0;
5f81932b   David Mayerich   restored Pavel's ...
451
452
  				}
  			}
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
453
454
  			CHECK_OPENGL_ERROR
  			glMatrixMode(GL_MODELVIEW);
5f81932b   David Mayerich   restored Pavel's ...
455
  		}
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
456
457
  		
  		
5f81932b   David Mayerich   restored Pavel's ...
458
  
42145f38   Pavel Govyadinov   Fixed the issues ...
459
460
461
462
463
  
  //--------------------------------------------------------------------------//
  //--------------------------------CUDA METHODS------------------------------//
  //--------------------------------------------------------------------------//
  		
2a18be6d   Pavel Govyadinov   New comments and ...
464
465
  		/// Method for registering the texture with Cuda for shared
  		///	access.
42145f38   Pavel Govyadinov   Fixed the issues ...
466
467
468
469
470
471
472
473
474
475
476
477
478
  		void
  		createResource()
  		{
  			HANDLE_ERROR(
  				cudaGraphicsGLRegisterImage(
  					 &resource,
  				 	texbufferID,
  				 	GL_TEXTURE_2D,
  				 	//CU_GRAPHICS_REGISTER_FLAGS_NONE)
  					cudaGraphicsMapFlagsReadOnly)
  			);
  		} 
  		
2a18be6d   Pavel Govyadinov   New comments and ...
479
  		///Method for freeing the texture from Cuda for gl access.
42145f38   Pavel Govyadinov   Fixed the issues ...
480
481
482
483
484
485
486
487
  		void
  		destroyResource()
  		{
  			HANDLE_ERROR(
  				cudaGraphicsUnregisterResource(resource)
  			);		
  		}
  
2a18be6d   Pavel Govyadinov   New comments and ...
488
489
  		///Entry-point into the cuda code for calculating the cost
  		///	of a given samples array (in texture form) 
42145f38   Pavel Govyadinov   Fixed the issues ...
490
491
492
493
  		int
  		getCost()
  		{
  			createResource();
5e7c7581   Pavel Govyadinov   Debugging build f...
494
  			int cost = 	get_cost(resource, iter, numSamples);
42145f38   Pavel Govyadinov   Fixed the issues ...
495
496
497
  			destroyResource();
  			iter++;
  			return cost;
a9f956be   Pavel Govyadinov   Fixed the cost fu...
498
499
  		}
  
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
500
  	public:
13c2a7d4   Pavel Govyadinov   some changes to t...
501
502
503
  		stim::rect<float> hor;
  		stim::rect<float> ver;	
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
504
505
506
507
508
  //--------------------------------------------------------------------------//
  //-----------------------------CONSTRUCTORS---------------------------------//
  //--------------------------------------------------------------------------//
  
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
509
  		///Default Constructor
13c2a7d4   Pavel Govyadinov   some changes to t...
510
511
  		gl_spider
  		()
4cefeb6d   Pavel Govyadinov   Changes to the re...
512
513
  		{
  			setPosition(0.0,0.0,0.0);
5f81932b   David Mayerich   restored Pavel's ...
514
  			setDirection(0.0,0.0,1.0);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
515
  			setMagnitude(1.0);
957a4248   Pavel Govyadinov   renamed getSample...
516
517
  			//numSamples = 1089;
  			numSamples = 9;
2a18be6d   Pavel Govyadinov   New comments and ...
518
  		}
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
519
520
521
  		///@param samples, the number of samples this spider is going to use.
  		///best results if samples is can create a perfect root.
  		///Default Constructor
2a18be6d   Pavel Govyadinov   New comments and ...
522
523
524
525
526
527
528
  		gl_spider
  		(int samples)
  		{
  			setPosition(0.0,0.0,0.0);
  			setDirection(0.0,0.0,1.0);
  			setMagnitude(1.0);
  			numSamples = samples;
4cefeb6d   Pavel Govyadinov   Changes to the re...
529
530
  		}
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
531
  		///temporary constructor for convenience, will be removed in further updates.	
13c2a7d4   Pavel Govyadinov   some changes to t...
532
533
  		gl_spider
  		(float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z,
5f81932b   David Mayerich   restored Pavel's ...
534
  			float mag_x)
13c2a7d4   Pavel Govyadinov   some changes to t...
535
  		{
887a3e49   Pavel Govyadinov   fixed some order ...
536
537
538
  			setPosition(pos_x, pos_y, pos_z);
  			setDirection(dir_x, dir_y, dir_z);
  			setMagnitude(mag_x);
2a18be6d   Pavel Govyadinov   New comments and ...
539
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
540
541
  		}
  		///@param GLuint id texture that is going to be sampled.
385d2447   Pavel Govyadinov   Checkpoint: Conve...
542
  		///Attached the spider to the texture with the given GLuint ID.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
543
544
  		///Samples in the default direction acting as the init method.
  		///Also acts an init.	
a9b45efe   Pavel Govyadinov   changes to spider
545
  		void
2a18be6d   Pavel Govyadinov   New comments and ...
546
  		attachSpider(GLuint id)
a9b45efe   Pavel Govyadinov   changes to spider
547
548
  		{
  			texID = id;
2a18be6d   Pavel Govyadinov   New comments and ...
549
  			iter = 0; ///for debugging purposes
5f81932b   David Mayerich   restored Pavel's ...
550
  			GenerateFBO(20, numSamples*10);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
551
  			genDirectionVectors();
887a3e49   Pavel Govyadinov   fixed some order ...
552
553
554
  			genPositionVectors();
  			genMagnitudeVectors();
  			gl_texture<T>::setDims(0.6, 0.6, 2.0);
2a18be6d   Pavel Govyadinov   New comments and ...
555
  			genTemplate(dirVectors, 0);
a39577bf   Pavel Govyadinov   Changes to the sp...
556
  		}
5f81932b   David Mayerich   restored Pavel's ...
557
  		
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
558
559
560
  //--------------------------------------------------------------------------//
  //-----------------------------ACCESS METHODS-------------------------------//
  //--------------------------------------------------------------------------//
385d2447   Pavel Govyadinov   Checkpoint: Conve...
561
  		///Returns the position vector.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
562
  		vec<float>
a39577bf   Pavel Govyadinov   Changes to the sp...
563
  		getPosition()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
564
565
566
567
  		{
  			return position;
  		}
  	
385d2447   Pavel Govyadinov   Checkpoint: Conve...
568
  		///Returns the direction vector.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
569
  		vec<float>
a39577bf   Pavel Govyadinov   Changes to the sp...
570
  		getDirection()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
571
572
573
574
  		{
  			return direction;
  		}
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
575
  		///Returns the magnitude vector.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
576
  		vec<float>
a39577bf   Pavel Govyadinov   Changes to the sp...
577
  		getMagnitude()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
578
579
580
581
  		{
  			return magnitude;
  		}
  	
385d2447   Pavel Govyadinov   Checkpoint: Conve...
582
583
  		///@param vector pos, the new position.
  		///Sets the position vector to input vector pos.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
584
585
586
587
588
  		void
  		setPosition(vec<float> pos)
  		{
  			position = pos;
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
589
590
591
592
593
  
  		///@param x x-coordinate.
  		///@param y y-coordinate.
  		///@param z z-coordinate.
  		///Sets the position vector to the input float coordinates x,y,z.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
594
595
596
597
598
599
600
  		void
  		setPosition(float x, float y, float z)
  		{
  			position[0] = x;
  			position[1] = y;
  			position[2] = z;
  		}
887a3e49   Pavel Govyadinov   fixed some order ...
601
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
602
603
  		///@param vector dir, the new direction.
  		///Sets the direction vector to input vector dir.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
604
605
606
607
608
609
  		void
  		setDirection(vec<float> dir)
  		{
  			direction = dir;
  		}
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
610
611
612
613
  		///@param x x-coordinate.
  		///@param y y-coordinate.
  		///@param z z-coordinate.
  		///Sets the direction vector to the input float coordinates x,y,z.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
614
615
616
617
618
619
620
  		void
  		setDirection(float x, float y, float z)
  		{
  			direction[0] = x;
  			direction[1] = y;
  			direction[2] = z;
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
621
622
623
  			
  		///@param vector dir, the new direction.
  		///Sets the magnitude vector to the input vector mag.	
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
624
625
626
  		void
  		setMagnitude(vec<float> mag)
  		{
5f81932b   David Mayerich   restored Pavel's ...
627
628
  			magnitude[0] = mag[0];
  			magnitude[1] = mag[0];
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
629
630
  		}
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
631
632
  		///@param mag size of the sampled region.
  		///Sets the magnitude vector to the input mag for both templates.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
633
  		void
5f81932b   David Mayerich   restored Pavel's ...
634
  		setMagnitude(float mag)
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
635
  		{
5f81932b   David Mayerich   restored Pavel's ...
636
637
  			magnitude[0] = mag;
  			magnitude[1] = mag;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
638
  		}
5f81932b   David Mayerich   restored Pavel's ...
639
  		
f304d6de   Pavel Govyadinov   added rotation to...
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
  		///@param dir, the vector to which we are rotating
  		///given a vector to align to, finds the required
  		///axis and angle for glRotatef
  		stim::vec<float, 4>
  		getRotation(stim::vec<float> dir)
  		{
  			stim::vec<float, 4> out;
  			stim::vec<float> from(0,0,1);
  			out[0] = acos(from.dot(dir))*M_PI/180;
  			if(out[0] < 0.0001){
  				out[0] = 0.0;
  				out[1] = 0.0;
  				out[2] = 0.0;
  				out[3] = 1.0;
  			} else {
  				stim::vec<float> temp;
  				temp = (from.cross(dir)).norm();
  				out[1] = temp[0];
  				out[2] = temp[1];
  				out[3] = temp[2];
  			}
  			return out;
  		}
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
664
  		///temporary method for visualization.
a9b45efe   Pavel Govyadinov   changes to spider
665
666
667
668
669
  		GLuint
  		getFB()
  		{
  			return fboID;
  		}
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
670
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
  //--------------------------------------------------------------------------//
  //-----------------------------TEMPORARY METHODS----------------------------//
  //--------------------------------------------------------------------------//
  
  		///temporary Method necessary for visualization and testing.
  		void
  		Update()
  		{
  			vec<float> Y(1.0,0.0,0.0);
  			if(cos(Y.dot(direction))< 0.087){
  				Y[0] = 0.0; Y[1] = 1.0;}
  			hor = stim::rect<float>(magnitude, position, direction.norm(),
  				((Y.cross(direction)).cross(direction)).norm());
  			ver = stim::rect<float>(magnitude, position, direction.norm(),
  				 hor.n());
  		}
  
  
13c2a7d4   Pavel Govyadinov   some changes to t...
689
690
691
  		void
  		Step()
  		{
42145f38   Pavel Govyadinov   Fixed the issues ...
692
  			findOptimalDirection();
13c2a7d4   Pavel Govyadinov   some changes to t...
693
  		}
a9b45efe   Pavel Govyadinov   changes to spider
694
  
42145f38   Pavel Govyadinov   Fixed the issues ...
695
  
a9b45efe   Pavel Govyadinov   changes to spider
696
  
42145f38   Pavel Govyadinov   Fixed the issues ...
697
698
699
700
701
  		/* Method for initializing the cuda devices, necessary only
  			there are multiple cuda devices */
  		void
  		initCuda()
  		{	
2a18be6d   Pavel Govyadinov   New comments and ...
702
  			stim::cudaSetDevice();
42145f38   Pavel Govyadinov   Fixed the issues ...
703
704
705
  			//GLint max;
  			//glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
  			//std::cout << max << std::endl;
a9b45efe   Pavel Govyadinov   changes to spider
706
  		}
4cefeb6d   Pavel Govyadinov   Changes to the re...
707
  };
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
708
709
  }
  #endif