From 8e7a8c64322148d2d7054879d34bc63aea84c504 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 10 May 2024 17:50:11 +0200 Subject: [PATCH] Fix namespaces --- Angle16.h | 5 +++++ Angle32.h | 4 ++++ Angle8.h | 5 +++++ AngleAxis.h | 4 ++++ AngleUsing.h | 4 ++++ Axis.h | 4 ++++ FloatSingle.h | 11 +++++++--- Matrix.h | 4 ++++ Polar.h | 12 ++++++++--- Quaternion.h | 56 ++++++++++++++++++++++++++------------------------- Range.h | 4 ++++ Spherical.cpp | 40 ++++++++++++++++++------------------ Spherical.h | 12 +++++------ Vector2.h | 13 ++++++++++-- Vector3.cpp | 20 ++++++++++++++++++ Vector3.h | 10 +++++++++ 16 files changed, 147 insertions(+), 61 deletions(-) diff --git a/Angle16.h b/Angle16.h index fd62d1d..47d31ce 100644 --- a/Angle16.h +++ b/Angle16.h @@ -3,6 +3,8 @@ #include "Angle.h" #include +namespace Passer { + typedef AngleUsing Angle16; template <> Angle16::AngleUsing(float angle) { @@ -19,3 +21,6 @@ template <> float Angle16::ToFloat() const { float f = ((this->value * 180) / 32768.0F); return f; } + +} // namespace Passer +using namespace Passer; \ No newline at end of file diff --git a/Angle32.h b/Angle32.h index 7d8ce10..cded359 100644 --- a/Angle32.h +++ b/Angle32.h @@ -3,6 +3,7 @@ #include "Angle.h" #include +namespace Passer { typedef AngleUsing Angle32; template <> Angle32::AngleUsing(float angle) { @@ -19,3 +20,6 @@ template <> float Angle32::ToFloat() const { float f = ((this->value * 180) / 2147483648.0F); return f; } + +} // namespace Passer +using namespace Passer; \ No newline at end of file diff --git a/Angle8.h b/Angle8.h index 22c8745..5ba5f6e 100644 --- a/Angle8.h +++ b/Angle8.h @@ -3,6 +3,8 @@ #include "Angle.h" #include +namespace Passer { + typedef AngleUsing Angle8; template <> Angle8::AngleUsing(float angle) { @@ -20,3 +22,6 @@ template <> float Angle8::ToFloat() const { float f = (this->value * 180) / 128.0F; return f; } + +} // namespace Passer +using namespace Passer; \ No newline at end of file diff --git a/AngleAxis.h b/AngleAxis.h index 68eda55..b387521 100644 --- a/AngleAxis.h +++ b/AngleAxis.h @@ -8,6 +8,8 @@ #include "Angle.h" #include "Axis.h" +namespace Passer { + class AngleAxis { public: Angle angle; @@ -16,5 +18,7 @@ public: AngleAxis(); AngleAxis(Angle angle, Axis axis); }; +} // namespace Passer +using namespace Passer; #endif \ No newline at end of file diff --git a/AngleUsing.h b/AngleUsing.h index 156fd6e..ca179db 100644 --- a/AngleUsing.h +++ b/AngleUsing.h @@ -4,6 +4,8 @@ #include "Angle.h" #include "Range.h" +namespace Passer { + // A fixed angle between (-180..180] template class AngleUsing { @@ -38,5 +40,7 @@ public: // protected: T value; }; +} // namespace Passer +using namespace Passer; #endif diff --git a/Axis.h b/Axis.h index 992ab58..0325257 100644 --- a/Axis.h +++ b/Axis.h @@ -7,6 +7,8 @@ #include "Angle.h" +namespace Passer { + class Vector3; class Axis { @@ -27,5 +29,7 @@ public: Vector3 ToVector3(); }; +} // namespace Passer +using namespace Passer; #endif \ No newline at end of file diff --git a/FloatSingle.h b/FloatSingle.h index 7aa2251..8922f06 100644 --- a/FloatSingle.h +++ b/FloatSingle.h @@ -5,11 +5,16 @@ #ifndef FLOAT_H #define FLOAT_H +namespace Passer { + class Float { public: - static const float epsilon; - static const float sqrEpsilon; + static const float epsilon; + static const float sqrEpsilon; - static float Clamp(float f, float min, float max); + static float Clamp(float f, float min, float max); }; +} // namespace Passer +using namespace Passer; + #endif diff --git a/Matrix.h b/Matrix.h index 7eb80a9..3997464 100644 --- a/Matrix.h +++ b/Matrix.h @@ -3,6 +3,8 @@ #include "Vector3.h" +namespace Passer { + /// @brief Single precision float matrix template class MatrixOf { public: @@ -110,5 +112,7 @@ private: unsigned int cols; T *data; }; +} // namespace Passer +using namespace Passer; #endif \ No newline at end of file diff --git a/Polar.h b/Polar.h index 78d2b69..9e2c8ca 100644 --- a/Polar.h +++ b/Polar.h @@ -5,10 +5,9 @@ #ifndef POLAR_H #define POLAR_H -struct Vector2; - namespace Passer { +struct Vector2; struct Spherical; /// @@ -40,8 +39,16 @@ public: /// The distance in meters Polar(float angle, float distance); + /// + /// Convert a Vector2 to a Polar coordinate + /// + /// The 2D carthesian vector Polar(Vector2 v); + /// + /// Convert a Spherical coordinate to a Polar coordinate + /// + /// The spherical coordinate Polar(Spherical s); /// @@ -54,7 +61,6 @@ public: /// /// This will rotate the vector by 180 degrees. Distance will stay the same. /// The negated vector - Polar operator-(); /// /// Substract a polar vector from this coordinate diff --git a/Quaternion.h b/Quaternion.h index eb40a5d..079128f 100644 --- a/Quaternion.h +++ b/Quaternion.h @@ -7,6 +7,8 @@ #include "Vector3.h" +namespace Passer { + extern "C" { /// /// A quaternion @@ -36,7 +38,7 @@ typedef struct Quat { /// A quaternion /// struct Quaternion : Quat { - public: +public: /// /// Create a new identity quaternion /// @@ -54,6 +56,9 @@ struct Quaternion : Quat { /// /// Quaternion(Quat q); + /// + /// Quaternion destructor + /// ~Quaternion(); /// @@ -74,7 +79,7 @@ struct Quaternion : Quat { /// A unit quaternion /// This will preserve the orientation, /// but ensures that it is a unit quaternion. - static Quaternion Normalize(const Quaternion& q); + static Quaternion Normalize(const Quaternion &q); /// /// Convert to euler angles @@ -82,14 +87,14 @@ struct Quaternion : Quat { /// The quaternion to convert /// A vector containing euler angles /// The euler angles performed in the order: Z, X, Y - static Vector3 ToAngles(const Quaternion& q); + static Vector3 ToAngles(const Quaternion &q); /// /// Rotate a vector using this quaterion /// /// The vector to rotate /// The rotated vector - Vector3 operator*(const Vector3& vector) const; + Vector3 operator*(const Vector3 &vector) const; /// /// Multiply this quaternion with another quaternion /// @@ -97,7 +102,7 @@ struct Quaternion : Quat { /// The resulting rotation /// The result will be this quaternion rotated according to /// the give rotation. - Quaternion operator*(const Quaternion& rotation) const; + Quaternion operator*(const Quaternion &rotation) const; /// /// Check the equality of two quaternions @@ -108,7 +113,7 @@ struct Quaternion : Quat { /// themselves. Two quaternions with the same rotational effect may have /// different components. Use Quaternion::Angle to check if the rotations are /// the same. - bool operator==(const Quaternion& quaternion); + bool operator==(const Quaternion &quaternion); /// /// The inverse of quaterion @@ -123,8 +128,8 @@ struct Quaternion : Quat { /// The look direction /// The up direction /// The look rotation - static Quaternion LookRotation(const Vector3& forward, - const Vector3& upwards); + static Quaternion LookRotation(const Vector3 &forward, + const Vector3 &upwards); /// /// Creates a quaternion with the given forward direction with up = /// Vector3::up @@ -134,7 +139,7 @@ struct Quaternion : Quat { /// For the rotation, Vector::up is used for the up direction. /// Note: if the forward direction == Vector3::up, the result is /// Quaternion::identity - static Quaternion LookRotation(const Vector3& forward); + static Quaternion LookRotation(const Vector3 &forward); /// /// Calculat the rotation from on vector to another @@ -151,8 +156,7 @@ struct Quaternion : Quat { /// The destination rotation /// The maximum amount of degrees to /// rotate The possibly limited rotation - static Quaternion RotateTowards(const Quaternion& from, - const Quaternion& to, + static Quaternion RotateTowards(const Quaternion &from, const Quaternion &to, float maxDegreesDelta); /// @@ -161,13 +165,13 @@ struct Quaternion : Quat { /// The angle /// The axis /// The resulting quaternion - static Quaternion AngleAxis(float angle, const Vector3& axis); + static Quaternion AngleAxis(float angle, const Vector3 &axis); /// /// Convert this quaternion to angle/axis representation /// /// A pointer to the angle for the result /// A pointer to the axis for the result - void ToAngleAxis(float* angle, Vector3* axis); + void ToAngleAxis(float *angle, Vector3 *axis); /// /// Get the angle between two orientations @@ -185,9 +189,8 @@ struct Quaternion : Quat { /// The factor between 0 and 1. /// The resulting rotation /// A factor 0 returns rotation1, factor1 returns rotation2. - static Quaternion Slerp(const Quaternion& rotation1, - const Quaternion& rotation2, - float factor); + static Quaternion Slerp(const Quaternion &rotation1, + const Quaternion &rotation2, float factor); /// /// Unclamped sherical lerp between two rotations /// @@ -197,9 +200,8 @@ struct Quaternion : Quat { /// The resulting rotation /// A factor 0 returns rotation1, factor1 returns rotation2. /// Values outside the 0..1 range will result in extrapolated rotations - static Quaternion SlerpUnclamped(const Quaternion& rotation1, - const Quaternion& rotation2, - float factor); + static Quaternion SlerpUnclamped(const Quaternion &rotation1, + const Quaternion &rotation2, float factor); /// /// Create a rotation from euler angles @@ -257,10 +259,8 @@ struct Quaternion : Quat { /// A pointer to the quaternion for the swing /// result A pointer to the quaternion for the /// twist result - static void GetSwingTwist(Vector3 axis, - Quaternion rotation, - Quaternion* swing, - Quaternion* twist); + static void GetSwingTwist(Vector3 axis, Quaternion rotation, + Quaternion *swing, Quaternion *twist); /// /// Calculate the dot product of two quaternions @@ -270,16 +270,18 @@ struct Quaternion : Quat { /// static float Dot(Quaternion rotation1, Quaternion rotation2); - private: +private: float GetLength() const; float GetLengthSquared() const; - static float GetLengthSquared(const Quaternion& q); + static float GetLengthSquared(const Quaternion &q); - void ToAxisAngleRad(const Quaternion& q, Vector3* const axis, float* angle); + void ToAxisAngleRad(const Quaternion &q, Vector3 *const axis, float *angle); static Quaternion FromEulerRad(Vector3 euler); static Quaternion FromEulerRadXYZ(Vector3 euler); Vector3 xyz() const; }; +} // namespace Passer +using namespace Passer; -#endif \ No newline at end of file +#endif diff --git a/Range.h b/Range.h index bf632ae..59ac3f5 100644 --- a/Range.h +++ b/Range.h @@ -1,6 +1,8 @@ #ifndef RANGE_H #define RANGE_H +namespace Passer { + /* /// @brief Signed range. May be renamed to SignedRange later class Range16 { @@ -39,5 +41,7 @@ public: T value; }; +} // namespace Passer +using namespace Passer; #endif \ No newline at end of file diff --git a/Spherical.cpp b/Spherical.cpp index 2449a6c..4abebbb 100644 --- a/Spherical.cpp +++ b/Spherical.cpp @@ -36,25 +36,25 @@ Spherical::Spherical(Vector3 v) { const Spherical Spherical::zero = Spherical(0.0F, (Angle)0.0F, (Angle)0.0F); -float Spherical::GetSwing() { - // Not sure if this is correct - return sqrtf(horizontalAngle * horizontalAngle + - verticalAngle * verticalAngle); -} +// float Spherical::GetSwing() { +// // Not sure if this is correct +// return sqrtf(horizontalAngle * horizontalAngle + +// verticalAngle * verticalAngle); +// } -Polar Spherical::ProjectOnHorizontalPlane() { - return Polar(horizontalAngle, distance); -} +// Polar Spherical::ProjectOnHorizontalPlane() { +// return Polar(horizontalAngle, distance); +// } -Vector3 Spherical::ToVector3() { - float verticalRad = (90 - verticalAngle) * Angle::Deg2Rad; - float horizontalRad = horizontalAngle * Angle::Deg2Rad; - float cosVertical = cosf(verticalRad); - float sinVertical = sinf(verticalRad); - float cosHorizontal = cosf(horizontalRad); - float sinHorizontal = sinf(horizontalRad); - Vector3 v = Vector3(this->distance * sinVertical * sinHorizontal, - this->distance * cosVertical, - this->distance * sinVertical * cosHorizontal); - return v; -} \ No newline at end of file +// Vector3 Spherical::ToVector3() { +// float verticalRad = (90 - verticalAngle) * Angle::Deg2Rad; +// float horizontalRad = horizontalAngle * Angle::Deg2Rad; +// float cosVertical = cosf(verticalRad); +// float sinVertical = sinf(verticalRad); +// float cosHorizontal = cosf(horizontalRad); +// float sinHorizontal = sinf(horizontalRad); +// Vector3 v = Vector3(this->distance * sinVertical * sinHorizontal, +// this->distance * cosVertical, +// this->distance * sinVertical * cosHorizontal); +// return v; +// } \ No newline at end of file diff --git a/Spherical.h b/Spherical.h index 8231507..db75845 100644 --- a/Spherical.h +++ b/Spherical.h @@ -9,10 +9,10 @@ #include "Angle.h" #include "Polar.h" -struct Vector3; - namespace Passer { +struct Vector3; + /// @brief A spherical vector /// @details /// This is a vector in 3D space using a spherical coordinate system. @@ -39,7 +39,6 @@ public: /// zero is forward, positive is upward /// @param distance The distance in meters // Spherical(float polarAngle, float elevationAngle, float distance); - Spherical(float distance, Angle horizontalAngle, Angle verticalAngle); /// @brief Convert polar coordinates to spherical coordinates @@ -53,10 +52,11 @@ public: /// @brief A spherical vector with zero degree angles and distance const static Spherical zero; - float GetSwing(); + // float GetSwing(); - Polar ProjectOnHorizontalPlane(); - Vector3 ToVector3(); + // Polar ProjectOnHorizontalPlane(); + + // Vector3 ToVector3(); }; } // namespace Passer diff --git a/Vector2.h b/Vector2.h index 4d88ed3..5986451 100644 --- a/Vector2.h +++ b/Vector2.h @@ -24,7 +24,9 @@ typedef struct Vec2 { } Vec2; } -class Vector3; +namespace Passer { + +struct Vector3; /// /// A 2-dimensional vector @@ -48,8 +50,15 @@ public: /// The C-style Vec Vector2(Vec2 v); + /// + /// Convert a Vector3 to a Vector3 + /// + /// The 3D vector Vector2(Vector3 v); + /// + /// Vector2 destructor + /// @@ -234,5 +243,5 @@ public: /// between *from* and *to* etc. static Vector2 Lerp(Vector2 from, Vector2 to, float f); }; - +} // namespace Passer #endif \ No newline at end of file diff --git a/Vector3.cpp b/Vector3.cpp index c08c1c0..3ee597e 100644 --- a/Vector3.cpp +++ b/Vector3.cpp @@ -3,6 +3,9 @@ // file, You can obtain one at https ://mozilla.org/MPL/2.0/. #include "Vector3.h" +#include "Angle.h" +#include "Spherical.h" + #include const float Deg2Rad = 0.0174532924F; @@ -27,6 +30,23 @@ Vector3::Vector3(Vec3 v) { z = v.z; } +Vector3::Vector3(Spherical s) { + float verticalRad = (90 - s.verticalAngle) * Angle::Deg2Rad; + float horizontalRad = s.horizontalAngle * Angle::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); diff --git a/Vector3.h b/Vector3.h index 50fc1f3..13d2aa0 100644 --- a/Vector3.h +++ b/Vector3.h @@ -7,6 +7,10 @@ #include "Vector2.h" +namespace Passer { + +struct Spherical; + extern "C" { /// /// 3-dimensional Vector representation @@ -52,6 +56,10 @@ public: /// /// The C-style Vec Vector3(Vec3 v); + /// + /// Vector3 destructor + /// + Vector3(Spherical s); ~Vector3(); /// @@ -267,5 +275,7 @@ public: /// between *from* and *to* etc. static Vector3 Lerp(const Vector3 &from, const Vector3 &to, float f); }; +} // namespace Passer +using namespace Passer; #endif \ No newline at end of file