GlPortal Class Reference  0.1.1
Source code documentation of the free and open 3D action puzzle game.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Vector4f.hpp
Go to the documentation of this file.
1 #ifndef VECTOR4F_HPP
2 #define VECTOR4F_HPP
3 
4 #include <cmath>
5 #include <string>
6 
7 #include <serine/Serializable.hpp>
8 
9 class btVector4;
10 class btQuaternion;
11 
12 namespace radix {
13 
14 struct Vector2f;
15 struct Vector3f;
16 class Matrix3f;
17 class Matrix4f;
18 
22 struct Vector4f : public serine::Serializable {
23  union {
24  float x, r, s, yaw, heading, azimuth, tetha;
25  };
26  union {
27  float y, g, t, pitch, attitude, elevation, phi;
28  };
29  union {
30  float z, b, u, roll, bank, tilt, psi;
31  };
32  union {
33  float w, a, v, d;
34  };
35 
36  constexpr Vector4f()
37  : x(0), y(0), z(0), w(0) {}
38  constexpr Vector4f(float x, float y, float z, float w)
39  : x(x), y(y), z(z), w(w) {}
40  constexpr Vector4f(float v)
41  : x(v), y(v), z(v), d(v) {}
42  Vector4f(const Vector3f&, float w);
43  Vector4f(const Vector2f&, float z, float w);
44 
45  void serialize(serine::Archiver&);
46 
47  constexpr Vector4f operator-() const {
48  return Vector4f(-x, -y, -z, -w);
49  }
50 
51  constexpr Vector4f operator*(const Vector4f &v) const {
52  return Vector4f(x*v.x, y*v.y, z*v.z, w*v.w);
53  }
55  x *= v.x; y *= v.y; z *= v.z; w *= v.w;
56  return *this;
57  }
58 
59  constexpr Vector4f operator*(float v) const {
60  return Vector4f(x*v, y*v, z*v, w*v);
61  }
62  Vector4f& operator*=(float v) {
63  x *= v; y *= v; z *= v; w *= v;
64  return *this;
65  }
66 
67  constexpr Vector4f operator/(const Vector4f &v) const {
68  return Vector4f(x/v.x, y/v.y, z/v.z, w/v.w);
69  }
71  x /= v.x; y /= v.y; z /= v.z; w /= v.w;
72  return *this;
73  }
74 
75  constexpr Vector4f operator/(float v) const {
76  return Vector4f(x/v, y/v, z/v, w/v);
77  }
78  Vector4f& operator/=(float v) {
79  x /= v; y /= v; z /= v; w /= v;
80  return *this;
81  }
82 
83  constexpr Vector4f operator+(const Vector4f &v) const {
84  return Vector4f(x+v.x, y+v.y, z+v.z, w+v.w);
85  }
87  x += v.x; y += v.y; z += v.z; w += v.w;
88  return *this;
89  }
90 
91  constexpr Vector4f operator-(const Vector4f &v) const {
92  return Vector4f(x-v.x, y-v.y, z-v.z, w-v.w);
93  }
95  x -= v.x; y -= v.y; z -= v.z; w -= v.w;
96  return *this;
97  }
98 
99  bool fuzzyEqual(const Vector4f&, float threshold = .01f) const;
100 
101  inline std::string toString() const {
102  return (std::string("Vec4f{x=") + std::to_string(x) +
103  ", y=" + std::to_string(y) +
104  ", z=" + std::to_string(z) +
105  ", w=" + std::to_string(w) + "}");
106  }
107 
108  operator btVector4() const;
109  Vector4f(const btVector4&);
110  Vector4f& operator=(const btVector4&);
111 
112  operator btQuaternion() const;
113  Vector4f(const btQuaternion&);
114  Vector4f& operator=(const btQuaternion&);
115 };
116 
117 struct Quaternion : public Vector4f {
118  using Vector4f::Vector4f;
119  constexpr Quaternion()
120  : Vector4f(0, 0, 0, 1) {}
121  Quaternion(const btQuaternion&);
122 
123  Quaternion operator*(const Quaternion&) const;
125 
126  Vector3f operator*(const Vector3f&) const;
127 
128  // Quaternion multiplication isn't commutative, and so isn't division
129  Quaternion operator/(const Quaternion&) const = delete;
130  Quaternion& operator/=(const Quaternion&) = delete;
131 
132 
133  Quaternion& fromAxAngle(float x, float y, float z, float r);
134  Quaternion& fromAxAngle(const Vector3f &a, float r);
135  Quaternion& fromAxAngle(const Vector4f &a);
136 
137  Vector4f toAxAngle() const;
138 
139 
140  Quaternion& fromAero(float tetha, float phi, float psi);
141  Quaternion& fromAero(const Vector3f&);
142 
143  Vector3f toAero() const;
144 
145  Matrix4f toMatrix() const;
146 
147  inline std::string toString() const {
148  return (std::string("Quat{x=") + std::to_string(x) +
149  ", y=" + std::to_string(y) +
150  ", z=" + std::to_string(z) +
151  ", w=" + std::to_string(w) + "}");
152  }
153 
154  using Vector4f::operator=;
155  using Vector4f::operator btVector4;
156  using Vector4f::operator btQuaternion;
157 };
158 
159 constexpr inline float length2(const Vector4f &v) {
160  return v.x*v.x + v.y*v.y + v.z*v.z + v.d*v.d;
161 }
162 
163 inline float length(const Vector4f &v) {
164  return std::sqrt(v.x*v.x + v.y*v.y + v.z*v.z + v.d*v.d);
165 }
166 
167 constexpr inline float dot(const Vector4f &v, const Vector4f &w) {
168  return v.x*w.x + v.y*w.y + v.z*w.z + v.w*w.w;
169 }
170 
171 inline Vector4f normalize(const Vector4f &v) {
172  return v / length(v);
173 }
174 
175 inline Quaternion normalize(const Quaternion &q) {
176  const float l = length(q);
177  return Quaternion(q.x/l, q.y/l, q.z/l, q.w/l);
178 }
179 
180 constexpr inline Quaternion conjugate(const Quaternion &q) {
181  return Quaternion(-q.x, -q.y, -q.z, q.w);
182 }
183 
184 } /* namespace radix */
185 
186 #endif /* VECTOR4F_HPP */
float s
Definition: Vector4f.hpp:24
float yaw
Definition: Vector4f.hpp:24
Quaternion & operator*=(const Quaternion &)
Definition: Vector4f.cpp:72
constexpr Vector4f(float x, float y, float z, float w)
Definition: Vector4f.hpp:38
Definition: GameController.hpp:7
float w
Definition: Vector4f.hpp:33
float g
Definition: Vector4f.hpp:27
std::string toString() const
Definition: Vector4f.hpp:101
float u
Definition: Vector4f.hpp:30
float psi
Definition: Vector4f.hpp:30
float heading
Definition: Vector4f.hpp:24
constexpr Vector4f(float v)
Definition: Vector4f.hpp:40
constexpr Vector4f()
Definition: Vector4f.hpp:36
float attitude
Definition: Vector4f.hpp:27
Definition: Vector4f.hpp:117
Quaternion operator/(const Quaternion &) const =delete
Vector4f & operator-=(const Vector4f &v)
Definition: Vector4f.hpp:94
float tilt
Definition: Vector4f.hpp:30
bool fuzzyEqual(const Vector4f &, float threshold=.01f) const
Definition: Vector4f.cpp:32
float x
Definition: Vector4f.hpp:24
float r
Definition: Vector4f.hpp:24
constexpr Vector4f operator-(const Vector4f &v) const
Definition: Vector4f.hpp:91
4-dimensional float-based vector/point storage and manipulation struct
Definition: Vector4f.hpp:22
Quaternion & operator/=(const Quaternion &)=delete
constexpr float dot(const Vector2f &v1, const Vector2f &v2)
Definition: Vector2f.hpp:115
Vector4f toAxAngle() const
Definition: Vector4f.cpp:106
Quaternion & fromAero(float tetha, float phi, float psi)
Definition: Vector4f.cpp:117
float bank
Definition: Vector4f.hpp:30
Vector4f & operator+=(const Vector4f &v)
Definition: Vector4f.hpp:86
constexpr Quaternion()
Definition: Vector4f.hpp:119
float phi
Definition: Vector4f.hpp:27
float z
Definition: Vector4f.hpp:30
float azimuth
Definition: Vector4f.hpp:24
Vector4f & operator*=(float v)
Definition: Vector4f.hpp:62
Quaternion & fromAxAngle(float x, float y, float z, float r)
Definition: Vector4f.cpp:98
2-dimensional float-based vector/point storage and manipulation struct
Definition: Vector2f.hpp:23
constexpr Quaternion conjugate(const Quaternion &q)
Definition: Vector4f.hpp:180
float t
Definition: Vector4f.hpp:27
float d
Definition: Vector4f.hpp:33
float y
Definition: Vector4f.hpp:27
float b
Definition: Vector4f.hpp:30
std::string toString() const
Definition: Vector4f.hpp:147
constexpr Vector4f operator*(float v) const
Definition: Vector4f.hpp:59
Vector4f & operator/=(const Vector4f &v)
Definition: Vector4f.hpp:70
constexpr Vector4f operator*(const Vector4f &v) const
Definition: Vector4f.hpp:51
Vector4f & operator/=(float v)
Definition: Vector4f.hpp:78
void serialize(serine::Archiver &)
Definition: Vector4f.cpp:51
float pitch
Definition: Vector4f.hpp:27
float a
Definition: Vector4f.hpp:33
Definition: Matrix4f.hpp:25
float length(const Vector4f &v)
Definition: Vector4f.hpp:163
Vector4f & operator*=(const Vector4f &v)
Definition: Vector4f.hpp:54
float elevation
Definition: Vector4f.hpp:27
Vector3f toAero() const
Definition: Vector4f.cpp:138
Quaternion operator*(const Quaternion &) const
Definition: Vector4f.cpp:63
constexpr Vector4f operator-() const
Definition: Vector4f.hpp:47
constexpr Vector4f operator/(float v) const
Definition: Vector4f.hpp:75
Matrix4f toMatrix() const
Definition: Vector4f.cpp:166
constexpr float length2(const Vector4f &v)
Definition: Vector4f.hpp:159
float tetha
Definition: Vector4f.hpp:24
float v
Definition: Vector4f.hpp:33
constexpr Vector4f operator+(const Vector4f &v) const
Definition: Vector4f.hpp:83
Vector4f & operator=(const btVector4 &)
Definition: Vector4f.cpp:27
float roll
Definition: Vector4f.hpp:30
3-dimensional float-based vector/point storage and manipulation struct
Definition: Vector3f.hpp:27
Vector2f normalize(const Vector2f &v)
Definition: Vector2f.hpp:118
constexpr Vector4f operator/(const Vector4f &v) const
Definition: Vector4f.hpp:67