Skip to content

Commit

Permalink
greatly simplify CAngle
Browse files Browse the repository at this point in the history
  • Loading branch information
SizzinSeal committed Dec 29, 2024
1 parent 2a20ff5 commit ce0bd31
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 58 deletions.
45 changes: 5 additions & 40 deletions include/units/Angle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,25 @@ template <> struct LookupName<Quantity<std::ratio<0>, std::ratio<0>, std::ratio<
* because the constructor is private. However, you can do
* Angle angle = 2_cDeg
*/
class CAngle : public Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
std::ratio<0>, std::ratio<0>> {
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);
friend constexpr CAngle operator*(double multiple, CAngle quantity);
friend constexpr CAngle operator*(CAngle quantity, double multiple);
friend constexpr CAngle operator/(double multiple, CAngle quantity);
friend constexpr CAngle operator/(CAngle quantity, double multiple);
public:
// make CAngle able to be implicitly converted to Angle
constexpr operator Angle() const { return Angle(M_PI_2 - this->value); }

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

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

constexpr Angle operator+(Angle other) const { return Angle(*this) + other; }

constexpr CAngle operator+() const { return CAngle(this->value); }
constexpr Angle operator+() const { return CAngle(this->value); }
private:
// only allow construction through literals
constexpr CAngle(double value)
: Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
std::ratio<0>, std::ratio<0>>(value) {}
const double value;

constexpr CAngle(Angle value)
: Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
std::ratio<0>, std::ratio<0>>(value) {}
constexpr CAngle(double value) : value(value) {}
};

constexpr bool operator==(Angle lhs, CAngle rhs) { return lhs == Angle(rhs); }
Expand All @@ -83,27 +69,6 @@ inline std::ostream& operator<<(std::ostream& os, const Angle& quantity) {
return os;
}

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

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

constexpr CAngle operator*(double multiple, CAngle quantity) { return CAngle(multiple * quantity.internal()); }

constexpr CAngle operator*(CAngle quantity, double multiple) { return CAngle(multiple * quantity.internal()); }

constexpr CAngle operator/(CAngle quantity, double multiple) { return CAngle(quantity.internal() / multiple); }

namespace units {
template <typename T>
concept isAngle = std::same_as<T, CAngle> || std::same_as<T, Angle> ||
std::same_as<T, Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>,
std::ratio<0>, std::ratio<0>, std::ratio<0>>>;

template <isAngle Q, isAngle R, isAngle S> constexpr Angle clamp(Q lhs, R lo, S hi) {
return Angle(std::clamp(lhs.internal(), lo.internal(), hi.internal()));
}
} // namespace units

constexpr Angle rad = Angle(1.0);
constexpr Angle deg = Angle(M_PI / 180);
constexpr Angle rot = Angle(M_TWOPI);
Expand Down
19 changes: 1 addition & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ void initialize() {
std::ratio<0>, std::ratio<0>>(1.0);
a.orientation += 2_rpm2;
2_rpm2 -= a.orientation;
to_cDeg(Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
std::ratio<0>, std::ratio<0>>(5.0) -
a.theta() + 5_cDeg);
Quantity<std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>, std::ratio<1>, std::ratio<0>, std::ratio<0>,
std::ratio<0>>
c = Multiplied<Angle, Time>();
Expand All @@ -32,9 +29,6 @@ void initialize() {
Length z = toLinear<Angle>(y, 2_cm);
static_assert(Angle(5.1) >= Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>,
std::ratio<0>, std::ratio<0>, std::ratio<0>>(5.0));
units::clamp(2_cDeg, a.theta(),
Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
std::ratio<0>, std::ratio<0>>(5.0));
units::max(10_celsius, Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>,
std::ratio<1>, std::ratio<0>, std::ratio<0>>(1.0));
// check Vector3D overloads
Expand All @@ -54,18 +48,7 @@ void initialize() {
void angleTests() {
static_assert(+15_cDeg == 75_stDeg);
static_assert(to_stDeg(-15_cDeg) == to_stDeg(105_stDeg));
static_assert(r2i(to_stDeg(2 * 15_cDeg)) == r2i(to_stDeg(60_stDeg)));
static_assert(r2i(to_stDeg(30_cDeg)) == r2i(to_stDeg(60_stDeg)));
static_assert(r2i(to_stDeg(+0_cDeg)) == r2i(to_stDeg(90_stDeg)));
static_assert(90_stDeg == +0_cDeg);
Angle a = 2_cDeg;
Angle b = 2_cDeg + 2_stDeg;
Angle c = 2_stDeg - 2_cDeg;
Angle d = 2_cDeg + Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>,
std::ratio<0>, std::ratio<0>, std::ratio<0>>(5.0);
Angle e = Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
std::ratio<0>, std::ratio<0>>(5.0) +
2_cDeg;
units::clamp(2_cDeg, 2_stDeg,
Quantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>,
std::ratio<0>, std::ratio<0>>(5.0));
}

0 comments on commit ce0bd31

Please sign in to comment.