void cons(){ int i,j,k; pos t,tn,t0,t1,t2,t3; for(i=0;i<MN;i++)for(j=0;j<MN;j++){ t.y=i;t.x=j; //printf("%c ",frt[i][j]); switch(frt[i][j]){ case 'F': case 'C':{ if(frt[i][j]=='F'){frm.y=i;frm.x=j;} if(frt[i][j]=='C'){cow.y=i;cow.x=j;} } case '.':{ for(k=0;k<4;k++){ t0=nbr(t,k); t1=nbr(t,(k+1)%4); t2=nbr(t,(k+2)%4); t3=nbr(t,(k+3)%4); if(!obs(t2)) edg[nod[i][j][k]][nod[t2.y][t2.x][k]]=1; if(obs(t2)&&!obs(t3)) edg[nod[i][j][k]][nod[t3.y][t3.x][(k+1)%4]]=2; if(obs(t2)&&obs(t3)&&!obs(t0)) edg[nod[i][j][k]][nod[t0.y][t0.x][(k+2)%4]]=3; if(obs(t2)&&obs(t3)&&obs(t0)&&!obs(t1)) edg[nod[i][j][k]][nod[t1.y][t1.x][(k+3)%4]]=4; } break; } case '*':break; }; } }
int print_es(const char *format, int *i, va_list list) { int nb; int ret; ret = 0; (*i)++; if (format[(*i)] == '0') { nb = ft_atoi(nbr(format, i)); ret += print_e(format, i, list, nb); } else if (format[(*i)] == '+') ret += print_ps(format, i, list); else { nb = ft_atoi(nbr(format, i)); ret += print(format, i, list, nb); } return(ret); }
BipartiteMatch( int na, int nb, const std::vector< Edge > & edges ):match_a( na, -1 ), match_b( nb, -1 ) { // const float inf = std::numeric_limits<float>::infinity(); // Build the graph std::vector< std::vector<int> > nbr( na ); for( Edge e: edges ) nbr[e.a].push_back( e.b ); // Run Hopcroft-Karp std::vector<float> dist( na+1, 0 ); while(1) { // Run BFS if( !bfs( na, nbr, dist ) ) break; // Start matching for( int i=0; i<na; i++ ) if( match_a[i] == -1 ) { // Run a DFS dfs( i, nbr, dist ); } } }
int ft_printf(const char *format, ...) { int i; va_list list; int size; int nb; nb = 0; size = 0; i = 0; va_start(list, format); while (format[i]) { size += search_char(format, &i); if(format[i] == '\0') return (size); i++; if (format[i] == '0') size += print_0(format, &i, list); else if (format[i] == ' ') size += print_es(format, &i, list); else if (format[i] == '+') size += print_ps(format, &i, list); else if (format[i] == '-') size += print_ng(format, &i, list); else { if(format[i] == '\0') return (size); nb = ft_atoi(nbr(format, &i)); size += print(format, &i, list, nb); } i++; } va_end(list); return (size); }
void MainWindow::givesol (int sol[][5],int grid[][5],int * n1,int * n2) { int hint_matrix[23][23] = { { 0,1,1,1,0,0,0,1,0,1,0,0,0,1,1,0,0,0,0,1,0,0,0 }, { 1,1,0,1,1,0,1,0,0,0,0,0,1,1,1,0,0,0,1,0,0,0,0 }, { 1,0,1,1,1,1,0,1,1,0,0,0,1,1,0,1,1,1,1,1,0,1,0 }, { 1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1 }, { 0,1,1,0,1,1,0,0,0,0,1,0,1,0,1,0,0,1,0,1,1,1,0 }, { 0,0,1,0,1,0,1,1,0,1,0,0,1,0,0,0,0,0,1,1,0,0,0 }, { 0,1,0,1,0,1,1,0,1,1,0,0,0,1,0,1,1,1,0,0,0,1,0 }, { 1,0,1,0,0,1,0,1,1,0,0,0,0,0,1,1,0,1,0,1,1,0,1 }, { 0,0,1,0,0,0,1,1,1,0,1,0,0,1,1,1,0,0,1,0,0,1,1 }, { 1,0,0,0,0,1,1,0,0,0,1,0,1,0,1,0,1,1,0,1,0,0,1 }, { 0,0,0,0,1,0,0,0,1,1,0,0,1,0,1,1,1,1,1,0,0,1,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1 }, { 0,1,1,0,1,1,0,0,0,1,1,0,1,1,0,0,0,1,0,0,1,1,0 }, { 1,1,1,0,0,0,1,0,1,0,0,0,1,1,1,0,0,0,1,0,0,0,0 }, { 1,1,0,0,1,0,0,1,1,1,1,0,0,1,1,1,1,0,1,0,1,0,0 }, { 0,0,1,0,0,0,1,1,1,0,1,0,0,0,1,1,0,1,0,1,1,0,1 }, { 0,0,1,1,0,0,1,0,0,1,1,1,0,0,1,0,1,1,1,0,0,0,1 }, { 0,0,1,0,1,0,1,1,0,1,1,0,1,0,0,1,1,0,1,1,1,0,0 }, { 0,1,1,0,0,1,0,0,1,0,1,0,0,1,1,0,1,1,1,0,0,0,1 }, { 1,0,1,0,1,1,0,1,0,1,0,0,0,0,0,1,0,1,0,1,1,0,1 }, { 0,0,0,1,1,0,0,1,0,0,0,1,1,0,1,1,0,1,0,1,1,1,0 }, { 0,0,1,1,1,0,1,0,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1 }, { 0,0,0,1,0,0,0,1,1,1,0,1,0,0,0,1,1,0,1,1,0,1,0 }}; int current_state[23]; int hint_vector[25]; for (int i=0; i<25; i++) { hint_vector[i] = 0; } for (int i=0; i<23; i++) { if (grid[i%5][i/5]) { current_state[i] = 1; } else { current_state[i] = 0; } } for (int i=0; i<23; i++) { for (int j=0; j<23; j++) { hint_vector[i] = (hint_vector[i] + current_state[j]*hint_matrix[i][j])%2; } } int optimal[25]; int aux[25]; copy(hint_vector,optimal); copy(hint_vector,aux); add(aux,n1); if ( nbr(aux) < nbr(optimal) ) { copy(aux,optimal); } copy(hint_vector,aux); add(aux,n2); if ( nbr(aux) < nbr(optimal) ) { copy(aux,optimal); } copy(hint_vector,aux); add(aux,n1); add(aux,n2); if ( nbr(aux) < nbr(optimal) ) { copy(aux,optimal); } for (int i=0; i<25; i++) { if (optimal[i] == 1) { sol[i%5][i/5]=3; } else { sol[i%5][i/5]=4; } } }
cv::Mat BoxFilter::perform(std::vector<cv::Mat> in_d){ assert(in_d.size() > 0); assert(in_d[0].type() == CV_64F); int R = in_d[0].rows; int C = in_d[0].cols; int Z = in_d.size(); cv::Mat out = cv::Mat::zeros(R, C, CV_64F); for (int b = 0; b < _boxes.size(); b++){ int l_start = 0; // near layer = 0 int l_end = Z - 1; // far layer = last // Near layers cv::Mat ntl(R - _shape[0], C - _shape[1], CV_64F, cv::Scalar::all(0.0)); cv::Mat ntr(R - _shape[0], C - _shape[1], CV_64F, cv::Scalar::all(0.0)); cv::Mat nbl(R - _shape[0], C - _shape[1], CV_64F, cv::Scalar::all(0.0)); cv::Mat nbr(R - _shape[0], C - _shape[1], CV_64F, cv::Scalar::all(0.0)); cv::Range rs[2]; if (l_start > 0){ rs[0] = cv::Range(_boxes[b].tl().y, R - _shape[0] + _boxes[b].tl().y); rs[1] = cv::Range(_boxes[b].tl().x, C - _shape[1] + _boxes[b].tl().x); ntl = cv::Mat(in_d[l_start-1], rs); // ntl: Near-Top-Left layer, -1 because near layer of box filtering process rs[0] = cv::Range(_boxes[b].tl().y, R - _shape[0] + _boxes[b].tl().y); rs[1] = cv::Range(_boxes[b].br().x, C - _shape[1] + _boxes[b].br().x); ntr = cv::Mat(in_d[l_start-1], rs); // ntr: Near-Top-Right layer rs[0] = cv::Range(_boxes[b].br().y, R - _shape[0] + _boxes[b].br().y); rs[1] = cv::Range(_boxes[b].tl().x, C - _shape[1] + _boxes[b].tl().x); nbl = cv::Mat(in_d[l_start-1], rs); // nbl: Near-Bottom-Left layer rs[0] = cv::Range(_boxes[b].br().y, R - _shape[0] + _boxes[b].br().y); rs[1] = cv::Range(_boxes[b].br().x, C - _shape[1] + _boxes[b].br().x); nbr = cv::Mat(in_d[l_start-1], rs); // nbr: Near-Bottom-Right layer } else{ ntl = cv::Mat(R - _shape[0], C - _shape[1], CV_64F, cv::Scalar::all(0.0)); ntr = cv::Mat(R - _shape[0], C - _shape[1], CV_64F, cv::Scalar::all(0.0)); nbl = cv::Mat(R - _shape[0], C - _shape[1], CV_64F, cv::Scalar::all(0.0)); nbr = cv::Mat(R - _shape[0], C - _shape[1], CV_64F, cv::Scalar::all(0.0)); } // Far layers rs[0] = cv::Range(_boxes[b].tl().y, R - _shape[0] + _boxes[b].tl().y); rs[1] = cv::Range(_boxes[b].tl().x, C - _shape[1] + _boxes[b].tl().x); cv::Mat ftl = cv::Mat(in_d[l_end], rs); // ftl: Far-Top-Left layer rs[0] = cv::Range(_boxes[b].tl().y, R - _shape[0] + _boxes[b].tl().y); rs[1] = cv::Range(_boxes[b].br().x, C - _shape[1] + _boxes[b].br().x); cv::Mat ftr = cv::Mat(in_d[l_end], rs); // ftr: Far-Top-Right layer rs[0] = cv::Range(_boxes[b].br().y, R - _shape[0] + _boxes[b].br().y); rs[1] = cv::Range(_boxes[b].tl().x, C - _shape[1] + _boxes[b].tl().x); rs[2] = cv::Range(l_end, l_end + 1); cv::Mat fbl = cv::Mat(in_d[l_end], rs); // fbl: Far-Bottom-Left layer rs[0] = cv::Range(_boxes[b].br().y, R - _shape[0] + _boxes[b].br().y); rs[1] = cv::Range(_boxes[b].br().x, C - _shape[1] + _boxes[b].br().x); rs[2] = cv::Range(l_end, l_end + 1); cv::Mat fbr = cv::Mat(in_d[l_end], rs); // fbr: Far-Bottom-Right layer // apply zero padding int pad_left = ceil((double) _shape[1] / 2.0); int pad_top = ceil((double) _shape[0] / 2.0); int pad_right = _shape[1]/2; int pad_bottom = _shape[0]/2; cv::Mat ntl_p(R, C, CV_64F); // ntl: Near-Top-Left layer cv::copyMakeBorder(ntl, ntl_p, pad_top + 1, pad_bottom - 1, pad_left + 1, pad_right - 1, cv::BORDER_CONSTANT, cv::Scalar::all(0)); cv::Mat ntr_p(R, C, CV_64F); // ntr: Near-Top-Right layer cv::copyMakeBorder(ntr, ntr_p, pad_top + 1, pad_bottom - 1, pad_left, pad_right, cv::BORDER_CONSTANT, cv::Scalar::all(0)); cv::Mat nbl_p(R, C, CV_64F); // nbl: Near-Bottom-Left layer cv::copyMakeBorder(nbl, nbl_p, pad_top, pad_bottom, pad_left + 1, pad_right - 1, cv::BORDER_CONSTANT, cv::Scalar::all(0)); cv::Mat nbr_p(R, C, CV_64F); // nbr: Near-Bottom-Right layer cv::copyMakeBorder(nbr, nbr_p, pad_top, pad_bottom, pad_left, pad_right, cv::BORDER_CONSTANT, cv::Scalar::all(0)); cv::Mat ftl_p(R, C, CV_64F); // ftl: Far-Top-Left layer cv::copyMakeBorder(ftl, ftl_p, pad_top + 1, pad_bottom - 1, pad_left + 1, pad_right - 1, cv::BORDER_CONSTANT, cv::Scalar::all(0)); cv::Mat ftr_p(R, C, CV_64F); // ftr: Far-Top-Right layer cv::copyMakeBorder(ftr, ftr_p, pad_top + 1, pad_bottom - 1, pad_left, pad_right, cv::BORDER_CONSTANT, cv::Scalar::all(0)); cv::Mat fbl_p(R, C, CV_64F); // fbl: Far-Bottom-Left layer cv::copyMakeBorder(fbl, fbl_p, pad_top, pad_bottom, pad_left + 1, pad_right - 1, cv::BORDER_CONSTANT, cv::Scalar::all(0)); cv::Mat fbr_p(R, C, CV_64F); // fbr: Far-Bottom-Right layer cv::copyMakeBorder(fbr, fbr_p, pad_top, pad_bottom, pad_left, pad_right, cv::BORDER_CONSTANT, cv::Scalar::all(0)); // obtain filtered image with a weighted sum of layers // equation for box filtering out += _boxes[b].v() * (fbr_p - nbr_p - ftr_p - fbl_p + ftl_p + nbl_p + ntr_p - ntl_p); } // cv::Range ran[2]; // ran[0] = cv::Range(_shape[0], out.rows - shape[0]); // ran[1] = cv::Range(_shape[1], out.cols - shape[1]); return out(cv::Range(_shape[0]/2 + 1, out.rows - _shape[0]/2 - 1), cv::Range(_shape[1]/2 + 1, out.cols - _shape[1]/2 - 1)); }
Wpt nbr_loc(CBedge* e) const { Bvert* n = nbr(e); assert(n); return n->loc(); }
// Like previous 2 methods, but return neighbor location. // Must be valid or assertion fails: Wpt nbr_loc(int k) const { Bvert* n = nbr(k); assert(n); return n->loc(); }
// Determine order for faces: // - upper-triangular order for internal faces // - external faces after internal faces (were already so) Foam::labelList Foam::polyDualMesh::getFaceOrder ( const labelList& faceOwner, const labelList& faceNeighbour, const cellList& cells, label& nInternalFaces ) { labelList oldToNew(faceOwner.size(), -1); // First unassigned face label newFacei = 0; forAll(cells, celli) { const labelList& cFaces = cells[celli]; SortableList<label> nbr(cFaces.size()); forAll(cFaces, i) { label facei = cFaces[i]; label nbrCelli = faceNeighbour[facei]; if (nbrCelli != -1) { // Internal face. Get cell on other side. if (nbrCelli == celli) { nbrCelli = faceOwner[facei]; } if (celli < nbrCelli) { // Celli is master nbr[i] = nbrCelli; } else { // nbrCell is master. Let it handle this face. nbr[i] = -1; } } else { // External face. Do later. nbr[i] = -1; } } nbr.sort(); forAll(nbr, i) { if (nbr[i] != -1) { oldToNew[cFaces[nbr.indices()[i]]] = newFacei++; } } }
void MarchingTriangles::improve_mesh() { // first get adjacency information std::vector<Array1ui> nbr(x.size()); for(unsigned int e=0; e<edge.size(); ++e) { unsigned int p = edge[e][0]; unsigned int q = edge[e][1]; nbr[p].add_unique(q); nbr[q].add_unique(p); } // then sweep through the mesh a few times incrementally improving positions for(unsigned int sweep=0; sweep<3; ++sweep) { for(unsigned int p=0; p<x.size(); ++p) { // get a weighted average of neighbourhood positions Vec2f target = x[p]; target += x[ nbr[p][0] ]; target += x[ nbr[p][1] ]; target /= 3.0f; // project onto level set surface with Newton for(int projection_step=0; projection_step<5; ++projection_step) { float i=(target[0]-origin[0])/dx, j=(target[1]-origin[1])/dx; float f=eval(i,j); Vec2f g; eval_gradient(i,j,g); float m2=mag2(g), m=std::sqrt(m2); float alpha = clamp( -f/(m2+1e-10f), -0.25f*m, 0.25f*m ); // clamp to avoid stepping more than a fraction of a grid cell // do line search to make sure we actually are getting closer to the zero level set bool line_search_success=false; for(int line_search_step=0; line_search_step<10; ++line_search_step) { double fnew=eval( i+alpha*g[0], j+alpha*g[1] ); if(std::fabs(fnew) <= std::fabs(f)) { target += Vec2f( (alpha*dx)*g[0], (alpha*dx)*g[0] ); target += Vec2f( (alpha*dx)*g[1], (alpha*dx)*g[1] ); line_search_success=true; break; }else alpha*=0.5f; } if(!line_search_success) // if we stalled trying to find the zero isocontour... { // weight the target closer to the original x[p] std::cout<<"line search failed (p="<<p<<" project="<<projection_step<<" sweep="<<sweep<<")"<<std::endl; target= 0.5f * (x[p]+target); } } x[p]=target; } } }