diff --git a/stim/biomodels/centerline.h b/stim/biomodels/centerline.h index a57ded9..40d56eb 100644 --- a/stim/biomodels/centerline.h +++ b/stim/biomodels/centerline.h @@ -163,25 +163,25 @@ public: /// Return the point on the fiber closest to q /// @param q is the query point used to locate the nearest point on the fiber centerline - stim::vec nearest(stim::vec q){ - - stim::vec temp( (double) q[0], (double) q[1], (double) q[2]); - - unsigned int idx = ann(temp); //determine the index of the nearest neighbor - - return stim::vec((T) c[idx][0], (T) c[idx][1], (T) c[idx][2]); //return the nearest centerline point - } +// stim::vec nearest(stim::vec q){ +// +// stim::vec temp( (double) q[0], (double) q[1], (double) q[2]); +// +// unsigned int idx = ann(temp); //determine the index of the nearest neighbor +// +// return stim::vec((T) c[idx][0], (T) c[idx][1], (T) c[idx][2]); //return the nearest centerline point +// } /// Return the point index on the fiber closest to q /// @param q is the query point used to locate the nearest point on the fiber centerline - unsigned int nearest_idx(stim::vec q){ - - stim::vec temp((double) q[0], (double) q[1], (double) q[2]); - - unsigned int idx = ann(temp); //determine the index of the nearest neighbor - - return idx; //return the nearest centerline point index - } +// unsigned int nearest_idx(stim::vec q){ +// +// stim::vec temp((double) q[0], (double) q[1], (double) q[2]); +// +// unsigned int idx = ann(temp); //determine the index of the nearest neighbor +// +// return idx; //return the nearest centerline point index +// } /// Returns the fiber centerline as an array of stim::vec points std::vector< stim::vec > get_centerline(){ diff --git a/stim/gl/gl_spider.h b/stim/gl/gl_spider.h index 90ff7ca..c792fe6 100644 --- a/stim/gl/gl_spider.h +++ b/stim/gl/gl_spider.h @@ -28,9 +28,6 @@ #include "../../../volume-spider/glnetwork.h" #include #include - -//#include - #include #include #ifdef TIMING @@ -39,7 +36,6 @@ #endif #ifdef TESTING - #include #include #include #endif @@ -67,16 +63,16 @@ class gl_spider // : public virtual gl_texture stim::vec3 p; //vector designating the position of the spider. stim::vec3 d; //normalized direction of travel - stim::vec m; //size of the spider in tissue space + float m; //size of the spider in tissue space. std::vector > dV; //A list of all the direction vectors. std::vector > pV; //A list of all test positions (relative to p) - std::vector > mV; //A list of all the size vectors. + std::vector mV; //A list of all the size vectors. stim::matrix cT; //current Transformation matrix (tissue)->(texture) GLuint texID; //OpenGL ID for the texture to be traced - stim::vec S; //Size of a voxel in the volume. - stim::vec R; //Dimensions of the volume. + stim::vec3 S; //Size of a voxel in the volume. + stim::vec3 R; //Dimensions of the volume. //GL and Cuda variables @@ -88,15 +84,17 @@ class gl_spider // : public virtual gl_texture GLuint fboID; //framebuffer ID for direction templates GLuint texbufferID; //texture ID for direction templates + GLuint direction_buffID; //framebuffer ID, position templates + GLuint direction_texID; //texture ID, position templates - GLuint pfboID; //framebuffer ID, position templates - GLuint ptexbufferID; //texture ID, position templates + GLuint position_buffID; //framebuffer ID, position templates + GLuint position_texID; //texture ID, position templates - GLuint mfboID; //framebuffer ID, radius templates - GLuint mtexbufferID; //texture ID, radius templates + GLuint radius_buffID; //framebuffer ID, radius templates + GLuint radius_texID; //texture ID, radius templates - GLuint bfboID; //framebuffer ID, cylinder (surrounding fiber) - GLuint btexbufferID; //texture ID, cylinder + GLuint cylinder_buffID; //framebuffer ID, cylinder (surrounding fiber) + GLuint cylinder_texID; //texture ID, cylinder int numSamples; //The number of templates in the buffer. int numSamplesPos; @@ -114,7 +112,7 @@ class gl_spider // : public virtual gl_texture std::vector< stim::vec3 > cL; //centerline up to the current point std::vector< stim::vec3 > cD; //directions up to the current point (debugging) - std::vector< stim::vec > cM; //radius up to the current point + std::vector< float > cM; //radius up to the current point stim::glnetwork nt; //network object holding the currently traced centerlines stim::glObj sk; //OBJ file storing the network (identical to above) @@ -128,12 +126,12 @@ class gl_spider // : public virtual gl_texture stim::vec3 ups; //up direction for the selection camera stim::vec3 ds; //direction for the selection camera - float t_length; //length of the template (in pixels) + float n_pixels; //length of the template (in pixels) //cuda texture variables that keep track of the binding. - stim::cuda::cuda_texture t_dir; - stim::cuda::cuda_texture t_pos; - stim::cuda::cuda_texture t_mag; + stim::cuda::cuda_texture t_dir; //cuda_texture object used as an interface between OpenGL and cuda for direction vectors. + stim::cuda::cuda_texture t_pos; //cuda_texture object used as an interface between OpenGL and cuda for position vectors. + stim::cuda::cuda_texture t_mag; //cuda_texture object used as an interface between OpenGL and cuda for size vectors. //--------------------------------------------------------------------------// @@ -147,30 +145,29 @@ class gl_spider // : public virtual gl_texture findOptimalDirection() { #ifdef TIMING - gpuStartTimer(); + gpuStartTimer(); //Timer for profiling #endif setMatrix(); //create the transformation matrix. glCallList(dList); //move the templates to p, d, m. - glFinish(); + glFinish(); //flush the pipeline #ifdef TIMING - direction_time += gpuStopTimer(); + direction_time += gpuStopTimer(); //profiling #endif #ifdef TESTING -// test(texbufferID, GL_TEXTURE_2D,2*t_length,numSamples*t_length, "Final_Cost_Direction.bmp"); +// test(texbufferID, GL_TEXTURE_2D,2*n_pixels,numSamples*n_pixels, "Final_Cost_Direction.bmp"); #endif int best = getCost(t_dir.getTexture(), t_dir.getAuxArray() ,numSamples); //find min cost. - stim::vec next( //find next vector. + stim::vec next( ///calculate the next vector. dV[best][0]*S[0]*R[0], dV[best][1]*S[1]*R[1], dV[best][2]*S[2]*R[2], 0); - next = (cT*next).norm(); //find next vector. - setPosition( p[0]+next[0]*m[0]/stepsize, - p[1]+next[1]*m[0]/stepsize, - p[2]+next[2]*m[0]/stepsize); - setDirection(next[0], next[1], next[2]); - //move forward and change direction. + next = (cT*next).norm(); ///transform the next vector into Tissue space. + setPosition( p[0]+next[0]*m/stepsize, + p[1]+next[1]*m/stepsize, + p[2]+next[2]*m/stepsize); + setDirection(next[0], next[1], next[2]); //move forward and change direction. } /// Method for finding the best d (direction) for the spider. @@ -180,17 +177,18 @@ class gl_spider // : public virtual gl_texture findOptimalPosition() { #ifdef TIMING - gpuStartTimer(); + gpuStartTimer(); //timer for profiling #endif setMatrix(); //create the transformation matrix. glCallList(dList+1); //move the templates to p, d, m. - glFinish(); + glFinish(); //flush the pipeline // glFlush(); #ifdef TIMING - position_time += gpuStopTimer(); + position_time += gpuStopTimer(); ///timer for profiling #endif #ifdef TESTING +// test(texbufferID, GL_TEXTURE_2D,2*n_pixels,numSamples*n_pixels, "Final_Cost_Direction.bmp"); #endif int best = getCost(t_pos.getTexture(), t_pos.getAuxArray(), numSamplesPos); //find min cost. stim::vec next( //find next position. @@ -198,7 +196,7 @@ class gl_spider // : public virtual gl_texture pV[best][1], pV[best][2], 1); - next = cT*next; //find next position. + next = cT*next; //transform the next position vector into tissue space. setPosition( next[0]*S[0]*R[0], next[1]*S[1]*R[1], @@ -217,14 +215,14 @@ class gl_spider // : public virtual gl_texture #endif setMatrix(); //create the transformation. glCallList(dList+2); //move the templates to p, d, m. - glFinish(); + glFinish(); //flush the drawing pipeline. #ifdef TIMING size_time += gpuStopTimer(); #endif #ifdef TESTING #endif int best = getCost(t_mag.getTexture(), t_mag.getAuxArray(), numSamplesMag); //get best cost. - setMagnitude(m[0]*mV[best][0]); //adjust the magnitude. + setMagnitude(m*mV[best]); //adjust the magnitude. } @@ -236,80 +234,78 @@ class gl_spider // : public virtual gl_texture branchDetection2(int n = 8, int l_template = 8, int l_square = 8) { #ifdef TIMING - gpuStartTimer(); + gpuStartTimer(); ///timer for performance analysis #endif - if(cL.size() < 4){} + if(cL.size() < 4){} ///if the size of the fiber is less then 4 we do nothing. else{ - setMatrix(1); - DrawLongCylinder(n, l_template, l_square); + setMatrix(1); ///finds the current transformation matrix + DrawLongCylinder(n, l_template, l_square); ///Draw the cylinder. stim::cylinder cyl(cL, cM); - std::vector< stim::vec > result = find_branch(btexbufferID, GL_TEXTURE_2D, n*l_square, (cL.size()-1)*l_template); - stim::vec3 size(S[0]*R[0], S[1]*R[1], S[2]*R[2]); - float pval; - if(!result.empty()) + std::vector< stim::vec > result = find_branch(cylinder_texID, GL_TEXTURE_2D, n*l_square, (cL.size()-1)*l_template); ///find all the centers in cuda + stim::vec3 size(S[0]*R[0], S[1]*R[1], S[2]*R[2]); ///the borders of the texture. + float pval; //pvalue associated with the points on the cylinder. + if(!result.empty()) ///if we have any points { - for(int i = 0; i < result.size(); i++) + for(int i = 0; i < result.size(); i++) ///for each point { int id = result[i][2]; - if(fmod(result[i][2], id) != 0 && id != 0) + if(fmod(result[i][2], id) != 0 && id != 0) ///if the remainer is odd { pval = ((cyl.getl(id+1)-cyl.getl(id))* - (fmod(result[i][2], id))+cyl.getl(id))/cyl.getl(cL.size()-1); + (fmod(result[i][2], id))+cyl.getl(id))/cyl.getl(cL.size()-1); ///calculate pvalue } - else if(id == 0) + else if(id == 0) ///if the point is on the edge { - pval = (cyl.getl(id+1)*result[i][2])/cyl.getl(cL.size()-1); + pval = (cyl.getl(id+1)*result[i][2])/cyl.getl(cL.size()-1); } else { - pval = (cyl.getl(id)/cyl.getl(cL.size()-1)); + pval = (cyl.getl(id)/cyl.getl(cL.size()-1)); ///if the point is somewhere on the surface of the cylinder other than the edge } - stim::vec3 v = cyl.surf(pval, result[i][0]); - stim::vec3 di = cyl.p(pval); - float rad = cyl.r(pval); - - std::cout << v.str() << " " << di.str() << " " << rad << std::endl; + stim::vec3 v = cyl.surf(pval, result[i][0]); ///find the coordinates of the point at pval on the surface in tissue space. + stim::vec3 di = cyl.p(pval); ///find the coord of v in tissue space projected on the centerline. + float rad = cyl.r(pval); ///find the radius at the pvalue's location if( !(v[0] > size[0] || v[1] > size[1] || v[2] > size[2] || v[0] < 0 - || v[1] < 0 || v[2] < 0)) + || v[1] < 0 || v[2] < 0)) ///if the v point is INSIDE the volume { - setSeed(v); - setSeedVec((v-di).norm()); - setSeedMag(rad); + setSeed(v); ///add a seedpoint's position. + setSeedVec((v-di).norm()); ///add a seedpoints direction + setSeedMag(rad); ///add the starting radius. } } } } #ifdef TIMING - branch_time += gpuStopTimer(); + branch_time += gpuStopTimer(); ///timer for performance. #endif } float uniformRandom() { - return ( (float)(rand()))/( (float)(RAND_MAX)); + return ( (float)(rand()))/( (float)(RAND_MAX)); ///generates a random number between 0 and 1 using the uniform distribution. } float normalRandom() { float u1 = uniformRandom(); float u2 = uniformRandom(); - return cos(2.0*atan(1.0)*u2)*sqrt(-1.0*log(u1)); + return cos(2.0*atan(1.0)*u2)*sqrt(-1.0*log(u1)); ///generate a random number using the normal distribution between 0 and 1. } stim::vec3 uniformRandVector() { - stim::vec3 r(uniformRandom(), uniformRandom(), 1.0); + stim::vec3 r(uniformRandom(), uniformRandom(), 1.0); ///generate a random vector using the uniform distribution between 0 and 1. return r; } stim::vec3 normalRandVector() { - stim::vec3 r(normalRandom(), normalRandom(), 1.0); + stim::vec3 r(normalRandom(), normalRandom(), 1.0); ///generate a random vector using the normal distribution between 0 and 1. return r; } @@ -332,44 +328,44 @@ class gl_spider // : public virtual gl_texture //Set up the vectors necessary for Rectangle creation. stim::vec3 Y(1.0,0.0,0.0); //orthogonal vec. - stim::vec3 pos(0.0,0.0,0.0); - stim::vec3 mag(1.0, 1.0, 1.0); - stim::vec3 dir(0.0, 0.0, 1.0); + stim::vec3 pos(0.0,0.0,0.0); //center point of a rectangle + float mag = 1.0; //size of the generated rectangle. + stim::vec3 dir(0.0, 0.0, 1.0); //normal of the rectangle - float PHI[2], Z[2], range; - PHI[0] = solidAngle/2; + float PHI[2], Z[2], range; + PHI[0] = solidAngle/2; ///Project the solid angle into spherical coordinates PHI[1] = asin(0); - Z[0] = cos(PHI[0]); + Z[0] = cos(PHI[0]); ///Project the z into spherical coordinates Z[1] = cos(PHI[1]); - range = Z[0] - Z[1]; + range = Z[0] - Z[1]; ///The range the possible values can be. float z, theta, phi; - glNewList(dList, GL_COMPILE); - for(int i = 0; i < numSamples; i++) + glNewList(dList, GL_COMPILE); ///create a display list of all the direction templates. + for(int i = 0; i < numSamples; i++) ///for each sample { - z = uniformRandom()*range + Z[1]; - theta = uniformRandom()*stim::TAU; - phi = acos(z); - stim::vec3 sph(1, theta, phi); - stim::vec3 cart = sph.sph2cart(); - dV.push_back(cart); - if(cos(Y.dot(cart)) < 0.087) + z = uniformRandom()*range + Z[1]; ///generate a z coordinate + theta = uniformRandom()*stim::TAU; ///generate a theta coordinate + phi = acos(z); ///generate a phi from the z. + stim::vec3 sph(1, theta, phi); ///combine into a vector in spherical coordinates. + stim::vec3 cart = sph.sph2cart();///convert to cartesian. + dV.push_back(cart); ///save the generated vector for further use. + if(cos(Y.dot(cart)) < 0.087) ///make sure that the Y is not parallel to the new vector. { Y[0] = 0.0; Y[1] = 1.0; }else{ Y[0] = 1.0; Y[1] = 0.0; } - hor = stim::rect(mag, + hor = stim::rect(mag, ///generate a rectangle with the new vectro as a normal. pos, cart, ((Y.cross(cart)).cross(cart)).norm()); - ver = stim::rect(mag, + ver = stim::rect(mag, ///generate another rectangle that's perpendicular the first but parallel to the cart vector. pos, cart, hor.n()); - UpdateBuffer(0.0, 0.0+i*t_length); + UpdateBuffer(0.0, 0.0+i*n_pixels); ///Put the necessary points into the diplaylist. } - glEndList(); + glEndList(); ///finilize the display list. } ///@param float delta, How much the rectangles vary in position. @@ -383,28 +379,28 @@ class gl_spider // : public virtual gl_texture { //Set up the vectors necessary for Rectangle creation. stim::vec3 Y(1.0,0.0,0.0); //orthogonal vec. - stim::vec3 pos(0.0,0.0,0.0); - stim::vec3 mag(1.0, 1.0, 1.0); - stim::vec3 dir(0.0, 0.0, 1.0); + stim::vec3 pos(0.0,0.0,0.0); //center point of a rectangle + float mag = 1.0; ///size of each rectangle + stim::vec3 dir(0.0, 0.0, 1.0); ///normal of the rectangle plane. //Set up the variable necessary for vector creation. - glNewList(dList+1, GL_COMPILE); - for(int i = 0; i < numSamplesPos; i++) + glNewList(dList+1, GL_COMPILE); ///generate a new display list. + for(int i = 0; i < numSamplesPos; i++) ///for the number of position samples { - stim::vec3 temp = uniformRandVector(); - temp = temp*delta*2.0 - delta/2.0; + stim::vec3 temp = uniformRandVector(); ///generate a random point on a plane. + temp = temp*delta*2.0 - delta/2.0; ///scale the point temp[2] = 0.0; - pV.push_back(temp); - hor = stim::rect(mag, + pV.push_back(temp); ///save the point for further use. + hor = stim::rect(mag, ///generate a rectangle with the new vector as a normal. temp, dir, ((Y.cross(d)).cross(d)) .norm()); - ver = stim::rect(mag, + ver = stim::rect(mag, ///generate another rectangle that's perpendicular the first but parallel to the cart vector. temp, dir, hor.n()); - UpdateBuffer(0.0, 0.0+i*t_length); + UpdateBuffer(0.0, 0.0+i*n_pixels); ///sample the necessary points and put them into a display list. } - glEndList(); + glEndList(); ///finilize the display list. } ///@param float delta, How much the rectangles are allowed to expand. @@ -419,35 +415,32 @@ class gl_spider // : public virtual gl_texture //Set up the vectors necessary for Rectangle creation. stim::vec3 Y(1.0, 0.0, 0.0); //orthogonal vec. - stim::vec3 pos(0.0, 0.0, 0.0); - stim::vec3 mag(1.0, 1.0, 1.0); - stim::vec3 dir(0.0, 0.0, 1.0); + stim::vec3 pos(0.0, 0.0, 0.0); //center of the future rect. + float mag = 1.0; ///size of the rectangle + stim::vec3 dir(0.0, 0.0, 1.0); ///normal of the rectangle plane. //Set up the variable necessary for vector creation. - int dim = (sqrt(numSamplesMag)-1)/2; - float min = 1.0-delta; - float max = 1.0+delta; - float step = (max-min)/(numSamplesMag-1); + float min = 1.0-delta; ///smallest size + float max = 1.0+delta; ///largers size. + float step = (max-min)/(numSamplesMag-1); ///the size variation from one rect to the next. float factor; - stim::vec3 temp(0.0,0.0,0.0); glNewList(dList+2, GL_COMPILE); - for(int i = 0; i < numSamplesMag; i++){ + for(int i = 0; i < numSamplesMag; i++){ ///for the number of position samples //Create linear index - factor = (min+step*i)*mag[0]; - temp = factor; - mV.push_back(temp); - hor = stim::rect(temp, + factor = (min+step*i)*mag; ///scaling factor + mV.push_back(factor); ///save the size factor for further use. + hor = stim::rect(factor, ///generate a rectangle with the new vector as a normal. pos, dir, ((Y.cross(d)).cross(d)) .norm()); - ver = stim::rect(temp, + ver = stim::rect(factor, ///generate another rectangle that's perpendicular the first but parallel to the cart vector. pos, dir, hor.n()); - UpdateBuffer(0.0, 0.0+i*t_length); + UpdateBuffer(0.0, 0.0+i*n_pixels); ///sample the necessary points and put them into a display list. CHECK_OPENGL_ERROR } - glEndList(); + glEndList(); ///finilize the displaylist. } ///@param float v_x x-coordinate in buffer-space, @@ -457,15 +450,15 @@ class gl_spider // : public virtual gl_texture void UpdateBuffer(float v_x, float v_y) { - stim::vec3p1; - stim::vec3p2; - stim::vec3p3; - stim::vec3p4; - p1 = hor.p(1,1); - p2 = hor.p(1,0); - p3 = hor.p(0,0); - p4 = hor.p(0,1); - glBegin(GL_QUADS); + stim::vec3p1; ///first point. + stim::vec3p2; ///second point. + stim::vec3p3; ///third point. + stim::vec3p4; ///fourth point. + p1 = hor.p(1,1); ///generate the top right point from the horizontal template. + p2 = hor.p(1,0); ///generate the bottom right point from the horizonatal template. + p3 = hor.p(0,0); ///generate the bottom left point from the horizontal template. + p4 = hor.p(0,1); ///generate the top left point from the horizonatal template. + glBegin(GL_QUADS); ///generate the Quad from the 4 points. glTexCoord3f( p1[0], p1[1], @@ -477,51 +470,51 @@ class gl_spider // : public virtual gl_texture p2[1], p2[2] ); - glVertex2f(v_x+t_length, v_y); + glVertex2f(v_x+n_pixels, v_y); glTexCoord3f( p3[0], p3[1], p3[2] ); - glVertex2f(v_x+t_length, v_y+t_length); + glVertex2f(v_x+n_pixels, v_y+n_pixels); glTexCoord3f( p4[0], p4[1], p4[2] ); - glVertex2f(v_x, v_y+t_length); - glEnd(); - - p1 = ver.p(1,1); - p2 = ver.p(1,0); - p3 = ver.p(0,0); - p4 = ver.p(0,1); - glBegin(GL_QUADS); + glVertex2f(v_x, v_y+n_pixels); + glEnd(); ///finish the quad. + + p1 = ver.p(1,1); ///generate the top right point from the vertical template. + p2 = ver.p(1,0); ///generate the bottom right point from the vertical template. + p3 = ver.p(0,0); ///generate the bottom left point from the vertical template. + p4 = ver.p(0,1); ///generate the top left point from the vertical template. + glBegin(GL_QUADS); ///generate the Quad from the 4 points. glTexCoord3f( p1[0], p1[1], p1[2] ); - glVertex2f(v_x+t_length, v_y); + glVertex2f(v_x+n_pixels, v_y); glTexCoord3f( p2[0], p2[1], p2[2] ); - glVertex2f(v_x+2.0*t_length, v_y); + glVertex2f(v_x+2.0*n_pixels, v_y); glTexCoord3f( p3[0], p3[1], p3[2] ); - glVertex2f(v_x+2.0*t_length, v_y+t_length); + glVertex2f(v_x+2.0*n_pixels, v_y+n_pixels); glTexCoord3f( p4[0], p4[1], p4[2] ); - glVertex2f(v_x+t_length, v_y+t_length); - glEnd(); + glVertex2f(v_x+n_pixels, v_y+n_pixels); + glEnd(); ///finish the quad. } @@ -539,50 +532,24 @@ class gl_spider // : public virtual gl_texture void GenerateFBO(unsigned int width, unsigned int height, GLuint &textureID, GLuint &framebufferID) { - glDeleteFramebuffers(1, &framebufferID); - glGenFramebuffers(1, &framebufferID); - glBindFramebuffer(GL_FRAMEBUFFER, framebufferID); + glDeleteFramebuffers(1, &framebufferID); ///clear the framebuffer. + glGenFramebuffers(1, &framebufferID); ///generate a clean buffer. + glBindFramebuffer(GL_FRAMEBUFFER, framebufferID); ///bind the new buffer. // int numChannels = 1; // unsigned char* texels = new unsigned char[width * height * numChannels]; - glGenTextures(1, &textureID); + glGenTextures(1, &textureID); ///generate a texture that will attach to the buffer. glBindTexture(GL_TEXTURE_2D, textureID); //Textures repeat and use linear interpolation, luminance format. - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); ///Set up the texture to repeat at edges. + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); ///Set up the texture to use Linear interpolation glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, + glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, ///Create the texture with no data. width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL); // delete[] texels; - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glBindTexture(GL_TEXTURE_2D, 0); - } - - ///@param uint width sets the width of the buffer. - ///@param uint height sets the height of the buffer. - ///Function for setting up the 2D buffer that stores the samples. - void - GenerateFBO(unsigned int width, unsigned int height) - { - glGenFramebuffers(1, &fboID); - glBindFramebuffer(GL_FRAMEBUFFER, fboID); -// int numChannels = 1; -// unsigned char* texels = new unsigned char[width * height * numChannels]; - glGenTextures(1, &texbufferID); - glBindTexture(GL_TEXTURE_2D, texbufferID); - - //Textures repeat and use linear interpolation, luminance format. - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, - width, height, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL); -// delete[] texels; - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glBindTexture(GL_TEXTURE_2D, 0); - CHECK_OPENGL_ERROR + glBindFramebuffer(GL_FRAMEBUFFER, 0); ///Bind the frontbuffer + glBindTexture(GL_TEXTURE_2D, 0); ///Unbind the texture. } @@ -614,9 +581,9 @@ class gl_spider // : public virtual gl_texture //rotate to the current direction of the spider. glRotatef(rot[0], rot[1], rot[2], rot[3]); //scale to the magnitude of the spider. - glScalef(m[0], - m[0], - m[0]); + glScalef(m, + m, + m); //get and store the current transformation matrix for later use. glGetFloatv(GL_TEXTURE_MATRIX, curTrans); cT.set(curTrans); @@ -636,36 +603,6 @@ class gl_spider // : public virtual gl_texture ///Method for controling the buffer and texture binding. ///Clears the buffer upon binding. - void - Bind() - { - glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer - glFramebufferTexture2D( - GL_FRAMEBUFFER, - GL_COLOR_ATTACHMENT0, - GL_TEXTURE_2D, - texbufferID, - 0); - glBindFramebuffer(GL_FRAMEBUFFER, fboID); - GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; - glDrawBuffers(1, DrawBuffers); - glBindTexture(GL_TEXTURE_2D, texbufferID); - glClearColor(1,1,1,1); - glClear(GL_COLOR_BUFFER_BIT); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glViewport(0,0,2.0*t_length, numSamples*t_length); - gluOrtho2D(0.0,2.0*t_length,0.0,numSamples*t_length); - glEnable(GL_TEXTURE_3D); - glBindTexture(GL_TEXTURE_3D, texID); - - CHECK_OPENGL_ERROR - } - - ///Method for controling the buffer and texture binding. - ///Clears the buffer upon binding. ///@param GLuint &textureID, texture to be bound. ///@param GLuint &framebufferID, framebuffer used for storage. ///@param int nSamples, number of rectanges to create. @@ -673,25 +610,25 @@ class gl_spider // : public virtual gl_texture Bind(GLuint &textureID, GLuint &framebufferID, int nSamples, float len = 8.0) { - glBindFramebuffer(GL_FRAMEBUFFER, framebufferID);//set up GL buffer - glFramebufferTexture2D( + glBindFramebuffer(GL_FRAMEBUFFER, framebufferID); ///Bind the framebuffer. + glFramebufferTexture2D( ///associate it with the texture GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0); - glBindFramebuffer(GL_FRAMEBUFFER, framebufferID); - GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; - glDrawBuffers(1, DrawBuffers); - glBindTexture(GL_TEXTURE_2D, textureID); - glMatrixMode(GL_PROJECTION); + glBindFramebuffer(GL_FRAMEBUFFER, framebufferID); ///Bind the framebuffer. + GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0}; ///generate the drawbuffer. + glDrawBuffers(1, DrawBuffers); ///set the drawbuffer. + glBindTexture(GL_TEXTURE_2D, textureID); ///Bind the texture passed. + glMatrixMode(GL_PROJECTION); ///clear out the draw matrices glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - glViewport(0,0,2.0*len, nSamples*len); - gluOrtho2D(0.0,2.0*len,0.0,nSamples*len); - glEnable(GL_TEXTURE_3D); - glBindTexture(GL_TEXTURE_3D, texID); + glViewport(0,0,2.0*len, nSamples*len); ///set up viewport + gluOrtho2D(0.0,2.0*len,0.0,nSamples*len); ///Set up ortho + glEnable(GL_TEXTURE_3D); + glBindTexture(GL_TEXTURE_3D, texID); ///bind the main texture (return to original state). CHECK_OPENGL_ERROR } @@ -701,32 +638,14 @@ class gl_spider // : public virtual gl_texture Unbind() { //Finalize GL_buffer - glBindTexture(GL_TEXTURE_3D, 0); - CHECK_OPENGL_ERROR - glBindTexture(GL_TEXTURE_2D, 0); + glBindTexture(GL_TEXTURE_3D, 0); ///Bind the front buffer. CHECK_OPENGL_ERROR - glBindFramebuffer(GL_FRAMEBUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); ///Bind the default GL texture. CHECK_OPENGL_ERROR - glDisable(GL_TEXTURE_3D); + glBindFramebuffer(GL_FRAMEBUFFER, 0); ///Bind the defautl framebuffer. CHECK_OPENGL_ERROR - } - - ///Makes the spider take a step. - ///starting with the current p, d, m, find the next optimal p, d, m. - ///Performs the branch detection on each step. - int - StepP() - { - Bind(); + glDisable(GL_TEXTURE_3D); ///Turn off texturing. CHECK_OPENGL_ERROR - findOptimalDirection(); - findOptimalPosition(); - findOptimalScale(); - Unbind(); - Bind(btexbufferID, bfboID, 27); - branchDetection2(); - Unbind(); - return current_cost; } @@ -741,27 +660,17 @@ class gl_spider // : public virtual gl_texture ///finds the minimum cost and sets the current_cost to that value. /// and returns the index of the template with the minimal cost. int - getCost() - { - stim::vec cost = -// stim::cuda::get_cost(texbufferID, GL_TEXTURE_2D, numSamples); - cudaDeviceSynchronize(); - current_cost = cost[1]; - return cost[0]; - } - - int getCost(cudaTextureObject_t tObj, float* result, int n) { #ifdef TIMING gpuStartTimer(); #endif stim::vec cost = - stim::cuda::get_cost(tObj, result, n, 2*t_length, t_length); + stim::cuda::get_cost(tObj, result, n, 2*n_pixels, n_pixels); ///call the cuda function with the appropriate texture buffer. #ifdef TIMING cost_time += gpuStopTimer(); #endif - current_cost = cost[1]; + current_cost = cost[1]; ///current cost. return cost[0]; } @@ -798,7 +707,7 @@ class gl_spider // : public virtual gl_texture // std::cout << "I ran this constructor" << std::endl; p = stim::vec3(0.0, 0.0, 0.0); d = stim::vec3(0.0, 0.0, 1.0); - m = stim::vec(1.0, 1.0); + m = 1.0; S = stim::vec3(1.0, 1.0, 1.0); R = stim::vec3(1.0, 1.0, 1.0); numSamples = samples; @@ -821,7 +730,7 @@ class gl_spider // : public virtual gl_texture { p = stim::vec3(pos_x, pos_y, pos_z); d = stim::vec3(dir_x, dir_y, dir_z); - m = stim::vec(mag_x, mag_x, mag_x); + m = mag_x; S = stim::vec3(1.0,1.0,1.0); R = stim::vec3(1.0,1.0,1.0); numSamples = numsamples; @@ -839,7 +748,7 @@ class gl_spider // : public virtual gl_texture { p = pos; d = dir; - m = vec(mag, mag, mag); + m = mag; S = vec3(1.0,1.0,1.0); R = vec3(1.0,1.0,1.0); numSamples = samples; @@ -852,14 +761,14 @@ class gl_spider // : public virtual gl_texture (void) { Unbind(); - glDeleteTextures(1, &texbufferID); - glDeleteBuffers(1, &fboID); - glDeleteTextures(1, &ptexbufferID); - glDeleteBuffers(1, &pfboID); - glDeleteTextures(1, &mtexbufferID); - glDeleteBuffers(1, &mfboID); - glDeleteTextures(1, &btexbufferID); - glDeleteBuffers(1, &bfboID); + glDeleteTextures(1, &direction_texID); + glDeleteBuffers(1, &direction_buffID); + glDeleteTextures(1, &position_texID); + glDeleteBuffers(1, &position_buffID); + glDeleteTextures(1, &radius_texID); + glDeleteBuffers(1, &radius_buffID); + glDeleteTextures(1, &cylinder_texID); + glDeleteBuffers(1, &cylinder_buffID); } ///@param GLuint id, texture that is going to be sampled. @@ -878,42 +787,39 @@ class gl_spider // : public virtual gl_texture network_time = 0; hit_time = 0; #endif - stepsize = 2.5; - t_length = 16.0; + stepsize = 3.0; + n_pixels = 16.0; srand(100); texID = id; - GenerateFBO(t_length*2, numSamples*t_length, texbufferID, fboID); - std::cout << numSamples << std::endl; + GenerateFBO(n_pixels*2, numSamples*n_pixels, direction_texID, direction_buffID); CHECK_OPENGL_ERROR - GenerateFBO(t_length*2, numSamplesPos*t_length, ptexbufferID, pfboID); - std::cout << numSamplesPos << std::endl; + GenerateFBO(n_pixels*2, numSamplesPos*n_pixels, position_texID, position_buffID); CHECK_OPENGL_ERROR - GenerateFBO(t_length*2, numSamplesMag*t_length, mtexbufferID, mfboID); - std::cout << numSamplesMag << std::endl; + GenerateFBO(n_pixels*2, numSamplesMag*n_pixels, radius_texID, radius_buffID); CHECK_OPENGL_ERROR - GenerateFBO(16, 216, btexbufferID, bfboID); + GenerateFBO(16, 216, cylinder_texID, cylinder_buffID); CHECK_OPENGL_ERROR t_dir.MapCudaTexture(texbufferID, GL_TEXTURE_2D); t_dir.Alloc(numSamples); - t_pos.MapCudaTexture(ptexbufferID, GL_TEXTURE_2D); + t_pos.MapCudaTexture(position_texID, GL_TEXTURE_2D); t_pos.Alloc(numSamplesPos); - t_mag.MapCudaTexture(mtexbufferID, GL_TEXTURE_2D); + t_mag.MapCudaTexture(radius_texID, GL_TEXTURE_2D); t_mag.Alloc(numSamplesMag); setMatrix(); dList = glGenLists(3); glListBase(dList); - Bind(texbufferID, fboID, numSamples, t_length); + Bind(direction_texID, direction_buffID, numSamples, n_pixels); genDirectionVectors(5*stim::PI/4); Unbind(); - Bind(ptexbufferID, pfboID, numSamplesPos, t_length); + Bind(position_texID, position_buffID, numSamplesPos, n_pixels); genPositionVectors(); Unbind(); - Bind(mtexbufferID, mfboID, numSamplesMag, t_length); + Bind(radius_texID, radius_buffID, numSamplesMag, n_pixels); genMagnitudeVectors(); Unbind(); - Bind(btexbufferID, bfboID, 27); - DrawCylinder(); + Bind(cylinder_texID, cylinder_buffID, 27); + DrawCylinder(); Unbind(); } @@ -981,22 +887,13 @@ class gl_spider // : public virtual gl_texture d[2] = z; } - ///@param stim::vec dir, the new d. - ///Sets the m vector to the input vector mag. - void - setMagnitude(stim::vec mag) - { - m[0] = mag[0]; - m[1] = mag[0]; - } ///@param float mag, size of the sampled region. ///Sets the m vector to the input mag for both templates. void setMagnitude(float mag) { - m[0] = mag; - m[1] = mag; + m = mag; } ///@param float x, voxel size in the x direction. @@ -1269,7 +1166,7 @@ class gl_spider // : public virtual gl_texture GLuint getFB() { - return bfboID; + return cylinder_buffID; } //--------------------------------------------------------------------------// @@ -1293,14 +1190,14 @@ class gl_spider // : public virtual gl_texture int Step() { - Bind(texbufferID, fboID, numSamples, t_length); + Bind(direction_texID, direction_buffID, numSamples, n_pixels); CHECK_OPENGL_ERROR findOptimalDirection(); Unbind(); - Bind(ptexbufferID, pfboID, numSamplesPos, t_length); + Bind(position_texID, position_buffID, numSamplesPos, n_pixels); findOptimalPosition(); Unbind(); - Bind(mtexbufferID, mfboID, numSamplesMag, t_length); + Bind(radius_texID, radius_buffID, numSamplesMag, n_pixels); findOptimalScale(); Unbind(); CHECK_OPENGL_ERROR @@ -1387,12 +1284,13 @@ class gl_spider // : public virtual gl_texture } ///need to return the cylinder. +///SOMETHING MIGHT BE GOING ON HERE IN GENERATE BUFFER. void DrawLongCylinder(int n = 8, int l_template = 8,int l_square = 8) { int cylLen = cL.size()-1; - GenerateFBO(n*l_square, cylLen*l_template, btexbufferID, bfboID); - Bind(btexbufferID, bfboID, cylLen, l_template*l_square/2.0); + GenerateFBO(n*l_square, cylLen*l_template, cylinder_texID, cylinder_buffID); + Bind(cylinder_texID, cylinder_buffID, cylLen, l_template*l_square/2.0); stim::cylinder cyl(cL, cM); std::vector > > p = cyl.getPoints(n); for(int i = 0; i < p.size()-1; i++) ///number of circles @@ -1636,12 +1534,9 @@ class gl_spider // : public virtual gl_texture { //starting (seed) position and magnitude. stim::vec3 spos = getPosition(); -// std::cout << "I did this" << std::endl; -// findOptimalScale(); stim::vec smag = getMagnitude(); stim::vec3 sdir = getDirection(); - Bind(); // sk.Begin(stim::OBJ_LINE); @@ -1662,7 +1557,6 @@ class gl_spider // : public virtual gl_texture int cost = Step(); if (cost > min_cost){ running = false; -// sk.End(); branchDetection2(); addToNetwork(cL, cM); std::cout << "the cost of " << cost << " > " << min_cost << std::endl; @@ -1698,7 +1592,7 @@ class gl_spider // : public virtual gl_texture } else { - h = selectObject(p, getDirection(), m[0]); + h = selectObject(p, getDirection(), m); //Have we hit something previously traced? if(h != -1){ std::cout << "I hit the fiber " << h << std::endl; @@ -1709,11 +1603,8 @@ class gl_spider // : public virtual gl_texture } else { cL.push_back(stim::vec3(p[0], p[1],p[2])); - cM.push_back(stim::vec(m[0], m[0])); -// Bind(btexbufferID, bfboID, 27); - Unbind(); + cM.push_back(m); CHECK_OPENGL_ERROR - } } } -- libgit2 0.21.4