Update scale to return new vector and template out the norm functions
This commit is contained in:
parent
a6c14ce0ab
commit
fbc34a0bdd
2 changed files with 61 additions and 23 deletions
|
@ -2,6 +2,7 @@
|
|||
#define VEC3_H
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
template <typename T> struct Vec3 {
|
||||
T x;
|
||||
T y;
|
||||
|
@ -15,11 +16,7 @@ template <typename T> struct Vec3 {
|
|||
return {x - other.x, y - other.y, z - other.z};
|
||||
};
|
||||
|
||||
inline void scale(T scalar) {
|
||||
x *= scalar;
|
||||
y *= scalar;
|
||||
z *= scalar;
|
||||
};
|
||||
inline Vec3 scale(T scalar) { return {x * scalar, y * scalar, z * scalar}; };
|
||||
|
||||
inline T dot(Vec3<T> other) const {
|
||||
return x * other.x + y * other.y + z * other.z;
|
||||
|
@ -30,9 +27,15 @@ template <typename T> struct Vec3 {
|
|||
x * other.y - y * other.x};
|
||||
}
|
||||
|
||||
inline double squared_norm2() const { return x * x + y * y + z * z; }
|
||||
inline T squared_norm2() const { return x * x + y * y + z * z; }
|
||||
|
||||
inline double norm2() const { return std::sqrt(squared_norm2()); }
|
||||
inline T norm2() const { return std::sqrt(squared_norm2()); }
|
||||
|
||||
inline Vec3<T> normalized() {
|
||||
// Add epsilon to the norm for stability when the norm is 0
|
||||
T norm = std::max(norm2(), std::numeric_limits<T>::epsilon());
|
||||
return {x / norm, y / norm, z / norm};
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue