2023-10-21 03:45:39 +00:00
|
|
|
///
|
|
|
|
/// \file include/scmp/geom/Vector.h
|
|
|
|
/// \author K. Isom <kyle@imap.cc>
|
|
|
|
/// \date 2017-06-05
|
|
|
|
/// \brief Linear algebraic vector class.
|
|
|
|
///
|
|
|
|
/// Copyright 2017 K. Isom <kyle@imap.cc>
|
|
|
|
///
|
|
|
|
/// Permission to use, copy, modify, and/or distribute this software for
|
|
|
|
/// any purpose with or without fee is hereby granted, provided that
|
|
|
|
/// the above copyright notice and this permission notice appear in all /// copies.
|
|
|
|
///
|
|
|
|
/// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
|
|
|
|
/// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
|
|
|
|
/// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
|
|
|
|
/// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
/// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
|
|
|
|
/// OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
/// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
/// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
///
|
|
|
|
|
|
|
|
#ifndef SCMATH_GEOM_VECTORS_H
|
|
|
|
#define SCMATH_GEOM_VECTORS_H
|
2023-10-19 06:44:05 +00:00
|
|
|
|
2023-10-21 02:05:55 +00:00
|
|
|
|
2023-10-19 06:44:05 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cmath>
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <ostream>
|
|
|
|
#include <iostream>
|
|
|
|
|
2023-10-19 06:57:50 +00:00
|
|
|
#include <scmp/Math.h>
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
// This implementation is essentially a C++ translation of a Python library
|
|
|
|
// I wrote for Coursera's "Linear Algebra for Machine Learning" course. Many
|
|
|
|
// of the test vectors come from quiz questions in the class.
|
|
|
|
|
|
|
|
|
2023-10-19 06:57:50 +00:00
|
|
|
namespace scmp {
|
2023-10-19 06:44:05 +00:00
|
|
|
namespace geom {
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Vectors represent a direction and Magnitude.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
|
|
|
/// Vector provides a standard interface for dimensionless fixed-size
|
|
|
|
/// vectors. Once instantiated, they cannot be modified.
|
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// Note that while the class is templated, it's intended to be used
|
|
|
|
/// with floating-point types.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// Vectors can be indexed like arrays, and they contain an epsilon
|
|
|
|
/// value that defines a tolerance for equality.
|
2023-10-21 02:05:55 +00:00
|
|
|
template<typename T, size_t N>
|
2023-10-19 06:44:05 +00:00
|
|
|
class Vector {
|
|
|
|
public:
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Construct a unit vector of a given type and size.
|
2023-10-19 06:44:05 +00:00
|
|
|
Vector()
|
|
|
|
{
|
2023-10-21 02:05:55 +00:00
|
|
|
T unitLength = (T) 1.0 / std::sqrt(N);
|
|
|
|
for (size_t i = 0; i < N; i++) {
|
2023-10-19 06:44:05 +00:00
|
|
|
this->arr[i] = unitLength;
|
|
|
|
}
|
|
|
|
|
2023-10-19 06:57:50 +00:00
|
|
|
scmp::DefaultEpsilon(this->epsilon);
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Construct a Vector with initial values.
|
|
|
|
///
|
2023-10-19 06:44:05 +00:00
|
|
|
/// If given an initializer_list, the vector is created with
|
|
|
|
/// those values. There must be exactly N elements in the list.
|
2023-10-21 03:45:39 +00:00
|
|
|
///
|
|
|
|
/// \param ilst An intializer list with N elements of type T.
|
2023-10-21 02:05:55 +00:00
|
|
|
Vector(std::initializer_list<T> ilst)
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
assert(ilst.size() == N);
|
|
|
|
|
2023-10-19 06:57:50 +00:00
|
|
|
scmp::DefaultEpsilon(this->epsilon);
|
2023-10-19 06:44:05 +00:00
|
|
|
std::copy(ilst.begin(), ilst.end(), this->arr.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Return the element At index i.
|
2023-10-21 02:05:55 +00:00
|
|
|
///
|
|
|
|
/// \throws std::out_of_range if the index is out of bounds.
|
|
|
|
///
|
|
|
|
/// \param index The index of the item to retrieve.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \return The value At the index.
|
|
|
|
T At(size_t index) const
|
2023-10-21 02:05:55 +00:00
|
|
|
{
|
|
|
|
if (index > this->arr.size()) {
|
|
|
|
throw std::out_of_range("index " +
|
|
|
|
std::to_string(index) + " > " +
|
|
|
|
std::to_string(this->arr.size()));
|
|
|
|
}
|
|
|
|
return this->arr.at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// \brief Set a new value for the vector.
|
|
|
|
///
|
|
|
|
/// This is used to modify the vector in place.
|
|
|
|
///
|
|
|
|
/// \throws std::out_of_range if the index is out of bounds.
|
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param index The index to insert the value At.
|
2023-10-21 02:05:55 +00:00
|
|
|
/// \param value
|
|
|
|
void Set(size_t index, T value)
|
|
|
|
{
|
|
|
|
if (index > this->arr.size()) {
|
|
|
|
throw std::out_of_range("index " +
|
|
|
|
std::to_string(index) + " > " +
|
|
|
|
std::to_string(this->arr.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
this->arr[index] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Compute the length of the vector.
|
|
|
|
///
|
|
|
|
/// \return The length of the vector.
|
|
|
|
T Magnitude() const
|
2023-10-21 02:05:55 +00:00
|
|
|
{
|
|
|
|
T result = 0;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < N; i++) {
|
|
|
|
result += (this->arr[i] * this->arr[i]);
|
|
|
|
}
|
|
|
|
return std::sqrt(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Set equivalence tolerance.
|
|
|
|
///
|
|
|
|
/// Set the tolerance for equality checks. At a minimum, this
|
|
|
|
/// accounts for systemic errors in floating math arithmetic.
|
|
|
|
///
|
|
|
|
/// \param eps is the maximum difference between this vector and
|
2023-10-19 06:44:05 +00:00
|
|
|
/// another.
|
|
|
|
void
|
2023-10-21 03:45:39 +00:00
|
|
|
SetEpsilon(T eps)
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
this->epsilon = eps;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Determine whether this is a zero vector.
|
|
|
|
///
|
|
|
|
/// \return true if the vector is zero.
|
2023-10-19 06:44:05 +00:00
|
|
|
bool
|
2023-10-21 03:45:39 +00:00
|
|
|
IsZero() const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < N; i++) {
|
2023-10-21 02:05:55 +00:00
|
|
|
if (!scmp::WithinTolerance(this->arr[i], (T) 0.0, this->epsilon)) {
|
2023-10-19 06:44:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Obtain the unit vector for this vector.
|
|
|
|
///
|
|
|
|
/// \return The unit vector
|
2023-10-19 06:44:05 +00:00
|
|
|
Vector
|
2023-10-21 03:45:39 +00:00
|
|
|
UnitVector() const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
return *this / this->Magnitude();
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Determine if this is a unit vector.
|
|
|
|
///
|
|
|
|
/// \return true if the vector is a unit vector.
|
2023-10-19 06:44:05 +00:00
|
|
|
bool
|
2023-10-21 03:45:39 +00:00
|
|
|
IsUnitVector() const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
return scmp::WithinTolerance(this->Magnitude(), (T) 1.0, this->epsilon);
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Compute the Angle between two vectors.
|
|
|
|
///
|
|
|
|
/// \param other Another vector.
|
|
|
|
/// \return The Angle in radians between the two vectors.
|
2023-10-19 06:44:05 +00:00
|
|
|
T
|
2023-10-21 03:45:39 +00:00
|
|
|
Angle(const Vector<T, N> &other) const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
Vector<T, N> unitA = this->UnitVector();
|
|
|
|
Vector<T, N> unitB = other.UnitVector();
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
// Can't compute angles with a zero vector.
|
2023-10-21 03:45:39 +00:00
|
|
|
assert(!this->IsZero());
|
|
|
|
assert(!other.IsZero());
|
|
|
|
return static_cast<T>(std::acos(unitA * unitB));
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Determine whether two vectors are parallel.
|
|
|
|
///
|
|
|
|
/// \param other Another vector
|
|
|
|
/// \return True if the Angle between the vectors is zero.
|
2023-10-19 06:44:05 +00:00
|
|
|
bool
|
2023-10-21 03:45:39 +00:00
|
|
|
IsParallel(const Vector<T, N> &other) const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
if (this->IsZero() || other.IsZero()) {
|
2023-10-19 06:44:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
T angle = this->Angle(other);
|
2023-10-21 02:05:55 +00:00
|
|
|
if (scmp::WithinTolerance(angle, (T) 0.0, this->epsilon)) {
|
2023-10-19 06:44:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Determine if two vectors are orthogonal or
|
|
|
|
/// perpendicular to each other.
|
|
|
|
///
|
|
|
|
/// \param other Another vector
|
|
|
|
/// \return True if the two vectors are orthogonal.
|
2023-10-19 06:44:05 +00:00
|
|
|
bool
|
2023-10-21 03:45:39 +00:00
|
|
|
IsOrthogonal(const Vector<T, N> &other) const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
if (this->IsZero() || other.IsZero()) {
|
2023-10-19 06:44:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-10-21 02:05:55 +00:00
|
|
|
return scmp::WithinTolerance(*this * other, (T) 0.0, this->epsilon);
|
2023-10-19 06:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Project this vector onto some basis vector.
|
|
|
|
///
|
|
|
|
/// \param basis The basis vector to be projected onto.
|
|
|
|
/// \return A vector that is the projection of this onto the basis
|
2023-10-19 06:44:05 +00:00
|
|
|
/// vector.
|
|
|
|
Vector
|
2023-10-21 03:45:39 +00:00
|
|
|
ProjectParallel(const Vector<T, N> &basis) const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
Vector<T, N> unit_basis = basis.UnitVector();
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
return unit_basis * (*this * unit_basis);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Project this vector perpendicularly onto some basis vector.
|
|
|
|
///
|
|
|
|
/// This is also called the *rejection* of the vector.
|
|
|
|
///
|
|
|
|
/// \param basis The basis vector to be projected onto.
|
|
|
|
/// \return A vector that is the orthogonal projection of this onto
|
2023-10-19 06:44:05 +00:00
|
|
|
/// the basis vector.
|
|
|
|
Vector
|
2023-10-21 03:45:39 +00:00
|
|
|
ProjectOrthogonal(const Vector<T, N> &basis)
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
2023-10-21 03:45:39 +00:00
|
|
|
Vector<T, N> spar = this->ProjectParallel(basis);
|
2023-10-19 06:44:05 +00:00
|
|
|
return *this - spar;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Compute the cross product of two vectors.
|
|
|
|
///
|
|
|
|
/// This is only defined over three-dimensional vectors.
|
|
|
|
///
|
|
|
|
/// \throw std::out_of_range if this is not a three-dimensional vector.
|
|
|
|
///
|
|
|
|
/// \param other Another 3D vector.
|
|
|
|
/// \return The Cross product vector.
|
2023-10-19 06:44:05 +00:00
|
|
|
Vector
|
2023-10-21 03:45:39 +00:00
|
|
|
Cross(const Vector<T, N> &other) const
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
assert(N == 3);
|
2023-10-21 03:45:39 +00:00
|
|
|
if (N != 3) {
|
|
|
|
throw std::out_of_range("Cross-product can only called on Vector<T, 3>.");
|
|
|
|
}
|
|
|
|
|
2023-10-21 02:05:55 +00:00
|
|
|
return Vector<T, N>{
|
|
|
|
(this->arr[1] * other.arr[2]) - (other.arr[1] * this->arr[2]),
|
|
|
|
-((this->arr[0] * other.arr[2]) - (other.arr[0] * this->arr[2])),
|
|
|
|
(this->arr[0] * other.arr[1]) - (other.arr[0] * this->arr[1])
|
2023-10-19 06:44:05 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Vector addition.
|
|
|
|
///
|
|
|
|
/// \param other The vector to be added.
|
|
|
|
/// \return A new vector that is the result of adding this and the
|
2023-10-19 06:44:05 +00:00
|
|
|
/// other vector.
|
|
|
|
Vector
|
|
|
|
operator+(const Vector<T, N> &other) const
|
|
|
|
{
|
2023-10-21 02:05:55 +00:00
|
|
|
Vector<T, N> vec;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < N; i++) {
|
|
|
|
vec.arr[i] = this->arr[i] + other.arr[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Vector subtraction.
|
|
|
|
///
|
|
|
|
/// \param other The vector to be subtracted from this vector.
|
|
|
|
/// \return A new vector that is the result of subtracting the
|
2023-10-19 06:44:05 +00:00
|
|
|
/// other vector from this one.
|
|
|
|
Vector
|
|
|
|
operator-(const Vector<T, N> &other) const
|
|
|
|
{
|
2023-10-21 02:05:55 +00:00
|
|
|
Vector<T, N> vec;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < N; i++) {
|
|
|
|
vec.arr[i] = this->arr[i] - other.arr[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Scalar multiplication.
|
|
|
|
///
|
|
|
|
/// \param k The scaling value.
|
|
|
|
/// \return A new vector that is this vector scaled by k.
|
2023-10-19 06:44:05 +00:00
|
|
|
Vector
|
|
|
|
operator*(const T k) const
|
|
|
|
{
|
2023-10-21 02:05:55 +00:00
|
|
|
Vector<T, N> vec;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < N; i++) {
|
|
|
|
vec.arr[i] = this->arr[i] * k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Scalar division.
|
|
|
|
///
|
|
|
|
/// \param k The scaling value
|
|
|
|
/// \return A new vector that is this vector scaled by 1/k.
|
2023-10-19 06:44:05 +00:00
|
|
|
Vector
|
|
|
|
operator/(const T k) const
|
|
|
|
{
|
2023-10-21 02:05:55 +00:00
|
|
|
Vector<T, N> vec;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < N; i++) {
|
|
|
|
vec.arr[i] = this->arr[i] / k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Compute the Dot product between two vectors.
|
|
|
|
///
|
|
|
|
/// \param other The other vector.
|
|
|
|
/// \return A scalar value that is the Dot product of the two vectors.
|
2023-10-19 06:44:05 +00:00
|
|
|
T
|
|
|
|
operator*(const Vector<T, N> &other) const
|
|
|
|
{
|
2023-10-21 02:05:55 +00:00
|
|
|
T result = 0;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < N; i++) {
|
|
|
|
result += (this->arr[i] * other.arr[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Vector equivalence
|
|
|
|
///
|
|
|
|
/// \param other The other vector.
|
|
|
|
/// \return Return true if all the components of both vectors are
|
2023-10-19 06:44:05 +00:00
|
|
|
/// within the tolerance value.
|
|
|
|
bool
|
|
|
|
operator==(const Vector<T, N> &other) const
|
|
|
|
{
|
2023-10-21 02:05:55 +00:00
|
|
|
for (size_t i = 0; i < N; i++) {
|
2023-10-19 06:57:50 +00:00
|
|
|
if (!scmp::WithinTolerance(this->arr[i], other.arr[i], this->epsilon)) {
|
2023-10-19 06:44:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Vector non-equivalence.
|
|
|
|
///
|
|
|
|
/// \param other The other vector.
|
|
|
|
/// \return Return true if any of the components of both vectors are
|
2023-10-19 06:44:05 +00:00
|
|
|
/// not within the tolerance value.
|
|
|
|
bool
|
|
|
|
operator!=(const Vector<T, N> &other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Array indexing into vector.
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// Note that the values of the vector cannot be modified.
|
|
|
|
/// Instead, something like the following must be done:
|
2023-10-19 06:44:05 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2023-10-21 03:45:39 +00:00
|
|
|
/// Vector3D a {1.0, 2.0, 3.0};
|
|
|
|
/// Vector3D b {a[0], a[1]*2.0, a[2]};
|
2023-10-19 06:44:05 +00:00
|
|
|
/// ```
|
|
|
|
///
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \param i The component index.
|
|
|
|
/// \return The value of the vector component At i.
|
2023-10-21 02:05:55 +00:00
|
|
|
const T &
|
2023-10-19 06:44:05 +00:00
|
|
|
operator[](size_t i) const
|
|
|
|
{
|
|
|
|
return this->arr[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Write a vector a stream in the form "<i, j, ...>".
|
|
|
|
///
|
|
|
|
/// \param outs An output stream.
|
|
|
|
/// \param vec The vector to be formatted.
|
|
|
|
/// \return The output stream.
|
2023-10-21 02:05:55 +00:00
|
|
|
friend std::ostream &
|
|
|
|
operator<<(std::ostream &outs, const Vector<T, N> &vec)
|
2023-10-19 06:44:05 +00:00
|
|
|
{
|
|
|
|
outs << "<";
|
|
|
|
for (size_t i = 0; i < N; i++) {
|
|
|
|
outs << vec.arr[i];
|
2023-10-21 02:05:55 +00:00
|
|
|
if (i < (N - 1)) {
|
2023-10-19 06:44:05 +00:00
|
|
|
outs << ", ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outs << ">";
|
|
|
|
return outs;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-10-21 02:05:55 +00:00
|
|
|
static const size_t dim = N;
|
|
|
|
T epsilon;
|
|
|
|
std::array<T, N> arr;
|
2023-10-19 06:44:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///
|
|
|
|
/// \defgroup vector_aliases Vector type aliases.
|
|
|
|
///
|
|
|
|
|
|
|
|
/// \ingroup vector_aliases
|
|
|
|
/// A number of shorthand aliases for vectors are provided. They follow
|
|
|
|
/// the form of VectorNt, where N is the dimension and t is the type.
|
2023-10-21 03:45:39 +00:00
|
|
|
/// For example, a 2D float vector is Vector2F.
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
/// \ingroup vector_aliases
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Type alias for a two-dimensional float vector.
|
|
|
|
typedef Vector<float, 2> Vector2F;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
/// \ingroup vector_aliases
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Type alias for a three-dimensional float vector.
|
|
|
|
typedef Vector<float, 3> Vector3F;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
/// \ingroup vector_aliases
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Type alias for a four-dimensional float vector.
|
|
|
|
typedef Vector<float, 4> Vector4F;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
/// \ingroup vector_aliases
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Type alias for a two-dimensional double vector.
|
|
|
|
typedef Vector<double, 2> Vector2D;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
/// \ingroup vector_aliases
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Type alias for a three-dimensional double vector.
|
|
|
|
typedef Vector<double, 3> Vector3D;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
/// \ingroup vector_aliases
|
2023-10-21 03:45:39 +00:00
|
|
|
/// \brief Type alias for a four-dimensional double vector.
|
|
|
|
typedef Vector<double, 4> Vector4D;
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace geom
|
2023-10-19 07:37:56 +00:00
|
|
|
} // namespace scmp
|
2023-10-19 06:44:05 +00:00
|
|
|
|
|
|
|
|
2023-10-21 03:45:39 +00:00
|
|
|
#endif // SCMATH_GEOM_VECTORS_H
|