Add to_index() function to convert coords to scalar.
This commit is contained in:
parent
a1996b2d75
commit
cb9e4bce19
3 changed files with 59 additions and 0 deletions
|
@ -48,6 +48,12 @@ namespace dk {
|
|||
CoordinateScalarType sub_mod ( CoordinateScalarType parA, CoordinateScalarType parB, CoordinateScalarType parDiv ) a_pure;
|
||||
CoordinateScalarType sub_div ( CoordinateScalarType parA, CoordinateScalarType parB, CoordinateScalarType parDiv ) a_pure;
|
||||
} //namespace implem
|
||||
|
||||
template <uint32_t D>
|
||||
CoordinateDistType to_index ( const Vector<D>& parPos, const Vector<D>& parUpper ) a_pure;
|
||||
|
||||
template <uint32_t D>
|
||||
CoordinateDistType to_index ( const TileCoords<D>& parTC ) a_pure;
|
||||
} //namespace dk
|
||||
|
||||
#include "doorkeeper/implem/tilecoords.inl"
|
||||
|
|
|
@ -40,6 +40,32 @@ namespace dk {
|
|||
}
|
||||
} //namespace implem
|
||||
|
||||
template <uint32_t D>
|
||||
inline CoordinateDistType to_index (const Vector<D>& parPos, const Vector<D>& parUpper) {
|
||||
CoordinateDistType index = 0;
|
||||
for (CoordinateDistType d = 0; d < D; ++d) {
|
||||
auto pos = static_cast<CoordinateDistType>(parPos[D - 1 - d]);
|
||||
for (CoordinateDistType p = 0; p < D - 1 - d; ++p) {
|
||||
pos *= static_cast<CoordinateDistType>(parUpper[p] + 1);
|
||||
}
|
||||
|
||||
index += pos;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
#if defined(NDEBUG)
|
||||
template <>
|
||||
inline CoordinateDistType to_index<2> (const Vector<2>& parPos, const Vector<2>& parUpper) {
|
||||
return parPos.y() * (parUpper.x() + 1) + parPos.x();
|
||||
}
|
||||
#endif
|
||||
|
||||
template <uint32_t D>
|
||||
inline CoordinateDistType to_index (const TileCoords<D>& parTC) {
|
||||
return to_index<D>(parTC.position(), parTC.upper());
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
TileCoords<D>::TileCoords (const coords& parSize) :
|
||||
m_pos(CoordinateScalarType(0)),
|
||||
|
|
|
@ -140,3 +140,30 @@ TEST(back_forward, tilecoords) {
|
|||
EXPECT_EQ(coords4(69, 1934, 3, 0), test.position());
|
||||
}
|
||||
}
|
||||
|
||||
TEST(to_index, tilecoords) {
|
||||
typedef dk::TileCoords<2> tcoords2;
|
||||
typedef dk::TileCoords<2>::coords coords2;
|
||||
|
||||
{
|
||||
const coords2 max_coords(1000);
|
||||
tcoords2 test(max_coords);
|
||||
|
||||
EXPECT_EQ(0, dk::to_index(test));
|
||||
|
||||
++test;
|
||||
EXPECT_EQ(1, dk::to_index(test));
|
||||
|
||||
--test;
|
||||
EXPECT_EQ(0, dk::to_index(test));
|
||||
|
||||
test += 1;
|
||||
EXPECT_EQ(1, dk::to_index(test));
|
||||
|
||||
test += 1;
|
||||
EXPECT_EQ(2, dk::to_index(test));
|
||||
|
||||
test += 1501;
|
||||
EXPECT_EQ(1503, dk::to_index(test));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue