long int csv_compressed_read_vector_grid(char *filename_csv, long int ni, long int nj, long int nk, double *v0, double *v1, double *v2) { gzFile *fp; long int i, j, k, count; char text[1024]; char filename[1024]; double f, g, h; if(filename_csv == NULL || v0 == NULL || v1 == NULL || v2 == NULL) { printf("error: passed null arguments to csv_read_scalar_grid\n"); return -1; } strncpy(filename, filename_csv, strlen(filename_csv) + 1); strncat(filename, ".gz", 3); fp = gzopen(filename, "r"); if(fp == NULL) { printf("error: csv_compressed_read_vector_grid cannot open %s to read\n", filename); return -1; } if(!gzgets(fp, text, sizeof(text))) { if(!gzeof(fp)) { printf("error: gzgets in csv_compressed_read_vector_grid\n"); return(-1); } return 0; } count=0; while(!gzeof(fp)) { if(!gzgets(fp, text, sizeof(text))) { if(!gzeof(fp)) { printf("error: gzgets in csv_compressed_read_vector_grid\n"); return(-1); } break; } sscanf(text, "%ld%*c %ld%*c %ld%*c %lf%*c %lf%*c %lf%*c", &i, &j, &k, &f, &g, &h); if(i>ni || j>nj || k>nk) { printf("error: data out of bounds in csv_compressed_read_vector_grid\n"); break; } v0[CELL_INDEX(i,j,k)] = f; v1[CELL_INDEX(i,j,k)] = g; v2[CELL_INDEX(i,j,k)] = h; count++; } gzclose(fp); return count; }
/** * Prepare a bilinear-leveled linear move on Cartesian, * splitting the move where it crosses grid borders. */ void bilinear_line_to_destination(const float fr_mm_s, uint16_t x_splits, uint16_t y_splits) { // Get current and destination cells for this line int cx1 = CELL_INDEX(X, current_position[X_AXIS]), cy1 = CELL_INDEX(Y, current_position[Y_AXIS]), cx2 = CELL_INDEX(X, destination[X_AXIS]), cy2 = CELL_INDEX(Y, destination[Y_AXIS]); cx1 = constrain(cx1, 0, ABL_BG_POINTS_X - 2); cy1 = constrain(cy1, 0, ABL_BG_POINTS_Y - 2); cx2 = constrain(cx2, 0, ABL_BG_POINTS_X - 2); cy2 = constrain(cy2, 0, ABL_BG_POINTS_Y - 2); // Start and end in the same cell? No split needed. if (cx1 == cx2 && cy1 == cy2) { buffer_line_to_destination(fr_mm_s); set_current_from_destination(); return; } #define LINE_SEGMENT_END(A) (current_position[_AXIS(A)] + (destination[_AXIS(A)] - current_position[_AXIS(A)]) * normalized_dist) float normalized_dist, end[XYZE]; const int8_t gcx = MAX(cx1, cx2), gcy = MAX(cy1, cy2); // Crosses on the X and not already split on this X? // The x_splits flags are insurance against rounding errors. if (cx2 != cx1 && TEST(x_splits, gcx)) { // Split on the X grid line CBI(x_splits, gcx); COPY(end, destination); destination[X_AXIS] = bilinear_start[X_AXIS] + ABL_BG_SPACING(X_AXIS) * gcx; normalized_dist = (destination[X_AXIS] - current_position[X_AXIS]) / (end[X_AXIS] - current_position[X_AXIS]); destination[Y_AXIS] = LINE_SEGMENT_END(Y); } // Crosses on the Y and not already split on this Y? else if (cy2 != cy1 && TEST(y_splits, gcy)) { // Split on the Y grid line CBI(y_splits, gcy); COPY(end, destination); destination[Y_AXIS] = bilinear_start[Y_AXIS] + ABL_BG_SPACING(Y_AXIS) * gcy; normalized_dist = (destination[Y_AXIS] - current_position[Y_AXIS]) / (end[Y_AXIS] - current_position[Y_AXIS]); destination[X_AXIS] = LINE_SEGMENT_END(X); } else { // Must already have been split on these border(s) // This should be a rare case. buffer_line_to_destination(fr_mm_s); set_current_from_destination(); return; } destination[Z_AXIS] = LINE_SEGMENT_END(Z); destination[E_AXIS] = LINE_SEGMENT_END(E); // Do the split and look for more borders bilinear_line_to_destination(fr_mm_s, x_splits, y_splits); // Restore destination from stack COPY(destination, end); bilinear_line_to_destination(fr_mm_s, x_splits, y_splits); }
int vtk_write_scalar_grid(char *filename, char *dataset_name, long int ni, long int nj, long int nk, double oi, double oj, double ok, double di, double dj, double dk, double *scalars) { FILE *fp; long int i, j, k; double d; if(filename == NULL || scalars == NULL) { printf("error: passed null arguments to vtk_write_scalar_grid\n"); return 1; } vtk_remove(filename); fp = fopen(filename, "w"); if(fp == NULL) { printf("error: vtk_write_scalar_grid cannot open %s to write\n", filename); return 1; } fprintf(fp, "# vtk DataFile Version 2.0\n"); fprintf(fp, "%s\n", dataset_name); #ifdef VTK_BINARY fprintf(fp, "BINARY\n"); #else fprintf(fp, "ASCII\n"); #endif fprintf(fp, "DATASET STRUCTURED_POINTS\n"); fprintf(fp, "DIMENSIONS %ld %ld %ld\n", ni, nj, nk); fprintf(fp, "ORIGIN %lf %lf %lf\n", oi, oj, ok); fprintf(fp, "SPACING %lf %lf %lf\n", di, dj, dk); fprintf(fp, "POINT_DATA %ld\n", ni*nj*nk); fprintf(fp, "SCALARS %s_data double 1\n", dataset_name); fprintf(fp, "LOOKUP_TABLE default\n"); for(k=0; k<nk; k++) { for(j=0; j<nj; j++) { for(i=0; i<ni; i++) { #ifdef VTK_BINARY d = double_swap(scalars[CELL_INDEX(i,j,k)]); fwrite(&d, sizeof(double), 1, fp) #else fprintf(fp, "%4.6lf\n", scalars[CELL_INDEX(i,j,k)]); #endif } } } fclose(fp); return 0; }
long int csv_compressed_read_integer_grid(char *filename_csv, long int ni, long int nj, long int nk, int *scalars) { FILE *fp; long int i, j, k, count; char text[1024]; char filename[1024]; int f; if(filename_csv == NULL || scalars == NULL) { printf("error: passed null arguments to csv_read_scalar_grid\n"); return -1; } strncpy(filename, filename_csv, strlen(filename_csv) + 1); strncat(filename, ".gz", 3); fp = gzopen(filename, "r"); if(fp == NULL) { printf("error: csv_compressed_read_scalar_grid cannot open %s to read\n", filename); return -1; } if(!gzgets(fp, text, sizeof(text))) { if(!gzeof(fp)) { printf("error: fgets in csv_compressed_read_scalar_grid\n"); return(-1); } return 0; } count=0; while(!gzeof(fp)) { if(!gzgets(fp, text, sizeof(text))) { if(!gzeof(fp)) { printf("error: gzgets in csv_compressed_read_integer_grid\n"); return(-1); } break; } sscanf(text, "%ld%*c %ld%*c %ld%*c %d", &i, &j, &k, &f); if(i>ni || j>nj || k>nk) { printf("error: data out of bounds in csv_compressed_read_integer_grid\n"); break; } scalars[CELL_INDEX(i,j,k)] = f; count++; } gzclose(fp); return count; }
int csv_compressed_write_scalar_grid(char *filename_csv, char *dataset_name, long int ni, long int nj, long int nk, double *scalars) { gzFile *fp; long int i, j, k; char filename[1024]; const double emf = 0.00000001; if(filename_csv == NULL || scalars == NULL) { printf("error: passed null arguments to csv_compressed_write_scalar_grid\n"); return 1; } csv_remove(filename_csv); strncpy(filename, filename_csv, strlen(filename_csv) + 1); strncat(filename, ".gz", 3); fp = gzopen(filename, "w"); if(fp == NULL) { printf("error: csv_compressed_write_scalar_grid cannot open %s to write\n", filename); return 1; } gzprintf(fp,"x, y, z, %s\n",dataset_name); for(i=0; i<ni; i++) { for(j=0; j<nj; j++) { for(k=0; k<nk; k++) { if(fabs(scalars[CELL_INDEX(i,j,k)]) > emf) gzprintf(fp, "%ld, %ld, %ld, %10.8lf\n", i, j, k, scalars[CELL_INDEX(i,j,k)]); } } } gzclose(fp); return 0; }
/** * @brief Clears the cell ( @a col, @a row ): it removes from the table the widget that is in this cell * @param table a table * @param col the column in which the widget to remove is (starting from 0) * @param row the row in which the widget to remove is (starting from 0) */ void etk_table_cell_clear(Etk_Table *table, int col, int row) { Etk_Table_Cell *cell; Etk_Widget *child; int i, j; if (!table || !table->cells || col < 0 || col > table->num_cols - 1 || row < 0 || row > table->num_rows - 1) return; if (!(cell = table->cells[CELL_INDEX(table, col, row)]) || !(child = cell->child)) return; for (i = cell->left_attach; i <= cell->right_attach; i++) { for (j = cell->top_attach; j <= cell->bottom_attach; j++) table->cells[CELL_INDEX(table, i, j)] = NULL; } table->cells_list = eina_list_remove_list(table->cells_list, cell->node); free(cell); etk_object_data_set(ETK_OBJECT(child), "_Etk_Table::Cell", NULL); etk_widget_parent_set(child, NULL); etk_signal_emit(ETK_CONTAINER_CHILD_REMOVED_SIGNAL, ETK_OBJECT(table), child); }
/* Check to see if the ship has collided with the life form. */ int ship_collision(ship_t *s, lf_t *lf) { GLfloat brd_w = MAX_X - MIN_X; GLfloat brd_h = MAX_Y - MIN_Y; GLfloat cell_w = brd_w / lf->width; GLfloat cell_h = brd_h / lf->height; int col = (int) (s->position.x / cell_w); int row = (int) ((brd_h - s->position.y) / cell_h); int cell_i = CELL_INDEX(lf, row, col); /* DEBUG */ assert(cell_i >= 0); assert(cell_i < LF_SIZE(lf)); if (lf->board[cell_i] == 1) { return 1; } else { return 0; } }
map_t* map_create(const char* file_name) { FILE* file = fopen(file_name, "r"); if (!file) return NULL; int size_x, size_y; int count = fscanf(file, "%d,%d\n", &size_x, &size_y); if (EOF == count || count < 2 || size_x <= 0 || size_y <= 0) { fclose(file); return NULL; } map_t* map = (map_t*)malloc(sizeof(map_t)); map->terrain = (cell_t*)malloc(sizeof(cell_t)*size_x*size_y); map->size_x = size_x; map->size_y = size_y; char buffer[1024]; int x, y; for (y = 0; y < size_y; ++y) { for (x = 0; x < size_x; ++x) { char flag = fgetc(file); if (EOF == flag || '\n' == flag || '\r' == flag) { free(map->terrain); free(map); fclose(file); return NULL; } int index = XY_TO_CELL_INDEX(map, x, y); cell_t* cell = INDEX_TO_CELL(map, index); CELL_INDEX(map, cell) = index; CELL_X(map, cell) = x; CELL_Y(map, cell) = y; CELL_FLAG(cell) = (unsigned)(flag - '0'); } fgets(buffer, sizeof(buffer), file); } map->astar = astar_create(map); return map; }
/** * @brief Attachs a widget to the table * @param table a table * @param child the widget to attach * @param left_attach the column where the left side of the child will be attached (starting from 0) * @param right_attach the column where the right side of the child will be attached (starting from 0) * @param top_attach the row where the top side of the child will be attached (starting from 0) * @param bottom_attach the row where the bottom side of the child will be attached (starting from 0) * @param fill_policy The fill policy of the child * @param x_padding the amount of free space on the left and on the right sides of the child widget * @param y_padding the amount of free space on the top and on the bottom sides of the child widget */ void etk_table_attach(Etk_Table *table, Etk_Widget *child, int left_attach, int right_attach, int top_attach, int bottom_attach, Etk_Table_Fill_Policy fill_policy, int x_padding, int y_padding) { Etk_Table_Cell *cell; int i, j; if (!table || !table->cells || !child) return; left_attach = ETK_CLAMP(left_attach, 0, table->num_cols - 1); right_attach = ETK_CLAMP(right_attach, left_attach, table->num_cols - 1); top_attach = ETK_CLAMP(top_attach, 0, table->num_rows - 1); bottom_attach = ETK_CLAMP(bottom_attach, top_attach, table->num_rows - 1); cell = malloc(sizeof(Etk_Table_Cell)); cell->left_attach = left_attach; cell->right_attach = right_attach; cell->top_attach = top_attach; cell->bottom_attach = bottom_attach; cell->x_padding = x_padding; cell->y_padding = y_padding; cell->fill_policy = fill_policy; cell->child = child; for (i = left_attach; i <= right_attach; i++) { for (j = top_attach; j <= bottom_attach; j++) { etk_table_cell_clear(table, i, j); table->cells[CELL_INDEX(table, i, j)] = cell; } } table->cells_list = eina_list_append(table->cells_list, cell); cell->node = eina_list_last(table->cells_list); etk_object_data_set(ETK_OBJECT(child), "_Etk_Table::Cell", cell); etk_widget_parent_set(child, ETK_WIDGET(table)); etk_signal_emit(ETK_CONTAINER_CHILD_ADDED_SIGNAL, ETK_OBJECT(table), child); }
/** * @brief Gets the col and row of the given child * @param table a table * @param left_attach the variable to store the left_attach * @param right_attach the variable to store the right_attach * @param top_attach the variable to store the top_attach * @param bottom_attach the variable to store the bottom_attach */ void etk_table_child_position_get(Etk_Table *table, Etk_Widget *child, int *left_attach, int *right_attach, int *top_attach, int *bottom_attach) { int i, j; if (left_attach) *left_attach = -1; if (right_attach) *right_attach = -1; if (top_attach) *top_attach = -1; if (bottom_attach) *bottom_attach = -1; if (!table || !child) return; for (i = 0; i < table->num_cols; i++) { for (j = 0; j < table->num_rows; j++) { Etk_Table_Cell *cell = NULL; cell = table->cells[CELL_INDEX(table, i, j)]; if (cell && cell->child == child) { if (left_attach) *left_attach = cell->left_attach; if (right_attach) *right_attach = cell->right_attach; if (top_attach) *top_attach = cell->top_attach; if (bottom_attach) *bottom_attach = cell->bottom_attach; return; } } } }
/* Updates the state of game play. This includes interactions between bullets, * the life form and the environment. */ void update_game(lf_t *lf, const blist_t *bs, int *score) { for (int i = 0; i < bs->count; i++) { /* Position of bullet */ GLfloat bx = bs->bullets[i].position.x; GLfloat by = bs->bullets[i].position.y; /* Skip bullets outside active area */ if (by >= MAX_Y) { continue; } /* Game board dimensions */ GLfloat brd_w = MAX_X - MIN_X; GLfloat brd_h = MAX_Y - MIN_Y; /* Cell dimensions */ GLfloat cell_w = brd_w / lf->width; GLfloat cell_h = brd_h / lf->height; //DEBUG assert(by >= 0.0); assert(by <= brd_h); assert(bx >= 0.0); assert(bx <= brd_w); /* Get the cell index from all that junk */ int col = (int) (bx / cell_w); int row = (int) ((brd_h - by) / cell_h); int cell_i = CELL_INDEX(lf, row, col); // DEBUG assert(cell_i >= 0); assert(cell_i < LF_SIZE(lf)); /* Adjust the cell accordingly */ if (lf->board[cell_i] == 1) { /* Bullet hit a live cell, kill it and drop the bullet */ toggle_cell(lf, row, col); bs->bullets[i].active = 0; /* Increment the score */ *score += 1; } } }
int vtk_write_vector_grid(char *filename, char *dataset_name, long int ni, long int nj, long int nk, double oi, double oj, double ok, double di, double dj, double dk, double *v1, double *v2, double *v3) { FILE *fp; long int i, j, k; const double emf = 0.000001, d; if(filename == NULL || v1 == NULL || v2 == NULL || v3 == NULL) { printf("error: passed null arguments to vtk_write_vector_grid\n"); return 1; } vtk_remove(filename); fp = fopen(filename, "w"); if(fp == NULL) { printf("error: vtk_write_vector_grid cannot open %s to write\n", filename); return 1; } fprintf(fp, "# vtk DataFile Version 2.0\n"); fprintf(fp, "%s\n", dataset_name); #ifdef VTK_BINARY fprintf(fp, "BINARY\n"); #else fprintf(fp, "ASCII\n"); #endif fprintf(fp, "DATASET STRUCTURED_POINTS\n"); fprintf(fp, "DIMENSIONS %ld %ld %ld\n", ni-1, nj-1, nk-1); fprintf(fp, "ORIGIN %lf %lf %lf\n", oi, oj, ok); fprintf(fp, "SPACING %lf %lf %lf\n", di, dj, dk); fprintf(fp, "POINT_DATA %ld\n", (ni-1)*(nj-1)*(nk-1)); fprintf(fp, "VECTORS %s_data double\n", dataset_name); for(k=0; k<nk-1; k++) { for(j=0; j<nj-1; j++) { for(i=0; i<ni-1; i++) { #ifdef VTK_BINARY d=double_swap(v1[CELL_INDEX(i,j,k)]); fwrite(&d, sizeof(double), 1, fp) d=double_swap(v2[CELL_INDEX(i,j,k)]); fwrite(&d, sizeof(double), 1, fp) d=double_swap(v3[CELL_INDEX(i,j,k)]); fwrite(&d, sizeof(double), 1, fp) #else if( (fabs(v1[CELL_INDEX(i,j,k)]) > emf && fabs(v1[CELL_INDEX(i+1,j,k)]) > emf) || (fabs(v2[CELL_INDEX(i,j,k)]) > emf && fabs(v2[CELL_INDEX(i,j+1,k)]) > emf) || (fabs(v3[CELL_INDEX(i,j,k)]) > emf && fabs(v3[CELL_INDEX(i,j,k+1)]) > emf) ) fprintf(fp, "%4.6lf %4.6lf %4.6lf\n", (v1[CELL_INDEX(i,j,k)] + v1[CELL_INDEX(i+1,j,k)])/2, (v2[CELL_INDEX(i,j,k)] + v2[CELL_INDEX(i,j+1,k)])/2, (v3[CELL_INDEX(i,j,k)] + v3[CELL_INDEX(i,j,k+1)])/2); else fprintf(fp, "0.0 0.0 0.0\n"); #endif } } } fclose(fp); return 0; }