#ifndef __WRNAV_GEOM_VECTOR_H #define __WRNAV_GEOM_VECTOR_H #include #include #include #include #include #include #include namespace wr { namespace geom { template class Vector { public: Vector() { wr::util::DefaultEpsilon(this->epsilon); } Vector(std::initializer_list ilst) { assert(ilst.size() == N); wr::util::DefaultEpsilon(this->epsilon); std::copy(ilst.begin(), ilst.end(), this->arr.begin()); } T magnitude() const { T result = 0; for (size_t i = 0; i < N; i++) { result += (this->arr[i] * this->arr[i]); } return std::sqrt(result); }; void setEpsilon(T epsilon) { this->epsilon = epsilon; } bool isZero() const { for (size_t i = 0; i < N; i++) { if (!wr::util::WithinTolerance(this->arr[i], 0.0, this->epsilon)) { return false; } } return true; } Vector unitVector() const { return *this / this->magnitude(); } T angle(const Vector &rhs) const { Vector unitA = this->unitVector(); Vector unitB = rhs.unitVector(); return std::acos(unitA * unitB); } Vector operator+(const Vector &rhs) const { Vector vec; for (size_t i = 0; i < N; i++) { vec.arr[i] = this->arr[i] + rhs.arr[i]; } return vec; } Vector operator-(const Vector &rhs) const { Vector vec; for (size_t i = 0; i < N; i++) { vec.arr[i] = this->arr[i] - rhs.arr[i]; } return vec; } // Scalar multiplication. Vector operator*(const T k) const { Vector vec; for (size_t i = 0; i < N; i++) { vec.arr[i] = this->arr[i] * k; } return vec; } // Scalar division. Vector operator/(const T k) const { Vector vec; for (size_t i = 0; i < N; i++) { vec.arr[i] = this->arr[i] / k; } return vec; } // Dot product. T operator*(const Vector &rhs) const { T result = 0; for (size_t i = 0; i < N; i++) { result += (this->arr[i] * rhs.arr[i]); } return result; } bool operator==(const Vector &rhs) const { for (size_t i = 0; iarr[i], rhs.arr[i], this->epsilon)) { return false; } } return true; } bool operator!=(const Vector &rhs) const { return !(*this == rhs); } friend std::ostream& operator<<(std::ostream& outs, const Vector& vec) { outs << "<"; for (size_t i = 0; i < N; i++) { outs << vec.arr[i]; if (i < (N-1)) { outs << ", "; } } outs << ">"; return outs; } private: static const size_t dim = N; T epsilon; std::array arr; }; typedef Vector Vector3f; typedef Vector Vector4f; typedef Vector Vector3d; typedef Vector Vector4d; } // namespace geom } // namespace wr #endif // __WRNAV_GEOM_VECTOR_H