From 65c69e11dbb53ffd1197aa0adb0c42a62975bc90 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 14 Feb 2011 01:26:19 +0000 Subject: [PATCH] Create a few matrix handling classes and operators. --- src/matrix.h | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/matrix.h b/src/matrix.h index b5128930..b407ed64 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -175,5 +175,66 @@ GLOBALS(2) GLOBALS(3) GLOBALS(4) +template struct Vec4x4 +{ + inline Vec4x4() { v[0] = v[1] = v[2] = v[3] = 0; } + inline Vec4x4(T val) { v[0] = v[1] = v[2] = v[3] = val; } + inline Vec4x4(Vec4 v0, Vec4 v1, Vec4 v2, Vec4 v3) + { v[0] = v0; v[1] = v1; v[2] = v2; v[3] = v3; } + + inline Vec4& operator[](int n) { return v[n]; } + inline Vec4 const& operator[](int n) const { return v[n]; } + + inline Vec4x4 operator +(Vec4x4 const val) const + { + Vec4x4 ret; + for (int j = 0; j < 4; j++) + for (int i = 0; i < 4; i++) + ret[i][j] = v[i][j] + val[i][j]; + return ret; + } + + inline Vec4x4 operator -(Vec4x4 const val) const + { + Vec4x4 ret; + for (int j = 0; j < 4; j++) + for (int i = 0; i < 4; i++) + ret[i][j] = v[i][j] - val[i][j]; + return ret; + } + + inline Vec4x4 operator *(Vec4x4 const val) const + { + Vec4x4 ret; + for (int j = 0; j < 4; j++) + for (int i = 0; i < 4; i++) + { + T tmp = 0; + for (int k = 0; k < 4; k++) + tmp += v[k][j] * val[i][k]; + ret[i][j] = tmp; + } + return ret; + } + + inline Vec4 operator *(Vec4 const val) const + { + Vec4 ret; + for (int j = 0; j < 4; j++) + { + T tmp = 0; + for (int i = 0; i < 4; i++) + tmp += v[i][j] * val[i]; + ret[j] = tmp; + } + return ret; + } + + Vec4 v[4]; +}; + +typedef Vec4x4 float4x4; +typedef Vec4x4 int4x4; + #endif // __DH_MATRIX_H__