Euphoria
vec3.cc
Go to the documentation of this file.
1 #include "base/vec3.h"
2 
3 
4 namespace eu
5 {
6  float*
8  {
9  return &x;
10  }
11 
12 
13  [[nodiscard]] const float*
15  {
16  return &x;
17  }
18 
19 
21  : x(a)
22  , y(a)
23  , z(a)
24  {
25  }
26 
27 
28  Scale3f::Scale3f(const std::tuple<float, float, float>& a)
29  : x(std::get<0>(a))
30  , y(std::get<1>(a))
31  , z(std::get<2>(a))
32  {
33  }
34 
35 
36  Scale3f::Scale3f(float ax, float ay, float az)
37  : x(ax)
38  , y(ay)
39  , z(az)
40  {
41  }
42 
43 
44  Scale3f::Scale3f(const float* a)
45  : x(a[0])
46  , y(a[1])
47  , z(a[2])
48  {
49  }
50 
51 
53 
54 
55  float*
57  {
58  return &x;
59  }
60 
61 
62  [[nodiscard]] const float*
64  {
65  return &x;
66  }
67 
68 
69  vec3f::vec3f(float a)
70  : x(a)
71  , y(a)
72  , z(a)
73  {
74  }
75 
76 
77  vec3f::vec3f(const std::tuple<float, float, float>& a)
78  : x(std::get<0>(a))
79  , y(std::get<1>(a))
80  , z(std::get<2>(a))
81  {
82  }
83 
84 
85  vec3f::vec3f(const float* a)
86  : x(a[0])
87  , y(a[1])
88  , z(a[2])
89  {
90  }
91 
92 
93  vec3f::vec3f(const vec2f& a, float az)
94  : x(a.x)
95  , y(a.y)
96  , z(az)
97  {
98  }
99 
100 
101  void
103  {
104  x += rhs.x;
105  y += rhs.y;
106  z += rhs.z;
107  }
108 
109 
110  void
112  {
113  x -= rhs.x;
114  y -= rhs.y;
115  z -= rhs.z;
116  }
117 
118 
119  vec3f
121  {
122  return {-this->x, -this->y, -this->z};
123  }
124 
125 
126  vec3f
127  vec3f::from_to(const vec3f& from, const vec3f& to)
128  {
129  return {to.x - from.x, to.y - from.y, to.z - from.z};
130  }
131 
132 
133  void
134  vec3f::operator/=(float rhs)
135  {
136  x /= rhs;
137  y /= rhs;
138  z /= rhs;
139  }
140 
141 
142  void
143  vec3f::operator*=(float rhs)
144  {
145  x *= rhs;
146  y *= rhs;
147  z *= rhs;
148  }
149 
150 
151  [[nodiscard]] float
153  {
154  return sqrt(get_length_squared());
155  }
156 
157 
158  void
160  {
161  const float l2 = get_length_squared();
162  if(is_equal(l2, 0.0f))
163  {
164  *this = common::up;
165  }
166  else
167  {
168  *this /= sqrt(l2);
169  }
170  }
171 
172 
173  [[nodiscard]] unit3f
175  {
176  vec3f r = *this;
177  r.normalize();
178  return unit3f::to_unit(r);
179  }
180 
181  [[nodiscard]] unit3f
183  {
184  return unit3f::to_unit(*this);
185  }
186 
187 
189 
190 
191  unit3f
192  unit3f::to_unit(float x, float y, float z)
193  {
194  return {x, y, z};
195  }
196 
197 
198  unit3f
200  {
201  return {v.x, v.y, v.z};
202  }
203 
204 
207 
208 
209  vec3f
210  operator+(const vec3f& lhs, const vec3f& rhs)
211  {
212  vec3f r = lhs;
213  r += rhs;
214  return r;
215  }
216 
217 
218  vec3f
219  operator-(const vec3f& lhs, const vec3f& rhs)
220  {
221  vec3f r = lhs;
222  r -= rhs;
223  return r;
224  }
225 
226 
227  vec3f operator*(float lhs, const vec3f& rhs)
228  {
229  vec3f r = rhs;
230  r *= lhs;
231  return r;
232  }
233 
234 
235  vec3f operator*(const vec3f& lhs, float rhs)
236  {
237  vec3f r = lhs;
238  r *= rhs;
239  return r;
240  }
241 
242 
243  vec3f
244  operator/(const vec3f& lhs, float rhs)
245  {
246  vec3f r = lhs;
247  r /= rhs;
248  return r;
249  }
250 
251 
252  vec3f
253  operator/(float lhs, const vec3f& rhs)
254  {
255  const vec3f r {lhs / rhs.x, lhs / rhs.y, lhs / rhs.z};
256  return r;
257  }
258 
259 
262 
263 
264  vec3f lerp_vec3f(const vec3f& f, float v, const vec3f& t)
265  {
266  return vec3f
267  {
268  lerp_float(f.x, v, t.x),
269  lerp_float(f.y, v, t.y),
270  lerp_float(f.z, v, t.z)
271  };
272  }
273 
274 
275  float
276  vec3f::dot(const vec3f& rhs) const
277  {
278  return x * rhs.x + y * rhs.y + z * rhs.z;
279  }
280 
281 
282  vec3f
283  vec3f::cross(const vec3f& u) const
284  {
285  return
286  {
287  (y * u.z) - (z * u.y),
288  (z * u.x) - (x * u.z),
289  (x * u.y) - (y * u.x)
290  };
291  }
292 
293 
296 
297 
298  // todo(Gustav): implement transformations
299 
300 
303 
304 
305  std::string to_string(const vec3f& v) { return fmt::format("({}, {}, {})", v.x, v.y, v.z); }
306  std::string to_string(const unit3f& v) { return fmt::format("({}, {}, {})", v.x, v.y, v.z); }
307  std::string to_string(const Scale3f& v) { return fmt::format("({}, {}, {})", v.x, v.y, v.z); }
308 }
constexpr unit3f up
Definition: vec3.h:131
Definition: assert.h:90
float sqrt(float r)
Definition: numeric.cc:101
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
float lerp_float(float f, float scale, float t)
Definition: numeric.cc:87
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
Angle operator*(const Angle &lhs, float rhs)
Definition: angle.cc:149
float * get_data_ptr()
Definition: vec3.cc:7
Scale3f(float a)
Definition: vec3.cc:20
float x
Definition: vec3.h:28
static unit3f to_unit(float x, float y, float z)
Definition: vec3.cc:192
Definition: vec2.h:33
Definition: vec3.h:48
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
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