diff --git a/stim/gl/gl_spider.h b/stim/gl/gl_spider.h index 2d2cb43..b46205b 100644 --- a/stim/gl/gl_spider.h +++ b/stim/gl/gl_spider.h @@ -41,7 +41,7 @@ class gl_spider : public virtual gl_texture cudaGraphicsResource_t resource; GLuint fboID; GLuint texbufferID; - int iter = 0; + int iter = 0; //temporary for testing void findOptimalPosition() @@ -185,39 +185,45 @@ class gl_spider : public virtual gl_texture } void - Update(float v_x, float v_y, vec dir) + UpdatePlanes(float v_x, float v_y, vec vctr, int type = 0) { vec Y(1.0,0.0,0.0); - if(cos(Y.dot(dir))< 0.087){ - Y[0] = 0.0; Y[1] = 1.0;} - hor = stim::rect(magnitude, position, dir.norm(), - ((Y.cross(dir)).cross(dir)).norm()); - ver = stim::rect(magnitude, position, dir.norm(), - hor.n()); + if(cos(Y.dot(vctr))< 0.087){ Y[0] = 0.0; Y[1] = 1.0;} + 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()); + break; + case 1: + hor = stim::rect(magnitude, vctr, direction, + ((Y.cross(direction)).cross(direction)).norm()); + ver = stim::rect(magnitude, vctr, direction, + hor.n()); + break; + case 2: + hor = stim::rect(vctr, position, direction, + ((Y.cross(direction)).cross(direction)).norm()); + ver = stim::rect(vctr, position, direction, + hor.n()); + break; + default: + std::cout << "unknown case have been passed" + << std::endl; + break; + } UpdateBuffer(v_x, v_y); } - - + + /* + Method for controling the buffer and texture binding in order to properly + do the render to texture. + */ void - Sample(vec in = (0,0,1), int numSamples = 1089, int solidAngle = M_PI/2) + Bind(int numSamples = 1089) { - GenerateFBO(20, numSamples*10); - - 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 = in.cart2sph(); - vec temp; - int dim = (sqrt(numSamples)-1)/2; - //std::cout << dim << std::endl; - float y_0 = 0.0; - float len = 10.0; - float p0 = M_PI/3; - float dt = solidAngle/(1.0 * dim); - float dp = p0/(1.0*dim); - - + float len = 10.0; glBindFramebuffer(GL_FRAMEBUFFER, fboID);//set up GL buffer glFramebufferTexture2D( GL_FRAMEBUFFER, @@ -239,11 +245,45 @@ class gl_spider : public virtual gl_texture gluOrtho2D(0.0,2.0*len,0.0,numSamples*len); glEnable(GL_TEXTURE_3D); glBindTexture(GL_TEXTURE_3D, texID); + } + + void + Unbind() + { + //Finalize GL_buffer + glBindTexture(GL_TEXTURE_3D, 0); + glDisable(GL_TEXTURE_3D); + 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) + { + //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(); + 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] << "," < samples[idx][1] = temp[1]; samples[idx][2] = temp[2]; - //file << idx << ":" <<"[" << samples[idx][0] << "," << samples[idx][1] << "," << samples[idx][2] << "]" << "\n"; + // file << idx << ":" <<"[" << samples[idx][0] << "," << samples[idx][1] << "," << samples[idx][2] << "]" << "\n"; - Update(0.0, y_0+(idx)*10, temp); + UpdatePlanes(0.0, y_0+(idx)*10, temp); } } - //Finalize GL_buffer - glBindTexture(GL_TEXTURE_3D, 0); - glDisable(GL_TEXTURE_3D); - glBindFramebuffer(GL_FRAMEBUFFER,0); - glBindTexture(GL_TEXTURE_2D, 0); + Unbind(); int nxt = getCost(); - stim::vec next; - next[0] = samples[nxt][0]; - next[1] = samples[nxt][1]; - next[2] = samples[nxt][2]; + stim::vec 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]/500, - position[1]+next[1]/500, - position[2]+next[2]/500); + 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. + + int idx; + 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); + } + } + Unbind(); + int nxt = getCost(); + setPosition(samples[nxt][0], samples[nxt][1], samples[nxt][2]); + } + + void + sampleMagnitude(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); + } + Unbind(); + int nxt = getCost(); + setMagnitude(samples[nxt]); + file << position[0] << "," < stim::rect hor; stim::rect ver; - + //Default Constructor gl_spider () { setPosition(0.0,0.0,0.0); - setDirection(1.0,1.0,1.0); - setMagnitude(0.1,0.1); - //GenerateFBO(400,200); - //Update(); + setDirection(0.0,0.0,1.0); + setMagnitude(0.03); } - gl_spider - (vec pos, vec dir, vec mag) - { - position = pos; - direction = dir; - magnitude = mag; - //GenerateFBO(400,200); - //Update(); - } - //temporary cost for convenience. + //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, float mag_y) + float mag_x) { setPosition(pos_x, pos_y, pos_z); setDirection(dir_x, dir_y, dir_z); - setMagnitude(mag_x, mag_y); - //GenerateFBO(400,200); - //Update(); + setMagnitude(mag_x); } + //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) + attachSpider(GLuint id, int numSamples = 1089) { texID = id; - Sample(direction); - //GenerateFBO(20,10000); - // Update(); - // generateVectorField(direction, 4.0); + GenerateFBO(20, numSamples*10); + sampleDirection(); } - + + //temporary Method necessary for visualization and testing. void Update() { @@ -381,35 +473,37 @@ class gl_spider : public virtual gl_texture ((Y.cross(direction)).cross(direction)).norm()); ver = stim::rect(magnitude, position, direction.norm(), hor.n()); - //UpdateBuffer(); - //generateVectorField(direction, 4.0); } - + //Returns the position vector. vec getPosition() { return position; } + //Returns the direction vector. vec getDirection() { return direction; } + //Returns the magnitude vector. vec getMagnitude() { return magnitude; } + //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 void setPosition(float x, float y, float z) { @@ -418,12 +512,15 @@ class gl_spider : public virtual gl_texture position[2] = z; } + + //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 void setDirection(float x, float y, float z) { @@ -432,19 +529,22 @@ class gl_spider : public virtual gl_texture direction[2] = z; } + //Sets the magnitude vector to the input vector mag. void setMagnitude(vec mag) { - magnitude = mag; + magnitude[0] = mag[0]; + magnitude[1] = mag[0]; } void - setMagnitude(float x, float y) + setMagnitude(float mag) { - magnitude[0] = x; - magnitude[1] = y; + magnitude[0] = mag; + magnitude[1] = mag; } - + + //temporary method for visualization. GLuint getFB() { @@ -454,142 +554,9 @@ class gl_spider : public virtual gl_texture void Step() { - //std::cout << position[0] << "," << position[1] << "," << position[1] - // << std::endl; - //setPosition(direction*magnitude[1]/2+position); findOptimalDirection(); - //Update(); - //std::cout << position[0] << "," << position[1] << "," << position[1] - // << std::endl; - } -/* void - UpdateBuffer() - { - stim::vecp1; - stim::vecp2; - stim::vecp3; - stim::vecp4; - glBindFramebuffer(GL_FRAMEBUFFER, fboID); - 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(0,0,0,0); - glClear(GL_COLOR_BUFFER_BIT); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glViewport(0,0,400,200); - gluOrtho2D(0.0,2.0,0.0,2.0); - glEnable(GL_TEXTURE_3D); - glBindTexture(GL_TEXTURE_3D, texID); - p1 = hor.p(1,1); - p2 = hor.p(1,0); - p3 = hor.p(0,0); - p4 = hor.p(0,1); - glBegin(GL_QUADS); - glTexCoord3f( - p1[0], - p1[1], - p1[2] - ); - glVertex2f(0.0,0.0); - glTexCoord3f( - p2[0], - p2[1], - p2[2] - ); - glVertex2f(1.0, 0.0); - glTexCoord3f( - p3[0], - p3[1], - p3[2] - ); - glVertex2f(1.0, 2.0); - glTexCoord3f( - p4[0], - p4[1], - p4[2] - ); - glVertex2f(0.0, 2.0); - glEnd(); - p1 = ver.p(1,1); - p2 = ver.p(1,0); - p3 = ver.p(0,0); - p4 = ver.p(0,1); - glBegin(GL_QUADS); - glTexCoord3f( - p1[0], - p1[1], - p1[2] - ); - glVertex2f(1.0, 0.0); - glTexCoord3f( - p2[0], - p2[1], - p2[2] - ); - glVertex2f(2.0, 0.0); - glTexCoord3f( - p3[0], - p3[1], - p3[2] - ); - glVertex2f(2.0, 2.0); - glTexCoord3f( - p4[0], - p4[1], - p4[2] - ); - glVertex2f(1.0, 2.0); - glEnd(); - glBindTexture(GL_TEXTURE_3D, 0); - glDisable(GL_TEXTURE_3D); - glBindFramebuffer(GL_FRAMEBUFFER,0); - glBindTexture(GL_TEXTURE_2D, 0); - } */ - - - /*void - generateVectorField(stim::vec d, float dim) - { - vec d_s = d.cart2sph(); - vec temp; - float Dim = (float) dim; - float y_0 = 0.0; - float x_0 = 0.0; - float len = 4.0/(2.0*Dim+1.0); - float t0 = M_PI/2; - float p0 = M_PI/3; - float dt = t0/Dim; - float dp = p0/Dim; - for(int i = -dim; i <= dim; i++){ - for(int j = -dim; j <= dim; j++){ - //field[i+dim][j+dim][0] = d[0]; - //field[i+dim][j+dim][1] = d[1]+dt*i; - //field[i+dim][j+dim][2] = d[2]+dp*j; - temp[0] = 1; - temp[1] = d_s[1]+dt*i; - temp[2] = d_s[2]+dp*j; - temp = temp.sph2cart(); - Update(x_0+2.0*(i+dim)*len, y_0+(j+dim)*len, temp); - } - } - - }*/ - - - - void findOptimalDirection() @@ -597,7 +564,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. */ - Sample(direction); + sampleDirection(); + samplePosition(); + sampleMagnitude(); } /* Method for initializing the cuda devices, necessary only diff --git a/stim/math/rect.h b/stim/math/rect.h index 5a155cf..cb15b23 100644 --- a/stim/math/rect.h +++ b/stim/math/rect.h @@ -92,12 +92,20 @@ public: Y = directionY; } - CUDA_CALLABLE rect(vec mag, vec center, vec directionX, vec directionY ) + CUDA_CALLABLE rect(T size, vec center, vec directionX, vec directionY ) { C = center; X = directionX; Y = directionY; - scale(mag[0], mag[1]); + scale(size); + } + + CUDA_CALLABLE rect(vec size, vec center, vec directionX, vec directionY ) + { + C = center; + X = directionX; + Y = directionY; + scale(size[0], size[1]); } CUDA_CALLABLE void scale(T factor1, T factor2){ -- libgit2 0.21.4