Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Ray.hpp
1 // Copyright (C) 2017 Gawaboumga (https://github.com/Gawaboumga) - Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Mathematics module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_RAY_HPP
8 #define NAZARA_RAY_HPP
9 
10 #include <Nazara/Core/String.hpp>
11 #include <Nazara/Math/Box.hpp>
12 #include <Nazara/Math/Frustum.hpp>
13 #include <Nazara/Math/Matrix4.hpp>
14 #include <Nazara/Math/OrientedBox.hpp>
15 #include <Nazara/Math/Plane.hpp>
16 #include <Nazara/Math/Sphere.hpp>
17 #include <Nazara/Math/Vector3.hpp>
18 
19 namespace Nz
20 {
21  struct SerializationContext;
22 
23  template<typename T>
24  class Ray
25  {
26  public:
27  Ray() = default;
28  Ray(T X, T Y, T Z, T directionX, T directionY, T directionZ);
29  Ray(const Vector3<T>& origin, const Vector3<T>& direction);
30  Ray(const T origin[3], const T direction[3]);
31  Ray(const Plane<T>& planeOne, const Plane<T>& planeTwo);
32  template<typename U> explicit Ray(const Ray<U>& ray);
33  template<typename U> explicit Ray(const Vector3<U>& origin, const Vector3<U>& direction);
34  Ray(const Ray<T>& ray) = default;
35  ~Ray() = default;
36 
37  T ClosestPoint(const Vector3<T>& point) const;
38 
39  Vector3<T> GetPoint(T lambda) const;
40 
41  bool Intersect(const BoundingVolume<T>& volume, T* closestHit = nullptr, T* furthestHit = nullptr) const;
42  bool Intersect(const Box<T>& box, T* closestHit = nullptr, T* furthestHit = nullptr) const;
43  bool Intersect(const Box<T>& box, const Matrix4<T>& transform, T* closestHit = nullptr, T* furthestHit = nullptr) const;
44  bool Intersect(const OrientedBox<T>& orientedBox, T* closestHit = nullptr, T* furthestHit = nullptr) const;
45  bool Intersect(const Plane<T>& plane, T* hit = nullptr) const;
46  bool Intersect(const Sphere<T>& sphere, T* closestHit = nullptr, T* furthestHit = nullptr) const;
47  bool Intersect(const Vector3<T>& firstPoint, const Vector3<T>& secondPoint, const Vector3<T>& thirdPoint, T* hit = nullptr) const;
48 
49  Ray& MakeAxisX();
50  Ray& MakeAxisY();
51  Ray& MakeAxisZ();
52 
53  Ray& Set(T X, T Y, T Z, T directionX, T directionY, T directionZ);
54  Ray& Set(const Vector3<T>& origin, const Vector3<T>& direction);
55  Ray& Set(const T origin[3], const T direction[3]);
56  Ray& Set(const Plane<T>& planeOne, const Plane<T>& planeTwo);
57  Ray& Set(const Ray& ray);
58  template<typename U> Ray& Set(const Ray<U>& ray);
59  template<typename U> Ray& Set(const Vector3<U>& origin, const Vector3<U>& direction);
60 
61  String ToString() const;
62 
63  Vector3<T> operator*(T lambda) const;
64  Ray& operator=(const Ray& other) = default;
65 
66  bool operator==(const Ray& ray) const;
67  bool operator!=(const Ray& ray) const;
68 
69  static Ray AxisX();
70  static Ray AxisY();
71  static Ray AxisZ();
72  static Ray Lerp(const Ray& from, const Ray& to, T interpolation);
73 
74  Vector3<T> direction, origin;
75  };
76 
77  typedef Ray<double> Rayd;
78  typedef Ray<float> Rayf;
79 
80  template<typename T> bool Serialize(SerializationContext& context, const Ray<T>& ray);
81  template<typename T> bool Unserialize(SerializationContext& context, Ray<T>* ray);
82 }
83 
84 template<typename T> std::ostream& operator<<(std::ostream& out, const Nz::Ray<T>& vec);
85 
86 #include <Nazara/Math/Ray.inl>
87 
88 #endif // NAZARA_RAY_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Math class that represents an oriented three dimensional box.
Definition: OrientedBox.hpp:20
static Ray AxisX()
Shorthand for the ray (0, 0, 0), (1, 0, 0)
Definition: Ray.inl:708
bool Unserialize(SerializationContext &context, bool *value)
Unserializes a boolean.
Definition: Algorithm.inl:279
T ClosestPoint(const Vector3< T > &point) const
Finds the closest point of the ray from point.
Definition: Ray.inl:116
Math class that represents a plane in 3D.
Definition: Plane.hpp:18
Math class that represents a sphere "S2" in a three dimensional euclidean space.
Definition: Sphere.hpp:20
Core class that represents a string.
Definition: String.hpp:22
static Ray AxisY()
Shorthand for the ray (0, 0, 0), (0, 1, 0)
Definition: Ray.inl:724
String ToString() const
Gives a string representation.
Definition: Ray.inl:652
bool Intersect(const BoundingVolume< T > &volume, T *closestHit=nullptr, T *furthestHit=nullptr) const
Checks whether or not this ray intersects with the BoundingVolume.
Definition: Ray.inl:154
Math class that represents a bounding volume, a combination of a box and an oriented box...
Definition: BoundingVolume.hpp:20
Vector3< T > GetPoint(T lambda) const
Gets the point along the ray for this parameter.
Definition: Ray.inl:133
bool operator!=(const Ray &ray) const
Compares the ray to other one.
Definition: Ray.inl:695
Structure containing a serialization/unserialization context states.
Definition: SerializationContext.hpp:18
static Ray Lerp(const Ray &from, const Ray &to, T interpolation)
Interpolates the ray to other one with a factor of interpolation.
Definition: Ray.inl:762
Ray & MakeAxisY()
Makes the ray with position (0, 0, 0) and direction (0, 1, 0)
Definition: Ray.inl:484
Ray & Set(T X, T Y, T Z, T directionX, T directionY, T directionZ)
Sets the components of the ray with position and direction.
Definition: Ray.inl:515
bool Serialize(SerializationContext &context, bool value)
Serializes a boolean.
Definition: Algorithm.inl:214
Ray & MakeAxisX()
Makes the ray with position (0, 0, 0) and direction (1, 0, 0)
Definition: Ray.inl:471
Vector3< T > operator*(T lambda) const
Multiplies the direction ray with the lambda to get the point along the ray for this parameter...
Definition: Ray.inl:669
Math class that represents a transformation of the four dimensional vector space with the notion of p...
Definition: Matrix4.hpp:26
Ray & MakeAxisZ()
Makes the ray with position (0, 0, 0) and direction (0, 0, 1)
Definition: Ray.inl:497
static Ray AxisZ()
Shorthand for the ray (0, 0, 0), (0, 0, 1)
Definition: Ray.inl:740
bool operator==(const Ray &ray) const
Compares the ray to other one.
Definition: Ray.inl:682
Math class that represents an element of the three dimensional vector space.
Definition: Matrix4.hpp:22
Math class that represents a ray or a straight line in 3D space.
Definition: Ray.hpp:24
Math class that represents a three dimensional box.
Definition: Box.hpp:22