/* This draws a triangle with points (x0, y0), (x1, y1), (x2, y2) in the colour * defined by (r, g, b). It uses the computation of barycentric coordinates * to check whether each point is inside the triangle. It only checks points * that are inside the bounding box of the triangle. */ void draw_triangle(float x0, float y0, float x1, float y1, float x2, float y2, byte r, byte g, byte b) { /* Finds the bounding box of the triangle */ int xmin = (int) floor(min_3(x0, x1, x2)); int xmax = (int) ceil(max_3(x0, x1, x2)); int ymin = (int) floor(min_3(y0, y1, y2)); int ymax = (int) ceil(max_3(y0, y1, y2)); float alpha, beta, gamma; /* Equations for the alpha, beta and gamma points in respect to the lines * of the triangle */ float alpha_function = f(x1, x2, y1, y2, x0, y0); float beta_function = f(x2, x0, y2, y0, x1, y1); float gamma_function = f(x0, x1, y0, y1, x2, y2); /* Equations for the offscreen points in respect to the * lines of the triangle */ float alpha_function_offscreen = f(x1, x2, y1, y2, OFFSCREEN[0], OFFSCREEN[1]); float beta_function_offscreen = f(x2, x0, y2, y0, OFFSCREEN[0], OFFSCREEN[1]); float gamma_function_offscreen = f(x0, x1, y0, y1, OFFSCREEN[0], OFFSCREEN[1]); for(int y = ymin; y <= ymax; y++) { for(int x = xmin; x <= xmax; x++) { /* Computes the new alpha, beta and gamma values and divides by * The function value of the corresponding lines to get a value * between 0 and 1 when it is inside the triangle, below zero * when not. */ alpha = f(x1, x2, y1, y2, x, y) / alpha_function; beta = f(x2, x0, y2, y0, x, y) / beta_function; gamma = f(x0, x1, y0, y1, x, y) / gamma_function; if(alpha >= 0 && beta >= 0 && gamma >= 0) { /* Only draw if it's inside the triangle, or on the triangle edge * if the triangle is on the side of the arbitrarily chosen * offscreen point. This prevents shared edges being drawn twice */ if((alpha > 0 || alpha_function * alpha_function_offscreen > 0) && (beta > 0 || beta_function * beta_function_offscreen > 0) && (gamma > 0 || gamma_function * gamma_function_offscreen > 0)) { PutPixel(x, y, r, g, b); } } } } }
void diff_c2 ( unsigned char *src, unsigned char *src_2, unsigned char *dst, int m, // columnas int n, // filas int src_row_size, int src_2_row_size, int dst_row_size ) { unsigned char (*src_matrix)[src_row_size] = (unsigned char (*)[src_row_size]) src; unsigned char (*src_2_matrix)[src_2_row_size] = (unsigned char (*)[src_2_row_size]) src_2; unsigned char (*dst_matrix)[dst_row_size] = (unsigned char (*)[dst_row_size]) dst; for (int i = 0; i < n; i++) { for (int j = 0; j < 4 * m; j = j + 4) { unsigned char b_diff = abs_diff(src_matrix[i][j], src_2_matrix[i][j]); unsigned char g_diff = abs_diff(src_matrix[i][j+1], src_2_matrix[i][j+1]); unsigned char r_diff = abs_diff(src_matrix[i][j+2], src_2_matrix[i][j+2]); unsigned char diff = max_3(b_diff, g_diff, r_diff); dst_matrix[i][j] = diff; dst_matrix[i][j+1] = diff; dst_matrix[i][j+2] = diff; dst_matrix[i][j+3] = 255; } } }
/* An optimised version of the draw_triangle function. It uses an incremental * approach to computing the barycentric coordinates, only computing a start * value and then incrementing that value every step along the way, using * f_incr. It also gets rid of all floating point operations, favouring * integers instead. This means that the alpha, beta and gamma values are not * between 0 and 1 when a point is within the triangle, but instead it is just * greater than or equal to 0, so it can still be tested equally well. * If you were to use it for interpolation (eg. of colours) however, you would * run into problems. */ void draw_triangle_optimized(float x0, float y0, float x1, float y1, float x2, float y2, byte r, byte g, byte b) { /* Finds the bounding box of the triangle */ int xmin = (int) floor(min_3(x0, x1, x2)); int xmax = (int) ceil(max_3(x0, x1, x2)); int ymin = (int) floor(min_3(y0, y1, y2)); int ymax = (int) ceil(max_3(y0, y1, y2)); /* Finds the sign of all points in respect to the lines */ int alpha_sign = 1; int beta_sign = 1; int gamma_sign = 1; if(signbit(f(x1, x2, y1, y2, x0, y0))) alpha_sign = -1; if(signbit(f(x2, x0, y2, y0, x1, y1))) beta_sign = -1; if(signbit(f(x0, x1, y0, y1, x2, y2))) gamma_sign = -1; /* Finds the sign of the offscreen point in respect to the lines */ int alpha_sign_offscreen = 1; int beta_sign_offscreen = 1; int gamma_sign_offscreen = 1; if(signbit(f(x1, x2, y1, y2, OFFSCREEN[0], OFFSCREEN[1]))) alpha_sign_offscreen = -1; if(signbit(f(x2, x0, y2, y0, OFFSCREEN[0], OFFSCREEN[1]))) beta_sign_offscreen = -1; if(signbit(f(x0, x1, y0, y1, OFFSCREEN[0], OFFSCREEN[1]))) gamma_sign_offscreen = -1; /* Computes the starting values for alph, beta and gamma, for the point * (xmin - 1, ymin - 1) so that it will be incremented to the correct * starting value in the first loop cycle. */ int alpha = f(x1, x2, y1, y2, xmin - 1, ymin - 1) * alpha_sign; int beta = f(x2, x0, y2, y0, xmin - 1, ymin - 1) * beta_sign; int gamma = f(x0, x1, y0, y1, xmin - 1, ymin - 1) * gamma_sign; for(int y = ymin; y <= ymax; y++) { alpha += f_incr(x1, x2, 0, 0, alpha_sign); beta += f_incr(x2, x0, 0, 0, beta_sign); gamma += f_incr(x0, x1, 0, 0, gamma_sign); /* Saves the current values to jump back to after the * x loop is finished */ int prev_alpha = alpha; int prev_beta = beta; int prev_gamma = gamma; for(int x = xmin; x <= xmax; x++) { alpha += f_incr(0, 0, y1, y2, alpha_sign); beta += f_incr(0, 0, y2, y0, beta_sign); gamma += f_incr(0, 0, y0, y1, gamma_sign); if(alpha >= 0 && beta >= 0 && gamma >= 0) { /* Only draw if it's inside the triangle, or on the triangle edge * if the triangle is on the side of the arbitrarily chosen * offscreen point. This prevents shared edges being drawn twice */ if((alpha > 0 || alpha_sign * alpha_sign_offscreen > 0) && (beta > 0 || beta_sign * beta_sign_offscreen > 0) && (gamma > 0 || gamma_sign * gamma_sign_offscreen > 0)) { PutPixel(x, y, r, g, b); } } } alpha = prev_alpha; beta = prev_beta; gamma = prev_gamma; } }
INT dyNUMA_Utilities_read_mapping_predictor_training_elements() { CHAR line[MAX_CHAR_PERLINE]; INT fldcnt=0; INT index=0; INT elmt_index=0; CHAR arr[MAXFLDS][MAXFLDSIZE]; INT thread_pos[MAX_NUM_THREAD]; INT i,j,tid,record_id; FILE *training_file = fopen ( env.TRAINING_FILE, "r" ); if(training_file == NULL) { #if DEBUG_GENERAL fprintf(stderr,"Error open training file. %s\n",env.TRAINING_FILE); #endif exit(1); } /* read a line */ while ( fgets ( line, sizeof line, training_file ) != NULL ) { parse(line,",",arr,&fldcnt); /* save to elements */ elmt_index=1; for(j=0;j<env.NUM_REGIONS;j++) { glb.te[index+j].lid=0; glb.te[index+j].rid=j; } for(i=0;i<8;i++) { for(j=0;j<env.NUM_REGIONS;j++,elmt_index++) { glb.te[index+j].mode=arr[0][0]; glb.te[index+j].target[i]=atof(arr[elmt_index]); //fprintf(stdout,"Run %d Region %d Target %f\n",i,j,glb.te[index+j].target[i]); } } //thread pos for(tid=0;tid<env.NUM_THREADS;tid++,elmt_index++) { thread_pos[tid]=atoi(arr[elmt_index]); } for(j=0;j<env.NUM_REGIONS;j++) { //fprintf(stdout,"Region %d Thread( ",j); for(tid=0;tid<env.NUM_THREADS;tid++) { glb.te[index+j].thread_pos[tid]=thread_pos[tid]; //fprintf(stdout," %d,",glb.te[index+j].thread_pos[tid]); } //fprintf(stdout,")\n"); } for(j=0;j<env.NUM_REGIONS;j++) { //fprintf(stdout,"Region %d Input( ",j); for(tid=0;tid<env.NUM_THREADS;tid++) { for(record_id=0;record_id<env.NUM_EXE_SIGNATURE;record_id++,elmt_index++) { glb.te[index+j].input[tid][record_id]=atof(arr[elmt_index]); } //fprintf(stdout," (%f,%f),",glb.te[index+j].input[tid][0],glb.te[index+j].input[tid][1]); } //fprintf(stdout,")\n"); } index+=env.NUM_REGIONS; } glb.num_te = index; for(i=0;i<(index+1)/3;i++) { DOUBLE val_n= 0; DOUBLE val_c= 0; DOUBLE val_s= 0; for(j=0;j<8;j++) { val_n+=glb.te[i].target[j]; val_c+=glb.te[i+1080].target[j]; val_s+=glb.te[i+2160].target[j]; } glb.te[i].select[0] = max_3(val_n,val_c,val_s); } /* validate the trining element*/ #if DEBUG printf("index=%d\n",index); for(i=0;i<index;i++) { fprintf(stdout,"index=%d\n",i); fprintf(stdout,"lid=%d\n",glb.te[i].lid); fprintf(stdout,"rid=%d\n",glb.te[i].rid); fprintf(stdout,"mode=%c\n",glb.te[i].mode); fprintf(stdout,"select=%f\n",glb.te[i].select[0]); fprintf(stdout,"thraead pos( "); for(j=0;j<env.NUM_THREADS;j++) { fprintf(stdout,"%d,",glb.te[i].thread_pos[j]); } fprintf(stdout," )\n"); fprintf(stdout,"input ( "); for(j=0;j<env.NUM_THREADS;j++) { fprintf(stdout,"(%f,%f),",glb.te[i].input[j][0],glb.te[i].input[j][1]); } fprintf(stdout," )\n"); fprintf(stdout,"target ( "); for(j=0;j<8;j++) { fprintf(stdout,"%f,",glb.te[i].target[j]); } fprintf(stdout," )\n"); } #endif fclose ( training_file ); return SUCCESS; }