diff options
-rw-r--r-- | include/xas/spatial.h | 28 | ||||
-rw-r--r-- | src/spatial.c | 80 |
2 files changed, 34 insertions, 74 deletions
diff --git a/include/xas/spatial.h b/include/xas/spatial.h index 5c706d0..e888f7b 100644 --- a/include/xas/spatial.h +++ b/include/xas/spatial.h @@ -12,33 +12,17 @@ typedef struct _xas_spatial_coord { float x, y, z; } xas_spatial_coord; -typedef struct _xas_spatial_rotation { - union { - float x; - float pitch; - }; - - union { - float y; - float roll; - }; - - union { - float z; - float yaw; - }; -} xas_spatial_rotation; - typedef struct _xas_spatial_cone { - xas_spatial_coord coord; - xas_spatial_rotation rotation; + xas_spatial_coord coord, + rotation; float angle; } xas_spatial_cone; typedef struct _xas_spatial_observer { - xas_spatial_coord coord; - xas_spatial_rotation rotation; + xas_spatial_coord coord, + rotation; + float width; } xas_spatial_observer; @@ -89,7 +73,7 @@ void xas_spatial_scene_destroy(xas_spatial_scene *scene); void xas_spatial_scene_set_observer(xas_spatial_scene *scene, xas_spatial_coord coord, - xas_spatial_rotation rotation, + xas_spatial_coord rotation, float width); void xas_spatial_scene_set_speaker_coords(xas_spatial_scene *scene, diff --git a/src/spatial.c b/src/spatial.c index 74a0cad..791c6f9 100644 --- a/src/spatial.c +++ b/src/spatial.c @@ -12,29 +12,37 @@ static inline float dist(xas_spatial_coord a, xas_spatial_coord b) { + powf(b.z - a.z, 2.0f), 0.5f); } -static float dotf(float *sets, size_t count, size_t len) { - float ret = 0.0f; - - size_t x, y; +static float dotf(xas_spatial_coord a, xas_spatial_coord b) { + return a.x * b.x + + a.y * b.y + + a.z * b.z; +} - if (count == 0 || len == 0) { - return 0.0f; - } +static void diff(xas_spatial_coord *ret, + xas_spatial_coord a, + xas_spatial_coord b) { + ret->x = a.x - b.x; + ret->y = a.y - b.y; + ret->z = a.z - b.z; +} - for (x=0; x<count; x++) { - float num = sets[x*len+0]; +static float magnf(xas_spatial_coord coord) { + return sqrtf(coord.x * coord.x + + coord.y * coord.y + + coord.z * coord.z); +} - for (y=1; y<len; y++) { - num *= sets[x*len+y]; - } +static inline float degf(float rad) { + float ret = (rad / M_PI) * 180.0f; - ret += num; + while (ret >= 360.0f) { + ret -= 360.0f; } return ret; } -static void rotate(xas_spatial_rotation rotation, +static void rotate(xas_spatial_coord rotation, xas_spatial_coord *coord) { double cosY = cos(rotation.y), sinY = sin(rotation.y), @@ -68,48 +76,16 @@ static void rotate(xas_spatial_rotation rotation, coord->z = Yz; } -static inline float degf(float rad) { - float ret = (rad / M_PI) * 180.0f; - - while (ret >= 360.0f) { - ret -= 360.0f; - } - - return ret; -} - static int within_cone(xas_spatial_coord coord, xas_spatial_cone cone) { - xas_spatial_rotation rotation = { - .x = (M_PI * 2.0f) - cone.rotation.x, - .y = (M_PI * 2.0f) - cone.rotation.y, - .z = (M_PI * 2.0f) - cone.rotation.z - }; - - float radius; + xas_spatial_coord apex_to_x; - coord.x -= cone.coord.x; - coord.y -= cone.coord.y; - coord.z -= cone.coord.z; + float cosf_angle = cosf(cone.angle / 2.0f); - cone.coord.x = 0.0f; - cone.coord.y = 0.0f; - cone.coord.z = 0.0f; - - rotate(rotation, &coord); - - - radius = tanf(cone.angle / 2.0f) * coord.x; - - /* - * If the given point is outside the cone radius at its X coordinate, then - * it is outside of the cone. - */ - if (fabs(coord.y) > radius || fabs(coord.z) > radius) { - return 0; - } + diff(&apex_to_x, cone.coord, coord); - return 1; + return dotf(apex_to_x, cone.rotation) / magnf(apex_to_x) / magnf(cone.rotation) + > cosf_angle; } static int buffer_realloc(xas_spatial_scene *scene, @@ -347,7 +323,7 @@ void xas_spatial_scene_destroy(xas_spatial_scene *scene) { void xas_spatial_scene_set_observer(xas_spatial_scene *scene, xas_spatial_coord coord, - xas_spatial_rotation rotation, + xas_spatial_coord rotation, float width) { scene->observer.coord = coord; scene->observer.rotation = rotation; |