Преглед на файлове

math: add Array3D template class. Happy, Touky? :-)

undefined
Sam Hocevar преди 10 години
родител
ревизия
8abafbc686
променени са 9 файла, в които са добавени 329 реда и са изтрити 4 реда
  1. +1
    -1
      src/Makefile.am
  2. +1
    -0
      src/lol/math/all.h
  3. +215
    -0
      src/lol/math/array3d.h
  4. +1
    -0
      src/lolcore.vcxproj
  5. +3
    -0
      src/lolcore.vcxproj.filters
  6. +4
    -3
      test/Makefile.am
  7. +2
    -0
      test/testsuite.vcxproj
  8. +51
    -0
      test/unit/array2d.cpp
  9. +51
    -0
      test/unit/array3d.cpp

+ 1
- 1
src/Makefile.am Целия файл

@@ -40,7 +40,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/array2d.h \
lol/math/constants.h \
lol/math/array3d.h lol/math/constants.h \
\
lol/algorithm/all.h \
lol/algorithm/sort.h lol/algorithm/portal.h lol/algorithm/aabb_tree.h \


+ 1
- 0
src/lol/math/all.h Целия файл

@@ -17,6 +17,7 @@
#include <lol/math/real.h>
#include <lol/math/vector.h>
#include <lol/math/array2d.h>
#include <lol/math/array3d.h>
#include <lol/math/geometry.h>
#include <lol/math/interp.h>
#include <lol/math/rand.h>


+ 215
- 0
src/lol/math/array3d.h Целия файл

@@ -0,0 +1,215 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
// (c) 2013-2014 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
// 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 Array3D class
// -----------------
// A very simple 3D array class allowing var[i][j][k] indexing, with some nice
// additional features, eg. Array3D<int,float> for automatic arrays of tuples.
//

//
// FIXME: This file is in lol/math/ instead of lol/base/ because it
// uses ivec3.
//

#if !defined __LOL_MATH_ARRAY3D_H__
#define __LOL_MATH_ARRAY3D_H__

#include <lol/base/array.h>
#include <lol/base/assert.h>

namespace lol
{

template<typename T1, typename T2 = void, typename T3 = void,
typename T4 = void, typename T5 = void, typename T6 = void,
typename T7 = void, typename T8 = void>
class Array3D : protected Array<T1, T2, T3, T4, T5, T6, T7, T8>
{
public:
typedef Array<T1, T2, T3, T4, T5, T6, T7, T8> Super;
typedef typename Super::Element Element;

inline Array3D()
: m_size(0, 0, 0)
{
}

inline Array3D(int w, int h, int d)
{
SetSize(ivec3(w, h, d));
}

inline Array3D(ivec3 size)
{
SetSize(size);
}

/* Access elements directly using an ivec3 index */
inline Element const &operator [](ivec3 pos) const
{
ASSERT(pos.x >= 0);
ASSERT(pos.y >= 0);
ASSERT(pos.z >= 0);
ASSERT(pos.x < m_size.x);
ASSERT(pos.y < m_size.y);
ASSERT(pos.z < m_size.z);

int n = (pos.z * m_size.y + pos.y) * m_size.x + pos.x;
ASSERT(n >= 0);
ASSERT(n < this->m_count);

return this->m_data[n];
}

inline Element &operator [](ivec3 pos)
{
ASSERT(pos.x >= 0);
ASSERT(pos.y >= 0);
ASSERT(pos.z >= 0);
ASSERT(pos.x < m_size.x);
ASSERT(pos.y < m_size.y);
ASSERT(pos.z < m_size.z);

int n = (pos.z * m_size.y + pos.y) * m_size.x + pos.x;
ASSERT(n >= 0);
ASSERT(n < this->m_count);

return this->m_data[n];
}

class Slice
{
public:
inline Slice(Array3D &array, int i)
: m_array(array),
m_slice(i)
{
}

class Line
{
public:
inline Line(Array3D &array, ivec2 ij)
: m_array(array),
m_line(ij)
{
}

inline Element &operator [](int k)
{
ASSERT(k >= 0);
ASSERT(k < m_array.m_size.z);
return m_array[ivec3(m_line, k)];
}

private:
Array3D<T1, T2, T3, T4, T5, T6, T7, T8> &m_array;
ivec2 m_line;
};

inline class Line operator [](int j)
{
ASSERT(j >= 0);
ASSERT(j < m_array.m_size.y);
return Line(m_array, ivec2(m_slice, j));
}

private:
Array3D<T1, T2, T3, T4, T5, T6, T7, T8> &m_array;
int m_slice;
};

class ConstSlice
{
public:
inline ConstSlice(Array3D const &array, int i)
: m_array(array),
m_slice(i)
{
}

class Line
{
public:
inline Line(Array3D const &array, ivec2 ij)
: m_array(array),
m_line(ij)
{
}

inline Element const &operator [](int k) const
{
ASSERT(k >= 0);
ASSERT(k < m_array.m_size.z);
return m_array[ivec3(m_line, k)];
}

private:
Array3D<T1, T2, T3, T4, T5, T6, T7, T8> const &m_array;
ivec2 m_line;
};

inline class Line operator [](int j) const
{
ASSERT(j >= 0);
ASSERT(j < m_array.m_size.y);
return Line(m_array, ivec2(m_slice, j));
}

private:
Array3D<T1, T2, T3, T4, T5, T6, T7, T8> const &m_array;
int m_slice;
};

/* Access addressable slices, allowing for array[i][j][k] syntax. */
inline class Slice operator [](int i)
{
ASSERT(i >= 0);
ASSERT(i < m_size.x);
return Slice(*this, i);
}

inline class ConstSlice operator [](int i) const
{
ASSERT(i >= 0);
ASSERT(i < m_size.x);
return ConstSlice(*this, i);
}

/* Resize the array.
* FIXME: data gets scrambled; should we care? */
inline void SetSize(ivec3 size, Element e = Element())
{
this->Resize(size.x * size.y * size.z, e);
m_size = size;
}

inline ivec3 GetSize() const
{
return m_size;
}

public:
inline Element *Data() { return Super::Data(); }
inline Element const *Data() const { return Super::Data(); }
inline int Count() const { return Super::Count(); }
inline int Bytes() const { return Super::Bytes(); }

private:
ivec3 m_size;
};

} /* namespace lol */

#endif // __LOL_MATH_ARRAY3D_H__


+ 1
- 0
src/lolcore.vcxproj Целия файл

@@ -316,6 +316,7 @@
<ClInclude Include="lol\image\pixel.h" />
<ClInclude Include="lol\math\all.h" />
<ClInclude Include="lol\math\array2d.h" />
<ClInclude Include="lol\math\array3d.h" />
<ClInclude Include="lol\math\constants.h" />
<ClInclude Include="lol\math\functions.h" />
<ClInclude Include="lol\math\geometry.h" />


+ 3
- 0
src/lolcore.vcxproj.filters Целия файл

@@ -615,6 +615,9 @@
<ClInclude Include="lol\math\array2d.h">
<Filter>lol\base</Filter>
</ClInclude>
<ClInclude Include="lol\math\array3d.h">
<Filter>lol\base</Filter>
</ClInclude>
<ClInclude Include="lol\math\geometry.h">
<Filter>lol\math</Filter>
</ClInclude>


+ 4
- 3
test/Makefile.am Целия файл

@@ -18,9 +18,10 @@ endif
testsuite_SOURCES = testsuite.cpp \
unit/vector.cpp unit/matrix.cpp unit/half.cpp unit/trig.cpp \
unit/build.cpp unit/real.cpp unit/image.cpp unit/quat.cpp unit/cmplx.cpp \
unit/array.cpp unit/rotation.cpp unit/string.cpp unit/map.cpp \
unit/color.cpp unit/atomic.cpp unit/interp.cpp unit/box.cpp \
unit/rand.cpp unit/thread.cpp unit/camera.cpp unit/enum.cpp
unit/array.cpp unit/array2d.cpp unit/array3d.cpp unit/rotation.cpp \
unit/string.cpp unit/map.cpp unit/color.cpp unit/atomic.cpp \
unit/interp.cpp unit/box.cpp unit/rand.cpp unit/thread.cpp \
unit/camera.cpp unit/enum.cpp
testsuite_CPPFLAGS = $(AM_CPPFLAGS)
testsuite_DEPENDENCIES = @LOL_DEPS@
noinst_DATA = data/gradient.png


+ 2
- 0
test/testsuite.vcxproj Целия файл

@@ -45,6 +45,8 @@
<ItemGroup>
<ClCompile Include="testsuite.cpp" />
<ClCompile Include="unit\array.cpp" />
<ClCompile Include="unit\array2d.cpp" />
<ClCompile Include="unit\array3d.cpp" />
<ClCompile Include="unit\box.cpp" />
<ClCompile Include="unit\build.cpp" />
<ClCompile Include="unit\camera.cpp" />


+ 51
- 0
test/unit/array2d.cpp Целия файл

@@ -0,0 +1,51 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
// 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.
//

#if defined HAVE_CONFIG_H
# include "config.h"
#endif

#include "core.h"
#include "lol/unit.h"

namespace lol
{

LOLUNIT_FIXTURE(Array2DTest)
{
void SetUp() {}

void TearDown() {}

LOLUNIT_TEST(Array2DCreate)
{
Array2D<int> a(ivec2(10, 10));

a[0][0] = 2;
a[9][0] = 4;
a[0][9] = 6;
a[9][9] = 8;

LOLUNIT_ASSERT_EQUAL(a[0][0], 2);
LOLUNIT_ASSERT_EQUAL(a[9][0], 4);
LOLUNIT_ASSERT_EQUAL(a[0][9], 6);
LOLUNIT_ASSERT_EQUAL(a[9][9], 8);

Array2D<int> const &b = a;

LOLUNIT_ASSERT_EQUAL(b[0][0], 2);
LOLUNIT_ASSERT_EQUAL(b[9][0], 4);
LOLUNIT_ASSERT_EQUAL(b[0][9], 6);
LOLUNIT_ASSERT_EQUAL(b[9][9], 8);
}
};

} /* namespace lol */


+ 51
- 0
test/unit/array3d.cpp Целия файл

@@ -0,0 +1,51 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
// 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.
//

#if defined HAVE_CONFIG_H
# include "config.h"
#endif

#include "core.h"
#include "lol/unit.h"

namespace lol
{

LOLUNIT_FIXTURE(Array3DTest)
{
void SetUp() {}

void TearDown() {}

LOLUNIT_TEST(Array3DCreate)
{
Array3D<int> a(ivec3(10, 10, 10));

a[0][0][0] = 2;
a[9][0][0] = 4;
a[0][9][9] = 6;
a[9][9][9] = 8;

LOLUNIT_ASSERT_EQUAL(a[0][0][0], 2);
LOLUNIT_ASSERT_EQUAL(a[9][0][0], 4);
LOLUNIT_ASSERT_EQUAL(a[0][9][9], 6);
LOLUNIT_ASSERT_EQUAL(a[9][9][9], 8);

Array3D<int> const &b = a;

LOLUNIT_ASSERT_EQUAL(b[0][0][0], 2);
LOLUNIT_ASSERT_EQUAL(b[9][0][0], 4);
LOLUNIT_ASSERT_EQUAL(b[0][9][9], 6);
LOLUNIT_ASSERT_EQUAL(b[9][9][9], 8);
}
};

} /* namespace lol */


Зареждане…
Отказ
Запис