Cleanup, added utility functions

This commit is contained in:
Pascal Serrarens 2024-05-13 15:32:42 +02:00
parent f7d9d976fc
commit 95713c8621
11 changed files with 376 additions and 380 deletions

16
Angle.h
View File

@ -39,23 +39,7 @@ private:
}; };
using Angle = AngleOf<float>; using Angle = AngleOf<float>;
/*
class Angle {
public:
const static float Rad2Deg;
const static float Deg2Rad;
static float Normalize(float angle);
static float Clamp(float angle, float min, float max);
static float Difference(float a, float b);
static float MoveTowards(float fromAngle, float toAngle, float maxAngle);
static float CosineRuleSide(float a, float b, float gamma);
static float CosineRuleAngle(float a, float b, float c);
static float SineRuleAngle(float a, float beta, float c);
};
*/
} // namespace Passer } // namespace Passer
using namespace Passer; using namespace Passer;

View File

@ -15,9 +15,9 @@ template <> MatrixOf<float>::MatrixOf(unsigned int rows, unsigned int cols) {
} }
template <> MatrixOf<float>::MatrixOf(Vector3 v) : MatrixOf(3, 1) { template <> MatrixOf<float>::MatrixOf(Vector3 v) : MatrixOf(3, 1) {
Set(0, 0, v.x); Set(0, 0, v.Right());
Set(1, 0, v.y); Set(1, 0, v.Up());
Set(2, 0, v.z); Set(2, 0, v.Forward());
} }
template <> template <>
@ -27,7 +27,7 @@ void MatrixOf<float>::Multiply(const MatrixOf<float> *m1,
for (unsigned int colIx2 = 0; colIx2 < m2->cols; colIx2++) { for (unsigned int colIx2 = 0; colIx2 < m2->cols; colIx2++) {
unsigned int rDataIx = colIx2 * m2->cols + rowIx1; unsigned int rDataIx = colIx2 * m2->cols + rowIx1;
r->data[rDataIx] = 0.0F; r->data[rDataIx] = 0.0F;
for (int kIx = 0; kIx < m2->rows; kIx++) { for (unsigned int kIx = 0; kIx < m2->rows; kIx++) {
unsigned int dataIx1 = rowIx1 * m1->cols + kIx; unsigned int dataIx1 = rowIx1 * m1->cols + kIx;
unsigned int dataIx2 = kIx * m2->cols + colIx2; unsigned int dataIx2 = kIx * m2->cols + colIx2;
r->data[rDataIx] += m1->data[dataIx1] * m2->data[dataIx2]; r->data[rDataIx] += m1->data[dataIx1] * m2->data[dataIx2];

View File

@ -115,13 +115,21 @@ Vector3 Quaternion::operator*(const Vector3 &p) const {
float num10 = this->w * num; float num10 = this->w * num;
float num11 = this->w * num2; float num11 = this->w * num2;
float num12 = this->w * num3; float num12 = this->w * num3;
Vector3 result = Vector3::zero;
result.x = float px = p.Right();
(1 - (num5 + num6)) * p.x + (num7 - num12) * p.y + (num8 + num11) * p.z; float py = p.Up();
result.y = float pz = p.Forward();
(num7 + num12) * p.x + (1 - (num4 + num6)) * p.y + (num9 - num10) * p.z; // Vector3 result = Vector3::zero;
result.z = // result.x =
(num8 - num11) * p.x + (num9 + num10) * p.y + (1 - (num4 + num5)) * p.z; float rx =
(1 - (num5 + num6)) * px + (num7 - num12) * py + (num8 + num11) * pz;
// result.y =
float ry =
(num7 + num12) * px + (1 - (num4 + num6)) * py + (num9 - num10) * pz;
// result.z =
float rz =
(num8 - num11) * px + (num9 + num10) * py + (1 - (num4 + num5)) * pz;
Vector3 result = Vector3(rx, ry, rz);
return result; return result;
} }
@ -142,15 +150,15 @@ Quaternion Quaternion::LookRotation(const Vector3 &forward, const Vector3 &up) {
Vector3 nForward = Vector3::Normalize(forward); Vector3 nForward = Vector3::Normalize(forward);
Vector3 nRight = Vector3::Normalize(Vector3::Cross(up, nForward)); Vector3 nRight = Vector3::Normalize(Vector3::Cross(up, nForward));
Vector3 nUp = Vector3::Cross(nForward, nRight); Vector3 nUp = Vector3::Cross(nForward, nRight);
float m00 = nRight.x; float m00 = nRight.Right(); // x;
float m01 = nRight.y; float m01 = nRight.Up(); // y;
float m02 = nRight.z; float m02 = nRight.Forward(); // z;
float m10 = nUp.x; float m10 = nUp.Right(); // x;
float m11 = nUp.y; float m11 = nUp.Up(); // y;
float m12 = nUp.z; float m12 = nUp.Forward(); // z;
float m20 = nForward.x; float m20 = nForward.Right(); // x;
float m21 = nForward.y; float m21 = nForward.Up(); // y;
float m22 = nForward.z; float m22 = nForward.Forward(); // z;
float num8 = (m00 + m11) + m22; float num8 = (m00 + m11) + m22;
Quaternion quaternion = Quaternion(); Quaternion quaternion = Quaternion();
@ -219,9 +227,9 @@ Quaternion Quaternion::AngleAxis(float angle, const Vector3 &axis) {
radians *= 0.5f; radians *= 0.5f;
Vector3 axis2 = axis * (float)sin(radians); Vector3 axis2 = axis * (float)sin(radians);
result.x = axis2.x; result.x = axis2.Right(); // x;
result.y = axis2.y; result.y = axis2.Up(); // y;
result.z = axis2.z; result.z = axis2.Forward(); // z;
result.w = (float)cos(radians); result.w = (float)cos(radians);
return Quaternion::Normalize(result); return Quaternion::Normalize(result);
@ -293,7 +301,8 @@ Quaternion Quaternion::SlerpUnclamped(const Quaternion &a, const Quaternion &b,
blendB = t; blendB = t;
} }
Vector3 v = axyz * blendA + b2.xyz() * blendB; Vector3 v = axyz * blendA + b2.xyz() * blendB;
Quaternion result = Quaternion(v.x, v.y, v.z, blendA * a.w + blendB * b2.w); Quaternion result =
Quaternion(v.Right(), v.Up(), v.Forward(), blendA * a.w + blendB * b2.w);
if (result.GetLengthSquared() > 0.0f) if (result.GetLengthSquared() > 0.0f)
return Quaternion::Normalize(result); return Quaternion::Normalize(result);
else else
@ -317,9 +326,9 @@ Quaternion Quaternion::Euler(Vector3 euler) {
} }
Quaternion Quaternion::FromEulerRad(Vector3 euler) { Quaternion Quaternion::FromEulerRad(Vector3 euler) {
float yaw = euler.x; float yaw = euler.Right();
float pitch = euler.y; float pitch = euler.Up();
float roll = euler.z; float roll = euler.Forward();
float rollOver2 = roll * 0.5f; float rollOver2 = roll * 0.5f;
float sinRollOver2 = (float)sin((float)rollOver2); float sinRollOver2 = (float)sin((float)rollOver2);
float cosRollOver2 = (float)cos((float)rollOver2); float cosRollOver2 = (float)cos((float)rollOver2);
@ -348,9 +357,9 @@ Quaternion Quaternion::EulerXYZ(Vector3 euler) {
return Quaternion::FromEulerRadXYZ(euler * Deg2Rad); return Quaternion::FromEulerRadXYZ(euler * Deg2Rad);
} }
Quaternion Quaternion::FromEulerRadXYZ(Vector3 euler) { Quaternion Quaternion::FromEulerRadXYZ(Vector3 euler) {
float yaw = euler.x; float yaw = euler.Right(); // x;
float pitch = euler.y; float pitch = euler.Up(); // y;
float roll = euler.z; float roll = euler.Forward(); // z;
float rollOver2 = roll * 0.5f; float rollOver2 = roll * 0.5f;
float sinRollOver2 = (float)sin((float)rollOver2); float sinRollOver2 = (float)sin((float)rollOver2);
float cosRollOver2 = (float)cos((float)rollOver2); float cosRollOver2 = (float)cos((float)rollOver2);
@ -389,7 +398,7 @@ Quaternion Quaternion::GetRotationAround(Vector3 axis, Quaternion rotation) {
Vector3 ra = Vector3(rotation.x, rotation.y, rotation.z); // rotation axis Vector3 ra = Vector3(rotation.x, rotation.y, rotation.z); // rotation axis
Vector3 p = Vector3::Project( Vector3 p = Vector3::Project(
ra, axis); // return projection ra on to axis (parallel component) ra, axis); // return projection ra on to axis (parallel component)
Quaternion twist = Quaternion(p.x, p.y, p.z, rotation.w); Quaternion twist = Quaternion(p.Right(), p.Up(), p.Forward(), rotation.w);
twist = Quaternion::Normalize(twist); twist = Quaternion::Normalize(twist);
return twist; return twist;
} }

View File

@ -37,8 +37,8 @@ Spherical::Spherical(Vector3 v) {
this->horizontalAngle = 0.0f; this->horizontalAngle = 0.0f;
} else { } else {
this->verticalAngle = this->verticalAngle =
(90.0f - acosf(v.y / this->distance) * Angle::Rad2Deg); (90.0f - acosf(v.Up() / this->distance) * Angle::Rad2Deg);
this->horizontalAngle = atan2f(v.x, v.z) * Angle::Rad2Deg; this->horizontalAngle = atan2f(v.Right(), v.Forward()) * Angle::Rad2Deg;
} }
} }

View File

@ -21,13 +21,13 @@ Vector2::Vector2(float _x, float _y) {
x = _x; x = _x;
y = _y; y = _y;
} }
Vector2::Vector2(Vec2 v) { // Vector2::Vector2(Vec2 v) {
x = v.x; // x = v.x;
y = v.y; // y = v.y;
} // }
Vector2::Vector2(Vector3 v) { Vector2::Vector2(Vector3 v) {
x = v.x; x = v.Right(); // x;
y = v.z; y = v.Forward(); // z;
} }
Vector2::Vector2(Polar p) { Vector2::Vector2(Polar p) {
float horizontalRad = p.angle * Angle::Deg2Rad; float horizontalRad = p.angle * Angle::Deg2Rad;
@ -53,11 +53,11 @@ bool Vector2::operator==(const Vector2 &v) {
return (this->x == v.x && this->y == v.y); return (this->x == v.x && this->y == v.y);
} }
float Vector2::Magnitude(const Vector2 &a) { float Vector2::Magnitude(const Vector2 &v) {
return sqrtf(a.x * a.x + a.y * a.y); return sqrtf(v.x * v.x + v.y * v.y);
} }
float Vector2::magnitude() const { return (float)sqrtf(x * x + y * y); } float Vector2::magnitude() const { return (float)sqrtf(x * x + y * y); }
float Vector2::SqrMagnitude(const Vector2 &a) { return a.x * a.x + a.y * a.y; } float Vector2::SqrMagnitude(const Vector2 &v) { return v.x * v.x + v.y * v.y; }
float Vector2::sqrMagnitude() const { return (x * x + y * y); } float Vector2::sqrMagnitude() const { return (x * x + y * y); }
Vector2 Vector2::Normalize(const Vector2 &v) { Vector2 Vector2::Normalize(const Vector2 &v) {
@ -79,21 +79,47 @@ Vector2 Vector2::normalized() const {
Vector2 Vector2::operator-() { return Vector2(-this->x, -this->y); } Vector2 Vector2::operator-() { return Vector2(-this->x, -this->y); }
Vector2 Vector2::operator-(const Vector2 &v2) const { Vector2 Vector2::operator-(const Vector2 &v) const {
return Vector2(this->x - v2.x, this->y - v2.y); return Vector2(this->x - v.x, this->y - v.y);
} }
Vector2 Vector2::operator+(const Vector2 &v2) const { Vector2 Vector2::operator-=(const Vector2 &v) {
return Vector2(this->x + v2.x, this->y + v2.y); this->x -= v.x;
this->y -= v.y;
return *this;
}
Vector2 Vector2::operator+(const Vector2 &v) const {
return Vector2(this->x + v.x, this->y + v.y);
}
Vector2 Vector2::operator+=(const Vector2 &v) {
this->x += v.x;
this->y += v.y;
return *this;
} }
Vector2 Vector2::Scale(const Vector2 &p1, const Vector2 &p2) { Vector2 Vector2::Scale(const Vector2 &v1, const Vector2 &v2) {
return Vector2(p1.x * p2.x, p1.y * p2.y); return Vector2(v1.x * v2.x, v1.y * v2.y);
} }
Vector2 Vector2::operator*(float f) const { Vector2 Passer::operator*(const Vector2 &v, const float f) {
return Vector2(this->x * f, this->y * f); return Vector2(v.x * f, v.y * f);
} }
Vector2 Vector2::operator/(float f) const { Vector2 Passer::operator*(const float f, const Vector2 &v) {
return Vector2(this->x / f, this->y / f); return Vector2(v.x * f, v.y * f);
}
Vector2 Vector2::operator*=(const float f) {
this->x *= f;
this->y *= f;
return *this;
}
Vector2 Passer::operator/(const Vector2 &v, const float f) {
return Vector2(v.x / f, v.y / f);
}
Vector2 Passer::operator/(const float f, const Vector2 &v) {
return Vector2(v.x / f, v.y / f);
}
Vector2 Vector2::operator/=(const float f) {
this->x /= f;
this->y /= f;
return *this;
} }
float Vector2::Dot(const Vector2 &v1, const Vector2 &v2) { float Vector2::Dot(const Vector2 &v1, const Vector2 &v2) {
@ -120,8 +146,8 @@ float Vector2::SignedAngle(const Vector2 &v1, const Vector2 &v2) {
return nanf(""); return nanf("");
#endif #endif
float angleFrom = atan2(v1.y, v1.x); float angleFrom = atan2f(v1.y, v1.x);
float angleTo = atan2(v2.y, v2.x); float angleTo = atan2f(v2.y, v2.x);
return -(angleTo - angleFrom) * Angle::Rad2Deg; return -(angleTo - angleFrom) * Angle::Rad2Deg;
} }

View File

@ -35,16 +35,15 @@ struct Polar;
/// @remark This uses the right=handed carthesian coordinate system. /// @remark This uses the right=handed carthesian coordinate system.
/// @note This implementation intentionally avoids the use of x and y /// @note This implementation intentionally avoids the use of x and y
struct Vector2 : Vec2 { struct Vector2 : Vec2 {
friend struct Vec2;
public: public:
/// @brief A new 2-dimensional zero vector /// @brief A new 2-dimensional zero vector
Vector2(); Vector2();
/// @brief A new 2-dimensional vector /// @brief A new 2-dimensional vector
/// @param sideward The sideward value /// @param right The distance in the right direction in meters
/// @param forward The forward value /// @param forward The distance in the forward direction in meters
Vector2(float sideward, float forward); Vector2(float right, float forward);
/// @brief A vector created from a C-style Vec2
/// @param v The C-syle Vec2
Vector2(Vec2 v);
/// @brief Convert a Vector3 to a Vector2 /// @brief Convert a Vector3 to a Vector2
/// @param v The 3D vector /// @param v The 3D vector
/// @note This will project the vector to the horizontal plane /// @note This will project the vector to the horizontal plane
@ -64,18 +63,18 @@ public:
const static Vector2 right; const static Vector2 right;
/// @brief A normalized left-oriented vector /// @brief A normalized left-oriented vector
const static Vector2 left; const static Vector2 left;
/// @brief A normalized up-oriented vector
/// @note This is equal to Vector2::forward
const static Vector2 up;
/// @brief A normalized down-oriented vector
/// @note This is equal to Vector2::down
const static Vector2 down;
/// @brief A normalized forward-oriented vector /// @brief A normalized forward-oriented vector
/// @note This is equal to Vector2::up /// @note This is equal to Vector2::up
const static Vector2 forward; const static Vector2 forward;
/// @brief A normalized back-oriented vector /// @brief A normalized back-oriented vector
/// @note This is equal to Vector2::down /// @note This is equal to Vector2::down
const static Vector2 back; const static Vector2 back;
/// @brief A normalized up-oriented vector
/// @note This is a convenience function which is equal to Vector2::forward
const static Vector2 up;
/// @brief A normalized down-oriented vector
/// @note This is a convenience function which is equal to Vector2::down
const static Vector2 down;
/// @brief Check if this vector to the given vector /// @brief Check if this vector to the given vector
/// @param v The vector to check against /// @param v The vector to check against
@ -121,12 +120,14 @@ public:
/// @param v The vector to subtract from this vector /// @param v The vector to subtract from this vector
/// @return The result of the subtraction /// @return The result of the subtraction
Vector2 operator-(const Vector2 &v) const; Vector2 operator-(const Vector2 &v) const;
Vector2 operator-=(const Vector2 &v);
/// @brief Add a vector to this vector /// @brief Add a vector to this vector
/// @param v The vector to add to this vector /// @param v The vector to add to this vector
/// @return The result of the addition /// @return The result of the addition
Vector2 operator+(const Vector2 &v) const; Vector2 operator+(const Vector2 &v) const;
Vector2 operator+=(const Vector2 &v);
/// @brief Scale a vector using another vector /// @brief Scale the vector using another vector
/// @param v1 The vector to scale /// @param v1 The vector to scale
/// @param v2 A vector with the scaling factors /// @param v2 A vector with the scaling factors
/// @return The scaled vector /// @return The scaled vector
@ -138,12 +139,16 @@ public:
/// @return The scaled vector /// @return The scaled vector
/// @remark Each component of the vector will be multipled with the same /// @remark Each component of the vector will be multipled with the same
/// factor f. /// factor f.
Vector2 operator*(float f) const; friend Vector2 operator*(const Vector2 &v, const float f);
friend Vector2 operator*(const float f, const Vector2 &v);
Vector2 operator*=(const float f);
/// @brief Scale the vector uniformly down /// @brief Scale the vector uniformly down
/// @param f The scaling factor /// @param f The scaling factor
/// @return The scaled vector /// @return The scaled vector
/// @remark Each componet of the vector will be divided by the same factor. /// @remark Each componet of the vector will be divided by the same factor.
Vector2 operator/(float f) const; friend Vector2 operator/(const Vector2 &v, const float f);
friend Vector2 operator/(const float f, const Vector2 &v);
Vector2 operator/=(const float f);
/// @brief The dot product of two vectors /// @brief The dot product of two vectors
/// @param v1 The first vector /// @param v1 The first vector

View File

@ -13,21 +13,21 @@ const float Rad2Deg = 57.29578F;
const float epsilon = 1E-05f; const float epsilon = 1E-05f;
Vector3::Vector3() { Vector3::Vector3() {
x = 0; this->x = 0;
y = 0; this->y = 0;
z = 0; this->z = 0;
} }
Vector3::Vector3(float _x, float _y, float _z) { Vector3::Vector3(float right, float up, float forward) {
x = _x; this->x = right;
y = _y; this->y = up;
z = _z; this->z = forward;
} }
Vector3::Vector3(Vec3 v) { Vector3::Vector3(Vector2 v) {
x = v.x; this->x = v.x;
y = v.y; this->y = 0.0f;
z = v.z; this->z = v.y;
} }
Vector3::Vector3(Spherical s) { Vector3::Vector3(Spherical s) {
@ -61,17 +61,17 @@ const Vector3 Vector3::back = Vector3(0, 0, -1);
// inline float Vector3::Forward() { return z; } // inline float Vector3::Forward() { return z; }
// inline float Vector3::Up() { return y; } // inline float Vector3::Up() { return y; }
// inline float Vector3::Right() { return x; } // inline float Vector3::Right() { return x; }
Vector3 Vector3::FromHorizontal(const Vector2 &v) { // Vector3 Vector3::FromHorizontal(const Vector2 &v) {
return Vector3(v.x, 0, v.y); // return Vector3(v.x, 0, v.y);
} // }
float Vector3::Magnitude(const Vector3 &a) { float Vector3::Magnitude(const Vector3 &v) {
return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z); 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::magnitude() const { return (float)sqrtf(x * x + y * y + z * z); }
float Vector3::SqrMagnitude(const Vector3 &a) { float Vector3::SqrMagnitude(const Vector3 &v) {
return a.x * a.x + a.y * a.y + a.z * a.z; return v.x * v.x + v.y * v.y + v.z * v.z;
} }
float Vector3::sqrMagnitude() const { return (x * x + y * y + z * z); } float Vector3::sqrMagnitude() const { return (x * x + y * y + z * z); }
@ -92,26 +92,55 @@ Vector3 Vector3::normalized() const {
return result; return result;
} }
Vector3 Vector3::operator-(const Vector3 &v2) const { Vector3 Vector3::operator-() const {
return Vector3(this->x - v2.x, this->y - v2.y, this->z - v2.z); return Vector3(-this->x, -this->y, -this->z);
} }
Vector3 Vector3::operator-() { 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 &v2) const { }
return Vector3(this->x + v2.x, this->y + v2.y, this->z + v2.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 &p1, const Vector3 &p2) { Vector3 Vector3::Scale(const Vector3 &v1, const Vector3 &v2) {
return Vector3(p1.x * p2.x, p1.y * p2.y, p1.z * p2.z); return Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
} }
Vector3 Passer::operator*(const Vector3 &v, const float f) {
Vector3 Vector3::operator*(float f) const { return Vector3(v.x * f, v.y * f, v.z * f);
return Vector3(this->x * f, this->y * f, this->z * f);
} }
Vector3 Passer::operator*(const float f, const Vector3 &v) {
Vector3 Vector3::operator/(float d) const { return Vector3(v.x * f, v.y * f, v.z * f);
return Vector3(this->x / d, this->y / d, this->z / d); }
Vector3 Vector3::operator*=(const float f) {
this->x *= f;
this->y *= f;
this->z *= f;
return *this;
}
Vector3 Passer::operator/(const Vector3 &v, const float f) {
return Vector3(v.x / f, v.y / f, v.z / f);
}
Vector3 Passer::operator/(const float f, const Vector3 &v) {
return Vector3(v.x / f, v.y / f, v.z / f);
}
Vector3 Vector3::operator/=(const float f) {
this->x /= f;
this->y /= f;
this->z /= f;
return *this;
} }
float Vector3::Dot(const Vector3 &v1, const Vector3 &v2) { float Vector3::Dot(const Vector3 &v1, const Vector3 &v2) {
@ -122,8 +151,8 @@ bool Vector3::operator==(const Vector3 &v) {
return (this->x == v.x && this->y == v.y && this->z == v.z); return (this->x == v.x && this->y == v.y && this->z == v.z);
} }
float Vector3::Distance(const Vector3 &p1, const Vector3 &p2) { float Vector3::Distance(const Vector3 &v1, const Vector3 &v2) {
return Magnitude(p1 - p2); return Magnitude(v1 - v2);
} }
Vector3 Vector3::Cross(const Vector3 &v1, const Vector3 &v2) { Vector3 Vector3::Cross(const Vector3 &v1, const Vector3 &v2) {
@ -131,25 +160,19 @@ Vector3 Vector3::Cross(const Vector3 &v1, const Vector3 &v2) {
v1.x * v2.y - v1.y * v2.x); v1.x * v2.y - v1.y * v2.x);
} }
Vector3 Vector3::Project(const Vector3 &vector, const Vector3 &onNormal) { Vector3 Vector3::Project(const Vector3 &v, const Vector3 &n) {
float sqrMagnitude = Dot(onNormal, onNormal); float sqrMagnitude = Dot(n, n);
if (sqrMagnitude < epsilon) if (sqrMagnitude < epsilon)
return Vector3::zero; return Vector3::zero;
else { else {
float dot = Dot(vector, onNormal); float dot = Dot(v, n);
Vector3 r = onNormal * dot / sqrMagnitude; Vector3 r = n * dot / sqrMagnitude;
return r; return r;
} }
} }
Vector3 Vector3::ProjectOnPlane(const Vector3 &vector, Vector3 Vector3::ProjectOnPlane(const Vector3 &v, const Vector3 &n) {
const Vector3 &planeNormal) { Vector3 r = v - Project(v, n);
Vector3 r = vector - Project(vector, planeNormal);
return r;
}
Vector2 Vector3::ProjectHorizontalPlane(const Vector3 &vector) {
Vector2 r = Vector2(vector.x, vector.z);
return r; return r;
} }
@ -159,12 +182,12 @@ float clamp(float x, float lower, float upper) {
return upperClamp; return upperClamp;
} }
float Vector3::Angle(const Vector3 &from, const Vector3 &to) { float Vector3::Angle(const Vector3 &v1, const Vector3 &v2) {
float denominator = sqrtf(from.sqrMagnitude() * to.sqrMagnitude()); float denominator = sqrtf(v1.sqrMagnitude() * v2.sqrMagnitude());
if (denominator < epsilon) if (denominator < epsilon)
return 0; return 0;
float dot = Vector3::Dot(from, to); float dot = Vector3::Dot(v1, v2);
float fraction = dot / denominator; float fraction = dot / denominator;
if (isnan(fraction)) if (isnan(fraction))
return fraction; // short cut to returning NaN universally return fraction; // short cut to returning NaN universally
@ -174,12 +197,12 @@ float Vector3::Angle(const Vector3 &from, const Vector3 &to) {
return r; return r;
} }
float Vector3::SignedAngle(const Vector3 &from, const Vector3 &to, float Vector3::SignedAngle(const Vector3 &v1, const Vector3 &v2,
const Vector3 &axis) { const Vector3 &axis) {
// angle in [0,180] // angle in [0,180]
float angle = Vector3::Angle(from, to); float angle = Vector3::Angle(v1, v2);
Vector3 cross = Vector3::Cross(from, to); Vector3 cross = Vector3::Cross(v1, v2);
float b = Vector3::Dot(axis, cross); float b = Vector3::Dot(axis, cross);
float signd = b < 0 ? -1.0F : (b > 0 ? 1.0F : 0.0F); float signd = b < 0 ? -1.0F : (b > 0 ? 1.0F : 0.0F);
@ -189,7 +212,7 @@ float Vector3::SignedAngle(const Vector3 &from, const Vector3 &to,
return signed_angle; return signed_angle;
} }
Vector3 Vector3::Lerp(const Vector3 &from, const Vector3 &to, float f) { Vector3 Vector3::Lerp(const Vector3 &v1, const Vector3 &v2, float f) {
Vector3 v = from + (to - from) * f; Vector3 v = v1 + (v2 - v1) * f;
return v; return v;
} }

360
Vector3.h
View File

@ -18,6 +18,7 @@ extern "C" {
/// This is a C-style implementation /// This is a C-style implementation
/// This uses the right-handed coordinate system. /// This uses the right-handed coordinate system.
typedef struct Vec3 { typedef struct Vec3 {
protected:
/// <summary> /// <summary>
/// The right axis of the vector /// The right axis of the vector
/// </summary> /// </summary>
@ -34,247 +35,184 @@ typedef struct Vec3 {
} Vec3; } Vec3;
} }
/// <summary> /// @brief A 3-dimensional vector
/// A 3-dimensional vector /// @remark This uses a right-handed carthesian coordinate system.
/// </summary> /// @note This implementation intentionally avoids the use of x, y and z values.
/// This uses the right-handed coordinate system.
struct Vector3 : Vec3 { struct Vector3 : Vec3 {
friend struct Vec3;
public: public:
/// <summary> /// @brief A new 3-dimensional zero vector
/// Create a new 3-dimensinal zero vector
/// </summary>
Vector3(); Vector3();
/// <summary> /// @brief A new 3-dimensional vector
/// Create a new 3-dimensional vector /// @param right The distance in the right direction in meters
/// </summary> /// @param up The distance in the upward direction in meters
/// <param name="x">x axis value</param> /// @param forward The distance in the forward direction in meters
/// <param name="y">y axis value</param> Vector3(float right, float up, float forward);
/// <param name="z">z axis value</param> /// @brief Convert a 2-dimenstional vector to a 3-dimensional vector
Vector3(float x, float y, float z); /// @param v The vector to convert
/// <summary> Vector3(Vector2 v);
/// Create a vector from C-style Vec3 /// @brief Convert vector in spherical coordinates to 3d carthesian
/// </summary> /// coordinates
/// <param name="v">The C-style Vec</param> /// @param v The vector to convert
Vector3(Vec3 v); Vector3(Spherical v);
/// <summary>
/// Vector3 destructor /// @brief Vector3 destructor
/// </summary>
Vector3(Spherical s);
~Vector3(); ~Vector3();
/// <summary> /// @brief A vector with zero for all axis
/// A vector with zero for all axis
/// </summary>
const static Vector3 zero; const static Vector3 zero;
/// <summary> /// @brief A vector with one for all axis
/// A vector with one for all axis
/// </summary>
const static Vector3 one; const static Vector3 one;
/// <summary> /// @brief A normalized right-oriented vector
/// A normalized vector pointing in the right direction
/// </summary>
const static Vector3 right; const static Vector3 right;
/// <summary> /// @brief A normalized left-oriented vector
/// A normalized vector pointing in the left direction
/// </summary>
const static Vector3 left; const static Vector3 left;
/// <summary> /// @brief A normalized up-oriented vector
/// A normalized vector pointing in the upward direction
/// </summary>
const static Vector3 up; const static Vector3 up;
/// <summary> /// @brief A normalized down-oriented vector
/// A normalized vector pointing in the downward direcion
/// </summary>
const static Vector3 down; const static Vector3 down;
/// <summary> /// @brief A normalized forward-oriented vector
/// A normalized vector pointing in the forward direction
/// </summary>
const static Vector3 forward; const static Vector3 forward;
/// <summary> /// @brief A normalized back-oriented vector
/// A normalized vector pointing in the backward direction
/// </summary>
const static Vector3 back; const static Vector3 back;
// Experimental Access functions which are intended to replace the use of XYZ // Access functions which are intended to replace the use of XYZ
inline float Forward() { return z; }; inline float Forward() const { return z; };
inline float Up() { return y; }; inline float Up() const { return y; };
inline float Right() { return x; }; inline float Right() const { return x; };
static Vector3 FromHorizontal(const Vector2 &vector);
/// <summary> /// @brief Check if this vector to the given vector
/// The length of a vector /// @param v The vector to check against
/// </summary> /// @return true if it is identical to the given vector
/// <param name="vector">The vector for which you need the length</param> /// @note This uses float comparison to check equality which may have strange
/// <returns>The length of the given vector</returns> /// effects. Equality on floats should be avoided.
static float Magnitude(const Vector3 &vector); bool operator==(const Vector3 &v);
/// <summary>
/// The length of this vector /// @brief The vector length
/// </summary> /// @param v The vector for which you need the length
/// <returns>The length of this vector</returns> /// @return The vector length
static float Magnitude(const Vector3 &v);
/// @brief The vector length
/// @return The vector length
float magnitude() const; float magnitude() const;
/// <summary> /// @brief The squared vector length
/// The squared length of a vector /// @param v The vector for which you need the length
/// </summary> /// @return The squared vector length
/// <param name="vector">The vector for which you need the squared /// @remark The squared length is computationally simpler than the real
/// length</param> <returns>The squatred length</returns> The squared length /// length. Think of Pythagoras A^2 + B^2 = C^2. This leaves out the
/// is computationally simpler than the real length. Think of Pythagoras A^2 + /// calculation of the squared root of C.
/// B^2 = C^2. This leaves out the calculation of the squared root of C. static float SqrMagnitude(const Vector3 &v);
static float SqrMagnitude(const Vector3 &vector); /// @brief The squared vector length
/// <summary> /// @return The squared vector length
/// The squared length of this vector /// @remark The squared length is computationally simpler than the real
/// </summary> /// length. Think of Pythagoras A^2 + B^2 = C^2. This leaves out the
/// <returns>The squared length</returns> /// calculation of the squared root of C.
/// The squared length is computationally simpler than the real length.
/// Think of Pythagoras A^2 + B^2 = C^2.
/// This leaves out the calculation of the squared root of C.
float sqrMagnitude() const; float sqrMagnitude() const;
/// <summary>
/// Connvert a vector to a length of 1 /// @brief Convert the vector to a length of 1
/// </summary> /// @param v The vector to convert
/// <param name="vector">The vector to convert</param> /// @return The vector normalized to a length of 1
/// <returns>The vector with length 1</returns> static Vector3 Normalize(const Vector3 &v);
static Vector3 Normalize(const Vector3 &vector); /// @brief Convert the vector to a length of 1
/// <summary> /// @return The vector normalized to a length of 1
/// Convert the vector to a length of a
/// </summary>
/// <returns>The vector with length 1</returns>
Vector3 normalized() const; Vector3 normalized() const;
/// <summary> /// @brief Negate te vector such that it points in the opposite direction
/// Negate the vector /// @return The negated vector
/// </summary> Vector3 operator-() const;
/// <returns>The negated vector</returns>
/// This will result in a vector pointing in the opposite direction
Vector3 operator-();
/// <summary>
/// Subtract a vector from this vector
/// </summary>
/// <param name="vector">The vector to subtract from this vector</param>
/// <returns>The result of the subtraction</returns>
Vector3 operator-(const Vector3 &vector) const;
/// <summary> /// @brief Subtract a vector from this vector
/// Add another vector to this vector /// @param v The vector to subtract from this vector
/// </summary> /// @return The result of this subtraction
/// <param name="vector2">The vector to add</param> Vector3 operator-(const Vector3 &v) const;
/// <returns>The result of adding the vector</returns> Vector3 operator-=(const Vector3 &v);
Vector3 operator+(const Vector3 &vector2) const; /// @brief Add a vector to this vector
/// @param v The vector to add to this vector
/// @return The result of the addition
Vector3 operator+(const Vector3 &v) const;
Vector3 operator+=(const Vector3 &v);
/// <summary> /// @brief Scale the vector using another vector
/// Scale a vector using another vector /// @param v1 The vector to scale
/// </summary> /// @param v2 A vector with the scaling factors
/// <param name="vector1">The vector to scale</param> /// @return The scaled vector
/// <param name="vector2">A vector with scaling factors</param> /// @remark Each component of the vector v1 will be multiplied with the
/// <returns>The scaled vector</returns> /// matching component from the scaling vector v2.
/// Each component of the vector v1 will be multiplied with the static Vector3 Scale(const Vector3 &v1, const Vector3 &v2);
/// component from the scaling vector v2. /// @brief Scale the vector uniformly up
static Vector3 Scale(const Vector3 &vector1, const Vector3 &vector2); /// @param f The scaling factor
/// <summary> /// @return The scaled vector
/// Scale a vector uniformly up /// @remark Each component of the vector will be multipled with the same
/// </summary> /// factor f.
/// <param name="factor">The scaling factor</param> friend Vector3 operator*(const Vector3 &v, const float f);
/// <returns>The scaled vector</returns> friend Vector3 operator*(const float f, const Vector3 &v);
/// Each component of the vector will be multipled with the same factor. Vector3 operator*=(const float f);
Vector3 operator*(const float factor) const; /// @brief Scale the vector uniformly down
/// <summary> /// @param f The scaling factor
/// Scale a vector uniformy down /// @return The scaled vector
/// </summary> /// @remark Each componet of the vector will be divided by the same factor.
/// <param name="factor">The scaling factor</param> friend Vector3 operator/(const Vector3 &v, const float f);
/// <returns>The scaled vector</returns> friend Vector3 operator/(const float f, const Vector3 &v);
/// Each componet of the vector will be divided by the same factor. Vector3 operator/=(const float f);
Vector3 operator/(const float factor) const;
/// <summary> /// @brief The distance between two vectors
/// The dot product of two vectors /// @param v1 The first vector
/// </summary> /// @param v2 The second vector
/// <param name="vector1">The first vector</param> /// @return The distance between the two vectors
/// <param name="vector2">The second vector</param> static float Distance(const Vector3 &v1, const Vector3 &v2);
/// <returns>The dot product of the two vectors</returns>
static float Dot(const Vector3 &vector1, const Vector3 &vector2);
/// <summary> /// @brief The dot product of two vectors
/// Check is this vector is equal to the given vector /// @param v1 The first vector
/// </summary> /// @param v2 The second vector
/// <param name="vector">The vector to check against</param> /// @return The dot product of the two vectors
/// <returns>True if it is identical to the given vector</returns> static float Dot(const Vector3 &v1, const Vector3 &v2);
/// Note this uses float comparison to check equality which
/// may have strange effects. Equality on float should be avoided.
bool operator==(const Vector3 &vector);
/// <summary> /// @brief The cross product of two vectors
/// The distance between two vectors /// @param v1 The first vector
/// </summary> /// @param v2 The second vector
/// <param name="vector1">The first vector</param> /// @return The cross product of the two vectors
/// <param name="vector2">The second vectors</param> static Vector3 Cross(const Vector3 &v1, const Vector3 &v2);
/// <returns>The distance between the two vectors</returns>
static float Distance(const Vector3 &vector1, const Vector3 &vector2);
/// <summary> /// @brief Project the vector on another vector
/// The cross product of two vectors /// @param v The vector to project
/// </summary> /// @param n The normal vecto to project on
/// <param name="vector1">The first vector</param> /// @return The projected vector
/// <param name="vector2">The second vector</param> static Vector3 Project(const Vector3 &v, const Vector3 &n);
/// <returns>The cross product of the two vectors</returns> /// @brief Project the vector on a plane defined by a normal orthogonal to the
static Vector3 Cross(const Vector3 &vector1, const Vector3 &vector2);
/// <summary>
/// Project a vector on another vector
/// </summary>
/// <param name="vector">The vector to project</param>
/// <param name="onNormal">The normal vector to project on</param>
/// <returns>The projected vector</returns>
static Vector3 Project(const Vector3 &vector, const Vector3 &onNormal);
/// <summary>
/// Projects a vector onto a plane defined by a normal orthogonal to the
/// plane. /// plane.
/// </summary> /// @param v The vector to project
/// <param name="vector">The vector to project</param> /// @param n The normal of the plane to project on
/// <param name="planeNormal">The normal of the plane to project on</param> /// @return Teh projected vector
/// <returns></returns> static Vector3 ProjectOnPlane(const Vector3 &v, const Vector3 &n);
static Vector3 ProjectOnPlane(const Vector3 &vector,
const Vector3 &planeNormal);
/// <summary> /// @brief The angle between two vectors
/// Projects a vector onto the horizontal plane. /// @param v1 The first vector
/// </summary> /// @param v2 The second vector
/// <param name="vector">The vector to project</param> /// @return The angle between the two vectors
/// <returns>A 2D carthesian vector with the coordinates in the horizontal /// @remark This reterns an unsigned angle which is the shortest distance
/// plane.</returns> /// between the two vectors. Use Vector3::SignedAngle if a signed angle is
static Vector2 ProjectHorizontalPlane(const Vector3 &vector); /// needed.
static float Angle(const Vector3 &v1, const Vector3 &v2);
/// <summary> /// @brief The signed angle between two vectors
/// Calculate the angle between two vectors /// @param v1 The starting vector
/// </summary> /// @param v2 The ending vector
/// <param name="vector1">The first vector</param> /// @param axis The axis to rotate around
/// <param name="vector2">The second vector</param> /// @return The signed angle between the two vectors
/// <returns></returns> static float SignedAngle(const Vector3 &v1, const Vector3 &v2,
/// This reterns an unsigned angle which is the shortest distance
/// between the two vectors. Use Vector3::SignedAngle if a
/// signed angle is needed.
static float Angle(const Vector3 &vector1, const Vector3 &vector2);
/// <summary>
/// Calculate the angle between two vectors rotation around an axis.
/// </summary>
/// <param name="from">The starting vector</param>
/// <param name="to">The ending vector</param>
/// <param name="axis">The axis to rotate around</param>
/// <returns>The signed angle</returns>
static float SignedAngle(const Vector3 &from, const Vector3 &to,
const Vector3 &axis); const Vector3 &axis);
/// <summary> /// @brief Lerp (linear interpolation) between two vectors
/// Lerp between two vectors /// @param v1 The starting vector
/// </summary> /// @param v2 The ending vector
/// <param name="from">The from vector</param> /// @param f The interpolation distance
/// <param name="to">The to vector</param> /// @return The lerped vector
/// <param name="f">The interpolation distance (0..1)</param> /// @remark The factor f is unclamped. Value 0 matches the vector *v1*, Value
/// <returns>The lerped vector</returns> /// 1 matches vector *v2*. Value -1 is vector *v1* minus the difference
/// The factor f is unclamped. Value 0 matches the *from* vector, Value 1 /// between *v1* and *v2* etc.
/// matches the *to* vector Value -1 is *from* vector minus the difference static Vector3 Lerp(const Vector3 &v1, const Vector3 &v2, float f);
/// between *from* and *to* etc.
static Vector3 Lerp(const Vector3 &from, const Vector3 &to, float f);
}; };
} // namespace Passer } // namespace Passer
using namespace Passer; using namespace Passer;

View File

@ -77,9 +77,9 @@ TEST(Spherical, Incident1) {
EXPECT_NEAR(s.verticalAngle, sr.verticalAngle, 1.0e-02); EXPECT_NEAR(s.verticalAngle, sr.verticalAngle, 1.0e-02);
Vector3 r = Vector3(sr); Vector3 r = Vector3(sr);
EXPECT_NEAR(r.x, v.x, 1.0e-02) << "toVector3.x 1 0 0"; EXPECT_NEAR(r.Right(), v.Right(), 1.0e-02) << "toVector3.x 1 0 0";
EXPECT_NEAR(r.y, v.y, 1.0e-02) << "toVector3.y 1 0 0"; EXPECT_NEAR(r.Up(), v.Up(), 1.0e-02) << "toVector3.y 1 0 0";
EXPECT_NEAR(r.z, v.z, 1.0e-02) << "toVector3.z 1 0 0"; EXPECT_NEAR(r.Forward(), v.Forward(), 1.0e-02) << "toVector3.z 1 0 0";
} }
TEST(Spherical, Incident2) { TEST(Spherical, Incident2) {
@ -92,9 +92,9 @@ TEST(Spherical, Incident2) {
EXPECT_NEAR(s.verticalAngle, sr.verticalAngle, 1.0e-05); EXPECT_NEAR(s.verticalAngle, sr.verticalAngle, 1.0e-05);
Vector3 r = Vector3(sr); Vector3 r = Vector3(sr);
EXPECT_NEAR(r.x, v.x, 1.0e-06); EXPECT_NEAR(r.Right(), v.Right(), 1.0e-06);
EXPECT_NEAR(r.y, v.y, 1.0e-06); EXPECT_NEAR(r.Up(), v.Up(), 1.0e-06);
EXPECT_NEAR(r.z, v.z, 1.0e-06); EXPECT_NEAR(r.Forward(), v.Forward(), 1.0e-06);
v = Vector3(0.0f, 1.0f, 1.0f); v = Vector3(0.0f, 1.0f, 1.0f);
s = Spherical(v); s = Spherical(v);
@ -105,9 +105,9 @@ TEST(Spherical, Incident2) {
EXPECT_NEAR(s.verticalAngle, sr.verticalAngle, 1.0e-05); EXPECT_NEAR(s.verticalAngle, sr.verticalAngle, 1.0e-05);
r = Vector3(sr); r = Vector3(sr);
EXPECT_NEAR(r.x, v.x, 1.0e-06); EXPECT_NEAR(r.Right(), v.Right(), 1.0e-06);
EXPECT_NEAR(r.y, v.y, 1.0e-06); EXPECT_NEAR(r.Up(), v.Up(), 1.0e-06);
EXPECT_NEAR(r.z, v.z, 1.0e-06); EXPECT_NEAR(r.Forward(), v.Forward(), 1.0e-06);
v = Vector3(1.0f, 1.0f, 1.0f); v = Vector3(1.0f, 1.0f, 1.0f);
s = Spherical(v); s = Spherical(v);
@ -117,9 +117,9 @@ TEST(Spherical, Incident2) {
EXPECT_NEAR(s.horizontalAngle, 45.0F, 1.0e-02); EXPECT_NEAR(s.horizontalAngle, 45.0F, 1.0e-02);
EXPECT_NEAR(s.verticalAngle, 35.26F, 1.0e-02); EXPECT_NEAR(s.verticalAngle, 35.26F, 1.0e-02);
EXPECT_NEAR(r.x, v.x, 1.0e-06); EXPECT_NEAR(r.Right(), v.Right(), 1.0e-06);
EXPECT_NEAR(r.y, v.y, 1.0e-06); EXPECT_NEAR(r.Up(), v.Up(), 1.0e-06);
EXPECT_NEAR(r.z, v.z, 1.0e-06); EXPECT_NEAR(r.Forward(), v.Forward(), 1.0e-06);
// s = Spherical(10, 45, 45); // s = Spherical(10, 45, 45);
// r = s.ToVector3(); // r = s.ToVector3();

View File

@ -168,11 +168,17 @@ TEST(Vector2, Subtract) {
v2 = Vector2(4, 5); v2 = Vector2(4, 5);
v = v1 - v2; v = v1 - v2;
EXPECT_TRUE(v == Vector2(0, 0)) << "4 5 - 4 5"; EXPECT_TRUE(v == Vector2(0, 0)) << "4 5 - 4 5";
v = v1;
v -= v2;
EXPECT_TRUE(v == Vector2(0, 0)) << "4 5 - 4 5";
v2 = Vector2(0, 0); v2 = Vector2(0, 0);
v = v1 - v2; v = v1 - v2;
EXPECT_TRUE(v == Vector2(4, 5)) << "4 5 - 0 0"; EXPECT_TRUE(v == Vector2(4, 5)) << "4 5 - 0 0";
v -= v2;
EXPECT_TRUE(v == Vector2(4, 5)) << "4 5 - 0 0";
if (std::numeric_limits<float>::is_iec559) { if (std::numeric_limits<float>::is_iec559) {
v2 = Vector2(FLOAT_INFINITY, FLOAT_INFINITY); v2 = Vector2(FLOAT_INFINITY, FLOAT_INFINITY);
v = v1 - v2; v = v1 - v2;
@ -197,10 +203,15 @@ TEST(Vector2, Addition) {
v2 = Vector2(-1, -2); v2 = Vector2(-1, -2);
v = v1 + v2; v = v1 + v2;
EXPECT_TRUE(v == Vector2(3, 3)) << "4 5 + -1 -2"; EXPECT_TRUE(v == Vector2(3, 3)) << "4 5 + -1 -2";
v = v1;
v += v2;
EXPECT_TRUE(v == Vector2(3, 3)) << "4 5 + -1 -2";
v2 = Vector2(0, 0); v2 = Vector2(0, 0);
v = v1 + v2; v = v1 + v2;
EXPECT_TRUE(v == Vector2(4, 5)) << "4 5 + 0 0"; EXPECT_TRUE(v == Vector2(4, 5)) << "4 5 + 0 0";
v += v2;
EXPECT_TRUE(v == Vector2(4, 5)) << "4 5 + 0 0";
if (std::numeric_limits<float>::is_iec559) { if (std::numeric_limits<float>::is_iec559) {
v2 = Vector2(FLOAT_INFINITY, FLOAT_INFINITY); v2 = Vector2(FLOAT_INFINITY, FLOAT_INFINITY);

View File

@ -12,25 +12,25 @@ TEST(Vector3, FromSpherical) {
Spherical s = Spherical(v); Spherical s = Spherical(v);
Vector3 r = Vector3(s); Vector3 r = Vector3(s);
EXPECT_FLOAT_EQ(r.x, 0.0F) << "toVector3.x 0 0 1"; EXPECT_FLOAT_EQ(r.Right(), 0.0F) << "toVector3.x 0 0 1";
EXPECT_NEAR(r.y, 0.0F, 1.0e-06) << "toVector3.y 0 0 1"; EXPECT_NEAR(r.Up(), 0.0F, 1.0e-06) << "toVector3.y 0 0 1";
EXPECT_FLOAT_EQ(r.z, 1.0F) << "toVector3.z 0 0 1"; EXPECT_FLOAT_EQ(r.Forward(), 1.0F) << "toVector3.z 0 0 1";
v = Vector3(0, 1, 0); v = Vector3(0, 1, 0);
s = Spherical(v); s = Spherical(v);
r = Vector3(s); r = Vector3(s);
EXPECT_FLOAT_EQ(r.x, 0.0F) << "toVector3.x 0 1 0"; EXPECT_FLOAT_EQ(r.Right(), 0.0F) << "toVector3.x 0 1 0";
EXPECT_FLOAT_EQ(r.y, 1.0F) << "toVector3.y 0 1 0"; EXPECT_FLOAT_EQ(r.Up(), 1.0F) << "toVector3.y 0 1 0";
EXPECT_NEAR(r.z, 0.0F, 1.0e-06) << "toVector3.z 0 1 0"; EXPECT_NEAR(r.Forward(), 0.0F, 1.0e-06) << "toVector3.z 0 1 0";
v = Vector3(1, 0, 0); v = Vector3(1, 0, 0);
s = Spherical(v); s = Spherical(v);
r = Vector3(s); r = Vector3(s);
EXPECT_FLOAT_EQ(r.x, 1.0F) << "toVector3.x 1 0 0"; EXPECT_FLOAT_EQ(r.Right(), 1.0F) << "toVector3.x 1 0 0";
EXPECT_NEAR(r.y, 0.0F, 1.0e-06) << "toVector3.y 1 0 0"; EXPECT_NEAR(r.Up(), 0.0F, 1.0e-06) << "toVector3.y 1 0 0";
EXPECT_NEAR(r.z, 0.0F, 1.0e-06) << "toVector3.z 1 0 0"; EXPECT_NEAR(r.Forward(), 0.0F, 1.0e-06) << "toVector3.z 1 0 0";
} }
TEST(Vector3, Magnitude) { TEST(Vector3, Magnitude) {
@ -118,12 +118,12 @@ TEST(Vector3, Normalize) {
if (std::numeric_limits<float>::is_iec559) { if (std::numeric_limits<float>::is_iec559) {
v1 = Vector3(FLOAT_INFINITY, FLOAT_INFINITY, FLOAT_INFINITY); v1 = Vector3(FLOAT_INFINITY, FLOAT_INFINITY, FLOAT_INFINITY);
v = v1.normalized(); v = v1.normalized();
r = isnan(v.x) && isnan(v.y) && isnan(v.z); r = isnan(v.Right()) && isnan(v.Up()) && isnan(v.Forward());
EXPECT_TRUE(r) << "v.normalized INFINITY INFINITY INFINITY"; EXPECT_TRUE(r) << "v.normalized INFINITY INFINITY INFINITY";
v1 = Vector3(-FLOAT_INFINITY, -FLOAT_INFINITY, -FLOAT_INFINITY); v1 = Vector3(-FLOAT_INFINITY, -FLOAT_INFINITY, -FLOAT_INFINITY);
v = v1.normalized(); v = v1.normalized();
r = isnan(v.x) && isnan(v.y) && isnan(v.z); r = isnan(v.Right()) && isnan(v.Up()) && isnan(v.Forward());
EXPECT_TRUE(r) << "v.normalized -INFINITY -INFINITY -INFINITY"; EXPECT_TRUE(r) << "v.normalized -INFINITY -INFINITY -INFINITY";
} }
} }
@ -409,12 +409,12 @@ TEST(Vector3, Cross) {
if (std::numeric_limits<float>::is_iec559) { if (std::numeric_limits<float>::is_iec559) {
v2 = Vector3(FLOAT_INFINITY, FLOAT_INFINITY, FLOAT_INFINITY); v2 = Vector3(FLOAT_INFINITY, FLOAT_INFINITY, FLOAT_INFINITY);
v = Vector3::Cross(v1, v2); v = Vector3::Cross(v1, v2);
r = isnan(v.x) && isnan(v.y) && isnan(v.z); r = isnan(v.Right()) && isnan(v.Up()) && isnan(v.Forward());
EXPECT_TRUE(r) << "Cross(4 5 6, INFINITY INFINITY INFINITY)"; EXPECT_TRUE(r) << "Cross(4 5 6, INFINITY INFINITY INFINITY)";
v2 = Vector3(-FLOAT_INFINITY, -FLOAT_INFINITY, -FLOAT_INFINITY); v2 = Vector3(-FLOAT_INFINITY, -FLOAT_INFINITY, -FLOAT_INFINITY);
v = Vector3::Cross(v1, v2); v = Vector3::Cross(v1, v2);
r = isnan(v.x) && isnan(v.y) && isnan(v.z); r = isnan(v.Right()) && isnan(v.Up()) && isnan(v.Forward());
EXPECT_TRUE(r) << "Cross(4 5 6, -INFINITY -INFINITY -INFINITY)"; EXPECT_TRUE(r) << "Cross(4 5 6, -INFINITY -INFINITY -INFINITY)";
} }
} }
@ -442,12 +442,12 @@ TEST(Vector3, Project) {
if (std::numeric_limits<float>::is_iec559) { if (std::numeric_limits<float>::is_iec559) {
v2 = Vector3(FLOAT_INFINITY, FLOAT_INFINITY, FLOAT_INFINITY); v2 = Vector3(FLOAT_INFINITY, FLOAT_INFINITY, FLOAT_INFINITY);
v = Vector3::Project(v1, v2); v = Vector3::Project(v1, v2);
r = isnan(v.x) && isnan(v.y) && isnan(v.z); r = isnan(v.Right()) && isnan(v.Up()) && isnan(v.Forward());
EXPECT_TRUE(r) << "Project(4 5 6, INFINITY INFINITY INFINITY)"; EXPECT_TRUE(r) << "Project(4 5 6, INFINITY INFINITY INFINITY)";
v2 = Vector3(-FLOAT_INFINITY, -FLOAT_INFINITY, -FLOAT_INFINITY); v2 = Vector3(-FLOAT_INFINITY, -FLOAT_INFINITY, -FLOAT_INFINITY);
v = Vector3::Project(v1, v2); v = Vector3::Project(v1, v2);
r = isnan(v.x) && isnan(v.y) && isnan(v.z); r = isnan(v.Right()) && isnan(v.Up()) && isnan(v.Forward());
EXPECT_TRUE(r) << "Project(4 5 6, -INFINITY -INFINITY -INFINITY)"; EXPECT_TRUE(r) << "Project(4 5 6, -INFINITY -INFINITY -INFINITY)";
} }
} }
@ -475,12 +475,12 @@ TEST(Vector3, ProjectOnPlane) {
if (std::numeric_limits<float>::is_iec559) { if (std::numeric_limits<float>::is_iec559) {
v2 = Vector3(FLOAT_INFINITY, FLOAT_INFINITY, FLOAT_INFINITY); v2 = Vector3(FLOAT_INFINITY, FLOAT_INFINITY, FLOAT_INFINITY);
v = Vector3::ProjectOnPlane(v1, v2); v = Vector3::ProjectOnPlane(v1, v2);
r = isnan(v.x) && isnan(v.y) && isnan(v.z); r = isnan(v.Right()) && isnan(v.Up()) && isnan(v.Forward());
EXPECT_TRUE(r) << "ProjectOnPlane(4 5 6, INFINITY INFINITY INFINITY)"; EXPECT_TRUE(r) << "ProjectOnPlane(4 5 6, INFINITY INFINITY INFINITY)";
v2 = Vector3(-FLOAT_INFINITY, -FLOAT_INFINITY, -FLOAT_INFINITY); v2 = Vector3(-FLOAT_INFINITY, -FLOAT_INFINITY, -FLOAT_INFINITY);
v = Vector3::ProjectOnPlane(v1, v2); v = Vector3::ProjectOnPlane(v1, v2);
r = isnan(v.x) && isnan(v.y) && isnan(v.z); r = isnan(v.Right()) && isnan(v.Up()) && isnan(v.Forward());
EXPECT_TRUE(r) << "ProjectOnPlane(4 5 6, -INFINITY -INFINITY -INFINITY)"; EXPECT_TRUE(r) << "ProjectOnPlane(4 5 6, -INFINITY -INFINITY -INFINITY)";
} }
} }