Implement (automatic) node IDs

This commit is contained in:
Markus Koch 2020-05-02 16:59:13 +02:00
parent 0d2945d76f
commit f3e5582af0
2 changed files with 15 additions and 15 deletions

View File

@ -24,7 +24,8 @@ int dijkstra_node_same (struct dijkstra_node *node_a,
* \return a new dijkstra node or NULL. Needs to be freed manually. * \return a new dijkstra node or NULL. Needs to be freed manually.
*/ */
struct dijkstra_node *dijkstra_node_new(float x, struct dijkstra_node *dijkstra_node_new(float x,
float y) float y,
int uid)
{ {
struct dijkstra_node *node; struct dijkstra_node *node;
@ -32,6 +33,7 @@ struct dijkstra_node *dijkstra_node_new(float x,
if (node) { if (node) {
node->position.x = x; node->position.x = x;
node->position.y = y; node->position.y = y;
node->uid = uid;
node->paths = NULL; node->paths = NULL;
} }
@ -64,25 +66,23 @@ struct dijkstra_node *dijkstra_node_exists(GList *list,
*/ */
struct dijkstra_node *dijkstra_node_new_to_list(GList **list, struct dijkstra_node *dijkstra_node_new_to_list(GList **list,
float x, float x,
float y, float y)
int *is_new)
{ {
struct dijkstra_node *node; struct dijkstra_node *node;
struct dijkstra_node *found; struct dijkstra_node *found;
int uid;
node = dijkstra_node_new(x, y); uid = g_list_length(*list);
node = dijkstra_node_new(x, y, uid);
if (!node) if (!node)
return NULL; return NULL;
if ((found = dijkstra_node_exists(*list, node))) { if ((found = dijkstra_node_exists(*list, node))) {
free(node); free(node);
node = found; node = found;
if (is_new)
*is_new = 0;
} else { } else {
*list = g_list_append(*list, node); *list = g_list_append(*list, node);
if (is_new)
*is_new = 1;
} }
return node; return node;
@ -234,7 +234,7 @@ int dijkstra_path_intersect(struct dijkstra_solver *solver,
i_x = p0_x + (t * s1_x); i_x = p0_x + (t * s1_x);
i_y = p0_y + (t * s1_y); i_y = p0_y + (t * s1_y);
node_new = dijkstra_node_new_to_list(&solver->nodes, i_x, i_y, &is_new); node_new = dijkstra_node_new_to_list(&solver->nodes, i_x, i_y);
if (node_new != path_a->source && node_new != path_a->destination) { if (node_new != path_a->source && node_new != path_a->destination) {
path_new = dijkstra_path_dup_shallow(path_a); path_new = dijkstra_path_dup_shallow(path_a);
@ -272,8 +272,8 @@ struct dijkstra_path *dijkstra_path_new_to_list(struct dijkstra_solver *solver,
struct dijkstra_path *existing_path; struct dijkstra_path *existing_path;
GList *l, *l_last; GList *l, *l_last;
n1 = dijkstra_node_new_to_list(&solver->nodes, x1, y1, NULL); n1 = dijkstra_node_new_to_list(&solver->nodes, x1, y1);
n2 = dijkstra_node_new_to_list(&solver->nodes, x2, y2, NULL); n2 = dijkstra_node_new_to_list(&solver->nodes, x2, y2);
path = dijkstra_path_new(name, n1, n2, weight); path = dijkstra_path_new(name, n1, n2, weight);
l_last = g_list_last(solver->paths); l_last = g_list_last(solver->paths);

View File

@ -86,15 +86,15 @@ int main(int argc, char *argv[])
if (should_report(LL_NOISY)) { if (should_report(LL_NOISY)) {
for (l = solver->nodes; l != NULL; l = l->next) { for (l = solver->nodes; l != NULL; l = l->next) {
node = (struct dijkstra_node*) l->data; node = (struct dijkstra_node*) l->data;
printf("Node %p: %f,\t%f [%d paths]\n", node, node->position.x, node->position.y, g_list_length(node->paths)); 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"); printf("\n");
for (l = solver->paths; l != NULL; l = l->next) { for (l = solver->paths; l != NULL; l = l->next) {
path = (struct dijkstra_path*) l->data; path = (struct dijkstra_path*) l->data;
printf("Path %p: %p (%f,%f) -> %p (%f,%f) \t\"%s\"\n", path, printf("Path %p: %5d (%f,%f) -> %5d (%f,%f) \t\"%s\"\n", path,
path->source, path->source->position.x, path->source->position.y, path->source->uid, path->source->position.x, path->source->position.y,
path->destination, path->destination->position.x, path->destination->position.y, path->destination->uid, path->destination->position.x, path->destination->position.y,
path->name); path->name);
} }
printf("\n"); printf("\n");