From 47c21a2035d7c80f0eee7d69499c921b131859e3 Mon Sep 17 00:00:00 2001 From: Mikhail Romanko Date: Wed, 29 Jan 2025 09:14:58 +0300 Subject: Refactor math functions, add unit tests After some testing, I decided to ditch the DirectX approach of writing math functions. Additionally, I added unit tests (with reference wxMaxima calculations) for most math functions and fixed several bugs along the way. --- include/bh/math.h | 1295 ++++++++++++++++++------------------------ src/math.c | 883 ++++++++++++---------------- test/reference/matrix3f.wxmx | Bin 0 -> 5986 bytes test/reference/matrix4f.wxmx | Bin 0 -> 7100 bytes test/reference/point2f.wxmx | Bin 0 -> 8202 bytes test/reference/point3f.wxmx | Bin 0 -> 8290 bytes test/reference/point4f.wxmx | Bin 0 -> 8910 bytes test/src/testmath.c | 825 +++++++++++++++++++++++++++ 8 files changed, 1745 insertions(+), 1258 deletions(-) create mode 100644 test/reference/matrix3f.wxmx create mode 100644 test/reference/matrix4f.wxmx create mode 100644 test/reference/point2f.wxmx create mode 100644 test/reference/point3f.wxmx create mode 100644 test/reference/point4f.wxmx create mode 100644 test/src/testmath.c diff --git a/include/bh/math.h b/include/bh/math.h index 3d3a53d..25aef35 100644 --- a/include/bh/math.h +++ b/include/bh/math.h @@ -103,7 +103,7 @@ typedef struct bh_point4i_s /** * 3x3 floating point matrix. - * + * * Each vector represent one column of the matrix. */ typedef struct bh_matrix3f_s @@ -121,7 +121,7 @@ typedef struct bh_matrix3f_s /** * 4x4 floating point matrix. - * + * * Each vector represent one column of the matrix. */ typedef struct bh_matrix4f_s @@ -153,6 +153,19 @@ typedef struct bh_line2f_s } bh_line2f_t; +/** + * 3D floating point line. + */ +typedef struct bh_line3f_s +{ + /** Origin */ + bh_point3f_t origin; + + /** Normal vector */ + bh_point3f_t normal; +} bh_line3f_t; + + /** * 3D floating point plane. */ @@ -182,15 +195,7 @@ typedef struct bh_ray2f_s /** * 3D floating point ray. */ -typedef struct bh_ray3f_s -{ - /** Origin */ - bh_point3f_t origin; - - /** Normal or direction */ - bh_point3f_t normal; -} bh_ray3f_t; - +typedef bh_line3f_t bh_ray3f_t; /** * 2D floating point AABB @@ -252,106 +257,94 @@ typedef bh_point4f_t bh_quat_t; /** * Adds components of two vectors. - * + * * result = a + b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_add(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); +void bh_point4f_add(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result); /** * Subtracts components of two vectors. - * + * * result = a - b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_sub(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); +void bh_point4f_sub(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result); /** * Multiplies components of two vectors. - * + * * result = a * b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_mul(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); +void bh_point4f_mul(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result); /** * Scales the vector. - * + * * result = a * b - * + * * \param a Value * \param b Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_scale(const bh_point4f_t *a, - float b, - bh_point4f_t *result); +void bh_point4f_scale(const bh_point4f_t *a, + float b, + bh_point4f_t *result); /** * Multiplies and adds three vectors. - * + * * result = a * b + c - * + * * \param a Value * \param b Value * \param c Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_madd(const bh_point4f_t *a, - const bh_point4f_t *b, - const bh_point4f_t *c, - bh_point4f_t *result); +void bh_point4f_madd(const bh_point4f_t *a, + const bh_point4f_t *b, + const bh_point4f_t *c, + bh_point4f_t *result); /** * Negates the vector. - * + * * result = -in - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_negate(const bh_point4f_t *in, - bh_point4f_t *result); +void bh_point4f_negate(const bh_point4f_t *in, + bh_point4f_t *result); /** * Calculates the dot product of two vectors. - * + * * \param a Value * \param b Value - * + * * \return Returns the dot product. */ float bh_point4f_dot(const bh_point4f_t *a, @@ -360,10 +353,10 @@ float bh_point4f_dot(const bh_point4f_t *a, /** * Calculates the dot product of two vectors with w component. - * + * * \param a Value * \param b Value - * + * * \return Returns the dot product. */ float bh_point4f_dot3(const bh_point4f_t *a, @@ -371,26 +364,24 @@ float bh_point4f_dot3(const bh_point4f_t *a, /** - * Calculates the cross product of two vectors without w component. - * + * Calculates the cross product of two vectors without w component. + * * result = a x b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_cross(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); +void bh_point4f_cross(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result); /** * Calculates the length of the vector. - * + * * \param in Value - * + * * \return Returns the length. */ float bh_point4f_length(const bh_point4f_t *in); @@ -398,186 +389,164 @@ float bh_point4f_length(const bh_point4f_t *in); /** * Normilizes the vector. - * + * * result = in / |in| - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_normal(const bh_point4f_t *in, - bh_point4f_t *result); +void bh_point4f_normal(const bh_point4f_t *in, + bh_point4f_t *result); /** * Calculates vector, containing minimum components of two vectors. - * + * * result = min(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_min(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); +void bh_point4f_min(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result); /** * Calculates vector, containing maximum components of two vectors. - * + * * result = max(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_max(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); +void bh_point4f_max(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result); /** * Calculates linear interpolation between two vectors. - * + * * result = a + (b - a) * t - * + * * \param a Value * \param b Value * \param t Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_lerp(const bh_point4f_t *a, - const bh_point4f_t *b, - float t, - bh_point4f_t *result); +void bh_point4f_lerp(const bh_point4f_t *a, + const bh_point4f_t *b, + float t, + bh_point4f_t *result); /** * Calculates spherical linear interpolation between two vectors. - * + * * \param a Value * \param b Value * \param t Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_point4f_slerp(const bh_point4f_t *a, - const bh_point4f_t *b, - float t, - bh_point4f_t *result); +void bh_point4f_slerp(const bh_point4f_t *a, + const bh_point4f_t *b, + float t, + bh_point4f_t *result); /** * Adds components of two vectors. - * + * * result = a + b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_add(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void bh_point3f_add(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result); /** * Subtracts components of two vectors. - * + * * result = a - b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_sub(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void bh_point3f_sub(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result); /** * Multiplies components of two vectors. - * + * * result = a * b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_mul(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void bh_point3f_mul(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result); /** * Scales the vector. - * + * * result = a * b - * + * * \param a Value * \param b Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_scale(const bh_point3f_t *a, - float b, - bh_point3f_t *result); +void bh_point3f_scale(const bh_point3f_t *a, + float b, + bh_point3f_t *result); /** * Multiplies and adds three vectors. - * + * * result = a * b + c - * + * * \param a Value * \param b Value * \param c Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_madd(const bh_point3f_t *a, - const bh_point3f_t *b, - const bh_point3f_t *c, - bh_point3f_t *result); +void bh_point3f_madd(const bh_point3f_t *a, + const bh_point3f_t *b, + const bh_point3f_t *c, + bh_point3f_t *result); /** * Negates the vector. - * + * * result = -in - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_negate(const bh_point3f_t *in, - bh_point3f_t *result); +void bh_point3f_negate(const bh_point3f_t *in, + bh_point3f_t *result); /** * Calculates the dot product of two vectors. - * + * * \param a Value * \param b Value - * + * * \return Returns the dot product. */ float bh_point3f_dot(const bh_point3f_t *a, @@ -585,26 +554,24 @@ float bh_point3f_dot(const bh_point3f_t *a, /** - * Calculates the cross product of two vectors. - * + * Calculates the cross product of two vectors. + * * result = a x b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_cross(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void bh_point3f_cross(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result); /** * Calculates the length of the vector. - * + * * \param in Value - * + * * \return Returns the length. */ float bh_point3f_length(const bh_point3f_t *in); @@ -612,186 +579,164 @@ float bh_point3f_length(const bh_point3f_t *in); /** * Normilizes the vector. - * + * * result = in / |in| - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_normal(const bh_point3f_t *in, - bh_point3f_t *result); +void bh_point3f_normal(const bh_point3f_t *in, + bh_point3f_t *result); /** * Calculates vector, containing minimum components of two vectors. - * + * * result = min(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_min(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void bh_point3f_min(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result); /** * Calculates vector, containing maximum components of two vectors. - * + * * result = max(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_max(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void bh_point3f_max(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result); /** * Calculates linear interpolation between two vectors. - * + * * result = a + (b - a) * t - * + * * \param a Value * \param b Value * \param t Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_lerp(const bh_point3f_t *a, - const bh_point3f_t *b, - float t, - bh_point3f_t *result); +void bh_point3f_lerp(const bh_point3f_t *a, + const bh_point3f_t *b, + float t, + bh_point3f_t *result); /** * Calculates spherical linear interpolation between two vectors. - * + * * \param a Value * \param b Value * \param t Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_point3f_slerp(const bh_point3f_t *a, - const bh_point3f_t *b, - float t, - bh_point3f_t *result); +void bh_point3f_slerp(const bh_point3f_t *a, + const bh_point3f_t *b, + float t, + bh_point3f_t *result); /** * Adds components of two vectors. - * + * * result = a + b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_point2f_add(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void bh_point2f_add(const bh_point2f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result); /** * Subtracts components of two vectors. - * + * * result = a - b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_point2f_sub(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void bh_point2f_sub(const bh_point2f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result); /** * Multiplies components of two vectors. - * + * * result = a * b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_point2f_mul(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void bh_point2f_mul(const bh_point2f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result); /** * Scales the vector. - * + * * result = a * b - * + * * \param a Value * \param b Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_point2f_scale(const bh_point2f_t *a, - float b, - bh_point2f_t *result); +void bh_point2f_scale(const bh_point2f_t *a, + float b, + bh_point2f_t *result); /** * Multiplies and adds three vectors. - * + * * result = a * b + c - * + * * \param a Value * \param b Value * \param c Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_point2f_madd(const bh_point2f_t *a, - const bh_point2f_t *b, - const bh_point2f_t *c, - bh_point2f_t *result); +void bh_point2f_madd(const bh_point2f_t *a, + const bh_point2f_t *b, + const bh_point2f_t *c, + bh_point2f_t *result); /** * Negates the vector. - * + * * result = -in - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_point2f_negate(const bh_point2f_t *in, - bh_point2f_t *result); +void bh_point2f_negate(const bh_point2f_t *in, + bh_point2f_t *result); /** * Calculates the dot product of two vectors. - * + * * \param a Value * \param b Value - * + * * \return Returns the dot product. */ float bh_point2f_dot(const bh_point2f_t *a, @@ -799,13 +744,13 @@ float bh_point2f_dot(const bh_point2f_t *a, /** - * Calculates the cross product of two vectors and returns its z component. - * + * Calculates the cross product of two vectors and returns its z component. + * * result = a x b - * + * * \param a Value * \param b Value - * + * * \return Returns z component of the result vector. */ float bh_point2f_cross(const bh_point2f_t *a, @@ -814,9 +759,9 @@ float bh_point2f_cross(const bh_point2f_t *a, /** * Calculates the length of the vector. - * + * * \param in Value - * + * * \return Returns the length. */ float bh_point2f_length(const bh_point2f_t *in); @@ -824,583 +769,511 @@ float bh_point2f_length(const bh_point2f_t *in); /** * Normilizes the vector. - * + * * result = in / |in| - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_point2f_normal(const bh_point2f_t *in, - bh_point2f_t *result); +void bh_point2f_normal(const bh_point2f_t *in, + bh_point2f_t *result); /** * Calculates vector, containing minimum components of two vectors. - * + * * result = min(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_point2f_min(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void bh_point2f_min(const bh_point2f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result); /** * Calculates vector, containing maximum components of two vectors. - * + * * result = max(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_point2f_max(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void bh_point2f_max(const bh_point2f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result); /** * Calculates linear interpolation between two vectors. - * + * * result = a + (b - a) * t - * + * * \param a Value * \param b Value * \param t Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_point2f_lerp(const bh_point2f_t *a, - const bh_point2f_t *b, - float t, - bh_point2f_t *result); +void bh_point2f_lerp(const bh_point2f_t *a, + const bh_point2f_t *b, + float t, + bh_point2f_t *result); /** * Calculates spherical linear interpolation between two vectors. - * + * * \param a Value * \param b Value * \param t Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_point2f_slerp(const bh_point2f_t *a, - const bh_point2f_t *b, - float t, - bh_point2f_t *result); +void bh_point2f_slerp(const bh_point2f_t *a, + const bh_point2f_t *b, + float t, + bh_point2f_t *result); /** * Adds components of two vectors. - * + * * result = a + b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4i_t *bh_point4i_add(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result); +void bh_point4i_add(const bh_point4i_t *a, + const bh_point4i_t *b, + bh_point4i_t *result); /** * Subtracts components of two vectors. - * + * * result = a - b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4i_t *bh_point4i_sub(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result); +void bh_point4i_sub(const bh_point4i_t *a, + const bh_point4i_t *b, + bh_point4i_t *result); /** * Multiplies components of two vectors. - * + * * result = a * b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4i_t *bh_point4i_mul(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result); +void bh_point4i_mul(const bh_point4i_t *a, + const bh_point4i_t *b, + bh_point4i_t *result); /** * Scales the vector. - * + * * result = a * b - * + * * \param a Value * \param b Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4i_t *bh_point4i_scale(const bh_point4i_t *a, - int b, - bh_point4i_t *result); +void bh_point4i_scale(const bh_point4i_t *a, + int b, + bh_point4i_t *result); /** * Multiplies and adds three vectors. - * + * * result = a * b + c - * + * * \param a Value * \param b Value * \param c Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4i_t *bh_point4i_madd(const bh_point4i_t *a, - const bh_point4i_t *b, - const bh_point4i_t *c, - bh_point4i_t *result); +void bh_point4i_madd(const bh_point4i_t *a, + const bh_point4i_t *b, + const bh_point4i_t *c, + bh_point4i_t *result); /** * Negates the vector. - * + * * result = -in - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4i_t *bh_point4i_negate(const bh_point4i_t *in, - bh_point4i_t *result); +void bh_point4i_negate(const bh_point4i_t *in, + bh_point4i_t *result); /** * Calculates vector, containing minimum components of two vectors. - * + * * result = min(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4i_t *bh_point4i_min(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result); +void bh_point4i_min(const bh_point4i_t *a, + const bh_point4i_t *b, + bh_point4i_t *result); /** * Calculates vector, containing maximum components of two vectors. - * + * * result = max(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4i_t *bh_point4i_max(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result); +void bh_point4i_max(const bh_point4i_t *a, + const bh_point4i_t *b, + bh_point4i_t *result); /** * Calculates linear interpolation between two vectors. - * + * * result = a + (b - a) * t - * + * * \param a Value * \param b Value * \param t Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4i_t *bh_point4i_lerp(const bh_point4i_t *a, - const bh_point4i_t *b, - float t, - bh_point4i_t *result); +void bh_point4i_lerp(const bh_point4i_t *a, + const bh_point4i_t *b, + float t, + bh_point4i_t *result); /** * Adds components of two vectors. - * + * * result = a + b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3i_t *bh_point3i_add(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result); +void bh_point3i_add(const bh_point3i_t *a, + const bh_point3i_t *b, + bh_point3i_t *result); /** * Subtracts components of two vectors. - * + * * result = a - b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3i_t *bh_point3i_sub(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result); +void bh_point3i_sub(const bh_point3i_t *a, + const bh_point3i_t *b, + bh_point3i_t *result); /** * Multiplies components of two vectors. - * + * * result = a * b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3i_t *bh_point3i_mul(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result); +void bh_point3i_mul(const bh_point3i_t *a, + const bh_point3i_t *b, + bh_point3i_t *result); /** * Scales the vector. - * + * * result = a * b - * + * * \param a Value * \param b Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3i_t *bh_point3i_scale(const bh_point3i_t *a, - int b, - bh_point3i_t *result); +void bh_point3i_scale(const bh_point3i_t *a, + int b, + bh_point3i_t *result); /** * Multiplies and adds three vectors. - * + * * result = a * b + c - * + * * \param a Value * \param b Value * \param c Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3i_t *bh_point3i_madd(const bh_point3i_t *a, - const bh_point3i_t *b, - const bh_point3i_t *c, - bh_point3i_t *result); +void bh_point3i_madd(const bh_point3i_t *a, + const bh_point3i_t *b, + const bh_point3i_t *c, + bh_point3i_t *result); /** * Negates the vector. - * + * * result = -in - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3i_t *bh_point3i_negate(const bh_point3i_t *in, - bh_point3i_t *result); +void bh_point3i_negate(const bh_point3i_t *in, + bh_point3i_t *result); /** * Calculates vector, containing minimum components of two vectors. - * + * * result = min(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3i_t *bh_point3i_min(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result); +void bh_point3i_min(const bh_point3i_t *a, + const bh_point3i_t *b, + bh_point3i_t *result); /** * Calculates vector, containing maximum components of two vectors. - * + * * result = max(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3i_t *bh_point3i_max(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result); +void bh_point3i_max(const bh_point3i_t *a, + const bh_point3i_t *b, + bh_point3i_t *result); /** * Calculates linear interpolation between two vectors. - * + * * result = a + (b - a) * t - * + * * \param a Value * \param b Value * \param t Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3i_t *bh_point3i_lerp(const bh_point3i_t *a, - const bh_point3i_t *b, - float t, - bh_point3i_t *result); +void bh_point3i_lerp(const bh_point3i_t *a, + const bh_point3i_t *b, + float t, + bh_point3i_t *result); /** * Adds components of two vectors. - * + * * result = a + b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2i_t *bh_point2i_add(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result); +void bh_point2i_add(const bh_point2i_t *a, + const bh_point2i_t *b, + bh_point2i_t *result); /** * Subtracts components of two vectors. - * + * * result = a - b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2i_t *bh_point2i_sub(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result); +void bh_point2i_sub(const bh_point2i_t *a, + const bh_point2i_t *b, + bh_point2i_t *result); /** * Multiplies components of two vectors. - * + * * result = a * b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2i_t *bh_point2i_mul(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result); +void bh_point2i_mul(const bh_point2i_t *a, + const bh_point2i_t *b, + bh_point2i_t *result); /** * Scales the vector. - * + * * result = a * b - * + * * \param a Value * \param b Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2i_t *bh_point2i_scale(const bh_point2i_t *a, - int b, - bh_point2i_t *result); +void bh_point2i_scale(const bh_point2i_t *a, + int b, + bh_point2i_t *result); /** * Multiplies and adds three vectors. - * + * * result = a * b + c - * + * * \param a Value * \param b Value * \param c Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2i_t *bh_point2i_madd(const bh_point2i_t *a, - const bh_point2i_t *b, - const bh_point2i_t *c, - bh_point2i_t *result); +void bh_point2i_madd(const bh_point2i_t *a, + const bh_point2i_t *b, + const bh_point2i_t *c, + bh_point2i_t *result); /** * Negates the vector. - * + * * result = -in - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2i_t *bh_point2i_negate(const bh_point2i_t *in, - bh_point2i_t *result); +void bh_point2i_negate(const bh_point2i_t *in, + bh_point2i_t *result); /** * Calculates vector, containing minimum components of two vectors. - * + * * result = min(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2i_t *bh_point2i_min(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result); +void bh_point2i_min(const bh_point2i_t *a, + const bh_point2i_t *b, + bh_point2i_t *result); /** * Calculates vector, containing maximum components of two vectors. - * + * * result = max(a, b) - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2i_t *bh_point2i_max(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result); +void bh_point2i_max(const bh_point2i_t *a, + const bh_point2i_t *b, + bh_point2i_t *result); /** * Calculates linear interpolation between two vectors. - * + * * result = a + (b - a) * t - * + * * \param a Value * \param b Value * \param t Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2i_t *bh_point2i_lerp(const bh_point2i_t *a, - const bh_point2i_t *b, - float t, - bh_point2i_t *result); +void bh_point2i_lerp(const bh_point2i_t *a, + const bh_point2i_t *b, + float t, + bh_point2i_t *result); /** * Adds components of two quaternions. - * + * * result = a + b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ #define bh_quat_add bh_point4f_add /** * Subtracts components of two quaternions. - * + * * result = a - b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ #define bh_quat_sub bh_point4f_sub /** * Scales the quaternion. - * + * * result = a * b - * + * * \param a Value * \param b Factor * \param result Result - * - * \return Returns pointer to the result. */ #define bh_quat_scale bh_point4f_scale /** * Negates the quaternion. - * + * * result = -in - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ #define bh_quat_negate bh_point4f_negate /** * Calculates the dot product of two quaternions. - * + * * \param a Value * \param b Value - * + * * \return Returns the dot product. */ #define bh_quat_dot bh_point4f_dot @@ -1408,9 +1281,9 @@ bh_point2i_t *bh_point2i_lerp(const bh_point2i_t *a, /** * Calculates the length of the quaternion. - * + * * \param in Value - * + * * \return Returns the length. */ #define bh_quat_length bh_point4f_length @@ -1418,134 +1291,116 @@ bh_point2i_t *bh_point2i_lerp(const bh_point2i_t *a, /** * Normilizes the quaternion. - * + * * result = in / |in| - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ #define bh_quat_normal bh_point4f_normal /** * Calculates linear interpolation between two quaternions. - * + * * result = a + (b - a) * t - * + * * \param a Value * \param b Value * \param t Factor * \param result Result - * - * \return Returns pointer to the result. */ #define bh_quat_lerp bh_point4f_lerp /** * Calculates spherical linear interpolation between two quaternions. - * + * * \param a Value * \param b Value * \param t Factor * \param result Result - * - * \return Returns pointer to the result. */ #define bh_quat_slerp bh_point4f_slerp /** * Sets quaternions to identity. - * + * * \param result Result - * - * \return Returns pointer to the result. */ -bh_quat_t *bh_quat_identity(bh_quat_t *result); +void bh_quat_identity(bh_quat_t *result); /** * Conjugates the quaternion. - * + * * result = (-in.x, -in.y, -in.z, in.w) - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_quat_t *bh_quat_conjugate(const bh_quat_t *in, - bh_quat_t *result); +void bh_quat_conjugate(const bh_quat_t *in, + bh_quat_t *result); /** * Calculates the inverse of the quaternion. - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_quat_t *bh_quat_inverse(const bh_quat_t *in, - bh_quat_t *result); +void bh_quat_inverse(const bh_quat_t *in, + bh_quat_t *result); /** * Multiplies two quaternions. - * + * * result = a * b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_quat_t *bh_quat_mul(const bh_quat_t *a, - const bh_quat_t *b, - bh_quat_t *result); +void bh_quat_mul(const bh_quat_t *a, + const bh_quat_t *b, + bh_quat_t *result); /** * Sets quaternion from euler angles (roll, pitch, yaw). - * + * * Order of the rotation is ZYX (yaw, pitch, roll). - * + * * \param roll Roll * \param pitch Pitch * \param yaw Yaw * \param result Result - * - * \return Returns pointer to the result. */ -bh_quat_t *bh_quat_set_euler(float roll, - float pitch, - float yaw, - bh_quat_t *result); +void bh_quat_set_euler(float roll, + float pitch, + float yaw, + bh_quat_t *result); /** * Sets quaternion from axis of rotation and angle. - * + * * \param axis Axis * \param angle Angle * \param result Result - * - * \return Returns pointer to the result. */ -bh_quat_t *bh_quat_set_rotation(const bh_point3f_t *axis, - float angle, - bh_quat_t *result); +void bh_quat_set_rotation(const bh_point3f_t *axis, + float angle, + bh_quat_t *result); /** * Calculates euler angles (roll, pitch, yaw) from quaternion. - * + * * Order of the rotation is ZYX (yaw, pitch, roll). - * + * * \param in Value * \param roll Roll * \param pitch Pitch @@ -1559,7 +1414,7 @@ void bh_quat_euler(const bh_quat_t *in, /** * Calculates axis of rotation and angle from quaternion. - * + * * \param in Value * \param axis Axis * \param angle Angle @@ -1571,109 +1426,95 @@ void bh_quat_rotation(const bh_quat_t *in, /** * Calculates rotation matrix from quaternion. - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_quat_matrix(const bh_quat_t *in, - bh_matrix4f_t *result); +void bh_quat_matrix(const bh_quat_t *in, + bh_matrix4f_t *result); /** * Sets matrix to identity. - * + * * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_identity(bh_matrix4f_t *result); +void bh_matrix4f_identity(bh_matrix4f_t *result); /** * Adds two matricies. - * + * * result = a + b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_add(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result); +void bh_matrix4f_add(const bh_matrix4f_t *a, + const bh_matrix4f_t *b, + bh_matrix4f_t *result); /** * Subtracts two matricies. - * + * * result = a - b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_sub(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result); +void bh_matrix4f_sub(const bh_matrix4f_t *a, + const bh_matrix4f_t *b, + bh_matrix4f_t *result); /** * Multiplies two matricies. - * + * * result = a * b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_mul(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result); +void bh_matrix4f_mul(const bh_matrix4f_t *a, + const bh_matrix4f_t *b, + bh_matrix4f_t *result); /** * Scales the matrix. - * + * * result = a * b - * + * * \param a Value * \param b Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_scale(const bh_matrix4f_t *a, - float b, - bh_matrix4f_t *result); +void bh_matrix4f_scale(const bh_matrix4f_t *a, + float b, + bh_matrix4f_t *result); /** * Transposes the matrix. - * + * * result = a^T - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_transpose(const bh_matrix4f_t *in, - bh_matrix4f_t *result); +void bh_matrix4f_transpose(const bh_matrix4f_t *in, + bh_matrix4f_t *result); /** * Calculates the trace of the matrix. * * \param in Value - * + * * \return Returns trace value. */ float bh_matrix4f_trace(const bh_matrix4f_t *in); @@ -1681,9 +1522,9 @@ float bh_matrix4f_trace(const bh_matrix4f_t *in); /** * Calculates the determinant of the matrix. - * + * * \param in Value - * + * * \return Returns determinant value. */ float bh_matrix4f_determinant(const bh_matrix4f_t *in); @@ -1691,136 +1532,120 @@ float bh_matrix4f_determinant(const bh_matrix4f_t *in); /** * Calculates the inverse of the matrix. - * + * * If matrix has no inverse - identity matrix will be returned. - * + * * result = in^-1 - * + * * \param in Value * \param result Result - * + * * \return On success, returns inverse of the matrix. * \return On failure, returns identity matrix. */ -bh_matrix4f_t *bh_matrix4f_inverse(const bh_matrix4f_t *in, - bh_matrix4f_t *result); +int bh_matrix4f_inverse(const bh_matrix4f_t *in, + bh_matrix4f_t *result); /** * Calculates scaling matrix. - * + * * \param x X scale * \param y Y scale * \param z Z scale * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_scaling(float x, - float y, - float z, - bh_matrix4f_t *result); +void bh_matrix4f_scaling(float x, + float y, + float z, + bh_matrix4f_t *result); /** * Calculates translation matrix. - * + * * \param x X scale * \param y Y scale * \param z Z scale * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_translation(float x, - float y, - float z, - bh_matrix4f_t *result); +void bh_matrix4f_translation(float x, + float y, + float z, + bh_matrix4f_t *result); /** * Calculates x-rotation matrix (or rotation around x axis). - * + * * \param angle Angle * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_rotation_x(float angle, - bh_matrix4f_t *result); +void bh_matrix4f_rotation_x(float angle, + bh_matrix4f_t *result); /** * Calculates y-rotation matrix (or rotation around y axis). - * + * * \param angle Angle * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_rotation_y(float angle, - bh_matrix4f_t *result); +void bh_matrix4f_rotation_y(float angle, + bh_matrix4f_t *result); /** * Calculates z-rotation matrix (or rotation around z axis). - * + * * \param angle Angle * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_rotation_z(float angle, - bh_matrix4f_t *result); +void bh_matrix4f_rotation_z(float angle, + bh_matrix4f_t *result); /** * Calculates rotation matrix around axis. - * + * * \param axis Axis * \param angle Angle * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_rotation(const bh_point3f_t *axis, - float angle, - bh_matrix4f_t *result); +void bh_matrix4f_rotation(const bh_point3f_t *axis, + float angle, + bh_matrix4f_t *result); /** * Calculates rotation matrix from euler angles (roll, pitch, yaw). - * + * * Order of the rotation is ZYX (yaw, pitch, roll). - * + * * \param roll Roll * \param pitch Pitch * \param yaw Yaw * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_rotation_euler(float roll, - float pitch, - float yaw, - bh_matrix4f_t *result); +void bh_matrix4f_rotation_euler(float roll, + float pitch, + float yaw, + bh_matrix4f_t *result); /** * Calculates rotation matrix from quaternion. - * + * * \param rotation Quaternion * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_rotation_quat(bh_quat_t *rotation, - bh_matrix4f_t *result); +void bh_matrix4f_rotation_quat(bh_quat_t *rotation, + bh_matrix4f_t *result); /** * Calculates orthographic projection matrix. - * + * * \param x_min Min x * \param x_max Max x * \param y_min Min y @@ -1828,173 +1653,151 @@ bh_matrix4f_t *bh_matrix4f_rotation_quat(bh_quat_t *rotation, * \param z_min Min z * \param z_max Max z * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_ortho(float x_min, - float x_max, - float y_min, - float y_max, - float z_min, - float z_max, - bh_matrix4f_t *result); +void bh_matrix4f_ortho(float x_min, + float x_max, + float y_min, + float y_max, + float z_min, + float z_max, + bh_matrix4f_t *result); /** * Calculates perspective projection matrix. - * + * * \param fov Field of view * \param aspect Aspect ratio * \param z_min Min z * \param z_max Max z * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_perspective(float fov, - float aspect, - float z_min, - float z_max, - bh_matrix4f_t *result); +void bh_matrix4f_perspective(float fov, + float aspect, + float z_min, + float z_max, + bh_matrix4f_t *result); /** * Calculates camera view matrix. - * + * * \param camera Position * \param at Target * \param up Up * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix4f_t *bh_matrix4f_lookat(const bh_point3f_t *camera, - const bh_point3f_t *at, - const bh_point3f_t *up, - bh_matrix4f_t *result); +void bh_matrix4f_lookat(const bh_point3f_t *camera, + const bh_point3f_t *at, + const bh_point3f_t *up, + bh_matrix4f_t *result); /** * Applies matrix to vector. - * + * * \param a Matrix * \param b Vector * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_matrix4f_transform_point3f(const bh_matrix4f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void bh_matrix4f_transform_point3f(const bh_matrix4f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result); /** * Applies matrix to vector. - * + * * \param a Matrix * \param b Vector * \param result Result - * - * \return Returns pointer to the result. */ -bh_point4f_t *bh_matrix4f_transform_point4f(const bh_matrix4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result); +void bh_matrix4f_transform_point4f(const bh_matrix4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result); /** * Sets matrix to identity. - * + * * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix3f_t *bh_matrix3f_identity(bh_matrix3f_t *result); +void bh_matrix3f_identity(bh_matrix3f_t *result); /** * Adds two matricies. - * + * * result = a + b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix3f_t *bh_matrix3f_add(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result); +void bh_matrix3f_add(const bh_matrix3f_t *a, + const bh_matrix3f_t *b, + bh_matrix3f_t *result); /** * Subtracts two matricies. - * + * * result = a - b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix3f_t *bh_matrix3f_sub(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result); +void bh_matrix3f_sub(const bh_matrix3f_t *a, + const bh_matrix3f_t *b, + bh_matrix3f_t *result); /** * Multiplies two matricies. - * + * * result = a * b - * + * * \param a Value * \param b Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix3f_t *bh_matrix3f_mul(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result); +void bh_matrix3f_mul(const bh_matrix3f_t *a, + const bh_matrix3f_t *b, + bh_matrix3f_t *result); /** * Scales the matrix. - * + * * result = a * b - * + * * \param a Value * \param b Factor * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix3f_t *bh_matrix3f_scale(const bh_matrix3f_t *a, - float b, - bh_matrix3f_t *result); +void bh_matrix3f_scale(const bh_matrix3f_t *a, + float b, + bh_matrix3f_t *result); /** * Transposes the matrix. - * + * * result = a^T - * + * * \param in Value * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix3f_t *bh_matrix3f_transpose(const bh_matrix3f_t *in, - bh_matrix3f_t *result); +void bh_matrix3f_transpose(const bh_matrix3f_t *in, + bh_matrix3f_t *result); /** * Calculates the trace of the matrix. * * \param in Value - * + * * \return Returns trace value. */ float bh_matrix3f_trace(const bh_matrix3f_t *in); @@ -2002,9 +1805,9 @@ float bh_matrix3f_trace(const bh_matrix3f_t *in); /** * Calculates the determinant of the matrix. - * + * * \param in Value - * + * * \return Returns determinant value. */ float bh_matrix3f_determinant(const bh_matrix3f_t *in); @@ -2012,89 +1815,79 @@ float bh_matrix3f_determinant(const bh_matrix3f_t *in); /** * Calculates the inverse of the matrix. - * + * * If matrix has no inverse - identity matrix will be returned. - * + * * result = in^-1 - * + * * \param in Value * \param result Result - * + * * \return On success, returns inverse of the matrix. * \return On failure, returns identity matrix. */ -bh_matrix3f_t *bh_matrix3f_inverse(const bh_matrix3f_t *in, - bh_matrix3f_t *result); +int bh_matrix3f_inverse(const bh_matrix3f_t *in, + bh_matrix3f_t *result); /** * Calculates scaling matrix. - * + * * \param x X scale * \param y Y scale * \param z Z scale * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix3f_t *bh_matrix3f_scaling(float x, - float y, - bh_matrix3f_t *result); +void bh_matrix3f_scaling(float x, + float y, + bh_matrix3f_t *result); /** * Calculates translation matrix. - * + * * \param x X scale * \param y Y scale * \param z Z scale * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix3f_t *bh_matrix3f_translation(float x, - float y, - bh_matrix3f_t *result); +void bh_matrix3f_translation(float x, + float y, + bh_matrix3f_t *result); /** * Calculates rotation matrix. - * + * * \param angle Angle * \param result Result - * - * \return Returns pointer to the result. */ -bh_matrix3f_t *bh_matrix3f_rotation(float angle, - bh_matrix3f_t *result); +void bh_matrix3f_rotation(float angle, + bh_matrix3f_t *result); /** * Applies matrix to vector. - * + * * \param a Matrix * \param b Vector * \param result Result - * - * \return Returns pointer to the result. */ -bh_point2f_t *bh_matrix3f_transform_point2f(const bh_matrix3f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result); +void bh_matrix3f_transform_point2f(const bh_matrix3f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result); /** * Applies matrix to vector. - * + * * \param a Matrix * \param b Vector * \param result Result - * - * \return Returns pointer to the result. */ -bh_point3f_t *bh_matrix3f_transform_point3f(const bh_matrix3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result); +void bh_matrix3f_transform_point3f(const bh_matrix3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result); #endif /* BH_MATH_H */ diff --git a/src/math.c b/src/math.c index 18ec135..2e1d0cd 100644 --- a/src/math.c +++ b/src/math.c @@ -2,101 +2,89 @@ #include -bh_point4f_t *bh_point4f_add(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void bh_point4f_add(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result) { result->x = a->x + b->x; result->y = a->y + b->y; result->z = a->z + b->z; result->w = a->w + b->w; - - return result; } -bh_point4f_t *bh_point4f_sub(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void bh_point4f_sub(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result) { result->x = a->x - b->x; result->y = a->y - b->y; result->z = a->z - b->z; result->w = a->w - b->w; - - return result; } -bh_point4f_t *bh_point4f_mul(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void bh_point4f_mul(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result) { result->x = a->x * b->x; result->y = a->y * b->y; result->z = a->z * b->z; result->w = a->w * b->w; - - return result; } -bh_point4f_t *bh_point4f_scale(const bh_point4f_t *a, - float b, - bh_point4f_t *result) +void bh_point4f_scale(const bh_point4f_t *a, + float b, + bh_point4f_t *result) { result->x = a->x * b; result->y = a->y * b; result->z = a->z * b; result->w = a->w * b; - - return result; } -bh_point4f_t *bh_point4f_madd(const bh_point4f_t *a, - const bh_point4f_t *b, - const bh_point4f_t *c, - bh_point4f_t *result) +void bh_point4f_madd(const bh_point4f_t *a, + const bh_point4f_t *b, + const bh_point4f_t *c, + bh_point4f_t *result) { result->x = a->x * b->x + c->x; result->y = a->y * b->y + c->y; result->z = a->z * b->z + c->z; result->w = a->w * b->w + c->w; - - return result; } -bh_point4f_t *bh_point4f_negate(const bh_point4f_t *in, - bh_point4f_t *result) +void bh_point4f_negate(const bh_point4f_t *in, + bh_point4f_t *result) { result->x = -in->x; result->y = -in->y; result->z = -in->z; result->w = -in->w; - - return result; } float bh_point4f_dot(const bh_point4f_t *a, const bh_point4f_t *b) { - return a->x * b->x + a->y * b->y + a->z * b->y + a->w * b->w; + return a->x * b->x + a->y * b->y + a->z * b->z + a->w * b->w; } float bh_point4f_dot3(const bh_point4f_t *a, const bh_point4f_t *b) { - return a->x * b->x + a->y * b->y + a->z * b->y; + return a->x * b->x + a->y * b->y + a->z * b->z; } -bh_point4f_t *bh_point4f_cross(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void bh_point4f_cross(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result) { bh_point4f_t tmp; @@ -106,7 +94,6 @@ bh_point4f_t *bh_point4f_cross(const bh_point4f_t *a, tmp.w = 0.0f; *result = tmp; - return result; } @@ -116,57 +103,55 @@ float bh_point4f_length(const bh_point4f_t *in) } -bh_point4f_t *bh_point4f_normal(const bh_point4f_t *in, - bh_point4f_t *result) +void bh_point4f_normal(const bh_point4f_t *in, + bh_point4f_t *result) { float length; length = 1.0f / bh_point4f_length(in); - return bh_point4f_scale(in, length, result); + bh_point4f_scale(in, length, result); } -bh_point4f_t *bh_point4f_min(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void bh_point4f_min(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result) { if (a->x < b->x) result->x = a->x; else result->x = b->x; if (a->y < b->y) result->y = a->y; else result->y = b->y; if (a->z < b->z) result->z = a->z; else result->z = b->z; if (a->w < b->w) result->w = a->w; else result->w = b->w; - - return result; } -bh_point4f_t *bh_point4f_max(const bh_point4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void bh_point4f_max(const bh_point4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result) { if (a->x > b->x) result->x = a->x; else result->x = b->x; if (a->y > b->y) result->y = a->y; else result->y = b->y; if (a->z > b->z) result->z = a->z; else result->z = b->z; if (a->w > b->w) result->w = a->w; else result->w = b->w; - - return result; } -bh_point4f_t *bh_point4f_lerp(const bh_point4f_t *a, - const bh_point4f_t *b, - float t, - bh_point4f_t *result) +void bh_point4f_lerp(const bh_point4f_t *a, + const bh_point4f_t *b, + float t, + bh_point4f_t *result) { bh_point4f_t tmp; - bh_point4f_scale(bh_point4f_sub(b, a, &tmp), t, &tmp); - return bh_point4f_add(a, &tmp, result); + + bh_point4f_sub(b, a, &tmp); + bh_point4f_scale(&tmp, t, &tmp); + bh_point4f_add(a, &tmp, result); } -bh_point4f_t *bh_point4f_slerp(const bh_point4f_t *a, - const bh_point4f_t *b, - float t, - bh_point4f_t *result) +void bh_point4f_slerp(const bh_point4f_t *a, + const bh_point4f_t *b, + float t, + bh_point4f_t *result) { float angle, denom; bh_point4f_t from, to; @@ -175,97 +160,89 @@ bh_point4f_t *bh_point4f_slerp(const bh_point4f_t *a, /* Special case - reducing to linear interpolation */ if (angle == 0.0f) - return bh_point4f_lerp(a, b, t, result); - - denom = 1.0f / sinf(angle); - bh_point4f_scale(a, sinf((1 - t) * angle) * denom, &from); - bh_point4f_scale(b, sinf(t * angle) * denom, &to); - return bh_point4f_add(&from, &to, result); + { + bh_point4f_lerp(a, b, t, result); + } + else + { + denom = 1.0f / sinf(angle); + bh_point4f_scale(a, sinf((1 - t) * angle) * denom, &from); + bh_point4f_scale(b, sinf(t * angle) * denom, &to); + bh_point4f_add(&from, &to, result); + } } -bh_point3f_t *bh_point3f_add(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void bh_point3f_add(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result) { result->x = a->x + b->x; result->y = a->y + b->y; result->z = a->z + b->z; - - return result; } -bh_point3f_t *bh_point3f_sub(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void bh_point3f_sub(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result) { result->x = a->x - b->x; result->y = a->y - b->y; result->z = a->z - b->z; - - return result; } -bh_point3f_t *bh_point3f_mul(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void bh_point3f_mul(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result) { result->x = a->x * b->x; result->y = a->y * b->y; result->z = a->z * b->z; - - return result; } -bh_point3f_t *bh_point3f_scale(const bh_point3f_t *a, - float b, - bh_point3f_t *result) +void bh_point3f_scale(const bh_point3f_t *a, + float b, + bh_point3f_t *result) { result->x = a->x * b; result->y = a->y * b; result->z = a->z * b; - - return result; } -bh_point3f_t *bh_point3f_madd(const bh_point3f_t *a, - const bh_point3f_t *b, - const bh_point3f_t *c, - bh_point3f_t *result) +void bh_point3f_madd(const bh_point3f_t *a, + const bh_point3f_t *b, + const bh_point3f_t *c, + bh_point3f_t *result) { result->x = a->x * b->x + c->x; result->y = a->y * b->y + c->y; result->z = a->z * b->z + c->z; - - return result; } -bh_point3f_t *bh_point3f_negate(const bh_point3f_t *in, - bh_point3f_t *result) +void bh_point3f_negate(const bh_point3f_t *in, + bh_point3f_t *result) { result->x = -in->x; result->y = -in->y; result->z = -in->z; - - return result; } float bh_point3f_dot(const bh_point3f_t *a, const bh_point3f_t *b) { - return a->x * b->x + a->y * b->y + a->z * b->y; + return a->x * b->x + a->y * b->y + a->z * b->z; } -bh_point3f_t *bh_point3f_cross(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void bh_point3f_cross(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result) { bh_point3f_t tmp; @@ -274,7 +251,6 @@ bh_point3f_t *bh_point3f_cross(const bh_point3f_t *a, tmp.z = a->x * b->y - a->y * b->x; *result = tmp; - return result; } @@ -284,55 +260,53 @@ float bh_point3f_length(const bh_point3f_t *in) } -bh_point3f_t *bh_point3f_normal(const bh_point3f_t *in, - bh_point3f_t *result) +void bh_point3f_normal(const bh_point3f_t *in, + bh_point3f_t *result) { float length; length = 1.0f / bh_point3f_length(in); - return bh_point3f_scale(in, length, result); + bh_point3f_scale(in, length, result); } -bh_point3f_t *bh_point3f_min(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void bh_point3f_min(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result) { if (a->x < b->x) result->x = a->x; else result->x = b->x; if (a->y < b->y) result->y = a->y; else result->y = b->y; if (a->z < b->z) result->z = a->z; else result->z = b->z; - - return result; } -bh_point3f_t *bh_point3f_max(const bh_point3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void bh_point3f_max(const bh_point3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result) { if (a->x > b->x) result->x = a->x; else result->x = b->x; if (a->y > b->y) result->y = a->y; else result->y = b->y; if (a->z > b->z) result->z = a->z; else result->z = b->z; - - return result; } -bh_point3f_t *bh_point3f_lerp(const bh_point3f_t *a, - const bh_point3f_t *b, - float t, - bh_point3f_t *result) +void bh_point3f_lerp(const bh_point3f_t *a, + const bh_point3f_t *b, + float t, + bh_point3f_t *result) { bh_point3f_t tmp; - bh_point3f_scale(bh_point3f_sub(b, a, &tmp), t, &tmp); - return bh_point3f_add(a, &tmp, result); + + bh_point3f_sub(b, a, &tmp); + bh_point3f_scale(&tmp, t, &tmp); + bh_point3f_add(a, &tmp, result); } -bh_point3f_t *bh_point3f_slerp(const bh_point3f_t *a, - const bh_point3f_t *b, - float t, - bh_point3f_t *result) +void bh_point3f_slerp(const bh_point3f_t *a, + const bh_point3f_t *b, + float t, + bh_point3f_t *result) { float angle, denom; bh_point3f_t from, to; @@ -341,78 +315,70 @@ bh_point3f_t *bh_point3f_slerp(const bh_point3f_t *a, /* Special case - reducing to linear interpolation */ if (angle == 0.0f) - return bh_point3f_lerp(a, b, t, result); - - denom = 1.0f / sinf(angle); - bh_point3f_scale(a, sinf((1 - t) * angle) * denom, &from); - bh_point3f_scale(b, sinf(t * angle) * denom, &to); - return bh_point3f_add(&from, &to, result); + { + bh_point3f_lerp(a, b, t, result); + } + else + { + denom = 1.0f / sinf(angle); + bh_point3f_scale(a, sinf((1 - t) * angle) * denom, &from); + bh_point3f_scale(b, sinf(t * angle) * denom, &to); + bh_point3f_add(&from, &to, result); + } } -bh_point2f_t *bh_point2f_add(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void bh_point2f_add(const bh_point2f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result) { result->x = a->x + b->x; result->y = a->y + b->y; - - return result; } -bh_point2f_t *bh_point2f_sub(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void bh_point2f_sub(const bh_point2f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result) { result->x = a->x - b->x; result->y = a->y - b->y; - - return result; } -bh_point2f_t *bh_point2f_mul(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void bh_point2f_mul(const bh_point2f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result) { result->x = a->x * b->x; result->y = a->y * b->y; - - return result; } -bh_point2f_t *bh_point2f_scale(const bh_point2f_t *a, - float b, - bh_point2f_t *result) +void bh_point2f_scale(const bh_point2f_t *a, + float b, + bh_point2f_t *result) { result->x = a->x * b; result->y = a->y * b; - - return result; } -bh_point2f_t *bh_point2f_madd(const bh_point2f_t *a, - const bh_point2f_t *b, - const bh_point2f_t *c, - bh_point2f_t *result) +void bh_point2f_madd(const bh_point2f_t *a, + const bh_point2f_t *b, + const bh_point2f_t *c, + bh_point2f_t *result) { result->x = a->x * b->x + c->x; result->y = a->y * b->y + c->y; - - return result; } -bh_point2f_t *bh_point2f_negate(const bh_point2f_t *in, - bh_point2f_t *result) +void bh_point2f_negate(const bh_point2f_t *in, + bh_point2f_t *result) { result->x = -in->x; result->y = -in->y; - - return result; } @@ -436,53 +402,51 @@ float bh_point2f_length(const bh_point2f_t *in) } -bh_point2f_t *bh_point2f_normal(const bh_point2f_t *in, - bh_point2f_t *result) +void bh_point2f_normal(const bh_point2f_t *in, + bh_point2f_t *result) { float length; length = 1.0f / bh_point2f_length(in); - return bh_point2f_scale(in, length, result); + bh_point2f_scale(in, length, result); } -bh_point2f_t *bh_point2f_min(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void bh_point2f_min(const bh_point2f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result) { if (a->x < b->x) result->x = a->x; else result->x = b->x; if (a->y < b->y) result->y = a->y; else result->y = b->y; - - return result; } -bh_point2f_t *bh_point2f_max(const bh_point2f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void bh_point2f_max(const bh_point2f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result) { if (a->x > b->x) result->x = a->x; else result->x = b->x; if (a->y > b->y) result->y = a->y; else result->y = b->y; - - return result; } -bh_point2f_t *bh_point2f_lerp(const bh_point2f_t *a, - const bh_point2f_t *b, - float t, - bh_point2f_t *result) +void bh_point2f_lerp(const bh_point2f_t *a, + const bh_point2f_t *b, + float t, + bh_point2f_t *result) { bh_point2f_t tmp; - bh_point2f_scale(bh_point2f_sub(b, a, &tmp), t, &tmp); - return bh_point2f_add(a, &tmp, result); + + bh_point2f_sub(b, a, &tmp); + bh_point2f_scale(&tmp, t, &tmp); + bh_point2f_add(a, &tmp, result); } -bh_point2f_t *bh_point2f_slerp(const bh_point2f_t *a, - const bh_point2f_t *b, - float t, - bh_point2f_t *result) +void bh_point2f_slerp(const bh_point2f_t *a, + const bh_point2f_t *b, + float t, + bh_point2f_t *result) { float angle, denom; bh_point2f_t from, to; @@ -491,123 +455,110 @@ bh_point2f_t *bh_point2f_slerp(const bh_point2f_t *a, /* Special case - reducing to linear interpolation */ if (angle == 0.0f) - return bh_point2f_lerp(a, b, t, result); - - denom = 1.0f / sinf(angle); - bh_point2f_scale(a, sinf((1 - t) * angle) * denom, &from); - bh_point2f_scale(b, sinf(t * angle) * denom, &to); - return bh_point2f_add(&from, &to, result); + { + bh_point2f_lerp(a, b, t, result); + } + else { + denom = 1.0f / sinf(angle); + bh_point2f_scale(a, sinf((1 - t) * angle) * denom, &from); + bh_point2f_scale(b, sinf(t * angle) * denom, &to); + bh_point2f_add(&from, &to, result); + } } -bh_point4i_t *bh_point4i_add(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result) +void bh_point4i_add(const bh_point4i_t *a, + const bh_point4i_t *b, + bh_point4i_t *result) { result->x = a->x + b->x; result->y = a->y + b->y; result->z = a->z + b->z; result->w = a->w + b->w; - - return result; } -bh_point4i_t *bh_point4i_sub(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result) +void bh_point4i_sub(const bh_point4i_t *a, + const bh_point4i_t *b, + bh_point4i_t *result) { result->x = a->x - b->x; result->y = a->y - b->y; result->z = a->z - b->z; result->w = a->w - b->w; - - return result; } -bh_point4i_t *bh_point4i_mul(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result) +void bh_point4i_mul(const bh_point4i_t *a, + const bh_point4i_t *b, + bh_point4i_t *result) { result->x = a->x * b->x; result->y = a->y * b->y; result->z = a->z * b->z; result->w = a->w * b->w; - - return result; } -bh_point4i_t *bh_point4i_scale(const bh_point4i_t *a, - int b, - bh_point4i_t *result) +void bh_point4i_scale(const bh_point4i_t *a, + int b, + bh_point4i_t *result) { result->x = a->x * b; result->y = a->y * b; result->z = a->z * b; result->w = a->w * b; - - return result; } -bh_point4i_t *bh_point4i_madd(const bh_point4i_t *a, - const bh_point4i_t *b, - const bh_point4i_t *c, - bh_point4i_t *result) +void bh_point4i_madd(const bh_point4i_t *a, + const bh_point4i_t *b, + const bh_point4i_t *c, + bh_point4i_t *result) { result->x = a->x * b->x + c->x; result->y = a->y * b->y + c->y; result->z = a->z * b->z + c->z; result->w = a->w * b->w + c->w; - - return result; } -bh_point4i_t *bh_point4i_negate(const bh_point4i_t *in, - bh_point4i_t *result) +void bh_point4i_negate(const bh_point4i_t *in, + bh_point4i_t *result) { result->x = -in->x; result->y = -in->y; result->z = -in->z; result->w = -in->w; - - return result; } -bh_point4i_t *bh_point4i_min(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result) +void bh_point4i_min(const bh_point4i_t *a, + const bh_point4i_t *b, + bh_point4i_t *result) { if (a->x < b->x) result->x = a->x; else result->x = b->x; if (a->y < b->y) result->y = a->y; else result->y = b->y; if (a->z < b->z) result->z = a->z; else result->z = b->z; if (a->w < b->w) result->w = a->w; else result->w = b->w; - - return result; } -bh_point4i_t *bh_point4i_max(const bh_point4i_t *a, - const bh_point4i_t *b, - bh_point4i_t *result) +void bh_point4i_max(const bh_point4i_t *a, + const bh_point4i_t *b, + bh_point4i_t *result) { if (a->x > b->x) result->x = a->x; else result->x = b->x; if (a->y > b->y) result->y = a->y; else result->y = b->y; if (a->z > b->z) result->z = a->z; else result->z = b->z; if (a->w > b->w) result->w = a->w; else result->w = b->w; - - return result; } -bh_point4i_t *bh_point4i_lerp(const bh_point4i_t *a, - const bh_point4i_t *b, - float t, - bh_point4i_t *result) +void bh_point4i_lerp(const bh_point4i_t *a, + const bh_point4i_t *b, + float t, + bh_point4i_t *result) { bh_point4i_t tmp; @@ -616,110 +567,94 @@ bh_point4i_t *bh_point4i_lerp(const bh_point4i_t *a, tmp.z = (b->z - a->z) * t; tmp.w = (b->w - a->w) * t; - return bh_point4i_add(a, &tmp, result); + bh_point4i_add(a, &tmp, result); } -bh_point3i_t *bh_point3i_add(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result) +void bh_point3i_add(const bh_point3i_t *a, + const bh_point3i_t *b, + bh_point3i_t *result) { result->x = a->x + b->x; result->y = a->y + b->y; result->z = a->z + b->z; - - return result; } -bh_point3i_t *bh_point3i_sub(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result) +void bh_point3i_sub(const bh_point3i_t *a, + const bh_point3i_t *b, + bh_point3i_t *result) { result->x = a->x - b->x; result->y = a->y - b->y; result->z = a->z - b->z; - - return result; } -bh_point3i_t *bh_point3i_mul(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result) +void bh_point3i_mul(const bh_point3i_t *a, + const bh_point3i_t *b, + bh_point3i_t *result) { result->x = a->x * b->x; result->y = a->y * b->y; result->z = a->z * b->z; - - return result; } -bh_point3i_t *bh_point3i_scale(const bh_point3i_t *a, - int b, - bh_point3i_t *result) +void bh_point3i_scale(const bh_point3i_t *a, + int b, + bh_point3i_t *result) { result->x = a->x * b; result->y = a->y * b; result->z = a->z * b; - - return result; } -bh_point3i_t *bh_point3i_madd(const bh_point3i_t *a, - const bh_point3i_t *b, - const bh_point3i_t *c, - bh_point3i_t *result) +void bh_point3i_madd(const bh_point3i_t *a, + const bh_point3i_t *b, + const bh_point3i_t *c, + bh_point3i_t *result) { result->x = a->x * b->x + c->x; result->y = a->y * b->y + c->y; result->z = a->z * b->z + c->z; - - return result; } -bh_point3i_t *bh_point3i_negate(const bh_point3i_t *in, - bh_point3i_t *result) +void bh_point3i_negate(const bh_point3i_t *in, + bh_point3i_t *result) { result->x = -in->x; result->y = -in->y; result->z = -in->z; - - return result; } -bh_point3i_t *bh_point3i_min(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result) +void bh_point3i_min(const bh_point3i_t *a, + const bh_point3i_t *b, + bh_point3i_t *result) { if (a->x < b->x) result->x = a->x; else result->x = b->x; if (a->y < b->y) result->y = a->y; else result->y = b->y; if (a->z < b->z) result->z = a->z; else result->z = b->z; - - return result; } -bh_point3i_t *bh_point3i_max(const bh_point3i_t *a, - const bh_point3i_t *b, - bh_point3i_t *result) +void bh_point3i_max(const bh_point3i_t *a, + const bh_point3i_t *b, + bh_point3i_t *result) { if (a->x > b->x) result->x = a->x; else result->x = b->x; if (a->y > b->y) result->y = a->y; else result->y = b->y; if (a->z > b->z) result->z = a->z; else result->z = b->z; - - return result; } -bh_point3i_t *bh_point3i_lerp(const bh_point3i_t *a, - const bh_point3i_t *b, - float t, - bh_point3i_t *result) +void bh_point3i_lerp(const bh_point3i_t *a, + const bh_point3i_t *b, + float t, + bh_point3i_t *result) { bh_point3i_t tmp; @@ -727,147 +662,128 @@ bh_point3i_t *bh_point3i_lerp(const bh_point3i_t *a, tmp.y = (b->y - a->y) * t; tmp.z = (b->z - a->z) * t; - return bh_point3i_add(a, &tmp, result); + bh_point3i_add(a, &tmp, result); } -bh_point2i_t *bh_point2i_add(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result) +void bh_point2i_add(const bh_point2i_t *a, + const bh_point2i_t *b, + bh_point2i_t *result) { result->x = a->x + b->x; result->y = a->y + b->y; - - return result; } -bh_point2i_t *bh_point2i_sub(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result) +void bh_point2i_sub(const bh_point2i_t *a, + const bh_point2i_t *b, + bh_point2i_t *result) { result->x = a->x - b->x; result->y = a->y - b->y; - - return result; } -bh_point2i_t *bh_point2i_mul(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result) +void bh_point2i_mul(const bh_point2i_t *a, + const bh_point2i_t *b, + bh_point2i_t *result) { result->x = a->x * b->x; result->y = a->y * b->y; - - return result; } -bh_point2i_t *bh_point2i_scale(const bh_point2i_t *a, - int b, - bh_point2i_t *result) +void bh_point2i_scale(const bh_point2i_t *a, + int b, + bh_point2i_t *result) { result->x = a->x * b; result->y = a->y * b; - - return result; } -bh_point2i_t *bh_point2i_madd(const bh_point2i_t *a, - const bh_point2i_t *b, - const bh_point2i_t *c, - bh_point2i_t *result) +void bh_point2i_madd(const bh_point2i_t *a, + const bh_point2i_t *b, + const bh_point2i_t *c, + bh_point2i_t *result) { result->x = a->x * b->x + c->x; result->y = a->y * b->y + c->y; - - return result; } -bh_point2i_t *bh_point2i_negate(const bh_point2i_t *in, - bh_point2i_t *result) +void bh_point2i_negate(const bh_point2i_t *in, + bh_point2i_t *result) { result->x = -in->x; result->y = -in->y; - - return result; } -bh_point2i_t *bh_point2i_min(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result) +void bh_point2i_min(const bh_point2i_t *a, + const bh_point2i_t *b, + bh_point2i_t *result) { if (a->x < b->x) result->x = a->x; else result->x = b->x; if (a->y < b->y) result->y = a->y; else result->y = b->y; - - return result; } -bh_point2i_t *bh_point2i_max(const bh_point2i_t *a, - const bh_point2i_t *b, - bh_point2i_t *result) +void bh_point2i_max(const bh_point2i_t *a, + const bh_point2i_t *b, + bh_point2i_t *result) { if (a->x > b->x) result->x = a->x; else result->x = b->x; if (a->y > b->y) result->y = a->y; else result->y = b->y; - - return result; } -bh_point2i_t *bh_point2i_lerp(const bh_point2i_t *a, - const bh_point2i_t *b, - float t, - bh_point2i_t *result) +void bh_point2i_lerp(const bh_point2i_t *a, + const bh_point2i_t *b, + float t, + bh_point2i_t *result) { bh_point2i_t tmp; tmp.x = (b->x - a->x) * t; tmp.y = (b->y - a->y) * t; - return bh_point2i_add(a, &tmp, result); + bh_point2i_add(a, &tmp, result); } -bh_quat_t *bh_quat_identity(bh_quat_t *result) +void bh_quat_identity(bh_quat_t *result) { static const bh_quat_t ident = {0.0f, 0.0f, 0.0f, 1.0f}; *result = ident; - return result; } -bh_quat_t *bh_quat_conjugate(const bh_quat_t *in, - bh_quat_t *result) +void bh_quat_conjugate(const bh_quat_t *in, + bh_quat_t *result) { result->x = -in->x; result->y = -in->y; result->z = -in->z; result->w = in->w; - - return result; } -bh_quat_t *bh_quat_inverse(const bh_quat_t *in, - bh_quat_t *result) +void bh_quat_inverse(const bh_quat_t *in, + bh_quat_t *result) { float length; length = bh_quat_dot(in, in); bh_quat_conjugate(in, result); - return bh_quat_scale(result, 1.0f / length, result); + bh_quat_scale(result, 1.0f / length, result); } -bh_quat_t *bh_quat_mul(const bh_quat_t *a, - const bh_quat_t *b, - bh_quat_t *result) +void bh_quat_mul(const bh_quat_t *a, + const bh_quat_t *b, + bh_quat_t *result) { bh_quat_t tmp1, tmp2, tmp3; float w; @@ -880,15 +796,13 @@ bh_quat_t *bh_quat_mul(const bh_quat_t *a, bh_point4f_add(&tmp1, &tmp2, result); bh_point4f_add(&tmp3, result, result); result->w = w; - - return result; } -bh_quat_t *bh_quat_set_euler(float roll, - float pitch, - float yaw, - bh_quat_t *result) +void bh_quat_set_euler(float roll, + float pitch, + float yaw, + bh_quat_t *result) { float cr, cp, cy, sr, sp, sy; @@ -903,14 +817,12 @@ bh_quat_t *bh_quat_set_euler(float roll, result->y = cr * sp * cy + sr * cp * sy; result->z = cr * cp * sy - sr * sp * cy; result->w = cr * cp * cy + sr * sp * sy; - - return result; } -bh_quat_t *bh_quat_set_rotation(const bh_point3f_t *axis, - float angle, - bh_quat_t *result) +void bh_quat_set_rotation(const bh_point3f_t *axis, + float angle, + bh_quat_t *result) { float c, s; @@ -921,8 +833,6 @@ bh_quat_t *bh_quat_set_rotation(const bh_point3f_t *axis, result->y = axis->y * s; result->z = axis->z * s; result->w = c; - - return result; } @@ -994,8 +904,8 @@ void bh_quat_rotation(const bh_quat_t *in, } -bh_matrix4f_t *bh_quat_matrix(const bh_quat_t *in, - bh_matrix4f_t *result) +void bh_quat_matrix(const bh_quat_t *in, + bh_matrix4f_t *result) { float xx, xy, xz, xw, yy, yz, yw, zz, zw; @@ -1019,12 +929,10 @@ bh_matrix4f_t *bh_quat_matrix(const bh_quat_t *in, result->z.x = 2.0f * (xz + yw); result->z.y = 2.0f * (yz - xw); result->z.z = 1.0f - 2.0f * (xx + yy); - - return result; } -bh_matrix4f_t *bh_matrix4f_identity(bh_matrix4f_t *result) +void bh_matrix4f_identity(bh_matrix4f_t *result) { static const bh_matrix4f_t ident = { {1.0f, 0.0f, 0.0f, 0.0f}, @@ -1034,39 +942,34 @@ bh_matrix4f_t *bh_matrix4f_identity(bh_matrix4f_t *result) }; *result = ident; - return result; } -bh_matrix4f_t *bh_matrix4f_add(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result) +void bh_matrix4f_add(const bh_matrix4f_t *a, + const bh_matrix4f_t *b, + bh_matrix4f_t *result) { bh_point4f_add(&a->x, &b->x, &result->x); bh_point4f_add(&a->y, &b->y, &result->y); bh_point4f_add(&a->z, &b->z, &result->z); bh_point4f_add(&a->w, &b->w, &result->w); - - return result; } -bh_matrix4f_t *bh_matrix4f_sub(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result) +void bh_matrix4f_sub(const bh_matrix4f_t *a, + const bh_matrix4f_t *b, + bh_matrix4f_t *result) { bh_point4f_sub(&a->x, &b->x, &result->x); bh_point4f_sub(&a->y, &b->y, &result->y); bh_point4f_sub(&a->z, &b->z, &result->z); bh_point4f_sub(&a->w, &b->w, &result->w); - - return result; } -bh_matrix4f_t *bh_matrix4f_mul(const bh_matrix4f_t *a, - const bh_matrix4f_t *b, - bh_matrix4f_t *result) +void bh_matrix4f_mul(const bh_matrix4f_t *a, + const bh_matrix4f_t *b, + bh_matrix4f_t *result) { bh_matrix4f_t tmp; bh_point4f_t row; @@ -1092,25 +995,22 @@ bh_matrix4f_t *bh_matrix4f_mul(const bh_matrix4f_t *a, row.x = row.y = row.z = row.w = b->w.w; bh_point4f_madd(&a->w, &row, &tmp.w, &tmp.w); *result = tmp; - return result; } -bh_matrix4f_t *bh_matrix4f_scale(const bh_matrix4f_t *a, - float b, - bh_matrix4f_t *result) +void bh_matrix4f_scale(const bh_matrix4f_t *a, + float b, + bh_matrix4f_t *result) { bh_point4f_scale(&a->x, b, &result->x); bh_point4f_scale(&a->y, b, &result->y); bh_point4f_scale(&a->z, b, &result->z); bh_point4f_scale(&a->w, b, &result->w); - - return result; } -bh_matrix4f_t *bh_matrix4f_transpose(const bh_matrix4f_t *in, - bh_matrix4f_t *result) +void bh_matrix4f_transpose(const bh_matrix4f_t *in, + bh_matrix4f_t *result) { bh_matrix4f_t tmp; @@ -1135,7 +1035,6 @@ bh_matrix4f_t *bh_matrix4f_transpose(const bh_matrix4f_t *in, tmp.w.w = in->w.w; *result = tmp; - return result; } @@ -1166,8 +1065,8 @@ float bh_matrix4f_determinant(const bh_matrix4f_t *in) } -bh_matrix4f_t *bh_matrix4f_inverse(const bh_matrix4f_t *in, - bh_matrix4f_t *result) +int bh_matrix4f_inverse(const bh_matrix4f_t *in, + bh_matrix4f_t *result) { float a, b, c, d, e, f, det; bh_matrix4f_t tmp; @@ -1191,7 +1090,7 @@ bh_matrix4f_t *bh_matrix4f_inverse(const bh_matrix4f_t *in, det += in->w.x * tmp.x.w; if (det == 0.0f) - return bh_matrix4f_identity(result); + return BH_ERROR; tmp.y.x = -(in->y.x * f - in->z.x * e + in->w.x * d); tmp.y.y = (in->x.x * f - in->z.x * c + in->w.x * b); @@ -1222,40 +1121,37 @@ bh_matrix4f_t *bh_matrix4f_inverse(const bh_matrix4f_t *in, tmp.w.z = -(in->x.x * e - in->y.x * c + in->w.x * a); tmp.w.w = (in->x.x * d - in->y.x * b + in->z.x * a); - return bh_matrix4f_scale(&tmp, 1.0f / det, result); + bh_matrix4f_scale(&tmp, 1.0f / det, result); + return BH_OK; } -bh_matrix4f_t *bh_matrix4f_scaling(float x, - float y, - float z, - bh_matrix4f_t *result) +void bh_matrix4f_scaling(float x, + float y, + float z, + bh_matrix4f_t *result) { bh_matrix4f_identity(result); result->x.x = x; result->y.y = y; result->z.z = z; - - return result; } -bh_matrix4f_t *bh_matrix4f_translation(float x, - float y, - float z, - bh_matrix4f_t *result) +void bh_matrix4f_translation(float x, + float y, + float z, + bh_matrix4f_t *result) { bh_matrix4f_identity(result); result->w.x = x; result->w.y = y; result->w.z = z; - - return result; } -bh_matrix4f_t *bh_matrix4f_rotation_x(float angle, - bh_matrix4f_t *result) +void bh_matrix4f_rotation_x(float angle, + bh_matrix4f_t *result) { float c, s; @@ -1267,13 +1163,11 @@ bh_matrix4f_t *bh_matrix4f_rotation_x(float angle, result->z.z = c; result->z.y = -s; result->y.z = s; - - return result; } -bh_matrix4f_t *bh_matrix4f_rotation_y(float angle, - bh_matrix4f_t *result) +void bh_matrix4f_rotation_y(float angle, + bh_matrix4f_t *result) { float c, s; @@ -1285,13 +1179,11 @@ bh_matrix4f_t *bh_matrix4f_rotation_y(float angle, result->z.z = c; result->z.x = s; result->x.z = -s; - - return result; } -bh_matrix4f_t *bh_matrix4f_rotation_z(float angle, - bh_matrix4f_t *result) +void bh_matrix4f_rotation_z(float angle, + bh_matrix4f_t *result) { float c, s; @@ -1303,14 +1195,12 @@ bh_matrix4f_t *bh_matrix4f_rotation_z(float angle, result->y.y = c; result->y.x = -s; result->x.y = s; - - return result; } -bh_matrix4f_t *bh_matrix4f_rotation(const bh_point3f_t *axis, - float angle, - bh_matrix4f_t *result) +void bh_matrix4f_rotation(const bh_point3f_t *axis, + float angle, + bh_matrix4f_t *result) { float x, y, z, length; float c, s, moc, xx, xy, xz, yy, yz, zz; @@ -1318,7 +1208,10 @@ bh_matrix4f_t *bh_matrix4f_rotation(const bh_point3f_t *axis, length = bh_point3f_length(axis); if (length == 0.0f) - return bh_matrix4f_identity(result); + { + bh_matrix4f_identity(result); + return; + } x = axis->x / length; y = axis->y / length; @@ -1332,25 +1225,31 @@ bh_matrix4f_t *bh_matrix4f_rotation(const bh_point3f_t *axis, if (z != 0.0f) { if (z < 0.0f) - return bh_matrix4f_rotation_z(-angle, result); + bh_matrix4f_rotation_z(-angle, result); + else + bh_matrix4f_rotation_z(angle, result); - return bh_matrix4f_rotation_z(angle, result); + return; } } else if (z == 0.0f) { if (y < 0.0f) - return bh_matrix4f_rotation_y(-angle, result); + bh_matrix4f_rotation_y(-angle, result); + else + bh_matrix4f_rotation_y(angle, result); - return bh_matrix4f_rotation_y(angle, result); + return; } } else if (y == 0.0f && z == 0.0f) { if (x < 0.0f) - return bh_matrix4f_rotation_x(-angle, result); + bh_matrix4f_rotation_x(-angle, result); + else + bh_matrix4f_rotation_x(angle, result); - return bh_matrix4f_rotation_x(angle, result); + return; } /* Rotate around arbitrary axis */ @@ -1378,15 +1277,13 @@ bh_matrix4f_t *bh_matrix4f_rotation(const bh_point3f_t *axis, result->x.z = xz * moc - y * s; result->y.z = yz * moc + x * s; result->z.z = c + zz * moc; - - return result; } -bh_matrix4f_t *bh_matrix4f_rotation_euler(float roll, - float pitch, - float yaw, - bh_matrix4f_t *result) +void bh_matrix4f_rotation_euler(float roll, + float pitch, + float yaw, + bh_matrix4f_t *result) { float rs, rc, ys, yc, ps, pc; @@ -1407,25 +1304,23 @@ bh_matrix4f_t *bh_matrix4f_rotation_euler(float roll, result->z.x = rs * ys + ps * rc * yc; result->z.y = ps * rc * ys - rs * yc; result->z.z = pc * rc; - - return result; } -bh_matrix4f_t *bh_matrix4f_rotation_quat(bh_quat_t *rotation, - bh_matrix4f_t *result) +void bh_matrix4f_rotation_quat(bh_quat_t *rotation, + bh_matrix4f_t *result) { - return bh_quat_matrix(rotation, result); + bh_quat_matrix(rotation, result); } -bh_matrix4f_t *bh_matrix4f_ortho(float x_min, - float x_max, - float y_min, - float y_max, - float z_min, - float z_max, - bh_matrix4f_t *result) +void bh_matrix4f_ortho(float x_min, + float x_max, + float y_min, + float y_max, + float z_min, + float z_max, + bh_matrix4f_t *result) { float dx, dy, dz; @@ -1441,16 +1336,14 @@ bh_matrix4f_t *bh_matrix4f_ortho(float x_min, result->w.x = -(x_max + x_min) / dx; result->w.y = -(y_max + y_min) / dy; result->w.z = -(z_max + z_min) / dz; - - return result; } -bh_matrix4f_t *bh_matrix4f_perspective(float fov, - float aspect, - float z_min, - float z_max, - bh_matrix4f_t *result) +void bh_matrix4f_perspective(float fov, + float aspect, + float z_min, + float z_max, + bh_matrix4f_t *result) { float t, dz; @@ -1465,15 +1358,13 @@ bh_matrix4f_t *bh_matrix4f_perspective(float fov, result->z.w = -1.0f; result->w.z = -(2.0f * z_max * z_min) / dz; result->w.w = 0.0f; - - return result; } -bh_matrix4f_t *bh_matrix4f_lookat(const bh_point3f_t *camera, - const bh_point3f_t *at, - const bh_point3f_t *up, - bh_matrix4f_t *result) +void bh_matrix4f_lookat(const bh_point3f_t *camera, + const bh_point3f_t *at, + const bh_point3f_t *up, + bh_matrix4f_t *result) { bh_point3f_t cdir, cright, cup; @@ -1500,14 +1391,12 @@ bh_matrix4f_t *bh_matrix4f_lookat(const bh_point3f_t *camera, result->w.y = -bh_point3f_dot(&cup, camera); result->w.z = -bh_point3f_dot(&cdir, camera); result->w.w = 1.0f; - - return result; } -bh_point3f_t *bh_matrix4f_transform_point3f(const bh_matrix4f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void bh_matrix4f_transform_point3f(const bh_matrix4f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result) { bh_point4f_t tmp, row; @@ -1519,13 +1408,12 @@ bh_point3f_t *bh_matrix4f_transform_point3f(const bh_matrix4f_t *a, result->x = tmp.x; result->y = tmp.y; result->z = tmp.z; - return result; } -bh_point4f_t *bh_matrix4f_transform_point4f(const bh_matrix4f_t *a, - const bh_point4f_t *b, - bh_point4f_t *result) +void bh_matrix4f_transform_point4f(const bh_matrix4f_t *a, + const bh_point4f_t *b, + bh_point4f_t *result) { bh_point4f_t tmp, row; @@ -1535,11 +1423,10 @@ bh_point4f_t *bh_matrix4f_transform_point4f(const bh_matrix4f_t *a, row.x = row.y = row.z = row.w = b->w; bh_point4f_madd(&a->w, &row, &tmp, &tmp); *result = tmp; - return result; } -bh_matrix3f_t *bh_matrix3f_identity(bh_matrix3f_t *result) +void bh_matrix3f_identity(bh_matrix3f_t *result) { static const bh_matrix3f_t ident = { {1.0f, 0.0f, 0.0f}, @@ -1548,37 +1435,32 @@ bh_matrix3f_t *bh_matrix3f_identity(bh_matrix3f_t *result) }; *result = ident; - return result; } -bh_matrix3f_t *bh_matrix3f_add(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result) +void bh_matrix3f_add(const bh_matrix3f_t *a, + const bh_matrix3f_t *b, + bh_matrix3f_t *result) { bh_point3f_add(&a->x, &b->x, &result->x); bh_point3f_add(&a->y, &b->y, &result->y); bh_point3f_add(&a->z, &b->z, &result->z); - - return result; } -bh_matrix3f_t *bh_matrix3f_sub(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result) +void bh_matrix3f_sub(const bh_matrix3f_t *a, + const bh_matrix3f_t *b, + bh_matrix3f_t *result) { bh_point3f_sub(&a->x, &b->x, &result->x); bh_point3f_sub(&a->y, &b->y, &result->y); bh_point3f_sub(&a->z, &b->z, &result->z); - - return result; } -bh_matrix3f_t *bh_matrix3f_mul(const bh_matrix3f_t *a, - const bh_matrix3f_t *b, - bh_matrix3f_t *result) +void bh_matrix3f_mul(const bh_matrix3f_t *a, + const bh_matrix3f_t *b, + bh_matrix3f_t *result) { bh_matrix3f_t tmp; bh_point3f_t row; @@ -1596,24 +1478,21 @@ bh_matrix3f_t *bh_matrix3f_mul(const bh_matrix3f_t *a, row.x = row.y = row.z = b->z.z; bh_point3f_madd(&a->z, &row, &tmp.z, &tmp.z); *result = tmp; - return result; } -bh_matrix3f_t *bh_matrix3f_scale(const bh_matrix3f_t *a, - float b, - bh_matrix3f_t *result) +void bh_matrix3f_scale(const bh_matrix3f_t *a, + float b, + bh_matrix3f_t *result) { bh_point3f_scale(&a->x, b, &result->x); bh_point3f_scale(&a->y, b, &result->y); bh_point3f_scale(&a->z, b, &result->z); - - return result; } -bh_matrix3f_t *bh_matrix3f_transpose(const bh_matrix3f_t *in, - bh_matrix3f_t *result) +void bh_matrix3f_transpose(const bh_matrix3f_t *in, + bh_matrix3f_t *result) { bh_matrix3f_t tmp; @@ -1630,7 +1509,6 @@ bh_matrix3f_t *bh_matrix3f_transpose(const bh_matrix3f_t *in, tmp.z.z = in->z.z; *result = tmp; - return result; } @@ -1657,8 +1535,8 @@ float bh_matrix3f_determinant(const bh_matrix3f_t *in) } -bh_matrix3f_t *bh_matrix3f_inverse(const bh_matrix3f_t *in, - bh_matrix3f_t *result) +int bh_matrix3f_inverse(const bh_matrix3f_t *in, + bh_matrix3f_t *result) { float a, b, c, det; bh_matrix3f_t tmp; @@ -1677,7 +1555,7 @@ bh_matrix3f_t *bh_matrix3f_inverse(const bh_matrix3f_t *in, det += in->z.x * tmp.x.z; if (det == 0.0f) - return bh_matrix3f_identity(result); + return BH_ERROR; a = in->y.x * in->z.z - in->z.x * in->y.z; b = in->x.x * in->z.z - in->z.x * in->x.z; @@ -1695,38 +1573,35 @@ bh_matrix3f_t *bh_matrix3f_inverse(const bh_matrix3f_t *in, tmp.z.y = -b; tmp.z.z = c; - return bh_matrix3f_scale(&tmp, 1.0f / det, result); + bh_matrix3f_scale(&tmp, 1.0f / det, result); + return BH_OK; } -bh_matrix3f_t *bh_matrix3f_scaling(float x, - float y, - bh_matrix3f_t *result) +void bh_matrix3f_scaling(float x, + float y, + bh_matrix3f_t *result) { bh_matrix3f_identity(result); result->x.x = x; result->y.y = y; - - return result; } -bh_matrix3f_t *bh_matrix3f_translation(float x, - float y, - bh_matrix3f_t *result) +void bh_matrix3f_translation(float x, + float y, + bh_matrix3f_t *result) { bh_matrix3f_identity(result); result->z.x = x; result->z.y = y; - - return result; } -bh_matrix3f_t *bh_matrix3f_rotation(float angle, - bh_matrix3f_t *result) +void bh_matrix3f_rotation(float angle, + bh_matrix3f_t *result) { float c, s; @@ -1738,14 +1613,12 @@ bh_matrix3f_t *bh_matrix3f_rotation(float angle, result->y.y = c; result->y.x = -s; result->x.y = s; - - return result; } -bh_point2f_t *bh_matrix3f_transform_point2f(const bh_matrix3f_t *a, - const bh_point2f_t *b, - bh_point2f_t *result) +void bh_matrix3f_transform_point2f(const bh_matrix3f_t *a, + const bh_point2f_t *b, + bh_point2f_t *result) { bh_point3f_t tmp, row; @@ -1755,14 +1628,12 @@ bh_point2f_t *bh_matrix3f_transform_point2f(const bh_matrix3f_t *a, result->x = tmp.x; result->y = tmp.y; - - return result; } -bh_point3f_t *bh_matrix3f_transform_point3f(const bh_matrix3f_t *a, - const bh_point3f_t *b, - bh_point3f_t *result) +void bh_matrix3f_transform_point3f(const bh_matrix3f_t *a, + const bh_point3f_t *b, + bh_point3f_t *result) { bh_point3f_t tmp, row; @@ -1773,6 +1644,4 @@ bh_point3f_t *bh_matrix3f_transform_point3f(const bh_matrix3f_t *a, result->x = tmp.x; result->y = tmp.y; result->z = tmp.z; - - return result; } diff --git a/test/reference/matrix3f.wxmx b/test/reference/matrix3f.wxmx new file mode 100644 index 0000000..592ce61 Binary files /dev/null and b/test/reference/matrix3f.wxmx differ diff --git a/test/reference/matrix4f.wxmx b/test/reference/matrix4f.wxmx new file mode 100644 index 0000000..0195e40 Binary files /dev/null and b/test/reference/matrix4f.wxmx differ diff --git a/test/reference/point2f.wxmx b/test/reference/point2f.wxmx new file mode 100644 index 0000000..76cb024 Binary files /dev/null and b/test/reference/point2f.wxmx differ diff --git a/test/reference/point3f.wxmx b/test/reference/point3f.wxmx new file mode 100644 index 0000000..a8c30c8 Binary files /dev/null and b/test/reference/point3f.wxmx differ diff --git a/test/reference/point4f.wxmx b/test/reference/point4f.wxmx new file mode 100644 index 0000000..10af820 Binary files /dev/null and b/test/reference/point4f.wxmx differ diff --git a/test/src/testmath.c b/test/src/testmath.c new file mode 100644 index 0000000..c927861 --- /dev/null +++ b/test/src/testmath.c @@ -0,0 +1,825 @@ +#include +#include + + +#define ACCEPTABLE_DELTA 0.0001f + + +static int check_point4f(void) +{ + bh_point4f_t a, b, c, r; + float value; + + a.x = 1.0f; a.y = 2.0f; a.z = 3.0f; a.w = 4.0f; + b.x = 5.0f; b.y = 6.0f; b.z = 7.0f; b.w = 8.0f; + c.x = 1.5f; c.y = 2.5f; c.z = 3.5f; c.w = 4.5f; + + bh_point4f_add(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 8.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 12.0000f, ACCEPTABLE_DELTA); + + bh_point4f_sub(&a, &b, &r); + BH_VERIFY_DELTA(r.x, -4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, -4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, -4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, -4.0000f, ACCEPTABLE_DELTA); + + bh_point4f_mul(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 12.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 21.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 32.0000f, ACCEPTABLE_DELTA); + + bh_point4f_scale(&a, 10.0f, &r); + BH_VERIFY_DELTA(r.x, 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 20.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 30.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 40.0000f, ACCEPTABLE_DELTA); + + bh_point4f_madd(&a, &b, &c, &r); + BH_VERIFY_DELTA(r.x, 6.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 14.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 24.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 36.5000f, ACCEPTABLE_DELTA); + + bh_point4f_negate(&a, &r); + BH_VERIFY_DELTA(r.x, -1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, -2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, -3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, -4.0000f, ACCEPTABLE_DELTA); + + value = bh_point4f_dot(&a, &b); + BH_VERIFY_DELTA(value, 70.0000f, ACCEPTABLE_DELTA); + + value = bh_point4f_dot3(&a, &b); + BH_VERIFY_DELTA(value, 38.0000f, ACCEPTABLE_DELTA); + + bh_point4f_cross(&a, &b, &r); + BH_VERIFY_DELTA(r.x, -4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 8.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, -4.0000f, ACCEPTABLE_DELTA); + + value = bh_point4f_length(&a); + BH_VERIFY_DELTA(value, sqrt(30.0f), ACCEPTABLE_DELTA); + + bh_point4f_normal(&a, &r); + BH_VERIFY_DELTA(r.x, 1.0f / sqrt(30.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0f / sqrt(30.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 3.0f / sqrt(30.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 4.0f / sqrt(30.0f), ACCEPTABLE_DELTA); + + bh_point4f_min(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 4.0000f, ACCEPTABLE_DELTA); + + bh_point4f_max(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 7.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 8.0000f, ACCEPTABLE_DELTA); + + bh_point4f_lerp(&a, &b, 0.0f, &r); + BH_VERIFY_DELTA(r.x, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 4.0000f, ACCEPTABLE_DELTA); + + bh_point4f_lerp(&a, &b, 0.5f, &r); + BH_VERIFY_DELTA(r.x, 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 6.0000f, ACCEPTABLE_DELTA); + + bh_point4f_lerp(&a, &b, 1.0f, &r); + BH_VERIFY_DELTA(r.x, 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 7.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 8.0000f, ACCEPTABLE_DELTA); + + bh_point4f_slerp(&a, &b, 0.0f, &r); + BH_VERIFY_DELTA(r.x, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 4.0000f, ACCEPTABLE_DELTA); + + bh_point4f_slerp(&a, &b, 0.5f, &r); + BH_VERIFY_DELTA(r.x, 0.50350f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 0.67135f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 0.83918f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 1.00702f, ACCEPTABLE_DELTA); + + bh_point4f_slerp(&a, &b, 1.0f, &r); + BH_VERIFY_DELTA(r.x, 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 7.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w, 8.0000f, ACCEPTABLE_DELTA); + + return 0; +} + + +static int check_point3f(void) +{ + bh_point3f_t a, b, c, r; + float value; + + a.x = 1.0f; a.y = 2.0f; a.z = 3.0f; + b.x = 5.0f; b.y = 6.0f; b.z = 7.0f; + c.x = 1.5f; c.y = 2.5f; c.z = 3.5f; + + bh_point3f_add(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 6.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 8.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 10.0f, ACCEPTABLE_DELTA); + + bh_point3f_sub(&a, &b, &r); + BH_VERIFY_DELTA(r.x, -4.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, -4.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, -4.0f, ACCEPTABLE_DELTA); + + bh_point3f_mul(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 12.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 21.0f, ACCEPTABLE_DELTA); + + bh_point3f_scale(&a, 10.0f, &r); + BH_VERIFY_DELTA(r.x, 10.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 20.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 30.0f, ACCEPTABLE_DELTA); + + bh_point3f_madd(&a, &b, &c, &r); + BH_VERIFY_DELTA(r.x, 6.5f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 14.5f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 24.5f, ACCEPTABLE_DELTA); + + bh_point3f_negate(&a, &r); + BH_VERIFY_DELTA(r.x, -1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, -2.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, -3.0f, ACCEPTABLE_DELTA); + + value = bh_point3f_dot(&a, &b); + BH_VERIFY_DELTA(value, 38.0f, ACCEPTABLE_DELTA); + + bh_point3f_cross(&a, &b, &r); + BH_VERIFY_DELTA(r.x, -4.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 8.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, -4.0f, ACCEPTABLE_DELTA); + + value = bh_point3f_length(&a); + BH_VERIFY_DELTA(value, sqrt(14.0f), ACCEPTABLE_DELTA); + + bh_point3f_normal(&a, &r); + BH_VERIFY_DELTA(r.x, 1.0f / sqrt(14.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0f / sqrt(14.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 3.0f / sqrt(14.0f), ACCEPTABLE_DELTA); + + bh_point3f_min(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 3.0f, ACCEPTABLE_DELTA); + + bh_point3f_max(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 7.0f, ACCEPTABLE_DELTA); + + bh_point3f_lerp(&a, &b, 0.0f, &r); + BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 3.0f, ACCEPTABLE_DELTA); + + bh_point3f_lerp(&a, &b, 0.5f, &r); + BH_VERIFY_DELTA(r.x, 3.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 4.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 5.0f, ACCEPTABLE_DELTA); + + bh_point3f_lerp(&a, &b, 1.0f, &r); + BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 7.0f, ACCEPTABLE_DELTA); + + bh_point3f_slerp(&a, &b, 0.0f, &r); + BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 3.0f, ACCEPTABLE_DELTA); + + bh_point3f_slerp(&a, &b, 0.5f, &r); + BH_VERIFY_DELTA(r.x, 0.67937f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 0.90582f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 1.13228f, ACCEPTABLE_DELTA); + + bh_point3f_slerp(&a, &b, 1.0f, &r); + BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z, 7.0f, ACCEPTABLE_DELTA); + + return 0; +} + + +static int check_point2f(void) +{ + bh_point2f_t a, b, c, r; + float value; + + a.x = 1.0f; a.y = 2.0f; + b.x = 5.0f; b.y = 6.0f; + c.x = 1.5f; c.y = 2.5f; + + bh_point2f_add(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 6.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 8.0f, ACCEPTABLE_DELTA); + + bh_point2f_sub(&a, &b, &r); + BH_VERIFY_DELTA(r.x, -4.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, -4.0f, ACCEPTABLE_DELTA); + + bh_point2f_mul(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 12.0f, ACCEPTABLE_DELTA); + + bh_point2f_scale(&a, 10.0f, &r); + BH_VERIFY_DELTA(r.x, 10.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 20.0f, ACCEPTABLE_DELTA); + + bh_point2f_madd(&a, &b, &c, &r); + BH_VERIFY_DELTA(r.x, 6.5f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 14.5f, ACCEPTABLE_DELTA); + + bh_point2f_negate(&a, &r); + BH_VERIFY_DELTA(r.x, -1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, -2.0f, ACCEPTABLE_DELTA); + + value = bh_point2f_dot(&a, &b); + BH_VERIFY_DELTA(value, 17.0f, ACCEPTABLE_DELTA); + + value = bh_point2f_cross(&a, &b); + BH_VERIFY_DELTA(value, -4.0f, ACCEPTABLE_DELTA); + + value = bh_point2f_length(&a); + BH_VERIFY_DELTA(value, sqrt(5.0f), ACCEPTABLE_DELTA); + + bh_point2f_normal(&a, &r); + BH_VERIFY_DELTA(r.x, 1.0f / sqrt(5.0f), ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0f / sqrt(5.0f), ACCEPTABLE_DELTA); + + bh_point2f_min(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); + + bh_point2f_max(&a, &b, &r); + BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); + + bh_point2f_lerp(&a, &b, 0.0f, &r); + BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); + + bh_point2f_lerp(&a, &b, 0.5f, &r); + BH_VERIFY_DELTA(r.x, 3.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 4.0f, ACCEPTABLE_DELTA); + + bh_point2f_lerp(&a, &b, 1.0f, &r); + BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); + + bh_point2f_slerp(&a, &b, 0.0f, &r); + BH_VERIFY_DELTA(r.x, 1.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 2.0f, ACCEPTABLE_DELTA); + + bh_point2f_slerp(&a, &b, 0.5f, &r); + BH_VERIFY_DELTA(r.x, 1.00000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 1.33333f, ACCEPTABLE_DELTA); + + bh_point2f_slerp(&a, &b, 1.0f, &r); + BH_VERIFY_DELTA(r.x, 5.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y, 6.0f, ACCEPTABLE_DELTA); + + return 0; +} + + +static int check_point4i(void) +{ + bh_point4i_t a, b, c, r; + + a.x = 1; a.y = 2; a.z = 3; a.w = 4; + b.x = 5; b.y = 6; b.z = 7; b.w = 8; + c.x = 4; c.y = 3; c.z = 2; c.w = 1; + + bh_point4i_add(&a, &b, &r); + BH_VERIFY(r.x == 6); + BH_VERIFY(r.y == 8); + BH_VERIFY(r.z == 10); + BH_VERIFY(r.w == 12); + + bh_point4i_sub(&a, &b, &r); + BH_VERIFY(r.x == -4); + BH_VERIFY(r.y == -4); + BH_VERIFY(r.z == -4); + BH_VERIFY(r.w == -4); + + bh_point4i_mul(&a, &b, &r); + BH_VERIFY(r.x == 5); + BH_VERIFY(r.y == 12); + BH_VERIFY(r.z == 21); + BH_VERIFY(r.w == 32); + + bh_point4i_scale(&a, 10, &r); + BH_VERIFY(r.x == 10); + BH_VERIFY(r.y == 20); + BH_VERIFY(r.z == 30); + BH_VERIFY(r.w == 40); + + bh_point4i_madd(&a, &b, &c, &r); + BH_VERIFY(r.x == 9); + BH_VERIFY(r.y == 15); + BH_VERIFY(r.z == 23); + BH_VERIFY(r.w == 33); + + bh_point4i_negate(&a, &r); + BH_VERIFY(r.x == -1); + BH_VERIFY(r.y == -2); + BH_VERIFY(r.z == -3); + BH_VERIFY(r.w == -4); + + bh_point4i_min(&a, &b, &r); + BH_VERIFY(r.x == 1); + BH_VERIFY(r.y == 2); + BH_VERIFY(r.z == 3); + BH_VERIFY(r.w == 4); + + bh_point4i_max(&a, &b, &r); + BH_VERIFY(r.x == 5); + BH_VERIFY(r.y == 6); + BH_VERIFY(r.z == 7); + BH_VERIFY(r.w == 8); + + bh_point4i_lerp(&a, &b, 0.0f, &r); + BH_VERIFY(r.x == 1); + BH_VERIFY(r.y == 2); + BH_VERIFY(r.z == 3); + BH_VERIFY(r.w == 4); + + bh_point4i_lerp(&a, &b, 0.5f, &r); + BH_VERIFY(r.x == 3); + BH_VERIFY(r.y == 4); + BH_VERIFY(r.z == 5); + BH_VERIFY(r.w == 6); + + bh_point4i_lerp(&a, &b, 1.0f, &r); + BH_VERIFY(r.x == 5); + BH_VERIFY(r.y == 6); + BH_VERIFY(r.z == 7); + BH_VERIFY(r.w == 8); + + return 0; +} + + +static int check_point3i(void) +{ + bh_point3i_t a, b, c, r; + + a.x = 1; a.y = 2; a.z = 3; + b.x = 5; b.y = 6; b.z = 7; + c.x = 4; c.y = 3; c.z = 2; + + bh_point3i_add(&a, &b, &r); + BH_VERIFY(r.x == 6); + BH_VERIFY(r.y == 8); + BH_VERIFY(r.z == 10); + + bh_point3i_sub(&a, &b, &r); + BH_VERIFY(r.x == -4); + BH_VERIFY(r.y == -4); + BH_VERIFY(r.z == -4); + + bh_point3i_mul(&a, &b, &r); + BH_VERIFY(r.x == 5); + BH_VERIFY(r.y == 12); + BH_VERIFY(r.z == 21); + + bh_point3i_scale(&a, 10, &r); + BH_VERIFY(r.x == 10); + BH_VERIFY(r.y == 20); + BH_VERIFY(r.z == 30); + + bh_point3i_madd(&a, &b, &c, &r); + BH_VERIFY(r.x == 9); + BH_VERIFY(r.y == 15); + BH_VERIFY(r.z == 23); + + bh_point3i_negate(&a, &r); + BH_VERIFY(r.x == -1); + BH_VERIFY(r.y == -2); + BH_VERIFY(r.z == -3); + + bh_point3i_min(&a, &b, &r); + BH_VERIFY(r.x == 1); + BH_VERIFY(r.y == 2); + BH_VERIFY(r.z == 3); + + bh_point3i_max(&a, &b, &r); + BH_VERIFY(r.x == 5); + BH_VERIFY(r.y == 6); + BH_VERIFY(r.z == 7); + + bh_point3i_lerp(&a, &b, 0.0f, &r); + BH_VERIFY(r.x == 1); + BH_VERIFY(r.y == 2); + BH_VERIFY(r.z == 3); + + bh_point3i_lerp(&a, &b, 0.5f, &r); + BH_VERIFY(r.x == 3); + BH_VERIFY(r.y == 4); + BH_VERIFY(r.z == 5); + + bh_point3i_lerp(&a, &b, 1.0f, &r); + BH_VERIFY(r.x == 5); + BH_VERIFY(r.y == 6); + BH_VERIFY(r.z == 7); + + return 0; +} + + +static int check_point2i(void) +{ + bh_point2i_t a, b, c, r; + + a.x = 1; a.y = 2; + b.x = 5; b.y = 6; + c.x = 4; c.y = 3; + + bh_point2i_add(&a, &b, &r); + BH_VERIFY(r.x == 6); + BH_VERIFY(r.y == 8); + + bh_point2i_sub(&a, &b, &r); + BH_VERIFY(r.x == -4); + BH_VERIFY(r.y == -4); + + bh_point2i_mul(&a, &b, &r); + BH_VERIFY(r.x == 5); + BH_VERIFY(r.y == 12); + + bh_point2i_scale(&a, 10, &r); + BH_VERIFY(r.x == 10); + BH_VERIFY(r.y == 20); + + bh_point2i_madd(&a, &b, &c, &r); + BH_VERIFY(r.x == 9); + BH_VERIFY(r.y == 15); + + bh_point2i_negate(&a, &r); + BH_VERIFY(r.x == -1); + BH_VERIFY(r.y == -2); + + bh_point2i_min(&a, &b, &r); + BH_VERIFY(r.x == 1); + BH_VERIFY(r.y == 2); + + bh_point2i_max(&a, &b, &r); + BH_VERIFY(r.x == 5); + BH_VERIFY(r.y == 6); + + bh_point2i_lerp(&a, &b, 0.0f, &r); + BH_VERIFY(r.x == 1); + BH_VERIFY(r.y == 2); + + bh_point2i_lerp(&a, &b, 0.5f, &r); + BH_VERIFY(r.x == 3); + BH_VERIFY(r.y == 4); + + bh_point2i_lerp(&a, &b, 1.0f, &r); + BH_VERIFY(r.x == 5); + BH_VERIFY(r.y == 6); + + return 0; +} + + +static int check_quat(void) +{ + return 0; +} + + +static int check_matrix4f(void) +{ + bh_matrix4f_t a, b, r; + float value; + + a.x.x = 5.0f; a.y.x = 1.0f; a.z.x = 2.0f; a.w.x = 7.0f; + a.x.y = 3.0f; a.y.y = 0.0f; a.z.y = 0.0f; a.w.y = 2.0f; + a.x.z = 1.0f; a.y.z = 3.0f; a.z.z = 4.0f; a.w.z = 5.0f; + a.x.w = 2.0f; a.y.w = 0.0f; a.z.w = 0.0f; a.w.w = 3.0f; + + b.x.x = 5.0f; b.y.x = 1.0f; b.z.x = 2.0f; b.w.x = 7.0f; + b.x.y = 3.0f; b.y.y = 1.0f; b.z.y = 0.0f; b.w.y = 2.0f; + b.x.z = 4.0f; b.y.z = 2.0f; b.z.z = 4.0f; b.w.z = 5.0f; + b.x.w = 6.0f; b.y.w = 2.0f; b.z.w = 0.0f; b.w.w = 4.0f; + + bh_matrix4f_identity(&r); + BH_VERIFY(r.x.x == 1.0f); + BH_VERIFY(r.x.y == 0.0f); + BH_VERIFY(r.x.z == 0.0f); + BH_VERIFY(r.x.w == 0.0f); + + BH_VERIFY(r.y.x == 0.0f); + BH_VERIFY(r.y.y == 1.0f); + BH_VERIFY(r.y.z == 0.0f); + BH_VERIFY(r.y.w == 0.0f); + + BH_VERIFY(r.z.x == 0.0f); + BH_VERIFY(r.z.y == 0.0f); + BH_VERIFY(r.z.z == 1.0f); + BH_VERIFY(r.z.w == 0.0f); + + BH_VERIFY(r.w.x == 0.0f); + BH_VERIFY(r.w.y == 0.0f); + BH_VERIFY(r.w.z == 0.0f); + BH_VERIFY(r.w.w == 1.0f); + + bh_matrix4f_add(&a, &b, &r); + BH_VERIFY_DELTA(r.x.x, 10.0f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.w, 8.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.w, 2.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, 8.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.w, 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.w.x, 14.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.y, 4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.z, 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.w, 7.0000f, ACCEPTABLE_DELTA); + + bh_matrix4f_sub(&a, &b, &r); + BH_VERIFY_DELTA(r.x.x, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, -3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.w, -4.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, -1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.w, -2.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.w, 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.w.x, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.z, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.w, -1.0000f, ACCEPTABLE_DELTA); + + bh_matrix4f_mul(&a, &b, &r); + BH_VERIFY_DELTA(r.x.x, 78.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, 27.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, 60.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.w, 28.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 24.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, 7.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, 22.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.w, 8.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 18.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, 18.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.w, 4.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.w.x, 75.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.y, 29.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.z, 53.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.w, 26.0000f, ACCEPTABLE_DELTA); + + bh_matrix4f_scale(&a, 10, &r); + BH_VERIFY_DELTA(r.x.x, 50.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, 30.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.w, 20.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, 30.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.w, 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 20.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, 40.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.w, 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.w.x, 70.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.y, 20.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.z, 50.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.w, 30.0000f, ACCEPTABLE_DELTA); + + bh_matrix4f_transpose(&a, &r); + BH_VERIFY_DELTA(r.x.x, 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.w, 7.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.w, 2.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, 4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.w, 5.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.w.x, 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.z, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.w, 3.0000f, ACCEPTABLE_DELTA); + + value = bh_matrix4f_trace(&a); + BH_VERIFY_DELTA(value, 12.0000f, ACCEPTABLE_DELTA); + + value = bh_matrix4f_determinant(&a); + BH_VERIFY_DELTA(value, 10.0000f, ACCEPTABLE_DELTA); + + value = bh_matrix4f_determinant(&b); + BH_VERIFY_DELTA(value, 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY(bh_matrix4f_inverse(&a, &r) == BH_OK); + BH_VERIFY_DELTA(r.x.x, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, -2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, 1.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.w, 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 0.6000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, 1.8000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, -1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.w, -0.4000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, -0.5000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.w, 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.w.x, -0.4000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.y, 1.8000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.z, -2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.w.w, 0.6000f, ACCEPTABLE_DELTA); + + BH_VERIFY(bh_matrix4f_inverse(&b, &r) != BH_OK); + + return 0; +} + + +static int check_matrix3f(void) +{ + bh_matrix3f_t a, b, r; + float value; + + a.x.x = 5.0f; a.y.x = 1.0f; a.z.x = 2.0f; + a.x.y = 3.0f; a.y.y = 0.0f; a.z.y = 0.0f; + a.x.z = 1.0f; a.y.z = 3.0f; a.z.z = 4.0f; + + b.x.x = 2.0f; b.y.x = 1.0f; b.z.x = 2.0f; + b.x.y = 3.0f; b.y.y = 1.0f; b.z.y = 0.0f; + b.x.z = 4.0f; b.y.z = 2.0f; b.z.z = 4.0f; + + bh_matrix3f_identity(&r); + BH_VERIFY(r.x.x == 1.0f); + BH_VERIFY(r.x.y == 0.0f); + BH_VERIFY(r.x.z == 0.0f); + + BH_VERIFY(r.y.x == 0.0f); + BH_VERIFY(r.y.y == 1.0f); + BH_VERIFY(r.y.z == 0.0f); + + BH_VERIFY(r.z.x == 0.0f); + BH_VERIFY(r.z.y == 0.0f); + BH_VERIFY(r.z.z == 1.0f); + + bh_matrix3f_add(&a, &b, &r); + BH_VERIFY_DELTA(r.x.x, 7.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, 5.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, 5.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 4.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, 8.0000f, ACCEPTABLE_DELTA); + + bh_matrix3f_sub(&a, &b, &r); + BH_VERIFY_DELTA(r.x.x, 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, -3.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, -1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, 1.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, 0.0000f, ACCEPTABLE_DELTA); + + bh_matrix3f_mul(&a, &b, &r); + BH_VERIFY_DELTA(r.x.x, 21.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, 27.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, 12.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 18.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 6.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, 18.0000f, ACCEPTABLE_DELTA); + + bh_matrix3f_scale(&a, 10, &r); + BH_VERIFY_DELTA(r.x.x, 50.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, 30.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, 10.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 10.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, 30.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 20.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, 40.0000f, ACCEPTABLE_DELTA); + + bh_matrix3f_transpose(&a, &r); + BH_VERIFY_DELTA(r.x.x, 5.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, 2.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, 4.0000f, ACCEPTABLE_DELTA); + + value = bh_matrix3f_trace(&a); + BH_VERIFY_DELTA(value, 9.0000f, ACCEPTABLE_DELTA); + + value = bh_matrix3f_determinant(&a); + BH_VERIFY_DELTA(value, 6.0000f, ACCEPTABLE_DELTA); + + value = bh_matrix3f_determinant(&b); + BH_VERIFY_DELTA(value, 0.0000f, ACCEPTABLE_DELTA); + + BH_VERIFY(bh_matrix3f_inverse(&a, &r) == BH_OK); + BH_VERIFY_DELTA(r.x.x, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.y, -2.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.x.z, 1.5000f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.y.x, 0.3333f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.y, 3.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.y.z, -2.3333f, ACCEPTABLE_DELTA); + + BH_VERIFY_DELTA(r.z.x, 0.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.y, 1.0000f, ACCEPTABLE_DELTA); + BH_VERIFY_DELTA(r.z.z, -0.5000f, ACCEPTABLE_DELTA); + + BH_VERIFY(bh_matrix3f_inverse(&b, &r) != BH_OK); + + return 0; +} + + +int main(int argc, char **argv) +{ + (void)argc; + (void)argv; + + bh_unit_add("point4f", check_point4f); + bh_unit_add("point3f", check_point3f); + bh_unit_add("point2f", check_point2f); + bh_unit_add("point4i", check_point4i); + bh_unit_add("point3i", check_point3i); + bh_unit_add("point2i", check_point2i); + bh_unit_add("quat", check_quat); + bh_unit_add("matrix4f", check_matrix4f); + bh_unit_add("matrix3f", check_matrix3f); + + return bh_unit_run(); +} -- cgit v1.2.3