Commit f304d6de925e9e2fab6e742c391f92b1ad0bd447

Authored by Pavel Govyadinov
1 parent 957a4248

added rotation to positionTemplate and a function that finds the axis of rotatio…

…n and the angle between two arbitrary vectors. Added a function for recovering the transformation matrix in order to progress the dir, pos and magnitudes of the spider.TO DO: move hardcoded dimensions of the search space (# of slices) into variables inherited from gl_texture.
Showing 1 changed file with 46 additions and 112 deletions   Show diff stats
stim/gl/gl_spider.h
... ... @@ -37,11 +37,10 @@ class gl_spider : public virtual gl_texture<T>
37 37 std::vector<stim::vec<float> > dirVectors;
38 38 std::vector<stim::vec<float> > posVectors;
39 39 std::vector<stim::vec<float> > magVectors;
  40 + double currentTransform[16];
40 41 using gl_texture<T>::texID;
41 42 using gl_texture<T>::S;
42 43 cudaArray* c_Array;
43   - //void** devPtr;
44   - //size_t size;
45 44 cudaGraphicsResource_t resource;
46 45 GLuint fboID;
47 46 GLuint texbufferID;
... ... @@ -76,7 +75,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
76 75 function. */
77 76 //getSample(position, direction, magnitude);
78 77 genTemplate(dirVectors, 0);
79   - int bets = getCost();
  78 + int best = getCost();
80 79 //getSample(position, direction, magnitude);
81 80 //int best = getCost();
82 81 }
... ... @@ -118,127 +117,70 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
118 117 void
119 118 UpdateBuffer(float v_x, float v_y)
120 119 {
121   - //std::cout << v_y << std::endl;
122 120 float len = 10.0;
123 121 stim::vec<float>p1;
124 122 stim::vec<float>p2;
125 123 stim::vec<float>p3;
126 124 stim::vec<float>p4;
127   - //std::cout << "hor" << std::endl;
128 125 p1 = hor.p(1,1);
129   - //std::cout << p1 << std::endl;
130 126 p2 = hor.p(1,0);
131   - //std::cout << p2 << std::endl;
132 127 p3 = hor.p(0,0);
133   - //std::cout << p3 << std::endl;
134 128 p4 = hor.p(0,1);
135   - //std::cout << p4 << std::endl;
136 129 glBegin(GL_QUADS);
137 130 glTexCoord3f(
138   - // p1[0]/S[1],
139   - // p1[1]/S[2],
140   - // p1[2]/S[3]
141   - // );
142   - // ((p1[0]*magnitude[1])+position[0])/512/0.6,
143   - // ((p1[1]*magnitude[1])+position[1])/512/0.6,
144   - // ((p1[2]*magnitude[1])+position[2])/426/2.0
145   -
146 131 p1[0],
147 132 p1[1],
148 133 p1[2]
149 134 );
150   - //glVertex2f(0.0,0.0);
151 135 glVertex2f(v_x,v_y);
152 136 glTexCoord3f(
153   - // p2[0]/S[1],
154   - // p2[1]/S[2],
155   - // p2[2]/S[3]
156   - // ((p2[0]*magnitude[1])+position[0])/512/0.6,
157   - // ((p2[1]*magnitude[1])+position[1])/512/0.6,
158   - // ((p2[2]*magnitude[1])+position[2])/426/2.0
159 137 p2[0],
160 138 p2[1],
161 139 p2[2]
162 140 );
163   - //glVertex2f(1.0, 0.0);
164 141 glVertex2f(v_x+len, v_y);
165 142 glTexCoord3f(
166   - // p2[0]/S[1],
167   - // p2[1]/S[2],
168   - // p2[2]/S[3]
169   - // ((p3[0]*magnitude[1])+position[0])/512/0.6,
170   - // ((p3[1]*magnitude[1])+position[1])/512/0.6,
171   - // ((p3[2]*magnitude[1])+position[2])/426/2.0
172 143 p3[0],
173 144 p3[1],
174 145 p3[2]
175 146 );
176   - //glVertex2f(1.0, 2.0);
177 147 glVertex2f(v_x+len, v_y+len);
178 148 glTexCoord3f(
179   - // p4[0]/S[1],
180   - // p4[1]/S[2],
181   - // p4[2]/S[3]
182 149 p4[0],
183 150 p4[1],
184 151 p4[2]
185   - // ((p4[0]*magnitude[1])+position[0])/512/0.6,
186   - // ((p4[1]*magnitude[1])+position[1])/512/0.6,
187   - // ((p4[2]*magnitude[1])+position[2])/426/2.0
188 152 );
189   - //glVertex2f(0.0, 2.0);
190 153 glVertex2f(v_x, v_y+len);
191 154 glEnd();
192 155  
193   - //std::cout << "ver" << std::endl;
194 156 p1 = ver.p(1,1);
195   - //std::cout << p1 << std::endl;
196 157 p2 = ver.p(1,0);
197   - //std::cout << p2 << std::endl;
198 158 p3 = ver.p(0,0);
199   - //std::cout << p3 << std::endl;
200 159 p4 = ver.p(0,1);
201   - //std::cout << p4 << std::endl;
202 160 glBegin(GL_QUADS);
203 161 glTexCoord3f(
204   - // p1[0]/S[1],
205   - // p1[1]/S[2],
206   - // p1[2]/S[3]
207 162 p1[0],
208 163 p1[1],
209 164 p1[2]
210 165 );
211   - //glVertex2f(1.0, 0.0);
212 166 glVertex2f(v_x+len, v_y);
213 167 glTexCoord3f(
214   - // p2[0]/S[1],
215   - // p2[1]/S[2],
216   - // p2[2]/S[3]
217 168 p2[0],
218 169 p2[1],
219 170 p2[2]
220 171 );
221   - //glVertex2f(2.0, 0.0);
222 172 glVertex2f(v_x+2*len, v_y);
223 173 glTexCoord3f(
224   - // p3[0]/S[1],
225   - // p3[1]/S[2],
226   - // p3[2]/S[3]
227 174 p3[0],
228 175 p3[1],
229 176 p3[2]
230 177 );
231   - //glVertex2f(2.0, 2.0);
232 178 glVertex2f(v_x+2*len, v_y+len);
233 179 glTexCoord3f(
234   - // p4[0]/S[1],
235   - // p4[1]/S[2],
236   - // p4[2]/S[3]
237 180 p4[0],
238 181 p4[1],
239 182 p4[2]
240 183 );
241   - //glVertex2f(1.0, 2.0);
242 184 glVertex2f(v_x+len, v_y+len);
243 185 glEnd();
244 186 CHECK_OPENGL_ERROR
... ... @@ -341,7 +283,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
341 283 glBindTexture(GL_TEXTURE_2D, texbufferID);
342 284 glClearColor(1,1,1,1);
343 285 glClear(GL_COLOR_BUFFER_BIT);
344   - //glMatrixMode(GL_TEXTURE);
345 286 glMatrixMode(GL_PROJECTION);
346 287 glLoadIdentity();
347 288 glMatrixMode(GL_MODELVIEW);
... ... @@ -350,12 +291,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
350 291 gluOrtho2D(0.0,2.0*len,0.0,numSamples*len);
351 292 glEnable(GL_TEXTURE_3D);
352 293 glBindTexture(GL_TEXTURE_3D, texID);
353   -
354   -
355   - //glScalef(1/512/S[1], 1/512/S[2], 1/426/S[3]);
356   - //glLoadIdentity();
357   - //glViewport(0,0,2.0*len, numSamples*len);
358   - //gluOrtho2D(0.0,2.0*len,0.0,numSamples*len);
359 294  
360 295 CHECK_OPENGL_ERROR
361 296 }
... ... @@ -395,7 +330,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
395 330 temp[2] = d_s[2]+dp*j;
396 331  
397 332 temp = (temp.sph2cart()).norm(); //back to cart
398   - //std::cout << temp << std::endl;
399 333 dirVectors.push_back(temp);
400 334 }
401 335 }
... ... @@ -537,15 +471,11 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
537 471 texID = id;
538 472 iter = 0; ///for debugging purposes
539 473 GenerateFBO(20, numSamples*10);
540   - //sampleDirection();
541 474 genDirectionVectors();
542 475 genPositionVectors();
543 476 genMagnitudeVectors();
544 477 gl_texture<T>::setDims(0.6, 0.6, 2.0);
545   - //getSample(position, direction, magnitude);
546 478 genTemplate(dirVectors, 0);
547   - std::cout << S[1] << ":" << S[2] << ":" << S[3] << std::endl;
548   - //gl_texture<T>::setDims(1.0, 1.0,1.0);
549 479 }
550 480  
551 481 ///temporary Method necessary for visualization and testing.
... ... @@ -604,49 +534,29 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
604 534  
605 535 void positionTemplate(vec<float> pos, vec<float> dir, vec<float> mag)
606 536 {
607   - //Bind();
608   -
609   - //stim::vec<float>p1;
610   - //stim::vec<float>p2;
611   - //stim::vec<float>p3;
612   - //stim::vec<float>p4;
613   - //p1 = hor.p(1,1);
614   - //p2 = hor.p(1,0);
615   - //p3 = hor.p(0,0);
616   - //p4 = hor.p(0,1);
  537 + stim::vec<float, 4> rot = getRotation(dir);
617 538 glMatrixMode(GL_TEXTURE);
618 539 glLoadIdentity();
619   - //glTranslatef(pos[0], pos[1], pos[2]);
620   - //glScalef(mag[0], mag[1], mag[0]);
621   -
622   - //glScalef(1/512/S[1], 1/512/S[2], 1/426/S[3]);
623   - //glMatrixMode(GL_TEXTURE);
624   - //glLoadIdentity();
625   - glTranslatef(position[0]/512/S[1], position[1]/512/S[2], position[2]/426/S[3]);
626   - glScalef(magnitude[0]/512/S[1], magnitude[1]/512/S[2], magnitude[0]/426/S[3]);
627   - std::cout << "DEBUG: gl_spider::605" << std::endl;
628   - std::cout << "pos: " << position << std::endl;
629   - std::cout << "dir: " << direction << std::endl;
630   - std::cout << "mag: " << magnitude << std::endl;
631   - /*std::cout << "Before Transformation" << std::endl;
632   - std::cout << p1 << ":" << p2 << ":" << p3 << ":" << p4 << std::endl;
633   - std::cout << "After Transformation" << std::endl;
634   - std::cout << "[" << (p1[0]+pos[0])/512/S[1] << ", "
635   - <<(p1[1]+pos[1])/512/S[2] << ", "
636   - <<(p1[2]+pos[2])/426/S[3] << "]" << ":"
637   - << "[" << (p2[0]+pos[0])/512/S[1] << ", "
638   - << (p2[1]+pos[1])/512/S[2] << ", "
639   - << (p2[2]+pos[2])/426/S[3] << "]" << ":"
640   - << "[" << (p3[0]+pos[0])/512/S[1] << ", "
641   - << (p3[1]+pos[1])/512/S[2] << ", "
642   - << (p3[2]+pos[2])/426/S[3] << "]" << ":"
643   - << "[" << (p4[0]+pos[0])/512/S[1] << ", "
644   - << (p4[1]+pos[1])/512/S[2] << ", "
645   - << (p4[2]+pos[2])/426/S[3] << "]" << std::endl;*/
646   - //GLdouble mat[16];
647   - //glGetDoublev(GL_TEXTURE, mat);
  540 + glTranslatef(position[0]/512/S[1],
  541 + position[1]/512/S[2],
  542 + position[2]/426/S[3]);
  543 + glScalef(magnitude[0]/512/S[1],
  544 + magnitude[1]/512/S[2],
  545 + magnitude[0]/426/S[3]);
  546 + glRotatef(rot[0], rot[1], rot[2], rot[3]);
  547 + glGetDoublev(GL_TEXTURE_MATRIX, currentTransform);
  548 + int c = 0;
  549 + for(int i = 0; i < 16; i++){
  550 + if(c<3){
  551 + std::cerr << "[" << currentTransform[i] << "]" << " ";
  552 + c++;
  553 + } else {
  554 + std::cerr << "[" << currentTransform[i] << "]" << "\n";
  555 + c = 0;
  556 + }
  557 + }
  558 + CHECK_OPENGL_ERROR
648 559 glMatrixMode(GL_MODELVIEW);
649   - //Unbind();
650 560 }
651 561  
652 562 void positionTemplate()
... ... @@ -691,6 +601,30 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
691 601 magnitude[1] = mag;
692 602 }
693 603  
  604 + ///@param dir, the vector to which we are rotating
  605 + ///given a vector to align to, finds the required
  606 + ///axis and angle for glRotatef
  607 + stim::vec<float, 4>
  608 + getRotation(stim::vec<float> dir)
  609 + {
  610 + stim::vec<float, 4> out;
  611 + stim::vec<float> from(0,0,1);
  612 + out[0] = acos(from.dot(dir))*M_PI/180;
  613 + if(out[0] < 0.0001){
  614 + out[0] = 0.0;
  615 + out[1] = 0.0;
  616 + out[2] = 0.0;
  617 + out[3] = 1.0;
  618 + } else {
  619 + stim::vec<float> temp;
  620 + temp = (from.cross(dir)).norm();
  621 + out[1] = temp[0];
  622 + out[2] = temp[1];
  623 + out[3] = temp[2];
  624 + }
  625 + return out;
  626 + }
  627 +
694 628 ///temporary method for visualization.
695 629 GLuint
696 630 getFB()
... ...