-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec.hpp
111 lines (87 loc) · 2.45 KB
/
Vec.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Created by Tizian Rettig on 11.03.2021.
//
#ifndef RAYTRACER_VEC_HPP
#define RAYTRACER_VEC_HPP
#include <cmath>
#include <string>
/** Simple Vector class to avoid complex libraries */
class Vec {
public:
float m_x, m_y, m_z;
Vec(): m_x(-100000), m_y(-100000), m_z(-100000){}
Vec(float x, float y, float z): m_x(x), m_y(y), m_z(z){}
~Vec() = default;
[[nodiscard]] std::string toString() const {
return "(" + std::to_string(m_x) + "," + std::to_string(m_y) + "," + std::to_string(m_z) + ")";
}
};
[[nodiscard]] inline Vec scalarMultiply(const Vec &a, const float& x) {
return {
a.m_x * x,
a.m_y * x,
a.m_z * x
};
}
[[nodiscard]] inline float dot(const Vec &a, const Vec &b) {
return a.m_x * b.m_x + a.m_y * b.m_y + a.m_z * b.m_z;
}
[[nodiscard]] inline Vec cross(const Vec &a, const Vec &b) {
return {
a.m_y * b.m_z - a.m_z * b.m_y,
a.m_z * b.m_x - a.m_x * b.m_z,
a.m_x * b.m_y - a.m_y * b.m_x
};
}
[[nodiscard]] inline float length(const Vec &a) {
float l2 = a.m_x * a.m_x + a.m_y * a.m_y + a.m_z * a.m_z;
return (l2 != 1) ? sqrt(l2): 1;
}
[[nodiscard]] inline float length2(const Vec &a) {
return a.m_x * a.m_x + a.m_y * a.m_y + a.m_z * a.m_z;
}
[[nodiscard]] inline Vec normalize(const Vec &a){
float l = length(a);
return (l != 1.f) ? Vec(
a.m_x / l,
a.m_y / l,
a.m_z / l
) : a;
}
[[nodiscard]] inline Vec inverse(const Vec &a) {
return {
- a.m_x,
- a.m_y,
- a.m_z
};
}
[[nodiscard]] inline float angleTo(const Vec &a, const Vec &b) {
return acos(dot(a, b));
}
[[nodiscard]] inline Vec operator+ (const Vec &a, const Vec &b) {
return {
a.m_x + b.m_x,
a.m_y + b.m_y,
a.m_z + b.m_z
};
}
[[nodiscard]] inline Vec operator- (const Vec &a, const Vec &b) {
return {
a.m_x - b.m_x,
a.m_y - b.m_y,
a.m_z - b.m_z
};
}
[[nodiscard]] inline Vec operator* (const float& a, const Vec &b) {
return scalarMultiply(b, a);
}
[[nodiscard]] inline Vec operator* (const Vec &a, const float& b) {
return scalarMultiply(a, b);
}
[[nodiscard]] inline Vec operator/ (const float& a, const Vec &b) {
return scalarMultiply(b, 1 / a);
}
[[nodiscard]] inline Vec operator/ (const Vec &a, const float& b) {
return scalarMultiply(a, 1 / b);
}
#endif //RAYTRACER_VEC_HPP