From 71855943e0fb729a1bb3b7b6f752755ab52fac91 Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Sun, 29 Sep 2013 19:44:59 +0200 Subject: [PATCH] Added initializer lists --- glm/core/setup.hpp | 7 ++++ glm/core/type_vec1.hpp | 9 +++++ glm/core/type_vec1.inl | 10 +++++ glm/core/type_vec2.hpp | 8 ++++ glm/core/type_vec2.inl | 11 ++++++ glm/core/type_vec3.hpp | 8 ++++ glm/core/type_vec3.inl | 12 ++++++ glm/core/type_vec4.hpp | 8 ++++ glm/core/type_vec4.inl | 13 ++++++ readme.txt | 1 + test/core/core_type_vec4.cpp | 77 ++++++++++++++++++++++++++++++++++++ test/gtc/gtc_quaternion.cpp | 33 ++++++++++++++++ 12 files changed, 197 insertions(+) diff --git a/glm/core/setup.hpp b/glm/core/setup.hpp index 1c877af3..5a490d99 100644 --- a/glm/core/setup.hpp +++ b/glm/core/setup.hpp @@ -511,6 +511,13 @@ ((GLM_LANG & GLM_LANG_CXX0X_FLAG) && (GLM_COMPILER & GLM_COMPILER_GCC) && (GLM_COMPILER >= GLM_COMPILER_GCC46)) || \ __has_feature(cxx_constexpr)) +// N2672 +#define GLM_HAS_INITIALIZER_LISTS ( \ + (GLM_LANG & GLM_LANG_CXX11_FLAG) || \ + ((GLM_LANG & GLM_LANG_CXX0X_FLAG) && (GLM_COMPILER & GLM_COMPILER_VC) && (GLM_COMPILER >= GLM_COMPILER_VC2012)) || \ + ((GLM_LANG & GLM_LANG_CXX0X_FLAG) && (GLM_COMPILER & GLM_COMPILER_GCC) && (GLM_COMPILER >= GLM_COMPILER_GCC44)) || \ + __has_feature(cxx_generalized_initializers)) + // OpenMP #ifdef _OPENMP # if(GLM_COMPILER & GLM_COMPILER_GCC) diff --git a/glm/core/type_vec1.hpp b/glm/core/type_vec1.hpp index 1f56c0e0..4b1fe320 100644 --- a/glm/core/type_vec1.hpp +++ b/glm/core/type_vec1.hpp @@ -38,6 +38,10 @@ # include "_swizzle_func.hpp" # endif #endif //GLM_SWIZZLE +#if(GLM_HAS_INITIALIZER_LISTS) +# include +#endif //GLM_HAS_INITIALIZER_LISTS +#include namespace glm{ namespace detail @@ -78,6 +82,11 @@ namespace detail template GLM_FUNC_DECL tvec1(tvec1 const & v); +#if(GLM_HAS_INITIALIZER_LISTS) + template + GLM_FUNC_DECL tvec1(std::initializer_list const & v); +#endif//GLM_HAS_INITIALIZER_LISTS + ////////////////////////////////////// // Explicit basic constructors diff --git a/glm/core/type_vec1.inl b/glm/core/type_vec1.inl index d193a062..735d4c0c 100644 --- a/glm/core/type_vec1.inl +++ b/glm/core/type_vec1.inl @@ -71,6 +71,16 @@ namespace detail x(v.x) {} +#if(GLM_HAS_INITIALIZER_LISTS) + template + template + GLM_FUNC_QUALIFIER tvec1::tvec1(std::initializer_list const & v) : + x(static_cast(v.begin()[0])) + { + assert(v.size() >= this->length()); + } +#endif//GLM_HAS_INITIALIZER_LISTS + ////////////////////////////////////// // Explicit basic constructors diff --git a/glm/core/type_vec2.hpp b/glm/core/type_vec2.hpp index 7688879e..00e1d7af 100644 --- a/glm/core/type_vec2.hpp +++ b/glm/core/type_vec2.hpp @@ -38,6 +38,9 @@ # include "_swizzle_func.hpp" # endif #endif //GLM_SWIZZLE +#if(GLM_HAS_INITIALIZER_LISTS) +# include +#endif //GLM_HAS_INITIALIZER_LISTS #include namespace glm{ @@ -103,6 +106,11 @@ namespace detail template GLM_FUNC_DECL tvec2(tvec2 const & v); +#if(GLM_HAS_INITIALIZER_LISTS) + template + GLM_FUNC_DECL tvec2(std::initializer_list const & v); +#endif//GLM_HAS_INITIALIZER_LISTS + ////////////////////////////////////// // Explicit basic constructors diff --git a/glm/core/type_vec2.inl b/glm/core/type_vec2.inl index 057b2a82..339d46bb 100644 --- a/glm/core/type_vec2.inl +++ b/glm/core/type_vec2.inl @@ -74,6 +74,17 @@ namespace detail y(v.y) {} +#if(GLM_HAS_INITIALIZER_LISTS) + template + template + GLM_FUNC_QUALIFIER tvec2::tvec2(std::initializer_list const & v) : + x(static_cast(v.begin()[0])), + y(static_cast(v.begin()[1])) + { + assert(v.size() >= this->length()); + } +#endif//GLM_HAS_INITIALIZER_LISTS + ////////////////////////////////////// // Explicit basic constructors diff --git a/glm/core/type_vec3.hpp b/glm/core/type_vec3.hpp index a0494792..cd48a6ed 100644 --- a/glm/core/type_vec3.hpp +++ b/glm/core/type_vec3.hpp @@ -38,6 +38,9 @@ # include "_swizzle_func.hpp" # endif #endif //GLM_SWIZZLE +#if(GLM_HAS_INITIALIZER_LISTS) +# include +#endif //GLM_HAS_INITIALIZER_LISTS #include namespace glm{ @@ -104,6 +107,11 @@ namespace detail template GLM_FUNC_DECL tvec3(tvec3 const & v); +#if(GLM_HAS_INITIALIZER_LISTS) + template + GLM_FUNC_DECL tvec3(std::initializer_list const & v); +#endif//GLM_HAS_INITIALIZER_LISTS + ////////////////////////////////////// // Explicit basic constructors diff --git a/glm/core/type_vec3.inl b/glm/core/type_vec3.inl index 7ba1dbea..5ccb1e37 100644 --- a/glm/core/type_vec3.inl +++ b/glm/core/type_vec3.inl @@ -77,6 +77,18 @@ namespace detail z(v.z) {} +#if(GLM_HAS_INITIALIZER_LISTS) + template + template + GLM_FUNC_QUALIFIER tvec3::tvec3(std::initializer_list const & v) : + x(static_cast(v.begin()[0])), + y(static_cast(v.begin()[1])), + z(static_cast(v.begin()[2])) + { + assert(v.size() >= this->length()); + } +#endif//GLM_HAS_INITIALIZER_LISTS + ////////////////////////////////////// // Explicit basic constructors diff --git a/glm/core/type_vec4.hpp b/glm/core/type_vec4.hpp index daef7f7e..9f4f07b2 100644 --- a/glm/core/type_vec4.hpp +++ b/glm/core/type_vec4.hpp @@ -39,6 +39,9 @@ # include "_swizzle_func.hpp" # endif #endif //GLM_SWIZZLE +#if(GLM_HAS_INITIALIZER_LISTS) +# include +#endif //GLM_HAS_INITIALIZER_LISTS #include namespace glm{ @@ -106,6 +109,11 @@ namespace detail template GLM_FUNC_DECL tvec4(tvec4 const & v); +#if(GLM_HAS_INITIALIZER_LISTS) + template + GLM_FUNC_DECL tvec4(std::initializer_list const & v); +#endif//GLM_HAS_INITIALIZER_LISTS + ////////////////////////////////////// // Explicit basic constructors diff --git a/glm/core/type_vec4.inl b/glm/core/type_vec4.inl index e1f339a4..78410301 100644 --- a/glm/core/type_vec4.inl +++ b/glm/core/type_vec4.inl @@ -80,6 +80,19 @@ namespace detail w(v.w) {} +#if(GLM_HAS_INITIALIZER_LISTS) + template + template + GLM_FUNC_QUALIFIER tvec4::tvec4(std::initializer_list const & v) : + x(static_cast(v.begin()[0])), + y(static_cast(v.begin()[1])), + z(static_cast(v.begin()[2])), + w(static_cast(v.begin()[3])) + { + assert(v.size() >= this->length()); + } +#endif//GLM_HAS_INITIALIZER_LISTS + ////////////////////////////////////// // Explicit basic constructors diff --git a/readme.txt b/readme.txt index a392c6ca..7fa5f0c9 100644 --- a/readme.txt +++ b/readme.txt @@ -66,6 +66,7 @@ GLM 0.9.5.0: 2013-XX-XX - Allowed including individual core feature - Increased unit tests completness - Added creating of a quaternion from two vectors +- Added C++11 initializer lists ================================================================================ GLM 0.9.4.6: 2013-09-20 diff --git a/test/core/core_type_vec4.cpp b/test/core/core_type_vec4.cpp index f22d03c0..b97be6d7 100644 --- a/test/core/core_type_vec4.cpp +++ b/test/core/core_type_vec4.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include template @@ -39,6 +40,10 @@ int test_vec4_ctor() { int Error = 0; +#if(GLM_HAS_INITIALIZER_LISTS) + glm::vec4 v{0, 1, 2, 3}; +#endif + { glm::vec4 A(1); glm::vec4 B(1, 1, 1, 1); @@ -282,6 +287,74 @@ int test_operator_increment() return Error; } +struct AoS +{ + glm::vec4 A; + glm::vec3 B; + glm::vec3 C; + glm::vec2 D; +}; + +int test_vec4_perf_AoS(std::size_t Size) +{ + int Error(0); + + std::vector In; + std::vector Out; + In.resize(Size); + Out.resize(Size); + + std::clock_t StartTime = std::clock(); + + for(std::size_t i = 0; i < In.size(); ++i) + Out[i] = In[i]; + + std::clock_t EndTime = std::clock(); + + printf("AoS: %d\n", EndTime - StartTime); + + return Error; +} + +int test_vec4_perf_SoA(std::size_t Size) +{ + int Error(0); + + std::vector InA; + std::vector InB; + std::vector InC; + std::vector InD; + std::vector OutA; + std::vector OutB; + std::vector OutC; + std::vector OutD; + + InA.resize(Size); + InB.resize(Size); + InC.resize(Size); + InD.resize(Size); + OutA.resize(Size); + OutB.resize(Size); + OutC.resize(Size); + OutD.resize(Size); + + std::clock_t StartTime = std::clock(); + + for(std::size_t i = 0; i < InA.size(); ++i) + { + OutA[i] = InA[i]; + OutB[i] = InB[i]; + OutC[i] = InC[i]; + OutD[i] = InD[i]; + } + + std::clock_t EndTime = std::clock(); + + printf("SoA: %d\n", EndTime - StartTime); + + return Error; +} + int main() { //__m128 DataA = swizzle(glm::vec4(1.0f, 2.0f, 3.0f, 4.0f)); @@ -289,6 +362,10 @@ int main() int Error(0); + std::size_t const Size(100000000); + + Error += test_vec4_perf_AoS(Size); + Error += test_vec4_perf_SoA(Size); Error += test_vec4_ctor(); Error += test_vec4_size(); Error += test_vec4_operators(); diff --git a/test/gtc/gtc_quaternion.cpp b/test/gtc/gtc_quaternion.cpp index e3528679..be1638f7 100644 --- a/test/gtc/gtc_quaternion.cpp +++ b/test/gtc/gtc_quaternion.cpp @@ -197,6 +197,37 @@ int test_quat_slerp() return Error; } +int test_quat_mul() +{ + int Error(0); + + glm::quat temp1 = glm::normalize(glm::quat(1.0f, glm::vec3(0.0, 1.0, 0.0))); + glm::quat temp2 = glm::normalize(glm::quat(0.5f, glm::vec3(1.0, 0.0, 0.0))); + + glm::vec3 transformed0 = (temp1 * glm::vec3(0.0, 1.0, 0.0) * glm::inverse(temp1)); + glm::vec3 temp4 = temp2 * transformed0 * glm::inverse(temp2); + + glm::quat temp5 = glm::normalize(temp1 * temp2); + glm::vec3 temp6 = temp5 * glm::vec3(0.0, 1.0, 0.0) * glm::inverse(temp5); + + return Error; +} + +int test_quat_two_axis_ctr() +{ + int Error(0); + + glm::quat q1(glm::vec3(1, 0, 0), glm::vec3(0, 1, 0)); + glm::vec3 v1 = q1 * glm::vec3(1, 0, 0); + Error += glm::all(glm::epsilonEqual(v1, glm::vec3(0, 1, 0), 0.0001f)) ? 0 : 1; + + glm::quat q2 = q1 * q1; + glm::vec3 v2 = q2 * glm::vec3(1, 0, 0); + Error += glm::all(glm::epsilonEqual(v2, glm::vec3(-1, 0, 0), 0.0001f)) ? 0 : 1; + + return Error; +} + int test_quat_type() { glm::quat A; @@ -209,6 +240,8 @@ int main() { int Error(0); + Error += test_quat_two_axis_ctr(); + Error += test_quat_mul(); Error += test_quat_precision(); Error += test_quat_type(); Error += test_quat_angle();