Extend AngleOf support
This commit is contained in:
parent
c70c079efc
commit
b81b77b1c9
31
Angle.cpp
31
Angle.cpp
@ -3,8 +3,8 @@
|
|||||||
// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
|
// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
#include "Angle.h"
|
#include "Angle.h"
|
||||||
#include "FloatSingle.h"
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include "FloatSingle.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
const float Angle::Rad2Deg = 57.29578F;
|
const float Angle::Rad2Deg = 57.29578F;
|
||||||
@ -73,17 +73,29 @@ float Angle::SineRuleAngle(float a, float beta, float b) {
|
|||||||
*/
|
*/
|
||||||
//----------------------
|
//----------------------
|
||||||
|
|
||||||
template <> AngleOf<float> AngleOf<float>::pi = 3.1415927410125732421875F;
|
template <>
|
||||||
|
AngleOf<float>::AngleOf(float angle) : value(angle) {}
|
||||||
|
|
||||||
template <> AngleOf<float> AngleOf<float>::Rad2Deg = 360.0f / (pi * 2);
|
template <>
|
||||||
template <> AngleOf<float> AngleOf<float>::Deg2Rad = (pi * 2) / 360.0f;
|
AngleOf<float>::operator float() const {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
AngleOf<float> AngleOf<float>::pi = 3.1415927410125732421875F;
|
||||||
|
|
||||||
|
template <>
|
||||||
|
AngleOf<float> AngleOf<float>::Rad2Deg = 360.0f / (pi * 2);
|
||||||
|
template <>
|
||||||
|
AngleOf<float> AngleOf<float>::Deg2Rad = (pi * 2) / 360.0f;
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
bool Passer::LinearAlgebra::AngleOf<float>::operator==(AngleOf<float> a) {
|
bool Passer::LinearAlgebra::AngleOf<float>::operator==(AngleOf<float> a) {
|
||||||
return (float)*this == (float)a;
|
return (float)*this == (float)a;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <> AngleOf<float> AngleOf<float>::Normalize(AngleOf<float> angle) {
|
template <>
|
||||||
|
AngleOf<float> AngleOf<float>::Normalize(AngleOf<float> angle) {
|
||||||
float angleValue = angle;
|
float angleValue = angle;
|
||||||
if (!isfinite(angleValue))
|
if (!isfinite(angleValue))
|
||||||
return angleValue;
|
return angleValue;
|
||||||
@ -96,7 +108,8 @@ template <> AngleOf<float> AngleOf<float>::Normalize(AngleOf<float> angle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
AngleOf<float> AngleOf<float>::Clamp(AngleOf<float> angle, AngleOf<float> min,
|
AngleOf<float> AngleOf<float>::Clamp(AngleOf<float> angle,
|
||||||
|
AngleOf<float> min,
|
||||||
AngleOf<float> max) {
|
AngleOf<float> max) {
|
||||||
float normalizedAngle = Normalize(angle);
|
float normalizedAngle = Normalize(angle);
|
||||||
float r = Float::Clamp(normalizedAngle, min, max);
|
float r = Float::Clamp(normalizedAngle, min, max);
|
||||||
@ -120,7 +133,8 @@ AngleOf<float> AngleOf<float>::MoveTowards(AngleOf<float> fromAngle,
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
AngleOf<float> AngleOf<float>::CosineRuleSide(float a, float b,
|
AngleOf<float> AngleOf<float>::CosineRuleSide(float a,
|
||||||
|
float b,
|
||||||
AngleOf<float> gamma) {
|
AngleOf<float> gamma) {
|
||||||
float a2 = a * a;
|
float a2 = a * a;
|
||||||
float b2 = b * b;
|
float b2 = b * b;
|
||||||
@ -150,7 +164,8 @@ AngleOf<float> AngleOf<float>::CosineRuleAngle(float a, float b, float c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
AngleOf<float> AngleOf<float>::SineRuleAngle(float a, AngleOf<float> beta,
|
AngleOf<float> AngleOf<float>::SineRuleAngle(float a,
|
||||||
|
AngleOf<float> beta,
|
||||||
float b) {
|
float b) {
|
||||||
float alpha = asin(a * sin(beta * Angle::Deg2Rad) / b);
|
float alpha = asin(a * sin(beta * Angle::Deg2Rad) / b);
|
||||||
return alpha;
|
return alpha;
|
||||||
|
14
Angle.h
14
Angle.h
@ -8,11 +8,15 @@
|
|||||||
namespace Passer {
|
namespace Passer {
|
||||||
namespace LinearAlgebra {
|
namespace LinearAlgebra {
|
||||||
|
|
||||||
template <typename T> class AngleOf {
|
template <typename T>
|
||||||
|
class AngleOf {
|
||||||
public:
|
public:
|
||||||
AngleOf() {};
|
AngleOf() {};
|
||||||
AngleOf(T v) : value(v) {}
|
AngleOf(float f);
|
||||||
operator T() const { return value; }
|
// AngleOf(T v) : value(v) {}
|
||||||
|
// operator T() const; // { return value; }
|
||||||
|
operator float() const;
|
||||||
|
inline T GetBinary() const { return this->value; }
|
||||||
|
|
||||||
static AngleOf<T> Rad2Deg;
|
static AngleOf<T> Rad2Deg;
|
||||||
static AngleOf<T> Deg2Rad;
|
static AngleOf<T> Deg2Rad;
|
||||||
@ -27,7 +31,8 @@ public:
|
|||||||
AngleOf<T> r = Normalize(b - a);
|
AngleOf<T> r = Normalize(b - a);
|
||||||
return r;
|
return r;
|
||||||
};
|
};
|
||||||
static AngleOf<T> MoveTowards(AngleOf<T> fromAngle, AngleOf<T> toAngle,
|
static AngleOf<T> MoveTowards(AngleOf<T> fromAngle,
|
||||||
|
AngleOf<T> toAngle,
|
||||||
AngleOf<T> maxAngle);
|
AngleOf<T> maxAngle);
|
||||||
|
|
||||||
static AngleOf<T> CosineRuleSide(float a, float b, AngleOf<T> gamma);
|
static AngleOf<T> CosineRuleSide(float a, float b, AngleOf<T> gamma);
|
||||||
@ -40,6 +45,7 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
using Angle = AngleOf<float>;
|
using Angle = AngleOf<float>;
|
||||||
|
// using Angle = AngleOf<signed short>;
|
||||||
|
|
||||||
} // namespace LinearAlgebra
|
} // namespace LinearAlgebra
|
||||||
} // namespace Passer
|
} // namespace Passer
|
||||||
|
74
Angle16.cpp
Normal file
74
Angle16.cpp
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0.If a copy of the MPL was not distributed with this
|
||||||
|
// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#include "Angle.h"
|
||||||
|
|
||||||
|
template <>
|
||||||
|
AngleOf<signed short>::AngleOf(float angle) {
|
||||||
|
if (!isfinite(angle)) {
|
||||||
|
value = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// map float [-180..180) to integer [-32768..32767]
|
||||||
|
this->value = (signed short)((angle / 360.0F) * 65536.0F);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
AngleOf<signed short>::operator float() const {
|
||||||
|
float f = ((this->value * 180) / 32768.0F);
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This should not exist...
|
||||||
|
// template <>
|
||||||
|
// AngleOf<signed short> AngleOf<signed short>::pi = 3.1415927410125732421875F;
|
||||||
|
|
||||||
|
// template <>
|
||||||
|
// AngleOf<signed short> AngleOf<signed short>::Rad2Deg = 360.0f / (pi * 2);
|
||||||
|
// template <>
|
||||||
|
// AngleOf<signed short> AngleOf<signed short>::Deg2Rad = (pi * 2) / 360.0f;
|
||||||
|
|
||||||
|
// template <>
|
||||||
|
// AngleOf<signed short> AngleOf<signed short>::Normalize(
|
||||||
|
// AngleOf<signed short> angle) {
|
||||||
|
// return angle;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Not correct!!! just for syntactical compilation ATM
|
||||||
|
template <>
|
||||||
|
AngleOf<signed short> AngleOf<signed short>::CosineRuleSide(
|
||||||
|
float a,
|
||||||
|
float b,
|
||||||
|
AngleOf<signed short> gamma) {
|
||||||
|
float a2 = a * a;
|
||||||
|
float b2 = b * b;
|
||||||
|
float d = a2 + b2 - 2 * a * b * cos(gamma * AngleOf<float>::Deg2Rad);
|
||||||
|
// Catch edge cases where float inacuracies lead tot nans
|
||||||
|
if (d < 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
float c = sqrtf(d);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Not correct!!! just for syntactical compilation ATM
|
||||||
|
template <>
|
||||||
|
AngleOf<signed short> AngleOf<signed short>::CosineRuleAngle(float a,
|
||||||
|
float b,
|
||||||
|
float c) {
|
||||||
|
float a2 = a * a;
|
||||||
|
float b2 = b * b;
|
||||||
|
float c2 = c * c;
|
||||||
|
float d = (a2 + b2 - c2) / (2 * a * b);
|
||||||
|
// Catch edge cases where float inacuracies lead tot nans
|
||||||
|
if (d >= 1)
|
||||||
|
return 0;
|
||||||
|
if (d <= -1)
|
||||||
|
return 180;
|
||||||
|
|
||||||
|
float gamma = acos(d) * Angle::Rad2Deg;
|
||||||
|
return gamma;
|
||||||
|
}
|
30
Angle16.h
30
Angle16.h
@ -1,27 +1,27 @@
|
|||||||
#include "AngleUsing.h"
|
// #include "AngleUsing.h"
|
||||||
|
|
||||||
#include "Angle.h"
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include "Angle.h"
|
||||||
|
|
||||||
namespace Passer {
|
namespace Passer {
|
||||||
namespace LinearAlgebra {
|
namespace LinearAlgebra {
|
||||||
|
|
||||||
typedef AngleUsing<signed short> Angle16;
|
typedef AngleOf<signed short> Angle16;
|
||||||
|
|
||||||
template <> Angle16::AngleUsing(float angle) {
|
// template <> Angle16::AngleOf(float angle) {
|
||||||
if (!isfinite(angle)) {
|
// if (!isfinite(angle)) {
|
||||||
value = 0;
|
// value = 0;
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// map float [-180..180) to integer [-32768..32767]
|
// // map float [-180..180) to integer [-32768..32767]
|
||||||
this->value = (signed short)((angle / 360.0F) * 65536.0F);
|
// this->value = (signed short)((angle / 360.0F) * 65536.0F);
|
||||||
}
|
// }
|
||||||
|
|
||||||
template <> float Angle16::ToFloat() const {
|
// template <> float Angle16::ToFloat() const {
|
||||||
float f = ((this->value * 180) / 32768.0F);
|
// float f = ((this->value * 180) / 32768.0F);
|
||||||
return f;
|
// return f;
|
||||||
}
|
// }
|
||||||
|
|
||||||
} // namespace LinearAlgebra
|
} // namespace LinearAlgebra
|
||||||
} // namespace Passer
|
} // namespace Passer
|
||||||
|
32
Angle32.h
32
Angle32.h
@ -1,27 +1,29 @@
|
|||||||
#include "AngleUsing.h"
|
// #include "AngleUsing.h"
|
||||||
|
|
||||||
#include "Angle.h"
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include "Angle.h"
|
||||||
|
|
||||||
namespace Passer {
|
namespace Passer {
|
||||||
namespace LinearAlgebra {
|
namespace LinearAlgebra {
|
||||||
|
|
||||||
typedef AngleUsing<signed long> Angle32;
|
typedef AngleOf<signed long> Angle32;
|
||||||
|
|
||||||
template <> Angle32::AngleUsing(float angle) {
|
// template <>
|
||||||
if (!isfinite(angle)) {
|
// Angle32::AngleOf(float angle) {
|
||||||
value = 0;
|
// if (!isfinite(angle)) {
|
||||||
return;
|
// value = 0;
|
||||||
}
|
// return;
|
||||||
|
// }
|
||||||
|
|
||||||
// map float [-180..180) to integer [-2147483648..2147483647]
|
// // map float [-180..180) to integer [-2147483648..2147483647]
|
||||||
this->value = (signed long)((angle / 360.0F) * 4294967295.0F);
|
// this->value = (signed long)((angle / 360.0F) * 4294967295.0F);
|
||||||
}
|
// }
|
||||||
|
|
||||||
template <> float Angle32::ToFloat() const {
|
// template <>
|
||||||
float f = ((this->value * 180) / 2147483648.0F);
|
// float Angle32::ToFloat() const {
|
||||||
return f;
|
// float f = ((this->value * 180) / 2147483648.0F);
|
||||||
}
|
// return f;
|
||||||
|
// }
|
||||||
|
|
||||||
} // namespace LinearAlgebra
|
} // namespace LinearAlgebra
|
||||||
} // namespace Passer
|
} // namespace Passer
|
||||||
|
24
Angle8.cpp
Normal file
24
Angle8.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0.If a copy of the MPL was not distributed with this
|
||||||
|
// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#include "Angle.h"
|
||||||
|
|
||||||
|
template <>
|
||||||
|
AngleOf<signed char>::AngleOf(float angle) {
|
||||||
|
if (!isfinite(angle)) {
|
||||||
|
value = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// map float [-180..180) to integer [-128..127]
|
||||||
|
float f = angle / 360.0F;
|
||||||
|
this->value = (signed char)(f * 256.0F);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
AngleOf<signed char>::operator float() const {
|
||||||
|
float f = (this->value * 180) / 128.0F;
|
||||||
|
return f;
|
||||||
|
}
|
32
Angle8.h
32
Angle8.h
@ -1,28 +1,28 @@
|
|||||||
#include "AngleUsing.h"
|
// #include "AngleUsing.h"
|
||||||
|
|
||||||
#include "Angle.h"
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include "Angle.h"
|
||||||
|
|
||||||
namespace Passer {
|
namespace Passer {
|
||||||
namespace LinearAlgebra {
|
namespace LinearAlgebra {
|
||||||
|
|
||||||
typedef AngleUsing<signed char> Angle8;
|
typedef AngleOf<signed char> Angle8;
|
||||||
|
|
||||||
template <> Angle8::AngleUsing(float angle) {
|
// template <> Angle8::AngleOf(float angle) {
|
||||||
if (!isfinite(angle)) {
|
// if (!isfinite(angle)) {
|
||||||
value = 0;
|
// value = 0;
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// map float [-180..180) to integer [-128..127]
|
// // map float [-180..180) to integer [-128..127]
|
||||||
float f = angle / 360.0F;
|
// float f = angle / 360.0F;
|
||||||
this->value = (signed char)(f * 256.0F);
|
// this->value = (signed char)(f * 256.0F);
|
||||||
}
|
// }
|
||||||
|
|
||||||
template <> float Angle8::ToFloat() const {
|
// template <> float Angle8::ToFloat() const {
|
||||||
float f = (this->value * 180) / 128.0F;
|
// float f = (this->value * 180) / 128.0F;
|
||||||
return f;
|
// return f;
|
||||||
}
|
// }
|
||||||
|
|
||||||
} // namespace LinearAlgebra
|
} // namespace LinearAlgebra
|
||||||
} // namespace Passer
|
} // namespace Passer
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/*
|
||||||
#ifndef DISCRETEANGLE_H
|
#ifndef DISCRETEANGLE_H
|
||||||
#define DISCRETEANGLE_H
|
#define DISCRETEANGLE_H
|
||||||
|
|
||||||
@ -9,7 +10,8 @@ namespace LinearAlgebra {
|
|||||||
|
|
||||||
// A fixed angle between (-180..180]
|
// A fixed angle between (-180..180]
|
||||||
|
|
||||||
template <typename T> class AngleUsing {
|
template <typename T>
|
||||||
|
class AngleUsing {
|
||||||
public:
|
public:
|
||||||
AngleUsing(T sourceValue) { this->value = sourceValue; }
|
AngleUsing(T sourceValue) { this->value = sourceValue; }
|
||||||
AngleUsing(float f);
|
AngleUsing(float f);
|
||||||
@ -47,3 +49,4 @@ public:
|
|||||||
using namespace Passer::LinearAlgebra;
|
using namespace Passer::LinearAlgebra;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
*/
|
24
Polar.cpp
24
Polar.cpp
@ -94,22 +94,22 @@ Polar Polar::operator+=(const Polar &v) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Polar Passer::LinearAlgebra::operator*(const Polar &v, float f) {
|
// Polar Passer::LinearAlgebra::operator*(const Polar &v, float f) {
|
||||||
return Polar(v.distance * f, v.angle);
|
// return Polar(v.distance * f, v.angle);
|
||||||
}
|
// }
|
||||||
Polar Passer::LinearAlgebra::operator*(float f, const Polar &v) {
|
// Polar Passer::LinearAlgebra::operator*(float f, const Polar &v) {
|
||||||
return Polar(v.distance * f, v.angle);
|
// return Polar(v.distance * f, v.angle);
|
||||||
}
|
// }
|
||||||
Polar Polar::operator*=(float f) {
|
Polar Polar::operator*=(float f) {
|
||||||
this->distance *= f;
|
this->distance *= f;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
Polar Passer::LinearAlgebra::operator/(const Polar &v, float f) {
|
// Polar Passer::LinearAlgebra::operator/(const Polar& v, float f) {
|
||||||
return Polar(v.distance / f, v.angle);
|
// return Polar(v.distance / f, v.angle);
|
||||||
}
|
// }
|
||||||
Polar Passer::LinearAlgebra::operator/(float f, const Polar &v) {
|
// Polar Passer::LinearAlgebra::operator/(float f, const Polar& v) {
|
||||||
return Polar(v.distance / f, v.angle);
|
// return Polar(v.distance / f, v.angle);
|
||||||
}
|
// }
|
||||||
Polar Polar::operator/=(float f) {
|
Polar Polar::operator/=(float f) {
|
||||||
this->distance /= f;
|
this->distance /= f;
|
||||||
return *this;
|
return *this;
|
||||||
|
16
Polar.h
16
Polar.h
@ -98,16 +98,24 @@ public:
|
|||||||
/// @return The scaled vector
|
/// @return The scaled vector
|
||||||
/// @remark This operation will scale the distance of the vector. The angle
|
/// @remark This operation will scale the distance of the vector. The angle
|
||||||
/// will be unaffected.
|
/// will be unaffected.
|
||||||
friend Polar operator*(const Polar &v, float f);
|
friend Polar operator*(const Polar& v, float f) {
|
||||||
friend Polar operator*(float f, const Polar &v);
|
return Polar(v.distance * f, v.angle);
|
||||||
|
}
|
||||||
|
friend Polar operator*(float f, const Polar& v) {
|
||||||
|
return Polar(f * v.distance, v.angle);
|
||||||
|
}
|
||||||
Polar operator*=(float f);
|
Polar operator*=(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 factor
|
/// @return The scaled factor
|
||||||
/// @remark This operation will scale the distance of the vector. The angle
|
/// @remark This operation will scale the distance of the vector. The angle
|
||||||
/// will be unaffected.
|
/// will be unaffected.
|
||||||
friend Polar operator/(const Polar &v, float f);
|
friend Polar operator/(const Polar& v, float f) {
|
||||||
friend Polar operator/(float f, const Polar &v);
|
return Polar(v.distance / f, v.angle);
|
||||||
|
}
|
||||||
|
friend Polar operator/(float f, const Polar& v) {
|
||||||
|
return Polar(f / v.distance, v.angle);
|
||||||
|
}
|
||||||
Polar operator/=(float f);
|
Polar operator/=(float f);
|
||||||
|
|
||||||
/// @brief The distance between two vectors
|
/// @brief The distance between two vectors
|
||||||
|
@ -17,7 +17,8 @@ Spherical::Spherical(Polar polar) {
|
|||||||
this->verticalAngle = 0.0f;
|
this->verticalAngle = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
Spherical::Spherical(float distance, Angle horizontalAngle,
|
Spherical::Spherical(float distance,
|
||||||
|
Angle horizontalAngle,
|
||||||
Angle verticalAngle) {
|
Angle verticalAngle) {
|
||||||
if (distance < 0) {
|
if (distance < 0) {
|
||||||
this->distance = -distance;
|
this->distance = -distance;
|
||||||
@ -143,23 +144,23 @@ Spherical Spherical::operator+=(const Spherical &v) {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Spherical Passer::LinearAlgebra::operator*(const Spherical &v, float f) {
|
// Spherical Passer::LinearAlgebra::operator*(const Spherical &v, float f) {
|
||||||
return Spherical(v.distance * f, v.horizontalAngle, v.verticalAngle);
|
// return Spherical(v.distance * f, v.horizontalAngle, v.verticalAngle);
|
||||||
}
|
// }
|
||||||
Spherical Passer::LinearAlgebra::operator*(float f, const Spherical &v) {
|
// Spherical Passer::LinearAlgebra::operator*(float f, const Spherical &v) {
|
||||||
return Spherical(v.distance * f, v.horizontalAngle, v.verticalAngle);
|
// return Spherical(v.distance * f, v.horizontalAngle, v.verticalAngle);
|
||||||
}
|
// }
|
||||||
Spherical Spherical::operator*=(float f) {
|
Spherical Spherical::operator*=(float f) {
|
||||||
this->distance *= f;
|
this->distance *= f;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Spherical Passer::LinearAlgebra::operator/(const Spherical &v, float f) {
|
// Spherical Passer::LinearAlgebra::operator/(const Spherical &v, float f) {
|
||||||
return Spherical(v.distance / f, v.horizontalAngle, v.verticalAngle);
|
// return Spherical(v.distance / f, v.horizontalAngle, v.verticalAngle);
|
||||||
}
|
// }
|
||||||
Spherical Passer::LinearAlgebra::operator/(float f, const Spherical &v) {
|
// Spherical Passer::LinearAlgebra::operator/(float f, const Spherical &v) {
|
||||||
return Spherical(v.distance / f, v.horizontalAngle, v.verticalAngle);
|
// return Spherical(v.distance / f, v.horizontalAngle, v.verticalAngle);
|
||||||
}
|
// }
|
||||||
Spherical Spherical::operator/=(float f) {
|
Spherical Spherical::operator/=(float f) {
|
||||||
this->distance /= f;
|
this->distance /= f;
|
||||||
return *this;
|
return *this;
|
||||||
@ -184,7 +185,6 @@ const float epsilon = 1E-05f;
|
|||||||
const float Rad2Deg = 57.29578F;
|
const float Rad2Deg = 57.29578F;
|
||||||
|
|
||||||
Angle Spherical::AngleBetween(const Spherical& v1, const Spherical& v2) {
|
Angle Spherical::AngleBetween(const Spherical& v1, const Spherical& v2) {
|
||||||
|
|
||||||
// float denominator = sqrtf(v1_3.sqrMagnitude() * v2_3.sqrMagnitude());
|
// float denominator = sqrtf(v1_3.sqrMagnitude() * v2_3.sqrMagnitude());
|
||||||
float denominator =
|
float denominator =
|
||||||
v1.distance * v2.distance; // sqrtf(v1.distance * v1.distance *
|
v1.distance * v2.distance; // sqrtf(v1.distance * v1.distance *
|
||||||
@ -204,7 +204,8 @@ Angle Spherical::AngleBetween(const Spherical &v1, const Spherical &v2) {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
Spherical Spherical::Rotate(const Spherical &v, Angle horizontalAngle,
|
Spherical Spherical::Rotate(const Spherical& v,
|
||||||
|
Angle horizontalAngle,
|
||||||
Angle verticalAngle) {
|
Angle verticalAngle) {
|
||||||
Spherical r = Spherical(v.distance, v.horizontalAngle + horizontalAngle,
|
Spherical r = Spherical(v.distance, v.horizontalAngle + horizontalAngle,
|
||||||
v.verticalAngle + verticalAngle);
|
v.verticalAngle + verticalAngle);
|
||||||
|
21
Spherical.h
21
Spherical.h
@ -106,16 +106,26 @@ public:
|
|||||||
/// @return The scaled vector
|
/// @return The scaled vector
|
||||||
/// @remark This operation will scale the distance of the vector. The angle
|
/// @remark This operation will scale the distance of the vector. The angle
|
||||||
/// will be unaffected.
|
/// will be unaffected.
|
||||||
friend Spherical operator*(const Spherical &v, float f);
|
friend Spherical operator*(const Spherical& v, float f) {
|
||||||
friend Spherical operator*(float f, const Spherical &v);
|
return Spherical(v.distance * f, v.horizontalAngle, v.verticalAngle);
|
||||||
|
}
|
||||||
|
friend Spherical operator*(float f, const Spherical& v) {
|
||||||
|
return Spherical(v.distance * f, v.horizontalAngle,
|
||||||
|
v.verticalAngle); // not correct, should be f * v.distance
|
||||||
|
}
|
||||||
Spherical operator*=(float f);
|
Spherical operator*=(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 factor
|
/// @return The scaled factor
|
||||||
/// @remark This operation will scale the distance of the vector. The angle
|
/// @remark This operation will scale the distance of the vector. The angle
|
||||||
/// will be unaffected.
|
/// will be unaffected.
|
||||||
friend Spherical operator/(const Spherical &v, float f);
|
friend Spherical operator/(const Spherical& v, float f) {
|
||||||
friend Spherical operator/(float f, const Spherical &v);
|
return Spherical(v.distance / f, v.horizontalAngle, v.verticalAngle);
|
||||||
|
}
|
||||||
|
friend Spherical operator/(float f, const Spherical& v) {
|
||||||
|
return Spherical(v.distance / f, v.horizontalAngle,
|
||||||
|
v.verticalAngle); // not correct, should be f / v.distance
|
||||||
|
}
|
||||||
Spherical operator/=(float f);
|
Spherical operator/=(float f);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -128,7 +138,8 @@ public:
|
|||||||
|
|
||||||
static Angle AngleBetween(const Spherical& v1, const Spherical& v2);
|
static Angle AngleBetween(const Spherical& v1, const Spherical& v2);
|
||||||
|
|
||||||
static Spherical Rotate(const Spherical &v, Angle horizontalAngle,
|
static Spherical Rotate(const Spherical& v,
|
||||||
|
Angle horizontalAngle,
|
||||||
Angle verticalAngle);
|
Angle verticalAngle);
|
||||||
static Spherical RotateHorizontal(const Spherical& v, Angle angle);
|
static Spherical RotateHorizontal(const Spherical& v, Angle angle);
|
||||||
static Spherical RotateVertical(const Spherical& v, Angle angle);
|
static Spherical RotateVertical(const Spherical& v, Angle angle);
|
||||||
|
40
Vector2.cpp
40
Vector2.cpp
@ -56,9 +56,15 @@ bool Vector2::operator==(const Vector2 &v) {
|
|||||||
float Vector2::Magnitude(const Vector2& v) {
|
float Vector2::Magnitude(const Vector2& v) {
|
||||||
return sqrtf(v.x * v.x + v.y * v.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 {
|
||||||
float Vector2::SqrMagnitude(const Vector2 &v) { return v.x * v.x + v.y * v.y; }
|
return (float)sqrtf(x * x + y * y);
|
||||||
float Vector2::sqrMagnitude() const { return (x * x + y * 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);
|
||||||
|
}
|
||||||
|
|
||||||
Vector2 Vector2::Normalize(const Vector2& v) {
|
Vector2 Vector2::Normalize(const Vector2& v) {
|
||||||
float num = Vector2::Magnitude(v);
|
float num = Vector2::Magnitude(v);
|
||||||
@ -77,7 +83,9 @@ Vector2 Vector2::normalized() const {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2 Vector2::operator-() { return Vector2(-this->x, -this->y); }
|
Vector2 Vector2::operator-() {
|
||||||
|
return Vector2(-this->x, -this->y);
|
||||||
|
}
|
||||||
|
|
||||||
Vector2 Vector2::operator-(const Vector2& v) const {
|
Vector2 Vector2::operator-(const Vector2& v) const {
|
||||||
return Vector2(this->x - v.x, this->y - v.y);
|
return Vector2(this->x - v.x, this->y - v.y);
|
||||||
@ -99,23 +107,23 @@ Vector2 Vector2::operator+=(const Vector2 &v) {
|
|||||||
Vector2 Vector2::Scale(const Vector2& v1, const Vector2& v2) {
|
Vector2 Vector2::Scale(const Vector2& v1, const Vector2& v2) {
|
||||||
return Vector2(v1.x * v2.x, v1.y * v2.y);
|
return Vector2(v1.x * v2.x, v1.y * v2.y);
|
||||||
}
|
}
|
||||||
Vector2 Passer::LinearAlgebra::operator*(const Vector2 &v, float f) {
|
// Vector2 Passer::LinearAlgebra::operator*(const Vector2 &v, float f) {
|
||||||
return Vector2(v.x * f, v.y * f);
|
// return Vector2(v.x * f, v.y * f);
|
||||||
}
|
// }
|
||||||
Vector2 Passer::LinearAlgebra::operator*(float f, const Vector2 &v) {
|
// Vector2 Passer::LinearAlgebra::operator*(float f, const Vector2 &v) {
|
||||||
return Vector2(v.x * f, v.y * f);
|
// return Vector2(v.x * f, v.y * f);
|
||||||
}
|
// }
|
||||||
Vector2 Vector2::operator*=(float f) {
|
Vector2 Vector2::operator*=(float f) {
|
||||||
this->x *= f;
|
this->x *= f;
|
||||||
this->y *= f;
|
this->y *= f;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
Vector2 Passer::LinearAlgebra::operator/(const Vector2 &v, float f) {
|
// Vector2 Passer::LinearAlgebra::operator/(const Vector2 &v, float f) {
|
||||||
return Vector2(v.x / f, v.y / f);
|
// return Vector2(v.x / f, v.y / f);
|
||||||
}
|
// }
|
||||||
Vector2 Passer::LinearAlgebra::operator/(float f, const Vector2 &v) {
|
// Vector2 Passer::LinearAlgebra::operator/(float f, const Vector2 &v) {
|
||||||
return Vector2(v.x / f, v.y / f);
|
// return Vector2(v.x / f, v.y / f);
|
||||||
}
|
// }
|
||||||
Vector2 Vector2::operator/=(float f) {
|
Vector2 Vector2::operator/=(float f) {
|
||||||
this->x /= f;
|
this->x /= f;
|
||||||
this->y /= f;
|
this->y /= f;
|
||||||
|
@ -138,8 +138,13 @@ 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.
|
||||||
friend Vector2 operator*(const Vector2 &v, float f);
|
friend Vector2 operator*(const Vector2& v, float f) {
|
||||||
friend Vector2 operator*(float f, const Vector2 &v);
|
return Vector2(v.x * f, v.y * f);
|
||||||
|
}
|
||||||
|
friend Vector2 operator*(float f, const Vector2& v) {
|
||||||
|
return Vector2(v.x * f, v.y * f);
|
||||||
|
// return Vector2(f * v.x, f * v.y);
|
||||||
|
}
|
||||||
Vector2 operator*=(float f);
|
Vector2 operator*=(float f);
|
||||||
/// @brief Scale the vector uniformly down
|
/// @brief Scale the vector uniformly down
|
||||||
/// @param f The scaling factor
|
/// @param f The scaling factor
|
||||||
|
35
Vector3.cpp
35
Vector3.cpp
@ -68,12 +68,16 @@ const Vector3 Vector3::back = Vector3(0, 0, -1);
|
|||||||
float Vector3::Magnitude(const Vector3& v) {
|
float Vector3::Magnitude(const Vector3& v) {
|
||||||
return sqrtf(v.x * v.x + v.y * v.y + v.z * v.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& v) {
|
float Vector3::SqrMagnitude(const Vector3& v) {
|
||||||
return v.x * v.x + v.y * v.y + v.z * v.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);
|
||||||
|
}
|
||||||
|
|
||||||
Vector3 Vector3::Normalize(const Vector3& v) {
|
Vector3 Vector3::Normalize(const Vector3& v) {
|
||||||
float num = Vector3::Magnitude(v);
|
float num = Vector3::Magnitude(v);
|
||||||
@ -118,24 +122,24 @@ Vector3 Vector3::operator+=(const Vector3 &v) {
|
|||||||
Vector3 Vector3::Scale(const Vector3& v1, const Vector3& v2) {
|
Vector3 Vector3::Scale(const Vector3& v1, const Vector3& v2) {
|
||||||
return Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
|
return Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
|
||||||
}
|
}
|
||||||
Vector3 Passer::LinearAlgebra::operator*(const Vector3 &v, float f) {
|
// Vector3 Passer::LinearAlgebra::operator*(const Vector3 &v, float f) {
|
||||||
return Vector3(v.x * f, v.y * f, v.z * f);
|
// return Vector3(v.x * f, v.y * f, v.z * f);
|
||||||
}
|
// }
|
||||||
Vector3 Passer::LinearAlgebra::operator*(float f, const Vector3 &v) {
|
// Vector3 Passer::LinearAlgebra::operator*(float f, const Vector3 &v) {
|
||||||
return Vector3(v.x * f, v.y * f, v.z * f);
|
// return Vector3(v.x * f, v.y * f, v.z * f);
|
||||||
}
|
// }
|
||||||
Vector3 Vector3::operator*=(float f) {
|
Vector3 Vector3::operator*=(float f) {
|
||||||
this->x *= f;
|
this->x *= f;
|
||||||
this->y *= f;
|
this->y *= f;
|
||||||
this->z *= f;
|
this->z *= f;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
Vector3 Passer::LinearAlgebra::operator/(const Vector3 &v, float f) {
|
// Vector3 Passer::LinearAlgebra::operator/(const Vector3 &v, float f) {
|
||||||
return Vector3(v.x / f, v.y / f, v.z / f);
|
// return Vector3(v.x / f, v.y / f, v.z / f);
|
||||||
}
|
// }
|
||||||
Vector3 Passer::LinearAlgebra::operator/(float f, const Vector3 &v) {
|
// Vector3 Passer::LinearAlgebra::operator/(float f, const Vector3 &v) {
|
||||||
return Vector3(v.x / f, v.y / f, v.z / f);
|
// return Vector3(v.x / f, v.y / f, v.z / f);
|
||||||
}
|
// }
|
||||||
Vector3 Vector3::operator/=(float f) {
|
Vector3 Vector3::operator/=(float f) {
|
||||||
this->x /= f;
|
this->x /= f;
|
||||||
this->y /= f;
|
this->y /= f;
|
||||||
@ -197,7 +201,8 @@ float Vector3::Angle(const Vector3 &v1, const Vector3 &v2) {
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
float Vector3::SignedAngle(const Vector3 &v1, const Vector3 &v2,
|
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(v1, v2);
|
float angle = Vector3::Angle(v1, v2);
|
||||||
|
21
Vector3.h
21
Vector3.h
@ -146,15 +146,25 @@ 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.
|
||||||
friend Vector3 operator*(const Vector3 &v, float f);
|
friend Vector3 operator*(const Vector3& v, float f) {
|
||||||
friend Vector3 operator*(float f, const Vector3 &v);
|
return Vector3(v.x * f, v.y * f, v.z * f);
|
||||||
|
}
|
||||||
|
friend Vector3 operator*(float f, const Vector3& v) {
|
||||||
|
// return Vector3(f * v.x, f * v.y, f * v.z);
|
||||||
|
return Vector3(v.x * f, v.y * f, v.z * f);
|
||||||
|
}
|
||||||
Vector3 operator*=(float f);
|
Vector3 operator*=(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.
|
||||||
friend Vector3 operator/(const Vector3 &v, float f);
|
friend Vector3 operator/(const Vector3& v, float f) {
|
||||||
friend Vector3 operator/(float f, const Vector3 &v);
|
return Vector3(v.x / f, v.y / f, v.z / f);
|
||||||
|
}
|
||||||
|
friend Vector3 operator/(float f, const Vector3& v) {
|
||||||
|
// return Vector3(f / v.x, f / v.y, f / v.z);
|
||||||
|
return Vector3(v.x / f, v.y / f, v.z / f);
|
||||||
|
}
|
||||||
Vector3 operator/=(float f);
|
Vector3 operator/=(float f);
|
||||||
|
|
||||||
/// @brief The distance between two vectors
|
/// @brief The distance between two vectors
|
||||||
@ -200,7 +210,8 @@ public:
|
|||||||
/// @param v2 The ending vector
|
/// @param v2 The ending vector
|
||||||
/// @param axis The axis to rotate around
|
/// @param axis The axis to rotate around
|
||||||
/// @return The signed angle between the two vectors
|
/// @return The signed angle between the two vectors
|
||||||
static float SignedAngle(const Vector3 &v1, const Vector3 &v2,
|
static float SignedAngle(const Vector3& v1,
|
||||||
|
const Vector3& v2,
|
||||||
const Vector3& axis);
|
const Vector3& axis);
|
||||||
|
|
||||||
/// @brief Lerp (linear interpolation) between two vectors
|
/// @brief Lerp (linear interpolation) between two vectors
|
||||||
|
Loading…
x
Reference in New Issue
Block a user