Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
sufferiing committed Jan 7, 2025
2 parents 3b3ad0d + 550edc1 commit 628c3d2
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 153 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ IS_LIBRARY:=1
# Be sure that your header files are in the include directory inside of a folder with the
# same name as what you set LIBNAME to below.
LIBNAME:=units
VERSION:=0.3.3
VERSION:=0.5.2
# EXCLUDE_SRC_FROM_LIB= $(SRCDIR)/unpublishedfile.c
# this line excludes opcontrol.c and similar files
EXCLUDE_SRC_FROM_LIB+=$(foreach file, $(SRCDIR)/main,$(foreach cext,$(CEXTS),$(file).$(cext)) $(foreach cxxext,$(CXXEXTS),$(file).$(cxxext)))
Expand Down
95 changes: 77 additions & 18 deletions include/units/Angle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,73 @@ inline std::ostream& operator<<(std::ostream& os, const Angle& quantity) {
return os;
}

/**
* @brief DO NOT USE
*
* this class prevents conversion errors from compass angles to angles in standard position.
*
* consider the following:
* 0_cDeg gets converted to standard position angles internally, so it's converted to 90
* -0_cDeg is converted to standard position angles internally, and only afterwards is the
* negative applied, so now it equals -90 internally
*
* This class solves this problem by introducing the CAngle type. You can do things like
* negate it, multiply it, etc. without messing up the angle. However, this class can
* only be created through string literals, you can't do something like
* CAngle angle = 2_cDeg;
* because the constructor is private. However, you can do
* Angle angle = 2_cDeg;
*/
class CAngle {
// make string literals friends, so they have access to the constructor
friend constexpr CAngle operator""_cRad(long double value);
friend constexpr CAngle operator""_cRad(unsigned long long value);
friend constexpr CAngle operator""_cDeg(long double value);
friend constexpr CAngle operator""_cDeg(unsigned long long value);
friend constexpr CAngle operator""_cRot(long double value);
friend constexpr CAngle operator""_cRot(unsigned long long value);
public:
// we don't want CAngle to have move, copy, or assignment operators
constexpr CAngle& operator=(const CAngle&) = delete;
constexpr CAngle(const CAngle&) = delete;

// make CAngle able to be implicitly converted to Angle
constexpr operator Angle() const { return Angle(M_PI_2 - this->value); }

constexpr CAngle operator-() const { return CAngle(-this->value); }

constexpr CAngle operator+() const { return CAngle(this->value); }
private:
const double value;

constexpr CAngle(double value) : value(value) {}
};

/**
* @brief Angle Distance class
*
* yet another helper class to manage the compass angle fiasco (it's getting nuked on May 15 2025)
*
* consider the following:
* Angle exitRange1 = 0_cDeg;
* Angle exitRange2 = 0_stDeg;
*
* It is expected that exitRange1 and exitRange2 is equal to each other.
* However, this is not the case. 0_cDeg gets converted to 90_stDeg
* implicitly. So, yet another helper class is necessary (hooray)
*
*/
class AngleRange : public Angle {
public:
explicit constexpr AngleRange(double value) : Angle(fabs(value)) {}

constexpr AngleRange(Angle value) : Angle(units::abs(value)) {}

constexpr AngleRange(CAngle value) : Angle(units::abs(Angle(value) - Angle(M_PI_2))) {}
};

constexpr bool operator==(Angle lhs, CAngle rhs) { return lhs == Angle(rhs); }

constexpr Angle rad = Angle(1.0);
constexpr Angle deg = Angle(M_PI / 180);
constexpr Angle rot = Angle(M_TWOPI);
Expand Down Expand Up @@ -59,17 +126,21 @@ constexpr Angle operator""_stRot(long double value) { return static_cast<double>
constexpr Angle operator""_stRot(unsigned long long value) { return static_cast<double>(value) * rot; }

// Compass orientation
constexpr Angle operator""_cRad(long double value) { return 90_stDeg - Angle(static_cast<double>(value)); }
constexpr CAngle operator""_cRad(long double value) { return CAngle(static_cast<double>(value)); }

constexpr Angle operator""_cRad(unsigned long long value) { return 90_stDeg - Angle(static_cast<double>(value)); }
constexpr CAngle operator""_cRad(unsigned long long value) { return CAngle(static_cast<double>(value)); }

constexpr Angle operator""_cDeg(long double value) { return 90_stDeg - static_cast<double>(value) * deg; }
constexpr CAngle operator""_cDeg(long double value) { return CAngle(static_cast<double>(value) * deg.internal()); }

constexpr Angle operator""_cDeg(unsigned long long value) { return 90_stDeg - static_cast<double>(value) * deg; }
constexpr CAngle operator""_cDeg(unsigned long long value) {
return CAngle(static_cast<double>(value) * deg.internal());
}

constexpr Angle operator""_cRot(long double value) { return 90_stDeg - static_cast<double>(value) * rot; }
constexpr CAngle operator""_cRot(long double value) { return CAngle(static_cast<double>(value) * rot.internal()); }

constexpr Angle operator""_cRot(unsigned long long value) { return 90_stDeg - static_cast<double>(value) * rot; }
constexpr CAngle operator""_cRot(unsigned long long value) {
return CAngle(static_cast<double>(value) * rot.internal());
}

// Angle functions
namespace units {
Expand Down Expand Up @@ -99,39 +170,27 @@ static inline Angle constrainAngle180(Angle in) {

// Angle to/from operators
// Standard orientation
constexpr inline Angle from_stRad(double value) { return Angle(value); }

constexpr inline Angle from_stRad(Number value) { return Angle(value.internal()); }

constexpr inline double to_stRad(Angle quantity) { return quantity.internal(); }

constexpr inline Angle from_stDeg(double value) { return value * deg; }

constexpr inline Angle from_stDeg(Number value) { return value * deg; }

constexpr inline double to_stDeg(Angle quantity) { return quantity.convert(deg); }

constexpr inline Angle from_stRot(double value) { return value * rot; }

constexpr inline Angle from_stRot(Number value) { return value * rot; }

constexpr inline double to_stRot(Angle quantity) { return quantity.convert(rot); }

// Compass orientation
constexpr inline Angle from_cRad(double value) { return 90 * deg - Angle(value); }

constexpr inline Angle from_cRad(Number value) { return 90 * deg - Angle(value.internal()); }

constexpr inline double to_cRad(Angle quantity) { return quantity.internal(); }

constexpr inline Angle from_cDeg(double value) { return (90 - value) * deg; }

constexpr inline Angle from_cDeg(Number value) { return (90 - value.internal()) * deg; }

constexpr inline double to_cDeg(Angle quantity) { return (90 * deg - quantity).convert(deg); }

constexpr inline Angle from_cRot(double value) { return (90 - value) * deg; }

constexpr inline Angle from_cRot(Number value) { return (90 - value.internal()) * deg; }

constexpr inline double to_cRot(Angle quantity) { return (90 * deg - quantity).convert(rot); }
6 changes: 0 additions & 6 deletions include/units/Temperature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,14 @@ constexpr Temperature operator""_fahrenheit(unsigned long long value) {

namespace units {

constexpr inline Temperature from_kelvin(double value) { return Temperature(value); }

constexpr inline Temperature from_kelvin(Number value) { return Temperature(value.internal()); }

constexpr inline double to_kelvin(Temperature quantity) { return quantity.internal(); }

constexpr inline Temperature from_celsius(double value) { return Temperature(value + 273.15); }

constexpr inline Temperature from_celsius(Number value) { return Temperature(value.internal() + 273.15); }

constexpr inline double to_celsius(Temperature quantity) { return quantity.internal() - 273.15; }

constexpr inline Temperature from_fahrenheit(double value) { return Temperature((value - 32) * (5.0 / 9.0) + 273.15); }

constexpr inline Temperature from_fahrenheit(Number value) {
return Temperature((value.internal() - 32) * (5.0 / 9.0) + 273.15);
}
Expand Down
46 changes: 6 additions & 40 deletions include/units/Vector2D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,6 @@ template <isQuantity T> class Vector2D {
return (*this);
}

/**
* @brief angle of the vector from the origin
*
* @return Angle
*/
constexpr Angle theta() const { return atan2(this->y, this->x); }

/**
* @brief magnitude of the vector
*
Expand All @@ -228,7 +221,7 @@ template <isQuantity T> class Vector2D {
* @return T
*/
constexpr T distanceTo(const Vector2D<T>& other) const {
return sqrt(square(this->x - other.x, 2) + square(this->y - other.y, 2));
return sqrt(square(this->x - other.x) + square(this->y - other.y));
}

/**
Expand All @@ -240,46 +233,19 @@ template <isQuantity T> class Vector2D {
*/
constexpr Vector2D<T> normalize() const { return (*this) / magnitude(); }

/**
* @brief rotate the vector by an angle
*
* @param angle
*/
constexpr void rotateBy(Angle angle) {
const T m = magnitude();
const Angle t = theta() + angle;
this->x = m * cos(t);
this->y = m * sin(t);
}

/**
* @brief rotate the vector to an angle
*
* @param angle
*/
constexpr void rotateTo(Angle angle) {
const T m = magnitude();
this->x = m * cos(angle);
this->y = m * sin(angle);
}

/**
* @brief get a copy of this vector rotated by an angle
*
* @param angle
* @return Vector2D<T>
*/
constexpr Vector2D<T> rotatedBy(Angle angle) const { return fromPolar(theta() + angle, magnitude()); }

/**
* @brief get a copy of this vector rotated to an angle
*
* @param angle
* @return Vector2D<T>
*/
constexpr Vector2D<T> rotatedTo(Angle angle) const { return fromPolar(angle, magnitude()); }
constexpr Vector2D<T> rotatedBy(Angle angle) const {
return fromPolar(Vector2D<T>({T(0.0), T(0.0)}).angleTo(*this) + angle, magnitude());
}
};

template <typename T> inline constexpr Vector2D<T> origin({T(0.0), T(0.0)});

/**
* @brief * operator overload. Multiplies a quantity and a vector
*
Expand Down
Loading

0 comments on commit 628c3d2

Please sign in to comment.