From a9ab80d5fd64461864f3cf2f944c09ee8d7ebea1 Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Fri, 25 Sep 2015 03:14:11 +0200 Subject: [PATCH] Added compNormalize function to GTX_component_wise --- glm/detail/func_geometric.inl | 20 +++--- glm/ext.hpp | 1 - glm/gtx/component_wise.hpp | 18 ++--- glm/gtx/component_wise.inl | 45 ++++++++++++- glm/gtx/float_normalize.hpp | 64 ------------------ glm/gtx/float_normalize.inl | 77 ---------------------- test/gtx/CMakeLists.txt | 1 - test/gtx/gtx_component_wise.cpp | 73 +++++++++++++++++++- test/gtx/gtx_float_normalize.cpp | 110 ------------------------------- 9 files changed, 136 insertions(+), 273 deletions(-) delete mode 100644 glm/gtx/float_normalize.hpp delete mode 100644 glm/gtx/float_normalize.inl delete mode 100644 test/gtx/gtx_float_normalize.cpp diff --git a/glm/detail/func_geometric.inl b/glm/detail/func_geometric.inl index 0673718b..6c1663d3 100644 --- a/glm/detail/func_geometric.inl +++ b/glm/detail/func_geometric.inl @@ -86,7 +86,7 @@ namespace detail template GLM_FUNC_QUALIFIER genType length(genType x) { - GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'length' only accept floating-point inputs"); + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'length' accepts only floating-point inputs"); return abs(x); } @@ -94,7 +94,7 @@ namespace detail template class vecType> GLM_FUNC_QUALIFIER T length(vecType const & v) { - GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'length' only accept floating-point inputs"); + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'length' accepts only floating-point inputs"); return sqrt(dot(v, v)); } @@ -103,7 +103,7 @@ namespace detail template GLM_FUNC_QUALIFIER genType distance(genType const & p0, genType const & p1) { - GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'distance' only accept floating-point inputs"); + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'distance' accepts only floating-point inputs"); return length(p1 - p0); } @@ -118,14 +118,14 @@ namespace detail template GLM_FUNC_QUALIFIER T dot(T x, T y) { - GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'dot' only accept floating-point inputs"); + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'dot' accepts only floating-point inputs"); return x * y; } template class vecType> GLM_FUNC_QUALIFIER T dot(vecType const & x, vecType const & y) { - GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'dot' only accept floating-point inputs"); + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'dot' accepts only floating-point inputs"); return detail::compute_dot::call(x, y); } @@ -133,7 +133,7 @@ namespace detail template GLM_FUNC_QUALIFIER tvec3 cross(tvec3 const & x, tvec3 const & y) { - GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'cross' only accept floating-point inputs"); + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'cross' accepts only floating-point inputs"); return tvec3( x.y * y.z - y.y * x.z, @@ -145,7 +145,7 @@ namespace detail template GLM_FUNC_QUALIFIER genType normalize(genType const & x) { - GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'normalize' only accept floating-point inputs"); + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'normalize' accepts only floating-point inputs"); return x < genType(0) ? genType(-1) : genType(1); } @@ -153,7 +153,7 @@ namespace detail template class vecType> GLM_FUNC_QUALIFIER vecType normalize(vecType const & x) { - GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'normalize' only accept floating-point inputs"); + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'normalize' accepts only floating-point inputs"); return x * inversesqrt(dot(x, x)); } @@ -182,7 +182,7 @@ namespace detail template GLM_FUNC_QUALIFIER genType refract(genType const & I, genType const & N, genType const & eta) { - GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'refract' only accept floating-point inputs"); + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'refract' accepts only floating-point inputs"); genType const dotValue(dot(N, I)); genType const k(static_cast(1) - eta * eta * (static_cast(1) - dotValue * dotValue)); @@ -192,7 +192,7 @@ namespace detail template class vecType> GLM_FUNC_QUALIFIER vecType refract(vecType const & I, vecType const & N, T eta) { - GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'refract' only accept floating-point inputs"); + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'refract' accepts only floating-point inputs"); T const dotValue(dot(N, I)); T const k(static_cast(1) - eta * eta * (static_cast(1) - dotValue * dotValue)); diff --git a/glm/ext.hpp b/glm/ext.hpp index 736c386d..bdf050a9 100644 --- a/glm/ext.hpp +++ b/glm/ext.hpp @@ -94,7 +94,6 @@ #include "./gtx/fast_exponential.hpp" #include "./gtx/fast_square_root.hpp" #include "./gtx/fast_trigonometry.hpp" -#include "./gtx/float_normalize.hpp" #include "./gtx/gradient_paint.hpp" #include "./gtx/handed_coordinate_space.hpp" #include "./gtx/integer.hpp" diff --git a/glm/gtx/component_wise.hpp b/glm/gtx/component_wise.hpp index d5b3d818..4c19f85b 100644 --- a/glm/gtx/component_wise.hpp +++ b/glm/gtx/component_wise.hpp @@ -54,29 +54,31 @@ namespace glm /// @addtogroup gtx_component_wise /// @{ + /// Convert an integer vector to a normalized float vector. + /// If the parameter value type is already a floating precision type, the value is passed through. + /// @see gtx_component_wise + template class vecType> + GLM_FUNC_DECL vecType compNormalize(vecType const & v); + /// Add all vector components together. /// @see gtx_component_wise template - GLM_FUNC_DECL typename genType::value_type compAdd( - genType const & v); + GLM_FUNC_DECL typename genType::value_type compAdd(genType const & v); /// Multiply all vector components together. /// @see gtx_component_wise template - GLM_FUNC_DECL typename genType::value_type compMul( - genType const & v); + GLM_FUNC_DECL typename genType::value_type compMul(genType const & v); /// Find the minimum value between single vector components. /// @see gtx_component_wise template - GLM_FUNC_DECL typename genType::value_type compMin( - genType const & v); + GLM_FUNC_DECL typename genType::value_type compMin(genType const & v); /// Find the maximum value between single vector components. /// @see gtx_component_wise template - GLM_FUNC_DECL typename genType::value_type compMax( - genType const & v); + GLM_FUNC_DECL typename genType::value_type compMax(genType const & v); /// @} }//namespace glm diff --git a/glm/gtx/component_wise.inl b/glm/gtx/component_wise.inl index f944faeb..2a27f6f0 100644 --- a/glm/gtx/component_wise.inl +++ b/glm/gtx/component_wise.inl @@ -30,8 +30,51 @@ /// @author Christophe Riccio /////////////////////////////////////////////////////////////////////////////////////////////////// -namespace glm +namespace glm{ +namespace detail { + template class vecType, bool isInteger, bool signedType> + struct compute_compNormalize + {}; + + template class vecType> + struct compute_compNormalize + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & v) + { + floatType const Min = static_cast(std::numeric_limits::min()); + floatType const Max = static_cast(std::numeric_limits::max()); + return (vecType(v) - Min) / (Max - Min) * static_cast(2) - static_cast(1); + } + }; + + template class vecType> + struct compute_compNormalize + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & v) + { + return vecType(v) / static_cast(std::numeric_limits::max()); + } + }; + + template class vecType> + struct compute_compNormalize + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & v) + { + return v; + } + }; +}//namespace detail + + template class vecType> + GLM_FUNC_QUALIFIER vecType compNormalize(vecType const & v) + { + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'compNormalize' accepts only floating-point types for 'floatType' template parameter"); + + return detail::compute_compNormalize::is_integer, std::numeric_limits::is_signed>::call(v); + } + template class vecType> GLM_FUNC_QUALIFIER T compAdd(vecType const & v) { diff --git a/glm/gtx/float_normalize.hpp b/glm/gtx/float_normalize.hpp deleted file mode 100644 index c34fcd28..00000000 --- a/glm/gtx/float_normalize.hpp +++ /dev/null @@ -1,64 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////////// -/// OpenGL Mathematics (glm.g-truc.net) -/// -/// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net) -/// Permission is hereby granted, free of charge, to any person obtaining a copy -/// of this software and associated documentation files (the "Software"), to deal -/// in the Software without restriction, including without limitation the rights -/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -/// copies of the Software, and to permit persons to whom the Software is -/// furnished to do so, subject to the following conditions: -/// -/// The above copyright notice and this permission notice shall be included in -/// all copies or substantial portions of the Software. -/// -/// Restrictions: -/// By making use of the Software for military purposes, you choose to make -/// a Bunny unhappy. -/// -/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -/// THE SOFTWARE. -/// -/// @ref gtx_float_normalize -/// @file glm/gtx/float_normalize.hpp -/// @date 2015-09-25 / 2015-09-25 -/// @author Christophe Riccio -/// -/// @see core (dependence) -/// -/// @defgroup gtx_float_normalize GLM_GTX_float_normalize -/// @ingroup gtx -/// -/// @brief Convert integer vectors into normalized float vectors. -/// -/// need to be included to use these functionalities. -/////////////////////////////////////////////////////////////////////////////////// - -#pragma once - -// Dependency: -#include "../glm.hpp" - -#if(defined(GLM_MESSAGES) && !defined(GLM_EXT_INCLUDED)) -# pragma message("GLM: GLM_GTX_float_normalize extension included") -#endif - -namespace glm -{ - /// @addtogroup gtx_float_normalize - /// @{ - - /// Converts a color from HSV color space to its color in RGB color space. - /// @see gtx_float_normalize - template class vecType> - GLM_FUNC_DECL vecType floatNormalize(vecType const & v); - - /// @} -}//namespace glm - -#include "float_normalize.inl" diff --git a/glm/gtx/float_normalize.inl b/glm/gtx/float_normalize.inl deleted file mode 100644 index 9caebd18..00000000 --- a/glm/gtx/float_normalize.inl +++ /dev/null @@ -1,77 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////////// -/// OpenGL Mathematics (glm.g-truc.net) -/// -/// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net) -/// Permission is hereby granted, free of charge, to any person obtaining a copy -/// of this software and associated documentation files (the "Software"), to deal -/// in the Software without restriction, including without limitation the rights -/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -/// copies of the Software, and to permit persons to whom the Software is -/// furnished to do so, subject to the following conditions: -/// -/// The above copyright notice and this permission notice shall be included in -/// all copies or substantial portions of the Software. -/// -/// Restrictions: -/// By making use of the Software for military purposes, you choose to make -/// a Bunny unhappy. -/// -/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -/// THE SOFTWARE. -/// -/// @ref gtx_float_normalize -/// @file glm/gtx/float_normalize.inl -/// @date 2015-09-25 / 2015-09-25 -/// @author Christophe Riccio -/////////////////////////////////////////////////////////////////////////////////// - -#include - -namespace glm{ -namespace detail -{ - template class vecType, bool isInteger, bool signedType> - struct compute_floatNormalize - {}; - - template class vecType> - struct compute_floatNormalize - { - GLM_FUNC_QUALIFIER static vecType call(vecType const & v) - { - floatType const Min = static_cast(std::numeric_limits::min()); - floatType const Max = static_cast(std::numeric_limits::max()); - return (vecType(v) - Min) / (Max - Min) * static_cast(2) - static_cast(1); - } - }; - - template class vecType> - struct compute_floatNormalize - { - GLM_FUNC_QUALIFIER static vecType call(vecType const & v) - { - return vecType(v) / static_cast(std::numeric_limits::max()); - } - }; - - template class vecType> - struct compute_floatNormalize - { - GLM_FUNC_QUALIFIER static vecType call(vecType const & v) - { - return v; - } - }; -}//namespace detail - - template class vecType> - GLM_FUNC_QUALIFIER vecType floatNormalize(vecType const & v) - { - return detail::compute_floatNormalize::is_integer, std::numeric_limits::is_signed>::call(v); - } -}//namespace glm diff --git a/test/gtx/CMakeLists.txt b/test/gtx/CMakeLists.txt index 3f39423b..3d61f1dd 100644 --- a/test/gtx/CMakeLists.txt +++ b/test/gtx/CMakeLists.txt @@ -11,7 +11,6 @@ glmCreateTestGTC(gtx_extented_min_max) glmCreateTestGTC(gtx_fast_exponential) glmCreateTestGTC(gtx_fast_square_root) glmCreateTestGTC(gtx_fast_trigonometry) -glmCreateTestGTC(gtx_float_normalize) glmCreateTestGTC(gtx_gradient_paint) glmCreateTestGTC(gtx_handed_coordinate_space) glmCreateTestGTC(gtx_integer) diff --git a/test/gtx/gtx_component_wise.cpp b/test/gtx/gtx_component_wise.cpp index 3979d823..2556a717 100644 --- a/test/gtx/gtx_component_wise.cpp +++ b/test/gtx/gtx_component_wise.cpp @@ -25,15 +25,86 @@ /// THE SOFTWARE. /// /// @file test/gtx/gtx_component_wise.cpp -/// @date 2013-10-25 / 2014-11-25 +/// @date 2013-10-25 / 2015-09-25 /// @author Christophe Riccio /////////////////////////////////////////////////////////////////////////////////// #include +#include +#include +#include +#include + +namespace integer_8bit_test +{ + int run() + { + int Error(0); + + { + glm::vec4 const A = glm::compNormalize(glm::u8vec4(0, 127, 128, 255)); + + Error += glm::epsilonEqual(A.x, 0.0f, glm::epsilon()) ? 0 : 1; + Error += A.y < 0.5f ? 0 : 1; + Error += A.z > 0.5f ? 0 : 1; + Error += glm::epsilonEqual(A.w, 1.0f, glm::epsilon()) ? 0 : 1; + } + + { + glm::vec4 const A = glm::compNormalize(glm::i8vec4(-128, -1, 0, 127)); + + Error += glm::epsilonEqual(A.x,-1.0f, glm::epsilon()) ? 0 : 1; + Error += A.y < 0.0f ? 0 : 1; + Error += A.z > 0.0f ? 0 : 1; + Error += glm::epsilonEqual(A.w, 1.0f, glm::epsilon()) ? 0 : 1; + } + + return Error; + } +}//namespace integer_8bit_test + +namespace integer_16bit_test +{ + int run() + { + int Error(0); + + { + glm::vec4 const A = glm::compNormalize(glm::u16vec4( + std::numeric_limits::min(), + (std::numeric_limits::max() >> 1) + 0, + (std::numeric_limits::max() >> 1) + 1, + std::numeric_limits::max())); + + Error += glm::epsilonEqual(A.x, 0.0f, glm::epsilon()) ? 0 : 1; + Error += A.y < 0.5f ? 0 : 1; + Error += A.z > 0.5f ? 0 : 1; + Error += glm::epsilonEqual(A.w, 1.0f, glm::epsilon()) ? 0 : 1; + } + + { + glm::vec4 const A = glm::compNormalize(glm::i16vec4( + std::numeric_limits::min(), + static_cast(-1), + static_cast(0), + std::numeric_limits::max())); + + Error += glm::epsilonEqual(A.x,-1.0f, glm::epsilon()) ? 0 : 1; + Error += A.y < 0.0f ? 0 : 1; + Error += A.z > 0.0f ? 0 : 1; + Error += glm::epsilonEqual(A.w, 1.0f, glm::epsilon()) ? 0 : 1; + } + + return Error; + } +}//namespace integer_16bit_test int main() { int Error(0); + Error += integer_8bit_test::run(); + Error += integer_16bit_test::run(); + return Error; } diff --git a/test/gtx/gtx_float_normalize.cpp b/test/gtx/gtx_float_normalize.cpp deleted file mode 100644 index bceaa494..00000000 --- a/test/gtx/gtx_float_normalize.cpp +++ /dev/null @@ -1,110 +0,0 @@ -////////////////////////////////////////////////////////////////////////////////// -/// OpenGL Mathematics (glm.g-truc.net) -/// -/// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net) -/// Permission is hereby granted, free of charge, to any person obtaining a copy -/// of this software and associated documentation files (the "Software"), to deal -/// in the Software without restriction, including without limitation the rights -/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -/// copies of the Software, and to permit persons to whom the Software is -/// furnished to do so, subject to the following conditions: -/// -/// The above copyright notice and this permission notice shall be included in -/// all copies or substantial portions of the Software. -/// -/// Restrictions: -/// By making use of the Software for military purposes, you choose to make -/// a Bunny unhappy. -/// -/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -/// THE SOFTWARE. -/// -/// @file test/gtx/gtx_float_normalize.cpp -/// @date 2015-09-25 / 2015-09-25 -/// @author Christophe Riccio -/////////////////////////////////////////////////////////////////////////////////// - -#include -#include -#include -#include -#include - -namespace integer_8bit_test -{ - int run() - { - int Error(0); - - { - glm::vec4 const A = glm::floatNormalize(glm::u8vec4(0, 127, 128, 255)); - - Error += glm::epsilonEqual(A.x, 0.0f, glm::epsilon()) ? 0 : 1; - Error += A.y < 0.5f ? 0 : 1; - Error += A.z > 0.5f ? 0 : 1; - Error += glm::epsilonEqual(A.w, 1.0f, glm::epsilon()) ? 0 : 1; - } - - { - glm::vec4 const A = glm::floatNormalize(glm::i8vec4(-128, -1, 0, 127)); - - Error += glm::epsilonEqual(A.x,-1.0f, glm::epsilon()) ? 0 : 1; - Error += A.y < 0.0f ? 0 : 1; - Error += A.z > 0.0f ? 0 : 1; - Error += glm::epsilonEqual(A.w, 1.0f, glm::epsilon()) ? 0 : 1; - } - - return Error; - } -}//namespace integer_8bit_test - -namespace integer_16bit_test -{ - int run() - { - int Error(0); - - { - glm::vec4 const A = glm::floatNormalize(glm::u16vec4( - std::numeric_limits::min(), - (std::numeric_limits::max() >> 1) + 0, - (std::numeric_limits::max() >> 1) + 1, - std::numeric_limits::max())); - - Error += glm::epsilonEqual(A.x, 0.0f, glm::epsilon()) ? 0 : 1; - Error += A.y < 0.5f ? 0 : 1; - Error += A.z > 0.5f ? 0 : 1; - Error += glm::epsilonEqual(A.w, 1.0f, glm::epsilon()) ? 0 : 1; - } - - { - glm::vec4 const A = glm::floatNormalize(glm::i16vec4( - std::numeric_limits::min(), - static_cast(-1), - static_cast(0), - std::numeric_limits::max())); - - Error += glm::epsilonEqual(A.x,-1.0f, glm::epsilon()) ? 0 : 1; - Error += A.y < 0.0f ? 0 : 1; - Error += A.z > 0.0f ? 0 : 1; - Error += glm::epsilonEqual(A.w, 1.0f, glm::epsilon()) ? 0 : 1; - } - - return Error; - } -}//namespace integer_16bit_test - -int main() -{ - int Error(0); - - Error += integer_8bit_test::run(); - Error += integer_16bit_test::run(); - - return Error; -}