const NAString ElemDDLFileAttrMVCommitEach::displayLabel1() const { char buffer[80]; sprintf(buffer, "%d", getNRows()); return NAString("nrows: ") + NAString(buffer); }
void filterObjects(Image *im, ObjectDB *test, ObjectDB *known) { int i, j, px, n=0; recognize(test, known); for (i=0; i < test->nObjects; ++i) { if (test->objs[i].label) { n++; } } for (i=0; i < getNRows(im); ++i) { for (j=0; j < getNCols(im); ++j) { px=getPixel(im, i, j); if ((px > 0) && !test->objs[px-1].label) { setPixel(im, i, j, 0); } } } i=0; for (j=n; j < test->nObjects && i < n; ++j) { while (i < n && test->objs[i].label) i++; if (test->objs[j].label) { memcpy(test->objs+i, test->objs+j, sizeof(Object)); } } test->objs=(Object *)realloc(test->objs, n*sizeof(Object)); if (test->objs == NULL) { fprintf(stderr, "ran out of memory while filtering objects.\n"); exit(1); } test->nObjects=n; }
int FacilityLocation::createConsAssignment() { int numCons = 0; VariableHash::iterator vit; Row::ROWSENSE sense = Row::EQUAL; int maxnnz = (g->nVertices - g->nTerminals) + 1; double rhs = 1.0; for (int i = 1; i <= g->nVertices; ++i) { Vertex client = g->vertices[i]; if (!client.isTerminal()) continue; Constraint c(maxnnz, sense, rhs); c.setType(Constraint::C_ASSIGNMENT); c.setNode(client); Variable x; x.setType(Variable::V_X); for (int j = 1; j <= g->nVertices; ++j) { Vertex router = g->vertices[j]; // @annotation: comment next filter to resolve instances where // the vpn terminals may be considered internal nodes // 20160125 if (client == router || router.isTerminal()) continue; x.setArc(Arc(router, client, 0.)); vit = vHash[Variable::V_X].find(x); if (vit != vHash[Variable::V_X].end()) { int colVarX = vit->second; c.rowAddVar(colVarX, 1.0); } } if (c.getRowNnz() > 0) { bool isInserted = addRow(&c); if (isInserted) { c.setRowIdx(getNRows() - 1); cHash[c] = c.getRowIdx(); numCons++; } } } return numCons; }
Image::Image(const Image &im) { /* initialize image class */ /* Copy from im */ setSize(im.getNRows(), im.getNCols()); setColors(im.getColors()); int i,j; for (i=0; i<getNRows(); ++i) for (j=0; j<getNCols(); ++j) { setPixel(i,j, im.getPixel(i,j)); } }
int main(int nargin, char** argv) { if(nargin != 5) { printf("Usage: p1 <img> <hough img> <hough thresh> <line-detected output>\n"); return -1; } Image* im = (Image*) malloc(sizeof(Image)); Image* hough = (Image*) malloc(sizeof(Image)); readImage(im, argv[1]); readImage(hough, argv[2]); int i, j, nR, nC; nR = getNRows(im); nC = getNCols(im); int thresh = atoi(argv[3]); int p = 0; for(i=0; i < getNRows(hough); i++) for(j=0; j < getNCols(hough); j++) { p = getPixel(hough,i,j); if(p > thresh) { // draw our line // the i'th value is our p value. j'th is theta + 90 degrees. // equation was: p = - x sin(t) + y cos(t) // now we have: y = x tan(t) + p/cos(t) float t = j - 180; float p = i; float m = tan(t / 180 * PI); float b = p / cos(t / 180 * PI); line(im, (int) (m*0+b), 0, (int) (m*nC+b), nC, 0); } } writeImage(im, argv[4]); free(im); free(hough); return 0; }
void Image2D<T>::printEntireImage (int Nx90) { cout << "Image2D<T>: Rotation by 90*" << Nx90 << "=" << Nx90*90 << " degree" << endl; cout << " ncols=" << this->getNCols(Nx90) << " nrows=" << this->getNRows(Nx90) << endl; for (size_t row = 0; row < getNRows(Nx90); row++) { for (size_t col = 0; col < getNCols(Nx90); col++) { cout << this->rotN90 (row,col,Nx90) << " "; } cout << endl; } }
int main(int nargin, char** argv){ if(nargin != 4){ printf("Usage: p1 <greylevel img> <threshold> <output file>"); return -1; } im = (Image*) malloc(sizeof(Image)); thresh = atoi(argv[2]); readImage(im, argv[1]); int i, j; setColors(im, 1); for(i = 0; i < getNRows(im); i++){ for(j = 0; j < getNCols(im); j++){ setPixel(im, i, j, getPixel(im, i, j) > thresh); } } writeImage(im, argv[3]); }
/** Calculate the the number of holes in the object o of the image i * The function attempts to reverse the idea of sequential labeling * in order to find black spots on objects * It still needs work. * TODO Make this work properly * */ void euler(Image *im, Object *o) { int i, j, k, c, neighbors[NNEIGHB], newObj, left, right; LabelMap lm=makeLabelMap(im); /* this sets up a dummy class for background darkness */ addLabel(&lm); for (i=0; i < getNRows(im); ++i) for (j=0; j < getNCols(im); ++j) if (!getPixel(im, i, j)) setLabel(&lm, i, j, 1); for (i=o->top; i <= o->bottom; ++i) { left=-1; for (j=o->left; j <= o->right; ++j) { if (getPixel(im, i, j) == o->label) { if (left < 0) left=j; /* left edge for this row */ right=j; /* right-most so far */ } } for (j=left; j <= right; ++j) { if (!getPixel(im, i, j)) { newObj=getNeighbors(&lm, i, o->top, j, left, neighbors); if (newObj) { addLabel(&lm); c=getNLabels(&lm); } else { for (k=0; k < NNEIGHB; ++k) if (neighbors[k] > 0) { c=evalNeighbor(k, &lm, neighbors); break; /* break since finding one means checking the rest */ } } setLabel(&lm, i, j, c); } } } o->holes=getNClasses(&lm)-1; /* number is off by one because of dummy class */ freeLabelMap(&lm); }
int FacilityLocation::createConsTree() { int numCons = 0; VariableHash::iterator vit; int maxnnz = vHash[Variable::V_Y].size() + vHash[Variable::V_Z].size(); Row::ROWSENSE sense = Row::EQUAL; double rhs = -1; Constraint c(maxnnz, sense, rhs); c.setType(Constraint::C_TREE); if (cHash.find(c) == cHash.end()) { for (vit = vHash[Variable::V_Z].begin(); vit != vHash[Variable::V_Z].end(); ++vit) { int colidx = vit->second; c.rowAddVar(colidx, 1.0); } for (vit = vHash[Variable::V_Y].begin(); vit != vHash[Variable::V_Y].end(); ++vit) { int colidx = vit->second; c.rowAddVar(colidx, -1.0); } if (c.getRowNnz() > 0) { bool isInserted = addRow(&c); if (isInserted) { c.setRowIdx(getNRows() - 1); cHash[c] = c.getRowIdx(); numCons++; } } } return numCons; }
void Image2D<T>::saveImageInFile (const std::string &fname, int Nx90) { cout << "Image2D<T>::saveImageInFile: "; ofstream file; file.open(fname.c_str(),ios_base::out); for (size_t row = 0; row < getNRows(Nx90); row++) { for (size_t col = 0; col < getNCols(Nx90); col++) { file << this->rotN90 (row,col,Nx90) << " "; } file << endl; } file.close(); cout << "The 2x1 image (ncols,nrows=" << this->getNCols(Nx90) << "," << this->getNRows(Nx90) << " with rotation by 90*" << Nx90 << "=" << Nx90*90 << " degree)" << " is saved in file " << fname << endl; }
int writeImage(const Image *im, const char *fname) { FILE *output; int nRows; int nCols; int colors; int i, j; /* open the file */ if (!fname || (output=fopen(fname,"wb"))==0) return(-1); nRows=getNRows(im); nCols=getNCols(im); colors=getColors(im); /* write the header */ fprintf(output,"P5\n"); /* magic number */ fprintf(output,"#\n"); /* empty comment */ fprintf(output,"%d %d\n%03d\n",nCols,nRows,colors); /* image info */ /* write pixels row by row */ for(i=0;i<nRows;i++) { for(j=0;j<nCols;j++) { int byte=getPixel(im,i,j); if (fputc(byte,output)==EOF) /* couldn't write */ { fclose(output); return -1; } } } /* close the file */ fclose(output); return 0; /* OK */ }
int FacilityLocation::createConsFacilityActivation(int) { int numCons = 0; VariableHash::iterator vit; Row::ROWSENSE sense = Row::LESS; int maxnnz = 2; double rhs = 0.0; int colVarX, colVarY, colVarZ; for (vit = vHash[Variable::V_X].begin(); vit != vHash[Variable::V_X].end(); ++vit) { colVarX = vit->second; Arc arc = vit->first.getArc(); Vertex router = vit->first.getArc().getHead(); Vertex client = vit->first.getArc().getTail(); Constraint c(maxnnz, sense, rhs); c.setType(Constraint::C_FACILITY_ACTIVATION); c.setClass(1); c.setArc(arc); Variable y; y.setType(Variable::V_Y); y.setVertex1(router); VariableHash::iterator aux = vHash[Variable::V_Y].find(y); if (aux != vHash[Variable::V_Y].end()) { colVarY = aux->second; c.rowAddVar(colVarX, 1.0); c.rowAddVar(colVarY, -1.0); } if (c.getRowNnz() > 0) { bool isInserted = addRow(&c); if (isInserted) { c.setRowIdx(getNRows() - 1); cHash[c] = c.getRowIdx(); numCons++; } } } for (vit = vHash[Variable::V_Z].begin(); vit != vHash[Variable::V_Z].end(); ++vit) { colVarZ = vit->second; Arc arc = vit->first.getArc(); Vertex head = vit->first.getArc().getHead(); Vertex tail = vit->first.getArc().getTail(); Constraint c1(maxnnz, sense, rhs); c1.setType(Constraint::C_FACILITY_ACTIVATION); c1.setClass(2); c1.setArc(arc); Variable yHead; yHead.setType(Variable::V_Y); yHead.setVertex1(head); VariableHash::iterator aux = vHash[Variable::V_Y].find(yHead); if (aux != vHash[Variable::V_Y].end()) { colVarY = aux->second; c1.rowAddVar(colVarZ, 1.0); c1.rowAddVar(colVarY, -1.0); } if (c1.getRowNnz() > 0) { bool isInserted = addRow(&c1); if (isInserted) { c1.setRowIdx(getNRows() - 1); cHash[c1] = c1.getRowIdx(); numCons++; } } Constraint c2(maxnnz, sense, rhs); c2.setType(Constraint::C_FACILITY_ACTIVATION); c2.setClass(2); c2.setArc(arc.reverse()); Variable yTail; yTail.setType(Variable::V_Y); yTail.setVertex1(tail); aux = vHash[Variable::V_Y].find(yTail); if (aux != vHash[Variable::V_Y].end()) { colVarY = aux->second; c2.rowAddVar(colVarZ, 1.0); c2.rowAddVar(colVarY, -1.0); } if (c2.getRowNnz() > 0) { bool isInserted = addRow(&c2); if (isInserted) { c2.setRowIdx(getNRows() - 1); cHash[c2] = c2.getRowIdx(); numCons++; } } } return numCons; }
int main(int argc, char *argv[]) { int threshold, rh, theta, rows, cols, rScale, tScale, hitImage, i; char *inputo, *inputh, *inpute, *outputl; float r, t, diag, x, y, c, s; Image io, ih, ie; if (argc < 5) { fprintf(stderr, "usage: %s <input original scene image> <input hough image> <input thresholded image> <hough threshold> <cropped line-detected output image>\n", argv[0]); exit(0); } inputo = argv[1]; inputh = argv[2]; inpute = argv[3]; if (sscanf(argv[4], "%d", &threshold) != 1) { fprintf(stderr, "error: threshold not an integer\n"); exit(1); } outputl = argv[5]; if (readImage(&io, inputo) == -1) { std::cerr << "Error reading file " << inputo << "\n"; exit(1); } else if (readImage(&ih, inputh) == -1) { std::cerr << "Error reading file " << inputh << "\n"; exit(1); } else if (readImage(&ie, inpute) == -1) { std::cerr << "Error reading file " << inpute << "\n"; exit(1); } rows = getNRows(&io); cols = getNCols(&io); diag = sqrt(pow(rows, 2) + pow(cols, 2)); rScale = getNRows(&ih); tScale = getNCols(&ih); for (rh = 0; rh < rScale; ++rh) { for (theta = 0; theta < tScale; ++theta) { if (getPixel(&ih, rh, theta) >= threshold) { r = ((2*diag)/rScale)*rh - diag; t = (PI/tScale)*theta - PI/2; c = cos(t); s = sin(t); x = -r*s; // cos(t + pi/2) = -sin(t) y = r*c; // sin(t + pi/2) = cos(t) if (inImage(&io, x, y)) { x -= diag*c; y -= diag*s; } hitImage = 0; i = 0; while (!hitImage || inImage(&io, x + i*c, y + i*s)) { if (!hitImage && inImage(&io, x + i*c, y + i*s)) hitImage = 1; if (hitImage && getPixel(&ie, y + i*s, x + i*c)) { setPixel(&io, y + i*s, x + i*c, 255); } i++; } } } } setColors(&io, 255); if (writeImage(&io, outputl) == -1) { std::cerr << "Error writing file " << outputl << "\n"; } free(io.data); free(ih.data); free(ie.data); }
int Flow::createConsDualFeasibility() { int ncons = 0; VariableHash::iterator vit; Row::ROWSENSE sense = Row::GREATER; int maxnnz = 4; double rhs = 0.0; for (int sIt = 0; sIt < g->nTerminals; ++sIt) { Vertex s = g->terminals[sIt]; for (int tIt = 0; tIt < g->nTerminals; ++tIt) { Vertex t = g->terminals[tIt]; for (int i = 1; i <= g->nVertices; ++i) { for (int j = 0; j < g->adjList[i].size(); ++j) { Arc arc = g->adjList[i][j]; Constraint c(maxnnz, sense, rhs); c.setType(Constraint::C_DUAL_FEASIBILITY); c.setS(s); c.setT(t); c.setArc(arc.toEdge()); Variable wp; wp.setType(Variable::VARTYPE::V_W); wp.setCategory('p'); wp.setVertex1(s); wp.setArc(arc.toEdge()); vit = vHash[Variable::VARTYPE::V_W].find(wp); if (vit != vHash[Variable::V_W].end()) { int col = vit->second; c.rowAddVar(col, 1.0); } Variable wm; wm.setType(Variable::VARTYPE::V_W); wm.setCategory('m'); wm.setVertex1(s); wm.setArc(arc.toEdge()); vit = vHash[Variable::VARTYPE::V_W].find(wm); if (vit != vHash[Variable::V_W].end()) { int col = vit->second; c.rowAddVar(col, 1.0); } Variable y; y.setType(Variable::VARTYPE::V_Y); y.setVertex1(s); y.setVertex2(t); y.setArc(arc); vit = vHash[Variable::VARTYPE::V_Y].find(y); if (vit != vHash[Variable::V_Y].end()) { int col = vit->second; c.rowAddVar(col, -1.0); } y.setArc(arc.reverse()); vit = vHash[Variable::VARTYPE::V_Y].find(y); if (vit != vHash[Variable::V_Y].end()) { int col = vit->second; c.rowAddVar(col, -1.0); } if (c.getRowNnz() > 0) { bool isInserted = addRow(&c); if (isInserted) { c.setRowIdx(getNRows() - 1); cHash[c] = c.getRowIdx(); ncons++; } } } } } } return ncons; }
int FacilityLocation::createConsEdgeActivation() { // TODO // Implementação do corte a seguir: os vértices roteadores da árvore VPN não poderão ser // folhas na solução. Isso implica que deverá existir pelo menos dois arcos com extremidada // em cada vértice roteador: // // 2 y_i \leq \sum\limits_{e \in \delta(i)} z_e + \sum\limits_{p \in P} x_{ip}, \forall i \in I int numCons = 0; VariableHash::iterator vit; Row::ROWSENSE sense = Row::LESS; int maxnnz = vHash[Variable::V_X].size() + vHash[Variable::V_Z].size() + 1; double rhs = 0.0; int colVarX, colVarZ, colVarY; for (auto & varY : vHash[Variable::V_Y]) { int colVarY = varY.second; Vertex vertex = varY.first.getVertex1(); Constraint c(maxnnz, sense, rhs); c.setType(Constraint::C_EDGE_ACTIVATION); c.setNode(vertex); c.rowAddVar(colVarY, 2.0); for (auto & varZ : vHash[Variable::V_Z]) { colVarZ = varZ.second; Arc arc = varZ.first.getArc(); //if (arc.getHead() == vertex.getCode() || arc.getTail() == vertex.getCode()) if (arc.isIncident(vertex)) { c.rowAddVar(colVarZ, -1.0); } } for (auto & varX : vHash[Variable::V_X]) { colVarX = varX.second; Arc arc = varX.first.getArc(); //if (arc.getHead() == vertex.getCode() || arc.getTail() == vertex.getCode()) if (arc.isIncident(vertex)) { c.rowAddVar(colVarX, -1.0); } } if (c.getRowNnz() > 0) { bool isInserted = addRow(&c); if (isInserted) { c.setRowIdx(getNRows() - 1); cHash[c] = c.getRowIdx(); numCons++; } } } return numCons; }
int FacilityLocation::createConsRadiusDistance() { // TODO // Implementação do corte a seguir: os vértices roteadores da árvore VPN não poderão ser // folhas na solução. Isso implica que deverá existir pelo menos dois arcos com extremidada // em cada vértice roteador: // // 2 y_i \leq \sum\limits_{e \in \delta(i)} z_e + \sum\limits_{p \in P} x_{ip}, \forall i \in I int numCons = 0; std::vector<std::vector<int> > dist((g->nVertices + 1), std::vector<int>((g->nVertices + 1), 0)); clock_t start, end; start = clock(); bfs(g, dist); end = clock(); #ifdef VERBOSE std::cout << "\nBFS_time: " << (end - start) / (double)CLOCKS_PER_SEC << "\n"; #endif for (auto & terminal : g->terminals) { // Identifyng all possible distances from node terminal std::set<int> distSet; for (auto & d : dist[terminal.getCode()]) { if (d > 1) distSet.insert(d); } printf(""); std::map<int, std::vector<Vertex>> auxp; for (auto & router : g->vertices) { // @annotation: comment next filter to resolve instances where // the vpn terminals may be considered internal nodes // 20160125 if (terminal == router || router.isTerminal()) continue; // The pair (terminal, router) corresponds to a variable x_ip // and now we know the distance (number of hops) between these // two nodes, given by d. int dtr = dist[terminal.getCode()][router.getCode()]; std::vector<Arc> path; findShortestPath(router, terminal, g, path); if (path.size() > 0) { Vertex element = (*path.rbegin()).getHead(); auxp[dtr].push_back(element); printf(""); } } printf(""); for (auto & eleDist : auxp) { int dist = eleDist.first; std::vector<Vertex> & elements = eleDist.second; std::vector<Vertex> & elementsConstrained = auxp[dist - 1]; for (int j = 0; j < elementsConstrained.size(); ++j) { Vertex vj = elementsConstrained[j]; Variable yj; yj.setType(Variable::V_Y); yj.setVertex1(vj); VariableHash::iterator yIt = vHash[Variable::V_Y].find(yj); if (yIt == vHash[Variable::V_Y].end()) continue; Constraint c(elements.size() + 2, Row::LESS, 1.0); c.setType(Constraint::C_UNKNOWN); c.setClass(dist); c.setNode(vj); c.rowAddVar(yIt->second, 1.0); //std::cout << c.toString() << ": " << yj.toString(); for (int i = 0; i < elements.size(); ++i) { Vertex router = elements[i]; Arc a = Arc(router, terminal, 0.); Variable xrt; xrt.setType(Variable::V_X); xrt.setArc(a); VariableHash::iterator xIt = vHash[Variable::V_X].find(xrt); if (xIt == vHash[Variable::V_X].end()) continue; c.rowAddVar(xIt->second, 1.0); //std::cout << " + " << xrt.toString(); } if (c.getRowNnz() > 1) { bool isInserted = addRow(&c); if (isInserted) { c.setRowIdx(getNRows() - 1); cHash[c] = c.getRowIdx(); numCons++; } } //std::cout << " <= " << 1 << std::endl; } } printf(""); } printf(""); return numCons; }
void getObjects(Image *im, ObjectDB *odb) { int i, j, px, a, b, c, jp, ip, rows=getNRows(im), cols=getNCols(im); float theta[2]; double E[2]; Object *obj; /* get area, calculate bounding boxes, and begin calculating centers */ for (i=0; i < rows; ++i) { for (j=0; j < cols; ++j) { px=getPixel(im, i, j); if (px > 0) { obj=odb->objs+px-1; if (i < obj->top) obj->top=i; if (i > obj->bottom) obj->bottom=i; if (j < obj->left) obj->left=j; if (j > obj->right) obj->right=j; obj->fm[0]+=i; obj->fm[1]+=j; obj->area++; } } } /* finish centers */ for (i=0; i < odb->nObjects; ++i) if (odb->objs[i].area > 0) { obj=odb->objs+i; for (j=0; j < DIM; ++j) obj->fm[j]/=obj->area; } /* calculate a, b, c */ for (i=0; i < rows; ++i) { for (j=0; j < cols; ++j) { px=getPixel(im, i, j); if (px > 0) { obj=odb->objs+px-1; ip=i - obj->fm[0]; jp=j - obj->fm[1]; obj->sm.a+=jp*jp; obj->sm.b+=2*jp*ip; obj->sm.c+=ip*ip; } } } /* calculate theatmin */ for (i=0; i < odb->nObjects; ++i) if (odb->objs[i].area > 0) { obj=odb->objs+i; a=obj->sm.a; b=obj->sm.b; c=obj->sm.c; theta[0]=0.5f*atan2(b, a-c); theta[1]=theta[0] + PI/2.0; E[0]=secondMoment(a, b, c, theta[0]); E[1]=secondMoment(a, b, c, theta[1]); if (E[0] > E[1]) { obj->sm.thetaMin=theta[1]; obj->sm.thetaMax=theta[0]; } else { obj->sm.thetaMin=theta[0]; obj->sm.thetaMax=theta[1]; } } }
int Flow::createConsCapacityDetermination() { int ncons = 0; VariableHash::iterator vit; Row::ROWSENSE sense = Row::LESS; int maxnnz = (2 * g->nTerminals) + 1; double rhs = 0.0; for (int i = 1; i <= g->nVertices; ++i) { for (int j = 0; j < g->adjList[i].size(); ++j) { Arc arc = g->adjList[i][j]; Constraint c(maxnnz, sense, rhs); c.setType(Constraint::C_CAPACITY); c.setArc(arc.toEdge()); for (int sIt = 0; sIt < g->nTerminals; ++sIt) { Vertex s = g->terminals[sIt]; double bPlus = s.getEgree(); double bMinus = s.getIngree(); Variable w; w.setType(Variable::VARTYPE::V_W); w.setCategory('p'); w.setVertex1(s); w.setArc(arc.toEdge()); vit = vHash[Variable::VARTYPE::V_W].find(w); if (vit != vHash[Variable::V_W].end()) { int col = vit->second; c.rowAddVar(col, bPlus); } w.setCategory('m'); vit = vHash[Variable::VARTYPE::V_W].find(w); if (vit != vHash[Variable::V_W].end()) { int col = vit->second; c.rowAddVar(col, bMinus); } } Variable x; x.setType(Variable::VARTYPE::V_X); x.setArc(arc.toEdge()); vit = vHash[Variable::VARTYPE::V_X].find(x); if (vit != vHash[Variable::V_X].end()) { int col = vit->second; c.rowAddVar(col, -1.0); } if (c.getRowNnz() > 0) { bool isInserted = addRow(&c); if (isInserted) { c.setRowIdx(getNRows() - 1); cHash[c] = c.getRowIdx(); ncons++; } } } } return ncons; }
int main(int nargin, char** argv){ if(nargin != 5){ printf("Usage: p5 <input gradient> <seed point> <mask> <output depth>"); return -1; } int index, i,j; mask = (Image*) malloc(sizeof(Image)); readImage(mask, argv[3]); FILE* file = fopen(argv[1], "rb"); FILE* seeds = fopen(argv[2], "r"); Gradient* grid = (Gradient*) malloc(sizeof(Gradient)*getNRows(mask)*getNCols(mask)); fread(grid, getNRows(mask)*getNCols(mask), sizeof(Gradient), file); double** points = (double**) malloc(sizeof(double)*getNRows(mask)); for(i = 0; i < getNRows(mask); i++) points[i] = (double*)malloc(sizeof(double)*getNCols(mask)); double* allpoints = (double*) malloc(sizeof(double)*getNRows(mask)*getNCols(mask)); char line[1024]; int r = getNRows(mask), c = getNCols(mask); int max = 0; while(fgets(line, 1024, seeds) != NULL){ int x0 = atoi(strtok(line, " ")); int y0 = atoi(strtok(NULL, " ")); for(i = 0; i < getNRows(mask); i++) for(j = 0; j < getNCols(mask); j++) points[i][j] = 0; int x, y; for(x = x0-1; x >=0; x--) points[x][y0] = (points[x+1][y0] + (grid+x*r+y0)->p); for(x = x0+1; x < r; x++) points[x][y0] = (points[x-1][y0] - (grid+x*r+y0)->p); for(y = y0-1; y >=0; y--) points[x0][y] = (points[x0][y+1] + (grid+x0*r+y)->q); for(y = y0+1; y < r; y++) points[x0][y] = (points[x0][y-1] - (grid+x0*r+y)->q); for(x = x0-1; x >=0; x--){ for(y = y0-1; y >=0; y--) points[x][y] = (0.5 * points[x][y+1] + (grid+x*r+y)->q + 0.5 * points[x+1][y] + (grid+x*r+y)->p); for(y = y0+1; y < r; y++) points[x][y] = (0.5 * points[x][y-1] - (grid+x*r+y)->q + 0.5 * points[x+1][y] + (grid+x*r+y)->p); } for(x = x0+1; x < r; x++){ for(y = y0-1; y >=0; y--) points[x][y] = (0.5 * points[x][y+1] + (grid+x*r+y)->q + 0.5 * points[x-1][y] - (grid+x*r+y)->p); for(y = y0+1; y < r; y++) points[x][y] = (0.5 * points[x][y-1] - (grid+x*r+y)->q + 0.5 * points[x-1][y] - (grid+x*r+y)->p); } for(x = 0; x < r; x++) for(y = 0; y < r; y++){ allpoints[x*r+y] += points[x][y]; max = (allpoints[x*r+y] > max) ? allpoints[x*r+y] : max; } } Image* im = (Image*) malloc(sizeof(Image)); setSize(im, r, c); setColors(im, 255); int x,y; for(x = 0; x < r; x++) for(y = 0; y < c; y++) setPixel(im, x, y, getPixel(mask,x,y) ? allpoints[x*r+y] / (double) max * 255 : 0); writeImage(im, argv[4]); }
int Flow::createConsFlowConservation() { int ncons = 0; VariableHash::iterator vit; Row::ROWSENSE sense = Row::EQUAL; int maxnnz = 0; double rhs = 0.0; for (int iIt = 1; iIt <= g->nVertices; ++iIt) { Vertex i = g->vertices[iIt]; for (int sIt = 0; sIt < g->nTerminals; ++sIt) { Vertex s = g->terminals[sIt]; for (int tIt = 0; tIt < g->nTerminals; ++tIt) { Vertex t = g->terminals[tIt]; if (s.getCode() == t.getCode()) continue; if (i.getCode() == s.getCode()) rhs = 1.0; else if (i.getCode() == t.getCode()) rhs = -1.0; else rhs = 0.0; maxnnz = (2 * g->adjList[iIt].size()) + 1; Constraint c(maxnnz, sense, rhs); c.setType(Constraint::C_FLOW_CONSERVATION); c.setNode(i); c.setS(s); c.setT(t); for (int j = 0; j < g->adjList[iIt].size(); ++j) { Arc arc = g->adjList[iIt][j]; Variable y; y.setType(Variable::VARTYPE::V_Y); y.setVertex1(s); y.setVertex2(t); y.setArc(arc); vit = vHash[Variable::VARTYPE::V_Y].find(y); if (vit != vHash[Variable::V_Y].end()) { int col = vit->second; c.rowAddVar(col, 1.0); } y.setArc(arc.reverse()); vit = vHash[Variable::VARTYPE::V_Y].find(y); if (vit != vHash[Variable::V_Y].end()) { int col = vit->second; c.rowAddVar(col, -1.0); } } if (c.getRowNnz() > 0) { bool isInserted = addRow(&c); if (isInserted) { c.setRowIdx(getNRows() - 1); cHash[c] = c.getRowIdx(); ncons++; } } } } } return ncons; }