Line data Source code
1 : #ifndef F49356D0_94E7_4146_8837_E2FA0C87BEBB_HPP
2 : #define F49356D0_94E7_4146_8837_E2FA0C87BEBB_HPP
3 :
4 : /**
5 : * @brief The math utilities here are partly explained in the physics part of the documentation
6 : */
7 :
8 : #include "Types.hpp"
9 : #include "Vector2d.hpp"
10 :
11 : #include <SFML/System/Time.hpp>
12 :
13 : #include <ostream>
14 : #include <random>
15 : #include <unordered_map>
16 : #include <utility>
17 :
18 : /**
19 : * @brief Entry-wise addition of 2 maps, i. e. {1: 1, 2: 3} + {1: 0, 2: 4} = {1: 1, 2: 7}
20 : */
21 : template <typename T1, typename T2, typename T3, typename T4>
22 : std::unordered_map<T1, T2, T3, T4>& operator+=(std::unordered_map<T1, T2, T3, T4>& a,
23 : const std::unordered_map<T1, T2, T3, T4>& b)
24 : {
25 : for (const auto& [key, value] : b)
26 : a[key] += value;
27 :
28 : return a;
29 : }
30 :
31 : /**
32 : * @brief Entry-wise division of 2 maps
33 : */
34 : template <typename T1, typename T2, typename T3, typename T4, typename T5>
35 : std::unordered_map<T1, T2, T3, T5>& operator/=(std::unordered_map<T1, T2, T3, T5>& a, const T4& b)
36 : {
37 : for (const auto& [key, value] : a)
38 : a[key] /= b;
39 :
40 : return a;
41 : }
42 :
43 : template <typename T1, typename T2, typename T3, typename T4, typename T5>
44 : std::unordered_map<T1, T2, T3, T5> operator*(std::unordered_map<T1, T2, T3, T5> a, const T4& b)
45 : {
46 : for (const auto& [key, value] : a)
47 : a[key] *= b;
48 :
49 : return a;
50 : }
51 :
52 : sf::Time operator*(const sf::Time& time, double factor);
53 :
54 : /**
55 : * @brief Prints the x and y coordinates to the given stream
56 : */
57 : std::ostream& operator<<(std::ostream& os, const sf::Vector2d& v);
58 :
59 : /**
60 : * @brief Scalar product of 2 vectors
61 : */
62 : double operator*(const sf::Vector2d& a, const sf::Vector2d& b);
63 :
64 : namespace cell
65 : {
66 : class Disc;
67 : }
68 :
69 : namespace cell::mathutils
70 : {
71 :
72 : /**
73 : * @returns |vec|
74 : */
75 : double abs(const sf::Vector2d& vec);
76 :
77 : sf::Vector2d calculateNormal(const sf::Vector2d& v1, const sf::Vector2d& v2);
78 :
79 : /**
80 : * @brief Returns a number in the given range
81 : */
82 12 : template <typename T> T getRandomNumber(std::type_identity_t<T> low, std::type_identity_t<T> high)
83 : {
84 12 : static thread_local std::mt19937 gen{std::random_device{}()};
85 : if constexpr (std::is_integral_v<T>)
86 : {
87 6 : std::uniform_int_distribution<T> dist(low, high);
88 12 : return dist(gen);
89 : }
90 : else
91 : {
92 6 : std::uniform_real_distribution<T> dist(low, high);
93 12 : return dist(gen);
94 : }
95 : }
96 :
97 : /**
98 : * @brief Calculates a grid of starting positions for discs based on the largest radius of all disc types in the
99 : * settings.
100 : */
101 : std::vector<sf::Vector2d> calculateGrid(double width, double height, double edgeLength);
102 :
103 : bool pointIsInCircle(const sf::Vector2d& point, const sf::Vector2d& M, double R);
104 :
105 : /**
106 : * @param M1 Center point of contained circle
107 : * @param R1 Radius of contained circle
108 : * @param M2 Center point of containing circle
109 : * @param R2 Radius of containing circle
110 : */
111 : bool circleIsFullyContainedByCircle(const sf::Vector2d& M1, double R1, const sf::Vector2d& M2, double R2);
112 :
113 : bool circlesOverlap(const sf::Vector2d& M1, double R1, const sf::Vector2d& M2, double R2);
114 : bool circlesOverlap(const sf::Vector2d& M1, double R1, const sf::Vector2d& M2, double R2, MinOverlap minOverlap);
115 :
116 : bool circlesIntersect(const sf::Vector2d& M1, double R1, const sf::Vector2d& M2, double R2);
117 :
118 : /**
119 : * @return `true` if the moving object at position `pos1` with velocity `velocity` is moving towards the point `point`
120 : */
121 : bool isMovingTowards(const sf::Vector2d& pos1, const sf::Vector2d& velocity, const sf::Vector2d& point);
122 :
123 : double calculateOverlap(const sf::Vector2d& r, double R1, double R2);
124 :
125 : double getAngleBetween(const sf::Vector2d& a, const sf::Vector2d& b);
126 :
127 : } // namespace cell::mathutils
128 :
129 : #endif /* F49356D0_94E7_4146_8837_E2FA0C87BEBB_HPP */
|