-
Notifications
You must be signed in to change notification settings - Fork 3
/
vector3.h
251 lines (187 loc) · 6.82 KB
/
vector3.h
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#ifndef _VECTOR3_H_
#define _VECTOR3_H_ 1
#include <cmath>
#include <iostream>
class Vector3
{
public:
Vector3() {}
Vector3(float e0, float e1, float e2);
Vector3(const Vector3 &v) { *this = v; }
float x() const { return e[0]; }
float y() const { return e[1]; }
float z() const { return e[2]; }
const Vector3& operator+() const;
Vector3 operator-() const;
float operator[](int i) const { return e[i]; }
float& operator[](int i) { return e[i]; }
/* could be precomputed during initialization */
float length() const;
float squaredLength() const;
void makeUnitVector();
void setX(float _x) { e[0] = _x; }
void setY(float _y) { e[1] = _y; }
void setZ(float _z) { e[2] = _z; }
float minComponent() const;
float maxComponent() const;
float maxAbsComponent() const;
float minAbsComponent() const;
int indexOfMinComponent() const;
int indexOfMaxComponent() const;
int indexOfMaxAbsComponent() const;
int indexOfMinAbsComponent() const;
friend bool operator==(const Vector3& v1, const Vector3& v2);
friend bool operator!=(const Vector3& v1, const Vector3& v2);
friend std::istream &operator>>(std::istream &is, Vector3& t);
friend std::ostream &operator<<(std::ostream &os, const Vector3& t);
friend Vector3 operator+(const Vector3& v1, const Vector3& v2);
friend Vector3 operator-(const Vector3& v1, const Vector3& v2);
friend Vector3 operator/(const Vector3& vec, float scalar);
friend Vector3 operator*(const Vector3& vec, float scalar);
friend Vector3 operator*(float scalar, const Vector3& vec);
Vector3& operator=(const Vector3& v2);
Vector3& operator+=(const Vector3& v2);
Vector3& operator-=(const Vector3& v2);
Vector3& operator*=(const float t);
Vector3& operator/=(const float t);
friend Vector3 unitVector(const Vector3& v);
friend Vector3 minVec(const Vector3& v1, const Vector3& v2);
friend Vector3 maxVec(const Vector3& v1, const Vector3& v2);
friend Vector3 cross(const Vector3& v1, const Vector3& v2);
friend float dot(const Vector3& v1, const Vector3& v2);
friend float tripleProduct(const Vector3& v1, const Vector3& v2, const Vector3& v3);
float e[3];
};
inline Vector3::Vector3(float e0, float e1, float e2)
{ e[0] = e0; e[1] = e1; e[2] = e2; }
inline const Vector3& Vector3::operator+() const
{ return *this; }
inline Vector3 Vector3::operator-() const
{ return Vector3(-e[0], -e[1], -e[2]); }
inline float Vector3::length() const
{ return sqrt(e[0]*e[0] + e[1]*e[1] + e[2]*e[2]); }
inline float Vector3::squaredLength() const
{ return e[0]*e[0] + e[1]*e[1] + e[2]*e[2]; }
inline void Vector3::makeUnitVector()
{ *this = *this / (*this).length(); }
/* ToDo: test ? operator, arithmetic solution */
inline float Vector3::minComponent() const {
float temp = e[0];
if (e[1] < temp) temp = e[1];
if (e[2] < temp) temp = e[2];
return temp;
}
inline float Vector3::maxComponent() const {
float temp = e[0];
if (e[1] > temp) temp = e[1];
if (e[2] > temp) temp = e[2];
return temp;
}
inline float Vector3::maxAbsComponent() const {
float temp = fabs(e[0]);
if (fabs(e[1]) > temp) temp = fabs(e[1]);
if (fabs(e[2]) > temp) temp = fabs(e[2]);
return temp;
}
inline float Vector3::minAbsComponent() const {
float temp = fabs(e[0]);
if (fabs(e[1]) < temp) temp = fabs(e[1]);
if (fabs(e[2]) < temp) temp = fabs(e[2]);
return temp;
}
inline int Vector3::indexOfMinComponent() const {
int index = 0;
float temp = e[0];
if (e[1] < temp) { temp = e[1]; index = 1;}
if (e[2] < temp) index = 2;
return index;
}
inline int Vector3::indexOfMinAbsComponent() const {
int index = 0;
float temp = fabs(e[0]);
if (fabs(e[1]) < temp) { temp = fabs(e[1]); index = 1;}
if (fabs(e[2]) < temp) index = 2;
return index;
}
inline bool operator==(const Vector3& v1, const Vector3& v2){
if (v1.e[0] != v2.e[0]) return false;
if (v1.e[1] != v2.e[1]) return false;
if (v1.e[2] != v2.e[2]) return false;
return true;
}
inline bool operator!=(const Vector3& v1, const Vector3& v2)
{ return !(v1 == v2); }
inline int Vector3::indexOfMaxComponent() const {
int index = 0;
float temp = e[0];
if (e[1] > temp) { temp = e[1]; index = 1;}
if (e[2] > temp) index = 2;
return index;
}
inline int Vector3::indexOfMaxAbsComponent() const {
int index = 0;
float temp = e[0];
if (fabs(e[1]) > temp) { temp = fabs(e[1]); index = 1;}
if (fabs(e[2]) > temp) index = 2;
return index;
}
inline Vector3 operator*(float scalar, const Vector3& vec)
{ return Vector3(vec.e[0]*scalar, vec.e[1]*scalar, vec.e[2]*scalar); }
inline Vector3 operator*(const Vector3& vec, float scalar)
{ return Vector3(vec.e[0]*scalar, vec.e[1]*scalar, vec.e[2]*scalar); }
inline Vector3 operator/(const Vector3& vec, float scalar)
{ return Vector3(vec.e[0]/scalar, vec.e[1]/scalar, vec.e[2]/scalar); }
inline Vector3 operator+(const Vector3& v1, const Vector3& v2)
{ return Vector3(v1.e[0]+v2.e[0], v1.e[1]+v2.e[1], v1.e[2]+v2.e[2]); }
inline Vector3 operator-(const Vector3& v1, const Vector3& v2)
{ return Vector3(v1.e[0]-v2.e[0], v1.e[1]-v2.e[1], v1.e[2]-v2.e[2]); }
inline Vector3& Vector3::operator+=(const Vector3& vec) {
*this = *this + vec;
return *this;
}
inline Vector3& Vector3::operator=(const Vector3& vec) {
e[0] = vec.e[0];
e[1] = vec.e[1];
e[2] = vec.e[2];
return *this;
}
inline Vector3& Vector3::operator-=(const Vector3& vec) {
*this = *this - vec;
return *this;
}
inline Vector3& Vector3::operator*=(float t) {
*this = *this * t;
return *this;
}
inline Vector3& Vector3::operator/=(float t) {
*this = *this / t;
return *this;
}
inline float dot(const Vector3& v1, const Vector3& v2)
{ return v1.x() * v2.x() + v1.y() * v2.y() + v1.z() * v2.z(); }
inline Vector3 cross(const Vector3& v1, const Vector3& v2) {
Vector3 temp;
temp.e[0] = v1.y() * v2.z() - v1.z() * v2.y();
temp.e[1] = v1.z() * v2.x() - v1.x() * v2.z();
temp.e[2] = v1.x() * v2.y() - v1.y() * v2.x();
return temp;
}
inline Vector3 unitVector(const Vector3& v) {
float length = v.length();
return v / length;
}
inline Vector3 minVec(const Vector3& v1, const Vector3& v2) {
Vector3 vec(v1);
if (v2.x() < v1.x()) vec.setX(v2.x());
if (v2.y() < v1.y()) vec.setY(v2.y());
if (v2.z() < v1.z()) vec.setZ(v2.z());
return vec;
}
inline Vector3 maxVec(const Vector3& v1, const Vector3& v2) {
Vector3 vec(v1);
if (v2.x() > v1.x()) vec.setX(v2.x());
if (v2.y() > v1.y()) vec.setY(v2.y());
if (v2.z() > v1.z()) vec.setZ(v2.z());
return vec;
}
#endif // _VECTOR3_H_