Esempio n. 1
0
int main()
{
  int i;
  int j;
  int m;
  i = 10;
  j = 30;
  {
    int TEMP_functionCall_5;
    TEMP_functionCall_5 = max4(i, 20, j, 40);
    m = TEMP_functionCall_5;
  }
  printf("%d\n", m);
  {
    int TEMP_functionCall_6;
    TEMP_functionCall_6 = max4(40, j, 20, i);
    m = TEMP_functionCall_6;
  }
  printf("%d\n", m);
  {
    int TEMP_functionCall_7;
    TEMP_functionCall_7 = max4(1, j, i, i);
    m = TEMP_functionCall_7;
  }
  printf("%d\n", m);
  {
    int TEMP_functionCall_8;
    TEMP_functionCall_8 = max4(i, i, i, i);
    m = TEMP_functionCall_8;
  }
  printf("%d\n", m);
  return 0;
}
Esempio n. 2
0
static void
compute_extents (pixman_f_transform_t *trans, double *sx, double *sy)
{
    double min_x, max_x, min_y, max_y;
    pixman_f_vector_t v[4] =
    {
	{ { 1, 1, 1 } },
	{ { -1, 1, 1 } },
	{ { -1, -1, 1 } },
	{ { 1, -1, 1 } },
    };

    pixman_f_transform_point (trans, &v[0]);
    pixman_f_transform_point (trans, &v[1]);
    pixman_f_transform_point (trans, &v[2]);
    pixman_f_transform_point (trans, &v[3]);

    min_x = min4 (v[0].v[0], v[1].v[0], v[2].v[0], v[3].v[0]);
    max_x = max4 (v[0].v[0], v[1].v[0], v[2].v[0], v[3].v[0]);
    min_y = min4 (v[0].v[1], v[1].v[1], v[2].v[1], v[3].v[1]);
    max_y = max4 (v[0].v[1], v[1].v[1], v[2].v[1], v[3].v[1]);

    *sx = (max_x - min_x) / 2.0;
    *sy = (max_y - min_y) / 2.0;
}
Esempio n. 3
0
// _TrackMouse
void
TestView::_TrackMouse(BPoint where)
{
	BRect before;
	BRect after;
	bool invalidate = false;
	switch (fTracking) {
		case TRACKING_SOURCE:
			before = fSourceRect;
			fSourceRect.right = where.x;
			fSourceRect.bottom = where.y;
			after = fSourceRect;
			invalidate = true;
			break;
		case TRACKING_DEST:
			before = fDestRect;
			fDestRect.right = where.x;
			fDestRect.bottom = where.y;
			after = fDestRect;
			invalidate = true;
			break;
	}
	if (invalidate) {
		BRect dirty(min4(before.left, before.right, after.left, after.right),
					min4(before.top, before.bottom, after.top, after.bottom),
					max4(before.left, before.right, after.left, after.right),
					max4(before.top, before.bottom, after.top, after.bottom));
		Invalidate(dirty);
	}
}
Esempio n. 4
0
static void cmd640_set_timing(int if_num, int dr_num)
{
	int	b_reg;
	int	ac, rc, at;

	/*
	 * Set address setup count and drive read/write timing registers.
	 * Primary interface has individual count/timing registers for
	 * each drive. Secondary interface has common set of registers, and
	 * we should set timings for the slowest drive.
	 */

	if (if_num == 0) {
		b_reg = dr_num ? ARTTIM1 : ARTTIM0;
		at = arttim[dr_num];
		ac = a_count[dr_num];
		rc = r_count[dr_num];
	} else {
		b_reg = ARTTIM23;
		at = max(arttim[2], arttim[3]);
		ac = max(a_count[2], a_count[3]);
		rc = max(r_count[2], r_count[3]);
	}

	put_cmd640_reg(b_reg, pack_arttim(at));
	put_cmd640_reg(b_reg + 1, pack_counts(ac, rc));

	/*
	 * Update CMDTIM (IDE Command Block Timing Register) 
	 */

	ac = max4(r_count);
	rc = max4(a_count);
	put_cmd640_reg(CMDTIM, pack_counts(ac, rc));
}
void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3)
{
    float left = min4(p0.x(), p1.x(), p2.x(), p3.x());
    float top = min4(p0.y(), p1.y(), p2.y(), p3.y());
    float right = max4(p0.x(), p1.x(), p2.x(), p3.x());
    float bottom = max4(p0.y(), p1.y(), p2.y(), p3.y());

    setLocationAndSizeFromEdges(left, top, right, bottom);
}
Esempio n. 6
0
FloatRect FloatQuad::boundingBox() const
{
    float left   = min4(m_p1.x(), m_p2.x(), m_p3.x(), m_p4.x());
    float top    = min4(m_p1.y(), m_p2.y(), m_p3.y(), m_p4.y());

    float right  = max4(m_p1.x(), m_p2.x(), m_p3.x(), m_p4.x());
    float bottom = max4(m_p1.y(), m_p2.y(), m_p3.y(), m_p4.y());
    
    return FloatRect(left, top, right - left, bottom - top);
}
Esempio n. 7
0
int main()
{
  int i;
  int j;
  i = 10;
  j = 30;
  printf("%d\n", max4(i, 20, j, 40));
  printf("%d\n", max4(40, j, 20, i));
  printf("%d\n", max4(1, j, i, i));
  printf("%d\n", max4(i, i, i, i));
  return 0;
}
static inline double contrast(double a, double b, double c, double d)
{
    const double min = min4(a, b, c, d);
    const double max = max4(a, b, c, d);

    return (max - min) / (max + min);
}
Esempio n. 9
0
static double _get_move_time()
{
	uint8_t i;
	double inv_time=0;	// inverse time if doing a feed in G93 mode
	double xyz_time=0;	// coordinated move linear part at req feed rate
	double abc_time=0;	// coordinated move rotary part at req feed rate
	double max_time=0;	// time required for the rate-limiting axis

	// compute times for feed motion
	if (gm.motion_mode == MOTION_MODE_STRAIGHT_FEED) {
		if (gm.inverse_feed_rate_mode == true) {
			inv_time = gm.inverse_feed_rate;
		} else {
			xyz_time = sqrt(square(gm.target[X] - gm.position[X]) + // in mm
							square(gm.target[Y] - gm.position[Y]) +
							square(gm.target[Z] - gm.position[Z])) / gm.feed_rate;

			abc_time = sqrt(square(gm.target[A] - gm.position[A]) + // in deg
							square(gm.target[B] - gm.position[B]) +
							square(gm.target[C] - gm.position[C])) / gm.feed_rate;
		}
	}
 	for (i=0; i<AXES; i++) {
		if (gm.motion_mode == MOTION_MODE_STRAIGHT_FEED) {
			max_time = max(max_time, (fabs(gm.target[i] - gm.position[i]) / cfg.a[i].feedrate_max));
		} else { // gm.motion_mode == MOTION_MODE_STRAIGHT_TRAVERSE
			max_time = max(max_time, (fabs(gm.target[i] - gm.position[i]) / cfg.a[i].velocity_max));
		}
	}
	return (max4(inv_time, max_time, xyz_time, abc_time));
}
Esempio n. 10
0
/* Main computational kernel. The whole function will be timed,
   including the call and return. */
static
void kernel_mvt(int q, int d,
		DATA_TYPE POLYBENCH_1D(QS,Q,q),
		DATA_TYPE POLYBENCH_1D(DB,D,d),
		DATA_TYPE POLYBENCH_2D(MatchQ,Q,D,q,d),
		DATA_TYPE POLYBENCH_2D(M,Q,D,q,d))
{
  int i, j;

#pragma scop

for(i=0;i< _PB_Q; i++)
	for(j=0;j< _PB_D; j++)
	{
		if(i==0 || j==0)
			M[i][j] = 0;
		else
			M[i][j] = max4(0, M[i][j-1]-8, M[i-1][j]-8,M[i-1][j-1]+ MatchQ[i][j]);

 
	}
/*
case
{| i=0} | {| 1<=i; j=0} : 0;
{| 1<=i; 1<=j} : Max4(0, M[i,j-1] - 8,
M[i-1,j] - 8, M[i-1,j-1] + MatchQ[i,j]);
esac;*/


#pragma endscop

}
Esempio n. 11
0
void main()
{ int max4(int a,int b,int c,int d); 
  int a,b,c,d,max;
  printf("Please enter 4 interger numbers:");
  scanf("%d %d %d %d",&a,&b,&c,&d);
  max=max4(a,b,c,d);
  printf("max=%d \n",max);
} 
Esempio n. 12
0
// _ControlPointRect
BRect
PathManipulator::_ControlPointRect(int32 index, uint32 mode) const
{
	BRect rect(0.0, 0.0, -1.0, -1.0);
	if (index >= 0) {
		BPoint p, pIn, pOut;
		fPath->GetPointsAt(index, p, pIn, pOut);
		switch (mode) {
			case MOVE_POINT:
			case TOGGLE_SHARP:
			case REMOVE_POINT:
			case CLOSE_PATH:
				rect.Set(p.x, p.y, p.x, p.y);
				rect.InsetBy(-POINT_EXTEND, -POINT_EXTEND);
				break;
			case MOVE_POINT_IN:
			case TOGGLE_SHARP_IN:
			case REMOVE_POINT_IN:
				rect.Set(pIn.x, pIn.y, pIn.x, pIn.y);
				rect.InsetBy(-CONTROL_POINT_EXTEND, -CONTROL_POINT_EXTEND);
				break;
			case MOVE_POINT_OUT:
			case TOGGLE_SHARP_OUT:
			case REMOVE_POINT_OUT:
				rect.Set(pOut.x, pOut.y, pOut.x, pOut.y);
				rect.InsetBy(-CONTROL_POINT_EXTEND, -CONTROL_POINT_EXTEND);
				break;
			case SELECT_POINTS:
				rect.Set(min4(p.x, pIn.x, pOut.x, pOut.x),
						 min4(p.y, pIn.y, pOut.y, pOut.y),
						 max4(p.x, pIn.x, pOut.x, pOut.x),
						 max4(p.y, pIn.y, pOut.y, pOut.y));
				rect.InsetBy(-POINT_EXTEND, -POINT_EXTEND);
				break;
		}
	}
	return rect;
}
int main()
{
	int n1, n2, n3, n4;
	puts("四つの整数を入力してください。");
	printf("整数n1:");   scanf("%d", &n1);
	printf("整数n2:");   scanf("%d", &n2);
	printf("整数n3:");   scanf("%d", &n3);
	printf("整数n4:");   scanf("%d", &n4);

	printf("最も大きい値は%dです。\n", max4(n1, n2, n3, n4));

	getchar();
	getchar();
	return 0;
}
// 0:00 1:01 2:10 3:11 4:All
int main(){
	scanf("%u%u%u",&N,&M,&K);
	memset(f[F],0x80,sizeof(f[F]));
	f[F][0][0]=0;
	if(M==1)
		for(i=1;i<=N;++i){
			F^=1;
			scanf("%d",&v0);
			memcpy(f[F],f[!F],sizeof(f[F]));
			for(k=0;k<=K;++k){
				if(k<1) continue;
				maxi(f[F][k][1],f[!F][k-1][0]);
			}
			for(k=0;k<=K;++k)
				f[F][k][1]+=v0;
			for(k=K;k>=0;--k)
				maxi(f[F][k][0],f[F][k][1]);
		}
	else
		for(i=1;i<=N;++i){
			F^=1;
			scanf("%d %d",&v0,&v1);
			memcpy(f[F],f[!F],sizeof(f[F]));
			for(k=0;k<=K;++k){
				if(k<1) continue;
				maxi(f[F][k][1],f[!F][k-1][0]);
				maxi(f[F][k][2],f[!F][k-1][0]);
				maxi(f[F][k][3],max(f[!F][k-1][1],f[!F][k-1][2]));
				maxi(f[F][k][4],f[!F][k-1][0]);
				if(k<2) continue;
				maxi(f[F][k][3],f[!F][k-2][0]);
			}
			for(k=0;k<=K;++k){
				f[F][k][1]+=v0;
				f[F][k][2]+=v1;
				f[F][k][3]+=v0+v1;
				f[F][k][4]+=v0+v1;
			}
			for(k=K;k>=0;--k){
				maxi(f[F][k][0],max4(f[F][k][1],f[F][k][2],f[F][k][3],f[F][k][4]));
				maxi(f[F][k][1],f[F][k][3]);
				maxi(f[F][k][2],f[F][k][3]);
			}
		}
	printf("%d\n",f[F][K][0]);
}
Esempio n. 15
0
static void _calc_move_times(GCodeState_t *gms, const float axis_length[], const float axis_square[])
										// gms = Gcode model state
{
	float inv_time=0;				// inverse time if doing a feed in G93 mode
	float xyz_time=0;				// coordinated move linear part at requested feed rate
	float abc_time=0;				// coordinated move rotary part at requested feed rate
	float max_time=0;				// time required for the rate-limiting axis
	float tmp_time=0;				// used in computation
	gms->minimum_time = 8675309;	// arbitrarily large number

	// compute times for feed motion
	if (gms->motion_mode != MOTION_MODE_STRAIGHT_TRAVERSE) {
		if (gms->feed_rate_mode == INVERSE_TIME_MODE) {
			inv_time = gms->feed_rate;	// NB: feed rate was un-inverted to minutes by cm_set_feed_rate()
			gms->feed_rate_mode = UNITS_PER_MINUTE_MODE;
		} else {
			// compute length of linear move in millimeters. Feed rate is provided as mm/min
			xyz_time = sqrt(axis_square[AXIS_X] + axis_square[AXIS_Y] + axis_square[AXIS_Z]) / gms->feed_rate;

			// if no linear axes, compute length of multi-axis rotary move in degrees. Feed rate is provided as degrees/min
			if (fp_ZERO(xyz_time)) {
				abc_time = sqrt(axis_square[AXIS_A] + axis_square[AXIS_B] + axis_square[AXIS_C]) / gms->feed_rate;
			}
		}
	}
	for (uint8_t axis = AXIS_X; axis < AXES; axis++) {
		if (gms->motion_mode == MOTION_MODE_STRAIGHT_TRAVERSE) {
			tmp_time = fabs(axis_length[axis]) / cm.a[axis].velocity_max;
		} else { // MOTION_MODE_STRAIGHT_FEED
			tmp_time = fabs(axis_length[axis]) / cm.a[axis].feedrate_max;
		}
		max_time = max(max_time, tmp_time);

		if (tmp_time > 0) { 	// collect minimum time if this axis is not zero
			gms->minimum_time = min(gms->minimum_time, tmp_time);
		}
	}
	gms->move_time = max4(inv_time, max_time, xyz_time, abc_time);
}
Esempio n. 16
0
File: dp.c Progetto: stollcri/fux
static int fillmatrix(int *dpmatrix, int xsize, int ysize, char *xstring, char *ystring)
{
	int result = 0;

	int currentxchar = 0;
	int currentychar = 0;
	int gapscore = 0;
	int charscore = 0;

	int currentindex = 0;
	int indexdiag = 0;
	int indexleft = 0;
	int indexabove = 0;

	if (DEBUG_PRINT_MATRIX) printf("\n");
	for (int y = 0; y < ysize; ++y) {
		for (int x = 0; x < xsize; ++x) {
			currentxchar = xstring[x - 1];
			currentychar = ystring[y - 1];

			// TODO: start both of the above loops at 1
			// 		 and remove this condition
			// 		 and remove the print condition below
			if ((x > 0) & (y > 0)) {
				gapscore = gappedscore(currentxchar, currentychar);
				charscore = characterscore(currentxchar, currentychar);

				currentindex = (y * xsize) + x;
				indexdiag = ((y - 1) * xsize) + (x - 1);
				indexleft = (y * xsize) + (x - 1);
				indexabove = ((y - 1) * xsize) + x;
				
				// the value of the current pixels is:
				// - the maximum of:
				//   - the diagonal value and the current score (there is a series of matches)
				//   - 0 (just in case all scores are below zero)
				//   - the above value and the gap score (there is a gap in the matches)
				//   - the left value and the gap score (there is a gap in the matches)
				// - minus the cost of movement
				dpmatrix[currentindex] = max4( \
					dpmatrix[indexdiag] + charscore, \
					0, \
					dpmatrix[indexabove] + gapscore, \
					dpmatrix[indexleft] + gapscore \
				) + MOVE_COST;

				// values above and left are higher than diagonal, suggesting a transpose
				if ((dpmatrix[indexabove] > dpmatrix[indexdiag]) && (dpmatrix[indexleft] > dpmatrix[indexdiag])) {
					// give the diagonal the maximum value of the other neighbors
					dpmatrix[indexdiag] = max( \
						dpmatrix[indexabove], \
						dpmatrix[indexleft] \
					);
					// give current space the value of transposed caharacters, if less than current value
					dpmatrix[currentindex] = max( \
						dpmatrix[indexdiag] + transposescore(currentxchar, currentychar), \
						dpmatrix[currentindex] \
					);
				}
			}

			if (DEBUG_PRINT_MATRIX) {
				if ((x == 0) || (y == 0)) {
					if ((x == 0) && (y == 0)) {
						printf("  ");
					}
					if ((x != 0) && (y == 0)) {
						printf("_%c_ ", currentxchar);
					}
					if ((x == 0) && (y != 0)) {
						printf("%c ", currentychar);
					}
				} else {
					printf("%3d ", dpmatrix[currentindex]);
				}
			}
		}
		if (DEBUG_PRINT_MATRIX) printf("\n");
	}
	if (DEBUG_PRINT_MATRIX) printf("\n");
	return result;
}
Esempio n. 17
0
int main()
{
    FILE *fin = fopen("packrec.in", "r");
    FILE *fout = fopen("packrec.out", "w");
    int i, j, k, l, p, index;
    int min = 0x7fff;
    index = 0;
    struct rec in[4], out[100];
    
    for(i = 0; i < 4; i++)
        fscanf(fin, "%d %d", &in[i].side[0], &in[i].side[1]);
    
    for(i = 0; i < 4; i++){
        for(j = 0; j < 4; j++){
            if(i == j)
                continue;
            for(k = 0; k < 4; k++){
                if(k == i || k == j)
                    continue;
                l = 6 - i - j - k;
                for(p = 0; p < 16; p++){
                    int h1, w1, h2, w2, h3, w3, h4, w4;
                    h1 = in[i].side[1 - (ri(p))];
                    w1 = in[i].side[ri(p)];
                    h2 = in[j].side[1 - (rj(p))];
                    w2 = in[j].side[rj(p)];
                    h3 = in[k].side[1 - (rk(p))];
                    w3 = in[k].side[rk(p)];
                    h4 = in[l].side[1 - (rl(p))];
                    w4 = in[l].side[rl(p)];
                    
                    int win, hin;
                    win = w1 + w2 + w3 + w4;
                    hin = max4(h1, h2, h3, h4);
                    store(out, &index, win, hin, &min); 

                    win = max2(w1+w2+w3, w4);
                    hin = max3(h1, h2, h3)+h4;
                    store(out, &index, win, hin, &min);
                    
                    win = max2(w1+w2, w3)+w4;
                    hin = max3(h1+h3, h2+h3, h4);
                    store(out, &index, win, hin, &min);
                    
                    win = w1+w4+max2(w2, w3);
                    hin = max3(h1, h2+h3, h4);
                    store(out, &index, win, hin, &min);
                    
                    if(h3 >= h2+h4)
                        win = max3(w1, w2+w3, w3+w4);
                    else if(h3 > h4 && h3 < h2+h4)
                        win = max3(w1+w2, w2+w3, w3+w4);
                    else if(h3 < h4 && h2+h4 <= h1+h3)
                        win = max3(w1+w2, w1+w4, w3+w4);
                    else if(h4 >= h1+h3)
                        win = max3(w2, w1+w4, w3+w4);
                    else if(h3 == h4)
                        win = max2(w1+w2, w3+w4);
                    hin = max2(h1+h3, h2+h4);
                    store(out, &index, win, hin, &min);
                }
            }
        }
    }
    
    insertionSort(out, index);
    fprintf(fout, "%d\n", min);
    for(i = 0; i < index; i++){
        if(i != 0 && out[i].side[0] == out[i-1].side[0])
            continue;
        fprintf(fout, "%d %d\n", out[i].side[0], out[i].side[1]);
    }
    
    fclose(fin);
    fclose(fout);
}
Intervall operator*(Intervall a, Intervall b){
    return Intervall(min4(a.inf*b.inf,a.inf*b.sup,a.sup*b.inf,a.sup*b.sup),max4(a.inf*b.inf,a.inf*b.sup,a.sup*b.inf,a.sup*b.sup));
}
Esempio n. 19
0
vector<squares::pair> squares::bound_box_detect()
{
	vector<pair> collision_susp;
	priority_queue<points,vector<points>,points_compare_x> posi_x;
	vector<points> boundbox_info;
	for(int i=0;i<position.size();i++)
	{
		points sq;
		sq.posi_x=max4(position[i].pos[0].x,position[i].pos[1].x,position[i].pos[2].x,
				  position[i].pos[3].x)+dynamics[i].x.x;
		sq.posi_y=max4(position[i].pos[0].y,position[i].pos[1].y,position[i].pos[2].y,
					   position[i].pos[3].y)+dynamics[i].x.y;
		sq.square_no=i;
		sq.end=true;
		posi_x.push(sq);
		boundbox_info.push_back(sq);

		sq.posi_x=min4(position[i].pos[0].x,position[i].pos[1].x,position[i].pos[2].x,
					   position[i].pos[3].x)+dynamics[i].x.x;
		sq.posi_y=min4(position[i].pos[0].y,position[i].pos[1].y,position[i].pos[2].y,
					   position[i].pos[3].y)+dynamics[i].x.y;
		sq.square_no=i;
		sq.end=false;
		posi_x.push(sq);
		boundbox_info.push_back(sq);
		
	}

	list<int> conflict_list;
	list<int>::iterator conflict_list_itr=conflict_list.begin();
	priority_queue<points,vector<points>,points_compare_x> posi_y;

	while(posi_x.size())
	{
		if(posi_x.top().end==false)
			conflict_list.push_back(posi_x.top().square_no);

		if(posi_x.top().end==true)
		{
			conflict_list_itr=conflict_list.begin();
			for(int i=0;i<conflict_list.size();i++)
			{
				posi_y.push(boundbox_info[*conflict_list_itr*2]);
				posi_y.push(boundbox_info[*conflict_list_itr*2+1]);
				conflict_list_itr++;
			}
			list<int> conflict_list_y;
			list<int>::iterator conflict_list_y_itr=conflict_list_y.begin();
			pair pair_temp(posi_x.top().square_no,0);
			while(posi_y.size())
			{
				if(posi_y.top().end==false)
					conflict_list_y.push_back(posi_y.top().square_no);

				if(posi_y.top().end==true)
				{
					conflict_list_y.push_back(posi_y.top().square_no);
					while(conflict_list_y_itr!=conflict_list_y.end())
					{
						pair_temp.b=*conflict_list_y_itr;
						collision_susp.push_back(pair_temp);
						conflict_list_y_itr++;
					}
				}

				posi_y.pop();
			}
			conflict_list_itr=conflict_list.begin();
			conflict_list.remove(posi_x.top().square_no);

		}

		posi_x.pop();
	}

	return collision_susp;
}
Esempio n. 20
0
static int
proj_to_sr(const char *infile, const char *outfile, double pixel_size)
{
    int ii, jj, kk;
    const float_image_sample_method_t sampling_method =
        FLOAT_IMAGE_SAMPLE_METHOD_BILINEAR;

    // overall algorithm:
    // 1. find extents in time/slant space
    // 2. for each pixel in output, resample in input space

    meta_parameters *inMeta = meta_read(infile);
    int nl = inMeta->general->line_count;
    int ns = inMeta->general->sample_count;

    if (!inMeta->projection && !inMeta->transform)
        asfPrintError("Expected a projection/transform block!\n");
    if (!inMeta->state_vectors)
        asfPrintError("Input data does not have state vectors!\n");

    //asfPrintStatus("Converting %s to slant range...\n", infile);

    // first, find extents in time/slant space
    // do this by projecting image corners to time/slant
    int tl_x=0,    tl_y=0;
    int tr_x=ns-1, tr_y=0;
    int bl_x=0,    bl_y=nl-1;
    int br_x=ns-1, br_y=nl-1;

    // we have to find the "real" corners of the image
    // do this using the first band of the input image as a reference
    if (inMeta->general->band_count == 1)
        asfPrintStatus("Tiling the input image...\n");
    else
        asfPrintStatus("Tiling the reference band of the input image...\n");

    FloatImage *in = float_image_new_from_metadata(inMeta, infile);

    // find top left pixel -- TOP-most non-no-data pixel in the image
    for (ii=0; ii<nl; ++ii)
        for (jj=0; jj<ns; ++jj) {
            double val = float_image_get_pixel(in, jj, ii);
            if (val != inMeta->general->no_data && val != 0.0) {
                tl_x = jj; tl_y = ii;
                goto found_tl;
            }
        }

    asfPrintError("Couldn't find top-left pixel! Entire image no data?\n");

  found_tl:

    // find top right pixel -- RIGHT-most non-no-data pixel in the image
    for (jj=ns-1; jj>=0; --jj)
        for (ii=0; ii<nl; ++ii) {
            double val = float_image_get_pixel(in, jj, ii);
            if (val != inMeta->general->no_data && val != 0.0) {
                tr_x = jj; tr_y = ii;
                goto found_tr;
            }
        }

    asfPrintError("Couldn't find top-right pixel! Entire image no data?\n");

  found_tr:

    // find bottom left pixel -- LEFT-most non-no-data pixel in the image
    for (jj=0; jj<ns; ++jj)
        for (ii=nl-1; ii>=0; --ii) {
            double val = float_image_get_pixel(in, jj, ii);
            if (val != inMeta->general->no_data && val != 0.0) {
                bl_x = jj; bl_y = ii;
                goto found_bl;
            }
        }

    asfPrintError("Couldn't find bottom-left pixel! Entire image no data?\n");

  found_bl:

    // find bottom right pixel -- BOTTOM-most non-no-data pixel in the image
    for (ii=nl-1; ii>=0; --ii)
        for (jj=ns-1; jj>=0; --jj) {
            double val = float_image_get_pixel(in, jj, ii);
            if (val != inMeta->general->no_data && val != 0.0) {
                br_x = jj; br_y = ii;
                goto found_br;
            }
        }

    asfPrintError("Couldn't find bottom-right pixel! Entire image no data?\n");

  found_br:

    asfPrintStatus("Determining image extents in time/slant coordinates.\n");
    //asfPrintStatus("Corners are at: TL (%d,%d)\n", tl_y, tl_x);
    //asfPrintStatus(" (line,sample)  TR (%d,%d)\n", tr_y, tr_x);
    //asfPrintStatus("                BL (%d,%d)\n", bl_y, bl_x);
    //asfPrintStatus("                BR (%d,%d)\n", br_y, br_x);

    double tl_time, tl_slant;
    double tr_time, tr_slant;
    double bl_time, bl_slant;
    double br_time, br_slant;

    meta_get_timeSlantDop(inMeta, tl_y, tl_x, &tl_time, &tl_slant, NULL);
    meta_get_timeSlantDop(inMeta, tr_y, tr_x, &tr_time, &tr_slant, NULL);
    meta_get_timeSlantDop(inMeta, bl_y, bl_x, &bl_time, &bl_slant, NULL);
    meta_get_timeSlantDop(inMeta, br_y, br_x, &br_time, &br_slant, NULL);

    //asfPrintStatus("Corners are at: TL (%f,%f)\n", tl_time, tl_slant);
    //asfPrintStatus(" (time,slant)   TR (%f,%f)\n", tr_time, tr_slant);
    //asfPrintStatus("                BL (%f,%f)\n", bl_time, bl_slant);
    //asfPrintStatus("                BR (%f,%f)\n", br_time, br_slant);

    double slant_start = min4(tl_slant, tr_slant, bl_slant, br_slant);
    double slant_end = max4(tl_slant, tr_slant, bl_slant, br_slant);

    double time_min = min4(tl_time, tr_time, bl_time, br_time);
    double time_max = max4(tl_time, tr_time, bl_time, br_time);

    double slant_incr;
    double time_start, time_end, time_incr;
    int onl, ons;

    if (pixel_size > 0) {
        slant_incr = pixel_size;
        ons = (slant_end - slant_start) / slant_incr;

        if (inMeta->sar) {
            // in this case, the original data has a SAR block, we will use the
            // same azimuth time per pixel.
            time_incr = inMeta->sar->azimuth_time_per_pixel;

            // we always want to be DECREASING in time
            // latest time is on top (line 1), earliest on bottom (line ONL)
            if (time_incr > 0) {
                time_incr = -time_incr;
                inMeta->sar->azimuth_time_per_pixel =
                    -inMeta->sar->azimuth_time_per_pixel;
            }

            time_start = time_max;
            time_end = time_min;

            onl = (time_end - time_start) / time_incr;
        }
        else {
            // here, no sar block in the original data, just make a square
            // image with decreasing time
            onl = ons;
            time_incr = (time_min - time_max) / (double)onl;
            time_start = time_max;
            time_end = time_min;
        }
    }
    else {
        // not provided a slant range pixel size, we'll figure something out

        if (inMeta->sar) {
            // use the same azimuth time per pixel.
            time_incr = inMeta->sar->azimuth_time_per_pixel;

            // we always want to be DECREASING in time
            // latest time is on top (line 1), earliest on bottom (line ONL)
            if (time_incr > 0) {
                time_incr = -time_incr;
                inMeta->sar->azimuth_time_per_pixel =
                    -inMeta->sar->azimuth_time_per_pixel;
            }

            time_start = time_max;
            time_end = time_min;

            onl = (time_end - time_start) / time_incr;
        }
        else {
            // no info... determine azimuth time per pixel by keeping
            // the height the same as in the original image
            onl = nl;
            time_incr = (time_min - time_max) / (double)onl;
            time_start = time_max;
            time_end = time_min;
        }

        // make it square, to get the slant range pixel size
        ons = onl;
        pixel_size = slant_incr = (slant_end - slant_start) / (double)ons;
    }

    asfRequire(onl > 0, "Internal Error: Invalid output line count: %d\n", onl);
    asfRequire(ons > 0, "Internal Error: Invalid output sample count: %d\n", ons);

    asfPrintStatus("  Slant range values: %f -> %f\n", slant_start, slant_end);
    asfPrintStatus("  Slant range pixel size: %f\n", pixel_size);
    asfPrintStatus("  Time values: %f -> %f\n", time_start, time_end);
    asfPrintStatus("  Output Image will be %5d x %5d LxS\n", onl, ons);
    asfPrintStatus("      (Input Image was %5d x %5d LxS)\n", nl, ns);

    // generate a grid over the image, to generate our splines
    // this grid size seems to work pretty well...
    int n = 120;

    asfPrintStatus("Creating %dx%d mapping grid...\n", n, n);
 
    // changed how these are calculated, so that the spline will cover
    // the entire value range
    double time_grid_incr = fabs(time_end - time_start) / (double)(n-1);
    if (time_incr < 0) time_grid_incr = -time_grid_incr;
    double slant_grid_incr = fabs(slant_end - slant_start) / (double)(n-1);
    if (slant_incr < 0) slant_grid_incr = -slant_grid_incr;    

    // allocating memory for the splines, and the arrays to generate them
    gsl_interp_accel **samp_accels = MALLOC(sizeof(gsl_interp_accel *) * n);
    gsl_spline **samp_splines = MALLOC(sizeof(gsl_spline *) * n);

    gsl_interp_accel **line_accels = MALLOC(sizeof(gsl_interp_accel *) * n);
    gsl_spline **line_splines = MALLOC(sizeof(gsl_spline *) * n);

    double *slant_in = MALLOC(sizeof(double)*n);
    double *line_out = MALLOC(sizeof(double)*n);
    double *samp_out = MALLOC(sizeof(double)*n);

    // an alias -- use the same array (to save memory -- these are not used
    // at the same time), but create an alias for it, so it is not so confusing
    double *time_in = slant_in;
    //double max_err = 0;

    // set up the vertical splines
    for (jj=0; jj<n; ++jj) {
        double slant = slant_start + jj*slant_grid_incr;

        for (ii=0; ii<n; ++ii) {
            // splines need strictly increasing range variables
            if (time_grid_incr > 0)
                time_in[ii] = time_start + ii*time_grid_incr;
            else
                time_in[ii] = time_end - ii*time_grid_incr;

            ts2ls(inMeta, time_in[ii], slant, &line_out[ii], &samp_out[ii]);
            //printf("time: %f, slant: %f ==> line: %f, samp %f\n",
            //       time_in[ii], slant, line_out[ii], samp_out[ii]);
        }

        samp_accels[jj] = gsl_interp_accel_alloc();
        samp_splines[jj] = gsl_spline_alloc(gsl_interp_cspline, n);
        gsl_spline_init(samp_splines[jj], time_in, samp_out, n);

        line_accels[jj] = gsl_interp_accel_alloc();
        line_splines[jj] = gsl_spline_alloc(gsl_interp_cspline, n);
        gsl_spline_init(line_splines[jj], time_in, line_out, n);
    }

    // now, we're on to the resampling stage.. loop through output pixels
    asfPrintStatus("Generating slant range image...\n");

    double no_data_value = meta_is_valid_double(inMeta->general->no_data) ?
        inMeta->general->no_data : 0;

    // keep track of error sizes
    double max_error = 0;
    double avg_error = 0;
    int count = 0;

    // these stride values allow us to track when we're in between grid points
    int ii_n = onl/n;
    int jj_n = ons/n;
    int ii_n2 = ii_n/2;
    int jj_n2 = jj_n/2;

    // set up output metadata
    meta_parameters *outMeta = meta_read(infile);

    if (outMeta->transform) {
        FREE(outMeta->transform);
        outMeta->transform = NULL;
    }

    if (outMeta->projection) {
        FREE(outMeta->projection);
        outMeta->projection = NULL;
    }

    outMeta->general->line_count = onl;
    outMeta->general->sample_count = ons;

    if (!outMeta->sar)
        outMeta->sar = meta_sar_init();

    outMeta->sar->image_type = 'S';

    outMeta->sar->azimuth_time_per_pixel = time_incr;
    assert(outMeta->sar->azimuth_time_per_pixel < 0);

    outMeta->sar->time_shift = time_start;

    outMeta->general->y_pixel_size = inMeta->sar->azimuth_time_per_pixel / 
                                     time_incr * inMeta->general->y_pixel_size;
    assert(outMeta->general->y_pixel_size > 0);

    outMeta->sar->slant_range_first_pixel = slant_start;
    outMeta->general->x_pixel_size = slant_incr;

    outMeta->sar->line_increment = outMeta->sar->sample_increment = 1;
    outMeta->general->start_sample = outMeta->general->start_line = 0;

    outMeta->general->no_data = no_data_value;

    char **band_name = extract_band_names(inMeta->general->bands,
                                          inMeta->general->band_count);

    // now generate output image
    char *img_file = appendExt(outfile, ".img");
    float *out = MALLOC(sizeof(float) * ons);

    for (kk=0; kk<inMeta->general->band_count; ++kk) {
        if (inMeta->general->band_count != 1)
            asfPrintStatus("Working on band: %s\n", band_name[kk]);

        // for the 2nd and higher bands, free the band from the previous iteration,
        // and read in the next band from the input image
        if (kk>0) {
            float_image_free(in);
            asfPrintStatus("Loading input...\n");
            in = float_image_band_new_from_metadata(inMeta, kk, infile);
        }

        FILE *ofp = FOPEN(img_file, kk==0 ? "wb" : "ab");
        asfPrintStatus("Generating output...\n");
        for (ii=0; ii<onl; ++ii) {
            asfLineMeter(ii,onl);
            double time = time_start + ii * time_incr;

            // set up horizontal splines for this row
            gsl_interp_accel *samp_accel = gsl_interp_accel_alloc();
            gsl_spline *samp_spline = gsl_spline_alloc(gsl_interp_cspline, n);

            gsl_interp_accel *line_accel = gsl_interp_accel_alloc();
            gsl_spline *line_spline = gsl_spline_alloc(gsl_interp_cspline, n);

            //printf("time: %f slant: %f\n", time, slant_start);
            for (jj=0; jj<n; ++jj) {
                slant_in[jj] = slant_start + jj * slant_grid_incr;
                //printf("time: %f slant: %f\n", time, slant_in[jj]);
                samp_out[jj] = gsl_spline_eval_check(samp_splines[jj], time,
                                               samp_accels[jj]);
                line_out[jj] = gsl_spline_eval_check(line_splines[jj], time,
                                               line_accels[jj]);
                //printf("samp_out: %f line_out: %f\n", samp_out[jj], line_out[jj]);
            }

            gsl_spline_init(samp_spline, slant_in, samp_out, n);
            gsl_spline_init(line_spline, slant_in, line_out, n);

            // use the splines to produce output pixels
            for (jj=0; jj<ons; ++jj) {
                double slant = slant_start + jj * slant_incr;
                double samp = gsl_spline_eval_check(samp_spline, slant, samp_accel);
                double line = gsl_spline_eval_check(line_spline, slant, line_accel);

                // check the spline every so often (halfway between grid points)
                // only do this on band #1 (the reference band)
                if (kk==0 && ii%ii_n2==0 && 
                    ii%ii_n!=0 && jj%jj_n2==0 && jj%jj_n!=0)
                {
                    double samp_real, line_real;
                    ts2ls(inMeta, time, slant, &line_real, &samp_real);

                    double err = (line-line_real)*(line-line_real) +
                                 (samp-samp_real)*(samp-samp_real);

                    //printf("(%d,%d) -- Actual: (%f,%f) Splined: (%f,%f)\n",
                    //       ii, jj, line_real, samp_real, line, samp);

                    if (err > max_error) max_error = err;
                    avg_error += err;
                    ++count;
                }

                // now interpolate within the original image
                // if we are outside, use "no_data" from metadata
                double val = no_data_value;
                if (line > 0 && line < nl-1 && samp > 0 && samp < ns-1)
                    val = float_image_sample(in, samp, line, sampling_method);

                out[jj] = (float)val;
            }

            gsl_interp_accel_free(samp_accel);
            gsl_spline_free(samp_spline);

            gsl_interp_accel_free(line_accel);
            gsl_spline_free(line_spline);

            put_float_line(ofp, outMeta, ii, out);
        }

        fclose(ofp);
    }

    // free the last band of the input
    float_image_free(in);

    FREE(slant_in);
    FREE(line_out);
    FREE(samp_out);

    for (ii=0; ii<n; ++ii) {
        gsl_interp_accel_free(samp_accels[ii]);
        gsl_spline_free(samp_splines[ii]);
        gsl_interp_accel_free(line_accels[ii]);
        gsl_spline_free(line_splines[ii]);
    }

    FREE(samp_accels);
    FREE(samp_splines);
    FREE(line_accels);
    FREE(line_splines);

    FREE(out);

    for (kk=0; kk<inMeta->general->band_count; ++kk)
        FREE(band_name[kk]);
    FREE(band_name);

    // see how bad our errors were
    avg_error /= (double)count;
    asfPrintStatus("Model max error: %f, avg: %f\n",
                   max_error, avg_error);

    double thresh = 0.1;
    if (max_error > 100*thresh)
        asfPrintError("Maximum error exceeded threshold: %f > %f\n",
                      max_error, 100*thresh);
    else if (avg_error > 10*thresh)
        asfPrintError("Average error exceeded threshold: %f > %f\n",
                      avg_error, 10*thresh);
    if (max_error > 10*thresh)
        asfPrintWarning("Maximum error exceeds threshold: %f > %f\n",
                        max_error, 10*thresh);
    if (avg_error > thresh)
        asfPrintWarning("Average error exceeds threshold: %f > %f\n",
                        avg_error, thresh);

    char *meta_file = appendExt(outfile, ".meta");
    asfPrintStatus("Writing %s\n", meta_file);
    meta_write(outMeta, meta_file);
    free(meta_file);
    free(img_file);

    meta_free(outMeta);
    meta_free(inMeta);

    return 0; //success
}
Esempio n. 21
0
// Fill in traceback matrix
static void alignment_fill_matrices(aligner_t *aligner, char is_sw)
{
  score_t *match_scores = aligner->match_scores;
  score_t *gap_a_scores = aligner->gap_a_scores;
  score_t *gap_b_scores = aligner->gap_b_scores;
  const scoring_t *scoring = aligner->scoring;
  size_t score_width = aligner->score_width;
  size_t score_height = aligner->score_height;

  size_t i, j;

  const score_t min = is_sw ? 0 : SCORE_MIN;

  size_t seq_i, seq_j, len_i = score_width-1, len_j = score_height-1;
  size_t index, index_left, index_up, index_upleft;

  // [0][0]
  match_scores[0] = 0;
  gap_a_scores[0] = 0;
  gap_b_scores[0] = 0;

  if(is_sw)
  {
    for(i = 1; i < score_width; i++)
      match_scores[i] = gap_a_scores[i] = gap_b_scores[i] = 0;
    for(j = 1, index = score_width; j < score_height; j++, index += score_width)
      match_scores[index] = gap_a_scores[index] = gap_b_scores[index] = min;
  }
  else
  {
    // work along first row -> [i][0]
    for(i = 1; i < score_width; i++)
    {
      match_scores[i] = min;

      // Think carefully about which way round these are
      gap_a_scores[i] = min;
      gap_b_scores[i] = scoring->no_start_gap_penalty ? 0
                        : scoring->gap_open + (long)i * scoring->gap_extend;
    }

    // work down first column -> [0][j]
    for(j = 1, index = score_width; j < score_height; j++, index += score_width)
    {
      match_scores[index] = min;

      // Think carefully about which way round these are
      gap_a_scores[index] = scoring->no_start_gap_penalty ? 0
                            : scoring->gap_open + (long)j * scoring->gap_extend;
      gap_b_scores[index] = min;
    }
  }

  // These are longs to force addition to be done with higher accuracy
  long gap_open_penalty = scoring->gap_extend + scoring->gap_open;
  long gap_extend_penalty = scoring->gap_extend;
  long substitution_penalty;

  // start at position [1][1]
  index_upleft = 0;
  index_up = 1;
  index_left = score_width;
  index = score_width+1;

  for(seq_j = 0; seq_j < len_j; seq_j++)
  {
    for(seq_i = 0; seq_i < len_i; seq_i++)
    {
      // Update match_scores[i][j] with position [i-1][j-1]
      // substitution penalty
      bool is_match;
      int tmp_penalty;

      scoring_lookup(scoring, aligner->seq_a[seq_i], aligner->seq_b[seq_j],
                     &tmp_penalty, &is_match);

      if(scoring->no_mismatches && !is_match)
      {
        match_scores[index] = min;
      }
      else
      {
        substitution_penalty = tmp_penalty; // cast to long

        // substitution
        // 1) continue alignment
        // 2) close gap in seq_a
        // 3) close gap in seq_b
        match_scores[index]
          = max4(match_scores[index_upleft] + substitution_penalty,
                 gap_a_scores[index_upleft] + substitution_penalty,
                 gap_b_scores[index_upleft] + substitution_penalty,
                 min);
      }

      // Long arithmetic since some INTs are set to min and penalty is -ve
      // (adding as ints would cause an integer overflow)

      // Update gap_a_scores[i][j] from position [i][j-1]
      if(seq_i == len_i-1 && scoring->no_end_gap_penalty)
      {
        gap_a_scores[index] = MAX3(match_scores[index_up],
                                   gap_a_scores[index_up],
                                   gap_b_scores[index_up]);
      }
      else if(!scoring->no_gaps_in_a || seq_i == len_i-1)
      {
        gap_a_scores[index]
          = max4(match_scores[index_up] + gap_open_penalty,
                 gap_a_scores[index_up] + gap_extend_penalty,
                 gap_b_scores[index_up] + gap_open_penalty,
                 min);
      }
      else
        gap_a_scores[index] = min;

      // Update gap_b_scores[i][j] from position [i-1][j]
      if(seq_j == len_j-1 && scoring->no_end_gap_penalty)
      {
        gap_b_scores[index] = MAX3(match_scores[index_left],
                                   gap_a_scores[index_left],
                                   gap_b_scores[index_left]);
      }
      else if(!scoring->no_gaps_in_b || seq_j == len_j-1)
      {
        gap_b_scores[index]
          = max4(match_scores[index_left] + gap_open_penalty,
                 gap_a_scores[index_left] + gap_open_penalty,
                 gap_b_scores[index_left] + gap_extend_penalty,
                 min);
      }
      else
        gap_b_scores[index] = min;

      index++;
      index_left++;
      index_up++;
      index_upleft++;
    }

    index++;
    index_left++;
    index_up++;
    index_upleft++;
  }
}
Esempio n. 22
0
/*
 * TODO move this function to similarity.c
 */
static double _smithwaterman(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[i] == 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++)
    {
        /*
        		XXX why simmetrics does this way?
        		XXX original algorithm initializes first column with zeros

        		float c = swcost(a, b, i, 0);

        		if (i == 0)
        			matrix[0][0] = max3(0.0, -1 * PGS_SW_GAP_COST, c);
        		else
        			matrix[i][0] = max3(0.0, matrix[i-1][0] - PGS_SW_GAP_COST, c);

        		if (matrix[i][0] > maxvalue)
        			maxvalue = matrix[i][0];
        */
        matrix[i][0] = 0.0;
    }
    for (j = 0; j <= blen; j++)
    {
        /*
        		XXX why simmetrics does this way?
        		XXX original algorithm initializes first row with zeros

        		float c = swcost(a, b, 0, j);

        		if (j == 0)
        			matrix[0][0] = max3(0.0, -1 * PGS_SW_GAP_COST, c);
        		else
        			matrix[0][j] = max3(0.0, matrix[0][j-1] - PGS_SW_GAP_COST, c);

        		if (matrix[0][j] > maxvalue)
        			maxvalue = matrix[0][j];
        */
        matrix[0][j] = 0.0;
    }

    for (i = 1; i <= alen; i++)
    {
        for (j = 1; j <= blen; j++)
        {
            /* get operation cost */
            float c = swcost(a, b, i - 1, j - 1);

            matrix[i][j] = max4(0.0,
                                matrix[i-1][j] + PGS_SW_GAP_COST,
                                matrix[i][j-1] + PGS_SW_GAP_COST,
                                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 -- %.3f (%d, %d)",
                 i, j, a[i-1], b[j-1], c,
                 matrix[i-1][j] + PGS_SW_GAP_COST,
                 matrix[i][j-1] + PGS_SW_GAP_COST,
                 matrix[i-1][j-1] + c, matrix[i][j], matrix[i][j-1], i, j-1);

            if (matrix[i][j] > maxvalue)
                maxvalue = matrix[i][j];
        }
    }

    for (i = 0; i <= alen; i++)
        for (j = 0; j <= blen; j++)
            elog(DEBUG1, "(%d, %d) = %.3f", i, j, matrix[i][j]);

    for (i = 0; i <= alen; i++)
        free(matrix[i]);
    free(matrix);

    return maxvalue;
}
Esempio n. 23
0
/*
 * 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;
}
Esempio n. 24
0
void
transform_setup_source_rect(struct transformation *t)
{
	int i;
	struct coord screen[4];
	struct point screen_pnt[4];
	struct point_rect *pr;
	struct map_selection *ms,*msm,*next,**msm_last;
	ms=t->map_sel;
	while (ms) {
		next=ms->next;
		g_free(ms);
		ms=next;
	}
	t->map_sel=NULL;
	msm_last=&t->map_sel;
	ms=t->screen_sel;
	while (ms) {
		msm=g_new0(struct map_selection, 1);
		*msm=*ms;
		pr=&ms->u.p_rect;
		screen_pnt[0].x=pr->lu.x;	/* left upper */
		screen_pnt[0].y=pr->lu.y;
		screen_pnt[1].x=pr->rl.x;	/* right upper */
		screen_pnt[1].y=pr->lu.y;
		screen_pnt[2].x=pr->rl.x;	/* right lower */
		screen_pnt[2].y=pr->rl.y;
		screen_pnt[3].x=pr->lu.x;	/* left lower */
		screen_pnt[3].y=pr->rl.y;
		if (t->ddd) {
			struct coord_geo_cart tmp,cg[8];
			struct coord c;
			int valid=0;
			unsigned char edgenodes[]={
				0,1,
				1,2,
				2,3,
				3,0,
				4,5,
				5,6,
				6,7,
				7,4,
				0,4,
				1,5,
				2,6,
				3,7};	
			for (i = 0 ; i < 8 ; i++) {
				transform_screen_to_3d(t, &screen_pnt[i%4], (i >= 4 ? t->zfar:t->znear), &tmp);
				transform_apply_inverse_matrix(t, &tmp, &cg[i]);
			}
			msm->u.c_rect.lu.x=0;
			msm->u.c_rect.lu.y=0;
			msm->u.c_rect.rl.x=0;
			msm->u.c_rect.rl.y=0;
			for (i = 0 ; i < 12 ; i++) {
				if (transform_zplane_intersection(&cg[edgenodes[i*2]], &cg[edgenodes[i*2+1]], HOG(*t), &tmp) == 1) {
					c.x=tmp.x*(1 << t->scale_shift)+t->map_center.x;
					c.y=tmp.y*(1 << t->scale_shift)+t->map_center.y;
					dbg(1,"intersection with edge %d at 0x%x,0x%x\n",i,c.x,c.y);
					if (valid)
						coord_rect_extend(&msm->u.c_rect, &c);
					else {
						msm->u.c_rect.lu=c;
						msm->u.c_rect.rl=c;
						valid=1;
					}
					dbg(1,"rect 0x%x,0x%x - 0x%x,0x%x\n",msm->u.c_rect.lu.x,msm->u.c_rect.lu.y,msm->u.c_rect.rl.x,msm->u.c_rect.rl.y);
				}
			}
		} else {
			for (i = 0 ; i < 4 ; i++) {
				transform_reverse(t, &screen_pnt[i], &screen[i]);
				dbg(1,"map(%d) %d,%d=0x%x,0x%x\n", i,screen_pnt[i].x, screen_pnt[i].y, screen[i].x, screen[i].y);
			}
			msm->u.c_rect.lu.x=min4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
			msm->u.c_rect.rl.x=max4(screen[0].x,screen[1].x,screen[2].x,screen[3].x);
			msm->u.c_rect.rl.y=min4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
			msm->u.c_rect.lu.y=max4(screen[0].y,screen[1].y,screen[2].y,screen[3].y);
		}
		dbg(1,"%dx%d\n", msm->u.c_rect.rl.x-msm->u.c_rect.lu.x,
				 msm->u.c_rect.lu.y-msm->u.c_rect.rl.y);
		*msm_last=msm;
		msm_last=&msm->next;
		ms=ms->next;
	}
}