#include #include #include #include #include "dijkstragraph.h" #include "dijkstrasearch.h" #include "geojson.h" #include "logging.h" struct dijkstra_node *start; struct dijkstra_node *dest; void add_test_map(struct dijkstra_solver *solver) { dijkstra_path_new_to_list(solver, 5,0, 5,10, "Vertical", 1); dijkstra_path_new_to_list(solver, 0,5, 10,5, "Horizontal", 1); } void help(const char *exec) { fprintf(stderr, "Usage: %s [OPTION] [FILE]...\n" "Start the lifo-dijkstraserv routing server.\n\n" " -s\tset output to silent\n" " -v\tincrease verbosity. Maybe used multiple times.\n" " -p\tport number (default 6802)\n", exec); } int main(int argc, char *argv[]) { int opt; int i; struct dijkstra_solver *solver; GList *l = NULL; struct dijkstra_node *node; struct dijkstra_path *path; struct dijkstra_search *search; int iterations; /* Init stuff */ set_log_level(LL_DEBUG); /* Process arguments */ while((opt = getopt(argc, argv, "vs")) != -1) { switch(opt) { case 'v': set_log_level(get_log_level() + 1); break; case 's': set_log_level(0); break; case ':': case '?': help(argv[0]); return 1; default: break; } } if (optind == argc) { help(argv[0]); return 1; } /* Generate solver */ solver = dijkstra_solver_new(); if (!solver) { report(LL_CRITICAL, "Memory allocation failure."); return 2; } /* Load map data -> Generate graph */ for (i = optind; i < argc; ++i) { add_geojson_to_dijkstra(solver, argv[i]); } // add_test_map(solver); // TODO: This start / end coordinates are only for testing start = dijkstra_node_new_to_list(&solver->nodes, -1213, -305); dest = dijkstra_node_new_to_list(&solver->nodes, 388, 99); if (should_report(LL_NOISY)) { for (l = solver->nodes; l != NULL; l = l->next) { node = (struct dijkstra_node*) l->data; printf("Node %5d @%p: %f,\t%f [%d paths]\n", node->uid, node, node->position.x, node->position.y, g_list_length(node->paths)); } printf("\n"); for (l = solver->paths; l != NULL; l = l->next) { path = (struct dijkstra_path*) l->data; printf("Path %p: %5d (%f,%f) -> %5d (%f,%f) \t\"%s\"\n", path, path->source->uid, path->source->position.x, path->source->position.y, path->destination->uid, path->destination->position.x, path->destination->position.y, path->name); } printf("\n"); } report(LL_INFO, "Created graph with %d nodes and %d paths.", g_list_length(solver->nodes), g_list_length(solver->paths)); /* Perform search */ search = dijkstra_search_new(solver); if (!search) { report(LL_ERROR, "Error allocating dijkstra_search"); dijkstra_solver_free(solver); return -3; } dijkstra_search_set_start(search, start); if ((iterations = dijkstra_search_find_path(search, dest)) > 0) { report(LL_INFO, "Found path after %d iterations.", iterations); dijkstra_print_path(search, dest); } else { report(LL_WARNING, "Couldn't find path."); } dijkstra_search_free(search); dijkstra_solver_free(solver); return 0; }