From bd5563862408db339a339f6d81d795f1bef1d63f Mon Sep 17 00:00:00 2001 From: Edgar Velazquez-Armendariz Date: Thu, 21 Feb 2013 02:20:07 -0500 Subject: [PATCH] Make sure the return type of TVectorN::length is a floating point value. For example, in TVector2 the length will almost never be an integer. If the return type of length were T the resoult would be implicitly truncated from float/double (the result of std::sqrt) to T. --- include/mitsuba/core/vector.h | 36 +++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/include/mitsuba/core/vector.h b/include/mitsuba/core/vector.h index df8a10e3..d259dc3f 100644 --- a/include/mitsuba/core/vector.h +++ b/include/mitsuba/core/vector.h @@ -214,6 +214,27 @@ template <> inline TVector1 &TVector1::operator/=(int s) { return *this; } +namespace detail { +// Helper structures to define "length" as a floating point value + +template struct VectorLength { + typedef T type; +}; + +template struct VectorLength { + typedef Float type; +}; + +template <> struct VectorLength { + typedef double type; +}; + +template <> struct VectorLength { + typedef double type; +}; + +} + /** * \headerfile mitsuba/core/vector.h mitsuba/mitsuba.h * \brief Parameterizable two-dimensional vector data structure @@ -222,6 +243,7 @@ template <> inline TVector1 &TVector1::operator/=(int s) { template struct TVector2 { typedef T Scalar; typedef TPoint2 PointType; + typedef typename detail::VectorLength::is_integer>::type LengthType; T x, y; @@ -338,8 +360,8 @@ template struct TVector2 { } /// Return the 2-norm of this vector - T length() const { - return std::sqrt(lengthSquared()); + LengthType length() const { + return static_cast (std::sqrt(lengthSquared())); } /// Return whether or not this vector is identically zero @@ -421,6 +443,7 @@ template <> inline TVector2 &TVector2::operator/=(int s) { template struct TVector3 { typedef T Scalar; typedef TPoint3 PointType; + typedef typename detail::VectorLength::is_integer>::type LengthType; T x, y, z; @@ -538,8 +561,8 @@ template struct TVector3 { } /// Return the 2-norm of this vector - T length() const { - return std::sqrt(lengthSquared()); + LengthType length() const { + return static_cast (std::sqrt(lengthSquared())); } /// Return whether or not this vector is identically zero @@ -632,6 +655,7 @@ template <> inline TVector3 &TVector3::operator/=(int s) { template struct TVector4 { typedef T Scalar; typedef TPoint4 PointType; + typedef typename detail::VectorLength::is_integer>::type LengthType; T x, y, z, w; @@ -751,8 +775,8 @@ template struct TVector4 { } /// Return the 2-norm of this vector - T length() const { - return std::sqrt(lengthSquared()); + LengthType length() const { + return static_cast (std::sqrt(lengthSquared())); } /// Return whether or not this vector is identically zero