Skip to content

Commit

Permalink
feat: added visualization tool
Browse files Browse the repository at this point in the history
thiago-rezende committed May 13, 2022
1 parent 68484a9 commit 211ac7f
Showing 14 changed files with 318 additions and 29 deletions.
Binary file modified .github/graph_path.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/visualization.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 30 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -19,9 +19,29 @@ This project was developed as a solution for an assignment in the subject of art

> **Green**: start
>
> **Yellow**: path
> **Orange**: path
>
> **Orange**: target
> **Red**: target
## Visualization
> Graphical visualization of the [A*][astar-url] in action
<img src=".github/visualization.gif" width="700"/>

> **Green**: start
>
> **Orange**: path
>
> **Gray**: visited
>
> **Red**: target
## Dependencies
> [A*][astar-url] algorithm
- [GLib][glib-url]

> Visualization
- [RayLib][raylib-url]

## Build Instructions
> Configure
@@ -34,16 +54,23 @@ This project was developed as a solution for an assignment in the subject of art
ninja -C build
```

> Run
> Run **[simple]**
```sh
./build/hstar
```

> Run **[visualization]**
```sh
./build/visualization
```

<!-- Links -->
[license-url]: https://opensource.org/licenses/BSD-3-Clause
[c-url]: https://en.cppreference.com/w/c
[meson-url]: https://mesonbuild.com/
[astar-url]: https://en.wikipedia.org/wiki/A*_search_algorithm
[glib-url]: https://gitlab.gnome.org/GNOME/glib
[raylib-url]: https://github.com/raysan5/raylib

<!-- Badges -->
[license-badge]: https://img.shields.io/badge/license-BSD_3_Clause-blue.svg?style=flat-square
6 changes: 3 additions & 3 deletions include/horus/astar.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* A* algorithm
*
* @file astar.h
* @brief A* algorithm
* @maintainer Thiago Rezende <[email protected]>
* @file astar.h
* @brief A* algorithm
* @author Thiago Rezende ([email protected])
*/

#ifndef HORUS_ASTAR_H
6 changes: 3 additions & 3 deletions include/horus/core/types.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Base types naming shorthand
*
* @file types.h
* @brief Short type naming as useful memory size macros
* @maintainer Thiago Rezende <[email protected]>
* @file types.h
* @brief Short type naming as useful memory size macros
* @author Thiago Rezende ([email protected])
*/

#ifndef HORUS_CORE_TYPES_H
6 changes: 3 additions & 3 deletions include/horus/horus.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Horus A*
*
* @file horus.h
* @brief Central include file for the usage of the A* algorithm
* @maintainer Thiago Rezende <[email protected]>
* @file horus.h
* @brief Central include file for the usage of the A* algorithm
* @author Thiago Rezende ([email protected])
*/

#ifndef HORUS_H
13 changes: 10 additions & 3 deletions include/horus/node.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/**
* Node structure
*
* @file node.h
* @brief Node struture used in a graph for the A* algorithm
* @maintainer Thiago Rezende <[email protected]>
* @file node.h
* @brief Node struture used in a graph for the A* algorithm
* @author Thiago Rezende ([email protected])
*/

#ifndef HORUS_NODE_H
#define HORUS_NODE_H

#include <math.h>
#include <stddef.h>
#include <stdbool.h>

#include <horus/core/types.h>

@@ -30,6 +31,12 @@ typedef struct node
f64 g;
f64 h;

/* Obstacle */
bool obstacle;

/* Visited */
bool visited;

/* parent in found path */
struct node *parent;

6 changes: 3 additions & 3 deletions include/horus/platform/detection.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @file detection.h
* @brief Platorm detection using predefined macros
* @maintainer Thiago Rezende <[email protected]>
* @file detection.h
* @brief Platorm detection using predefined macros
* @author Thiago Rezende ([email protected])
*
* This header will provide some macros to identify the platform.
* This can be used for compile time restrictions or for platform specific code
6 changes: 0 additions & 6 deletions include/horus/utils.h

This file was deleted.

25 changes: 24 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project('HStar', 'c',
version : '0.1.0',
version : '1.0.0',
license : 'MIT',
default_options : ['c_std=c11',
'warning_level=3'])
@@ -26,3 +26,26 @@ hstar_exe = executable('hstar', hstar_src,

test('hstar', hstar_exe)

# Visualization
if get_option('build_visualization')

raylib_dep = dependency('raylib')

visualization_src = [
'src/visualization.c',
'src/node.c',
'src/astar.c'
]

visualization_inc = [
'include'
]

visualization_exe = executable('visualization', visualization_src,
dependencies : [glib_dep, m_dep, raylib_dep],
include_directories : visualization_inc,
install : true)

test('visualization', visualization_exe)

endif
2 changes: 2 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
option('build_visualization', type : 'boolean', value : true)

10 changes: 8 additions & 2 deletions src/astar.c
Original file line number Diff line number Diff line change
@@ -17,15 +17,18 @@ node_t *astar(node_t *start, node_t *target)
node_t *node = (node_t *)g_queue_peek_head(open_list);

/* return the first found path */
// if (node == target)
// return node;
if (node == target)
return node;

printf("[expanding] <p:%p> <x:%.2f, y:%.2f> <f:%.2f> <g:%.2f> <h:%.2f>\n", (void *)node, node->x, node->y, node->f, node->g, node->h);

for (size_t i = 0; i < node->neighbors->len; i++)
{
node_t *neighbor = node->neighbors->pdata[i];

if (neighbor->obstacle)
continue;

f64 total_cost = node->g + node_distance(node, neighbor);

if (!g_queue_find(closed_list, neighbor) && !g_queue_find(open_list, neighbor))
@@ -34,6 +37,7 @@ node_t *astar(node_t *start, node_t *target)
neighbor->g = total_cost;
neighbor->h = node_distance(neighbor, target);
neighbor->f = neighbor->g + neighbor->h;
// neighbor->visited = true;

g_queue_insert_sorted(open_list, neighbor, node_compare, NULL);
}
@@ -59,6 +63,8 @@ node_t *astar(node_t *start, node_t *target)

printf("\n");

node->visited = true;

g_queue_remove(open_list, node);
g_queue_insert_sorted(closed_list, node, node_compare, NULL);
}
8 changes: 6 additions & 2 deletions src/node.c
Original file line number Diff line number Diff line change
@@ -6,13 +6,17 @@ node_t *node_new(f64 x, f64 y)
{
node_t *node = (node_t *)malloc(sizeof(node_t));

node->f = max_f64; /* infinity */
node->g = max_f64; /* infinity */
node->f = 0.0;
node->g = 0.0;
node->h = 0.0;

node->x = x;
node->y = y;

node->obstacle = false;

node->visited = false;

node->parent = NULL;

node->neighbors = g_ptr_array_new();
Loading

0 comments on commit 211ac7f

Please sign in to comment.