Commit 08ec34fa68a744a3a58d4b78ca92c413dd4d8bf6

Authored by Pavel Govyadinov
1 parent ae035186

Changes to the gl_spider class: Cleaned up the class, removing depreciated const…

…ructors and methods. Added two more functions, one for sampling the position, and another sample the scale (still some tweaking needs to be done). Broke up the GL aspect into three functions to make things simpler renamed methods in order to better communicate what they do. Added more comments. Added another constructor to the rect.h class for future use.
Showing 2 changed files with 190 additions and 213 deletions   Show diff stats
stim/gl/gl_spider.h
... ... @@ -41,7 +41,7 @@ class gl_spider : public virtual gl_texture<T>
41 41 cudaGraphicsResource_t resource;
42 42 GLuint fboID;
43 43 GLuint texbufferID;
44   - int iter = 0;
  44 + int iter = 0; //temporary for testing
45 45  
46 46 void
47 47 findOptimalPosition()
... ... @@ -185,39 +185,45 @@ class gl_spider : public virtual gl_texture<T>
185 185 }
186 186  
187 187 void
188   - Update(float v_x, float v_y, vec<float> dir)
  188 + UpdatePlanes(float v_x, float v_y, vec<float> vctr, int type = 0)
189 189 {
190 190 vec<float> Y(1.0,0.0,0.0);
191   - if(cos(Y.dot(dir))< 0.087){
192   - Y[0] = 0.0; Y[1] = 1.0;}
193   - hor = stim::rect<float>(magnitude, position, dir.norm(),
194   - ((Y.cross(dir)).cross(dir)).norm());
195   - ver = stim::rect<float>(magnitude, position, dir.norm(),
196   - hor.n());
  191 + if(cos(Y.dot(vctr))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;}
  192 + switch(type) {
  193 + case 0:
  194 + hor = stim::rect<float>(magnitude, position, vctr.norm(),
  195 + ((Y.cross(vctr)).cross(vctr)).norm());
  196 + ver = stim::rect<float>(magnitude, position, vctr.norm(),
  197 + hor.n());
  198 + break;
  199 + case 1:
  200 + hor = stim::rect<float>(magnitude, vctr, direction,
  201 + ((Y.cross(direction)).cross(direction)).norm());
  202 + ver = stim::rect<float>(magnitude, vctr, direction,
  203 + hor.n());
  204 + break;
  205 + case 2:
  206 + hor = stim::rect<float>(vctr, position, direction,
  207 + ((Y.cross(direction)).cross(direction)).norm());
  208 + ver = stim::rect<float>(vctr, position, direction,
  209 + hor.n());
  210 + break;
  211 + default:
  212 + std::cout << "unknown case have been passed"
  213 + << std::endl;
  214 + break;
  215 + }
197 216 UpdateBuffer(v_x, v_y);
198 217 }
199   -
200   -
  218 +
  219 + /*
  220 + Method for controling the buffer and texture binding in order to properly
  221 + do the render to texture.
  222 + */
201 223 void
202   - Sample(vec<float> in = (0,0,1), int numSamples = 1089, int solidAngle = M_PI/2)
  224 + Bind(int numSamples = 1089)
203 225 {
204   - GenerateFBO(20, numSamples*10);
205   -
206   - ofstream file;
207   - file.open("samples.txt", std::ios_base::app);
208   - float samples[numSamples][3]; //Set up the variables
209   - //necessary for sample generation
210   - vec<float> d_s = in.cart2sph();
211   - vec<float> temp;
212   - int dim = (sqrt(numSamples)-1)/2;
213   - //std::cout << dim << std::endl;
214   - float y_0 = 0.0;
215   - float len = 10.0;
216   - float p0 = M_PI/3;
217   - float dt = solidAngle/(1.0 * dim);
218   - float dp = p0/(1.0*dim);
219   -
220   -
  226 + float len = 10.0;
221 227 glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer
222 228 glFramebufferTexture2D(
223 229 GL_FRAMEBUFFER,
... ... @@ -239,11 +245,45 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
239 245 gluOrtho2D(0.0,2.0*len,0.0,numSamples*len);
240 246 glEnable(GL_TEXTURE_3D);
241 247 glBindTexture(GL_TEXTURE_3D, texID);
  248 + }
  249 +
  250 + void
  251 + Unbind()
  252 + {
  253 + //Finalize GL_buffer
  254 + glBindTexture(GL_TEXTURE_3D, 0);
  255 + glDisable(GL_TEXTURE_3D);
  256 + glBindFramebuffer(GL_FRAMEBUFFER,0);
  257 + glBindTexture(GL_TEXTURE_2D, 0);
  258 + }
  259 + /*
  260 + Method for populating the buffer with the sampled texture.
  261 + usually uses the default direction vector and then
  262 + */
  263 + void
  264 + sampleDirection(int numSamples = 1089, float solidAngle = M_PI)
  265 + {
242 266  
  267 + //ofstream file;
  268 + //file.open("samples.txt", std::ios_base::app);
  269 + float samples[numSamples][3]; //Set up the variables
  270 + //necessary for sample generation
  271 + vec<float> d_s = direction.cart2sph();
  272 + vec<float> temp;
  273 + int dim = (sqrt(numSamples)-1)/2;
  274 + //std::cout << dim << std::endl;
  275 + float y_0 = 0.0;
  276 + float p0 = M_PI/3;
  277 + float dt = solidAngle/(1.0 * dim);
  278 + float dp = p0/(1.0*dim);
  279 +
  280 + Bind();
  281 + //file << "Step: Direction" << "\n";
243 282 //file << "iteration: " << iter << "\n";
244 283 //file << "starting pos and dir:" << "[" << position[0] << "," <<position[1] << "," << position[2] << "]" << ":" << "[" << direction[0] << "," << direction[1] << "," << direction[2] << "]\n";
245   - //Loop over the samples
246   - file << position[0] << "," <<position[1] << "," << position[2] << "\n";
  284 + //Loop over the samples such that the original orientation is in the
  285 + //center of the resulting texture index (544)
  286 + //file << position[0] << "," <<position[1] << "," << position[2] << "\n";
247 287 int idx;
248 288 for(int i = -dim; i <= dim; i++){
249 289 for(int j = -dim; j <= dim; j++){
... ... @@ -259,29 +299,93 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
259 299 samples[idx][1] = temp[1];
260 300 samples[idx][2] = temp[2];
261 301  
262   - //file << idx << ":" <<"[" << samples[idx][0] << "," << samples[idx][1] << "," << samples[idx][2] << "]" << "\n";
  302 + // file << idx << ":" <<"[" << samples[idx][0] << "," << samples[idx][1] << "," << samples[idx][2] << "]" << "\n";
263 303  
264   - Update(0.0, y_0+(idx)*10, temp);
  304 + UpdatePlanes(0.0, y_0+(idx)*10, temp);
265 305 }
266 306 }
267   - //Finalize GL_buffer
268   - glBindTexture(GL_TEXTURE_3D, 0);
269   - glDisable(GL_TEXTURE_3D);
270   - glBindFramebuffer(GL_FRAMEBUFFER,0);
271   - glBindTexture(GL_TEXTURE_2D, 0);
  307 + Unbind();
272 308 int nxt = getCost();
273   - stim::vec<float> next;
274   - next[0] = samples[nxt][0];
275   - next[1] = samples[nxt][1];
276   - next[2] = samples[nxt][2];
  309 + stim::vec<float> next(samples[nxt][0], samples[nxt][1], samples[nxt][2]);
  310 + //next[0] = samples[nxt][0];
  311 + //next[1] = samples[nxt][1];
  312 + //next[2] = samples[nxt][2];
277 313 next.norm();
278 314 //file << "next direction" << "[" << next[0] << "," << next[1] << "," << next[2] << "]\n\n";
279   - setPosition(position[0] + next[0]/500,
280   - position[1]+next[1]/500,
281   - position[2]+next[2]/500);
  315 + setPosition(position[0] + next[0]*magnitude[0]/2,
  316 + position[1]+next[1]*magnitude[0]/2,
  317 + position[2]+next[2]*magnitude[0]/2);
282 318 setDirection(next[0], next[1], next[2]);
283 319 //file.close();
284 320 setDirection(next);
  321 + //file << "ending pos and dir:" << "[" << position[0] << "," <<position[1] << "," << position[2] << "]" << ":" << "[" << direction[0] << "," << direction[1] << "," << direction[2] << "]\n";
  322 + }
  323 +
  324 + void
  325 + samplePosition(int numSamples = 1089, float delta = 0.2)
  326 + {
  327 +
  328 + float samples[numSamples][3]; //Set up the variables
  329 + vec<float> temp;
  330 + int dim = (sqrt(numSamples)-1)/2;
  331 + float y_0 = 0.0; //location of the first sample in the buffer
  332 + stim::rect<float> samplingPlane =
  333 + stim::rect<float>(magnitude[0]*delta, position, direction);
  334 + float step = 1.0/(dim);
  335 + Bind();
  336 + //Loop over the samples, keeping the original position sample
  337 + //in the center of the resulting texture.
  338 +
  339 + int idx;
  340 + for(int i = -dim; i <= dim; i++){
  341 + for(int j = -dim; j <= dim; j++){
  342 + //Create linear index
  343 + idx = (i+dim)*(dim*2+1) + (j+dim);
  344 +
  345 + temp = samplingPlane.p(
  346 + 0.5+step*i,
  347 + 0.5+step*j
  348 + );
  349 +
  350 + samples[idx][0] = temp[0]; //save sample vector
  351 + samples[idx][1] = temp[1];
  352 + samples[idx][2] = temp[2];
  353 +
  354 + UpdatePlanes(0.0, y_0+(idx)*10, temp, 1);
  355 + }
  356 + }
  357 + Unbind();
  358 + int nxt = getCost();
  359 + setPosition(samples[nxt][0], samples[nxt][1], samples[nxt][2]);
  360 + }
  361 +
  362 + void
  363 + sampleMagnitude(int numSamples = 1089, float delta = 0.5)
  364 + {
  365 +
  366 + ofstream file;
  367 + file.open("samples.txt", std::ios_base::app);
  368 + float samples[numSamples]; //Set up the variables
  369 + int dim = (sqrt(numSamples)-1)/2;
  370 + float y_0 = 0.0; //location of the first sample in the buffer
  371 + float min = 1.0-delta;
  372 + float max = 1.0+delta;
  373 + float step = (max-min)/(numSamples-1);
  374 + float factor;
  375 + vec<float> temp;
  376 + Bind();
  377 +
  378 + for(int i = 0; i < numSamples; i++){
  379 + //Create linear index
  380 + factor = (min+step*i)*magnitude[0];
  381 + samples[i] = factor; //save sample vector
  382 + stim::vec<float> temp(factor);
  383 + UpdatePlanes(0.0, y_0+(i)*10, temp, 2);
  384 + }
  385 + Unbind();
  386 + int nxt = getCost();
  387 + setMagnitude(samples[nxt]);
  388 + file << position[0] << "," <<position[1] << "," << position[2] << "\n";
285 389 }
286 390  
287 391 //--------------------------------------------------------------------------//
... ... @@ -329,48 +433,36 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
329 433 stim::rect<float> hor;
330 434 stim::rect<float> ver;
331 435  
332   -
  436 + //Default Constructor
333 437 gl_spider
334 438 ()
335 439 {
336 440 setPosition(0.0,0.0,0.0);
337   - setDirection(1.0,1.0,1.0);
338   - setMagnitude(0.1,0.1);
339   - //GenerateFBO(400,200);
340   - //Update();
  441 + setDirection(0.0,0.0,1.0);
  442 + setMagnitude(0.03);
341 443 }
342 444  
343   - gl_spider
344   - (vec<float> pos, vec<float> dir, vec<float> mag)
345   - {
346   - position = pos;
347   - direction = dir;
348   - magnitude = mag;
349   - //GenerateFBO(400,200);
350   - //Update();
351   - }
352   - //temporary cost for convenience.
  445 + //temporary constructor for convenience, will be removed in further updates.
353 446 gl_spider
354 447 (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z,
355   - float mag_x, float mag_y)
  448 + float mag_x)
356 449 {
357 450 setPosition(pos_x, pos_y, pos_z);
358 451 setDirection(dir_x, dir_y, dir_z);
359   - setMagnitude(mag_x, mag_y);
360   - //GenerateFBO(400,200);
361   - //Update();
  452 + setMagnitude(mag_x);
362 453 }
363 454  
  455 + //Attached the spider to the texture with the given GLuint ID.
  456 + //Samples in the default direction acting as the init method.
364 457 void
365   - attachSpider(GLuint id)
  458 + attachSpider(GLuint id, int numSamples = 1089)
366 459 {
367 460 texID = id;
368   - Sample(direction);
369   - //GenerateFBO(20,10000);
370   - // Update();
371   - // generateVectorField(direction, 4.0);
  461 + GenerateFBO(20, numSamples*10);
  462 + sampleDirection();
372 463 }
373   -
  464 +
  465 + //temporary Method necessary for visualization and testing.
374 466 void
375 467 Update()
376 468 {
... ... @@ -381,35 +473,37 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
381 473 ((Y.cross(direction)).cross(direction)).norm());
382 474 ver = stim::rect<float>(magnitude, position, direction.norm(),
383 475 hor.n());
384   - //UpdateBuffer();
385   - //generateVectorField(direction, 4.0);
386 476 }
387 477  
388   -
  478 + //Returns the position vector.
389 479 vec<float>
390 480 getPosition()
391 481 {
392 482 return position;
393 483 }
394 484  
  485 + //Returns the direction vector.
395 486 vec<float>
396 487 getDirection()
397 488 {
398 489 return direction;
399 490 }
400 491  
  492 + //Returns the magnitude vector.
401 493 vec<float>
402 494 getMagnitude()
403 495 {
404 496 return magnitude;
405 497 }
406 498  
  499 + //Sets the position vector to input vector pos
407 500 void
408 501 setPosition(vec<float> pos)
409 502 {
410 503 position = pos;
411 504 }
412 505  
  506 + //Sets the position vector to the input float coordinates x,y,z
413 507 void
414 508 setPosition(float x, float y, float z)
415 509 {
... ... @@ -418,12 +512,15 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
418 512 position[2] = z;
419 513 }
420 514  
  515 +
  516 + //Sets the direction vector to input vector dir
421 517 void
422 518 setDirection(vec<float> dir)
423 519 {
424 520 direction = dir;
425 521 }
426 522  
  523 + //Sets the direction vector to the input float coordinates x,y,z
427 524 void
428 525 setDirection(float x, float y, float z)
429 526 {
... ... @@ -432,19 +529,22 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
432 529 direction[2] = z;
433 530 }
434 531  
  532 + //Sets the magnitude vector to the input vector mag.
435 533 void
436 534 setMagnitude(vec<float> mag)
437 535 {
438   - magnitude = mag;
  536 + magnitude[0] = mag[0];
  537 + magnitude[1] = mag[0];
439 538 }
440 539  
441 540 void
442   - setMagnitude(float x, float y)
  541 + setMagnitude(float mag)
443 542 {
444   - magnitude[0] = x;
445   - magnitude[1] = y;
  543 + magnitude[0] = mag;
  544 + magnitude[1] = mag;
446 545 }
447   -
  546 +
  547 + //temporary method for visualization.
448 548 GLuint
449 549 getFB()
450 550 {
... ... @@ -454,142 +554,9 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
454 554 void
455 555 Step()
456 556 {
457   - //std::cout << position[0] << "," << position[1] << "," << position[1]
458   - // << std::endl;
459   - //setPosition(direction*magnitude[1]/2+position);
460 557 findOptimalDirection();
461   - //Update();
462   - //std::cout << position[0] << "," << position[1] << "," << position[1]
463   - // << std::endl;
464   -
465 558 }
466 559  
467   -/* void
468   - UpdateBuffer()
469   - {
470   - stim::vec<float>p1;
471   - stim::vec<float>p2;
472   - stim::vec<float>p3;
473   - stim::vec<float>p4;
474   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);
475   - glFramebufferTexture2D(
476   - GL_FRAMEBUFFER,
477   - GL_COLOR_ATTACHMENT0,
478   - GL_TEXTURE_2D,
479   - texbufferID,
480   - 0);
481   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);
482   - GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
483   - glDrawBuffers(1, DrawBuffers);
484   - glBindTexture(GL_TEXTURE_2D, texbufferID);
485   - glClearColor(0,0,0,0);
486   - glClear(GL_COLOR_BUFFER_BIT);
487   - glMatrixMode(GL_PROJECTION);
488   - glLoadIdentity();
489   - glMatrixMode(GL_MODELVIEW);
490   - glLoadIdentity();
491   - glViewport(0,0,400,200);
492   - gluOrtho2D(0.0,2.0,0.0,2.0);
493   - glEnable(GL_TEXTURE_3D);
494   - glBindTexture(GL_TEXTURE_3D, texID);
495   - p1 = hor.p(1,1);
496   - p2 = hor.p(1,0);
497   - p3 = hor.p(0,0);
498   - p4 = hor.p(0,1);
499   - glBegin(GL_QUADS);
500   - glTexCoord3f(
501   - p1[0],
502   - p1[1],
503   - p1[2]
504   - );
505   - glVertex2f(0.0,0.0);
506   - glTexCoord3f(
507   - p2[0],
508   - p2[1],
509   - p2[2]
510   - );
511   - glVertex2f(1.0, 0.0);
512   - glTexCoord3f(
513   - p3[0],
514   - p3[1],
515   - p3[2]
516   - );
517   - glVertex2f(1.0, 2.0);
518   - glTexCoord3f(
519   - p4[0],
520   - p4[1],
521   - p4[2]
522   - );
523   - glVertex2f(0.0, 2.0);
524   - glEnd();
525   - p1 = ver.p(1,1);
526   - p2 = ver.p(1,0);
527   - p3 = ver.p(0,0);
528   - p4 = ver.p(0,1);
529   - glBegin(GL_QUADS);
530   - glTexCoord3f(
531   - p1[0],
532   - p1[1],
533   - p1[2]
534   - );
535   - glVertex2f(1.0, 0.0);
536   - glTexCoord3f(
537   - p2[0],
538   - p2[1],
539   - p2[2]
540   - );
541   - glVertex2f(2.0, 0.0);
542   - glTexCoord3f(
543   - p3[0],
544   - p3[1],
545   - p3[2]
546   - );
547   - glVertex2f(2.0, 2.0);
548   - glTexCoord3f(
549   - p4[0],
550   - p4[1],
551   - p4[2]
552   - );
553   - glVertex2f(1.0, 2.0);
554   - glEnd();
555   - glBindTexture(GL_TEXTURE_3D, 0);
556   - glDisable(GL_TEXTURE_3D);
557   - glBindFramebuffer(GL_FRAMEBUFFER,0);
558   - glBindTexture(GL_TEXTURE_2D, 0);
559   - } */
560   -
561   -
562   - /*void
563   - generateVectorField(stim::vec<float> d, float dim)
564   - {
565   - vec<float> d_s = d.cart2sph();
566   - vec<float> temp;
567   - float Dim = (float) dim;
568   - float y_0 = 0.0;
569   - float x_0 = 0.0;
570   - float len = 4.0/(2.0*Dim+1.0);
571   - float t0 = M_PI/2;
572   - float p0 = M_PI/3;
573   - float dt = t0/Dim;
574   - float dp = p0/Dim;
575   - for(int i = -dim; i <= dim; i++){
576   - for(int j = -dim; j <= dim; j++){
577   - //field[i+dim][j+dim][0] = d[0];
578   - //field[i+dim][j+dim][1] = d[1]+dt*i;
579   - //field[i+dim][j+dim][2] = d[2]+dp*j;
580   - temp[0] = 1;
581   - temp[1] = d_s[1]+dt*i;
582   - temp[2] = d_s[2]+dp*j;
583   - temp = temp.sph2cart();
584   - Update(x_0+2.0*(i+dim)*len, y_0+(j+dim)*len, temp);
585   - }
586   - }
587   -
588   - }*/
589   -
590   -
591   -
592   -
593 560  
594 561 void
595 562 findOptimalDirection()
... ... @@ -597,7 +564,9 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
597 564 /* Method for finding the best direction for the spider.
598 565 Uses the camera to rotate. Then Calls Evaluate to find new cost.
599 566 */
600   - Sample(direction);
  567 + sampleDirection();
  568 + samplePosition();
  569 + sampleMagnitude();
601 570 }
602 571  
603 572 /* Method for initializing the cuda devices, necessary only
... ...
stim/math/rect.h
... ... @@ -92,12 +92,20 @@ public:
92 92 Y = directionY;
93 93 }
94 94  
95   - CUDA_CALLABLE rect(vec<T,N> mag, vec<T, N> center, vec<T, N> directionX, vec<T, N> directionY )
  95 + CUDA_CALLABLE rect(T size, vec<T, N> center, vec<T, N> directionX, vec<T, N> directionY )
96 96 {
97 97 C = center;
98 98 X = directionX;
99 99 Y = directionY;
100   - scale(mag[0], mag[1]);
  100 + scale(size);
  101 + }
  102 +
  103 + CUDA_CALLABLE rect(vec<T,N> size, vec<T, N> center, vec<T, N> directionX, vec<T, N> directionY )
  104 + {
  105 + C = center;
  106 + X = directionX;
  107 + Y = directionY;
  108 + scale(size[0], size[1]);
101 109 }
102 110  
103 111 CUDA_CALLABLE void scale(T factor1, T factor2){
... ...