-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.h
299 lines (280 loc) · 5.92 KB
/
Vector.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>
#include "GLIncludes.h"
template <class T>
class Point2;
template <class T>
class Vector2
{
inline friend Vector2<T> operator*(T scalar, const Vector2<T> &vec)
{
return vec * scalar;
}
friend inline std::ostream &operator<<(std::ostream &stream, const Vector2<T> &vec)
{
return (stream << "(" << vec.x << ", " << vec.y << ")");
}
private:
T x, y;
public:
Vector2(T _x = 0, T _y = 0) : x(_x), y(_y)
{
}
Vector2(const Vector2<T> &v) : x(v.x), y(v.y)
{
}
void set(const Vector2<T> &v)
{
set(v.x, v.y);
}
void set(T _x, T _y)
{
x = _x;
y = _y;
}
Vector2<T> operator*(T scalar) const
{
return Vector2<T>(x * scalar, y * scalar);
}
Vector2<T> &operator*=(T scalar)
{
return (*this = *this * scalar);
}
T operator*(const Vector2<T> &v) const
{
return dot(v);
}
Vector2<T> &operator*=(const Vector2<T> &v)
{
return (*this = dot(v));
}
Vector2<T> operator/(T scalar) const
{
return *this * (1.0 / scalar);
}
Vector2<T> operator+(const Vector2<T> &v) const
{
return Vector2<T>(x + v.x, y + v.y);
}
Point2<T> operator+(const Point2<T> &p) const
{
return Point2<T>(p.x + x, p.y + y);
}
Vector2<T> operator+=(const Vector2<T> &v)
{
return (*this = *this + v);
}
Vector2<T> operator-(const Vector2<T> &v) const
{
return Vector2<T>(x - v.x, y - v.y);
}
Vector2<T> operator-=(const Vector2<T> &v)
{
return (*this = *this - v);
}
Vector2<T> operator-() const
{
return Vector2<T>(-x, -y);
}
T dot(const Vector2<T> &) const;
T length() const;
Vector2<T> normal() const;
void normalize()
{
*this = normal();
}
Vector2<T> &operator=(const Vector2<T> &v)
{
set(v);
return *this;
}
bool operator==(const Vector2<T> &v) const
{
return (x == v.x && y == v.y);
}
bool operator!=(const Vector2<T> &v) const
{
return !(*this == v);
}
bool operator!() const
{
return (x == 0 && y == 0);
}
T getX() const
{
return x;
}
T getY() const
{
return y;
}
void setX(T val)
{
x = val;
}
void setY(T val)
{
y = val;
}
operator Point2<T>() const
{
return Point2<T>(x, y);
}
};
typedef Vector2<GLfloat> Vector2f;
typedef Vector2<GLint> Vector2i;
typedef Vector2<GLdouble> Vector2d;
typedef Vector2<GLshort> Vector2s;
template <class T>
class Point3;
class Quaternion;
template <class T>
class Vector3
{
inline friend Vector3<T> operator*(T scalar, const Vector3<T> &vec)
{
return vec * scalar;
}
friend inline std::ostream &operator<<(std::ostream &stream, const Vector3<T> &vec)
{
return (stream << "(" << vec.x << ", " << vec.y << ", " << vec.z << ")");
}
private:
T x, y, z;
public:
Vector3(T _x = 0, T _y = 0, T _z = 0) : x(_x), y(_y), z(_z)
{
}
Vector3(const Vector3<T> &v) : x(v.x), y(v.y), z(v.z)
{
}
void set(const Vector3<T> &v)
{
set(v.getX(), v.getY(), v.getZ());
}
void set(T _x, T _y, T _z)
{
x = _x;
y = _y;
z = _z;
}
Vector3<T> operator*(T scalar) const
{
return Vector3<T>(x * scalar, y * scalar, z * scalar);
}
Vector3<T> &operator*=(T scalar)
{
return (*this = *this * scalar);
}
T operator*(const Vector3<T> &v) const
{
return dot(v);
}
Vector3<T> &operator*=(const Vector3<T> &v)
{
return (*this = dot(v));
}
Vector3<T> operator/(T scalar) const
{
return *this * (1.0 / scalar);
}
Vector3<T> operator+(const Vector3<T> &v) const
{
return Vector3<T>(x + v.x, y + v.y, z + v.z);
}
Point3<T> operator+(const Point3<T> &p) const
{
return Point3<T>(p.getX() + x, p.getY() + y, p.getZ() + z);
}
Vector3<T> operator+=(const Vector3<T> &v)
{
return (*this = *this + v);
}
Vector3<T> operator-(const Vector3<T> &v) const
{
return Vector3<T>(x - v.x, y - v.y, z - v.z);
}
Vector3<T> operator-=(const Vector3<T> &v)
{
return (*this = *this - v);
}
Vector3<T> operator-() const
{
return Vector3<T>(-x, -y, -z);
}
T dot(const Vector3<T> &) const;
Vector3<T> cross(const Vector3<T> &v) const
{
return Vector3<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
T length() const;
Vector3<T> normal() const;
void normalize()
{
*this = normal();
}
Vector3<T> &operator=(const Vector3<T> &v)
{
set(v);
return *this;
}
bool operator==(const Vector3<T> &v) const
{
return (x == v.x && y == v.y && z == v.z);
}
bool operator!=(const Vector3<T> &v) const
{
return !(*this == v);
}
T getX() const
{
return x;
}
T getY() const
{
return y;
}
T getZ() const
{
return z;
}
void setX(T val)
{
x = val;
}
void setY(T val)
{
y = val;
}
void setZ(T val)
{
z = val;
}
operator Point3<T>() const
{
return Point3<T>(x, y, z);
}
inline operator Quaternion() const;
};
typedef Vector3<GLfloat> Vector3f;
typedef Vector3<GLint> Vector3i;
typedef Vector3<GLdouble> Vector3d;
typedef Vector3<GLshort> Vector3s;
inline void normal3f(const Vector3f &n)
{
glNormal3f(n.getX(), n.getY(), n.getZ());
}
inline void normal3i(const Vector3i &n)
{
glNormal3i(n.getX(), n.getY(), n.getZ());
}
inline void normal3d(const Vector3d &n)
{
glNormal3d(n.getX(), n.getY(), n.getZ());
}
inline void normal3s(const Vector3s &n)
{
glNormal3s(n.getX(), n.getY(), n.getZ());
}
#endif