Vec3/include/vec3.h

39 lines
816 B
C++

#ifndef VEC3_H
#define VEC3_H
#include <cmath>
template <typename T> struct Vec3 {
T x;
T y;
T z;
inline Vec3<T> operator+(Vec3<T> other) const {
return {x + other.x, y + other.y, z + other.z};
};
inline Vec3<T> operator-(Vec3<T> other) const {
return {x - other.x, y - other.y, z - other.z};
};
inline void scale(T scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
};
inline T dot(Vec3<T> other) const {
return x * other.x + y * other.y + z * other.z;
}
inline Vec3<T> cross(Vec3<T> other) const {
return {y * other.z - z * other.y, z * other.x - x * other.z,
x * other.y - y * other.x};
}
inline double squared_norm2() const { return x * x + y * y + z * z; }
inline double norm2() const { return std::sqrt(squared_norm2()); }
};
#endif