sphere and aabb

This commit is contained in:
Recep Aslantas
2018-05-30 23:42:22 +03:00
parent 3dc9070909
commit 720b617ee0
10 changed files with 235 additions and 4 deletions

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

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

View File

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