Euphoria
vec3.h
Go to the documentation of this file.
1#pragma once
2
3#include <tuple>
4
5#include "assert/assert.h"
6
7#include "base/numeric.h"
8
9namespace eu
10{
16 // Forward declarations
17
18 struct Q;
19
20 struct v3;
21 struct n3;
22
24
26 struct v3
27 {
28 float x;
29 float y;
30 float z;
31
32 explicit v3(float a);
33 explicit v3(const std::tuple<float, float, float> &a);
34 constexpr v3(float ax, float ay, float az)
35 : x(ax), y(ay), z(az)
36 {
37 }
38
40 explicit v3(const float *a);
41
43 static v3 from_to(const v3 &from, const v3 &to);
44
46 [[nodiscard]] static v3 from_localspace_rui(const Q& rotation, float right, float up, float in);
47
48 [[nodiscard]] float dot(const v3 &rhs) const;
49 [[nodiscard]] v3 cross(const v3 &u) const;
50
51 void operator+=(const v3 &rhs);
52 void operator-=(const v3 &rhs);
53 void operator/=(float rhs);
54 void operator*=(float rhs);
55 v3 operator-() const;
56
59 float *get_data_ptr();
60
62 [[nodiscard]] const float *get_data_ptr() const;
63
66 [[nodiscard]] constexpr float get_length_squared() const
67 {
68 return x * x + y * y + z * z;
69 }
70
72 [[nodiscard]] float get_length() const;
73
77 bool normalize();
78
81 [[nodiscard]] std::optional<n3> get_normalized() const;
82
83 bool operator==(const v3 &rhs) = delete;
84 };
85
86 constexpr v3 zero3f = v3{0.0f, 0.0f, 0.0f};
87
89
91 struct n3 : public v3
92 {
93 constexpr n3 operator-() const
94 {
95 return {-this->x, -this->y, -this->z};
96 }
97
99 [[nodiscard]] constexpr bool
100 is_valid() const
101 {
102 return is_equal(get_length_squared(), 1.0f);
103 }
104
105 bool operator==(const n3 &rhs) = delete;
106
108 constexpr n3(float a, float b, float c)
109 : v3(a, b, c)
110 {
111 ASSERT(is_valid());
112 }
113
115 constexpr explicit n3(const v3& v)
116 : v3(v)
117 {
118 ASSERT(is_valid());
119 }
120 };
121
122 namespace kk
123 {
124 constexpr n3 x_axis = n3{1.0f, 0.0f, 0.0f};
125 constexpr n3 y_axis = n3{0.0f, 1.0f, 0.0f};
126 constexpr n3 z_axis = n3{0.0f, 0.0f, 1.0f};
127 constexpr n3 up = y_axis;
128 constexpr n3 down = -y_axis;
129 constexpr n3 right = x_axis;
130 constexpr n3 left = -x_axis;
131 constexpr n3 in = -z_axis;
132 constexpr n3 out = z_axis;
133 }
134
135 v3 operator+(const v3 &lhs, const v3 &rhs);
136 v3 operator-(const v3 &lhs, const v3 &rhs);
137 v3 operator*(float lhs, const v3 &rhs);
138 v3 operator*(const v3 &lhs, float rhs);
139 v3 operator/(const v3 &lhs, float rhs);
140 v3 operator/(float lhs, const v3 &rhs);
141
142
143 v3 lerp_v3(const v3 &f, float v, const v3 &t);
144
146 std::string string_from(const v3 &v);
147
149 std::string string_from(const n3 &v);
150
152 constexpr v3
153 min(const v3 &lhs, const v3 &rhs)
154 {
155 return {
156 eu::min(lhs.x, rhs.x),
157 eu::min(lhs.y, rhs.y),
158 eu::min(lhs.z, rhs.z)
159 };
160 }
161
163 constexpr v3
164 max(const v3 &lhs, const v3 &rhs)
165 {
166 return {
167 eu::max(lhs.x, rhs.x),
168 eu::max(lhs.y, rhs.y),
169 eu::max(lhs.z, rhs.z)
170 };
171 }
172
177
178}
179
180ADD_DEFAULT_FORMATTER(eu::v3, std::string, eu::string_from);
181ADD_DEFAULT_FORMATTER(eu::n3, std::string, eu::string_from);
182
#define ASSERT(x)
Assert that a value is true.
Definition assert.h:36
#define ADD_CATCH_FORMATTER_DEF(TYPE)
Definition pch.public.h:22
An operator/(const An &lhs, float rhs)
constexpr float min(float lhs, float rhs)
Definition numeric.h:78
An operator-(const An &lhs, const An &rhs)
constexpr bool is_equal(float lhs, float rhs, float epsilon=kk::epsilon)
Definition numeric.h:33
v3 lerp_v3(const v3 &f, float v, const v3 &t)
An operator+(const An &lhs, const An &rhs)
An operator*(const An &lhs, float rhs)
constexpr v3 zero3f
Definition vec3.h:86
std::string string_from(const An &a)
constexpr float max(float lhs, float rhs)
Definition numeric.h:79
constexpr n3 y_axis
Definition vec3.h:125
constexpr n3 x_axis
Definition vec3.h:124
constexpr n3 right
Definition vec3.h:129
constexpr n3 in
Definition vec3.h:131
constexpr n3 z_axis
Definition vec3.h:126
constexpr n3 left
Definition vec3.h:130
constexpr n3 out
Definition vec3.h:132
constexpr n3 down
Definition vec3.h:128
constexpr n3 up
Definition vec3.h:127
Definition assert.h:109
A quaternion representing a rotation in 3d.
Definition quat.h:19
A (inclusive) range between two values.
Definition range.h:19
a 3d unit (vector)
Definition vec3.h:92
constexpr n3 operator-() const
Definition vec3.h:93
bool operator==(const n3 &rhs)=delete
constexpr n3(float a, float b, float c)
asserts that the length is 1
Definition vec3.h:108
constexpr n3(const v3 &v)
asserts that the length is 1
Definition vec3.h:115
constexpr bool is_valid() const
returns false if the length isn't 1
Definition vec3.h:100
a 3d vector
Definition vec3.h:27
float * get_data_ptr()
Returns an array to the data.
void operator/=(float rhs)
constexpr v3(float ax, float ay, float az)
Definition vec3.h:34
constexpr float get_length_squared() const
Returns the squared length of the vector.
Definition vec3.h:66
static v3 from_to(const v3 &from, const v3 &to)
creates a vector going from from to to
void operator+=(const v3 &rhs)
v3 operator-() const
void operator-=(const v3 &rhs)
float z
Definition vec3.h:30
v3(float a)
std::optional< n3 > get_normalized() const
Returns a unit vector.
static v3 from_localspace_rui(const Q &rotation, float right, float up, float in)
Creates a vector in quaterion local space from Right Up In.
float dot(const v3 &rhs) const
bool normalize()
Changes the length to 1.
float x
Definition vec3.h:28
void operator*=(float rhs)
v3(const std::tuple< float, float, float > &a)
float y
Definition vec3.h:29
const float * get_data_ptr() const
Returns an array to the data.
v3 cross(const v3 &u) const
float get_length() const
Returns the length of the vector.
bool operator==(const v3 &rhs)=delete
v3(const float *a)
assumes the given pointer is a array of 3 floats