]> mj.ucw.cz Git - leo.git/commitdiff
Průvodce: Identifying endpoints
authorMartin Mares <mj@ucw.cz>
Mon, 14 Mar 2022 15:11:38 +0000 (16:11 +0100)
committerMartin Mares <mj@ucw.cz>
Mon, 14 Mar 2022 15:11:38 +0000 (16:11 +0100)
graph.c

diff --git a/graph.c b/graph.c
index 2b1c36a6bb53bc04e3358688f861b68d2b9fdfc9..f82f06a839c1828c3dd8df780a7c5b0e09d55399 100644 (file)
--- a/graph.c
+++ b/graph.c
@@ -4,6 +4,8 @@
  *     (c) 2022 Martin Mares <mj@ucw.cz>
  */
 
+#include "leo.h"
+
 #include <ucw/lib.h>
 #include <ucw/clists.h>
 #include <ucw/mempool.h>
@@ -11,7 +13,6 @@
 #include <stdio.h>
 #include <math.h>
 
-#include "leo.h"
 #include "osm.h"
 #include "map.h"
 #include "graph.h"
@@ -50,6 +51,16 @@ static struct graph_vertex *graph_lookup_vertex(struct osm_node *n)
   return n->vertex;
 }
 
+static struct graph_vertex *graph_vertex_by_node_id(osm_id_t id)
+{
+  struct osm_node *n = (struct osm_node *) osm_obj_find_by_id(OSM_TYPE_NODE, id);
+  if (!n)
+    die("Cannot find node #%jd", (uintmax_t) id);
+  ASSERT(n->vertex);
+  ASSERT(n->vertex->node);
+  return n->vertex;
+}
+
 static struct graph_edge *graph_add_edge(struct graph_vertex *u, struct graph_vertex *v, struct osm_way *way, double length)
 {
   struct graph_edge *e1 = mp_alloc_zero(graph_pool, sizeof(*e1));
@@ -141,14 +152,23 @@ void graph_build(void)
   graph_pool = mp_new(65536);
   clist_init(&graph_vertices);
 
-  CLIST_FOR_EACH(struct data_source *, ds, map_sources)
-    CLIST_FOR_EACH(struct osm_way *, w, ds->osm->obj_list[OSM_TYPE_WAY])
-      {
-       if (way_is_edge(w))
-         way_add_edge(w);
-      }
+  // We are considering only the first data source for the graph.
+  // Otherwise, things start becoming ugly, because node IDs are generally not unique.
+
+  struct data_source *ds = clist_head(&map_sources);
+  ASSERT(ds);
+  osm_this = ds->osm;
+  CLIST_FOR_EACH(struct osm_way *, w, osm_this->obj_list[OSM_TYPE_WAY])
+    {
+      if (way_is_edge(w))
+       way_add_edge(w);
+    }
   msg(L_INFO, "Built road graph: %u vertices, %u edges", num_vertices, num_edges);
 
   graph_optimize();
   msg(L_INFO, "Optimized road graph: %u vertices, %u edges", num_vertices, num_edges);
+
+  struct graph_vertex *v1 = graph_vertex_by_node_id(32292698); // Praha-Zbraslav, K Přehradám
+  struct graph_vertex *v2 = graph_vertex_by_node_id(21289321); // Brno, Heršpická x Opuštěná
+  msg(L_INFO, "Finding path from #%jd to #%jd", (uintmax_t) v1->node->o.id, (uintmax_t) v2->node->o.id);
 }