snoopy/include/vec3.h

56 lines
1.6 KiB
C
Raw Normal View History

2020-06-01 02:49:50 +02:00
#pragma once
#include <iostream>
#include "math.h"
2020-06-07 00:01:46 +02:00
2020-06-01 02:49:50 +02:00
class vec3
{
public:
2023-02-19 22:30:48 +01:00
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();
2023-02-19 22:30:48 +01:00
static vec3 random_in_hemisphere(const vec3& normal);
2020-06-01 02:49:50 +02:00
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]; }
2023-02-19 22:30:48 +01:00
double length() const;
double length_squared() const;
2020-06-01 02:49:50 +02:00
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]; }
2023-02-19 22:30:48 +01:00
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:
2020-06-01 02:49:50 +02:00
double e[3];
};
// type aliases for vec3
using point3 = vec3; // 3D point