From 998feff87e8e13c719c20c4003c1866d3b0f9097 Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Fri, 25 Sep 2015 02:39:41 +0200 Subject: [PATCH] Added GTX_float_normalize --- glm/ext.hpp | 1 + glm/gtx/float_normalize.hpp | 64 ++++++++++++++++++ glm/gtx/float_normalize.inl | 77 ++++++++++++++++++++++ glm/gtx/float_notmalize.inl | 43 ++++++++++++ test/gtx/CMakeLists.txt | 1 + test/gtx/gtx_float_normalize.cpp | 110 +++++++++++++++++++++++++++++++ 6 files changed, 296 insertions(+) create mode 100644 glm/gtx/float_normalize.hpp create mode 100644 glm/gtx/float_normalize.inl create mode 100644 glm/gtx/float_notmalize.inl create mode 100644 test/gtx/gtx_float_normalize.cpp diff --git a/glm/ext.hpp b/glm/ext.hpp index bdf050a9..736c386d 100644 --- a/glm/ext.hpp +++ b/glm/ext.hpp @@ -94,6 +94,7 @@ #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/float_normalize.hpp b/glm/gtx/float_normalize.hpp new file mode 100644 index 00000000..c34fcd28 --- /dev/null +++ b/glm/gtx/float_normalize.hpp @@ -0,0 +1,64 @@ +/////////////////////////////////////////////////////////////////////////////////// +/// 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 new file mode 100644 index 00000000..9caebd18 --- /dev/null +++ b/glm/gtx/float_normalize.inl @@ -0,0 +1,77 @@ +/////////////////////////////////////////////////////////////////////////////////// +/// 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/glm/gtx/float_notmalize.inl b/glm/gtx/float_notmalize.inl new file mode 100644 index 00000000..361c8829 --- /dev/null +++ b/glm/gtx/float_notmalize.inl @@ -0,0 +1,43 @@ +/////////////////////////////////////////////////////////////////////////////////// +/// 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 +{ + template class vecType> + GLM_FUNC_QUALIFIER vecType floatNormalize(vecType const & v) + { + return vecType(v) / static_cast(std::numeric_limits::max()); + } + +}//namespace glm diff --git a/test/gtx/CMakeLists.txt b/test/gtx/CMakeLists.txt index 3d61f1dd..3f39423b 100644 --- a/test/gtx/CMakeLists.txt +++ b/test/gtx/CMakeLists.txt @@ -11,6 +11,7 @@ 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_float_normalize.cpp b/test/gtx/gtx_float_normalize.cpp new file mode 100644 index 00000000..bceaa494 --- /dev/null +++ b/test/gtx/gtx_float_normalize.cpp @@ -0,0 +1,110 @@ +////////////////////////////////////////////////////////////////////////////////// +/// 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; +}