-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec3.h
83 lines (78 loc) · 2.46 KB
/
Vec3.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
#ifndef VEC3_H
#define VEC3_H
#include <cmath>
#include <iostream>
class Vec3 {
private:
double mVals[3];
public:
Vec3() {}
Vec3( double x , double y , double z ) {
mVals[0] = x; mVals[1] = y; mVals[2] = z;
}
template< class point_t >
Vec3(point_t const & p) {
mVals[0] = p[0]; mVals[1] = p[1]; mVals[2] = p[2];
}
double & operator [] (unsigned int c) { return mVals[c]; }
double operator [] (unsigned int c) const { return mVals[c]; }
void operator = (Vec3 const & other) {
mVals[0] = other[0] ; mVals[1] = other[1]; mVals[2] = other[2];
}
double squareLength() const {
return mVals[0]*mVals[0] + mVals[1]*mVals[1] + mVals[2]*mVals[2];
}
double length() const { return sqrt( squareLength() ); }
inline double norm() const { return length(); }
inline double sqrnorm() const { return squareLength(); }
void normalize() { double L = length(); mVals[0] /= L; mVals[1] /= L; mVals[2] /= L; }
static double dot( Vec3 const & a , Vec3 const & b ) {
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}
static Vec3 cross( Vec3 const & a , Vec3 const & b ) {
return Vec3( a[1]*b[2] - a[2]*b[1] ,
a[2]*b[0] - a[0]*b[2] ,
a[0]*b[1] - a[1]*b[0] );
}
void operator += (Vec3 const & other) {
mVals[0] += other[0];
mVals[1] += other[1];
mVals[2] += other[2];
}
void operator -= (Vec3 const & other) {
mVals[0] -= other[0];
mVals[1] -= other[1];
mVals[2] -= other[2];
}
void operator *= (double s) {
mVals[0] *= s;
mVals[1] *= s;
mVals[2] *= s;
}
void operator /= (double s) {
mVals[0] /= s;
mVals[1] /= s;
mVals[2] /= s;
}
};
static inline Vec3 operator + (Vec3 const & a , Vec3 const & b) {
return Vec3(a[0]+b[0] , a[1]+b[1] , a[2]+b[2]);
}
static inline Vec3 operator - (Vec3 const & a , Vec3 const & b) {
return Vec3(a[0]-b[0] , a[1]-b[1] , a[2]-b[2]);
}
static inline Vec3 operator * (double a , Vec3 const & b) {
return Vec3(a*b[0] , a*b[1] , a*b[2]);
}
static inline Vec3 operator / (Vec3 const & a , double b) {
return Vec3(a[0]/b , a[1]/b , a[2]/b);
}
static inline std::ostream & operator << (std::ostream & s , Vec3 const & p) {
s << p[0] << " " << p[1] << " " << p[2];
return s;
}
static inline std::istream & operator >> (std::istream & s , Vec3 & p) {
s >> p[0] >> p[1] >> p[2];
return s;
}
#endif