Luanti 5.19.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
38inline v2s16 getContainerPos(v2s16 p, s16 d)
39{
40 return v2s16(
41 getContainerPos(p.X, d),
42 getContainerPos(p.Y, d)
43 );
44}
45
46inline v3s16 getContainerPos(v3s16 p, s16 d)
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
320bool isBlockInSight(const v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
321 const f32 camera_fov, const f32 range, f32 *distance_ptr=nullptr);
322
332bool isBlockInSightEx(const v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
333 const f32 target_cos_sq, const f32 adjdist, const f32 range, f32 *distance_ptr);
334
335s16 adjustDist(s16 dist, float zoom_fov);
336
337
338inline f32 getFovAdj(f32 camera_fov)
339{
340 // Adjust camera position, for purposes of computing the angle,
341 // such that a block that has any portion visible with the
342 // current camera position will have the center visible at the
343 // adjusted position
344 return BLOCK_MAX_RADIUS / std::sin(camera_fov * 0.5f);
345}
346
347inline f32 getFovCosSq(f32 camera_fov)
348{
349 // If block is not in the field of view, skip it
350 // HOTFIX: use slightly increased angle (+10%) to fix too aggressive
351 // culling. Somebody has to find out what's wrong with the math here.
352 // Previous value: camera_fov / 2
353 const f32 target_cos = std::cos(camera_fov * 0.55f);
354 return target_cos * target_cos;
355}
356
357/*
358 Returns nearest 32-bit integer for given floating point number.
359 <cmath> and <math.h> in VC++ don't provide round().
360*/
361[[nodiscard]]
362inline s32 myround(f32 f)
363{
364 return (s32)(f < 0.f ? (f - 0.5f) : (f + 0.5f));
365}
366
367template <typename T>
368[[nodiscard]]
369inline constexpr T sqr(T f)
370{
371 return f * f;
372}
373
374/*
375 Returns integer position of node in given floating point position
376*/
377[[nodiscard]]
378inline v3s16 floatToInt(v3f p, f32 d)
379{
380 return v3s16(
381 (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
382 (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
383 (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
384}
385
386/*
387 Returns integer position of node in given double precision position
388 */
389[[nodiscard]]
390inline v3s16 doubleToInt(v3d p, double d)
391{
392 return v3s16(
393 (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
394 (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
395 (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
396}
397
398/*
399 Returns floating point position of node in given integer position
400*/
401[[nodiscard]]
402inline v3f intToFloat(v3s16 p, f32 d)
403{
404 return v3f::from(p) * d;
405}
406
407// Returns box of a node as in-world box. Usually d=BS
408[[nodiscard]]
409inline aabb3f getNodeBox(v3s16 p, float d)
410{
411 return aabb3f(
412 v3f::from(p) * d - 0.5f * d,
413 v3f::from(p) * d + 0.5f * d
414 );
415}
416
417
419{
420public:
421 IntervalLimiter() = default;
422
428 [[nodiscard]]
429 bool step(float dtime, float wanted_interval)
430 {
431 m_accumulator += dtime;
432 if (m_accumulator < wanted_interval)
433 return false;
434 m_accumulator -= wanted_interval;
435 return true;
436 }
437
438private:
439 float m_accumulator = 0.0f;
440};
441
442// For details about how framerate independent lerping works, see:
443// https://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/
444inline f32 damp(f32 a, f32 b, f32 lambda) {
445 return core::lerp(a, b, 1.0f - std::exp(-lambda));
446}
447
448constexpr inline bool is_power_of_two(u32 n)
449{
450 return n != 0 && (n & (n - 1)) == 0;
451}
452
453// Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
454// Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
455constexpr inline u32 npot2(u32 orig)
456{
457 orig--;
458 orig |= orig >> 1;
459 orig |= orig >> 2;
460 orig |= orig >> 4;
461 orig |= orig >> 8;
462 orig |= orig >> 16;
463 return orig + 1;
464}
465
466// Distance between two values in a wrapped (circular) system
467template<typename T>
468inline unsigned wrappedDifference(T a, T b, const T maximum)
469{
470 if (a > b)
471 std::swap(a, b);
472 // now b >= a
473 unsigned s = b - a, l = static_cast<unsigned>(maximum - b) + a + 1;
474 return std::min(s, l);
475}
476
477// Gradual steps towards the target value in a wrapped (circular) system
478// using the shorter of both ways
479template<typename T>
480inline void wrappedApproachShortest(T &current, const T target, const T stepsize,
481 const T maximum)
482{
483 T delta = target - current;
484 if (delta < 0)
485 delta += maximum;
486
487 if (delta > stepsize && maximum - delta > stepsize) {
488 current += (delta < maximum / 2) ? stepsize : -stepsize;
489 if (current >= maximum)
490 current -= maximum;
491 } else {
492 current = target;
493 }
494}
495
498void setPitchYawRollRad(core::matrix4 &m, v3f rot);
499
500inline void setPitchYawRoll(core::matrix4 &m, v3f rot)
501{
502 setPitchYawRollRad(m, rot * core::DEGTORAD);
503}
504
506v3f getPitchYawRollRad(const core::matrix4 &m);
507
508inline v3f getPitchYawRoll(const core::matrix4 &m)
509{
510 return getPitchYawRollRad(m) * core::RADTODEG;
511}
512
513// Multiply the RGB value of a color linearly, and clamp to black/white
514inline video::SColor multiplyColorValue(const video::SColor &color, float mod)
515{
516 return video::SColor(color.getAlpha(),
517 core::clamp<u32>(color.getRed() * mod, 0, 255),
518 core::clamp<u32>(color.getGreen() * mod, 0, 255),
519 core::clamp<u32>(color.getBlue() * mod, 0, 255));
520}
521
522template <typename T>
523constexpr inline T numericAbsolute(T v)
524{
525 return v < 0 ? T(-v) : v;
526}
527
528template <typename T>
529constexpr inline T numericSign(T v)
530{
531 return T(v < 0 ? -1 : (v == 0 ? 0 : 1));
532}
533
534template <typename T>
535inline constexpr core::vector3d<T> vecAbsolute(const core::vector3d<T> &v)
536{
537 return {
538 numericAbsolute(v.X),
539 numericAbsolute(v.Y),
540 numericAbsolute(v.Z)
541 };
542}
543
544template <typename T>
545inline constexpr core::vector3d<T> vecSign(const core::vector3d<T> &v)
546{
547 return {
548 numericSign(v.X),
549 numericSign(v.Y),
550 numericSign(v.Z)
551 };
552}
float m_accumulator
Definition numeric.h:439
IntervalLimiter()=default
bool step(float dtime, float wanted_interval)
Definition numeric.h:429
#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
v3f getPitchYawRollRad(const core::matrix4 &m)
Definition numeric.cpp:188
void mysrand(u64 seed)
Definition numeric.cpp:22
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
bool isBlockInSightEx(const v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, const f32 target_cos_sq, const f32 adjdist, f32 range, f32 *distance_ptr)
Definition numeric.cpp:95
void setPitchYawRollRad(core::matrix4 &m, v3f rot)
Definition numeric.cpp:167
u32 myrand()
Definition numeric.cpp:17
float adjustDist(float dist, float zoom_fov)
Definition numeric.cpp:150
bool isBlockInSight(const v3s16 blockpos_b, v3f camera_pos, v3f camera_dir, const f32 camera_fov, const f32 range, f32 *distance_ptr)
Definition numeric.cpp:89
float myrand_float()
Definition numeric.cpp:32
int myrand_range(int min, int max)
Definition numeric.cpp:38
void myrand_bytes(void *out, size_t len)
Definition numeric.cpp:27
constexpr core::vector3d< T > vecAbsolute(const core::vector3d< T > &v)
Definition numeric.h:535
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
constexpr T sqr(T f)
Definition numeric.h:369
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:480
v3s16 floatToInt(v3f p, f32 d)
Definition numeric.h:378
float wrapDegrees_0_360(float f)
Returns f wrapped to the range [0, 360].
Definition numeric.h:219
f32 damp(f32 a, f32 b, f32 lambda)
Definition numeric.h:444
constexpr core::vector3d< T > vecSign(const core::vector3d< T > &v)
Definition numeric.h:545
constexpr T numericAbsolute(T v)
Definition numeric.h:523
void setPitchYawRoll(core::matrix4 &m, v3f rot)
Definition numeric.h:500
void sortBoxVerticies(core::vector3d< T > &p1, core::vector3d< T > &p2)
Definition numeric.h:118
v3f getPitchYawRoll(const core::matrix4 &m)
Definition numeric.h:508
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
void set_bits(u32 *x, u32 pos, u32 len, u32 val)
Definition numeric.h:286
constexpr core::vector2d< T > componentwise_max(const core::vector2d< T > &a, const core::vector2d< T > &b)
Definition numeric.h:136
f32 getFovAdj(f32 camera_fov)
Definition numeric.h:338
u32 myrand()
Definition numeric.cpp:17
v3f intToFloat(v3s16 p, f32 d)
Definition numeric.h:402
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:468
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:390
#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:409
void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
Definition numeric.h:72
f32 getFovCosSq(f32 camera_fov)
Definition numeric.h:347
video::SColor multiplyColorValue(const video::SColor &color, float mod)
Definition numeric.h:514
constexpr bool is_power_of_two(u32 n)
Definition numeric.h:448
constexpr u32 npot2(u32 orig)
Definition numeric.h:455
s32 myround(f32 f)
Definition numeric.h:362
constexpr T numericSign(T v)
Definition numeric.h:529
u32 calc_parity(u32 v)
Definition numeric.h:293
Describes a grid with given step, originating 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
constexpr v3f x
Definition test_irr_matrix4.cpp:18