AcousticTracer 0.1.0
An acoustic raytracing library.
Loading...
Searching...
No Matches
at_math.h
Go to the documentation of this file.
1
5
6#ifndef AT_MATH_H
7#define AT_MATH_H
8
9#define EPSILON 1e-6f
10
11#include <math.h>
12#include <float.h>
13#include <stdbool.h>
14
25
29typedef union {
30 struct {
31 float x, y, z;
32 };
33 float arr[3];
34} AT_Vec3;
35
39typedef struct {
40 int x;
41 int y;
42 int z;
43} AT_Vec3i;
44
48typedef struct {
51 AT_Vec3 midpoint;
52 float SA;
53} AT_AABB;
54
58typedef struct {
63 bool left;
65
76static inline AT_Vec3 AT_vec3(float x, float y, float z)
77{
78 return (AT_Vec3){{x, y, z}};
79}
80
87static inline AT_Vec3 AT_vec3_zero(void)
88{
89 return (AT_Vec3){{0.0f, 0.0f, 0.0f}};
90}
91
102{
103 return (AT_Vec3){{a.x + b.x, a.y + b.y, a.z + b.z}};
104}
105
116{
117 return (AT_Vec3){{a.x - b.x, a.y - b.y, a.z - b.z}};
118}
119
130{
131 return (AT_Vec3){{a.x * b.x, a.y * b.y, a.z * b.z}};
132}
133
143{
144 return (AT_Vec3){
145 {(fabsf(v.x) < EPSILON ? FLT_MAX : (1.0f / fabsf(v.x))),
146 (fabsf(v.y) < EPSILON ? FLT_MAX : (1.0f / fabsf(v.y))),
147 (fabsf(v.z) < EPSILON ? FLT_MAX : (1.0f / fabsf(v.z)))}
148 };
149}
150
160static inline AT_Vec3 AT_vec3_scale(AT_Vec3 v, float s)
161{
162 return (AT_Vec3){{v.x * s, v.y * s, v.z * s}};
163}
164
174static inline float AT_vec3_dot(AT_Vec3 a, AT_Vec3 b)
175{
176 return a.x * b.x + a.y * b.y + a.z * b.z;
177}
178
189{
190 return (AT_Vec3){
191 {a.y * b.z - a.z * b.y,
192 a.z * b.x - a.x * b.z,
193 a.x * b.y - a.y * b.x}
194 };
195}
196
205static inline float AT_vec3_length(AT_Vec3 v)
206{
207 return sqrtf(AT_vec3_dot(v, v));
208}
209
219{
220 float len = AT_vec3_length(v);
221 return (len > 0.0f) ? AT_vec3_scale(v, 1.0f / len) : AT_vec3_zero();
222}
223
233static inline float AT_vec3_distance(AT_Vec3 a, AT_Vec3 b)
234{
235 return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y) + (b.z - a.z) * (b.z - a.z));
236}
237
247static inline float AT_vec3_distance_sq(AT_Vec3 a, AT_Vec3 b)
248{
249 return (b.x - a.x) * (b.x - a.x) +
250 (b.y - a.y) * (b.y - a.y) +
251 (b.z - a.z) * (b.z - a.z);
252}
253
254/* \brief Computes per axis step distance used in DDA.
255 \relates AT_Vec3
256 \ingroup vec
257
258 \param v Vector being traversed.
259
260 \retval AT_Vec3 Per axis step distance.
261 */
262static inline AT_Vec3 AT_vec3_delta(AT_Vec3 v)
263{
264 return (AT_Vec3){
265 {v.x != 0.0f ? fabsf(1.0f / v.x) : FLT_MAX,
266 v.y != 0.0f ? fabsf(1.0f / v.y) : FLT_MAX,
267 v.z != 0.0f ? fabsf(1.0f / v.z) : FLT_MAX}
268 };
269}
270
271#endif // AT_MATH_H
static AT_Vec3 AT_vec3_inv(AT_Vec3 v)
Calculates the inverse of a vector.
Definition at_math.h:142
static AT_Vec3 AT_vec3_zero(void)
AT_Vec3 constructor for a zero initialised vector.
Definition at_math.h:87
static AT_Vec3 AT_vec3(float x, float y, float z)
AT_Vec3 constructor for a given point.
Definition at_math.h:76
static AT_Vec3 AT_vec3_cross(AT_Vec3 a, AT_Vec3 b)
Performs vector cross multiplication on two given AT_Vec3.
Definition at_math.h:188
static AT_Vec3 AT_vec3_add(AT_Vec3 a, AT_Vec3 b)
Adds two AT_Vec3.
Definition at_math.h:101
static float AT_vec3_dot(AT_Vec3 a, AT_Vec3 b)
Performs vector dot operation on two given AT_Vec3.
Definition at_math.h:174
static AT_Vec3 AT_vec3_scale(AT_Vec3 v, float s)
Scales an AT_Vec3 by a given scalar.
Definition at_math.h:160
static AT_Vec3 AT_vec3_sub(AT_Vec3 a, AT_Vec3 b)
Subtracts two AT_Vec3.
Definition at_math.h:115
static float AT_vec3_distance(AT_Vec3 a, AT_Vec3 b)
Calculates the distance between two AT_Vec3's.
Definition at_math.h:233
static float AT_vec3_length(AT_Vec3 v)
Calculate the length of an AT_Vec3.
Definition at_math.h:205
static float AT_vec3_distance_sq(AT_Vec3 a, AT_Vec3 b)
Calculates the squared distance between two AT_Vec3's.
Definition at_math.h:247
static AT_Vec3 AT_vec3_mul(AT_Vec3 a, AT_Vec3 b)
Product of two AT_Vec3.
Definition at_math.h:129
static AT_Vec3 AT_vec3_normalize(AT_Vec3 v)
Normalise a given vector to be within 0.0 -> 1.0.
Definition at_math.h:218
Axis-Aligned Bounding Box for triangle(s).
Definition at_math.h:48
AT_Vec3 max
Definition at_math.h:50
AT_Vec3 min
Definition at_math.h:49
Groups three AT_Vec3 to represent a triangle.
Definition at_math.h:58
AT_Vec3 v1
Definition at_math.h:59
AT_Vec3 v3
Definition at_math.h:61
AT_AABB aabb
Definition at_math.h:62
AT_Vec3 v2
Definition at_math.h:60
Groups three integers to represent a vector of size 3.
Definition at_math.h:39
int x
Definition at_math.h:40
int y
Definition at_math.h:41
int z
Definition at_math.h:42
Groups three floats to represent a vector of size 3.
Definition at_math.h:29
float arr[3]
Definition at_math.h:33