Blame view

stim/gl/gl_spider.h 18.3 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
  
  		/// 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();
b710b044   Pavel Govyadinov   Added more tempor...
65
66
  			stim::vec<float> next = dirVectors[best];
  		//	next[0] = next[0]*		
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
67
68
69
70
71
72
  			
  		}
  
  		/// 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...
73
74
75
  		void
  		findOptimalPosition()
  		{
887a3e49   Pavel Govyadinov   fixed some order ...
76
  			
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
77
  			genTemplate(magVectors, 1);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
78
  			int best = getCost();
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
79
80
  		}
  	
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
81
82
83
  		/// 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...
84
85
86
  		void
  		findOptimalScale()
  		{
2a18be6d   Pavel Govyadinov   New comments and ...
87
  			genTemplate(magVectors, 2);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
88
  			int best = getCost();
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
89
90
  		}
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
91
92
  
  		
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
93
  		void
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
94
  		Optimize()
385d2447   Pavel Govyadinov   Checkpoint: Conve...
95
  		{
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
96
  			/*find the optimum direction and scale */ 
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
97
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
98
  
a9b45efe   Pavel Govyadinov   changes to spider
99
  		
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
100
101
102
103
104
105
106
107
108
  		
  		
  //--------------------------------------------------------------------------//
  //---------------------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...
109
  		void
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
110
  		genDirectionVectors(float solidAngle = M_PI/2)
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
111
  		{
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  			
  			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...
132
  		}
2a18be6d   Pavel Govyadinov   New comments and ...
133
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
134
135
136
  		///@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...
137
  		void
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
138
  		genPositionVectors(float delta = 0.2)
a9f956be   Pavel Govyadinov   Fixed the cost fu...
139
  		{
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
140
141
142
143
144
145
146
147
148
  			
  			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.
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
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
  			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();
b710b044   Pavel Govyadinov   Added more tempor...
422
423
424
  			glScalef(1.0/512.0/0.6, 1.0/512.0/0.6, 1.0/426.0/2.0);
  		glGetDoublev(GL_TEXTURE_MATRIX, currentTransform);
  		printTransform();
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
425
426
427
  			glTranslatef(position[0],
  				     position[1],
  				     position[2]);
b710b044   Pavel Govyadinov   Added more tempor...
428
429
  		glGetDoublev(GL_TEXTURE_MATRIX, currentTransform);
  		printTransform();
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
430
431
432
  			glScalef(magnitude[0],
  				 magnitude[1],
  				 magnitude[0]);
b710b044   Pavel Govyadinov   Added more tempor...
433
434
  		glGetDoublev(GL_TEXTURE_MATRIX, currentTransform);
  		printTransform();
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
435
  			glRotatef(rot[0], rot[1], rot[2], rot[3]);
b710b044   Pavel Govyadinov   Added more tempor...
436
437
  		glGetDoublev(GL_TEXTURE_MATRIX, currentTransform);
  		printTransform();
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
438
  			glGetDoublev(GL_TEXTURE_MATRIX, currentTransform);
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
439
440
  			CHECK_OPENGL_ERROR
  			glMatrixMode(GL_MODELVIEW);
5f81932b   David Mayerich   restored Pavel's ...
441
  		}
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
442
443
  		
  		
5f81932b   David Mayerich   restored Pavel's ...
444
  
42145f38   Pavel Govyadinov   Fixed the issues ...
445
446
447
448
449
  
  //--------------------------------------------------------------------------//
  //--------------------------------CUDA METHODS------------------------------//
  //--------------------------------------------------------------------------//
  		
2a18be6d   Pavel Govyadinov   New comments and ...
450
451
  		/// Method for registering the texture with Cuda for shared
  		///	access.
42145f38   Pavel Govyadinov   Fixed the issues ...
452
453
454
455
456
457
458
459
460
461
462
463
464
  		void
  		createResource()
  		{
  			HANDLE_ERROR(
  				cudaGraphicsGLRegisterImage(
  					 &resource,
  				 	texbufferID,
  				 	GL_TEXTURE_2D,
  				 	//CU_GRAPHICS_REGISTER_FLAGS_NONE)
  					cudaGraphicsMapFlagsReadOnly)
  			);
  		} 
  		
2a18be6d   Pavel Govyadinov   New comments and ...
465
  		///Method for freeing the texture from Cuda for gl access.
42145f38   Pavel Govyadinov   Fixed the issues ...
466
467
468
469
470
471
472
473
  		void
  		destroyResource()
  		{
  			HANDLE_ERROR(
  				cudaGraphicsUnregisterResource(resource)
  			);		
  		}
  
2a18be6d   Pavel Govyadinov   New comments and ...
474
475
  		///Entry-point into the cuda code for calculating the cost
  		///	of a given samples array (in texture form) 
42145f38   Pavel Govyadinov   Fixed the issues ...
476
477
478
479
  		int
  		getCost()
  		{
  			createResource();
5e7c7581   Pavel Govyadinov   Debugging build f...
480
  			int cost = 	get_cost(resource, iter, numSamples);
42145f38   Pavel Govyadinov   Fixed the issues ...
481
482
483
  			destroyResource();
  			iter++;
  			return cost;
a9f956be   Pavel Govyadinov   Fixed the cost fu...
484
485
  		}
  
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
486
  	public:
13c2a7d4   Pavel Govyadinov   some changes to t...
487
488
489
  		stim::rect<float> hor;
  		stim::rect<float> ver;	
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
490
491
492
493
494
  //--------------------------------------------------------------------------//
  //-----------------------------CONSTRUCTORS---------------------------------//
  //--------------------------------------------------------------------------//
  
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
495
  		///Default Constructor
13c2a7d4   Pavel Govyadinov   some changes to t...
496
497
  		gl_spider
  		()
4cefeb6d   Pavel Govyadinov   Changes to the re...
498
499
  		{
  			setPosition(0.0,0.0,0.0);
5f81932b   David Mayerich   restored Pavel's ...
500
  			setDirection(0.0,0.0,1.0);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
501
  			setMagnitude(1.0);
957a4248   Pavel Govyadinov   renamed getSample...
502
503
  			//numSamples = 1089;
  			numSamples = 9;
2a18be6d   Pavel Govyadinov   New comments and ...
504
  		}
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
505
506
507
  		///@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 ...
508
509
510
511
512
513
514
  		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...
515
516
  		}
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
517
  		///temporary constructor for convenience, will be removed in further updates.	
13c2a7d4   Pavel Govyadinov   some changes to t...
518
519
  		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 ...
520
  			float mag_x)
13c2a7d4   Pavel Govyadinov   some changes to t...
521
  		{
887a3e49   Pavel Govyadinov   fixed some order ...
522
523
524
  			setPosition(pos_x, pos_y, pos_z);
  			setDirection(dir_x, dir_y, dir_z);
  			setMagnitude(mag_x);
2a18be6d   Pavel Govyadinov   New comments and ...
525
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
526
527
  		}
  		///@param GLuint id texture that is going to be sampled.
385d2447   Pavel Govyadinov   Checkpoint: Conve...
528
  		///Attached the spider to the texture with the given GLuint ID.
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
529
530
  		///Samples in the default direction acting as the init method.
  		///Also acts an init.	
a9b45efe   Pavel Govyadinov   changes to spider
531
  		void
2a18be6d   Pavel Govyadinov   New comments and ...
532
  		attachSpider(GLuint id)
a9b45efe   Pavel Govyadinov   changes to spider
533
534
  		{
  			texID = id;
2a18be6d   Pavel Govyadinov   New comments and ...
535
  			iter = 0; ///for debugging purposes
5f81932b   David Mayerich   restored Pavel's ...
536
  			GenerateFBO(20, numSamples*10);
385d2447   Pavel Govyadinov   Checkpoint: Conve...
537
  			genDirectionVectors();
887a3e49   Pavel Govyadinov   fixed some order ...
538
539
540
  			genPositionVectors();
  			genMagnitudeVectors();
  			gl_texture<T>::setDims(0.6, 0.6, 2.0);
2a18be6d   Pavel Govyadinov   New comments and ...
541
  			genTemplate(dirVectors, 0);
a39577bf   Pavel Govyadinov   Changes to the sp...
542
  		}
5f81932b   David Mayerich   restored Pavel's ...
543
  		
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
544
545
546
  //--------------------------------------------------------------------------//
  //-----------------------------ACCESS METHODS-------------------------------//
  //--------------------------------------------------------------------------//
385d2447   Pavel Govyadinov   Checkpoint: Conve...
547
  		///Returns the position vector.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
548
  		vec<float>
a39577bf   Pavel Govyadinov   Changes to the sp...
549
  		getPosition()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
550
551
552
553
  		{
  			return position;
  		}
  	
385d2447   Pavel Govyadinov   Checkpoint: Conve...
554
  		///Returns the direction vector.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
555
  		vec<float>
a39577bf   Pavel Govyadinov   Changes to the sp...
556
  		getDirection()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
557
558
559
560
  		{
  			return direction;
  		}
  
385d2447   Pavel Govyadinov   Checkpoint: Conve...
561
  		///Returns the magnitude vector.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
562
  		vec<float>
a39577bf   Pavel Govyadinov   Changes to the sp...
563
  		getMagnitude()
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
564
565
566
567
  		{
  			return magnitude;
  		}
  	
385d2447   Pavel Govyadinov   Checkpoint: Conve...
568
569
  		///@param vector pos, the new position.
  		///Sets the position vector to input vector pos.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
570
571
572
573
574
  		void
  		setPosition(vec<float> pos)
  		{
  			position = pos;
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
575
576
577
578
579
  
  		///@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...
580
581
582
583
584
585
586
  		void
  		setPosition(float x, float y, float z)
  		{
  			position[0] = x;
  			position[1] = y;
  			position[2] = z;
  		}
887a3e49   Pavel Govyadinov   fixed some order ...
587
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
588
589
  		///@param vector dir, the new direction.
  		///Sets the direction vector to input vector dir.
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
590
591
592
593
594
595
  		void
  		setDirection(vec<float> dir)
  		{
  			direction = dir;
  		}
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
596
597
598
599
  		///@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...
600
601
602
603
604
605
606
  		void
  		setDirection(float x, float y, float z)
  		{
  			direction[0] = x;
  			direction[1] = y;
  			direction[2] = z;
  		}
385d2447   Pavel Govyadinov   Checkpoint: Conve...
607
608
609
  			
  		///@param vector dir, the new direction.
  		///Sets the magnitude vector to the input vector mag.	
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
610
611
612
  		void
  		setMagnitude(vec<float> mag)
  		{
5f81932b   David Mayerich   restored Pavel's ...
613
614
  			magnitude[0] = mag[0];
  			magnitude[1] = mag[0];
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
615
616
  		}
  		
385d2447   Pavel Govyadinov   Checkpoint: Conve...
617
618
  		///@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...
619
  		void
5f81932b   David Mayerich   restored Pavel's ...
620
  		setMagnitude(float mag)
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
621
  		{
5f81932b   David Mayerich   restored Pavel's ...
622
623
  			magnitude[0] = mag;
  			magnitude[1] = mag;
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
624
  		}
5f81932b   David Mayerich   restored Pavel's ...
625
  		
b710b044   Pavel Govyadinov   Added more tempor...
626
627
628
629
630
631
632
633
  		void
  		setSize(int x, int y, int z)
  		{
  			R[1] = x;
  			R[2] = y;
  			R[3] = z;
  		}
  		
f304d6de   Pavel Govyadinov   added rotation to...
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
  		///@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...
658
  		///temporary method for visualization.
a9b45efe   Pavel Govyadinov   changes to spider
659
660
661
662
663
  		GLuint
  		getFB()
  		{
  			return fboID;
  		}
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
664
  
266aa74a   Pavel Govyadinov   CHECKPOINT: Minor...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
  //--------------------------------------------------------------------------//
  //-----------------------------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...
683
684
685
  		void
  		Step()
  		{
42145f38   Pavel Govyadinov   Fixed the issues ...
686
  			findOptimalDirection();
13c2a7d4   Pavel Govyadinov   some changes to t...
687
  		}
a9b45efe   Pavel Govyadinov   changes to spider
688
  
b710b044   Pavel Govyadinov   Added more tempor...
689
690
691
692
693
694
695
696
697
698
699
  		void
  		printTransform()
  		{
  		for(int i = 0; i < 4; i++){
  			std::cout << "[" << currentTransform[i] << "]" <<
  				     "[" << currentTransform[i+4] << "]" <<
  				     "[" << currentTransform[i+8] << "]" <<
  				     "[" << currentTransform[i+12] << "]" <<
  			std::endl;
  				}
  		}
a9b45efe   Pavel Govyadinov   changes to spider
700
  
42145f38   Pavel Govyadinov   Fixed the issues ...
701
702
703
704
705
  		/* Method for initializing the cuda devices, necessary only
  			there are multiple cuda devices */
  		void
  		initCuda()
  		{	
2a18be6d   Pavel Govyadinov   New comments and ...
706
  			stim::cudaSetDevice();
42145f38   Pavel Govyadinov   Fixed the issues ...
707
708
709
  			//GLint max;
  			//glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max);
  			//std::cout << max << std::endl;
a9b45efe   Pavel Govyadinov   changes to spider
710
  		}
4cefeb6d   Pavel Govyadinov   Changes to the re...
711
  };
fb0bc2f1   Pavel Govyadinov   added the gl_spid...
712
713
  }
  #endif