static int _nwunsch(char *a, char *b, int gap) { int *arow, *brow, *trow; int alen, blen; int i, j; int res; alen = strlen(a); blen = strlen(b); elog(DEBUG2, "alen: %d; blen: %d", alen, blen); if (alen == 0) return blen; if (blen == 0) return alen; arow = (int *) malloc((blen + 1) * sizeof(int)); brow = (int *) malloc((blen + 1) * sizeof(int)); if (arow == NULL) elog(ERROR, "memory exaushted for array size %d", (alen + 1)); if (brow == NULL) elog(ERROR, "memory exaushted for array size %d", (blen + 1)); #ifdef PGS_IGNORE_CASE elog(DEBUG2, "case-sensitive turns off"); for (i = 0; i < alen; i++) a[i] = tolower(a[i]); for (j = 0; j < blen; j++) b[j] = tolower(b[j]); #endif /* initial values */ for (i = 0; i <= blen; i++) arow[i] = gap * i; for (i = 1; i <= alen; i++) { /* first value is 'i' */ brow[0] = gap * i; for (j = 1; j <= blen; j++) { /* TODO change it to a callback function */ /* get operation cost */ int scost = nwcost(a[i - 1], b[j - 1]); brow[j] = max3(brow[j - 1] + gap, arow[j] + gap, arow[j - 1] + scost); elog(DEBUG2, "(i, j) = (%d, %d); cost(%c, %c): %d; max(top, left, diag) = (%d, %d, %d) = %d", i, j, a[i - 1], b[j - 1], scost, brow[j - 1] + gap, arow[j] + gap, arow[j - 1] + scost, brow[j]); } /* * below row becomes above row * above row is reused as below row */ trow = arow; arow = brow; brow = trow; } res = arow[blen]; free(arow); free(brow); return res; }
/* * TODO move this function to similarity.c */ static double _smithwatermangotoh(char *a, char *b) { float **matrix; /* dynamic programming matrix */ int alen, blen; int i, j; double maxvalue; alen = strlen(a); blen = strlen(b); elog(DEBUG2, "alen: %d; blen: %d", alen, blen); if (alen == 0) return blen; if (blen == 0) return alen; matrix = (float **) malloc((alen + 1) * sizeof(float *)); if (matrix == NULL) elog(ERROR, "memory exaushted for array size %d", alen); for (i = 0; i <= alen; i++) { matrix[i] = (float *) malloc((blen + 1) * sizeof(float)); if (matrix == NULL) elog(ERROR, "memory exaushted for array size %d", blen); } #ifdef PGS_IGNORE_CASE elog(DEBUG2, "case-sensitive turns off"); for (i = 0; i < alen; i++) a[i] = tolower(a[i]); for (j = 0; j < blen; j++) b[j] = tolower(b[j]); #endif maxvalue = 0.0; /* initial values */ for (i = 0; i <= alen; i++) { float c = megapcost(a, b, i, 0); if (i == 0) { matrix[0][0] = max2(0.0, c); } else { float maxgapcost = 0.0; int wstart = i - PGS_SWG_WINDOW_SIZE; int k; if (wstart < 1) wstart = 1; for (k = wstart; k < i; k++) maxgapcost = max2(maxgapcost, matrix[i - k][0] - swggapcost(i - k, i)); matrix[i][0] = max3(0.0, maxgapcost, c); } if (matrix[i][0] > maxvalue) maxvalue = matrix[i][0]; } for (j = 0; j <= blen; j++) { float c = megapcost(a, b, 0, j); if (j == 0) { matrix[0][0] = max2(0.0, c); } else { float maxgapcost = 0.0; int wstart = j - PGS_SWG_WINDOW_SIZE; int k; if (wstart < 1) wstart = 1; for (k = wstart; k < j; k++) maxgapcost = max2(maxgapcost, matrix[0][j - k] - swggapcost(j - k, j)); matrix[0][j] = max3(0.0, maxgapcost, c); } if (matrix[0][j] > maxvalue) maxvalue = matrix[0][j]; } for (i = 1; i <= alen; i++) { for (j = 1; j <= blen; j++) { int wstart; int k; float maxgapcost1 = 0.0, maxgapcost2 = 0.0; /* get operation cost */ float c = megapcost(a, b, i, j); wstart = i - PGS_SWG_WINDOW_SIZE; if (wstart < 1) wstart = 1; for (k = wstart; k < i; k++) maxgapcost1 = max2(maxgapcost1, matrix[i - k][0] - swggapcost(i - k, i)); wstart = j - PGS_SWG_WINDOW_SIZE; if (wstart < 1) wstart = 1; for (k = wstart; k < j; k++) maxgapcost2 = max2(maxgapcost2, matrix[0][j - k] - swggapcost(j - k, j)); matrix[i][j] = max4(0.0, maxgapcost1, maxgapcost2, matrix[i-1][j-1] + c); elog(DEBUG2, "(i, j) = (%d, %d); cost(%c, %c): %.3f; max(zero, top, left, diag) = (0.0, %.3f, %.3f, %.3f) = %.3f", i, j, a[i-1], b[j-1], c, maxgapcost1, maxgapcost2, matrix[i-1][j-1] + c, matrix[i][j]); if (matrix[i][j] > maxvalue) maxvalue = matrix[i][j]; } } for (i = 0; i <= alen; i++) free(matrix[i]); free(matrix); return maxvalue; }
//Read in and load the model bool readin(char *FileName) { const int MAX_MATERIAL_COUNT = 255; glm::vec3 ambient[MAX_MATERIAL_COUNT], diffuse[MAX_MATERIAL_COUNT], specular[MAX_MATERIAL_COUNT]; GLfloat x_temp, y_temp, z_temp; GLfloat shine[MAX_MATERIAL_COUNT]; char ch; FILE* fp = fopen(FileName, "r"); if (fp == NULL){ printf(FileName, "doesn't exist"); exit(1); } fscanf(fp, "%c", &ch); while (ch != '\n') fscanf(fp, "%c", &ch); fscanf(fp, "# triangles = %d\n", &NumTris); fscanf(fp, "Material count = %d\n", &material_count); Mate = new material[material_count]; for (int i = 0; i < material_count; i++){ fscanf(fp, "ambient color %f %f %f\n", &(Mate[i].ambient.x), &(Mate[i].ambient.y), &(Mate[i].ambient.z)); fscanf(fp, "diffuse color %f %f %f\n", &(Mate[i].diffuse.x), &(Mate[i].diffuse.y), &(Mate[i].diffuse.z)); fscanf(fp, "specular color %f %f %f\n", &(Mate[i].specular.x), &(Mate[i].specular.y), &(Mate[i].specular.z)); fscanf(fp, "material shine %f\n", &(Mate[i].shine)); } fscanf(fp, "%c", &ch); while (ch != '\n') // skip documentation line fscanf(fp, "%c", &ch); Tris = new triangle[NumTris]; for (int i = 0; i<NumTris; i++) // read triangles { fscanf(fp, "v0 %f %f %f %f %f %f %d\n", &(Tris[i].v0.x), &(Tris[i].v0.y), &(Tris[i].v0.z), &(Tris[i].normal[0].x), &(Tris[i].normal[0].y), &(Tris[i].normal[0].z), &(Tris[i].Color[0])); fscanf(fp, "v1 %f %f %f %f %f %f %d\n", &(Tris[i].v1.x), &(Tris[i].v1.y), &(Tris[i].v1.z), &(Tris[i].normal[1].x), &(Tris[i].normal[1].y), &(Tris[i].normal[1].z), &(Tris[i].Color[1])); fscanf(fp, "v2 %f %f %f %f %f %f %d\n", &(Tris[i].v2.x), &(Tris[i].v2.y), &(Tris[i].v2.z), &(Tris[i].normal[2].x), &(Tris[i].normal[2].y), &(Tris[i].normal[2].z), &(Tris[i].Color[2])); fscanf(fp, "face normal %f %f %f\n", &(Tris[i].face_normal.x), &(Tris[i].face_normal.y), &(Tris[i].face_normal.z)); //Get the minimum and maximum value to calculate the position x_temp = min3(Tris[i].v0.x, Tris[i].v1.x, Tris[i].v2.x); y_temp = min3(Tris[i].v0.y, Tris[i].v1.y, Tris[i].v2.y); z_temp = min3(Tris[i].v0.z, Tris[i].v1.z, Tris[i].v2.z); x_min = x_min < x_temp ? x_min : x_temp; y_min = y_min < y_temp ? y_min : y_temp; z_min = z_min < z_temp ? z_min : z_temp; x_temp = max3(Tris[i].v0.x, Tris[i].v1.x, Tris[i].v2.x); y_temp = max3(Tris[i].v0.y, Tris[i].v1.y, Tris[i].v2.y); z_temp = max3(Tris[i].v0.z, Tris[i].v1.z, Tris[i].v2.z); x_min = x_min > x_temp ? x_min : x_temp; y_min = y_min > y_temp ? y_min : y_temp; z_min = z_min > z_temp ? z_min : z_temp; vertices.push_back(Tris[i].v0); normals.push_back(Tris[i].normal[0]); mindices.push_back(Tris[i].Color[0]); vertices.push_back(Tris[i].v1); normals.push_back(Tris[i].normal[1]); mindices.push_back(Tris[i].Color[1]); vertices.push_back(Tris[i].v2); normals.push_back(Tris[i].normal[2]); mindices.push_back(Tris[i].Color[2]); } fclose(fp); return true; }
/* ** Use the clip box to generate a series of triangles that ** together make up the clipped portion of the source triangle. ** "clippath" should have at least 16 allocated elements. */ int render_clipped_triangle(POINT2D *clippath,int *nc, RENDER_CLIPBOX *box,TRIANGLE2D *triangle) { TRIANGLE2D _tri,*tri; double xmin,xmax; static POINT2D vertex[13],cp[4]; static int clipedge[13],vin[4]; static double position[13]; int iv,nv,i,j,ic; double x,y; tri=&_tri; (*tri) = (*triangle); (*nc)=0; tri2d_sort_ypoints(tri); rtol=p2d_dist(box->p[0],box->p[1])/1e5; if (tri->p[0].y >= box->p[1].y || tri->p[2].y <= box->p[0].y) return(0); xmin = min3(tri->p[0].x,tri->p[1].x,tri->p[2].x); xmax = max3(tri->p[0].x,tri->p[1].x,tri->p[2].x); if (xmin >= box->p[1].x || xmax <= box->p[0].x) return(0); iv=0; for (i=0;i<10;i++) clipedge[i]=0; for (i=0;i<4;i++) vin[i]=0; for (i=0;i<3;i++) { int i1,i2; i1 = i; i2 = (i+1)%3; if (tri->p[i1].x >= box->p[0].x && tri->p[i1].x <= box->p[1].x && tri->p[i1].y >= box->p[0].y && tri->p[i1].y <= box->p[1].y) { vertex[iv] = tri->p[i1]; position[iv] = i; if (tri->p[i1].x == box->p[0].x) clipedge[iv] |= 2; else if (tri->p[i1].x == box->p[1].x) clipedge[iv] |= 8; else if (tri->p[i1].y == box->p[0].y) clipedge[iv] |= 1; else if (tri->p[i1].y == box->p[1].y) clipedge[iv] |= 4; else clipedge[iv] = 0; iv++; } for (j=0;j<2;j++) { /* side 1 */ if ((tri->p[i1].x < box->p[j].x && tri->p[i2].x > box->p[j].x) || (tri->p[i1].x > box->p[j].x && tri->p[i2].x < box->p[j].x)) { POINT2D pp1,pp2; y = p2d_y_intercept(box->p[j].x,tri->p[i1],tri->p[i2]); pp1.x = pp2.x = box->p[j].x; pp1.y = y; pp2.y = box->p[0].y; /* Check if it's close to a corner. */ if (render_p2d_same(pp1,pp2)) y=box->p[0].y; else { pp2.y = box->p[1].y; if (render_p2d_same(pp1,pp2)) y=box->p[1].y; } if (y>=box->p[0].y && y<= box->p[1].y) { vertex[iv].x = box->p[j].x; vertex[iv].y = y; clipedge[iv] |= 1<<(j*2+1); position[iv] = i+(box->p[j].x-tri->p[i1].x) /(tri->p[i2].x-tri->p[i1].x); iv++; } } /* side 2 */ if ((tri->p[i1].y < box->p[j].y && tri->p[i2].y > box->p[j].y) || (tri->p[i1].y > box->p[j].y && tri->p[i2].y < box->p[j].y)) { POINT2D pp1,pp2; x = p2d_x_intercept(box->p[j].y,tri->p[i1],tri->p[i2]); pp1.x = x; pp1.y = pp2.y = box->p[j].y; pp2.x = box->p[0].x; /* Check if it's close to a corner. */ if (render_p2d_same(pp1,pp2)) x=box->p[0].x; else { pp2.x = box->p[1].x; if (render_p2d_same(pp1,pp2)) x=box->p[1].x; } if (x>= box->p[0].x && x<= box->p[1].x) { vertex[iv].x = x; vertex[iv].y = box->p[j].y; clipedge[iv] |= 1<<(j*2); position[iv] = i+(box->p[j].y-tri->p[i1].y) /(tri->p[i2].y-tri->p[i1].y); iv++; } } } } nv=iv; cp[0] = box->p[0]; cp[1] = p2d_point(box->p[0].x,box->p[1].y); cp[2] = box->p[1]; cp[3] = p2d_point(box->p[1].x,box->p[0].y); /* Check any unevaluated corners to see if they're in. */ for (ic=i=0;i<4;i++) { if (!vin[i]) vin[i] = tri2d_point_inside(tri,cp[i]); if (vin[i]) { ic++; } } vertex_sort(vertex,clipedge,position,nv); /* Remove duplicate points */ for (i=0;i<nv;i++) for (j=i+1;j<nv;j++) if (render_p2d_same(vertex[i],vertex[j])) { int k; for (k=j+1;k<nv;k++) { vertex[k-1]=vertex[k]; clipedge[k-1]=clipedge[k]; position[k-1]=position[k]; } j--; nv--; } if (ic==4 || nv<2) { if (ic>1) { clippath[0] = box->p[0]; clippath[1] = p2d_point(box->p[0].x,box->p[1].y); clippath[2] = box->p[1]; clippath[3] = p2d_point(box->p[1].x,box->p[0].y); (*nc)=4; return(2); } else return(0); } /* Now figure out the clip path */ iv=0; clippath[iv++] = vertex[0]; for (i=1;i<=nv;i++) { int i2; i2=i%nv; if (clipedge[i-1]>0 && clipedge[i2]>0 && (clipedge[i-1]&clipedge[i2])==0) { int jend; jend = (corner_before(clipedge[i2])+1)%4; for (j=corner_after(clipedge[i-1]);j!=jend;j=(j+1)%4) if (!vin[j]) break; if (j==jend) { for (j=corner_after(clipedge[i-1]);j!=jend;j=(j+1)%4) { if (render_p2d_same(cp[j],clippath[iv-1])) continue; clippath[iv++]=cp[j]; } } else { jend = (corner_after(clipedge[i2])+3)%4; for (j=corner_before(clipedge[i-1]);j!=jend;j=(j+3)%4) if (!vin[j]) break; if (j==jend) { for (j=corner_before(clipedge[i-1]);j!=jend;j=(j+3)%4) { if (render_p2d_same(cp[j],clippath[iv-1])) continue; clippath[iv++]=cp[j]; } } } } if (!render_p2d_same(clippath[iv-1],vertex[i2])) clippath[iv++]=vertex[i2]; if (i==nv-1 && nv==2) { iv++; break; } } nv = iv-1; for (i=0;i<nv;i++) for (j=i+1;j<nv;j++) if (render_p2d_same(clippath[i],clippath[j])) { int k; for (k=j+1;k<nv;k++) clippath[k-1]=clippath[k]; j--; nv--; } /* Add any corners that are in but weren't included */ for (i=0;i<4;i++) if (vin[i]) { for (j=0;j<nv;j++) if (render_p2d_same(clippath[j],cp[i])) break; if (j>=nv) clippath[nv++] = cp[i]; } (*nc)=nv; return(1); }
/* ** ** Flushed a lot of bugs out of this on 9-26-06. ** ** Draw 3D triangle using Z-buffer to correctly display triangles in front ** of each other. ** */ void render_triangle_3d(WILLUSBITMAP *bmp,double *zbuffer, TRIANGLE3D *srctri,RENDER_COLOR *color, RENDER_COLOR *edge_color) { double x1,y1,z1,x2,y2,z2,x3,y3,z3,ylast; double px1,py1,px2,py2,px3,py3,ldx,rdx,ldy,rdy,ldz,rdz; double x1clip,x2clip,y1clip,y2clip; double lz,rz; int yi,yf,lx,rx,y,edgeflag; // printf("render_triangle_3d.\n"); x1 = srctri->p[0].x; y1 = srctri->p[0].y; z1 = srctri->p[0].z; x2 = srctri->p[1].x; y2 = srctri->p[1].y; z2 = srctri->p[1].z; x3 = srctri->p[2].x; y3 = srctri->p[2].y; z3 = srctri->p[2].z; // printf("/td xy.\n"); // printf(" %9.5f %9.5f %9.5f\n",x1,y1,z1); // printf(" %9.5f %9.5f %9.5f\n",x2,y2,z2); // printf(" %9.5f %9.5f %9.5f\n",x3,y3,z3); // printf(" %9.5f %9.5f %9.5f\n",x1,y1,z1); // printf("//nc\n"); x1clip=0; x2clip=bmp->width; y1clip=0; y2clip=bmp->height; px1=x1*bmp->width; py1=y1*bmp->height; px2=x2*bmp->width; py2=y2*bmp->height; px3=x3*bmp->width; py3=y3*bmp->height; if (py1>py2) pswap3d(px1,py1,z1,px2,py2,z2); if (py2>py3) pswap3d(px2,py2,z2,px3,py3,z3); if (py1>py2) pswap3d(px1,py1,z1,px2,py2,z2); if (py1>y2clip || py3<y1clip) return; if (py1==py2 && py2==py3) { lx = min3(px1,px2,px3); rx = max3(px1,px2,px3); if (lx>x2clip || rx<x1clip) return; if (lx<x1clip) lx=x1clip; if (rx>x2clip) rx=x2clip; } if (py1==py3) x1=(double)(px1+px3)/2.; else x1=px1+(double)(px3-px1)*(double)(py2-py1)/(double)(py3-py1); if (py2>=y1clip && py2!=py1) { yi = floor((py1>y1clip ? py1 : y1clip)+.5); yf = floor((py2<y2clip ? py2 : y2clip)-.5); if (x1>(double)px2) { ldx=px2-px1; rdx=px3-px1; ldy=py2-py1; rdy=py3-py1; ldz=z2-z1; rdz=z3-z1; } else { ldx=px3-px1; rdx=px2-px1; ldy=py3-py1; rdy=py2-py1; ldz=z3-z1; rdz=z2-z1; } for (y=yi;y<=yf;y++) { double lx1,rx1,lz1,rz1,dx,dz; lx1=px1+ldx*(y-py1)/ldy; rx1=px1+rdx*(y-py1)/rdy; dx=rx1-lx1; lx=floor((px1+ldx*(y+.5-py1)/ldy)+.5); rx=floor((px1+rdx*(y+.5-py1)/rdy)-.5); if (lx>rx) continue; if (lx>x2clip || rx<x1clip) continue; edgeflag=3; if (lx<x1clip) { edgeflag&=(~1); lx=x1clip; } if (rx>x2clip) { edgeflag&=(~2); rx=x2clip; } if (lx>rx) continue; // lz = ldx==0 ? (z1+ldz/2.) : z1 + ldz*(lx-px1)/ldx; // rz = rdx==0 ? (z1+rdz/2.) : z1 + rdz*(rx-px1)/rdx; lz1 = z1+ldz*(y-py1)/ldy; rz1 = z1+rdz*(y-py1)/rdy; dz = rz1-lz1; if (dx==0) { lz=lz1; rz=rz1; } else { lz=lz1+(lx-lx1)*dz/dx; rz=lz1+(rx-lx1)*dz/dx; } if ((y==py1 && y==py2) || (y==py2 && y==py3)) edgeflag|=4; render_horizontal_3d(bmp,zbuffer,lx,rx,y,lz,rz,color, edgeflag,edge_color); } } ylast=py2; if (ylast<=y2clip && py2!=py3) { yi = floor((ylast>y1clip ? ylast : y1clip)+.5); yf = floor((py3<y2clip ? py3 : y2clip)-.5); if (x1>px2) { ldx=px2-px3; rdx=px1-px3; ldy=py3-py2; rdy=py3-py1; ldz=z2-z3; rdz=z1-z3; } else { ldx=px1-px3; rdx=px2-px3; ldy=py3-py1; rdy=py3-py2; ldz=z1-z3; rdz=z2-z3; } for (y=yi;y<=yf;y++) { double lx1,rx1,lz1,rz1,dx,dz; lx1=px3+ldx*(py3-y)/ldy; rx1=px3+rdx*(py3-y)/rdy; dx=rx1-lx1; lx=floor((px3+ldx*(py3-(y+.5))/ldy)+.5); rx=floor((px3+rdx*(py3-(y+.5))/rdy)-.5); edgeflag=3; if (lx>x2clip || rx<x1clip) continue; if (lx<x1clip) { edgeflag&=(~1); lx=x1clip; } if (rx>x2clip) { edgeflag&=(~2); rx=x2clip; } if (lx>rx) continue; // lz = ldx==0 ? (z3+ldz/2.) : z3 + ldz*(lx-px3)/ldx; // rz = rdx==0 ? (z3+rdz/2.) : z3 + rdz*(rx-px3)/rdx; lz1 = z3+ldz*(py3-y)/ldy; rz1 = z3+rdz*(py3-y)/rdy; dz = rz1-lz1; if (dx==0) { lz=lz1; rz=rz1; } else { lz=lz1+(lx-lx1)*dz/dx; rz=lz1+(rx-lx1)*dz/dx; } if ((y==py1 && y==py2) || (y==py2 && y==py3)) edgeflag|=4; render_horizontal_3d(bmp,zbuffer,lx,rx,y,lz,rz,color, edgeflag,edge_color); } } }
void ot_index_box(const Vector3d& min_point, const Vector3d& max_point, OT_ID *id) { // TODO OPTIMIZE DBL dx, dy, dz, desiredSize; DBL bsized, maxord; POW2OP_DECLARE() // Calculate the absolute minimum required size of the node, assuming it is perfectly centered within the node; // Node size must be a power of 2, and be large enough to accomodate box's biggest dimensions with maximum overhang to all sides // compute ideal size of the node for a perfect fit without any overhang dx = max_point.x() - min_point.x(); dy = max_point.y() - min_point.y(); dz = max_point.z() - min_point.z(); desiredSize = max3(dx, dy, dz); // compute ideal size of the node for a perfect fit with full overhang to all sides // desiredSize /= (1 + 2 * 0.5); // compute best-matching power-of-two size for a perfect fit with overhang // (Note: theoretically this might pick a size larger than required if desiredSize is already a power of two) // desiredSize *= 2.0; POW2OP_FLOOR(bsized,desiredSize) // avoid divisions by zero if(bsized == 0.0) bsized = 1.0; #ifdef SAFE_METHOD // This block checks for the case where the node id would cause integer // overflow, since it is a small buffer far away maxord = max3(fabs(min_point[X]), fabs(min_point[Y]), fabs(min_point[Z])); maxord += OT_BIAS; while (maxord / bsized > 1000000000.0) { #ifdef RADSTATS overflows++; #endif bsized *= 2.0; } #endif // SAFE_METHOD // The node we chose so far would be ideal for a box of identical size positioned at the node's center, // but the actual box is probably somewhat off-center and therefore may have excessive overhang in some directions; // check and possibly fix this. Vector3d center = (min_point + max_point) / 2; id->x = (int) floor((center[X] + OT_BIAS) / bsized); id->y = (int) floor((center[Y] + OT_BIAS) / bsized); id->z = (int) floor((center[Z] + OT_BIAS) / bsized); POW2OP_ENCODE(id->Size, bsized) #ifdef RADSTATS thisloops = 0; #endif while (!ot_point_in_node(min_point, id) || !ot_point_in_node(max_point, id)) { // Debug_Info("looping %d,%d,%d,%d min=%d, max=%d\n", test_id.x, test_id.y, // test_id.z, test_id.Size, ot_point_in_node(min_point, &test_id), // ot_point_in_node(max_point, &test_id)); ot_parent(id, id); #ifdef RADSTATS totloops++; thisloops++; #endif } #ifdef RADSTATS if (thisloops < minloops) minloops = thisloops; if (thisloops > maxloops) maxloops = thisloops; #endif #ifdef OT_DEBUG if (id->Size > 139) { Debug_Info("unusually large id, maxdel=%.4f, bsized=%.4f, isize=%d\n", maxdel, bsized, id->Size); } #endif }
static void nextcolumn (const Limdfsconstinfo *lci, LocaliColumn *outcol, const GtUchar dbchar, const LocaliColumn *incol) { GtUword i; #ifndef AFFINE Scoretype temp; #endif gt_assert(outcol != incol); gt_assert(outcol->colvalues != incol->colvalues); gt_assert(incol->lenval >= lci->querylength+1); if (outcol->lenval < lci->querylength+1) { REALLOCMSG(outcol); outcol->colvalues = gt_realloc(outcol->colvalues, sizeof (LocaliMatrixvalue) * lci->maxcollen); outcol->lenval = lci->maxcollen; ATADDRESS("",outcol); } gt_assert(outcol->lenval >= lci->querylength+1); #ifdef AFFINE outcol->colvalues[0].repcell = outcol->colvalues[0].delcell = MINUSINFTY; if (incol->colvalues[0].inscell > 0) { if (incol->colvalues[0].bestcell > 0) { outcol->colvalues[0].inscell = max2 (incol->colvalues[0].inscell + lci->scorevalues.gapextend, incol->colvalues[0].bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend); } else { outcol->colvalues[0].inscell = incol->colvalues[0].inscell + lci->scorevalues.gapextend; } } else { if (incol->colvalues[0].bestcell > 0) { outcol->colvalues[0].inscell = incol->colvalues[0].bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend; } else { outcol->colvalues[0].inscell = MINUSINFTY; } } outcol->colvalues[0].bestcell = max3 (outcol->colvalues[0].repcell, outcol->colvalues[0].inscell, outcol->colvalues[0].delcell); #else outcol->colvalues[0].bestcell = MINUSINFTY; outcol->colvalues[0].tracebit = Notraceback; #endif outcol->maxvalue = 0; outcol->pprefixlen = 0; for (i = 1UL; i <= lci->querylength; i++) { #ifdef AFFINE if (incol->colvalues[i-1].bestcell > 0) { outcol->colvalues[i].repcell = incol->colvalues[i-1].bestcell + REPLACEMENTSCORE(&lci->scorevalues, dbchar,lci->query[i-1]); } else { outcol->colvalues[i].repcell = MINUSINFTY; } if (incol->colvalues[i].inscell > 0) { if (incol->colvalues[i].bestcell > 0) { outcol->colvalues[i].inscell = max2 (incol->colvalues[i].inscell + lci->scorevalues.gapextend, incol->colvalues[i].bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend); } else { outcol->colvalues[i].inscell = incol->colvalues[i].inscell + lci->scorevalues.gapextend; } } else { if (incol->colvalues[i].bestcell > 0) { outcol->colvalues[i].inscell = incol->colvalues[i].bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend; } else { outcol->colvalues[i].inscell = MINUSINFTY; } } if (outcol->colvalues[i-1].delcell > 0) { if (outcol->colvalues[i-1].bestcell > 0) { outcol->colvalues[i].delcell = max2 (outcol->colvalues[i-1].delcell + lci->scorevalues.gapextend, outcol->colvalues[i-1].bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend); } else { outcol->colvalues[i].delcell = outcol->colvalues[i-1].delcell + lci->scorevalues.gapextend; } } else { if (outcol->colvalues[i-1].bestcell > 0) { outcol->colvalues[i].delcell = outcol->colvalues[i-1].bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend; } else { outcol->colvalues[i].delcell = MINUSINFTY; } } outcol->colvalues[i].bestcell = max3 (outcol->colvalues[i].repcell, outcol->colvalues[i].inscell, outcol->colvalues[i].delcell); #else outcol->colvalues[i].bestcell = MINUSINFTY; outcol->colvalues[i].tracebit = Notraceback; if (outcol->colvalues[i-1].bestcell > 0) { IDXLOCALIDP_UPDATEMAX(outcol->colvalues[i-1].bestcell + lci->scorevalues.gapextend, Deletebit); } if (incol->colvalues[i-1].bestcell > 0) { IDXLOCALIDP_UPDATEMAX(incol->colvalues[i-1].bestcell + REPLACEMENTSCORE(&lci->scorevalues, dbchar,lci->query[i-1]),Replacebit); } if (incol->colvalues[i].bestcell > 0) { IDXLOCALIDP_UPDATEMAX(incol->colvalues[i].bestcell + lci->scorevalues.gapextend, Insertbit); } #endif if (outcol->colvalues[i].bestcell > 0 && outcol->colvalues[i].bestcell > (Scoretype) outcol->maxvalue) { outcol->maxvalue = (GtUword) outcol->colvalues[i].bestcell; outcol->pprefixlen = i; } } }
static void exynos5250_monitor(struct busfreq_data *data, struct opp **mif_opp, struct opp **int_opp) { int i; unsigned int cpu_load_average = 0; unsigned int dmc_c_load_average = 0; unsigned int dmc_l_load_average = 0; unsigned int dmc_r1_load_average = 0; unsigned int dmc_load_average; unsigned long cpufreq = 0; unsigned long lockfreq; unsigned long dmcfreq; unsigned long cpu_load; unsigned long dmc_load; unsigned long dmc_c_load; unsigned long dmc_r1_load; unsigned long dmc_l_load; struct opp *opp[PPMU_TYPE_END]; unsigned long newfreq[PPMU_TYPE_END]; ppmu_update(data->dev[PPMU_MIF], 3); /* Convert from base xxx to base maxfreq */ cpu_load = div64_u64(ppmu_load[PPMU_CPU] * data->curr_freq[PPMU_MIF], data->max_freq[PPMU_MIF]); dmc_c_load = div64_u64(ppmu_load[PPMU_DDR_C] * data->curr_freq[PPMU_MIF], data->max_freq[PPMU_MIF]); dmc_r1_load = div64_u64(ppmu_load[PPMU_DDR_R1] * data->curr_freq[PPMU_MIF], data->max_freq[PPMU_MIF]); dmc_l_load = div64_u64(ppmu_load[PPMU_DDR_L] * data->curr_freq[PPMU_MIF], data->max_freq[PPMU_MIF]); data->load_history[PPMU_CPU][data->index] = cpu_load; data->load_history[PPMU_DDR_C][data->index] = dmc_c_load; data->load_history[PPMU_DDR_R1][data->index] = dmc_r1_load; data->load_history[PPMU_DDR_L][data->index++] = dmc_l_load; if (data->index >= LOAD_HISTORY_SIZE) data->index = 0; for (i = 0; i < LOAD_HISTORY_SIZE; i++) { cpu_load_average += data->load_history[PPMU_CPU][i]; dmc_c_load_average += data->load_history[PPMU_DDR_C][i]; dmc_r1_load_average += data->load_history[PPMU_DDR_R1][i]; dmc_l_load_average += data->load_history[PPMU_DDR_L][i]; } /* Calculate average Load */ cpu_load_average /= LOAD_HISTORY_SIZE; dmc_c_load_average /= LOAD_HISTORY_SIZE; dmc_r1_load_average /= LOAD_HISTORY_SIZE; dmc_l_load_average /= LOAD_HISTORY_SIZE; if (dmc_c_load >= dmc_r1_load) { dmc_load = dmc_c_load; dmc_load_average = dmc_c_load_average; } else { dmc_load = dmc_r1_load; dmc_load_average = dmc_r1_load_average; } if (dmc_l_load >= dmc_load) { dmc_load = dmc_l_load; dmc_load_average = dmc_l_load_average; } if (dmc_load >= DMC_MAX_THRESHOLD) { dmcfreq = data->max_freq[PPMU_MIF]; } else if (dmc_load < IDLE_THRESHOLD) { if (dmc_load_average < IDLE_THRESHOLD) dmcfreq = step_down(data, PPMU_MIF, 1); else dmcfreq = data->curr_freq[PPMU_MIF]; } else { if (dmc_load < dmc_load_average) { dmc_load = dmc_load_average; if (dmc_load >= DMC_MAX_THRESHOLD) dmc_load = DMC_MAX_THRESHOLD; } dmcfreq = div64_u64(data->max_freq[PPMU_MIF] * dmc_load, DMC_MAX_THRESHOLD); } lockfreq = dev_max_freq(data->dev[PPMU_MIF]); newfreq[PPMU_MIF] = max3(lockfreq, dmcfreq, cpufreq); opp[PPMU_MIF] = opp_find_freq_ceil(data->dev[PPMU_MIF], &newfreq[PPMU_MIF]); opp[PPMU_INT] = opp_find_freq_ceil(data->dev[PPMU_INT], &data->max_freq[PPMU_INT]); *mif_opp = opp[PPMU_MIF]; /* temporary */ *int_opp = opp[PPMU_INT]; }
static GENERIC int shmem_per_system(T compile_time_param){ const int grav = Gravitation<T>::shmem_per_system(); const int prop = Propagator<T,Gravitation<T> >::shmem_per_system(); const int moni = Monitor::shmem_per_system(compile_time_param); return max3( grav, prop, moni); }
Mesh* Resources::LoadMesh(string filename, string geometryName) { string filenameAndGeometryName = dataPath + "\\" + filename + "\\" + geometryName; // Cached if (loadedMeshes.count(filenameAndGeometryName) > 0) { return loadedMeshes[filenameAndGeometryName]; } domCOLLADA* doc = LoadCollada(dataPath + "\\" + filename); // Look for the geometry domGeometry* geom = NULL; domElement* elem = dae.getDatabase()->idLookup(geometryName, doc->getDocument()); if (elem != NULL && elem->typeID() == domNode::ID()) { // Forward from node name domNode* node = daeSafeCast<domNode>(elem); geom = daeSafeCast<domGeometry>(node->getInstance_geometry_array().get(0)->getUrl().getElement()); } else { geom = daeSafeCast<domGeometry>(elem); } if (geom == NULL) { MessageBoxA(NULL, ("Could not find mesh " + filenameAndGeometryName).c_str(), "COLLADA", MB_OK); exit(1); } domMeshRef meshRef = geom->getMesh(); Mesh* mesh = new Mesh(filename, geometryName); loadedMeshes[filenameAndGeometryName] = mesh; // Cache // Load the <tristrips/> elements // (other types are ignored for now) D3DXVECTOR3 minCorner(0,0,0), maxCorner(0,0,0); bool minMaxSet = false; for(u_int i = 0; i < meshRef->getTristrips_array().getCount(); i++) { domTristripsRef tristripsRef = meshRef->getTristrips_array().get(i); Tristrip ts; ts.fvf = 0; ts.buffer = NULL; // Resolve all data sources int posOffset = -1; domListOfFloats* posSrc; int norOffset = -1; domListOfFloats* norSrc; int colOffset = -1; domListOfFloats* colSrc; int texOffset = -1; domListOfFloats* texSrc; for(u_int j = 0; j < tristripsRef->getInput_array().getCount(); j++) { domInputLocalOffsetRef input = tristripsRef->getInput_array().get(j); daeElementRef source = input->getSource().getElement(); // Defined per vertex - forward if (source->typeID() == domVertices::ID()) { source = daeSafeCast<domVertices>(source)->getInput_array().get(0)->getSource().getElement(); } int offset = (int)input->getOffset(); domListOfFloats* src = &(daeSafeCast<domSource>(source)->getFloat_array()->getValue()); if (input->getSemantic() == string("VERTEX")) { posOffset = offset; posSrc = src; ts.fvf |= D3DFVF_XYZ; } else if (input->getSemantic() == string("NORMAL")) { norOffset = offset; norSrc = src; ts.fvf |= D3DFVF_NORMAL; } else if (input->getSemantic() == string("COLOR")) { colOffset = offset; colSrc = src; ts.fvf |= D3DFVF_DIFFUSE; } else if (input->getSemantic() == string("TEXCOORD")) { texOffset = offset; texSrc = src; ts.fvf |= D3DFVF_TEX2; } } // Load the <P/> elementes int pStride = 0; pStride = max(pStride, posOffset + 1); pStride = max(pStride, norOffset + 1); pStride = max(pStride, colOffset + 1); pStride = max(pStride, texOffset + 1); vector<float>& vb = ts.vb; // Vertex buffer data for(u_int j = 0; j < tristripsRef->getP_array().getCount(); j++) { domListOfUInts p = tristripsRef->getP_array().get(j)->getValue(); ts.vertexCounts.push_back(p.getCount() / pStride); for(u_int k = 0; k < p.getCount(); k += pStride) { if (posOffset != -1) { int index = (int)p.get(k + posOffset); float x = (float)posSrc->get(3 * index + 0); float y = (float)posSrc->get(3 * index + 1); float z = (float)posSrc->get(3 * index + 2); if (!minMaxSet) { minCorner = maxCorner = D3DXVECTOR3(x, y, z); minMaxSet = true; } else { minCorner = min3(minCorner, D3DXVECTOR3(x, y, z)); maxCorner = max3(maxCorner, D3DXVECTOR3(x, y, z)); } vb.push_back(x); vb.push_back(y); vb.push_back(z); } if (norOffset != -1) { int index = (int)p.get(k + norOffset); vb.push_back((float)norSrc->get(3 * index + 0)); vb.push_back((float)norSrc->get(3 * index + 1)); vb.push_back((float)norSrc->get(3 * index + 2)); } if (colOffset != -1) { int index = (int)p.get(k + colOffset); u_int argb = GetColor(colSrc, index); vb.push_back(*((float*)&argb)); } if (texOffset != -1) { int index = (int)p.get(k + texOffset); vb.push_back((float)texSrc->get(2 * index + 0)); vb.push_back(1 - (float)texSrc->get(2 * index + 1)); } // Note vertex buffer stride (bytes) if (j == 0 && k == 0) { ts.vbStride_floats = vb.size(); ts.vbStride_bytes = vb.size() * sizeof(float); } } } // Load the material ts.materialName = tristripsRef->getMaterial(); LoadMaterial(doc, ts.materialName, &ts.material, &ts.textureFilename); // Done with this <tristrips/> mesh->tristrips.push_back(ts); } mesh->boundingBox = BoundingBox(minCorner, maxCorner); return mesh; }
// [*blech*, this looks and feels like FORTRAN.] unsigned myers_diff( const char *seq_a, enum myers_align_mode mode, const char* seq_b, int maxd, char *bt_a, char *bt_b ) { int len_a = strlen( seq_a ), len_b = strlen( seq_b ) ; if( maxd > len_a + len_b ) maxd = len_a + len_b ; // in vee[d][k], d runs from 0 to maxd; k runs from -d to +d int **vee = calloc( maxd, sizeof(int*) ) ; int d, dd, k, x, y, r = UINT_MAX ; int *v_d_1 = 0, *v_d = 0 ; // "array slice" vee[.][d-1] for( d = 0 ; d != maxd ; ++d, v_d_1 = v_d ) // D-paths in order of increasing D { v_d = d + (vee[d] = malloc( (2 * d + 1) * sizeof( int ) )) ; // "array slice" vee[.][d] for( k = max(-d,-len_a) ; k <= min(d,len_b) ; ++k ) // diagonals { if( d == 0 ) x = 0 ; else if(d==1&&k==0) x = v_d_1[ k ]+1 ; else if( k == -d ) x = v_d_1[ k+1 ] ; else if( k == d ) x = v_d_1[ k-1 ]+1 ; // argh, need to check for d first, b/c -d+2 could be equal to d else if( k == -d+1 ) x = max( v_d_1[ k ]+1, v_d_1[ k+1 ] ) ; else if( k == d-1 ) x = max( v_d_1[ k-1 ]+1, v_d_1[ k ]+1 ) ; else x = max3( v_d_1[ k-1 ]+1, v_d_1[ k ]+1, v_d_1[ k+1 ] ) ; y = x-k ; while( x < len_b && y < len_a && match( seq_b[x], seq_a[y] ) ) ++x, ++y ; v_d[ k ] = x ; if( (mode == myers_align_is_prefix || y == len_a) && (mode == myers_align_has_prefix || x == len_b) ) { char *out_a = bt_a + len_a + d +2 ; char *out_b = bt_b + len_b + d +2 ; *--out_a = 0 ; *--out_a = 0 ; for( dd = d ; dd != 0 ; ) { if( k != -dd && k != dd && x == vee[ dd-1 ][ k + dd-1 ]+1 ) { --dd ; --x ; --y ; *--out_b = seq_b[x] ; *--out_a = seq_a[y] ; } else if( k > -dd+1 && x == vee[ dd-1 ][ k-1 + dd-1 ]+1 ) { --x ; --k ; --dd ; *--out_b = seq_b[x] ; *--out_a = '-' ; } else if( k < dd-1 && x == vee[ dd-1 ][ k+1 + dd-1 ] ) { ++k ; --y ; --dd ; *--out_b = '-' ; *--out_a = seq_a[y] ; } else // this better had been a match... { --x ; --y ; *--out_b = seq_b[x] ; *--out_a = seq_a[y] ; } } while( x > 0 ) { --x ; *--out_b = seq_b[x] ; *--out_a = seq_a[x] ; } memmove( bt_a, out_a, bt_a + len_a + d + 2 - out_a ) ; memmove( bt_b, out_b, bt_b + len_b + d + 2 - out_b ) ; r = d ; goto cleanup ; } } } cleanup: for( dd = maxd ; dd != 0 ; --dd ) free( vee[dd-1] ) ; free( vee ) ; return r ; }
/*! Função para encontrar o melhor alinhamento entre as cadeias s e t, dado que s e t sejam cadeias validas de nucleotideos (a,c,g,t). @param s ponteiro para a cadeia s @param t ponteiro para a cadeia t, a ser alinhada com s @return TODO */ void p1_alinhar_s_t (char *s, char *t, char **als, char **alt) { int slen, tlen, lignlen; int i, j, k, maxij, diagv, esqv, cimav; char dir; int **m, **mdir; char *slign, *tlign; int mrows, mcols; /*Alocação e inicialização da matriz de scores*/ slen = strlen(s); tlen = strlen(t); mrows = tlen + 1; mcols = slen + 1; m = alloccharmtx(mrows,mcols); mdir = alloccharmtx(mrows,mcols); for (i = 1; i < mcols; i++) { m[0][i] = i * SCR_R; mdir[0][i] = DIRESQ; } for (i = 1; i < mrows; i++) { m[i][0] = i * SCR_I; mdir[i][0] = DIRSUB; } for (i = 1; i < mrows; i++) for (j = 1; j < mcols; j++) { esqv = m[i][j-1] + SCR_R; diagv = (s[j-1] == t[i-1]) ? (m[i-1][j-1] + SCR_M) : (m[i-1][j-1] + SCR_S); cimav = m[i-1][j] + SCR_I; maxij = max3(esqv,diagv,cimav); dir = 0; if (maxij == esqv) dir = DIRESQ; if (maxij == cimav) dir = dir | DIRSUB; if (maxij == diagv) dir = dir | DIRDIA; mdir[i][j] = dir; m[i][j] = maxij; } /*Alocação das strings s e t alinhadas*/ lignlen = slen > tlen ? slen : tlen; slign = (char *) calloc(lignlen + 1, sizeof(char)); tlign = (char *) calloc(lignlen + 1, sizeof(char)); i = mrows - 1; j = mcols - 1; slign[lignlen] = '\0'; tlign[lignlen] = '\0'; k = lignlen - 1; do { dir = mdir[i][j]; if (goDG(dir)) { slign[k] = s[j-1]; tlign[k] = t[i-1]; i--; j--; } else if (goLF(dir)) { slign[k] = s[j-1]; tlign[k] = GAP; j--; } else if (goUP(dir)) { slign[k] = GAP; tlign[k] = t[i-1]; i--; } k--; }while(dir); printf("\n%s\n%s\n\n",slign,tlign); *als = slign; *alt = tlign; for (i = 0; i < mrows; i++) { for (j = 0; j < mcols; j++) { printf("%03d ", m[i][j]); } printf("\n"); } printf("\n"); for (i = 0; i < mrows; i++) { for (j = 0; j < mcols; j++) { printf("%03d ", mdir[i][j]); } printf("\n"); } freemtx(mdir,mrows); freemtx(m,mrows); }
int main(void) { printf("max3(%d, %d, %d) = %d\n", 3, 2, 1, max3(3, 2, 1)); printf("max3(%d, %d, %d) = %d\n", 3, 2, 2, max3(3, 2, 2)); printf("max3(%d, %d, %d) = %d\n", 3, 1, 2, max3(3, 1, 2)); printf("max3(%d, %d, %d) = %d\n", 3, 2, 3, max3(3, 2, 3)); printf("max3(%d, %d, %d) = %d\n", 2, 1, 3, max3(2, 1, 3)); printf("max3(%d, %d, %d) = %d\n", 3, 3, 1, max3(3, 3, 2)); printf("max3(%d, %d, %d) = %d\n", 3, 3, 3, max3(3, 3, 3)); printf("max3(%d, %d, %d) = %d\n", 2, 2, 3, max3(2, 2, 3)); printf("max3(%d, %d, %d) = %d\n", 2, 3, 1, max3(2, 3, 1)); printf("max3(%d, %d, %d) = %d\n", 2, 3, 2, max3(2, 3, 2)); printf("max3(%d, %d, %d) = %d\n", 1, 3, 2, max3(1, 3, 2)); printf("max3(%d, %d, %d) = %d\n", 2, 3, 3, max3(2, 3, 3)); printf("max3(%d, %d, %d) = %d\n", 1, 2, 3, max3(1, 2, 3)); return (0); }
void triangle(Vec4 v1, Vec4 v2, Vec4 v3) { // sort_counterclockwise(&v1, &v2, &v3); // 28.4 fixed-point coordinates const int Y1 = iround(16.0f * v1.y); const int Y2 = iround(16.0f * v2.y); const int Y3 = iround(16.0f * v3.y); const int X1 = iround(16.0f * v1.x); const int X2 = iround(16.0f * v2.x); const int X3 = iround(16.0f * v3.x); // Deltas const int DX12 = X1 - X2; const int DX23 = X2 - X3; const int DX31 = X3 - X1; const int DY12 = Y1 - Y2; const int DY23 = Y2 - Y3; const int DY31 = Y3 - Y1; // Fixed-point deltas const int FDX12 = DX12 << 4; const int FDX23 = DX23 << 4; const int FDX31 = DX31 << 4; const int FDY12 = DY12 << 4; const int FDY23 = DY23 << 4; const int FDY31 = DY31 << 4; // Bounding rectangle int minx = (min3(X1, X2, X3) + 0xF) >> 4; const int maxx = (max3(X1, X2, X3) + 0xF) >> 4; int miny = (min3(Y1, Y2, Y3) + 0xF) >> 4; const int maxy = (max3(Y1, Y2, Y3) + 0xF) >> 4; // Block size, standard 8x8 (must be power of two) const int q = 8; // Start in corner of qxq block minx &= ~(q - 1); miny &= ~(q - 1); Point brush; brush.x = 0; brush.y = miny; // Half-edge constants int C1 = DY12 * X1 - DX12 * Y1; int C2 = DY23 * X2 - DX23 * Y2; int C3 = DY31 * X3 - DX31 * Y3; // Correct for fill convention if (DY12 < 0 || (DY12 == 0 && DX12 > 0)) C1++; if (DY23 < 0 || (DY23 == 0 && DX23 > 0)) C2++; if (DY31 < 0 || (DY31 == 0 && DX31 > 0)) C3++; // Loop through blocks for (int y = miny; y < maxy; y += q) { for (int x = minx; x < maxx; x += q) { // Corners of block int x0 = x << 4; int x1 = (x + q - 1) << 4; int y0 = y << 4; int y1 = (y + q - 1) << 4; // Evaluate half-space functions byte a00 = C1 + DX12 * y0 - DY12 * x0 > 0; byte a10 = C1 + DX12 * y0 - DY12 * x1 > 0; byte a01 = C1 + DX12 * y1 - DY12 * x0 > 0; byte a11 = C1 + DX12 * y1 - DY12 * x1 > 0; int a = (a00 << 0) | (a10 << 1) | (a01 << 2) | (a11 << 3); byte b00 = C2 + DX23 * y0 - DY23 * x0 > 0; byte b10 = C2 + DX23 * y0 - DY23 * x1 > 0; byte b01 = C2 + DX23 * y1 - DY23 * x0 > 0; byte b11 = C2 + DX23 * y1 - DY23 * x1 > 0; int b = (b00 << 0) | (b10 << 1) | (b01 << 2) | (b11 << 3); byte c00 = C3 + DX31 * y0 - DY31 * x0 > 0; byte c10 = C3 + DX31 * y0 - DY31 * x1 > 0; byte c01 = C3 + DX31 * y1 - DY31 * x0 > 0; byte c11 = C3 + DX31 * y1 - DY31 * x1 > 0; int c = (c00 << 0) | (c10 << 1) | (c01 << 2) | (c11 << 3); // Skip block when outside an edge if (a == 0x0 || b == 0x0 || c == 0x0) continue; Point buffer = brush; // Copy point really needed? // Accept whole block when totally covered if (a == 0xF && b == 0xF && c == 0xF) { for (int iy = 0; iy < q; iy++) { for (int ix = x; ix < x + q; ix++) { fragmentShader(buffer.x + ix, buffer.y); } buffer.y++; } } else { // Partially covered block int CY1 = C1 + DX12 * y0 - DY12 * x0; int CY2 = C2 + DX23 * y0 - DY23 * x0; int CY3 = C3 + DX31 * y0 - DY31 * x0; for (int iy = y; iy < y + q; iy++) { int CX1 = CY1; int CX2 = CY2; int CX3 = CY3; for (int ix = x; ix < x + q; ix++) { if (CX1 > 0 && CX2 > 0 && CX3 > 0) { fragmentShader(buffer.x + ix, buffer.y); } CX1 -= FDY12; CX2 -= FDY23; CX3 -= FDY31; } CY1 += FDX12; CY2 += FDX23; CY3 += FDX31; buffer.y++; } } } brush.y += q; } }
uint txt_insert(uint elem_, char val) { txt* elem=(txt*)elem_; txtContent* nextPos; uint size=sizeof(txtContent); uint x,y; uint minX, minY, maxX, maxY; if (elem->cursor==(txtContent*)(uint)-1) return -1; if (elem->chImgArray[(uint)val]==0) return -1; if (elem->tail==0) nextPos=(txtContent*)((uint)elem->blockTail+4); else if ((uint)elem->tail+size-(uint)elem->blockTail>4096-size) { if ((*(uint*)elem->blockTail=(uint)kalloc()) == 0) { panic("Bingolingo!"); return -1; } elem->blockTail=(void*)(*(uint*)elem->blockTail); *(uint*)elem->blockTail=(uint)0; nextPos=(txtContent*)((uint)elem->blockTail+4); } else nextPos=(txtContent*)((uint)elem->tail+size); if (elem->cursor==0) x=y=0; else if (elem->cursor->data.ch=='\n') { x=0; y=elem->cursor->data.ds.y+elem->chHeight; if (y+elem->chHeight>elem->ds.height) elem->ds.height=y+elem->chHeight; } else if (elem->cursor->data.ds.x+2*elem->chWidth<=elem->ds.width) { x=elem->cursor->data.ds.x+elem->chWidth; y=elem->cursor->data.ds.y; } else { x=0; y=elem->cursor->data.ds.y+elem->chHeight; if (y+elem->chHeight>elem->ds.height) elem->ds.height=y+elem->chHeight; } cha_createDomOrphan((cha*)nextPos, x, y, elem->chWidth, elem->chHeight, elem->ds.pid); ((cha*)nextPos)->ds.parent=&elem->ds; if (elem->cursor!=0) ((cha*)nextPos)->ds.frater=&(elem->cursor->data.ds); else ((cha*)nextPos)->ds.frater=&(elem->cursorDiv->ds); if (elem->cursor!=0 && elem->cursor->next!=0) elem->cursor->next->data.ds.frater=&(((cha*)nextPos)->ds); else if (elem->cursor==0 && elem->head!=0) elem->head->data.ds.frater=&(((cha*)nextPos)->ds); else elem->ds.descent=&(((cha*)nextPos)->ds); if (elem->cursor!=0 && elem->cursor->next==0) nextPos->next=0; else if (elem->cursor==0 && elem->head==0) nextPos->next=0; else if (elem->cursor==0 && elem->head!=0) { nextPos->next=elem->head; elem->head->prev=nextPos; } else { nextPos->next=elem->cursor->next; elem->cursor->next->prev=nextPos; } if (elem->cursor==0) { nextPos->prev=0; elem->head=nextPos; } else { nextPos->prev=elem->cursor; elem->cursor->next=nextPos; } elem->tail=nextPos; cha_setContentNotRedraw((uint)nextPos, elem->chImgArray[(uint)val], val); cha_setColor((uint)nextPos, elem->txtColor); elem->cursor=nextPos; minX=elem->cursor->data.ds.x; minY=elem->cursor->data.ds.y; maxX=elem->cursor->data.ds.x+elem->chWidth; maxY=elem->cursor->data.ds.y+elem->chHeight; while (nextPos!=0) { if (nextPos==0) x=y=0; else if (nextPos->data.ch=='\n') { x=0; y=nextPos->data.ds.y+elem->chHeight; if (y+elem->chHeight>elem->ds.height) elem->ds.height=y+elem->chHeight; } else if (nextPos->data.ds.x+2*elem->chWidth<=elem->ds.width) { x=nextPos->data.ds.x+elem->chWidth; y=nextPos->data.ds.y; } else { x=0; y=nextPos->data.ds.y+elem->chHeight; if (y+elem->chHeight>elem->ds.height) elem->ds.height=y+elem->chHeight; } if (nextPos==elem->cursor) { minX=min3(minX,x,elem->cursorDiv->ds.x); minY=min3(minY,y,elem->cursorDiv->ds.y); maxX=max3(maxX,x+elem->chWidth,elem->cursorDiv->ds.x+elem->chWidth); maxY=max3(maxY,y+elem->chHeight,elem->cursorDiv->ds.y+elem->chHeight); elem->cursorDiv->ds.x=x; elem->cursorDiv->ds.y=y; } if (nextPos->next==0) break; if (x==nextPos->next->data.ds.x && y==nextPos->next->data.ds.y) break; minX=min3(minX,x,nextPos->next->data.ds.x); minY=min3(minY,y,nextPos->next->data.ds.y); maxX=max3(maxX,x+elem->chWidth,nextPos->next->data.ds.x+elem->chWidth); maxY=max3(maxY,y+elem->chHeight,nextPos->next->data.ds.y+elem->chHeight); nextPos->next->data.ds.x=x; nextPos->next->data.ds.y=y; nextPos=nextPos->next; } reDraw_(&elem->ds,minX,minY,maxX-minX,maxY-minY); return 0; }
int main(int argc, char** argv) { int ratio = 3; int kernel_size = 3; int sigma = 200; //connection to camera CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY); if(!capture) { std::cerr << "ERROR: capture is NULL\n"; getchar(); return -1; } /* ONLY NEEDED FOR TESTING PURPOSES cvNamedWindow("Camera Image", CV_WINDOW_AUTOSIZE); // Create a Trackbar for user to enter threshold cv::createTrackbar("Min Threshold:","Camera Image" , &lowThreshold, max_lowThreshold, DummyCallback); // Create a Trackbar for user to enter threshold cv::createTrackbar("Hough Threshold:","Camera Image" , &houghThreshold, max_houghThreshold, DummyCallback); // Create a Trackbar for user to enter threshold cv::createTrackbar("Sigma:","Camera Image" , &sigma, 1000, DummyCallback); cvNamedWindow("Edge Image", CV_WINDOW_AUTOSIZE); */ ros::init(argc,argv, "circle_publisher"); ros::NodeHandle nh; //Let's publish messages as defined in /msg/cirlce.msg on a topic called 'detected_circles' with a max. buffer of 1000 messages ros::Publisher circle_publisher = nh.advertise<circle_detection::circle_msg>("detected_circles",1000); ros::Rate loop_rate(10); //used to create an Image Id size_t id_count = 0; while(ros::ok) { // Get a frame IplImage* frame = cvQueryFrame(capture); if (!frame) { std::cerr << "ERROR: frame is null...\n" ; getchar(); break; } // image id id_count++; cv::Mat src(frame); cv::Mat src_gray; cv::Mat dst; cv::Mat detected_edges; dst.create(src.size(), src.type()); // covert the image to gray cvtColor(src,src_gray,CV_BGR2GRAY); // Reduce the noise so we avoid false circle detection GaussianBlur(src_gray, detected_edges, cv::Size(9, 9), sigma/100.0, 0); equalizeHist(detected_edges, detected_edges); // Canny detector Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size); // Using Canny's output as a mask, we display our result dst = cv::Scalar::all(0); src.copyTo(dst, detected_edges); std::vector<Point> edgePoints; // iterate through the pixels of the canny image for(int j=0;j<detected_edges.cols;j++) { for(int i=0;i<detected_edges.rows;i++) { unsigned char &color = *(detected_edges.data+detected_edges.step*i + j*detected_edges.elemSize()); unsigned char &bb = *(src.data+src.step*i + j*src.elemSize()); unsigned char &gg = *(src.data+src.step*i + j*src.elemSize() + 1); unsigned char &rr = *(src.data+src.step*i + j*src.elemSize() + 2); // check if the pixel is black or white (only edges are white) if(color) { int max = max3(rr,gg,bb); int min = min3(rr,gg,bb); int delta = max - min; // check saturation (only colorfull circles will be detacted if(delta > 20) { edgePoints.push_back(Point((double) j,(double) i)); } else { // mark pixel as no longer relevant, i.e. the pixel isn't recognized as edge color = 0; } } } } std::vector<Circle> detectedCircles; /* Apply the RANSAC algorithm to find circles in the camera image * Paramters: sink vector, points, eps, iterations, minSupporters, maxCircles */ ransac(detectedCircles, edgePoints, 5.0, 10000, 100, 1); /* ONLY NEDDED FOR TESTIGN PURPOSES // Draw the circles detected for(size_t i = 0; i < detectedCircles.size(); i++) { cv::Point center(cvRound(detectedCircles[i].center.x), cvRound(detectedCircles[i].center.y)); int radius = cvRound(detectedCircles[i].radius); // assert(radius > 0); // circle center circle(src, center, 3, cv::Scalar(0,255,0), -1, 8, 0 ); // circle outline circle(src, center, radius, cv::Scalar(0,0,255), 3, 8, 0 ); } //Results imshow("Camera Image", src); imshow("Edge Image", detected_edges); */ for(size_t i=0;i < detectedCircles.size();i++) { circle_detection::circle_msg msg; std::stringstream ss; ss << "IMG" << id_count; msg.image_id = ss.str(); unsigned int radius = cvRound(detectedCircles[i].radius); msg.radius = radius; msg.center_x = cvRound(detectedCircles[i].center.x) ; msg.center_y = cvRound(detectedCircles[i].center.y); circle_publisher.publish(msg); ros::spinOnce(); loop_rate.sleep(); } //Do not release the frame! //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version), //remove higher bits using AND operator //cvWaitKey() is used as delay between the frames if ( (cvWaitKey(100) & 255) == 27 ) break; } /* ONLY NEEDED FOR TESTING PURPOSES // Release the capture device housekeeping cvReleaseCapture( &capture ); cvDestroyWindow( "Display Image" ); */ return 0; }
uint txt_bckspc(uint elem_, char* des) { txt* elem=(txt*)elem_; txtContent* nextPos; uint x,y; uint minX, minY, maxX, maxY; if (elem->cursor==(txtContent*)(uint)-1) return -1; if (elem->cursor==0) return -1; *des=elem->cursor->data.ch; nextPos=elem->cursor; txt_decCursor(elem_); cha_release((uint)&nextPos->data); if (nextPos==elem->head && nextPos->next==0) elem->head=0; else if (nextPos==elem->head) { elem->head=nextPos->next; nextPos->next->prev=0; } else if (nextPos->next==0) nextPos->prev->next=0; else { nextPos->prev->next=nextPos->next; nextPos->next->prev=nextPos->prev; } nextPos=elem->cursor; if (nextPos!=0) { minX=nextPos->data.ds.x; minY=nextPos->data.ds.y; maxX=nextPos->data.ds.x+elem->chWidth; maxY=nextPos->data.ds.y+elem->chHeight; } else { minX=0; minY=0; maxX=elem->chWidth; maxY=elem->chHeight; } while (1) { if (nextPos==0) x=y=0; else if (nextPos->data.ch=='\n') { x=0; y=nextPos->data.ds.y+elem->chHeight; if (y+elem->chHeight>elem->ds.height) elem->ds.height=y+elem->chHeight; } else if (nextPos->data.ds.x+2*elem->chWidth<=elem->ds.width) { x=nextPos->data.ds.x+elem->chWidth; y=nextPos->data.ds.y; } else { x=0; y=nextPos->data.ds.y+elem->chHeight; if (y+elem->chHeight>elem->ds.height) elem->ds.height=y+elem->chHeight; } if ((nextPos==0&&elem->head==0) || nextPos->next==0) break; if (nextPos!=0) { if (x==nextPos->next->data.ds.x && y==nextPos->next->data.ds.y) break; minX=min3(minX,x,nextPos->next->data.ds.x); minY=min3(minY,y,nextPos->next->data.ds.y); maxX=max3(maxX,x+elem->chWidth,nextPos->next->data.ds.x+elem->chWidth); maxY=max3(maxY,y+elem->chHeight,nextPos->next->data.ds.y+elem->chHeight); nextPos->next->data.ds.x=x; nextPos->next->data.ds.y=y; nextPos=nextPos->next; } else { if (x==elem->head->data.ds.x && y==elem->head->data.ds.y) break; minX=min3(minX,x,elem->head->data.ds.x); minY=min3(minY,y,elem->head->data.ds.y); maxX=max3(maxX,x+elem->chWidth,elem->head->data.ds.x+elem->chWidth); maxY=max3(maxY,y+elem->chHeight,elem->head->data.ds.y+elem->chHeight); elem->head->data.ds.x=x; elem->head->data.ds.y=y; nextPos=elem->head; } } reDraw_(&elem->ds,minX,minY,maxX-minX,maxY-minY); return 0; }
/** The purpose of this function is to compute a safeguarded step for * a linesearch and to update an interval of uncertainty for * a minimizer of the function.<p> * * The parameter <code>stx</code> contains the step with the least function * value. The parameter <code>stp</code> contains the current step. It is * assumed that the derivative at <code>stx</code> is negative in the * direction of the step. If <code>brackt[0]</code> is <code>true</code> * when <code>mcstep</code> returns then a * minimizer has been bracketed in an interval of uncertainty * with endpoints <code>stx</code> and <code>sty</code>.<p> * * Variables that must be modified by <code>mcstep</code> are * implemented as 1-element arrays. * * @param stx Step at the best step obtained so far. * This variable is modified by <code>mcstep</code>. * @param fx Function value at the best step obtained so far. * This variable is modified by <code>mcstep</code>. * @param dx Derivative at the best step obtained so far. The derivative * must be negative in the direction of the step, that is, <code>dx</code> * and <code>stp-stx</code> must have opposite signs. * This variable is modified by <code>mcstep</code>. * * @param sty Step at the other endpoint of the interval of uncertainty. * This variable is modified by <code>mcstep</code>. * @param fy Function value at the other endpoint of the interval of uncertainty. * This variable is modified by <code>mcstep</code>. * @param dy Derivative at the other endpoint of the interval of * uncertainty. This variable is modified by <code>mcstep</code>. * * @param stp Step at the current step. If <code>brackt</code> is set * then on input <code>stp</code> must be between <code>stx</code> * and <code>sty</code>. On output <code>stp</code> is set to the * new step. * @param fp Function value at the current step. * @param dp Derivative at the current step. * * @param brackt Tells whether a minimizer has been bracketed. * If the minimizer has not been bracketed, then on input this * variable must be set <code>false</code>. If the minimizer has * been bracketed, then on output this variable is <code>true</code>. * * @param stpmin Lower bound for the step. * @param stpmax Upper bound for the step. * * @param info On return from <code>mcstep</code>, this is set as follows: * If <code>info</code> is 1, 2, 3, or 4, then the step has been * computed successfully. Otherwise <code>info</code> = 0, and this * indicates improper input parameters. * * @author Jorge J. More, David J. Thuente: original Fortran version, * as part of Minpack project. Argonne Nat'l Laboratory, June 1983. * Robert Dodier: Java translation, August 1997. */ void CMcsrch::mcstep ( double stx[] , double fx[] , double dx[] , double sty[], double fy[], double dy[] , double stp[] , double fp , double dp , bool brackt[], double stpmin , double stpmax , int info[] ) { bool bound; double gamma, p, q, r, s, sgnd, stpc, stpf, stpq, theta; info[0] = 0; if ( ( brackt[0] && ( stp[0] <= min ( stx[0] , sty[0] ) || stp[0] >= max ( stx[0] , sty[0] ) ) ) || dx[0] * ( stp[0] - stx[0] ) >= 0.0 || stpmax < stpmin ) return; // Determine if the derivatives have opposite sign. sgnd = dp * ( dx[0] / fabs ( dx[0] ) ); if ( fp > fx[0] ) { // First case. A higher function value. // The minimum is bracketed. If the cubic step is closer // to stx than the quadratic step, the cubic step is taken, // else the average of the cubic and quadratic steps is taken. info[0] = 1; bound = true; theta = 3 * ( fx[0] - fp ) / ( stp[0] - stx[0] ) + dx[0] + dp; s = max3 ( fabs ( theta ) , fabs ( dx[0] ) , fabs ( dp ) ); gamma = s * sqrt ( sqr( theta / s ) - ( dx[0] / s ) * ( dp / s ) ); if ( stp[0] < stx[0] ) gamma = - gamma; p = ( gamma - dx[0] ) + theta; q = ( ( gamma - dx[0] ) + gamma ) + dp; r = p/q; stpc = stx[0] + r * ( stp[0] - stx[0] ); stpq = stx[0] + ( ( dx[0] / ( ( fx[0] - fp ) / ( stp[0] - stx[0] ) + dx[0] ) ) / 2 ) * ( stp[0] - stx[0] ); if ( fabs ( stpc - stx[0] ) < fabs ( stpq - stx[0] ) ) { stpf = stpc; } else { stpf = stpc + ( stpq - stpc ) / 2; } brackt[0] = true; } else if ( sgnd < 0.0 ) { // Second case. A lower function value and derivatives of // opposite sign. The minimum is bracketed. If the cubic // step is closer to stx than the quadratic (secant) step, // the cubic step is taken, else the quadratic step is taken. info[0] = 2; bound = false; theta = 3 * ( fx[0] - fp ) / ( stp[0] - stx[0] ) + dx[0] + dp; s = max3 ( fabs ( theta ) , fabs ( dx[0] ) , fabs ( dp ) ); gamma = s * sqrt ( sqr( theta / s ) - ( dx[0] / s ) * ( dp / s ) ); if ( stp[0] > stx[0] ) gamma = - gamma; p = ( gamma - dp ) + theta; q = ( ( gamma - dp ) + gamma ) + dx[0]; r = p/q; stpc = stp[0] + r * ( stx[0] - stp[0] ); stpq = stp[0] + ( dp / ( dp - dx[0] ) ) * ( stx[0] - stp[0] ); if ( fabs ( stpc - stp[0] ) > fabs ( stpq - stp[0] ) ) { stpf = stpc; } else { stpf = stpq; } brackt[0] = true; } else if ( fabs ( dp ) < fabs ( dx[0] ) ) { // Third case. A lower function value, derivatives of the // same sign, and the magnitude of the derivative decreases. // The cubic step is only used if the cubic tends to infinity // in the direction of the step or if the minimum of the cubic // is beyond stp. Otherwise the cubic step is defined to be // either stpmin or stpmax. The quadratic (secant) step is also // computed and if the minimum is bracketed then the the step // closest to stx is taken, else the step farthest away is taken. info[0] = 3; bound = true; theta = 3 * ( fx[0] - fp ) / ( stp[0] - stx[0] ) + dx[0] + dp; s = max3 ( fabs ( theta ) , fabs ( dx[0] ) , fabs ( dp ) ); gamma = s * sqrt ( max ( 0.0, sqr( theta / s ) - ( dx[0] / s ) * ( dp / s ) ) ); if ( stp[0] > stx[0] ) gamma = - gamma; p = ( gamma - dp ) + theta; q = ( gamma + ( dx[0] - dp ) ) + gamma; r = p/q; if ( r < 0.0 && gamma != 0.0 ) { stpc = stp[0] + r * ( stx[0] - stp[0] ); } else if ( stp[0] > stx[0] ) { stpc = stpmax; } else { stpc = stpmin; } stpq = stp[0] + ( dp / ( dp - dx[0] ) ) * ( stx[0] - stp[0] ); if ( brackt[0] ) { if ( fabs ( stp[0] - stpc ) < fabs ( stp[0] - stpq ) ) { stpf = stpc; } else { stpf = stpq; } } else { if ( fabs ( stp[0] - stpc ) > fabs ( stp[0] - stpq ) ) { stpf = stpc; } else { stpf = stpq; } } } else { // Fourth case. A lower function value, derivatives of the // same sign, and the magnitude of the derivative does // not decrease. If the minimum is not bracketed, the step // is either stpmin or stpmax, else the cubic step is taken. info[0] = 4; bound = false; if ( brackt[0] ) { theta = 3 * ( fp - fy[0] ) / ( sty[0] - stp[0] ) + dy[0] + dp; s = max3 ( fabs ( theta ) , fabs ( dy[0] ) , fabs ( dp ) ); gamma = s * sqrt ( sqr( theta / s ) - ( dy[0] / s ) * ( dp / s ) ); if ( stp[0] > sty[0] ) gamma = - gamma; p = ( gamma - dp ) + theta; q = ( ( gamma - dp ) + gamma ) + dy[0]; r = p/q; stpc = stp[0] + r * ( sty[0] - stp[0] ); stpf = stpc; } else if ( stp[0] > stx[0] ) { stpf = stpmax; } else { stpf = stpmin; } } // Update the interval of uncertainty. This update does not // depend on the new step or the case analysis above. if ( fp > fx[0] ) { sty[0] = stp[0]; fy[0] = fp; dy[0] = dp; } else { if ( sgnd < 0.0 ) { sty[0] = stx[0]; fy[0] = fx[0]; dy[0] = dx[0]; } stx[0] = stp[0]; fx[0] = fp; dx[0] = dp; } // Compute the new step and safeguard it. stpf = min ( stpmax , stpf ); stpf = max ( stpmin , stpf ); stp[0] = stpf; if ( brackt[0] && bound ) { if ( sty[0] > stx[0] ) { stp[0] = min ( stx[0] + 0.66 * ( sty[0] - stx[0] ) , stp[0] ); } else { stp[0] = max ( stx[0] + 0.66 * ( sty[0] - stx[0] ) , stp[0] ); } } return; }
void construct_tour(arrow_problem *problem, int solve_btsp, int *node_list, int list_size, arrow_llist *tour, double *length, arrow_llist_item **ins_list) { int i, j, u, v, w; int cost, in_cost, out_cost; double best_cost, ins_cost; int alpha, beta; arrow_llist_item *node; /* If our partial tour has less than two nodes, then we'll pick off the last two nodes from the randomized node_list until we have at least two nodes to work with. */ while(tour->size < 2) { arrow_llist_insert_tail(tour, node_list[list_size - 1]); list_size--; } /* If we're solving for the BTSP, then we need to find the largest and second largest costs. Otherwise, we want the total length. */ node = tour->head; alpha = INT_MIN; beta = INT_MIN; *length = 0.0; while(node != NULL) { u = node->data; if(node->next == NULL) v = tour->head->data; else v = node->next->data; cost = problem->get_cost(problem, u, v); *length += cost; if(cost > alpha) { beta = alpha; alpha = cost; } else if(cost > beta) { beta = cost; } node = node->next; } /* Find the best place to insert the left over nodes */ for(i = 0; i < list_size; i++) { /* Te current node is v, we want to insert between nodes u and w. */ v = node_list[i]; best_cost = DBL_MAX; node = tour->head; while(node != NULL) { u = node->data; if(node->next == NULL) w = tour->head->data; else w = node->next->data; cost = problem->get_cost(problem, u, w); in_cost = problem->get_cost(problem, u, v); out_cost = problem->get_cost(problem, v, w); if(solve_btsp) { if(cost == alpha) ins_cost = max3(beta, in_cost, out_cost); else ins_cost = max3(alpha, in_cost, out_cost); } else { ins_cost = *length + in_cost + out_cost - cost; } /* When looking for the best spot to insert v, we keep a list of all the best spots (in ins_list). Later on, we'll pick one at random */ if(ins_cost < best_cost) { best_cost = ins_cost; ins_list[0] = node; j = 1; } else if(ins_cost == best_cost) { ins_list[j] = node; j++; } node = node->next; } /* Of the available spots to place the node, we pick one randomly */ j = arrow_util_random_between(0, j - 1); node = ins_list[j]; arrow_llist_insert_after(tour, node, v); /* Update alpha and beta, and the length */ node = tour->head; alpha = INT_MIN; beta = INT_MIN; *length = 0.0; while(node != NULL) { u = node->data; if(node->next == NULL) w = tour->head->data; else w = node->next->data; cost = problem->get_cost(problem, u, w); *length += cost; if(cost > alpha) { beta = alpha; alpha = cost; } else if(cost > beta) { beta = cost; } node = node->next; } } /* If we're solving BTSP, return the length as the largest cost */ if(solve_btsp) { *length = alpha; } }
bool n3d_prepare( n3d_rasterizer_t::triangle_t& tri, const n3d_vertex_t& v0, const n3d_vertex_t& v1, const n3d_vertex_t& v2, const uint32_t flags) { const vec4f_t& vp0 = v0.p_; const vec4f_t& vp1 = v1.p_; const vec4f_t& vp2 = v2.p_; // the signed triangle area const float t_area = (vp1.x - vp0.x) * (vp2.y - vp0.y) - (vp2.x - vp0.x) * (vp1.y - vp0.y); // check for back face if (t_area <= 0.f) return false; // reciprocal of area for normalization const float rt_area = 1.f / t_area; // find normalized barycentric coordinates tri.v_ [e_attr_b0] = orient2d(vp1, vp2) * rt_area; tri.sx_[e_attr_b0] = (vp1.y - vp2.y) * rt_area; tri.sy_[e_attr_b0] = (vp2.x - vp1.x) * rt_area; tri.v_ [e_attr_b1] = orient2d(vp2, vp0) * rt_area; tri.sx_[e_attr_b1] = (vp2.y - vp0.y) * rt_area; tri.sy_[e_attr_b1] = (vp0.x - vp2.x) * rt_area; tri.v_ [e_attr_b2] = orient2d(vp0, vp1) * rt_area; tri.sx_[e_attr_b2] = (vp0.y - vp1.y) * rt_area; tri.sy_[e_attr_b2] = (vp1.x - vp0.x) * rt_area; // calculate 1 / w for vertices const float v0w = 1.f / v0.p_.w; const float v1w = 1.f / v1.p_.w; const float v2w = 1.f / v2.p_.w; //(todo) update all of this to use SSE // find triangle bounds tri.min_.x = min3(vp0.x, vp1.x, vp2.x); tri.min_.y = min3(vp0.y, vp1.y, vp2.y); tri.max_.x = max3(vp0.x, vp1.x, vp2.x) + 1.f; tri.max_.y = max3(vp0.y, vp1.y, vp2.y) + 1.f; // barycenteric interpolate 1 param #define BLERPW(B) ((tri.B[0] * v0w) + \ (tri.B[1] * v1w) + \ (tri.B[2] * v2w)) // barycentric interpolate 3 param #define BLERPA(B, A) ((tri.B[0] * v0.attr_[A] * v0w) + \ (tri.B[1] * v1.attr_[A] * v1w) + \ (tri.B[2] * v2.attr_[A] * v2w)) // interplate 1 / w tri.v_ [e_attr_w] = BLERPW(v_); tri.sx_[e_attr_w] = BLERPW(sx_); tri.sy_[e_attr_w] = BLERPW(sy_); #if 0 for (uint32_t i = 0; i < v0.attr_.size(); ++i) { tri.v_ [e_attr_custom + i] = BLERPA(v_, i); tri.sx_[e_attr_custom + i] = BLERPA(sx_, i); tri.sy_[e_attr_custom + i] = BLERPA(sy_, i); } #endif #undef BLERPW #undef BLERPA return true; }
static void inplacenextcolumn (const Limdfsconstinfo *lci, const GtUchar dbchar, LocaliColumn *column) { GtUword i; LocaliMatrixvalue nw, west; column->colvalues[0].repcell = column->colvalues[0].delcell = MINUSINFTY; if (column->colvalues[0].inscell > 0) { if (column->colvalues[0].bestcell > 0) { column->colvalues[0].inscell = max2 (column->colvalues[0].inscell + lci->scorevalues.gapextend, column->colvalues[0].bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend); } else { column->colvalues[0].inscell = column->colvalues[0].inscell + lci->scorevalues.gapextend; } } else { if (column->colvalues[0].bestcell > 0) { column->colvalues[0].inscell = column->colvalues[0].bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend; } else { column->colvalues[0].inscell = MINUSINFTY; } } column->colvalues[0].bestcell = max3 (column->colvalues[0].repcell, column->colvalues[0].inscell, column->colvalues[0].delcell); column->maxvalue = (GtUword) max2(0,column->colvalues[0].bestcell); column->pprefixlen = 0; nw = column->colvalues[0]; for (i = 1UL; i <= lci->querylength; i++) { west = column->colvalues[i]; if (nw.bestcell > 0) { column->colvalues[i].repcell = nw.bestcell + REPLACEMENTSCORE(&lci->scorevalues, dbchar,lci->query[i-1]); } else { column->colvalues[i].repcell = MINUSINFTY; } if (west.inscell > 0) { if (west.bestcell > 0) { column->colvalues[i].inscell = max2 (west.inscell + lci->scorevalues.gapextend, west.bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend); } else { column->colvalues[i].inscell = west.inscell + lci->scorevalues.gapextend; } } else { if (west.bestcell > 0) { column->colvalues[i].inscell = west.bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend; } else { column->colvalues[i].inscell = MINUSINFTY; } } if (column->colvalues[i-1].delcell > 0) { if (column->colvalues[i-1].bestcell > 0) { column->colvalues[i].delcell = max2 (column->colvalues[i-1].delcell + lci->scorevalues.gapextend, column->colvalues[i-1].bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend); } else { column->colvalues[i].delcell = column->colvalues[i-1].delcell + lci->scorevalues.gapextend; } } else { if (column->colvalues[i-1].bestcell > 0) { column->colvalues[i].delcell = column->colvalues[i-1].bestcell + lci->scorevalues.gapstart + lci->scorevalues.gapextend; } else { column->colvalues[i].delcell = MINUSINFTY; } } nw = west; column->colvalues[i].bestcell = max3 (column->colvalues[i].repcell, column->colvalues[i].inscell, column->colvalues[i].delcell); if (column->colvalues[i].bestcell > 0 && column->colvalues[i].bestcell > (Scoretype) column->maxvalue) { column->maxvalue = (GtUword) column->colvalues[i].bestcell; column->pprefixlen = i; } } }
int AlignerLocal::stopTB(int i,int j) { return (i == 0 || j == 0 || max3(matrix[i][j],gap1[i][j],gap2[i][j]) <= 0); }
/* ** Uses G-drive non-anti-aliased method to draw triangle. Set only for now. */ static void render_triangle_2(WILLUSBITMAP *bmp,TRIANGLE2D *srctri, RENDER_COLOR *color) { double x1,y1,x2,y2,x3,y3,ylast; int *pattern; int def[2] = {1,0xffff}; double px1,py1,px2,py2,px3,py3; double ldy,rdy; double x1clip,x2clip,y1clip,y2clip; double ldx,rdx; int lx,rx,y,yi,yf,yinc; /* printf("@rt2 (%6.4f,%6.4f)-(%6.4f,%6.4f)-(%6.4f,%6.4f)\n", srctri->p[0].x,srctri->p[0].y, srctri->p[1].x,srctri->p[1].y, srctri->p[2].x,srctri->p[2].y); */ pattern = NULL; x1 = srctri->p[0].x; y1 = srctri->p[0].y; x2 = srctri->p[1].x; y2 = srctri->p[1].y; x3 = srctri->p[2].x; y3 = srctri->p[2].y; x1clip=0; x2clip=bmp->width; y1clip=0; y2clip=bmp->height; /* px1=render_col(bmp,x1); py1=render_row(bmp,y1); px2=render_col(bmp,x2); py2=render_row(bmp,y2); px3=render_col(bmp,x3); py3=render_row(bmp,y3); */ px1=x1*bmp->width; py1=y1*bmp->height; px2=x2*bmp->width; py2=y2*bmp->height; px3=x3*bmp->width; py3=y3*bmp->height; if (py1>py2) dswap(px1,py1,px2,py2); if (py2>py3) dswap(px2,py2,px3,py3); if (py1>py2) dswap(px1,py1,px2,py2); if (py1>y2clip || py3<y1clip) return; if (pattern==NULL) pattern=def; if (py1==py2 && py2==py3) { lx = min3(px1,px2,px3); rx = max3(px1,px2,px3); if (lx>x2clip || rx<x1clip) return; if (lx<x1clip) lx=x1clip; if (rx>x2clip) rx=x2clip; } if (py1==py3) x1=(double)(px1+px3)/2.; else x1=px1+(double)(px3-px1)*(double)(py2-py1)/(double)(py3-py1); yinc=1; // printf("py1=%7.2f, py2=%7.2f\n",py1,py2); if (py2>=y1clip && py2!=py1) { yi = floor((py1>y1clip ? py1 : y1clip)+.5); yf = floor((py2<y2clip ? py2 : y2clip)-.5); // printf("yi=%d, yf=%d\n",yi,yf); if (x1>(double)px2) { ldx=px2-px1; rdx=px3-px1; ldy=py2-py1; rdy=py3-py1; } else { ldx=px3-px1; rdx=px2-px1; ldy=py3-py1; rdy=py2-py1; } for (y=yi;y<=yf;y+=yinc) { lx=floor((px1+ldx*(y+.5-py1)/ldy)+.5); rx=floor((px1+rdx*(y+.5-py1)/rdy)-.5); // printf("lx,rx[%d] = %d, %d\n",y,lx,rx); if (lx>rx) continue; if (lx>x2clip || rx<x1clip) continue; if (lx<x1clip) lx=x1clip; if (rx>x2clip) rx=x2clip; if (lx>rx) continue; render_horizontal_line(bmp,lx,y,rx,color); /* if ((status=hlinepat(lx,y,rx,pen_color,pattern[y%pattern[0]+1]))!=NO_ERROR) return(status); */ } } ylast=py2; // printf("ylast=%7.2f, py3=%7.2f\n",ylast,py3); if (ylast<=y2clip && py2!=py3) { yi = floor((ylast>y1clip ? ylast : y1clip)+.5); yf = floor((py3<y2clip ? py3 : y2clip)-.5); // printf("yi=%d, yf=%d\n",yi,yf); if (x1>px2) { ldx=px2-px3; rdx=px1-px3; ldy=py3-py2; rdy=py3-py1; } else { ldx=px1-px3; rdx=px2-px3; ldy=py3-py1; rdy=py3-py2; } // printf("px3=%g, ldx=%g, rdx=%g, ldy=%g, rdy=%g\n",px3,ldx,rdx,ldy,rdy); for (y=yi;y<=yf;y+=yinc) { lx=floor((px3+ldx*(py3-(y+.5))/ldy)+.5); rx=floor((px3+rdx*(py3-(y+.5))/rdy)-.5); // printf("lx,rxdp[%d] = %15.10f, %15.10f\n",y,px3+ldx*(py3-(y+.5))/ldy,px3+rdx*(py3-(y+.5))/rdy); // printf("lx,rx[%d] = %d, %d\n",y,lx,rx); if (lx>x2clip || rx<x1clip) continue; if (lx<x1clip) lx=x1clip; if (rx>x2clip) rx=x2clip; if (lx>rx) continue; render_horizontal_line(bmp,lx,y,rx,color); /* if ((status=hlinepat(lx,y,rx,pen_color,pattern[y%pattern[0]+1]))!=NO_ERROR) return(status); */ } } }
int AlignerLocal::findEnd() { return max3(matrix[end1][end2],gap1[end1][end2],gap2[end1][end2]); }
static bool triangleRender(const Triangle2i& triangle,SmartPointer<Texture> _rgb,SmartPointer<Texture> _alpha,Color4f color,const Box2i& box, bool bWrite) { XgeDebugAssert(_rgb->bpp==24 && _alpha->bpp==8 && _rgb->width==_alpha->width && _rgb->height==_alpha->height); int x1=triangle.p0.x,y1=triangle.p0.y; int x2=triangle.p1.x,y2=triangle.p1.y; int x3=triangle.p2.x,y3=triangle.p2.y; unsigned char* rgb =_rgb ->buffer; unsigned char* alpha =_alpha->buffer; int W =_rgb ->width; int minx = min3(x1, x2, x3) , maxx = max3(x1, x2, x3); int miny = min3(y1, y2, y3) , maxy = max3(y1, y2, y3); //must be completely be contained in the current area if (!(minx>=box.left() && maxx<=box.right() && miny>=box.bottom() && maxy<=box.top())) { XgeDebugAssert(!bWrite); return false; } int Sign=(((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))>=0)?(+1):(-1); for(int y = miny; y <= maxy; y++) for(int x = minx; x <= maxx; x++) { // if the pixel is inside.... if ( (Sign*((x2 - x1) * (y - y1) - (y2 - y1) * (x - x1)) >= 0) && (Sign*((x3 - x2) * (y - y2) - (y3 - y2) * (x - x2)) >= 0) && (Sign*((x1 - x3) * (y - y3) - (y1 - y3) * (x - x3)) >= 0) ) { int idx=y*W+x; //test if it has already been written //note: for each pixel consider also the pixel (-1,0) (+1,0) (0,-1) (0,+1) if (!bWrite && (alpha[idx] || alpha[idx-1] || alpha[idx+1] ||alpha[idx-W] || alpha[idx+W])) return false; //if write.... if (bWrite) { unsigned char R=(unsigned char)(255.0f*color.r); unsigned char G=(unsigned char)(255.0f*color.g); unsigned char B=(unsigned char)(255.0f*color.b); rgb[idx*3+0] = R; rgb[idx*3+1] = G; rgb[idx*3+2] = B; rgb[(idx-1)*3+0] = rgb[(idx+1)*3+0] = rgb[(idx-W)*3+0] = rgb[(idx+W)*3+0] =R ; rgb[(idx-1)*3+1] = rgb[(idx+1)*3+1] = rgb[(idx-W)*3+1] = rgb[(idx+W)*3+1] =G ; rgb[(idx-1)*3+2] = rgb[(idx+1)*3+2] = rgb[(idx-W)*3+2] =rgb[(idx+W)*3+2] =B ; //sign as drawn alpha [idx ] = 0x01; alpha [idx-1] = 0x01; alpha [idx+1] = 0x01; alpha [idx-W] = 0x01; alpha [idx+W] = 0x01; } } } return true; }
/* * NAME: optimize->expr() * DESCRIPTION: optimize an expression */ static Uint opt_expr(node **m, int pop) { Uint d1, d2, i; node *n; node **oldside, *side; Uint olddepth; n = *m; switch (n->type) { case N_FLOAT: case N_GLOBAL: case N_INT: case N_LOCAL: case N_STR: case N_NIL: return !pop; case N_TOINT: case N_CAST: return opt_expr(&n->l.left, FALSE); case N_CATCH: oldside = side_start(&side, &olddepth); d1 = opt_expr(&n->l.left, TRUE); d1 = max2(d1, side_end(&n->l.left, side, oldside, olddepth)); if (d1 == 0) { *m = node_nil(); (*m)->line = n->line; return !pop; } return d1; case N_TOFLOAT: if (n->l.left->mod != T_INT) { return opt_expr(&n->l.left, FALSE); } /* fall through */ case N_NOT: case N_TST: if (pop) { *m = n->l.left; return opt_expr(m, TRUE); } return opt_expr(&n->l.left, FALSE); case N_TOSTRING: if (pop && (n->l.left->mod == T_INT || n->l.left->mod == T_FLOAT)) { *m = n->l.left; return opt_expr(m, TRUE); } return opt_expr(&n->l.left, FALSE); case N_LVALUE: return opt_lvalue(n->l.left); case N_ADD_EQ_1: case N_ADD_EQ_1_INT: case N_SUB_EQ_1: case N_SUB_EQ_1_INT: return opt_lvalue(n->l.left) + 1; case N_MIN_MIN: if (pop) { n->type = N_SUB_EQ_1; } return opt_lvalue(n->l.left) + 1; case N_MIN_MIN_INT: if (pop) { n->type = N_SUB_EQ_1_INT; } return opt_lvalue(n->l.left) + 1; case N_PLUS_PLUS: if (pop) { n->type = N_ADD_EQ_1; } return opt_lvalue(n->l.left) + 1; case N_PLUS_PLUS_INT: if (pop) { n->type = N_ADD_EQ_1_INT; } return opt_lvalue(n->l.left) + 1; case N_FUNC: m = &n->l.left->r.right; n = *m; if (n == (node *) NULL) { return 1; } d1 = 0; for (i = 0; n->type == N_PAIR; ) { oldside = side_start(&side, &olddepth); d2 = opt_expr(&n->l.left, FALSE); d1 = max3(d1, i + d2, i + side_end(&n->l.left, side, oldside, olddepth)); m = &n->r.right; n = n->l.left; i += (n->type == N_LVALUE || (n->type == N_COMMA && n->r.right->type == N_LVALUE)) ? 4 : 1; n = *m; } if (n->type == N_SPREAD) { m = &n->l.left; } oldside = side_start(&side, &olddepth); d2 = opt_expr(m, FALSE); return max3(d1, i + d2, i + side_end(m, side, oldside, olddepth)); case N_INSTANCEOF: return opt_expr(&n->l.left, FALSE) + 1; case N_GE: case N_GT: case N_LE: case N_LT: if (n->l.left->mod != n->r.right->mod) { return max2(opt_expr(&n->l.left, FALSE), opt_expr(&n->r.right, FALSE) + 1); } /* fall through */ case N_EQ: case N_NE: if (pop) { d1 = opt_expr(&n->l.left, TRUE); if (d1 == 0) { *m = n->r.right; return opt_expr(m, TRUE); } d2 = opt_expr(&n->r.right, TRUE); if (d2 == 0) { *m = n->l.left; return d1; } n->type = N_COMMA; side_add(m, d1); return d2; } return opt_binop(m); case N_DIV_INT: case N_MOD_INT: if (n->r.right->type == N_INT && n->r.right->l.number == 0) { d1 = opt_binop(m); return (d1 == 1) ? !pop : d1; } /* fall through */ case N_ADD_INT: case N_AND_INT: case N_EQ_INT: case N_GE_INT: case N_GT_INT: case N_LE_INT: case N_LSHIFT_INT: case N_LT_INT: case N_MULT_INT: case N_NE_INT: case N_OR_INT: case N_RSHIFT_INT: case N_SUB_INT: case N_XOR_INT: if (pop) { d1 = opt_expr(&n->l.left, TRUE); if (d1 == 0) { *m = n->r.right; return opt_expr(m, TRUE); } d2 = opt_expr(&n->r.right, TRUE); if (d2 == 0) { *m = n->l.left; return d1; } n->type = N_COMMA; side_add(m, d1); return d2; } /* fall through */ case N_ADD: case N_AND: case N_DIV: case N_LSHIFT: case N_MOD: case N_MULT: case N_OR: case N_RSHIFT: case N_SUB: case N_SUM: case N_XOR: d1 = opt_binop(m); return (d1 == 1) ? !pop : d1; case N_INDEX: if (n->l.left->type == N_STR && n->r.right->type == N_INT) { if (n->r.right->l.number < 0 || n->r.right->l.number >= (long) n->l.left->l.string->len) { return 2; } node_toint(n, (Int) str_index(n->l.left->l.string, (long) n->r.right->l.number)); return !pop; } if (n->l.left->type == N_FUNC && n->r.right->mod == T_INT) { if (n->l.left->r.number == kf_status) { n->type = N_FUNC; if (n->l.left->l.left->r.right != (node *) NULL) { /* status(obj)[i] */ n = n->l.left; n->type = N_STR; n->r.right = n->l.left; n->l.string = n->l.left->l.string; n = n->r.right; n->type = N_PAIR; n->l.left = n->r.right; n->r.right = (*m)->r.right; (*m)->r.number = ((long) KFCALL << 24) | KF_STATUSO_IDX; } else { /* status()[i] */ n->l.left = n->l.left->l.left; n->l.left->r.right = n->r.right; n->r.number = ((long) KFCALL << 24) | KF_STATUS_IDX; } return opt_expr(m, pop); } if (n->l.left->r.number == kf_call_trace) { /* call_trace()[i] */ n->type = N_FUNC; n->l.left = n->l.left->l.left; n->l.left->r.right = n->r.right; n->r.number = ((long) KFCALL << 24) | KF_CALLTR_IDX; return opt_expr(m, pop); } } return max2(opt_expr(&n->l.left, FALSE), opt_expr(&n->r.right, FALSE) + 1); case N_ADD_EQ: case N_ADD_EQ_INT: case N_AND_EQ: case N_AND_EQ_INT: case N_DIV_EQ: case N_DIV_EQ_INT: case N_LSHIFT_EQ: case N_LSHIFT_EQ_INT: case N_MOD_EQ: case N_MOD_EQ_INT: case N_MULT_EQ: case N_MULT_EQ_INT: case N_OR_EQ: case N_OR_EQ_INT: case N_RSHIFT_EQ: case N_RSHIFT_EQ_INT: case N_SUB_EQ: case N_SUB_EQ_INT: case N_SUM_EQ: case N_XOR_EQ: case N_XOR_EQ_INT: return opt_asgnexp(m, pop); case N_ASSIGN: if (n->l.left->type == N_AGGR) { d2 = 0; for (n = n->l.left->l.left; n->type == N_PAIR; n = n->r.right) { d1 = opt_lvalue(n->l.left); d2 += (d1 < 4) ? d1 : 4; } d1 = opt_lvalue(n); d2 += (d1 < 4) ? d1 : 4; return d2 + max2(2, opt_expr(&(*m)->r.right, FALSE)); } else { d1 = opt_lvalue(n->l.left); return max2(d1, ((d1 < 4) ? d1 : 4) + opt_expr(&n->r.right, FALSE)); } case N_COMMA: side_add(m, opt_expr(&n->l.left, TRUE)); return opt_expr(m, pop); case N_LAND: d1 = opt_cond(&n->l.left, FALSE); if (n->l.left->flags & F_CONST) { if (!opt_ctest(n->l.left)) { /* false && x */ *m = n->l.left; return !pop; } /* true && x */ n->type = N_TST; n->l.left = n->r.right; return opt_expr(m, pop); } oldside = side_start(&side, &olddepth); d2 = opt_cond(&n->r.right, pop); d2 = max2(d2, side_end(&n->r.right, side, oldside, olddepth)); if (n->r.right->flags & F_CONST) { if (pop) { *m = n->l.left; return opt_expr(m, TRUE); } if (!opt_ctest(n->r.right)) { /* x && false */ n->type = N_COMMA; return opt_expr(m, FALSE); } /* x && true */ n->type = N_TST; return d1; } if (n->r.right->type == N_COMMA) { n = n->r.right; if ((n->r.right->flags & F_CONST) && !opt_ctest(n->r.right)) { /* x && (y, false) --> (x && y, false) */ (*m)->r.right = n->l.left; n->l.left = *m; *m = n; return opt_expr(m, pop); } } return max2(d1, d2); case N_LOR: d1 = opt_cond(&n->l.left, FALSE); if (n->l.left->flags & F_CONST) { if (opt_ctest(n->l.left)) { /* true || x */ *m = n->l.left; return !pop; } /* false || x */ n->type = N_TST; n->l.left = n->r.right; return opt_expr(m, pop); } oldside = side_start(&side, &olddepth); d2 = opt_cond(&n->r.right, pop); d2 = max2(d2, side_end(&n->r.right, side, oldside, olddepth)); if (n->r.right->flags & F_CONST) { if (pop) { *m = n->l.left; return opt_expr(m, TRUE); } if (opt_ctest(n->r.right)) { /* x || true */ n->type = N_COMMA; return opt_expr(m, FALSE); } /* x || false */ n->type = N_TST; return d1; } if (n->r.right->type == N_COMMA) { n = n->r.right; if ((n->r.right->flags & F_CONST) && opt_ctest(n->r.right)) { /* x || (y, true) --> (x || y, true) */ (*m)->r.right = n->l.left; n->l.left = *m; *m = n; return opt_expr(m, pop); } } return max2(d1, d2); case N_QUEST: i = opt_cond(&n->l.left, FALSE); if (n->l.left->flags & F_CONST) { if (opt_ctest(n->l.left)) { *m = n->r.right->l.left; } else { *m = n->r.right->r.right; } return opt_expr(m, pop); } if (n->l.left->type == N_COMMA && (n->l.left->r.right->flags & F_CONST)) { side_add(&n->l.left, i); if (opt_ctest(n->l.left)) { *m = n->r.right->l.left; } else { *m = n->r.right->r.right; } return opt_expr(m, pop); } n = n->r.right; oldside = side_start(&side, &olddepth); d1 = opt_expr(&n->l.left, pop); d1 = max2(d1, side_end(&n->l.left, side, oldside, olddepth)); if (d1 == 0) { n->l.left = (node *) NULL; } oldside = side_start(&side, &olddepth); d2 = opt_expr(&n->r.right, pop); d2 = max2(d2, side_end(&n->r.right, side, oldside, olddepth)); if (d2 == 0) { n->r.right = (node *) NULL; } return max3(i, d1, d2); case N_RANGE: d1 = opt_expr(&n->l.left, FALSE); d2 = 1; if (n->r.right->l.left != (node *) NULL) { d2 = opt_expr(&n->r.right->l.left, FALSE); if ((n->l.left->mod == T_STRING || (n->l.left->mod & T_REF) != 0) && n->r.right->l.left->type == N_INT && n->r.right->l.left->l.number == 0) { /* * str[0 .. x] or arr[0 .. x] */ n->r.right->l.left = (node *) NULL; d2 = 1; } else { d1 = max2(d1, d2 + 1); d2 = 2; } } if (n->r.right->r.right != (node *) NULL) { d1 = max2(d1, d2 + opt_expr(&n->r.right->r.right, FALSE)); } if (n->l.left->type == N_STR) { long from, to; if (n->r.right->l.left == (node *) NULL) { from = 0; } else { if (n->r.right->l.left->type != N_INT) { return d1; } from = n->r.right->l.left->l.number; } if (n->r.right->r.right == (node *) NULL) { to = n->l.left->l.string->len - 1; } else { if (n->r.right->r.right->type != N_INT) { return d1; } to = n->r.right->r.right->l.number; } if (from >= 0 && from <= to + 1 && to < (long) n->l.left->l.string->len) { node_tostr(n, str_range(n->l.left->l.string, from, to)); return !pop; } } return d1; case N_AGGR: if (n->mod == T_MAPPING) { n = n->l.left; if (n == (node *) NULL) { return 1; } d1 = 0; for (i = 0; n->type == N_PAIR; i += 2) { oldside = side_start(&side, &olddepth); d2 = opt_expr(&n->l.left->l.left, FALSE); d1 = max3(d1, i + d2, i + side_end(&n->l.left->l.left, side, oldside, olddepth)); oldside = side_start(&side, &olddepth); d2 = opt_expr(&n->l.left->r.right, FALSE); d1 = max3(d1, i + 1 + d2, i + 1 + side_end(&n->l.left->r.right, side, oldside, olddepth)); n = n->r.right; } oldside = side_start(&side, &olddepth); d2 = opt_expr(&n->l.left, FALSE); d1 = max3(d1, i + d2, i + side_end(&n->l.left, side, oldside, olddepth)); oldside = side_start(&side, &olddepth); d2 = opt_expr(&n->r.right, FALSE); return max3(d1, i + 1 + d2, i + 1 + side_end(&n->r.right, side, oldside, olddepth)); } else { m = &n->l.left; n = *m; if (n == (node *) NULL) { return 1; } d1 = 0; for (i = 0; n->type == N_PAIR; i++) { oldside = side_start(&side, &olddepth); d2 = opt_expr(&n->l.left, FALSE); d1 = max3(d1, i + d2, i + side_end(&n->l.left, side, oldside, olddepth)); m = &n->r.right; n = *m; } oldside = side_start(&side, &olddepth); d2 = opt_expr(m, FALSE); return max3(d1, i + d2, i + side_end(m, side, oldside, olddepth)); } } # ifdef DEBUG fatal("unknown expression type %d", n->type); # endif return 0; }
int longestUnivaluePath(struct TreeNode* root) { if(root==NULL) return 0; return max3(longestUnivaluePath(root->left), longestUnivaluePath(root->right), (longestU(root->left, root->val) + longestU(root->right, root->val))); }
/* * NAME: optimize->stmt() * DESCRIPTION: optimize a statement */ node *opt_stmt(node *first, Uint *depth) { node *n, **m, **prev; Uint d; Uint d1, d2; int i; node *side; if (first == (node *) NULL) { *depth = 0; return (node *) NULL; } d = 0; prev = m = &first; for (;;) { n = ((*m)->type == N_PAIR) ? (*m)->l.left : *m; switch (n->type) { case N_BLOCK: n->l.left = opt_stmt(n->l.left, &d1); if (n->l.left == (node *) NULL) { n = (node *) NULL; } d = max2(d, d1); break; case N_CASE: n->l.left = opt_stmt(n->l.left, &d1); d = max2(d, d1); break; case N_COMPOUND: n->l.left = opt_stmt(n->l.left, &d1); if (n->l.left == (node *) NULL) { n = (node *) NULL; } else if (n->r.right != (node *) NULL) { n->r.right = opt_stmt(n->r.right, &d2); d1 = max2(d1, d2); } d = max2(d, d1); break; case N_DO: n->r.right = opt_stmt(n->r.right, &d1); side_start(&side, depth); d2 = opt_cond(&n->l.left, FALSE); d2 = max2(d2, side_end(&n->l.left, side, (node **) NULL, 0)); d = max3(d, d1, d2); break; case N_FOR: side_start(&side, depth); d1 = opt_cond(&n->l.left, FALSE); d1 = max2(d1, side_end(&n->l.left, side, (node **) NULL, 0)); i = opt_const(n->l.left); if (i == 0) { /* never */ n->r.right = opt_skip(n->r.right); if (n->r.right == (node *) NULL) { n->type = N_POP; n = opt_stmt(n, &d1); d = max2(d, d1); break; } } else if (i > 0) { /* always */ n->type = N_FOREVER; n = opt_stmt(n, &d1); d = max2(d, d1); break; } n->r.right = opt_stmt(n->r.right, &d2); d = max3(d, d1, d2); break; case N_FOREVER: if (n->l.left != (node *) NULL) { side_start(&side, depth); d1 = opt_expr(&n->l.left, TRUE); d1 = max2(d1, side_end(&n->l.left, side, (node **) NULL, 0)); if (d1 == 0) { n->l.left = (node *) NULL; } } else { d1 = 0; } n->r.right = opt_stmt(n->r.right, &d2); d = max3(d, d1, d2); break; case N_RLIMITS: side_start(&side, depth); d1 = opt_expr(&n->l.left->l.left, FALSE); d1 = max2(d1, side_end(&n->l.left->l.left, side, (node **) NULL, 0)); side_start(&side, depth); d2 = opt_expr(&n->l.left->r.right, FALSE); d2 = max2(d2, side_end(&n->l.left->r.right, side, (node **) NULL, 0)); d1 = max2(d1, d2 + 1); n->r.right = opt_stmt(n->r.right, &d2); d = max3(d, d1, d2); break; case N_CATCH: n->l.left = opt_stmt(n->l.left, &d1); if (n->l.left == (node *) NULL) { n = opt_stmt(opt_skip(n->r.right), &d1); d = max2(d, d1); } else { n->r.right = opt_stmt(n->r.right, &d2); d = max3(d, d1, d2); } break; case N_IF: side_start(&side, depth); d1 = opt_cond(&n->l.left, FALSE); d1 = max2(d1, side_end(&n->l.left, side, (node **) NULL, 0)); i = opt_const(n->l.left); if (i == 0) { n->r.right->l.left = opt_skip(n->r.right->l.left); } else if (i > 0) { n->r.right->r.right = opt_skip(n->r.right->r.right); } n->r.right->l.left = opt_stmt(n->r.right->l.left, &d2); d1 = max2(d1, d2); n->r.right->r.right = opt_stmt(n->r.right->r.right, &d2); if (n->r.right->l.left == (node *) NULL) { if (n->r.right->r.right == (node *) NULL) { n->type = N_POP; n = opt_stmt(n, &d1); d = max2(d, d1); break; } n->l.left = opt_not(n->l.left); n->r.right->l.left = n->r.right->r.right; n->r.right->r.right = (node *) NULL; } d = max3(d, d1, d2); break; case N_PAIR: n = opt_stmt(n, &d1); d = max2(d, d1); break; case N_POP: side_start(&side, depth); d1 = opt_expr(&n->l.left, TRUE); if (d1 == 0) { n->l.left = (node *) NULL; } d = max3(d, d1, side_end(&n->l.left, side, (node **) NULL, 0)); if (n->l.left == (node *) NULL) { n = (node *) NULL; } break; case N_RETURN: side_start(&side, depth); d1 = opt_expr(&n->l.left, FALSE); d = max3(d, d1, side_end(&n->l.left, side, (node **) NULL, 0)); break; case N_SWITCH_INT: case N_SWITCH_RANGE: case N_SWITCH_STR: n->r.right->r.right = opt_stmt(n->r.right->r.right, &d1); if (n->r.right->r.right == (node *) NULL) { n = n->r.right; n->type = N_POP; n = opt_stmt(n, &d1); d = max2(d, d1); } else { side_start(&side, depth); d2 = opt_expr(&n->r.right->l.left, FALSE); d2 = max2(d2, side_end(&n->r.right->l.left, side, (node **) NULL, 0)); d = max3(d, d1, d2); } break; } if ((*m)->type == N_PAIR) { if (n == (node *) NULL) { *m = (*m)->r.right; } else { (*m)->l.left = n; if (n->flags & F_END) { n = opt_skip((*m)->r.right); if (n == (node *) NULL) { *m = (*m)->l.left; break; } (*m)->r.right = n; } prev = m; m = &(*m)->r.right; } } else { *m = n; if (n == (node *) NULL && prev != m) { *prev = (*prev)->l.left; } break; } } *depth = d; return first; }
/** * @brief Changes colour space from RGB to HSV. * * All values go from 0 to 1, except H which is 0-360. * * Taken from (GIFT) GNU Image Finding Tool. * * @param[out] H Stores Hue. * @param[out] S Stores Saturation. * @param[out] V Stores Value. * @param R Red to convert. * @param G Green to convert. * @param B Blue to convert. */ void col_rgb2hsv( double *H, double *S, double *V, double R, double G, double B ) { double H1, S1, V1; #ifdef HSV_TRAVIS double R1, G1, B1; #endif /* HSV_TRAVIS */ double max, min, diff; max = max3( R, G, B ); min = min3( R, G, B ); diff = max - min; if (max == 0) H1 = S1 = V1 = 0; else { V1 = max; S1 = diff/max; if (S1 == 0) /* H1 is undefined, but give it a value anyway */ H1 = 0; else { #ifdef HSV_TRAVIS R1 = (max - R)/diff; G1 = (max - G)/diff; B1 = (max - B)/diff; if ((R == max) && (G == min)) H1 = 5 + B1; else { if ((R == max) && (G != min)) H1 = 1 - G1; else { if ((G == max) && (B == min)) H1 = 1 + R1; else { if ((G == max) && (B != min)) H1 = 3 - B1; else { if (R == max) H1 = 3 + G1; else H1 = 5 - R1; } } } } H1 *= 60; /* convert to range [0, 360] degrees */ #else /* HSV_TRAVIS */ H1 = 0.; /* Shuts up Clang. */ /* assume Foley & VanDam HSV */ if (R == max) H1 = (G - B)/diff; if (G == max) H1 = 2 + (B - R)/diff; if (B == max) H1 = 4 + (R - G)/diff; H1 *= 60; /* convert to range [0, 360] degrees */ if (H1 < 0) H1 += 360; #endif /* HSV_TRAVIS */ } } *H = H1; *S = S1; *V = V1; }
/* smith-waterman alignment function */ int swalign (char seq1[], char seq2[], char bts1[], char bts2[], char aln[]) { int mat[ MAXSEQ + 1 ][ MAXSEQ + 1 ]; // score matrix int i, j; // matrix indices int m, n; // sequences length char btm[ MAXSEQ + 1 ][ MAXSEQ + 1 ]; // back trace mark int d, u, l; // source score int me, mi, mj; // max element and its indices int bi; // back trace index, forward /* initialization the matrix */ m = strlen( seq1 ); n = strlen( seq2 ); for ( i = 0; i <= m; i++ ) mat[i][0] = 0; for ( j = 0; j <= n; j++ ) mat[0][j] = 0; /* filling the matrix */ for ( i = 1; i <= m; i ++ ) for ( j = 1; j <= n; j++ ) { // diagonal d = mat[i-1][j-1] + score( seq1[i-1], seq2[j-1] ); // up u = mat[i-1][j] + score( seq1[i-1], '-' ); // left l = mat[i][j-1] + score( '-', seq2[j-1] ); // find the max score mat[i][j] = max3( d, u, l ); // mark the trace path btm[i][j] = mss( d, u, l ); } /* back trace */ /* // print the back trace matrix for ( i = 0; i <= m; i++ ) { for ( j = 0; j <= n; j++ ) printf("%2d %c\t", mat[i][j], btm[i][j]); printf("\n"); } */ // find max element and its indices me = 0; for ( i = 1; i <= m; i++ ) for ( j = 1; j <= n; j++ ) { if ( mat[i][j] > me ) { me = mat[i][j]; mi = i; mj = j; } } // back trace for ( i = mi, j = mj, bi = 0 ; i >= 0; bi++ ) if ( btm[i][j] == '\\' ) { bts1[bi] = seq1[i - 1]; bts2[bi] = seq2[j - 1]; i--; j--; } else if ( btm[i][j] == '_') { bts1[bi] = '-'; bts2[bi] = seq2[j - 1]; j--; } else if ( btm[i][j] == '|' ) { bts1[bi] = seq1[i - 1] ; bts2[bi] = '-'; i--; } else { bts1[bi] = '\0'; bts2[bi] = '\0'; revseq(bts1); revseq(bts2); break; } // get aligment string align_str( bts1, bts2, aln ); return me; } // end of swalign