diff --git a/stim/cuda/cost.h b/stim/cuda/cost.h index 6e24333..c89e392 100644 --- a/stim/cuda/cost.h +++ b/stim/cuda/cost.h @@ -2,8 +2,6 @@ #include #include #include -//#include "cuPrintf.cu" -//#include "cuPrintf.cuh" #include #include "../visualization/colormap.h" #include @@ -11,9 +9,6 @@ #define DIM_Y 10890 #define DIM_X 20 typedef unsigned char uchar; -//surface texOut; ///// maybe just do a normal array instead of a surface. - //we may not need a surface at all. -//texture texTemplate texture texIn; float *result; float* v_dif; @@ -66,11 +61,9 @@ void get_diff (float *result) float valIn = tex2D(texIn, x, y)/255.0; float valTemp = Template(x); result[idx] = abs(valIn-valTemp); - //result[idx] = abs(valTemp); // #if __CUDA_ARCH__>=200 // printf("Value is : %f\n and the result is : %f\n", valIn, result[idx]); // #endif - //cuPrintf("Value is : %f\n and the result is : %f\n", valIn, result[idx]); } @@ -128,7 +121,6 @@ int get_cost(cudaGraphicsResource_t src, int inter) initArray(src); dim3 grid(20, 10890); dim3 block(1, 1); - //texIn.normalized = 1; get_diff <<< grid, block >>> (result); stim::gpu2image(result, "test.bmp", 20,10890,0,1); for (int i = 0; i < 1089; i++){ @@ -136,19 +128,11 @@ int get_cost(cudaGraphicsResource_t src, int inter) if(output[i] <= mini){ mini = output[i]; idx = i; -// if(!testing) - -// testing = true; - } //float* out = (float*) malloc(sizeof(float)); + } } name << "sample_" << inter << "_" << idx << ".bmp"; output[idx] = get_sum(result+(20*10*idx)); stim::gpu2image(v_dif, name.str(), 20,10,0,1); - std::cout << output[0] << std::endl; - std::cout << output[100] << std::endl; - std::cout << output[500] << std::endl; - std::cout << output[1000] << std::endl; - std::cout << idx << std::endl; cleanUP(src); return idx; } diff --git a/stim/gl/gl_spider.h b/stim/gl/gl_spider.h index b46205b..d6ee3f2 100644 --- a/stim/gl/gl_spider.h +++ b/stim/gl/gl_spider.h @@ -14,6 +14,7 @@ #include "../math/rect.h" #include "../cuda/cost.h" #include "../cuda/glbind.h" +#include #include #include @@ -33,8 +34,11 @@ class gl_spider : public virtual gl_texture stim::vec magnitude; //magnitude of the direction vector. //mag[0] = length. //mag[1] = width. + std::vector > dirVectors; + std::vector > posVectors; + std::vector > magVectors; using gl_texture::texID; - //using image_stack::S; + using gl_texture::S; cudaArray* c_Array; //void** devPtr; //size_t size; @@ -49,6 +53,8 @@ class gl_spider : public virtual gl_texture /* 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. */ + genTemplate(posVectors, 1); + int best = getCost(); } void @@ -57,20 +63,20 @@ class gl_spider : public virtual gl_texture /* 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, 1); + int best = getCost(); } void - Evaluate() - { - /* Uses uniform sampler2D in order to take a difference between - the colors of two textures. 1st texture is the spider template, - the 2nd is the location of the spider's overlap with the - gl_template - - does the spider need to track it's location? Prob not since - position can be set with gl_texture coordinates */ - + findOptimalDirection() + { + /* Method for finding the best scale for the spider. + changes the x, y, z size of the spider to minimize the cost + function. */ + genTemplate(dirVectors, 1); + int best = getCost(); } + void Optimize() @@ -120,6 +126,10 @@ class gl_spider : public virtual gl_texture p4 = hor.p(0,1); glBegin(GL_QUADS); glTexCoord3f( + // p1[0]/S[1], + // p1[1]/S[2], + // p1[2]/S[3] + // ); p1[0], p1[1], p1[2] @@ -127,6 +137,9 @@ class gl_spider : public virtual gl_texture //glVertex2f(0.0,0.0); glVertex2f(v_x,v_y); glTexCoord3f( + // p2[0]/S[1], + // p2[1]/S[2], + // p2[2]/S[3] p2[0], p2[1], p2[2] @@ -134,6 +147,9 @@ class gl_spider : public virtual gl_texture //glVertex2f(1.0, 0.0); glVertex2f(v_x+len, v_y); glTexCoord3f( + // p2[0]/S[1], + // p2[1]/S[2], + // p2[2]/S[3] p3[0], p3[1], p3[2] @@ -141,6 +157,9 @@ class gl_spider : public virtual gl_texture //glVertex2f(1.0, 2.0); glVertex2f(v_x+len, v_y+len); glTexCoord3f( + // p4[0]/S[1], + // p4[1]/S[2], + // p4[2]/S[3] p4[0], p4[1], p4[2] @@ -154,6 +173,9 @@ class gl_spider : public virtual gl_texture p4 = ver.p(0,1); glBegin(GL_QUADS); glTexCoord3f( + // p1[0]/S[1], + // p1[1]/S[2], + // p1[2]/S[3] p1[0], p1[1], p1[2] @@ -161,6 +183,9 @@ class gl_spider : public virtual gl_texture //glVertex2f(1.0, 0.0); glVertex2f(v_x+len, v_y); glTexCoord3f( + // p2[0]/S[1], + // p2[1]/S[2], + // p2[2]/S[3] p2[0], p2[1], p2[2] @@ -168,6 +193,9 @@ class gl_spider : public virtual gl_texture //glVertex2f(2.0, 0.0); glVertex2f(v_x+2*len, v_y); glTexCoord3f( + // p3[0]/S[1], + // p3[1]/S[2], + // p3[2]/S[3] p3[0], p3[1], p3[2] @@ -175,6 +203,9 @@ class gl_spider : public virtual gl_texture //glVertex2f(2.0, 2.0); glVertex2f(v_x+2*len, v_y+len); glTexCoord3f( + // p4[0]/S[1], + // p4[1]/S[2], + // p4[2]/S[3] p4[0], p4[1], p4[2] @@ -185,35 +216,60 @@ class gl_spider : public virtual gl_texture } void - UpdatePlanes(float v_x, float v_y, vec vctr, int type = 0) + genTemplate(std::vector > in, int type = 0) { + float x = 0.0; vec Y(1.0,0.0,0.0); - if(cos(Y.dot(vctr))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} + Bind(); switch(type) { - case 0: - hor = stim::rect(magnitude, position, vctr.norm(), - ((Y.cross(vctr)).cross(vctr)).norm()); - ver = stim::rect(magnitude, position, vctr.norm(), - hor.n()); + case 0: //Direction + 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(magnitude, + position, in[i], + ((Y.cross(in[i])).cross(in[i])).norm()); + ver = stim::rect(magnitude, + position, in[i], + hor.n()); + UpdateBuffer(x, x+i*10.0); + } break; - case 1: - hor = stim::rect(magnitude, vctr, direction, - ((Y.cross(direction)).cross(direction)).norm()); - ver = stim::rect(magnitude, vctr, direction, - hor.n()); + case 1: //Position + 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); + } break; - case 2: - hor = stim::rect(vctr, position, direction, - ((Y.cross(direction)).cross(direction)).norm()); - ver = stim::rect(vctr, position, direction, - hor.n()); - break; + case 2: //Scale + 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()); + } + break; default: std::cout << "unknown case have been passed" << std::endl; break; } - UpdateBuffer(v_x, v_y); + Unbind(); } /* @@ -247,6 +303,9 @@ class gl_spider : public virtual gl_texture glBindTexture(GL_TEXTURE_3D, texID); } + /* + Method for Unbinding all of the texture resources + */ void Unbind() { @@ -256,83 +315,46 @@ class gl_spider : public virtual gl_texture glBindFramebuffer(GL_FRAMEBUFFER,0); glBindTexture(GL_TEXTURE_2D, 0); } + /* Method for populating the buffer with the sampled texture. usually uses the default direction vector and then */ void - sampleDirection(int numSamples = 1089, float solidAngle = M_PI) + genDirectionVectors(int numSamples = 1089, float solidAngle = M_PI/1.5) { - //ofstream file; - //file.open("samples.txt", std::ios_base::app); - float samples[numSamples][3]; //Set up the variables - //necessary for sample generation vec d_s = direction.cart2sph(); + dirVectors.resize(1089); vec temp; int dim = (sqrt(numSamples)-1)/2; - //std::cout << dim << std::endl; - float y_0 = 0.0; float p0 = M_PI/3; float dt = solidAngle/(1.0 * dim); float dp = p0/(1.0*dim); - Bind(); - //file << "Step: Direction" << "\n"; - //file << "iteration: " << iter << "\n"; - //file << "starting pos and dir:" << "[" << position[0] << "," < next(samples[nxt][0], samples[nxt][1], samples[nxt][2]); - //next[0] = samples[nxt][0]; - //next[1] = samples[nxt][1]; - //next[2] = samples[nxt][2]; - next.norm(); - //file << "next direction" << "[" << next[0] << "," << next[1] << "," << next[2] << "]\n\n"; - setPosition(position[0] + next[0]*magnitude[0]/2, - position[1]+next[1]*magnitude[0]/2, - position[2]+next[2]*magnitude[0]/2); - setDirection(next[0], next[1], next[2]); - //file.close(); - setDirection(next); - //file << "ending pos and dir:" << "[" << position[0] << "," < temp; int dim = (sqrt(numSamples)-1)/2; - float y_0 = 0.0; //location of the first sample in the buffer stim::rect samplingPlane = stim::rect(magnitude[0]*delta, position, direction); float step = 1.0/(dim); - Bind(); //Loop over the samples, keeping the original position sample //in the center of the resulting texture. @@ -340,52 +362,34 @@ class gl_spider : public virtual gl_texture for(int i = -dim; i <= dim; i++){ for(int j = -dim; j <= dim; j++){ //Create linear index - idx = (i+dim)*(dim*2+1) + (j+dim); temp = samplingPlane.p( 0.5+step*i, 0.5+step*j ); - - samples[idx][0] = temp[0]; //save sample vector - samples[idx][1] = temp[1]; - samples[idx][2] = temp[2]; - - UpdatePlanes(0.0, y_0+(idx)*10, temp, 1); + dirVectors.push_back(temp); } } - Unbind(); - int nxt = getCost(); - setPosition(samples[nxt][0], samples[nxt][1], samples[nxt][2]); } void - sampleMagnitude(int numSamples = 1089, float delta = 0.5) + genMagnitudeVectors(int numSamples = 1089, float delta = 0.5) { - ofstream file; - file.open("samples.txt", std::ios_base::app); - float samples[numSamples]; //Set up the variables int dim = (sqrt(numSamples)-1)/2; - float y_0 = 0.0; //location of the first sample in the buffer float min = 1.0-delta; float max = 1.0+delta; float step = (max-min)/(numSamples-1); float factor; vec temp; - Bind(); for(int i = 0; i < numSamples; i++){ - //Create linear index - factor = (min+step*i)*magnitude[0]; - samples[i] = factor; //save sample vector - stim::vec temp(factor); - UpdatePlanes(0.0, y_0+(i)*10, temp, 2); + //Create linear index + factor = (min+step*i)*magnitude[0]; + temp[0] = factor; + temp[1] = factor; + magVectors.push_back(temp); } - Unbind(); - int nxt = getCost(); - setMagnitude(samples[nxt]); - file << position[0] << "," < stim::rect hor; stim::rect ver; - //Default Constructor + ///Default Constructor gl_spider () { setPosition(0.0,0.0,0.0); setDirection(0.0,0.0,1.0); - setMagnitude(0.03); + setMagnitude(1.0); } - //temporary constructor for convenience, will be removed in further updates. + ///temporary constructor for convenience, will be removed in further updates. gl_spider (float pos_x, float pos_y, float pos_z, float dir_x, float dir_y, float dir_z, float mag_x) { - setPosition(pos_x, pos_y, pos_z); - setDirection(dir_x, dir_y, dir_z); - setMagnitude(mag_x); - } - - //Attached the spider to the texture with the given GLuint ID. - //Samples in the default direction acting as the init method. + //setPosition(pos_x, pos_y, pos_z); + //setDirection(dir_x, dir_y, dir_z); + //setMagnitude(mag_x); + } + ///@param GLuint id texture that is going to be sampled. + ///@param numSamples number of samples per operation. + ///Attached the spider to the texture with the given GLuint ID. + ///Samples in the default direction acting as the init method. void attachSpider(GLuint id, int numSamples = 1089) { texID = id; GenerateFBO(20, numSamples*10); - sampleDirection(); + //sampleDirection(); + genDirectionVectors(); + gl_texture::setDims(0.6, 0.6, 1.0); + //gl_texture::setDims(1.0, 1.0,1.0); } - //temporary Method necessary for visualization and testing. + ///temporary Method necessary for visualization and testing. void Update() { @@ -475,35 +483,39 @@ class gl_spider : public virtual gl_texture hor.n()); } - //Returns the position vector. + ///Returns the position vector. vec getPosition() { return position; } - //Returns the direction vector. + ///Returns the direction vector. vec getDirection() { return direction; } - //Returns the magnitude vector. + ///Returns the magnitude vector. vec getMagnitude() { return magnitude; } - //Sets the position vector to input vector pos + ///@param vector pos, the new position. + ///Sets the position vector to input vector pos. void setPosition(vec pos) { position = pos; } - - //Sets the position vector to the input float coordinates x,y,z + + ///@param x x-coordinate. + ///@param y y-coordinate. + ///@param z z-coordinate. + ///Sets the position vector to the input float coordinates x,y,z. void setPosition(float x, float y, float z) { @@ -513,14 +525,18 @@ class gl_spider : public virtual gl_texture } - //Sets the direction vector to input vector dir + ///@param vector dir, the new direction. + ///Sets the direction vector to input vector dir. void setDirection(vec dir) { direction = dir; } - //Sets the direction vector to the input float coordinates x,y,z + ///@param x x-coordinate. + ///@param y y-coordinate. + ///@param z z-coordinate. + ///Sets the direction vector to the input float coordinates x,y,z. void setDirection(float x, float y, float z) { @@ -528,8 +544,9 @@ class gl_spider : public virtual gl_texture direction[1] = y; direction[2] = z; } - - //Sets the magnitude vector to the input vector mag. + + ///@param vector dir, the new direction. + ///Sets the magnitude vector to the input vector mag. void setMagnitude(vec mag) { @@ -537,6 +554,8 @@ class gl_spider : public virtual gl_texture magnitude[1] = mag[0]; } + ///@param mag size of the sampled region. + ///Sets the magnitude vector to the input mag for both templates. void setMagnitude(float mag) { @@ -544,7 +563,7 @@ class gl_spider : public virtual gl_texture magnitude[1] = mag; } - //temporary method for visualization. + ///temporary method for visualization. GLuint getFB() { @@ -564,9 +583,9 @@ class gl_spider : public virtual gl_texture /* Method for finding the best direction for the spider. Uses the camera to rotate. Then Calls Evaluate to find new cost. */ - sampleDirection(); - samplePosition(); - sampleMagnitude(); + //sampleDirection(); + //samplePosition(); + //sampleMagnitude(); } /* Method for initializing the cuda devices, necessary only diff --git a/stim/gl/gl_texture.h b/stim/gl/gl_texture.h index d99164c..89fcbad 100644 --- a/stim/gl/gl_texture.h +++ b/stim/gl/gl_texture.h @@ -31,13 +31,10 @@ template class gl_texture : public virtual image_stack { private: - /* - Method: setTextureType - Inputs: - Outputs: - Sets the internal texture_type, based on the data - size. Either 3D, 2D, 1D textures. - */ + + ///Method: setTextureType + /// Sets the internal texture_type, based on the data + /// size. Either 3D, 2D, 1D textures. void setTextureType() @@ -54,28 +51,22 @@ class gl_texture : public virtual image_stack GLuint texID; //OpenGL object GLenum texture_type; //1D, 2D, 3D using image_stack::R; + using image_stack::S; using image_stack::ptr; using image_stack::samples; public: - /* - Method: Basic Constructor - Inputs: - Outputs: - Creates an instance of the gl_texture object. - */ + ///Method: Basic Constructor + /// Creates an instance of the gl_texture object. gl_texture() { } - /* - Method: Path Constructor - Inputs: string file_path - Outputs: - Creates an instance of the gl_texture object with a path to the data. - */ + ///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. gl_texture(std::string file_path) { @@ -84,13 +75,34 @@ class gl_texture : public virtual image_stack setTextureType(); } - /* - Method: setPath - Inputs:string file_Path - Outputs: - sets the protected path variable of an instance of the gl_texture class - to the method input. - */ + + ///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 + /// Sets the dimenstions of the voxels. + void + setDims(float x, float y, float z) + { + S[1] = x; + S[2] = y; + S[3] = z; + } + + ///Method:getDims + /// get the dimenstions of the voxels. + + vec + getDims() + { + vec dims(S[1], S[2], S[3]); + 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) @@ -100,12 +112,9 @@ class gl_texture : public virtual image_stack setTextureType(); } - /* - Method: getPath - Inputs: - Outputs: string path - Returns the path associated with an instance of the gl_texture class. - */ + ///Method: getPath + ///Outputs: string path + /// Returns the path associated with an instance of the gl_texture class. std::string getPath() @@ -113,27 +122,20 @@ class gl_texture : public virtual image_stack return path; } - /* - Method: getTexture - Inputs: - Outputs: GLuint texID - Returns the id of the texture create by/associated with the - instance of the gl_texture class. - */ + ///Method: getTexture + ///Outputs: GLuint texID + /// Returns the id of the texture create by/associated with the + /// instance of the gl_texture class. GLuint getTexture() { return texID; } - - /* - Method: createTexture - Inputs: - Outputs: - Creates a texture and from the data located at and - assigns that texture to texID - */ + + ///Method: createTexture + /// Creates a texture and from the loaded data and + /// assigns that texture to texID //TO DO :::: 1D textures //TO DO:::add methods for handling the cases of T // and convert them to GL equivalent. @@ -154,11 +156,11 @@ class gl_texture : public virtual image_stack { case GL_TEXTURE_3D: glTexParameteri(texture_type, - GL_TEXTURE_WRAP_S, GL_REPEAT); + GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(texture_type, - GL_TEXTURE_WRAP_T, GL_REPEAT); + GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(texture_type, - GL_TEXTURE_WRAP_R, GL_REPEAT); + GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glTexImage3D(texture_type, 0, // GL_RGB16, @@ -190,15 +192,8 @@ class gl_texture : public virtual image_stack break; } } - /* - Temporary methods for debugging and testing are below. - Self-explanatory. - */ - stim::vec - getDims() - { - return R; - } + ///Temporary methods for debugging and testing are below. + ///Self-explanatory. T* getData() diff --git a/stim/grids/grid.h b/stim/grids/grid.h index b4fa734..c8596e2 100644 --- a/stim/grids/grid.h +++ b/stim/grids/grid.h @@ -21,6 +21,7 @@ class grid{ protected: stim::vec R; //elements in each dimension + stim::vec S; T* ptr; //pointer to the data (on the GPU or CPU) ///Return the total number of values in the binary file @@ -63,6 +64,12 @@ public: init(); } + void + setDim(stim::vec s) + { + S = s; + } + ///Constructor used to specify the grid size as a set of parameters /// @param X0... is a list of values describing the grid size along each dimension @@ -156,6 +163,7 @@ public: ptr[p] = value; } + ///Outputs grid data as a string std::string str(){ diff --git a/stim/grids/image_stack.h b/stim/grids/image_stack.h index 3de2a67..ddc0895 100644 --- a/stim/grids/image_stack.h +++ b/stim/grids/image_stack.h @@ -17,6 +17,7 @@ class image_stack : public virtual stim::grid{ enum image_type {stimAuto, stimMono, stimRGB, stimRGBA}; protected: + using stim::grid::S; using stim::grid::R; using stim::grid::ptr; using stim::grid::samples; @@ -83,7 +84,19 @@ public: I.save(file_name); } - + ///Sets the dimensions of the image in each direction + ///Voxel-size. + /// @param x size in the x direction + /// @param y size in the y direction + /// @param z size in the z direction + void + set_dim(float x, float y, float z) + { + S[0] = 1; + S[1] = x; + S[2] = y; + S[3] = z; + } ///Saves the entire stack to a set of images /// @param file_mask is the mask describing how the file names will be saved (ex. image????.bmp) -- libgit2 0.21.4