diff --git a/stim/gl/gl_spider.h b/stim/gl/gl_spider.h index e6442db..91d28f6 100644 --- a/stim/gl/gl_spider.h +++ b/stim/gl/gl_spider.h @@ -19,6 +19,11 @@ #include #include + + +/* Technically since gl_spider inherits from gl_texture, we could + call the init with a path to an image stack, and upload + the images while creating the spider (calling init) */ namespace stim { @@ -40,76 +45,225 @@ class gl_spider : public virtual gl_texture double currentTransform[16]; using gl_texture::texID; using gl_texture::S; + using gl_texture::R; cudaArray* c_Array; cudaGraphicsResource_t resource; + GLuint fboID; GLuint texbufferID; int iter; //temporary for testing int numSamples; + + /// Method for finding the best scale for the spider. + /// changes the x, y, z size of the spider to minimize the cost + /// function. + void + findOptimalDirection() + { + genTemplate(dirVectors, 0); + int best = getCost(); + + + } + + /// Method for finding the best direction for the spider. + /// Not sure if necessary since the next position for the spider + /// will be at direction * magnitude. void findOptimalPosition() { - /* Method for finding the best direction for the spider. - Not sure if necessary since the next position for the spider - will be at direction * magnitude. */ - positionTemplate(position, direction, magnitude); + genTemplate(magVectors, 1); int best = getCost(); } + /// Method for finding the best scale for the spider. + /// changes the x, y, z size of the spider to minimize the cost + /// function. */ void findOptimalScale() { - /* Method for finding the best scale for the spider. - changes the x, y, z size of the spider to minimize the cost - function. */ genTemplate(magVectors, 2); int best = getCost(); } + + void - findOptimalDirection() + Optimize() { - /* Method for finding the best scale for the spider. - changes the x, y, z size of the spider to minimize the cost - function. */ - //getSample(position, direction, magnitude); - genTemplate(dirVectors, 0); - int best = getCost(); - //getSample(position, direction, magnitude); - //int best = getCost(); + /*find the optimum direction and scale */ } + + +//--------------------------------------------------------------------------// +//---------------------TEMPLATE CREATION METHODS----------------------------// +//--------------------------------------------------------------------------// + + ///@param solidAngle, the size of the arc to sample. + ///Method for populating the vector arrays with sampled vectors. + ///uses the default direction vector <0,0,1> void - Optimize() + genDirectionVectors(float solidAngle = M_PI/2) { - /*find the optimum direction and scale */ + + vec d_s = direction.cart2sph().norm(); + vec temp; + int dim = (sqrt(numSamples)-1)/2; + float p0 = M_PI/3; + float dt = solidAngle/(2.0 * (dim + 1)); + float dp = p0/(2.0*(dim + 1)); + + for(int i = -dim; i <= dim; i++){ + for(int j = -dim; j <= dim; j++){ + //Create linear index + + temp[0] = d_s[0]; //rotate vector + temp[1] = d_s[1]+dt*i; + temp[2] = d_s[2]+dp*j; + + temp = (temp.sph2cart()).norm(); //back to cart + dirVectors.push_back(temp); + } + } } - ///@param width sets the width of the buffer. - ///@param height sets the height of the buffer. - ///Function for setting up the 2D buffer that stores the samples. + ///@param solidAngle, the size of the arc to sample. + ///Method for populating the buffer with the sampled texture. + ///uses the default vector <0,0,0> void - GenerateFBO(unsigned int width, unsigned int height) + genPositionVectors(float delta = 0.2) { - 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); - 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, texels); - delete[] texels; - glBindFramebuffer(GL_FRAMEBUFFER, 0); - glBindTexture(GL_TEXTURE_2D, 0); + + vec temp; + int dim = (sqrt(numSamples)-1)/2; + stim::rect samplingPlane = + stim::rect(magnitude[0]*delta, position, direction); + float step = 1.0/(dim); + //Loop over the samples, keeping the original position sample + //in the center of the resulting texture. + + int idx; + for(int i = -dim; i <= dim; i++){ + for(int j = -dim; j <= dim; j++){ + //Create linear index + + temp = samplingPlane.p( + 0.5+step*i, + 0.5+step*j + ); + posVectors.push_back(temp); + } + } + } + + ///@param solidAngle, the size of the arc to sample. + ///Method for populating the buffer with the sampled texture. + ///uses the default magnitude <1,1,0> + void + genMagnitudeVectors(float delta = 0.5) + { + + int dim = (sqrt(numSamples)-1)/2; + float min = 1.0-delta; + float max = 1.0+delta; + float step = (max-min)/(numSamples-1); + float factor; + vec temp; + + for(int i = 0; i < numSamples; i++){ + //Create linear index + factor = (min+step*i)*magnitude[0]; + temp[0] = factor; + temp[1] = factor; + magVectors.push_back(temp); + } + } + ///@param vector of stim::vec in that stores all of the samplable vectors. + ///@param type, one of three operations, 0 for Direction vectors + /// 1 for Position, 2 for Magnitude. + ///Function for filling the buffer up with the data based on the vectors + ///Each vector represents a two rectangular templates. + ///Loops through all of the vectors and transfers rect. associated with it + ///Into buffer-space. + void + genTemplate(std::vector > in, int type) + { + float x = 0.0; + vec Y(1.0,0.0,0.0); + vec pos(0.0,0.0,0.0); + vec mag(1.0, 1.0, 1.0); + switch(type) { + case 0: //Direction + Bind(); + positionTemplate(); ///this can be placed anywhere + ///as long as the GL_TEXTURE + ///is not cleared after this call. + + for(int i = 0; i < in.size(); i++) + { + if(cos(Y.dot(in[i]))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} + else{Y[0] = 1.0; Y[1] = 0.0;} + + hor = stim::rect(mag, + pos, in[i], + ((Y.cross(in[i])).cross(in[i])).norm()); + ver = stim::rect(mag, + pos, in[i], + hor.n()); + UpdateBuffer(x, x+i*10.0); + } + //getSample(); + Unbind(); + break; + case 1: //Position + Bind(); + positionTemplate(); + if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} + else{Y[0] = 1.0; Y[1] = 0.0;} + + for(int i = 0; i < in.size(); i++) + { + hor = stim::rect(magnitude, + in[i], direction, + ((Y.cross(direction)).cross(direction)) + .norm()); + ver = stim::rect(magnitude, + in[i], direction, + hor.n()); + UpdateBuffer(x, x+i*10.0); + } + Unbind(); + break; + case 2: //Scale + Bind(); + positionTemplate(); + if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} + else{Y[0] = 1.0; Y[1] = 0.0;} + + for(int i = 0; i < in.size(); i++) + { + hor = stim::rect(in[i], + position, direction, + ((Y.cross(direction)).cross(direction)) + .norm()); + ver = stim::rect(in[i], + position, direction, + hor.n()); + UpdateBuffer(x, x+i*10.0); + } + Unbind(); + break; + default: + std::cout << "unknown case have been passed" + << std::endl; + break; + } + Unbind(); + CHECK_OPENGL_ERROR } - ///@param v_x x-coordinate in buffer-space, ///@param v_y y-coordinate in buffer-space. ///Samples the texturespace and places a sample in the provided coordinates @@ -186,84 +340,35 @@ class gl_spider : public virtual gl_texture CHECK_OPENGL_ERROR } - ///@param vector of stim::vec in that stores all of the samplable vectors. - ///@param type, one of three operations, 0 for Direction vectors - /// 1 for Position, 2 for Magnitude. - ///Function for filling the buffer up with the data based on the vectors - ///Each vector represents a two rectangular templates. - ///Loops through all of the vectors and transfers rect. associated with it - ///Into buffer-space. - void - genTemplate(std::vector > in, int type) - { - float x = 0.0; - vec Y(1.0,0.0,0.0); - vec pos(0.0,0.0,0.0); - vec mag(1.0, 1.0, 1.0); - switch(type) { - case 0: //Direction - Bind(); - positionTemplate(); - for(int i = 0; i < in.size(); i++) - { - if(cos(Y.dot(in[i]))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} - else{Y[0] = 1.0; Y[1] = 0.0;} - hor = stim::rect(mag, - pos, in[i], - ((Y.cross(in[i])).cross(in[i])).norm()); - ver = stim::rect(mag, - pos, in[i], - hor.n()); - // std::cout<< (x+i*10.0) << std::endl; - UpdateBuffer(x, x+i*10.0); - } - //getSample(); - Unbind(); - break; - case 1: //Position - Bind(); - if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} - else{Y[0] = 1.0; Y[1] = 0.0;} - for(int i = 0; i < in.size(); i++) - { - hor = stim::rect(magnitude, in[i], direction, - ((Y.cross(direction)).cross(direction)) - .norm()); - ver = stim::rect(magnitude, in[i], direction, - hor.n()); - //UpdateBuffer(x, x+i*10.0); - std::cout<< "Hmmm" << std::endl; - } - Unbind(); - break; - case 2: //Scale - Bind(); - if(cos(Y.dot(direction))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} - else{Y[0] = 1.0; Y[1] = 0.0;} +//--------------------------------------------------------------------------// +//--------------------------------GL METHODS--------------------------------// +//--------------------------------------------------------------------------// - for(int i = 0; i < in.size(); i++) - { - hor = stim::rect(in[i], position, direction, - ((Y.cross(direction)).cross(direction)) - .norm()); - ver = stim::rect(in[i], position, direction, - hor.n()); - //UpdateBuffer(x, x+i*10.0); - std::cout<< "Hmmm" << std::endl; - } - Unbind(); - break; - default: - std::cout << "unknown case have been passed" - << std::endl; - break; - } - Unbind(); - CHECK_OPENGL_ERROR + ///@param width sets the width of the buffer. + ///@param 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); + 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, texels); + delete[] texels; + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); } - + ///Method for controling the buffer and texture binding in order to properly ///do the render to texture. void @@ -306,86 +411,51 @@ class gl_spider : public virtual gl_texture glBindTexture(GL_TEXTURE_2D, 0); } - ///@param solidAngle, the size of the arc to sample. - ///Method for populating the vector arrays with sampled vectors. - ///uses the default direction vector <0,0,1> - void - genDirectionVectors(float solidAngle = M_PI/2) - { - - vec d_s = direction.cart2sph().norm(); - //dirVectors.resize(numSamples); //THIS LINE ADDS AN ERROR - vec temp; - int dim = (sqrt(numSamples)-1)/2; - float p0 = M_PI/3; - float dt = solidAngle/(2.0 * (dim + 1)); - float dp = p0/(2.0*(dim + 1)); - - for(int i = -dim; i <= dim; i++){ - for(int j = -dim; j <= dim; j++){ - //Create linear index - - temp[0] = d_s[0]; //rotate vector - temp[1] = d_s[1]+dt*i; - temp[2] = d_s[2]+dp*j; - - temp = (temp.sph2cart()).norm(); //back to cart - dirVectors.push_back(temp); - } - } - } - - ///@param solidAngle, the size of the arc to sample. - ///Method for populating the buffer with the sampled texture. - ///uses the default vector <0,0,0> - void - genPositionVectors(float delta = 0.2) + ///Method for using the gl manipulation to alighn templates from + ///Template space (-0.5 0.5) to Texture space (0.0, 1.0), + ///Based on the position of the spider in real space (arbitrary). + void positionTemplate() { + stim::vec rot = getRotation(direction); + glMatrixMode(GL_TEXTURE); + glLoadIdentity(); - vec temp; - int dim = (sqrt(numSamples)-1)/2; - stim::rect samplingPlane = - stim::rect(magnitude[0]*delta, position, direction); - float step = 1.0/(dim); - //Loop over the samples, keeping the original position sample - //in the center of the resulting texture. + glTranslatef(position[0]/512/S[1], + position[1]/512/S[2], + position[2]/426/S[3]); + glScalef(magnitude[0]/512/S[1], + magnitude[1]/512/S[2], + magnitude[0]/426/S[3]); + glRotatef(rot[0], rot[1], rot[2], rot[3]); - int idx; - for(int i = -dim; i <= dim; i++){ - for(int j = -dim; j <= dim; j++){ - //Create linear index +/* + glTranslatef(position[0], + position[1], + position[2]); + glScalef(magnitude[0], + magnitude[1], + magnitude[0]); + glRotatef(rot[0], rot[1], rot[2], rot[3]); + glScalef(1/512/0.6, 1/512/0.6, 1/426/2.0); //this does not work +*/ - temp = samplingPlane.p( - 0.5+step*i, - 0.5+step*j - ); - posVectors.push_back(temp); + glGetDoublev(GL_TEXTURE_MATRIX, currentTransform); + int c = 0; + for(int i = 0; i < 16; i++){ + if(c<3){ + std::cerr << "[" << currentTransform[i] << "]" << " "; + c++; + } else { + std::cerr << "[" << currentTransform[i] << "]" << "\n"; + c = 0; } } + CHECK_OPENGL_ERROR + glMatrixMode(GL_MODELVIEW); } + + - ///@param solidAngle, the size of the arc to sample. - ///Method for populating the buffer with the sampled texture. - ///uses the default magnitude <1,1,0> - void - genMagnitudeVectors(float delta = 0.5) - { - - int dim = (sqrt(numSamples)-1)/2; - float min = 1.0-delta; - float max = 1.0+delta; - float step = (max-min)/(numSamples-1); - float factor; - vec temp; - - for(int i = 0; i < numSamples; i++){ - //Create linear index - factor = (min+step*i)*magnitude[0]; - temp[0] = factor; - temp[1] = factor; - magVectors.push_back(temp); - } - } //--------------------------------------------------------------------------// //--------------------------------CUDA METHODS------------------------------// @@ -428,10 +498,14 @@ class gl_spider : public virtual gl_texture } public: - stim::rect hor; stim::rect ver; +//--------------------------------------------------------------------------// +//-----------------------------CONSTRUCTORS---------------------------------// +//--------------------------------------------------------------------------// + + ///Default Constructor gl_spider () @@ -441,8 +515,10 @@ class gl_spider : public virtual gl_texture setMagnitude(1.0); //numSamples = 1089; numSamples = 9; - std::cout << "I did this" < } ///@param GLuint id texture that is going to be sampled. ///Attached the spider to the texture with the given GLuint ID. - ///Samples in the default direction acting as the init method. + ///Samples in the default direction acting as the init method. + ///Also acts an init. void attachSpider(GLuint id) { @@ -478,19 +555,9 @@ class gl_spider : public virtual gl_texture genTemplate(dirVectors, 0); } - ///temporary Method necessary for visualization and testing. - void - Update() - { - vec Y(1.0,0.0,0.0); - if(cos(Y.dot(direction))< 0.087){ - Y[0] = 0.0; Y[1] = 1.0;} - hor = stim::rect(magnitude, position, direction.norm(), - ((Y.cross(direction)).cross(direction)).norm()); - ver = stim::rect(magnitude, position, direction.norm(), - hor.n()); - } - +//--------------------------------------------------------------------------// +//-----------------------------ACCESS METHODS-------------------------------// +//--------------------------------------------------------------------------// ///Returns the position vector. vec getPosition() @@ -532,37 +599,6 @@ class gl_spider : public virtual gl_texture position[2] = z; } - void positionTemplate(vec pos, vec dir, vec mag) - { - stim::vec rot = getRotation(dir); - glMatrixMode(GL_TEXTURE); - glLoadIdentity(); - glTranslatef(position[0]/512/S[1], - position[1]/512/S[2], - position[2]/426/S[3]); - glScalef(magnitude[0]/512/S[1], - magnitude[1]/512/S[2], - magnitude[0]/426/S[3]); - glRotatef(rot[0], rot[1], rot[2], rot[3]); - glGetDoublev(GL_TEXTURE_MATRIX, currentTransform); - int c = 0; - for(int i = 0; i < 16; i++){ - if(c<3){ - std::cerr << "[" << currentTransform[i] << "]" << " "; - c++; - } else { - std::cerr << "[" << currentTransform[i] << "]" << "\n"; - c = 0; - } - } - CHECK_OPENGL_ERROR - glMatrixMode(GL_MODELVIEW); - } - - void positionTemplate() - { - positionTemplate(position, direction, magnitude); - } ///@param vector dir, the new direction. ///Sets the direction vector to input vector dir. void @@ -632,6 +668,24 @@ class gl_spider : public virtual gl_texture return fboID; } +//--------------------------------------------------------------------------// +//-----------------------------TEMPORARY METHODS----------------------------// +//--------------------------------------------------------------------------// + + ///temporary Method necessary for visualization and testing. + void + Update() + { + vec Y(1.0,0.0,0.0); + if(cos(Y.dot(direction))< 0.087){ + Y[0] = 0.0; Y[1] = 1.0;} + hor = stim::rect(magnitude, position, direction.norm(), + ((Y.cross(direction)).cross(direction)).norm()); + ver = stim::rect(magnitude, position, direction.norm(), + hor.n()); + } + + void Step() { diff --git a/stim/gl/gl_texture.h b/stim/gl/gl_texture.h index 86a1692..6a1ac36 100644 --- a/stim/gl/gl_texture.h +++ b/stim/gl/gl_texture.h @@ -207,7 +207,12 @@ class gl_texture : public virtual image_stack return ptr; } - + stim::vec + getSize() + { + stim::vec size(R[1], R[2], R[3]); + return size; + } }; } -- libgit2 0.21.4