From 035d798f5b89a6f60dab19e6c5c24ef2c1f73fd6 Mon Sep 17 00:00:00 2001 From: pgovyadi Date: Wed, 13 Jan 2016 15:16:25 -0600 Subject: [PATCH] modified the spider to have a separate buffer for each of the predictor corrector algorithms. Now using ~400 position and ~100 size templates. --- stim/cuda/spider_cost.cuh | 2 +- stim/gl/gl_spider.h | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------- stim/gl/gl_texture.h | 35 ++++++++++------------------------- 3 files changed, 88 insertions(+), 65 deletions(-) diff --git a/stim/cuda/spider_cost.cuh b/stim/cuda/spider_cost.cuh index ecf85b3..d35ef75 100644 --- a/stim/cuda/spider_cost.cuh +++ b/stim/cuda/spider_cost.cuh @@ -150,7 +150,7 @@ namespace stim{ // stringstream name; //for debugging // name << "Test.bmp"; -// stim::gpu2image(print, name.str(),16,218,0,1); +// stim::gpu2image(print, name.str(),16,218,0,256); t.UnmapCudaTexture(); cleanUP(); diff --git a/stim/gl/gl_spider.h b/stim/gl/gl_spider.h index d886938..4217c71 100644 --- a/stim/gl/gl_spider.h +++ b/stim/gl/gl_spider.h @@ -74,11 +74,15 @@ class gl_spider : public virtual gl_texture GLuint mfboID; //buffer object for magnitude adjustment. GLuint mtexbufferID; //texture object for magnitude adjustment. - GLuint bfboId; //buffer object for position adjustment. + GLuint bfboID; //buffer object for position adjustment. GLuint btexbufferID; //buffer object for position adjustment. int numSamples; //The number of templates in the buffer. - float stepsize = 4.0; //Step size. + int numSamplesPos; + int numSamplesMag; + +// float stepsize = 4.0; //Step size. + float stepsize = 3.0; //Step size. int current_cost; //variable to store the cost of the current step. @@ -113,7 +117,7 @@ class gl_spider : public virtual gl_texture { setMatrix(); //create the transformation matrix. glCallList(dList); //move the templates to p, d, m. - int best = getCost(); //find min cost. + int best = getCost(texbufferID,numSamples); //find min cost. stim::vec next( //find next vector. dV[best][0]*S[0]*R[0], dV[best][1]*S[1]*R[1], @@ -135,7 +139,7 @@ class gl_spider : public virtual gl_texture { setMatrix(); //create the transformation matrix. glCallList(dList+1); //move the templates to p, d, m. - int best = getCost(); //find min cost. + int best = getCost(ptexbufferID, numSamplesPos); //find min cost. stim::vec next( //find next position. pV[best][0], pV[best][1], @@ -157,7 +161,7 @@ class gl_spider : public virtual gl_texture { setMatrix(); //create the transformation. glCallList(dList+2); //move the templates to p, d, m. - int best = getCost(); //get best cost. + int best = getCost(mtexbufferID, numSamplesMag); //get best cost. setMagnitude(m[0]*mV[best][0]); //adjust the magnitude. } @@ -288,7 +292,7 @@ class gl_spider : public virtual gl_texture //Set up the variable necessary for vector creation. vec temp(0,0,0); - int dim = (sqrt(numSamples)-1)/2; //number of position vectors. + int dim = (sqrt(numSamplesPos)-1)/2; //number of position vectors. stim::rect samplingPlane = //plane from which we pull position samples stim::rect(p, d); samplingPlane.scale(mag[0]*delta, mag[0]*delta); @@ -338,15 +342,15 @@ class gl_spider : public virtual gl_texture vec dir(0.0, 0.0, 1.0); //Set up the variable necessary for vector creation. - int dim = (sqrt(numSamples)-1)/2; + int dim = (sqrt(numSamplesMag)-1)/2; float min = 1.0-delta; float max = 1.0+delta; - float step = (max-min)/(numSamples-1); + float step = (max-min)/(numSamplesMag-1); float factor; vec temp(0.0,0.0,0.0); glNewList(dList+2, GL_COMPILE); - for(int i = 0; i < numSamples; i++){ + for(int i = 0; i < numSamplesMag; i++){ //Create linear index factor = (min+step*i)*mag[0]; temp = factor; @@ -585,8 +589,8 @@ class gl_spider : public virtual gl_texture GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; glDrawBuffers(1, DrawBuffers); glBindTexture(GL_TEXTURE_2D, textureID); - glClearColor(1,1,1,1); - glClear(GL_COLOR_BUFFER_BIT); +// glClearColor(1,1,1,1); +// glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); @@ -629,7 +633,7 @@ class gl_spider : public virtual gl_texture findOptimalPosition(); findOptimalScale(); Unbind(); - Bind(btexbufferID, bfboId, 27); + Bind(btexbufferID, bfboID, 27); branchDetection(); Unbind(); @@ -670,6 +674,23 @@ class gl_spider : public virtual gl_texture return cost[0]; } + int + getCost(GLuint tID, int n) + { + #ifdef TESTING + start = std::clock(); + #endif + stim::vec cost = + stim::cuda::get_cost(tID, GL_TEXTURE_2D, n); + cudaDeviceSynchronize(); + #ifdef TESTING + duration_cuda = duration_cuda + + (std::clock() - start) / (double) CLOCKS_PER_SEC; + num_cuda = num_cuda + 1.0; + #endif + current_cost = cost[1]; + return cost[0]; + } public: @@ -706,7 +727,7 @@ class gl_spider : public virtual gl_texture ///Best results if samples is can create a perfect root. ///Default Constructor gl_spider - (int samples = 1089) + (int samples = 1089, int samplespos = 400,int samplesmag = 144) { p = vec(0.0, 0.0, 0.0); d = vec(0.0, 0.0, 1.0); @@ -714,6 +735,8 @@ class gl_spider : public virtual gl_texture S = vec(1.0, 1.0, 1.0); R = vec(1.0, 1.0, 1.0); numSamples = samples; + numSamplesPos = samplespos; + numSamplesMag = samplesmag; } ///Position constructor: floats. @@ -727,14 +750,16 @@ class gl_spider : public virtual gl_texture ///@param int samples, number of templates this spider is going to use. gl_spider (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z, - float mag_x, int numSamples = 1089) + float mag_x, int numsamples = 1089, int numsamplespos = 400, int numsamplesmag =144) { p = vec(pos_x, pos_y, pos_z); d = vec(dir_x, dir_y, dir_z); m = vec(mag_x, mag_x, mag_x); S = vec(1.0,1.0,1.0); R = vec(1.0,1.0,1.0); -// numSamples = samples; + numSamples = numsamples; + numSamplesPos = numsamplespos; + numSamplesMag = numsamplesmag; } ///Position constructor: vecs of floats. @@ -743,7 +768,7 @@ class gl_spider : public virtual gl_texture ///@param float mag, size of the vector. ///@param int samples, number of templates this spider is going to use. gl_spider - (stim::vec pos, stim::vec dir, float mag, int samples = 1089) + (stim::vec pos, stim::vec dir, float mag, int samples = 1089, int samplesPos = 400, int samplesMag = 144) { p = pos; d = dir; @@ -751,6 +776,8 @@ class gl_spider : public virtual gl_texture S = vec(1.0,1.0,1.0); R = vec(1.0,1.0,1.0); numSamples = samples; + numSamplesPos = samplesPos; + numSamplesMag = samplesMag; } ///destructor @@ -760,12 +787,12 @@ class gl_spider : public virtual gl_texture Unbind(); glDeleteTextures(1, &texbufferID); glDeleteBuffers(1, &fboID); - /* glDeleteTextures(1, &ptexbufferID); - glDeleteBuffers(1, &pfboId); + glDeleteTextures(1, &ptexbufferID); + glDeleteBuffers(1, &pfboID); glDeleteTextures(1, &mtexbufferID); - glDeleteBuffers(1, &mfboId); */ + glDeleteBuffers(1, &mfboID); glDeleteTextures(1, &btexbufferID); - glDeleteBuffers(1, &bfboId); + glDeleteBuffers(1, &bfboID); } ///@param GLuint id, texture that is going to be sampled. @@ -776,19 +803,26 @@ class gl_spider : public virtual gl_texture attachSpider(GLuint id) { texID = id; - GenerateFBO(16, numSamples*8); - GenerateFBO(16, 216, btexbufferID, bfboId); + //GenerateFBO(16, numSamples*8); + GenerateFBO(16, numSamples*8, texbufferID, fboID); + GenerateFBO(16, numSamplesPos*8, ptexbufferID, pfboID); + GenerateFBO(16, numSamplesMag*8, mtexbufferID, mfboID); + GenerateFBO(16, 216, btexbufferID, bfboID); setDims(0.6, 0.6, 1.0); setSize(512.0, 512.0, 426.0); setMatrix(); dList = glGenLists(3); glListBase(dList); - Bind(); - genDirectionVectors(5*M_PI/4); - genPositionVectors(); - genMagnitudeVectors(); - Unbind(); - Bind(btexbufferID, bfboId, 27); + Bind(texbufferID, fboID, numSamples); + genDirectionVectors(5*M_PI/4); +// Unbind(); + Bind(ptexbufferID, bfboID, numSamplesPos); + genPositionVectors(); +// Unbind(); + Bind(mtexbufferID, mfboID, numSamplesMag); + genMagnitudeVectors(); +// Unbind(); + Bind(btexbufferID, bfboID, 27); DrawCylinder(); Unbind(); } @@ -1086,7 +1120,7 @@ class gl_spider : public virtual gl_texture GLuint getFB() { - return bfboId; + return bfboID; } //--------------------------------------------------------------------------// @@ -1110,15 +1144,19 @@ class gl_spider : public virtual gl_texture int Step() { - Bind(); + Bind(texbufferID, fboID, numSamples); CHECK_OPENGL_ERROR #ifdef TESTING start = std::clock(); #endif - findOptimalDirection(); - findOptimalPosition(); - findOptimalScale(); - Unbind(); + findOptimalDirection(); + //Unbind(); + Bind(ptexbufferID, pfboID, numSamplesPos); + findOptimalPosition(); + //Unbind(); + Bind(mtexbufferID, mfboID, numSamplesMag); + findOptimalScale(); + //Unbind(); CHECK_OPENGL_ERROR #ifdef TESTING @@ -1279,7 +1317,7 @@ class gl_spider : public virtual gl_texture cL.push_back(stim::vec(p[0], p[1],p[2]));// sk.TexCoord(m[0]); sk.Vertex(p[0], p[1], p[2]); - Bind(btexbufferID, bfboId, 27); + Bind(btexbufferID, bfboID, 27); CHECK_OPENGL_ERROR branchDetection(); CHECK_OPENGL_ERROR @@ -1386,7 +1424,7 @@ class gl_spider : public virtual gl_texture } else { - printf ("%u ", *ptr); +// printf ("%u ", *ptr); return *ptr; } } @@ -1441,8 +1479,8 @@ class gl_spider : public virtual gl_texture void printSizes() { - std::cout << nt.sizeE() << "edges" << std::endl; - std::cout << nt.sizeV() << "nodes" << std::endl; + std::cout << nt.sizeE() << " edges " << std::endl; + std::cout << nt.sizeV() << " nodes " << std::endl; } @@ -1531,7 +1569,7 @@ class gl_spider : public virtual gl_texture cM.push_back(m[0]); sk.TexCoord(m[0]); sk.Vertex(p[0], p[1], p[2]); - Bind(btexbufferID, bfboId, 27); + Bind(btexbufferID, bfboID, 27); CHECK_OPENGL_ERROR branchDetection(); CHECK_OPENGL_ERROR diff --git a/stim/gl/gl_texture.h b/stim/gl/gl_texture.h index dbab17e..041f52c 100644 --- a/stim/gl/gl_texture.h +++ b/stim/gl/gl_texture.h @@ -31,10 +31,9 @@ template class gl_texture : public virtual image_stack { private: - ///Method: setTextureType /// Sets the internal texture_type, based on the data /// size. Either 3D, 2D, 1D textures. - + void setTextureType() { @@ -60,16 +59,14 @@ class gl_texture : public virtual image_stack public: - ///Method: Basic Constructor - /// Creates an instance of the gl_texture object. + ///default constructor gl_texture() { } - ///Method: Path Constructor - ///@param string file_path path to the directory with the files. - /// Creates an instance of the gl_texture object with a path to the data. + ///@param string path to the directory with the image files. + ///Creates an instance of the gl_texture object with a path to the data. gl_texture(std::string file_path) { @@ -77,8 +74,8 @@ class gl_texture : public virtual image_stack image_stack::load_images(path.append("/*.jpg")); setTextureType(); } - ///Method:getSize - ///returns the dimentions of + + ///returns the dimentions of the data in the x, y, z directions. vec getSize() { @@ -86,7 +83,6 @@ class gl_texture : public virtual image_stack return size; } - ///Method:setTexParam ///@param GLint interp --GL_LINEAR, GL_NEAREST... ///@param GLint twrap --GL_REPEAR, GL_CLAMP_TO_EDGE... ///@param GLenum dataType --GL_UNSIGNED_BYTE, GL_FLOAT16... @@ -103,7 +99,7 @@ class gl_texture : public virtual image_stack type = dataType; format = dataFormat; } - ///Method:setDims + ///@param x size of the voxel in x direction ///@param y size of the voxel in y direction ///@param z size of the voxel in z direction @@ -116,9 +112,7 @@ class gl_texture : public virtual image_stack S[3] = z; } - ///Method:getDims - /// get the dimenstions of the voxels. - + ///Returns a stim::vec that contains the x, y, z sizes of the voxel. vec getDims() { @@ -126,11 +120,8 @@ class gl_texture : public virtual image_stack return dims; } - ///Method:setPath ///@param file_Path location of the directory with the files /// Sets the path and calls the loader on that path. - - void setPath(std::string file_path) { @@ -139,19 +130,14 @@ class gl_texture : public virtual image_stack setTextureType(); } - ///Method: getPath - ///Outputs: string path - /// Returns the path associated with an instance of the gl_texture class. - + /// Returns an std::string path associated with an instance of the gl_texture class. std::string getPath() { return path; } - ///Method: getTexture - ///Outputs: GLuint texID - /// Returns the id of the texture create by/associated with the + /// Returns the GLuint id of the texture created by/associated with the /// instance of the gl_texture class. GLuint @@ -160,7 +146,6 @@ class gl_texture : public virtual image_stack return texID; } - ///Method: createTexture /// Creates a texture and from the loaded data and /// assigns that texture to texID //TO DO :::: 1D textures -- libgit2 0.21.4