Wrong place
Former-commit-id: 909750e9bca284c2a0096a51c782c1083b258cef
This commit is contained in:
parent
282bdf9864
commit
9d7dc63574
|
|
@ -37,6 +37,9 @@ class NzPlane
|
|||
|
||||
NzString ToString() const;
|
||||
|
||||
bool operator==(const NzPlane& plane) const;
|
||||
bool operator!=(const NzPlane& plane) const;
|
||||
|
||||
static NzPlane Lerp(const NzPlane& from, const NzPlane& to, T interpolation);
|
||||
static NzPlane XY();
|
||||
static NzPlane XZ();
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ NzPlane<T>::NzPlane(const NzPlane<U>& plane)
|
|||
template<typename T>
|
||||
T NzPlane<T>::Distance(const NzVector3<T>& point) const
|
||||
{
|
||||
return normal.DotProduct(point) + distance;
|
||||
return normal.DotProduct(point) - distance; // ax + by + cd - d = 0.
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -110,7 +110,7 @@ NzPlane<T>& NzPlane<T>::Set(const NzVector3<T>& point1, const NzVector3<T>& poin
|
|||
normal = edge1.CrossProduct(edge2);
|
||||
normal.Normalize();
|
||||
|
||||
distance = -normal.DotProduct(point3);
|
||||
distance = normal.DotProduct(point3);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
|
@ -133,6 +133,18 @@ NzString NzPlane<T>::ToString() const
|
|||
return ss << "Plane(Normal: " << normal.ToString() << "; Distance: " << distance << ')';
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzPlane<T>::operator==(const NzPlane& plane) const
|
||||
{
|
||||
return (normal == plane.normal && NzNumberEquals(distance, plane.distance)) || (normal == -plane.normal && NzNumberEquals(distance, -plane.distance));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzPlane<T>::operator!=(const NzPlane& plane) const
|
||||
{
|
||||
return !operator==(plane);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T> NzPlane<T>::Lerp(const NzPlane& from, const NzPlane& to, T interpolation)
|
||||
{
|
||||
|
|
@ -155,19 +167,19 @@ NzPlane<T> NzPlane<T>::Lerp(const NzPlane& from, const NzPlane& to, T interpolat
|
|||
template<typename T>
|
||||
NzPlane<T> NzPlane<T>::XY()
|
||||
{
|
||||
return NzPlane<T>(F(0.0), F(0.0), F(1.0), F(0.0));
|
||||
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));
|
||||
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));
|
||||
return NzPlane<T>(F(1.0), F(0.0), F(0.0), F(0.0));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
|
|||
|
|
@ -34,12 +34,12 @@ class NzRay
|
|||
|
||||
NzVector3<T> GetPoint(T lambda) 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 NzBoundingVolume<T>& volume, T* closestHit = nullptr, T* furthestHit = nullptr) const;
|
||||
bool Intersect(const NzBox<T>& box, T* closestHit = nullptr, T* furthestHit = nullptr) const;
|
||||
bool Intersect(const NzBox<T>& box, const NzMatrix4<T>& transform, T* closestHit = nullptr, T* furthestHit = nullptr) const;
|
||||
bool Intersect(const NzOrientedBox<T>& orientedBox, T* closestHit = nullptr, T* furthestHit = 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;
|
||||
bool Intersect(const NzSphere<T>& sphere, T* closestHit = nullptr, T* furthestHit = nullptr) const;
|
||||
|
||||
NzRay& MakeAxisX();
|
||||
NzRay& MakeAxisY();
|
||||
|
|
@ -57,6 +57,9 @@ class NzRay
|
|||
|
||||
NzVector3<T> operator*(T lambda) const;
|
||||
|
||||
bool operator==(const NzRay& ray) const;
|
||||
bool operator!=(const NzRay& ray) const;
|
||||
|
||||
static NzRay AxisX();
|
||||
static NzRay AxisY();
|
||||
static NzRay AxisZ();
|
||||
|
|
|
|||
|
|
@ -59,18 +59,18 @@ T NzRay<T>::ClosestPoint(const NzVector3<T>& point) const
|
|||
template<typename T>
|
||||
NzVector3<T> NzRay<T>::GetPoint(T lambda) const
|
||||
{
|
||||
return origin + lambda*direction;
|
||||
return origin + lambda * direction;
|
||||
}
|
||||
/*
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzBoundingVolume<T>& volume, T* closestHit, T* farthestHit) const
|
||||
bool NzRay<T>::Intersect(const NzBoundingVolume<T>& volume, T* closestHit, T* furthestHit) const
|
||||
{
|
||||
switch (volume.extend)
|
||||
{
|
||||
case nzExtend_Finite:
|
||||
{
|
||||
if (Intersect(volume.aabb))
|
||||
return Intersect(volume.obb, closestHit, farthestHit);
|
||||
return Intersect(volume.obb, closestHit, furthestHit);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
@ -80,8 +80,8 @@ bool NzRay<T>::Intersect(const NzBoundingVolume<T>& volume, T* closestHit, T* fa
|
|||
if (closestHit)
|
||||
*closestHit = F(0.0);
|
||||
|
||||
if (farthestHit)
|
||||
*farthestHit = std::numeric_limits<T>::infinity();
|
||||
if (furthestHit)
|
||||
*furthestHit = std::numeric_limits<T>::infinity();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
@ -93,9 +93,9 @@ bool NzRay<T>::Intersect(const NzBoundingVolume<T>& volume, T* closestHit, T* fa
|
|||
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
|
||||
bool NzRay<T>::Intersect(const NzBox<T>& box, T* closestHit, T* furthestHit) const
|
||||
{
|
||||
// http://www.gamedev.net/topic/429443-obb-ray-and-obb-plane-intersection/
|
||||
T tfirst = F(0.0);
|
||||
|
|
@ -134,14 +134,14 @@ bool NzRay<T>::Intersect(const NzBox<T>& box, T* closestHit, T* farthestHit) con
|
|||
if (closestHit)
|
||||
*closestHit = tfirst;
|
||||
|
||||
if (farthestHit)
|
||||
*farthestHit = tlast;
|
||||
if (furthestHit)
|
||||
*furthestHit = tlast;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzBox<T>& box, const NzMatrix4<T>& transform, T* closestHit, T* farthestHit) const
|
||||
bool NzRay<T>::Intersect(const NzBox<T>& box, const NzMatrix4<T>& transform, T* closestHit, T* furthestHit) 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
|
||||
|
|
@ -192,32 +192,39 @@ bool NzRay<T>::Intersect(const NzBox<T>& box, const NzMatrix4<T>& transform, T*
|
|||
if (closestHit)
|
||||
*closestHit = tMin;
|
||||
|
||||
if (farthestHit)
|
||||
*farthestHit = tMax;
|
||||
if (furthestHit)
|
||||
*furthestHit = 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
|
||||
bool NzRay<T>::Intersect(const NzOrientedBox<T>& orientedBox, T* closestHit, T* furthestHit) const
|
||||
{
|
||||
NzVector3<T> width = (orientedBox.GetCorner(nzBoxCorner_NearLeftBottom) - orientedBox.GetCorner(nzBoxCorner_FarLeftBottom)).Normalize();
|
||||
NzVector3<T> height = (orientedBox.GetCorner(nzBoxCorner_FarLeftTop) - orientedBox.GetCorner(nzBoxCorner_FarLeftBottom)).Normalize();
|
||||
NzVector3<T> depth = (orientedBox.GetCorner(nzBoxCorner_FarRightBottom) - orientedBox.GetCorner(nzBoxCorner_FarLeftBottom)).Normalize();
|
||||
NzVector3<T> corner = orientedBox.GetCorner(nzBoxCorner_FarLeftBottom);
|
||||
NzVector3<T> oppositeCorner = orientedBox.GetCorner(nzBoxCorner_NearRightTop);
|
||||
|
||||
NzVector3<T> width = (orientedBox.GetCorner(nzBoxCorner_NearLeftBottom) - corner);
|
||||
NzVector3<T> height = (orientedBox.GetCorner(nzBoxCorner_FarLeftTop) - corner);
|
||||
NzVector3<T> depth = (orientedBox.GetCorner(nzBoxCorner_FarRightBottom) - corner);
|
||||
|
||||
// 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),
|
||||
NzMatrix4<T> matrix(width.x, height.x, depth.x, corner.x,
|
||||
width.y, height.y, depth.y, corner.y,
|
||||
width.z, height.z, depth.z, corner.z,
|
||||
F(0.0), F(0.0), F(0.0), F(1.0));
|
||||
|
||||
// Test en tant qu'AABB avec une matrice de rotation
|
||||
return Intersect(orientedBox.localBox, matrix, closestHit, farthestHit);
|
||||
matrix.InverseAffine();
|
||||
|
||||
corner = matrix.Transform(corner);
|
||||
oppositeCorner = matrix.Transform(oppositeCorner);
|
||||
|
||||
NzBox<T> tmpBox(corner, oppositeCorner);
|
||||
NzRay<T> tmpRay(matrix.Transform(origin), matrix.Transform(direction));
|
||||
|
||||
return tmpRay.Intersect(tmpBox, closestHit, furthestHit);
|
||||
}
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzPlane<T>& plane, T* hit) const
|
||||
{
|
||||
|
|
@ -225,9 +232,9 @@ bool NzRay<T>::Intersect(const NzPlane<T>& plane, T* hit) const
|
|||
if (NzNumberEquals(divisor, F(0.0)))
|
||||
return false; // perpendicular
|
||||
|
||||
T lambda = -(plane.normal.DotProduct(origin) + plane.distance) / divisor; // The plane is ax+by+cz=d
|
||||
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 false; // The plane is 'behind' the ray.
|
||||
|
||||
if (hit)
|
||||
*hit = lambda;
|
||||
|
|
@ -236,7 +243,7 @@ bool NzRay<T>::Intersect(const NzPlane<T>& plane, T* hit) const
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzSphere<T>& sphere, T* closestHit, T* farthestHit) const
|
||||
bool NzRay<T>::Intersect(const NzSphere<T>& sphere, T* closestHit, T* furthestHit) const
|
||||
{
|
||||
NzVector3<T> sphereRay = sphere.GetPosition() - origin;
|
||||
T length = sphereRay.DotProduct(direction);
|
||||
|
|
@ -251,15 +258,15 @@ bool NzRay<T>::Intersect(const NzSphere<T>& sphere, T* closestHit, T* farthestHi
|
|||
return false; // if the ray is further than the radius
|
||||
|
||||
// Calcul des points d'intersection si besoin
|
||||
if (closestHit || farthestHit)
|
||||
if (closestHit || furthestHit)
|
||||
{
|
||||
T deltaLambda = std::sqrt(squaredRadius - squaredDistance);
|
||||
|
||||
if (closestHit)
|
||||
*closestHit = length - deltaLambda;
|
||||
|
||||
if (farthestHit)
|
||||
*farthestHit = length + deltaLambda;
|
||||
if (furthestHit)
|
||||
*furthestHit = length + deltaLambda;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
@ -380,6 +387,18 @@ NzVector3<T> NzRay<T>::operator*(T lambda) const
|
|||
return GetPoint(lambda);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::operator==(const NzRay& ray) const
|
||||
{
|
||||
return direction == ray.direction && origin == ray.origin;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::operator!=(const NzRay& ray) const
|
||||
{
|
||||
return !operator==(ray);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T> NzRay<T>::AxisX()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,60 +0,0 @@
|
|||
// Copyright (C) 2015 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_PLANE_HPP
|
||||
#define NAZARA_PLANE_HPP
|
||||
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <Nazara/Math/Vector3.hpp>
|
||||
|
||||
template<typename T>
|
||||
class NzPlane
|
||||
{
|
||||
public:
|
||||
NzPlane() = default;
|
||||
NzPlane(T normalX, T normalY, T normalZ, T Distance);
|
||||
NzPlane(const T plane[4]);
|
||||
NzPlane(const NzVector3<T>& Normal, T Distance);
|
||||
NzPlane(const NzVector3<T>& Normal, const NzVector3<T>& point);
|
||||
NzPlane(const NzVector3<T>& point1, const NzVector3<T>& point2, const NzVector3<T>& point3);
|
||||
template<typename U> explicit NzPlane(const NzPlane<U>& plane);
|
||||
NzPlane(const NzPlane& plane) = default;
|
||||
~NzPlane() = default;
|
||||
|
||||
T Distance(const NzVector3<T>& point) const;
|
||||
T Distance(T x, T y, T z) const;
|
||||
|
||||
NzPlane& Set(T normalX, T normalY, T normalZ, T Distance);
|
||||
NzPlane& Set(const T plane[4]);
|
||||
NzPlane& Set(const NzPlane& plane);
|
||||
NzPlane& Set(const NzVector3<T>& Normal, T Distance);
|
||||
NzPlane& Set(const NzVector3<T>& Normal, const NzVector3<T>& point);
|
||||
NzPlane& Set(const NzVector3<T>& point1, const NzVector3<T>& point2, const NzVector3<T>& point3);
|
||||
template<typename U> NzPlane& Set(const NzPlane<U>& plane);
|
||||
|
||||
NzString ToString() const;
|
||||
|
||||
bool operator==(const NzPlane& plane) const;
|
||||
bool operator!=(const NzPlane& plane) 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;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
std::ostream& operator<<(std::ostream& out, const NzPlane<T>& plane);
|
||||
|
||||
typedef NzPlane<double> NzPlaned;
|
||||
typedef NzPlane<float> NzPlanef;
|
||||
|
||||
#include <Nazara/Math/Plane.inl>
|
||||
|
||||
#endif // NAZARA_PLANE_HPP
|
||||
|
|
@ -1,193 +0,0 @@
|
|||
// Copyright (C) 2015 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 <Nazara/Math/Algorithm.hpp>
|
||||
#include <cstring>
|
||||
#include <Nazara/Core/Debug.hpp>
|
||||
|
||||
#define F(a) static_cast<T>(a)
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T>::NzPlane(T normalX, T normalY, T normalZ, T D)
|
||||
{
|
||||
Set(normalX, normalY, normalZ, D);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T>::NzPlane(const T plane[4])
|
||||
{
|
||||
Set(plane);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T>::NzPlane(const NzVector3<T>& Normal, T D)
|
||||
{
|
||||
Set(Normal, D);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T>::NzPlane(const NzVector3<T>& Normal, const NzVector3<T>& point)
|
||||
{
|
||||
Set(Normal, point);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T>::NzPlane(const NzVector3<T>& point1, const NzVector3<T>& point2, const NzVector3<T>& point3)
|
||||
{
|
||||
Set(point1, point2, point3);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename U>
|
||||
NzPlane<T>::NzPlane(const NzPlane<U>& plane)
|
||||
{
|
||||
Set(plane);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T NzPlane<T>::Distance(const NzVector3<T>& point) const
|
||||
{
|
||||
return normal.DotProduct(point) - distance; // ax + by + cd - d = 0.
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T NzPlane<T>::Distance(T x, T y, T z) const
|
||||
{
|
||||
return Distance(NzVector3<T>(x, y, z));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T>& NzPlane<T>::Set(T normalX, T normalY, T normalZ, T D)
|
||||
{
|
||||
distance = D;
|
||||
normal.Set(normalX, normalY, normalZ);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T>& NzPlane<T>::Set(const T plane[4])
|
||||
{
|
||||
normal.Set(plane[0], plane[1], plane[2]);
|
||||
distance = plane[3];
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T>& NzPlane<T>::Set(const NzPlane& plane)
|
||||
{
|
||||
std::memcpy(this, &plane, sizeof(NzPlane));
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T>& NzPlane<T>::Set(const NzVector3<T>& Normal, T D)
|
||||
{
|
||||
distance = D;
|
||||
normal = Normal;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T>& NzPlane<T>::Set(const NzVector3<T>& Normal, const NzVector3<T>& point)
|
||||
{
|
||||
normal = Normal;
|
||||
distance = -normal.DotProduct(point);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T>& NzPlane<T>::Set(const NzVector3<T>& point1, const NzVector3<T>& point2, const NzVector3<T>& point3)
|
||||
{
|
||||
NzVector3<T> edge1 = point2 - point1;
|
||||
NzVector3<T> edge2 = point3 - point1;
|
||||
normal = edge1.CrossProduct(edge2);
|
||||
normal.Normalize();
|
||||
|
||||
distance = normal.DotProduct(point3);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename U>
|
||||
NzPlane<T>& NzPlane<T>::Set(const NzPlane<U>& plane)
|
||||
{
|
||||
normal.Set(plane.normal);
|
||||
distance = F(plane.distance);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzString NzPlane<T>::ToString() const
|
||||
{
|
||||
NzStringStream ss;
|
||||
|
||||
return ss << "Plane(Normal: " << normal.ToString() << "; Distance: " << distance << ')';
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzPlane<T>::operator==(const NzPlane& plane) const
|
||||
{
|
||||
return (normal == plane.normal && NzNumberEquals(distance, plane.distance)) || (normal == -plane.normal && NzNumberEquals(distance, -plane.distance));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzPlane<T>::operator!=(const NzPlane& plane) const
|
||||
{
|
||||
return !operator==(plane);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzPlane<T> NzPlane<T>::Lerp(const NzPlane& from, const NzPlane& to, T interpolation)
|
||||
{
|
||||
#ifdef NAZARA_DEBUG
|
||||
if (interpolation < F(0.0) || interpolation > F(1.0))
|
||||
{
|
||||
NazaraError("Interpolation must be in range [0..1] (Got " + NzString::Number(interpolation) + ')');
|
||||
return NzPlane();
|
||||
}
|
||||
#endif
|
||||
|
||||
NzPlane plane;
|
||||
plane.distance = NzLerp(from.distance, to.distance, interpolation);
|
||||
plane.normal = NzVector3<T>::Lerp(from.normal, to.normal, interpolation);
|
||||
plane.normal.Normalize();
|
||||
|
||||
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)
|
||||
{
|
||||
return out << plane.ToString();
|
||||
}
|
||||
|
||||
#undef F
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
|
@ -1,78 +0,0 @@
|
|||
// Copyright (C) 2015 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_RAY_HPP
|
||||
#define NAZARA_RAY_HPP
|
||||
|
||||
#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
|
||||
{
|
||||
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 NzPlane<T>& planeOne, const NzPlane<T>& planeTwo);
|
||||
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;
|
||||
|
||||
T ClosestPoint(const NzVector3<T>& point) const;
|
||||
|
||||
NzVector3<T> GetPoint(T lambda) const;
|
||||
|
||||
bool Intersect(const NzBoundingVolume<T>& volume, T* closestHit = nullptr, T* furthestHit = nullptr) const;
|
||||
bool Intersect(const NzBox<T>& box, T* closestHit = nullptr, T* furthestHit = nullptr) const;
|
||||
bool Intersect(const NzBox<T>& box, const NzMatrix4<T>& transform, T* closestHit = nullptr, T* furthestHit = nullptr) const;
|
||||
bool Intersect(const NzOrientedBox<T>& orientedBox, T* closestHit = nullptr, T* furthestHit = nullptr) const;
|
||||
bool Intersect(const NzPlane<T>& plane, T* hit = nullptr) const;
|
||||
bool Intersect(const NzSphere<T>& sphere, T* closestHit = nullptr, T* furthestHit = nullptr) 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 NzPlane<T>& planeOne, const NzPlane<T>& planeTwo);
|
||||
NzRay& Set(const NzRay& ray);
|
||||
NzRay& Set(const NzVector3<T>& origin, const NzVector3<T>& direction);
|
||||
template<typename U> NzRay& Set(const NzRay<U>& ray);
|
||||
template<typename U> NzRay& Set(const NzVector3<U>& origin, const NzVector3<U>& direction);
|
||||
|
||||
NzString ToString() const;
|
||||
|
||||
NzVector3<T> operator*(T lambda) const;
|
||||
|
||||
bool operator==(const NzRay& ray) const;
|
||||
bool operator!=(const NzRay& ray) const;
|
||||
|
||||
static NzRay AxisX();
|
||||
static NzRay AxisY();
|
||||
static NzRay AxisZ();
|
||||
static NzRay Lerp(const NzRay& from, const NzRay& to, T interpolation);
|
||||
|
||||
NzVector3<T> direction, origin;
|
||||
};
|
||||
|
||||
template<typename T> std::ostream& operator<<(std::ostream& out, const NzRay<T>& vec);
|
||||
|
||||
typedef NzRay<double> NzRayd;
|
||||
typedef NzRay<float> NzRayf;
|
||||
|
||||
#include <Nazara/Math/Ray.inl>
|
||||
|
||||
#endif // NAZARA_RAY_HPP
|
||||
|
|
@ -1,443 +0,0 @@
|
|||
// Copyright (C) 2015 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)
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>::NzRay(const T Origin[3], const T Direction[3])
|
||||
{
|
||||
Set(Origin, Direction);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>::NzRay(const NzPlane<T>& planeOne, const NzPlane<T>& planeTwo)
|
||||
{
|
||||
Set(planeOne, planeTwo);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>::NzRay(const NzVector3<T>& Origin, const NzVector3<T>& Direction)
|
||||
{
|
||||
Set(Origin, Direction);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename U>
|
||||
NzRay<T>::NzRay(const NzRay<U>& ray)
|
||||
{
|
||||
Set(ray);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename U>
|
||||
NzRay<T>::NzRay(const NzVector3<U>& Origin, const NzVector3<U>& Direction)
|
||||
{
|
||||
Set(Origin, Direction);
|
||||
}
|
||||
|
||||
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 origin + lambda * direction;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzBoundingVolume<T>& volume, T* closestHit, T* furthestHit) const
|
||||
{
|
||||
switch (volume.extend)
|
||||
{
|
||||
case nzExtend_Finite:
|
||||
{
|
||||
if (Intersect(volume.aabb))
|
||||
return Intersect(volume.obb, closestHit, furthestHit);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
case nzExtend_Infinite:
|
||||
{
|
||||
if (closestHit)
|
||||
*closestHit = F(0.0);
|
||||
|
||||
if (furthestHit)
|
||||
*furthestHit = std::numeric_limits<T>::infinity();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
case nzExtend_Null:
|
||||
return false;
|
||||
}
|
||||
|
||||
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* furthestHit) 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();
|
||||
|
||||
NzVector3<T> boxMin = box.GetMinimum();
|
||||
NzVector3<T> boxMax = box.GetMaximum();
|
||||
|
||||
for (unsigned int i = 0; i < 3; ++i)
|
||||
{
|
||||
T dir = direction[i];
|
||||
T ori = origin[i];
|
||||
T max = boxMax[i];
|
||||
T min = boxMin[i];
|
||||
|
||||
if (NzNumberEquals(dir, F(0.0)))
|
||||
{
|
||||
if (ori < max && ori > min)
|
||||
continue;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
T tmin = (min - ori) / dir;
|
||||
T tmax = (max - ori) / dir;
|
||||
if (tmin > tmax)
|
||||
std::swap(tmin, tmax);
|
||||
|
||||
if (tmax < tfirst || tmin > tlast)
|
||||
return false;
|
||||
|
||||
tfirst = std::max(tfirst, tmin);
|
||||
tlast = std::min(tlast, tmax);
|
||||
}
|
||||
|
||||
if (closestHit)
|
||||
*closestHit = tfirst;
|
||||
|
||||
if (furthestHit)
|
||||
*furthestHit = tlast;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzBox<T>& box, const NzMatrix4<T>& transform, T* closestHit, T* furthestHit) 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 (furthestHit)
|
||||
*furthestHit = tMax;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzOrientedBox<T>& orientedBox, T* closestHit, T* furthestHit) const
|
||||
{
|
||||
NzVector3<T> corner = orientedBox.GetCorner(nzBoxCorner_FarLeftBottom);
|
||||
NzVector3<T> oppositeCorner = orientedBox.GetCorner(nzBoxCorner_NearRightTop);
|
||||
|
||||
NzVector3<T> width = (orientedBox.GetCorner(nzBoxCorner_NearLeftBottom) - corner);
|
||||
NzVector3<T> height = (orientedBox.GetCorner(nzBoxCorner_FarLeftTop) - corner);
|
||||
NzVector3<T> depth = (orientedBox.GetCorner(nzBoxCorner_FarRightBottom) - corner);
|
||||
|
||||
// Construction de la matrice de transformation de l'OBB
|
||||
NzMatrix4<T> matrix(width.x, height.x, depth.x, corner.x,
|
||||
width.y, height.y, depth.y, corner.y,
|
||||
width.z, height.z, depth.z, corner.z,
|
||||
F(0.0), F(0.0), F(0.0), F(1.0));
|
||||
|
||||
matrix.InverseAffine();
|
||||
|
||||
corner = matrix.Transform(corner);
|
||||
oppositeCorner = matrix.Transform(oppositeCorner);
|
||||
|
||||
NzBox<T> tmpBox(corner, oppositeCorner);
|
||||
NzRay<T> tmpRay(matrix.Transform(origin), matrix.Transform(direction));
|
||||
|
||||
return tmpRay.Intersect(tmpBox, closestHit, furthestHit);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
T lambda = -(plane.normal.DotProduct(origin) - plane.distance) / divisor; // The plane is ax + by + cz = d
|
||||
if (lambda < F(0.0))
|
||||
return false; // The plane is 'behind' the ray.
|
||||
|
||||
if (hit)
|
||||
*hit = lambda;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::Intersect(const NzSphere<T>& sphere, T* closestHit, T* furthestHit) const
|
||||
{
|
||||
NzVector3<T> sphereRay = sphere.GetPosition() - origin;
|
||||
T length = sphereRay.DotProduct(direction);
|
||||
|
||||
if (length < F(0.0))
|
||||
return false; // ray is perpendicular to the vector origin - center
|
||||
|
||||
T squaredDistance = sphereRay.GetSquaredLength() - length*length;
|
||||
T squaredRadius = sphere.radius*sphere.radius;
|
||||
|
||||
if (squaredDistance > squaredRadius)
|
||||
return false; // if the ray is further than the radius
|
||||
|
||||
// Calcul des points d'intersection si besoin
|
||||
if (closestHit || furthestHit)
|
||||
{
|
||||
T deltaLambda = std::sqrt(squaredRadius - squaredDistance);
|
||||
|
||||
if (closestHit)
|
||||
*closestHit = length - deltaLambda;
|
||||
|
||||
if (furthestHit)
|
||||
*furthestHit = length + deltaLambda;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::MakeAxisX()
|
||||
{
|
||||
return Set(NzVector3<T>::Zero(), NzVector3<T>::UnitX());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::MakeAxisY()
|
||||
{
|
||||
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.Set(directionX, directionY, directionZ);
|
||||
origin.Set(X, Y, Z);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::Set(const T Origin[3], const T Direction[3])
|
||||
{
|
||||
direction.Set(Direction);
|
||||
origin.Set(Origin);
|
||||
|
||||
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;
|
||||
|
||||
#if NAZARA_MATH_SAFE
|
||||
if (NzNumberEquals(det, F(0.0)))
|
||||
{
|
||||
NzString error("Planes are parallel.");
|
||||
|
||||
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;
|
||||
|
||||
direction = planeOne.normal.CrossProduct(planeTwo.normal);
|
||||
origin = planeOne.normal * fc0 + planeTwo.normal * fc1;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::Set(const NzRay& ray)
|
||||
{
|
||||
std::memcpy(this, &ray, sizeof(NzRay));
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
NzRay<T>& NzRay<T>::Set(const NzVector3<T>& Origin, const NzVector3<T>& Direction)
|
||||
{
|
||||
direction = Direction;
|
||||
origin = Origin;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<typename U>
|
||||
NzRay<T>& NzRay<T>::Set(const NzRay<U>& ray)
|
||||
{
|
||||
direction.Set(ray.direction);
|
||||
origin.Set(ray.origin);
|
||||
|
||||
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>
|
||||
NzString NzRay<T>::ToString() const
|
||||
{
|
||||
NzStringStream ss;
|
||||
|
||||
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>
|
||||
bool NzRay<T>::operator==(const NzRay& ray) const
|
||||
{
|
||||
return direction == ray.direction && origin == ray.origin;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool NzRay<T>::operator!=(const NzRay& ray) const
|
||||
{
|
||||
return !operator==(ray);
|
||||
}
|
||||
|
||||
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>
|
||||
std::ostream& operator<<(std::ostream& out, const NzRay<T>& ray)
|
||||
{
|
||||
return out << ray.ToString();
|
||||
}
|
||||
|
||||
#undef F
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
Loading…
Reference in New Issue