Commit 92aa7027 authored by Jean-Benoist Leger's avatar Jean-Benoist Leger

Reading and writing from and to stdin and stdout

parent 81b8d4ab
...@@ -26,7 +26,7 @@ route_query_nodes_database: route_query_nodes_database.o nodes_db.o conv_functio ...@@ -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 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) $(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) $(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 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 ...@@ -83,5 +83,12 @@ route_create_databases_from_pbf.o: route_create_databases_from_pbf.cc
analyze_pbf.o: analyze_pbf.cc analyze_pbf.o: analyze_pbf.cc
$(CC) $(CXXFLAGS) $(INCLUDE_OSMPBF) -c $< -o $@ $(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: clean:
rm -f *.o rm -f *.o
...@@ -3,12 +3,14 @@ ...@@ -3,12 +3,14 @@
std::list<std::list<route_point_t> > get_routes( std::list<std::list<route_point_t> > get_routes(
route_db_t* route_db, route_db_t* route_db,
parameters_t & parameters, parameters_t & parameters,
std::list< std::pair<double,double> > & geos) std::list< std::pair<double,double> > & geos,
stdout_output_t stdout_output)
{ {
std::list<nid_t> nids; std::list<nid_t> nids;
route_db->lookup(geos, nids); route_db->lookup(geos, nids);
std::list<std::list<route_point_t> > ret; std::list<std::list<route_point_t> > ret;
unsigned int track = 0;
for(std::list<nid_t>::iterator nids_it = nids.begin(); for(std::list<nid_t>::iterator nids_it = nids.begin();
nids_it != nids.end(); nids_it != nids.end();
...@@ -18,13 +20,18 @@ std::list<std::list<route_point_t> > get_routes( ...@@ -18,13 +20,18 @@ std::list<std::list<route_point_t> > get_routes(
nids_it_next++; nids_it_next++;
if(nids_it_next!=nids.end()) if(nids_it_next!=nids.end())
{ {
ret.push_back( std::list<route_point_t> current_route =
get_route_between_nids( get_route_between_nids(
route_db, route_db,
parameters, parameters,
*nids_it, *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); return(ret);
} }
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "conf.h" #include "conf.h"
#include "route_db.h" #include "route_db.h"
#include "types.h" #include "types.h"
#include "write_to_stdout.h"
#include <queue> #include <queue>
std::list<route_point_t> get_route_between_nids( std::list<route_point_t> get_route_between_nids(
...@@ -15,6 +17,7 @@ std::list<route_point_t> get_route_between_nids( ...@@ -15,6 +17,7 @@ std::list<route_point_t> get_route_between_nids(
std::list<std::list<route_point_t> > get_routes( std::list<std::list<route_point_t> > get_routes(
route_db_t* route_db, route_db_t* route_db,
parameters_t & parameters, parameters_t & parameters,
std::list< std::pair<double,double> > & geos); std::list< std::pair<double,double> > & geos,
stdout_output_t stdout_output = RV_STDOUT_OUTPUT_NONE);
#endif #endif
#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<std::pair<double,double>> read_points_from_stdin()
{
std::list<std::pair<double,double>> 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<double,double>(
atof(v1.c_str()),
atof(v2.c_str())
)
);
}
return(points);
}
#ifndef H_READ_FROM_STDIN
#define H_READ_FROM_STDIN 1
#include "conf.h"
#include "types.h"
#include <list>
#include <string>
#include <iostream>
parameters_t read_parameters_from_stdin();
std::list<std::pair<double,double>> read_points_from_stdin();
#endif
#include "get_route.h" #include "get_route.h"
#include "read_from_stdin.h"
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
// if(argc<4) if(argc<2)
// abort(); abort();
route_db_t route_db("/home/rv/tmpfs/route_db"); route_db_t route_db(argv[1]);
parameters_t parameters; parameters_t parameters = read_parameters_from_stdin();
parameters.mass = 90; std::list<std::pair<double,double>> geos = read_points_from_stdin();
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;
//std::pair<double,double> ptA(-78.39191437,40.51849719); get_routes(&route_db, parameters, geos, RV_STDOUT_OUTPUT_TEXT);
//std::pair<double,double> ptB(-77.86422729,40.79197912);
// std::pair<double,double> ptA(-76.79992676,40.88444794);
// std::pair<double,double> ptB(-79.96948242,40.43858587);
std::pair<double,double> ptA(atof(argv[1]),atof(argv[2]));
std::pair<double,double> ptB(atof(argv[3]),atof(argv[4]));
std::list<std::pair<double,double> > geos;
geos.push_back(ptA);
geos.push_back(ptB);
std::list<std::list<route_point_t> > res = get_routes(&route_db, parameters, geos);
unsigned int track=0;
for(std::list<std::list<route_point_t> >::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<route_point_t> & l = *res_it;
for(std::list<route_point_t>::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++;
}
return(0); return(0);
} }
......
...@@ -54,6 +54,14 @@ struct node_info_t ...@@ -54,6 +54,14 @@ struct node_info_t
typedef struct node_info_t 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 enum criterion_t
{ {
RV_CRITERION_ENERGY, RV_CRITERION_ENERGY,
......
#include "write_to_stdout.h"
void write_to_stdout(
route_db_t* route_db,
unsigned int track,
std::list<route_point_t> 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);
}
#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<route_point_t> points);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment