Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Sphere.hpp
1 // Copyright (C) 2017 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_SPHERE_HPP
8 #define NAZARA_SPHERE_HPP
9 
10 #include <Nazara/Core/String.hpp>
11 #include <Nazara/Math/Vector3.hpp>
12 
13 namespace Nz
14 {
15  struct SerializationContext;
16 
17  template<typename T> class Box;
18 
19  template<typename T>
20  class Sphere
21  {
22  public:
23  Sphere() = default;
24  Sphere(T X, T Y, T Z, T Radius);
25  //Sphere(const Circle<T>& circle);
26  Sphere(const Vector3<T>& center, T Radius);
27  Sphere(const T sphere[4]);
28  template<typename U> explicit Sphere(const Sphere<U>& sphere);
29  Sphere(const Sphere& sphere) = default;
30  ~Sphere() = default;
31 
32  bool Contains(T X, T Y, T Z) const;
33  bool Contains(const Box<T>& box) const;
34  bool Contains(const Vector3<T>& point) const;
35 
36  T Distance(T X, T Y, T Z) const;
37  T Distance(const Vector3<T>& point) const;
38 
39  Sphere& ExtendTo(T X, T Y, T Z);
40  Sphere& ExtendTo(const Vector3<T>& point);
41 
42  Vector3<T> GetNegativeVertex(const Vector3<T>& normal) const;
43  Vector3<T> GetPosition() const;
44  Vector3<T> GetPositiveVertex(const Vector3<T>& normal) const;
45 
46  bool Intersect(const Box<T>& box) const;
47  bool Intersect(const Sphere& sphere) const;
48 
49  bool IsValid() const;
50 
51  Sphere& MakeUnit();
52  Sphere& MakeZero();
53 
54  Sphere& Set(T X, T Y, T Z, T Radius);
55  //Sphere& Set(const Circle<T>& rect);
56  Sphere& Set(const Sphere& sphere);
57  Sphere& Set(const Vector3<T>& center, T Radius);
58  Sphere& Set(const T sphere[4]);
59  template<typename U> Sphere& Set(const Sphere<U>& sphere);
60 
61  String ToString() const;
62 
63  T& operator[](std::size_t i);
64  T operator[](std::size_t i) const;
65 
66  Sphere operator*(T scalar) const;
67  Sphere& operator=(const Sphere& other) = default;
68 
69  Sphere& operator*=(T scalar);
70 
71  bool operator==(const Sphere& sphere) const;
72  bool operator!=(const Sphere& sphere) const;
73 
74  static Sphere Lerp(const Sphere& from, const Sphere& to, T interpolation);
75  static Sphere Unit();
76  static Sphere Zero();
77 
78  T x, y, z, radius;
79  };
80 
81  typedef Sphere<double> Sphered;
82  typedef Sphere<float> Spheref;
83 
84  template<typename T> bool Serialize(SerializationContext& context, const Sphere<T>& sphere);
85  template<typename T> bool Unserialize(SerializationContext& context, Sphere<T>* sphere);
86 }
87 
88 template<typename T>
89 std::ostream& operator<<(std::ostream& out, const Nz::Sphere<T>& sphere);
90 
91 #include <Nazara/Math/Sphere.inl>
92 
93 #endif // NAZARA_SPHERE_HPP
bool IsValid() const
Checks whether this sphere is valid.
Definition: Sphere.inl:307
static Sphere Unit()
Shorthand for the sphere (0, 0, 0, 1)
Definition: Sphere.inl:560
Sphere operator*(T scalar) const
Multiplies the radius of the sphere with a scalar.
Definition: Sphere.inl:507
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
bool Unserialize(SerializationContext &context, bool *value)
Unserializes a boolean.
Definition: Algorithm.inl:279
T Distance(T X, T Y, T Z) const
Returns the distance from the sphere to the point (is negative when the point is inside the sphere) ...
Definition: Sphere.inl:141
Vector3< T > GetNegativeVertex(const Vector3< T > &normal) const
Computes the negative vertex of one direction.
Definition: Sphere.inl:203
static Sphere Lerp(const Sphere &from, const Sphere &to, T interpolation)
Interpolates the sphere to other one with a factor of interpolation.
Definition: Sphere.inl:583
Math class that represents a sphere "S2" in a three dimensional euclidean space.
Definition: Sphere.hpp:20
Sphere & MakeUnit()
Makes the sphere position (0, 0, 0) and radius 1.
Definition: Sphere.inl:320
Core class that represents a string.
Definition: String.hpp:22
Vector3< T > GetPositiveVertex(const Vector3< T > &normal) const
Computes the positive vertex of one direction.
Definition: Sphere.inl:232
bool Intersect(const Box< T > &box) const
Checks whether or not this sphere intersects a box.
Definition: Sphere.inl:248
Sphere & operator*=(T scalar)
Multiplies the radius of other sphere with a scalar.
Definition: Sphere.inl:520
bool Contains(T X, T Y, T Z) const
Tests whether the sphere contains the provided point inclusive of the edge of the sphere...
Definition: Sphere.inl:95
String ToString() const
Gives a string representation.
Definition: Sphere.inl:458
static Sphere Zero()
Shorthand for the sphere (0, 0, 0, 0)
Definition: Sphere.inl:610
Structure containing a serialization/unserialization context states.
Definition: SerializationContext.hpp:18
Sphere & MakeZero()
Makes the sphere position (0, 0, 0) and radius 0.
Definition: Sphere.inl:338
bool operator!=(const Sphere &sphere) const
Compares the sphere to other one.
Definition: Sphere.inl:547
T & operator[](std::size_t i)
Returns the ith element of the sphere.
Definition: Sphere.inl:475
bool Serialize(SerializationContext &context, bool value)
Serializes a boolean.
Definition: Algorithm.inl:214
Vector3< T > GetPosition() const
Gets a Vector3 of the position.
Definition: Sphere.inl:217
Sphere & ExtendTo(T X, T Y, T Z)
Extends the sphere to contain the point in the boundary.
Definition: Sphere.inl:171
bool operator==(const Sphere &sphere) const
Compares the sphere to other one.
Definition: Sphere.inl:533
Math class that represents an element of the three dimensional vector space.
Definition: Matrix4.hpp:22
Sphere & Set(T X, T Y, T Z, T Radius)
Sets the components of the sphere with center and radius.
Definition: Sphere.inl:359
Math class that represents a three dimensional box.
Definition: Box.hpp:22