From 615d243b14033ad7c140ef4b9426d59ec1ee687e Mon Sep 17 00:00:00 2001 From: Irlan <-> Date: Mon, 2 Apr 2018 12:45:54 -0300 Subject: [PATCH] separate square from rectangular matrices --- include/bounce/common/math/mat.h | 103 ++--------------------------- include/bounce/common/math/mat44.h | 66 ++++++++++++++++++ include/bounce/common/math/vec2.h | 6 ++ include/bounce/common/math/vec3.h | 6 ++ include/bounce/common/math/vec4.h | 75 +++++++++++++++++++++ src/bounce/common/math/mat.cpp | 9 +++ 6 files changed, 166 insertions(+), 99 deletions(-) create mode 100644 include/bounce/common/math/mat44.h create mode 100644 include/bounce/common/math/vec4.h diff --git a/include/bounce/common/math/mat.h b/include/bounce/common/math/mat.h index 163c573..75eab32 100644 --- a/include/bounce/common/math/mat.h +++ b/include/bounce/common/math/mat.h @@ -19,9 +19,12 @@ #ifndef B3_MAT_H #define B3_MAT_H -#include #include #include +#include + +// This header contain implementations for some small rectangular +// matrices. struct b3Mat23 { @@ -109,29 +112,6 @@ struct b3Mat34 b3Vec3 x, y, z, w; }; -struct b3Vec4 -{ - b3Vec4() { } - - b3Vec4(float32 _x, float32 _y, float32 _z, float32 _w) - { - x = _x; - y = _y; - z = _z; - w = _w; - } - - void SetZero() - { - x = 0.0f; - y = 0.0f; - z = 0.0f; - w = 0.0f; - } - - float32 x, y, z, w; -}; - struct b3Mat43 { b3Mat43() { } @@ -153,63 +133,6 @@ struct b3Mat43 b3Vec4 x, y, z; }; -struct b3Mat44 -{ - b3Mat44() { } - - b3Mat44(const b3Vec4& _x, const b3Vec4& _y, const b3Vec4& _z, const b3Vec4& _w) - { - x = _x; - y = _y; - z = _z; - w = _w; - } - - void SetZero() - { - x.SetZero(); - y.SetZero(); - z.SetZero(); - w.SetZero(); - } - - b3Vec4 x, y, z, w; -}; - -inline b3Vec4 operator+(const b3Vec4& a, const b3Vec4& b) -{ - return b3Vec4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); -} - -inline b3Vec4 operator-(const b3Vec4& a, const b3Vec4& b) -{ - return b3Vec4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); -} - -// 1x1 * 1x4 = 1x1 -inline b3Vec4 operator*(float32 s, const b3Vec4& v) -{ - return b3Vec4(s * v.x, s * v.y, s * v.z, s * v.w); -} - -// a * 4x4 = 4x4 -inline b3Mat44 operator*(float32 s, const b3Mat44& A) -{ - return b3Mat44(s * A.x, s * A.y, s * A.z, s * A.w); -} - -// 4x4 * 4x1 = 4x1 -inline b3Vec4 operator*(const b3Mat44& A, const b3Vec4& v) -{ - return v.x * A.x + v.y * A.y + v.z * A.z + v.w * A.w; -} - -// 4x4 * 4x4 = 4x4 -inline b3Mat44 operator*(const b3Mat44& A, const b3Mat44& B) -{ - return b3Mat44(A * B.x, A * B.y, A * B.z, A * B.w); -} - // a * 3x4 = 3x4 inline b3Mat34 operator*(float32 s, const b3Mat34& A) { @@ -228,24 +151,6 @@ inline b3Vec3 operator*(const b3Mat34& A, const b3Vec4& v) return v.x * A.x + v.y * A.y + v.z * A.z + v.w * A.w; } -// 1x4 * 4x1 = 1x1 -inline float32 operator*(const b3Vec4& A, const b3Vec4& B) -{ - return A.x * B.x + A.y * B.y + A.z * B.z + A.w * B.w; -} - -// 3x1 * 1x1 = 3x1 -inline b3Vec3 operator*(const b3Vec3& v, float32 s) -{ - return s * v; -} - -// 2x1 * 1x1 = 2x1 -inline b3Vec2 operator*(const b3Vec2& v, float32 s) -{ - return s * v; -} - // 1x3 * 3x1 = 1x1 inline float32 operator*(const b3Vec3& A, const b3Vec3& B) { diff --git a/include/bounce/common/math/mat44.h b/include/bounce/common/math/mat44.h new file mode 100644 index 0000000..0285422 --- /dev/null +++ b/include/bounce/common/math/mat44.h @@ -0,0 +1,66 @@ +/* +* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net +* +* This software is provided 'as-is', without any express or implied +* warranty. In no event will the authors be held liable for any damages +* arising from the use of this software. +* Permission is granted to anyone to use this software for any purpose, +* including commercial applications, and to alter it and redistribute it +* freely, subject to the following restrictions: +* 1. The origin of this software must not be misrepresented; you must not +* claim that you wrote the original software. If you use this software +* in a product, an acknowledgment in the product documentation would be +* appreciated but is not required. +* 2. Altered source versions must be plainly marked as such, and must not be +* misrepresented as being the original software. +* 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef B3_MAT_44_H +#define B3_MAT_44_H + +#include + +// A 4-by-4 matrix stored in column-major order. +struct b3Mat44 +{ + b3Mat44() { } + + b3Mat44(const b3Vec4& _x, const b3Vec4& _y, const b3Vec4& _z, const b3Vec4& _w) + { + x = _x; + y = _y; + z = _z; + w = _w; + } + + void SetZero() + { + x.SetZero(); + y.SetZero(); + z.SetZero(); + w.SetZero(); + } + + b3Vec4 x, y, z, w; +}; + +// +inline b3Mat44 operator*(float32 s, const b3Mat44& A) +{ + return b3Mat44(s * A.x, s * A.y, s * A.z, s * A.w); +} + +// +inline b3Vec4 operator*(const b3Mat44& A, const b3Vec4& v) +{ + return v.x * A.x + v.y * A.y + v.z * A.z + v.w * A.w; +} + +// +inline b3Mat44 operator*(const b3Mat44& A, const b3Mat44& B) +{ + return b3Mat44(A * B.x, A * B.y, A * B.z, A * B.w); +} + +#endif \ No newline at end of file diff --git a/include/bounce/common/math/vec2.h b/include/bounce/common/math/vec2.h index cf7dcd6..1fe96d3 100644 --- a/include/bounce/common/math/vec2.h +++ b/include/bounce/common/math/vec2.h @@ -115,6 +115,12 @@ inline b3Vec2 operator*(float32 s, const b3Vec2& v) return b3Vec2(s * v.x, s * v.y); } +// Multiply a vector by a scalar. +inline b3Vec2 operator*(const b3Vec2& v, float32 s) +{ + return s * v; +} + // Compute the dot product of two vectors. inline float32 b3Dot(const b3Vec2& a, const b3Vec2& b) { diff --git a/include/bounce/common/math/vec3.h b/include/bounce/common/math/vec3.h index 25f380b..66a6cca 100644 --- a/include/bounce/common/math/vec3.h +++ b/include/bounce/common/math/vec3.h @@ -130,6 +130,12 @@ inline b3Vec3 operator*(float32 s, const b3Vec3& v) return b3Vec3(s * v.x, s * v.y, s * v.z); } +// Compute a scalar-vector product. +inline b3Vec3 operator*(const b3Vec3& v, float32 s) +{ + return s * v; +} + // Inverse multiply a scalar-vector. inline b3Vec3 operator/(const b3Vec3& v, float32 s) { diff --git a/include/bounce/common/math/vec4.h b/include/bounce/common/math/vec4.h new file mode 100644 index 0000000..304e45f --- /dev/null +++ b/include/bounce/common/math/vec4.h @@ -0,0 +1,75 @@ +/* +* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net +* +* This software is provided 'as-is', without any express or implied +* warranty. In no event will the authors be held liable for any damages +* arising from the use of this software. +* Permission is granted to anyone to use this software for any purpose, +* including commercial applications, and to alter it and redistribute it +* freely, subject to the following restrictions: +* 1. The origin of this software must not be misrepresented; you must not +* claim that you wrote the original software. If you use this software +* in a product, an acknowledgment in the product documentation would be +* appreciated but is not required. +* 2. Altered source versions must be plainly marked as such, and must not be +* misrepresented as being the original software. +* 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef B3_VEC_4_H +#define B3_VEC_4_H + +#include + +// A 4D column vector. +struct b3Vec4 +{ + // + b3Vec4() { } + + // + b3Vec4(float32 _x, float32 _y, float32 _z, float32 _w) + { + x = _x; + y = _y; + z = _z; + w = _w; + } + + // + void SetZero() + { + x = 0.0f; + y = 0.0f; + z = 0.0f; + w = 0.0f; + } + + float32 x, y, z, w; +}; + +// +inline b3Vec4 operator+(const b3Vec4& a, const b3Vec4& b) +{ + return b3Vec4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); +} + +// +inline b3Vec4 operator-(const b3Vec4& a, const b3Vec4& b) +{ + return b3Vec4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); +} + +// +inline b3Vec4 operator*(float32 s, const b3Vec4& v) +{ + return b3Vec4(s * v.x, s * v.y, s * v.z, s * v.w); +} + +// +inline float32 operator*(const b3Vec4& A, const b3Vec4& B) +{ + return A.x * B.x + A.y * B.y + A.z * B.z + A.w * B.w; +} + +#endif \ No newline at end of file diff --git a/src/bounce/common/math/mat.cpp b/src/bounce/common/math/mat.cpp index 619a958..f1d744c 100644 --- a/src/bounce/common/math/mat.cpp +++ b/src/bounce/common/math/mat.cpp @@ -18,6 +18,15 @@ #include #include +#include + +b3Mat22 b3Mat22_zero = b3Mat22( + b3Vec2(0.0f, 0.0f), + b3Vec2(0.0f, 0.0f)); + +b3Mat22 b3Mat22_identity = b3Mat22( + b3Vec2(1.0f, 0.0f), + b3Vec2(0.0f, 1.0f)); b3Mat33 b3Mat33_zero = b3Mat33( b3Vec3(0.0f, 0.0f, 0.0f),