Updated swizzle impl and tests

This commit is contained in:
Christophe Riccio 2011-01-15 02:37:34 +00:00
parent e145f7e389
commit b77d66317e
5 changed files with 355 additions and 121 deletions

View File

@ -1,134 +1,249 @@
///////////////////////////////////////////////////////////////////////////////////////////////////
// OpenGL Mathematics Copyright (c) 2005 - 2010 G-Truc Creation (www.g-truc.net)
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2011-01-15
// Updated : 2011-01-15
// Licence : This source is under MIT License
// File : glm/gtc/swizzle.inl
///////////////////////////////////////////////////////////////////////////////////////////////////
// Dependency:
// - GLM core
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace glm{ namespace glm{
namespace gtc{ namespace gtc{
namespace swizzle namespace swizzle{
{
template <typename T, template <typename> class vecType> template <typename T, template <typename> class vecType>
inline T swizzle inline T swizzle
( (
vecType<T> const & v, vecType<T> const & v,
comp x comp x
) )
{ {
assert(int(x) < int(typename vecType<T>::_size)); assert(int(x) < int(typename vecType<T>::_size));
return v[x]; return v[x];
} }
template <typename T> template <typename T>
inline detail::tvec2<T> swizzle inline detail::tvec2<T> swizzle
( (
detail::tvec4<T> const & v, detail::tvec4<T> const & v,
comp x, comp y comp x, comp y
) )
{ {
return detail::tvec2<T>( return detail::tvec2<T>(
v[x], v[x],
v[y]); v[y]);
} }
template <typename T> template <typename T>
inline detail::tvec3<T> swizzle inline detail::tvec3<T> swizzle
( (
detail::tvec4<T> const & v, detail::tvec4<T> const & v,
comp x, comp y, comp z comp x, comp y, comp z
) )
{ {
return detail::tvec3<T>( return detail::tvec3<T>(
v[x], v[x],
v[y], v[y],
v[z]); v[z]);
} }
template <typename T> template <typename T>
inline detail::tvec4<T> swizzle inline detail::tvec4<T> swizzle
( (
detail::tvec4<T> const & v, detail::tvec4<T> const & v,
comp x, comp y, comp z, comp w comp x, comp y, comp z, comp w
) )
{ {
return detail::tvec4<T>(v[x], v[y], v[z], v[w]); return detail::tvec4<T>(v[x], v[y], v[z], v[w]);
} }
template <typename T> template <comp X>
inline detail::tref4<T> swizzle inline int swizzle
( (
detail::tvec4<int> const & v
)
{
return v[X];
}
template <comp X>
inline float swizzle
(
detail::tvec4<float> const & v
)
{
return v[X];
}
template <comp X, comp Y>
inline detail::tvec2<int> swizzle
(
detail::tvec4<int> const & v
)
{
return detail::tvec2<int>(v[X], v[Y]);
}
template <comp X, comp Y>
inline detail::tvec2<float> swizzle
(
detail::tvec4<float> const & v
)
{
return detail::tvec2<float>(v[X], v[Y]);
}
template <comp X, comp Y, comp Z>
inline detail::tvec3<int> swizzle
(
detail::tvec4<int> const & v
)
{
return detail::tvec3<int>(v[X], v[Y], v[Z]);
}
template <comp X, comp Y, comp Z>
inline detail::tvec3<float> swizzle
(
detail::tvec4<float> const & v
)
{
return detail::tvec3<float>(v[X], v[Y], v[Z]);
}
template <comp X, comp Y, comp Z, comp W>
inline detail::tvec4<int> swizzle
(
detail::tvec4<int> const & v
)
{
return detail::tvec4<int>(v[X], v[Y], v[Z], v[W]);
}
template <comp X, comp Y, comp Z, comp W>
inline detail::tvec4<float> swizzle
(
detail::tvec4<float> const & v
)
{
return detail::tvec4<float>(v[X], v[Y], v[Z], v[W]);
}
template <typename T>
inline T& swizzle
(
detail::tvec4<T> & v,
comp x
)
{
return v[x];
}
template <typename T>
inline detail::tref2<T> swizzle
(
detail::tvec4<T> & v,
comp x, comp y
)
{
return detail::tref2<T>(v[x], v[y]);
}
template <typename T>
inline detail::tref3<T> swizzle
(
detail::tvec4<T> & v,
comp x, comp y, comp z
)
{
return detail::tref3<T>(v[x], v[y], v[z]);
}
template <typename T>
inline detail::tref4<T> swizzle
(
detail::tvec4<T> & v, detail::tvec4<T> & v,
comp x, comp y, comp z, comp w comp x, comp y, comp z, comp w
) )
{ {
return detail::tref4<T>(v[x], v[y], v[z], v[w]); return detail::tref4<T>(v[x], v[y], v[z], v[w]);
} }
template <comp X> template <comp x>
inline int swizzle inline float& swizzle
( (
detail::tvec4<int> const & v detail::tvec4<float> & v
) )
{ {
return v[X]; return v[x];
} }
template <comp X> template <comp x>
inline float swizzle inline int& swizzle
( (
detail::tvec4<float> const & v detail::tvec4<int> & v
) )
{ {
return v[X]; return v[x];
} }
template <comp X, comp Y> template <comp x, comp y>
inline detail::tvec2<int> swizzle inline detail::tref2<float> swizzle
( (
detail::tvec4<int> const & v detail::tvec4<float> & v
) )
{ {
return detail::tvec2<int>(v[X], v[Y]); return detail::tref2<float>(v[x], v[y]);
} }
template <comp X, comp Y> template <comp x, comp y>
inline detail::tvec2<float> swizzle inline detail::tref2<int> swizzle
( (
detail::tvec4<float> const & v detail::tvec4<int> & v
) )
{ {
return detail::tvec2<float>(v[X], v[Y]); return detail::tref2<int>(v[x], v[y]);
} }
template <comp X, comp Y, comp Z> template <comp x, comp y, comp z>
inline detail::tvec3<int> swizzle inline detail::tref3<float> swizzle
( (
detail::tvec4<int> const & v detail::tvec4<float> & v
) )
{ {
return detail::tvec3<int>(v[X], v[Y], v[Z]); return detail::tref3<float>(v[x], v[y], v[z]);
} }
template <comp X, comp Y, comp Z> template <comp x, comp y, comp z>
inline detail::tvec3<float> swizzle inline detail::tref3<int> swizzle
( (
detail::tvec4<float> const & v detail::tvec4<int> & v
) )
{ {
return detail::tvec3<float>(v[X], v[Y], v[Z]); return detail::tref3<int>(v[x], v[y], v[z]);
} }
template <comp X, comp Y, comp Z, comp W> template <comp x, comp y, comp z, comp w>
inline detail::tvec4<int> swizzle inline detail::tref4<float> swizzle
( (
detail::tvec4<int> const & v detail::tvec4<float> & v
) )
{ {
return detail::tvec4<int>(v[X], v[Y], v[Z], v[W]); return detail::tref4<float>(v[x], v[y], v[z], v[w]);
} }
template <comp X, comp Y, comp Z, comp W> template <comp x, comp y, comp z, comp w>
inline detail::tvec4<float> swizzle inline detail::tref4<int> swizzle
( (
detail::tvec4<float> const & v detail::tvec4<int> & v
) )
{ {
return detail::tvec4<float>(v[X], v[Y], v[Z], v[W]); return detail::tref4<int>(v[x], v[y], v[z], v[w]);
} }
}//namespace swizzle }//namespace swizzle
}//namespace gtc }//namespace gtc

View File

@ -1,7 +1,7 @@
function(glmCreateTestGTC NAME) function(glmCreateTestGTC NAME)
set(SAMPLE_NAME test-${NAME}) set(SAMPLE_NAME test-${NAME})
add_executable(${SAMPLE_NAME} ${NAME}.cpp) add_executable(${SAMPLE_NAME} ${NAME}.cpp ../test.hpp ../test.cpp)
endfunction(glmCreateTestGTC) endfunction(glmCreateTestGTC)
add_subdirectory(bug) add_subdirectory(bug)

View File

@ -11,12 +11,84 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/swizzle.hpp> #include <glm/gtc/swizzle.hpp>
int test_swizzle_vec4_dynamic() int test_swizzle_vec4_ref_dynamic()
{
{
glm::ivec4 A(0, 1, 2, 3);
glm::ivec4 B(2, 1, 0, 3);
glm::swizzle(A, glm::Z, glm::Y, glm::X, glm::W) = B;
assert(A.x == B.x && A.y == B.y && A.z == B.z && A.w == B.w);
}
{
glm::ivec4 A(0, 1, 2, 3);
glm::ivec3 B(2, 1, 0);
glm::swizzle(A, glm::Z, glm::Y, glm::X) = B;
assert(A.x == B.x && A.y == B.y && A.z == B.z);
}
{
glm::ivec4 A(0, 1, 2, 3);
glm::ivec2 B(2, 1);
glm::swizzle(A, glm::Z, glm::Y) = B;
assert(A.x == B.x && A.y == B.y);
}
{
glm::ivec4 A(0, 1, 2, 3);
int B(2);
glm::swizzle(A, glm::Z) = B;
assert(A.x == B.x && A.y == B.y);
}
return 0;
}
int test_swizzle_vec4_ref_static()
{
{
glm::ivec4 A(0, 1, 2, 3);
glm::ivec4 B(2, 1, 0, 3);
glm::swizzle<glm::Z, glm::Y, glm::X, glm::W>(A) = B;
assert(A.x == B.x && A.y == B.y && A.z == B.z && A.w == B.w);
}
{
glm::ivec4 A(0, 1, 2, 3);
glm::ivec3 B(2, 1, 0);
glm::swizzle<glm::Z, glm::Y, glm::X>(A) = B;
assert(A.x == B.x && A.y == B.y && A.z == B.z);
}
{
glm::ivec4 A(0, 1, 2, 3);
glm::ivec2 B(2, 1);
glm::swizzle<glm::Z, glm::Y>(A) = B;
assert(A.x == B.x && A.y == B.y);
}
{
glm::ivec4 A(0, 1, 2, 3);
int B(2);
glm::swizzle<glm::Z>(A) = B;
assert(A.x == B.x && A.y == B.y);
}
return 0;
}
int test_swizzle_vec4_const_dynamic()
{ {
glm::ivec4 A(0, 1, 2, 3); glm::ivec4 A(0, 1, 2, 3);
glm::ivec4 B = glm::swizzle(A, glm::B, glm::G, glm::R, glm::A); glm::ivec4 B = glm::swizzle(A, glm::B, glm::G, glm::R, glm::A);
assert(glm::all(glm::equal(A, B)));
glm::ivec3 C = glm::swizzle(A, glm::W, glm::Y, glm::Z); glm::ivec3 C = glm::swizzle(A, glm::W, glm::Y, glm::Z);
assert(glm::all(glm::equal(A, C)));
glm::ivec2 D = glm::swizzle(A, glm::W, glm::X); glm::ivec2 D = glm::swizzle(A, glm::W, glm::X);
assert(glm::all(glm::equal(A, D)));
assert(D.x == A.w && D.y == A.x); assert(D.x == A.w && D.y == A.x);
int E = glm::swizzle(A, glm::Q); int E = glm::swizzle(A, glm::Q);
assert(E == A.q); assert(E == A.q);
@ -24,12 +96,19 @@ int test_swizzle_vec4_dynamic()
return 0; return 0;
} }
int test_swizzle_vec4_static() int test_swizzle_vec4_const_static()
{ {
glm::ivec4 A(0, 1, 2, 3); glm::ivec4 A(0, 1, 2, 3);
glm::ivec4 B = glm::swizzle<glm::B, glm::G, glm::R, glm::A>(A); glm::ivec4 B = glm::swizzle<glm::B, glm::G, glm::R, glm::A>(A);
assert(glm::all(glm::equal(A, B)));
glm::ivec3 C = glm::swizzle<glm::W, glm::Y, glm::Z>(A); glm::ivec3 C = glm::swizzle<glm::W, glm::Y, glm::Z>(A);
assert(glm::all(glm::equal(A, C)));
glm::ivec2 D = glm::swizzle<glm::W, glm::X>(A); glm::ivec2 D = glm::swizzle<glm::W, glm::X>(A);
assert(glm::all(glm::equal(A, D)));
int E = glm::swizzle<glm::Q>(A); int E = glm::swizzle<glm::Q>(A);
assert(E == A.q); assert(E == A.q);
@ -39,8 +118,10 @@ int test_swizzle_vec4_static()
int main(int argc, void* argv[]) int main(int argc, void* argv[])
{ {
int Failed = 0; int Failed = 0;
Failed += test_swizzle_vec4_dynamic(); Failed += test_swizzle_vec4_ref_dynamic();
Failed += test_swizzle_vec4_static(); Failed += test_swizzle_vec4_ref_static();
Failed += test_swizzle_vec4_const_dynamic();
Failed += test_swizzle_vec4_const_static();
return Failed; return Failed;
} }

0
test/test.cpp Normal file
View File

38
test/test.hpp Normal file
View File

@ -0,0 +1,38 @@
#ifndef glm_test_included
#define glm_test_included
#include <string>
namespace glm{
namespace test
{
class test
{
enum result
{
PASSED,
FAILED,
ASSERT,
STATIC,
MAX
};
public:
test(std::string const & Name, std::size_t const & Count);
result & operator[](std::size_t const & Index);
result const & operator[](std::size_t const & Index) const;
static int get(result const Result) const;
static void log(test const & Test);
protected:
std::string Name;
std::vertor<result> Tests;
static test Result[MAX];
};
}//namespace test
}//namespace glm
#endif//glm_test_included