Conquer Space 0.0.0
A space themed grand strategy game set in the near future, with realistic orbital mechanics, and an emphasis on economics and politics.
units.h
Go to the documentation of this file.
1/* Conquer Space
2 * Copyright (C) 2021-2023 Conquer Space
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <cmath>
20
23 Distance, // Default distance: kilometer
24 Angle, // Default distance: degree
25 Mass, // Default distance: kilogram
26 Volume, // Default distance: m^3
27 Time // Default distance: second
28};
29
31
32typedef double astronomical_unit;
33typedef double light_year;
34typedef double kilometer;
35typedef double solar_mass;
36typedef double degree;
37typedef double radian;
38typedef double meter_cube;
39typedef double kilogram;
40typedef double earth_masses;
41typedef double joule;
42typedef double second;
43
44// Constants
45constexpr double PI = 3.14159265358979323846;
46constexpr double TWOPI = PI * 2;
47constexpr double HALFPI = PI / 2;
48
49constexpr double KmInAu = 149597870.700;
50
51// Gravitional constant in m^3 * kg^-1 * s^-2
52constexpr double G = 6.6743015e-11;
53// Gravitional constant in km^3 * kg^-1 * s^-2
54constexpr double G_km = 6.6743015e-20;
55
56// GM of sun/sun gravitational constant in km^3 * s^-2
57constexpr double SunMu = 1.32712400188e11;
58
59#if __cplusplus == 202302L
60// Then use fmod
61#define floatmod std::fmod
62#else
63inline double constexpr floatmod(double x, double y) { return x - y * (int)(x / y); }
64#endif
65
70inline constexpr radian normalize_radian(const radian& radian) {
71 double x = floatmod(radian, TWOPI);
72 if (x < 0) {
73 x += TWOPI;
74 }
75 return x;
76}
77
78inline constexpr double normalize_radian_coord(const radian& radian) {
79 double r = floatmod(radian + PI, TWOPI);
80 if (r < 0) r += TWOPI;
81 return r - PI;
82}
83
84inline constexpr degree normalize_degree(const degree& radian) {
85 double x = floatmod(radian, 360);
86 if (x < 0) {
87 x += 360;
88 }
89 return x;
90}
91
92// Conversions
93inline constexpr astronomical_unit toAU(kilometer km) { return km / KmInAu; }
94inline constexpr light_year toLightYear(astronomical_unit au) { return au / 63241; }
95inline constexpr astronomical_unit LtyrtoAU(light_year ltyr) { return ltyr * 63241; }
96inline constexpr kilometer toKm(astronomical_unit au) { return au * KmInAu; }
97inline constexpr radian toRadian(degree theta) { return theta * (PI / 180.f); }
98inline constexpr degree toDegree(radian theta) { return theta * (180.f / PI); }
99
100inline constexpr double operator""_deg(const long double deg) { return normalize_radian(toRadian(deg)); }
101inline constexpr double operator""_au(const long double au) { return toKm(au); }
102#ifdef floatmod
103#undef floatmod
104#endif // floatmod
105} // namespace cqsp::common::components::types
Definition: coordinates.cpp:22
constexpr degree toDegree(radian theta)
Definition: units.h:98
constexpr astronomical_unit LtyrtoAU(light_year ltyr)
Definition: units.h:95
double second
Definition: units.h:42
double earth_masses
Definition: units.h:40
UnitType
Definition: units.h:22
@ Mass
Definition: units.h:25
@ Time
Definition: units.h:27
@ Volume
Definition: units.h:26
@ Angle
Definition: units.h:24
Distance
Definition: units.h:30
@ Millimeter
Definition: units.h:30
@ AstronomicalUnit
Definition: units.h:30
@ LightYear
Definition: units.h:30
@ Centimeter
Definition: units.h:30
@ Kilometer
Definition: units.h:30
@ Meter
Definition: units.h:30
constexpr double G
Definition: units.h:52
constexpr double KmInAu
Definition: units.h:49
constexpr double SunMu
Definition: units.h:57
double kilogram
Definition: units.h:39
double kilometer
Definition: units.h:34
constexpr double floatmod(double x, double y)
Definition: units.h:63
double joule
Definition: units.h:41
double radian
Definition: units.h:37
constexpr radian toRadian(degree theta)
Definition: units.h:97
constexpr radian normalize_radian(const radian &radian)
Normalizes a radian to [0, PI*2)
Definition: units.h:70
constexpr double TWOPI
Definition: units.h:46
double light_year
Definition: units.h:33
constexpr kilometer toKm(astronomical_unit au)
Definition: units.h:96
constexpr double PI
Definition: units.h:45
double degree
Definition: units.h:36
double solar_mass
Definition: units.h:35
constexpr astronomical_unit toAU(kilometer km)
Definition: units.h:93
constexpr light_year toLightYear(astronomical_unit au)
Definition: units.h:94
constexpr degree normalize_degree(const degree &radian)
Definition: units.h:84
double meter_cube
Definition: units.h:38
constexpr double G_km
Definition: units.h:54
constexpr double normalize_radian_coord(const radian &radian)
Definition: units.h:78
double astronomical_unit
Definition: units.h:32
constexpr double HALFPI
Definition: units.h:47