LinearAlgebra
Loading...
Searching...
No Matches
Vector3.h
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0.If a copy of the MPL was not distributed with this
3// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
4
5#ifndef VECTOR3_H
6#define VECTOR3_H
7
8#include "Vector2.h"
9
10extern "C" {
16typedef struct Vec3 {
17protected:
21 float x;
25 float y;
29 float z;
30
31} Vec3;
32}
33
34namespace Passer {
35namespace LinearAlgebra {
36
37template <typename T> class SphericalOf;
38
42struct Vector3 : Vec3 {
43 friend struct Vec3;
44
45public:
47 Vector3();
52 Vector3(float right, float up, float forward);
60
62 ~Vector3();
63
65 const static Vector3 zero;
67 const static Vector3 one;
69 const static Vector3 forward;
71 const static Vector3 back;
73 const static Vector3 right;
75 const static Vector3 left;
77 const static Vector3 up;
79 const static Vector3 down;
80
81 // Access functions which are intended to replace the use of XYZ
82 inline float Forward() const { return z; };
83 inline float Up() const { return y; };
84 inline float Right() const { return x; };
85
91 bool operator==(const Vector3 &v) const;
92
96 static float Magnitude(const Vector3 &v);
99 float magnitude() const;
106 static float SqrMagnitude(const Vector3 &v);
112 float sqrMagnitude() const;
113
117 static Vector3 Normalize(const Vector3 &v);
120 Vector3 normalized() const;
121
124 Vector3 operator-() const;
125
129 Vector3 operator-(const Vector3 &v) const;
130 Vector3 operator-=(const Vector3 &v);
134 Vector3 operator+(const Vector3 &v) const;
135 Vector3 operator+=(const Vector3 &v);
136
143 static Vector3 Scale(const Vector3 &v1, const Vector3 &v2);
149 friend Vector3 operator*(const Vector3 &v, float f) {
150 return Vector3(v.x * f, v.y * f, v.z * f);
151 }
152 friend Vector3 operator*(float f, const Vector3 &v) {
153 // return Vector3(f * v.x, f * v.y, f * v.z);
154 return Vector3(v.x * f, v.y * f, v.z * f);
155 }
156 Vector3 operator*=(float f);
161 friend Vector3 operator/(const Vector3 &v, float f) {
162 return Vector3(v.x / f, v.y / f, v.z / f);
163 }
164 friend Vector3 operator/(float f, const Vector3 &v) {
165 // return Vector3(f / v.x, f / v.y, f / v.z);
166 return Vector3(v.x / f, v.y / f, v.z / f);
167 }
168 Vector3 operator/=(float f);
169
174 static float Distance(const Vector3 &v1, const Vector3 &v2);
175
180 static float Dot(const Vector3 &v1, const Vector3 &v2);
181
186 static Vector3 Cross(const Vector3 &v1, const Vector3 &v2);
187
192 static Vector3 Project(const Vector3 &v, const Vector3 &n);
198 static Vector3 ProjectOnPlane(const Vector3 &v, const Vector3 &n);
199
207 static AngleOf<float> Angle(const Vector3 &v1, const Vector3 &v2);
213 static AngleOf<float> SignedAngle(const Vector3 &v1, const Vector3 &v2,
214 const Vector3 &axis);
215
224 static Vector3 Lerp(const Vector3 &v1, const Vector3 &v2, float f);
225};
226
227} // namespace LinearAlgebra
228} // namespace Passer
229using namespace Passer::LinearAlgebra;
230
231#include "Spherical.h"
232
233#endif
An angle in various representations.
Definition Angle.h:21
A spherical vector using angles in various representations.
Definition Spherical.h:18
A 2-dimensional vector.
Definition Vector2.h:38
A 3-dimensional vector.
Definition Vector3.h:42
static const Vector3 back
A normalized back-oriented vector.
Definition Vector3.h:71
static const Vector3 left
A normalized left-oriented vector.
Definition Vector3.h:75
static AngleOf< float > Angle(const Vector3 &v1, const Vector3 &v2)
The angle between two vectors.
Definition Vector3.cpp:187
Vector3()
A new 3-dimensional zero vector.
Definition Vector3.cpp:15
static float Distance(const Vector3 &v1, const Vector3 &v2)
The distance between two vectors.
Definition Vector3.cpp:156
Vector3(SphericalOf< float > v)
Convert vector in spherical coordinates to 3d carthesian coordinates.
bool operator==(const Vector3 &v) const
Check if this vector to the given vector.
Definition Vector3.cpp:152
static const Vector3 one
A vector with one for all axis.
Definition Vector3.h:67
static float Dot(const Vector3 &v1, const Vector3 &v2)
The dot product of two vectors.
Definition Vector3.cpp:148
static float SqrMagnitude(const Vector3 &v)
The squared vector length.
Definition Vector3.cpp:75
static Vector3 Normalize(const Vector3 &v)
Convert the vector to a length of 1.
Definition Vector3.cpp:80
friend Vector3 operator*(const Vector3 &v, float f)
Scale the vector uniformly up.
Definition Vector3.h:149
static Vector3 Lerp(const Vector3 &v1, const Vector3 &v2, float f)
Lerp (linear interpolation) between two vectors.
Definition Vector3.cpp:218
~Vector3()
Vector3 destructor.
Definition Vector3.cpp:52
float magnitude() const
The vector length.
Definition Vector3.cpp:73
Vector3 normalized() const
Convert the vector to a length of 1.
Definition Vector3.cpp:88
Vector3 operator+(const Vector3 &v) const
Add a vector to this vector.
Definition Vector3.cpp:110
static const Vector3 up
A normalized up-oriented vector.
Definition Vector3.h:77
static const Vector3 zero
A vector with zero for all axis.
Definition Vector3.h:65
static Vector3 Cross(const Vector3 &v1, const Vector3 &v2)
The cross product of two vectors.
Definition Vector3.cpp:160
static const Vector3 forward
A normalized forward-oriented vector.
Definition Vector3.h:69
static Vector3 ProjectOnPlane(const Vector3 &v, const Vector3 &n)
Project the vector on a plane defined by a normal orthogonal to the plane.
Definition Vector3.cpp:176
static const Vector3 down
A normalized down-oriented vector.
Definition Vector3.h:79
float sqrMagnitude() const
The squared vector length.
Definition Vector3.cpp:78
Vector3 operator-() const
Negate te vector such that it points in the opposite direction.
Definition Vector3.cpp:97
static float Magnitude(const Vector3 &v)
The vector length.
Definition Vector3.cpp:70
friend Vector3 operator/(const Vector3 &v, float f)
Scale the vector uniformly down.
Definition Vector3.h:161
static Vector3 Project(const Vector3 &v, const Vector3 &n)
Project the vector on another vector.
Definition Vector3.cpp:165
static const Vector3 right
A normalized right-oriented vector.
Definition Vector3.h:73
static AngleOf< float > SignedAngle(const Vector3 &v1, const Vector3 &v2, const Vector3 &axis)
The signed angle between two vectors.
Definition Vector3.cpp:203
static Vector3 Scale(const Vector3 &v1, const Vector3 &v2)
Scale the vector using another vector.
Definition Vector3.cpp:120
3-dimensional Vector representation (C-style)
Definition Vector3.h:16
float x
The right axis of the vector.
Definition Vector3.h:21
float z
The forward axis of the vector.
Definition Vector3.h:29
float y
The upward axis of the vector.
Definition Vector3.h:25