diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2018-09-13 16:03:19 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2018-09-13 16:29:40 -0700 |
commit | 2d30a3aa692213a117637e78b82304ecd39a90a9 (patch) | |
tree | 78f041cf8f8bc85f3ab150c90821f4f7740a2a40 | |
parent | d00a10798e6453f44697103d1f0323446ca4c155 (diff) |
libix2: s/aabb_t/bb2f_t/g
aabb_t is a dimensionless name, and I've started mixing 2D and 3D
paradigms in the same projects so it's time to include the number
of dimensions for the bounding box. bb2f_t is also more consistent
with the vector and matrix header naming schemes I've been using.
-rw-r--r-- | src/ix2.c | 48 | ||||
-rw-r--r-- | src/ix2.h | 13 |
2 files changed, 31 insertions, 30 deletions
@@ -30,9 +30,9 @@ typedef struct v2f_t { float x, y; } v2f_t; -typedef struct aabb_t { +typedef struct bb2f_t { v2f_t min, max; -} aabb_t; +} bb2f_t; typedef struct ix2_node_t ix2_node_t; @@ -45,7 +45,7 @@ typedef struct ix2_object_t { */ v2f_t position; /* position of this object's AABB in space (may leave as 0 if .aabb is absolute) */ v2f_t origin; /* origin of position within object's AABB, 0,0 = center, -1,-1 = min 1,1 = max */ - aabb_t aabb; /* AABB of this object */ + bb2f_t aabb; /* AABB of this object */ float aabb_len_sq; /* length(aabb.max - aabb.min)^2 TODO: this isn't utilized yet, optimization potential */ void *object; /* user handle linking this object to the index */ } ix2_object_t; @@ -65,17 +65,17 @@ struct ix2_node_t { }; typedef struct ix2_t { - aabb_t aabb; /* spatial bounds of ix2 root */ + bb2f_t aabb; /* spatial bounds of ix2 root */ unsigned max_per_node; /* maximum number of objects to put in a node until max_depth reached */ unsigned max_depth; /* maximum depth of ix2 tree where nodes cease being divided further */ ix2_node_t root; /* root node of ix2 quadtree */ } ix2_t; -static ix2_object_t * add_object(ix2_t *ix2, unsigned *depth, ix2_node_t *node, aabb_t *node_aabb, ix2_object_t *object, ix2_object_ref_t **reference_cache); +static ix2_object_t * add_object(ix2_t *ix2, unsigned *depth, ix2_node_t *node, bb2f_t *node_aabb, ix2_object_t *object, ix2_object_ref_t **reference_cache); /* Check if point resides within aabb */ -static inline int aabb_contains_point(v2f_t *aabb_position, v2f_t *aabb_origin, aabb_t *aabb, v2f_t *point) +static inline int aabb_contains_point(v2f_t *aabb_position, v2f_t *aabb_origin, bb2f_t *aabb, v2f_t *point) { v2f_t scaled_origin = {}; v2f_t position = {}; @@ -108,7 +108,7 @@ static inline int aabb_contains_point(v2f_t *aabb_position, v2f_t *aabb_origin, /* check if a and b overlap */ -static inline int aabb_overlap(v2f_t *a_position, v2f_t *a_origin, aabb_t *a, v2f_t *b_position, v2f_t *b_origin, aabb_t *b) +static inline int aabb_overlap(v2f_t *a_position, v2f_t *a_origin, bb2f_t *a, v2f_t *b_position, v2f_t *b_origin, bb2f_t *b) { v2f_t a_scaled_origin = {}, b_scaled_origin = {}; v2f_t position = {}; @@ -140,7 +140,7 @@ static inline int aabb_overlap(v2f_t *a_position, v2f_t *a_origin, aabb_t *a, v2 /* initialize a node centered in aabb */ -static void init_node(ix2_node_t *node, aabb_t *aabb) +static void init_node(ix2_node_t *node, bb2f_t *aabb) { assert(node); assert(aabb); @@ -154,8 +154,8 @@ static void init_node(ix2_node_t *node, aabb_t *aabb) } -/* initialize a quadrants aabb_t array from a parent node aabb and center point */ -static void init_quadrants(aabb_t *quadrants, aabb_t *parent_aabb, v2f_t *quadrants_center) +/* initialize a quadrants bb2f_t array from a parent node aabb and center point */ +static void init_quadrants(bb2f_t *quadrants, bb2f_t *parent_aabb, v2f_t *quadrants_center) { assert(quadrants); assert(parent_aabb); @@ -264,7 +264,7 @@ static ix2_object_ref_t * link_object(ix2_object_t *object, ix2_node_t *node, ix /* Compute the diagonal length of the aabb, leaving it squared */ -static inline float aabb_len_sq(aabb_t *aabb) +static inline float aabb_len_sq(bb2f_t *aabb) { float len_sq; @@ -291,9 +291,9 @@ static void remove_object(ix2_object_t *object) /* Split node and distribute objects referenced from node->objects into a * newly created node->children. */ -ix2_node_t * split_node(ix2_t *ix2, unsigned *depth, ix2_node_t *node, aabb_t *node_aabb) +ix2_node_t * split_node(ix2_t *ix2, unsigned *depth, ix2_node_t *node, bb2f_t *node_aabb) { - aabb_t quadrants[4]; + bb2f_t quadrants[4]; ix2_object_ref_t *r, *_r; int i; @@ -350,7 +350,7 @@ _fail: * On failure NULL is returned and object is freed (including all its references). * On success the supplied object is simply returned, with potentialy new references added. */ -static ix2_object_t * add_object(ix2_t *ix2, unsigned *depth, ix2_node_t *node, aabb_t *node_aabb, ix2_object_t *object, ix2_object_ref_t **reference_cache) +static ix2_object_t * add_object(ix2_t *ix2, unsigned *depth, ix2_node_t *node, bb2f_t *node_aabb, ix2_object_t *object, ix2_object_ref_t **reference_cache) { assert(ix2); assert(depth); @@ -360,7 +360,7 @@ static ix2_object_t * add_object(ix2_t *ix2, unsigned *depth, ix2_node_t *node, /* If not a leaf, simply recur on overlapping children */ if (node->children) { - aabb_t quadrants[4]; + bb2f_t quadrants[4]; int i; *depth++; @@ -404,9 +404,9 @@ _fail: * If the supplied aabb is NULL, a default one of -1,-1...1,1 is used. * Returns NULL on failure. */ -ix2_t * ix2_new(aabb_t *aabb, unsigned max_per_node, unsigned max_depth) +ix2_t * ix2_new(bb2f_t *aabb, unsigned max_per_node, unsigned max_depth) { - aabb_t default_aabb = {{-1.f, -1.f}, {1.f, 1.f}}; + bb2f_t default_aabb = {{-1.f, -1.f}, {1.f, 1.f}}; ix2_t *ix2; if (!aabb) @@ -436,7 +436,7 @@ void ix2_free(ix2_t *ix2) /* Create and insert object spatially described by x,y,origin_x,origin_y,object_aabb into the index ix2 */ -ix2_object_t * ix2_object_new(ix2_t *ix2, v2f_t *object_position, v2f_t *object_origin, aabb_t *object_aabb, void *object) +ix2_object_t * ix2_object_new(ix2_t *ix2, v2f_t *object_position, v2f_t *object_origin, bb2f_t *object_aabb, void *object) { unsigned depth = 0; ix2_object_t *o; @@ -487,7 +487,7 @@ void ix2_object_free(ix2_t *ix2, ix2_object_t *object) /* If object_aabb is omitted, only the x,y coordinates are updated */ /* Returns object on success, NULL on failure. */ /* On failure object is removed and freed. XXX: change to leave where it was? */ -ix2_object_t * ix2_object_move(ix2_t *ix2, ix2_object_t *object, v2f_t *object_position, v2f_t *object_origin, aabb_t *object_aabb) +ix2_object_t * ix2_object_move(ix2_t *ix2, ix2_object_t *object, v2f_t *object_position, v2f_t *object_origin, bb2f_t *object_aabb) { unsigned depth = 0; @@ -529,7 +529,7 @@ unsigned ix2_search_by_point(ix2_t *ix2, v2f_t *point, ix2_search_cb cb, void *c unsigned n_hits = 0; ix2_object_ref_t *ref, *_ref; ix2_node_t *node; - aabb_t node_aabb; + bb2f_t node_aabb; assert(ix2); assert(point); @@ -542,7 +542,7 @@ unsigned ix2_search_by_point(ix2_t *ix2, v2f_t *point, ix2_search_cb cb, void *c /* find the leaf node containing point */ while (node->children) { - aabb_t quadrants[4]; + bb2f_t quadrants[4]; int i; /* TODO: this can be done more efficiently - @@ -589,7 +589,7 @@ unsigned ix2_search_by_point(ix2_t *ix2, v2f_t *point, ix2_search_cb cb, void *c /* recursively search the specified node for objects overlapping with search_aabb, * add hits to the list @ hits. */ -static void search_node_by_aabb(ix2_node_t *node, aabb_t *node_aabb, v2f_t *search_position, v2f_t *search_origin, aabb_t *search_aabb, list_head_t *hits) +static void search_node_by_aabb(ix2_node_t *node, bb2f_t *node_aabb, v2f_t *search_position, v2f_t *search_origin, bb2f_t *search_aabb, list_head_t *hits) { ix2_object_ref_t *ref, *_ref; @@ -599,7 +599,7 @@ static void search_node_by_aabb(ix2_node_t *node, aabb_t *node_aabb, v2f_t *sear assert(hits); if (node->children) { - aabb_t quadrants[4]; + bb2f_t quadrants[4]; int i; init_quadrants(quadrants, node_aabb, &node->center); @@ -623,7 +623,7 @@ static void search_node_by_aabb(ix2_node_t *node, aabb_t *node_aabb, v2f_t *sear /* Search for objects overlapping an aabb in index ix2. */ /* Returns number of cb calls performed on hits, if cb is NULL calls will be skipped but hits still counted for return. */ -unsigned ix2_search_by_aabb(ix2_t *ix2, v2f_t *search_position, v2f_t *search_origin, aabb_t *search_aabb, ix2_search_cb cb, void *cb_context) +unsigned ix2_search_by_aabb(ix2_t *ix2, v2f_t *search_position, v2f_t *search_origin, bb2f_t *search_aabb, ix2_search_cb cb, void *cb_context) { unsigned n_hits = 0; LIST_HEAD (hits); @@ -19,7 +19,7 @@ typedef struct ix2_object_t ix2_object_t; typedef struct ix2_t ix2_t; -typedef struct aabb_t aabb_t; +typedef struct bb2f_t bb2f_t; typedef struct v2f_t v2f_t; typedef enum ix2_search_status_t { @@ -28,15 +28,16 @@ typedef enum ix2_search_status_t { IX2_SEARCH_CONTINUE } ix2_search_status_t; -typedef ix2_search_status_t (*ix2_search_cb)(void *cb_context, ix2_object_t *ix2_object, v2f_t *ix2_object_position, aabb_t *ix2_object_aabb, void *object); +typedef ix2_search_status_t (*ix2_search_cb)(void *cb_context, ix2_object_t *ix2_object, v2f_t *ix2_object_position, bb2f_t *ix2_object_aabb, void *object); -ix2_t * ix2_new(aabb_t *aabb, unsigned max_per_node, unsigned max_depth); +ix2_t * ix2_new(bb2f_t *aabb, unsigned max_per_node, unsigned max_depth); void ix2_free(ix2_t *ix2); -ix2_object_t * ix2_object_new(ix2_t *ix2, v2f_t *position, v2f_t *origin, aabb_t *aabb, void *object); +ix2_object_t * ix2_object_new(ix2_t *ix2, v2f_t *position, v2f_t *origin, bb2f_t *aabb, void *object); void ix2_object_free(ix2_t *ix2, ix2_object_t *object); -ix2_object_t * ix2_object_move(ix2_t *ix2, ix2_object_t *object, v2f_t *object_position, v2f_t *object_origin, aabb_t *object_aabb); +ix2_object_t * ix2_object_move(ix2_t *ix2, ix2_object_t *object, v2f_t *object_position, v2f_t *object_origin, bb2f_t *object_aabb); +int ix2_object_aabb_overlap(ix2_t *ix2, ix2_object_t *object, v2f_t *aabb_position, v2f_t *aabb_origin, bb2f_t *aabb); unsigned ix2_search_by_point(ix2_t *ix2, v2f_t *point, ix2_search_cb cb, void *arg); -unsigned ix2_search_by_aabb(ix2_t *ix2, v2f_t *search_position, v2f_t *search_origin, aabb_t *search_aabb, ix2_search_cb cb, void *arg); +unsigned ix2_search_by_aabb(ix2_t *ix2, v2f_t *search_position, v2f_t *search_origin, bb2f_t *search_aabb, ix2_search_cb cb, void *arg); unsigned ix2_search_by_ray(ix2_t *ix2, v2f_t *origin, v2f_t *direction, ix2_search_cb cb, void *arg); #endif |