Merge pull request #56 from recp/sphere

aabb and sphere intersect functions
This commit is contained in:
Recep Aslantas
2018-06-09 18:43:54 +03:00
committed by GitHub
24 changed files with 466 additions and 20 deletions

View File

@@ -1,7 +1,7 @@
This library [initially] used some [piece of] implementations
(may include codes) from these open source projects/resources:
1. Affine Transforms
1. Initial Affine Transforms
The original glm repo (g-truc), url: https://github.com/g-truc/glm
LICENSE[S]:
@@ -11,7 +11,7 @@ LICENSE[S]:
FULL LICENSE: https://github.com/g-truc/glm/blob/master/copying.txt
2. Quaternions
2. Initial Quaternions
Anton's OpenGL 4 Tutorials book source code:
LICENSE:
@@ -47,6 +47,8 @@ http://old.cescg.org/CESCG-2002/DSykoraJJelinek/
7. Quaternions
Initial mat4_quat is borrowed from Apple's simd library
8. Vector Rotation using Quaternion
https://gamedev.stackexchange.com/questions/28395/rotating-vector3-by-a-quaternion
9. Sphere AABB intersect
https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c

View File

@@ -7,7 +7,7 @@
#*****************************************************************************
AC_PREREQ([2.69])
AC_INIT([cglm], [0.4.6], [info@recp.me])
AC_INIT([cglm], [0.4.7], [info@recp.me])
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects])
AC_CONFIG_MACRO_DIR([m4])

View File

@@ -45,3 +45,4 @@ Follow the :doc:`build` documentation for this
util
io
call
sphere

View File

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

View File

@@ -62,9 +62,9 @@ author = u'Recep Aslantas'
# built documents.
#
# The short X.Y version.
version = u'0.4.6'
version = u'0.4.7'
# The full version, including alpha/beta/rc tags.
release = u'0.4.6'
release = u'0.4.7'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.

74
docs/source/sphere.rst Normal file
View File

@@ -0,0 +1,74 @@
.. 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`
#. :c:func:`glm_sphere_point`
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
.. c:function:: bool glm_sphere_point(vec4 s, vec3 point)
| check if sphere intersects with point
Parameters:
| *[in]* **s** sphere
| *[in]* **point** point

View File

@@ -56,6 +56,7 @@ Functions:
#. :c:func:`glm_vec_inv_to`
#. :c:func:`glm_vec_normalize`
#. :c:func:`glm_vec_normalize_to`
#. :c:func:`glm_vec_distance2`
#. :c:func:`glm_vec_distance`
#. :c:func:`glm_vec_angle`
#. :c:func:`glm_vec_rotate`
@@ -366,6 +367,17 @@ Functions documentation
| *[in]* **v2** vector2
| *[out]* **dest** center point
.. c:function:: float glm_vec_distance2(vec3 v1, vec3 v2)
squared distance between two vectors
Parameters:
| *[in]* **mat** vector1
| *[in]* **row1** vector2
Returns:
| squared distance (distance * distance)
.. c:function:: float glm_vec_distance(vec3 v1, vec3 v2)
distance between two vectors

View File

@@ -212,4 +212,72 @@ 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
*
* https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c
* Solid Box - Solid Sphere test.
*
* @param[in] box solid bounding box
* @param[in] s solid sphere
*/
CGLM_INLINE
bool
glm_aabb_sphere(vec3 box[2], vec4 s) {
float dmin;
int a, b, c;
a = s[0] >= box[0][0];
b = s[1] >= box[0][1];
c = s[2] >= box[0][2];
dmin = glm_pow2(s[0] - box[a][0])
+ glm_pow2(s[1] - box[b][1])
+ glm_pow2(s[2] - box[c][2]);
return dmin <= glm_pow2(s[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 */

View File

@@ -25,6 +25,7 @@ extern "C" {
#include "call/box.h"
#include "call/io.h"
#include "call/project.h"
#include "call/sphere.h"
#ifdef __cplusplus
}

View File

@@ -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 s);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,36 @@
/*
* 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);
CGLM_EXPORT
bool
glmc_sphere_point(vec4 s, vec3 point);
#endif /* cglmc_sphere_h */

View File

@@ -148,6 +148,10 @@ CGLM_EXPORT
void
glmc_vec_center(vec3 v1, vec3 v2, vec3 dest);
CGLM_EXPORT
float
glmc_vec_distance2(vec3 v1, vec3 v2);
CGLM_EXPORT
float
glmc_vec_distance(vec3 v1, vec3 v2);

View File

@@ -24,5 +24,6 @@
#include "util.h"
#include "io.h"
#include "project.h"
#include "sphere.h"
#endif /* cglm_h */

View File

@@ -646,15 +646,12 @@ glm_quat_slerp(versor from, versor to, float t, versor dest) {
CGLM_INLINE
void
glm_quat_look(vec3 eye, versor ori, mat4 dest) {
CGLM_ALIGN(16) vec4 t;
/* orientation */
glm_quat_mat4t(ori, dest);
/* translate */
glm_vec4(eye, 1.0f, t);
glm_mat4_mulv(dest, t, t);
glm_vec_flipsign_to(t, dest[3]);
glm_mat4_mulv3(dest, eye, 1.0f, dest[3]);
glm_vec_flipsign(dest[3]);
}
/*!

99
include/cglm/sphere.h Normal file
View File

@@ -0,0 +1,99 @@
/*
* Copyright (c), Recep Aslantas.
*
* MIT License (MIT), http://opensource.org/licenses/MIT
* Full license can be found in the LICENSE file
*/
#ifndef cglm_sphere_h
#define cglm_sphere_h
#include "common.h"
#include "mat4.h"
/*
Sphere Representation in cglm: [center.x, center.y, center.z, radii]
You could use this representation or you can convert it to vec4 before call
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) {
glm_mat4_mulv3(m, s, 1.0f, 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) {
float dist, radii;
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]);
}
/*!
* @brief check if sphere intersects with point
*
* @param[in] s sphere
* @param[in] point point
*/
CGLM_INLINE
bool
glm_sphere_point(vec4 s, vec3 point) {
float rr;
rr = s[3] * s[3];
return glm_vec_distance2(point, s) <= rr;
}
#endif /* cglm_sphere_h */

View File

@@ -98,7 +98,6 @@ glm_make_deg(float *rad) {
CGLM_INLINE
float
glm_pow2(float x) {
return x * x;
}

View File

@@ -633,6 +633,21 @@ glm_vec_center(vec3 v1, vec3 v2, vec3 dest) {
glm_vec_scale(dest, 0.5f, dest);
}
/**
* @brief squared distance between two vectors
*
* @param[in] v1 vector1
* @param[in] v2 vector2
* @return returns squared distance (distance * distance)
*/
CGLM_INLINE
float
glm_vec_distance2(vec3 v1, vec3 v2) {
return glm_pow2(v2[0] - v1[0])
+ glm_pow2(v2[1] - v1[1])
+ glm_pow2(v2[2] - v1[2]);
}
/**
* @brief distance between two vectors
*
@@ -643,9 +658,7 @@ glm_vec_center(vec3 v1, vec3 v2, vec3 dest) {
CGLM_INLINE
float
glm_vec_distance(vec3 v1, vec3 v2) {
return sqrtf(glm_pow2(v2[0] - v1[0])
+ glm_pow2(v2[1] - v1[1])
+ glm_pow2(v2[2] - v1[2]));
return sqrtf(glm_vec_distance2(v1, v2));
}
/*!

View File

@@ -10,6 +10,6 @@
#define CGLM_VERSION_MAJOR 0
#define CGLM_VERSION_MINOR 4
#define CGLM_VERSION_PATCH 6
#define CGLM_VERSION_PATCH 7
#endif /* cglm_version_h */

View File

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

View File

@@ -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 s) {
return glm_aabb_sphere(box, s);
}

39
src/sphere.c Normal file
View File

@@ -0,0 +1,39 @@
/*
* 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);
}
CGLM_EXPORT
bool
glmc_sphere_point(vec4 s, vec3 point) {
return glm_sphere_point(s, point);
}

View File

@@ -206,6 +206,12 @@ glmc_vec_center(vec3 v1, vec3 v2, vec3 dest) {
glm_vec_center(v1, v2, dest);
}
CGLM_EXPORT
float
glmc_vec_distance2(vec3 v1, vec3 v2) {
return glm_vec_distance2(v1, v2);
}
CGLM_EXPORT
float
glmc_vec_distance(vec3 v1, vec3 v2) {

View File

@@ -31,6 +31,7 @@
<ClCompile Include="..\src\plane.c" />
<ClCompile Include="..\src\project.c" />
<ClCompile Include="..\src\quat.c" />
<ClCompile Include="..\src\sphere.c" />
<ClCompile Include="..\src\vec3.c" />
<ClCompile Include="..\src\vec4.c" />
</ItemGroup>
@@ -50,6 +51,7 @@
<ClInclude Include="..\include\cglm\call\plane.h" />
<ClInclude Include="..\include\cglm\call\project.h" />
<ClInclude Include="..\include\cglm\call\quat.h" />
<ClInclude Include="..\include\cglm\call\sphere.h" />
<ClInclude Include="..\include\cglm\call\vec3.h" />
<ClInclude Include="..\include\cglm\call\vec4.h" />
<ClInclude Include="..\include\cglm\cam.h" />
@@ -72,6 +74,7 @@
<ClInclude Include="..\include\cglm\simd\sse2\mat3.h" />
<ClInclude Include="..\include\cglm\simd\sse2\mat4.h" />
<ClInclude Include="..\include\cglm\simd\sse2\quat.h" />
<ClInclude Include="..\include\cglm\sphere.h" />
<ClInclude Include="..\include\cglm\types.h" />
<ClInclude Include="..\include\cglm\util.h" />
<ClInclude Include="..\include\cglm\vec3-ext.h" />

View File

@@ -78,6 +78,9 @@
<ClCompile Include="..\src\project.c">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\src\sphere.c">
<Filter>src</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\config.h">
@@ -215,5 +218,11 @@
<ClInclude Include="..\include\cglm\call\project.h">
<Filter>include\cglm\call</Filter>
</ClInclude>
<ClInclude Include="..\include\cglm\call\sphere.h">
<Filter>include\cglm\call</Filter>
</ClInclude>
<ClInclude Include="..\include\cglm\sphere.h">
<Filter>include\cglm</Filter>
</ClInclude>
</ItemGroup>
</Project>