Compare commits
2 Commits
main
...
Experiment
Author | SHA1 | Date | |
---|---|---|---|
05a3b09974 | |||
![]() |
95a248ab6e |
113
Angle.cpp
113
Angle.cpp
@ -3,15 +3,15 @@
|
||||
// file, You can obtain one at https ://mozilla.org/MPL/2.0/.
|
||||
|
||||
#include "Angle.h"
|
||||
#include "FloatSingle.h"
|
||||
#include <math.h>
|
||||
#include "FloatSingle.h"
|
||||
|
||||
const float Rad2Deg = 57.29578F;
|
||||
const float Deg2Rad = 0.0174532924F;
|
||||
namespace LinearAlgebra {
|
||||
|
||||
//===== AngleSingle, AngleOf<float>
|
||||
|
||||
template <> AngleOf<float> Passer::LinearAlgebra::AngleOf<float>::Degrees(float degrees) {
|
||||
template <>
|
||||
AngleOf<float> AngleOf<float>::Degrees(float degrees) {
|
||||
if (isfinite(degrees)) {
|
||||
while (degrees < -180)
|
||||
degrees += 360;
|
||||
@ -22,7 +22,8 @@ template <> AngleOf<float> Passer::LinearAlgebra::AngleOf<float>::Degrees(float
|
||||
return AngleOf<float>(degrees);
|
||||
}
|
||||
|
||||
template <> AngleOf<float> AngleOf<float>::Radians(float radians) {
|
||||
template <>
|
||||
AngleOf<float> AngleOf<float>::Radians(float radians) {
|
||||
if (isfinite(radians)) {
|
||||
while (radians <= -pi)
|
||||
radians += 2 * pi;
|
||||
@ -33,9 +34,13 @@ template <> AngleOf<float> AngleOf<float>::Radians(float radians) {
|
||||
return Binary(radians * Rad2Deg);
|
||||
}
|
||||
|
||||
template <> float AngleOf<float>::InDegrees() const { return this->value; }
|
||||
template <>
|
||||
float AngleOf<float>::InDegrees() const {
|
||||
return this->value;
|
||||
}
|
||||
|
||||
template <> float AngleOf<float>::InRadians() const {
|
||||
template <>
|
||||
float AngleOf<float>::InRadians() const {
|
||||
return this->value * Deg2Rad;
|
||||
}
|
||||
|
||||
@ -58,25 +63,29 @@ AngleOf<signed short> AngleOf<signed short>::Radians(float radians) {
|
||||
return Binary(value);
|
||||
}
|
||||
|
||||
template <> float AngleOf<signed short>::InDegrees() const {
|
||||
template <>
|
||||
float AngleOf<signed short>::InDegrees() const {
|
||||
float degrees = this->value / 65536.0f * 360.0f;
|
||||
return degrees;
|
||||
}
|
||||
|
||||
template <> float AngleOf<signed short>::InRadians() const {
|
||||
template <>
|
||||
float AngleOf<signed short>::InRadians() const {
|
||||
float radians = this->value / 65536.0f * (2 * pi);
|
||||
return radians;
|
||||
}
|
||||
|
||||
//===== Angle8, AngleOf<signed char>
|
||||
|
||||
template <> AngleOf<signed char> AngleOf<signed char>::Degrees(float degrees) {
|
||||
template <>
|
||||
AngleOf<signed char> AngleOf<signed char>::Degrees(float degrees) {
|
||||
// map float [-180..180) to integer [-128..127)
|
||||
signed char value = (signed char)roundf(degrees / 360.0F * 256.0F);
|
||||
return Binary(value);
|
||||
}
|
||||
|
||||
template <> AngleOf<signed char> AngleOf<signed char>::Radians(float radians) {
|
||||
template <>
|
||||
AngleOf<signed char> AngleOf<signed char>::Radians(float radians) {
|
||||
if (!isfinite(radians))
|
||||
return AngleOf<signed char>::zero;
|
||||
|
||||
@ -85,32 +94,42 @@ template <> AngleOf<signed char> AngleOf<signed char>::Radians(float radians) {
|
||||
return Binary(value);
|
||||
}
|
||||
|
||||
template <> float AngleOf<signed char>::InDegrees() const {
|
||||
template <>
|
||||
float AngleOf<signed char>::InDegrees() const {
|
||||
float degrees = this->value / 256.0f * 360.0f;
|
||||
return degrees;
|
||||
}
|
||||
|
||||
template <> float AngleOf<signed char>::InRadians() const {
|
||||
template <>
|
||||
float AngleOf<signed char>::InRadians() const {
|
||||
float radians = this->value / 128.0f * pi;
|
||||
return radians;
|
||||
}
|
||||
|
||||
//===== Generic
|
||||
|
||||
template <typename T> AngleOf<T>::AngleOf() : value(0) {}
|
||||
template <typename T>
|
||||
AngleOf<T>::AngleOf() : value(0) {}
|
||||
|
||||
template <typename T> AngleOf<T>::AngleOf(T rawValue) : value(rawValue) {}
|
||||
template <typename T>
|
||||
AngleOf<T>::AngleOf(T rawValue) : value(rawValue) {}
|
||||
|
||||
template <typename T> const AngleOf<T> AngleOf<T>::zero = AngleOf<T>();
|
||||
template <typename T>
|
||||
const AngleOf<T> AngleOf<T>::zero = AngleOf<T>();
|
||||
|
||||
template <typename T> AngleOf<T> AngleOf<T>::Binary(T rawValue) {
|
||||
template <typename T>
|
||||
AngleOf<T> AngleOf<T>::Binary(T rawValue) {
|
||||
AngleOf<T> angle = AngleOf<T>();
|
||||
angle.SetBinary(rawValue);
|
||||
return angle;
|
||||
}
|
||||
|
||||
template <typename T> T AngleOf<T>::GetBinary() const { return this->value; }
|
||||
template <typename T> void AngleOf<T>::SetBinary(T rawValue) {
|
||||
template <typename T>
|
||||
T AngleOf<T>::GetBinary() const {
|
||||
return this->value;
|
||||
}
|
||||
template <typename T>
|
||||
void AngleOf<T>::SetBinary(T rawValue) {
|
||||
this->value = rawValue;
|
||||
}
|
||||
|
||||
@ -119,24 +138,28 @@ bool AngleOf<T>::operator==(const AngleOf<T> angle) const {
|
||||
return this->value == angle.value;
|
||||
}
|
||||
|
||||
template <typename T> bool AngleOf<T>::operator>(AngleOf<T> angle) const {
|
||||
template <typename T>
|
||||
bool AngleOf<T>::operator>(AngleOf<T> angle) const {
|
||||
return this->value > angle.value;
|
||||
}
|
||||
|
||||
template <typename T> bool AngleOf<T>::operator>=(AngleOf<T> angle) const {
|
||||
template <typename T>
|
||||
bool AngleOf<T>::operator>=(AngleOf<T> angle) const {
|
||||
return this->value >= angle.value;
|
||||
}
|
||||
|
||||
template <typename T> bool AngleOf<T>::operator<(AngleOf<T> angle) const {
|
||||
template <typename T>
|
||||
bool AngleOf<T>::operator<(AngleOf<T> angle) const {
|
||||
return this->value < angle.value;
|
||||
}
|
||||
|
||||
template <typename T> bool AngleOf<T>::operator<=(AngleOf<T> angle) const {
|
||||
template <typename T>
|
||||
bool AngleOf<T>::operator<=(AngleOf<T> angle) const {
|
||||
return this->value <= angle.value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
signed int Passer::LinearAlgebra::AngleOf<T>::Sign(AngleOf<T> angle) {
|
||||
signed int AngleOf<T>::Sign(AngleOf<T> angle) {
|
||||
if (angle.value < 0)
|
||||
return -1;
|
||||
if (angle.value > 0)
|
||||
@ -145,14 +168,15 @@ signed int Passer::LinearAlgebra::AngleOf<T>::Sign(AngleOf<T> angle) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
AngleOf<T> Passer::LinearAlgebra::AngleOf<T>::Abs(AngleOf<T> angle) {
|
||||
AngleOf<T> AngleOf<T>::Abs(AngleOf<T> angle) {
|
||||
if (Sign(angle) < 0)
|
||||
return -angle;
|
||||
else
|
||||
return angle;
|
||||
}
|
||||
|
||||
template <typename T> AngleOf<T> AngleOf<T>::operator-() const {
|
||||
template <typename T>
|
||||
AngleOf<T> AngleOf<T>::operator-() const {
|
||||
AngleOf<T> angle = Binary(-this->value);
|
||||
return angle;
|
||||
}
|
||||
@ -206,7 +230,8 @@ AngleOf<T> AngleOf<T>::operator+=(const AngleOf<T> &angle) {
|
||||
// return AngleOf::Degrees((float)factor * angle.InDegrees());
|
||||
// }
|
||||
|
||||
template <typename T> void AngleOf<T>::Normalize() {
|
||||
template <typename T>
|
||||
void AngleOf<T>::Normalize() {
|
||||
float angleValue = this->InDegrees();
|
||||
if (!isfinite(angleValue))
|
||||
return;
|
||||
@ -218,7 +243,8 @@ template <typename T> void AngleOf<T>::Normalize() {
|
||||
*this = AngleOf::Degrees(angleValue);
|
||||
}
|
||||
|
||||
template <typename T> AngleOf<T> AngleOf<T>::Normalize(AngleOf<T> angle) {
|
||||
template <typename T>
|
||||
AngleOf<T> AngleOf<T>::Normalize(AngleOf<T> angle) {
|
||||
float angleValue = angle.InDegrees();
|
||||
if (!isfinite(angleValue))
|
||||
return angle;
|
||||
@ -237,7 +263,8 @@ AngleOf<T> AngleOf<T>::Clamp(AngleOf<T> angle, AngleOf<T> min, AngleOf<T> max) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
AngleOf<T> AngleOf<T>::MoveTowards(AngleOf<T> fromAngle, AngleOf<T> toAngle,
|
||||
AngleOf<T> AngleOf<T>::MoveTowards(AngleOf<T> fromAngle,
|
||||
AngleOf<T> toAngle,
|
||||
float maxDegrees) {
|
||||
maxDegrees = fmaxf(0, maxDegrees); // filter out negative distances
|
||||
AngleOf<T> d = toAngle - fromAngle;
|
||||
@ -249,28 +276,34 @@ AngleOf<T> AngleOf<T>::MoveTowards(AngleOf<T> fromAngle, AngleOf<T> toAngle,
|
||||
return fromAngle + d;
|
||||
}
|
||||
|
||||
template <typename T> float AngleOf<T>::Cos(AngleOf<T> angle) {
|
||||
template <typename T>
|
||||
float AngleOf<T>::Cos(AngleOf<T> angle) {
|
||||
return cosf(angle.InRadians());
|
||||
}
|
||||
template <typename T> float AngleOf<T>::Sin(AngleOf<T> angle) {
|
||||
template <typename T>
|
||||
float AngleOf<T>::Sin(AngleOf<T> angle) {
|
||||
return sinf(angle.InRadians());
|
||||
}
|
||||
template <typename T> float AngleOf<T>::Tan(AngleOf<T> angle) {
|
||||
template <typename T>
|
||||
float AngleOf<T>::Tan(AngleOf<T> angle) {
|
||||
return tanf(angle.InRadians());
|
||||
}
|
||||
|
||||
template <typename T> AngleOf<T> AngleOf<T>::Acos(float f) {
|
||||
template <typename T>
|
||||
AngleOf<T> AngleOf<T>::Acos(float f) {
|
||||
return AngleOf<T>::Radians(acosf(f));
|
||||
}
|
||||
template <typename T> AngleOf<T> AngleOf<T>::Asin(float f) {
|
||||
template <typename T>
|
||||
AngleOf<T> AngleOf<T>::Asin(float f) {
|
||||
return AngleOf<T>::Radians(asinf(f));
|
||||
}
|
||||
template <typename T> AngleOf<T> AngleOf<T>::Atan(float f) {
|
||||
template <typename T>
|
||||
AngleOf<T> AngleOf<T>::Atan(float f) {
|
||||
return AngleOf<T>::Radians(atanf(f));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
AngleOf<T> Passer::LinearAlgebra::AngleOf<T>::Atan2(float y, float x) {
|
||||
AngleOf<T> AngleOf<T>::Atan2(float y, float x) {
|
||||
return AngleOf<T>::Radians(atan2f(y, x));
|
||||
}
|
||||
|
||||
@ -354,6 +387,8 @@ AngleOf<T> AngleOf<T>::SineRuleAngle(float a, AngleOf<T> beta, float b) {
|
||||
return alpha;
|
||||
}
|
||||
|
||||
template class Passer::LinearAlgebra::AngleOf<float>;
|
||||
template class Passer::LinearAlgebra::AngleOf<signed char>;
|
||||
template class Passer::LinearAlgebra::AngleOf<signed short>;
|
||||
template class AngleOf<float>;
|
||||
template class AngleOf<signed char>;
|
||||
template class AngleOf<signed short>;
|
||||
|
||||
} // namespace LinearAlgebra
|
15
Angle.h
15
Angle.h
@ -5,7 +5,6 @@
|
||||
#ifndef ANGLE_H
|
||||
#define ANGLE_H
|
||||
|
||||
namespace Passer {
|
||||
namespace LinearAlgebra {
|
||||
|
||||
static float pi = 3.1415927410125732421875F;
|
||||
@ -18,7 +17,8 @@ static float Deg2Rad = (pi * 2) / 360.0f;
|
||||
/// The angle is internally limited to (-180..180] degrees or (-PI...PI]
|
||||
/// radians. When an angle exceeds this range, it is normalized to a value
|
||||
/// within the range.
|
||||
template <typename T> class AngleOf {
|
||||
template <typename T>
|
||||
class AngleOf {
|
||||
public:
|
||||
/// @brief Create a new angle with a zero value
|
||||
AngleOf<T>();
|
||||
@ -150,7 +150,8 @@ public:
|
||||
/// @param toAngle The angle to rotate towards
|
||||
/// @param maxAngle The maximum angle to rotate
|
||||
/// @return The rotated angle
|
||||
static AngleOf<T> MoveTowards(AngleOf<T> fromAngle, AngleOf<T> toAngle,
|
||||
static AngleOf<T> MoveTowards(AngleOf<T> fromAngle,
|
||||
AngleOf<T> toAngle,
|
||||
float maxAngle);
|
||||
|
||||
/// @brief Calculates the cosine of an angle
|
||||
@ -215,8 +216,12 @@ using AngleSingle = AngleOf<float>;
|
||||
using Angle16 = AngleOf<signed short>;
|
||||
using Angle8 = AngleOf<signed char>;
|
||||
|
||||
#if defined(ARDUINO)
|
||||
using Angle = Angle16;
|
||||
#else
|
||||
using Angle = AngleSingle;
|
||||
#endif
|
||||
|
||||
} // namespace LinearAlgebra
|
||||
} // namespace Passer
|
||||
using namespace Passer::LinearAlgebra;
|
||||
|
||||
#endif
|
@ -9,7 +9,8 @@
|
||||
|
||||
#include <math.h>
|
||||
|
||||
template <typename T> DirectionOf<T>::DirectionOf() {
|
||||
template <typename T>
|
||||
DirectionOf<T>::DirectionOf() {
|
||||
this->horizontal = AngleOf<T>();
|
||||
this->vertical = AngleOf<T>();
|
||||
}
|
||||
@ -41,7 +42,7 @@ const DirectionOf<T> DirectionOf<T>::right =
|
||||
DirectionOf<T>(AngleOf<T>::Degrees(90), AngleOf<T>());
|
||||
|
||||
template <typename T>
|
||||
Vector3 Passer::LinearAlgebra::DirectionOf<T>::ToVector3() const {
|
||||
Vector3 DirectionOf<T>::ToVector3() const {
|
||||
Quaternion q = Quaternion::Euler(-this->vertical.InDegrees(),
|
||||
this->horizontal.InDegrees(), 0);
|
||||
Vector3 v = q * Vector3::forward;
|
||||
@ -49,12 +50,12 @@ Vector3 Passer::LinearAlgebra::DirectionOf<T>::ToVector3() const {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DirectionOf<T>
|
||||
Passer::LinearAlgebra::DirectionOf<T>::FromVector3(Vector3 vector) {
|
||||
DirectionOf<T> DirectionOf<T>::FromVector3(Vector3 vector) {
|
||||
DirectionOf<T> d;
|
||||
d.horizontal = AngleOf<T>::Atan2(
|
||||
vector.Right(),
|
||||
vector.Forward()); // AngleOf<T>::Radians(atan2f(v.Right(), v.Forward()));
|
||||
vector
|
||||
.Forward()); // AngleOf<T>::Radians(atan2f(v.Right(), v.Forward()));
|
||||
d.vertical =
|
||||
AngleOf<T>::Degrees(-90) -
|
||||
AngleOf<T>::Acos(
|
||||
@ -64,34 +65,32 @@ Passer::LinearAlgebra::DirectionOf<T>::FromVector3(Vector3 vector) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DirectionOf<T> Passer::LinearAlgebra::DirectionOf<T>::Degrees(float horizontal,
|
||||
float vertical) {
|
||||
DirectionOf<T> DirectionOf<T>::Degrees(float horizontal, float vertical) {
|
||||
return DirectionOf<T>(AngleOf<T>::Degrees(horizontal),
|
||||
AngleOf<T>::Degrees(vertical));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DirectionOf<T> Passer::LinearAlgebra::DirectionOf<T>::Radians(float horizontal,
|
||||
float vertical) {
|
||||
DirectionOf<T> DirectionOf<T>::Radians(float horizontal, float vertical) {
|
||||
return DirectionOf<T>(AngleOf<T>::Radians(horizontal),
|
||||
AngleOf<T>::Radians(vertical));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool Passer::LinearAlgebra::DirectionOf<T>::operator==(
|
||||
const DirectionOf<T> direction) const {
|
||||
bool DirectionOf<T>::operator==(const DirectionOf<T> direction) const {
|
||||
return (this->horizontal == direction.horizontal) &&
|
||||
(this->vertical == direction.vertical);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DirectionOf<T> Passer::LinearAlgebra::DirectionOf<T>::operator-() const {
|
||||
DirectionOf<T> DirectionOf<T>::operator-() const {
|
||||
DirectionOf<T> r = DirectionOf<T>(this->horizontal + AngleOf<T>::Degrees(180),
|
||||
-this->vertical);
|
||||
return r;
|
||||
}
|
||||
|
||||
template <typename T> void DirectionOf<T>::Normalize() {
|
||||
template <typename T>
|
||||
void DirectionOf<T>::Normalize() {
|
||||
if (this->vertical > AngleOf<T>::Degrees(90) ||
|
||||
this->vertical < AngleOf<T>::Degrees(-90)) {
|
||||
this->horizontal += AngleOf<T>::Degrees(180);
|
||||
@ -99,5 +98,5 @@ template <typename T> void DirectionOf<T>::Normalize() {
|
||||
}
|
||||
}
|
||||
|
||||
template class Passer::LinearAlgebra::DirectionOf<float>;
|
||||
template class Passer::LinearAlgebra::DirectionOf<signed short>;
|
||||
template class DirectionOf<float>;
|
||||
template class DirectionOf<signed short>;
|
||||
|
14
Direction.h
14
Direction.h
@ -7,7 +7,6 @@
|
||||
|
||||
#include "Angle.h"
|
||||
|
||||
namespace Passer {
|
||||
namespace LinearAlgebra {
|
||||
|
||||
struct Vector3;
|
||||
@ -22,7 +21,8 @@ struct Vector3;
|
||||
/// rotation has been applied.
|
||||
/// The angles are automatically normalized to stay within the abovenmentioned
|
||||
/// ranges.
|
||||
template <typename T> class DirectionOf {
|
||||
template <typename T>
|
||||
class DirectionOf {
|
||||
public:
|
||||
/// @brief horizontal angle, range= (-180..180]
|
||||
AngleOf<T> horizontal;
|
||||
@ -91,8 +91,14 @@ protected:
|
||||
using DirectionSingle = DirectionOf<float>;
|
||||
using Direction16 = DirectionOf<signed short>;
|
||||
|
||||
#if defined(ARDUINO)
|
||||
using Direction = Direction16;
|
||||
#else
|
||||
using Direction = DirectionSingle;
|
||||
#endif
|
||||
|
||||
} // namespace LinearAlgebra
|
||||
} // namespace Passer
|
||||
using namespace Passer::LinearAlgebra;
|
||||
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#endif
|
@ -5,7 +5,6 @@
|
||||
#ifndef FLOAT_H
|
||||
#define FLOAT_H
|
||||
|
||||
namespace Passer {
|
||||
namespace LinearAlgebra {
|
||||
|
||||
class Float {
|
||||
@ -17,7 +16,7 @@ public:
|
||||
};
|
||||
|
||||
} // namespace LinearAlgebra
|
||||
} // namespace Passer
|
||||
using namespace Passer::LinearAlgebra;
|
||||
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#endif
|
||||
|
10
Matrix.h
10
Matrix.h
@ -3,11 +3,11 @@
|
||||
|
||||
#include "Vector3.h"
|
||||
|
||||
namespace Passer {
|
||||
namespace LinearAlgebra {
|
||||
|
||||
/// @brief Single precision float matrix
|
||||
template <typename T> class MatrixOf {
|
||||
template <typename T>
|
||||
class MatrixOf {
|
||||
public:
|
||||
MatrixOf(unsigned int rows, unsigned int cols);
|
||||
MatrixOf(unsigned int rows, unsigned int cols, const T* source)
|
||||
@ -54,7 +54,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
static void Multiply(const MatrixOf<T> *m1, const MatrixOf<T> *m2,
|
||||
static void Multiply(const MatrixOf<T>* m1,
|
||||
const MatrixOf<T>* m2,
|
||||
MatrixOf<T>* r);
|
||||
void Multiply(const MatrixOf<T>* m, MatrixOf<T>* r) const {
|
||||
Multiply(this, m, r);
|
||||
@ -115,7 +116,6 @@ private:
|
||||
};
|
||||
|
||||
} // namespace LinearAlgebra
|
||||
} // namespace Passer
|
||||
using namespace Passer::LinearAlgebra;
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#endif
|
50
Polar.cpp
50
Polar.cpp
@ -3,11 +3,13 @@
|
||||
#include "Polar.h"
|
||||
#include "Vector2.h"
|
||||
|
||||
template <typename T> PolarOf<T>::PolarOf() {
|
||||
template <typename T>
|
||||
PolarOf<T>::PolarOf() {
|
||||
this->distance = 0.0f;
|
||||
this->angle = AngleOf<T>();
|
||||
}
|
||||
template <typename T> PolarOf<T>::PolarOf(float distance, AngleOf<T> angle) {
|
||||
template <typename T>
|
||||
PolarOf<T>::PolarOf(float distance, AngleOf<T> angle) {
|
||||
// distance should always be 0 or greater
|
||||
if (distance < 0.0f) {
|
||||
this->distance = -distance;
|
||||
@ -34,16 +36,18 @@ PolarOf<T> PolarOf<T>::Radians(float distance, float radians) {
|
||||
return PolarOf<T>(distance, AngleOf<T>::Radians(radians));
|
||||
}
|
||||
|
||||
template <typename T> PolarOf<T> PolarOf<T>::FromVector2(Vector2 v) {
|
||||
template <typename T>
|
||||
PolarOf<T> PolarOf<T>::FromVector2(Vector2 v) {
|
||||
float distance = v.magnitude();
|
||||
AngleOf<T> angle =
|
||||
AngleOf<T>::Degrees(Vector2::SignedAngle(Vector2::forward, v));
|
||||
PolarOf<T> p = PolarOf(distance, angle);
|
||||
return p;
|
||||
}
|
||||
template <typename T> PolarOf<T> PolarOf<T>::FromSpherical(SphericalOf<T> v) {
|
||||
float distance = v.distance * cosf(v.direction.vertical.InDegrees() *
|
||||
Passer::LinearAlgebra::Deg2Rad);
|
||||
template <typename T>
|
||||
PolarOf<T> PolarOf<T>::FromSpherical(SphericalOf<T> v) {
|
||||
float distance =
|
||||
v.distance * cosf(v.direction.vertical.InDegrees() * Deg2Rad);
|
||||
AngleOf<T> angle = v.direction.horizontal;
|
||||
PolarOf<T> p = PolarOf(distance, angle);
|
||||
return p;
|
||||
@ -60,31 +64,37 @@ const PolarOf<T> PolarOf<T>::right = PolarOf(1.0, AngleOf<T>::Degrees(90));
|
||||
template <typename T>
|
||||
const PolarOf<T> PolarOf<T>::left = PolarOf(1.0, AngleOf<T>::Degrees(-90));
|
||||
|
||||
template <typename T> bool PolarOf<T>::operator==(const PolarOf &v) const {
|
||||
template <typename T>
|
||||
bool PolarOf<T>::operator==(const PolarOf& v) const {
|
||||
return (this->distance == v.distance &&
|
||||
this->angle.InDegrees() == v.angle.InDegrees());
|
||||
}
|
||||
|
||||
template <typename T> PolarOf<T> PolarOf<T>::Normalize(const PolarOf &v) {
|
||||
template <typename T>
|
||||
PolarOf<T> PolarOf<T>::Normalize(const PolarOf& v) {
|
||||
PolarOf<T> r = PolarOf(1, v.angle);
|
||||
return r;
|
||||
}
|
||||
template <typename T> PolarOf<T> PolarOf<T>::normalized() const {
|
||||
template <typename T>
|
||||
PolarOf<T> PolarOf<T>::normalized() const {
|
||||
PolarOf<T> r = PolarOf(1, this->angle);
|
||||
return r;
|
||||
}
|
||||
|
||||
template <typename T> PolarOf<T> PolarOf<T>::operator-() const {
|
||||
template <typename T>
|
||||
PolarOf<T> PolarOf<T>::operator-() const {
|
||||
PolarOf<T> v =
|
||||
PolarOf(this->distance, this->angle + AngleOf<T>::Degrees(180));
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename T> PolarOf<T> PolarOf<T>::operator-(const PolarOf &v) const {
|
||||
template <typename T>
|
||||
PolarOf<T> PolarOf<T>::operator-(const PolarOf& v) const {
|
||||
PolarOf<T> r = -v;
|
||||
return *this + r;
|
||||
}
|
||||
template <typename T> PolarOf<T> PolarOf<T>::operator-=(const PolarOf &v) {
|
||||
template <typename T>
|
||||
PolarOf<T> PolarOf<T>::operator-=(const PolarOf& v) {
|
||||
*this = *this - v;
|
||||
return *this;
|
||||
// angle = AngleOf<T>::Normalize(newAngle);
|
||||
@ -105,7 +115,8 @@ template <typename T> PolarOf<T> PolarOf<T>::operator-=(const PolarOf &v) {
|
||||
// return d;
|
||||
// }
|
||||
|
||||
template <typename T> PolarOf<T> PolarOf<T>::operator+(const PolarOf &v) const {
|
||||
template <typename T>
|
||||
PolarOf<T> PolarOf<T>::operator+(const PolarOf& v) const {
|
||||
if (v.distance == 0)
|
||||
return PolarOf(this->distance, this->angle);
|
||||
if (this->distance == 0.0f)
|
||||
@ -133,16 +144,19 @@ template <typename T> PolarOf<T> PolarOf<T>::operator+(const PolarOf &v) const {
|
||||
PolarOf vector = PolarOf(newDistance, newAngleA);
|
||||
return vector;
|
||||
}
|
||||
template <typename T> PolarOf<T> PolarOf<T>::operator+=(const PolarOf &v) {
|
||||
template <typename T>
|
||||
PolarOf<T> PolarOf<T>::operator+=(const PolarOf& v) {
|
||||
*this = *this + v;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T> PolarOf<T> PolarOf<T>::operator*=(float f) {
|
||||
template <typename T>
|
||||
PolarOf<T> PolarOf<T>::operator*=(float f) {
|
||||
this->distance *= f;
|
||||
return *this;
|
||||
}
|
||||
template <typename T> PolarOf<T> PolarOf<T>::operator/=(float f) {
|
||||
template <typename T>
|
||||
PolarOf<T> PolarOf<T>::operator/=(float f) {
|
||||
this->distance /= f;
|
||||
return *this;
|
||||
}
|
||||
@ -161,5 +175,5 @@ PolarOf<T> PolarOf<T>::Rotate(const PolarOf &v, AngleOf<T> angle) {
|
||||
return r;
|
||||
}
|
||||
|
||||
template class Passer::LinearAlgebra::PolarOf<float>;
|
||||
template class Passer::LinearAlgebra::PolarOf<signed short>;
|
||||
template class PolarOf<float>;
|
||||
template class PolarOf<signed short>;
|
10
Polar.h
10
Polar.h
@ -7,15 +7,16 @@
|
||||
|
||||
#include "Angle.h"
|
||||
|
||||
namespace Passer {
|
||||
namespace LinearAlgebra {
|
||||
|
||||
struct Vector2;
|
||||
template <typename T> class SphericalOf;
|
||||
template <typename T>
|
||||
class SphericalOf;
|
||||
|
||||
/// @brief A polar vector using an angle in various representations
|
||||
/// @tparam T The implementation type used for the representation of the angle
|
||||
template <typename T> class PolarOf {
|
||||
template <typename T>
|
||||
class PolarOf {
|
||||
public:
|
||||
/// @brief The distance in meters
|
||||
/// @remark The distance shall never be negative
|
||||
@ -153,8 +154,7 @@ using Polar16 = PolarOf<signed short>;
|
||||
// using Polar = PolarSingle;
|
||||
|
||||
} // namespace LinearAlgebra
|
||||
} // namespace Passer
|
||||
using namespace Passer::LinearAlgebra;
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#include "Spherical.h"
|
||||
#include "Vector2.h"
|
||||
|
19
Quaternion.h
19
Quaternion.h
@ -32,7 +32,6 @@ typedef struct Quat {
|
||||
} Quat;
|
||||
}
|
||||
|
||||
namespace Passer {
|
||||
namespace LinearAlgebra {
|
||||
|
||||
/// <summary>
|
||||
@ -157,7 +156,8 @@ public:
|
||||
/// <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>
|
||||
@ -191,7 +191,8 @@ public:
|
||||
/// <returns>The resulting rotation</returns>
|
||||
/// A factor 0 returns rotation1, factor1 returns rotation2.
|
||||
static Quaternion Slerp(const Quaternion& rotation1,
|
||||
const Quaternion &rotation2, float factor);
|
||||
const Quaternion& rotation2,
|
||||
float factor);
|
||||
/// <summary>
|
||||
/// Unclamped sherical lerp between two rotations
|
||||
/// </summary>
|
||||
@ -202,7 +203,8 @@ public:
|
||||
/// 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);
|
||||
const Quaternion& rotation2,
|
||||
float factor);
|
||||
|
||||
/// <summary>
|
||||
/// Create a rotation from euler angles
|
||||
@ -260,8 +262,10 @@ public:
|
||||
/// <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
|
||||
@ -284,7 +288,6 @@ private:
|
||||
};
|
||||
|
||||
} // namespace LinearAlgebra
|
||||
} // namespace Passer
|
||||
using namespace Passer::LinearAlgebra;
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#endif
|
||||
|
@ -5,13 +5,15 @@
|
||||
|
||||
#include <math.h>
|
||||
|
||||
template <typename T> SphericalOf<T>::SphericalOf() {
|
||||
template <typename T>
|
||||
SphericalOf<T>::SphericalOf() {
|
||||
this->distance = 0.0f;
|
||||
this->direction = DirectionOf<T>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
SphericalOf<T>::SphericalOf(float distance, AngleOf<T> horizontal,
|
||||
SphericalOf<T>::SphericalOf(float distance,
|
||||
AngleOf<T> horizontal,
|
||||
AngleOf<T> vertical) {
|
||||
if (distance < 0) {
|
||||
this->distance = -distance;
|
||||
@ -34,7 +36,8 @@ SphericalOf<T>::SphericalOf(float distance, DirectionOf<T> direction) {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
SphericalOf<T> SphericalOf<T>::Degrees(float distance, float horizontal,
|
||||
SphericalOf<T> SphericalOf<T>::Degrees(float distance,
|
||||
float horizontal,
|
||||
float vertical) {
|
||||
AngleOf<T> horizontalAngle = AngleOf<T>::Degrees(horizontal);
|
||||
AngleOf<T> verticalAngle = AngleOf<T>::Degrees(vertical);
|
||||
@ -43,7 +46,8 @@ SphericalOf<T> SphericalOf<T>::Degrees(float distance, float horizontal,
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
SphericalOf<T> SphericalOf<T>::Radians(float distance, float horizontal,
|
||||
SphericalOf<T> SphericalOf<T>::Radians(float distance,
|
||||
float horizontal,
|
||||
float vertical) {
|
||||
return SphericalOf<T>(distance, AngleOf<T>::Radians(horizontal),
|
||||
AngleOf<T>::Radians(vertical));
|
||||
@ -57,7 +61,8 @@ SphericalOf<T> SphericalOf<T>::FromPolar(PolarOf<T> polar) {
|
||||
return r;
|
||||
}
|
||||
|
||||
template <typename T> SphericalOf<T> SphericalOf<T>::FromVector3(Vector3 v) {
|
||||
template <typename T>
|
||||
SphericalOf<T> SphericalOf<T>::FromVector3(Vector3 v) {
|
||||
float distance = v.magnitude();
|
||||
if (distance == 0.0f) {
|
||||
return SphericalOf(distance, AngleOf<T>(), AngleOf<T>());
|
||||
@ -81,7 +86,8 @@ template <typename T> SphericalOf<T> SphericalOf<T>::FromVector3(Vector3 v) {
|
||||
* @tparam T The type of the distance and direction values.
|
||||
* @return Vector3 The 3D vector representation of the spherical coordinates.
|
||||
*/
|
||||
template <typename T> Vector3 SphericalOf<T>::ToVector3() const {
|
||||
template <typename T>
|
||||
Vector3 SphericalOf<T>::ToVector3() const {
|
||||
float verticalRad = (pi / 2) - this->direction.vertical.InRadians();
|
||||
float horizontalRad = this->direction.horizontal.InRadians();
|
||||
|
||||
@ -126,7 +132,8 @@ SphericalOf<T> SphericalOf<T>::WithDistance(float distance) {
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename T> SphericalOf<T> SphericalOf<T>::operator-() const {
|
||||
template <typename T>
|
||||
SphericalOf<T> SphericalOf<T>::operator-() const {
|
||||
SphericalOf<T> v = SphericalOf<T>(
|
||||
this->distance, this->direction.horizontal + AngleOf<T>::Degrees(180),
|
||||
this->direction.vertical + AngleOf<T>::Degrees(180));
|
||||
@ -209,12 +216,14 @@ SphericalOf<T> SphericalOf<T>::operator+=(const SphericalOf<T> &v) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T> SphericalOf<T> SphericalOf<T>::operator*=(float f) {
|
||||
template <typename T>
|
||||
SphericalOf<T> SphericalOf<T>::operator*=(float f) {
|
||||
this->distance *= f;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T> SphericalOf<T> SphericalOf<T>::operator/=(float f) {
|
||||
template <typename T>
|
||||
SphericalOf<T> SphericalOf<T>::operator/=(float f) {
|
||||
this->distance /= f;
|
||||
return *this;
|
||||
}
|
||||
@ -256,8 +265,8 @@ AngleOf<T> SphericalOf<T>::AngleBetween(const SphericalOf &v1,
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
AngleOf<T> Passer::LinearAlgebra::SphericalOf<T>::SignedAngleBetween(
|
||||
const SphericalOf<T> &v1, const SphericalOf<T> &v2,
|
||||
AngleOf<T> SphericalOf<T>::SignedAngleBetween(const SphericalOf<T>& v1,
|
||||
const SphericalOf<T>& v2,
|
||||
const SphericalOf<T>& axis) {
|
||||
Vector3 v1_vector = v1.ToVector3();
|
||||
Vector3 v2_vector = v2.ToVector3();
|
||||
@ -290,5 +299,5 @@ SphericalOf<T> SphericalOf<T>::RotateVertical(const SphericalOf<T> &v,
|
||||
return r;
|
||||
}
|
||||
|
||||
template class Passer::LinearAlgebra::SphericalOf<float>;
|
||||
template class Passer::LinearAlgebra::SphericalOf<signed short>;
|
||||
template class SphericalOf<float>;
|
||||
template class SphericalOf<signed short>;
|
||||
|
25
Spherical.h
25
Spherical.h
@ -7,15 +7,16 @@
|
||||
|
||||
#include "Direction.h"
|
||||
|
||||
namespace Passer {
|
||||
namespace LinearAlgebra {
|
||||
|
||||
struct Vector3;
|
||||
template <typename T> class PolarOf;
|
||||
template <typename T>
|
||||
class PolarOf;
|
||||
|
||||
/// @brief A spherical vector using angles in various representations
|
||||
/// @tparam T The implementation type used for the representations of the agles
|
||||
template <typename T> class SphericalOf {
|
||||
template <typename T>
|
||||
class SphericalOf {
|
||||
public:
|
||||
/// @brief The distance in meters
|
||||
/// @remark The distance should never be negative
|
||||
@ -38,7 +39,8 @@ public:
|
||||
/// @param horizontal The horizontal angle in degrees
|
||||
/// @param vertical The vertical angle in degrees
|
||||
/// @return The spherical vector
|
||||
static SphericalOf<T> Degrees(float distance, float horizontal,
|
||||
static SphericalOf<T> Degrees(float distance,
|
||||
float horizontal,
|
||||
float vertical);
|
||||
/// @brief Short-hand Deg alias for the Degrees function
|
||||
constexpr static auto Deg = Degrees;
|
||||
@ -48,7 +50,8 @@ public:
|
||||
/// @param horizontal The horizontal angle in radians
|
||||
/// @param vertical The vertical angle in radians
|
||||
/// @return The spherical vectpr
|
||||
static SphericalOf<T> Radians(float distance, float horizontal,
|
||||
static SphericalOf<T> Radians(float distance,
|
||||
float horizontal,
|
||||
float vertical);
|
||||
// Short-hand Rad alias for the Radians function
|
||||
constexpr static auto Rad = Radians;
|
||||
@ -154,7 +157,8 @@ public:
|
||||
/// @param horizontalAngle The horizontal rotation angle in local space
|
||||
/// @param verticalAngle The vertical rotation angle in local space
|
||||
/// @return The rotated vector
|
||||
static SphericalOf<T> Rotate(const SphericalOf &v, AngleOf<T> horizontalAngle,
|
||||
static SphericalOf<T> Rotate(const SphericalOf& v,
|
||||
AngleOf<T> horizontalAngle,
|
||||
AngleOf<T> verticalAngle);
|
||||
/// @brief Rotate a spherical vector horizontally
|
||||
/// @param v The vector to rotate
|
||||
@ -180,9 +184,14 @@ using SphericalSingle = SphericalOf<float>;
|
||||
/// hardware
|
||||
using Spherical16 = SphericalOf<signed short>;
|
||||
|
||||
#if defined(ARDUINO)
|
||||
using Spherical = Spherical16;
|
||||
#else
|
||||
using Spherical = SphericalSingle;
|
||||
#endif
|
||||
|
||||
} // namespace LinearAlgebra
|
||||
} // namespace Passer
|
||||
using namespace Passer::LinearAlgebra;
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#include "Polar.h"
|
||||
#include "Vector3.h"
|
||||
|
@ -164,5 +164,5 @@ void SwingTwistOf<T>::Normalize() {
|
||||
}
|
||||
}
|
||||
|
||||
template class Passer::LinearAlgebra::SwingTwistOf<float>;
|
||||
template class Passer::LinearAlgebra::SwingTwistOf<signed short>;
|
||||
template class SwingTwistOf<float>;
|
||||
template class SwingTwistOf<signed short>;
|
16
SwingTwist.h
16
SwingTwist.h
@ -10,13 +10,13 @@
|
||||
#include "Quaternion.h"
|
||||
#include "Spherical.h"
|
||||
|
||||
namespace Passer {
|
||||
namespace LinearAlgebra {
|
||||
|
||||
/// @brief An orientation using swing and twist angles in various
|
||||
/// representations
|
||||
/// @tparam T The implmentation type used for the representation of the angles
|
||||
template <typename T> class SwingTwistOf {
|
||||
template <typename T>
|
||||
class SwingTwistOf {
|
||||
public:
|
||||
DirectionOf<T> swing;
|
||||
AngleOf<T> twist;
|
||||
@ -25,7 +25,8 @@ public:
|
||||
SwingTwistOf<T>(DirectionOf<T> swing, AngleOf<T> twist);
|
||||
SwingTwistOf<T>(AngleOf<T> horizontal, AngleOf<T> vertical, AngleOf<T> twist);
|
||||
|
||||
static SwingTwistOf<T> Degrees(float horizontal, float vertical = 0,
|
||||
static SwingTwistOf<T> Degrees(float horizontal,
|
||||
float vertical = 0,
|
||||
float twist = 0);
|
||||
|
||||
Quaternion ToQuaternion() const;
|
||||
@ -72,8 +73,13 @@ public:
|
||||
using SwingTwistSingle = SwingTwistOf<float>;
|
||||
using SwingTwist16 = SwingTwistOf<signed short>;
|
||||
|
||||
#if defined(ARDUINO)
|
||||
using SwingTwist = SwingTwist16;
|
||||
#else
|
||||
using SwingTwist = SwingTwistSingle;
|
||||
#endif
|
||||
|
||||
} // namespace LinearAlgebra
|
||||
} // namespace Passer
|
||||
using namespace Passer::LinearAlgebra;
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#endif
|
||||
|
25
Vector2.cpp
25
Vector2.cpp
@ -30,7 +30,7 @@ Vector2::Vector2(Vector3 v) {
|
||||
y = v.Forward(); // z;
|
||||
}
|
||||
Vector2::Vector2(PolarSingle p) {
|
||||
float horizontalRad = p.angle.InDegrees() * Passer::LinearAlgebra::Deg2Rad;
|
||||
float horizontalRad = p.angle.InDegrees() * Deg2Rad;
|
||||
float cosHorizontal = cosf(horizontalRad);
|
||||
float sinHorizontal = sinf(horizontalRad);
|
||||
|
||||
@ -56,9 +56,15 @@ bool Vector2::operator==(const Vector2 &v) {
|
||||
float Vector2::Magnitude(const Vector2& v) {
|
||||
return sqrtf(v.x * v.x + v.y * v.y);
|
||||
}
|
||||
float Vector2::magnitude() const { return (float)sqrtf(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); }
|
||||
float Vector2::magnitude() const {
|
||||
return (float)sqrtf(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) {
|
||||
float num = Vector2::Magnitude(v);
|
||||
@ -77,7 +83,9 @@ Vector2 Vector2::normalized() const {
|
||||
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 {
|
||||
return Vector2(this->x - v.x, this->y - v.y);
|
||||
@ -148,12 +156,11 @@ float Vector2::SignedAngle(const Vector2 &v1, const Vector2 &v2) {
|
||||
|
||||
float angleFrom = atan2f(v1.y, v1.x);
|
||||
float angleTo = atan2f(v2.y, v2.x);
|
||||
return -(angleTo - angleFrom) * Passer::LinearAlgebra::Rad2Deg;
|
||||
return -(angleTo - angleFrom) * Rad2Deg;
|
||||
}
|
||||
|
||||
Vector2 Vector2::Rotate(const Vector2 &v,
|
||||
Passer::LinearAlgebra::AngleSingle a) {
|
||||
float angleRad = a.InDegrees() * Passer::LinearAlgebra::Deg2Rad;
|
||||
Vector2 Vector2::Rotate(const Vector2& v, AngleSingle a) {
|
||||
float angleRad = a.InDegrees() * Deg2Rad;
|
||||
#if defined(AVR)
|
||||
float sinValue = sin(angleRad);
|
||||
float cosValue = cos(angleRad); // * Angle::Deg2Rad);
|
||||
|
@ -26,11 +26,11 @@ typedef struct Vec2 {
|
||||
} Vec2;
|
||||
}
|
||||
|
||||
namespace Passer {
|
||||
namespace LinearAlgebra {
|
||||
|
||||
struct Vector3;
|
||||
template <typename T> class PolarOf;
|
||||
template <typename T>
|
||||
class PolarOf;
|
||||
|
||||
/// @brief A 2-dimensional vector
|
||||
/// @remark This uses the right=handed carthesian coordinate system.
|
||||
@ -188,7 +188,7 @@ public:
|
||||
/// @param v The vector to rotate
|
||||
/// @param a The angle in degrees to rotate
|
||||
/// @return The rotated vector
|
||||
static Vector2 Rotate(const Vector2 &v, Passer::LinearAlgebra::AngleSingle a);
|
||||
static Vector2 Rotate(const Vector2& v, AngleSingle a);
|
||||
|
||||
/// @brief Lerp (linear interpolation) between two vectors
|
||||
/// @param v1 The starting vector
|
||||
@ -202,8 +202,7 @@ public:
|
||||
};
|
||||
|
||||
} // namespace LinearAlgebra
|
||||
} // namespace Passer
|
||||
using namespace Passer::LinearAlgebra;
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#include "Polar.h"
|
||||
|
||||
|
17
Vector3.cpp
17
Vector3.cpp
@ -31,10 +31,8 @@ Vector3::Vector3(Vector2 v) {
|
||||
}
|
||||
|
||||
Vector3::Vector3(SphericalOf<float> s) {
|
||||
float verticalRad = (90.0f - s.direction.vertical.InDegrees()) *
|
||||
Passer::LinearAlgebra::Deg2Rad;
|
||||
float horizontalRad =
|
||||
s.direction.horizontal.InDegrees() * Passer::LinearAlgebra::Deg2Rad;
|
||||
float verticalRad = (90.0f - s.direction.vertical.InDegrees()) * Deg2Rad;
|
||||
float horizontalRad = s.direction.horizontal.InDegrees() * Deg2Rad;
|
||||
float cosVertical = cosf(verticalRad);
|
||||
float sinVertical = sinf(verticalRad);
|
||||
float cosHorizontal = cosf(horizontalRad);
|
||||
@ -70,12 +68,16 @@ const Vector3 Vector3::back = Vector3(0, 0, -1);
|
||||
float Vector3::Magnitude(const Vector3& v) {
|
||||
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) {
|
||||
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) {
|
||||
float num = Vector3::Magnitude(v);
|
||||
@ -200,7 +202,8 @@ AngleOf<float> Vector3::Angle(const Vector3 &v1, const Vector3 &v2) {
|
||||
return AngleOf<float>::Radians(r);
|
||||
}
|
||||
|
||||
AngleOf<float> Vector3::SignedAngle(const Vector3 &v1, const Vector3 &v2,
|
||||
AngleOf<float> Vector3::SignedAngle(const Vector3& v1,
|
||||
const Vector3& v2,
|
||||
const Vector3& axis) {
|
||||
// angle in [0,180]
|
||||
AngleOf<float> angle = Vector3::Angle(v1, v2);
|
||||
|
10
Vector3.h
10
Vector3.h
@ -31,10 +31,10 @@ protected:
|
||||
} Vec3;
|
||||
}
|
||||
|
||||
namespace Passer {
|
||||
namespace LinearAlgebra {
|
||||
|
||||
template <typename T> class SphericalOf;
|
||||
template <typename T>
|
||||
class SphericalOf;
|
||||
|
||||
/// @brief A 3-dimensional vector
|
||||
/// @remark This uses a right-handed carthesian coordinate system.
|
||||
@ -210,7 +210,8 @@ public:
|
||||
/// @param v2 The ending vector
|
||||
/// @param axis The axis to rotate around
|
||||
/// @return The signed angle between the two vectors
|
||||
static AngleOf<float> SignedAngle(const Vector3 &v1, const Vector3 &v2,
|
||||
static AngleOf<float> SignedAngle(const Vector3& v1,
|
||||
const Vector3& v2,
|
||||
const Vector3& axis);
|
||||
|
||||
/// @brief Lerp (linear interpolation) between two vectors
|
||||
@ -225,8 +226,7 @@ public:
|
||||
};
|
||||
|
||||
} // namespace LinearAlgebra
|
||||
} // namespace Passer
|
||||
using namespace Passer::LinearAlgebra;
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#include "Spherical.h"
|
||||
|
||||
|
@ -1,11 +1,13 @@
|
||||
#if GTEST
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include <limits>
|
||||
#include <math.h>
|
||||
#include <limits>
|
||||
|
||||
#include "Angle.h"
|
||||
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#define FLOAT_INFINITY std::numeric_limits<float>::infinity()
|
||||
|
||||
TEST(Angle16, Construct) {
|
||||
|
@ -1,11 +1,13 @@
|
||||
#if GTEST
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <limits>
|
||||
#include <math.h>
|
||||
#include <limits>
|
||||
|
||||
#include "Angle.h"
|
||||
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#define FLOAT_INFINITY std::numeric_limits<float>::infinity()
|
||||
|
||||
TEST(Angle8, Construct) {
|
||||
|
@ -1,11 +1,13 @@
|
||||
#if GTEST
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <limits>
|
||||
#include <math.h>
|
||||
#include <limits>
|
||||
|
||||
#include "Angle.h"
|
||||
|
||||
using namespace LinearAlgebra;
|
||||
|
||||
#define FLOAT_INFINITY std::numeric_limits<float>::infinity()
|
||||
|
||||
TEST(AngleSingle, Construct) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user