/*reads header and data from file */ Grid *read_grid_from_arcascii_file(char *filename) { assert(filename); FILE *fp = fopen(filename, "r"); assert(fp); Grid *grid = create_empty_grid(); grid->hd = fread_header_from_arcascii_file(fp); alloc_grid_data(grid); /*READ DATA */ dimensionType i, j; int first_flag = 1; float value = 0; for (i = 0; i < grid->hd->nrows; i++) { for (j = 0; j < grid->hd->ncols; j++) { fscanf(fp, "%f", &value); grid->grid_data[i][j] = value; if (first_flag) { if (is_nodata_grid(grid, value)) continue; grid->minvalue = grid->maxvalue = value; first_flag = 0; } else { if (is_nodata_grid(grid, value)) continue; if (value > grid->maxvalue) grid->maxvalue = value; if (value < grid->minvalue) grid->minvalue = value; } } fscanf(fp, "\n"); } fclose(fp); #ifdef DEBUG_ON printf("**DEBUG: readGridFromArcasciiFile():\n"); fflush(stdout); #endif return grid; }
//for NVIEWSHEDS small, the resulting map is basically all empty; //the interpolate function extends each non-empty output viewshed //value to a ball centered at that point void interpolate_raster(Grid* outgrid, char* output_name) { assert(outgrid); printf("\n-----\n"); printf("Multiviewshed done, interpolating result grid."); printf("total %d viewsheds: ", nvp); /*create the interpolated output raster */ Grid* intgrid = create_empty_grid(); assert(intgrid); copy_header(intgrid->hd, outgrid->hd); alloc_grid_data(intgrid); int nrows, ncols, row, col; nrows = intgrid->hd->nrows; ncols = intgrid->hd->ncols; int nvis; for(row = 0; row < nrows; row++) { for(col = 0; col < ncols; col++) { nvis = findClosestViewpoint(row,col); //printf("%d %d :%d\n", row, col, nvis); set(intgrid, row, col, nvis); }//for col } //for row /* write grid to file */ char* interp_name = (char*)malloc((strlen(output_name+8))*sizeof(char)); assert(interp_name); strcpy(interp_name,output_name); strcat(interp_name, "-interp"); printf("creating interpolated raster: %s\n", interp_name); save_grid_to_arcascii_file(intgrid, interp_name); destroy_grid(intgrid); }
int main(int argc, char* argv[]) { //this variable collects all user options MultiviewOptions options; parse_args(argc, argv, &options); record_args(options); #ifdef INTERPOLATE_RESULT viewsheds = (Nvis*) malloc((options->NVIEWSHEDS+10)*sizeof(Nvis)); assert(viewsheds); #endif //read input raster printf("reading input grid %s ", options.input_name); Grid *ingrid = read_grid_from_arcascii_file(options.input_name); assert(ingrid); printf("..done\n"); //number of rows and columns in the grid int nrows, ncols; nrows = ingrid->hd->nrows; ncols = ingrid->hd->ncols; printf("grid: rows = %d, cols = %d\n", nrows, ncols); if (options.NVIEWSHEDS ==0) options.NVIEWSHEDS = nrows * ncols; //create an output grid Grid* outgrid = create_empty_grid(); assert(outgrid); //outgrid->hd = create_empty_header(); //the header is allocated in create_empty_grid() copy_header(outgrid->hd, *(ingrid->hd)); alloc_grid_data(outgrid); /* **************************************** */ /* INITIALIZE EVENT LIST */ /* **************************************** */ /*allocate the eventlist to hold the maximum number of events possible*/ Event* eventList; eventList = (Event*) malloc(ncols * nrows * 3 * sizeof(Event)); assert(eventList); /*initialize the eventList with the info common to all viewpoints */ long nevents; Rtimer initTime; rt_start(initTime); nevents = init_event_list(eventList, ingrid ); printf("nb events = %ld\n", nevents); rt_stop(initTime); print_init_timings(initTime); /* ****************************** */ /* compute the viewshed of the i % DO_EVERY point */ /* ****************************** */ assert(options.NVIEWSHEDS > 0); int DO_EVERY = nrows * ncols/ options.NVIEWSHEDS; /* start going through the data and considering each point, in turn, as a viewshed */ if (options.SWEEP_MODE == SWEEP_DISTRIBUTE) { assert(options.BASECASE_THRESHOLD >0 && options.NUM_SECTORS >0); compute_multiviewshed_distribution(options, DO_EVERY, ingrid, outgrid, nevents, eventList); } else { compute_multiviewshed_radial(options, DO_EVERY, ingrid, outgrid, nevents, eventList); } /* ****************************** */ /*all sweeping and computing done - clean up */ free(eventList); //write output grid to file save_grid_to_arcascii_file(outgrid, options.output_name); //clean up destroy_grid(ingrid); destroy_grid(outgrid); #ifdef INTERPOLATE_RESULT //for NVIEWSHEDS small, the resulting map is basically all empty; //the interpolate function extends each non-empty output viewshed //value to a ball centered at that point interpolate_raster(outgrid, output_name); #endif exit(0); }
int main(void) { int dir; srand(time(NULL)); uint8_t nb_empty_box, nb_merge; #if USE_BIN /* Creation of a grid */ grid_t grid = create_empty_grid_bin(); /* Grid backup */ grid_t saved_grid; /* Initialisation of the grid */ grid = init_grid_bin(grid, &nb_empty_box); /* Display the grid */ display_grid_bin(grid); /* Main loop for a game */ while (nb_empty_box > 0 || merge_possible_bin(grid)) { do { saved_grid = grid; /* While the move does not lead to a change on the grid */ // dir = random_move(); dir = best_move_bin(grid, nb_empty_box); /* Trying to play the move */ grid = move_bin(grid, dir, &nb_merge); nb_empty_box += nb_merge; } while (grid == saved_grid); /* Add tile */ grid = add_tile_bin(grid); nb_empty_box--; /* Display the new grid */ display_grid_bin(grid); printf("Empty boxes: %u\n", nb_empty_box); } #else /* Creation of a grid */ uint16_t **grid = create_empty_grid(); /* Initialisation of the grid */ init_grid(grid); /* Display the grid */ display_grid(grid); #if DEBUG printf("Value: %i\n", evaluate(grid)); #endif /* Main loop for a game */ while (!game_over(grid)) { /* While the move does not lead to a change on the grid */ do { #if IA #if RANDOM dir = random_move(); #else dir = best_move(grid); #endif #else dir = ask_dir(); #endif /* Trying to play the move */ } while (!move(grid, dir)); /* Add a new tile at the end of each round */ add_tile(grid); /* Display the new grid */ display_grid(grid); #if DEBUG printf("Value: %i\n", evaluate(grid)); #endif } #endif return 0; }