Loading...
Searching...
No Matches
Vec3.h
1
19
20#pragma once
21#include <assert.h>
22#include <math.h>
23#include <limits>
24
25namespace CommonMath
26{
27
28 class Vec3;
29 inline Vec3 operator*(const double lhs, const Vec3 rhs);
30 inline Vec3 operator*(const Vec3 lhs, const double rhs);
31 inline Vec3 operator*(const Vec3 lhs, const int rhs);
32 inline Vec3 operator*(const int lhs, const Vec3 rhs);
33
35 class Vec3
36 {
37 public:
38 double x, y, z;
39
40 Vec3(void)
41 : x(std::numeric_limits<double>::quiet_NaN())
42 , y(std::numeric_limits<double>::quiet_NaN())
43 , z(std::numeric_limits<double>::quiet_NaN())
44 {
45 }
46 Vec3(double xin, double yin, double zin) : x(xin), y(yin), z(zin)
47 {
48 }
49
50 Vec3(const double in[3]) : x(in[0]), y(in[1]), z(in[2])
51 {
52 }
53
54 Vec3(Vec3 const &in) : x(in.x), y(in.y), z(in.z)
55 {
56 }
57
59 inline double operator[](int i) const
60 {
61 switch (i)
62 {
63 case 0:
64 return x;
65 case 1:
66 return y;
67 case 2:
68 return z;
69 break;
70 }
71 // we're doing something wrong if we get here
72 assert(0);
73 return std::numeric_limits<double>::quiet_NaN();
74 }
75
77 inline double &operator[](int i)
78 {
79 switch (i)
80 {
81 case 0:
82 return x;
83 case 1:
84 return y;
85 case 2:
86 return z;
87 break;
88 }
89 // we're doing something wrong if we get here
90 assert(0);
91 // fail loudly:
92 x = y = z = std::numeric_limits<double>::quiet_NaN();
93 return x;
94 }
95
97 inline double Dot(const Vec3 rhs) const
98 {
99 return x * rhs.x + y * rhs.y + z * rhs.z;
100 }
101
103 inline Vec3 Cross(const Vec3 rhs) const
104 {
105 return Vec3(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
106 }
107
109 inline double GetNorm2Squared(void) const
110 {
111 return this->Dot(*this);
112 }
113
115 inline double GetNorm2(void) const
116 {
117 return sqrt(GetNorm2Squared());
118 }
119
121 inline Vec3 GetUnitVector(void) const
122 {
123 float const n = this->GetNorm2();
124 return (*this) / n;
125 }
126
127 inline Vec3 &operator=(const Vec3 &other)
128 {
129 if (this != &other)
130 {
131 x = other.x;
132 y = other.y;
133 z = other.z;
134 }
135 return *this;
136 }
137
138 inline Vec3 operator+(const Vec3 rhs) const
139 {
140 return Vec3(x + rhs.x, y + rhs.y, z + rhs.z);
141 }
142 inline Vec3 operator-(const Vec3 rhs) const
143 {
144 return Vec3(x - rhs.x, y - rhs.y, z - rhs.z);
145 }
146 inline Vec3 operator/(const double rhs) const
147 {
148 return Vec3(x / rhs, y / rhs, z / rhs);
149 }
150 inline Vec3 operator+() const
151 {
152 return (*this);
153 } // mostly used to write things prettily, contrasting to the below.
154 inline Vec3 operator-() const
155 {
156 return (*this) * double(-1);
157 }
158
159 inline Vec3 operator+=(const Vec3 rhs)
160 {
161 (*this) = (*this) + rhs;
162 return *this;
163 }
164 inline Vec3 operator-=(const Vec3 rhs)
165 {
166 (*this) = (*this) - rhs;
167 return *this;
168 }
169 inline Vec3 operator*=(const double &rhs)
170 {
171 *this = (*this) * rhs;
172 return *this;
173 }
174 inline Vec3 operator/=(const double &rhs)
175 {
176 *this = (*this) / rhs;
177 return *this;
178 }
179 };
180
181 // Multiply with scalar of double type:
182 inline Vec3 operator*(const double lhs, const Vec3 rhs)
183 {
184 return Vec3(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
185 }
186 inline Vec3 operator*(const Vec3 lhs, const double rhs)
187 {
188 return Vec3(lhs.x * rhs, lhs.y * rhs, lhs.z * rhs);
189 }
190
191 // Multiply with scalar of integer type:
192 inline Vec3 operator*(const Vec3 lhs, const int rhs)
193 {
194 return Vec3(rhs * lhs.x, rhs * lhs.y, rhs * lhs.z);
195 }
196
197 inline Vec3 operator*(const int lhs, const Vec3 rhs)
198 {
199 return Vec3(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z);
200 }
201
202} // namespace CommonMath
3D vector class with common vector operations.
Definition Vec3.h:36
Vec3(Vec3 const &in)
Initialise from Vec3.
Definition Vec3.h:54
double GetNorm2Squared(void) const
Calculate the Euclidean norm of the vector, squared (= sum of squared elements).
Definition Vec3.h:109
Vec3 GetUnitVector(void) const
Get the unit vector pointing along the same direction as this vector. Will fail for zero vectors.
Definition Vec3.h:121
double operator[](int i) const
Getter function, index 0 <-> x, 1 <-> y, 2 <-> z.
Definition Vec3.h:59
double & operator[](int i)
Setter function, index 0 <-> x, 1 <-> y, 2 <-> z.
Definition Vec3.h:77
Vec3 Cross(const Vec3 rhs) const
Calculate the cross product of two vectors.
Definition Vec3.h:103
double GetNorm2(void) const
Calculate the Euclidean norm of the vector.
Definition Vec3.h:115
Vec3(double xin, double yin, double zin)
Initialise vector.
Definition Vec3.h:46
Vec3(const double in[3])
Initialise vector.
Definition Vec3.h:50
double z
the three components of the vector
Definition Vec3.h:38
Vec3(void)
Initialises all members to NaN.
Definition Vec3.h:40
double Dot(const Vec3 rhs) const
Calculate the dot product of two vectors.
Definition Vec3.h:97
STL namespace.