-
Notifications
You must be signed in to change notification settings - Fork 6
/
threevector.hh
311 lines (266 loc) · 9.65 KB
/
threevector.hh
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
306
307
308
309
310
311
/*
ThreeVector class
Time-stamp: <threevector.cc on Saturday, 8 September, 2012 at 16:05:45 MST (philip)>
*/
#ifndef __THREEVECTOR_CC__
#define __THREEVECTOR_CC__
#include <iostream>
#include <iomanip>
#include <limits>
#include <cassert>
#include "promote_numeric.h"
template <class T>
class ThreeVector {
public:
T x, y, z;
// constructors:
inline ThreeVector<T>()
: x(static_cast<T>(0)),
y(static_cast<T>(0)),
z(static_cast<T>(0))
{}
template <class U>
inline ThreeVector<T>( const ThreeVector<U>& other )
: x(static_cast<T>(other.x)),
y(static_cast<T>(other.y)),
z(static_cast<T>(other.z))
{}
// want to be able to write: ThreeVector<double> x(2,2.5,4)
template <class U, class V, class W>
inline ThreeVector<T>( const U rhsx, const V rhsy, W const rhsz )
: x(static_cast<T>(rhsx)),
y(static_cast<T>(rhsy)),
z(static_cast<T>(rhsz))
{}
// broadcast a scalar
template <class U>
inline explicit ThreeVector<T>( const U& s)
: x(static_cast<T>(s)),
y(static_cast<T>(s)),
z(static_cast<T>(s))
{}
// allow access as a three-vector
inline T operator [] ( const size_t i ) const {
assert( i < 3 );
return *(&x+i);
}
inline T& operator [] ( const size_t i ) {
assert( i < 3 );
return *(&x+i);
}
// assignment
template <class U>
inline ThreeVector<T>& operator = ( const ThreeVector<U>& other ) {
x = static_cast<T>(other.x);
y = static_cast<T>(other.y);
z = static_cast<T>(other.z);
return *this;
}
//unary operations (sign)
inline const ThreeVector<T>& operator +() {
return *this;
}
inline ThreeVector<T> operator -() {
return ThreeVector<T>(-x, -y, -z);
}
template <class U>
inline ThreeVector<T>& operator += ( const ThreeVector<U>& rhs ) {
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
template <class U>
inline ThreeVector<T>& operator -= ( const ThreeVector<U>& rhs ) {
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
template <class U>
inline ThreeVector<T>& operator *= ( const ThreeVector<U>& rhs ) {
x *= rhs.x;
y *= rhs.y;
z *= rhs.z;
return *this;
}
template <class U>
inline ThreeVector<T>& operator *= ( const U rhs ) {
x *= rhs;
y *= rhs;
z *= rhs;
return *this;
}
template <class U>
inline ThreeVector<T>& operator /= ( const U rhs ) {
x /= rhs;
y /= rhs;
z /= rhs;
return *this;
}
inline typename PromoteNumeric<T, float>::type norm() const {
return sqrt( x * x + y * y + z * z );
}
inline typename PromoteNumeric<T, float>::type norm2() const {
return ( x * x + y * y + z * z );
}
inline T maxcomponent() const {
T maxxy = (x>y)?x:y;
return maxxy>z?maxxy:z;
}
inline T mincomponent() const {
T maxxy = (x<y)?x:y;
return maxxy<z?maxxy:z;
}
template<class U>
inline typename PromoteNumeric<T, U>::type dot(const ThreeVector<U>& rhs) const {
return x * rhs.x + y * rhs.y + z * rhs.z;
}
template <class U>
inline ThreeVector<typename PromoteNumeric<T, U>::type>
cross( const ThreeVector<U>& rhs ) const {
return ThreeVector<typename PromoteNumeric<T, U>::type>(
y * rhs.z - z * rhs.y,
z * rhs.x - x * rhs.z,
x * rhs.y - y * rhs.x);
}
// abs. diff. between *this and rhs is less than epsilon in each component
template <class U, class V>
inline int absclose( const ThreeVector<U>& rhs, const V epsilon ) const {
ThreeVector<typename PromoteNumeric<T, U>::type> diff;
diff = *this - rhs;
return
fabs(diff.x) < epsilon &&
fabs(diff.y) < epsilon &&
fabs(diff.z) < epsilon;
}
// rel. diff. between *this and rhs is less than epsilon in each component
template <class U, class V>
inline int relclose( const ThreeVector<U>& rhs, const V epsilon ) const {
ThreeVector<typename PromoteNumeric<T, U>::type> sum, diff;
sum.x = fabs(x) + fabs(rhs.x);
sum.y = fabs(y) + fabs(rhs.y);
sum.z = fabs(z) + fabs(rhs.z);
diff = *this - rhs;
return
( 2*fabs(diff.x) / sum.x ) < epsilon &&
( 2*fabs(diff.y) / sum.y ) < epsilon &&
( 2*fabs(diff.z) / sum.z ) < epsilon;
}
inline int is_finite() const {
return isfinite(x) && isfinite(y) && isfinite(z);
}
// relational operators
template <class U>
inline bool operator == ( const ThreeVector<U>& rhs ) const {
return ( x == rhs.x && y == rhs.y && z == rhs.z );
}
template <class U>
inline bool operator != ( const ThreeVector<U>& rhs ) const {
return ( x != rhs.x || y != rhs.y || z != rhs.z );
}
template <class U>
inline bool operator < ( const ThreeVector<U>& rhs ) const {
if( x < rhs.x && y < rhs.y && z < rhs.z ) return true;
return false;
}
template <class U>
inline bool operator <= ( const ThreeVector<U>& rhs ) const {
if( x <= rhs.x && y <= rhs.y && z <= rhs.z ) return true;
return false;
}
template <class U>
inline bool operator > ( const ThreeVector<U>& rhs ) const {
if( x > rhs.x && y > rhs.y && z > rhs.z ) return true;
return false;
}
template <class U>
inline bool operator >= ( const ThreeVector<U>& rhs ) const {
if( x >= rhs.x && y >= rhs.y && z >= rhs.z ) return true;
return false;
}
// stream operator: keep the format settings from being destroyed by the
// non-numeric characters output
inline friend std::ostream& operator <<( std::ostream& o, const ThreeVector<T>& v ) {
std::streamsize tmpw = o.width();
std::streamsize tmpp = o.precision();
char tmps = o.fill();
std::ios::fmtflags tmpf = o.flags(); // format flags like "scientific" and "left" and "showpoint"
o << std::setw(1);
o << "(";
o.flags(tmpf); o << std::setfill(tmps) << std::setprecision(tmpp) << std::setw(tmpw);
o << v.x;
o << ",";
o.flags(tmpf); o << std::setfill(tmps) << std::setprecision(tmpp) << std::setw(tmpw);
o << v.y;
o << ",";
o.flags(tmpf); o << std::setfill(tmps) << std::setprecision(tmpp) << std::setw(tmpw);
o << v.z;
o << ")";
return o;
}
inline ThreeVector<T> zero() {
x = 0;
y = 0;
z = 0;
return *this;
}
// return true if all components are on [a,b]
template <class U, class V>
inline int inrange(U low, V hi) {
return (x >= low && x <= hi) && (y >= low && y <= hi) && (z >= low && z <= hi);
}
};
// componentwise addition and subtraction
template <class T, class U>
inline ThreeVector<typename PromoteNumeric<T, U>::type>
operator + (const ThreeVector<T>& lhs, const ThreeVector<U>& rhs ) {
return ThreeVector<typename PromoteNumeric<T, U>::type> (
lhs.x + rhs.x,
lhs.y + rhs.y,
lhs.z + rhs.z
);
}
template <class T, class U>
inline ThreeVector<typename PromoteNumeric<T, U>::type>
operator - (const ThreeVector<T>& lhs, const ThreeVector<U>& rhs ) {
return ThreeVector<typename PromoteNumeric<T, U>::type> (
lhs.x - rhs.x,
lhs.y - rhs.y,
lhs.z - rhs.z
);
}
// left and right multiplication by a scalar
template <class T, class U>
inline ThreeVector<typename PromoteNumeric<T, U>::type>
operator * ( const ThreeVector<T>& lhs, const U rhs ) {
return ThreeVector<typename PromoteNumeric<T, U>::type> (
lhs.x * rhs,
lhs.y * rhs,
lhs.z * rhs
);
}
template <class T, class U>
inline ThreeVector<typename PromoteNumeric<T, U>::type>
operator * ( const T lhs, const ThreeVector<U>& rhs ) {
return ThreeVector<typename PromoteNumeric<T, U>::type> (
lhs * rhs.x,
lhs * rhs.y,
lhs * rhs.z
);
}
// right division by a scalar
template <class T, class U>
inline ThreeVector<typename PromoteNumeric<T, U>::type>
operator / ( const ThreeVector<T>& lhs, const U rhs ) {
return ThreeVector<typename PromoteNumeric<T, U>::type> (
lhs.x / rhs,
lhs.y / rhs,
lhs.z / rhs
);
}
// three most common cases
typedef ThreeVector<double> double3;
typedef ThreeVector<float> float3;
typedef ThreeVector<int> integer3;
#endif // __THREEVECTOR_CC__