Commit 5f81932bea9ef343eb64fc57009739f83d585684

Authored by David Mayerich
1 parent e843658b

restored Pavel's changes

Showing 2 changed files with 191 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,28 +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();
  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";
284 389 }
285 390  
286 391 //--------------------------------------------------------------------------//
... ... @@ -328,48 +433,36 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
328 433 stim::rect<float> hor;
329 434 stim::rect<float> ver;
330 435  
331   -
  436 + //Default Constructor
332 437 gl_spider
333 438 ()
334 439 {
335 440 setPosition(0.0,0.0,0.0);
336   - setDirection(1.0,1.0,1.0);
337   - setMagnitude(0.1,0.1);
338   - //GenerateFBO(400,200);
339   - //Update();
  441 + setDirection(0.0,0.0,1.0);
  442 + setMagnitude(0.03);
340 443 }
341 444  
342   - gl_spider
343   - (vec<float> pos, vec<float> dir, vec<float> mag)
344   - {
345   - position = pos;
346   - direction = dir;
347   - magnitude = mag;
348   - //GenerateFBO(400,200);
349   - //Update();
350   - }
351   - //temporary cost for convenience.
  445 + //temporary constructor for convenience, will be removed in further updates.
352 446 gl_spider
353 447 (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z,
354   - float mag_x, float mag_y)
  448 + float mag_x)
355 449 {
356 450 setPosition(pos_x, pos_y, pos_z);
357 451 setDirection(dir_x, dir_y, dir_z);
358   - setMagnitude(mag_x, mag_y);
359   - //GenerateFBO(400,200);
360   - //Update();
  452 + setMagnitude(mag_x);
361 453 }
362 454  
  455 + //Attached the spider to the texture with the given GLuint ID.
  456 + //Samples in the default direction acting as the init method.
363 457 void
364   - attachSpider(GLuint id)
  458 + attachSpider(GLuint id, int numSamples = 1089)
365 459 {
366 460 texID = id;
367   - Sample(direction);
368   - //GenerateFBO(20,10000);
369   - // Update();
370   - // generateVectorField(direction, 4.0);
  461 + GenerateFBO(20, numSamples*10);
  462 + sampleDirection();
371 463 }
372   -
  464 +
  465 + //temporary Method necessary for visualization and testing.
373 466 void
374 467 Update()
375 468 {
... ... @@ -380,35 +473,37 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
380 473 ((Y.cross(direction)).cross(direction)).norm());
381 474 ver = stim::rect<float>(magnitude, position, direction.norm(),
382 475 hor.n());
383   - //UpdateBuffer();
384   - generateVectorField(direction, 4.0);
385 476 }
386 477  
387   -
  478 + //Returns the position vector.
388 479 vec<float>
389 480 getPosition()
390 481 {
391 482 return position;
392 483 }
393 484  
  485 + //Returns the direction vector.
394 486 vec<float>
395 487 getDirection()
396 488 {
397 489 return direction;
398 490 }
399 491  
  492 + //Returns the magnitude vector.
400 493 vec<float>
401 494 getMagnitude()
402 495 {
403 496 return magnitude;
404 497 }
405 498  
  499 + //Sets the position vector to input vector pos
406 500 void
407 501 setPosition(vec<float> pos)
408 502 {
409 503 position = pos;
410 504 }
411 505  
  506 + //Sets the position vector to the input float coordinates x,y,z
412 507 void
413 508 setPosition(float x, float y, float z)
414 509 {
... ... @@ -417,12 +512,15 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
417 512 position[2] = z;
418 513 }
419 514  
  515 +
  516 + //Sets the direction vector to input vector dir
420 517 void
421 518 setDirection(vec<float> dir)
422 519 {
423 520 direction = dir;
424 521 }
425 522  
  523 + //Sets the direction vector to the input float coordinates x,y,z
426 524 void
427 525 setDirection(float x, float y, float z)
428 526 {
... ... @@ -431,19 +529,22 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
431 529 direction[2] = z;
432 530 }
433 531  
  532 + //Sets the magnitude vector to the input vector mag.
434 533 void
435 534 setMagnitude(vec<float> mag)
436 535 {
437   - magnitude = mag;
  536 + magnitude[0] = mag[0];
  537 + magnitude[1] = mag[0];
438 538 }
439 539  
440 540 void
441   - setMagnitude(float x, float y)
  541 + setMagnitude(float mag)
442 542 {
443   - magnitude[0] = x;
444   - magnitude[1] = y;
  543 + magnitude[0] = mag;
  544 + magnitude[1] = mag;
445 545 }
446   -
  546 +
  547 + //temporary method for visualization.
447 548 GLuint
448 549 getFB()
449 550 {
... ... @@ -453,142 +554,9 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
453 554 void
454 555 Step()
455 556 {
456   - std::cout << position[0] << "," << position[1] << "," << position[1]
457   - << std::endl;
458   - //setPosition(direction*magnitude[1]/2+position);
459 557 findOptimalDirection();
460   - //Update();
461   - std::cout << position[0] << "," << position[1] << "," << position[1]
462   - << std::endl;
463   -
464   - }
465   -
466   - void
467   - UpdateBuffer()
468   - {
469   - stim::vec<float>p1;
470   - stim::vec<float>p2;
471   - stim::vec<float>p3;
472   - stim::vec<float>p4;
473   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);
474   - glFramebufferTexture2D(
475   - GL_FRAMEBUFFER,
476   - GL_COLOR_ATTACHMENT0,
477   - GL_TEXTURE_2D,
478   - texbufferID,
479   - 0);
480   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);
481   - GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
482   - glDrawBuffers(1, DrawBuffers);
483   - glBindTexture(GL_TEXTURE_2D, texbufferID);
484   - glClearColor(0,0,0,0);
485   - glClear(GL_COLOR_BUFFER_BIT);
486   - glMatrixMode(GL_PROJECTION);
487   - glLoadIdentity();
488   - glMatrixMode(GL_MODELVIEW);
489   - glLoadIdentity();
490   - glViewport(0,0,400,200);
491   - gluOrtho2D(0.0,2.0,0.0,2.0);
492   - glEnable(GL_TEXTURE_3D);
493   - glBindTexture(GL_TEXTURE_3D, texID);
494   - p1 = hor.p(1,1);
495   - p2 = hor.p(1,0);
496   - p3 = hor.p(0,0);
497   - p4 = hor.p(0,1);
498   - glBegin(GL_QUADS);
499   - glTexCoord3f(
500   - p1[0],
501   - p1[1],
502   - p1[2]
503   - );
504   - glVertex2f(0.0,0.0);
505   - glTexCoord3f(
506   - p2[0],
507   - p2[1],
508   - p2[2]
509   - );
510   - glVertex2f(1.0, 0.0);
511   - glTexCoord3f(
512   - p3[0],
513   - p3[1],
514   - p3[2]
515   - );
516   - glVertex2f(1.0, 2.0);
517   - glTexCoord3f(
518   - p4[0],
519   - p4[1],
520   - p4[2]
521   - );
522   - glVertex2f(0.0, 2.0);
523   - glEnd();
524   - p1 = ver.p(1,1);
525   - p2 = ver.p(1,0);
526   - p3 = ver.p(0,0);
527   - p4 = ver.p(0,1);
528   - glBegin(GL_QUADS);
529   - glTexCoord3f(
530   - p1[0],
531   - p1[1],
532   - p1[2]
533   - );
534   - glVertex2f(1.0, 0.0);
535   - glTexCoord3f(
536   - p2[0],
537   - p2[1],
538   - p2[2]
539   - );
540   - glVertex2f(2.0, 0.0);
541   - glTexCoord3f(
542   - p3[0],
543   - p3[1],
544   - p3[2]
545   - );
546   - glVertex2f(2.0, 2.0);
547   - glTexCoord3f(
548   - p4[0],
549   - p4[1],
550   - p4[2]
551   - );
552   - glVertex2f(1.0, 2.0);
553   - glEnd();
554   - glBindTexture(GL_TEXTURE_3D, 0);
555   - glDisable(GL_TEXTURE_3D);
556   - glBindFramebuffer(GL_FRAMEBUFFER,0);
557   - glBindTexture(GL_TEXTURE_2D, 0);
558 558 }
559 559  
560   -
561   - void
562   - generateVectorField(stim::vec<float> d, float dim)
563   - {
564   - vec<float> d_s = d.cart2sph();
565   - vec<float> temp;
566   - float Dim = (float) dim;
567   - float y_0 = 0.0;
568   - float x_0 = 0.0;
569   - float len = 4.0/(2.0*Dim+1.0);
570   - float t0 = M_PI/2;
571   - float p0 = M_PI/3;
572   - float dt = t0/Dim;
573   - float dp = p0/Dim;
574   - for(int i = -dim; i <= dim; i++){
575   - for(int j = -dim; j <= dim; j++){
576   - //field[i+dim][j+dim][0] = d[0];
577   - //field[i+dim][j+dim][1] = d[1]+dt*i;
578   - //field[i+dim][j+dim][2] = d[2]+dp*j;
579   - temp[0] = 1;
580   - temp[1] = d_s[1]+dt*i;
581   - temp[2] = d_s[2]+dp*j;
582   - temp = temp.sph2cart();
583   - Update(x_0+2.0*(i+dim)*len, y_0+(j+dim)*len, temp);
584   - }
585   - }
586   -
587   - }
588   -
589   -
590   -
591   -
592 560  
593 561 void
594 562 findOptimalDirection()
... ... @@ -596,7 +564,9 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
596 564 /* Method for finding the best direction for the spider.
597 565 Uses the camera to rotate. Then Calls Evaluate to find new cost.
598 566 */
599   - Sample(direction);
  567 + sampleDirection();
  568 + samplePosition();
  569 + sampleMagnitude();
600 570 }
601 571  
602 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){
... ...