32 template<
class T>
class Vec3 {
50 v[0] =
v[1] =
v[2] = 0;
59 template<
class U,
class V,
class W>
74 v[0] =
v[1] =
v[2] = v0;
99 assert(i >= 0 && i < 3);
109 assert(i >= 0 && i < 3);
119 assert(i >= 0 && i < 3);
129 assert(i >= 0 && i < 3);
158 for (
int i=0; i<3; i++) rv[i] = -
v[i];
173 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
175 for (
int i=0; i<3; i++)
v[i] += a;
185 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
187 for (
int i=0; i<3; i++)
v[i] -= a;
197 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
199 for (
int i=0; i<3; i++)
v[i] *= a;
209 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
211 for (
int i=0; i<3; i++)
v[i] /= a;
226 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
228 for (
int i=0; i<3; i++)
v[i] += a[i];
238 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
240 for (
int i=0; i<3; i++)
v[i] -= a[i];
250 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
252 for (
int i=0; i<3; i++)
v[i] *= a[i];
262 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
264 for (
int i=0; i<3; i++)
v[i] /= a[i];
279 for (
int i=0; i<3; i++)
v[i] = a;
289 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
291 Vec3<
decltype(a+
v[0])> rv;
292 for (
int i=0; i<3; i++) rv[i] =
v[i] + a;
302 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
304 Vec3<
decltype(a-
v[0])> rv;
305 for (
int i=0; i<3; i++) rv[i] =
v[i] - a;
315 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
317 Vec3<
decltype(a*
v[0])> rv;
318 for (
int i=0; i<3; i++) rv[i] =
v[i] * a;
328 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
330 Vec3<
decltype(
v[0]/a)> rv;
331 for (
int i=0; i<3; i++) rv[i] =
v[i] / a;
341 std::enable_if_t<std::is_arithmetic<U>::value ||
342 std::is_enum<T>::value,
bool> =
true >
345 for (
int i=0; i<3; i++) rv[i] = (
v[i] == a);
355 std::enable_if_t<std::is_arithmetic<U>::value ||
356 std::is_enum<T>::value,
bool> =
true >
359 for (
int i=0; i<3; i++) rv[i] = (
v[i] != a);
374 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
376 ->
Vec3<
decltype(this->v[0]+a[0])> {
377 Vec3<
decltype(
v[0]+a[0])> rv;
378 for (
int i=0; i<3; i++) rv[i] =
v[i] + a[i];
388 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
390 ->
Vec3<
decltype(this->v[0]-a[0])> {
391 Vec3<
decltype(
v[0]+a[0])> rv;
392 for (
int i=0; i<3; i++) rv[i] =
v[i] - a[i];
402 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
404 ->
Vec3<
decltype(this->v[0]*a[0])> {
405 Vec3<
decltype(
v[0]*a[0])> rv;
406 for (
int i=0; i<3; i++) rv[i] =
v[i] * a[i];
416 std::enable_if_t<std::is_arithmetic<U>::value,
bool> =
true >
418 ->
Vec3<
decltype(this->v[0]/a[0])> {
419 Vec3<
decltype(
v[0]/a[0])> rv;
420 for (
int i=0; i<3; i++) rv[i] =
v[i] / a[i];
431 std::enable_if_t<std::is_arithmetic<U>::value ||
432 std::is_enum<T>::value,
bool> =
true >
435 for (
int i=0; i<3; i++) rv[i] = (
v[i] == a[i]);
446 std::enable_if_t<std::is_arithmetic<U>::value ||
447 std::is_enum<T>::value,
bool> =
true >
450 for (
int i=0; i<3; i++) rv[i] = (
v[i] != a[i]);
463 return v[0]*
v[0] +
v[1]*
v[1] +
v[2]*
v[2];
471 return std::sqrt(
mag2());
483 return (*
this) * 0.0;
505 if (
v[1] > m) m =
v[1];
506 if (
v[2] > m) m =
v[2];
534 if (
v[1] < m) m =
v[1];
535 if (
v[2] < m) m =
v[2];
562 return v[0] +
v[1] +
v[2];
576 return a[0]*
v[0] + a[1]*
v[1] + a[2]*
v[2];
586 Vec3<
decltype(
v[0]*a[0])> rv;
587 rv[0] =
v[1]*a[2] -
v[2]*a[1];
588 rv[1] =
v[2]*a[0] -
v[0]*a[2];
589 rv[2] =
v[0]*a[1] -
v[1]*a[0];
606 assert(a.size() >= 3);
607 for (
int i=0; i<3; i++)
v[i] = a[i];
638 template<
class T,
class U,
639 std::enable_if_t<std::is_arithmetic<T>::value,
bool> =
true >
642 Vec3<
decltype(a+v[0])> rv;
643 for (
int i=0; i<3; i++) rv[i] = a + v[i];
652 template<
class T,
class U,
653 std::enable_if_t<std::is_arithmetic<T>::value,
bool> =
true >
656 Vec3<
decltype(a-v[0])> rv;
657 for (
int i=0; i<3; i++) rv[i] = a - v[i];
666 template<
class T,
class U,
667 std::enable_if_t<std::is_arithmetic<T>::value,
bool> =
true >
670 Vec3<
decltype(a*v[0])> rv;
671 for (
int i=0; i<3; i++) rv[i] = a * v[i];
680 template<
class T,
class U,
681 std::enable_if_t<std::is_arithmetic<T>::value,
bool> =
true >
684 Vec3<
decltype(a/v[0])> rv;
685 for (
int i=0; i<3; i++) rv[i] = a / v[i];
694 template<
class T,
class U,
695 std::enable_if_t<std::is_arithmetic<T>::value ||
696 std::is_enum<T>::value,
bool> =
true >
700 for (
int i=0; i<3; i++) rv[i] = (a == v[i]);
709 template<
class T,
class U,
710 std::enable_if_t<std::is_arithmetic<T>::value ||
711 std::is_enum<T>::value,
bool> =
true >
715 for (
int i=0; i<3; i++) rv[i] = (a != v[i]);
727 template<
class T,
class U>
730 Vec3<
decltype(v1[0]+v2[0])> rv;
731 for (
int i=0; i<3; i++) rv[i] = std::min(v1[i], v2[i]);
741 template<
class T,
class U>
744 Vec3<
decltype(v1[0]+v2[0])> rv;
745 for (
int i=0; i<3; i++) rv[i] = std::max(v1[i], v2[i]);
757 for (
int i=0; i<3; i++) rv[i] = std::floor(v[i]);
Basic integer and real types.
std::ostream & operator<<(std::ostream &os, const criptic::Vec3< T > &v)
ASCII-formatted output of Vec3 to stream.
Definition Vec3.cpp:14
std::istream & operator>>(std::istream &is, criptic::Vec3< T > &v)
ASCII-formatted read of Vec3 from stream.
Definition Vec3.cpp:7
Class that represents a mathematical vector.
Definition Vec3.H:32
T min() const
Return the value of the smallest element.
Definition Vec3.H:532
Vec3(const Vec3< U > &a)
Conversion constructor.
Definition Vec3.H:82
Vec3< T > & operator+=(const Vec3< U > &a)
Add another Vec3 to the Vec3 elementwise.
Definition Vec3.H:227
T * data()
Return pointer to data.
Definition Vec3.H:136
int argmax() const
Return the index of the largest element.
Definition Vec3.H:514
T max() const
Return the value of the largest element.
Definition Vec3.H:503
Real mag() const
Computes the magnitude of the vector.
Definition Vec3.H:470
int argmin() const
Return the index of the smallest element.
Definition Vec3.H:543
Vec3< bool > operator!=(const U &a) const
Evaluate inequality of vector elementwise.
Definition Vec3.H:357
constexpr T mag2() const
Computes the squared magnitude of the vector.
Definition Vec3.H:462
Vec3 & operator/=(const Vec3< U > &a)
Divide this Vec3 by another Vec3 elementwise.
Definition Vec3.H:263
Vec3< bool > operator!=(const Vec3< U > &a) const
Evaluate inequality of two Vec3's elementwise.
Definition Vec3.H:448
auto operator/(const Vec3< U > &a) const -> Vec3< decltype(this->v[0]/a[0])>
Divide two Vec3s elementwise.
Definition Vec3.H:417
auto operator*(const U &a) const -> Vec3< decltype(this->v[0] *a)>
Multiply a scalar by a Vec3 elementwise.
Definition Vec3.H:316
Vec3< T > & operator+=(const U &a)
Add a scalar to the Vec3 elementwise.
Definition Vec3.H:174
auto operator-(const U &a) const -> Vec3< decltype(this->v[0]-a)>
Subtract a scalar from a Vec3 elementwise.
Definition Vec3.H:303
auto dot(const Vec3< U > &a) const
Returns the dot product of this vector with another vector.
Definition Vec3.H:575
Vec3(const U &v0, const V &v1, const W &v2)
Construct a vector from 3 scalars.
Definition Vec3.H:60
T sum() const
Return the sum of the elements.
Definition Vec3.H:561
auto operator/(const U &a) const -> Vec3< decltype(this->v[0]/a)>
Divide a Vec3 by a scalar elementwise.
Definition Vec3.H:329
Vec3< T > & operator/=(const U &a)
Divide the Vec3 by a scalar elementwise.
Definition Vec3.H:210
Vec3(const U &v0)
Construct a vector with every element equal to given value.
Definition Vec3.H:73
auto operator*(const Vec3< U > &a) const -> Vec3< decltype(this->v[0] *a[0])>
Multiply two Vec3s elementwise.
Definition Vec3.H:403
Vec3 & operator*=(const Vec3< U > &a)
Multiply another Vec3 by the Vec3 elementwise.
Definition Vec3.H:251
Vec3< T > & operator=(const T &a)
Assign a scalar value to every element of a Vec3.
Definition Vec3.H:278
auto operator-(const Vec3< U > &a) const -> Vec3< decltype(this->v[0]-a[0])>
Subtract two Vec3s elementwise.
Definition Vec3.H:389
Vec3()
Construct a vector of all zeros.
Definition Vec3.H:49
Vec3< T > & operator*=(const U &a)
Multiply a scalar by the Vec3 elementwise.
Definition Vec3.H:198
Vec3< T > operator-()
Return the negative of a vector.
Definition Vec3.H:156
auto cross(const Vec3< U > &a) const -> Vec3< decltype(this->v[0]+a[0])>
Returns the cross product of this vector with another vector.
Definition Vec3.H:585
T v[3]
Definition Vec3.H:40
Vec3< Real > unit() const
Returns a unit vector parallel to the vector.
Definition Vec3.H:478
Vec3< bool > operator==(const Vec3< U > &a) const
Evaluate equality of two Vec3's elementwise.
Definition Vec3.H:433
Vec3< T > & operator=(const std::vector< U > &a)
Assign a Vec3 from a std::vector.
Definition Vec3.H:605
T & operator()(int i)
Return an element of the vector.
Definition Vec3.H:118
T & operator[](int i)
Return an element of the vector.
Definition Vec3.H:98
const T * data() const
Return const pointer to data.
Definition Vec3.H:143
auto operator+(const U &a) const -> Vec3< decltype(this->v[0]+a)>
Add a scalar and a Vec3 elementwise.
Definition Vec3.H:290
Vec3< bool > operator==(const U &a) const
Evaluate equality of vector elementwise.
Definition Vec3.H:343
auto operator+(const Vec3< U > &a) const -> Vec3< decltype(this->v[0]+a[0])>
Add two Vec3s elementwise.
Definition Vec3.H:375
void normalize()
Normalizes this vector to have unit length.
Definition Vec3.H:491
Vec3< T > & operator-=(const U &a)
Subtract a scalar from the Vec3 elementwise.
Definition Vec3.H:186
Vec3< T > & operator-=(const Vec3< U > &a)
Subtract another Vec3 from the Vec3 elementwise.
Definition Vec3.H:239
The primary namespace for criptic objects.
Definition AdvancePacket.H:25
Vec3< bool > operator!=(const T &a, const Vec3< U > &v)
Evaluate inequality of a Vec3 with a scalar elementwise.
Definition Vec3.H:712
static const RealVec yHat(0, 1, 0)
criptic::RealTensor2 operator-(const criptic::Real a, const criptic::RealTensor2 &t)
Subtract a scalar from a RealTensor2 elementwise.
Definition RealTensor2.H:847
Vec3< bool > operator==(const T &a, const Vec3< U > &v)
Evaluate equality of a Vec3 with a scalar elementwise.
Definition Vec3.H:697
static const SIdxVec zeroSIdxVec(0, 0, 0)
static const RealVec xHat(1, 0, 0)
Vec3< T > floor(const criptic::Vec3< T > &v)
Return the elementwise floor of a Vec3.
Definition Vec3.H:755
criptic::RealTensor2 operator+(const criptic::Real a, const criptic::RealTensor2 &t)
Add a scalar and a RealTensor2 elementwise.
Definition RealTensor2.H:834
Vec3< bool > BoolVec
Definition Vec3.H:618
criptic::RealTensor2 operator/(const criptic::Real a, const criptic::RealTensor2 &t)
Divide a RealTensor2 by a scalar elementwise.
Definition RealTensor2.H:873
Vec3< Real > RealVec
Definition Vec3.H:615
static const RealVec identityVec(1, 1, 1)
criptic::FieldQty max(const criptic::FieldQty &q1, const criptic::FieldQty &q2)
Take elementwise maximum of two FieldQty objects.
Definition FieldQty.H:277
double Real
Definition Types.H:38
static const RealVec zHat(0, 0, 1)
Vec3< SignedIdxType > SIdxVec
Definition Vec3.H:617
static const BoolVec trueVec(1, 1, 1)
static const BoolVec falseVec(1, 1, 1)
static const IdxVec zeroIdxVec(0, 0, 0)
Vec3< IdxType > IdxVec
Definition Vec3.H:616
static const RealVec zeroVec(0, 0, 0)
criptic::FieldQty min(const criptic::FieldQty &q1, const criptic::FieldQty &q2)
Take elementwise minimum of two FieldQty objects.
Definition FieldQty.H:263
criptic::FieldQty operator*(const criptic::Real a, const criptic::FieldQty &fq)
Multiply a scalar by a FieldQty elementwise.
Definition FieldQty.H:250