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
Vector3f.hpp
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2 ** This software is in the public domain, furnished "as is", without technical
3 ** support, and with no warranty, express or implied, as to its usefulness for
4 ** any purpose.
5 **
6 ** Vector3f.hpp
7 ** Declares a vector consisting of 3 float values and its helper functions
8 **
9 ** Author: Nim
10 ** -------------------------------------------------------------------------*/
11 
12 #pragma once
13 #ifndef VECTOR3F_HPP
14 #define VECTOR3F_HPP
15 
16 #include <string>
17 
18 #include <serine/Serializable.hpp>
19 
20 class btVector3;
21 
22 namespace radix {
23 
27 struct Vector3f : public serine::Serializable {
28  union {
29  float x, r, s, yaw, heading, azimuth, tetha;
30  };
31  union {
32  float y, g, t, pitch, attitude, elevation, phi;
33  };
34  union {
35  float z, b, u, roll, bank, tilt, psi;
36  };
37 
38  static const Vector3f ZERO, FORWARD, UP;
39 
40  /* Core */
41  constexpr Vector3f()
42  : x(0), y(0), z(0) {}
43  constexpr Vector3f(float x, float y, float z)
44  : x(x), y(y), z(z) {}
45  constexpr Vector3f(float v)
46  : x(v), y(v), z(v) {}
47 
48  float length() const;
49  std::string str() const;
50 
51  void serialize(serine::Archiver&);
52 
53  /* Operator overloads */
54  constexpr bool operator==(const Vector3f &v) const {
55  return x == v.x && y == v.y && z == v.z;
56  }
57 
58  constexpr bool operator!=(const Vector3f &v) const {
59  return x != v.x || y != v.y || z != v.z;
60  }
61 
62  constexpr Vector3f operator-() const {
63  return Vector3f(-x, -y, -z);
64  }
65 
66  constexpr Vector3f operator*(const Vector3f &v) const {
67  return Vector3f(x*v.x, y*v.y, z*v.z);
68  }
70  x *= v.x; y *= v.y; z *= v.z;
71  return *this;
72  }
73 
74  constexpr Vector3f operator*(float v) const {
75  return Vector3f(x*v, y*v, z*v);
76  }
77  Vector3f& operator*=(float v) {
78  x *= v; y *= v; z *= v;
79  return *this;
80  }
81 
82  constexpr Vector3f operator/(const Vector3f &v) const {
83  return Vector3f(x/v.x, y/v.y, z/v.z);
84  }
86  x /= v.x; y /= v.y; z /= v.z;
87  return *this;
88  }
89 
90  constexpr Vector3f operator/(float v) const {
91  return Vector3f(x/v, y/v, z/v);
92  }
93  Vector3f& operator/=(float v) {
94  x /= v; y /= v; z /= v;
95  return *this;
96  }
97 
98  constexpr Vector3f operator+(const Vector3f &v) const {
99  return Vector3f(x+v.x, y+v.y, z+v.z);
100  }
102  x += v.x; y += v.y; z += v.z;
103  return *this;
104  }
105 
106  constexpr Vector3f operator-(const Vector3f &v) const {
107  return Vector3f(x-v.x, y-v.y, z-v.z);
108  }
110  x -= v.x; y -= v.y; z -= v.z;
111  return *this;
112  }
113 
114  bool fuzzyEqual(const Vector3f&, float threshold = .02f) const;
115 
116  /* Bullet interop */
117  Vector3f(const btVector3&);
118  operator btVector3() const;
119  Vector3f& operator=(const btVector3&);
120 };
121 
122 /* Utility functions */
123 constexpr inline float dot(const Vector3f &v1, const Vector3f &v2) {
124  return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
125 }
126 
127 Vector3f cross(const Vector3f &v1, const Vector3f &v2);
128 
129 inline Vector3f normalize(const Vector3f &v) {
130  return v / v.length();
131 }
132 
133 } /* namespace radix */
134 
135 #endif /* VECTOR3F_HPP */
float r
Definition: Vector3f.hpp:29
constexpr Vector3f operator/(const Vector3f &v) const
Definition: Vector3f.hpp:82
Vector3f & operator=(const btVector3 &)
Definition: Vector3f.cpp:61
Definition: GameController.hpp:7
float bank
Definition: Vector3f.hpp:35
static const Vector3f UP
Definition: Vector3f.hpp:38
constexpr bool operator==(const Vector3f &v) const
Definition: Vector3f.hpp:54
Vector3f & operator/=(const Vector3f &v)
Definition: Vector3f.hpp:85
constexpr Vector3f operator-(const Vector3f &v) const
Definition: Vector3f.hpp:106
float x
Definition: Vector3f.hpp:29
constexpr Vector3f()
Definition: Vector3f.hpp:41
float attitude
Definition: Vector3f.hpp:32
float azimuth
Definition: Vector3f.hpp:29
constexpr Vector3f operator*(const Vector3f &v) const
Definition: Vector3f.hpp:66
Vector3f cross(const Vector3f &v1, const Vector3f &v2)
Definition: Vector3f.cpp:67
Vector3f & operator+=(const Vector3f &v)
Definition: Vector3f.hpp:101
float g
Definition: Vector3f.hpp:32
std::string str() const
Definition: Vector3f.cpp:32
constexpr float dot(const Vector2f &v1, const Vector2f &v2)
Definition: Vector2f.hpp:115
float y
Definition: Vector3f.hpp:32
bool fuzzyEqual(const Vector3f &, float threshold=.02f) const
Definition: Vector3f.cpp:45
float z
Definition: Vector3f.hpp:35
float b
Definition: Vector3f.hpp:35
float pitch
Definition: Vector3f.hpp:32
float psi
Definition: Vector3f.hpp:35
float roll
Definition: Vector3f.hpp:35
constexpr Vector3f(float x, float y, float z)
Definition: Vector3f.hpp:43
float s
Definition: Vector3f.hpp:29
Vector3f & operator*=(float v)
Definition: Vector3f.hpp:77
float u
Definition: Vector3f.hpp:35
void serialize(serine::Archiver &)
Definition: Vector3f.cpp:38
float tilt
Definition: Vector3f.hpp:35
constexpr Vector3f operator/(float v) const
Definition: Vector3f.hpp:90
constexpr Vector3f operator-() const
Definition: Vector3f.hpp:62
float heading
Definition: Vector3f.hpp:29
float tetha
Definition: Vector3f.hpp:29
Vector3f & operator/=(float v)
Definition: Vector3f.hpp:93
float yaw
Definition: Vector3f.hpp:29
constexpr Vector3f operator*(float v) const
Definition: Vector3f.hpp:74
static const Vector3f ZERO
Definition: Vector3f.hpp:38
constexpr Vector3f operator+(const Vector3f &v) const
Definition: Vector3f.hpp:98
constexpr bool operator!=(const Vector3f &v) const
Definition: Vector3f.hpp:58
static const Vector3f FORWARD
Definition: Vector3f.hpp:38
float phi
Definition: Vector3f.hpp:32
float t
Definition: Vector3f.hpp:32
constexpr Vector3f(float v)
Definition: Vector3f.hpp:45
Vector3f & operator*=(const Vector3f &v)
Definition: Vector3f.hpp:69
3-dimensional float-based vector/point storage and manipulation struct
Definition: Vector3f.hpp:27
Vector2f normalize(const Vector2f &v)
Definition: Vector2f.hpp:118
float elevation
Definition: Vector3f.hpp:32
float length() const
Definition: Vector3f.cpp:28
Vector3f & operator-=(const Vector3f &v)
Definition: Vector3f.hpp:109