From 5fb1ce656f38dc6d4aafafeb5c4db8ef009ef0af Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 18 Jun 2014 11:26:47 +0000 Subject: [PATCH] math: add an Array2D class that acts like a 2D matrix. --- src/Makefile.am | 2 +- src/lol/math/all.h | 1 + src/lol/math/array2d.h | 162 ++++++++++++++++++++++++++++++++++++ src/lolcore.vcxproj | 1 + src/lolcore.vcxproj.filters | 3 + 5 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/lol/math/array2d.h diff --git a/src/Makefile.am b/src/Makefile.am index 3226004a..10f7041e 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -39,7 +39,7 @@ liblolcore_headers = \ \ lol/math/all.h \ lol/math/functions.h lol/math/vector.h lol/math/half.h lol/math/real.h \ - lol/math/geometry.h lol/math/interp.h lol/math/rand.h \ + lol/math/geometry.h lol/math/interp.h lol/math/rand.h lol/math/array2d.h \ lol/math/constants.h \ \ lol/algorithm/all.h \ diff --git a/src/lol/math/all.h b/src/lol/math/all.h index f33b8056..7cd9fb2e 100644 --- a/src/lol/math/all.h +++ b/src/lol/math/all.h @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include diff --git a/src/lol/math/array2d.h b/src/lol/math/array2d.h new file mode 100644 index 00000000..f5d10efb --- /dev/null +++ b/src/lol/math/array2d.h @@ -0,0 +1,162 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2014 Sam Hocevar +// (c) 2013-2014 Benjamin "Touky" Huet +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://www.wtfpl.net/ for more details. +// + +// +// The Array2D class +// ----------------- +// A very simple 2D array class allowing var[i][j] indexing, with some nice +// additional features, eg. Array2D for automatic arrays of tuples. +// + +// +// FIXME: This file is in lol/math/ instead of lol/base/ because it +// uses ivec2. +// + +#if !defined __LOL_MATH_ARRAY2D_H__ +#define __LOL_MATH_ARRAY2D_H__ + +#include +#include + +namespace lol +{ + +template +class Array2D : protected Array +{ +public: + typedef typename Array::Element Element; + + inline Array2D() + : m_size(0, 0) + { + } + + inline Array2D(int w, int h) + { + SetSize(ivec2(w, h)); + } + + inline Array2D(ivec2 size) + { + SetSize(size); + } + + /* Access elements directly using an ivec2 index */ + inline Element const &operator [](ivec2 pos) const + { + ASSERT(pos.x >= 0); + ASSERT(pos.y >= 0); + ASSERT(pos.x < m_size.x); + ASSERT(pos.y < m_size.y); + + int n = pos.y * m_size.y + pos.x; + ASSERT(n >= 0); + ASSERT(n < this->m_count); + + return this->m_data[n]; + } + + inline Element &operator [](ivec2 pos) + { + ASSERT(pos.x >= 0); + ASSERT(pos.y >= 0); + ASSERT(pos.x < m_size.x); + ASSERT(pos.y < m_size.y); + + int n = pos.y * m_size.y + pos.x; + ASSERT(n >= 0); + ASSERT(n < this->m_count); + + return this->m_data[n]; + } + + class Column + { + public: + inline Column(Array2D &array, int i) + : m_array(array), + m_column(i) + { + } + + inline Element &operator [](int j) + { + ASSERT(j >= 0); + ASSERT(j < m_array.m_size.y); + return m_array[ivec2(m_column, j)]; + } + + private: + Array2D &m_array; + int m_column; + }; + + class ConstColumn + { + public: + inline ConstColumn(Array2D const &array, int i) + : m_array(array), + m_column(i) + { + } + + inline Element const &operator [](int j) const + { + ASSERT(j >= 0); + ASSERT(j < m_array.m_size.y); + return m_array[ivec2(m_column, j)]; + } + + private: + Array2D const &m_array; + int m_column; + }; + + /* Access addressable columns, allowing for array[i][j] syntax. */ + inline class Column operator [](int i) + { + ASSERT(i >= 0); + ASSERT(i < m_size.x); + return Column(*this, i); + } + + inline class ConstColumn operator [](int i) const + { + ASSERT(i >= 0); + ASSERT(i < m_size.x); + return ConstColumn(*this, i); + } + + /* Resize the array. + * FIXME: data gets scrambled; should we care? */ + inline void SetSize(ivec2 size) + { + this->Resize(size.x * size.y); + m_size = size; + } + + inline ivec2 GetSize() const + { + return m_size; + } + +private: + ivec2 m_size; +}; + +} /* namespace lol */ + +#endif // __LOL_MATH_ARRAY2D_H__ + diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index f8db905c..30870efd 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -301,6 +301,7 @@ + diff --git a/src/lolcore.vcxproj.filters b/src/lolcore.vcxproj.filters index 89379d38..775991a0 100644 --- a/src/lolcore.vcxproj.filters +++ b/src/lolcore.vcxproj.filters @@ -573,6 +573,9 @@ lol\base + + lol\base + lol\math