Add of some mathematical functions

Plane: Getters and classic planes
Sphere: Getter
Vector2: Angle between two vectors
Vector3: Same in 3D

Former-commit-id: 07e6421def813743e5fd8248fe2e9503cebbb8a8
This commit is contained in:
Gawaboumga 2014-06-27 19:33:28 +02:00
parent f84897e47a
commit ca595bca20
8 changed files with 82 additions and 1 deletions

View File

@ -1,4 +1,4 @@
// Copyright (C) 2014 Jérôme Leclercq
// Copyright (C) 2014 Jérôme Leclercq
// This file is part of the "Nazara Engine - Mathematics module"
// For conditions of distribution and use, see copyright notice in Config.hpp
@ -27,6 +27,9 @@ class NzPlane
T Distance(const NzVector3<T>& point) const;
T Distance(T x, T y, T z) const;
NzVector3 GetNormal() const;
T GetDistance() const;
NzPlane& Set(T normalX, T normalY, T normalZ, T Distance);
NzPlane& Set(const T plane[4]);
NzPlane& Set(const NzPlane& plane);
@ -38,6 +41,9 @@ class NzPlane
NzString ToString() const;
static NzPlane Lerp(const NzPlane& from, const NzPlane& to, T interpolation);
static NzPlane XY();
static NzPlane XZ();
static NzPlane YZ();
NzVector3<T> normal;
T distance;

View File

@ -58,6 +58,18 @@ T NzPlane<T>::Distance(T x, T y, T z) const
return Distance(NzVector3<T>(x, y, z));
}
template<typename T>
NzVector3 NzPlane<T>::GetNormal() const
{
return normal;
}
template<typename T>
T NzPlane<T>::GetDistance() const
{
return distance;
}
template<typename T>
NzPlane<T>& NzPlane<T>::Set(T normalX, T normalY, T normalZ, T D)
{
@ -152,6 +164,24 @@ NzPlane<T> NzPlane<T>::Lerp(const NzPlane& from, const NzPlane& to, T interpolat
return plane;
}
template<typename T>
NzPlane<T> NzPlane<T>::XY()
{
return NzPlane<T>(F(0.0), F(0.0), F(1.0), F(0.0));
}
template<typename T>
NzPlane<T> NzPlane<T>::XZ()
{
return NzPlane<T>(F(0.0), F(1.0), F(0.0), F(0.0));
}
template<typename T>
NzPlane<T> NzPlane<T>::YZ()
{
return NzPlane<T>(F(1.0), F(0.0), F(0.0), F(0.0));
}
template<typename T>
std::ostream& operator<<(std::ostream& out, const NzPlane<T>& plane)
{

View File

@ -39,6 +39,7 @@ class NzSphere
NzVector3<T> GetNegativeVertex(const NzVector3<T>& normal) const;
NzVector3<T> GetPosition() const;
NzVector3<T> GetPositiveVertex(const NzVector3<T>& normal) const;
T GetRadius() const;
bool Intersect(const NzBox<T>& box) const;
bool Intersect(const NzSphere& sphere) const;

View File

@ -117,6 +117,13 @@ NzVector3<T> NzSphere<T>::GetPositiveVertex(const NzVector3<T>& normal) const
return pos;
}
template<typename T>
T NzSphere<T>::GetRadius() const
{
return radius;
}
template<typename T>
bool NzSphere<T>::Intersect(const NzBox<T>& box) const
{

View File

@ -23,6 +23,8 @@ class NzVector2
T AbsDotProduct(const NzVector2& vec) const;
T AngleBetween(const NzVector2& vec, bool toDegree = true) const;
T Distance(const NzVector2& vec) const;
float Distancef(const NzVector2& vec) const;

View File

@ -54,6 +54,15 @@ inline unsigned int NzVector2<unsigned int>::AbsDotProduct(const NzVector2<unsig
return std::labs(x * vec.x) + std::labs(y * vec.y);
}
template<typename T>
T NzVector2<T>::AngleBetween(const NzVector2& vec, bool toDegree) const
{
if (toDegree)
return NzRadianToDegree(std::atan2(vec.y, vec.x) - std::atan2(y, x));
else
return std::atan2(vec.y, vec.x) - std::atan2(y, x);
}
template<typename T>
T NzVector2<T>::Distance(const NzVector2& vec) const
{

View File

@ -24,6 +24,8 @@ template<typename T> class NzVector3
T AbsDotProduct(const NzVector3& vec) const;
T AngleBetween(const NzVector3& vec, bool toDegree = true) const;
NzVector3 CrossProduct(const NzVector3& vec) const;
T Distance(const NzVector3& vec) const;

View File

@ -60,6 +60,30 @@ inline unsigned int NzVector3<unsigned int>::AbsDotProduct(const NzVector3<unsig
return std::labs(x * vec.x) + std::labs(y * vec.y) + std::labs(z * vec.z);
}
template<typename T>
T NzVector3<T>::AngleBetween(const NzVector3& vec, bool toDegree) const
{
T alpha = DotProduct(vec);
T divisor = (GetLength() * vec.GetLength());
#if NAZARA_MATH_SAFE
if (NzNumberEquals(divisor, F(0.0)))
{
NzString error("Division by zero");
NazaraError(error);
throw std::domain_error(error);
}
#endif
alpha /= divisor;
if (toDegree)
return NzRadianToDegree(std::acos(NzClamp(alpha, F(-1.0), F(1.0))));
else
return std::acos(NzClamp(alpha, F(-1.0), F(1.0)));
}
template<typename T>
NzVector3<T> NzVector3<T>::CrossProduct(const NzVector3& vec) const
{