quanta 0.1.0
Type-safe units for temperature, frequency, electrical and spatial quantities, modeled after std::chrono
Loading...
Searching...
No Matches
spatial.hpp File Reference

Spatial unit types and arithmetic, modeled after std::chrono. More...

#include <charconv>
#include <cmath>
#include <compare>
#include <concepts>
#include <cstdint>
#include <limits>
#include <quanta/core.hpp>
#include <ratio>
#include <string>

Go to the source code of this file.

Classes

struct  quanta::spatial::is_quantity< T >
 Trait to detect quantity specializations. More...
 
struct  quanta::spatial::is_quantity< quantity< Unit, Rep, Precision > >
 
struct  quanta::spatial::quantity_values< Rep >
 Provides special values for quantity representations. More...
 
struct  quanta::spatial::meter_unit
 Length, measured in meters. More...
 
struct  quanta::spatial::square_meter_unit
 Area, measured in square meters. More...
 
struct  quanta::spatial::cubic_meter_unit
 Volume, measured in cubic meters. More...
 
class  quanta::spatial::quantity< Unit, Rep, Precision >
 A quantity of a spatial unit with a representation and precision. More...
 
struct  quanta::spatial::quantity_suffix< Unit, Precision >
 Trait giving the display suffix for a (unit, precision) pair. More...
 
struct  quanta::spatial::power_form_t
 Tag type selecting the power-form suffix, see power_form. More...
 
struct  std::common_type< quanta::spatial::quantity< Unit, Rep1, Precision1 >, quanta::spatial::quantity< Unit, Rep2, Precision2 > >
 

Namespaces

namespace  quanta
 Type-safe units modeled after std::chrono.
 
namespace  quanta::spatial
 Spatial unit types and utilities.
 
namespace  std
 
namespace  quanta::literals
 
namespace  quanta::literals::spatial_literals
 

Typedefs

template<typename Rep , typename Precision = std::ratio<1>>
using quanta::spatial::length = quantity< meter_unit, Rep, Precision >
 Length quantity with configurable representation and precision.
 
using quanta::spatial::nanometers = length< int64_t, std::nano >
 Length with 1 nm precision.
 
using quanta::spatial::micrometers = length< int64_t, std::micro >
 Length with 1 µm precision.
 
using quanta::spatial::millimeters = length< int64_t, std::milli >
 Length with 1 mm precision.
 
using quanta::spatial::centimeters = length< int64_t, std::centi >
 Length with 1 cm precision.
 
using quanta::spatial::meters = length< int64_t >
 Length with 1 m precision.
 
using quanta::spatial::kilometers = length< int64_t, std::kilo >
 Length with 1 km precision.
 
using quanta::spatial::mils = length< int64_t, std::ratio< 127, 5000000 > >
 Length with 1 mil (thou, 1/1000 in = 25.4 µm) precision.
 
using quanta::spatial::inches = length< int64_t, std::ratio< 127, 5000 > >
 Length with 1 in (25.4 mm) precision.
 
using quanta::spatial::feet = length< int64_t, std::ratio< 381, 1250 > >
 Length with 1 ft (12 in = 0.3048 m) precision.
 
using quanta::spatial::yards = length< int64_t, std::ratio< 1143, 1250 > >
 Length with 1 yd (3 ft = 0.9144 m) precision.
 
using quanta::spatial::miles = length< int64_t, std::ratio< 201168, 125 > >
 Length with 1 mi (5280 ft = 1609.344 m) precision.
 
using quanta::spatial::nautical_miles = length< int64_t, std::ratio< 1852 > >
 Length with 1 nmi (1852 m) precision.
 
template<typename Rep , typename Precision = std::ratio<1>>
using quanta::spatial::area = quantity< square_meter_unit, Rep, Precision >
 Area quantity with configurable representation and precision (a ratio of one m²).
 
using quanta::spatial::square_millimeters = area< int64_t, std::micro >
 Area with 1 mm² precision.
 
using quanta::spatial::square_centimeters = area< int64_t, std::ratio< 1, 10000 > >
 Area with 1 cm² precision.
 
using quanta::spatial::square_meters = area< int64_t >
 Area with 1 m² precision.
 
using quanta::spatial::hectares = area< int64_t, std::ratio< 10000 > >
 Area with 1 ha (1 hm² = 10,000 m²) precision.
 
using quanta::spatial::square_kilometers = area< int64_t, std::mega >
 Area with 1 km² precision.
 
using quanta::spatial::square_inches = area< int64_t, std::ratio< 16129, 25000000 > >
 Area with 1 in² (645.16 mm²) precision.
 
using quanta::spatial::square_feet = area< int64_t, std::ratio< 145161, 1562500 > >
 Area with 1 ft² (144 in² = 0.09290304 m²) precision.
 
using quanta::spatial::square_yards = area< int64_t, std::ratio< 1306449, 1562500 > >
 Area with 1 yd² (9 ft² = 0.83612736 m²) precision.
 
using quanta::spatial::acres = area< int64_t, std::ratio< 316160658, 78125 > >
 Area with 1 acre (43,560 ft² = 4046.8564224 m²) precision.
 
using quanta::spatial::square_miles = area< int64_t, std::ratio< 40468564224, 15625 > >
 Area with 1 mi² (640 acres = 2,589,988.110336 m²) precision.
 
template<typename Rep , typename Precision = std::ratio<1>>
using quanta::spatial::volume = quantity< cubic_meter_unit, Rep, Precision >
 Volume quantity with configurable representation and precision (a ratio of one m³).
 
using quanta::spatial::microliters = volume< int64_t, std::nano >
 Volume with 1 µL (1 mm³) precision.
 
using quanta::spatial::cubic_millimeters = microliters
 Volume with 1 mm³ precision (the same type as microliters).
 
using quanta::spatial::milliliters = volume< int64_t, std::micro >
 Volume with 1 mL (1 cm³) precision.
 
using quanta::spatial::cubic_centimeters = milliliters
 Volume with 1 cm³ precision (the same type as milliliters).
 
using quanta::spatial::liters = volume< int64_t, std::milli >
 Volume with 1 L (1 dm³) precision.
 
using quanta::spatial::cubic_meters = volume< int64_t >
 Volume with 1 m³ precision.
 
using quanta::spatial::cubic_inches = volume< int64_t, std::ratio< 2048383, 125000000000 > >
 Volume with 1 in³ (16.387064 mL) precision.
 
using quanta::spatial::cubic_feet = volume< int64_t, std::ratio< 55306341, 1953125000 > >
 Volume with 1 ft³ (1728 in³ = 28.316846592 L) precision.
 
using quanta::spatial::cubic_yards = volume< int64_t, std::ratio< 1493271207, 1953125000 > >
 Volume with 1 yd³ (27 ft³ = 0.764554857984 m³) precision.
 
using quanta::spatial::us_gallons = volume< int64_t, std::ratio< 473176473, 125000000000 > >
 Volume with 1 US gallon (231 in³ = 3.785411784 L) precision.
 

Functions

template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
requires (!std::is_same_v<typename ToQuantity::unit, Unit>) || detail::_quantity_cast_requirements<quantity<Unit, Rep, Precision>, ToQuantity>
constexpr ToQuantity quanta::spatial::quantity_cast (const quantity< Unit, Rep, Precision > &q)
 Converts a quantity to a different precision or representation.
 
template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity quanta::spatial::floor (const quantity< Unit, Rep, Precision > &q)
 Converts a quantity to the target type, rounding toward negative infinity.
 
template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity quanta::spatial::ceil (const quantity< Unit, Rep, Precision > &q)
 Converts a quantity to the target type, rounding toward positive infinity.
 
template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity quanta::spatial::trunc (const quantity< Unit, Rep, Precision > &q)
 Converts a quantity to the target type, rounding toward zero.
 
template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity quanta::spatial::round (const quantity< Unit, Rep, Precision > &q)
 Converts a quantity to the target type, rounding to nearest (ties to even).
 
template<typename Unit , typename Rep , typename Precision >
constexpr quantity< Unit, Rep, Precision > quanta::spatial::abs (const quantity< Unit, Rep, Precision > &q)
 Returns the absolute value of a quantity.
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto quanta::spatial::operator+ (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< quantity< Unit, Rep1, Precision1 >, quantity< Unit, Rep2, Precision2 > >
 Returns the sum of two quantities of the same unit.
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto quanta::spatial::operator- (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< quantity< Unit, Rep1, Precision1 >, quantity< Unit, Rep2, Precision2 > >
 Returns the difference of two quantities of the same unit.
 
template<typename Unit , typename Rep1 , typename Precision , typename Rep2 >
requires not_quantity<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>>
constexpr auto quanta::spatial::operator* (const quantity< Unit, Rep1, Precision > &q, const Rep2 &r) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision >
 Multiplies a quantity by a scalar.
 
template<typename Unit , typename Rep1 , typename Rep2 , typename Precision >
requires not_quantity<Rep1> && std::convertible_to<const Rep1&, std::common_type_t<Rep1, Rep2>>
constexpr auto quanta::spatial::operator* (const Rep1 &r, const quantity< Unit, Rep2, Precision > &q) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision >
 Multiplies a scalar by a quantity.
 
template<typename Unit , typename Rep1 , typename Precision , typename Rep2 >
requires not_quantity<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>>
constexpr auto quanta::spatial::operator/ (const quantity< Unit, Rep1, Precision > &q, const Rep2 &s) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision >
 Divides a quantity by a scalar.
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto quanta::spatial::operator/ (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< Rep1, Rep2 >
 Divides two quantities of the same unit, returning a scalar.
 
template<typename Unit , typename Rep1 , typename Precision , typename Rep2 >
requires not_quantity<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>> && (!treat_as_inexact_v<Rep1> && !treat_as_inexact_v<Rep2>)
constexpr auto quanta::spatial::operator% (const quantity< Unit, Rep1, Precision > &q, const Rep2 &s) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision >
 Returns the remainder of dividing a quantity by a scalar.
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
requires (!treat_as_inexact_v<Rep1> && !treat_as_inexact_v<Rep2>)
constexpr auto quanta::spatial::operator% (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< quantity< Unit, Rep1, Precision1 >, quantity< Unit, Rep2, Precision2 > >
 Returns the remainder of dividing two quantities of the same unit.
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr bool quanta::spatial::operator== (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs)
 
template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
requires std::three_way_comparable<std::common_type_t<Rep1, Rep2>>
constexpr auto quanta::spatial::operator<=> (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs)
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto quanta::spatial::operator* (const quantity< meter_unit, R1, P1 > &lhs, const quantity< meter_unit, R2, P2 > &rhs)
 length x length = area.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto quanta::spatial::operator* (const quantity< meter_unit, R1, P1 > &lhs, const quantity< square_meter_unit, R2, P2 > &rhs)
 length x area = volume.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto quanta::spatial::operator* (const quantity< square_meter_unit, R1, P1 > &lhs, const quantity< meter_unit, R2, P2 > &rhs)
 area x length = volume.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto quanta::spatial::operator/ (const quantity< square_meter_unit, R1, P1 > &lhs, const quantity< meter_unit, R2, P2 > &rhs)
 area / length = length.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto quanta::spatial::operator/ (const quantity< cubic_meter_unit, R1, P1 > &lhs, const quantity< meter_unit, R2, P2 > &rhs)
 volume / length = area.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto quanta::spatial::operator/ (const quantity< cubic_meter_unit, R1, P1 > &lhs, const quantity< square_meter_unit, R2, P2 > &rhs)
 volume / area = length.
 
template<typename Unit , typename Rep , typename Precision >
requires _has_suffix<Unit, typename Precision::type>
std::string quanta::spatial::to_string (const quantity< Unit, Rep, Precision > &q)
 Converts a quantity to a string with its unit suffix.
 
template<typename Unit , typename Rep , typename Precision >
requires _has_suffix<Unit, typename Precision::type>
std::string quanta::spatial::to_string (const quantity< Unit, Rep, Precision > &q, power_form_t)
 Converts a quantity to a string with its power-form unit suffix.
 
template<typename Unit , typename Rep , typename Precision >
requires _has_suffix<Unit, typename Precision::type>
std::string quanta::spatial::to_string (const quantity< Unit, Rep, Precision > &q, base_unit_t)
 Converts a quantity to a string in its base unit (m, m² or m³).
 
template<typename Unit , typename Rep , typename Precision >
requires _has_suffix<Unit, typename Precision::type>
std::string quanta::spatial::to_string (const quantity< Unit, Rep, Precision > &q, scaled_t)
 Converts a quantity to a string in a unit scaled to its magnitude.
 
template<char... Digits>
constexpr quanta::spatial::nanometers quanta::literals::spatial_literals::operator""_nm ()
 Literal for nanometers (e.g., 850_nm).
 
template<char... Digits>
constexpr quanta::spatial::micrometers quanta::literals::spatial_literals::operator""_um ()
 Literal for micrometers (e.g., 50_um).
 
template<char... Digits>
constexpr quanta::spatial::micrometers quanta::literals::spatial_literals::operator""_µm ()
 Literal for micrometers (e.g., 50_µm).
 
template<char... Digits>
constexpr quanta::spatial::millimeters quanta::literals::spatial_literals::operator""_mm ()
 Literal for millimeters (e.g., 250_mm).
 
template<char... Digits>
constexpr quanta::spatial::centimeters quanta::literals::spatial_literals::operator""_cm ()
 Literal for centimeters (e.g., 30_cm).
 
template<char... Digits>
constexpr quanta::spatial::meters quanta::literals::spatial_literals::operator""_m ()
 Literal for meters (e.g., 100_m).
 
template<char... Digits>
constexpr quanta::spatial::kilometers quanta::literals::spatial_literals::operator""_km ()
 Literal for kilometers (e.g., 5_km).
 
template<char... Digits>
constexpr quanta::spatial::mils quanta::literals::spatial_literals::operator""_mil ()
 Literal for mils (e.g., 8_mil).
 
template<char... Digits>
constexpr quanta::spatial::inches quanta::literals::spatial_literals::operator""_in ()
 Literal for inches (e.g., 12_in).
 
template<char... Digits>
constexpr quanta::spatial::feet quanta::literals::spatial_literals::operator""_ft ()
 Literal for feet (e.g., 6_ft).
 
template<char... Digits>
constexpr quanta::spatial::yards quanta::literals::spatial_literals::operator""_yd ()
 Literal for yards (e.g., 100_yd).
 
template<char... Digits>
constexpr quanta::spatial::miles quanta::literals::spatial_literals::operator""_mi ()
 Literal for miles (e.g., 26_mi).
 
template<char... Digits>
constexpr quanta::spatial::nautical_miles quanta::literals::spatial_literals::operator""_nmi ()
 Literal for nautical miles (e.g., 12_nmi).
 
template<char... Digits>
constexpr quanta::spatial::square_millimeters quanta::literals::spatial_literals::operator""_mm2 ()
 Literal for square millimeters (e.g., 50_mm2).
 
template<char... Digits>
constexpr quanta::spatial::square_centimeters quanta::literals::spatial_literals::operator""_cm2 ()
 Literal for square centimeters (e.g., 100_cm2).
 
template<char... Digits>
constexpr quanta::spatial::square_meters quanta::literals::spatial_literals::operator""_m2 ()
 Literal for square meters (e.g., 12_m2).
 
template<char... Digits>
constexpr quanta::spatial::hectares quanta::literals::spatial_literals::operator""_ha ()
 Literal for hectares (e.g., 3_ha).
 
template<char... Digits>
constexpr quanta::spatial::square_kilometers quanta::literals::spatial_literals::operator""_km2 ()
 Literal for square kilometers (e.g., 5_km2).
 
template<char... Digits>
constexpr quanta::spatial::square_inches quanta::literals::spatial_literals::operator""_in2 ()
 Literal for square inches (e.g., 144_in2).
 
template<char... Digits>
constexpr quanta::spatial::square_feet quanta::literals::spatial_literals::operator""_ft2 ()
 Literal for square feet (e.g., 120_ft2).
 
template<char... Digits>
constexpr quanta::spatial::square_yards quanta::literals::spatial_literals::operator""_yd2 ()
 Literal for square yards (e.g., 9_yd2).
 
template<char... Digits>
constexpr quanta::spatial::acres quanta::literals::spatial_literals::operator""_ac ()
 Literal for acres (e.g., 40_ac).
 
template<char... Digits>
constexpr quanta::spatial::square_miles quanta::literals::spatial_literals::operator""_mi2 ()
 Literal for square miles (e.g., 2_mi2).
 
template<char... Digits>
constexpr quanta::spatial::microliters quanta::literals::spatial_literals::operator""_uL ()
 Literal for microliters (e.g., 50_uL).
 
template<char... Digits>
constexpr quanta::spatial::microliters quanta::literals::spatial_literals::operator""_µL ()
 Literal for microliters (e.g., 50_µL).
 
template<char... Digits>
constexpr quanta::spatial::milliliters quanta::literals::spatial_literals::operator""_mL ()
 Literal for milliliters (e.g., 250_mL).
 
template<char... Digits>
constexpr quanta::spatial::liters quanta::literals::spatial_literals::operator""_L ()
 Literal for liters (e.g., 2_L).
 
template<char... Digits>
constexpr quanta::spatial::cubic_meters quanta::literals::spatial_literals::operator""_m3 ()
 Literal for cubic meters (e.g., 3_m3).
 
template<char... Digits>
constexpr quanta::spatial::cubic_millimeters quanta::literals::spatial_literals::operator""_mm3 ()
 Literal for cubic millimeters, i.e.
 
template<char... Digits>
constexpr quanta::spatial::cubic_centimeters quanta::literals::spatial_literals::operator""_cm3 ()
 Literal for cubic centimeters, i.e.
 
template<char... Digits>
constexpr quanta::spatial::cubic_inches quanta::literals::spatial_literals::operator""_in3 ()
 Literal for cubic inches (e.g., 231_in3).
 
template<char... Digits>
constexpr quanta::spatial::cubic_feet quanta::literals::spatial_literals::operator""_ft3 ()
 Literal for cubic feet (e.g., 10_ft3).
 
template<char... Digits>
constexpr quanta::spatial::cubic_yards quanta::literals::spatial_literals::operator""_yd3 ()
 Literal for cubic yards (e.g., 3_yd3).
 
template<char... Digits>
constexpr quanta::spatial::us_gallons quanta::literals::spatial_literals::operator""_gal ()
 Literal for US gallons (e.g., 5_gal).
 

Variables

template<typename T >
constexpr bool quanta::spatial::is_quantity_v = is_quantity<T>::value
 
constexpr power_form_t quanta::spatial::power_form {}
 Selects the power-form suffix in to_string().
 

Detailed Description

Spatial unit types and arithmetic, modeled after std::chrono.

Definition in file spatial.hpp.