From ec45d1314cc8830147b47e436192d314d100affc Mon Sep 17 00:00:00 2001 From: Carsten Meyer Date: Wed, 23 Jul 2025 18:42:14 +0200 Subject: [PATCH 1/2] Adressed implicit conversion issues in quats.h Implicit conversions from constants (e.g. 1.0 which is a double) into value_type break compilation in VisualStudio with compiler settings "/W3 /WX". These changes also improve performance by allowing the compiler to use the value_type variants of sin and cos. --- include/vsg/maths/quat.h | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/include/vsg/maths/quat.h b/include/vsg/maths/quat.h index 726097b3d2..d49cde6642 100644 --- a/include/vsg/maths/quat.h +++ b/include/vsg/maths/quat.h @@ -93,7 +93,13 @@ namespace vsg void set(value_type angle_radians, const t_vec3& axis) { - const value_type epsilon = 1e-7; + //@todo epsilon might have to be made value_type dependent + static constexpr value_type epsilon = static_cast(1e-7); + // avoid implicit conversions. + // a performance optimization for the sin / cos calls below which otherwise operate in double precision. + static constexpr value_type one = static_cast(1.0); + static constexpr value_type half = static_cast(0.5); + value_type len = length(axis); if (len < epsilon) { @@ -102,9 +108,9 @@ namespace vsg return; } - value_type inversenorm = 1.0 / len; - value_type coshalfangle = cos(0.5 * angle_radians); - value_type sinhalfangle = sin(0.5 * angle_radians); + value_type inversenorm = one / len; + value_type coshalfangle = cos(half * angle_radians); + value_type sinhalfangle = sin(half * angle_radians); x = axis.x * sinhalfangle * inversenorm; y = axis.y * sinhalfangle * inversenorm; @@ -114,7 +120,12 @@ namespace vsg void set(const t_vec3& from, const t_vec3& to) { - const value_type epsilon = 1e-7; + //@todo epsilon might have to be made value_type dependent + static constexpr value_type epsilon = static_cast(1e-7); + // avoid implicit conversions. + // a performance optimization for the sin / cos calls below which otherwise operate in double precision. + static constexpr value_type one = static_cast(1.0); + static constexpr value_type half = static_cast(0.5); value_type dot_pd = vsg::dot(from, to); value_type div = std::sqrt(length2(from) * length2(to)); @@ -132,9 +143,9 @@ namespace vsg double angle_radians = acos(dot_pd / div); - value_type inversenorm = 1.0 / len; - value_type coshalfangle = cos(0.5 * angle_radians); - value_type sinhalfangle = sin(0.5 * angle_radians); + value_type inversenorm = one / len; + value_type coshalfangle = cos(half * angle_radians); + value_type sinhalfangle = sin(half * angle_radians); x = axis.x * sinhalfangle * inversenorm; y = axis.y * sinhalfangle * inversenorm; @@ -142,7 +153,11 @@ namespace vsg w = coshalfangle; } - explicit operator bool() const noexcept { return value[0] != 0.0 || value[1] != 0.0 || value[2] != 0.0 || value[3] != 0.0; } + explicit operator bool() const noexcept + { + static constexpr value_type zero = static_cast(1.0); + return value[0] != zero || value[1] != zero || value[2] != zero || value[3] != zero; + } }; using quat = t_quat; /// float quaternion @@ -275,7 +290,7 @@ namespace vsg T one(1.0); T cosomega = dot(from, to); - if (cosomega < 0.0) + if (cosomega < static_cast(0.0)) { cosomega = -cosomega; to.x = -to.x; From e7aeb99ee54e06a736d2365c6859255f8fa7d160 Mon Sep 17 00:00:00 2001 From: Carsten Meyer Date: Wed, 23 Jul 2025 21:00:33 +0200 Subject: [PATCH 2/2] Fixed a wrong constant in previous commit (ec45d1314cc8830147b47e436192d314d100affc) --- include/vsg/maths/quat.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/vsg/maths/quat.h b/include/vsg/maths/quat.h index d49cde6642..894b1ec0e9 100644 --- a/include/vsg/maths/quat.h +++ b/include/vsg/maths/quat.h @@ -155,7 +155,7 @@ namespace vsg explicit operator bool() const noexcept { - static constexpr value_type zero = static_cast(1.0); + static constexpr value_type zero = static_cast(0.0); return value[0] != zero || value[1] != zero || value[2] != zero || value[3] != zero; } };