diff --git a/Polar.cpp b/Polar.cpp index b3f09e8..cc86cca 100644 --- a/Polar.cpp +++ b/Polar.cpp @@ -1,9 +1,155 @@ #include -#include "Angle.h" #include "Polar.h" -#include "Spherical.h" +template +PolarOf::PolarOf() { + this->distance = 0.0f; + this->angle = 0.0f; +} +template +PolarOf::PolarOf(float distance, AngleOf angle) { + // distance should always be 0 or greater + if (distance < 0.0f) { + this->distance = -distance; + this->angle = AngleOf::Normalize(angle.ToFloat() - 180.0f); + } else { + this->distance = distance; + if (this->distance == 0.0f) + // angle is always 0 if distance is 0 + this->angle = 0.0f; + else + this->angle = AngleOf::Normalize(angle); + } +} + +template +PolarOf PolarOf::FromVector2(Vector2 v) { + float distance = v.magnitude(); + AngleOf angle = Vector2::SignedAngle(Vector2::forward, v); + PolarOf p = PolarOf(distance, angle); + return p; +} +template +PolarOf PolarOf::FromSpherical(SphericalOf v) { + float distance = + v.distance * cosf(v.vertical.ToFloat() * Passer::LinearAlgebra::Deg2Rad); + AngleOf angle = v.horizontal; + PolarOf p = PolarOf(distance, angle); + return p; +} + +template +const PolarOf PolarOf::zero = PolarOf(0.0f, 0.0f); +template +const PolarOf PolarOf::forward = PolarOf(1.0f, 0.0f); +template +const PolarOf PolarOf::back = PolarOf(1.0, 180.0f); +template +const PolarOf PolarOf::right = PolarOf(1.0, 90.0f); +template +const PolarOf PolarOf::left = PolarOf(1.0, -90.0f); + +template +bool PolarOf::operator==(const PolarOf& v) const { + return (this->distance == v.distance && + this->angle.ToFloat() == v.angle.ToFloat()); +} + +template +PolarOf PolarOf::Normalize(const PolarOf& v) { + PolarOf r = PolarOf(1, v.angle); + return r; +} +template +PolarOf PolarOf::normalized() const { + PolarOf r = PolarOf(1, this->angle); + return r; +} + +template +PolarOf PolarOf::operator-() const { + PolarOf v = PolarOf(this->distance, this->angle + AngleOf(180)); + return v; +} + +template +PolarOf PolarOf::operator-(const PolarOf& v) const { + PolarOf r = -v; + return *this + r; +} +template +PolarOf PolarOf::operator-=(const PolarOf& v) { + *this = *this - v; + return *this; +} + +template +PolarOf PolarOf::operator+(const PolarOf& v) const { + if (v.distance == 0) + return PolarOf(this->distance, this->angle); + if (this->distance == 0.0f) + return v; + + float deltaAngle = + Angle::Normalize(v.angle.ToFloat() - this->angle.ToFloat()).ToFloat(); + float rotation = + deltaAngle < 0.0f ? 180.0f + deltaAngle : 180.0f - deltaAngle; + + if (rotation == 180.0f && v.distance > 0.0f) { + // angle is too small, take this angle and add the distances + return PolarOf(this->distance + v.distance, this->angle); + } + + float newDistance = + Angle::CosineRuleSide(v.distance, this->distance, rotation).ToFloat(); + + float angle = + Angle::CosineRuleAngle(newDistance, this->distance, v.distance).ToFloat(); + + float newAngle = deltaAngle < 0.0f ? this->angle.ToFloat() - angle + : this->angle.ToFloat() + angle; + newAngle = Angle::Normalize(newAngle).ToFloat(); + PolarOf vector = PolarOf(newDistance, newAngle); + return vector; +} +template +PolarOf PolarOf::operator+=(const PolarOf& v) { + *this = *this + v; + return *this; +} + +template +PolarOf PolarOf::operator*=(float f) { + this->distance *= f; + return *this; +} +template +PolarOf PolarOf::operator/=(float f) { + this->distance /= f; + return *this; +} + +template +float PolarOf::Distance(const PolarOf& v1, const PolarOf& v2) { + float d = Angle::CosineRuleSide(v1.distance, v2.distance, + v2.angle.ToFloat() - v1.angle.ToFloat()) + .ToFloat(); + return d; +} + +template +PolarOf PolarOf::Rotate(const PolarOf& v, AngleOf angle) { + AngleOf a = AngleOf::Normalize(v.angle + angle); + PolarOf r = PolarOf(v.distance, a); + return r; +} + +template class PolarOf; +template class PolarOf; + +//===================================== +/* Polar::Polar() { this->distance = 0.0f; this->angle = 0.0f; @@ -85,7 +231,8 @@ Polar Polar::operator+(const Polar& v) const { Angle::CosineRuleSide(v.distance, this->distance, rotation).ToFloat(); float angle = - Angle::CosineRuleAngle(newDistance, this->distance, v.distance).ToFloat(); + Angle::CosineRuleAngle(newDistance, this->distance, +v.distance).ToFloat(); float newAngle = deltaAngle < 0.0f ? this->angle.ToFloat() - angle : this->angle.ToFloat() + angle; @@ -130,4 +277,5 @@ Polar Polar::Rotate(const Polar& v, Angle angle) { Angle a = Angle::Normalize(v.angle.ToFloat() + angle.ToFloat()); Polar r = Polar(v.distance, a); return r; -} \ No newline at end of file +} +*/ diff --git a/Polar.h b/Polar.h index d6eb5ff..3264880 100644 --- a/Polar.h +++ b/Polar.h @@ -11,8 +11,130 @@ namespace Passer { namespace LinearAlgebra { struct Vector2; -struct Spherical; +template +class SphericalOf; +template +class PolarOf { + public: + /// @brief The distance in meters + /// @remark The distance shall never be negative + float distance; + /// @brief The angle in degrees clockwise rotation + /// @remark The angle shall be between -180 .. 180 + AngleOf angle; + + /// @brief A new vector with polar coordinates with zero degrees and + /// distance + PolarOf(); + /// @brief A new vector with polar coordinates + /// @param distance The distance in meters + /// @param angle The angle in degrees, clockwise rotation + /// @note The distance is automatically converted to a positive value. + /// @note The angle is automatically normalized to -180 .. 180 + PolarOf(float distance, AngleOf angle); + /// @brief Convert a vector from 2D carthesian coordinates to polar + /// coordinates + /// @param v The vector to convert + static PolarOf FromVector2(Vector2 v); + /// @brief Convert a vector from spherical coordinates to polar coordinates + /// @param s The vector to convert + /// @note The resulting vector will be projected on the horizontal plane + static PolarOf FromSpherical(SphericalOf v); + + /// @brief A polar vector with zero degrees and distance + const static PolarOf zero; + /// @brief A normalized forward-oriented vector + const static PolarOf forward; + /// @brief A normalized back-oriented vector + const static PolarOf back; + /// @brief A normalized right-oriented vector + const static PolarOf right; + /// @brief A normalized left-oriented vector + const static PolarOf left; + + /// @brief Equality test to another vector + /// @param v The vector to check against + /// @return true: if it is identical to the given vector + /// @note This uses float comparison to check equality which may have + /// strange effects. Equality on floats should be avoided. + bool operator==(const PolarOf& v) const; + + /// @brief The vector length + /// @param v The vector for which you need the length + /// @return The vector length; + inline static float Magnitude(const PolarOf& v) { return v.distance; } + /// @brief The vector length + /// @return The vector length + inline float magnitude() const { return this->distance; } + + /// @brief Convert the vector to a length of 1 + /// @param v The vector to convert + /// @return The vector normalized to a length of 1 + static PolarOf Normalize(const PolarOf& v); + /// @brief Convert the vector to a length of a + /// @return The vector normalized to a length of 1 + PolarOf normalized() const; + + /// @brief Negate the vector + /// @return The negated vector + /// This will rotate the vector by 180 degrees. Distance will stay the same. + PolarOf operator-() const; + + /// @brief Subtract a polar vector from this vector + /// @param v The vector to subtract + /// @return The result of the subtraction + PolarOf operator-(const PolarOf& v) const; + PolarOf operator-=(const PolarOf& v); + /// @brief Add a polar vector to this vector + /// @param v The vector to add + /// @return The result of the addition + PolarOf operator+(const PolarOf& v) const; + PolarOf operator+=(const PolarOf& v); + + /// @brief Scale the vector uniformly up + /// @param f The scaling factor + /// @return The scaled vector + /// @remark This operation will scale the distance of the vector. The angle + /// will be unaffected. + friend PolarOf operator*(const PolarOf& v, float f) { + return PolarOf(v.distance * f, v.angle); + } + friend PolarOf operator*(float f, const PolarOf& v) { + return PolarOf(f * v.distance, v.angle); + } + PolarOf operator*=(float f); + /// @brief Scale the vector uniformly down + /// @param f The scaling factor + /// @return The scaled factor + /// @remark This operation will scale the distance of the vector. The angle + /// will be unaffected. + friend PolarOf operator/(const PolarOf& v, float f) { + return PolarOf(v.distance / f, v.angle); + } + friend PolarOf operator/(float f, const PolarOf& v) { + return PolarOf(f / v.distance, v.angle); + } + PolarOf operator/=(float f); + + /// @brief The distance between two vectors + /// @param v1 The first vector + /// @param v2 The second vector + /// @return The distance between the two vectors + static float Distance(const PolarOf& v1, const PolarOf& v2); + + /// @brief Rotate a vector + /// @param v The vector to rotate + /// @param a The angle in degreesto rotate + /// @return The rotated vector + static PolarOf Rotate(const PolarOf& v, AngleOf a); +}; + +using PolarSingle = PolarOf; +using Polar16 = PolarOf; +using Polar = PolarSingle; + +/* /// @brief A polar vector /// @details This will use the polar coordinate system consisting of a angle /// from a reference direction and a distance. @@ -130,6 +252,8 @@ struct Polar { /// @return The rotated vector static Polar Rotate(const Polar& v, Angle a); }; +*/ + } // namespace LinearAlgebra } // namespace Passer using namespace Passer::LinearAlgebra; diff --git a/Spherical.cpp b/Spherical.cpp index 69e2d9f..92efeb9 100644 --- a/Spherical.cpp +++ b/Spherical.cpp @@ -46,12 +46,11 @@ SphericalOf::SphericalOf(float distance, // this->vertical = vertical; // } -template <> -SphericalOf SphericalOf::FromPolar(Polar polar) { - AngleOf horizontal = polar.angle; - AngleOf vertical = AngleOf(0); - SphericalOf r = - SphericalOf(polar.distance, horizontal, vertical); +template +SphericalOf SphericalOf::FromPolar(PolarOf polar) { + AngleOf horizontal = polar.angle; + AngleOf vertical = AngleOf(0); + SphericalOf r = SphericalOf(polar.distance, horizontal, vertical); return r; } @@ -208,18 +207,18 @@ template class SphericalOf; template class SphericalOf; //--------------------------------------- - +/* Spherical::Spherical() { this->distance = 0.0f; this->horizontalAngle = 0.0f; this->verticalAngle = 0.0f; } -Spherical::Spherical(Polar polar) { - this->distance = polar.distance; - this->horizontalAngle = polar.angle; - this->verticalAngle = 0.0f; -} +// Spherical::Spherical(Polar polar) { +// this->distance = polar.distance; +// this->horizontalAngle = polar.angle; +// this->verticalAngle = 0.0f; +// } Spherical::Spherical(float distance, Angle horizontalAngle, @@ -299,52 +298,53 @@ Spherical Spherical::operator+(const Spherical& s2) const { Vector3 v = v1 + v2; Spherical r = Spherical(v); return r; - /* + // This is the hard way... - if (v2.distance <= 0) - return Spherical(this->distance, this->horizontalAngle, - this->verticalAngle); - if (this->distance <= 0) - return v2; + // if (v2.distance <= 0) + // return Spherical(this->distance, this->horizontalAngle, + // this->verticalAngle); + // if (this->distance <= 0) + // return v2; - float deltaHorizontalAngle = - (float)Angle::Normalize(v2.horizontalAngle - this->horizontalAngle); - float horizontalRotation = deltaHorizontalAngle < 0 - ? 180 + deltaHorizontalAngle - : 180 - deltaHorizontalAngle; - float deltaVerticalAngle = - Angle::Normalize(v2.verticalAngle - this->verticalAngle); - float verticalRotation = deltaVerticalAngle < 0 ? 180 + deltaVerticalAngle - : 180 - deltaVerticalAngle; + // float deltaHorizontalAngle = + // (float)Angle::Normalize(v2.horizontalAngle - this->horizontalAngle); + // float horizontalRotation = deltaHorizontalAngle < 0 + // ? 180 + deltaHorizontalAngle + // : 180 - deltaHorizontalAngle; + // float deltaVerticalAngle = + // Angle::Normalize(v2.verticalAngle - this->verticalAngle); + // float verticalRotation = deltaVerticalAngle < 0 ? 180 + deltaVerticalAngle + // : 180 - deltaVerticalAngle; - if (horizontalRotation == 180 && verticalRotation == 180) - // angle is too small, take this angle and add the distances - return Spherical(this->distance + v2.distance, this->horizontalAngle, - this->verticalAngle); + // if (horizontalRotation == 180 && verticalRotation == 180) + // // angle is too small, take this angle and add the distances + // return Spherical(this->distance + v2.distance, this->horizontalAngle, + // this->verticalAngle); - Angle rotation = AngleBetween(*this, v2); - float newDistance = - Angle::CosineRuleSide(v2.distance, this->distance, rotation); - float angle = - Angle::CosineRuleAngle(newDistance, this->distance, v2.distance); + // Angle rotation = AngleBetween(*this, v2); + // float newDistance = + // Angle::CosineRuleSide(v2.distance, this->distance, rotation); + // float angle = + // Angle::CosineRuleAngle(newDistance, this->distance, v2.distance); - // Now we have to project the angle to the horizontal and vertical planes... - // The axis for the angle is the cross product of the two spherical vectors - // (which function we do not have either...) - float horizontalAngle = 0; - float verticalAngle = 0; + // // Now we have to project the angle to the horizontal and vertical +planes... + // // The axis for the angle is the cross product of the two spherical vectors + // // (which function we do not have either...) + // float horizontalAngle = 0; + // float verticalAngle = 0; - float newHorizontalAngle = - deltaHorizontalAngle < 0 - ? Angle::Normalize(this->horizontalAngle - horizontalAngle) - : Angle::Normalize(this->horizontalAngle + horizontalAngle); - float newVerticalAngle = - deltaVerticalAngle < 0 - ? Angle::Normalize(this->verticalAngle - verticalAngle) - : Angle::Normalize(this->verticalAngle + verticalAngle); + // float newHorizontalAngle = + // deltaHorizontalAngle < 0 + // ? Angle::Normalize(this->horizontalAngle - horizontalAngle) + // : Angle::Normalize(this->horizontalAngle + horizontalAngle); + // float newVerticalAngle = + // deltaVerticalAngle < 0 + // ? Angle::Normalize(this->verticalAngle - verticalAngle) + // : Angle::Normalize(this->verticalAngle + verticalAngle); + + // Spherical v = Spherical(newDistance, newHorizontalAngle, newVerticalAngle); - Spherical v = Spherical(newDistance, newHorizontalAngle, newVerticalAngle); - */ } Spherical Spherical::operator+=(const Spherical& v) { *this = *this + v; @@ -428,4 +428,5 @@ Spherical Spherical::RotateVertical(const Spherical& v, Angle a) { Spherical r = Spherical(v.distance, v.horizontalAngle.ToFloat(), v.verticalAngle.ToFloat() + a.ToFloat()); return r; -} \ No newline at end of file +} +*/ diff --git a/Spherical.h b/Spherical.h index 3926b5d..06956ec 100644 --- a/Spherical.h +++ b/Spherical.h @@ -6,12 +6,14 @@ #define SPHERICAL_H #include "Angle.h" -#include "Polar.h" +// #include "Polar.h" namespace Passer { namespace LinearAlgebra { struct Vector3; +template +class PolarOf; template class SphericalOf { @@ -29,7 +31,7 @@ class SphericalOf { SphericalOf(); SphericalOf(float distance, AngleOf horizontal, AngleOf vertical); - static SphericalOf FromPolar(Polar v); + static SphericalOf FromPolar(PolarOf v); static SphericalOf FromVector3(Vector3 v); Vector3 ToVector3() const; @@ -96,7 +98,8 @@ class SphericalOf { using SphericalSingle = SphericalOf; using Spherical16 = SphericalOf; - +using Spherical = SphericalSingle; +/* /// @brief A spherical vector /// @details This is a vector in 3D space using a spherical coordinate system. /// It consists of a distance and the polar and elevation angles from a @@ -125,7 +128,7 @@ struct Spherical { Spherical(float distance, Angle horizontalAngle, Angle verticalAngle); /// @brief Convert polar coordinates to spherical coordinates /// @param polar The polar coordinate - Spherical(Polar polar); + // Spherical(Polar polar); /// @brief Convert 3D carthesian coordinates to spherical coordinates /// @param v Vector in 3D carthesian coordinates; Spherical(Vector3 v); @@ -228,11 +231,13 @@ struct Spherical { static Spherical RotateHorizontal(const Spherical& v, Angle angle); static Spherical RotateVertical(const Spherical& v, Angle angle); }; +*/ } // namespace LinearAlgebra } // namespace Passer using namespace Passer::LinearAlgebra; +#include "Polar.h" #include "Vector3.h" #endif \ No newline at end of file diff --git a/Vector2.h b/Vector2.h index c4abf6c..7805e69 100644 --- a/Vector2.h +++ b/Vector2.h @@ -30,7 +30,9 @@ namespace Passer { namespace LinearAlgebra { struct Vector3; -struct Polar; +template +class PolarOf; +// using Polar = PolarOf /// @brief A 2=dimensional vector /// @remark This uses the right=handed carthesian coordinate system. @@ -51,7 +53,7 @@ struct Vector2 : Vec2 { Vector2(Vector3 v); /// @brief Convert a Polar vector to a 2-dimensional vector /// @param v The vector in polar coordinates - Vector2(Polar v); + Vector2(PolarOf v); /// @brief Vector2 destructor ~Vector2(); diff --git a/Vector3.cpp b/Vector3.cpp index 36c2b3b..4f5a95b 100644 --- a/Vector3.cpp +++ b/Vector3.cpp @@ -32,9 +32,8 @@ Vector3::Vector3(Vector2 v) { Vector3::Vector3(Spherical s) { float verticalRad = - (90.0f - s.verticalAngle.ToFloat()) * Passer::LinearAlgebra::Deg2Rad; - float horizontalRad = - s.horizontalAngle.ToFloat() * Passer::LinearAlgebra::Deg2Rad; + (90.0f - s.vertical.ToFloat()) * Passer::LinearAlgebra::Deg2Rad; + float horizontalRad = s.horizontal.ToFloat() * Passer::LinearAlgebra::Deg2Rad; float cosVertical = cosf(verticalRad); float sinVertical = sinf(verticalRad); float cosHorizontal = cosf(horizontalRad); diff --git a/Vector3.h b/Vector3.h index 4f8aa1d..b480d20 100644 --- a/Vector3.h +++ b/Vector3.h @@ -10,7 +10,9 @@ namespace Passer { namespace LinearAlgebra { -struct Spherical; +// struct Spherical; +template +class SphericalOf; extern "C" { /// @@ -56,7 +58,7 @@ struct Vector3 : Vec3 { /// @brief Convert vector in spherical coordinates to 3d carthesian /// coordinates /// @param v The vector to convert - Vector3(Spherical v); + Vector3(SphericalOf v); /// @brief Vector3 destructor ~Vector3(); diff --git a/test/Polar_test.cc b/test/Polar_test.cc index c548d0f..4f28c17 100644 --- a/test/Polar_test.cc +++ b/test/Polar_test.cc @@ -4,24 +4,25 @@ #include #include "Polar.h" +#include "Spherical.h" #define FLOAT_INFINITY std::numeric_limits::infinity() TEST(Polar, FromVector2) { Vector2 v = Vector2(0, 1); - Polar p = Polar(v); + Polar p = Polar::FromVector2(v); EXPECT_FLOAT_EQ(p.distance, 1.0F) << "p.distance 0 1"; EXPECT_FLOAT_EQ(p.angle.ToFloat(), 0.0F) << "s.angle 0 0 1"; v = Vector2(1, 0); - p = Polar(v); + p = Polar::FromVector2(v); EXPECT_FLOAT_EQ(p.distance, 1.0F) << "p.distance 1 0"; EXPECT_FLOAT_EQ(p.angle.ToFloat(), 90.0F) << "s.angle 1 0"; v = Vector2(-1, 1); - p = Polar(v); + p = Polar::FromVector2(v); EXPECT_FLOAT_EQ(p.distance, sqrt(2.0F)) << "p.distance -1 1"; EXPECT_NEAR(p.angle.ToFloat(), -45.0F, 1.0e-05) << "s.angle -1 1"; @@ -32,38 +33,38 @@ TEST(Polar, FromSpherical) { Polar p; s = Spherical(1, 0, 0); - p = Polar(s); + p = Polar::FromSpherical(s); EXPECT_FLOAT_EQ(p.distance, 1.0F) << "p.distance FromSpherical(1 0 0)"; EXPECT_FLOAT_EQ(p.angle.ToFloat(), 0.0F) << "p.angle FromSpherical(1 0 0)"; s = Spherical(1, 45, 0); - p = Polar(s); + p = Polar::FromSpherical(s); EXPECT_FLOAT_EQ(p.distance, 1.0F) << "p.distance FromSpherical(1 45 0)"; EXPECT_FLOAT_EQ(p.angle.ToFloat(), 45.0F) << "p.angle FromSpherical(1 45 0)"; s = Spherical(1, -45, 0); - p = Polar(s); + p = Polar::FromSpherical(s); EXPECT_FLOAT_EQ(p.distance, 1.0F) << "p.distance FromSpherical(1 -45 0)"; EXPECT_FLOAT_EQ(p.angle.ToFloat(), -45.0F) << "p.angle FromSpherical(1 -45 0)"; s = Spherical(0, 0, 0); - p = Polar(s); + p = Polar::FromSpherical(s); EXPECT_FLOAT_EQ(p.distance, 0.0F) << "p.distance FromSpherical(0 0 0)"; EXPECT_FLOAT_EQ(p.angle.ToFloat(), 0.0F) << "p.angle FromSpherical(0 0 0)"; s = Spherical(-1, 0, 0); - p = Polar(s); + p = Polar::FromSpherical(s); EXPECT_FLOAT_EQ(p.distance, 1.0F) << "p.distance FromSpherical(-1 0 0)"; EXPECT_FLOAT_EQ(p.angle.ToFloat(), 180.0F) << "p.angle FromSpherical(-1 0 0)"; s = Spherical(0, 0, 90); - p = Polar(s); + p = Polar::FromSpherical(s); EXPECT_FLOAT_EQ(p.distance, 0.0F) << "p.distance FromSpherical(0 0 90)"; EXPECT_FLOAT_EQ(p.angle.ToFloat(), 0.0F) << "p.angle FromSpherical(0 0 90)"; diff --git a/test/Spherical16_test.cc b/test/Spherical16_test.cc index 88f6d0b..677c383 100644 --- a/test/Spherical16_test.cc +++ b/test/Spherical16_test.cc @@ -4,6 +4,7 @@ #include #include "Spherical.h" +#include "Vector3.h" #define FLOAT_INFINITY std::numeric_limits::infinity() diff --git a/test/Spherical_test.cc b/test/Spherical_test.cc index 9d963aa..df338d1 100644 --- a/test/Spherical_test.cc +++ b/test/Spherical_test.cc @@ -9,73 +9,72 @@ TEST(Spherical, FromVector3) { Vector3 v = Vector3(0, 0, 1); - Spherical s = Spherical(v); + Spherical s = Spherical::FromVector3(v); EXPECT_FLOAT_EQ(s.distance, 1.0F) << "s.distance 0 0 1"; - EXPECT_FLOAT_EQ(s.horizontalAngle.ToFloat(), 0.0F) << "s.hor 0 0 1"; - EXPECT_FLOAT_EQ(s.verticalAngle.ToFloat(), 0.0F) << "s.vert 0 0 1"; + EXPECT_FLOAT_EQ(s.horizontal.ToFloat(), 0.0F) << "s.hor 0 0 1"; + EXPECT_FLOAT_EQ(s.vertical.ToFloat(), 0.0F) << "s.vert 0 0 1"; v = Vector3(0, 1, 0); - s = Spherical(v); + s = Spherical::FromVector3(v); EXPECT_FLOAT_EQ(s.distance, 1.0F) << "s.distance 0 1 0"; - EXPECT_FLOAT_EQ(s.horizontalAngle.ToFloat(), 0.0F) << "s.hor 0 1 0"; - EXPECT_FLOAT_EQ(s.verticalAngle.ToFloat(), 90.0F) << "s.vert 0 1 0"; + EXPECT_FLOAT_EQ(s.horizontal.ToFloat(), 0.0F) << "s.hor 0 1 0"; + EXPECT_FLOAT_EQ(s.vertical.ToFloat(), 90.0F) << "s.vert 0 1 0"; v = Vector3(1, 0, 0); - s = Spherical(v); + s = Spherical::FromVector3(v); EXPECT_FLOAT_EQ(s.distance, 1.0F) << "s.distance 1 0 0"; - EXPECT_FLOAT_EQ(s.horizontalAngle.ToFloat(), 90.0F) << "s.hor 1 0 0"; - EXPECT_FLOAT_EQ(s.verticalAngle.ToFloat(), 0.0F) << "s.vert 1 0 0"; + EXPECT_FLOAT_EQ(s.horizontal.ToFloat(), 90.0F) << "s.hor 1 0 0"; + EXPECT_FLOAT_EQ(s.vertical.ToFloat(), 0.0F) << "s.vert 1 0 0"; } TEST(Spherical, FromPolar) { Polar p = Polar(1, 0); - Spherical s = Spherical(p); + Spherical s = Spherical::FromPolar(p); EXPECT_FLOAT_EQ(s.distance, 1.0F) << "s.distance Polar(1 0)"; - EXPECT_FLOAT_EQ(s.horizontalAngle.ToFloat(), 0.0F) << "s.hor Polar(1 0)"; - EXPECT_FLOAT_EQ(s.verticalAngle.ToFloat(), 0.0F) << "s.vert Polar(1 0)"; + EXPECT_FLOAT_EQ(s.horizontal.ToFloat(), 0.0F) << "s.hor Polar(1 0)"; + EXPECT_FLOAT_EQ(s.vertical.ToFloat(), 0.0F) << "s.vert Polar(1 0)"; p = Polar(1, 45); - s = Spherical(p); + s = Spherical::FromPolar(p); EXPECT_FLOAT_EQ(s.distance, 1.0F) << "s.distance Polar(1 45)"; - EXPECT_FLOAT_EQ(s.horizontalAngle.ToFloat(), 45.0F) << "s.hor Polar(1 45)"; - EXPECT_FLOAT_EQ(s.verticalAngle.ToFloat(), 0.0F) << "s.vert Polar(1 45)"; + EXPECT_FLOAT_EQ(s.horizontal.ToFloat(), 45.0F) << "s.hor Polar(1 45)"; + EXPECT_FLOAT_EQ(s.vertical.ToFloat(), 0.0F) << "s.vert Polar(1 45)"; p = Polar(1, -45); - s = Spherical(p); + s = Spherical::FromPolar(p); EXPECT_FLOAT_EQ(s.distance, 1.0F) << "s.distance Polar(1 -45)"; - EXPECT_FLOAT_EQ(s.horizontalAngle.ToFloat(), -45.0F) << "s.hor Polar(1 -45)"; - EXPECT_FLOAT_EQ(s.verticalAngle.ToFloat(), 0.0F) << "s.vert Polar(1 -45)"; + EXPECT_FLOAT_EQ(s.horizontal.ToFloat(), -45.0F) << "s.hor Polar(1 -45)"; + EXPECT_FLOAT_EQ(s.vertical.ToFloat(), 0.0F) << "s.vert Polar(1 -45)"; p = Polar(0, 0); - s = Spherical(p); + s = Spherical::FromPolar(p); EXPECT_FLOAT_EQ(s.distance, 0.0F) << "s.distance Polar(0 0)"; - EXPECT_FLOAT_EQ(s.horizontalAngle.ToFloat(), 0.0F) << "s.hor Polar(0 0)"; - EXPECT_FLOAT_EQ(s.verticalAngle.ToFloat(), 0.0F) << "s.vert Polar(0 0)"; + EXPECT_FLOAT_EQ(s.horizontal.ToFloat(), 0.0F) << "s.hor Polar(0 0)"; + EXPECT_FLOAT_EQ(s.vertical.ToFloat(), 0.0F) << "s.vert Polar(0 0)"; p = Polar(-1, 0); - s = Spherical(p); + s = Spherical::FromPolar(p); EXPECT_FLOAT_EQ(s.distance, 1.0F) << "s.distance Polar(-1 0)"; - EXPECT_FLOAT_EQ(s.horizontalAngle.ToFloat(), 180.0F) << "s.hor Polar(-1 0)"; - EXPECT_FLOAT_EQ(s.verticalAngle.ToFloat(), 0.0F) << "s.vert Polar(-1 0)"; + EXPECT_FLOAT_EQ(s.horizontal.ToFloat(), 180.0F) << "s.hor Polar(-1 0)"; + EXPECT_FLOAT_EQ(s.vertical.ToFloat(), 0.0F) << "s.vert Polar(-1 0)"; } TEST(Spherical, Incident1) { Vector3 v = Vector3(2.242557f, 1.027884f, -0.322347f); - Spherical s = Spherical(v); + Spherical s = Spherical::FromVector3(v); Spherical sr = Spherical(2.49F, 98.18f, 24.4F); EXPECT_NEAR(s.distance, sr.distance, 1.0e-01); - EXPECT_NEAR(s.horizontalAngle.ToFloat(), sr.horizontalAngle.ToFloat(), - 1.0e-02); - EXPECT_NEAR(s.verticalAngle.ToFloat(), sr.verticalAngle.ToFloat(), 1.0e-02); + EXPECT_NEAR(s.horizontal.ToFloat(), sr.horizontal.ToFloat(), 1.0e-02); + EXPECT_NEAR(s.vertical.ToFloat(), sr.vertical.ToFloat(), 1.0e-02); Vector3 r = Vector3(sr); EXPECT_NEAR(r.Right(), v.Right(), 1.0e-02) << "toVector3.x 1 0 0"; @@ -85,13 +84,12 @@ TEST(Spherical, Incident1) { TEST(Spherical, Incident2) { Vector3 v = Vector3(1.0f, 0.0f, 1.0f); - Spherical s = Spherical(v); + Spherical s = Spherical::FromVector3(v); Spherical sr = Spherical(1.4142135623F, 45.0f, 0.0F); EXPECT_NEAR(s.distance, sr.distance, 1.0e-05); - EXPECT_NEAR(s.horizontalAngle.ToFloat(), sr.horizontalAngle.ToFloat(), - 1.0e-05); - EXPECT_NEAR(s.verticalAngle.ToFloat(), sr.verticalAngle.ToFloat(), 1.0e-05); + EXPECT_NEAR(s.horizontal.ToFloat(), sr.horizontal.ToFloat(), 1.0e-05); + EXPECT_NEAR(s.vertical.ToFloat(), sr.vertical.ToFloat(), 1.0e-05); Vector3 r = Vector3(sr); EXPECT_NEAR(r.Right(), v.Right(), 1.0e-06); @@ -99,13 +97,12 @@ TEST(Spherical, Incident2) { EXPECT_NEAR(r.Forward(), v.Forward(), 1.0e-06); v = Vector3(0.0f, 1.0f, 1.0f); - s = Spherical(v); + s = Spherical::FromVector3(v); sr = Spherical(1.4142135623F, 0.0f, 45.0F); EXPECT_NEAR(s.distance, sr.distance, 1.0e-05); - EXPECT_NEAR(s.horizontalAngle.ToFloat(), sr.horizontalAngle.ToFloat(), - 1.0e-05); - EXPECT_NEAR(s.verticalAngle.ToFloat(), sr.verticalAngle.ToFloat(), 1.0e-05); + EXPECT_NEAR(s.horizontal.ToFloat(), sr.horizontal.ToFloat(), 1.0e-05); + EXPECT_NEAR(s.vertical.ToFloat(), sr.vertical.ToFloat(), 1.0e-05); r = Vector3(sr); EXPECT_NEAR(r.Right(), v.Right(), 1.0e-06); @@ -113,12 +110,12 @@ TEST(Spherical, Incident2) { EXPECT_NEAR(r.Forward(), v.Forward(), 1.0e-06); v = Vector3(1.0f, 1.0f, 1.0f); - s = Spherical(v); + s = Spherical::FromVector3(v); r = Vector3(s); EXPECT_NEAR(s.distance, 1.73205080F, 1.0e-02); - EXPECT_NEAR(s.horizontalAngle.ToFloat(), 45.0F, 1.0e-02); - EXPECT_NEAR(s.verticalAngle.ToFloat(), 35.26F, 1.0e-02); + EXPECT_NEAR(s.horizontal.ToFloat(), 45.0F, 1.0e-02); + EXPECT_NEAR(s.vertical.ToFloat(), 35.26F, 1.0e-02); EXPECT_NEAR(r.Right(), v.Right(), 1.0e-06); EXPECT_NEAR(r.Up(), v.Up(), 1.0e-06); @@ -146,14 +143,14 @@ TEST(Spherical, Addition) { v2 = Spherical(1, -45, 0); r = v1 + v2; EXPECT_FLOAT_EQ(r.distance, sqrtf(2)) << "Addition(1 -45 0)"; - EXPECT_FLOAT_EQ(r.horizontalAngle.ToFloat(), 0) << "Addition(1 -45 0)"; - EXPECT_FLOAT_EQ(r.verticalAngle.ToFloat(), 0) << "Addition(1 -45 0)"; + EXPECT_FLOAT_EQ(r.horizontal.ToFloat(), 0) << "Addition(1 -45 0)"; + EXPECT_FLOAT_EQ(r.vertical.ToFloat(), 0) << "Addition(1 -45 0)"; v2 = Spherical(1, 0, 90); r = v1 + v2; EXPECT_FLOAT_EQ(r.distance, sqrtf(2)) << "Addition(1 0 90)"; - EXPECT_FLOAT_EQ(r.horizontalAngle.ToFloat(), 45) << "Addition(1 0 90)"; - EXPECT_FLOAT_EQ(r.verticalAngle.ToFloat(), 45) << "Addition(1 0 90)"; + EXPECT_FLOAT_EQ(r.horizontal.ToFloat(), 45) << "Addition(1 0 90)"; + EXPECT_FLOAT_EQ(r.vertical.ToFloat(), 45) << "Addition(1 0 90)"; } #endif \ No newline at end of file diff --git a/test/Vector2_test.cc b/test/Vector2_test.cc index ba61ec6..a7561f2 100644 --- a/test/Vector2_test.cc +++ b/test/Vector2_test.cc @@ -1,7 +1,7 @@ #if GTEST #include -#include #include +#include #include "Vector2.h" @@ -13,21 +13,21 @@ TEST(Vector2, FromPolar) { Vector2 r; v = Vector2(0, 1); - p = Polar(v); + p = Polar::FromVector2(v); r = Vector2(p); EXPECT_FLOAT_EQ(r.x, 0.0F) << "FromPolar(0 1)"; EXPECT_FLOAT_EQ(r.y, 1.0F) << "FromPolar(0 1)"; v = Vector2(1, 0); - p = Polar(v); + p = Polar::FromVector2(v); r = Vector2(p); EXPECT_FLOAT_EQ(r.x, 1.0F) << "FromPolar(1 0)"; EXPECT_NEAR(r.y, 0.0F, 1.0e-07) << "FromPolar(1 0)"; v = Vector2(0, 0); - p = Polar(v); + p = Polar::FromVector2(v); r = Vector2(p); EXPECT_FLOAT_EQ(r.x, 0.0F) << "FromPolar(0 0)"; diff --git a/test/Vector3_test.cc b/test/Vector3_test.cc index c7ba095..01170da 100644 --- a/test/Vector3_test.cc +++ b/test/Vector3_test.cc @@ -9,7 +9,7 @@ TEST(Vector3, FromSpherical) { Vector3 v = Vector3(0, 0, 1); - Spherical s = Spherical(v); + Spherical s = Spherical::FromVector3(v); Vector3 r = Vector3(s); EXPECT_FLOAT_EQ(r.Right(), 0.0F) << "toVector3.x 0 0 1"; @@ -17,7 +17,7 @@ TEST(Vector3, FromSpherical) { EXPECT_FLOAT_EQ(r.Forward(), 1.0F) << "toVector3.z 0 0 1"; v = Vector3(0, 1, 0); - s = Spherical(v); + s = Spherical::FromVector3(v); r = Vector3(s); EXPECT_FLOAT_EQ(r.Right(), 0.0F) << "toVector3.x 0 1 0"; @@ -25,7 +25,7 @@ TEST(Vector3, FromSpherical) { EXPECT_NEAR(r.Forward(), 0.0F, 1.0e-06) << "toVector3.z 0 1 0"; v = Vector3(1, 0, 0); - s = Spherical(v); + s = Spherical::FromVector3(v); r = Vector3(s); EXPECT_FLOAT_EQ(r.Right(), 1.0F) << "toVector3.x 1 0 0";