Fix namespaces

This commit is contained in:
Pascal Serrarens 2024-05-10 17:50:11 +02:00
parent 64ca76830c
commit 8e7a8c6432
16 changed files with 147 additions and 61 deletions

View File

@ -3,6 +3,8 @@
#include "Angle.h"
#include <math.h>
namespace Passer {
typedef AngleUsing<signed short> 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;

View File

@ -3,6 +3,7 @@
#include "Angle.h"
#include <math.h>
namespace Passer {
typedef AngleUsing<signed long> 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;

View File

@ -3,6 +3,8 @@
#include "Angle.h"
#include <math.h>
namespace Passer {
typedef AngleUsing<signed char> 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;

View File

@ -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

View File

@ -4,6 +4,8 @@
#include "Angle.h"
#include "Range.h"
namespace Passer {
// A fixed angle between (-180..180]
template <typename T> class AngleUsing {
@ -38,5 +40,7 @@ public:
// protected:
T value;
};
} // namespace Passer
using namespace Passer;
#endif

4
Axis.h
View File

@ -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

View File

@ -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

View File

@ -3,6 +3,8 @@
#include "Vector3.h"
namespace Passer {
/// @brief Single precision float matrix
template <typename T> class MatrixOf {
public:
@ -110,5 +112,7 @@ private:
unsigned int cols;
T *data;
};
} // namespace Passer
using namespace Passer;
#endif

12
Polar.h
View File

@ -5,10 +5,9 @@
#ifndef POLAR_H
#define POLAR_H
struct Vector2;
namespace Passer {
struct Vector2;
struct Spherical;
/// <summary>
@ -40,8 +39,16 @@ public:
/// <param name="distance">The distance in meters</param>
Polar(float angle, float distance);
/// <summary>
/// Convert a Vector2 to a Polar coordinate
/// </summary>
/// <param name="v">The 2D carthesian vector</param>
Polar(Vector2 v);
/// <summary>
/// Convert a Spherical coordinate to a Polar coordinate
/// </summary>
/// <param name="s">The spherical coordinate</param>
Polar(Spherical s);
/// <summary>
@ -54,7 +61,6 @@ public:
/// </summary>
/// This will rotate the vector by 180 degrees. Distance will stay the same.
/// <returns>The negated vector</returns>
Polar operator-();
/// <summary>
/// Substract a polar vector from this coordinate

View File

@ -7,6 +7,8 @@
#include "Vector3.h"
namespace Passer {
extern "C" {
/// <summary>
/// A quaternion
@ -36,7 +38,7 @@ typedef struct Quat {
/// A quaternion
/// </summary>
struct Quaternion : Quat {
public:
public:
/// <summary>
/// Create a new identity quaternion
/// </summary>
@ -54,6 +56,9 @@ struct Quaternion : Quat {
/// </summary>
/// <param name="q"></param>
Quaternion(Quat q);
/// <summary>
/// Quaternion destructor
/// </summary>
~Quaternion();
/// <summary>
@ -74,7 +79,7 @@ struct Quaternion : Quat {
/// <returns>A unit quaternion</returns>
/// 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);
/// <summary>
/// Convert to euler angles
@ -82,14 +87,14 @@ struct Quaternion : Quat {
/// <param name="q">The quaternion to convert</param>
/// <returns>A vector containing euler angles</returns>
/// The euler angles performed in the order: Z, X, Y
static Vector3 ToAngles(const Quaternion& q);
static Vector3 ToAngles(const Quaternion &q);
/// <summary>
/// Rotate a vector using this quaterion
/// </summary>
/// <param name="vector">The vector to rotate</param>
/// <returns>The rotated vector</returns>
Vector3 operator*(const Vector3& vector) const;
Vector3 operator*(const Vector3 &vector) const;
/// <summary>
/// Multiply this quaternion with another quaternion
/// </summary>
@ -97,7 +102,7 @@ struct Quaternion : Quat {
/// <returns>The resulting rotation</returns>
/// The result will be this quaternion rotated according to
/// the give rotation.
Quaternion operator*(const Quaternion& rotation) const;
Quaternion operator*(const Quaternion &rotation) const;
/// <summary>
/// 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);
/// <summary>
/// The inverse of quaterion
@ -123,8 +128,8 @@ struct Quaternion : Quat {
/// <param name="forward">The look direction</param>
/// <param name="upwards">The up direction</param>
/// <returns>The look rotation</returns>
static Quaternion LookRotation(const Vector3& forward,
const Vector3& upwards);
static Quaternion LookRotation(const Vector3 &forward,
const Vector3 &upwards);
/// <summary>
/// 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);
/// <summary>
/// Calculat the rotation from on vector to another
@ -151,8 +156,7 @@ struct Quaternion : Quat {
/// <param name="to">The destination rotation</param>
/// <param name="maxDegreesDelta">The maximum amount of degrees to
/// rotate</param> <returns>The possibly limited rotation</returns>
static Quaternion RotateTowards(const Quaternion& from,
const Quaternion& to,
static Quaternion RotateTowards(const Quaternion &from, const Quaternion &to,
float maxDegreesDelta);
/// <summary>
@ -161,13 +165,13 @@ struct Quaternion : Quat {
/// <param name="angle">The angle</param>
/// <param name="axis">The axis</param>
/// <returns>The resulting quaternion</returns>
static Quaternion AngleAxis(float angle, const Vector3& axis);
static Quaternion AngleAxis(float angle, const Vector3 &axis);
/// <summary>
/// Convert this quaternion to angle/axis representation
/// </summary>
/// <param name="angle">A pointer to the angle for the result</param>
/// <param name="axis">A pointer to the axis for the result</param>
void ToAngleAxis(float* angle, Vector3* axis);
void ToAngleAxis(float *angle, Vector3 *axis);
/// <summary>
/// Get the angle between two orientations
@ -185,9 +189,8 @@ struct Quaternion : Quat {
/// <param name="factor">The factor between 0 and 1.</param>
/// <returns>The resulting rotation</returns>
/// 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);
/// <summary>
/// Unclamped sherical lerp between two rotations
/// </summary>
@ -197,9 +200,8 @@ struct Quaternion : Quat {
/// <returns>The resulting rotation</returns>
/// 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);
/// <summary>
/// Create a rotation from euler angles
@ -257,10 +259,8 @@ struct Quaternion : Quat {
/// <param name="swing">A pointer to the quaternion for the swing
/// result</param> <param name="twist">A pointer to the quaternion for the
/// twist result</param>
static void GetSwingTwist(Vector3 axis,
Quaternion rotation,
Quaternion* swing,
Quaternion* twist);
static void GetSwingTwist(Vector3 axis, Quaternion rotation,
Quaternion *swing, Quaternion *twist);
/// <summary>
/// Calculate the dot product of two quaternions
@ -270,16 +270,18 @@ struct Quaternion : Quat {
/// <returns></returns>
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
#endif

View File

@ -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

View File

@ -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;
}
// 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;
// }

View File

@ -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

View File

@ -24,7 +24,9 @@ typedef struct Vec2 {
} Vec2;
}
class Vector3;
namespace Passer {
struct Vector3;
/// <summary>
/// A 2-dimensional vector
@ -48,8 +50,15 @@ public:
/// <param name="v">The C-style Vec</param>
Vector2(Vec2 v);
/// <summary>
/// Convert a Vector3 to a Vector3
/// </summary>
/// <param name="v">The 3D vector</param>
Vector2(Vector3 v);
/// <summary>
/// Vector2 destructor
/// </summary
~Vector2();
/// <summary>
@ -234,5 +243,5 @@ public:
/// between *from* and *to* etc.
static Vector2 Lerp(Vector2 from, Vector2 to, float f);
};
} // namespace Passer
#endif

View File

@ -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 <math.h>
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);

View File

@ -7,6 +7,10 @@
#include "Vector2.h"
namespace Passer {
struct Spherical;
extern "C" {
/// <summary>
/// 3-dimensional Vector representation
@ -52,6 +56,10 @@ public:
/// </summary>
/// <param name="v">The C-style Vec</param>
Vector3(Vec3 v);
/// <summary>
/// Vector3 destructor
/// </summary>
Vector3(Spherical s);
~Vector3();
/// <summary>
@ -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