-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec3D.h
305 lines (279 loc) · 9.46 KB
/
Vec3D.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
300
301
302
303
304
305
// --------------------------------------------------------------------------
// gMini,
// a minimal Glut/OpenGL app to extend
//
// Copyright(C) 2007-2009
// Tamy Boubekeur
//
// All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
// for more details.
//
// --------------------------------------------------------------------------
#pragma once
#include <cmath>
#include <iostream>
template<typename T> class Vec3D;
template <class T> bool operator!= (const Vec3D<T> & p1, const Vec3D<T> & p2) {
return (p1[0] != p2[0] || p1[1] != p2[1] || p1[2] != p2[2]);
}
template <class T> const Vec3D<T> operator* (const Vec3D<T> & p, float factor) {
return Vec3D<T> (p[0] * factor, p[1] * factor, p[2] * factor);
}
template <class T> const Vec3D<T> operator* (float factor, const Vec3D<T> & p) {
return Vec3D<T> (p[0] * factor, p[1] * factor, p[2] * factor);
}
template <class T> const Vec3D<T> operator* (const Vec3D<T> & p1, const Vec3D<T> & p2) {
return Vec3D<T> (p1[0] * p2[0], p1[1] * p2[1], p1[2] * p2[2]);
}
template <class T> const Vec3D<T> operator+ (const Vec3D<T> & p1, const Vec3D<T> & p2) {
return Vec3D<T> (p1[0] + p2[0], p1[1] + p2[1], p1[2] + p2[2]);
}
template <class T> const Vec3D<T> operator- (const Vec3D<T> & p1, const Vec3D<T> & p2) {
return Vec3D<T> (p1[0] - p2[0], p1[1] - p2[1], p1[2] - p2[2]);
}
template <class T> const Vec3D<T> operator- (const Vec3D<T> & p) {
return Vec3D<T> (-p[0], -p[1], -p[2]);
}
template <class T> const Vec3D<T> operator/ (const Vec3D<T> & p, float divisor) {
return Vec3D<T> (p[0]/divisor, p[1]/divisor, p[2]/divisor);
}
template <class T> bool operator== (const Vec3D<T> & p1, const Vec3D<T> & p2) {
return (p1[0] == p2[0] && p1[1] == p2[1] && p1[2] == p2[2]);
}
template <class T> bool operator< (const Vec3D<T> & a, const Vec3D<T> & b) {
return (a[0] < b[0] && a[1] < b[1] && a[2] < b[2]);
}
template <class T> bool operator>= (const Vec3D<T> & a, const Vec3D<T> & b) {
return (a[0] >= b[0] || a[1] >= b[1] || a[2] >= b[2]);
}
/**
* Vector in 3 dimensions, with basics operators overloaded.
*/
template <typename T>
class Vec3D {
public:
inline Vec3D (void) {
p[0] = p[1] = p[2] = T ();
}
inline Vec3D (T p0, T p1, T p2) {
p[0] = p0;
p[1] = p1;
p[2] = p2;
};
inline Vec3D (const Vec3D & v) {
init (v[0], v[1], v[2]);
}
inline Vec3D (T* pp) {
p[0] = pp[0];
p[1] = pp[1];
p[2] = pp[2];
};
// ---------
// Operators
// ---------
inline T& operator[] (int Index) {
return (p[Index]);
};
inline const T& operator[] (int Index) const {
return (p[Index]);
};
inline Vec3D& operator= (const Vec3D & P) {
p[0] = P[0];
p[1] = P[1];
p[2] = P[2];
return (*this);
};
inline Vec3D& operator+= (const Vec3D & P) {
p[0] += P[0];
p[1] += P[1];
p[2] += P[2];
return (*this);
};
inline Vec3D& operator-= (const Vec3D & P) {
p[0] -= P[0];
p[1] -= P[1];
p[2] -= P[2];
return (*this);
};
inline Vec3D& operator*= (const Vec3D & P) {
p[0] *= P[0];
p[1] *= P[1];
p[2] *= P[2];
return (*this);
};
inline Vec3D& operator*= (T s) {
p[0] *= s;
p[1] *= s;
p[2] *= s;
return (*this);
};
inline Vec3D& operator/= (const Vec3D & P) {
p[0] /= P[0];
p[1] /= P[1];
p[2] /= P[2];
return (*this);
};
inline Vec3D& operator/= (T s) {
p[0] /= s;
p[1] /= s;
p[2] /= s;
return (*this);
};
//---------------------------------------------------------------
inline Vec3D & init (T x, T y, T z) {
p[0] = x;
p[1] = y;
p[2] = z;
return (*this);
};
inline T getSquaredLength() const {
return (dotProduct (*this, *this));
};
inline T getLength() const {
return (T)sqrt (getSquaredLength());
};
/// Return length after normalization
inline T normalize (void) {
T length = getLength();
if (length == 0.0f)
return 0;
T rezLength = 1.0f / length;
p[0] *= rezLength;
p[1] *= rezLength;
p[2] *= rezLength;
return length;
};
inline void fromTo (const Vec3D & P1, const Vec3D & P2) {
p[0] = P2[0] - P1[0];
p[1] = P2[1] - P1[1];
p[2] = P2[2] - P1[2];
};
inline float transProduct (const Vec3D & v) const {
return (p[0]*v[0] + p[1]*v[1] + p[2]*v[2]);
}
inline void getTwoOrthogonals (Vec3D & u, Vec3D & v) const {
if (fabs(p[0]) < fabs(p[1])) {
if (fabs(p[0]) < fabs(p[2]))
u = Vec3D (0, -p[2], p[1]);
else
u = Vec3D (-p[1], p[0], 0);
} else {
if (fabs(p[1]) < fabs(p[2]))
u = Vec3D (p[2], 0, -p[0]);
else
u = Vec3D(-p[1], p[0], 0);
}
v = crossProduct (*this, u);
}
inline Vec3D projectOn (const Vec3D & N, const Vec3D & P) const {
T w = dotProduct (((*this) - P), N);
return (*this) - (N * w);
}
static inline Vec3D segment (const Vec3D & a, const Vec3D & b) {
Vec3D r;
r[0] = b[0] - a[0];
r[1] = b[1] - a[1];
r[2] = b[2] - a[2];
return r;
};
static inline Vec3D crossProduct(const Vec3D & a, const Vec3D & b) {
Vec3D result;
result[0] = a[1] * b[2] - a[2] * b[1];
result[1] = a[2] * b[0] - a[0] * b[2];
result[2] = a[0] * b[1] - a[1] * b[0];
return(result);
}
static inline T dotProduct(const Vec3D & a, const Vec3D & b) {
return (a[0] * b[0] + a[1] * b[1] + a[2] * b[2]);
}
static inline T squaredDistance (const Vec3D &v1, const Vec3D &v2) {
Vec3D tmp = v1 - v2;
return (tmp.getSquaredLength());
}
static inline T distance (const Vec3D &v1, const Vec3D &v2) {
Vec3D tmp = v1 - v2;
return (tmp.getLength());
}
static inline Vec3D interpolate (const Vec3D & u, const Vec3D & v, T alpha) {
return (u * (1.0f - alpha) + v * alpha);
}
// cartesion to polar coordinates
// result:
// [0] = length
// [1] = angle with z-axis
// [2] = angle of projection into x,y, plane with x-axis
static inline Vec3D cartesianToPolar (const Vec3D &v) {
Vec3D polar;
polar[0] = v.getLength();
if (v[2] > 0.0f)
polar[1] = (T) atan (sqrt (v[0] * v[0] + v[1] * v[1]) / v[2]);
else if (v[2] < 0.0f)
polar[1] = (T) atan (sqrt (v[0] * v[0] + v[1] * v[1]) / v[2]) + M_PI;
else
polar[1] = M_PI * 0.5f;
if (v[0] > 0.0f)
polar[2] = (T) atan (v[1] / v[0]);
else if (v[0] < 0.0f)
polar[2] = (T) atan (v[1] / v[0]) + M_PI;
else if (v[1] > 0)
polar[2] = M_PI * 0.5f;
else
polar[2] = -M_PI * 0.5;
return polar;
}
// polar to cartesion coordinates
// input:
// [0] = length
// [1] = angle with z-axis
// [2] = angle of projection into x,y, plane with x-axis
static inline Vec3D polarToCartesian (const Vec3D & v) {
Vec3D cart;
cart[0] = v[0] * (T) sin (v[1]) * (T) cos (v[2]);
cart[1] = v[0] * (T) sin (v[1]) * (T) sin (v[2]);
cart[2] = v[0] * (T) cos (v[1]);
return cart;
}
static inline Vec3D projectOntoVector (const Vec3D & v1, const Vec3D & v2) {
return v2 * dotProduct (v1, v2);
}
inline Vec3D transformIn (const Vec3D & pos, const Vec3D & n, const Vec3D & u, const Vec3D & v) const {
Vec3D q = (*this) - pos;
return Vec3D (u[0]*q[0] + u[1]*q[1] + u[2]*q[2],
v[0]*q[0] + v[1]*q[1] + v[2]*q[2],
n[0]*q[0] + n[1]*q[1] + n[2]*q[2]);
}
protected:
T p[3];
};
template <class T> inline Vec3D<T> swap (Vec3D<T> & P, Vec3D<T> & Q) {
Vec3D<T> tmp = P;
P = Q;
Q = tmp;
}
template <class T> std::ostream & operator<< (std::ostream & output, const Vec3D<T> & v) {
output << v[0] << " " << v[1] << " " << v[2];
return output;
}
template <class T> std::istream & operator>> (std::istream & input, Vec3D<T> & v) {
input >> v[0] >> v[1] >> v[2];
return input;
}
typedef Vec3D<float> Vec3Df;
typedef Vec3D<double> Vec3Dd;
typedef Vec3D<int> Vec3Di;
// Some Emacs-Hints -- please don't remove:
//
// Local Variables:
// mode:C++
// tab-width:4
// End: