int main() { unsigned char rgb_buf[WIDTH*HEIGHT*3]; float hsv_buf[WIDTH*HEIGHT*3]; char *filename_in; char *filename_out; float f1, f2, f3, f4; FILE *fp; int i; filename_in = "testdata/pixbuff_7.ppm"; filename_out = "testdata/pixbuff_7_hsv.txt"; read_ppm(rgb_buf, filename_in); rgb2hsv(rgb_buf, hsv_buf); // verify sample hsv values f1 = hsv_buf[ind3(0, 0, 0)]; f2 = hsv_buf[ind3(0, 0, 1)]; f3 = hsv_buf[ind3(0, 0, 2)]; f4 = hsv_buf[ind3(1, 1, 0)]; printf("should be 53, 23, 40, 53\n"); printf("%f %f %f %f\n", f1*360, f2*100, f3*100, f4*360); printf("Writing buffer for verification...\n"); fp = fopen(filename_out, "w"); for (i=0; i<WIDTH*HEIGHT*3; i++) { fprintf(fp, "%f\n", hsv_buf[i]); } fclose(fp); return 0; }
void orange_thresh(float* fbuf, unsigned char* mask) { /* Find orange ball mask from HSV float image buffer. */ /* Thresholds from 'ppm_read_orange.m' */ float hue, sat, val; int i,x,y; for (x=0; x<WIDTH; x++) { for (y=0; y<HEIGHT; y++) { hue = fbuf[ind3(x,y,0)]; sat = fbuf[ind3(x,y,1)]; val = fbuf[ind3(x,y,2)]; i = ind1(x, y); mask[i] = 0; // highlights values if (val >= .99 && (hue < 30.0/360.0 || hue > 355.0/360.0) && sat > .1) { mask[i] = 1; } // mid values if (val >= .2 && (hue > 5.0/360.0 && hue < 15.0/360.0) && sat > .55) { mask[i] = 1; } if ((hue < 25.0/360.0 || hue > 358.0/360.0) && sat > .6) { mask[i] = 1; } // shadows if (val < .6 && val > .3 && (hue < 20.0/360.0 || hue > 355.0/360.0) && sat > .65) { mask[i] = 1; } } } }
void psi_interp_triquadratic(psi_rvec* in, psi_rvec* out) { psi_int i, j, k, ax; for(ax = 0; ax < PSI_NDIM; ++ax) { #if PSI_NDIM == 3 for(i = 0; i < 3; ++i) for(j = 0; j < 3; ++j) for(k = 0; k < 3; ++k) out[ind5(2*i, 2*j, 2*k)].xyz[ax] = in[ind3(i, j, k)].xyz[ax]; for(j = 0; j < 5; j += 2) for(k = 0; k < 5; k += 2) { out[ind5(1, j, k)].xyz[ax] = 0.125*(3.0*out[ind5(0, j, k)].xyz[ax] + 6.0*out[ind5(2, j, k)].xyz[ax] - out[ind5(4, j, k)].xyz[ax]); out[ind5(3, j, k)].xyz[ax] = 0.125*(3.0*out[ind5(4, j, k)].xyz[ax] + 6.0*out[ind5(2, j, k)].xyz[ax] - out[ind5(0, j, k)].xyz[ax]); } for(i = 0; i < 5; i += 1) for(k = 0; k < 5; k += 2) { out[ind5(i, 1, k)].xyz[ax] = 0.125*(3.0*out[ind5(i, 0, k)].xyz[ax] + 6.0*out[ind5(i, 2, k)].xyz[ax] - out[ind5(i, 4, k)].xyz[ax]); out[ind5(i, 3, k)].xyz[ax] = 0.125*(3.0*out[ind5(i, 4, k)].xyz[ax] + 6.0*out[ind5(i, 2, k)].xyz[ax] - out[ind5(i, 0, k)].xyz[ax]); } for(i = 0; i < 5; i += 1) for(j = 0; j < 5; j += 1) { out[ind5(i, j, 1)].xyz[ax] = 0.125*(3.0*out[ind5(i, j, 0)].xyz[ax] + 6.0*out[ind5(i, j, 2)].xyz[ax] - out[ind5(i, j, 4)].xyz[ax]); out[ind5(i, j, 3)].xyz[ax] = 0.125*(3.0*out[ind5(i, j, 4)].xyz[ax] + 6.0*out[ind5(i, j, 2)].xyz[ax] - out[ind5(i, j, 0)].xyz[ax]); } #elif PSI_NDIM == 2 for(i = 0; i < 3; ++i) for(j = 0; j < 3; ++j) out[ind5(2*i, 2*j)].xyz[ax] = in[ind3(i, j)].xyz[ax]; for(j = 0; j < 5; j += 2) { out[ind5(1, j)].xyz[ax] = 0.125*(3.0*out[ind5(0, j)].xyz[ax] + 6.0*out[ind5(2, j)].xyz[ax] - out[ind5(4, j)].xyz[ax]); out[ind5(3, j)].xyz[ax] = 0.125*(3.0*out[ind5(4, j)].xyz[ax] + 6.0*out[ind5(2, j)].xyz[ax] - out[ind5(0, j)].xyz[ax]); } for(i = 0; i < 5; i += 1) { out[ind5(i, 1)].xyz[ax] = 0.125*(3.0*out[ind5(i, 0)].xyz[ax] + 6.0*out[ind5(i, 2)].xyz[ax] - out[ind5(i, 4)].xyz[ax]); out[ind5(i, 3)].xyz[ax] = 0.125*(3.0*out[ind5(i, 4)].xyz[ax] + 6.0*out[ind5(i, 2)].xyz[ax] - out[ind5(i, 0)].xyz[ax]); } #endif } }
void psi_interp_trilinear(psi_rvec* in, psi_rvec* out) { psi_int i, j, k, ax; for(ax = 0; ax < PSI_NDIM; ++ax) { #if PSI_NDIM == 3 for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k) out[ind3(2*i, 2*j, 2*k)].xyz[ax] = in[ind2(i, j, k)].xyz[ax]; for(j = 0; j < 3; j += 2) for(k = 0; k < 3; k += 2) out[ind3(1, j, k)].xyz[ax] = 0.5*(out[ind3(0, j, k)].xyz[ax] + out[ind3(2, j, k)].xyz[ax]); for(i = 0; i < 3; i += 1) for(k = 0; k < 3; k += 2) out[ind3(i, 1, k)].xyz[ax] = 0.5*(out[ind3(i, 0, k)].xyz[ax] + out[ind3(i, 2, k)].xyz[ax]); for(i = 0; i < 3; i += 1) for(j = 0; j < 3; j += 1) out[ind3(i, j, 1)].xyz[ax] = 0.5*(out[ind3(i, j, 0)].xyz[ax] + out[ind3(i, j, 2)].xyz[ax]); #elif PSI_NDIM == 2 for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) out[ind3(2*i, 2*j)].xyz[ax] = in[ind2(i, j)].xyz[ax]; for(j = 0; j < 3; j += 2) out[ind3(1, j)].xyz[ax] = 0.5*(out[ind3(0, j)].xyz[ax] + out[ind3(2, j)].xyz[ax]); for(i = 0; i < 3; i += 1) out[ind3(i, 1)].xyz[ax] = 0.5*(out[ind3(i, 0)].xyz[ax] + out[ind3(i, 2)].xyz[ax]); #endif } }
void psi_refine_trilinear(psi_rvec* pos, psi_rvec* vel, psi_real mass, psi_tet_buffer* tetbuf, psi_int lvl) { // TODO: these are different for the 48-tet connectivity! #if PSI_NDIM == 3 // all unique tet edges across the 3-cube. Order is first, midpoint, second. const static psi_int edges3TL[7][3] = { { 2 , 4 , 6 }, { 2 , 10 , 18 }, { 6 , 12 , 18 }, { 8 , 13 , 18 }, { 8 , 14 , 20 }, { 8 , 16 , 24 }, { 20 , 22 , 24 } }; const static psi_int nedges3TL = 7; #elif PSI_NDIM == 2 const static psi_int edges3TL[5][3] = {{0,1,2}, {0,3,6}, {2,4,6}, {2,5,8}, {6,7,8}}; const static psi_int nedges3TL = 5; #endif psi_int e, i, j, k, ii, jj, kk; psi_rvec tmppos[27]; psi_rvec tmpvel[27]; psi_rvec childpos[8]; psi_rvec childvel[8]; psi_real midpt_tet, midpt_lin, err2; // check to see if all of the tet edges are within tolerance if(lvl < tetbuf->max_lvl) for(e = 0; e < nedges3TL; ++e) { err2 = 0.0; for(i = 0; i < PSI_NDIM; ++i) { midpt_tet = 0.5*(pos[edges3TL[e][0]].xyz[i] + pos[edges3TL[e][2]].xyz[i]); midpt_lin = pos[edges3TL[e][1]].xyz[i]; err2 += (midpt_tet - midpt_lin)*(midpt_tet - midpt_lin); } // if this edge gives too large an error in position, stop and recurse if(err2 > tetbuf->ptol2) { #if PSI_NDIM == 3 for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k) { for(ii = 0; ii < 2; ++ii) for(jj = 0; jj < 2; ++jj) for(kk = 0; kk < 2; ++kk) { childpos[ind2(ii,jj,kk)] = pos[ind3(ii+i,jj+j,kk+k)]; childvel[ind2(ii,jj,kk)] = vel[ind3(ii+i,jj+j,kk+k)]; } psi_interp_trilinear(childpos, tmppos); psi_interp_trilinear(childvel, tmpvel); psi_refine_trilinear(tmppos, tmpvel, 0.125*mass, tetbuf, lvl+1); } #elif PSI_NDIM == 2 for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) { for(ii = 0; ii < 2; ++ii) for(jj = 0; jj < 2; ++jj) { childpos[ind2(ii,jj)] = pos[ind3(ii+i,jj+j)]; childvel[ind2(ii,jj)] = vel[ind3(ii+i,jj+j)]; } psi_interp_trilinear(childpos, tmppos); psi_interp_trilinear(childvel, tmpvel); psi_refine_trilinear(tmppos, tmpvel, 0.25*mass, tetbuf, lvl+1); } #endif return; } } // if we have passed the tolerance test, refine to the buffer psi_tets_from_3cube(pos, vel, mass, tetbuf); }
void psi_refine_triquadratic(psi_rvec* pos, psi_rvec* vel, psi_real mass, psi_tet_buffer* tetbuf, psi_int lvl) { // TODO: these are different for the 48-tet connectivity! #if PSI_NDIM == 3 // all unique tet edges across the 3x3 unit cube. Order is first, midpoint, second. const static int edges3TQ[19][3] = { { 0 , 1 , 2 }, { 0 , 3 , 6 }, { 0 , 9 , 18 }, { 2 , 4 , 6 }, { 2 , 5 , 8 }, { 2 , 10 , 18 }, { 2 , 11 , 20 }, { 6 , 7 , 8 }, { 6 , 12 , 18 }, { 6 , 15 , 24 }, { 8 , 13 , 18 }, { 8 , 14 , 20 }, { 8 , 16 , 24 }, { 8 , 17 , 26 }, { 18 , 19 , 20 }, { 18 , 21 , 24 }, { 20 , 22 , 24 }, { 20 , 23 , 26 }, { 24 , 25 , 26 } }; const static int nedges3TQ = 19; #elif PSI_NDIM == 2 const static psi_int edges3TQ[5][3] = {{0,1,2}, {0,3,6}, {2,4,6}, {2,5,8}, {6,7,8}}; const static psi_int nedges3TQ = 5; #endif psi_rvec interppos[125]; psi_rvec interpvel[125]; psi_rvec childpos[27]; psi_rvec childvel[27]; psi_int e, i, j, k, ii, jj, kk; psi_real midpt_tet, midpt_quad, err2; // check to see if all of the tet edges are within tolerance if(lvl < tetbuf->max_lvl) for(e = 0; e < nedges3TQ; ++e) { err2 = 0.0; for(i = 0; i < PSI_NDIM; ++i) { midpt_tet = 0.5*(pos[edges3TQ[e][0]].xyz[i] + pos[edges3TQ[e][2]].xyz[i]); midpt_quad = pos[edges3TQ[e][1]].xyz[i]; err2 += (midpt_tet - midpt_quad)*(midpt_tet - midpt_quad); } // if this edge gives too large an error in position, stop and recurse if(err2 > tetbuf->ptol2) { // triquadratic interpolation psi_interp_triquadratic(pos, interppos); psi_interp_triquadratic(vel, interpvel); // TODO: option to use velocity #if PSI_NDIM == 3 // extract sub-cubes from interp for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) for(k = 0; k < 2; ++k) { for(ii = 0; ii < 3; ++ii) for(jj = 0; jj < 3; ++jj) for(kk = 0; kk < 3; ++kk) {// TODO: option to use velocity childpos[ind3(ii,jj,kk)] = interppos[ind5(ii+2*i,jj+2*j,kk+2*k)]; childvel[ind3(ii,jj,kk)] = interpvel[ind5(ii+2*i,jj+2*j,kk+2*k)]; } psi_refine_triquadratic(childpos, childvel, 0.125*mass, tetbuf, lvl+1); } #elif PSI_NDIM == 2 for(i = 0; i < 2; ++i) for(j = 0; j < 2; ++j) { for(ii = 0; ii < 3; ++ii) for(jj = 0; jj < 3; ++jj) { childpos[ind3(ii,jj)] = interppos[ind5(ii+2*i,jj+2*j)]; childvel[ind3(ii,jj)] = interpvel[ind5(ii+2*i,jj+2*j)]; } psi_refine_triquadratic(childpos, childvel, 0.25*mass, tetbuf, lvl+1); } #endif return; } } // if we have passed the tolerance test, refine the ucube to the buffer psi_tets_from_3cube(pos, vel, mass, tetbuf); }
int main(int argc, char * argv[]) { ProblemDefinitionsSPtr dummy_defs(new ProblemDefinitions(2,2,0,0)); PopulationSPtr myPop(new Population); IndividualSPtr ind1(new Individual(dummy_defs)); std::vector<double> dvs1 {1.0, 0.0}; ind1->setRealDVs(dvs1); ind1->setObjectives(std::vector<double>{1.0, 0.0}); myPop->push_back(ind1); IndividualSPtr ind2(new Individual(dummy_defs)); ind2->setRealDVs(std::vector<double>{0.7, 0.15}); ind2->setObjectives(std::vector<double>{0.49, 0.0225}); myPop->push_back(ind2); IndividualSPtr ind3(new Individual(dummy_defs)); ind3->setRealDVs(std::vector<double>{0.5, 0.26}); ind3->setObjectives(std::vector<double>{0.25, 0.0676}); myPop->push_back(ind3); IndividualSPtr ind4(new Individual(dummy_defs)); ind4->setRealDVs(std::vector<double>{0.44, 0.3}); ind4->setObjectives(std::vector<double>{0.1936, 0.09}); myPop->push_back(ind4); IndividualSPtr ind5(new Individual(dummy_defs)); ind5->setRealDVs(std::vector<double>{0.4, 0.37}); ind5->setObjectives(std::vector<double>{0.16, 0.1369}); myPop->push_back(ind5); IndividualSPtr ind6(new Individual(dummy_defs)); ind6->setRealDVs(std::vector<double>{0.2, 0.7}); ind6->setObjectives(std::vector<double>{0.04, 0.49}); myPop->push_back(ind6); IndividualSPtr ind7(new Individual(dummy_defs)); ind7->setRealDVs(std::vector<double>{0.0, 1.0}); ind7->setObjectives(std::vector<double>{0, 1}); myPop->push_back(ind7); IndividualSPtr ind1_(new Individual(dummy_defs)); ind1_->setRealDVs(std::vector<double> {1.0, 0.0}); ind1_->setObjectives(std::vector<double>{1.2, 0.2}); myPop->push_back(ind1_); IndividualSPtr ind2_(new Individual(dummy_defs)); ind2_->setRealDVs(std::vector<double>{0.7, 0.15}); ind2_->setObjectives(std::vector<double>{0.69, 0.2225}); myPop->push_back(ind2_); IndividualSPtr ind3_(new Individual(dummy_defs)); ind3_->setRealDVs(std::vector<double>{0.5, 0.26}); ind3_->setObjectives(std::vector<double>{0.45, 0.2676}); myPop->push_back(ind3_); IndividualSPtr ind4_(new Individual(dummy_defs)); ind4_->setRealDVs(std::vector<double>{0.44, 0.3}); ind4_->setObjectives(std::vector<double>{0.3936, 0.29}); myPop->push_back(ind4_); IndividualSPtr ind5_(new Individual(dummy_defs)); ind5_->setRealDVs(std::vector<double>{0.4, 0.37}); ind5_->setObjectives(std::vector<double>{0.36, 0.3369}); myPop->push_back(ind5_); IndividualSPtr ind6_(new Individual(dummy_defs)); ind6_->setRealDVs(std::vector<double>{0.2, 0.7}); ind6_->setObjectives(std::vector<double>{0.24, 0.69}); myPop->push_back(ind6_); IndividualSPtr ind7_(new Individual(dummy_defs)); ind7_->setRealDVs(std::vector<double>{0.0, 1.0}); ind7_->setObjectives(std::vector<double>{0.2, 1.2}); myPop->push_back(ind7_); std::vector< std::vector<std::pair<IndividualSPtr, double > > > values1 = DebsCrowdingDistance::calculate(myPop); FrontsSPtr values2 = myPop->getFronts(); // typedef std::pair<IndividualSPtr, double> IndDistPair; // FrontsSPtr values2(new Fronts(values1.size())); // boost::shared_ptr<std::vector<std::vector<IndividualSPtr> > > values2(new std::vector<std::vector<IndividualSPtr> >(values1.size())); // for (int i = 0; i < values2->size(); ++i) // { // BOOST_FOREACH(IndDistPair ind, values1[i]) // { // (*values2)[i].push_back(ind.first); // } // } #ifdef WITH_VTK PlotFrontVTK plot; plot(myPop); #endif }
Gradient::Gradient(Grid * g){ //cout << "Trying to create gradient with h = " << g->getH() << endl; this->g = g; double h = g->getH(); constantTerm.resize(g->faces.size()); constantTerm = 0; for(int i = g->iMin; i <= g->iMax; i++){ for(int j = g->jMin; j <= g->jMax; j++){ if(g->cells(i,j)->isUncovered()){ //cout << " is uncovered." << endl; CellIndex cellIndex(i,j); TinyVector<Face*,5> faces = g->cells(i,j)->faces; if(g->isFaceUncovered(i,j,B)){ FaceIndex faceIndex = faces(B)->getIndex(); Coord centroid = faces(B)->getCentroid(); TinyVector<double,2> n = faces(B)->getNormal(); double x, y, r; x = centroid(0); y = centroid(1); r = sqrt(pow2(x) + pow2(y)); TinyVector<double,2> gradU; double pi = acos(-1); //gradU(0) = 3*pow(x,2)*pow(y-2,2)+sin(2*(x-pow(y,2))); //gradU(1) = 2*pow(x,3)*(y-2) - 2*y*sin(2*(x-pow(y,2))); gradU(0) = 0;//-2*pi*sin(2*pi*r)*x/r; gradU(1) = 0;//-2*pi*sin(2*pi*r)*y/r; constantTerm(faceIndex) += gradU(0)*n(0) + gradU(1)*n(1); //cout << "\tAdding boundary condition to constant term." << endl; } if(faces(N) != 0){ FaceIndex faceIndex = faces(N)->getIndex(); if(faces(N)->isRegular()){ coefficients[faceIndex][cellIndex] = -1/h; } else if(faces(N)->isIrregular()){ if(coefficients[faceIndex].count(cellIndex) == 0){ double alpha = g->cells(i,j)->getFace(N)->getArea(); if(g->isFaceUncovered(i+1,j,N)){ CellIndex ind1(i,j); CellIndex ind2(i,j+1); CellIndex ind3(i+1,j); CellIndex ind4(i+1,j+1); interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex); } else if(g->isFaceUncovered(i-1,j,N)){ CellIndex ind1(i,j); CellIndex ind2(i,j+1); CellIndex ind3(i-1,j); CellIndex ind4(i-1,j+1); interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex); } } } //cout << "\tDid north face" << endl; } if(faces(S) != 0){ FaceIndex faceIndex = faces(S)->getIndex(); if(faces(S)->isRegular()){ coefficients[faceIndex][cellIndex] = 1/h; } else if(faces(S)->isIrregular()){ if(coefficients[faceIndex].count(cellIndex) == 0){ double alpha = g->cells(i,j)->getFace(S)->getArea(); if(g->isFaceUncovered(i+1,j,S)){ CellIndex ind1(i,j-1); CellIndex ind2(i,j); CellIndex ind3(i+1,j-1); CellIndex ind4(i+1,j); interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex); } else if(g->isFaceUncovered(i-1,j,S)){ CellIndex ind1(i,j-1); CellIndex ind2(i,j); CellIndex ind3(i-1,j-1); CellIndex ind4(i-1,j); interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex); } } } //cout << "\tDid south face" << endl; } if(faces(E) != 0){ FaceIndex faceIndex = faces(E)->getIndex(); CellIndex cellIndex(i,j); if(faces(E)->isRegular()){ coefficients[faceIndex][cellIndex] = -1/h; } else if(faces(E)->isIrregular()){ if(coefficients[faceIndex].count(cellIndex) == 0){ double alpha = g->cells(i,j)->getFace(E)->getArea(); if(g->isFaceUncovered(i,j+1,E)){ CellIndex ind1(i,j); CellIndex ind2(i+1,j); CellIndex ind3(i,j+1); CellIndex ind4(i+1,j+1); interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex); } else if(g->isFaceUncovered(i,j-1,E)){ CellIndex ind1(i,j); CellIndex ind2(i+1,j); CellIndex ind3(i,j-1); CellIndex ind4(i+1,j-1); interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex); } } } //cout << "\tDid east face" << endl; } if(faces(W) != 0){ FaceIndex faceIndex = faces(W)->getIndex(); CellIndex cellIndex(i,j); if(faces(W)->isRegular()){ coefficients[faceIndex][cellIndex] = 1/h; } else if(faces(W)->isIrregular()){ if(coefficients[faceIndex].count(cellIndex) == 0){ double alpha = g->cells(i,j)->getFace(W)->getArea(); if(g->isFaceUncovered(i,j+1,W)){ CellIndex ind1(i+1,j); CellIndex ind2(i,j); CellIndex ind3(i+1,j+1); CellIndex ind4(i,j+1); interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex); } else if(g->isFaceUncovered(i,j-1,W)){ CellIndex ind1(i+1,j); CellIndex ind2(i,j); CellIndex ind3(i+1,j-1); CellIndex ind4(i,j-1); interpolateIrregularFace(alpha,ind1,ind2,ind3,ind4,faceIndex); } } } //cout << "\tDid west face" << endl; } } } } //cout << "Resized" << endl; /* for(int i = g->iMin; i <= g->iMax; i++){ for(int j = g->jMin; j <= g->jMax; j++){ cout << "i = " << i << " j = " << j << endl; if(g->cells(i,j)->isUncovered()){ cout << "Uncovered" << endl; double c = 1; CellIndex index(i,j); TinyVector<Face*,5> faces = g->cells(i,j)->faces; cout << "Got faces" << endl; for(int k = 0; k < 5; k++){ cout << "Testing face " << k << endl; if(faces(k) != 0 && faces(k)->isUncovered()){ cout << "Face " << k << " is uncovered "; cout << "and has index " << faces(k)->getIndex() << endl; cout << coefficients(faces(k)->getIndex()).empty() << endl; coefficients(faces(k)->getIndex())[index] = c; } } } } }*/ }