From 28ee2254b89167641322ab0efb753fe2670efa8a Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Wed, 18 Dec 2024 16:54:33 +0100 Subject: [PATCH] Normalize Spherical at creation --- Spherical.cpp | 95 ++++++++++++++++++--------------------------------- 1 file changed, 34 insertions(+), 61 deletions(-) diff --git a/Spherical.cpp b/Spherical.cpp index a88f704..55836a9 100644 --- a/Spherical.cpp +++ b/Spherical.cpp @@ -5,55 +5,34 @@ #include -template -SphericalOf::SphericalOf() { +template SphericalOf::SphericalOf() { this->distance = 0.0f; this->direction = DirectionOf(); - // this->horizontal = AngleOf(); - // this->vertical = AngleOf(); } -// template <> -// SphericalOf::SphericalOf() { -// this->distance = 0.0f; -// this->horizontal = AngleOf(0); -// this->vertical = AngleOf(0); -// } - template -SphericalOf::SphericalOf(float distance, - AngleOf horizontal, +SphericalOf::SphericalOf(float distance, AngleOf horizontal, AngleOf vertical) { - this->distance = distance; - this->direction = DirectionOf(horizontal, vertical); - // this->horizontal = horizontal; - // this->vertical = vertical; + if (distance < 0) { + this->distance = distance; + this->direction = DirectionOf(horizontal, vertical); + } else { + this->distance = -distance; + this->direction = -DirectionOf(horizontal, vertical); + } } template SphericalOf::SphericalOf(float distance, DirectionOf direction) { - this->distance = distance; - this->direction = direction; + if (distance < 0) { + this->distance = -distance; + this->direction = -direction; + } else { + this->distance = distance; + this->direction = direction; + } } -// template <> -// SphericalOf::SphericalOf(float distance, -// AngleOf horizontal, -// AngleOf vertical) { -// this->distance = distance; -// this->horizontal = horizontal; -// this->vertical = vertical; -// } - -// template <> -// SphericalOf::SphericalOf(float distance, -// AngleOf horizontal, -// AngleOf vertical) { -// this->distance = distance; -// this->horizontal = horizontal; -// this->vertical = vertical; -// } - template SphericalOf SphericalOf::FromPolar(PolarOf polar) { AngleOf horizontal = polar.angle; @@ -62,8 +41,7 @@ SphericalOf SphericalOf::FromPolar(PolarOf polar) { return r; } -template -SphericalOf SphericalOf::FromVector3(Vector3 v) { +template SphericalOf SphericalOf::FromVector3(Vector3 v) { float distance = v.magnitude(); if (distance == 0.0f) { return SphericalOf(distance, AngleOf(), AngleOf()); @@ -76,8 +54,7 @@ SphericalOf SphericalOf::FromVector3(Vector3 v) { } } -template -Vector3 SphericalOf::ToVector3() const { +template Vector3 SphericalOf::ToVector3() const { float verticalRad = (pi / 2) - this->direction.vertical.InRadians(); float horizontalRad = this->direction.horizontal.InRadians(); @@ -122,8 +99,7 @@ SphericalOf SphericalOf::WithDistance(float distance) { return SphericalOf(); } -template -SphericalOf SphericalOf::operator-() const { +template SphericalOf SphericalOf::operator-() const { SphericalOf v = SphericalOf( this->distance, this->direction.horizontal + AngleOf::Degrees(180), this->direction.vertical + AngleOf::Degrees(180)); @@ -131,7 +107,7 @@ SphericalOf SphericalOf::operator-() const { } template -SphericalOf SphericalOf::operator-(const SphericalOf& s2) const { +SphericalOf SphericalOf::operator-(const SphericalOf &s2) const { // let's do it the easy way... Vector3 v1 = this->ToVector3(); Vector3 v2 = s2.ToVector3(); @@ -140,13 +116,13 @@ SphericalOf SphericalOf::operator-(const SphericalOf& s2) const { return r; } template -SphericalOf SphericalOf::operator-=(const SphericalOf& v) { +SphericalOf SphericalOf::operator-=(const SphericalOf &v) { *this = *this - v; return *this; } template -SphericalOf SphericalOf::operator+(const SphericalOf& s2) const { +SphericalOf SphericalOf::operator+(const SphericalOf &s2) const { // let's do it the easy way... Vector3 v1 = this->ToVector3(); Vector3 v2 = s2.ToVector3(); @@ -201,19 +177,17 @@ SphericalOf SphericalOf::operator+(const SphericalOf& s2) const { */ } template -SphericalOf SphericalOf::operator+=(const SphericalOf& v) { +SphericalOf SphericalOf::operator+=(const SphericalOf &v) { *this = *this + v; return *this; } -template -SphericalOf SphericalOf::operator*=(float f) { +template SphericalOf SphericalOf::operator*=(float f) { this->distance *= f; return *this; } -template -SphericalOf SphericalOf::operator/=(float f) { +template SphericalOf SphericalOf::operator/=(float f) { this->distance /= f; return *this; } @@ -224,8 +198,8 @@ SphericalOf SphericalOf::operator/=(float f) { const float epsilon = 1E-05f; template -float SphericalOf::DistanceBetween(const SphericalOf& v1, - const SphericalOf& v2) { +float SphericalOf::DistanceBetween(const SphericalOf &v1, + const SphericalOf &v2) { // SphericalOf difference = v1 - v2; // return difference.distance; Vector3 vec1 = v1.ToVector3(); @@ -235,8 +209,8 @@ float SphericalOf::DistanceBetween(const SphericalOf& v1, } template -AngleOf SphericalOf::AngleBetween(const SphericalOf& v1, - const SphericalOf& v2) { +AngleOf SphericalOf::AngleBetween(const SphericalOf &v1, + const SphericalOf &v2) { // float denominator = v1.distance * v2.distance; // if (denominator < epsilon) // return 0.0f; @@ -256,9 +230,8 @@ AngleOf SphericalOf::AngleBetween(const SphericalOf& v1, template AngleOf Passer::LinearAlgebra::SphericalOf::SignedAngleBetween( - const SphericalOf& v1, - const SphericalOf& v2, - const SphericalOf& axis) { + const SphericalOf &v1, const SphericalOf &v2, + const SphericalOf &axis) { Vector3 v1_vector = v1.ToVector3(); Vector3 v2_vector = v2.ToVector3(); Vector3 axis_vector = axis.ToVector3(); @@ -267,7 +240,7 @@ AngleOf Passer::LinearAlgebra::SphericalOf::SignedAngleBetween( } template -SphericalOf SphericalOf::Rotate(const SphericalOf& v, +SphericalOf SphericalOf::Rotate(const SphericalOf &v, AngleOf horizontalAngle, AngleOf verticalAngle) { SphericalOf r = @@ -276,14 +249,14 @@ SphericalOf SphericalOf::Rotate(const SphericalOf& v, return r; } template -SphericalOf SphericalOf::RotateHorizontal(const SphericalOf& v, +SphericalOf SphericalOf::RotateHorizontal(const SphericalOf &v, AngleOf a) { SphericalOf r = SphericalOf(v.distance, v.direction.horizontal + a, v.direction.vertical); return r; } template -SphericalOf SphericalOf::RotateVertical(const SphericalOf& v, +SphericalOf SphericalOf::RotateVertical(const SphericalOf &v, AngleOf a) { SphericalOf r = SphericalOf(v.distance, v.direction.horizontal, v.direction.vertical + a);