#ifndef VEC3_H #define VEC3_H #include #include template struct Vec3 { T x; T y; T z; inline Vec3 operator+(Vec3 other) const { return {x + other.x, y + other.y, z + other.z}; }; inline Vec3 operator-(Vec3 other) const { return {x - other.x, y - other.y, z - other.z}; }; inline Vec3 scale(T scalar) { return {x * scalar, y * scalar, z * scalar}; }; inline T dot(Vec3 other) const { return x * other.x + y * other.y + z * other.z; } inline Vec3 cross(Vec3 other) const { return {y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x}; } inline T squared_norm2() const { return x * x + y * y + z * z; } inline T norm2() const { return std::sqrt(squared_norm2()); } inline Vec3 normalized() { // Add epsilon to the norm for stability when the norm is 0 T norm = std::max(norm2(), std::numeric_limits::epsilon()); return {x / norm, y / norm, z / norm}; } }; #endif