-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec3.cpp
178 lines (145 loc) · 3.1 KB
/
vec3.cpp
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <cmath>
#include "vec3.h"
#define PI 3.1415926536f
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
Vec3::Vec3(double x, double y, double z) {
this->x = x;
this->y = y;
this->z = z;
}
Vec3::Vec3(double c[3]) {
x = c[0];
y = c[1];
z = c[2];
}
Vec3::Vec3(const Vec3& v) {
x = v.x;
y = v.y;
z = v.z;
}
Vec3& Vec3::operator=(const Vec3& v) {
x = v.x;
y = v.y;
z = v.z;
return *this;
}
void Vec3::normalize() {
double l = length();
x /= l;
y /= l;
z /= l;
}
void Vec3::clamp() {
x = min(x, 1.0f);
y = min(y, 1.0f);
z = min(z, 1.0f);
x = max(x, .0f);
y = max(y, .0f);
z = max(z, .0f);
}
Vec3& Vec3::operator+=(const Vec3& v) {
x += v.x;
y += v.y;
z += v.z;
return *this;
}
Vec3& Vec3::operator-=(const Vec3& v) {
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
Vec3& Vec3::operator*=(const Vec3& v) {
x *= v.x;
y *= v.y;
z *= v.z;
return *this;
}
void Vec3::print() const {
printf("%.3f\t\t%.3f\t\t%.3f\n", x, y ,z);
}
bool operator<(const Vec3& v1, const Vec3& v2) {
return v1.x < v2.x && v1.y < v2.y && v1.z < v2.z;
}
bool operator>(const Vec3& v1, const Vec3& v2) {
return v1.x > v2.x && v1.y > v2.y && v1.z > v2.z;
}
Vec3 operator+(const Vec3& v1, const Vec3& v2) {
return Vec3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);
}
Vec3 operator-(const Vec3& v1, const Vec3& v2) {
return Vec3(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
}
Vec3 operator*(const Vec3& v1, const Vec3& v2) {
return Vec3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
}
Vec3 operator*(const Vec3& v1, double n) {
return Vec3(v1.x * n, v1.y * n, v1.z * n);
}
Vec3 operator/(const Vec3& v1, double n) {
return Vec3(v1.x / n, v1.y / n, v1.z / n);
}
double dot(const Vec3& v1, const Vec3& v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
Vec3 cross(const Vec3& v1, const Vec3& v2) {
return Vec3(v1.y * v2.z - v1.z * v2.y,
v1.z * v2.x - v1.x * v2.z,
v1.x * v2.y - v1.y * v2.x);
}
std::ostream& operator<<(std::ostream& os, const Vec3& v) {
os << v.x << '\t' << v.y << '\t' << v.z << std::endl;
return os;
};
Vec3 Vec3::randomize(double r) {
double rand1 = (double)rand() / (double)RAND_MAX;
double rand2 = (double)rand() / (double)RAND_MAX;
double X = sqrt(- 2 * log(rand1)) * cos(2 * M_PI * rand2) * r / 10;
double Y = sqrt(- 2 * log(rand1)) * sin(2 * M_PI * rand2) * r / 10;
// printf("%.3f\t%.3f\n", X, Y);
Vec3 u = *this;
u.x += 1; // create a vector not parallel to v
Vec3 e1 = cross(u, *this);
Vec3 e2 = cross(*this, e1);
e1.normalize();
e2.normalize();
Vec3 randVec = *this + e1 * X + e2 * Y;
randVec.normalize();
return randVec;
}
double& Vec3::operator[](int n)
{
switch(n)
{
default:
return x;
case 1:
return y;
case 2:
return z;
}
}
double Vec3::get(int n) const
{
switch(n)
{
default:
return x;
case 1:
return y;
case 2:
return z;
}
}
int Vec3::maxElement()
{
if (x > y)
{
return x > z ? 0 : 2;
}
else
{
return y > z ? 1 : 2;
}
}