From 92aa7027ef3aa71e75f6da7ba42c529425d05e35 Mon Sep 17 00:00:00 2001 From: Jean-Benoist Leger Date: Thu, 15 Jan 2015 01:28:41 +0100 Subject: [PATCH] Reading and writing from and to stdin and stdout --- src/Makefile | 9 +++- src/get_route.cc | 13 +++-- src/get_route.h | 5 +- src/read_from_stdin.cc | 115 +++++++++++++++++++++++++++++++++++++++++ src/read_from_stdin.h | 13 +++++ src/route.cc | 67 +++--------------------- src/types.h | 8 +++ src/write_to_stdout.cc | 32 ++++++++++++ src/write_to_stdout.h | 14 +++++ 9 files changed, 211 insertions(+), 65 deletions(-) create mode 100644 src/read_from_stdin.cc create mode 100644 src/read_from_stdin.h create mode 100644 src/write_to_stdout.cc create mode 100644 src/write_to_stdout.h diff --git a/src/Makefile b/src/Makefile index de33fae..e787f8b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -26,7 +26,7 @@ route_query_nodes_database: route_query_nodes_database.o nodes_db.o conv_functio route_query_lookup_database: route_query_lookup_database.o lookup.o functions.o nodes_db.o lookup_db.o conv_functions.o $(CC) $(LDFLAGS) -o $@ $+ $(LDLIBS_LMDB) -route: route.o route_db.o lookup.o functions.o nodes_db.o lookup_db.o conv_functions.o get_route.o +route: route.o route_db.o lookup.o functions.o nodes_db.o lookup_db.o conv_functions.o get_route.o read_from_stdin.o write_to_stdout.o $(CC) $(LDFLAGS) -o $@ $+ $(LDLIBS_LMDB) route_create_databases_from_pbf: route_create_databases_from_pbf.o lookup_db.o nodes_db.o elevation.o parse_way.o conv_functions.o @@ -83,5 +83,12 @@ route_create_databases_from_pbf.o: route_create_databases_from_pbf.cc analyze_pbf.o: analyze_pbf.cc $(CC) $(CXXFLAGS) $(INCLUDE_OSMPBF) -c $< -o $@ +read_from_stdin.o: read_from_stdin.cc conf.h types.h + $(CC) $(CXXFLAGS) -c $< -o $@ + +write_to_stdout.o: write_to_stdout.cc conf.h types.h route_db.h + $(CC) $(CXXFLAGS) -c $< -o $@ + + clean: rm -f *.o diff --git a/src/get_route.cc b/src/get_route.cc index 7f8ed6f..1cc7afa 100644 --- a/src/get_route.cc +++ b/src/get_route.cc @@ -3,12 +3,14 @@ std::list > get_routes( route_db_t* route_db, parameters_t & parameters, - std::list< std::pair > & geos) + std::list< std::pair > & geos, + stdout_output_t stdout_output) { std::list nids; route_db->lookup(geos, nids); std::list > ret; + unsigned int track = 0; for(std::list::iterator nids_it = nids.begin(); nids_it != nids.end(); @@ -18,13 +20,18 @@ std::list > get_routes( nids_it_next++; if(nids_it_next!=nids.end()) { - ret.push_back( + std::list current_route = get_route_between_nids( route_db, parameters, *nids_it, - *nids_it_next)); + *nids_it_next); + ret.push_back(current_route); + + if(stdout_output == RV_STDOUT_OUTPUT_TEXT) + write_to_stdout(route_db, track, current_route); } + track++; } return(ret); } diff --git a/src/get_route.h b/src/get_route.h index a709c6c..fb2f963 100644 --- a/src/get_route.h +++ b/src/get_route.h @@ -4,6 +4,8 @@ #include "conf.h" #include "route_db.h" #include "types.h" +#include "write_to_stdout.h" + #include std::list get_route_between_nids( @@ -15,6 +17,7 @@ std::list get_route_between_nids( std::list > get_routes( route_db_t* route_db, parameters_t & parameters, - std::list< std::pair > & geos); + std::list< std::pair > & geos, + stdout_output_t stdout_output = RV_STDOUT_OUTPUT_NONE); #endif diff --git a/src/read_from_stdin.cc b/src/read_from_stdin.cc new file mode 100644 index 0000000..3fb0707 --- /dev/null +++ b/src/read_from_stdin.cc @@ -0,0 +1,115 @@ +#include "read_from_stdin.h" + +parameters_t read_parameters_from_stdin() +{ + parameters_t parameters; + + // default values + parameters.mass = 90; + parameters.power = 140; + parameters.SCx = .45; + parameters.Cr = .008; + parameters.velocity_nopower = 9.7; + parameters.velocity_brake = 13.9; + parameters.velocity_equilibrium = 1.4; + parameters.criterion = RV_CRITERION_ENERGY; + parameters.power_walk = 140; + parameters.lateral_acceleration = .5; + parameters.walk_penalty = 1; + parameters.Cw = .03; + + std::string line; + + while(true) + { + std::getline(std::cin, line); + + unsigned int k = line.find(' '); + std::string key = line.substr(0,k); + std::string val = line.substr(k+1,std::string::npos); + + if(key == "mass") + parameters.mass = atof(val.c_str()); + else if(key == "power") + parameters.power = atof(val.c_str()); + else if(key == "SCx") + parameters.SCx = atof(val.c_str()); + else if(key == "Cr") + parameters.Cr = atof(val.c_str()); + else if(key == "velocity_nopower") + parameters.velocity_nopower = atof(val.c_str()); + else if(key == "velocity_brake") + parameters.velocity_brake = atof(val.c_str()); + else if(key == "velocity_equilibrium") + parameters.velocity_equilibrium = atof(val.c_str()); + else if(key == "power_walk") + parameters.power_walk = atof(val.c_str()); + else if(key == "lateral_acceleration") + parameters.lateral_acceleration = atof(val.c_str()); + else if(key == "walk_penalty") + parameters.walk_penalty = atof(val.c_str()); + else if(key == "Cw") + parameters.Cw = atof(val.c_str()); + else if(key == "criterion") + { + if(val == "energy") + parameters.criterion = RV_CRITERION_ENERGY; + else if(val == "energy_d") + parameters.criterion = RV_CRITERION_ENERGY_D; + else if(val == "time") + parameters.criterion = RV_CRITERION_TIME; + else if(val == "time_d") + parameters.criterion = RV_CRITERION_TIME_D; + else if(val == "distance") + parameters.criterion = RV_CRITERION_DISTANCE; + else + { + fprintf(stderr,"Unknown criterion [%s]\n",val.c_str()); + abort(); + } + } + else if(key == "points") + break; + else + { + fprintf(stderr,"Unknown parameter [%s]\n",key.c_str()); + abort(); + } + + } + + return parameters; +} + +std::list> read_points_from_stdin() +{ + std::list> points; + std::string line; + + while(true) + { + std::getline(std::cin, line); + + unsigned int k = line.find(' '); + std::string v1 = line.substr(0,k); + std::string v2 = line.substr(k+1,std::string::npos); + + if(v1 == "route") + break; + + points.push_back( + std::pair( + atof(v1.c_str()), + atof(v2.c_str()) + ) + ); + + } + return(points); +} + + + + + + diff --git a/src/read_from_stdin.h b/src/read_from_stdin.h new file mode 100644 index 0000000..112d44a --- /dev/null +++ b/src/read_from_stdin.h @@ -0,0 +1,13 @@ +#ifndef H_READ_FROM_STDIN +#define H_READ_FROM_STDIN 1 + +#include "conf.h" +#include "types.h" +#include +#include +#include + +parameters_t read_parameters_from_stdin(); +std::list> read_points_from_stdin(); + +#endif diff --git a/src/route.cc b/src/route.cc index 251bd7d..d06e75b 100644 --- a/src/route.cc +++ b/src/route.cc @@ -1,69 +1,16 @@ #include "get_route.h" +#include "read_from_stdin.h" int main(int argc, char** argv) { -// if(argc<4) -// abort(); - route_db_t route_db("/home/rv/tmpfs/route_db"); + if(argc<2) + abort(); + route_db_t route_db(argv[1]); - parameters_t parameters; - parameters.mass = 90; - parameters.power = 140; - parameters.SCx = .45; - parameters.Cr = .008; - parameters.velocity_nopower = 9.7; - parameters.velocity_brake = 13.9; - parameters.velocity_equilibrium = 1.4; - parameters.criterion = RV_CRITERION_ENERGY_APPROX; - parameters.power_walk = 140; - parameters.lateral_acceleration = .5; - parameters.walk_penalty = 1; - parameters.Cw = .03; + parameters_t parameters = read_parameters_from_stdin(); + std::list> geos = read_points_from_stdin(); - //std::pair ptA(-78.39191437,40.51849719); - //std::pair ptB(-77.86422729,40.79197912); -// std::pair ptA(-76.79992676,40.88444794); -// std::pair ptB(-79.96948242,40.43858587); - std::pair ptA(atof(argv[1]),atof(argv[2])); - std::pair ptB(atof(argv[3]),atof(argv[4])); - - std::list > geos; - geos.push_back(ptA); - geos.push_back(ptB); - - std::list > res = get_routes(&route_db, parameters, geos); - - unsigned int track=0; - for(std::list >::iterator res_it = res.begin(); - res_it!=res.end(); - res_it++) - { -// printf("# track %u\n", track); - -// printf("#nid \tlongitud\tlatitude\televatio\tvelocity\tdistance\tenergy \ttime \twalk\n"); - - std::list & l = *res_it; - - for(std::list::iterator l_it = l.begin(); - l_it!=l.end(); - l_it++) - { - nid_t nid = l_it->nid; - node_info_t* pni = route_db.get_node(nid); - printf("%8lu\t%8f\t%8f\t%8f\t%8f\t%8f\t%8f\t%8f\t%8f\t%s\n", - nid, - pni->fixed->lon, - pni->fixed->lat, - pni->fixed->elevation, - l_it->velocity, - l_it->distance, - l_it->energy, - l_it->power, - l_it->time, - (l_it->walk)?"1":"0"); - } - track++; - } + get_routes(&route_db, parameters, geos, RV_STDOUT_OUTPUT_TEXT); return(0); } diff --git a/src/types.h b/src/types.h index e79be2e..734fb97 100644 --- a/src/types.h +++ b/src/types.h @@ -54,6 +54,14 @@ struct node_info_t typedef struct node_info_t node_info_t; +enum stdout_output_t +{ + RV_STDOUT_OUTPUT_NONE, + RV_STDOUT_OUTPUT_TEXT +}; + +typedef enum stdout_output_t stdout_output_t; + enum criterion_t { RV_CRITERION_ENERGY, diff --git a/src/write_to_stdout.cc b/src/write_to_stdout.cc new file mode 100644 index 0000000..ecbad3d --- /dev/null +++ b/src/write_to_stdout.cc @@ -0,0 +1,32 @@ +#include "write_to_stdout.h" + +void write_to_stdout( + route_db_t* route_db, + unsigned int track, + std::list points) +{ + fprintf(stdout,"begin track %u\n",track); + + for(auto points_it = points.begin(); + points_it != points.end(); + points_it++) + { + node_info_t* pni = route_db->get_node(points_it->nid); + + fprintf(stdout,"nid:%lu\t",points_it->nid); + fprintf(stdout,"lon:%.6f\t",pni->fixed->lon); + fprintf(stdout,"lat:%.6f\t",pni->fixed->lat); + fprintf(stdout,"elevation:%.3f\t",pni->fixed->elevation); + fprintf(stdout,"velocity:%.3f\t",points_it->velocity); + fprintf(stdout,"distance:%.3f\t",points_it->distance); + fprintf(stdout,"energy:%.3f\t",points_it->energy); + fprintf(stdout,"time:%.3f\t",points_it->time); + fprintf(stdout,"power:%.3f\t",points_it->power); + fprintf(stdout,"walk:%s\t",points_it->walk?"yes":"no"); + fprintf(stdout,"\n"); + } + + fprintf(stdout,"end track %u\n",track); + +} + diff --git a/src/write_to_stdout.h b/src/write_to_stdout.h new file mode 100644 index 0000000..749110e --- /dev/null +++ b/src/write_to_stdout.h @@ -0,0 +1,14 @@ +#ifndef H_WRITE_TO_STDOUT +#define H_WRITE_TO_STDOUT 1 + +#include "conf.h" +#include "types.h" +#include "route_db.h" + +void write_to_stdout( + route_db_t* route_db, + unsigned int track, + std::list points); + + +#endif -- GitLab