/// resizes the image to nh x nw using bilinear_interpolation uchar* resize_image( uchar* &image, int h, int w, int nh, int nw, bool in_place ) { uchar* out = kutility::zeros<uchar>( nh*nw ); double ratioy = h / (double)nh; double ratiox = w / (double)nw; int y, x, ynw; double ny, nx; #ifdef USE_OPENMP #pragma omp parallel for private( y, x, ny, nx, ynw ) #endif for( y=0; y<nh; y++ ) { ny = y * ratioy; ynw = y * nw; for( x=0; x<nw; x++ ) { nx = x * ratiox; out[ ynw + x ] = (uchar)bilinear_interpolation( image, w, nx, ny ); } } if( in_place ) { deallocate( image ); image = out; } return out; }
// zoom-in by replicating pixels into 2x2 blocks // no NAN's are expected in the input image static void zoom_in_by_factor_two(float *out, int ow, int oh, float *in, int iw, int ih) { for (int j = 0; j < oh; j++) for (int i = 0; i < ow; i++) out[ow*j+i] = bilinear_interpolation(in, iw, ih, (i-0.5)/2, (j-0.5)/2); }
// zoom-in by replicating pixels into 2x2 blocks // no NAN's are expected in the input image static void zoom_in_by_factor_two(float *out, int ow, int oh, float *in, int iw, int ih) { getpixel_operator p = getpixel_1; assert(abs(2*iw-ow) < 2); assert(abs(2*ih-oh) < 2); for (int j = 0; j < oh; j++) for (int i = 0; i < ow; i++) { float x = (i - 0.5)/2; float y = (j - 0.5)/2; out[ow*j+i] = bilinear_interpolation(in, iw, ih, x, y); //out[ow*j+i] = p(in, iw, ih, round(x), round(y)); } }
static force_inline uint32_t bits_image_fetch_pixel_bilinear (bits_image_t *image, pixman_fixed_t x, pixman_fixed_t y, get_pixel_t get_pixel) { pixman_repeat_t repeat_mode = image->common.repeat; int width = image->width; int height = image->height; int x1, y1, x2, y2; uint32_t tl, tr, bl, br; int32_t distx, disty; x1 = x - pixman_fixed_1 / 2; y1 = y - pixman_fixed_1 / 2; distx = pixman_fixed_to_bilinear_weight (x1); disty = pixman_fixed_to_bilinear_weight (y1); x1 = pixman_fixed_to_int (x1); y1 = pixman_fixed_to_int (y1); x2 = x1 + 1; y2 = y1 + 1; if (repeat_mode != PIXMAN_REPEAT_NONE) { repeat (repeat_mode, &x1, width); repeat (repeat_mode, &y1, height); repeat (repeat_mode, &x2, width); repeat (repeat_mode, &y2, height); tl = get_pixel (image, x1, y1, FALSE); bl = get_pixel (image, x1, y2, FALSE); tr = get_pixel (image, x2, y1, FALSE); br = get_pixel (image, x2, y2, FALSE); } else { tl = get_pixel (image, x1, y1, TRUE); tr = get_pixel (image, x2, y1, TRUE); bl = get_pixel (image, x1, y2, TRUE); br = get_pixel (image, x2, y2, TRUE); } return bilinear_interpolation (tl, tr, bl, br, distx, disty); }
void Solver::MiePrint(complex<double>* p, string name){ printf("Mie print\n"); ofstream ofs = WriteOpen(name+"Mie"); if(ofs) { for(int i=0; i<=180; i++){ double _x = 1.2*lambda_s*cos((180-i)*PI/180) + mField->getNx()/2; double _y = 1.2*lambda_s*sin((180-i)*PI/180) + mField->getNy()/2; double _val = bilinear_interpolation(p,_x,_y); ofs << _val << endl; } printf("output finished\n"); } else{ printf("No file"); } }
// Реализует функцию УОЗ от оборотов(мин-1) и нагрузки(кПа) для рабочего режима двигателя // Возвращает значение угла опережения в целом виде * 32, 2 * 16 = 32. int16_t work_function(struct ecudata_t* d, uint8_t i_update_airflow_only) { int16_t gradient, discharge, rpm = d->sens.inst_frq, l; int8_t f, fp1, lp1; discharge = (d->param.map_upper_pressure - d->sens.map); if (discharge < 0) discharge = 0; //map_upper_pressure - верхнее значение давления //map_lower_pressure - нижнее значение давления gradient = (d->param.map_upper_pressure - d->param.map_lower_pressure) / 16; //делим на количество узлов интерполяции по оси давления if (gradient < 1) gradient = 1; //исключаем деление на ноль и отрицательное значение если верхнее давление меньше нижнего l = (discharge / gradient); if (l >= (F_WRK_POINTS_F - 1)) lp1 = l = F_WRK_POINTS_F - 1; else lp1 = l + 1; //обновляем переменную расхода воздуха d->airflow = 16 - l; if (i_update_airflow_only) return 0; //выходим если вызвавший указал что мы должны обновить только расход воздуха //находим узлы интерполяции, вводим ограничение если обороты выходят за пределы for(f = 14; f >= 0; f--) if (rpm >= PGM_GET_WORD(&f_slots_ranges[f])) break; //рабочая карта работает на 600-х оборотах и выше if (f < 0) {f = 0; rpm = 600;} fp1 = f + 1; return bilinear_interpolation(rpm, discharge, _GB(&d->fn_dat->f_wrk[l][f]), _GB(&d->fn_dat->f_wrk[lp1][f]), _GB(&d->fn_dat->f_wrk[lp1][fp1]), _GB(&d->fn_dat->f_wrk[l][fp1]), PGM_GET_WORD(&f_slots_ranges[f]), (gradient * l), PGM_GET_WORD(&f_slots_length[f]), gradient); }
void BLI_bilinear_interpolation_char(const unsigned char *buffer, unsigned char *output, int width, int height, int components, float u, float v) { bilinear_interpolation(buffer, NULL, output, NULL, width, height, components, u, v); }
void BLI_bilinear_interpolation_fl(const float *buffer, float *output, int width, int height, int components, float u, float v) { bilinear_interpolation(NULL, buffer, NULL, output, width, height, components, u, v); }
/* only supports RGBA nodes now */ static void node_composit_exec_rotate(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out) { if(out[0]->hasoutput==0) return; if(in[0]->data) { CompBuf *cbuf= typecheck_compbuf(in[0]->data, CB_RGBA); CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); /* note, this returns zero'd image */ float rad, u, v, s, c, centx, centy, miny, maxy, minx, maxx; int x, y, yo, xo; ImBuf *ibuf, *obuf; rad= (M_PI*in[1]->vec[0])/180.0f; s= sin(rad); c= cos(rad); centx= cbuf->x/2; centy= cbuf->y/2; minx= -centx; maxx= -centx + (float)cbuf->x; miny= -centy; maxy= -centy + (float)cbuf->y; ibuf=IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0); obuf=IMB_allocImBuf(stackbuf->x, stackbuf->y, 32, 0); if(ibuf && obuf){ ibuf->rect_float=cbuf->rect; obuf->rect_float=stackbuf->rect; for(y=miny; y<maxy; y++) { yo= y+(int)centy; for(x=minx; x<maxx;x++) { u=c*x + y*s + centx; v=-s*x + c*y + centy; xo= x+(int)centx; switch(node->custom1) { case 0: neareast_interpolation(ibuf, obuf, u, v, xo, yo); break ; case 1: bilinear_interpolation(ibuf, obuf, u, v, xo, yo); break; case 2: bicubic_interpolation(ibuf, obuf, u, v, xo, yo); break; } } } /* rotate offset vector too, but why negative rad, ehh?? Has to be replaced with [3][3] matrix once (ton) */ s= sin(-rad); c= cos(-rad); centx= (float)cbuf->xof; centy= (float)cbuf->yof; stackbuf->xof= (int)( c*centx + s*centy); stackbuf->yof= (int)(-s*centx + c*centy); IMB_freeImBuf(ibuf); IMB_freeImBuf(obuf); } /* pass on output and free */ out[0]->data= stackbuf; if(cbuf!=in[0]->data) { free_compbuf(cbuf); } } }
CompBuf* node_composit_transform(CompBuf *cbuf, float x, float y, float angle, float scale, int filter_type) { CompBuf *stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); ImBuf *ibuf, *obuf; float mat[4][4], lmat[4][4], rmat[4][4], smat[4][4], cmat[4][4], icmat[4][4]; float svec[3]= {scale, scale, scale}, loc[2]= {x, y}; unit_m4(rmat); unit_m4(lmat); unit_m4(smat); unit_m4(cmat); /* image center as rotation center */ cmat[3][0]= (float)cbuf->x/2.0f; cmat[3][1]= (float)cbuf->y/2.0f; invert_m4_m4(icmat, cmat); size_to_mat4(smat, svec); /* scale matrix */ add_v2_v2(lmat[3], loc); /* tranlation matrix */ rotate_m4(rmat, 'Z', angle); /* rotation matrix */ /* compose transformation matrix */ mul_serie_m4(mat, lmat, cmat, rmat, smat, icmat, NULL, NULL, NULL); invert_m4(mat); ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0); obuf= IMB_allocImBuf(stackbuf->x, stackbuf->y, 32, 0); if (ibuf && obuf) { int i, j; ibuf->rect_float= cbuf->rect; obuf->rect_float= stackbuf->rect; for (j=0; j<cbuf->y; j++) { for (i=0; i<cbuf->x;i++) { float vec[3]= {i, j, 0}; mul_v3_m4v3(vec, mat, vec); switch(filter_type) { case 0: neareast_interpolation(ibuf, obuf, vec[0], vec[1], i, j); break; case 1: bilinear_interpolation(ibuf, obuf, vec[0], vec[1], i, j); break; case 2: bicubic_interpolation(ibuf, obuf, vec[0], vec[1], i, j); break; } } } IMB_freeImBuf(ibuf); IMB_freeImBuf(obuf); } /* pass on output and free */ return stackbuf; }
static force_inline void bits_image_fetch_bilinear_affine (pixman_image_t * image, int offset, int line, int width, uint32_t * buffer, const uint32_t * mask, convert_pixel_t convert_pixel, pixman_format_code_t format, pixman_repeat_t repeat_mode) { pixman_fixed_t x, y; pixman_fixed_t ux, uy; pixman_vector_t v; bits_image_t *bits = &image->bits; int i; /* reference point is the center of the pixel */ v.vector[0] = pixman_int_to_fixed (offset) + pixman_fixed_1 / 2; v.vector[1] = pixman_int_to_fixed (line) + pixman_fixed_1 / 2; v.vector[2] = pixman_fixed_1; if (!pixman_transform_point_3d (image->common.transform, &v)) return; ux = image->common.transform->matrix[0][0]; uy = image->common.transform->matrix[1][0]; x = v.vector[0]; y = v.vector[1]; for (i = 0; i < width; ++i) { int x1, y1, x2, y2; uint32_t tl, tr, bl, br; int32_t distx, disty; int width = image->bits.width; int height = image->bits.height; const uint8_t *row1; const uint8_t *row2; if (mask && !mask[i]) goto next; x1 = x - pixman_fixed_1 / 2; y1 = y - pixman_fixed_1 / 2; distx = pixman_fixed_to_bilinear_weight (x1); disty = pixman_fixed_to_bilinear_weight (y1); y1 = pixman_fixed_to_int (y1); y2 = y1 + 1; x1 = pixman_fixed_to_int (x1); x2 = x1 + 1; if (repeat_mode != PIXMAN_REPEAT_NONE) { uint32_t mask; mask = PIXMAN_FORMAT_A (format)? 0 : 0xff000000; repeat (repeat_mode, &x1, width); repeat (repeat_mode, &y1, height); repeat (repeat_mode, &x2, width); repeat (repeat_mode, &y2, height); row1 = (uint8_t *)bits->bits + bits->rowstride * 4 * y1; row2 = (uint8_t *)bits->bits + bits->rowstride * 4 * y2; tl = convert_pixel (row1, x1) | mask; tr = convert_pixel (row1, x2) | mask; bl = convert_pixel (row2, x1) | mask; br = convert_pixel (row2, x2) | mask; } else { uint32_t mask1, mask2; int bpp; /* Note: PIXMAN_FORMAT_BPP() returns an unsigned value, * which means if you use it in expressions, those * expressions become unsigned themselves. Since * the variables below can be negative in some cases, * that will lead to crashes on 64 bit architectures. * * So this line makes sure bpp is signed */ bpp = PIXMAN_FORMAT_BPP (format); if (x1 >= width || x2 < 0 || y1 >= height || y2 < 0) { buffer[i] = 0; goto next; } if (y2 == 0) { row1 = zero; mask1 = 0; } else { row1 = (uint8_t *)bits->bits + bits->rowstride * 4 * y1; row1 += bpp / 8 * x1; mask1 = PIXMAN_FORMAT_A (format)? 0 : 0xff000000; } if (y1 == height - 1) { row2 = zero; mask2 = 0; } else { row2 = (uint8_t *)bits->bits + bits->rowstride * 4 * y2; row2 += bpp / 8 * x1; mask2 = PIXMAN_FORMAT_A (format)? 0 : 0xff000000; } if (x2 == 0) { tl = 0; bl = 0; } else { tl = convert_pixel (row1, 0) | mask1; bl = convert_pixel (row2, 0) | mask2; } if (x1 == width - 1) { tr = 0; br = 0; } else { tr = convert_pixel (row1, 1) | mask1; br = convert_pixel (row2, 1) | mask2; } } buffer[i] = bilinear_interpolation ( tl, tr, bl, br, distx, disty); next: x += ux; y += uy; } }
static uint32_t * bits_image_fetch_bilinear_no_repeat_8888 (pixman_iter_t *iter, const uint32_t *mask) { pixman_image_t * ima = iter->image; int offset = iter->x; int line = iter->y++; int width = iter->width; uint32_t * buffer = iter->buffer; bits_image_t *bits = &ima->bits; pixman_fixed_t x_top, x_bottom, x; pixman_fixed_t ux_top, ux_bottom, ux; pixman_vector_t v; uint32_t top_mask, bottom_mask; uint32_t *top_row; uint32_t *bottom_row; uint32_t *end; uint32_t zero[2] = { 0, 0 }; uint32_t one = 1; int y, y1, y2; int disty; int mask_inc; int w; /* reference point is the center of the pixel */ v.vector[0] = pixman_int_to_fixed (offset) + pixman_fixed_1 / 2; v.vector[1] = pixman_int_to_fixed (line) + pixman_fixed_1 / 2; v.vector[2] = pixman_fixed_1; if (!pixman_transform_point_3d (bits->common.transform, &v)) return iter->buffer; ux = ux_top = ux_bottom = bits->common.transform->matrix[0][0]; x = x_top = x_bottom = v.vector[0] - pixman_fixed_1/2; y = v.vector[1] - pixman_fixed_1/2; disty = pixman_fixed_to_bilinear_weight (y); /* Load the pointers to the first and second lines from the source * image that bilinear code must read. * * The main trick in this code is about the check if any line are * outside of the image; * * When I realize that a line (any one) is outside, I change * the pointer to a dummy area with zeros. Once I change this, I * must be sure the pointer will not change, so I set the * variables to each pointer increments inside the loop. */ y1 = pixman_fixed_to_int (y); y2 = y1 + 1; if (y1 < 0 || y1 >= bits->height) { top_row = zero; x_top = 0; ux_top = 0; } else { top_row = bits->bits + y1 * bits->rowstride; x_top = x; ux_top = ux; } if (y2 < 0 || y2 >= bits->height) { bottom_row = zero; x_bottom = 0; ux_bottom = 0; } else { bottom_row = bits->bits + y2 * bits->rowstride; x_bottom = x; ux_bottom = ux; } /* Instead of checking whether the operation uses the mast in * each loop iteration, verify this only once and prepare the * variables to make the code smaller inside the loop. */ if (!mask) { mask_inc = 0; mask = &one; } else { /* If have a mask, prepare the variables to check it */ mask_inc = 1; } /* If both are zero, then the whole thing is zero */ if (top_row == zero && bottom_row == zero) { memset (buffer, 0, width * sizeof (uint32_t)); return iter->buffer; } else if (bits->format == PIXMAN_x8r8g8b8) { if (top_row == zero) { top_mask = 0; bottom_mask = 0xff000000; } else if (bottom_row == zero) { top_mask = 0xff000000; bottom_mask = 0; } else { top_mask = 0xff000000; bottom_mask = 0xff000000; } } else { top_mask = 0; bottom_mask = 0; } end = buffer + width; /* Zero fill to the left of the image */ while (buffer < end && x < pixman_fixed_minus_1) { *buffer++ = 0; x += ux; x_top += ux_top; x_bottom += ux_bottom; mask += mask_inc; } /* Left edge */ while (buffer < end && x < 0) { uint32_t tr, br; int32_t distx; tr = top_row[pixman_fixed_to_int (x_top) + 1] | top_mask; br = bottom_row[pixman_fixed_to_int (x_bottom) + 1] | bottom_mask; distx = pixman_fixed_to_bilinear_weight (x); *buffer++ = bilinear_interpolation (0, tr, 0, br, distx, disty); x += ux; x_top += ux_top; x_bottom += ux_bottom; mask += mask_inc; } /* Main part */ w = pixman_int_to_fixed (bits->width - 1); while (buffer < end && x < w) { if (*mask) { uint32_t tl, tr, bl, br; int32_t distx; tl = top_row [pixman_fixed_to_int (x_top)] | top_mask; tr = top_row [pixman_fixed_to_int (x_top) + 1] | top_mask; bl = bottom_row [pixman_fixed_to_int (x_bottom)] | bottom_mask; br = bottom_row [pixman_fixed_to_int (x_bottom) + 1] | bottom_mask; distx = pixman_fixed_to_bilinear_weight (x); *buffer = bilinear_interpolation (tl, tr, bl, br, distx, disty); } buffer++; x += ux; x_top += ux_top; x_bottom += ux_bottom; mask += mask_inc; } /* Right Edge */ w = pixman_int_to_fixed (bits->width); while (buffer < end && x < w) { if (*mask) { uint32_t tl, bl; int32_t distx; tl = top_row [pixman_fixed_to_int (x_top)] | top_mask; bl = bottom_row [pixman_fixed_to_int (x_bottom)] | bottom_mask; distx = pixman_fixed_to_bilinear_weight (x); *buffer = bilinear_interpolation (tl, 0, bl, 0, distx, disty); } buffer++; x += ux; x_top += ux_top; x_bottom += ux_bottom; mask += mask_inc; } /* Zero fill to the left of the image */ while (buffer < end) *buffer++ = 0; return iter->buffer; }
void Glyphs::draw_glyphs() { double wn = (double)(winWidth*0.95) / (double)(DIM + 1); // Grid cell width double hn = (double)(winHeight) / (double)(DIM + 1); // Grid cell heigh if (vector_field == VECTOR_VELOC) { vf_x = vx; vf_y = vy; } else if (vector_field == VECTOR_FORCE) { vf_x = fx; vf_y = fy; } switch (scalar_field) { case SCALAR_RHO: case SCALAR_VELOC_MAG: case SCALAR_FORCE_MAG: glEnable(GL_TEXTURE_1D); break; } if (glyph_type == GLYPH_LINE) { glBegin(GL_LINES); for (double i = 0; i < DIM+0.01; i += (DIM/(double)x_axis_samples)) { for (double j = 0; j < DIM+0.01; j += (DIM/(double)y_axis_samples)) { double vec [2] = {0,0}; srand (i*DIM +j); double ni = abs(fmod(i + jitter*3*sin(rand()), DIM-1)); double nj = abs(fmod(j + jitter*3*cos(rand()), DIM-1)); color_glyph(round(ni), round(nj)); // nearest neighbour policy for coloring bilinear_interpolation(vec, vf_x, vf_y, ni, nj); // interpolation for vetor values glVertex2f(wn + ni * wn, hn + nj * hn); glVertex2f((wn + ni * wn) + vec_scale * vec[0], (hn + nj * hn) + vec_scale * vec[1]); } } glEnd(); } else if (glyph_type == GLYPH_NEEDLE) { glBegin(GL_TRIANGLES); for (double i = 0; i < DIM+0.01; i += (DIM/(double)x_axis_samples)) for (double j = 0; j < DIM+0.01; j += (DIM/(double)y_axis_samples)) { double vec [2] = {0,0}; srand (i*DIM +j); double ni = abs(fmod(i + jitter*3*sin(rand()), DIM-1)); double nj = abs(fmod(j + jitter*3*cos(rand()), DIM-1)); color_glyph(round(ni), round(nj)); // nearest neighbour polocy for coloring bilinear_interpolation(vec, vf_x, vf_y, ni, nj); // interpolation for vetor values glVertex2f(wn + ni*wn, hn + nj*hn); glVertex2f((wn + ni*wn) + vec_scale * vec[0], (hn + nj*hn) + vec_scale * vec[1]); double angle_rad = 0.0; glVertex2f((wn + ni*wn) + 0.05*vec_scale*(cos(angle_rad)*vec[1] -sin(angle_rad)*vec[0]), (hn + nj*hn) + 0.05*vec_scale*(sin(angle_rad)*vec[1] -cos(angle_rad)*vec[0])); glVertex2f(wn + ni*wn, hn + nj*hn); glVertex2f((wn + ni*wn) + vec_scale * vec[0], (hn + nj*hn) + vec_scale * vec[1]); glVertex2f((wn + ni*wn) + (-0.05)*vec_scale*(cos(angle_rad)*vec[1] -sin(angle_rad)*vec[0]), (hn + nj*hn) + (-0.05)*vec_scale*(sin(angle_rad)*vec[1] -cos(angle_rad)*vec[0])); } glEnd(); } else if (glyph_type == GLYPH_ARROW) { glBegin(GL_LINES); for (double i = 0; i < DIM+0.01; i += (DIM/(double)x_axis_samples)) { for (double j = 0; j < DIM+0.01; j += (DIM/(double)y_axis_samples)) { double vec [2] = {0,0}; srand (i*DIM +j); double ni = abs(fmod(i + jitter*3*sin(rand()), DIM-1)); double nj = abs(fmod(j + jitter*3*cos(rand()), DIM-1)); color_glyph(round(ni), round(nj)); // nearest neighbour polocy for coloring bilinear_interpolation(vec, vf_x, vf_y, ni, nj); // interpolation for vetor values glVertex2f(wn + ni * wn, hn + nj * hn); glVertex2f((wn + ni * wn) + vec_scale * vec[0], (hn + nj * hn) + vec_scale * vec[1]); } } glEnd(); glBegin(GL_TRIANGLES); for (double i = 0; i < DIM+0.01; i += (DIM/(double)x_axis_samples)) for (double j = 0; j < DIM+0.01; j += (DIM/(double)y_axis_samples)) { double vec [2] = {0,0}; srand (i*DIM +j); double ni = abs(fmod(i + jitter*3*sin(rand()), DIM-1)); double nj = abs(fmod(j + jitter*3*cos(rand()), DIM-1)); color_glyph(round(ni), round(nj)); // nearest neighbour polocy for coloring bilinear_interpolation(vec, vf_x, vf_y, ni, nj); // interpolation for vetor values glVertex2f((wn + ni*wn) + vec_scale * vec[0], (hn + nj*hn) + vec_scale * vec[1]); double angle_rad = 0.0; glVertex2f((wn + ni*wn) + 0.8*vec_scale*vec[0] + 0.05*vec_scale*(cos(angle_rad)*vec[1] -sin(angle_rad)*vec[0]), (hn + nj*hn) + 0.8*vec_scale*vec[1] + 0.05*vec_scale*(sin(angle_rad)*vec[1] -cos(angle_rad)*vec[0])); glVertex2f((wn + ni*wn) + 0.8*vec_scale*vec[0] + (-0.05)*vec_scale*(cos(angle_rad)*vec[1] -sin(angle_rad)*vec[0]), (hn + nj*hn) + 0.8*vec_scale*vec[1] + (-0.05)*vec_scale*(sin(angle_rad)*vec[1] -cos(angle_rad)*vec[0])); } glEnd(); } switch (scalar_field) { case SCALAR_RHO: case SCALAR_VELOC_MAG: case SCALAR_FORCE_MAG: glDisable(GL_TEXTURE_1D); break; } }
//------------------------------------------------------------------------------------------------------------------------------------- //The Kernel void get_movementData(FILE* outTxt,float* udata,float* vdata,float* dirData, float* precipData,float* pressureData,float* u10data,float* v10data) { fprintf(outTxt,"pos_row \t pos_col \t u_val \t\t v_val \t\t dir_u \t\t dir_v \t\t u_air \t\t v_air \t\t bird_GroundSpeed \t wind_Speed \t\t distance \t l \t skip\n"); float distance,prev_row,prev_col,bird_AirSpeed,wind_Speed; distance = 0; bird_AirSpeed = 0; wind_Speed = 0; prev_row = STARTING_ROW; prev_col = STARTING_COL; float pos_row,pos_col; //pos_row = LONG_SIZE - STARTING_ROW; pos_row = STARTING_ROW; pos_col = STARTING_COL; fprintf(outTxt,"%f,%f\n",pos_row,pos_col); int k; //long l_old; float pressure_sum,pressure_MultSum,last_pressure,slope; pressure_MultSum = 0; pressure_sum = 0; slope = 1; last_pressure = 1011; //l = 18; //l_old = 18; float profit_value,dirAngleFromFile,dirAngle,actualAngle; float dir_v,dir_u; //long skip_size = (SKIP_TIMESTEPS * LONG_SIZE * LAT_SIZE) - 1; long skip_size = 0; float u_val,v_val,precip_val,v_ten,u_ten; int skip; skip=0; //skip_size = 120174 //fprintf(outTxt,"%f,%f\n",pos_row,pos_col); printf("i \t l \t k \t precipData \t profit_value \t pos_row \t pos_col \t (v_val,u_val)\n"); //i = skip_size +pos_row * LAT_SIZE + pos_col; //while( i <= (TIMESTEPS-1) * LAT_SIZE * LONG_SIZE ) { dir_v = 0; dir_u = 0; dirAngle = 0; dirAngleFromFile = 0; actualAngle = 0; u_val = 0; v_val = 0; precip_val = 0; v_ten = 0; u_ten = 0; pressure_sum = 0; pressure_MultSum = 0; //If current pressure is greater than pressure in the previous day //if(pressureData[i] - old_pressure > 0) { printf("Main loop[k]\n"); u_ten = bilinear_interpolation(pos_col,pos_row,u10data,l,1); v_ten = bilinear_interpolation(pos_col,pos_row,v10data,l,1); //The direction angle is chosen only once, before the flight. dirAngleFromFile = dirData[(int)(rintf(pos_row) * LAT_SIZE + rintf(pos_col))]; dirAngle = WrappedNormal(dirAngleFromFile,STD_BIRDANGLE); actualAngle = dirAngle; if(dirAngle <= 90){ dirAngle = 90 - dirAngle; dir_v = DESIRED_SPEED * sin(dirAngle * (PI/180)); dir_u = DESIRED_SPEED * cos(dirAngle * (PI/180)); } else if((dirAngle > 90) && (dirAngle <= 180)){ dirAngle -= 90; dir_v = DESIRED_SPEED * sin(dirAngle * (PI/180)) * -1; dir_u = DESIRED_SPEED * cos(dirAngle * (PI/180)); } else if((dirAngle > 180) && (dirAngle <= 270)) { dirAngle = 270 - dirAngle; dir_v = DESIRED_SPEED * sin(dirAngle * (PI/180)) * -1; dir_u = DESIRED_SPEED * cos(dirAngle * (PI/180)) * -1; } else if((dirAngle > 270) && (dirAngle <= 360)){ dirAngle -= 270; dir_v = DESIRED_SPEED * sin(dirAngle * (PI/180)); dir_u = DESIRED_SPEED * cos(dirAngle * (PI/180)) * -1; } printf("10 profit value::%f\n",getProfitValue(u_ten,v_ten,actualAngle,dir_u,dir_v)); printf("pressure value::%f,slope value::%f\n",last_pressure,slope); dirAngleFromFile = dirData[(int)(rintf(pos_row) * LAT_SIZE + rintf(pos_col))]; dirAngle = WrappedNormal(dirAngleFromFile,STD_BIRDANGLE); printf("\n First DirectionAngle = %f,AngleFromFile = %f\n",dirAngle,dirAngleFromFile); actualAngle = dirAngle; //Relation between last_pressure and slope is an OR if((getProfitValue(u_ten,v_ten,actualAngle,dir_u,dir_v) >= MIN_PROFIT) && ((last_pressure>=1009)||(slope >-1))){ //dirAngleFromFile = dirData[(int)(rintf(pos_row) * LAT_SIZE + rintf(pos_col))]; //dirAngle = WrappedNormal(dirAngleFromFile,STD_BIRDANGLE); printf("\n\nl value check: %ld\n\n",l); for(k=0;k<6;k++,l++ ) { i = skip_size + l * LAT_SIZE * LONG_SIZE + pos_row * LAT_SIZE + pos_col; skip = 0; //dirAngle is with respect to North or the positive y-axis //It is the genetic direction of the birds //actualAngle = dirAngle; //dirAngleFromFile = dirData[(int)(rintf(pos_row) * LAT_SIZE + rintf(pos_col))]; //dirAngle = WrappedNormal(dirAngleFromFile,STD_BIRDANGLE); dirAngle = actualAngle; printf("\n DirectionAngle = %f,AngleFromFile = %f\n",dirAngle,dirAngleFromFile); //The grid is upside down; y increases from top to bottom while x increases from left to right //dir_v and dir_u are the x and y components of the wind (v=y,u=x) if(dirAngle <= 90){//Checked OK dirAngle = 90 - dirAngle; dir_v = DESIRED_SPEED * sin(dirAngle * (PI/180)); dir_u = DESIRED_SPEED * cos(dirAngle * (PI/180)); } else if((dirAngle > 90) && (dirAngle <= 180)){//Checked OK dirAngle -= 90; dir_v = DESIRED_SPEED * sin(dirAngle * (PI/180)) * -1; dir_u = DESIRED_SPEED * cos(dirAngle * (PI/180)); } else if((dirAngle > 180) && (dirAngle <= 270)) { dirAngle = 270 - dirAngle; dir_v = DESIRED_SPEED * sin(dirAngle * (PI/180)) * -1; dir_u = DESIRED_SPEED * cos(dirAngle * (PI/180)) * -1; } else if((dirAngle > 270) && (dirAngle <= 360)){ dirAngle -= 270; dir_v = DESIRED_SPEED * sin(dirAngle * (PI/180)); dir_u = DESIRED_SPEED * cos(dirAngle * (PI/180)) * -1; } //Bilinear interpolation for u and v data u_val = bilinear_interpolation(pos_col,pos_row,udata,l,1); v_val = bilinear_interpolation(pos_col,pos_row,vdata,l,1); printf("(u_val,v_val)::(%f,%f)\n",u_val,v_val); profit_value = getProfitValue(u_val,v_val,actualAngle,dir_u,dir_v); precip_val = bilinear_interpolation(pos_col,pos_row,precipData,l,1); //Adding precip value //if ((profit_value >= MIN_PROFIT) && (precipData[i] < 2) ) { if ((profit_value >= MIN_PROFIT) && (precip_val < MAX_PRECIP) ) { //Positon is in a 10 km grid. This means for 1m/s, the //position change in 1 hour is 3.6/10 = 0.36units in the grid // pos_row = pos_row + (v_val + dir_v)*0.36; pos_row = pos_row + (v_val + dir_v)*0.36*-1; pos_col = pos_col + (u_val + dir_u)*0.36; //float tmp; //tmp = sqrtf((v_val+dir_v)*(v_val+dir_v) + (u_val+dir_u)*(u_val+dir_u)); //printf("\nTailComponent::%f,Speed::%f,dir_v::%f,dir_u::%f\n",tailComponent,tmp,dir_v,dir_u); printf("%ld \t %ld \t %d \t %f \t %f \t %f \t %f \t (%f,%f)\n",i,l,k,precip_val,profit_value,pos_row,pos_col,v_val,u_val); skip = 0; } else { //l increases but it goes back to the original starting hour for the bird; i.e 7pm // 6-k because l++ will not be done in the end because it breaks from the loop l += (6-k); skip = 1; printf("Skipped Wind (%f,%f) @ (%f,%f)w_profit = %f,precip=%f,And l = %ld\n",u_val,v_val,pos_row,pos_col,profit_value,precip_val,l); break; } //fprintf(outTxt,"%f,%f\n",pos_row,pos_col); distance = sqrt((pos_row - prev_row)*(pos_row - prev_row) + (pos_col - prev_col)*(pos_col - prev_col)); prev_col = pos_col; prev_row = pos_row; wind_Speed = sqrt(u_val * u_val + v_val * v_val); bird_AirSpeed = sqrt((u_val+dir_u)*(u_val+dir_u) +(v_val+dir_v)*(v_val+dir_v)); //Distance is in absolute value (kilometers rather than in units of grid points) fprintf(outTxt,"%f \t %f \t %f \t %f \t %f \t %f \t %f \t %f \t %f \t %f \t %f \t%ld\t%d\n",pos_row,pos_col,u_val,v_val,dir_u,dir_v,u_val+dir_u,v_val+dir_v,bird_AirSpeed,wind_Speed,distance*10,l,skip); } } //v10 and u10 profit values were too low if(l_old == l){ printf("u10 v10 profit value too low!\n"); l+=6; } //Every day has 6 time steps and this line skips the total desired number of days l += STOPOVER_DAYS * 6; l_old = l - REGRESSION_HRS; printf("check l %ld\n",l); //Taking the pressure from 6 hours earlier of the location where the bird landed for(k=1; (l_old < l) && (k<=REGRESSION_HRS); l_old++,k++){ //printf("\nPressure Sum Interpolation\n"); pressure_sum += bilinear_interpolation(pos_col,pos_row,pressureData,l_old,1); //pressure_sum += pressureData[skip_size + l_old * LAT_SIZE * LONG_SIZE + pos_row * LAT_SIZE + pos_col]; pressure_MultSum += k * bilinear_interpolation(pos_col,pos_row,pressureData,l_old,1); //printf("%f\n",pressureData[skip_size + l_old * LAT_SIZE * LONG_SIZE + pos_row * LAT_SIZE + pos_col]); if(k == REGRESSION_HRS) { last_pressure = bilinear_interpolation(pos_col,pos_row,pressureData,l_old,1); } } slope = ((REGRESSION_HRS * pressure_MultSum) - (pressure_sum * HRS_SUM))/(DENOM_SLOPE); if(slope <= -1){ if(last_pressure < 1009) { printf("Storm!!\n"); } else if( (last_pressure >= 1009) && (last_pressure < 1020) ){ printf("Precipitation is very likely````` \n"); } else if(last_pressure >1020) { printf("Cloudy & Warm\n"); } } //printf("\t Slope is:: %f\n",slope); //old_pressure = pressureData[i_old]; //Since the data now does not contain all 24 hours but just 7pm to 1am //l += 6; i = skip_size + l * LAT_SIZE * LONG_SIZE + pos_row * LAT_SIZE + pos_col; //l_old = l; //printf("Running\n"); printf("-----------------------------------------------------------------------------------------------------------------------\n"); //} //fprintf(outTxt,"----------------------------------------------------------\n"); }