// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0.If a copy of the MPL was not distributed with this // file, You can obtain one at https ://mozilla.org/MPL/2.0/. #include "Vector3.h" #include "Angle.h" #include "Spherical.h" //#include "TypeTraits.h" #include const float Deg2Rad = 0.0174532924F; const float Rad2Deg = 57.29578F; const float epsilon = 1E-05f; Vector3::Vector3() { this->x = 0; this->y = 0; this->z = 0; } Vector3::Vector3(float right, float up, float forward) { this->x = right; this->y = up; this->z = forward; } // Vector3::Vector3(Vector2 v) { // this->x = v.x; // this->y = 0.0f; // this->z = v.y; // } Vector3::Vector3(SphericalOf s) { float verticalRad = (90.0f - s.direction.vertical.InDegrees()) * Deg2Rad; float horizontalRad = s.direction.horizontal.InDegrees() * Deg2Rad; float cosVertical = cosf(verticalRad); float sinVertical = sinf(verticalRad); float cosHorizontal = cosf(horizontalRad); float sinHorizontal = sinf(horizontalRad); x = s.distance * sinVertical * sinHorizontal; y = s.distance * cosVertical; z = s.distance * sinVertical * cosHorizontal; // Vector3 v = Vector3(s.distance * sinVertical * sinHorizontal, // s.distance * cosVertical, // ); // return v; } Vector3::~Vector3() {} const Vector3 Vector3::zero = Vector3(0, 0, 0); const Vector3 Vector3::one = Vector3(1, 1, 1); const Vector3 Vector3::right = Vector3(1, 0, 0); const Vector3 Vector3::left = Vector3(-1, 0, 0); const Vector3 Vector3::up = Vector3(0, 1, 0); const Vector3 Vector3::down = Vector3(0, -1, 0); const Vector3 Vector3::forward = Vector3(0, 0, 1); const Vector3 Vector3::back = Vector3(0, 0, -1); // inline float Vector3::Forward() { return z; } // inline float Vector3::Up() { return y; } // inline float Vector3::Right() { return x; } // Vector3 Vector3::FromHorizontal(const Vector2 &v) { // return Vector3(v.x, 0, v.y); // } float Vector3::Magnitude(const Vector3& v) { return sqrtf(v.x * v.x + v.y * v.y + v.z * v.z); } float Vector3::magnitude() const { return (float)sqrtf(x * x + y * y + z * z); } float Vector3::SqrMagnitude(const Vector3& v) { return v.x * v.x + v.y * v.y + v.z * v.z; } float Vector3::sqrMagnitude() const { return (x * x + y * y + z * z); } Vector3 Vector3::Normalize(const Vector3& v) { float num = Vector3::Magnitude(v); Vector3 result = Vector3::zero; if (num > epsilon) { result = v / num; } return result; } Vector3 Vector3::normalized() const { float num = this->magnitude(); Vector3 result = Vector3::zero; if (num > epsilon) { result = ((Vector3) * this) / num; } return result; } Vector3 Vector3::operator-() const { return Vector3(-this->x, -this->y, -this->z); } Vector3 Vector3::operator-(const Vector3& v) const { return Vector3(this->x - v.x, this->y - v.y, this->z - v.z); } Vector3 Vector3::operator-=(const Vector3& v) { this->x -= v.x; this->y -= v.y; this->z -= v.z; return *this; } Vector3 Vector3::operator+(const Vector3& v) const { return Vector3(this->x + v.x, this->y + v.y, this->z + v.z); } Vector3 Vector3::operator+=(const Vector3& v) { this->x += v.x; this->y += v.y; this->z += v.z; return *this; } Vector3 Vector3::Scale(const Vector3& v1, const Vector3& v2) { return Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z); } // Vector3 Passer::LinearAlgebra::operator*(const Vector3 &v, float f) { // return Vector3(v.x * f, v.y * f, v.z * f); // } // Vector3 Passer::LinearAlgebra::operator*(float f, const Vector3 &v) { // return Vector3(v.x * f, v.y * f, v.z * f); // } Vector3 Vector3::operator*=(float f) { this->x *= f; this->y *= f; this->z *= f; return *this; } // Vector3 Passer::LinearAlgebra::operator/(const Vector3 &v, float f) { // return Vector3(v.x / f, v.y / f, v.z / f); // } // Vector3 Passer::LinearAlgebra::operator/(float f, const Vector3 &v) { // return Vector3(v.x / f, v.y / f, v.z / f); // } Vector3 Vector3::operator/=(float f) { this->x /= f; this->y /= f; this->z /= f; return *this; } float Vector3::Dot(const Vector3& v1, const Vector3& v2) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } bool Vector3::operator==(const Vector3& v) const { return (this->x == v.x && this->y == v.y && this->z == v.z); } float Vector3::Distance(const Vector3& v1, const Vector3& v2) { return Magnitude(v1 - v2); } Vector3 Vector3::Cross(const Vector3& v1, const Vector3& v2) { return Vector3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x); } Vector3 Vector3::Project(const Vector3& v, const Vector3& n) { float sqrMagnitude = Dot(n, n); if (sqrMagnitude < epsilon) return Vector3::zero; else { float dot = Dot(v, n); Vector3 r = n * dot / sqrMagnitude; return r; } } Vector3 Vector3::ProjectOnPlane(const Vector3& v, const Vector3& n) { Vector3 r = v - Project(v, n); return r; } float clamp(float x, float lower, float upper) { float lowerClamp = fmaxf(x, lower); float upperClamp = fminf(upper, lowerClamp); return upperClamp; } AngleOf Vector3::Angle(const Vector3& v1, const Vector3& v2) { float denominator = sqrtf(v1.sqrMagnitude() * v2.sqrMagnitude()); if (denominator < epsilon) return AngleOf(); float dot = Vector3::Dot(v1, v2); float fraction = dot / denominator; if (isnan(fraction)) return AngleOf::Degrees(fraction); // short cut to returning NaN universally float cdot = clamp(fraction, -1.0, 1.0); float r = ((float)acos(cdot)); return AngleOf::Radians(r); } AngleOf Vector3::SignedAngle(const Vector3& v1, const Vector3& v2, const Vector3& axis) { // angle in [0,180] AngleOf angle = Vector3::Angle(v1, v2); Vector3 cross = Vector3::Cross(v1, v2); float b = Vector3::Dot(axis, cross); float signd = b < 0 ? -1.0F : (b > 0 ? 1.0F : 0.0F); // angle in [-179,180] AngleOf signed_angle = angle * signd; return AngleOf(signed_angle); } Vector3 Vector3::Lerp(const Vector3& v1, const Vector3& v2, float f) { Vector3 v = v1 + (v2 - v1) * f; return v; } #pragma region Vector3Of namespace LinearAlgebra { template Vector3Of::Vector3Of() {} template Vector3Of::Vector3Of(T horizontal, T vertical, T depth) : horizontal(horizontal), vertical(vertical), depth(depth) {} template Vector3Of::Vector3Of(Vector2Of v) : horizontal(v.horizontal), vertical(v.vertical) {} template Vector3Of::Vector3Of(SphericalOf v) { float cosVertical = AngleOf::Cos(v.direction.vertical); float sinVertical = AngleOf::Sin(v.direction.vertical); float cosHorizontal = AngleOf::Cos(v.direction.horizontal); float sinHorizontal = AngleOf::Sin(v.direction.horizontal); horizontal = v.distance * sinVertical * sinHorizontal; vertical = v.distance * cosVertical; depth = v.distance * sinVertical * cosHorizontal; } template Vector3Of::Vector3Of(Vector3 v) : horizontal(v.x), vertical(v.y), depth(v.z) {} template Vector3 Vector3Of::ToVector3() { return Vector3(this->horizontal, this->vertical, this->depth); } template Vector3Of::~Vector3Of() {} template const Vector3Of Vector3Of::zero = Vector3Of(T{}, T{}, T{}); template <> const Vector3Of Vector3Of::unit = Vector3Of(1, 1, 1); template <> const Vector3Of Vector3Of::unit = Vector3Of(1.0f, 1.0f, 1.0f); template <> const Vector3Of Vector3Of::unit = Vector3Of(1.0f, 1.0f, 1.0f); // template // const Vector3Of Vector3Of::unit = // Vector3Of(TypeTraits::unit(), TypeTraits::unit(), TypeTraits::unit()); // const Vector3Of Vector3Of::right = Vector3Of(1, 0, 0); // const Vector3Of Vector3Of::left = Vector3Of(-1, 0, 0); // const Vector3Of Vector3Of::up = Vector3Of(0, 1, 0); // const Vector3Of Vector3Of::down = Vector3Of(0, -1, 0); // const Vector3Of Vector3Of::forward = Vector3Of(0, 0, 1); // const Vector3Of Vector3Of::back = Vector3Of(0, 0, -1); template T Vector3Of::MagnitudeOf(const Vector3Of& v) { return sqrtf(v.horizontal * v.horizontal + v.vertical * v.vertical + v.depth * v.depth); } template T Vector3Of::Magnitude() const { return (T)sqrtf(this->horizontal * this->horizontal + this->vertical * this->vertical + this->depth * this->depth); } template float Vector3Of::SqrMagnitudeOf(const Vector3Of& v) { return v.horizontal * v.horizontal + v.vertical * v.vertical + v.depth * v.depth; } template float Vector3Of::SqrMagnitude() const { return (horizontal * horizontal + vertical * vertical + depth * depth); } template Vector3Of Vector3Of::Normalize(const Vector3Of& v) { float num = Vector3Of::MagnitudeOf(v); Vector3Of result = Vector3Of::zero; if (num > epsilon) { result = v / num; } return result; } template Vector3Of Vector3Of::Normalized() const { float num = this->Magnitude(); Vector3Of result = Vector3Of::zero; if (num > epsilon) { result = ((Vector3Of) * this) / num; } return result; } template Vector3Of Vector3Of::operator-(const Vector3Of& v) const { return Vector3(this->horizontal - v.horizontal, this->vertical - v.vertical, this->depth - v.depth); } template Vector3Of Vector3Of::operator-=(const Vector3Of& v) { this->horizontal -= v.horizontal; this->vertical -= v.vertical; this->depth -= v.depth; return *this; } template Vector3Of Vector3Of::operator+(const Vector3Of& v) const { return Vector3(this->horizontal + v.horizontal, this->vertical + v.vertical, this->depth + v.depth); } template Vector3Of Vector3Of::operator+=(const Vector3Of& v) { this->horizontal += v.horizontal; this->vertical += v.vertical; this->depth += v.depth; return *this; } template Vector3Of Vector3Of::operator*=(float f) { this->horizontal *= f; this->vertical *= f; this->depth *= f; return *this; } template Vector3Of Vector3Of::operator/=(float f) { this->horizontal /= f; this->vertical /= f; this->depth /= f; return *this; } template float Vector3Of::Dot(const Vector3Of& v1, const Vector3Of& v2) { return v1.horizontal * v2.horizontal + v1.vertical * v2.vertical + v1.depth * v2.depth; } template AngleOf Vector3Of::UnsignedAngle(const Vector3Of& v1, const Vector3Of& v2) { float denominator = sqrtf(v1.SqrMagnitude() * v2.SqrMagnitude()); if (denominator < epsilon) return AngleOf(); float dot = Vector3Of::Dot(v1, v2); float fraction = dot / denominator; if (isnan(fraction)) return AngleOf::Degrees(fraction); // short cut to returning NaN universally float cdot = clamp(fraction, -1.0, 1.0); float r = ((float)acos(cdot)); return AngleOf::Radians(r); } template class Vector3Of; } // namespace LinearAlgebra #pragma endregion Vector3Of