Skip to content

Commit 020c9f2

Browse files
author
ThomasGeorge
committed
added 3D vector class
1 parent bbdca68 commit 020c9f2

File tree

5 files changed

+174
-5
lines changed

5 files changed

+174
-5
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
.vscode

Makefile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
SRC_DIR ?= src
2+
HEADER_DIR ?= include
3+
4+
SOURCES := $(shell find src -name "*.cpp" -or -name "*.cc")
5+
OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
6+
INCLUDES := $(shell find include -type d | sed s/^/-I/)
7+
8+
CPPC := g++
9+
CPPFLAGS := -std=c++17 -g -Wall -Werror $(INCLUDES)
10+
11+
tracer: $(OBJECTS)
12+
$(CPPC) -o $@ $^ $(CPPFLAGS)
13+
14+
%.o: %.cpp
15+
$(CPPC) $< -o $@ $(CPPFLAGS) -c
16+
17+
.PHONY: clean
18+
19+
clean:
20+
find . -type f -name '*.o' -delete
21+
rm tracer

include/Vec3.hpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
#include <array>
3+
4+
class Vec3
5+
{
6+
std::array<double, 3> c;
7+
8+
public:
9+
Vec3();
10+
Vec3(double c1, double c2, double c3);
11+
~Vec3() = default;
12+
13+
double x() const;
14+
double y() const;
15+
double z() const;
16+
17+
double operator[](int i) const;
18+
double &operator[](int i);
19+
20+
Vec3 &operator+=(const Vec3 &otherV);
21+
Vec3 &operator*=(double s);
22+
Vec3 &operator/=(double s);
23+
Vec3 operator-() const;
24+
25+
double len() const;
26+
void formatColor(std::ostream &out) const;
27+
28+
inline Vec3 operator+(const Vec3 &otherV) const;
29+
inline Vec3 operator-(const Vec3 &otherV) const;
30+
inline Vec3 operator*(const Vec3 &otherV) const;
31+
inline Vec3 operator*(double s) const;
32+
inline Vec3 operator/(double s) const;
33+
inline Vec3 getUnitVector() const;
34+
35+
inline double o(const Vec3 &otherV) const;
36+
inline Vec3 x(const Vec3 &otherV) const;
37+
};
38+
39+
inline std::ostream &operator<<(std::ostream &out, const Vec3 &v);
40+
inline Vec3 operator*(double s, const Vec3 &v);

src/Vec3.cpp

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include <Vec3.hpp>
2+
3+
#include <math.h>
4+
#include <iostream>
5+
6+
Vec3::Vec3() : c{0.0, 0.0, 0.0} {}
7+
8+
Vec3::Vec3(double c1, double c2, double c3) : c{c1, c2, c3} {}
9+
10+
double Vec3::x() const { return c[0]; }
11+
12+
double Vec3::y() const { return c[1]; }
13+
14+
double Vec3::z() const { return c[2]; }
15+
16+
double Vec3::operator[](int i) const { return c.at(i); }
17+
18+
double &Vec3::operator[](int i) { return c.at(i); }
19+
20+
Vec3 &Vec3::operator+=(const Vec3 &otherV)
21+
{
22+
c[0] += otherV.x();
23+
c[1] += otherV.y();
24+
c[2] += otherV.z();
25+
return *this;
26+
}
27+
28+
Vec3 &Vec3::operator*=(double s)
29+
{
30+
c[0] *= s;
31+
c[1] *= s;
32+
c[2] *= s;
33+
return *this;
34+
}
35+
36+
Vec3 &Vec3::operator/=(double s)
37+
{
38+
c[0] /= s;
39+
c[1] /= s;
40+
c[2] /= s;
41+
return *this;
42+
}
43+
44+
Vec3 Vec3::operator-() const { return Vec3(-c[0], -c[1], -c[2]); }
45+
46+
double Vec3::len() const { return sqrt(c[0] * c[0] + c[1] * c[1] + c[2] * c[2]); }
47+
48+
void Vec3::formatColor(std::ostream &out) const
49+
{
50+
out << static_cast<int>(255.999 * c[0]) << ' '
51+
<< static_cast<int>(255.999 * c[1]) << ' '
52+
<< static_cast<int>(255.999 * c[2]) << '\n';
53+
}
54+
55+
inline Vec3 Vec3::operator+(const Vec3 &otherV) const
56+
{
57+
return Vec3(c[0] + otherV.x(), c[1] + otherV.y(), c[2] + otherV.z());
58+
}
59+
60+
inline Vec3 Vec3::operator-(const Vec3 &otherV) const
61+
{
62+
return Vec3(c[0] - otherV.x(), c[1] - otherV.y(), c[2] - otherV.z());
63+
}
64+
65+
inline Vec3 Vec3::operator*(const Vec3 &otherV) const
66+
{
67+
return Vec3(c[0] * otherV.x(), c[1] * otherV.y(), c[2] * otherV.z());
68+
}
69+
70+
inline Vec3 Vec3::operator*(double s) const
71+
{
72+
return Vec3(c[0] * s, c[1] * s, c[2] * s);
73+
}
74+
75+
inline Vec3 Vec3::operator/(double s) const
76+
{
77+
return Vec3(c[0] / s, c[1] / s, c[2] / s);
78+
}
79+
80+
inline Vec3 Vec3::getUnitVector() const { return *this / len(); }
81+
82+
inline double Vec3::o(const Vec3 &otherV) const
83+
{
84+
return c[0] * otherV.x() + c[1] * otherV.y() + c[2] * otherV.z();
85+
}
86+
87+
inline Vec3 Vec3::x(const Vec3 &otherV) const
88+
{
89+
return Vec3(c[1] * otherV.z() - c[2] * otherV.y(),
90+
c[2] * otherV.x() - c[0] * otherV.z(),
91+
c[0] * otherV.y() - c[1] * otherV.x());
92+
}
93+
94+
inline std::ostream &operator<<(std::ostream &out, const Vec3 &v)
95+
{
96+
return out << v.x() << ' ' << v.y() << ' ' << v.z();
97+
}
98+
99+
inline Vec3 operator*(double s, const Vec3 &v)
100+
{
101+
return v * s;
102+
}

tracer.cpp src/main.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
#include <iostream>
22
#include <cstdint>
33

4+
#include <Vec3.hpp>
5+
46
void outputPPM(const uint16_t width, const uint16_t height)
57
{
6-
int r, g, b;
8+
Vec3 colorV;
79
std::cout << "P3\n"
810
<< width << ' ' << height << "\n255\n";
911
for (int32_t i = height - 1; i >= 0; --i)
1012
{
13+
std::cerr << "\rScanlines remaining: " << i << ' ' << std::flush;
1114
for (int32_t j = 0; j < width; ++j)
1215
{
13-
r = static_cast<int>((255.99 * j) / width);
14-
g = static_cast<int>((255.99 * i) / height);
15-
b = static_cast<int>(51.198);
16+
colorV[0] = static_cast<double>(j) / width;
17+
colorV[1] = static_cast<double>(i) / height;
18+
colorV[2] = .2;
1619

17-
std::cout << r << ' ' << g << ' ' << b << "\n";
20+
colorV.formatColor(std::cout);
1821
}
1922
}
23+
std::cerr << std::endl;
2024
}
2125

2226
int main()

0 commit comments

Comments
 (0)