//for pearson correlation coord_t c_pcc(mi_t* const m, const coord_t* const xs, const coord_t* const ys) { int i; make_grid(&m->grid, xs, ys, m->n, m->k); coord_t meanx = 0.0; coord_t meany = 0.0; coord_t sum1 = 0.0; coord_t sum2 = 0.0; coord_t sum3 = 0.0; const int len = m->n; for( i = 0; i < len; i++ ) { meanx += xs[i]; meany += ys[i]; } meanx = 1.0*meanx/len; meany = 1.0*meany/len; for( i = 0; i < len; i++ ) { sum1 += (xs[i] - meanx)*(ys[i] - meany); sum2 += (xs[i] - meanx)*(xs[i] - meanx); sum3 += (ys[i] - meany)*(ys[i] - meany); } destroy_grid(&m->grid); if( sum2 == 0.0 || sum3 == 0.0 ) return(0.0); else return( 1.0*sum1/(sqrt(sum2)*sqrt(sum3)) ); }
coord_t mutual_information(mi_t* const m, const coord_t* const xs, const coord_t* const ys) { const coord_t* pxs; const coord_t* pys; make_grid(&m->grid, xs, ys, m->n, m->k); ordered_points(&m->grid, &pxs, &pys); sort_coords(pxs, m->sxs, m->xiis, m->n); sort_coords(pys, m->sys, m->yiis, m->n); int i; coord_t accum = 0; for (i = 0; i < m->n; i++) { int kis[m->k]; search_knn(&m->grid, pxs[i], pys[i], kis); const dist_t mdx = find_range(pxs, i, kis, m->k); const int nx = region_count(m->sxs, m->n, m->xiis[i], mdx); const dist_t mdy = find_range(pys, i, kis, m->k); const int ny = region_count(m->sys, m->n, m->yiis[i], mdy); accum += get_psi(m, nx) + get_psi(m, ny); } destroy_grid(&m->grid); return get_psi(m, m->k) + get_psi(m, m->n) - (1.0/m->k) - (accum/m->n); }
//for spearman correlation coord_t c_scc(mi_t* const m, const coord_t* const xs, const coord_t* const ys, const int* const xsix, const int* const ysix ) { make_grid(&m->grid, xs, ys, m->n, m->k); const int len = m->n; int i, j; //sort y by the rank of x int xorder, yorder; coord_t vecx_x[len], vecix_x[len], vecy_x[len], vecixy_x[len]; for(i = 0; i < len; i++ ) { xorder = xsix[i]; vecx_x[xorder-1] = xs[i]; vecix_x[xorder-1] = xorder; vecy_x[xorder-1] = ys[i]; vecixy_x[xorder-1] = ysix[i]; } //mask the rank of x maskrankforSCC( vecx_x, vecix_x, len ); /* for( i = 0; i < len; i++ ) { printf("%f, %f, %f, %f\n", vecx_x[i], vecix_x[i], vecy_x[i], vecixy_x[i] ); } printf("\n\n"); */ //sort x by the rank of y coord_t vecy_y[len], vecixy_y[len], vecixx_y[len]; for( i = 0; i < len; i++ ) { yorder = vecixy_x[i]; vecy_y[yorder-1] = vecy_x[i]; vecixy_y[yorder-1] = yorder; vecixx_y[yorder-1] = vecix_x[i]; } //mask the rank of y maskrankforSCC( vecy_y, vecixy_y, len ); /* for( i = 0; i < len; i++ ) { printf("%f, %f, %f\n", vecy_y[i], vecixy_y[i], vecixx_y[i] ); } printf("\n\n"); */ coord_t tmp = 0; for( i = 0; i < len; i++ ) { tmp += (vecixy_y[i] - vecixx_y[i])*(vecixy_y[i] - vecixx_y[i]); } // printf("%d, %f\n", len, 1.0 - 6.0*tmp/(len*len*len - len) ); destroy_grid(&m->grid); return( 1.0 - 6.0*tmp/(len*len*len - len) ); }
//for spearman correlation coord_t c_kcc(mi_t* const m, const coord_t* const xs, const coord_t* const ys ) { make_grid(&m->grid, xs, ys, m->n, m->k); const int len = m->n; int i, j, num; num = 0; for( i = 1; i < len; i++ ) { for( j = 0; j < i; j++ ) { num += (xs[i] - xs[j])*(ys[i] - ys[j]) > 0 ? 1 : -1; }//end for j }//end for i destroy_grid(&m->grid); return( 2.0*num/(len*len - len) ); }
//for euclidean distance coord_t c_eudist( mi_t* const m, const coord_t* const xs, const coord_t* const ys) { int i; make_grid(&m->grid, xs, ys, m->n, m->k); coord_t sum1 = 0.0; coord_t t_tmp = 0.0; const int len = m->n; for( i = 0; i < len; i++ ) { t_tmp = xs[i] - ys[i]; sum1 += t_tmp*t_tmp; } destroy_grid(&m->grid); if( sum1 == 0.0 ) return(0.0); else return( sqrt(sum1) ); }
//for gini correlation coord_t c_gcc(mi_t* const m, const coord_t* const xs, const coord_t* const ys, const int* const xsix, const int* const ysix ) { make_grid(&m->grid, xs, ys, m->n, m->k); const int len = m->n; int xorder, yorder; coord_t vecx_x[len], vecx_y[len], vecy_y[len], vecy_x[len]; for (int i = 0; i < len; i++ ) { xorder = xsix[i]; vecx_x[xorder-1] = xs[i]; vecy_x[xorder-1] = ys[i]; yorder = ysix[i]; vecy_y[yorder-1] = ys[i]; vecx_y[yorder-1] = xs[i]; } coord_t accumx_x = 0; coord_t accumx_y = 0; coord_t accumy_y = 0; coord_t accumy_x = 0; for (int i = 0; i < len; i++) { //for order x coord_t weight = 2.0*(i+1)- m->n -1.0; accumx_x += weight*vecx_x[i]; accumx_y += weight*vecx_y[i]; accumy_y += weight*vecy_y[i]; accumy_x += weight*vecy_x[i]; }//for i coord_t gccx_y = accumx_y/accumx_x; coord_t gccy_x = accumy_x/accumy_y; coord_t final_gcc = gccx_y*gccx_y > gccy_x*gccy_x ? gccx_y : gccy_x; destroy_grid(&m->grid); return final_gcc; }
//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); }
/// Destructor. GridManager::~GridManager() { destroy_grid(ug_); }
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); }
CFUNC void F77_FUNC_(fdestroy_grid,FDESTROY_GRID) (NUgrid **grid) { destroy_grid (*grid); }