Commit 8e56a0a7ac63534d432d41c9b41c14f65c788d0b

Authored by Pavel Govyadinov
1 parent 57fbfbf9

Added the proposed changes to simplify and clean up the gl_spider class. Removed…

… genTemplate method and redistributed it's functionality into genDirectionVectors, genPositionVectors, genMagnitudeVectors. Renamed variable to make the names shorter and easier to use. Removed a number of calls to Bind() and Unbind(). Removed a constructor and added a destructor. TO DO: replace the fillMatrix method with a function call.
Showing 3 changed files with 195 additions and 625 deletions   Show diff stats
gl/gl_spider.h deleted
1   -#ifndef STIM_GL_SPIDER_H
2   -#define STIM_GL_SPIDER_H
3   -
4   -#include <GL/glew.h>
5   -#include <GL/glut.h>
6   -#include <cuda.h>
7   -#include <cuda_gl_interop.h>
8   -#include <cudaGL.h>
9   -#include "gl_texture.h"
10   -#include "../visualization/camera.h"
11   -#include "./error.h"
12   -#include "../math/vector.h"
13   -#include "../math/rect.h"
14   -#include "../cuda/cost.h"
15   -#include "../cuda/glbind.h"
16   -
17   -namespace stim
18   -{
19   -
20   -template<typename T>
21   -class gl_spider : public virtual gl_texture<T>
22   -{
23   - //doen't use gl_texture really, just needs the GLuint id.
24   - //doesn't even need the texture iD really.
25   - private:
26   - stim::camera rotator;
27   - stim::vec<float> position; //vector designating the position of the spider.
28   - stim::vec<float> direction; //vector designating the orientation of the spider
29   - //always a unit vector.
30   - stim::vec<float> magnitude; //magnitude of the direction vector.
31   - //mag[0] = length.
32   - //mag[1] = width.
33   - using gl_texture<T>::texID;
34   - //using image_stack<T>::S;
35   - cudaArray* c_Array;
36   - //void** devPtr;
37   - //size_t size;
38   - cudaGraphicsResource_t resource;
39   - GLuint fboID;
40   - GLuint texbufferID;
41   -
42   - void
43   - findOptimalDirection()
44   - {
45   - /* Method for finding the best direction for the spider.
46   - Uses the camera to rotate. Then Calls Evaluate to find new cost.
47   - */
48   - }
49   -
50   - void
51   - findOptimalPosition()
52   - {
53   - /* Method for finding the best direction for the spider.
54   - Not sure if necessary since the next position for the spider
55   - will be at direction * magnitude. */
56   - }
57   -
58   - void
59   - findOptimalScale()
60   - {
61   - /* Method for finding the best scale for the spider.
62   - changes the x, y, z size of the spider to minimize the cost
63   - function. */
64   - }
65   -
66   - void
67   - Evaluate()
68   - {
69   - /* Uses uniform sampler2D in order to take a difference between
70   - the colors of two textures. 1st texture is the spider template,
71   - the 2nd is the location of the spider's overlap with the
72   - gl_template
73   -
74   - does the spider need to track it's location? Prob not since
75   - position can be set with gl_texture coordinates */
76   -
77   - }
78   -
79   - void
80   - Optimize()
81   - {
82   - /*find the optimum direction and scale */
83   - }
84   - /*
85   - void
86   - Step()
87   - {
88   - // move to the new position
89   - }
90   - */
91   - public:
92   -
93   - stim::rect<float> hor;
94   - stim::rect<float> ver;
95   -
96   -
97   -
98   - gl_spider
99   - ()
100   - {
101   - setPosition(0.0,0.0,0.0);
102   - setDirection(1.0,1.0,1.0);
103   - setMagnitude(0.1,0.1);
104   - //GenerateFBO(400,200);
105   - //Update();
106   - }
107   -
108   - gl_spider
109   - (vec<float> pos, vec<float> dir, vec<float> mag)
110   - {
111   - position = pos;
112   - direction = dir;
113   - magnitude = mag;
114   - //GenerateFBO(400,200);
115   - //Update();
116   - }
117   - //temporary cost for convenience.
118   - gl_spider
119   - (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z,
120   - float mag_x, float mag_y)
121   - {
122   - setPosition(pos_x, pos_y, pos_z);
123   - setDirection(dir_x, dir_y, dir_z);
124   - setMagnitude(mag_x, mag_y);
125   - //GenerateFBO(400,200);
126   - //Update();
127   - }
128   -
129   - void
130   - attachSpider(GLuint id)
131   - {
132   - texID = id;
133   - GenerateFBO(400,200);
134   - Update();
135   - }
136   -
137   - void
138   - Update()
139   - {
140   - vec<float> Y(1.0,0.0,0.0);
141   - if(cos(Y.dot(direction))< 0.087){
142   - Y[0] = 0.0; Y[1] = 1.0;}
143   - hor = stim::rect<float>(magnitude, position, direction.norm(),
144   - ((Y.cross(direction)).cross(direction)).norm());
145   - ver = stim::rect<float>(magnitude, position, direction.norm(),
146   - hor.n());
147   - UpdateBuffer();
148   - }
149   -
150   - vec<float>
151   - getPosition()
152   - {
153   - return position;
154   - }
155   -
156   - vec<float>
157   - getDirection()
158   - {
159   - return direction;
160   - }
161   -
162   - vec<float>
163   - getMagnitude()
164   - {
165   - return magnitude;
166   - }
167   -
168   - void
169   - setPosition(vec<float> pos)
170   - {
171   - position = pos;
172   - }
173   -
174   - void
175   - setPosition(float x, float y, float z)
176   - {
177   - position[0] = x;
178   - position[1] = y;
179   - position[2] = z;
180   - }
181   -
182   - void
183   - setDirection(vec<float> dir)
184   - {
185   - direction = dir;
186   - }
187   -
188   - void
189   - setDirection(float x, float y, float z)
190   - {
191   - direction[0] = x;
192   - direction[1] = y;
193   - direction[2] = z;
194   - }
195   -
196   - void
197   - setMagnitude(vec<float> mag)
198   - {
199   - magnitude = mag;
200   - }
201   -
202   - void
203   - setMagnitude(float x, float y)
204   - {
205   - magnitude[0] = x;
206   - magnitude[1] = y;
207   - }
208   -
209   - GLuint
210   - getFB()
211   - {
212   - return fboID;
213   - }
214   -
215   - void
216   - Step()
217   - {
218   - std::cout << position[0] << "," << position[1] << "," << position[1]
219   - << std::endl;
220   - setPosition(direction*magnitude[1]/2+position);
221   - Update();
222   - std::cout << position[0] << "," << position[1] << "," << position[1]
223   - << std::endl;
224   -
225   - }
226   -
227   - void
228   - UpdateBuffer()
229   - {
230   - stim::vec<float>p1;
231   - stim::vec<float>p2;
232   - stim::vec<float>p3;
233   - stim::vec<float>p4;
234   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);
235   - glFramebufferTexture2D(
236   - GL_FRAMEBUFFER,
237   - GL_COLOR_ATTACHMENT0,
238   - GL_TEXTURE_2D,
239   - texbufferID,
240   - 0);
241   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);
242   - GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0};
243   - glDrawBuffers(1, DrawBuffers);
244   - glBindTexture(GL_TEXTURE_2D, texbufferID);
245   - glClearColor(0,0,0,0);
246   - glClear(GL_COLOR_BUFFER_BIT);
247   - glMatrixMode(GL_PROJECTION);
248   - glLoadIdentity();
249   - glMatrixMode(GL_MODELVIEW);
250   - glLoadIdentity();
251   - glViewport(0,0,400,200);
252   - gluOrtho2D(0.0,2.0,0.0,2.0);
253   - glEnable(GL_TEXTURE_3D);
254   - glBindTexture(GL_TEXTURE_3D, texID);
255   - p1 = hor.p(1,1);
256   - p2 = hor.p(1,0);
257   - p3 = hor.p(0,0);
258   - p4 = hor.p(0,1);
259   - glBegin(GL_QUADS);
260   - glTexCoord3f(
261   - p1[0],
262   - p1[1],
263   - p1[2]
264   - );
265   - glVertex2f(0.0,0.0);
266   - glTexCoord3f(
267   - p2[0],
268   - p2[1],
269   - p2[2]
270   - );
271   - glVertex2f(1.0, 0.0);
272   - glTexCoord3f(
273   - p3[0],
274   - p3[1],
275   - p3[2]
276   - );
277   - glVertex2f(1.0, 2.0);
278   - glTexCoord3f(
279   - p4[0],
280   - p4[1],
281   - p4[2]
282   - );
283   - glVertex2f(0.0, 2.0);
284   - glEnd();
285   - p1 = ver.p(1,1);
286   - p2 = ver.p(1,0);
287   - p3 = ver.p(0,0);
288   - p4 = ver.p(0,1);
289   - glBegin(GL_QUADS);
290   - glTexCoord3f(
291   - p1[0],
292   - p1[1],
293   - p1[2]
294   - );
295   - glVertex2f(1.0, 0.0);
296   - glTexCoord3f(
297   - p2[0],
298   - p2[1],
299   - p2[2]
300   - );
301   - glVertex2f(2.0, 0.0);
302   - glTexCoord3f(
303   - p3[0],
304   - p3[1],
305   - p3[2]
306   - );
307   - glVertex2f(2.0, 2.0);
308   - glTexCoord3f(
309   - p4[0],
310   - p4[1],
311   - p4[2]
312   - );
313   - glVertex2f(1.0, 2.0);
314   - glEnd();
315   - glBindTexture(GL_TEXTURE_3D, 0);
316   - glDisable(GL_TEXTURE_3D);
317   - glBindFramebuffer(GL_FRAMEBUFFER,0);
318   - glBindTexture(GL_TEXTURE_2D, 0);
319   - }
320   -
321   -
322   - void
323   - GenerateFBO(unsigned int width, unsigned int height)
324   - {
325   - glGenFramebuffers(1, &fboID);
326   - glBindFramebuffer(GL_FRAMEBUFFER, fboID);
327   - int numChannels = 1;
328   - unsigned char* texels = new unsigned char[width * height * numChannels];
329   - glGenTextures(1, &texbufferID);
330   - glBindTexture(GL_TEXTURE_2D, texbufferID);
331   - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
332   - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
333   - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
334   - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
335   - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE,
336   - width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texels);
337   - delete[] texels;
338   - glBindFramebuffer(GL_FRAMEBUFFER, 0);
339   - }
340   -
341   -
342   - void
343   - initCuda()
344   - {
345   - /* cudaDeviceProp prop;
346   - int device;
347   - memset( &prop, 0, sizeof(cudaDeviceProp) );
348   - prop.major = 1;
349   - prop.minor = 0;
350   - HANDLE_ERROR( cudaChooseDevice (&device, &prop ) );
351   - HANDLE_ERROR( cudaGetDeviceProperties(&prop, device));
352   - printf(" device number: %d\n", device);
353   - printf(" device name: %s\n", prop.name);
354   - printf(" device maxTexture3D: %d %d %d\n", prop.maxTexture3D[0] ,prop.maxTexture3D[0], prop.maxTexture3D[2]) ;
355   - HANDLE_ERROR( cudaGLSetGLDevice (device)); */
356   - stim::cudaSetDevice();
357   -/* HANDLE_ERROR(
358   - cudaGraphicsMapResources(1, &resource, 0)
359   - );
360   - HANDLE_ERROR(
361   - cudaGraphicsResourceGetMappedPointer(
362   - &devPtr,
363   - size,
364   - resource));
365   - HANDLE_ERROR(
366   - cudaGraphicsSubResourceGetMappedArray(
367   - &c_Array,
368   - resource,
369   - 0,0)
370   - );
371   - HANDLE_ERROR(
372   - cudaBindTextureToArray(fboID, c_Array)
373   - );
374   - HANDLE_ERROR(
375   - cudaGraphicsUnmapResources(1, &resource, 0)
376   - );
377   - //need to move the constants to video memory.
378   -*/
379   - }
380   -
381   - void
382   - createResource()
383   - {
384   - HANDLE_ERROR(
385   - cudaGraphicsGLRegisterImage(
386   - &resource,
387   - fboID,
388   - GL_TEXTURE_2D,
389   - CU_GRAPHICS_REGISTER_FLAGS_NONE)
390   - );
391   - }
392   -
393   - void
394   - destroyResource()
395   - {
396   - HANDLE_ERROR(
397   - cudaGraphicsUnregisterResource(resource)
398   - );
399   - }
400   -
401   - float
402   - getCost()
403   - {
404   - createResource();
405   - float cost = get_cost(resource);
406   - destroyResource();
407   - return cost;
408   - }
409   -};
410   -}
411   -#endif
stim/gl/gl_spider.h
... ... @@ -29,32 +29,33 @@ namespace stim
29 29 {
30 30  
31 31 template<typename T>
32   -class gl_spider : public virtual gl_texture<T>
  32 +class gl_spider
33 33 {
34 34 //doen't use gl_texture really, just needs the GLuint id.
35 35 //doesn't even need the texture iD really.
36 36 private:
37   - stim::vec<float> position; //vector designating the position of the spider.
38   - stim::vec<float> direction; //vector designating the orientation of the spider
  37 + stim::vec<float> p; //vector designating the position of the spider.
  38 + stim::vec<float> d; //vector designating the orientation of the spider
39 39 //always a unit vector.
40   - stim::vec<float> magnitude; //magnitude of the direction vector.
  40 + stim::vec<float> m; //magnitude of the spider vector.
41 41 //mag[0] = length.
42 42 //mag[1] = width.
43   - std::vector<stim::vec<float> > dirVectors;
44   - std::vector<stim::vec<float> > posVectors;
45   - std::vector<stim::vec<float> > magVectors;
46   - stim::matrix<float, 4> currentTransform;
47   - using gl_texture<T>::texID;
48   - using gl_texture<T>::S;
49   - using gl_texture<T>::R;
  43 + std::vector<stim::vec<float> > dV;
  44 + std::vector<stim::vec<float> > pV;
  45 + std::vector<stim::vec<float> > mV;
  46 + //currentTransform
  47 + stim::matrix<float, 4> cT;
  48 + GLuint texID;
  49 + stim::vec<float> S;
  50 + stim::vec<float> R;
50 51 cudaGraphicsResource_t resource;
51 52  
52 53 GLuint dList;
53   - GLubyte list[3];
54 54 GLuint fboID;
55 55 GLuint texbufferID;
56 56 int iter; //temporary for testing
57 57 int numSamples;
  58 + float stepsize = 3.0;
58 59  
59 60 /// Method for finding the best scale for the spider.
60 61 /// changes the x, y, z size of the spider to minimize the cost
... ... @@ -62,45 +63,41 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
62 63 void
63 64 findOptimalDirection()
64 65 {
65   - //genTemplate(dirVectors, 0);
66   - Bind();
67   - positionTemplate();
  66 + //genTemplate(dV, 0);
  67 + setMatrix();
68 68 glCallList(dList);
69 69 int best = getCost();
70 70 stim::vec<float, 4> next;
71   - next[0] = dirVectors[best][0]*S[1]*R[1];
72   - next[1] = dirVectors[best][1]*S[2]*R[2];
73   - next[2] = dirVectors[best][2]*S[3]*R[3];
  71 + next[0] = dV[best][0]*S[0]*R[0];
  72 + next[1] = dV[best][1]*S[1]*R[1];
  73 + next[2] = dV[best][2]*S[2]*R[2];
74 74 next[3] = 1;
75   - next = (currentTransform*next).norm();
76   - setPosition( position[0]+next[0]*magnitude[0]/3,
77   - position[1]+next[1]*magnitude[0]/3,
78   - position[2]+next[2]*magnitude[0]/3);
  75 + next = (cT*next).norm();
  76 + setPosition( p[0]+next[0]*m[0]/stepsize,
  77 + p[1]+next[1]*m[0]/stepsize,
  78 + p[2]+next[2]*m[0]/stepsize);
79 79 setDirection(next[0], next[1], next[2]);
80   - Unbind();
81 80 }
82 81  
83   - /// Method for finding the best direction for the spider.
84   - /// Not sure if necessary since the next position for the spider
85   - /// will be at direction * magnitude.
  82 + /// Method for finding the best d for the spider.
  83 + /// Not sure if necessary since the next p for the spider
  84 + /// will be at d * m.
86 85 void
87 86 findOptimalPosition()
88 87 {
89   - Bind();
90   - positionTemplate();
  88 + setMatrix();
91 89 glCallList(dList+1);
92 90 int best = getCost();
93 91 stim::vec<float, 4> next;
94   - next[0] = posVectors[best][0];
95   - next[1] = posVectors[best][1];
96   - next[2] = posVectors[best][2];
  92 + next[0] = pV[best][0];
  93 + next[1] = pV[best][1];
  94 + next[2] = pV[best][2];
97 95 next[3] = 1;
98   - next = currentTransform*next;
99   - std::cout << "Optimal Position:"<< next << std::endl;
100   - setPosition( next[0]*S[1]*R[1],
101   - next[1]*S[2]*R[2],
102   - next[2]*S[3]*R[3]);
103   - Unbind();
  96 + next = cT*next;
  97 + std::cout << "Optimal p:"<< next << std::endl;
  98 + setPosition( next[0]*S[0]*R[0],
  99 + next[1]*S[1]*R[1],
  100 + next[2]*S[2]*R[2]);
104 101 }
105 102  
106 103 /// Method for finding the best scale for the spider.
... ... @@ -109,19 +106,17 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
109 106 void
110 107 findOptimalScale()
111 108 {
112   - Bind();
113   - positionTemplate();
  109 + setMatrix();
114 110 glCallList(dList+2);
115 111 int best = getCost();
116 112 stim::vec<float, 4> next;
117   - next[0] = magVectors[best][0]*S[1]*R[1];
118   - next[1] = magVectors[best][1]*S[2]*R[2];
119   - next[2] = magVectors[best][2]*S[3]*R[3];
  113 + next[0] = mV[best][0]*S[0]*R[0];
  114 + next[1] = mV[best][1]*S[1]*R[1];
  115 + next[2] = mV[best][2]*S[2]*R[2];
120 116 next[3] = 1;
121   - next = currentTransform*next;
  117 + next = cT*next;
122 118 std::cout << "Optimal Scale:"<< next << std::endl;
123 119 setMagnitude(next[0]);
124   - Unbind();
125 120 }
126 121  
127 122  
... ... @@ -129,7 +124,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
129 124 void
130 125 Optimize()
131 126 {
132   - /*find the optimum direction and scale */
  127 + /*find the optimum d and scale */
133 128 }
134 129  
135 130  
... ... @@ -141,29 +136,51 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
141 136  
142 137 ///@param solidAngle, the size of the arc to sample.
143 138 ///Method for populating the vector arrays with sampled vectors.
144   - ///uses the default direction vector <0,0,1>
  139 + ///uses the default d vector <0,0,1>
145 140 void
146 141 genDirectionVectors(float solidAngle = M_PI)
147 142 {
148   - vec<float> d_s = direction.cart2sph().norm();
  143 + //Set up the vectors necessary for Rectangle creation.
  144 + vec<float> Y(1.0,0.0,0.0);
  145 + vec<float> pos(0.0,0.0,0.0);
  146 + vec<float> mag(1.0, 1.0, 1.0);
  147 + vec<float> dir(0.0, 0.0, 1.0);
  148 +
  149 + //Set up the variable necessary for vector creation.
  150 + vec<float> d_s = d.cart2sph().norm();
149 151 vec<float> temp;
150 152 int dim = (sqrt(numSamples)-1)/2;
151 153 float p0 = -M_PI;
152 154 float dt = solidAngle/(2.0 * ((float)dim + 1.0));
153 155 float dp = p0/(2.0*((float)dim + 1.0));
154   -
  156 +
  157 + glNewList(dList, GL_COMPILE);
  158 + //Loop over the space
  159 + int idx = 0;
155 160 for(int i = -dim; i <= dim; i++){
156 161 for(int j = -dim; j <= dim; j++){
157 162 //Create linear index
158   -
  163 + idx = (j+dim)+(i+dim)*((dim*2)+1);
159 164 temp[0] = d_s[0]; //rotate vector
160 165 temp[1] = d_s[1]+dp*(float) i;
161 166 temp[2] = d_s[2]+dt*(float) j;
162 167  
163 168 temp = (temp.sph2cart()).norm(); //back to cart
164   - dirVectors.push_back(temp);
  169 + dV.push_back(temp);
  170 + if(cos(Y.dot(temp))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;}
  171 + else{Y[0] = 1.0; Y[1] = 0.0;}
  172 +
  173 + hor = stim::rect<float>(mag,
  174 + pos, temp,
  175 + ((Y.cross(temp)).cross(temp)).norm());
  176 + ver = stim::rect<float>(mag,
  177 + pos, temp,
  178 + hor.n());
  179 + UpdateBuffer(0.0, 0.0+idx*10.0);
  180 + CHECK_OPENGL_ERROR
165 181 }
166 182 }
  183 + glEndList();
167 184 }
168 185  
169 186 ///@param solidAngle, the size of the arc to sample.
... ... @@ -172,38 +189,61 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
172 189 void
173 190 genPositionVectors(float delta = 0.2)
174 191 {
175   - ofstream file;
176   - //file.open("dvectors.txt");
  192 + //Set up the vectors necessary for Rectangle creation.
  193 + vec<float> Y(1.0,0.0,0.0);
  194 + vec<float> pos(0.0,0.0,0.0);
  195 + vec<float> mag(1.0, 1.0, 1.0);
  196 + vec<float> dir(0.0, 0.0, 1.0);
  197 +
  198 + //Set up the variable necessary for vector creation.
177 199 vec<float> temp;
178 200 int dim = (sqrt(numSamples)-1)/2;
179 201 stim::rect<float> samplingPlane =
180   - stim::rect<float>(magnitude[0]*delta, position, direction);
  202 + stim::rect<float>(m[0]*delta, p, d);
181 203 float step = 1.0/(dim);
182   - //Loop over the samples, keeping the original position sample
183   - //in the center of the resulting texture.
184 204  
  205 + //Loop over the samples, keeping the original p sample
  206 + //in the center of the resulting texture.
  207 + int idx;
  208 + glNewList(dList+1, GL_COMPILE);
185 209 for(int i = -dim; i <= dim; i++){
186 210 for(int j = -dim; j <= dim; j++){
187 211 //Create linear index
  212 + idx = (j+dim)+(i+dim)*((dim*2)+1);
188 213  
189 214 temp = samplingPlane.p(
190 215 0.5+step*i,
191 216 0.5+step*j
192 217 );
193   - //file << temp[0] << ";" << temp[1]
194   - // << ";" << temp[2] << endl;
195   - posVectors.push_back(temp);
  218 + pV.push_back(temp);
  219 + hor = stim::rect<float>(mag,
  220 + temp, dir,
  221 + ((Y.cross(d)).cross(d))
  222 + .norm());
  223 + ver = stim::rect<float>(mag,
  224 + temp, dir,
  225 + hor.n());
  226 + UpdateBuffer(0.0, 0.0+idx*10.0);
  227 + CHECK_OPENGL_ERROR
196 228 }
197 229 }
  230 + glEndList();
198 231 }
199 232  
200 233 ///@param solidAngle, the size of the arc to sample.
201 234 ///Method for populating the buffer with the sampled texture.
202   - ///uses the default magnitude <1,1,0>
  235 + ///uses the default m <1,1,0>
203 236 void
204 237 genMagnitudeVectors(float delta = 0.5)
205 238 {
206 239  
  240 + //Set up the vectors necessary for Rectangle creation.
  241 + vec<float> Y(1.0,0.0,0.0);
  242 + vec<float> pos(0.0,0.0,0.0);
  243 + vec<float> mag(1.0, 1.0, 1.0);
  244 + vec<float> dir(0.0, 0.0, 1.0);
  245 +
  246 + //Set up the variable necessary for vector creation.
207 247 int dim = (sqrt(numSamples)-1)/2;
208 248 float min = 1.0-delta;
209 249 float max = 1.0+delta;
... ... @@ -211,95 +251,23 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
211 251 float factor;
212 252 vec<float> temp;
213 253  
  254 + glNewList(dList+2, GL_COMPILE);
214 255 for(int i = 0; i < numSamples; i++){
215 256 //Create linear index
216   - factor = (min+step*i)*magnitude[0];
  257 + factor = (min+step*i)*m[0];
217 258 temp = factor;
218   - magVectors.push_back(temp);
  259 + mV.push_back(temp);
  260 + hor = stim::rect<float>(temp,
  261 + pos, dir,
  262 + ((Y.cross(d)).cross(d))
  263 + .norm());
  264 + ver = stim::rect<float>(temp,
  265 + pos, dir,
  266 + hor.n());
  267 + UpdateBuffer(0.0, 0.0+i*10.0);
  268 + CHECK_OPENGL_ERROR
219 269 }
220   - }
221   - ///@param vector of stim::vec in that stores all of the samplable vectors.
222   - ///@param type, one of three operations, 0 for Direction vectors
223   - /// 1 for Position, 2 for Magnitude.
224   - ///Function for filling the buffer up with the data based on the vectors
225   - ///Each vector represents a two rectangular templates.
226   - ///Loops through all of the vectors and transfers rect. associated with it
227   - ///Into buffer-space.
228   - void
229   - genTemplate(std::vector<stim::vec<float> > in, int type)
230   - {
231   - float x = 0.0;
232   - vec<float> Y(1.0,0.0,0.0);
233   - vec<float> pos(0.0,0.0,0.0);
234   - vec<float> mag(1.0, 1.0, 1.0);
235   - vec<float> dir(0.0, 0.0, 1.0);
236   - switch(type) {
237   - case 0: //Direction
238   - Bind();
239   - glNewList(dList+type, GL_COMPILE);
240   - for(int i = 0; i < in.size(); i++)
241   - {
242   - if(cos(Y.dot(in[i]))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;}
243   - else{Y[0] = 1.0; Y[1] = 0.0;}
244   -
245   - hor = stim::rect<float>(mag,
246   - pos, in[i],
247   - ((Y.cross(in[i])).cross(in[i])).norm());
248   - ver = stim::rect<float>(mag,
249   - pos, in[i],
250   - hor.n());
251   - UpdateBuffer(x, x+i*10.0);
252   - }
253   - glEndList();
254   - Unbind();
255   - break;
256   - case 1: //Position
257   - Bind();
258   - glNewList(dList+type, GL_COMPILE);
259   - if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;}
260   - else{Y[0] = 1.0; Y[1] = 0.0;}
261   -
262   - for(int i = 0; i < in.size(); i++)
263   - {
264   - hor = stim::rect<float>(mag,
265   - in[i], dir,
266   - ((Y.cross(direction)).cross(direction))
267   - .norm());
268   - ver = stim::rect<float>(mag,
269   - in[i], dir,
270   - hor.n());
271   - UpdateBuffer(x, x+i*10.0);
272   - }
273   - glEndList();
274   - Unbind();
275   - break;
276   - case 2: //Scale
277   - Bind();
278   - glNewList(dList+type, GL_COMPILE);
279   - if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;}
280   - else{Y[0] = 1.0; Y[1] = 0.0;}
281   -
282   - for(int i = 0; i < in.size(); i++)
283   - {
284   - hor = stim::rect<float>(in[i],
285   - pos, dir,
286   - ((Y.cross(direction)).cross(direction))
287   - .norm());
288   - ver = stim::rect<float>(in[i],
289   - pos, dir,
290   - hor.n());
291   - UpdateBuffer(x, x+i*10.0);
292   - }
293   - glEndList();
294   - Unbind();
295   - break;
296   - default:
297   - std::cout << "unknown case have been passed"
298   - << std::endl;
299   - break;
300   - }
301   - Unbind();
302   - CHECK_OPENGL_ERROR
  270 + glEndList();
303 271 }
304 272 ///@param v_x x-coordinate in buffer-space,
305 273 ///@param v_y y-coordinate in buffer-space.
... ... @@ -374,7 +342,6 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
374 342 );
375 343 glVertex2f(v_x+len, v_y+len);
376 344 glEnd();
377   - CHECK_OPENGL_ERROR
378 345 }
379 346  
380 347  
... ... @@ -450,21 +417,21 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
450 417  
451 418 ///Method for using the gl manipulation to alighn templates from
452 419 ///Template space (-0.5 0.5) to Texture space (0.0, 1.0),
453   - ///Based on the position of the spider in real space (arbitrary).
454   - void positionTemplate()
  420 + ///Based on the p of the spider in real space (arbitrary).
  421 + void setMatrix()
455 422 {
456   - stim::vec<float, 4> rot = getRotation(direction);
  423 + stim::vec<float, 4> rot = getRotation(d);
457 424 glMatrixMode(GL_TEXTURE);
458 425 glLoadIdentity();
459 426  
460 427 glRotatef(rot[0], rot[1], rot[2], rot[3]);
461   - glScalef(1.0/S[1]/R[1], 1.0/S[2]/R[2], 1.0/S[3]/R[3]);
462   - glTranslatef(position[0],
463   - position[1],
464   - position[2]);
465   - glScalef(magnitude[0],
466   - magnitude[1],
467   - magnitude[0]);
  428 + glScalef(1.0/S[0]/R[0], 1.0/S[1]/R[1], 1.0/S[2]/R[2]);
  429 + glTranslatef(p[0],
  430 + p[1],
  431 + p[2]);
  432 + glScalef(m[0],
  433 + m[1],
  434 + m[0]);
468 435  
469 436 float curTrans[16];
470 437 glGetFloatv(GL_TEXTURE_MATRIX, curTrans);
... ... @@ -527,21 +494,11 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
527 494 //--------------------------------------------------------------------------//
528 495  
529 496  
530   - ///Default Constructor
531   - gl_spider
532   - ()
533   - {
534   - setPosition(0.0,0.0,0.0);
535   - setDirection(0.0,0.0,1.0);
536   - setMagnitude(1.0);
537   - numSamples = 1089;
538   - //numSamples = 9;
539   - }
540 497 ///@param samples, the number of samples this spider is going to use.
541 498 ///best results if samples is can create a perfect root.
542 499 ///Default Constructor
543 500 gl_spider
544   - (int samples)
  501 + (int samples = 1089)
545 502 {
546 503 setPosition(0.0,0.0,0.0);
547 504 setDirection(0.0,0.0,1.0);
... ... @@ -559,9 +516,18 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
559 516 setMagnitude(mag_x);
560 517  
561 518 }
  519 +
  520 + ~gl_spider
  521 + (void)
  522 + {
  523 + Unbind();
  524 + glDeleteTextures(1, &texbufferID);
  525 + glDeleteBuffers(1, &fboID);
  526 + }
  527 +
562 528 ///@param GLuint id texture that is going to be sampled.
563 529 ///Attached the spider to the texture with the given GLuint ID.
564   - ///Samples in the default direction acting as the init method.
  530 + ///Samples in the default d acting as the init method.
565 531 ///Also acts an init.
566 532 void
567 533 attachSpider(GLuint id)
... ... @@ -569,109 +535,115 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
569 535 texID = id;
570 536 iter = 0; ///for debugging purposes
571 537 GenerateFBO(20, numSamples*10);
572   - genDirectionVectors();
573   - genPositionVectors();
574   - genMagnitudeVectors();
575   -
576   - gl_texture<T>::setDims(0.6, 0.6, 1.0);
577   - setSize(512, 512, 426);
  538 + setDims(0.6, 0.6, 1.0);
  539 + setSize(512.0, 512.0, 426.0);
578 540  
579 541 dList = glGenLists(3);
580   - list[0] = 0; list[1] = 1; list[2] = 2;
581 542 glListBase(dList);
582   - genTemplate(dirVectors, 0);
583   - genTemplate(posVectors, 1);
584   - genTemplate(magVectors, 2);
  543 + Bind();
  544 + genDirectionVectors(5*M_PI/4);
  545 + genPositionVectors();
  546 + genMagnitudeVectors();
  547 + Unbind();
585 548 }
586 549  
587 550 //--------------------------------------------------------------------------//
588 551 //-----------------------------ACCESS METHODS-------------------------------//
589 552 //--------------------------------------------------------------------------//
590   - ///Returns the position vector.
  553 + ///Returns the p vector.
591 554 vec<float>
592 555 getPosition()
593 556 {
594   - return position;
  557 + return p;
595 558 }
596 559  
597   - ///Returns the direction vector.
  560 + ///Returns the d vector.
598 561 vec<float>
599 562 getDirection()
600 563 {
601   - return direction;
  564 + return d;
602 565 }
603 566  
604   - ///Returns the magnitude vector.
  567 + ///Returns the m vector.
605 568 vec<float>
606 569 getMagnitude()
607 570 {
608   - return magnitude;
  571 + return m;
609 572 }
610 573  
611   - ///@param vector pos, the new position.
612   - ///Sets the position vector to input vector pos.
  574 + ///@param vector pos, the new p.
  575 + ///Sets the p vector to input vector pos.
613 576 void
614 577 setPosition(vec<float> pos)
615 578 {
616   - position = pos;
  579 + p = pos;
617 580 }
618 581  
619 582 ///@param x x-coordinate.
620 583 ///@param y y-coordinate.
621 584 ///@param z z-coordinate.
622   - ///Sets the position vector to the input float coordinates x,y,z.
  585 + ///Sets the p vector to the input float coordinates x,y,z.
623 586 void
624 587 setPosition(float x, float y, float z)
625 588 {
626   - position[0] = x;
627   - position[1] = y;
628   - position[2] = z;
  589 + p[0] = x;
  590 + p[1] = y;
  591 + p[2] = z;
629 592 }
630 593  
631   - ///@param vector dir, the new direction.
632   - ///Sets the direction vector to input vector dir.
  594 + ///@param vector dir, the new d.
  595 + ///Sets the d vector to input vector dir.
633 596 void
634 597 setDirection(vec<float> dir)
635 598 {
636   - direction = dir;
  599 + d = dir;
637 600 }
638 601  
639 602 ///@param x x-coordinate.
640 603 ///@param y y-coordinate.
641 604 ///@param z z-coordinate.
642   - ///Sets the direction vector to the input float coordinates x,y,z.
  605 + ///Sets the d vector to the input float coordinates x,y,z.
643 606 void
644 607 setDirection(float x, float y, float z)
645 608 {
646   - direction[0] = x;
647   - direction[1] = y;
648   - direction[2] = z;
  609 + d[0] = x;
  610 + d[1] = y;
  611 + d[2] = z;
649 612 }
650 613  
651   - ///@param vector dir, the new direction.
652   - ///Sets the magnitude vector to the input vector mag.
  614 + ///@param vector dir, the new d.
  615 + ///Sets the m vector to the input vector mag.
653 616 void
654 617 setMagnitude(vec<float> mag)
655 618 {
656   - magnitude[0] = mag[0];
657   - magnitude[1] = mag[0];
  619 + m[0] = mag[0];
  620 + m[1] = mag[0];
658 621 }
659 622  
660 623 ///@param mag size of the sampled region.
661   - ///Sets the magnitude vector to the input mag for both templates.
  624 + ///Sets the m vector to the input mag for both templates.
662 625 void
663 626 setMagnitude(float mag)
664 627 {
665   - magnitude[0] = mag;
666   - magnitude[1] = mag;
  628 + m[0] = mag;
  629 + m[1] = mag;
667 630 }
668 631  
  632 +
  633 + void
  634 + setDims(float x, float y, float z)
  635 + {
  636 + S[0] = x;
  637 + S[1] = y;
  638 + S[2] = z;
  639 + }
  640 +
669 641 void
670   - setSize(int x, int y, int z)
  642 + setSize(float x, float y, float z)
671 643 {
672   - R[1] = x;
673   - R[2] = y;
674   - R[3] = z;
  644 + R[0] = x;
  645 + R[1] = y;
  646 + R[2] = z;
675 647 }
676 648  
677 649 ///@param dir, the vector to which we are rotating
... ... @@ -704,9 +676,10 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
704 676 {
705 677 for(int r = 0; r < 4; r++){
706 678 for(int c = 0; c < 4; c++){
707   - currentTransform(r,c) = mat[c*4+r];
  679 + cT(r,c) = mat[c*4+r];
708 680 }
709 681 }
  682 + // cT.setMatrix(mat);
710 683  
711 684 }
712 685  
... ... @@ -727,11 +700,11 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
727 700 Update()
728 701 {
729 702 vec<float> Y(1.0,0.0,0.0);
730   - if(cos(Y.dot(direction))< 0.087){
  703 + if(cos(Y.dot(d))< 0.087){
731 704 Y[0] = 0.0; Y[1] = 1.0;}
732   - hor = stim::rect<float>(magnitude, position, direction.norm(),
733   - ((Y.cross(direction)).cross(direction)).norm());
734   - ver = stim::rect<float>(magnitude, position, direction.norm(),
  705 + hor = stim::rect<float>(m, p, d.norm(),
  706 + ((Y.cross(d)).cross(d)).norm());
  707 + ver = stim::rect<float>(m, p, d.norm(),
735 708 hor.n());
736 709 }
737 710  
... ... @@ -739,16 +712,18 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
739 712 void
740 713 Step()
741 714 {
  715 + Bind();
742 716 findOptimalDirection();
743 717 findOptimalPosition();
744 718 findOptimalScale();
  719 + Unbind();
745 720 }
746 721  
747 722  
748 723 void
749 724 printTransform()
750 725 {
751   - std::cout << currentTransform << std::endl;
  726 + std::cout << cT << std::endl;
752 727 }
753 728  
754 729 /* Method for initializing the cuda devices, necessary only
... ...
stim/math/matrix.h
... ... @@ -25,6 +25,11 @@ struct matrix
25 25 (*this)(r, c) = 0;
26 26 }
27 27  
  28 + CUDA_CALLABLE matrix<T,N> setMatrix(T rhs[])
  29 + {
  30 + M = rhs;
  31 + return *this;
  32 + }
28 33 CUDA_CALLABLE T& operator()(int row, int col)
29 34 {
30 35 return M[col * N + row];
... ... @@ -39,6 +44,7 @@ struct matrix
39 44 return *this;
40 45 }
41 46  
  47 +
42 48 template<typename Y>
43 49 CUDA_CALLABLE vec<Y, N> operator*(vec<Y, N> rhs)
44 50 {
... ...