flippy
a c++20 package for dynamically triangulated membrane simulations.
Loading...
Searching...
No Matches
vec3.hpp
Go to the documentation of this file.
1#ifndef FLIPPY_VEC3_HPP
2#define FLIPPY_VEC3_HPP
10#include <ostream>
11#include <iostream>
12#include <cmath>
13#include "custom_concepts.hpp"
14
15
16namespace fp{
17
41template<floating_point_number Real>
42class vec3
43{
44public:
45
46 Real x;
47 Real y;
48 Real z;
49
51
60 void add(vec3<Real> const& v)
61 {
62 x += v.x;
63 y += v.y;
64 z += v.z;
65 }
66
68
77 void subtract(vec3<Real> const& v)
78 {
79 x -= v.x;
80 y -= v.y;
81 z -= v.z;
82 }
84
88 void scale(Real s)
89 {
90 x = s*x;
91 y = s*y;
92 z = s*z;
93 }
94
96
106 Real dot(vec3<Real> const& v) const
107 {
108 Real res = x*v.x + y*v.y + z*v.z;
109 return res;
110 }
111
113
118 [[nodiscard]] constexpr std::size_t size() const { return 3; }
119
121
133 static inline vec3<Real> cross(vec3<Real> const& a, vec3<Real> const& b)
134 {
135 vec3<Real> res;
136 res.x = a.y*b.z - a.z*b.y;
137 res.y = a.z*b.x - a.x*b.z;
138 res.z = a.x*b.y - a.y*b.x;
139 return res;
140 }
141
143
153 vec3<Real> cross(vec3<Real> const& other) const { return cross(*this, other); }
154
156
164 Real norm() const { return std::sqrt(this->dot(*this)); }
165
167
175 Real norm_square() const { return this->dot(*this); }
176
178
185 *this= *this/this->norm();
186 return *this;
187 }
188
190 friend std::ostream& operator<<(std::ostream& os, const vec3<Real>& obj)
191 {
192 os << "{" << obj.x << ',' << obj.y << ',' << obj.z << '}';
193 return os;
194 }
195
197
201 bool operator==(vec3<Real> const& other) const =default;
202
203
205
212 {
213 lhs+=rhs;
214 return lhs;
215 }
216
218
223 friend void operator+=(vec3<Real>& lhs, vec3<Real> const& rhs)
224 {
225 lhs.add(rhs);
226 }
227
229
236 {
237 lhs-=rhs;
238 return lhs;
239 }
240
242
247 friend void operator-=(vec3<Real>& lhs, vec3<Real> const& rhs)
248 {
249 lhs.subtract(rhs);
250 }
251
253
259 friend vec3<Real> operator*(Real const& lhs, vec3<Real> rhs)
260 {
261 rhs.scale(lhs);
262 return rhs;
263 }
264
266
272 friend vec3<Real> operator*(vec3<Real> lhs, Real const& rhs)
273 {
274 lhs.scale(rhs);
275 return lhs;
276 }
277
279
285 friend void operator/=(vec3<Real>& lhs, Real const& rhs){
286 lhs.scale(static_cast<Real>(1.)/rhs);
287 }
288
290
297 friend vec3<Real> operator/(vec3<Real> lhs, Real const& rhs)
298 {
299 lhs/=rhs;
300 return lhs;
301 }
302
304
311 template<typename Index>
312 requires std::is_integral_v<Index>
313 Real& operator[](Index idx)
314 {
315 switch (idx) {
316 case 0:return x;
317 case 1:return y;
318 case 2:return z;
319 default:std::cerr << idx << "is out of range for as vec3 index";
320 exit(12);
321 }
322 }
323
325
332 template<typename Index>
333 requires std::is_integral_v<Index>
334 const Real& operator[](Index idx) const
335 {
336 switch (idx) {
337 case 0:return x;
338 case 1:return y;
339 case 2:return z;
340 default:std::cerr << idx << "is out of range for as vec3 index";
341 exit(12);
342 }
343 }
344
346
352 {
353 v.x = -v.x;
354 v.y = -v.y;
355 v.z = -v.z;
356 return v;
357 }
358
359};
360}
361
362#endif //FLIPPY_VEC3_HPP
Internal implementation of a 3D vector.
Definition vec3.hpp:43
constexpr std::size_t size() const
Always returns 3.
Definition vec3.hpp:118
vec3< Real > cross(vec3< Real > const &other) const
Calculate cross product with another vector.
Definition vec3.hpp:153
Real dot(vec3< Real > const &v) const
Calculate dot product with another vector.
Definition vec3.hpp:106
friend void operator/=(vec3< Real > &lhs, Real const &rhs)
Overloaded operator defined in terms of vec3::scale.
Definition vec3.hpp:285
vec3< Real > const & normalize()
Normalize the vector in place. And return a reference to the new normalized vector.
Definition vec3.hpp:184
friend vec3< Real > operator-(vec3< Real > lhs, vec3< Real > const &rhs)
Overloaded operator defined in terms of vec3::subtract.
Definition vec3.hpp:235
friend vec3< Real > operator*(vec3< Real > lhs, Real const &rhs)
Overloaded operator defined in terms of vec3::scale.
Definition vec3.hpp:272
Real z
The z component of the vector.
Definition vec3.hpp:48
void subtract(vec3< Real > const &v)
In place subtraction method.
Definition vec3.hpp:77
const Real & operator[](Index idx) const
element access operator for constant environments.
Definition vec3.hpp:334
Real norm() const
Returns the norm of the vector.
Definition vec3.hpp:164
friend void operator+=(vec3< Real > &lhs, vec3< Real > const &rhs)
Overloaded operator defined in terms of vec3::add.
Definition vec3.hpp:223
friend vec3< Real > operator/(vec3< Real > lhs, Real const &rhs)
Overloaded operator defined in terms of vec3::scale.
Definition vec3.hpp:297
Real norm_square() const
Returns the square of the norm of the vector.
Definition vec3.hpp:175
Real y
The y component of the vector.
Definition vec3.hpp:47
bool operator==(vec3< Real > const &other) const =default
default equality operator.
friend vec3< Real > operator*(Real const &lhs, vec3< Real > rhs)
Overloaded operator defined in terms of vec3::scale.
Definition vec3.hpp:259
void scale(Real s)
Scale the vector by a real number s.
Definition vec3.hpp:88
void add(vec3< Real > const &v)
In place addition method.
Definition vec3.hpp:60
friend vec3< Real > operator-(vec3< Real > v)
Unary minus operator.
Definition vec3.hpp:351
friend std::ostream & operator<<(std::ostream &os, const vec3< Real > &obj)
Streaming operator for easy printing of the vector.
Definition vec3.hpp:190
friend vec3< Real > operator+(vec3< Real > lhs, vec3< Real > const &rhs)
Overloaded operator defined in terms of vec2::add.
Definition vec3.hpp:211
Real & operator[](Index idx)
element access operator.
Definition vec3.hpp:313
Real x
The x component of the vector.
Definition vec3.hpp:46
static vec3< Real > cross(vec3< Real > const &a, vec3< Real > const &b)
Calculate cross product between two vectors.
Definition vec3.hpp:133
friend void operator-=(vec3< Real > &lhs, vec3< Real > const &rhs)
Overloaded operator defined in terms of vec3::subtract.
Definition vec3.hpp:247
This file contains the concepts that are costomly defined for the flippy class templates.
Definition custom_concepts.hpp:8