snoopy/include/vec3.h

57 lines
1.7 KiB
C++
Executable File

#pragma once
#include <iostream>
#include "math.h"
class vec3
{
public:
static vec3 random();
static vec3 random(double min, double max);
static vec3 random_in_unit_disk();
static vec3 random_unit_vector();
static vec3 random_in_unit_sphere();
static vec3 random_in_hemisphere(const vec3& normal);
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]; }
double length() const;
double length_squared() const;
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);
vec3& operator*=(const double t);
vec3& operator/=(const double t);
friend std::ostream& operator<<(std::ostream &out, const vec3 &v);
friend vec3 operator+(const vec3 &u, const vec3 &v);
friend vec3 operator-(const vec3 &u, const vec3 &v);
friend vec3 operator*(const vec3 &u, const vec3 &v);
friend vec3 operator*(double t, const vec3 &v);
friend vec3 operator*(const vec3 &v, double t);
friend vec3 operator/(vec3 v, double t);
friend vec3 lerp(const vec3 &a, const vec3 &b, double t);
friend vec3 reflect(const vec3& v, const vec3& n);
friend vec3 refract(const vec3& uv, const vec3& n, double etai_over_etat);
friend double dot(const vec3 &u, const vec3 &v);
friend vec3 cross(const vec3 &u, const vec3 &v);
friend vec3 normalize(vec3 v);
private:
double e[3];
};
// type aliases for vec3
using point3 = vec3; // 3D point
using colour = vec3; // RGB colour