From 53bde05bd90c589c2db67a98401da173580a873e Mon Sep 17 00:00:00 2001 From: duarm Date: Sat, 11 Nov 2023 08:13:28 -0300 Subject: [PATCH 01/10] aabb2d functions --- docs/source/aabb2d.rst | 181 +++++++++++++++++++++++ docs/source/features.rst | 1 + include/cglm/aabb2d.h | 269 +++++++++++++++++++++++++++++++++++ include/cglm/call.h | 1 + include/cglm/call/aabb2d.h | 80 +++++++++++ include/cglm/cglm.h | 1 + include/cglm/struct/aabb2d.h | 260 +++++++++++++++++++++++++++++++++ src/aabb2d.c | 96 +++++++++++++ 8 files changed, 889 insertions(+) create mode 100644 docs/source/aabb2d.rst create mode 100644 include/cglm/aabb2d.h create mode 100644 include/cglm/call/aabb2d.h create mode 100644 include/cglm/struct/aabb2d.h create mode 100644 src/aabb2d.c diff --git a/docs/source/aabb2d.rst b/docs/source/aabb2d.rst new file mode 100644 index 0000000..dcb1ac2 --- /dev/null +++ b/docs/source/aabb2d.rst @@ -0,0 +1,181 @@ +.. default-domain:: C + +2d axis aligned bounding box (AABB) +================================================================================ + +Header: cglm/aabb2d.h + +Some convenient functions provided for AABB. + +**Definition of box:** + +cglm defines box as two dimensional array of vec2. +The first element is **min** point and the second one is **max** point. +If you have another type e.g. struct or even another representation then you must +convert it before and after call cglm box function. + +Table of contents (click to go): +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Functions: + +1. :c:func:`glm_aabb2d_transform` +#. :c:func:`glm_aabb2d_merge` +#. :c:func:`glm_aabb2d_crop` +#. :c:func:`glm_aabb2d_crop_until` +#. :c:func:`glm_aabb2d_frustum` +#. :c:func:`glm_aabb2d_invalidate` +#. :c:func:`glm_aabb2d_isvalid` +#. :c:func:`glm_aabb2d_size` +#. :c:func:`glm_aabb2d_radius` +#. :c:func:`glm_aabb2d_center` +#. :c:func:`glm_aabb2d_aabb` +#. :c:func:`glm_aabb2d_sphere` +#. :c:func:`glm_aabb2d_point` +#. :c:func:`glm_aabb2d_contains` + +Functions documentation +~~~~~~~~~~~~~~~~~~~~~~~ + +.. c:function:: void glm_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]) + + | apply transform to Axis-Aligned Bounding Box + + Parameters: + | *[in]* **aabb** bounding box + | *[in]* **m** transform matrix + | *[out]* **dest** transformed bounding box + +.. c:function:: void glm_aabb2d_merge(vec2 aabb1[2], vec2 aabb2[2], vec2 dest[2]) + + | merges two AABB bounding box and creates new one + + two aabb must be in the same space + + Parameters: + | *[in]* **aabb1** bounding box 1 + | *[in]* **aabb2** bounding box 2 + | *[out]* **dest** merged bounding box + +.. c:function:: void glm_aabb2d_crop(vec2 aabb[2], vec2 cropAabb[2], vec2 dest[2]) + + | crops a bounding box with another one. + + this could be useful for gettng a bbox which fits with view frustum and + object bounding boxes. In this case you crop view frustum box with objects + box + + Parameters: + | *[in]* **aabb** bounding box 1 + | *[in]* **cropAabb** crop box + | *[out]* **dest** cropped bounding box + +.. c:function:: void glm_aabb2d_crop_until(vec2 aabb[2], vec2 cropAabb[2], vec2 clampAabb[2], vec2 dest[2]) + + | crops a bounding box with another one. + + this could be useful for gettng a bbox which fits with view frustum and + object bounding boxes. In this case you crop view frustum box with objects + box + + Parameters: + | *[in]* **aabb** bounding box + | *[in]* **cropAabb** crop box + | *[in]* **clampAabb** miniumum box + | *[out]* **dest** cropped bounding box + +.. c:function:: bool glm_aabb2d_frustum(vec2 aabb[2], vec4 planes[6]) + + | check if AABB intersects with frustum planes + + this could be useful for frustum culling using AABB. + + OPTIMIZATION HINT: + if planes order is similar to LEFT, RIGHT, BOTTOM, TOP, NEAR, FAR + then this method should run even faster because it would only use two + planes if object is not inside the two planes + fortunately cglm extracts planes as this order! just pass what you got! + + Parameters: + | *[in]* **aabb** bounding box + | *[out]* **planes** frustum planes + +.. c:function:: void glm_aabb2d_invalidate(vec2 aabb[2]) + + | invalidate AABB min and max values + + | It fills *max* values with -FLT_MAX and *min* values with +FLT_MAX + + Parameters: + | *[in, out]* **aabb** bounding box + +.. c:function:: bool glm_aabb2d_isvalid(vec2 aabb[2]) + + | check if AABB is valid or not + + Parameters: + | *[in]* **aabb** bounding box + + Returns: + returns true if aabb is valid otherwise false + +.. c:function:: float glm_aabb2d_size(vec2 aabb[2]) + + | distance between of min and max + + Parameters: + | *[in]* **aabb** bounding box + + Returns: + distance between min - max + +.. c:function:: float glm_aabb2d_radius(vec2 aabb[2]) + + | radius of sphere which surrounds AABB + + Parameters: + | *[in]* **aabb** bounding box + +.. c:function:: void glm_aabb2d_center(vec2 aabb[2], vec2 dest) + + | computes center point of AABB + + Parameters: + | *[in]* **aabb** bounding box + | *[out]* **dest** center of bounding box + +.. c:function:: bool glm_aabb2d_aabb(vec2 aabb[2], vec2 other[2]) + + | check if two AABB intersects + + Parameters: + | *[in]* **aabb** bounding box + | *[out]* **other** other bounding box + +.. c:function:: bool glm_aabb2d_sphere(vec2 aabb[2], vec4 s) + + | check if AABB intersects with sphere + + | https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c + | Solid Box - Solid Sphere test. + + Parameters: + | *[in]* **aabb** solid bounding box + | *[out]* **s** solid sphere + +.. c:function:: bool glm_aabb2d_point(vec2 aabb[2], vec2 point) + + | check if point is inside of AABB + + Parameters: + | *[in]* **aabb** bounding box + | *[out]* **point** point + +.. c:function:: bool glm_aabb2d_contains(vec2 aabb[2], vec2 other[2]) + + | check if AABB contains other AABB + + Parameters: + | *[in]* **aabb** bounding box + | *[out]* **other** other bounding box + diff --git a/docs/source/features.rst b/docs/source/features.rst index 7b61599..865a63c 100644 --- a/docs/source/features.rst +++ b/docs/source/features.rst @@ -18,6 +18,7 @@ Features * inline or pre-compiled function call * frustum (extract view frustum planes, corners...) * bounding box (AABB in Frustum (culling), crop, merge...) +* 2d bounding box (crop, merge...) * bounding sphere * project, unproject * easing functions diff --git a/include/cglm/aabb2d.h b/include/cglm/aabb2d.h new file mode 100644 index 0000000..acddf59 --- /dev/null +++ b/include/cglm/aabb2d.h @@ -0,0 +1,269 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_aabb2d_h +#define cglm_aabb2d_h + +#include "common.h" +#include "vec2.h" +#include "vec4.h" +#include "util.h" + +/*! + * @brief apply transform to Axis-Aligned Bounding aabb + * + * @param[in] aabb bounding aabb + * @param[in] m transform matrix + * @param[out] dest transformed bounding aabb + */ +CGLM_INLINE +void +glm_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]) { + vec2 v[2], xa, xb, ya, yb, za, zb; + + glm_vec2_scale(m[0], aabb[0][0], xa); + glm_vec2_scale(m[0], aabb[1][0], xb); + + glm_vec2_scale(m[1], aabb[0][1], ya); + glm_vec2_scale(m[1], aabb[1][1], yb); + + /* translation + min(xa, xb) + min(ya, yb) + min(za, zb) */ + glm_vec2(m[3], v[0]); + glm_vec2_minadd(xa, xb, v[0]); + glm_vec2_minadd(ya, yb, v[0]); + glm_vec2_minadd(za, zb, v[0]); + + /* translation + max(xa, xb) + max(ya, yb) + max(za, zb) */ + glm_vec2(m[3], v[1]); + glm_vec2_maxadd(xa, xb, v[1]); + glm_vec2_maxadd(ya, yb, v[1]); + glm_vec2_maxadd(za, zb, v[1]); + + glm_vec2_copy(v[0], dest[0]); + glm_vec2_copy(v[1], dest[1]); +} + +/*! + * @brief merges two AABB bounding aabb and creates new one + * + * two aabb must be in same space, if one of aabb is in different space then + * you should consider to convert it's space by glm_aabb_space + * + * @param[in] aabb1 bounding aabb 1 + * @param[in] aabb2 bounding aabb 2 + * @param[out] dest merged bounding aabb + */ +CGLM_INLINE +void +glm_aabb2d_merge(vec2 aabb1[2], vec2 aabb2[2], vec2 dest[2]) { + dest[0][0] = glm_min(aabb1[0][0], aabb2[0][0]); + dest[0][1] = glm_min(aabb1[0][1], aabb2[0][1]); + + dest[1][0] = glm_max(aabb1[1][0], aabb2[1][0]); + dest[1][1] = glm_max(aabb1[1][1], aabb2[1][1]); +} + +/*! + * @brief crops a bounding aabb with another one. + * + * this could be useful for gettng a baabb which fits with view frustum and + * object bounding aabbes. In this case you crop view frustum aabb with objects + * aabb + * + * @param[in] aabb bounding aabb 1 + * @param[in] cropaabb crop aabb + * @param[out] dest cropped bounding aabb + */ +CGLM_INLINE +void +glm_aabb2d_crop(vec2 aabb[2], vec2 cropAabb[2], vec2 dest[2]) { + dest[0][0] = glm_max(aabb[0][0], cropAabb[0][0]); + dest[0][1] = glm_max(aabb[0][1], cropAabb[0][1]); + + dest[1][0] = glm_min(aabb[1][0], cropAabb[1][0]); + dest[1][1] = glm_min(aabb[1][1], cropAabb[1][1]); +} + +/*! + * @brief crops a bounding aabb with another one. + * + * this could be useful for gettng a baabb which fits with view frustum and + * object bounding aabbes. In this case you crop view frustum aabb with objects + * aabb + * + * @param[in] aabb bounding aabb + * @param[in] cropaabb crop aabb + * @param[in] clampaabb miniumum aabb + * @param[out] dest cropped bounding aabb + */ +CGLM_INLINE +void +glm_aabb2d_crop_until(vec2 aabb[2], + vec2 cropAabb[2], + vec2 clampAabb[2], + vec2 dest[2]) { + glm_aabb2d_crop(aabb, cropAabb, dest); + glm_aabb2d_merge(clampAabb, dest, dest); +} + +/*! + * @brief check if AABB intersects with frustum planes + * + * this could be useful for frustum culling using AABB. + * + * OPTIMIZATION HINT: + * if planes order is similar to LEFT, RIGHT, BOTTOM, TOP, NEAR, FAR + * then this method should run even faster because it would only use two + * planes if object is not inside the two planes + * fortunately cglm extracts planes as this order! just pass what you got! + * + * @param[in] aabb bounding aabb + * @param[in] planes frustum planes + */ +CGLM_INLINE +bool +glm_aabb2d_frustum(vec2 aabb[2], vec4 planes[6]) { + float *p, dp; + int i; + + for (i = 0; i < 6; i++) { + p = planes[i]; + dp = p[0] * aabb[p[0] > 0.0f][0] + + p[1] * aabb[p[1] > 0.0f][1]; + + if (dp < -p[3]) + return false; + } + + return true; +} + +/*! + * @brief invalidate AABB min and max values + * + * @param[in, out] aabb bounding aabb + */ +CGLM_INLINE +void +glm_aabb2d_invalidate(vec2 aabb[2]) { + glm_vec2_fill(aabb[0], FLT_MAX); + glm_vec2_fill(aabb[1], -FLT_MAX); +} + +/*! + * @brief check if AABB is valid or not + * + * @param[in] aabb bounding aabb + */ +CGLM_INLINE +bool +glm_aabb2d_isvalid(vec2 aabb[2]) { + return glm_vec2_max(aabb[0]) != FLT_MAX + && glm_vec2_min(aabb[1]) != -FLT_MAX; +} + +/*! + * @brief distance between of min and max + * + * @param[in] aabb bounding aabb + */ +CGLM_INLINE +float +glm_aabb2d_size(vec2 aabb[2]) { + return glm_vec2_distance(aabb[0], aabb[1]); +} + +/*! + * @brief radius of sphere which surrounds AABB + * + * @param[in] aabb bounding aabb + */ +CGLM_INLINE +float +glm_aabb2d_radius(vec2 aabb[2]) { + return glm_aabb2d_size(aabb) * 0.5f; +} + +/*! + * @brief computes center point of AABB + * + * @param[in] aabb bounding aabb + * @param[out] dest center of bounding aabb + */ +CGLM_INLINE +void +glm_aabb2d_center(vec2 aabb[2], vec2 dest) { + glm_vec2_center(aabb[0], aabb[1], dest); +} + +/*! + * @brief check if two AABB intersects + * + * @param[in] aabb bounding aabb + * @param[in] other other bounding aabb + */ +CGLM_INLINE +bool +glm_aabb2d_aabb(vec2 aabb[2], vec2 other[2]) { + return (aabb[0][0] <= other[1][0] && aabb[1][0] >= other[0][0]) + && (aabb[0][1] <= other[1][1] && aabb[1][1] >= other[0][1]); +} + +/*! + * @brief check if AABB intersects with sphere + * + * https://github.com/erich666/GraphicsGems/blob/master/gems/aabbSphere.c + * Solid aabb - Solid Sphere test. + * + * Sphere Representation in cglm: [center.x, center.y, center.z, radii] + * + * @param[in] aabb solid bounding aabb + * @param[in] s solid sphere + */ +CGLM_INLINE +bool +glm_aabb2d_sphere(vec2 aabb[2], vec4 s) { + float dmin; + int a, b; + + a = (s[0] < aabb[0][0]) + (s[0] > aabb[1][0]); + b = (s[1] < aabb[0][1]) + (s[1] > aabb[1][1]); + + dmin = glm_pow2((s[0] - aabb[!(a - 1)][0]) * (a != 0)) + + glm_pow2((s[1] - aabb[!(b - 1)][1]) * (b != 0)); + + return dmin <= glm_pow2(s[3]); +} + +/*! + * @brief check if point is inside of AABB + * + * @param[in] aabb bounding aabb + * @param[in] point point + */ +CGLM_INLINE +bool +glm_aabb2d_point(vec2 aabb[2], vec2 point) { + return (point[0] >= aabb[0][0] && point[0] <= aabb[1][0]) + && (point[1] >= aabb[0][1] && point[1] <= aabb[1][1]); +} + +/*! + * @brief check if AABB contains other AABB + * + * @param[in] aabb bounding aabb + * @param[in] other other bounding aabb + */ +CGLM_INLINE +bool +glm_aabb2d_contains(vec2 aabb[2], vec2 other[2]) { + return (aabb[0][0] <= other[0][0] && aabb[1][0] >= other[1][0]) + && (aabb[0][1] <= other[0][1] && aabb[1][1] >= other[1][1]); +} + +#endif /* cglm_aabb2d_h */ + diff --git a/include/cglm/call.h b/include/cglm/call.h index c411b38..de775d2 100644 --- a/include/cglm/call.h +++ b/include/cglm/call.h @@ -33,6 +33,7 @@ extern "C" { #include "call/euler.h" #include "call/plane.h" #include "call/frustum.h" +#include "call/aabb2d.h" #include "call/box.h" #include "call/io.h" #include "call/project.h" diff --git a/include/cglm/call/aabb2d.h b/include/cglm/call/aabb2d.h new file mode 100644 index 0000000..6543db8 --- /dev/null +++ b/include/cglm/call/aabb2d.h @@ -0,0 +1,80 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglmc_aabb2d_h +#define cglmc_aabb2d_h +#ifdef __cplusplus +extern "C" { +#endif + +#include "../cglm.h" + +CGLM_EXPORT +void +glmc_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]); + +CGLM_EXPORT +void +glmc_aabb2d_merge(vec2 aabb1[2], vec2 aabb2[2], vec2 dest[2]); + +CGLM_EXPORT +void +glmc_aabb2d_crop(vec2 aabb[2], vec2 cropAabb[2], vec2 dest[2]); + +CGLM_EXPORT +void +glmc_aabb2d_crop_until(vec2 aabb[2], + vec2 cropAabb[2], + vec2 clampAabb[2], + vec2 dest[2]); + +CGLM_EXPORT +bool +glmc_aabb2d_frustum(vec2 aabb[2], vec4 planes[6]); + +CGLM_EXPORT +void +glmc_aabb2d_invalidate(vec2 aabb[2]); + +CGLM_EXPORT +bool +glmc_aabb2d_isvalid(vec2 aabb[2]); + +CGLM_EXPORT +float +glmc_aabb2d_size(vec2 aabb[2]); + +CGLM_EXPORT +float +glmc_aabb2d_radius(vec2 aabb[2]); + +CGLM_EXPORT +void +glmc_aabb2d_center(vec2 aabb[2], vec2 dest); + +CGLM_EXPORT +bool +glmc_aabb2d_aabb(vec2 aabb[2], vec2 other[2]); + +CGLM_EXPORT +bool +glmc_aabb2d_point(vec2 aabb[2], vec2 point); + +CGLM_EXPORT +bool +glmc_aabb2d_contains(vec2 aabb[2], vec2 other[2]); + +CGLM_EXPORT +bool +glmc_aabb2d_sphere(vec2 aabb[2], vec4 s); + +#ifdef __cplusplus +} +#endif +#endif /* cglmc_aabb2d_h */ + + diff --git a/include/cglm/cglm.h b/include/cglm/cglm.h index 17db214..5e15a3c 100644 --- a/include/cglm/cglm.h +++ b/include/cglm/cglm.h @@ -30,6 +30,7 @@ #include "quat.h" #include "euler.h" #include "plane.h" +#include "aabb2d.h" #include "box.h" #include "color.h" #include "util.h" diff --git a/include/cglm/struct/aabb2d.h b/include/cglm/struct/aabb2d.h new file mode 100644 index 0000000..cad3564 --- /dev/null +++ b/include/cglm/struct/aabb2d.h @@ -0,0 +1,260 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglms_aabb2ds_h +#define cglms_aabb2ds_h + +#include "../common.h" +#include "../types-struct.h" +#include "../aabb2d.h" +#include "vec2.h" +#include "vec4.h" +#include "mat4.h" + +/* api definition */ +#define glms_aabb2d_(NAME) CGLM_STRUCTAPI(aabb, NAME) + +/*! + * @brief apply transform to Axis-Aligned Bounding Box + * + * @param[in] aabb bounding box + * @param[in] m transform matrix + * @param[out] dest transformed bounding box + */ +CGLM_INLINE +void +glms_aabb2d_(transform)(vec2s aabb[2], mat4s m, vec2s dest[2]) { + vec2 rawAabb[2]; + vec2 rawDest[2]; + + glms_vec2_(unpack)(rawAabb, aabb, 2); + glm_aabb2d_transform(rawAabb, m.raw, rawDest); + glms_vec2_(pack)(dest, rawDest, 2); +} + +/*! + * @brief merges two AABB bounding box and creates new one + * + * two box must be in same space, if one of box is in different space then + * you should consider to convert it's space by glm_box_space + * + * @param[in] aabb1 bounding box 1 + * @param[in] aabb2 bounding box 2 + * @param[out] dest merged bounding box + */ +CGLM_INLINE +void +glms_aabb2d_(merge)(vec2s aabb1[2], vec2s aabb2[2], vec2s dest[2]) { + vec2 rawAabb1[2]; + vec2 rawAabb2[2]; + vec2 rawDest[2]; + + glms_vec2_(unpack)(rawAabb1, aabb1, 2); + glms_vec2_(unpack)(rawAabb2, aabb2, 2); + glm_aabb2d_merge(rawAabb1, rawAabb2, rawDest); + glms_vec2_(pack)(dest, rawDest, 2); +} + +/*! + * @brief crops a bounding box with another one. + * + * this could be useful for gettng a bbox which fits with view frustum and + * object bounding boxes. In this case you crop view frustum box with objects + * box + * + * @param[in] aabb bounding box 1 + * @param[in] cropAabb crop box + * @param[out] dest cropped bounding box + */ +CGLM_INLINE +void +glms_aabb2d_(crop)(vec2s aabb[2], vec2s cropAabb[2], vec2s dest[2]) { + vec2 rawAabb[2]; + vec2 rawCropAabb[2]; + vec2 rawDest[2]; + + glms_vec2_(unpack)(rawAabb, aabb, 2); + glms_vec2_(unpack)(rawCropAabb, cropAabb, 2); + glm_aabb2d_crop(rawAabb, rawCropAabb, rawDest); + glms_vec2_(pack)(dest, rawDest, 2); +} + +/*! + * @brief crops a bounding box with another one. + * + * this could be useful for gettng a bbox which fits with view frustum and + * object bounding boxes. In this case you crop view frustum box with objects + * box + * + * @param[in] aabb bounding box + * @param[in] cropAabb crop box + * @param[in] clampAabb miniumum box + * @param[out] dest cropped bounding box + */ +CGLM_INLINE +void +glms_aabb2d_(crop_until)(vec2s aabb[2], + vec2s cropAabb[2], + vec2s clampAabb[2], + vec2s dest[2]) { + glms_aabb2d_(crop)(aabb, cropAabb, dest); + glms_aabb2d_(merge)(clampAabb, dest, dest); +} + +/*! + * @brief check if AABB intersects with frustum planes + * + * this could be useful for frustum culling using AABB. + * + * OPTIMIZATION HINT: + * if planes order is similar to LEFT, RIGHT, BOTTOM, TOP, NEAR, FAR + * then this method should run even faster because it would only use two + * planes if object is not inside the two planes + * fortunately cglm extracts planes as this order! just pass what you got! + * + * @param[in] aabb bounding box + * @param[in] planes frustum planes + */ +CGLM_INLINE +bool +glms_aabb2d_(frustum)(vec2s box[2], vec4s planes[6]) { + vec2 rawBox[2]; + vec4 rawPlanes[6]; + + glms_vec2_(unpack)(rawBox, box, 2); + glms_vec4_(unpack)(rawPlanes, planes, 6); + return glm_aabb2d_frustum(rawBox, rawPlanes); +} + +/*! + * @brief invalidate AABB min and max values + * + * @param[in, out] aabb bounding box + */ +CGLM_INLINE +void +glms_aabb2d_(invalidate)(vec2s box[2]) { + box[0] = glms_vec2_(fill)(FLT_MAX); + box[1] = glms_vec2_(fill)(-FLT_MAX); +} + +/*! + * @brief check if AABB is valid or not + * + * @param[in] aabb bounding box + */ +CGLM_INLINE +bool +glms_aabb2d_(isvalid)(vec2s aabb[2]) { + vec2 rawAabb[2]; + glms_vec2_(unpack)(rawAabb, aabb, 2); + return glm_aabb2d_isvalid(rawAabb); +} + +/*! + * @brief distance between of min and max + * + * @param[in] aabb bounding box + */ +CGLM_INLINE +float +glms_aabb2d_(size)(vec2s aabb[2]) { + return glm_vec2_distance(aabb[0].raw, aabb[1].raw); +} + +/*! + * @brief radius of sphere which surrounds AABB + * + * @param[in] aabb bounding box + */ +CGLM_INLINE +float +glms_aabb2d_(radius)(vec2s aabb[2]) { + return glms_aabb2d_(size)(aabb) * 0.5f; +} + +/*! + * @brief computes center point of AABB + * + * @param[in] aabb bounding box + * @returns center of bounding box + */ +CGLM_INLINE +vec2s +glms_aabb2d_(center)(vec2s aabb[2]) { + return glms_vec2_(center)(aabb[0], aabb[1]); +} + +/*! + * @brief check if two AABB intersects + * + * @param[in] aabb bounding box + * @param[in] other other bounding box + */ +CGLM_INLINE +bool +glms_aabb2d_(aabb)(vec2s aabb[2], vec2s other[2]) { + vec2 rawAabb[2]; + vec2 rawOther[2]; + + glms_vec2_(unpack)(rawAabb, aabb, 2); + glms_vec2_(unpack)(rawOther, other, 2); + return glm_aabb2d_aabb(rawAabb, rawOther); +} + +/*! + * @brief check if AABB intersects with sphere + * + * https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c + * Solid Box - Solid Sphere test. + * + * @param[in] aabb solid bounding box + * @param[in] s solid sphere + */ +CGLM_INLINE +bool +glms_aabb2d_(sphere)(vec2s aabb[2], vec4s s) { + vec2 rawAabb[2]; + + glms_vec2_(unpack)(rawAabb, aabb, 2); + return glm_aabb2d_sphere(rawAabb, s.raw); +} + +/*! + * @brief check if point is inside of AABB + * + * @param[in] aabb bounding box + * @param[in] point point + */ +CGLM_INLINE +bool +glms_aabb2d_(point)(vec2s aabb[2], vec2s point) { + vec2 rawAabb[2]; + + glms_vec2_(unpack)(rawAabb, aabb, 2); + return glm_aabb2d_point(rawAabb, point.raw); +} + +/*! + * @brief check if AABB contains other AABB + * + * @param[in] box bounding box + * @param[in] other other bounding box + */ +CGLM_INLINE +bool +glms_aabb2d_(contains)(vec2s aabb[2], vec2s other[2]) { + vec2 rawAabb[2]; + vec2 rawOther[2]; + + glms_vec2_(unpack)(rawAabb, aabb, 2); + glms_vec2_(unpack)(rawOther, other, 2); + return glm_aabb2d_contains(rawAabb, rawOther); +} + +#endif /* cglms_aabb2ds_h */ + diff --git a/src/aabb2d.c b/src/aabb2d.c new file mode 100644 index 0000000..e64aa7e --- /dev/null +++ b/src/aabb2d.c @@ -0,0 +1,96 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#include "../include/cglm/cglm.h" +#include "../include/cglm/call.h" + +CGLM_EXPORT +void +glmc_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]) { + glm_aabb2d_transform(aabb, m, dest); +} + +CGLM_EXPORT +void +glmc_aabb2d_merge(vec2 aabb1[2], vec2 aabb2[2], vec2 dest[2]) { + glm_aabb2d_merge(aabb1, aabb2, dest); +} + +CGLM_EXPORT +void +glmc_aabb2d_crop(vec2 aabb[2], vec2 cropAabb[2], vec2 dest[2]) { + glm_aabb2d_crop(aabb, cropAabb, dest); +} + +CGLM_EXPORT +void +glmc_aabb2d_crop_until(vec2 aabb[2], + vec2 cropAabb[2], + vec2 clampAabb[2], + vec2 dest[2]) { + glm_aabb2d_crop_until(aabb, cropAabb, clampAabb, dest); +} + +CGLM_EXPORT +bool +glmc_aabb2d_frustum(vec2 aabb[2], vec4 planes[6]) { + return glm_aabb2d_frustum(aabb, planes); +} + +CGLM_EXPORT +void +glmc_aabb2d_invalidate(vec2 aabb[2]) { + glm_aabb2d_invalidate(aabb); +} + +CGLM_EXPORT +bool +glmc_aabb2d_isvalid(vec2 aabb[2]) { + return glm_aabb2d_isvalid(aabb); +} + +CGLM_EXPORT +float +glmc_aabb2d_size(vec2 aabb[2]) { + return glm_aabb2d_size(aabb); +} + +CGLM_EXPORT +float +glmc_aabb2d_radius(vec2 aabb[2]) { + return glm_aabb2d_radius(aabb); +} + +CGLM_EXPORT +void +glmc_aabb2d_center(vec2 aabb[2], vec2 dest) { + glm_aabb2d_center(aabb, dest); +} + +CGLM_EXPORT +bool +glmc_aabb2d_aabb(vec2 aabb[2], vec2 other[2]) { + return glm_aabb2d_aabb(aabb, other); +} + +CGLM_EXPORT +bool +glmc_aabb2d_point(vec2 aabb[2], vec2 point) { + return glm_aabb2d_point(aabb, point); +} + +CGLM_EXPORT +bool +glmc_aabb2d_contains(vec2 aabb[2], vec2 other[2]) { + return glm_aabb2d_contains(aabb, other); +} + +CGLM_EXPORT +bool +glmc_aabb2d_sphere(vec2 aabb[2], vec4 s) { + return glm_aabb2d_sphere(aabb, s); +} From 2a975a7d0a942c8108fb3e200bb36ff4c0b83d41 Mon Sep 17 00:00:00 2001 From: duarm Date: Sat, 2 Dec 2023 21:00:21 -0300 Subject: [PATCH 02/10] circle fix, new copy func --- docs/source/aabb2d.rst | 23 ++++++++++++++++------- include/cglm/aabb2d.h | 34 ++++++++++++++++++++++------------ include/cglm/call/aabb2d.h | 6 +++++- include/cglm/struct/aabb2d.h | 2 +- src/aabb2d.c | 10 ++++++++-- 5 files changed, 52 insertions(+), 23 deletions(-) diff --git a/docs/source/aabb2d.rst b/docs/source/aabb2d.rst index dcb1ac2..a4e5b6e 100644 --- a/docs/source/aabb2d.rst +++ b/docs/source/aabb2d.rst @@ -7,19 +7,20 @@ Header: cglm/aabb2d.h Some convenient functions provided for AABB. -**Definition of box:** +**Definition of aabb:** -cglm defines box as two dimensional array of vec2. -The first element is **min** point and the second one is **max** point. +cglm defines an aabb as a two dimensional array of vec2's. +The first element is the **min** point and the second one is the **max** point. If you have another type e.g. struct or even another representation then you must -convert it before and after call cglm box function. +convert it before and after calling a cglm aabb2d function. Table of contents (click to go): ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Functions: -1. :c:func:`glm_aabb2d_transform` +1. :c:func:`glm_aabb2d_copy` +#. :c:func:`glm_aabb2d_transform` #. :c:func:`glm_aabb2d_merge` #. :c:func:`glm_aabb2d_crop` #. :c:func:`glm_aabb2d_crop_until` @@ -30,13 +31,21 @@ Functions: #. :c:func:`glm_aabb2d_radius` #. :c:func:`glm_aabb2d_center` #. :c:func:`glm_aabb2d_aabb` -#. :c:func:`glm_aabb2d_sphere` +#. :c:func:`glm_aabb2d_circle` #. :c:func:`glm_aabb2d_point` #. :c:func:`glm_aabb2d_contains` Functions documentation ~~~~~~~~~~~~~~~~~~~~~~~ +.. c:function:: void glm_aabb2d_copy(vec2 aabb[2], vec2 dest[2]) + + | copy all members of [aabb] to [dest] + + Parameters: + | *[in]* **aabb** bounding box + | *[out]* **dest** destination + .. c:function:: void glm_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]) | apply transform to Axis-Aligned Bounding Box @@ -152,7 +161,7 @@ Functions documentation | *[in]* **aabb** bounding box | *[out]* **other** other bounding box -.. c:function:: bool glm_aabb2d_sphere(vec2 aabb[2], vec4 s) +.. c:function:: bool glm_aabb2d_circle(vec2 aabb[2], vec4 s) | check if AABB intersects with sphere diff --git a/include/cglm/aabb2d.h b/include/cglm/aabb2d.h index acddf59..ed09843 100644 --- a/include/cglm/aabb2d.h +++ b/include/cglm/aabb2d.h @@ -13,6 +13,19 @@ #include "vec4.h" #include "util.h" +/*! + * @brief copy all members of [aabb] to [dest] + * + * @param[in] aabb source + * @param[out] dest destination + */ +CGLM_INLINE +void +glm_aabb2d_copy(vec2 aabb[2], vec2 dest[2]) { + glm_vec2_copy(aabb[0], dest[0]); + glm_vec2_copy(aabb[1], dest[1]); +} + /*! * @brief apply transform to Axis-Aligned Bounding aabb * @@ -216,27 +229,24 @@ glm_aabb2d_aabb(vec2 aabb[2], vec2 other[2]) { /*! * @brief check if AABB intersects with sphere * - * https://github.com/erich666/GraphicsGems/blob/master/gems/aabbSphere.c - * Solid aabb - Solid Sphere test. + * Circle Representation in cglm: [center.x, center.y, radii] * - * Sphere Representation in cglm: [center.x, center.y, center.z, radii] - * - * @param[in] aabb solid bounding aabb - * @param[in] s solid sphere + * @param[in] aabb solid bounding aabb + * @param[in] c solid circle */ CGLM_INLINE bool -glm_aabb2d_sphere(vec2 aabb[2], vec4 s) { +glm_aabb2d_circle(vec2 aabb[2], vec3 c) { float dmin; int a, b; - a = (s[0] < aabb[0][0]) + (s[0] > aabb[1][0]); - b = (s[1] < aabb[0][1]) + (s[1] > aabb[1][1]); + a = (c[0] < aabb[0][0]) + (c[0] > aabb[1][0]); + b = (c[1] < aabb[0][1]) + (c[1] > aabb[1][1]); - dmin = glm_pow2((s[0] - aabb[!(a - 1)][0]) * (a != 0)) - + glm_pow2((s[1] - aabb[!(b - 1)][1]) * (b != 0)); + dmin = glm_pow2((c[0] - aabb[!(a - 1)][0]) * (a != 0)) + + glm_pow2((c[1] - aabb[!(b - 1)][1]) * (b != 0)); - return dmin <= glm_pow2(s[3]); + return dmin <= glm_pow2(c[2]); } /*! diff --git a/include/cglm/call/aabb2d.h b/include/cglm/call/aabb2d.h index 6543db8..255227a 100644 --- a/include/cglm/call/aabb2d.h +++ b/include/cglm/call/aabb2d.h @@ -13,6 +13,10 @@ extern "C" { #include "../cglm.h" +CGLM_EXPORT +void +glmc_aabb2d_copy(vec2 aabb[2], vec2 dest[2]); + CGLM_EXPORT void glmc_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]); @@ -70,7 +74,7 @@ glmc_aabb2d_contains(vec2 aabb[2], vec2 other[2]); CGLM_EXPORT bool -glmc_aabb2d_sphere(vec2 aabb[2], vec4 s); +glmc_aabb2d_circle(vec2 aabb[2], vec3 s); #ifdef __cplusplus } diff --git a/include/cglm/struct/aabb2d.h b/include/cglm/struct/aabb2d.h index cad3564..01067ad 100644 --- a/include/cglm/struct/aabb2d.h +++ b/include/cglm/struct/aabb2d.h @@ -221,7 +221,7 @@ glms_aabb2d_(sphere)(vec2s aabb[2], vec4s s) { vec2 rawAabb[2]; glms_vec2_(unpack)(rawAabb, aabb, 2); - return glm_aabb2d_sphere(rawAabb, s.raw); + return glm_aabb2d_circle(rawAabb, s.raw); } /*! diff --git a/src/aabb2d.c b/src/aabb2d.c index e64aa7e..ac8473c 100644 --- a/src/aabb2d.c +++ b/src/aabb2d.c @@ -8,6 +8,12 @@ #include "../include/cglm/cglm.h" #include "../include/cglm/call.h" +CGLM_EXPORT +void +glmc_aabb2d_copy(vec2 aabb[2], vec2 dest[2]) { + glm_aabb2d_copy(aabb, dest); +} + CGLM_EXPORT void glmc_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]) { @@ -91,6 +97,6 @@ glmc_aabb2d_contains(vec2 aabb[2], vec2 other[2]) { CGLM_EXPORT bool -glmc_aabb2d_sphere(vec2 aabb[2], vec4 s) { - return glm_aabb2d_sphere(aabb, s); +glmc_aabb2d_circle(vec2 aabb[2], vec3 s) { + return glm_aabb2d_circle(aabb, s); } From 44d103fa0048c4959cef6ada643d8d8fd80f0c85 Mon Sep 17 00:00:00 2001 From: duarm Date: Sun, 3 Dec 2023 02:35:41 -0300 Subject: [PATCH 03/10] typos --- include/cglm/aabb2d.h | 2 +- include/cglm/struct/aabb2d.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/cglm/aabb2d.h b/include/cglm/aabb2d.h index ed09843..45584fa 100644 --- a/include/cglm/aabb2d.h +++ b/include/cglm/aabb2d.h @@ -227,7 +227,7 @@ glm_aabb2d_aabb(vec2 aabb[2], vec2 other[2]) { } /*! - * @brief check if AABB intersects with sphere + * @brief check if AABB intersects with a circle * * Circle Representation in cglm: [center.x, center.y, radii] * diff --git a/include/cglm/struct/aabb2d.h b/include/cglm/struct/aabb2d.h index 01067ad..9b458de 100644 --- a/include/cglm/struct/aabb2d.h +++ b/include/cglm/struct/aabb2d.h @@ -207,7 +207,7 @@ glms_aabb2d_(aabb)(vec2s aabb[2], vec2s other[2]) { } /*! - * @brief check if AABB intersects with sphere + * @brief check if AABB intersects with a circle * * https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c * Solid Box - Solid Sphere test. @@ -217,11 +217,11 @@ glms_aabb2d_(aabb)(vec2s aabb[2], vec2s other[2]) { */ CGLM_INLINE bool -glms_aabb2d_(sphere)(vec2s aabb[2], vec4s s) { +glms_aabb2d_(circle)(vec2s aabb[2], vec3s c) { vec2 rawAabb[2]; glms_vec2_(unpack)(rawAabb, aabb, 2); - return glm_aabb2d_circle(rawAabb, s.raw); + return glm_aabb2d_circle(rawAabb, c.raw); } /*! From eb73e4123bd38984cb92a71f5af45bdf097ff10e Mon Sep 17 00:00:00 2001 From: duarm Date: Wed, 6 Dec 2023 16:13:08 -0300 Subject: [PATCH 04/10] docs fix --- docs/source/aabb2d.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/source/aabb2d.rst b/docs/source/aabb2d.rst index a4e5b6e..09b15f3 100644 --- a/docs/source/aabb2d.rst +++ b/docs/source/aabb2d.rst @@ -161,7 +161,7 @@ Functions documentation | *[in]* **aabb** bounding box | *[out]* **other** other bounding box -.. c:function:: bool glm_aabb2d_circle(vec2 aabb[2], vec4 s) +.. c:function:: bool glm_aabb2d_circle(vec2 aabb[2], vec3 s) | check if AABB intersects with sphere @@ -170,7 +170,7 @@ Functions documentation Parameters: | *[in]* **aabb** solid bounding box - | *[out]* **s** solid sphere + | *[out]* **c** solid circle .. c:function:: bool glm_aabb2d_point(vec2 aabb[2], vec2 point) From 7061df00667704d4600a249f1d5c0b9d14916ba6 Mon Sep 17 00:00:00 2001 From: duarm Date: Wed, 6 Dec 2023 16:14:59 -0300 Subject: [PATCH 05/10] circle docs fixup --- docs/source/aabb2d.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/aabb2d.rst b/docs/source/aabb2d.rst index 09b15f3..3857781 100644 --- a/docs/source/aabb2d.rst +++ b/docs/source/aabb2d.rst @@ -161,7 +161,7 @@ Functions documentation | *[in]* **aabb** bounding box | *[out]* **other** other bounding box -.. c:function:: bool glm_aabb2d_circle(vec2 aabb[2], vec3 s) +.. c:function:: bool glm_aabb2d_circle(vec2 aabb[2], vec3 c) | check if AABB intersects with sphere From c1d78d835b2d4e65aa1fa10425669da375800298 Mon Sep 17 00:00:00 2001 From: duarm Date: Wed, 6 Dec 2023 16:33:44 -0300 Subject: [PATCH 06/10] changing from mat4 to mat3 --- docs/source/aabb2d.rst | 2 +- include/cglm/aabb2d.h | 6 ++---- include/cglm/call/aabb2d.h | 2 +- include/cglm/struct/aabb2d.h | 2 +- src/aabb2d.c | 2 +- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/docs/source/aabb2d.rst b/docs/source/aabb2d.rst index 3857781..4bdf278 100644 --- a/docs/source/aabb2d.rst +++ b/docs/source/aabb2d.rst @@ -46,7 +46,7 @@ Functions documentation | *[in]* **aabb** bounding box | *[out]* **dest** destination -.. c:function:: void glm_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]) +.. c:function:: void glm_aabb2d_transform(vec2 aabb[2], mat3 m, vec2 dest[2]) | apply transform to Axis-Aligned Bounding Box diff --git a/include/cglm/aabb2d.h b/include/cglm/aabb2d.h index 45584fa..c88d72b 100644 --- a/include/cglm/aabb2d.h +++ b/include/cglm/aabb2d.h @@ -35,8 +35,8 @@ glm_aabb2d_copy(vec2 aabb[2], vec2 dest[2]) { */ CGLM_INLINE void -glm_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]) { - vec2 v[2], xa, xb, ya, yb, za, zb; +glm_aabb2d_transform(vec2 aabb[2], mat3 m, vec2 dest[2]) { + vec2 v[2], xa, xb, ya, yb; glm_vec2_scale(m[0], aabb[0][0], xa); glm_vec2_scale(m[0], aabb[1][0], xb); @@ -48,13 +48,11 @@ glm_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]) { glm_vec2(m[3], v[0]); glm_vec2_minadd(xa, xb, v[0]); glm_vec2_minadd(ya, yb, v[0]); - glm_vec2_minadd(za, zb, v[0]); /* translation + max(xa, xb) + max(ya, yb) + max(za, zb) */ glm_vec2(m[3], v[1]); glm_vec2_maxadd(xa, xb, v[1]); glm_vec2_maxadd(ya, yb, v[1]); - glm_vec2_maxadd(za, zb, v[1]); glm_vec2_copy(v[0], dest[0]); glm_vec2_copy(v[1], dest[1]); diff --git a/include/cglm/call/aabb2d.h b/include/cglm/call/aabb2d.h index 255227a..7e384fd 100644 --- a/include/cglm/call/aabb2d.h +++ b/include/cglm/call/aabb2d.h @@ -19,7 +19,7 @@ glmc_aabb2d_copy(vec2 aabb[2], vec2 dest[2]); CGLM_EXPORT void -glmc_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]); +glmc_aabb2d_transform(vec2 aabb[2], mat3 m, vec2 dest[2]); CGLM_EXPORT void diff --git a/include/cglm/struct/aabb2d.h b/include/cglm/struct/aabb2d.h index 9b458de..b2b4d3c 100644 --- a/include/cglm/struct/aabb2d.h +++ b/include/cglm/struct/aabb2d.h @@ -27,7 +27,7 @@ */ CGLM_INLINE void -glms_aabb2d_(transform)(vec2s aabb[2], mat4s m, vec2s dest[2]) { +glms_aabb2d_(transform)(vec2s aabb[2], mat3s m, vec2s dest[2]) { vec2 rawAabb[2]; vec2 rawDest[2]; diff --git a/src/aabb2d.c b/src/aabb2d.c index ac8473c..8cd3957 100644 --- a/src/aabb2d.c +++ b/src/aabb2d.c @@ -16,7 +16,7 @@ glmc_aabb2d_copy(vec2 aabb[2], vec2 dest[2]) { CGLM_EXPORT void -glmc_aabb2d_transform(vec2 aabb[2], mat4 m, vec2 dest[2]) { +glmc_aabb2d_transform(vec2 aabb[2], mat3 m, vec2 dest[2]) { glm_aabb2d_transform(aabb, m, dest); } From 2106f9ebcbe5e1b16589e8d501e5e4ed4289de8a Mon Sep 17 00:00:00 2001 From: duarm Date: Wed, 6 Dec 2023 16:48:48 -0300 Subject: [PATCH 07/10] remove _aabb2d_frustum --- docs/source/aabb2d.rst | 17 ----------------- include/cglm/aabb2d.h | 32 -------------------------------- include/cglm/call/aabb2d.h | 4 ---- include/cglm/struct/aabb2d.h | 25 ------------------------- src/aabb2d.c | 6 ------ 5 files changed, 84 deletions(-) diff --git a/docs/source/aabb2d.rst b/docs/source/aabb2d.rst index 4bdf278..d7ecdbb 100644 --- a/docs/source/aabb2d.rst +++ b/docs/source/aabb2d.rst @@ -24,7 +24,6 @@ Functions: #. :c:func:`glm_aabb2d_merge` #. :c:func:`glm_aabb2d_crop` #. :c:func:`glm_aabb2d_crop_until` -#. :c:func:`glm_aabb2d_frustum` #. :c:func:`glm_aabb2d_invalidate` #. :c:func:`glm_aabb2d_isvalid` #. :c:func:`glm_aabb2d_size` @@ -93,22 +92,6 @@ Functions documentation | *[in]* **clampAabb** miniumum box | *[out]* **dest** cropped bounding box -.. c:function:: bool glm_aabb2d_frustum(vec2 aabb[2], vec4 planes[6]) - - | check if AABB intersects with frustum planes - - this could be useful for frustum culling using AABB. - - OPTIMIZATION HINT: - if planes order is similar to LEFT, RIGHT, BOTTOM, TOP, NEAR, FAR - then this method should run even faster because it would only use two - planes if object is not inside the two planes - fortunately cglm extracts planes as this order! just pass what you got! - - Parameters: - | *[in]* **aabb** bounding box - | *[out]* **planes** frustum planes - .. c:function:: void glm_aabb2d_invalidate(vec2 aabb[2]) | invalidate AABB min and max values diff --git a/include/cglm/aabb2d.h b/include/cglm/aabb2d.h index c88d72b..cfb3480 100644 --- a/include/cglm/aabb2d.h +++ b/include/cglm/aabb2d.h @@ -121,38 +121,6 @@ glm_aabb2d_crop_until(vec2 aabb[2], glm_aabb2d_merge(clampAabb, dest, dest); } -/*! - * @brief check if AABB intersects with frustum planes - * - * this could be useful for frustum culling using AABB. - * - * OPTIMIZATION HINT: - * if planes order is similar to LEFT, RIGHT, BOTTOM, TOP, NEAR, FAR - * then this method should run even faster because it would only use two - * planes if object is not inside the two planes - * fortunately cglm extracts planes as this order! just pass what you got! - * - * @param[in] aabb bounding aabb - * @param[in] planes frustum planes - */ -CGLM_INLINE -bool -glm_aabb2d_frustum(vec2 aabb[2], vec4 planes[6]) { - float *p, dp; - int i; - - for (i = 0; i < 6; i++) { - p = planes[i]; - dp = p[0] * aabb[p[0] > 0.0f][0] - + p[1] * aabb[p[1] > 0.0f][1]; - - if (dp < -p[3]) - return false; - } - - return true; -} - /*! * @brief invalidate AABB min and max values * diff --git a/include/cglm/call/aabb2d.h b/include/cglm/call/aabb2d.h index 7e384fd..3cc0593 100644 --- a/include/cglm/call/aabb2d.h +++ b/include/cglm/call/aabb2d.h @@ -36,10 +36,6 @@ glmc_aabb2d_crop_until(vec2 aabb[2], vec2 clampAabb[2], vec2 dest[2]); -CGLM_EXPORT -bool -glmc_aabb2d_frustum(vec2 aabb[2], vec4 planes[6]); - CGLM_EXPORT void glmc_aabb2d_invalidate(vec2 aabb[2]); diff --git a/include/cglm/struct/aabb2d.h b/include/cglm/struct/aabb2d.h index b2b4d3c..091e3a1 100644 --- a/include/cglm/struct/aabb2d.h +++ b/include/cglm/struct/aabb2d.h @@ -105,31 +105,6 @@ glms_aabb2d_(crop_until)(vec2s aabb[2], glms_aabb2d_(merge)(clampAabb, dest, dest); } -/*! - * @brief check if AABB intersects with frustum planes - * - * this could be useful for frustum culling using AABB. - * - * OPTIMIZATION HINT: - * if planes order is similar to LEFT, RIGHT, BOTTOM, TOP, NEAR, FAR - * then this method should run even faster because it would only use two - * planes if object is not inside the two planes - * fortunately cglm extracts planes as this order! just pass what you got! - * - * @param[in] aabb bounding box - * @param[in] planes frustum planes - */ -CGLM_INLINE -bool -glms_aabb2d_(frustum)(vec2s box[2], vec4s planes[6]) { - vec2 rawBox[2]; - vec4 rawPlanes[6]; - - glms_vec2_(unpack)(rawBox, box, 2); - glms_vec4_(unpack)(rawPlanes, planes, 6); - return glm_aabb2d_frustum(rawBox, rawPlanes); -} - /*! * @brief invalidate AABB min and max values * diff --git a/src/aabb2d.c b/src/aabb2d.c index 8cd3957..80a3ee0 100644 --- a/src/aabb2d.c +++ b/src/aabb2d.c @@ -41,12 +41,6 @@ glmc_aabb2d_crop_until(vec2 aabb[2], glm_aabb2d_crop_until(aabb, cropAabb, clampAabb, dest); } -CGLM_EXPORT -bool -glmc_aabb2d_frustum(vec2 aabb[2], vec4 planes[6]) { - return glm_aabb2d_frustum(aabb, planes); -} - CGLM_EXPORT void glmc_aabb2d_invalidate(vec2 aabb[2]) { From 340292c0fb185d4891f70d387640da818ac4fe2b Mon Sep 17 00:00:00 2001 From: duarm Date: Wed, 6 Dec 2023 16:52:55 -0300 Subject: [PATCH 08/10] fix transform --- include/cglm/aabb2d.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/cglm/aabb2d.h b/include/cglm/aabb2d.h index cfb3480..ae44dad 100644 --- a/include/cglm/aabb2d.h +++ b/include/cglm/aabb2d.h @@ -45,12 +45,12 @@ glm_aabb2d_transform(vec2 aabb[2], mat3 m, vec2 dest[2]) { glm_vec2_scale(m[1], aabb[1][1], yb); /* translation + min(xa, xb) + min(ya, yb) + min(za, zb) */ - glm_vec2(m[3], v[0]); + glm_vec2(m[2], v[0]); glm_vec2_minadd(xa, xb, v[0]); glm_vec2_minadd(ya, yb, v[0]); /* translation + max(xa, xb) + max(ya, yb) + max(za, zb) */ - glm_vec2(m[3], v[1]); + glm_vec2(m[2], v[1]); glm_vec2_maxadd(xa, xb, v[1]); glm_vec2_maxadd(ya, yb, v[1]); From c431bbf190f210c170a0a7996c84f0ca4e784f60 Mon Sep 17 00:00:00 2001 From: duarm Date: Wed, 6 Dec 2023 16:54:46 -0300 Subject: [PATCH 09/10] fix procedure comment on transform --- include/cglm/aabb2d.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/cglm/aabb2d.h b/include/cglm/aabb2d.h index ae44dad..d7c6eee 100644 --- a/include/cglm/aabb2d.h +++ b/include/cglm/aabb2d.h @@ -44,12 +44,12 @@ glm_aabb2d_transform(vec2 aabb[2], mat3 m, vec2 dest[2]) { glm_vec2_scale(m[1], aabb[0][1], ya); glm_vec2_scale(m[1], aabb[1][1], yb); - /* translation + min(xa, xb) + min(ya, yb) + min(za, zb) */ + /* translation + min(xa, xb) + min(ya, yb) */ glm_vec2(m[2], v[0]); glm_vec2_minadd(xa, xb, v[0]); glm_vec2_minadd(ya, yb, v[0]); - /* translation + max(xa, xb) + max(ya, yb) + max(za, zb) */ + /* translation + max(xa, xb) + max(ya, yb) */ glm_vec2(m[2], v[1]); glm_vec2_maxadd(xa, xb, v[1]); glm_vec2_maxadd(ya, yb, v[1]); From 23d03ffe6c6c29dc74b2647b4dd9d986c8d2f678 Mon Sep 17 00:00:00 2001 From: Valeri Ochinski Date: Fri, 8 Dec 2023 10:14:43 +0300 Subject: [PATCH 10/10] Fix struct clipspace headers Add missing includes, fix signatures --- include/cglm/struct/clipspace/ortho_lh_no.h | 2 ++ include/cglm/struct/clipspace/ortho_lh_zo.h | 2 ++ include/cglm/struct/clipspace/ortho_rh_no.h | 2 ++ include/cglm/struct/clipspace/ortho_rh_zo.h | 2 ++ include/cglm/struct/clipspace/persp_lh_no.h | 1 + include/cglm/struct/clipspace/persp_lh_zo.h | 1 + include/cglm/struct/clipspace/persp_rh_no.h | 1 + include/cglm/struct/clipspace/persp_rh_zo.h | 1 + include/cglm/struct/clipspace/project_no.h | 12 +++++++----- include/cglm/struct/clipspace/project_zo.h | 12 +++++++----- include/cglm/struct/clipspace/view_lh_no.h | 1 + include/cglm/struct/clipspace/view_lh_zo.h | 1 + include/cglm/struct/clipspace/view_rh_no.h | 1 + include/cglm/struct/clipspace/view_rh_zo.h | 1 + 14 files changed, 30 insertions(+), 10 deletions(-) diff --git a/include/cglm/struct/clipspace/ortho_lh_no.h b/include/cglm/struct/clipspace/ortho_lh_no.h index 4060e59..a743fdf 100644 --- a/include/cglm/struct/clipspace/ortho_lh_no.h +++ b/include/cglm/struct/clipspace/ortho_lh_no.h @@ -24,6 +24,8 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../vec3.h" +#include "../../clipspace/ortho_lh_no.h" /*! * @brief set up orthographic projection matrix diff --git a/include/cglm/struct/clipspace/ortho_lh_zo.h b/include/cglm/struct/clipspace/ortho_lh_zo.h index b39a160..4f15656 100644 --- a/include/cglm/struct/clipspace/ortho_lh_zo.h +++ b/include/cglm/struct/clipspace/ortho_lh_zo.h @@ -24,6 +24,8 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../vec3.h" +#include "../../clipspace/ortho_lh_zo.h" /*! * @brief set up orthographic projection matrix diff --git a/include/cglm/struct/clipspace/ortho_rh_no.h b/include/cglm/struct/clipspace/ortho_rh_no.h index ab491af..ecb4d32 100644 --- a/include/cglm/struct/clipspace/ortho_rh_no.h +++ b/include/cglm/struct/clipspace/ortho_rh_no.h @@ -24,6 +24,8 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../vec3.h" +#include "../../clipspace/ortho_rh_no.h" /*! * @brief set up orthographic projection matrix diff --git a/include/cglm/struct/clipspace/ortho_rh_zo.h b/include/cglm/struct/clipspace/ortho_rh_zo.h index 22848d9..2d50ee1 100644 --- a/include/cglm/struct/clipspace/ortho_rh_zo.h +++ b/include/cglm/struct/clipspace/ortho_rh_zo.h @@ -24,6 +24,8 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../vec3.h" +#include "../../clipspace/ortho_rh_zo.h" /*! * @brief set up orthographic projection matrix diff --git a/include/cglm/struct/clipspace/persp_lh_no.h b/include/cglm/struct/clipspace/persp_lh_no.h index f31c4b2..fd3128c 100644 --- a/include/cglm/struct/clipspace/persp_lh_no.h +++ b/include/cglm/struct/clipspace/persp_lh_no.h @@ -39,6 +39,7 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../../clipspace/persp_lh_no.h" /*! * @brief set up perspective peprojection matrix diff --git a/include/cglm/struct/clipspace/persp_lh_zo.h b/include/cglm/struct/clipspace/persp_lh_zo.h index 52f1cc7..c3ade3d 100644 --- a/include/cglm/struct/clipspace/persp_lh_zo.h +++ b/include/cglm/struct/clipspace/persp_lh_zo.h @@ -39,6 +39,7 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../../clipspace/persp_lh_zo.h" /*! * @brief set up perspective peprojection matrix diff --git a/include/cglm/struct/clipspace/persp_rh_no.h b/include/cglm/struct/clipspace/persp_rh_no.h index d382bcf..5d34b90 100644 --- a/include/cglm/struct/clipspace/persp_rh_no.h +++ b/include/cglm/struct/clipspace/persp_rh_no.h @@ -39,6 +39,7 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../../clipspace/persp_rh_no.h" /*! * @brief set up perspective peprojection matrix diff --git a/include/cglm/struct/clipspace/persp_rh_zo.h b/include/cglm/struct/clipspace/persp_rh_zo.h index ca14402..2f98a31 100644 --- a/include/cglm/struct/clipspace/persp_rh_zo.h +++ b/include/cglm/struct/clipspace/persp_rh_zo.h @@ -39,6 +39,7 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../../clipspace/persp_rh_zo.h" /*! * @brief set up perspective peprojection matrix diff --git a/include/cglm/struct/clipspace/project_no.h b/include/cglm/struct/clipspace/project_no.h index a12fb61..1a28d47 100644 --- a/include/cglm/struct/clipspace/project_no.h +++ b/include/cglm/struct/clipspace/project_no.h @@ -7,8 +7,9 @@ /* Functions: - CGLM_INLINE vec3s glms_unprojecti_no(vec3s pos, mat4s invMat, vec4s vp, vec3 dest) - CGLM_INLINE vec3s glms_project_no(vec3s pos, mat4s m, vec4s vp, vec3s dest) + CGLM_INLINE vec3s glms_unprojecti_no(vec3s pos, mat4s invMat, vec4s vp) + CGLM_INLINE vec3s glms_project_no(vec3s pos, mat4s m, vec4s vp) + CGLM_INLINE float glms_project_z_no(vec3s v, mat4s m) */ #ifndef cglms_project_no_h @@ -18,6 +19,7 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../../clipspace/project_no.h" /*! * @brief maps the specified viewport coordinates into specified space [1] @@ -48,7 +50,7 @@ */ CGLM_INLINE vec3s -glms_unprojecti_no(vec3s pos, mat4s invMat, vec4s vp, vec3 dest) { +glms_unprojecti_no(vec3s pos, mat4s invMat, vec4s vp) { vec3s dest; glm_unprojecti_no(pos.raw, invMat.raw, vp.raw, dest.raw); return dest; @@ -69,7 +71,7 @@ glms_unprojecti_no(vec3s pos, mat4s invMat, vec4s vp, vec3 dest) { */ CGLM_INLINE vec3s -glms_project_no(vec3s pos, mat4s m, vec4s vp, vec3s dest) { +glms_project_no(vec3s pos, mat4s m, vec4s vp) { vec3s dest; glm_project_no(pos.raw, m.raw, vp.raw, dest.raw); return dest; @@ -88,7 +90,7 @@ glms_project_no(vec3s pos, mat4s m, vec4s vp, vec3s dest) { * @returns projected z coordinate */ CGLM_INLINE -vec3s +float glms_project_z_no(vec3s v, mat4s m) { return glm_project_z_no(v.raw, m.raw); } diff --git a/include/cglm/struct/clipspace/project_zo.h b/include/cglm/struct/clipspace/project_zo.h index c510396..13065f1 100644 --- a/include/cglm/struct/clipspace/project_zo.h +++ b/include/cglm/struct/clipspace/project_zo.h @@ -7,8 +7,9 @@ /* Functions: - CGLM_INLINE vec3s glms_unprojecti_no(vec3s pos, mat4s invMat, vec4s vp, vec3 dest) - CGLM_INLINE vec3s glms_project_no(vec3s pos, mat4s m, vec4s vp, vec3s dest) + CGLM_INLINE vec3s glms_unprojecti_no(vec3s pos, mat4s invMat, vec4s vp) + CGLM_INLINE vec3s glms_project_no(vec3s pos, mat4s m, vec4s vp) + CGLM_INLINE float glms_project_z_zo(vec3s v, mat4s m) */ #ifndef cglms_project_zo_h @@ -18,6 +19,7 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../../clipspace/project_zo.h" /*! * @brief maps the specified viewport coordinates into specified space [1] @@ -48,7 +50,7 @@ */ CGLM_INLINE vec3s -glms_unprojecti_zo(vec3s pos, mat4s invMat, vec4s vp, vec3 dest) { +glms_unprojecti_zo(vec3s pos, mat4s invMat, vec4s vp) { vec3s dest; glm_unprojecti_zo(pos.raw, invMat.raw, vp.raw, dest.raw); return dest; @@ -69,7 +71,7 @@ glms_unprojecti_zo(vec3s pos, mat4s invMat, vec4s vp, vec3 dest) { */ CGLM_INLINE vec3s -glms_project_zo(vec3s pos, mat4s m, vec4s vp, vec3 dest) { +glms_project_zo(vec3s pos, mat4s m, vec4s vp) { vec3s dest; glm_project_zo(pos.raw, m.raw, vp.raw, dest.raw); return dest; @@ -88,7 +90,7 @@ glms_project_zo(vec3s pos, mat4s m, vec4s vp, vec3 dest) { * @returns projected z coordinate */ CGLM_INLINE -vec3s +float glms_project_z_zo(vec3s v, mat4s m) { return glm_project_z_zo(v.raw, m.raw); } diff --git a/include/cglm/struct/clipspace/view_lh_no.h b/include/cglm/struct/clipspace/view_lh_no.h index cd8b5d9..e4ca5ba 100644 --- a/include/cglm/struct/clipspace/view_lh_no.h +++ b/include/cglm/struct/clipspace/view_lh_no.h @@ -19,6 +19,7 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../../clipspace/view_lh_no.h" /*! * @brief set up view matrix diff --git a/include/cglm/struct/clipspace/view_lh_zo.h b/include/cglm/struct/clipspace/view_lh_zo.h index e2f5f5c..ac1ada9 100644 --- a/include/cglm/struct/clipspace/view_lh_zo.h +++ b/include/cglm/struct/clipspace/view_lh_zo.h @@ -19,6 +19,7 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../../clipspace/view_lh_zo.h" /*! * @brief set up view matrix diff --git a/include/cglm/struct/clipspace/view_rh_no.h b/include/cglm/struct/clipspace/view_rh_no.h index e49e735..99b03c3 100644 --- a/include/cglm/struct/clipspace/view_rh_no.h +++ b/include/cglm/struct/clipspace/view_rh_no.h @@ -19,6 +19,7 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../../clipspace/view_rh_no.h" /*! * @brief set up view matrix diff --git a/include/cglm/struct/clipspace/view_rh_zo.h b/include/cglm/struct/clipspace/view_rh_zo.h index 1347522..14ffe32 100644 --- a/include/cglm/struct/clipspace/view_rh_zo.h +++ b/include/cglm/struct/clipspace/view_rh_zo.h @@ -19,6 +19,7 @@ #include "../../types-struct.h" #include "../../plane.h" #include "../../cam.h" +#include "../../clipspace/view_rh_zo.h" /*! * @brief set up view matrix