Improved Ray class
Former-commit-id: 97a9a50440476e962cc850a09859b0784976c242
This commit is contained in:
parent
2479588811
commit
3dac383486
|
|
@ -1,4 +1,4 @@
|
|||
// Copyright (C) 2014 Rémi Bèges - Jérôme Leclercq
|
||||
// Copyright (C) 2014 Gawaboumga (https://github.com/Gawaboumga) - 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
|
||||
|
||||
|
|
@ -10,51 +10,57 @@
|
|||
#include <Nazara/Core/String.hpp>
|
||||
#include <Nazara/Math/Box.hpp>
|
||||
#include <Nazara/Math/Frustum.hpp>
|
||||
#include <Nazara/Math/Matrix4.hpp>
|
||||
#include <Nazara/Math/OrientedBox.hpp>
|
||||
#include <Nazara/Math/Plane.hpp>
|
||||
#include <Nazara/Math/Sphere.hpp>
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
|
||||
template<typename T> class NzRay
|
||||
template<typename T>
|
||||
class NzRay
|
||||
{
|
||||
public:
|
||||
NzRay() = default;
|
||||
NzRay(T X, T Y, T Z, T directionX, T directionY, T directionZ);
|
||||
NzRay(const T origin[3], const T direction[3]);
|
||||
NzRay(const NzVector3<T>& origin, const NzVector3<T>& direction);
|
||||
NzRay(const NzPlane<T>& planeOne, const NzPlane<T>& planeTwo);
|
||||
template<typename U> explicit NzRay(const NzVector3<U>& origin, const NzVector3<U>& direction);
|
||||
NzRay(const NzVector3<T>& origin, const NzVector3<T>& direction);
|
||||
template<typename U> explicit NzRay(const NzRay<U>& ray);
|
||||
template<typename U> explicit NzRay(const NzVector3<U>& origin, const NzVector3<U>& direction);
|
||||
NzRay(const NzRay<T>& ray) = default;
|
||||
~NzRay() = default;
|
||||
|
||||
NzVector3<T> GetClosestPoint(const NzVector3<T>& point) const;
|
||||
T ClosestPoint(const NzVector3<T>& point) const;
|
||||
|
||||
NzVector3<T> GetPoint(T lambda) const;
|
||||
|
||||
bool Intersect(const NzBox<T>& box, NzVector3<T> * hitPoint = nullptr, NzVector3<T> * hitSecondPoint = nullptr) const;
|
||||
bool Intersect(const NzOrientedBox<T>& orientedBox, NzVector3<T> * hitPoint = nullptr, NzVector3<T> * hitSecondPoint = nullptr) const;
|
||||
bool Intersect(const NzPlane<T>& plane, NzVector3<T> * hitPoint = nullptr) const;
|
||||
bool Intersect(const NzSphere<T>& sphere, NzVector3<T> * hitPoint = nullptr, NzVector3<T> * hitSecondPoint = nullptr) const;
|
||||
//bool Intersect(const NzBoundingVolume<T>& volume, T* closestHit = nullptr, T* farthestHit = nullptr) const;
|
||||
bool Intersect(const NzBox<T>& box, T* closestHit = nullptr, T* farthestHit = nullptr) const;
|
||||
bool Intersect(const NzBox<T>& box, const NzMatrix4<T>& transform, T* closestHit = nullptr, T* farthestHit = nullptr) const;
|
||||
//bool Intersect(const NzOrientedBox<T>& orientedBox, T* closestHit = nullptr, T* farthestHit = nullptr) const;
|
||||
bool Intersect(const NzPlane<T>& plane, T* hit = nullptr) const;
|
||||
bool Intersect(const NzSphere<T>& sphere, T* closestHit = nullptr, T* farthestHit = nullptr) const;
|
||||
|
||||
NzVector3<T> operator*(T lambda) const;
|
||||
NzRay& MakeAxisX();
|
||||
NzRay& MakeAxisY();
|
||||
NzRay& MakeAxisZ();
|
||||
|
||||
NzRay& Set(T X, T Y, T Z, T directionX, T directionY, T directionZ);
|
||||
NzRay& Set(const T origin[3], const T direction[3]);
|
||||
NzRay& Set(const NzVector3<T>& origin, const NzVector3<T>& direction);
|
||||
NzRay& Set(const NzPlane<T>& planeOne, const NzPlane<T>& planeTwo);
|
||||
template<typename U> NzRay& Set(const NzVector3<U>& origin, const NzVector3<U>& direction);
|
||||
NzRay& Set(const NzRay& ray);
|
||||
NzRay& Set(const NzVector3<T>& origin, const NzVector3<T>& direction);
|
||||
template<typename U> NzRay& Set(const NzRay<U>& ray);
|
||||
NzRay& Set(const NzRay& ray);
|
||||
|
||||
NzRay& SetDirection(const NzVector3<T>& direction);
|
||||
NzRay& SetOrigin(const NzVector3<T>& origin);
|
||||
template<typename U> NzRay& Set(const NzVector3<U>& origin, const NzVector3<U>& direction);
|
||||
|
||||
NzString ToString() const;
|
||||
|
||||
NzVector3<T> operator*(T lambda) const;
|
||||
|
||||
static NzRay AxisX();
|
||||
static NzRay AxisY();
|
||||
static NzRay AxisZ();
|
||||
static NzRay Lerp(const NzRay& from, const NzRay& to, T interpolation);
|
||||
static NzRay UnitX();
|
||||
static NzRay UnitY();
|
||||
static NzRay UnitZ();
|
||||
|
||||
NzVector3<T> direction, origin;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
// Copyright (C) 2014 Jérôme Leclercq
|
||||
// Copyright (C) 2014 Gawaboumga (https://github.com/Gawaboumga) - 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
|
||||
|
||||
#include <Nazara/Core/StringStream.hpp>
|
||||
#include <limits>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
#define F(a) static_cast<T>(a)
|
||||
|
|
@ -10,30 +11,23 @@
|
|||
template<typename T>
|
||||
NzRay<T>::NzRay(T X, T Y, T Z, T DirectionX, T DirectionY, T DirectionZ)
|
||||
{
|
||||
Set(X, Y, Z, DirectionX, DirectionY, DirectionZ);
|
||||
Set(X, Y, Z, DirectionX, DirectionY, DirectionZ);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>::NzRay(const T Origin[3], const T Direction[3])
|
||||
{
|
||||
Set(Origin, Direction);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>::NzRay(const NzVector3<T>& Origin, const NzVector3<T>& Direction)
|
||||
{
|
||||
Set(Origin, Direction);
|
||||
Set(Origin, Direction);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>::NzRay(const NzPlane<T>& planeOne, const NzPlane<T>& planeTwo)
|
||||
{
|
||||
Set(planeOne, planeTwo);
|
||||
Set(planeOne, planeTwo);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename U>
|
||||
NzRay<T>::NzRay(const NzVector3<U>& Origin, const NzVector3<U>& Direction)
|
||||
NzRay<T>::NzRay(const NzVector3<T>& Origin, const NzVector3<T>& Direction)
|
||||
{
|
||||
Set(Origin, Direction);
|
||||
}
|
||||
|
|
@ -46,219 +40,294 @@ NzRay<T>::NzRay(const NzRay<U>& ray)
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
NzVector3<T> NzRay<T>::GetClosestPoint(const NzVector3<T>& point) const
|
||||
template<typename U>
|
||||
NzRay<T>::NzRay(const NzVector3<U>& Origin, const NzVector3<U>& Direction)
|
||||
{
|
||||
NzVector3<T> delta = point - origin;
|
||||
T vsq = direction.GetSquaredLength();
|
||||
T proj = delta.DotProduct(direction);
|
||||
Set(Origin, Direction);
|
||||
}
|
||||
|
||||
return GetPoint(proj/vsq);
|
||||
template<typename T>
|
||||
T NzRay<T>::ClosestPoint(const NzVector3<T>& point) const
|
||||
{
|
||||
NzVector3<T> delta = point - origin;
|
||||
T vsq = direction.GetSquaredLength();
|
||||
T proj = delta.DotProduct(direction);
|
||||
|
||||
return proj/vsq;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzVector3<T> NzRay<T>::GetPoint(T lambda) const
|
||||
{
|
||||
return NzVector3<T>(origin + direction * lambda);
|
||||
return origin + lambda*direction;
|
||||
}
|
||||
|
||||
/*
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzBox<T>& box, NzVector3<T> * hitPoint, NzVector3<T> * hitSecondPoint) const
|
||||
bool NzRay<T>::Intersect(const NzBoundingVolume<T>& volume, T* closestHit, T* farthestHit) const
|
||||
{
|
||||
// Slab method
|
||||
|
||||
#if NAZARA_MATH_SAFE
|
||||
if (NzNumberEquals(direction.x, F(0.0)) || NzNumberEquals(direction.y, F(0.0)) || NzNumberEquals(direction.z, F(0.0)))
|
||||
switch (volume.extend)
|
||||
{
|
||||
NazaraWarning("Division by zero !"); // The algorithm is still correct.
|
||||
case nzExtend_Finite:
|
||||
{
|
||||
if (Intersect(volume.aabb))
|
||||
return Intersect(volume.obb, closestHit, farthestHit);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
case nzExtend_Infinite:
|
||||
{
|
||||
if (closestHit)
|
||||
*closestHit = F(0.0);
|
||||
|
||||
if (farthestHit)
|
||||
*farthestHit = std::numeric_limits<T>::infinity();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
case nzExtend_Null:
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
T tx1 = (box.x - origin.x) / direction.x;
|
||||
T tx2 = (box.x + box.width - origin.x) / direction.x;
|
||||
NazaraError("Invalid extend type (0x" + NzString::Number(volume.extend, 16) + ')');
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzBox<T>& box, T* closestHit, T* farthestHit) const
|
||||
{
|
||||
// http://www.gamedev.net/topic/429443-obb-ray-and-obb-plane-intersection/
|
||||
T tfirst = F(0.0);
|
||||
T tlast = std::numeric_limits<T>::infinity();
|
||||
|
||||
T tmin = std::min(tx1, tx2);
|
||||
T tmax = std::max(tx1, tx2);
|
||||
NzVector3<T> boxMin = box.GetMinimum();
|
||||
NzVector3<T> boxMax = box.GetMaximum();
|
||||
|
||||
T ty1 = (box.y - origin.y) / direction.y;
|
||||
T ty2 = (box.y + box.height - origin.y) / direction.y;
|
||||
for (unsigned int i = 0; i < 3; ++i)
|
||||
{
|
||||
T dir = direction[i];
|
||||
T ori = origin[i];
|
||||
T max = boxMax[i];
|
||||
T min = boxMin[i];
|
||||
|
||||
tmin = std::max(tmin, std::min(ty1, ty2));
|
||||
tmax = std::min(tmax, std::max(ty1, ty2));
|
||||
if (NzNumberEquals(dir, F(0.0)))
|
||||
{
|
||||
if (ori < max && ori > min)
|
||||
continue;
|
||||
|
||||
T tz1 = (box.z - origin.z) / direction.z;
|
||||
T tz2 = (box.z + box.depth - origin.z) / direction.z;
|
||||
return false;
|
||||
}
|
||||
|
||||
tmin = std::max(tmin, std::min(tz1, tz2));
|
||||
tmax = std::min(tmax, std::max(tz1, tz2));
|
||||
T tmin = (min - ori) / dir;
|
||||
T tmax = (max - ori) / dir;
|
||||
if (tmin > tmax)
|
||||
std::swap(tmin, tmax);
|
||||
|
||||
if (hitPoint)
|
||||
hitPoint->Set(GetPoint(tmin));
|
||||
if (hitSecondPoint)
|
||||
hitSecondPoint->Set(GetPoint(tmax));
|
||||
if (tmax < tfirst || tmin > tlast)
|
||||
return false;
|
||||
|
||||
return tmax >= std::max(F(0.0), tmin) && tmin < INFINITY;
|
||||
tfirst = std::max(tfirst, tmin);
|
||||
tlast = std::min(tlast, tmax);
|
||||
}
|
||||
|
||||
if (closestHit)
|
||||
*closestHit = tfirst;
|
||||
|
||||
if (farthestHit)
|
||||
*farthestHit = tlast;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzOrientedBox<T>& orientedBox, NzVector3<T> * hitPoint, NzVector3<T> * hitSecondPoint) const
|
||||
bool NzRay<T>::Intersect(const NzBox<T>& box, const NzMatrix4<T>& transform, T* closestHit, T* farthestHit) const
|
||||
{
|
||||
// http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-custom-ray-obb-function/
|
||||
// Intersection method from Real-Time Rendering and Essential Mathematics for Games
|
||||
T tMin = F(0.0);
|
||||
T tMax = std::numeric_limits<T>::infinity();
|
||||
|
||||
NzVector3<T> boxMin = box.GetMinimum();
|
||||
NzVector3<T> boxMax = box.GetMaximum();
|
||||
NzVector3<T> delta = transform.GetTranslation() - origin;
|
||||
|
||||
// Test intersection with the 2 planes perpendicular to the OBB's X axis
|
||||
for (unsigned int i = 0; i < 3; ++i)
|
||||
{
|
||||
NzVector3<T> axis(transform(0, i), transform(1, i), transform(2, i));
|
||||
T e = axis.DotProduct(delta);
|
||||
T f = direction.DotProduct(axis);
|
||||
|
||||
if (!NzNumberEquals(f, F(0.0)))
|
||||
{
|
||||
T t1 = (e + boxMin[i]) / f; // Intersection with the "left" plane
|
||||
T t2 = (e + boxMax[i]) / f; // Intersection with the "right" plane
|
||||
// t1 and t2 now contain distances betwen ray origin and ray-plane intersections
|
||||
|
||||
// We want t1 to represent the nearest intersection,
|
||||
// so if it's not the case, invert t1 and t2
|
||||
if (t1 > t2)
|
||||
std::swap(t1, t2);
|
||||
|
||||
// tMax is the nearest "far" intersection (amongst the X,Y and Z planes pairs)
|
||||
if (t2 < tMax)
|
||||
tMax = t2;
|
||||
|
||||
// tMin is the farthest "near" intersection (amongst the X,Y and Z planes pairs)
|
||||
if (t1 > tMin)
|
||||
tMin = t1;
|
||||
|
||||
// And here's the trick :
|
||||
// If "far" is closer than "near", then there is NO intersection.
|
||||
if (tMax < tMin)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
// Rare case : the ray is almost parallel to the planes, so they don't have any "intersection"
|
||||
if (-e + boxMin[i] > F(0.0) || -e + boxMax[i] < F(0.0))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (closestHit)
|
||||
*closestHit = tMin;
|
||||
|
||||
if (farthestHit)
|
||||
*farthestHit = tMax;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
///FIXME: Le test ci-dessous est beaucoup trop approximatif pour être vraiment utile
|
||||
/// Mais le vrai problème vient certainement des OrientedBox en elles-mêmes, peut-être faut-il envisager de les refaire ?
|
||||
/*
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzOrientedBox<T>& orientedBox, T* closestHit, T* farthestHit) const
|
||||
{
|
||||
NzVector3<T> width = (orientedBox.GetCorner(nzCorner_NearLeftBottom) - orientedBox.GetCorner(nzCorner_FarLeftBottom)).Normalize();
|
||||
NzVector3<T> height = (orientedBox.GetCorner(nzCorner_FarLeftTop) - orientedBox.GetCorner(nzCorner_FarLeftBottom)).Normalize();
|
||||
NzVector3<T> depth = (orientedBox.GetCorner(nzCorner_FarRightBottom) - orientedBox.GetCorner(nzCorner_FarLeftBottom)).Normalize();
|
||||
|
||||
// Construction of the inverse of the matrix who did the rotation -> orthogonal matrix.
|
||||
NzMatrix4<T> transformation(width.x, height.x, depth.x, F(0.0),
|
||||
width.y, height.y, depth.y, F(0.0),
|
||||
width.z, height.z, depth.z, F(0.0),
|
||||
F(0.0), F(0.0), F(0.0), F(1.0));
|
||||
// Construction de la matrice de transformation de l'OBB
|
||||
NzMatrix4<T> matrix(width.x, height.x, depth.x, F(0.0),
|
||||
width.y, height.y, depth.y, F(0.0),
|
||||
width.z, height.z, depth.z, F(0.0),
|
||||
F(0.0), F(0.0), F(0.0), F(1.0));
|
||||
matrix.Transpose();
|
||||
|
||||
// Reduction to aabb problem
|
||||
NzVector3<T> newOrigin = transformation.Transform(origin);
|
||||
NzVector3<T> newDirection = transformation.Transform(direction);
|
||||
// Test en tant qu'AABB avec une matrice de rotation
|
||||
return Intersect(orientedBox.localBox, matrix, closestHit, farthestHit);
|
||||
}
|
||||
*/
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzPlane<T>& plane, T* hit) const
|
||||
{
|
||||
T divisor = plane.normal.DotProduct(direction);
|
||||
if (NzNumberEquals(divisor, F(0.0)))
|
||||
return false; // perpendicular
|
||||
|
||||
NzVector3<T> tmp, tmp2;
|
||||
if (NzRay<T>(newOrigin, newDirection).Intersect(NzBox<T>(orientedBox.GetCorner(nzCorner_NearRightTop), orientedBox.GetCorner(nzCorner_FarLeftBottom)), &tmp, &tmp2))
|
||||
{
|
||||
if (hitPoint)
|
||||
{
|
||||
transformation.Transpose();
|
||||
hitPoint->Set(transformation.Transform(tmp));
|
||||
if (hitSecondPoint)
|
||||
hitSecondPoint->Set(transformation.Transform(tmp2));
|
||||
}
|
||||
T lambda = -(plane.normal.DotProduct(origin) - plane.distance) / divisor; // The plane is ax+by+cz=d
|
||||
if (lambda < F(0.0))
|
||||
return false; // Le plan est derrière le rayon
|
||||
|
||||
return true;
|
||||
}
|
||||
if (hit)
|
||||
*hit = lambda;
|
||||
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzPlane<T>& plane, NzVector3<T> * hitPoint) const
|
||||
bool NzRay<T>::Intersect(const NzSphere<T>& sphere, T* closestHit, T* farthestHit) const
|
||||
{
|
||||
T divisor = plane.normal.DotProduct(direction);
|
||||
NzVector3<T> sphereRay = sphere.GetPosition() - origin;
|
||||
T length = sphereRay.DotProduct(direction);
|
||||
|
||||
if (NzNumberEquals(divisor, F(0.0)))
|
||||
return false; // perpendicular
|
||||
if (length < F(0.0))
|
||||
return false; // ray is perpendicular to the vector origin - center
|
||||
|
||||
if (!hitPoint)
|
||||
return true;
|
||||
T squaredDistance = sphereRay.GetSquaredLength() - length*length;
|
||||
T squaredRadius = sphere.radius*sphere.radius;
|
||||
|
||||
T lambda = - (plane.normal.DotProduct(origin) - plane.distance) / divisor; // The plane is ax+by+cz=d
|
||||
hitPoint->Set(GetPoint(lambda));
|
||||
if (squaredDistance > squaredRadius)
|
||||
return false; // if the ray is further than the radius
|
||||
|
||||
return true;
|
||||
// Calcul des points d'intersection si besoin
|
||||
if (closestHit || farthestHit)
|
||||
{
|
||||
T deltaLambda = std::sqrt(squaredRadius - squaredDistance);
|
||||
|
||||
if (closestHit)
|
||||
*closestHit = length - deltaLambda;
|
||||
|
||||
if (farthestHit)
|
||||
*farthestHit = length + deltaLambda;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzSphere<T>& sphere, NzVector3<T> * hitPoint, NzVector3<T> * hitSecondPoint) const
|
||||
NzRay<T>& NzRay<T>::MakeAxisX()
|
||||
{
|
||||
NzVector3<T> distanceCenterOrigin = sphere.GetPosition() - origin;
|
||||
T length = distanceCenterOrigin.DotProduct(direction);
|
||||
|
||||
if (length < F(0.0))
|
||||
return false; // ray is perpendicular to the vector origin - center
|
||||
|
||||
T squaredDistance = distanceCenterOrigin.GetSquaredLength() - length * length;
|
||||
|
||||
T squaredRadius = sphere.GetRadius() * sphere.GetRadius();
|
||||
|
||||
if (squaredDistance > squaredRadius)
|
||||
return false; // if the ray is further than the radius
|
||||
|
||||
if (!hitPoint)
|
||||
return true;
|
||||
|
||||
T deltaLambda = std::sqrt(squaredRadius - squaredDistance);
|
||||
|
||||
if (hitPoint)
|
||||
hitPoint->Set(GetPoint(length - deltaLambda));
|
||||
if (hitSecondPoint)
|
||||
hitSecondPoint->Set(GetPoint(length + deltaLambda));
|
||||
|
||||
return true;
|
||||
return Set(NzVector3<T>::Zero(), NzVector3<T>::UnitX());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzVector3<T> NzRay<T>::operator*(T lambda) const
|
||||
NzRay<T>& NzRay<T>::MakeAxisY()
|
||||
{
|
||||
return GetPoint(lambda);
|
||||
return Set(NzVector3<T>::Zero(), NzVector3<T>::UnitY());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::MakeAxisZ()
|
||||
{
|
||||
return Set(NzVector3<T>::Zero(), NzVector3<T>::UnitZ());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::Set(T X, T Y, T Z, T directionX, T directionY, T directionZ)
|
||||
{
|
||||
direction = NzVector3<T>(directionX, directionY, directionZ);
|
||||
origin = NzVector3<T>(X, Y, Z);
|
||||
direction.Set(directionX, directionY, directionZ);
|
||||
origin.Set(X, Y, Z);
|
||||
|
||||
return *this;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::Set(const T Origin[3], const T Direction[3])
|
||||
{
|
||||
direction = NzVector3<T>(Direction);
|
||||
origin = NzVector3<T>(Origin);
|
||||
direction.Set(Direction);
|
||||
origin.Set(Origin);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::Set(const NzVector3<T>& Origin, const NzVector3<T>& Direction)
|
||||
{
|
||||
direction = Direction;
|
||||
origin = Origin;
|
||||
|
||||
return *this;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::Set(const NzPlane<T>& planeOne, const NzPlane<T>& planeTwo)
|
||||
{
|
||||
T termOne = planeOne.normal.GetLength();
|
||||
T termTwo = planeOne.normal.DotProduct(planeTwo.normal);
|
||||
T termFour = planeTwo.normal.GetLength();
|
||||
T det = termOne * termFour - termTwo * termTwo;
|
||||
T termOne = planeOne.normal.GetLength();
|
||||
T termTwo = planeOne.normal.DotProduct(planeTwo.normal);
|
||||
T termFour = planeTwo.normal.GetLength();
|
||||
T det = termOne * termFour - termTwo * termTwo;
|
||||
|
||||
#if NAZARA_MATH_SAFE
|
||||
if (NzNumberEquals(det, F(0.0)))
|
||||
{
|
||||
#if NAZARA_MATH_SAFE
|
||||
if (NzNumberEquals(det, F(0.0)))
|
||||
{
|
||||
NzString error("Planes are parallel.");
|
||||
|
||||
NzString error("Planes are parallel.");
|
||||
NazaraError(error);
|
||||
throw std::domain_error(error);
|
||||
}
|
||||
#endif
|
||||
|
||||
NazaraError(error);
|
||||
throw std::domain_error(error);
|
||||
}
|
||||
#endif
|
||||
T invdet = F(1.0) / det;
|
||||
T fc0 = (termFour * -planeOne.distance + termTwo * planeTwo.distance) * invdet;
|
||||
T fc1 = (termOne * -planeTwo.distance + termTwo * planeOne.distance) * invdet;
|
||||
|
||||
T invdet = F(1.0) / det;
|
||||
T fc0 = (termFour * -planeOne.distance + termTwo * planeTwo.distance) * invdet;
|
||||
T fc1 = (termOne * -planeTwo.distance + termTwo * planeOne.distance) * invdet;
|
||||
direction = planeOne.normal.CrossProduct(planeTwo.normal);
|
||||
origin = planeOne.normal * fc0 + planeTwo.normal * fc1;
|
||||
|
||||
direction = planeOne.normal.CrossProduct(planeTwo.normal);
|
||||
origin = planeOne.normal * fc0 + planeTwo.normal * fc1;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename U>
|
||||
NzRay<T>& NzRay<T>::Set(const NzVector3<U>& Origin, const NzVector3<U>& Direction)
|
||||
{
|
||||
direction = NzVector3<T>(Direction);
|
||||
origin = NzVector3<T>(Origin);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename U>
|
||||
NzRay<T>& NzRay<T>::Set(const NzRay<U>& ray)
|
||||
{
|
||||
direction = NzVector3<T>(ray.direction);
|
||||
origin = NzVector3<T>(ray.origin);
|
||||
|
||||
return *this;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -270,19 +339,32 @@ NzRay<T>& NzRay<T>::Set(const NzRay& ray)
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::SetDirection(const NzVector3<T>& Direction)
|
||||
NzRay<T>& NzRay<T>::Set(const NzVector3<T>& Origin, const NzVector3<T>& Direction)
|
||||
{
|
||||
direction = Direction;
|
||||
direction = Direction;
|
||||
origin = Origin;
|
||||
|
||||
return *this;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::SetOrigin(const NzVector3<T>& Origin)
|
||||
template<typename U>
|
||||
NzRay<T>& NzRay<T>::Set(const NzRay<U>& ray)
|
||||
{
|
||||
origin = Origin;
|
||||
direction.Set(ray.direction);
|
||||
origin.Set(ray.origin);
|
||||
|
||||
return *this;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename U>
|
||||
NzRay<T>& NzRay<T>::Set(const NzVector3<U>& Origin, const NzVector3<U>& Direction)
|
||||
{
|
||||
direction.Set(Direction);
|
||||
origin.Set(Origin);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -293,30 +375,45 @@ NzString NzRay<T>::ToString() const
|
|||
return ss << "Ray(origin: " << origin.ToString() << ", direction: " << direction.ToString() << ")";
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzVector3<T> NzRay<T>::operator*(T lambda) const
|
||||
{
|
||||
return GetPoint(lambda);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T> NzRay<T>::AxisX()
|
||||
{
|
||||
NzRay axis;
|
||||
axis.MakeAxisX();
|
||||
|
||||
return axis;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T> NzRay<T>::AxisY()
|
||||
{
|
||||
NzRay axis;
|
||||
axis.MakeAxisY();
|
||||
|
||||
return axis;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T> NzRay<T>::AxisZ()
|
||||
{
|
||||
NzRay axis;
|
||||
axis.MakeAxisZ();
|
||||
|
||||
return axis;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T> NzRay<T>::Lerp(const NzRay& from, const NzRay& to, T interpolation)
|
||||
{
|
||||
return NzRay<T>(from.origin.Lerp(to.origin, interpolation), from.direction.Lerp(to.direction, interpolation));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T> NzRay<T>::UnitX()
|
||||
{
|
||||
return NzRay(NzVector3<T>::Zero(), NzVector3<T>::UnitX());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T> NzRay<T>::UnitY()
|
||||
{
|
||||
return NzRay(NzVector3<T>::Zero(), NzVector3<T>::UnitY());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T> NzRay<T>::UnitZ()
|
||||
{
|
||||
return NzRay(NzVector3<T>::Zero(), NzVector3<T>::UnitZ());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::ostream& operator<<(std::ostream& out, const NzRay<T>& ray)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue