Initial commit - test serial

This commit is contained in:
Cole A. Deck
2024-03-24 22:20:00 -05:00
commit a4b1c1b7ed
273 changed files with 43716 additions and 0 deletions

View File

@ -0,0 +1,36 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/QLength.hpp"
#include "okapi/api/units/QTime.hpp"
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(0, 1, -2, 0, QAcceleration)
constexpr QAcceleration mps2 = meter / (second * second);
constexpr QAcceleration G = 9.80665 * mps2;
inline namespace literals {
constexpr QAcceleration operator"" _mps2(long double x) {
return QAcceleration(x);
}
constexpr QAcceleration operator"" _mps2(unsigned long long int x) {
return QAcceleration(static_cast<double>(x));
}
constexpr QAcceleration operator"" _G(long double x) {
return static_cast<double>(x) * G;
}
constexpr QAcceleration operator"" _G(unsigned long long int x) {
return static_cast<double>(x) * G;
}
} // namespace literals
} // namespace okapi

View File

@ -0,0 +1,35 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/RQuantity.hpp"
#include <cmath>
namespace okapi {
QUANTITY_TYPE(0, 0, 0, 1, QAngle)
constexpr QAngle radian(1.0);
constexpr QAngle degree = static_cast<double>(2_pi / 360.0) * radian;
inline namespace literals {
constexpr QAngle operator"" _rad(long double x) {
return QAngle(x);
}
constexpr QAngle operator"" _rad(unsigned long long int x) {
return QAngle(static_cast<double>(x));
}
constexpr QAngle operator"" _deg(long double x) {
return static_cast<double>(x) * degree;
}
constexpr QAngle operator"" _deg(unsigned long long int x) {
return static_cast<double>(x) * degree;
}
} // namespace literals
} // namespace okapi

View File

@ -0,0 +1,16 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(0, 0, -2, 1, QAngularAcceleration)
}

View File

@ -0,0 +1,16 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(0, 0, -3, 1, QAngularJerk)
}

View File

@ -0,0 +1,39 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/QAngle.hpp"
#include "okapi/api/units/QFrequency.hpp"
#include "okapi/api/units/QTime.hpp"
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(0, 0, -1, 1, QAngularSpeed)
constexpr QAngularSpeed radps = radian / second;
constexpr QAngularSpeed rpm = (360 * degree) / minute;
constexpr QAngularSpeed cps = (0.01 * degree) / second; // centidegree per second
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
static QAngularSpeed convertHertzToRadPerSec(QFrequency in) {
return (in.convert(Hz) / 2_pi) * radps;
}
#pragma GCC diagnostic pop
inline namespace literals {
constexpr QAngularSpeed operator"" _rpm(long double x) {
return x * rpm;
}
constexpr QAngularSpeed operator"" _rpm(unsigned long long int x) {
return static_cast<double>(x) * rpm;
}
} // namespace literals
} // namespace okapi

View File

@ -0,0 +1,26 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/QLength.hpp"
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(0, 2, 0, 0, QArea)
constexpr QArea kilometer2 = kilometer * kilometer;
constexpr QArea meter2 = meter * meter;
constexpr QArea decimeter2 = decimeter * decimeter;
constexpr QArea centimeter2 = centimeter * centimeter;
constexpr QArea millimeter2 = millimeter * millimeter;
constexpr QArea inch2 = inch * inch;
constexpr QArea foot2 = foot * foot;
constexpr QArea mile2 = mile * mile;
} // namespace okapi

View File

@ -0,0 +1,43 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/QAcceleration.hpp"
#include "okapi/api/units/QMass.hpp"
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(1, 1, -2, 0, QForce)
constexpr QForce newton = (kg * meter) / (second * second);
constexpr QForce poundforce = pound * G;
constexpr QForce kilopond = kg * G;
inline namespace literals {
constexpr QForce operator"" _n(long double x) {
return QForce(x);
}
constexpr QForce operator"" _n(unsigned long long int x) {
return QForce(static_cast<double>(x));
}
constexpr QForce operator"" _lbf(long double x) {
return static_cast<double>(x) * poundforce;
}
constexpr QForce operator"" _lbf(unsigned long long int x) {
return static_cast<double>(x) * poundforce;
}
constexpr QForce operator"" _kp(long double x) {
return static_cast<double>(x) * kilopond;
}
constexpr QForce operator"" _kp(unsigned long long int x) {
return static_cast<double>(x) * kilopond;
}
} // namespace literals
} // namespace okapi

View File

@ -0,0 +1,27 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(0, 0, -1, 0, QFrequency)
constexpr QFrequency Hz(1.0);
inline namespace literals {
constexpr QFrequency operator"" _Hz(long double x) {
return QFrequency(x);
}
constexpr QFrequency operator"" _Hz(unsigned long long int x) {
return QFrequency(static_cast<long double>(x));
}
} // namespace literals
} // namespace okapi

View File

@ -0,0 +1,18 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/QLength.hpp"
#include "okapi/api/units/QTime.hpp"
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(0, 1, -3, 0, QJerk)
}

View File

@ -0,0 +1,84 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(0, 1, 0, 0, QLength)
constexpr QLength meter(1.0); // SI base unit
constexpr QLength decimeter = meter / 10;
constexpr QLength centimeter = meter / 100;
constexpr QLength millimeter = meter / 1000;
constexpr QLength kilometer = 1000 * meter;
constexpr QLength inch = 2.54 * centimeter;
constexpr QLength foot = 12 * inch;
constexpr QLength yard = 3 * foot;
constexpr QLength mile = 5280 * foot;
constexpr QLength tile = 24 * inch;
inline namespace literals {
constexpr QLength operator"" _mm(long double x) {
return static_cast<double>(x) * millimeter;
}
constexpr QLength operator"" _cm(long double x) {
return static_cast<double>(x) * centimeter;
}
constexpr QLength operator"" _m(long double x) {
return static_cast<double>(x) * meter;
}
constexpr QLength operator"" _km(long double x) {
return static_cast<double>(x) * kilometer;
}
constexpr QLength operator"" _mi(long double x) {
return static_cast<double>(x) * mile;
}
constexpr QLength operator"" _yd(long double x) {
return static_cast<double>(x) * yard;
}
constexpr QLength operator"" _ft(long double x) {
return static_cast<double>(x) * foot;
}
constexpr QLength operator"" _in(long double x) {
return static_cast<double>(x) * inch;
}
constexpr QLength operator"" _tile(long double x) {
return static_cast<double>(x) * tile;
}
constexpr QLength operator"" _mm(unsigned long long int x) {
return static_cast<double>(x) * millimeter;
}
constexpr QLength operator"" _cm(unsigned long long int x) {
return static_cast<double>(x) * centimeter;
}
constexpr QLength operator"" _m(unsigned long long int x) {
return static_cast<double>(x) * meter;
}
constexpr QLength operator"" _km(unsigned long long int x) {
return static_cast<double>(x) * kilometer;
}
constexpr QLength operator"" _mi(unsigned long long int x) {
return static_cast<double>(x) * mile;
}
constexpr QLength operator"" _yd(unsigned long long int x) {
return static_cast<double>(x) * yard;
}
constexpr QLength operator"" _ft(unsigned long long int x) {
return static_cast<double>(x) * foot;
}
constexpr QLength operator"" _in(unsigned long long int x) {
return static_cast<double>(x) * inch;
}
constexpr QLength operator"" _tile(unsigned long long int x) {
return static_cast<double>(x) * tile;
}
} // namespace literals
} // namespace okapi

View File

@ -0,0 +1,62 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(1, 0, 0, 0, QMass)
constexpr QMass kg(1.0); // SI base unit
constexpr QMass gramme = 0.001 * kg;
constexpr QMass tonne = 1000 * kg;
constexpr QMass ounce = 0.028349523125 * kg;
constexpr QMass pound = 16 * ounce;
constexpr QMass stone = 14 * pound;
inline namespace literals {
constexpr QMass operator"" _kg(long double x) {
return QMass(x);
}
constexpr QMass operator"" _g(long double x) {
return static_cast<double>(x) * gramme;
}
constexpr QMass operator"" _t(long double x) {
return static_cast<double>(x) * tonne;
}
constexpr QMass operator"" _oz(long double x) {
return static_cast<double>(x) * ounce;
}
constexpr QMass operator"" _lb(long double x) {
return static_cast<double>(x) * pound;
}
constexpr QMass operator"" _st(long double x) {
return static_cast<double>(x) * stone;
}
constexpr QMass operator"" _kg(unsigned long long int x) {
return QMass(static_cast<double>(x));
}
constexpr QMass operator"" _g(unsigned long long int x) {
return static_cast<double>(x) * gramme;
}
constexpr QMass operator"" _t(unsigned long long int x) {
return static_cast<double>(x) * tonne;
}
constexpr QMass operator"" _oz(unsigned long long int x) {
return static_cast<double>(x) * ounce;
}
constexpr QMass operator"" _lb(unsigned long long int x) {
return static_cast<double>(x) * pound;
}
constexpr QMass operator"" _st(unsigned long long int x) {
return static_cast<double>(x) * stone;
}
} // namespace literals
} // namespace okapi

View File

@ -0,0 +1,44 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/QAcceleration.hpp"
#include "okapi/api/units/QArea.hpp"
#include "okapi/api/units/QMass.hpp"
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(1, -1, -2, 0, QPressure)
constexpr QPressure pascal(1.0);
constexpr QPressure bar = 100000 * pascal;
constexpr QPressure psi = pound * G / inch2;
inline namespace literals {
constexpr QPressure operator"" _Pa(long double x) {
return QPressure(x);
}
constexpr QPressure operator"" _Pa(unsigned long long int x) {
return QPressure(static_cast<double>(x));
}
constexpr QPressure operator"" _bar(long double x) {
return static_cast<double>(x) * bar;
}
constexpr QPressure operator"" _bar(unsigned long long int x) {
return static_cast<double>(x) * bar;
}
constexpr QPressure operator"" _psi(long double x) {
return static_cast<double>(x) * psi;
}
constexpr QPressure operator"" _psi(unsigned long long int x) {
return static_cast<double>(x) * psi;
}
} // namespace literals
} // namespace okapi

View File

@ -0,0 +1,43 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/QLength.hpp"
#include "okapi/api/units/QTime.hpp"
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(0, 1, -1, 0, QSpeed)
constexpr QSpeed mps = meter / second;
constexpr QSpeed miph = mile / hour;
constexpr QSpeed kmph = kilometer / hour;
inline namespace literals {
constexpr QSpeed operator"" _mps(long double x) {
return static_cast<double>(x) * mps;
}
constexpr QSpeed operator"" _miph(long double x) {
return static_cast<double>(x) * mile / hour;
}
constexpr QSpeed operator"" _kmph(long double x) {
return static_cast<double>(x) * kilometer / hour;
}
constexpr QSpeed operator"" _mps(unsigned long long int x) {
return static_cast<double>(x) * mps;
}
constexpr QSpeed operator"" _miph(unsigned long long int x) {
return static_cast<double>(x) * mile / hour;
}
constexpr QSpeed operator"" _kmph(unsigned long long int x) {
return static_cast<double>(x) * kilometer / hour;
}
} // namespace literals
} // namespace okapi

View File

@ -0,0 +1,55 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(0, 0, 1, 0, QTime)
constexpr QTime second(1.0); // SI base unit
constexpr QTime millisecond = second / 1000;
constexpr QTime minute = 60 * second;
constexpr QTime hour = 60 * minute;
constexpr QTime day = 24 * hour;
inline namespace literals {
constexpr QTime operator"" _s(long double x) {
return QTime(x);
}
constexpr QTime operator"" _ms(long double x) {
return static_cast<double>(x) * millisecond;
}
constexpr QTime operator"" _min(long double x) {
return static_cast<double>(x) * minute;
}
constexpr QTime operator"" _h(long double x) {
return static_cast<double>(x) * hour;
}
constexpr QTime operator"" _day(long double x) {
return static_cast<double>(x) * day;
}
constexpr QTime operator"" _s(unsigned long long int x) {
return QTime(static_cast<double>(x));
}
constexpr QTime operator"" _ms(unsigned long long int x) {
return static_cast<double>(x) * millisecond;
}
constexpr QTime operator"" _min(unsigned long long int x) {
return static_cast<double>(x) * minute;
}
constexpr QTime operator"" _h(unsigned long long int x) {
return static_cast<double>(x) * hour;
}
constexpr QTime operator"" _day(unsigned long long int x) {
return static_cast<double>(x) * day;
}
} // namespace literals
} // namespace okapi

View File

@ -0,0 +1,43 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/QForce.hpp"
#include "okapi/api/units/QLength.hpp"
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(1, 2, -2, 0, QTorque)
constexpr QTorque newtonMeter = newton * meter;
constexpr QTorque footPound = 1.355817948 * newtonMeter;
constexpr QTorque inchPound = 0.083333333 * footPound;
inline namespace literals {
constexpr QTorque operator"" _nM(long double x) {
return QTorque(x);
}
constexpr QTorque operator"" _nM(unsigned long long int x) {
return QTorque(static_cast<double>(x));
}
constexpr QTorque operator"" _inLb(long double x) {
return static_cast<double>(x) * inchPound;
}
constexpr QTorque operator"" _inLb(unsigned long long int x) {
return static_cast<double>(x) * inchPound;
}
constexpr QTorque operator"" _ftLb(long double x) {
return static_cast<double>(x) * footPound;
}
constexpr QTorque operator"" _ftLb(unsigned long long int x) {
return static_cast<double>(x) * footPound;
}
} // namespace literals
} // namespace okapi

View File

@ -0,0 +1,28 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "okapi/api/units/QArea.hpp"
#include "okapi/api/units/QLength.hpp"
#include "okapi/api/units/RQuantity.hpp"
namespace okapi {
QUANTITY_TYPE(0, 3, 0, 0, QVolume)
constexpr QVolume kilometer3 = kilometer2 * kilometer;
constexpr QVolume meter3 = meter2 * meter;
constexpr QVolume decimeter3 = decimeter2 * decimeter;
constexpr QVolume centimeter3 = centimeter2 * centimeter;
constexpr QVolume millimeter3 = millimeter2 * millimeter;
constexpr QVolume inch3 = inch2 * inch;
constexpr QVolume foot3 = foot2 * foot;
constexpr QVolume mile3 = mile2 * mile;
constexpr QVolume litre = decimeter3;
} // namespace okapi

View File

@ -0,0 +1,419 @@
/*
* This code is a modified version of Benjamin Jurke's work in 2015. You can read his blog post
* here:
* https://benjaminjurke.com/content/articles/2015/compile-time-numerical-unit-dimension-checking/
*
* 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 http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <cmath>
#include <ratio>
namespace okapi {
template <typename MassDim, typename LengthDim, typename TimeDim, typename AngleDim>
class RQuantity {
public:
explicit constexpr RQuantity() : value(0.0) {
}
explicit constexpr RQuantity(double val) : value(val) {
}
explicit constexpr RQuantity(long double val) : value(static_cast<double>(val)) {
}
// The intrinsic operations for a quantity with a unit is addition and subtraction
constexpr RQuantity const &operator+=(const RQuantity &rhs) {
value += rhs.value;
return *this;
}
constexpr RQuantity const &operator-=(const RQuantity &rhs) {
value -= rhs.value;
return *this;
}
constexpr RQuantity operator-() {
return RQuantity(value * -1);
}
constexpr RQuantity const &operator*=(const double rhs) {
value *= rhs;
return *this;
}
constexpr RQuantity const &operator/=(const double rhs) {
value /= rhs;
return *this;
}
// Returns the value of the quantity in multiples of the specified unit
constexpr double convert(const RQuantity &rhs) const {
return value / rhs.value;
}
// returns the raw value of the quantity (should not be used)
constexpr double getValue() const {
return value;
}
constexpr RQuantity<MassDim, LengthDim, TimeDim, AngleDim> abs() const {
return RQuantity<MassDim, LengthDim, TimeDim, AngleDim>(std::fabs(value));
}
constexpr RQuantity<std::ratio_divide<MassDim, std::ratio<2>>,
std::ratio_divide<LengthDim, std::ratio<2>>,
std::ratio_divide<TimeDim, std::ratio<2>>,
std::ratio_divide<AngleDim, std::ratio<2>>>
sqrt() const {
return RQuantity<std::ratio_divide<MassDim, std::ratio<2>>,
std::ratio_divide<LengthDim, std::ratio<2>>,
std::ratio_divide<TimeDim, std::ratio<2>>,
std::ratio_divide<AngleDim, std::ratio<2>>>(std::sqrt(value));
}
private:
double value;
};
// Predefined (physical unit) quantity types:
// ------------------------------------------
#define QUANTITY_TYPE(_Mdim, _Ldim, _Tdim, _Adim, name) \
typedef RQuantity<std::ratio<_Mdim>, std::ratio<_Ldim>, std::ratio<_Tdim>, std::ratio<_Adim>> \
name;
// Unitless
QUANTITY_TYPE(0, 0, 0, 0, Number)
constexpr Number number(1.0);
// Standard arithmetic operators:
// ------------------------------
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> operator+(const RQuantity<M, L, T, A> &lhs,
const RQuantity<M, L, T, A> &rhs) {
return RQuantity<M, L, T, A>(lhs.getValue() + rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> operator-(const RQuantity<M, L, T, A> &lhs,
const RQuantity<M, L, T, A> &rhs) {
return RQuantity<M, L, T, A>(lhs.getValue() - rhs.getValue());
}
template <typename M1,
typename L1,
typename T1,
typename A1,
typename M2,
typename L2,
typename T2,
typename A2>
constexpr RQuantity<std::ratio_add<M1, M2>,
std::ratio_add<L1, L2>,
std::ratio_add<T1, T2>,
std::ratio_add<A1, A2>>
operator*(const RQuantity<M1, L1, T1, A1> &lhs, const RQuantity<M2, L2, T2, A2> &rhs) {
return RQuantity<std::ratio_add<M1, M2>,
std::ratio_add<L1, L2>,
std::ratio_add<T1, T2>,
std::ratio_add<A1, A2>>(lhs.getValue() * rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> operator*(const double &lhs, const RQuantity<M, L, T, A> &rhs) {
return RQuantity<M, L, T, A>(lhs * rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> operator*(const RQuantity<M, L, T, A> &lhs, const double &rhs) {
return RQuantity<M, L, T, A>(lhs.getValue() * rhs);
}
template <typename M1,
typename L1,
typename T1,
typename A1,
typename M2,
typename L2,
typename T2,
typename A2>
constexpr RQuantity<std::ratio_subtract<M1, M2>,
std::ratio_subtract<L1, L2>,
std::ratio_subtract<T1, T2>,
std::ratio_subtract<A1, A2>>
operator/(const RQuantity<M1, L1, T1, A1> &lhs, const RQuantity<M2, L2, T2, A2> &rhs) {
return RQuantity<std::ratio_subtract<M1, M2>,
std::ratio_subtract<L1, L2>,
std::ratio_subtract<T1, T2>,
std::ratio_subtract<A1, A2>>(lhs.getValue() / rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<std::ratio_subtract<std::ratio<0>, M>,
std::ratio_subtract<std::ratio<0>, L>,
std::ratio_subtract<std::ratio<0>, T>,
std::ratio_subtract<std::ratio<0>, A>>
operator/(const double &x, const RQuantity<M, L, T, A> &rhs) {
return RQuantity<std::ratio_subtract<std::ratio<0>, M>,
std::ratio_subtract<std::ratio<0>, L>,
std::ratio_subtract<std::ratio<0>, T>,
std::ratio_subtract<std::ratio<0>, A>>(x / rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> operator/(const RQuantity<M, L, T, A> &rhs, const double &x) {
return RQuantity<M, L, T, A>(rhs.getValue() / x);
}
// Comparison operators for quantities:
// ------------------------------------
template <typename M, typename L, typename T, typename A>
constexpr bool operator==(const RQuantity<M, L, T, A> &lhs, const RQuantity<M, L, T, A> &rhs) {
return (lhs.getValue() == rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr bool operator!=(const RQuantity<M, L, T, A> &lhs, const RQuantity<M, L, T, A> &rhs) {
return (lhs.getValue() != rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr bool operator<=(const RQuantity<M, L, T, A> &lhs, const RQuantity<M, L, T, A> &rhs) {
return (lhs.getValue() <= rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr bool operator>=(const RQuantity<M, L, T, A> &lhs, const RQuantity<M, L, T, A> &rhs) {
return (lhs.getValue() >= rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr bool operator<(const RQuantity<M, L, T, A> &lhs, const RQuantity<M, L, T, A> &rhs) {
return (lhs.getValue() < rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr bool operator>(const RQuantity<M, L, T, A> &lhs, const RQuantity<M, L, T, A> &rhs) {
return (lhs.getValue() > rhs.getValue());
}
// Common math functions:
// ------------------------------
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> abs(const RQuantity<M, L, T, A> &rhs) {
return RQuantity<M, L, T, A>(std::abs(rhs.getValue()));
}
template <typename R, typename M, typename L, typename T, typename A>
constexpr RQuantity<std::ratio_multiply<M, R>,
std::ratio_multiply<L, R>,
std::ratio_multiply<T, R>,
std::ratio_multiply<A, R>>
pow(const RQuantity<M, L, T, A> &lhs) {
return RQuantity<std::ratio_multiply<M, R>,
std::ratio_multiply<L, R>,
std::ratio_multiply<T, R>,
std::ratio_multiply<A, R>>(std::pow(lhs.getValue(), double(R::num) / R::den));
}
template <int R, typename M, typename L, typename T, typename A>
constexpr RQuantity<std::ratio_multiply<M, std::ratio<R>>,
std::ratio_multiply<L, std::ratio<R>>,
std::ratio_multiply<T, std::ratio<R>>,
std::ratio_multiply<A, std::ratio<R>>>
pow(const RQuantity<M, L, T, A> &lhs) {
return RQuantity<std::ratio_multiply<M, std::ratio<R>>,
std::ratio_multiply<L, std::ratio<R>>,
std::ratio_multiply<T, std::ratio<R>>,
std::ratio_multiply<A, std::ratio<R>>>(std::pow(lhs.getValue(), R));
}
template <int R, typename M, typename L, typename T, typename A>
constexpr RQuantity<std::ratio_divide<M, std::ratio<R>>,
std::ratio_divide<L, std::ratio<R>>,
std::ratio_divide<T, std::ratio<R>>,
std::ratio_divide<A, std::ratio<R>>>
root(const RQuantity<M, L, T, A> &lhs) {
return RQuantity<std::ratio_divide<M, std::ratio<R>>,
std::ratio_divide<L, std::ratio<R>>,
std::ratio_divide<T, std::ratio<R>>,
std::ratio_divide<A, std::ratio<R>>>(std::pow(lhs.getValue(), 1.0 / R));
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<std::ratio_divide<M, std::ratio<2>>,
std::ratio_divide<L, std::ratio<2>>,
std::ratio_divide<T, std::ratio<2>>,
std::ratio_divide<A, std::ratio<2>>>
sqrt(const RQuantity<M, L, T, A> &rhs) {
return RQuantity<std::ratio_divide<M, std::ratio<2>>,
std::ratio_divide<L, std::ratio<2>>,
std::ratio_divide<T, std::ratio<2>>,
std::ratio_divide<A, std::ratio<2>>>(std::sqrt(rhs.getValue()));
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<std::ratio_divide<M, std::ratio<3>>,
std::ratio_divide<L, std::ratio<3>>,
std::ratio_divide<T, std::ratio<3>>,
std::ratio_divide<A, std::ratio<3>>>
cbrt(const RQuantity<M, L, T, A> &rhs) {
return RQuantity<std::ratio_divide<M, std::ratio<3>>,
std::ratio_divide<L, std::ratio<3>>,
std::ratio_divide<T, std::ratio<3>>,
std::ratio_divide<A, std::ratio<3>>>(std::cbrt(rhs.getValue()));
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<std::ratio_multiply<M, std::ratio<2>>,
std::ratio_multiply<L, std::ratio<2>>,
std::ratio_multiply<T, std::ratio<2>>,
std::ratio_multiply<A, std::ratio<2>>>
square(const RQuantity<M, L, T, A> &rhs) {
return RQuantity<std::ratio_multiply<M, std::ratio<2>>,
std::ratio_multiply<L, std::ratio<2>>,
std::ratio_multiply<T, std::ratio<2>>,
std::ratio_multiply<A, std::ratio<2>>>(std::pow(rhs.getValue(), 2));
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<std::ratio_multiply<M, std::ratio<3>>,
std::ratio_multiply<L, std::ratio<3>>,
std::ratio_multiply<T, std::ratio<3>>,
std::ratio_multiply<A, std::ratio<3>>>
cube(const RQuantity<M, L, T, A> &rhs) {
return RQuantity<std::ratio_multiply<M, std::ratio<3>>,
std::ratio_multiply<L, std::ratio<3>>,
std::ratio_multiply<T, std::ratio<3>>,
std::ratio_multiply<A, std::ratio<3>>>(std::pow(rhs.getValue(), 3));
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> hypot(const RQuantity<M, L, T, A> &lhs,
const RQuantity<M, L, T, A> &rhs) {
return RQuantity<M, L, T, A>(std::hypot(lhs.getValue(), rhs.getValue()));
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> mod(const RQuantity<M, L, T, A> &lhs,
const RQuantity<M, L, T, A> &rhs) {
return RQuantity<M, L, T, A>(std::fmod(lhs.getValue(), rhs.getValue()));
}
template <typename M1,
typename L1,
typename T1,
typename A1,
typename M2,
typename L2,
typename T2,
typename A2>
constexpr RQuantity<M1, L1, T1, A1> copysign(const RQuantity<M1, L1, T1, A1> &lhs,
const RQuantity<M2, L2, T2, A2> &rhs) {
return RQuantity<M1, L1, T1, A1>(std::copysign(lhs.getValue(), rhs.getValue()));
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> ceil(const RQuantity<M, L, T, A> &lhs,
const RQuantity<M, L, T, A> &rhs) {
return RQuantity<M, L, T, A>(std::ceil(lhs.getValue() / rhs.getValue()) * rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> floor(const RQuantity<M, L, T, A> &lhs,
const RQuantity<M, L, T, A> &rhs) {
return RQuantity<M, L, T, A>(std::floor(lhs.getValue() / rhs.getValue()) * rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> trunc(const RQuantity<M, L, T, A> &lhs,
const RQuantity<M, L, T, A> &rhs) {
return RQuantity<M, L, T, A>(std::trunc(lhs.getValue() / rhs.getValue()) * rhs.getValue());
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<M, L, T, A> round(const RQuantity<M, L, T, A> &lhs,
const RQuantity<M, L, T, A> &rhs) {
return RQuantity<M, L, T, A>(std::round(lhs.getValue() / rhs.getValue()) * rhs.getValue());
}
// Common trig functions:
// ------------------------------
constexpr Number
sin(const RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> &rhs) {
return Number(std::sin(rhs.getValue()));
}
constexpr Number
cos(const RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> &rhs) {
return Number(std::cos(rhs.getValue()));
}
constexpr Number
tan(const RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> &rhs) {
return Number(std::tan(rhs.getValue()));
}
constexpr RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>
asin(const Number &rhs) {
return RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>(
std::asin(rhs.getValue()));
}
constexpr RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>
acos(const Number &rhs) {
return RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>(
std::acos(rhs.getValue()));
}
constexpr RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>
atan(const Number &rhs) {
return RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>(
std::atan(rhs.getValue()));
}
constexpr Number
sinh(const RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> &rhs) {
return Number(std::sinh(rhs.getValue()));
}
constexpr Number
cosh(const RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> &rhs) {
return Number(std::cosh(rhs.getValue()));
}
constexpr Number
tanh(const RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> &rhs) {
return Number(std::tanh(rhs.getValue()));
}
constexpr RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>
asinh(const Number &rhs) {
return RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>(
std::asinh(rhs.getValue()));
}
constexpr RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>
acosh(const Number &rhs) {
return RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>(
std::acosh(rhs.getValue()));
}
constexpr RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>
atanh(const Number &rhs) {
return RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>(
std::atanh(rhs.getValue()));
}
template <typename M, typename L, typename T, typename A>
constexpr RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>
atan2(const RQuantity<M, L, T, A> &lhs, const RQuantity<M, L, T, A> &rhs) {
return RQuantity<std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>>(
std::atan2(lhs.getValue(), rhs.getValue()));
}
inline namespace literals {
constexpr long double operator"" _pi(long double x) {
return static_cast<double>(x) * 3.1415926535897932384626433832795;
}
constexpr long double operator"" _pi(unsigned long long int x) {
return static_cast<double>(x) * 3.1415926535897932384626433832795;
}
} // namespace literals
} // namespace okapi
// Conversion macro, which utilizes the string literals
#define ConvertTo(_x, _y) (_x).convert(1.0_##_y)

View File

@ -0,0 +1,46 @@
#include "okapi/api/units/QAngle.hpp"
#include "okapi/api/units/QLength.hpp"
#include "okapi/api/units/QSpeed.hpp"
#include <stdexcept>
#include <typeindex>
#include <unordered_map>
#pragma once
namespace okapi {
/**
* Returns a short name for a unit.
* For example: `str(1_ft)` will return "ft", so will `1 * foot` or `0.3048_m`.
* Throws std::domain_error when `q` is a unit not defined in this function.
*
* @param q Your unit. Currently only QLength and QAngle are supported.
* @return The short string suffix for that unit.
*/
template <class QType> std::string getShortUnitName(QType q) {
const std::unordered_map<std::type_index, std::unordered_map<double, const char *>> shortNameMap =
{{typeid(meter),
{
{meter.getValue(), "m"},
{decimeter.getValue(), "dm"},
{centimeter.getValue(), "cm"},
{millimeter.getValue(), "mm"},
{kilometer.getValue(), "km"},
{inch.getValue(), "in"},
{foot.getValue(), "ft"},
{yard.getValue(), "yd"},
{mile.getValue(), "mi"},
{tile.getValue(), "tile"},
}},
{typeid(degree), {{degree.getValue(), "deg"}, {radian.getValue(), "rad"}}}};
try {
return shortNameMap.at(typeid(q)).at(q.getValue());
} catch (const std::out_of_range &e) {
throw std::domain_error(
"You have requested the shortname of an unknown unit somewhere (likely odometry strings). "
"Shortname for provided unit is unspecified. You can override this function to add more "
"names or manually specify the name instead.");
}
}
} // namespace okapi