Rationalize QVector[234]D's length-handling

Use qHypot() instead of casting to and from double so much,
use qFuzzyCompare(_, 1.0f) rather than qFuzzyIsNull(_ - 1.0f).

Change-Id: I70f38fe2d9aefe1ceb15e1a370f181a7856911b7
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
This commit is contained in:
Edward Welbourne 2020-09-28 17:05:26 +02:00
parent 8c1532eeaa
commit 96ef1769c1

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2016 The Qt Company Ltd. ** Copyright (C) 2020 The Qt Company Ltd.
** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> ** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
@ -508,10 +508,7 @@ constexpr inline float QVector2D::operator[](int i) const
inline float QVector2D::length() const noexcept inline float QVector2D::length() const noexcept
{ {
// Need some extra precision if the length is very small. return qHypot(v[0], v[1]);
double len = double(v[0]) * double(v[0]) +
double(v[1]) * double(v[1]);
return float(std::sqrt(len));
} }
constexpr inline float QVector2D::lengthSquared() const noexcept constexpr inline float QVector2D::lengthSquared() const noexcept
@ -521,31 +518,19 @@ constexpr inline float QVector2D::lengthSquared() const noexcept
inline QVector2D QVector2D::normalized() const noexcept inline QVector2D QVector2D::normalized() const noexcept
{ {
// Need some extra precision if the length is very small. const float len = length();
double len = double(v[0]) * double(v[0]) + return qFuzzyIsNull(len - 1.0f) ? *this : qFuzzyIsNull(len) ? QVector2D()
double(v[1]) * double(v[1]); : QVector2D(v[0] / len, v[1] / len);
if (qFuzzyIsNull(len - 1.0)) {
return *this;
} else if (!qFuzzyIsNull(len)) {
double sqrtLen = std::sqrt(len);
return QVector2D(float(double(v[0]) / sqrtLen), float(double(v[1]) / sqrtLen));
} else {
return QVector2D();
}
} }
inline void QVector2D::normalize() noexcept inline void QVector2D::normalize() noexcept
{ {
// Need some extra precision if the length is very small. const float len = length();
double len = double(v[0]) * double(v[0]) + if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
double(v[1]) * double(v[1]);
if (qFuzzyIsNull(len - 1.0) || qFuzzyIsNull(len))
return; return;
len = std::sqrt(len); v[0] /= len;
v[1] /= len;
v[0] = float(double(v[0]) / len);
v[1] = float(double(v[1]) / len);
} }
inline float QVector2D::distanceToPoint(QVector2D point) const noexcept inline float QVector2D::distanceToPoint(QVector2D point) const noexcept
@ -691,45 +676,25 @@ constexpr inline float QVector3D::operator[](int i) const
inline float QVector3D::length() const noexcept inline float QVector3D::length() const noexcept
{ {
// Need some extra precision if the length is very small. return qHypot(v[0], v[1], v[2]);
double len = double(v[0]) * double(v[0]) +
double(v[1]) * double(v[1]) +
double(v[2]) * double(v[2]);
return float(std::sqrt(len));
} }
inline QVector3D QVector3D::normalized() const noexcept inline QVector3D QVector3D::normalized() const noexcept
{ {
// Need some extra precision if the length is very small. const float len = length();
double len = double(v[0]) * double(v[0]) + return qFuzzyIsNull(len - 1.0f) ? *this : qFuzzyIsNull(len) ? QVector3D()
double(v[1]) * double(v[1]) + : QVector3D(v[0] / len, v[1] / len, v[2] / len);
double(v[2]) * double(v[2]);
if (qFuzzyIsNull(len - 1.0)) {
return *this;
} else if (!qFuzzyIsNull(len)) {
double sqrtLen = std::sqrt(len);
return QVector3D(float(double(v[0]) / sqrtLen),
float(double(v[1]) / sqrtLen),
float(double(v[2]) / sqrtLen));
} else {
return QVector3D();
}
} }
inline void QVector3D::normalize() noexcept inline void QVector3D::normalize() noexcept
{ {
// Need some extra precision if the length is very small. const float len = length();
double len = double(v[0]) * double(v[0]) + if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
double(v[1]) * double(v[1]) +
double(v[2]) * double(v[2]);
if (qFuzzyIsNull(len - 1.0) || qFuzzyIsNull(len))
return; return;
len = std::sqrt(len); v[0] /= len;
v[1] /= len;
v[0] = float(double(v[0]) / len); v[2] /= len;
v[1] = float(double(v[1]) / len);
v[2] = float(double(v[2]) / len);
} }
constexpr inline float QVector3D::lengthSquared() const noexcept constexpr inline float QVector3D::lengthSquared() const noexcept
@ -917,12 +882,7 @@ constexpr inline float QVector4D::operator[](int i) const
inline float QVector4D::length() const noexcept inline float QVector4D::length() const noexcept
{ {
// Need some extra precision if the length is very small. return qHypot(v[0], v[1], v[2], v[3]);
double len = double(v[0]) * double(v[0]) +
double(v[1]) * double(v[1]) +
double(v[2]) * double(v[2]) +
double(v[3]) * double(v[3]);
return float(std::sqrt(len));
} }
constexpr inline float QVector4D::lengthSquared() const noexcept constexpr inline float QVector4D::lengthSquared() const noexcept
@ -932,40 +892,21 @@ constexpr inline float QVector4D::lengthSquared() const noexcept
inline QVector4D QVector4D::normalized() const noexcept inline QVector4D QVector4D::normalized() const noexcept
{ {
// Need some extra precision if the length is very small. const float len = length();
double len = double(v[0]) * double(v[0]) + return qFuzzyIsNull(len - 1.0f) ? *this : qFuzzyIsNull(len) ? QVector4D()
double(v[1]) * double(v[1]) + : QVector4D(v[0] / len, v[1] / len, v[2] / len, v[3] / len);
double(v[2]) * double(v[2]) +
double(v[3]) * double(v[3]);
if (qFuzzyIsNull(len - 1.0)) {
return *this;
} else if (!qFuzzyIsNull(len)) {
double sqrtLen = std::sqrt(len);
return QVector4D(float(double(v[0]) / sqrtLen),
float(double(v[1]) / sqrtLen),
float(double(v[2]) / sqrtLen),
float(double(v[3]) / sqrtLen));
} else {
return QVector4D();
}
} }
inline void QVector4D::normalize() noexcept inline void QVector4D::normalize() noexcept
{ {
// Need some extra precision if the length is very small. const float len = length();
double len = double(v[0]) * double(v[0]) + if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
double(v[1]) * double(v[1]) +
double(v[2]) * double(v[2]) +
double(v[3]) * double(v[3]);
if (qFuzzyIsNull(len - 1.0) || qFuzzyIsNull(len))
return; return;
len = std::sqrt(len); v[0] /= len;
v[1] /= len;
v[0] = float(double(v[0]) / len); v[2] /= len;
v[1] = float(double(v[1]) / len); v[3] /= len;
v[2] = float(double(v[2]) / len);
v[3] = float(double(v[3]) / len);
} }
constexpr inline QVector4D &QVector4D::operator+=(QVector4D vector) noexcept constexpr inline QVector4D &QVector4D::operator+=(QVector4D vector) noexcept