diff --git a/docs/source/api.rst b/docs/source/api.rst index c61365b..d0e4721 100644 --- a/docs/source/api.rst +++ b/docs/source/api.rst @@ -45,3 +45,4 @@ Follow the :doc:`build` documentation for this util io call + sphere diff --git a/docs/source/box.rst b/docs/source/box.rst index 6430c64..7a388e2 100644 --- a/docs/source/box.rst +++ b/docs/source/box.rst @@ -29,6 +29,10 @@ Functions: #. :c:func:`glm_aabb_size` #. :c:func:`glm_aabb_radius` #. :c:func:`glm_aabb_center` +#. :c:func:`glm_aabb_aabb` +#. :c:func:`glm_aabb_sphere` +#. :c:func:`glm_aabb_point` +#. :c:func:`glm_aabb_contains` Functions documentation ~~~~~~~~~~~~~~~~~~~~~~~ @@ -137,6 +141,41 @@ Functions documentation | computes center point of AABB + Parameters: + | *[in]* **box** bounding box + | *[out]* **dest** center of bounding box + +.. c:function:: bool glm_aabb_aabb(vec3 box[2], vec3 other[2]) + + | check if two AABB intersects + Parameters: | *[in]* **box** bounding box - | *[out]* **box** center of bounding box + | *[out]* **other** other bounding box + +.. c:function:: bool glm_aabb_sphere(vec3 box[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]* **box** solid bounding box + | *[out]* **s** solid sphere + +.. c:function:: bool glm_aabb_point(vec3 box[2], vec3 point) + + | check if point is inside of AABB + + Parameters: + | *[in]* **box** bounding box + | *[out]* **point** point + +.. c:function:: bool glm_aabb_contains(vec3 box[2], vec3 other[2]) + + | check if AABB contains other AABB + + Parameters: + | *[in]* **box** bounding box + | *[out]* **other** other bounding box diff --git a/docs/source/sphere.rst b/docs/source/sphere.rst new file mode 100644 index 0000000..427200e --- /dev/null +++ b/docs/source/sphere.rst @@ -0,0 +1,65 @@ +.. default-domain:: C + +Sphere +================================================================================ + +Header: cglm/sphere.h + +**Definition of sphere:** + +Sphere Representation in cglm is *vec4*: **[center.x, center.y, center.z, radii]** + +You can call any vec3 function by pasing sphere. Because first three elements +defines center of sphere. + +Table of contents (click to go): +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Functions: + +1. :c:func:`glm_sphere_radii` +#. :c:func:`glm_sphere_transform` +#. :c:func:`glm_sphere_merge` +#. :c:func:`glm_sphere_sphere` + +Functions documentation +~~~~~~~~~~~~~~~~~~~~~~~ + +.. c:function:: float glm_sphere_radii(vec4 s) + + | helper for getting sphere radius + + Parameters: + | *[in]* **s** sphere + + Returns: + returns radii + +.. c:function:: void glm_sphere_transform(vec4 s, mat4 m, vec4 dest) + + | apply transform to sphere, it is just wrapper for glm_mat4_mulv3 + + Parameters: + | *[in]* **s** sphere + | *[in]* **m** transform matrix + | *[out]* **dest** transformed sphere + +.. c:function:: void glm_sphere_merge(vec4 s1, vec4 s2, vec4 dest) + + | merges two spheres and creates a new one + + two sphere must be in same space, for instance if one in world space then + the other must be in world space too, not in local space. + + Parameters: + | *[in]* **s1** sphere 1 + | *[in]* **s2** sphere 2 + | *[out]* **dest** merged/extended sphere + +.. c:function:: bool glm_sphere_sphere(vec4 s1, vec4 s2) + + | check if two sphere intersects + + Parameters: + | *[in]* **s1** sphere + | *[in]* **s2** other sphere diff --git a/include/cglm/call.h b/include/cglm/call.h index ce08b78..77148ac 100644 --- a/include/cglm/call.h +++ b/include/cglm/call.h @@ -25,6 +25,7 @@ extern "C" { #include "call/box.h" #include "call/io.h" #include "call/project.h" +#include "call/sphere.h" #ifdef __cplusplus } diff --git a/include/cglm/call/sphere.h b/include/cglm/call/sphere.h new file mode 100644 index 0000000..9a9a59f --- /dev/null +++ b/include/cglm/call/sphere.h @@ -0,0 +1,32 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglmc_sphere_h +#define cglmc_sphere_h +#ifdef __cplusplus +extern "C" { +#endif + +#include "../cglm.h" + +CGLM_EXPORT +float +glmc_sphere_radii(vec4 s); + +CGLM_EXPORT +void +glmc_sphere_transform(vec4 s, mat4 m, vec4 dest); + +CGLM_EXPORT +void +glmc_sphere_merge(vec4 s1, vec4 s2, vec4 dest); + +CGLM_EXPORT +bool +glmc_sphere_sphere(vec4 s1, vec4 s2); + +#endif /* cglmc_sphere_h */ diff --git a/include/cglm/sphere.h b/include/cglm/sphere.h index e837972..4c9d79b 100644 --- a/include/cglm/sphere.h +++ b/include/cglm/sphere.h @@ -18,6 +18,26 @@ any function */ +/*! + * @brief helper for getting sphere radius + * + * @param[in] s sphere + * + * @return returns radii + */ +CGLM_INLINE +float +glm_sphere_radii(vec4 s) { + return s[3]; +} + +/*! + * @brief apply transform to sphere, it is just wrapper for glm_mat4_mulv3 + * + * @param[in] s sphere + * @param[in] m transform matrix + * @param[out] dest transformed sphere + */ CGLM_INLINE void glm_sphere_transform(vec4 s, mat4 m, vec4 dest) { @@ -25,6 +45,16 @@ glm_sphere_transform(vec4 s, mat4 m, vec4 dest) { dest[3] = s[3]; } +/*! + * @brief merges two spheres and creates a new one + * + * two sphere must be in same space, for instance if one in world space then + * the other must be in world space too, not in local space. + * + * @param[in] s1 sphere 1 + * @param[in] s2 sphere 2 + * @param[out] dest merged/extended sphere + */ CGLM_INLINE void glm_sphere_merge(vec4 s1, vec4 s2, vec4 dest) { @@ -33,8 +63,23 @@ glm_sphere_merge(vec4 s1, vec4 s2, vec4 dest) { dist = glm_vec_distance(s1, s2); radii = dist + s1[3] + s2[3]; + radii = glm_max(radii, s1[3]); + radii = glm_max(radii, s2[3]); + glm_vec_center(s1, s2, dest); dest[3] = radii; } +/*! + * @brief check if two sphere intersects + * + * @param[in] s1 sphere + * @param[in] s2 other sphere + */ +CGLM_INLINE +bool +glm_sphere_sphere(vec4 s1, vec4 s2) { + return glm_vec_distance2(s1, s2) <= glm_pow2(s1[3] + s2[3]); +} + #endif /* cglm_sphere_h */ diff --git a/makefile.am b/makefile.am index 1999b3d..7ee0904 100644 --- a/makefile.am +++ b/makefile.am @@ -55,7 +55,8 @@ cglm_HEADERS = include/cglm/version.h \ include/cglm/frustum.h \ include/cglm/box.h \ include/cglm/color.h \ - include/cglm/project.h + include/cglm/project.h \ + include/cglm/sphere.h cglm_calldir=$(includedir)/cglm/call cglm_call_HEADERS = include/cglm/call/mat4.h \ @@ -70,7 +71,8 @@ cglm_call_HEADERS = include/cglm/call/mat4.h \ include/cglm/call/plane.h \ include/cglm/call/frustum.h \ include/cglm/call/box.h \ - include/cglm/call/project.h + include/cglm/call/project.h \ + include/cglm/call/sphere.h cglm_simddir=$(includedir)/cglm/simd cglm_simd_HEADERS = include/cglm/simd/intrin.h @@ -101,7 +103,8 @@ libcglm_la_SOURCES=\ src/plane.c \ src/frustum.c \ src/box.c \ - src/project.c + src/project.c \ + src/sphere.c test_tests_SOURCES=\ test/src/test_common.c \ diff --git a/src/sphere.c b/src/sphere.c new file mode 100644 index 0000000..e6b46fc --- /dev/null +++ b/src/sphere.c @@ -0,0 +1,33 @@ +/* + * 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 +float +glmc_sphere_radii(vec4 s) { + return glm_sphere_radii(s); +} + +CGLM_EXPORT +void +glmc_sphere_transform(vec4 s, mat4 m, vec4 dest) { + glm_sphere_transform(s, m, dest); +} + +CGLM_EXPORT +void +glmc_sphere_merge(vec4 s1, vec4 s2, vec4 dest) { + glm_sphere_merge(s1, s2, dest); +} + +CGLM_EXPORT +bool +glmc_sphere_sphere(vec4 s1, vec4 s2) { + return glm_sphere_sphere(s1, s2); +} diff --git a/win/cglm.vcxproj b/win/cglm.vcxproj index 415a87d..556a24e 100644 --- a/win/cglm.vcxproj +++ b/win/cglm.vcxproj @@ -31,6 +31,7 @@ + @@ -50,6 +51,7 @@ + @@ -72,6 +74,7 @@ + diff --git a/win/cglm.vcxproj.filters b/win/cglm.vcxproj.filters index 3090d72..cf7634b 100644 --- a/win/cglm.vcxproj.filters +++ b/win/cglm.vcxproj.filters @@ -78,6 +78,9 @@ src + + src + @@ -215,5 +218,11 @@ include\cglm\call + + include\cglm\call + + + include\cglm + \ No newline at end of file