23 #ifndef SCMATH_GEOM_VECTORS_H
24 #define SCMATH_GEOM_VECTORS_H
30 #include <initializer_list>
56 template<
typename T,
size_t N>
62 T unitLength = (T) 1.0 / std::sqrt(N);
63 for (
size_t i = 0; i < N; i++) {
64 this->arr[i] = unitLength;
77 Vector(std::initializer_list<T> ilst)
79 assert(ilst.size() == N);
82 std::copy(ilst.begin(), ilst.end(), this->arr.begin());
92 T
At(
size_t index)
const
94 if (index > this->arr.size()) {
95 throw std::out_of_range(
"index " +
96 std::to_string(index) +
" > " +
97 std::to_string(this->arr.size()));
99 return this->arr.at(index);
111 void Set(
size_t index, T value)
113 if (index > this->arr.size()) {
114 throw std::out_of_range(
"index " +
115 std::to_string(index) +
" > " +
116 std::to_string(this->arr.size()));
119 this->arr[index] = value;
133 for (
size_t i = 0; i < N; i++) {
134 result += (this->arr[i] * this->arr[i]);
136 return std::sqrt(result);
160 for (
size_t i = 0; i < N; i++) {
161 if (!scmp::WithinTolerance(this->arr[i], (T) 0.0, this->epsilon)) {
185 return scmp::WithinTolerance(this->
Magnitude(), (T) 1.0, this->epsilon);
202 return static_cast<T
>(std::acos(unitA * unitB));
234 return unitA == unitB;
250 return scmp::WithinTolerance(*
this * other, (T) 0.0, this->epsilon);
264 return unit_basis * (*
this * unit_basis);
296 throw std::out_of_range(
"Cross-product can only called on Vector<T, 3>.");
300 (this->arr[1] * other.arr[2]) - (other.arr[1] * this->arr[2]),
301 -((this->arr[0] * other.arr[2]) - (other.arr[0] * this->arr[2])),
302 (this->arr[0] * other.arr[1]) - (other.arr[0] * this->arr[1])
317 for (
size_t i = 0; i < N; i++) {
318 vec.arr[i] = this->arr[i] + other.arr[i];
335 for (
size_t i = 0; i < N; i++) {
336 vec.arr[i] = this->arr[i] - other.arr[i];
352 for (
size_t i = 0; i < N; i++) {
353 vec.arr[i] = this->arr[i] * k;
369 for (
size_t i = 0; i < N; i++) {
370 vec.arr[i] = this->arr[i] / k;
386 for (
size_t i = 0; i < N; i++) {
387 result += (this->arr[i] * other.arr[i]);
402 for (
size_t i = 0; i < N; i++) {
403 if (!scmp::WithinTolerance(this->arr[i], other.arr[i], this->epsilon)) {
419 return !(*
this == other);
447 friend std::ostream &
451 for (
size_t i = 0; i < N; i++) {
462 static const size_t dim = N;
464 std::array<T, N> arr;
Vectors represent a direction and Magnitude.
Definition: Vector.h:57
Vector ProjectOrthogonal(const Vector< T, N > &basis)
Project this vector perpendicularly onto some basis vector.
Definition: Vector.h:276
Vector operator/(const T k) const
Scalar division.
Definition: Vector.h:365
T Magnitude() const
Compute the length of the vector.
Definition: Vector.h:129
Vector Cross(const Vector< T, N > &other) const
Compute the cross product of two vectors.
Definition: Vector.h:292
bool IsUnitVector() const
Determine if this is a unit vector.
Definition: Vector.h:183
friend std::ostream & operator<<(std::ostream &outs, const Vector< T, N > &vec)
Write a vector a stream in the form "<i, j, ...>".
Definition: Vector.h:448
void Set(size_t index, T value)
Set a new value for the vector.
Definition: Vector.h:111
bool IsZero() const
Determine whether this is a zero vector.
Definition: Vector.h:158
bool operator==(const Vector< T, N > &other) const
Vector equivalence.
Definition: Vector.h:400
void SetEpsilon(T eps)
Set equivalence tolerance.
Definition: Vector.h:148
Vector operator*(const T k) const
Scalar multiplication.
Definition: Vector.h:348
T At(size_t index) const
Return the element At index i.
Definition: Vector.h:92
Vector UnitVector() const
Obtain the unit vector for this vector.
Definition: Vector.h:173
bool IsParallel(const Vector< T, N > &other) const
Determine whether two vectors are parallel.
Definition: Vector.h:211
Vector operator-(const Vector< T, N > &other) const
Vector subtraction.
Definition: Vector.h:331
T operator*(const Vector< T, N > &other) const
Compute the Dot product between two vectors.
Definition: Vector.h:382
T Angle(const Vector< T, N > &other) const
Compute the Angle between two vectors.
Definition: Vector.h:194
const T & operator[](size_t i) const
Array indexing into vector.
Definition: Vector.h:436
Vector()
Construct a unit vector of a given type and size.
Definition: Vector.h:60
bool operator!=(const Vector< T, N > &other) const
Vector non-equivalence.
Definition: Vector.h:417
Vector operator+(const Vector< T, N > &other) const
Vector addition.
Definition: Vector.h:313
Vector ProjectParallel(const Vector< T, N > &basis) const
Project this vector onto some basis vector.
Definition: Vector.h:260
Vector(std::initializer_list< T > ilst)
Construct a Vector with initial values.
Definition: Vector.h:77
bool IsOrthogonal(const Vector< T, N > &other) const
Determine if two vectors are orthogonal or perpendicular to each other.
Definition: Vector.h:244
Vector< float, 3 > Vector3F
Type alias for a three-dimensional float vector.
Definition: Vector.h:482
Vector< double, 2 > Vector2D
Type alias for a two-dimensional double vector.
Definition: Vector.h:490
Vector< double, 3 > Vector3D
Type alias for a three-dimensional double vector.
Definition: Vector.h:494
Vector< double, 4 > Vector4D
Type alias for a four-dimensional double vector.
Definition: Vector.h:498
Vector< float, 4 > Vector4F
Type alias for a four-dimensional float vector.
Definition: Vector.h:486
Vector< float, 2 > Vector2F
Type alias for a two-dimensional float vector.
Definition: Vector.h:478
Shimmering Clarity Math & Physics toolkit.
Definition: estimation.h:31
void DefaultEpsilon(double &epsilon)
Get the default epsilon value.