circle fix, new copy func

This commit is contained in:
duarm
2023-12-02 21:00:21 -03:00
parent 056b28e4da
commit 2a975a7d0a
5 changed files with 52 additions and 23 deletions

View File

@@ -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

View File

@@ -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]);
}
/*!

View File

@@ -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
}

View File

@@ -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);
}
/*!

View File

@@ -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);
}