From 99039d06ad001657da88018a2b85f654c33ca0fc Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 1 Nov 2014 06:12:47 +0100 Subject: add simple matrix operations impl --- opentrack/simple-mat.hpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 opentrack/simple-mat.hpp (limited to 'opentrack/simple-mat.hpp') diff --git a/opentrack/simple-mat.hpp b/opentrack/simple-mat.hpp new file mode 100644 index 00000000..5fa3acf9 --- /dev/null +++ b/opentrack/simple-mat.hpp @@ -0,0 +1,74 @@ +#pragma once + +template +struct Mat +{ + num data[h][w]; + + template + Mat operator*(const Mat& other) const + { + Mat ret; + for (int j = 0; j < w; j++) + for (int i = 0; i < p; i++) + { + num sum = num(0); + + for (int k = 0; k < h; k++) + sum += data[j][k]*other.data[k][i]; + + ret.data[j][i] = sum; + } + + return ret; + } + + num operator()(int j, int i) const { return data[j][i]; } + num& operator()(int j, int i) { return data[j][i]; } + + Mat() + { + for (int j = 0; j < h; j++) + for (int i = 0; i < w; i++) + data[j][i] = 0; + } + + Mat(const num* mem) + { + for (int j = 0; j < h; j++) + for (int i = 0; i < w; i++) + data[j][i] = mem[i*h+j]; + } + + // XXX add more operators as needed, third-party dependencies mostly + // not needed merely for matrix algebra -sh 20141030 + + static Mat eye() + { + Mat ret; + for (int j = 0; j < h; j++) + for (int i = 0; i < w; i++) + ret.data[j][i] = 0; + + for (int i = 0; i < h; i++) + ret.data[i][i] = 1; + + return ret; + } + + Mat t() + { + Mat ret; + + for (int j = 0; j < h; j++) + for (int i = 0; i < w; i++) + ret.data[i][j] = data[j][i]; + + return ret; + } +}; + +template using dmat = Mat; + +using rmat = dmat<3, 3>; + -- cgit v1.2.3