From 5b3aabc103efa7bf2d0a4dfe057e9cc5a24e1b27 Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Tue, 29 May 2018 23:19:39 +0300 Subject: [PATCH] aabb intersect functions * AABB vs AABB * AABB vs Point * AABB vs Sphere * AABB contains AABB --- include/cglm/box.h | 65 +++++++++++++++++++++++++++++++++++++++++ include/cglm/call/box.h | 16 ++++++++++ src/box.c | 24 +++++++++++++++ 3 files changed, 105 insertions(+) diff --git a/include/cglm/box.h b/include/cglm/box.h index 5ed135e..72f8b8f 100644 --- a/include/cglm/box.h +++ b/include/cglm/box.h @@ -212,4 +212,69 @@ glm_aabb_center(vec3 box[2], vec3 dest) { glm_vec_center(box[0], box[1], dest); } +/*! + * @brief check if two AABB intersects + * + * @param[in] box bounding box + * @param[in] other other bounding box + */ +CGLM_INLINE +bool +glm_aabb_aabb(vec3 box[2], vec3 other[2]) { + return (box[0][0] <= other[1][0] && box[1][0] >= other[0][0]) + && (box[0][1] <= other[1][1] && box[1][1] >= other[0][1]) + && (box[0][2] <= other[1][2] && box[1][2] >= other[0][2]); +} + +/*! + * @brief check if AABB intersects with sphere + * + * @param[in] box bounding box + * @param[in] other other bounding box + */ +CGLM_INLINE +bool +glm_aabb_sphere(vec3 box[2], vec4 sph) { + vec3 v; + float dist; + + /* get box closest point to sphere center by clamping */ + v[0] = glm_max(box[0][0], glm_min(sph[0], box[1][0])); + v[1] = glm_max(box[0][1], glm_min(sph[1], box[1][1])); + v[2] = glm_max(box[0][2], glm_min(sph[2], box[1][2])); + + /* this is the same as glm_sphere_point */ + dist = glm_vec_distance(v, sph); + + return dist < sph[3]; +} + +/*! + * @brief check if point is inside of AABB + * + * @param[in] box bounding box + * @param[in] point point + */ +CGLM_INLINE +bool +glm_aabb_point(vec3 box[2], vec3 point) { + return (point[0] >= box[0][0] && point[0] <= box[1][0]) + && (point[1] >= box[0][1] && point[1] <= box[1][1]) + && (point[2] >= box[0][2] && point[2] <= box[1][2]); +} + +/*! + * @brief check if AABB contains other AABB + * + * @param[in] box bounding box + * @param[in] other other bounding box + */ +CGLM_INLINE +bool +glm_aabb_contains(vec3 box[2], vec3 other[2]) { + return (box[0][0] <= other[0][0] && box[1][0] >= other[1][0]) + && (box[0][1] <= other[0][1] && box[1][1] >= other[1][1]) + && (box[0][2] <= other[0][2] && box[1][2] >= other[1][2]); +} + #endif /* cglm_box_h */ diff --git a/include/cglm/call/box.h b/include/cglm/call/box.h index bf25508..0d4c8c1 100644 --- a/include/cglm/call/box.h +++ b/include/cglm/call/box.h @@ -56,6 +56,22 @@ CGLM_EXPORT void glmc_aabb_center(vec3 box[2], vec3 dest); +CGLM_EXPORT +bool +glmc_aabb_aabb(vec3 box[2], vec3 other[2]); + +CGLM_EXPORT +bool +glmc_aabb_point(vec3 box[2], vec3 point); + +CGLM_EXPORT +bool +glmc_aabb_contains(vec3 box[2], vec3 other[2]); + +CGLM_EXPORT +bool +glmc_aabb_sphere(vec3 box[2], vec4 sph); + #ifdef __cplusplus } #endif diff --git a/src/box.c b/src/box.c index 0274390..1352f97 100644 --- a/src/box.c +++ b/src/box.c @@ -70,3 +70,27 @@ void glmc_aabb_center(vec3 box[2], vec3 dest) { glm_aabb_center(box, dest); } + +CGLM_EXPORT +bool +glmc_aabb_aabb(vec3 box[2], vec3 other[2]) { + return glm_aabb_aabb(box, other); +} + +CGLM_EXPORT +bool +glmc_aabb_point(vec3 box[2], vec3 point) { + return glm_aabb_point(box, point); +} + +CGLM_EXPORT +bool +glmc_aabb_contains(vec3 box[2], vec3 other[2]) { + return glm_aabb_contains(box, other); +} + +CGLM_EXPORT +bool +glmc_aabb_sphere(vec3 box[2], vec4 sph) { + return glm_aabb_sphere(box, sph); +}