Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
numeric.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "constants.h"
8#include "irrlichttypes.h"
9#include "irr_v2d.h"
10#include "irr_v3d.h"
11#include "irr_aabb3d.h"
12#include "SColor.h"
13#include <matrix4.h>
14#include <cmath>
15#include <algorithm>
16
17// Like std::clamp but allows mismatched types
18template <typename T, typename T2, typename T3>
19[[nodiscard]]
20inline constexpr T rangelim(const T &d, const T2 &min, const T3 &max)
21{
22 if (d < (T)min)
23 return (T)min;
24 if (d > (T)max)
25 return (T)max;
26 return d;
27}
28
29// Maximum radius of a block. The magic number is
30// sqrt(3.0) / 2.0 in literal form.
31static constexpr const f32 BLOCK_MAX_RADIUS = 0.866025403784f * MAP_BLOCKSIZE * BS;
32
33inline s16 getContainerPos(s16 p, s16 d)
34{
35 return (p >= 0 ? p : p - d + 1) / d;
36}
37
39{
40 return v2s16(
41 getContainerPos(p.X, d),
42 getContainerPos(p.Y, d)
43 );
44}
45
47{
48 return v3s16(
49 getContainerPos(p.X, d),
50 getContainerPos(p.Y, d),
51 getContainerPos(p.Z, d)
52 );
53}
54
56{
57 return v2s16(
58 getContainerPos(p.X, d.X),
59 getContainerPos(p.Y, d.Y)
60 );
61}
62
64{
65 return v3s16(
66 getContainerPos(p.X, d.X),
67 getContainerPos(p.Y, d.Y),
68 getContainerPos(p.Z, d.Z)
69 );
70}
71
72inline void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
73{
74 container = (p >= 0 ? p : p - d + 1) / d;
75 offset = p & (d - 1);
76}
77
78inline void getContainerPosWithOffset(const v2s16 &p, s16 d, v2s16 &container, v2s16 &offset)
79{
80 getContainerPosWithOffset(p.X, d, container.X, offset.X);
81 getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
82}
83
84inline void getContainerPosWithOffset(const v3s16 &p, s16 d, v3s16 &container, v3s16 &offset)
85{
86 getContainerPosWithOffset(p.X, d, container.X, offset.X);
87 getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
88 getContainerPosWithOffset(p.Z, d, container.Z, offset.Z);
89}
90
91inline bool isInArea(v3s16 p, s16 d)
92{
93 return (
94 p.X >= 0 && p.X < d &&
95 p.Y >= 0 && p.Y < d &&
96 p.Z >= 0 && p.Z < d
97 );
98}
99
100inline bool isInArea(v2s16 p, s16 d)
101{
102 return (
103 p.X >= 0 && p.X < d &&
104 p.Y >= 0 && p.Y < d
105 );
106}
107
108inline bool isInArea(v3s16 p, v3s16 d)
109{
110 return (
111 p.X >= 0 && p.X < d.X &&
112 p.Y >= 0 && p.Y < d.Y &&
113 p.Z >= 0 && p.Z < d.Z
114 );
115}
116
117template <typename T>
118inline void sortBoxVerticies(core::vector3d<T> &p1, core::vector3d<T> &p2)
119{
120 if (p1.X > p2.X)
121 std::swap(p1.X, p2.X);
122 if (p1.Y > p2.Y)
123 std::swap(p1.Y, p2.Y);
124 if (p1.Z > p2.Z)
125 std::swap(p1.Z, p2.Z);
126}
127
128template <typename T>
129inline constexpr core::vector2d<T> componentwise_min(const core::vector2d<T> &a,
130 const core::vector2d<T> &b)
131{
132 return {std::min(a.X, b.X), std::min(a.Y, b.Y)};
133}
134
135template <typename T>
136inline constexpr core::vector2d<T> componentwise_max(const core::vector2d<T> &a,
137 const core::vector2d<T> &b)
138{
139 return {std::max(a.X, b.X), std::max(a.Y, b.Y)};
140}
141
142template <typename T>
143inline constexpr core::vector3d<T> componentwise_min(const core::vector3d<T> &a,
144 const core::vector3d<T> &b)
145{
146 return {std::min(a.X, b.X), std::min(a.Y, b.Y), std::min(a.Z, b.Z)};
147}
148
149template <typename T>
150inline constexpr core::vector3d<T> componentwise_max(const core::vector3d<T> &a,
151 const core::vector3d<T> &b)
152{
153 return {std::max(a.X, b.X), std::max(a.Y, b.Y), std::max(a.Z, b.Z)};
154}
155
157struct MeshGrid {
159
160 u32 getCellVolume() const { return cell_size * cell_size * cell_size; }
161
163 s16 getCellPos(s16 p) const
164 {
165 return (p - (p < 0) * (cell_size - 1)) / cell_size;
166 }
167
169 v3s16 getCellPos(v3s16 block_pos) const
170 {
171 return v3s16(getCellPos(block_pos.X), getCellPos(block_pos.Y), getCellPos(block_pos.Z));
172 }
173
175 s16 getMeshPos(s16 p) const
176 {
177 return getCellPos(p) * cell_size;
178 }
179
182 {
183 return v3s16(getMeshPos(p.X), getMeshPos(p.Y), getMeshPos(p.Z));
184 }
185
187 bool isMeshPos(v3s16 &p) const
188 {
189 return p.X % cell_size == 0
190 && p.Y % cell_size == 0
191 && p.Z % cell_size == 0;
192 }
193
196 u16 getOffsetIndex(v3s16 offset) const
197 {
198 return (offset.Z * cell_size + offset.Y) * cell_size + offset.X;
199 }
200};
201
209[[nodiscard]]
210inline float modulo360f(float f)
211{
212 return fmodf(f, 360.0f);
213}
214
215
218[[nodiscard]]
219inline float wrapDegrees_0_360(float f)
220{
221 float value = modulo360f(f);
222 return value < 0 ? value + 360 : value;
223}
224
225
228[[nodiscard]]
230{
231 v3f value_v3f;
232 value_v3f.X = modulo360f(v.X);
233 value_v3f.Y = modulo360f(v.Y);
234 value_v3f.Z = modulo360f(v.Z);
235
236 // Now that values are wrapped, use to get values for certain ranges
237 value_v3f.X = value_v3f.X < 0 ? value_v3f.X + 360 : value_v3f.X;
238 value_v3f.Y = value_v3f.Y < 0 ? value_v3f.Y + 360 : value_v3f.Y;
239 value_v3f.Z = value_v3f.Z < 0 ? value_v3f.Z + 360 : value_v3f.Z;
240 return value_v3f;
241}
242
243
246[[nodiscard]]
247inline float wrapDegrees_180(float f)
248{
249 float value = modulo360f(f + 180);
250 if (value < 0)
251 value += 360;
252 return value - 180;
253}
254
255/*
256 Pseudo-random (VC++ rand() sucks)
257*/
258#define MYRAND_RANGE 0xffffffff
259u32 myrand();
260void mysrand(u64 seed);
261void myrand_bytes(void *out, size_t len);
262int myrand_range(int min, int max);
263float myrand_range(float min, float max);
264float myrand_float();
265
266// Implements a C++11 UniformRandomBitGenerator using the above functions
268 typedef u32 result_type;
269 static constexpr result_type min() { return 0; }
270 static constexpr result_type max() { return MYRAND_RANGE; }
272 return myrand();
273 }
274};
275
276/*
277 Miscellaneous functions
278*/
279
280inline u32 get_bits(u32 x, u32 pos, u32 len)
281{
282 u32 mask = (1 << len) - 1;
283 return (x >> pos) & mask;
284}
285
286inline void set_bits(u32 *x, u32 pos, u32 len, u32 val)
287{
288 u32 mask = (1 << len) - 1;
289 *x &= ~(mask << pos);
290 *x |= (val & mask) << pos;
291}
292
293inline u32 calc_parity(u32 v)
294{
295 v ^= v >> 16;
296 v ^= v >> 8;
297 v ^= v >> 4;
298 v &= 0xf;
299 return (0x6996 >> v) & 1;
300}
301
309[[nodiscard]]
310u64 murmur_hash_64_ua(const void *key, size_t len, unsigned int seed);
311
319bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
320 f32 camera_fov, f32 range, f32 *distance_ptr=nullptr);
321
322s16 adjustDist(s16 dist, float zoom_fov);
323
324/*
325 Returns nearest 32-bit integer for given floating point number.
326 <cmath> and <math.h> in VC++ don't provide round().
327*/
328[[nodiscard]]
329inline s32 myround(f32 f)
330{
331 return (s32)(f < 0.f ? (f - 0.5f) : (f + 0.5f));
332}
333
334template <typename T>
335[[nodiscard]]
336inline constexpr T sqr(T f)
337{
338 return f * f;
339}
340
341/*
342 Returns integer position of node in given floating point position
343*/
344[[nodiscard]]
345inline v3s16 floatToInt(v3f p, f32 d)
346{
347 return v3s16(
348 (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
349 (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
350 (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
351}
352
353/*
354 Returns integer position of node in given double precision position
355 */
356[[nodiscard]]
357inline v3s16 doubleToInt(v3d p, double d)
358{
359 return v3s16(
360 (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
361 (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
362 (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
363}
364
365/*
366 Returns floating point position of node in given integer position
367*/
368[[nodiscard]]
369inline v3f intToFloat(v3s16 p, f32 d)
370{
371 return v3f::from(p) * d;
372}
373
374// Returns box of a node as in-world box. Usually d=BS
375[[nodiscard]]
376inline aabb3f getNodeBox(v3s16 p, float d)
377{
378 return aabb3f(
379 v3f::from(p) * d - 0.5f * d,
380 v3f::from(p) * d + 0.5f * d
381 );
382}
383
384
386{
387public:
388 IntervalLimiter() = default;
389
395 [[nodiscard]]
396 bool step(float dtime, float wanted_interval)
397 {
398 m_accumulator += dtime;
399 if (m_accumulator < wanted_interval)
400 return false;
401 m_accumulator -= wanted_interval;
402 return true;
403 }
404
405private:
406 float m_accumulator = 0.0f;
407};
408
409
410constexpr inline bool is_power_of_two(u32 n)
411{
412 return n != 0 && (n & (n - 1)) == 0;
413}
414
415// Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
416// Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
417constexpr inline u32 npot2(u32 orig)
418{
419 orig--;
420 orig |= orig >> 1;
421 orig |= orig >> 2;
422 orig |= orig >> 4;
423 orig |= orig >> 8;
424 orig |= orig >> 16;
425 return orig + 1;
426}
427
428// Distance between two values in a wrapped (circular) system
429template<typename T>
430inline unsigned wrappedDifference(T a, T b, const T maximum)
431{
432 if (a > b)
433 std::swap(a, b);
434 // now b >= a
435 unsigned s = b - a, l = static_cast<unsigned>(maximum - b) + a + 1;
436 return std::min(s, l);
437}
438
439// Gradual steps towards the target value in a wrapped (circular) system
440// using the shorter of both ways
441template<typename T>
442inline void wrappedApproachShortest(T &current, const T target, const T stepsize,
443 const T maximum)
444{
445 T delta = target - current;
446 if (delta < 0)
447 delta += maximum;
448
449 if (delta > stepsize && maximum - delta > stepsize) {
450 current += (delta < maximum / 2) ? stepsize : -stepsize;
451 if (current >= maximum)
452 current -= maximum;
453 } else {
454 current = target;
455 }
456}
457
460void setPitchYawRollRad(core::matrix4 &m, v3f rot);
461
462inline void setPitchYawRoll(core::matrix4 &m, v3f rot)
463{
464 setPitchYawRollRad(m, rot * core::DEGTORAD);
465}
466
468v3f getPitchYawRollRad(const core::matrix4 &m);
469
470inline v3f getPitchYawRoll(const core::matrix4 &m)
471{
472 return getPitchYawRollRad(m) * core::RADTODEG;
473}
474
475// Muliply the RGB value of a color linearly, and clamp to black/white
476inline video::SColor multiplyColorValue(const video::SColor &color, float mod)
477{
478 return video::SColor(color.getAlpha(),
479 core::clamp<u32>(color.getRed() * mod, 0, 255),
480 core::clamp<u32>(color.getGreen() * mod, 0, 255),
481 core::clamp<u32>(color.getBlue() * mod, 0, 255));
482}
483
484template <typename T>
485constexpr inline T numericAbsolute(T v)
486{
487 return v < 0 ? T(-v) : v;
488}
489
490template <typename T>
491constexpr inline T numericSign(T v)
492{
493 return T(v < 0 ? -1 : (v == 0 ? 0 : 1));
494}
495
496template <typename T>
497inline constexpr core::vector3d<T> vecAbsolute(const core::vector3d<T> &v)
498{
499 return {
500 numericAbsolute(v.X),
501 numericAbsolute(v.Y),
502 numericAbsolute(v.Z)
503 };
504}
505
506template <typename T>
507inline constexpr core::vector3d<T> vecSign(const core::vector3d<T> &v)
508{
509 return {
510 numericSign(v.X),
511 numericSign(v.Y),
512 numericSign(v.Z)
513 };
514}
Definition numeric.h:386
float m_accumulator
Definition numeric.h:406
IntervalLimiter()=default
bool step(float dtime, float wanted_interval)
Definition numeric.h:396
#define BS
Definition constants.h:61
#define MAP_BLOCKSIZE
Definition constants.h:64
core::aabbox3d< f32 > aabb3f
Definition irr_aabb3d.h:11
core::vector2d< s16 > v2s16
Definition irr_v2d.h:12
core::vector3d< double > v3d
Definition irr_v3d.h:12
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
core::vector3df v3f
Definition irr_v3d.h:11
constexpr core::vector3d< T > vecAbsolute(const core::vector3d< T > &v)
Definition numeric.h:497
s16 getContainerPos(s16 p, s16 d)
Definition numeric.h:33
constexpr core::vector2d< T > componentwise_min(const core::vector2d< T > &a, const core::vector2d< T > &b)
Definition numeric.h:129
v3f getPitchYawRollRad(const core::matrix4 &m)
Definition numeric.cpp:181
constexpr T sqr(T f)
Definition numeric.h:336
bool isInArea(v3s16 p, s16 d)
Definition numeric.h:91
void wrappedApproachShortest(T &current, const T target, const T stepsize, const T maximum)
Definition numeric.h:442
v3s16 floatToInt(v3f p, f32 d)
Definition numeric.h:345
void mysrand(u64 seed)
Definition numeric.cpp:22
float wrapDegrees_0_360(float f)
Returns f wrapped to the range [0, 360].
Definition numeric.h:219
u64 murmur_hash_64_ua(const void *key, size_t len, unsigned int seed)
Calculate MurmurHash64A hash for an arbitrary block of data.
Definition numeric.cpp:49
constexpr core::vector3d< T > vecSign(const core::vector3d< T > &v)
Definition numeric.h:507
constexpr T numericAbsolute(T v)
Definition numeric.h:485
void setPitchYawRoll(core::matrix4 &m, v3f rot)
Definition numeric.h:462
void sortBoxVerticies(core::vector3d< T > &p1, core::vector3d< T > &p2)
Definition numeric.h:118
v3f getPitchYawRoll(const core::matrix4 &m)
Definition numeric.h:470
constexpr T rangelim(const T &d, const T2 &min, const T3 &max)
Definition numeric.h:20
v3f wrapDegrees_0_360_v3f(v3f v)
Returns v3f wrapped to the range [0, 360].
Definition numeric.h:229
s16 adjustDist(s16 dist, float zoom_fov)
Definition numeric.cpp:155
void set_bits(u32 *x, u32 pos, u32 len, u32 val)
Definition numeric.h:286
void setPitchYawRollRad(core::matrix4 &m, v3f rot)
Definition numeric.cpp:160
constexpr core::vector2d< T > componentwise_max(const core::vector2d< T > &a, const core::vector2d< T > &b)
Definition numeric.h:136
u32 myrand()
Definition numeric.cpp:17
v3f intToFloat(v3s16 p, f32 d)
Definition numeric.h:369
u32 get_bits(u32 x, u32 pos, u32 len)
Definition numeric.h:280
static constexpr const f32 BLOCK_MAX_RADIUS
Definition numeric.h:31
unsigned wrappedDifference(T a, T b, const T maximum)
Definition numeric.h:430
float modulo360f(float f)
Returns f wrapped to the range [-360, 360].
Definition numeric.h:210
v3s16 doubleToInt(v3d p, double d)
Definition numeric.h:357
#define MYRAND_RANGE
Definition numeric.h:258
float wrapDegrees_180(float f)
Returns f wrapped to the range [-180, 180].
Definition numeric.h:247
aabb3f getNodeBox(v3s16 p, float d)
Definition numeric.h:376
void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
Definition numeric.h:72
video::SColor multiplyColorValue(const video::SColor &color, float mod)
Definition numeric.h:476
constexpr bool is_power_of_two(u32 n)
Definition numeric.h:410
float myrand_float()
Definition numeric.cpp:32
constexpr u32 npot2(u32 orig)
Definition numeric.h:417
s32 myround(f32 f)
Definition numeric.h:329
int myrand_range(int min, int max)
Definition numeric.cpp:38
constexpr T numericSign(T v)
Definition numeric.h:491
u32 calc_parity(u32 v)
Definition numeric.h:293
void myrand_bytes(void *out, size_t len)
Definition numeric.cpp:27
bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, f32 camera_fov, f32 range, f32 *distance_ptr=nullptr)
Definition numeric.cpp:90
Describes a grid with given step, oirginating at (0,0,0)
Definition numeric.h:157
bool isMeshPos(v3s16 &p) const
Returns true if p is an origin of a cell in the grid.
Definition numeric.h:187
v3s16 getMeshPos(v3s16 p) const
Returns coordinates of the origin of the grid cell containing p.
Definition numeric.h:181
u16 cell_size
Definition numeric.h:158
s16 getCellPos(s16 p) const
returns coordinate of mesh cell given coordinate of a map block
Definition numeric.h:163
u32 getCellVolume() const
Definition numeric.h:160
v3s16 getCellPos(v3s16 block_pos) const
returns position of mesh cell in the grid given position of a map block
Definition numeric.h:169
s16 getMeshPos(s16 p) const
returns closest step of the grid smaller than p
Definition numeric.h:175
u16 getOffsetIndex(v3s16 offset) const
Returns index of the given offset in a grid cell All offset coordinates must be smaller than the size...
Definition numeric.h:196
Definition numeric.h:267
static constexpr result_type max()
Definition numeric.h:270
u32 result_type
Definition numeric.h:268
static constexpr result_type min()
Definition numeric.h:269
result_type operator()()
Definition numeric.h:271
static std::string p(std::string path)
Definition test_filesys.cpp:64
constexpr v3f x
Definition test_irr_matrix4.cpp:18