diff --git a/src/shared/units/Angle.h b/src/shared/units/Angle.h new file mode 100644 index 0000000000000000000000000000000000000000..4693f12d38a30906dd98e95986f498bc1615b559 --- /dev/null +++ b/src/shared/units/Angle.h @@ -0,0 +1,58 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <ratio> + +#include "Units.h" + +namespace Boardcore +{ +namespace Units +{ +namespace Angle +{ + +template <class Ratio = std::ratio<1>> +class Angle : public Unit<Ratio> +{ + using Unit<Ratio>::Unit; +}; + +using Degree = Angle<>; // Angle in degrees +using Radian = // Angle in radians + Angle<std::ratio<static_cast<std::intmax_t>(3.14159265358979323846 * 1e10), + static_cast<std::intmax_t>(180 * 1e10)>>; + +auto operator""_rad(long double n) { return Radian(static_cast<float>(n)); }; +auto operator""_deg(long double n) { return Degree(static_cast<float>(n)); }; + +template <class ToAngle, class FromAngle> +ToAngle angle_cast(FromAngle const &from) +{ + return ToAngle(from); +} + +} // namespace Angle +} // namespace Units +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/units/Length.h b/src/shared/units/Length.h new file mode 100644 index 0000000000000000000000000000000000000000..4408326e4cd4123b9bb6f333406c0a1ac8541968 --- /dev/null +++ b/src/shared/units/Length.h @@ -0,0 +1,62 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <ratio> + +#include "Units.h" + +namespace Boardcore +{ +namespace Units +{ +namespace Length +{ + +template <class Ratio = std::ratio<1>> +class Length : public Unit<Ratio> +{ + using Unit<Ratio>::Unit; +}; + +template <class ToLength, class FromLength> +ToLength length_cast(FromLength const &from) +{ + return ToLength(from); +} + +using Millimeter = Length<std::milli>; // Length in millimeters +using Centimeter = Length<std::centi>; // Length in centimeters +using Decimeter = Length<std::deci>; // Length in decimeters +using Meter = Length<>; // Length in meters +using Kilometer = Length<std::kilo>; // Length in kilometers + +auto operator""_mm(long double n) { return Millimeter(static_cast<float>(n)); }; +auto operator""_cm(long double n) { return Centimeter(static_cast<float>(n)); }; +auto operator""_dm(long double n) { return Decimeter(static_cast<float>(n)); }; +auto operator""_m(long double n) { return Meter(static_cast<float>(n)); }; +auto operator""_km(long double n) { return Kilometer(static_cast<float>(n)); }; + +} // namespace Length +} // namespace Units +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/units/Pressure.h b/src/shared/units/Pressure.h new file mode 100644 index 0000000000000000000000000000000000000000..452b3454c1d6e942f037955c334b3e8bee2bdcb8 --- /dev/null +++ b/src/shared/units/Pressure.h @@ -0,0 +1,58 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <ratio> + +#include "Units.h" + +namespace Boardcore +{ +namespace Units +{ +namespace Pressure +{ + +template <class Ratio = std::ratio<1>> +class Pressure : public Unit<Ratio> +{ + using Unit<Ratio>::Unit; +}; + +template <class ToPressure, class FromPressure> +ToPressure pressure_cast(FromPressure &from) +{ + return ToPressure(from); +} + +using Pascal = Pressure<>; // Pressure in Pascals +using Bar = Pressure<std::ratio<100000>>; // Pressure in Bars +using Atm = Pressure<std::ratio<101325>>; // Pressure in Atmospheres + +auto operator""_pa(long double n) { return Pascal(static_cast<float>(n)); }; +auto operator""_bar(long double n) { return Bar(static_cast<float>(n)); }; +auto operator""_atm(long double n) { return Atm(static_cast<float>(n)); }; + +} // namespace Pressure +} // namespace Units +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/units/Time.h b/src/shared/units/Time.h new file mode 100644 index 0000000000000000000000000000000000000000..7cd9f5000f9d5af6d0ebf481714128b9c3e3174d --- /dev/null +++ b/src/shared/units/Time.h @@ -0,0 +1,85 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <chrono> +#include <ratio> + +#include "Units.h" + +namespace Boardcore +{ +namespace Units +{ +namespace Time +{ + +template <class Ratio = std::ratio<1>> +class Time : public Unit<Ratio> +{ + using Unit<Ratio>::Unit; + +public: + std::chrono::duration<float> chrono() const + { + return std::chrono::duration<float, Ratio>(this->value()); + } +}; + +template <class ToTime, class FromTime> +ToTime time_cast(FromTime &from) +{ + return ToTime(from); +} + +using Nanosecond = Time<std::ratio<1, 1000000000>>; // Time in nanoseconds +using Microsecond = Time<std::ratio<1, 1000000>>; // Time in microseconds +using Millisecond = Time<std::ratio<1, 1000>>; // Time in milliseconds +using Second = Time<>; // Time in seconds +using Minute = Time<std::ratio<60>>; // Time in minutes +using Hour = Time<std::ratio<3600>>; // Time in hours +using Day = Time<std::ratio<86400>>; // Time in days +using Week = Time<std::ratio<604800>>; // Time in weeks +using Month = Time<std::ratio<262800>>; // Time in months +using Year = Time<std::ratio<31536000>>; // Time in years + +auto operator""_ns(long double n) { return Nanosecond(static_cast<float>(n)); }; +auto operator""_us(long double n) +{ + return Microsecond(static_cast<float>(n)); +}; +auto operator""_ms(long double n) +{ + return Millisecond(static_cast<float>(n)); +}; +auto operator""_s(long double n) { return Second(static_cast<float>(n)); }; +auto operator""_min(long double n) { return Minute(static_cast<float>(n)); }; +auto operator""_h(long double n) { return Hour(static_cast<float>(n)); }; +auto operator""_d(long double n) { return Day(static_cast<float>(n)); }; +auto operator""_w(long double n) { return Week(static_cast<float>(n)); }; +auto operator""_mo(long double n) { return Month(static_cast<float>(n)); }; +auto operator""_y(long double n) { return Year(static_cast<float>(n)); }; + +} // namespace Time +} // namespace Units +} // namespace Boardcore \ No newline at end of file diff --git a/src/shared/units/Units.h b/src/shared/units/Units.h new file mode 100644 index 0000000000000000000000000000000000000000..46aca63c8ec9c82e1d4ea59e1817228a09468408 --- /dev/null +++ b/src/shared/units/Units.h @@ -0,0 +1,161 @@ +/* Copyright (c) 2023 Skyward Experimental Rocketry + * Authors: Davide Basso + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#pragma once + +#include <utils/Debug.h> + +#include <ratio> +#include <typeinfo> + +namespace Boardcore +{ +namespace Units +{ + +template <class Ratio = std::ratio<1>> +// Base class to implement custom measurement units logic. +class Unit +{ +public: + Unit(float val) : _value(val){}; + template <class FromRatio> + constexpr explicit Unit(Unit<FromRatio> const &from) + : _value(from.template value<Ratio>()) + { + } + + // Get the value of the unit in the specified ratio. + template <class TargetRatio = Ratio> + float value() const + { + const auto currentRatio = + static_cast<float>(Ratio::num) / static_cast<float>(Ratio::den); + const auto targetRatio = static_cast<float>(TargetRatio::num) / + static_cast<float>(TargetRatio::den); + + return _value / currentRatio * targetRatio; + } + + template <class TargetRatio = Ratio> + constexpr explicit operator Unit<TargetRatio>() const + { + return Unit<TargetRatio>(value<TargetRatio>()); + } + +private: + float _value; +}; + +// Sum, Subtraction, Multiplication, Division +template <class DerivedUnit> +constexpr auto operator+(const DerivedUnit &lhs, const DerivedUnit &rhs) +{ + return DerivedUnit(lhs.template value() + rhs.template value()); +} + +template <class DerivedUnit> +constexpr auto operator-(const DerivedUnit &lhs, const DerivedUnit &rhs) +{ + return DerivedUnit(lhs.template value() - rhs.template value()); +} + +template <class DerivedUnit> +constexpr auto operator*(const DerivedUnit &lhs, const DerivedUnit &rhs) +{ + return DerivedUnit(lhs.template value() * rhs.template value()); +} + +template <class DerivedUnit> +constexpr auto operator/(const DerivedUnit &lhs, const DerivedUnit &rhs) +{ + return DerivedUnit(lhs.template value() / rhs.template value()); +} + +// Comparison operators +template <class DerivedUnit> +constexpr bool operator==(const DerivedUnit &lhs, const DerivedUnit &rhs) +{ + return lhs.template value() == rhs.template value(); +} + +template <class DerivedUnit> +constexpr bool operator!=(const DerivedUnit &lhs, const DerivedUnit &rhs) +{ + return lhs.template value() != rhs.template value(); +} + +template <class DerivedUnit> +constexpr bool operator<(const DerivedUnit &lhs, const DerivedUnit &rhs) +{ + return lhs.template value() < rhs.template value(); +} + +template <class DerivedUnit> +constexpr bool operator>(const DerivedUnit &lhs, const DerivedUnit &rhs) +{ + return lhs.template value() > rhs.template value(); +} + +template <class DerivedUnit> +constexpr bool operator<=(const DerivedUnit &lhs, const DerivedUnit &rhs) +{ + return lhs.template value() <= rhs.template value(); +} + +template <class DerivedUnit> +constexpr bool operator>=(const DerivedUnit &lhs, const DerivedUnit &rhs) +{ + return lhs.template value() >= rhs.template value(); +} + +// Direct assignment operators +template <class DerivedUnit> +constexpr DerivedUnit &operator+=(DerivedUnit &lhs, const DerivedUnit &rhs) +{ + lhs = lhs + rhs; + return lhs; +} + +template <class DerivedUnit> +constexpr DerivedUnit &operator-=(DerivedUnit &lhs, const DerivedUnit &rhs) +{ + lhs = lhs - rhs; + return lhs; +} + +template <class DerivedUnit> +constexpr DerivedUnit &operator*=(DerivedUnit &lhs, const DerivedUnit &rhs) +{ + lhs = lhs * rhs; + return lhs; +} + +template <class DerivedUnit> +constexpr DerivedUnit &operator/=(DerivedUnit &lhs, const DerivedUnit &rhs) +{ + lhs = lhs / rhs; + return lhs; +} + +} // namespace Units +} // namespace Boardcore