10template <
class T>
class Vec3
13 Vec3<T>() : x(T()), y(T()), z(T()){};
14 Vec3<T>(T value) : x(value), y(value), z(value){};
15 Vec3<T>(T xx, T yy, T zz) : x(xx), y(yy), z(zz){};
31 void set(
int index, T value)
41 void set(T newX, T newY, T newZ)
48 void add(
int index, T delta)
58 void add(T dx, T dy, T dz)
69 bool operator==(
const Vec3<T> &value)
const {
return x == value.x && y == value.y && z == value.z; }
70 bool operator!=(
const Vec3<T> &value)
const {
return !(*
this == value); }
71 bool operator==(
const T &value)
const {
return x == value && y == value && z == value; }
72 bool operator!=(
const T &value)
const {
return !(*
this == value); }
73 void operator=(
const T value)
80 void operator+=(
const T value)
86 void operator+=(
const Vec3<T> &v)
92 Vec3<T> operator+(
const T value)
const
110 void operator-=(
const T value)
116 void operator-=(
const Vec3<T> &v)
122 inline Vec3<T> operator-(
const T value)
const {
return Vec3<T>(x - value, y - value, z - value); }
125 void operator/=(
const T value)
131 void operator/=(
const Vec3<T> &v)
137 Vec3<T> operator/(
const T value)
const {
return Vec3<T>(x / value, y / value, z / value); }
140 Vec3<T> operator*(
const T value)
const {
return Vec3<T>(x * value, y * value, z * value); }
141 void operator*=(
const T value)
150 return Vec3<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
158 Vec3<T> abs()
const {
return Vec3<T>(fabs(x), fabs(y), fabs(z)); }
162 T a = (fabs(x) < fabs(y)) ? fabs(y) : fabs(x);
163 return (a < fabs(z)) ? fabs(z) : a;
166 int absMaxElement()
const
168 if ((fabs(x) >= fabs(y)) && (fabs(x) >= fabs(z)))
170 if ((fabs(y) >= fabs(x)) && (fabs(y) >= fabs(z)))
177 T a = (fabs(x) > fabs(y)) ? fabs(y) : fabs(x);
178 return (a > fabs(z)) ? fabs(z) : a;
181 int absMinElement()
const
183 if ((fabs(x) <= fabs(y)) && (fabs(x) <= fabs(z)))
185 if ((fabs(y) <= fabs(x)) && (fabs(y) <= fabs(z)))
190 double dp(
const Vec3<T> &v)
const {
return (x * v.x + y * v.y + z * v.z); }
192 double magAndNormalise()
194 double mag = sqrt(x * x + y * y + z * z);
206 double magSqAndNormalise()
208 double magSq = x * x + y * y + z * z;
209 double mag = sqrt(magSq);
221 inline double magnitude()
const {
return sqrt(x * x + y * y + z * z); }
223 inline double magnitudeSq()
const {
return x * x + y * y + z * z; }
227 T a = (x < y) ? y : x;
228 return (a < z) ? z : a;
231 int maxElement()
const
233 if ((x >= y) && (x >= z))
235 if ((y >= x) && (y >= z))
242 T a = (x > y) ? y : x;
243 return (a > z) ? z : a;
246 int minElement()
const
248 if ((x <= y) && (x <= z))
250 if ((y <= x) && (y <= z))
255 Vec3<T> adjusted(
int element,
double delta)
const
257 auto newValue = *
this;
258 newValue[element] += delta;
262 void multiply(
double facx,
double facy,
double facz)
278 double mag = sqrt(x * x + y * y + z * z);
292 const int maxComponent = absMaxElement();
293 if (maxComponent == 0)
300 else if (maxComponent == 1)
318 void orthogonalise(
const Vec3<T> &reference)
320 double sourcemag = reference.magnitude();
321 double dpovermagsq = dp(reference) / (sourcemag * sourcemag);
322 x = x - dpovermagsq * reference.x;
323 y = y - dpovermagsq * reference.y;
324 z = z - dpovermagsq * reference.z;
327 void orthogonalise(
const Vec3<T> &reference1,
const Vec3<T> &reference2)
331 Vec3<T> newvec = reference1 * reference2;
333 double dp = newvec.dp(*
this);
348 throw(std::runtime_error(
"Vec3 - unit() generation failed - index " + std::string(index) +
" is out of bounds."));