Browse Source

math: Interp class for frame-independent interpolation.

legacy
Sam Hocevar sam 11 years ago
parent
commit
6c8186389f
9 changed files with 141 additions and 6 deletions
  1. +1
    -1
      src/Makefile.am
  2. +88
    -0
      src/lol/math/interp.h
  3. +1
    -0
      src/lol/math/math.h
  4. +2
    -1
      src/lolcore.vcxproj
  5. +3
    -0
      src/lolcore.vcxproj.filters
  6. +1
    -1
      test/Makefile.am
  7. +1
    -0
      test/testsuite.vcxproj
  8. +3
    -3
      test/unit/color.cpp
  9. +41
    -0
      test/unit/interp.cpp

+ 1
- 1
src/Makefile.am View File

@@ -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 \


+ 88
- 0
src/lol/math/interp.h View File

@@ -0,0 +1,88 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2013 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.
//

//
// Interpolator classes
// --------------------
//

#if !defined __LOL_MATH_INTERP_H__
#define __LOL_MATH_INTERP_H__

namespace lol
{

template<typename T, int N = 16> 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__


+ 1
- 0
src/lol/math/math.h View File

@@ -16,6 +16,7 @@
#include <lol/math/real.h>
#include <lol/math/vector.h>
#include <lol/math/geometry.h>
#include <lol/math/interp.h>

#endif // __LOL_MATH_MATH_H__


+ 2
- 1
src/lolcore.vcxproj View File

@@ -214,6 +214,7 @@
<ClInclude Include="lol\math\functions.h" />
<ClInclude Include="lol\math\geometry.h" />
<ClInclude Include="lol\math\half.h" />
<ClInclude Include="lol\math\interp.h" />
<ClInclude Include="lol\math\math.h" />
<ClInclude Include="lol\math\real.h" />
<ClInclude Include="lol\math\remez.h" />
@@ -280,4 +281,4 @@
<ImportGroup Label="ExtensionTargets">
<Import Project="$(SolutionDir)\Lol.Fx.targets" />
</ImportGroup>
</Project>
</Project>

+ 3
- 0
src/lolcore.vcxproj.filters View File

@@ -512,6 +512,9 @@
<ClInclude Include="lol\math\geometry.h">
<Filter>lol\math</Filter>
</ClInclude>
<ClInclude Include="lol\math\interp.h">
<Filter>lol\math</Filter>
</ClInclude>
<ClInclude Include="easymesh\csgbsp.h">
<Filter>easymesh</Filter>
</ClInclude>


+ 1
- 1
test/Makefile.am View File

@@ -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@



+ 1
- 0
test/testsuite.vcxproj View File

@@ -42,6 +42,7 @@
<ClCompile Include="unit\color.cpp" />
<ClCompile Include="unit\half.cpp" />
<ClCompile Include="unit\image.cpp" />
<ClCompile Include="unit\interp.cpp" />
<ClCompile Include="unit\map.cpp" />
<ClCompile Include="unit\matrix.cpp" />
<ClCompile Include="unit\quat.cpp" />


+ 3
- 3
test/unit/color.cpp View File

@@ -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]);
}
}
};


+ 41
- 0
test/unit/interp.cpp View File

@@ -0,0 +1,41 @@
//
// Lol Engine
//
// Copyright: (c) 2010-2013 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(InterpTest)
{
void SetUp() {}

void TearDown() {}

LOLUNIT_TEST(FloatInterp)
{
Interp<float> 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 */


Loading…
Cancel
Save