Skip to content

Commit

Permalink
Decouple visualizer from quad tree
Browse files Browse the repository at this point in the history
  • Loading branch information
leonmavr committed Sep 19, 2024
1 parent 396c2d8 commit b9fbf06
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
17 changes: 7 additions & 10 deletions include/quad.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define QUAD_H

#include "stdbool.h"
#include <stdlib.h> // abs
#include <stddef.h> // size_t
#define IND_NW 0
#define IND_NE 1
Expand All @@ -18,20 +17,18 @@ typedef struct point_t {
} point_t;

typedef struct rect_t {
int x0;
int y0;
int x1;
int y1;
int x0, y0;
int x1, y1;
} rect_t;

typedef struct node_t {
rect_t boundary;
point_t *points;
int count;
struct node_t *nw; // Northwest
struct node_t *ne; // Northeast
struct node_t *sw; // Southwest
struct node_t *se; // Southeast
struct node_t *nw; // Northwest child
struct node_t *ne; // Northeast child
struct node_t *sw; // Southwest child
struct node_t *se; // Southeast child
} node_t;

typedef struct quadtree_t {
Expand All @@ -42,7 +39,7 @@ typedef struct quadtree_t {
void qtree_new(quadtree_t* qtree, rect_t* boundary);
void qtree_del(quadtree_t* qtree);
void qtree_insert(quadtree_t* qtree, point_t* point);
void qtree_graph(quadtree_t* qtree);
extern void (*qtree_graph)(quadtree_t*);
void qtree_query(quadtree_t* qtree, rect_t* search_area, int* count);
double qtree_nearest_neighbor(quadtree_t* qtree, point_t* query, point_t* nearest);
void qtree_update_point(quadtree_t* qtree, point_t* old_point, point_t* new_point);
Expand Down
1 change: 1 addition & 0 deletions include/viz.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ void viz_flush();
void viz_write_rect(rect_t* rect);
void viz_write_point(point_t* point);
void viz_close();
void viz_qtree_graph(quadtree_t* qtree) ;

#endif // VIZ_H
2 changes: 2 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ int main() {
particle_t particles[NPARTICLES];
quadtree_t qtree;
viz_init(boundary.x1, boundary.y1);
// to graph the particles
void (*qtree_graph)(quadtree_t*) = &viz_qtree_graph;
for (int i = 0; i < NPARTICLES; ++i) {
point_t p = {random() % boundary.x1, random() % boundary.y1, ids++};
particles[i].point = p;
Expand Down
30 changes: 4 additions & 26 deletions src/quad.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "quad.h"
#include "viz.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Expand All @@ -8,6 +7,7 @@
#include <string.h>
#include <unistd.h>
#include <float.h> // DBL_MAX
#include <stddef.h> // size_t

#define MAX(a, b) ((a) > (b) ? (a) : (b))

Expand All @@ -24,8 +24,6 @@ static void node_nearest_neighbor(node_t* node, point_t* query, point_t* nearest
static void node_remove_point(node_t* node, point_t* point);
static void node_merge(node_t* node);
static void node_del_all(node_t* node);
static void node_graph(node_t* node);



node_t* node_new(rect_t* boundary) {
Expand Down Expand Up @@ -127,7 +125,8 @@ else if ((rect->x0 <= x) && (x < rect->x0 + w/2) && (rect->y0 + h/2 <= y) && (y
}

static bool node_is_leaf(node_t* node) {
return (node != NULL) ? (node->nw == NULL && node->ne == NULL && node->se == NULL && node->sw == NULL) : false;
return (node != NULL) ? (node->nw == NULL && node->ne == NULL &&
node->se == NULL && node->sw == NULL) : false;
}

static void node_insert(node_t* node, point_t* point) {
Expand Down Expand Up @@ -279,6 +278,7 @@ static void node_merge(node_t* node) {
for (int j = 0; j < children[i]->count; ++j) {
node->points[ipoint].x = children[i]->points[j].x;
node->points[ipoint].y = children[i]->points[j].y;
node->points[ipoint].id = children[i]->points[j].id;
ipoint++;
(node->count)++;
}
Expand Down Expand Up @@ -323,25 +323,3 @@ void qtree_del(quadtree_t* qtree) {
}


static void node_graph(node_t* node) {
if (node == NULL) return;

// If it's a leaf node, print the boundary and the points in it
if (node_is_leaf(node)) {
viz_write_rect(&node->boundary);
// Print all points in this leaf
for (int i = 0; i < node->count; ++i) {
viz_write_point(&node->points[i]);
}
} else {
// Recursively visit all children (if not a leaf)
node_graph(node->nw);
node_graph(node->ne);
node_graph(node->sw);
node_graph(node->se);
}
}

void qtree_graph(quadtree_t* qtree) {
node_graph(qtree->root);
}
25 changes: 25 additions & 0 deletions src/viz.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,28 @@ void viz_write_point(point_t* point) {
void viz_close() {
pclose(_plot_pipe);
}

static bool _node_is_leaf(node_t* node) {
return (node != NULL) ? (node->nw == NULL && node->ne == NULL &&
node->se == NULL && node->sw == NULL) : false;
}

static void _node_graph(node_t* node) {
if (node == NULL) return;
if (_node_is_leaf(node)) {
viz_write_rect(&node->boundary);
for (int i = 0; i < node->count; ++i) {
viz_write_point(&node->points[i]);
}
} else {
_node_graph(node->nw);
_node_graph(node->ne);
_node_graph(node->sw);
_node_graph(node->se);
}
}

void viz_qtree_graph(quadtree_t* qtree) {
_node_graph(qtree->root);
}

0 comments on commit b9fbf06

Please sign in to comment.