Commit e843658b6c27157541f01e8c1bcb2d52a760758c

Authored by Brad Deutsch
1 parent 08ec34fa

Previous push did not work correctly. Added function 'sift-mask' which saves onl…

…y those spectral values corresponding to mask != 0. Syntax is 'hsiproc InputFile OutputFile --sift-mask MaskName'
stim/envi/bil.h
... ... @@ -228,6 +228,7 @@ public:
228 228 return true;
229 229  
230 230 }
  231 +
231 232  
232 233 /// Perform baseline correction given a list of baseline points and stores the result in a new BSQ file.
233 234  
... ... @@ -823,9 +824,9 @@ public:
823 824 for (unsigned i = 0; i < R[1]; i++) //for each value in R[1] (BIP should be X)
824 825 {
825 826 getY(temp, i); //retrieve an ZX slice, stored in temp
826   - for ( unsigned j = 0; j < R[2]; j++) //for each R[2] (BIP should be Y)
  827 + for ( unsigned j = 0; j < R[2]; j++) //for each R[2] (Y)
827 828 {
828   - for (unsigned k = 0; k < R[0]; k++)
  829 + for (unsigned k = 0; k < R[0]; k++) //for each band
829 830 {
830 831 if(p[i * R[0] + k] == 0)
831 832 temp[j * R[0] + k] = 0;
... ... @@ -840,6 +841,44 @@ public:
840 841 return true;
841 842 }
842 843  
  844 + ///Saves to disk only those spectra corresponding to mask values != 0
  845 + bool sift_mask(std::string outfile, unsigned char* p){
  846 + // Assume R[0] = X, R[1] = Y, R[2] = Z.
  847 + std::ofstream target(outfile.c_str(), std::ios::binary);
  848 +
  849 + //for loading pages:
  850 + unsigned XZ = R[0] * R[2]; //calculate the number of values in an XZ page on disk
  851 + unsigned L = XZ * sizeof(T); //calculate the size of the page (in bytes)
  852 + T * temp = (T*)malloc(L); //allocate memory for a temporary page
  853 +
  854 + //for saving spectra:
  855 + unsigned Z = R[2]; //calculate the number of values in a spectrum
  856 + unsigned LZ = Z * sizeof(T); //calculate the size of the spectrum (in bytes)
  857 + T * tempZ = (T*)malloc(LZ); //allocate memory for a temporary spectrum
  858 + spectrum(tempZ, R[0] - 1, R[1] - 1); //creates a dummy spectrum by taking the last spectrum in the image
  859 +
  860 + for (unsigned i = 0; i < R[1]; i++) //Select a page by choosing Y coordinate, R[1]
  861 + {
  862 + getY(temp, i); //retrieve an ZX page, store in "temp"
  863 + for (unsigned j = 0; j < R[0]; j++) //Select a pixel by choosing X coordinate in the page, R[0]
  864 + {
  865 + if (p[j * R[0] + i] != 0) //if the mask != 0 at that XY pixel
  866 + {
  867 + for (unsigned k = 0; k < R[2]; k++) //Select a voxel by choosing Z coordinate at the pixel
  868 + {
  869 + tempZ[k] = temp[k*R[0] + i]; //Pass the correct spectral value from XZ page into the spectrum to be saved.
  870 + }
  871 + target.write(reinterpret_cast<const char*>(tempZ), LZ); //write that spectrum to disk. Size is L2.
  872 + }
  873 + else
  874 + continue;
  875 + }
  876 + }
  877 + target.close();
  878 + free(temp);
  879 + return true;
  880 + }
  881 +
843 882 /// Calculate the mean band value (average along B) at each pixel location.
844 883  
845 884 /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages.
... ...
stim/envi/bip.h
... ... @@ -917,7 +917,7 @@ public:
917 917 {
918 918 for (unsigned k = 0; k < R[2]; k++) //for each B value (band)
919 919 {
920   - if(p[i * R[0] + k] == 0) //if the mask value is zero
  920 + if (p[i * R[0] + j] == 0) //if the mask value is zero
921 921 temp[j * R[2] + k] = 0; //set the pixel value to zero
922 922 else //otherwise just continue
923 923 continue;
... ... @@ -930,7 +930,46 @@ public:
930 930 return true; //return true
931 931 }
932 932  
933   - /// Calculate the mean band value (average along B) at each pixel location.
  933 +
  934 + ///Saves to disk only those spectra corresponding to mask values != 0
  935 + bool sift_mask(std::string outfile, unsigned char* p){
  936 + // Assume R[0] = X, R[1] = Y, R[2] = Z.
  937 + std::ofstream target(outfile.c_str(), std::ios::binary);
  938 +
  939 + //for loading pages:
  940 + unsigned ZX = R[0] * R[2]; //calculate the number of values in an XZ page on disk
  941 + unsigned L = ZX * sizeof(T); //calculate the size of the page (in bytes)
  942 + T * temp = (T*)malloc(L); //allocate memory for a temporary page
  943 +
  944 + //for saving spectra:
  945 + unsigned Z = R[2]; //calculate the number of values in a spectrum
  946 + unsigned LZ = Z * sizeof(T); //calculate the size of the spectrum (in bytes)
  947 + T * tempZ = (T*)malloc(LZ); //allocate memory for a temporary spectrum
  948 + spectrum(tempZ, R[0] - 1, R[1] - 1); //creates a dummy spectrum by taking the last spectrum in the image
  949 +
  950 + for (unsigned i = 0; i < R[1]; i++) //Select a page by choosing Y coordinate, R[1]
  951 + {
  952 + getY(temp, i); //retrieve an ZX page, store in "temp"
  953 + for (unsigned j = 0; j < R[0]; j++) //Select a pixel by choosing X coordinate in the page, R[0]
  954 + {
  955 + if (p[j * R[0] + i] != 0) //if the mask != 0 at that XY pixel
  956 + {
  957 + for (unsigned k = 0; k < R[2]; k++) //Select a voxel by choosing Z coordinate at the pixel
  958 + {
  959 + tempZ[k] = temp[i*R[2] + k]; //Pass the correct spectral value from XZ page into the spectrum to be saved.
  960 + }
  961 + target.write(reinterpret_cast<const char*>(tempZ), LZ); //write that spectrum to disk. Size is L2.
  962 + }
  963 + else
  964 + continue;
  965 + }
  966 + }
  967 + target.close();
  968 + free(temp);
  969 + return true;
  970 + }
  971 +
  972 +
934 973  
935 974 /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages.
936 975 bool band_avg(T* p){
... ...
stim/envi/bsq.h
... ... @@ -751,25 +751,55 @@ public:
751 751  
752 752 T * temp = (T*)malloc(L);
753 753  
754   - for (unsigned i = 0; i < R[2]; i++)
  754 + for (unsigned i = 0; i < R[2]; i++) //for each spectral bin
755 755 {
756   - band_index(temp, i);
757   - for ( unsigned j = 0; j < XY; j++)
  756 + band_index(temp, i); //get the specified band (by index)
  757 + for ( unsigned j = 0; j < XY; j++) // for each pixel
758 758 {
759   - if(p[j] == 0){
760   - temp[j] = 0;
  759 + if(p[j] == 0){ //if the mask is 0 at that pixel
  760 + temp[j] = 0; //set temp to zero
761 761 }
762 762 else{
763 763 continue;
764 764 }
765 765 }
766   - target.write(reinterpret_cast<const char*>(temp), L); //write a band data into target file
  766 + target.write(reinterpret_cast<const char*>(temp), L); //write the XY slice at that band to disk
767 767 }
768 768 target.close();
769 769 free(temp);
770 770 return true;
771 771 }
772 772  
  773 + ///Saves to disk only those spectra corresponding to mask values != 0
  774 + bool sift_mask(std::string outfile, unsigned char* p){
  775 + std::ofstream target(outfile.c_str(), std::ios::binary);
  776 + // open a band (XY plane)
  777 + unsigned XY = R[0] * R[1]; //Number of XY pixels
  778 + unsigned L = XY * sizeof(T); //size of XY pixels
  779 +
  780 + T * temp = (T*)malloc(L); //allocate memory for a band
  781 + T * temp_vox = (T*)malloc(sizeof(T)); //allocate memory for one voxel
  782 +
  783 + for (unsigned i = 0; i < R[2]; i++) //for each spectral bin
  784 + {
  785 + band_index(temp, i); //get the specified band (XY sheet by index)
  786 + for (unsigned j = 0; j < XY; j++) // for each pixel
  787 + {
  788 + if (p[j] != 0){ //if the mask is != 0 at that pixel
  789 + temp_vox[0] = temp[j];
  790 + target.write(reinterpret_cast<const char*>(temp_vox), sizeof(T)); //write the XY slice at that band to disk
  791 + }
  792 + else{
  793 + continue;
  794 + }
  795 + }
  796 + }
  797 + target.close();
  798 + free(temp);
  799 + return true;
  800 + }
  801 +
  802 +
773 803 /// Calculate the mean band value (average along B) at each pixel location.
774 804  
775 805 /// @param p is a pointer to memory of size X * Y * sizeof(T) that will store the band averages.
... ...
stim/envi/envi.h
... ... @@ -335,38 +335,76 @@ public:
335 335  
336 336 /// @param outfile is the name of the resulting masked output file
337 337 /// @param p is memory of size X*Y containing the mask (0 = false, all other values are true)
338   - bool apply_mask(std::string outfile, unsigned char* p)
  338 + bool apply_mask(std::string outfile, unsigned char* p)
339 339 {
340 340  
341   - if(header.interleave == envi_header::BSQ){ //if the infile is bsq file
342   - if(header.data_type ==envi_header::float32)
  341 + if (header.interleave == envi_header::BSQ){ //if the infile is bsq file
  342 + if (header.data_type == envi_header::float32)
343 343 return ((bsq<float>*)file)->apply_mask(outfile, p);
344   - else if(header.data_type == envi_header::float64)
  344 + else if (header.data_type == envi_header::float64)
345 345 return ((bsq<double>*)file)->apply_mask(outfile, p);
346 346 else
347   - std::cout<<"ERROR: unidentified data type"<<std::endl;
  347 + std::cout << "ERROR: unidentified data type" << std::endl;
348 348 }
349 349  
350   - else if(header.interleave == envi_header::BIL){ //if the infile is bil file
351   - if(header.data_type ==envi_header::float32)
  350 + else if (header.interleave == envi_header::BIL){ //if the infile is bil file
  351 + if (header.data_type == envi_header::float32)
352 352 return ((bil<float>*)file)->apply_mask(outfile, p);
353   - else if(header.data_type == envi_header::float64)
  353 + else if (header.data_type == envi_header::float64)
354 354 return ((bil<double>*)file)->apply_mask(outfile, p);
355 355 else
356   - std::cout<<"ERROR: unidentified data type"<<std::endl;
  356 + std::cout << "ERROR: unidentified data type" << std::endl;
357 357 }
358 358  
359   - else if(header.interleave == envi_header::BIP){ //if the infile is bip file
360   - if(header.data_type ==envi_header::float32)
  359 + else if (header.interleave == envi_header::BIP){ //if the infile is bip file
  360 + if (header.data_type == envi_header::float32)
361 361 return ((bip<float>*)file)->apply_mask(outfile, p);
362   - else if(header.data_type == envi_header::float64)
  362 + else if (header.data_type == envi_header::float64)
363 363 return ((bip<double>*)file)->apply_mask(outfile, p);
364 364 else
365   - std::cout<<"ERROR: unidentified data type"<<std::endl;
  365 + std::cout << "ERROR: unidentified data type" << std::endl;
366 366 }
367 367  
368 368 else{
369   - std::cout<<"ERROR: unidentified file type"<<std::endl;
  369 + std::cout << "ERROR: unidentified file type" << std::endl;
  370 + exit(1);
  371 + }
  372 + return false;
  373 + }
  374 +
  375 + /// sift-mask saves in an array only those spectra corresponding to nonzero values of the mask.
  376 + bool sift_mask(std::string outfile, unsigned char* p)
  377 + {
  378 +
  379 + if (header.interleave == envi_header::BSQ){ //if the infile is bsq file
  380 + if (header.data_type == envi_header::float32)
  381 + return ((bsq<float>*)file)->sift_mask(outfile, p);
  382 + else if (header.data_type == envi_header::float64)
  383 + return ((bsq<double>*)file)->sift_mask(outfile, p);
  384 + else
  385 + std::cout << "ERROR: unidentified data type" << std::endl;
  386 + }
  387 +
  388 + else if (header.interleave == envi_header::BIL){ //if the infile is bil file
  389 + if (header.data_type == envi_header::float32)
  390 + return ((bil<float>*)file)->sift_mask(outfile, p);
  391 + else if (header.data_type == envi_header::float64)
  392 + return ((bil<double>*)file)->sift_mask(outfile, p);
  393 + else
  394 + std::cout << "ERROR: unidentified data type" << std::endl;
  395 + }
  396 +
  397 + else if (header.interleave == envi_header::BIP){ //if the infile is bip file
  398 + if (header.data_type == envi_header::float32)
  399 + return ((bip<float>*)file)->sift_mask(outfile, p);
  400 + else if (header.data_type == envi_header::float64)
  401 + return ((bip<double>*)file)->sift_mask(outfile, p);
  402 + else
  403 + std::cout << "ERROR: unidentified data type" << std::endl;
  404 + }
  405 +
  406 + else{
  407 + std::cout << "ERROR: unidentified file type" << std::endl;
370 408 exit(1);
371 409 }
372 410 return false;
... ...
stim/gl/gl_spider.h
... ... @@ -41,7 +41,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
41 41 cudaGraphicsResource_t resource;
42 42 GLuint fboID;
43 43 GLuint texbufferID;
44   - int iter = 0; //temporary for testing
  44 + int iter = 0;
45 45  
46 46 void
47 47 findOptimalPosition()
... ... @@ -185,45 +185,39 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
185 185 }
186 186  
187 187 void
188   - UpdatePlanes(float v_x, float v_y, vec<float> vctr, int type = 0)
  188 + Update(float v_x, float v_y, vec<float> dir)
189 189 {
190 190 vec<float> Y(1.0,0.0,0.0);
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   - }
  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());
216 197 UpdateBuffer(v_x, v_y);
217 198 }
218   -
219   - /*
220   - Method for controling the buffer and texture binding in order to properly
221   - do the render to texture.
222   - */
  199 +
  200 +
223 201 void
224   - Bind(int numSamples = 1089)
  202 + Sample(vec<float> in = (0,0,1), int numSamples = 1089, int solidAngle = M_PI/2)
225 203 {
226   - float len = 10.0;
  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 +
227 221 glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer
228 222 glFramebufferTexture2D(
229 223 GL_FRAMEBUFFER,
... ... @@ -245,45 +239,11 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
245 239 gluOrtho2D(0.0,2.0*len,0.0,numSamples*len);
246 240 glEnable(GL_TEXTURE_3D);
247 241 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   - {
266 242  
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";
282 243 //file << "iteration: " << iter << "\n";
283 244 //file << "starting pos and dir:" << "[" << position[0] << "," <<position[1] << "," << position[2] << "]" << ":" << "[" << direction[0] << "," << direction[1] << "," << direction[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";
  245 + //Loop over the samples
  246 + file << position[0] << "," <<position[1] << "," << position[2] << "\n";
287 247 int idx;
288 248 for(int i = -dim; i <= dim; i++){
289 249 for(int j = -dim; j <= dim; j++){
... ... @@ -299,93 +259,28 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
299 259 samples[idx][1] = temp[1];
300 260 samples[idx][2] = temp[2];
301 261  
302   - // file << idx << ":" <<"[" << samples[idx][0] << "," << samples[idx][1] << "," << samples[idx][2] << "]" << "\n";
  262 + //file << idx << ":" <<"[" << samples[idx][0] << "," << samples[idx][1] << "," << samples[idx][2] << "]" << "\n";
303 263  
304   - UpdatePlanes(0.0, y_0+(idx)*10, temp);
  264 + Update(0.0, y_0+(idx)*10, temp);
305 265 }
306 266 }
307   - Unbind();
  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);
308 272 int nxt = getCost();
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];
  273 + stim::vec<float> next;
  274 + next[0] = samples[nxt][0];
  275 + next[1] = samples[nxt][1];
  276 + next[2] = samples[nxt][2];
313 277 next.norm();
314 278 //file << "next direction" << "[" << next[0] << "," << next[1] << "," << next[2] << "]\n\n";
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);
  279 + setPosition(position[0] + next[0]/500,
  280 + position[1]+next[1]/500,
  281 + position[2]+next[2]/500);
318 282 setDirection(next[0], next[1], next[2]);
319 283 //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";
389 284 }
390 285  
391 286 //--------------------------------------------------------------------------//
... ... @@ -433,36 +328,48 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
433 328 stim::rect<float> hor;
434 329 stim::rect<float> ver;
435 330  
436   - //Default Constructor
  331 +
437 332 gl_spider
438 333 ()
439 334 {
440 335 setPosition(0.0,0.0,0.0);
441   - setDirection(0.0,0.0,1.0);
442   - setMagnitude(0.03);
  336 + setDirection(1.0,1.0,1.0);
  337 + setMagnitude(0.1,0.1);
  338 + //GenerateFBO(400,200);
  339 + //Update();
443 340 }
444 341  
445   - //temporary constructor for convenience, will be removed in further updates.
  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.
446 352 gl_spider
447 353 (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z,
448   - float mag_x)
  354 + float mag_x, float mag_y)
449 355 {
450 356 setPosition(pos_x, pos_y, pos_z);
451 357 setDirection(dir_x, dir_y, dir_z);
452   - setMagnitude(mag_x);
  358 + setMagnitude(mag_x, mag_y);
  359 + //GenerateFBO(400,200);
  360 + //Update();
453 361 }
454 362  
455   - //Attached the spider to the texture with the given GLuint ID.
456   - //Samples in the default direction acting as the init method.
457 363 void
458   - attachSpider(GLuint id, int numSamples = 1089)
  364 + attachSpider(GLuint id)
459 365 {
460 366 texID = id;
461   - GenerateFBO(20, numSamples*10);
462   - sampleDirection();
  367 + Sample(direction);
  368 + //GenerateFBO(20,10000);
  369 + // Update();
  370 + // generateVectorField(direction, 4.0);
463 371 }
464   -
465   - //temporary Method necessary for visualization and testing.
  372 +
466 373 void
467 374 Update()
468 375 {
... ... @@ -473,37 +380,35 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
473 380 ((Y.cross(direction)).cross(direction)).norm());
474 381 ver = stim::rect<float>(magnitude, position, direction.norm(),
475 382 hor.n());
  383 + //UpdateBuffer();
  384 + generateVectorField(direction, 4.0);
476 385 }
477 386  
478   - //Returns the position vector.
  387 +
479 388 vec<float>
480 389 getPosition()
481 390 {
482 391 return position;
483 392 }
484 393  
485   - //Returns the direction vector.
486 394 vec<float>
487 395 getDirection()
488 396 {
489 397 return direction;
490 398 }
491 399  
492   - //Returns the magnitude vector.
493 400 vec<float>
494 401 getMagnitude()
495 402 {
496 403 return magnitude;
497 404 }
498 405  
499   - //Sets the position vector to input vector pos
500 406 void
501 407 setPosition(vec<float> pos)
502 408 {
503 409 position = pos;
504 410 }
505 411  
506   - //Sets the position vector to the input float coordinates x,y,z
507 412 void
508 413 setPosition(float x, float y, float z)
509 414 {
... ... @@ -512,15 +417,12 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
512 417 position[2] = z;
513 418 }
514 419  
515   -
516   - //Sets the direction vector to input vector dir
517 420 void
518 421 setDirection(vec<float> dir)
519 422 {
520 423 direction = dir;
521 424 }
522 425  
523   - //Sets the direction vector to the input float coordinates x,y,z
524 426 void
525 427 setDirection(float x, float y, float z)
526 428 {
... ... @@ -529,22 +431,19 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
529 431 direction[2] = z;
530 432 }
531 433  
532   - //Sets the magnitude vector to the input vector mag.
533 434 void
534 435 setMagnitude(vec<float> mag)
535 436 {
536   - magnitude[0] = mag[0];
537   - magnitude[1] = mag[0];
  437 + magnitude = mag;
538 438 }
539 439  
540 440 void
541   - setMagnitude(float mag)
  441 + setMagnitude(float x, float y)
542 442 {
543   - magnitude[0] = mag;
544   - magnitude[1] = mag;
  443 + magnitude[0] = x;
  444 + magnitude[1] = y;
545 445 }
546   -
547   - //temporary method for visualization.
  446 +
548 447 GLuint
549 448 getFB()
550 449 {
... ... @@ -554,9 +453,142 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
554 453 void
555 454 Step()
556 455 {
  456 + std::cout << position[0] << "," << position[1] << "," << position[1]
  457 + << std::endl;
  458 + //setPosition(direction*magnitude[1]/2+position);
557 459 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 +
560 592  
561 593 void
562 594 findOptimalDirection()
... ... @@ -564,9 +596,7 @@ class gl_spider : public virtual gl_texture&lt;T&gt;
564 596 /* Method for finding the best direction for the spider.
565 597 Uses the camera to rotate. Then Calls Evaluate to find new cost.
566 598 */
567   - sampleDirection();
568   - samplePosition();
569   - sampleMagnitude();
  599 + Sample(direction);
570 600 }
571 601  
572 602 /* Method for initializing the cuda devices, necessary only
... ...
stim/math/rect.h
... ... @@ -92,20 +92,12 @@ public:
92 92 Y = directionY;
93 93 }
94 94  
95   - CUDA_CALLABLE rect(T size, vec<T, N> center, vec<T, N> directionX, vec<T, N> directionY )
  95 + CUDA_CALLABLE rect(vec<T,N> mag, 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(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]);
  100 + scale(mag[0], mag[1]);
109 101 }
110 102  
111 103 CUDA_CALLABLE void scale(T factor1, T factor2){
... ...