#ifndef VEC3_H #define VEC3_H #include #include #include "util.h" class Vec3 { public: Vec3() : e{0, 0, 0} {} Vec3(double e0, double e1, double e2) : e{e0, e1, e2} {} double x() const { return e[0]; } double y() const { return e[1]; } double z() const { return e[2]; } Vec3 operator-() const { return Vec3(-e[0], -e[1], -e[2]); } double operator[](int i) const { return e[i]; } double& operator[](int i) { return e[i]; } Vec3& operator+=(const Vec3& v) { e[0] += v.e[0]; e[1] += v.e[1]; e[2] += v.e[2]; return *this; } Vec3& operator*=(const double t) { e[0] *= t; e[1] *= t; e[2] *= t; return *this; } Vec3& operator/=(const double t) { return *this *= 1 / t; } double length() const { return std::sqrt(length_squared()); } double length_squared() const { return e[0] * e[0] + e[1] * e[1] + e[2] * e[2]; } inline static Vec3 random() { return Vec3(random_double(), random_double(), random_double()); } inline static Vec3 random(double min, double max) { return Vec3(random_double(min, max), random_double(min, max), random_double(min, max)); } private: double e[3]; friend std::ostream& operator<<(std::ostream&, const Vec3&); friend Vec3 operator+(const Vec3&, const Vec3&); friend Vec3 operator-(const Vec3&, const Vec3&); friend Vec3 operator*(const Vec3&, const Vec3&); friend Vec3 operator*(double, const Vec3&); friend Vec3 operator*(const Vec3&, double); friend Vec3 operator/(const Vec3&, double); friend double dot(const Vec3&, const Vec3&); friend Vec3 cross(const Vec3&, const Vec3&); friend Vec3 unit_vector(const Vec3); }; using Point3 = Vec3; using Color = Vec3; // Vec3 utility functions inline std::ostream& operator<<(std::ostream& out, const Vec3& v) { return out << v.e[0] << " " << v.e[1] << " " << v.e[2]; } inline Vec3 operator+(const Vec3& u, const Vec3& v) { return Vec3(u.e[0] + v.e[0], u.e[1] + v.e[1], u.e[2] + v.e[2]); } inline Vec3 operator-(const Vec3& u, const Vec3& v) { return Vec3(u.e[0] - v.e[0], u.e[1] - v.e[1], u.e[2] - v.e[2]); } inline Vec3 operator*(const Vec3& u, const Vec3& v) { return Vec3(u.e[0] * v.e[0], u.e[1] * v.e[1], u.e[2] * v.e[2]); } inline Vec3 operator*(double t, const Vec3& v) { return Vec3(t * v.e[0], t * v.e[1], t * v.e[2]); } inline Vec3 operator*(const Vec3& v, double t) { return Vec3(t * v.e[0], t * v.e[1], t * v.e[2]); } inline Vec3 operator/(const Vec3& v, double t) { return (1/t) * v; } inline double dot(const Vec3 &u, const Vec3& v) { return u.e[0] * v.e[0] + u.e[1] * v.e[1] + u.e[2] * v.e[2]; } inline Vec3 cross(const Vec3 &u, const Vec3& v) { return Vec3(u.e[1] * v.e[2] - u.e[2] * v.e[1], u.e[2] * v.e[0] - u.e[0] * v.e[2], u.e[0] * v.e[1] - u.e[1] * v.e[0]); } inline Vec3 unit_vector(Vec3 v) { return v / v.length(); } Vec3 random_in_unit_sphere() { while (true) { auto p = Vec3::random(-1, 1); if (p.length_squared() >= 1) continue; return p; } } Vec3 random_unit_vector() { auto a = random_double(0, 2*pi); auto z = random_double(-1, 1); auto r = std::sqrt(1 - z*z); return Vec3(r*std::cos(a), r*std::sin(a), z); } Vec3 random_in_hemisphere(const Vec3& normal) { Vec3 in_unit_sphere = random_in_unit_sphere(); if (dot(in_unit_sphere, normal) > 0.0) return in_unit_sphere; else return -in_unit_sphere; } #endif // VEC3_H