// Copy vector from builtin backend. static boost::shared_ptr<vector> copy_vector(typename builtin<real>::vector const &x, const params &prm) { precondition(!prm.context().empty(), "Empty VexCL context!"); return boost::make_shared<vector>(prm.context(), x); }
////////////////////////////////////////////////////////////////// // fastuidraw::gl::GlyphAtlasGL methods fastuidraw::gl::GlyphAtlasGL:: GlyphAtlasGL(const params &P): GlyphAtlas(TexelStoreGL::create(P.texel_store_dimensions(), P.delayed()), GeometryStoreGL::create(P.number_floats(), P.delayed(), P.alignment())) { m_d = FASTUIDRAWnew GlyphAtlasGLPrivate(P); }
// Create vector of the specified size. static boost::shared_ptr<vector> create_vector(size_t size, const params &prm) { precondition(!prm.context().empty(), "Empty VexCL context!"); return boost::make_shared<vector>(prm.context(), size); }
wrapper(size_t n, params prm = params(), const backend_params &bprm = backend_params(), const InnerProduct &inner_product = InnerProduct() ) : s(prm.get("type", runtime::solver::bicgstab)), handle(0) { if (!prm.erase("type")) AMGCL_PARAM_MISSING("type"); switch(s) { #define AMGCL_RUNTIME_SOLVER(type) \ case type: \ handle = static_cast<void*>(new amgcl::solver::type<Backend, InnerProduct>(n, prm, bprm, inner_product)); \ break AMGCL_RUNTIME_SOLVER(cg); AMGCL_RUNTIME_SOLVER(bicgstab); AMGCL_RUNTIME_SOLVER(bicgstabl); AMGCL_RUNTIME_SOLVER(gmres); AMGCL_RUNTIME_SOLVER(lgmres); AMGCL_RUNTIME_SOLVER(fgmres); AMGCL_RUNTIME_SOLVER(idrs); #undef AMGCL_RUNTIME_SOLVER default: throw std::invalid_argument("Unsupported solver type"); } }
int ctable_factory_base::setup(int dfd, const char * name, const params & config, dtype::ctype key_type) { istr base; params base_config; if(!config.get("base", &base) || !config.get("base_config", &base_config)) return NULL; return setup(base, dfd, name, base_config, key_type); }
params loadParams(const std::string& filename, params& p) { CSVInput input(filename); CSVHeader header(filename); p.setInput(input); p.setHeader(header); return p; }
lemon_interface(const params &p) { state_ = ::ParseAlloc(&pooled::tracked_byte_alloc); if (p.trace()) { #if NERVE_TRACE_PARSER ::ParseTrace(stdout, detail::parse_trace_prefix); #endif } context_ = p.context(); }
preconditioner( const Matrix &A, params prm = params(), const backend_params &bprm = backend_params()) : _class(prm.get("class", runtime::precond_class::amg)), handle(0) { if (!prm.erase("class")) AMGCL_PARAM_MISSING("class"); switch(_class) { case precond_class::amg: { typedef runtime::amg<Backend> Precond; handle = static_cast<void*>(new Precond(A, prm, bprm)); } break; case precond_class::relaxation: { typedef runtime::relaxation::as_preconditioner<Backend> Precond; handle = static_cast<void*>(new Precond(A, prm, bprm)); } break; case precond_class::dummy: { typedef amgcl::preconditioner::dummy<Backend> Precond; handle = static_cast<void*>(new Precond(A, prm, bprm)); } break; case precond_class::nested: { typedef make_solver< preconditioner, runtime::iterative_solver<Backend> > Precond; handle = static_cast<void*>(new Precond(A, prm, bprm)); } break; default: throw std::invalid_argument("Unsupported preconditioner class"); } }
int exist_dtable::create(int dfd, const char * file, const params & config, dtable::iter * source, const ktable * shadow) { int e_dfd, r; params base_config, dnebase_config; const dtable_factory * base = dtable_factory::lookup(config, "base"); const dtable_factory * dnebase = dtable_factory::lookup(config, "dnebase"); if(!base || !dnebase) return -ENOENT; if(!config.get("base_config", &base_config, params())) return -EINVAL; if(!config.get("dnebase_config", &dnebase_config, params())) return -EINVAL; if(!source_shadow_ok(source, shadow)) return -EINVAL; r = mkdirat(dfd, file, 0755); if(r < 0) return r; e_dfd = openat(dfd, file, O_RDONLY); if(e_dfd < 0) goto fail_open; /* just to be sure */ source->first(); { dtable_skip_iter<dne_skip_test> base_source(source); r = base->create(e_dfd, "base", base_config, &base_source, NULL); if(r < 0) goto fail_base; } source->first(); { full_ktable full_shadow(source); nonshadow_skip_test skip_test(shadow); dtable_skip_iter<nonshadow_skip_test> dnebase_source(source, skip_test); r = dnebase->create(e_dfd, "dnebase", dnebase_config, &dnebase_source, &full_shadow); if(r < 0) goto fail_dnebase; } close(e_dfd); return 0; fail_dnebase: util::rm_r(e_dfd, "base"); fail_base: close(e_dfd); fail_open: unlinkat(dfd, file, AT_REMOVEDIR); return (r < 0) ? r : -1; }
int deltaint_dtable::init(int dfd, const char * file, const params & config, sys_journal * sysj) { const dtable_factory * base_factory; const dtable_factory * ref_factory; params base_config, ref_config; int di_dfd; if(base) deinit(); base_factory = dtable_factory::lookup(config, "base"); ref_factory = dtable_factory::lookup(config, "ref"); if(!base_factory || !ref_factory) return -ENOENT; if(!config.get("base_config", &base_config, params())) return -EINVAL; if(!config.get("ref_config", &ref_config, params())) return -EINVAL; di_dfd = openat(dfd, file, O_RDONLY); if(di_dfd < 0) return di_dfd; base = base_factory->open(di_dfd, "base", base_config, sysj); if(!base) goto fail_base; reference = ref_factory->open(di_dfd, "ref", ref_config, sysj); if(!reference) goto fail_reference; ktype = base->key_type(); cmp_name = base->get_cmp_name(); assert(ktype == reference->key_type()); scan_iter = base->iterator(); if(!scan_iter) goto fail_scan; ref_iter = reference->iterator(); if(!ref_iter) goto fail_iter; close(di_dfd); return 0; fail_iter: delete scan_iter; scan_iter = NULL; fail_scan: reference->destroy(); reference = NULL; fail_reference: base->destroy(); base = NULL; fail_base: close(di_dfd); return -1; }
// Copy matrix from builtin backend. static boost::shared_ptr<matrix> copy_matrix(boost::shared_ptr< typename builtin<real>::matrix > A, const params &prm) { precondition(!prm.context().empty(), "Empty VexCL context!"); const typename builtin<real>::matrix &a = *A; BOOST_AUTO(Aptr, a.ptr_data()); BOOST_AUTO(Acol, a.col_data()); BOOST_AUTO(Aval, a.val_data()); return boost::make_shared<matrix>(prm.context(), rows(*A), cols(*A), Aptr, Acol, Aval); }
amg( const Matrix &A, const params &prm = params(), const backend_params &backend_prm = backend_params() ) : coarsening(prm.get("coarsening.type", runtime::coarsening::smoothed_aggregation)), relaxation(prm.get("relaxation.type", runtime::relaxation::spai0)), handle(0) { runtime::detail::process_amg<Backend>( coarsening, relaxation, runtime::detail::amg_create<Backend, Matrix>(handle, A, prm, backend_prm) ); }
int exception_dtable::init(int dfd, const char * file, const params & config, sys_journal * sysj) { const dtable_factory * base_factory; const dtable_factory * alt_factory; params base_config, alt_config; int excp_dfd; if(base || alt) deinit(); base_factory = dtable_factory::lookup(config, "base"); alt_factory = dtable_factory::lookup(config, "alt"); if(!base_factory || !alt_factory) return -EINVAL; if(!config.get("base_config", &base_config, params())) return -EINVAL; if(!config.get("alt_config", &alt_config, params())) return -EINVAL; if(!config.get_blob_or_string("reject_value", &reject_value)) return -EINVAL; /* the reject value must exist, because nonexistent values * can get pruned out if the shadow does not require them */ if(!reject_value.exists()) return -EINVAL; excp_dfd = openat(dfd, file, O_RDONLY); if(excp_dfd < 0) return excp_dfd; base = base_factory->open(excp_dfd, "base", base_config, sysj); if(!base) goto fail_base; alt = alt_factory->open(excp_dfd, "alt", alt_config, sysj); if(!alt) goto fail_alt; ktype = base->key_type(); if(ktype != alt->key_type()) goto fail_ktype; cmp_name = base->get_cmp_name(); close(excp_dfd); return 0; fail_ktype: alt->destroy(); alt = NULL; fail_alt: base->destroy(); base = NULL; fail_base: close(excp_dfd); return -1; }
void read_dem_header ( image<short> & dem ) { char file[1000]; GDALDataset *df; GDALRasterBand *bd; double trans[6]; int rows, cols; strcpy ( file, p.value("dem_file").c_str()); if ( strlen(file) == 0 ) { fprintf(stderr,"Need to specify a dem file\n"); exit(1); } df = (GDALDataset *) GDALOpen( file, GA_ReadOnly ); if( df == NULL ) { fprintf(stderr,"Could not open dem file: %s\n", file ); exit(1); } rows = df->GetRasterYSize(); cols = df->GetRasterXSize(); dem.create(rows,cols); if( df->GetGeoTransform( trans ) == CE_None ) { dem_east = trans[0]; dem_north = trans[3]; dem_wid = trans[1]; } else { fprintf(stderr,"Dem file: %s has no geographic metadata\n", file ); exit(1); } delete df; }
inline void read(const FileNode& node, params &x, const params & default_value = params()) { if(node.empty()) x = default_value; else x.read(node); }
int btree_dtable::init(int dfd, const char * file, const params & config, sys_journal * sysj) { const dtable_factory * factory; params base_config; int r, bt_dfd; if(base) deinit(); factory = dtable_factory::lookup(config, "base"); if(!factory) return -ENOENT; if(!config.get("base_config", &base_config, params())) return -EINVAL; if(!factory->indexed_access(base_config)) return -ENOSYS; bt_dfd = openat(dfd, file, O_RDONLY); if(bt_dfd < 0) return bt_dfd; base = factory->open(bt_dfd, "base", base_config, sysj); if(!base) goto fail_base; ktype = base->key_type(); assert(ktype == dtype::UINT32); cmp_name = base->get_cmp_name(); /* open the btree */ btree = rofile::open<BTREE_PAGE_KB, 8>(bt_dfd, "btree"); if(!btree) goto fail_open; r = btree->read_type(0, &header); if(r < 0) goto fail_format; /* check the header */ if(header.magic != BTREE_DTABLE_MAGIC || header.version != BTREE_DTABLE_VERSION) goto fail_format; if(header.page_size != BTREE_PAGE_SIZE || header.pageno_size != BTREE_PAGENO_SIZE) goto fail_format; if(header.key_size != BTREE_KEY_SIZE || header.index_size != BTREE_INDEX_SIZE) goto fail_format; /* 1 -> uint32, and even with an empty table there will be a root page */ if(header.key_type != 1 || !header.root_page) goto fail_format; close(bt_dfd); return 0; fail_format: delete btree; fail_open: base->destroy(); base = NULL; fail_base: close(bt_dfd); return -1; }
int simple_stable::create(int dfd, const char * name, const params & config, dtype::ctype key_type) { int md_dfd, r; params meta_config, data_config; const dtable_factory * meta = dtable_factory::lookup(config, "meta"); const ctable_factory * data = ctable_factory::lookup(config, "data"); if(!meta || !data) return -ENOENT; if(!config.get("meta_config", &meta_config, params())) return -EINVAL; if(!config.get("data_config", &data_config, params())) return -EINVAL; r = mkdirat(dfd, name, 0755); if(r < 0) return r; md_dfd = openat(dfd, name, O_RDONLY); if(md_dfd < 0) { r = md_dfd; goto fail_open; } /* the metadata is keyed by named properties (strings) */ r = meta->create(md_dfd, "st_meta", meta_config, dtype::STRING); if(r < 0) goto fail_meta; r = data->create(md_dfd, "st_data", data_config, key_type); if(r < 0) goto fail_data; close(md_dfd); return 0; fail_data: util::rm_r(md_dfd, "st_meta"); fail_meta: close(md_dfd); fail_open: unlinkat(dfd, name, AT_REMOVEDIR); return r; }
int simple_stable::init(int dfd, const char * name, const params & config, sys_journal * sysj) { int r = -1; params meta_config, data_config; const dtable_factory * meta = dtable_factory::lookup(config, "meta"); const ctable_factory * data = ctable_factory::lookup(config, "data"); if(md_dfd >= 0) deinit(); assert(column_map.empty()); if(!meta || !data) return -ENOENT; if(!config.get("meta_config", &meta_config, params())) return -EINVAL; if(!config.get("data_config", &data_config, params())) return -EINVAL; md_dfd = openat(dfd, name, O_RDONLY); if(md_dfd < 0) return md_dfd; dt_meta = meta->open(md_dfd, "st_meta", meta_config, sysj); if(!dt_meta) goto fail_meta; ct_data = data->open(md_dfd, "st_data", data_config, sysj); if(!ct_data) goto fail_data; /* check sanity? */ r = load_columns(); if(r < 0) goto fail_check; return 0; fail_check: delete ct_data; fail_data: dt_meta->destroy(); fail_meta: close(md_dfd); md_dfd = -1; return r; }
wrapper(const amgcl::mpi::distributed_matrix<Backend> &A, params prm, const backend_params &bprm = backend_params()) : r(prm.get("type", runtime::relaxation::spai0)), handle(0) { if (!prm.erase("type")) AMGCL_PARAM_MISSING("type"); switch(r) { #define AMGCL_RELAX_DISTR(type) \ case runtime::relaxation::type: \ handle = static_cast<void*>(new amgcl::mpi::relaxation::type<Backend>(A, prm, bprm)); \ break #define AMGCL_RELAX_LOCAL_DISTR(type) \ case runtime::relaxation::type: \ handle = call_constructor<amgcl::relaxation::type>(A, prm, bprm); \ break; #define AMGCL_RELAX_LOCAL_LOCAL(type) \ case runtime::relaxation::type: \ handle = call_constructor<amgcl::relaxation::type>(*A.local(), prm, bprm); \ break; AMGCL_RELAX_DISTR(spai0); AMGCL_RELAX_LOCAL_DISTR(chebyshev); AMGCL_RELAX_LOCAL_LOCAL(damped_jacobi); AMGCL_RELAX_LOCAL_LOCAL(ilu0); AMGCL_RELAX_LOCAL_LOCAL(iluk); AMGCL_RELAX_LOCAL_LOCAL(ilut); AMGCL_RELAX_LOCAL_LOCAL(spai1); AMGCL_RELAX_LOCAL_LOCAL(gauss_seidel); #undef AMGCL_RELAX_LOCAL_LOCAL #undef AMGCL_RELAX_LOCAL_DISTR #undef AMGCL_RELAX_DISTR default: throw std::invalid_argument("Unsupported relaxation type"); } }
int cache_dtable::init(int dfd, const char * file, const params & config, sys_journal * sysj) { int r; const dtable_factory * factory; params base_config; if(base) deinit(); if(!config.get("cache_size", &r, 0) || r < 0) return -EINVAL; cache_size = r; factory = dtable_factory::lookup(config, "base"); if(!factory) return -EINVAL; if(!config.get("base_config", &base_config, params())) return -EINVAL; base = factory->open(dfd, file, base_config, sysj); if(!base) return -1; ktype = base->key_type(); cmp_name = base->get_cmp_name(); return 0; }
int btree_dtable::create(int dfd, const char * file, const params & config, dtable::iter * source, const ktable * shadow) { int bt_dfd, r; params base_config; dtable * base_dtable; const dtable_factory * base = dtable_factory::lookup(config, "base"); if(!base) return -ENOENT; if(!config.get("base_config", &base_config, params())) return -EINVAL; if(!base->indexed_access(base_config)) return -ENOSYS; if(!source_shadow_ok(source, shadow)) return -EINVAL; r = mkdirat(dfd, file, 0755); if(r < 0) return r; bt_dfd = openat(dfd, file, O_RDONLY); if(bt_dfd < 0) goto fail_open; r = base->create(bt_dfd, "base", base_config, source, shadow); if(r < 0) goto fail_create; base_dtable = base->open(bt_dfd, "base", base_config, NULL); if(!base_dtable) goto fail_reopen; r = write_btree(bt_dfd, "btree", base_dtable); if(r < 0) goto fail_write; base_dtable->destroy(); close(bt_dfd); return 0; fail_write: base_dtable->destroy(); fail_reopen: util::rm_r(bt_dfd, "base"); fail_create: close(bt_dfd); fail_open: unlinkat(dfd, file, AT_REMOVEDIR); return (r < 0) ? r : -1; }
int keydiv_dtable::load_dividers(const params & config, size_t dt_count, divider_list * list, bool skip_check) { std::vector<T> data; if(!config.get_seq("divider_", NULL, 0, true, &data)) return -1; /* if there are n dtables, there should be n - 1 dividers */ if(dt_count && data.size() != dt_count - 1) return -EINVAL; list->clear(); for(size_t i = 0; i < data.size(); i++) list->push_back(dtype((C) data[i])); /* dividers should be in increasing order, but we can't check blobs * since they might need a comparator that we don't have yet */ if(!skip_check) for(size_t i = 1; i < list->size(); i++) if((*list)[i - 1].compare((*list)[i]) >= 0) return -EINVAL; return 0; }
void run(params<T> p, multi_array<T,2> in) { vex::stopwatch<> w; auto prof = make_shared<vex::profiler<>>(vex::current_context().queue()); try { auto c = p.runner(); if(profile) c->profiler = prof; c->run(in); for(size_t i = 0 ; i < runs ; i++) { w.tic(); in = c->run(in); w.toc(); } } catch(...) {} if(profile) { cout << *prof << endl; } else { cout << '\t'; if(w.tics() > 0) cout << w.average(); else cout << "nan"; } }
void read_image_file(image<short> & img) { double camera_roll, camera_pitch, camera_yaw; FILE *fp; rgb_image rgb, *q; double east, north, alt; img_map::iterator i; vec3 utm(0,0,0), x(1,0,0), y(0,1,0), z(0,0,1); mat4 trans, roll, pitch, yaw; camera_roll = p.dvalue("camera_roll"); camera_pitch = p.dvalue("camera_pitch"); camera_yaw = p.dvalue("camera_yaw"); fp = fopen ( p.value("images_file").c_str(), "r" ); if ( !fp ) { fprintf(stderr,"Could not open images file: %s\n", p.value("images_file").c_str() ); exit(1); } xy[0][0] = -2*img.rows; xy[0][1] = -2*img.cols; while ( rgb.read_params(fp) ) { if ( rgb.id > max_img_id ) max_img_id = rgb.id; imgs[rgb.id] = rgb; } fclose(fp); img_indices = new int[max_img_id+1]; sea_level = p.dvalue("sea_level"); i = imgs.begin(); while ( i != imgs.end() ) { q = &i->second; q->create_image(input_rows,input_cols); //printf("%s\n", q->pathname.c_str()); //printf("%d %10.2f %10.2f %7.2f %7.2f %7.2f %7.2f\n",p->id, //p->east, p->north, p->alt, //p->omega, p->phi, p->kappa ); img_indices[img_ct] = q->id; img_ct++; utm[0]=q->east; utm[1]=q->north; trans = translation3D(utm); pitch = rotation3D ( x, q->omega); roll = rotation3D ( y, q->phi); yaw = rotation3D ( z, q->kappa); q->M = pitch; q->M = q->M * roll; q->M = q->M * yaw; q->zrot = -1.0 * z; q->zrot = q->M * q->zrot; q->M = trans; q->M = q->M * pitch; q->M = q->M * roll; q->M = q->M * yaw; q->MI = q->M.inverse(); q->unit_depth = p.dvalue("focal_length")*1000.0/p.dvalue("pixel_size"); q->cam = utm; q->cam[2] = 0; i++; } }
void set_defaults() { p.set("images_file", "centers" ); p.set("easting_left", "379000.0" ); p.set("easting_right", "382500.0" ); p.set("northing_top", "2938000.0" ); p.set("northing_bottom", "2937000.0" ); p.set("utm_zone", "17" ); p.set("output_cell_size", "5.0" ); p.set("input_cell_size", "0.2" ); p.set("input_rows", "1200" ); p.set("input_columns", "1600" ); p.set("output_file", "rgb.tif"); p.set("dist_file", ""); p.set("blob_file", ""); p.set("overviews", "3" ); p.set("focal_length", "16.065" ); p.set("pixel_size", "7.4" ); p.set("readers", "2" ); p.set("writers", "2" ); p.set("mappers", "2" ); }
int main ( int argc, char **argv ) { int i, j, n, pid, t; double pix; double ir, ic; int req, task, value; char task_name[16]; log_start(); if ( argc < 2 ) { fprintf(stderr,"Usage: %s param_file\n", argv[0]); exit(1); } pe.mq = msgget ( getpid(), 0777|IPC_CREAT ); set_defaults(); p.read_file(argv[1]); GDALAllRegister(); east1 = p.dvalue("easting_left"); east2 = p.dvalue("easting_right"); north1 = p.dvalue("northing_bottom"); north2 = p.dvalue("northing_top"); wid = p.dvalue("output_cell_size"); pix = p.dvalue("input_cell_size"); ir = p.dvalue("input_rows"); ic = p.dvalue("input_columns"); input_rows = (int)ir; input_cols = (int)ic; max_dist = 1.2 * pix * sqrt(ir*ir+ic*ic) / wid; max_dist_2 = max_dist/2; max_dist = max_dist * max_dist; readers = p.ivalue("readers"); mappers = p.ivalue("mappers"); writers = p.ivalue("writers"); rows = int((north2 - north1) / wid); cols = int((east2 - east1) / wid); cindex = new int[cols]; dist.create(rows,cols); img.create(rows,cols); srand(time(NULL)); read_image_file(img); fill ( img, dist, 0, rows ); //spread_dist ( img, dist ); //if ( p.value("dist_file") != "" ) output_dist_geotiff ( dist, v ); //if ( p.value("blob_file") != "" ) output_pcolor_geotiff ( img, dist, v ); sort ( img_indices, img_indices+img_ct, comp_row ); valid_img = new int[max_img_id]; for ( i = 0; i < img_ct; i++ ) { j = img_indices[i]; valid_img[j] = 0; if ( xy[j][0]+max_dist_2 >= 0 && xy[j][0]-max_dist_2 < rows ) { pe.add_task ( "image", j ); } } read_dem_header(dem); pe.launch ( dem_reader ); out.create_image(rows,cols); for ( i = 0; i < out.rows; i += bs ) pe.add_task ( "mapper", i ); for ( i = 0; i < readers; i++ ) { pe.launch ( reader ); } for ( i = 0; i < mappers; i++ ) { pe.launch ( mapper ); } for ( i = 0; i < writers; i++ ) { pe.launch ( writer ); } while ( pe.children > 0 ) { t = msgrcv ( pe.mq, &pe.msg, MSG_SIZE, 1, 0 ); if ( t < 0 ) perror(""); sscanf(pe.msg.s,"%d %d %s %d", &req, &pid, task_name, &value ); switch ( req ) { case EXIT: log("exit %d",pid); pe.children--; break; case WAIT: if ( pe.ready(task_name,value) ) { sprintf(pe.msg.s,"1"); pe.msg.t = pid; //printf("Sending %d task %d\n",pid,task); log("wait %d %s %d",pid,task_name,value); t = msgsnd ( pe.mq, &pe.msg, MSG_SIZE, 0 ); } else { log("sleep %d %s %d",pid,task_name,value); pe.enqueue ( task_name, value, pid ); } break; case TASK: task = pe.fetch_task(task_name); log("task %d %s %d",pid,task_name,task); sprintf(pe.msg.s,"%d",task); pe.msg.t = pid; //printf("Sending %d task %d\n",pid,task); t = msgsnd ( pe.mq, &pe.msg, MSG_SIZE, 0 ); break; case REPORT: log("complete %d %s %d",pid,task_name,value); pe.complete ( task_name, value ); break; } } msgctl(pe.mq,IPC_RMID,NULL); dump_log(); return 0; }
void map_block ( int row1 ) { int current = -1; int j, r, c, dr, dc, jr, jc; int n=0; rgb_image rgb, *q; double max_north, north, min_east, east; double dt, db, dx, dy, ddx, ddy, elev; double it, ib, ix, iy, dix, diy; double scale; double row_offset, column_offset; char s[2]; int br, bc, brlim, bclim; vec3 pt, diff; img_map::iterator i; int block = -1; rgb.rows = 0; row_offset = p.dvalue("row_offset"); column_offset = p.dvalue("column_offset"); max_north = p.dvalue("northing_top"); min_east = p.dvalue("easting_left"); r = row1; brlim = r + bs; if ( brlim > img.rows ) brlim = img.rows; fill(img,dist,r,brlim); //printf("%d filled\n",getpid()); spread_dist(img,dist,r,brlim); //printf("%d spread\n",getpid()); for ( c = 0; c < img.cols; c += bs ) { for ( br = r; br < brlim; br++ ) { bclim = c + bs; if ( bclim > img.cols ) bclim = img.cols; north = max_north - br * wid; dy = (dem_north - north) / dem_wid; dr = int(dy + 0.5); ddy = dy - int(dy); if ( dr < 0 || dr >= dem.rows ) { //printf("dem north %g, wid %g; north %g, dy %g\n", //dem_north, dem_wid, north, dy ); fprintf(stderr,"Dem row %d out of range for row %d\n", dr, r ); exit(1); } for ( bc = c; bc < bclim; bc++ ) { if ( img[br][bc] < 1 ) continue; east = min_east + bc*wid; if ( img[br][bc] != current ) { current = img[br][bc]; //printf("%d %d switching to %d after %d pixels\n", //br, bc, current, n ); n = 1; rgb = imgs[current]; if ( !valid_img[current] ) { pe.wait_for ( "image", current ); valid_img[current] = 1; } rgb.used = 1; imgs[current] = rgb; } n++; dx = (east - dem_east) / dem_wid; dc = int(dx + 0.5); ddx = dx - int(dx); if ( dc < 0 || dc >= dem.cols ) { fprintf(stderr,"Dem column %d out of range for column %d\n", dc, c ); exit(1); } if ( dc == 0 || dr == 0 || dc == dem.cols-1 || dr == dem.rows-1 ) { elev = -rgb.alt + dem[dr][dc]; } else { dr = (int)dy; dc = (int)dx; dt = (1.0-ddx)*dem[dr][dc] + ddx*dem[dr][dc+1]; db = (1.0-ddx)*dem[dr+1][dc] + ddx*dem[dr+1][dc+1]; elev = -rgb.alt + (1.0-ddy)*dt + ddy*db; } pt[0] = east; pt[1] = north; pt[2] = elev; //diff = pt - rgb.cam; //printf("pt %8g %12.8g %g, cam %8g %12.8g %g, zrot %g %g %g\n", //pt[0], pt[1], pt[2], rgb.cam[0], rgb.cam[1], rgb.cam[2], //rgb.zrot[0], rgb.zrot[1], rgb.zrot[2] ); //dist = diff.length(); //dist = diff * rgb.zrot; //scale = rgb.alt / dist; //printf("%d %d %d %d, cam %g, elev %g, dist %gm scale %g\n", //br,bc,dr,dc, rgb.cam.n[2], elev, dist, scale); //pt = scale * pt + (1-scale)*rgb.cam; pt = rgb.MI * pt; scale = -rgb.unit_depth / pt[2]; pt = scale * pt; iy = rgb.rows - 1 - (pt[1]+rgb.rows/2+row_offset); jr = int(iy+0.5); ix = int(pt[0]+rgb.cols/2+column_offset+0.5); jc = int(ix+0.5); //printf("scale %g, jr %g, jc %g, %g\n", //scale, pt[1]+600, pt[0]+800, pt[2] ); if ( jr >= 0 && jr < rgb.rows && jc >= 0 && jc < rgb.cols){ if ( jr == 0 || jc == 0 || jr == rgb.rows - 1 || jc == rgb.cols - 1 ) { out.img[0][br][bc] = rgb.img[0][jr][jc]; out.img[1][br][bc] = rgb.img[1][jr][jc]; out.img[2][br][bc] = rgb.img[2][jr][jc]; } else { jr = int(iy); jc = int(ix); diy = iy - jr; dix = ix - jc; for ( j = 0; j < 3; j++ ) { it = (1.0-dix)*rgb.img[j][jr][jc] + dix * rgb.img[j][jr][jc+1]; ib = (1.0-dix)*rgb.img[j][jr+1][jc] + dix * rgb.img[j][jr+1][jc+1]; out.img[j][br][bc] = int((1.0-diy)*it + diy*ib + 0.5); } } } } } } rgb.rows = 0; rgb.img[0].data = 0; rgb.img[1].data = 0; rgb.img[2].data = 0; }
void output_geotiff ( rgb_image & out ) { int i, r, c; int val; char s[2]; OGRSpatialReference ref; GDALDataset *df; char *wkt = NULL; GDALRasterBand *bd; double trans[6]; GDALDriver *gt; char **options = NULL; int ov[] = { 2, 4, 8, 16, 32 }; int nov; int n; int bands[] = { 1, 2, 3 }; char file[1000]; int block, ir, rows; options = CSLSetNameValue ( options, "TILED", "NO" ); options = CSLSetNameValue ( options, "BLOCKXSIZE", "256" ); options = CSLSetNameValue ( options, "BLOCKYSIZE", "256" ); options = CSLSetNameValue ( options, "COMPRESS", "LZW" ); gt = GetGDALDriverManager()->GetDriverByName("GTiff"); if ( !gt ) { fprintf(stderr,"Could not get GTiff driver\n"); exit(1); } strcpy ( file, p.value("output_file").c_str() ); df = gt->Create( file, out.cols, out.rows, 3, GDT_Byte, options ); if( df == NULL ) { fprintf(stderr,"Could not create %s\n", file ); exit(1); } trans[0] = p.dvalue("easting_left"); trans[1] = p.dvalue("output_cell_size"); trans[2] = 0.0; trans[3] = p.dvalue("northing_top"); trans[4] = 0.0; trans[5] = -p.dvalue("output_cell_size"); df->SetGeoTransform ( trans ); ref.SetUTM ( p.ivalue("utm_zone") ); ref.SetWellKnownGeogCS ( "NAD27" ); ref.exportToWkt ( &wkt ); df->SetProjection(wkt); CPLFree ( wkt ); for ( ir = 0; ir < out.rows; ir += bs ) { rows = out.rows - ir; if ( rows > bs ) rows = bs; //printf("Writer waiting for %d\n",ir ); pe.wait_for("data",ir); for ( i = 0; i < 3; i++ ) { bd = df->GetRasterBand(i+1); bd->RasterIO( GF_Write, 0, ir, out.cols, rows, out.img[i].data+ir*out.cols, out.cols, rows, GDT_Byte, 0, 0 ); } } delete df; df = (GDALDataset *)GDALOpen ( file, GA_Update ); if( df == NULL ) { fprintf(stderr,"Could not open for update %s\n", file ); exit(1); } nov = p.ivalue("overviews"); if ( nov > 5 ) nov = 5; if ( nov > 0 ) { df->BuildOverviews("NEAREST", nov, ov, 3, bands, NULL, NULL ); } }
void output_dist_geotiff ( image<float> & dist, image<unsigned char> & v ) { int r, c; int val; OGRSpatialReference ref; GDALDataset *df; char *wkt = NULL; GDALRasterBand *bd; double trans[6]; GDALDriver *gt; char **options = NULL; int ov[] = { 2, 4, 8, 16, 32 }; int nov; int n; char file[1000]; options = CSLSetNameValue ( options, "TILED", "NO" ); options = CSLSetNameValue ( options, "COMPRESS", "LZW" ); gt = GetGDALDriverManager()->GetDriverByName("GTiff"); if ( !gt ) { fprintf(stderr,"Could not get GTiff driver\n"); exit(1); } strcpy ( file, p.value("dist_file").c_str() ); df = gt->Create( file, dist.cols, dist.rows, 1, GDT_Byte, options ); if( df == NULL ) { fprintf(stderr,"Could not create %s\n", file ); exit(1); } trans[0] = p.dvalue("easting_left"); trans[1] = p.dvalue("output_cell_size"); trans[2] = 0.0; trans[3] = p.dvalue("northing_top"); trans[4] = 0.0; trans[5] = -p.dvalue("output_cell_size"); df->SetGeoTransform ( trans ); ref.SetUTM ( p.ivalue("utm_zone") ); ref.SetWellKnownGeogCS ( "NAD27" ); ref.exportToWkt ( &wkt ); df->SetProjection(wkt); CPLFree ( wkt ); for ( r=0; r < dist.rows; r++ ) { for ( c=0; c < dist.cols; c++ ) { val = int(sqrt(dist[r][c])+0.5); if ( val > 255 ) val = 255; v[r][c] = val; } } bd = df->GetRasterBand(1); bd->RasterIO( GF_Write, 0, 0, v.cols, v.rows, v.data, v.cols, v.rows, GDT_Byte, 0, 0 ); delete df; df = (GDALDataset *)GDALOpen ( file, GA_Update ); if( df == NULL ) { fprintf(stderr,"Could not open for update %s\n", file ); exit(1); } nov = p.ivalue("overviews"); if ( nov > 5 ) nov = 5; if ( nov > 0 ) { n = 1; df->BuildOverviews("NEAREST", nov, ov, 1, &n, NULL, NULL ); } }
int simple_ctable::create(int dfd, const char * file, const params & config, dtype::ctype key_type) { int ct_dfd, columns, r; params base_config; std::set<istr, strcmp_less> names; ctable_header meta; rwfile meta_file; const dtable_factory * base = dtable_factory::lookup(config, "base"); if(!base) return -ENOENT; if(!config.get("base_config", &base_config, params())) return -EINVAL; if(!config.get("columns", &columns, 0)) return -EINVAL; if(columns < 1) return -EINVAL; /* check that we have all the names */ for(int i = 0; i < columns; i++) { char string[32]; istr column_name; sprintf(string, "column%d_name", i); if(!config.get(string, &column_name) || !column_name) return -EINVAL; if(names.count(column_name)) return -EEXIST; names.insert(column_name); } names.clear(); r = mkdirat(dfd, file, 0755); if(r < 0) return r; ct_dfd = openat(dfd, file, O_RDONLY); if(ct_dfd < 0) goto fail_open; meta.magic = SIMPLE_CTABLE_MAGIC; meta.version = SIMPLE_CTABLE_VERSION; meta.columns = columns; r = meta_file.create(ct_dfd, "sct_meta"); if(r < 0) goto fail_meta; r = meta_file.append(&meta); if(r < 0) goto fail_create; /* record column names */ for(int i = 0; i < columns; i++) { uint32_t length; char string[32]; istr column_name; sprintf(string, "column%d_name", i); r = config.get(string, &column_name); assert(r && column_name); length = column_name.length(); r = meta_file.append(&length); if(r < 0) goto fail_create; r = meta_file.append(column_name); if(r < 0) goto fail_create; } r = base->create(ct_dfd, "base", base_config, key_type); if(r < 0) goto fail_create; meta_file.close(); close(ct_dfd); return 0; fail_create: meta_file.close(); unlinkat(dfd, "sct_meta", 0); fail_meta: close(ct_dfd); fail_open: unlinkat(dfd, file, AT_REMOVEDIR); return -1; }