Math/Sphere: Remove SquaredDistance method

This commit is contained in:
Lynix 2016-12-15 18:32:58 +01:00
parent 165b73acb3
commit f5f6c859d7
6 changed files with 27 additions and 68 deletions

View File

@ -163,7 +163,7 @@ namespace Nz
inline float ForwardRenderTechnique::ComputePointLightScore(const Spheref& object, const AbstractRenderQueue::PointLight& light) inline float ForwardRenderTechnique::ComputePointLightScore(const Spheref& object, const AbstractRenderQueue::PointLight& light)
{ {
///TODO: Compute a score depending on the light luminosity ///TODO: Compute a score depending on the light luminosity
return object.SquaredDistance(light.position); return object.GetPosition().SquaredDistance(light.position);
} }
/*! /*!
@ -177,7 +177,7 @@ namespace Nz
inline float ForwardRenderTechnique::ComputeSpotLightScore(const Spheref& object, const AbstractRenderQueue::SpotLight& light) inline float ForwardRenderTechnique::ComputeSpotLightScore(const Spheref& object, const AbstractRenderQueue::SpotLight& light)
{ {
///TODO: Compute a score depending on the light luminosity and spot direction ///TODO: Compute a score depending on the light luminosity and spot direction
return object.SquaredDistance(light.position); return object.GetPosition().SquaredDistance(light.position);
} }
/*! /*!
@ -199,29 +199,29 @@ namespace Nz
/*! /*!
* \brief Checks whether the point light is suitable for the computations * \brief Checks whether the point light is suitable for the computations
* \return true if light is enoughly close * \return true if light is close enough
* *
* \param object Sphere symbolising the object * \param object Sphere symbolizing the object
* \param light Light to compute * \param light Light to compute
*/ */
inline bool ForwardRenderTechnique::IsPointLightSuitable(const Spheref& object, const AbstractRenderQueue::PointLight& light) inline bool ForwardRenderTechnique::IsPointLightSuitable(const Spheref& object, const AbstractRenderQueue::PointLight& light)
{ {
// If the object is too far away from this point light, there is not way it could light it // If the object is too far away from this point light, there is not way it could light it
return object.SquaredDistance(light.position) <= light.radius * light.radius; return object.Contains(light.position);
} }
/*! /*!
* \brief Checks whether the spot light is suitable for the computations * \brief Checks whether the spot light is suitable for the computations
* \return true if light is enoughly close * \return true if light is close enough
* *
* \param object Sphere symbolising the object * \param object Sphere symbolizing the object
* \param light Light to compute * \param light Light to compute
*/ */
inline bool ForwardRenderTechnique::IsSpotLightSuitable(const Spheref& object, const AbstractRenderQueue::SpotLight& light) inline bool ForwardRenderTechnique::IsSpotLightSuitable(const Spheref& object, const AbstractRenderQueue::SpotLight& light)
{ {
///TODO: Exclude spot lights based on their direction and outer angle? ///TODO: Exclude spot lights based on their direction and outer angle?
return object.SquaredDistance(light.position) <= light.radius * light.radius; return object.Contains(light.position);
} }
} }

View File

@ -53,7 +53,7 @@ namespace Nz
unsigned int GetNumberLength(long double number, UInt8 precision = NAZARA_CORE_DECIMAL_DIGITS); unsigned int GetNumberLength(long double number, UInt8 precision = NAZARA_CORE_DECIMAL_DIGITS);
template<typename T> /*constexpr*/ unsigned int IntegralLog2(T number); template<typename T> /*constexpr*/ unsigned int IntegralLog2(T number);
template<typename T> /*constexpr*/ unsigned int IntegralLog2Pot(T pot); template<typename T> /*constexpr*/ unsigned int IntegralLog2Pot(T pot);
/*constexpr*/ unsigned int IntegralPow(unsigned int base, unsigned int exponent); template<typename T> /*constexpr*/ T IntegralPow(T base, unsigned int exponent);
template<typename T, typename T2> constexpr T Lerp(const T& from, const T& to, const T2& interpolation); template<typename T, typename T2> constexpr T Lerp(const T& from, const T& to, const T2& interpolation);
template<typename T> constexpr T MultiplyAdd(T x, T y, T z); template<typename T> constexpr T MultiplyAdd(T x, T y, T z);
template<typename T> /*constexpr*/ T NormalizeAngle(T angle); template<typename T> /*constexpr*/ T NormalizeAngle(T angle);

View File

@ -436,9 +436,10 @@ namespace Nz
*/ */
//TODO: Mark as constexpr when supported by all major compilers //TODO: Mark as constexpr when supported by all major compilers
/*constexpr*/ inline unsigned int IntegralPow(unsigned int base, unsigned int exponent) template<typename T>
/*constexpr*/ T IntegralPow(T base, unsigned int exponent)
{ {
unsigned int r = 1; T r = 1;
for (unsigned int i = 0; i < exponent; ++i) for (unsigned int i = 0; i < exponent; ++i)
r *= base; r *= base;

View File

@ -58,9 +58,6 @@ namespace Nz
Sphere& Set(const T sphere[4]); Sphere& Set(const T sphere[4]);
template<typename U> Sphere& Set(const Sphere<U>& sphere); template<typename U> Sphere& Set(const Sphere<U>& sphere);
T SquaredDistance(T X, T Y, T Z) const;
T SquaredDistance(const Vector3<T>& point) const;
String ToString() const; String ToString() const;
T& operator[](unsigned int i); T& operator[](unsigned int i);

View File

@ -94,7 +94,7 @@ namespace Nz
template<typename T> template<typename T>
bool Sphere<T>::Contains(T X, T Y, T Z) const bool Sphere<T>::Contains(T X, T Y, T Z) const
{ {
return SquaredDistance(X, Y, Z) <= radius * radius; return Contains(Vector3<T>(X, Y, Z));
} }
/*! /*!
@ -109,11 +109,8 @@ namespace Nz
template<typename T> template<typename T>
bool Sphere<T>::Contains(const Box<T>& box) const bool Sphere<T>::Contains(const Box<T>& box) const
{ {
if (box.GetMinimum().SquaredDistance(GetPosition()) <= radius * radius) if (Contains(box.GetMinimum()) && Contains(box.GetMaximum()))
{ return true;
if (box.GetMaximum().SquaredDistance(GetPosition()) <= radius * radius)
return true;
}
return false; return false;
} }
@ -128,7 +125,7 @@ namespace Nz
template<typename T> template<typename T>
bool Sphere<T>::Contains(const Vector3<T>& point) const bool Sphere<T>::Contains(const Vector3<T>& point) const
{ {
return Contains(point.x, point.y, point.z); return GetPosition().SquaredDistance(point) <= radius * radius;
} }
/*! /*!
@ -138,8 +135,6 @@ namespace Nz
* \param X X position of the point * \param X X position of the point
* \param Y Y position of the point * \param Y Y position of the point
* \param Z Z position of the point * \param Z Z position of the point
*
* \see SquaredDistance
*/ */
template<typename T> template<typename T>
@ -153,8 +148,6 @@ namespace Nz
* \return Distance to the point * \return Distance to the point
* *
* \param point Position of the point * \param point Position of the point
*
* \see SquaredDistance
*/ */
template<typename T> template<typename T>
@ -177,9 +170,7 @@ namespace Nz
template<typename T> template<typename T>
Sphere<T>& Sphere<T>::ExtendTo(T X, T Y, T Z) Sphere<T>& Sphere<T>::ExtendTo(T X, T Y, T Z)
{ {
T distance = SquaredDistance(X, Y, Z); radius = std::max(radius, Distance(X, Y, Z));
if (distance > radius*radius)
radius = std::sqrt(distance);
return *this; return *this;
} }
@ -304,7 +295,7 @@ namespace Nz
template<typename T> template<typename T>
bool Sphere<T>::Intersect(const Sphere& sphere) const bool Sphere<T>::Intersect(const Sphere& sphere) const
{ {
return SquaredDistance(sphere.x, sphere.y, sphere.z) <= sphere.radius * sphere.radius; return GetPosition().SquaredDistance(sphere.x, sphere.y, sphere.z) <= IntegralPow(radius + sphere.radius, 2);
} }
/*! /*!
@ -458,36 +449,6 @@ namespace Nz
return *this; return *this;
} }
/*!
* \brief Returns the squared distance from the sphere to the point (can be negative if the point is inside the sphere)
* \return Squared distance to the point
*
* \param X X position of the point
* \param Y Y position of the point
* \param Z Z position of the point
*
* \see Distance
*/
template<typename T>
T Sphere<T>::SquaredDistance(T X, T Y, T Z) const
{
return SquaredDistance({X, Y, Z});
}
/*!
* \brief Returns the squared distance from the sphere to the point (can be negative if the point is inside the sphere)
* \return Squared distance to the point
*
* \param point Position of the point
*
* \see Distance
*/
template<typename T>
T Sphere<T>::SquaredDistance(const Vector3<T>& point) const
{
return Vector3f::SquaredDistance(GetPosition(), point) - radius * radius;
}
/*! /*!
* \brief Gives a string representation * \brief Gives a string representation
* \return A string representation of the object: "Sphere(x, y, z; radius)" * \return A string representation of the object: "Sphere(x, y, z; radius)"

View File

@ -41,12 +41,10 @@ SCENARIO("Sphere", "[MATH][SPHERE]")
{ {
THEN("These results are expected because we don't take into account the border") THEN("These results are expected because we don't take into account the border")
{ {
REQUIRE(firstCenterAndUnit.Distance(Nz::Vector3f::UnitX() * 2.f) == Approx(1.f)); CHECK(firstCenterAndUnit.Distance(Nz::Vector3f::UnitX() * 2.f) == Approx(1.f));
REQUIRE(firstCenterAndUnit.SquaredDistance(Nz::Vector3f::UnitX() * 2.f) == Approx(1.f));
Nz::Spheref tmp(Nz::Vector3f::UnitX(), 1.f); Nz::Spheref tmp(Nz::Vector3f::UnitX(), 1.f);
REQUIRE(tmp.Distance(Nz::Vector3f::UnitX() * 4.f) == Approx(2.f)); CHECK(tmp.Distance(Nz::Vector3f::UnitX() * 4.f) == Approx(2.f));
REQUIRE(tmp.SquaredDistance(Nz::Vector3f::UnitX() * 4.f) == Approx(4.f));
} }
} }
@ -56,7 +54,7 @@ SCENARIO("Sphere", "[MATH][SPHERE]")
THEN("This is equal to sphere center and radius 0.75") THEN("This is equal to sphere center and radius 0.75")
{ {
REQUIRE(centerUnitBox.GetSquaredBoundingSphere() == Nz::Spheref(Nz::Vector3f::Zero(), 0.75f)); CHECK(centerUnitBox.GetSquaredBoundingSphere() == Nz::Spheref(Nz::Vector3f::Zero(), 0.75f));
} }
} }
@ -66,12 +64,12 @@ SCENARIO("Sphere", "[MATH][SPHERE]")
THEN("Positive vertex should be the same with centered and unit sphere") THEN("Positive vertex should be the same with centered and unit sphere")
{ {
REQUIRE(positiveVector == firstCenterAndUnit.GetPositiveVertex(positiveVector)); CHECK(positiveVector == firstCenterAndUnit.GetPositiveVertex(positiveVector));
} }
AND_THEN("Negative vertex should be the opposite") AND_THEN("Negative vertex should be the opposite")
{ {
REQUIRE(-positiveVector == firstCenterAndUnit.GetNegativeVertex(positiveVector)); CHECK(-positiveVector == firstCenterAndUnit.GetNegativeVertex(positiveVector));
} }
} }
@ -81,10 +79,12 @@ SCENARIO("Sphere", "[MATH][SPHERE]")
firstCenterAndUnit.ExtendTo(point); firstCenterAndUnit.ExtendTo(point);
REQUIRE(firstCenterAndUnit.radius == Approx(2.f));
THEN("Sphere must contain it and distance should be good") THEN("Sphere must contain it and distance should be good")
{ {
CHECK(firstCenterAndUnit.Contains(point)); CHECK(firstCenterAndUnit.Contains(point));
REQUIRE(firstCenterAndUnit.Distance(point) == 1.f); CHECK(firstCenterAndUnit.Distance(point) == Approx(1.f));
} }
} }