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