diff --git a/src/Makefile.am b/src/Makefile.am index 86ac0cc1..f348d140 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -37,7 +37,7 @@ liblolcore_headers = \ \ lol/math/math.h \ lol/math/functions.h lol/math/vector.h lol/math/half.h lol/math/real.h \ - lol/math/remez.h lol/math/math.h lol/math/geometry.h \ + lol/math/remez.h lol/math/math.h lol/math/geometry.h lol/math/interp.h \ \ lol/sys/sys.h \ lol/sys/init.h lol/sys/file.h lol/sys/thread.h lol/sys/atomic.h \ diff --git a/src/lol/math/interp.h b/src/lol/math/interp.h new file mode 100644 index 00000000..730b6024 --- /dev/null +++ b/src/lol/math/interp.h @@ -0,0 +1,88 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2013 Sam Hocevar +// 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. +// + +// +// Interpolator classes +// -------------------- +// + +#if !defined __LOL_MATH_INTERP_H__ +#define __LOL_MATH_INTERP_H__ + +namespace lol +{ + +template class Interp +{ +public: + inline Interp() : m_pos(-N) {} + inline ~Interp() {} + + void Set(float key, T const &val) + { + if (m_pos < 0) + { + m_key[m_pos + N] = key; + m_val[m_pos + N] = val; + ++m_pos; + } + else + { + m_key[m_pos] = key; + m_val[m_pos] = val; + m_pos = (m_pos + 1) % N; + } + } + + T Get(float key) + { + if (m_pos == -N) + return T(); + if (m_pos == 1 - N) + return m_val[0]; + + int start = max(0, m_pos); + int a = 0; + int b = min(m_pos + N, N); + + while (a + 1 < b) + { + int c = (a + b) / 2; + if (m_key[(start + c) % N] >= key) + b = c; + else + a = c; + } + + float ka = m_key[(start + a) % N]; + float kb = m_key[(start + b) % N]; + float u = (key - ka) / (kb - ka); + + return (1.f - u) * m_val[(start + a) % N] + u * m_val[(start + b) % N]; + } + + inline void Reset() + { + m_pos = -N; + } + +private: + float m_key[N]; + T m_val[N]; + + /* If m_pos < 0, the value indicates how many free slots + * there are. */ + int m_pos; +}; + +} /* namespace lol */ + +#endif // __LOL_MATH_INTERP_H__ + diff --git a/src/lol/math/math.h b/src/lol/math/math.h index 9128d64b..c5dea454 100644 --- a/src/lol/math/math.h +++ b/src/lol/math/math.h @@ -16,6 +16,7 @@ #include #include #include +#include #endif // __LOL_MATH_MATH_H__ diff --git a/src/lolcore.vcxproj b/src/lolcore.vcxproj index 46aa3a27..ace9e153 100644 --- a/src/lolcore.vcxproj +++ b/src/lolcore.vcxproj @@ -214,6 +214,7 @@ + @@ -280,4 +281,4 @@ - \ No newline at end of file + diff --git a/src/lolcore.vcxproj.filters b/src/lolcore.vcxproj.filters index 59d553a7..2f1a48c4 100644 --- a/src/lolcore.vcxproj.filters +++ b/src/lolcore.vcxproj.filters @@ -512,6 +512,9 @@ lol\math + + lol\math + easymesh diff --git a/test/Makefile.am b/test/Makefile.am index d2abf009..92d89f70 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -21,7 +21,7 @@ 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/color.cpp unit/atomic.cpp unit/interp.cpp testsuite_CPPFLAGS = $(AM_CPPFLAGS) testsuite_DEPENDENCIES = @LOL_DEPENDENCIES@ diff --git a/test/testsuite.vcxproj b/test/testsuite.vcxproj index 2ae27f05..9951989d 100644 --- a/test/testsuite.vcxproj +++ b/test/testsuite.vcxproj @@ -42,6 +42,7 @@ + diff --git a/test/unit/color.cpp b/test/unit/color.cpp index ed780930..6042343c 100644 --- a/test/unit/color.cpp +++ b/test/unit/color.cpp @@ -84,7 +84,7 @@ LOLUNIT_FIXTURE(ColorTest) LOLUNIT_SET_CONTEXT(n / 7); LOLUNIT_ASSERT_DOUBLES_EQUAL(d1, d2, 0.0001); LOLUNIT_ASSERT_DOUBLES_EQUAL(d2, d3, 0.0001); - LOLUNIT_UNSET_CONTEXT(); + LOLUNIT_UNSET_CONTEXT(n / 7); } } @@ -106,7 +106,7 @@ LOLUNIT_FIXTURE(ColorTest) LOLUNIT_ASSERT_DOUBLES_EQUAL(v1.g, v3.g, 0.0001); LOLUNIT_ASSERT_DOUBLES_EQUAL(v1.b, v3.b, 0.0001); - LOLUNIT_UNSET_CONTEXT(); + LOLUNIT_UNSET_CONTEXT(&rgb[0]); } } @@ -128,7 +128,7 @@ LOLUNIT_FIXTURE(ColorTest) LOLUNIT_ASSERT_DOUBLES_EQUAL(v2.y, v3.y, 0.0001); LOLUNIT_ASSERT_DOUBLES_EQUAL(v2.z, v3.z, 0.0001); - LOLUNIT_UNSET_CONTEXT(); + LOLUNIT_UNSET_CONTEXT(&rgb[0]); } } }; diff --git a/test/unit/interp.cpp b/test/unit/interp.cpp new file mode 100644 index 00000000..124ee308 --- /dev/null +++ b/test/unit/interp.cpp @@ -0,0 +1,41 @@ +// +// Lol Engine +// +// Copyright: (c) 2010-2013 Sam Hocevar +// 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(InterpTest) +{ + void SetUp() {} + + void TearDown() {} + + LOLUNIT_TEST(FloatInterp) + { + Interp interp; + + interp.Set(1.f, 100.f); + interp.Set(2.f, 150.f); + + LOLUNIT_ASSERT_DOUBLES_EQUAL(75.f, interp.Get(0.5f), 1.e-5f); + LOLUNIT_ASSERT_DOUBLES_EQUAL(125.f, interp.Get(1.5f), 1.e-5f); + LOLUNIT_ASSERT_DOUBLES_EQUAL(175.f, interp.Get(2.5f), 1.e-5f); + } +}; + +} /* namespace lol */ +