nomlib
Loading...
Searching...
No Matches
math_helpers.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_MATH_HELPERS_HPP
30#define NOMLIB_MATH_HELPERS_HPP
31
32#include <random>
33#include <cmath>
34
35#include "export.hpp"
36#include "nomlib/config.hpp"
37#include "nomlib/math/Point2.hpp"
38#include "nomlib/math/Size2.hpp"
39
40namespace nom {
41
42const double PI = 4.0 * atan ( 1.0 );
43
44namespace priv {
45
46extern std::random_device rd;
47extern std::default_random_engine rd_generator;
48
49} // namespace priv
50
62NOM_EXPORT void init_rand(uint32 seed_seq);
63
69template <typename T>
70NOM_EXPORT inline T uniform_int_rand(T start_range, T end_range)
71{
72 std::uniform_int_distribution<T> distribution(start_range, end_range);
73
74 return distribution(priv::rd_generator);
75}
76
82template <typename T>
83NOM_EXPORT inline T uniform_real_rand(T start_range, T end_range)
84{
85 std::uniform_real_distribution<T> distribution(start_range, end_range);
86
87 return distribution(priv::rd_generator);
88}
89
92const Point2d rotate_points ( float angle, float x, float y, float pivot_x, float pivot_y );
93
102template <typename T>
103inline T round_float(real32 number)
104{
105 real32 ret = number < 0.0f ? ceilf(number - 0.5f) : floorf(number + 0.5f);
106 return NOM_SCAST(T, ret);
107}
108
114template <typename T>
115inline T round_float_down(real32 number)
116{
117 real32 ret = floorf(number);
118 return NOM_SCAST(T, ret);
119}
120
126template <typename T>
127inline T round_float_up(real32 number)
128{
129 real32 ret = ceilf(number);
130 return NOM_SCAST(T, ret);
131}
132
141template <typename T>
142inline T round_double(real64 number)
143{
144 real64 ret = number < 0.0f ? ceil(number - 0.5f) : floor(number + 0.5f);
145 return NOM_SCAST(T, ret);
146}
147
148// TODO(jeff): Rename to truncate_real32
149template <typename T>
150inline T truncate_float(real32 number)
151{
152 T ret = NOM_SCAST(T, number);
153 return(ret);
154}
155
156// FIXME(jeff): We should provide two separate functions here, absolute_real32
157// and absolute_real64. See also: man 3 fabs
158inline real32 absolute_real32(real32 number)
159{
160 real32 result = fabsf(number);
161
162 return result;
163}
164
165inline real64 absolute_real64(real64 number)
166{
167 real64 result = fabs(number);
168
169 return result;
170}
171
172inline int absolute_int(int number)
173{
174 int result = std::abs(number);
175
176 return result;
177}
178
182template <typename T>
183inline T minimum(const T& a, const T& b)
184{
185 T result = (b < a) ? b : a;
186 return result;
187}
188
192template <typename T>
193inline T maximum(const T& a, const T& b)
194{
195 T result = (a < b) ? b : a;
196 return result;
197}
198
200template <typename T>
201inline T clamp_min(const T& a, const T& b)
202{
203 auto result = nom::minimum<T>(a, b);
204 return result;
205}
206
208template <typename T>
209inline T clamp_max(const T& a, const T& b)
210{
211 auto result = nom::maximum<T>(a, b);
212 return result;
213}
214
215// from audio2 -- common.h
216
217// It's pi(ish)...divided by 360.0f!
218static const float PI_OVER_360 = PI / 360.0f;
219
220// Radians to degrees
221static inline double RadToDeg(const double deg)
222{
223 return deg * 180.0 / PI;
224};
225
226// Degrees to radians
227static inline double DegToRad(const double rad)
228{
229 return rad * PI / 180.0;
230};
231
232} // namespace nom
233
234#endif // include guard defined