refactor: angular system created

This commit is contained in:
Mateusz Pusz
2022-12-25 17:32:29 +01:00
parent daf6d2fb7a
commit 2a3e1f6035
5 changed files with 209 additions and 232 deletions

View File

@@ -1,128 +0,0 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// 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
// IWYU pragma: begin_exports
#include <units/quantity.h>
#include <units/reference.h>
#include <units/symbol_text.h>
// IWYU pragma: end_exports
#include <units/isq/si/prefixes.h>
namespace units {
struct radian : named_unit<radian, "rad"> {};
struct revolution : named_scaled_unit<revolution, "rev", mag<2>() * mag_pi, radian> {};
struct degree : named_scaled_unit<degree, basic_symbol_text{"°", "deg"}, mag<ratio{1, 360}>(), revolution> {};
struct gradian : named_scaled_unit<gradian, basic_symbol_text{"", "grad"}, mag<ratio{1, 400}>(), revolution> {};
template<Unit U = radian>
struct dim_angle : base_dimension<"A", U> {};
template<typename T>
concept Angle = QuantityOfT<T, dim_angle>;
template<UnitOf<dim_angle<>> U, Representation Rep = double>
using angle = quantity<dim_angle<>, U, Rep>;
template<Representation Rep = double>
inline constexpr auto cotes_angle = angle<radian, Rep>(1);
#ifndef UNITS_NO_LITERALS
inline namespace literals {
// rad
constexpr auto operator"" _q_rad(unsigned long long l)
{
gsl_ExpectsAudit(std::in_range<std::int64_t>(l));
return angle<radian, std::int64_t>(static_cast<std::int64_t>(l));
}
constexpr auto operator"" _q_rad(long double l) { return angle<radian, long double>(l); }
// rot
constexpr auto operator"" _q_rev(unsigned long long l)
{
gsl_ExpectsAudit(std::in_range<std::int64_t>(l));
return angle<revolution, std::int64_t>(static_cast<std::int64_t>(l));
}
constexpr auto operator"" _q_rev(long double l) { return angle<revolution, long double>(l); }
// deg
constexpr auto operator"" _q_deg(unsigned long long l)
{
gsl_ExpectsAudit(std::in_range<std::int64_t>(l));
return angle<degree, std::int64_t>(static_cast<std::int64_t>(l));
}
constexpr auto operator"" _q_deg(long double l) { return angle<degree, long double>(l); }
// grad
constexpr auto operator"" _q_grad(unsigned long long l)
{
gsl_ExpectsAudit(std::in_range<std::int64_t>(l));
return angle<gradian, std::int64_t>(static_cast<std::int64_t>(l));
}
constexpr auto operator"" _q_grad(long double l) { return angle<gradian, long double>(l); }
} // namespace literals
#endif // UNITS_NO_LITERALS
#ifndef UNITS_NO_REFERENCES
namespace angle_references {
inline constexpr auto rad = reference<dim_angle<>, radian>{};
inline constexpr auto rev = reference<dim_angle<>, revolution>{};
inline constexpr auto deg = reference<dim_angle<>, degree>{};
inline constexpr auto grad = reference<dim_angle<>, gradian>{};
} // namespace angle_references
namespace references {
using namespace angle_references;
} // namespace references
#endif // UNITS_NO_REFERENCES
} // namespace units
#ifndef UNITS_NO_ALIASES
namespace units::aliases::inline angle {
template<Representation Rep = double>
using rad = units::angle<units::radian, Rep>;
template<Representation Rep = double>
using rev = units::angle<units::revolution, Rep>;
template<Representation Rep = double>
using deg = units::angle<units::degree, Rep>;
template<Representation Rep = double>
using grad = units::angle<units::gradian, Rep>;
} // namespace units::aliases::inline angle
#endif // UNITS_NO_ALIASES

View File

@@ -1,104 +0,0 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// 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
// IWYU pragma: begin_exports
#include <units/generic/angle.h>
#include <units/quantity.h>
#include <units/reference.h>
#include <units/symbol_text.h>
// IWYU pragma: end_exports
#include <units/isq/si/prefixes.h>
namespace units {
struct steradian : named_unit<steradian, "sr"> {};
template<Unit U = steradian>
struct dim_solid_angle : derived_dimension<dim_solid_angle<U>, U, exponent<dim_angle<>, 2>> {};
struct square_degree : derived_scaled_unit<square_degree, dim_solid_angle<>, degree> {};
template<typename T>
concept SolidAngle = QuantityOfT<T, dim_solid_angle>;
template<UnitOf<dim_solid_angle<>> U, Representation Rep = double>
using solid_angle = quantity<dim_solid_angle<>, U, Rep>;
#ifndef UNITS_NO_LITERALS
inline namespace literals {
// sr
constexpr auto operator"" _q_sr(unsigned long long l)
{
gsl_ExpectsAudit(std::in_range<std::int64_t>(l));
return solid_angle<steradian, std::int64_t>(static_cast<std::int64_t>(l));
}
constexpr auto operator"" _q_sr(long double l) { return solid_angle<steradian, long double>(l); }
// deg2
constexpr auto operator"" _q_deg2(unsigned long long l)
{
gsl_ExpectsAudit(std::in_range<std::int64_t>(l));
return solid_angle<square_degree, std::int64_t>(static_cast<std::int64_t>(l));
}
constexpr auto operator"" _q_deg2(long double l) { return solid_angle<square_degree, long double>(l); }
} // namespace literals
#endif // UNITS_NO_LITERALS
#ifndef UNITS_NO_REFERENCES
namespace solid_angle_references {
inline constexpr auto sr = reference<dim_solid_angle<>, steradian>{};
inline constexpr auto deg2 = reference<dim_solid_angle<>, square_degree>{};
} // namespace solid_angle_references
namespace references {
using namespace solid_angle_references;
} // namespace references
#endif // UNITS_NO_REFERENCES
} // namespace units
#ifndef UNITS_NO_ALIASES
namespace units::aliases::inline solid_angle {
template<Representation Rep = double>
using sr = units::solid_angle<units::steradian, Rep>;
template<Representation Rep = double>
using deg2 = units::solid_angle<units::square_degree, Rep>;
} // namespace units::aliases::inline solid_angle
#endif // UNITS_NO_ALIASES

View File

@@ -0,0 +1,28 @@
# The MIT License (MIT)
#
# Copyright (c) 2018 Mateusz Pusz
#
# 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.
cmake_minimum_required(VERSION 3.19)
add_units_module(
angular DEPENDENCIES mp-units::isq
HEADERS include/units/angular/angular.h
)

View File

@@ -0,0 +1,54 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// 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 <units/dimension.h>
#include <units/quantity_spec.h>
#include <units/unit.h>
namespace units::angular {
// clang-format off
inline constexpr struct dim_angle : base_dimension<"A"> {} dim_angle;
QUANTITY_SPEC(angle, dim_angle);
QUANTITY_SPEC(solid_angle, pow<2>(angle));
inline constexpr struct radian : named_unit<"rad", angle> {} radian;
inline constexpr struct revolution : named_unit<"rev", mag<2> * mag_pi * radian> {} revolution;
inline constexpr struct degree : named_unit<basic_symbol_text{"°", "deg"}, mag<ratio{1, 360}> * revolution> {} degree;
inline constexpr struct gradian : named_unit<basic_symbol_text{"", "grad"}, mag<ratio{1, 400}> * revolution> {} gradian;
inline constexpr struct steradian : named_unit<"sr", square<radian>> {} steradian;
// clang-format on
namespace unit_symbols {
inline constexpr auto rad = radian;
inline constexpr auto rev = revolution;
inline constexpr auto deg = degree;
inline constexpr auto grad = gradian;
inline constexpr auto sr = steradian;
inline constexpr auto deg2 = square<degree>;
} // namespace unit_symbols
} // namespace units::angular

View File

@@ -22,6 +22,7 @@
#include "almost_equals.h"
#include <catch2/catch_all.hpp>
#include <units/angular/angular.h>
#include <units/isq/space_and_time.h>
#include <units/math.h>
#include <units/quantity_io.h>
@@ -305,3 +306,129 @@ TEST_CASE("hypot functions", "[hypot]")
REQUIRE(hypot(2. * isq::length[km], 3000. * isq::length[m], 6. * isq::length[km]) == 7. * isq::length[km]);
}
}
TEST_CASE("ISQ trigonometric functions", "[trig][isq]")
{
SECTION("sin")
{
REQUIRE_THAT(isq::sin(isq::angular_measure[deg](0.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(isq::sin(isq::angular_measure[deg](90.)), AlmostEquals(quantity{1.}));
REQUIRE_THAT(isq::sin(isq::angular_measure[deg](180.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(isq::sin(isq::angular_measure[deg](270.)), AlmostEquals(quantity{-1.}));
}
SECTION("cos")
{
REQUIRE_THAT(isq::cos(isq::angular_measure[deg](0.)), AlmostEquals(quantity{1.}));
REQUIRE_THAT(isq::cos(isq::angular_measure[deg](90.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(isq::cos(isq::angular_measure[deg](180.)), AlmostEquals(quantity{-1.}));
REQUIRE_THAT(isq::cos(isq::angular_measure[deg](270.)), AlmostEquals(quantity{0.}));
}
SECTION("tan")
{
REQUIRE_THAT(isq::tan(isq::angular_measure[deg](0.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(isq::tan(isq::angular_measure[deg](45.)), AlmostEquals(quantity{1.}));
REQUIRE_THAT(isq::tan(isq::angular_measure[deg](135.)), AlmostEquals(quantity{-1.}));
REQUIRE_THAT(isq::tan(isq::angular_measure[deg](180.)), AlmostEquals(quantity{0.}));
}
}
TEST_CASE("ISQ inverse trigonometric functions", "[inv trig][isq]")
{
SECTION("asin")
{
REQUIRE_THAT(isq::asin(quantity{-1.}), AlmostEquals(isq::angular_measure[deg](-90.)));
REQUIRE_THAT(isq::asin(quantity{0.}), AlmostEquals(isq::angular_measure[deg](0.)));
REQUIRE_THAT(isq::asin(quantity{1.}), AlmostEquals(isq::angular_measure[deg](90.)));
}
SECTION("acos")
{
REQUIRE_THAT(isq::asin(quantity{-1.}), AlmostEquals(isq::angular_measure[deg](-90.)));
REQUIRE_THAT(isq::asin(quantity{0.}), AlmostEquals(isq::angular_measure[deg](0.)));
REQUIRE_THAT(isq::asin(quantity{1.}), AlmostEquals(isq::angular_measure[deg](90.)));
}
SECTION("atan")
{
REQUIRE_THAT(isq::atan(quantity{-1.}), AlmostEquals(isq::angular_measure[deg](-45.)));
REQUIRE_THAT(isq::atan(quantity{0.}), AlmostEquals(isq::angular_measure[deg](0.)));
REQUIRE_THAT(isq::atan(quantity{1.}), AlmostEquals(isq::angular_measure[deg](45.)));
}
}
TEST_CASE("Angle trigonometric functions", "[trig][angle]")
{
using namespace units::angular;
using namespace units::angular::unit_symbols;
using units::angular::unit_symbols::deg;
SECTION("sin")
{
REQUIRE_THAT(sin(angle[deg](0.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(sin(angle[deg](90.)), AlmostEquals(quantity{1.}));
REQUIRE_THAT(sin(angle[deg](180.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(sin(angle[deg](270.)), AlmostEquals(quantity{-1.}));
REQUIRE_THAT(sin(angle[grad](0.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(sin(angle[grad](100.)), AlmostEquals(quantity{1.}));
REQUIRE_THAT(sin(angle[grad](200.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(sin(angle[grad](300.)), AlmostEquals(quantity{-1.}));
}
SECTION("cos")
{
REQUIRE_THAT(cos(angle[deg](0.)), AlmostEquals(quantity{1.}));
REQUIRE_THAT(cos(angle[deg](90.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(cos(angle[deg](180.)), AlmostEquals(quantity{-1.}));
REQUIRE_THAT(cos(angle[deg](270.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(cos(angle[grad](0.)), AlmostEquals(quantity{1.}));
REQUIRE_THAT(cos(angle[grad](100.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(cos(angle[grad](200.)), AlmostEquals(quantity{-1.}));
REQUIRE_THAT(cos(angle[grad](300.)), AlmostEquals(quantity{0.}));
}
SECTION("tan")
{
REQUIRE_THAT(tan(angle[deg](0.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(tan(angle[deg](45.)), AlmostEquals(quantity{1.}));
REQUIRE_THAT(tan(angle[deg](135.)), AlmostEquals(quantity{-1.}));
REQUIRE_THAT(tan(angle[deg](180.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(tan(angle[grad](0.)), AlmostEquals(quantity{0.}));
REQUIRE_THAT(tan(angle[grad](50.)), AlmostEquals(quantity{1.}));
REQUIRE_THAT(tan(angle[grad](150.)), AlmostEquals(quantity{-1.}));
REQUIRE_THAT(tan(angle[grad](200.)), AlmostEquals(quantity{0.}));
}
}
TEST_CASE("Angle inverse trigonometric functions", "[inv trig][angle]")
{
using namespace units::angular;
using namespace units::angular::unit_symbols;
using units::angular::unit_symbols::deg;
SECTION("asin")
{
REQUIRE_THAT(asin(quantity{-1.}), AlmostEquals(angle[deg](-90.)));
REQUIRE_THAT(asin(quantity{0.}), AlmostEquals(angle[deg](0.)));
REQUIRE_THAT(asin(quantity{1.}), AlmostEquals(angle[deg](90.)));
}
SECTION("acos")
{
REQUIRE_THAT(asin(quantity{-1.}), AlmostEquals(angle[deg](-90.)));
REQUIRE_THAT(asin(quantity{0.}), AlmostEquals(angle[deg](0.)));
REQUIRE_THAT(asin(quantity{1.}), AlmostEquals(angle[deg](90.)));
}
SECTION("atan")
{
REQUIRE_THAT(atan(quantity{-1.}), AlmostEquals(angle[deg](-45.)));
REQUIRE_THAT(atan(quantity{0.}), AlmostEquals(angle[deg](0.)));
REQUIRE_THAT(atan(quantity{1.}), AlmostEquals(angle[deg](45.)));
}
}