raytracer
Parallel Ray Tracer written from scratch (no graphics API).
Loading...
Searching...
No Matches
vec3.h
Go to the documentation of this file.
1#pragma once
2#include <iostream>
3#include <cmath>
4
5namespace raytracer::common
6{
10struct Vec3
11{
12 double x;
13 double y;
14 double z;
15
16 constexpr Vec3(const double _x, const double _y, const double _z) : x(_x), y(_y), z(_z)
17 {
18 }
19
23 static const Vec3 ZERO;
24
29 void normalize()
30 {
31 const double denominator = length();
32 x /= denominator;
33 y /= denominator;
34 z /= denominator;
35 }
36
42 [[nodiscard]] Vec3 to_normalized() const
43 {
44 const double denominator = length();
45 return {x / denominator, y / denominator, z / denominator};
46 }
47
53 [[nodiscard]] double length() const
54 {
55 return std::sqrt(x * x + y * y + z * z);
56 }
57
64 [[nodiscard]] constexpr Vec3 cross(Vec3 const &other) const
65 {
66 return {y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x};
67 }
68
69 constexpr bool operator==(Vec3 const &other) const
70 {
71 return x == other.x && y == other.y && z == other.z;
72 }
73
74 constexpr Vec3 operator+(Vec3 const &other) const
75 {
76 return {x + other.x, y + other.y, z + other.z};
77 }
78
79 constexpr void operator+=(Vec3 const &other)
80 {
81 x += other.x;
82 y += other.y;
83 z += other.z;
84 }
85
86 constexpr Vec3 operator-(Vec3 const &other) const
87 {
88 return {x - other.x, y - other.y, z - other.z};
89 }
90
91 constexpr Vec3 operator-() const
92 {
93 return {-x, -y, -z};
94 }
95
96 constexpr double operator*(Vec3 const &other) const
97 {
98 return x * other.x + y * other.y + z * other.z;
99 }
100
101 constexpr Vec3 operator*(const double d) const
102 {
103 return {x * d, y * d, z * d};
104 }
105
106 constexpr Vec3 operator/(const double d) const
107 {
108 return {x / d, y / d, z / d};
109 }
110};
111
112inline std::ostream &operator<<(std::ostream &os, Vec3 const &v)
113{
114 return os << "Vec3(" << v.x << ", " << v.y << ", " << v.z << ")";
115}
116
117inline constexpr Vec3 Vec3::ZERO = {0, 0, 0};
118} // namespace raytracer::common
Definition raytracer.cpp:24
std::ostream & operator<<(std::ostream &os, Vec3 const &v)
Definition vec3.h:112
Definition vec3.h:11
constexpr Vec3 operator/(const double d) const
Definition vec3.h:106
double y
Definition vec3.h:13
constexpr Vec3 operator-(Vec3 const &other) const
Definition vec3.h:86
double length() const
Definition vec3.h:53
constexpr Vec3 cross(Vec3 const &other) const
Definition vec3.h:64
constexpr double operator*(Vec3 const &other) const
Definition vec3.h:96
constexpr Vec3 operator+(Vec3 const &other) const
Definition vec3.h:74
constexpr void operator+=(Vec3 const &other)
Definition vec3.h:79
Vec3 to_normalized() const
Definition vec3.h:42
double z
Definition vec3.h:14
constexpr Vec3 operator-() const
Definition vec3.h:91
constexpr Vec3 operator*(const double d) const
Definition vec3.h:101
double x
Definition vec3.h:12
constexpr Vec3(const double _x, const double _y, const double _z)
Definition vec3.h:16
static const Vec3 ZERO
Definition vec3.h:23
constexpr bool operator==(Vec3 const &other) const
Definition vec3.h:69
void normalize()
Definition vec3.h:29