Euphoria
vec3.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 
5 #include <tuple>
6 
7 #include "base/vec2.h"
8 #include "base/numeric.h"
9 #include "assert/assert.h"
10 #include "base/cint.h"
11 
12 
13 namespace eu
14 {
17 
18  struct vec3f;
19  struct unit3f;
20  struct Scale3f;
21 
22 
24 
25 
26  struct Scale3f
27  {
28  float x;
29  float y;
30  float z;
31 
32  explicit Scale3f(float a);
33  explicit Scale3f(const std::tuple<float, float, float>& a);
34  Scale3f(float ax, float ay, float az);
35  explicit Scale3f(const float* a);
36 
37  float* get_data_ptr();
38  [[nodiscard]] const float* get_data_ptr() const;
39 
40  bool operator==(const Scale3f& rhs) = delete;
41  };
42 
43 
45 
46 
47  struct vec3f
48  {
49  float x;
50  float y;
51  float z;
52 
53  explicit vec3f(float a);
54  explicit vec3f(const std::tuple<float, float, float>& a);
55  constexpr vec3f(float ax, float ay, float az)
56  : x(ax)
57  , y(ay)
58  , z(az)
59  {
60  }
61 
62  explicit vec3f(const float* a);
63  vec3f(const vec2f& a, float az);
64 
65  static vec3f from_to(const vec3f& from, const vec3f& to);
66 
67  [[nodiscard]] float dot(const vec3f& rhs) const;
68  [[nodiscard]] vec3f cross(const vec3f& u) const;
69 
70  void operator+=(const vec3f& rhs);
71  void operator-=(const vec3f& rhs);
72  void operator/=(float rhs);
73  void operator*=(float rhs);
74  vec3f operator-() const;
75 
76  float* get_data_ptr();
77  [[nodiscard]] const float* get_data_ptr() const;
78 
79  [[nodiscard]] constexpr float get_length_squared() const
80  {
81  return x * x + y * y + z * z;
82  }
83 
84  [[nodiscard]] float get_length() const;
85 
86  void normalize();
87  [[nodiscard]] unit3f get_normalized() const;
88 
89  // todo(Gustav): rename to assume_normalized
90  [[nodiscard]] unit3f as_normalized() const;
91 
92  bool operator==(const vec3f& rhs) = delete;
93  };
94 
95  constexpr vec3f zero3f = vec3f{ 0.0f, 0.0f, 0.0f };
96 
97 
99 
100 
101  struct unit3f : public vec3f
102  {
103  constexpr unit3f operator-() const
104  {
105  return {-this->x, -this->y, -this->z};
106  }
107 
108  [[nodiscard]] constexpr bool
109  is_valid() const
110  {
111  return is_equal(get_length_squared(), 1.0f);
112  }
113 
114  static unit3f to_unit(float x, float y, float z);
115  static unit3f to_unit(const vec3f& v);
116 
117  bool operator==(const unit3f& rhs) = delete;
118 
119  constexpr unit3f(float a, float b, float c)
120  : vec3f(a, b, c)
121  {
122  ASSERT(is_valid());
123  }
124  };
125 
126  namespace common
127  {
128  constexpr unit3f x_axis = unit3f {1.0f, 0.0f, 0.0f};
129  constexpr unit3f y_axis = unit3f {0.0f, 1.0f, 0.0f};
130  constexpr unit3f z_axis = unit3f {0.0f, 0.0f, 1.0f};
131  constexpr unit3f up = y_axis;
132  constexpr unit3f down = -y_axis;
133  constexpr unit3f right = x_axis;
134  constexpr unit3f left = -x_axis;
135  constexpr unit3f in = -z_axis;
136  constexpr unit3f out = z_axis;
137  }
138 
141 
142  vec3f operator+(const vec3f& lhs, const vec3f& rhs);
143  vec3f operator-(const vec3f& lhs, const vec3f& rhs);
144 
145  vec3f operator*(float lhs, const vec3f& rhs);
146  vec3f operator*(const vec3f& lhs, float rhs);
147 
148  vec3f operator/(const vec3f& lhs, float rhs);
149  vec3f operator/(float lhs, const vec3f& rhs);
150 
151 
154 
155  vec3f lerp_vec3f(const vec3f& f, float v, const vec3f& t);
156 
157 
160 
161  std::string to_string(const vec3f& v);
162  std::string to_string(const unit3f& v);
163  std::string to_string(const Scale3f& v);
164 
165 
167  // minmax
168  constexpr vec3f
169  min(const vec3f& lhs, const vec3f& rhs)
170  {
171 #define M(var) eu::min(lhs.var, rhs.var)
172  return vec3f {M(x), M(y), M(z)};
173 #undef M
174  }
175 
176 
177  constexpr vec3f
178  max(const vec3f& lhs, const vec3f& rhs)
179  {
180 #define M(var) eu::max(lhs.var, rhs.var)
181  return vec3f {M(x), M(y), M(z)};
182 #undef M
183  }
184 }
185 
#define ASSERT(x)
Definition: assert.h:29
constexpr unit3f in
Definition: vec3.h:135
constexpr unit3f x_axis
Definition: vec3.h:128
constexpr unit3f out
Definition: vec3.h:136
constexpr unit3f right
Definition: vec3.h:133
constexpr unit3f y_axis
Definition: vec3.h:129
constexpr unit3f left
Definition: vec3.h:134
constexpr unit3f z_axis
Definition: vec3.h:130
constexpr unit3f up
Definition: vec3.h:131
constexpr unit3f down
Definition: vec3.h:132
Definition: assert.h:90
Angle operator+(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:123
Angle operator-(const Angle &lhs, const Angle &rhs)
Definition: angle.cc:132
Angle operator/(const Angle &lhs, float rhs)
Definition: angle.cc:141
vec3f lerp_vec3f(const vec3f &f, float v, const vec3f &t)
Functions.
Definition: vec3.cc:264
std::string to_string(const Aabb &a)
Definition: aabb.cc:110
bool is_equal(int lhs, int rhs)
Definition: numeric.cc:13
size2f min(const size2f lhs, const size2f rhs)
Definition: size2.cc:140
size2f max(const size2f lhs, const size2f rhs)
Definition: size2.cc:149
Angle operator*(const Angle &lhs, float rhs)
Definition: angle.cc:149
constexpr vec3f zero3f
Definition: vec3.h:95
float y
Definition: vec3.h:29
float * get_data_ptr()
Definition: vec3.cc:7
Scale3f(float a)
Definition: vec3.cc:20
float x
Definition: vec3.h:28
float z
Definition: vec3.h:30
bool operator==(const Scale3f &rhs)=delete
constexpr bool is_valid() const
Definition: vec3.h:109
static unit3f to_unit(float x, float y, float z)
Definition: vec3.cc:192
bool operator==(const unit3f &rhs)=delete
constexpr unit3f(float a, float b, float c)
Definition: vec3.h:119
constexpr unit3f operator-() const
Definition: vec3.h:103
Definition: vec2.h:33
Definition: vec3.h:48
constexpr vec3f(float ax, float ay, float az)
Definition: vec3.h:55
void operator-=(const vec3f &rhs)
Definition: vec3.cc:111
float x
Definition: vec3.h:49
float y
Definition: vec3.h:50
unit3f get_normalized() const
Definition: vec3.cc:174
float * get_data_ptr()
Definition: vec3.cc:56
void operator*=(float rhs)
Definition: vec3.cc:143
vec3f operator-() const
Definition: vec3.cc:120
unit3f as_normalized() const
Definition: vec3.cc:182
float get_length() const
Definition: vec3.cc:152
void normalize()
Definition: vec3.cc:159
constexpr float get_length_squared() const
Definition: vec3.h:79
vec3f cross(const vec3f &u) const
Definition: vec3.cc:283
vec3f(float a)
Definition: vec3.cc:69
static vec3f from_to(const vec3f &from, const vec3f &to)
Definition: vec3.cc:127
float z
Definition: vec3.h:51
bool operator==(const vec3f &rhs)=delete
void operator+=(const vec3f &rhs)
Definition: vec3.cc:102
float dot(const vec3f &rhs) const
Definition: vec3.cc:276
void operator/=(float rhs)
Definition: vec3.cc:134
ADD_DEFAULT_FORMATTER(eu::vec3f, std::string, eu::to_string)
#define M(var)