2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-06-17 01:29:13 +03:00
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2012-06-17 01:29:13 +03:00
|
|
|
|
2015-10-31 03:06:36 +01:00
|
|
|
#include "basic_macros.h"
|
2021-02-26 21:23:46 +01:00
|
|
|
#include "constants.h"
|
2017-11-09 01:56:20 +03:00
|
|
|
#include "irrlichttypes.h"
|
|
|
|
#include "irr_v2d.h"
|
|
|
|
#include "irr_v3d.h"
|
|
|
|
#include "irr_aabb3d.h"
|
2020-04-11 16:39:30 -04:00
|
|
|
#include "SColor.h"
|
2019-02-07 16:26:06 -05:00
|
|
|
#include <matrix4.h>
|
2023-11-12 20:08:33 +01:00
|
|
|
#include <cmath>
|
2024-09-06 11:30:27 +02:00
|
|
|
#include <algorithm>
|
2012-06-17 01:29:13 +03:00
|
|
|
|
2025-03-01 11:53:37 +01:00
|
|
|
// Like std::clamp but allows mismatched types
|
|
|
|
template <typename T, typename T2, typename T3>
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2025-03-01 11:53:37 +01:00
|
|
|
inline constexpr T rangelim(const T &d, const T2 &min, const T3 &max)
|
|
|
|
{
|
|
|
|
if (d < (T)min)
|
|
|
|
return (T)min;
|
|
|
|
if (d > (T)max)
|
|
|
|
return (T)max;
|
|
|
|
return d;
|
|
|
|
}
|
2015-02-15 17:30:38 +01:00
|
|
|
|
2021-02-26 21:23:46 +01:00
|
|
|
// Maximum radius of a block. The magic number is
|
|
|
|
// sqrt(3.0) / 2.0 in literal form.
|
|
|
|
static constexpr const f32 BLOCK_MAX_RADIUS = 0.866025403784f * MAP_BLOCKSIZE * BS;
|
2012-06-17 01:29:13 +03:00
|
|
|
|
|
|
|
inline s16 getContainerPos(s16 p, s16 d)
|
|
|
|
{
|
2015-07-06 12:53:30 -04:00
|
|
|
return (p >= 0 ? p : p - d + 1) / d;
|
2012-06-17 01:29:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
inline v2s16 getContainerPos(v2s16 p, s16 d)
|
|
|
|
{
|
|
|
|
return v2s16(
|
|
|
|
getContainerPos(p.X, d),
|
|
|
|
getContainerPos(p.Y, d)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline v3s16 getContainerPos(v3s16 p, s16 d)
|
|
|
|
{
|
|
|
|
return v3s16(
|
|
|
|
getContainerPos(p.X, d),
|
|
|
|
getContainerPos(p.Y, d),
|
|
|
|
getContainerPos(p.Z, d)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline v2s16 getContainerPos(v2s16 p, v2s16 d)
|
|
|
|
{
|
|
|
|
return v2s16(
|
|
|
|
getContainerPos(p.X, d.X),
|
|
|
|
getContainerPos(p.Y, d.Y)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline v3s16 getContainerPos(v3s16 p, v3s16 d)
|
|
|
|
{
|
|
|
|
return v3s16(
|
|
|
|
getContainerPos(p.X, d.X),
|
|
|
|
getContainerPos(p.Y, d.Y),
|
|
|
|
getContainerPos(p.Z, d.Z)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-01-14 01:19:54 +11:00
|
|
|
inline void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
|
|
|
|
{
|
|
|
|
container = (p >= 0 ? p : p - d + 1) / d;
|
|
|
|
offset = p & (d - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void getContainerPosWithOffset(const v2s16 &p, s16 d, v2s16 &container, v2s16 &offset)
|
|
|
|
{
|
|
|
|
getContainerPosWithOffset(p.X, d, container.X, offset.X);
|
|
|
|
getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void getContainerPosWithOffset(const v3s16 &p, s16 d, v3s16 &container, v3s16 &offset)
|
|
|
|
{
|
|
|
|
getContainerPosWithOffset(p.X, d, container.X, offset.X);
|
|
|
|
getContainerPosWithOffset(p.Y, d, container.Y, offset.Y);
|
|
|
|
getContainerPosWithOffset(p.Z, d, container.Z, offset.Z);
|
|
|
|
}
|
|
|
|
|
2012-06-17 01:29:13 +03:00
|
|
|
inline bool isInArea(v3s16 p, s16 d)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
p.X >= 0 && p.X < d &&
|
|
|
|
p.Y >= 0 && p.Y < d &&
|
|
|
|
p.Z >= 0 && p.Z < d
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isInArea(v2s16 p, s16 d)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
p.X >= 0 && p.X < d &&
|
|
|
|
p.Y >= 0 && p.Y < d
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isInArea(v3s16 p, v3s16 d)
|
|
|
|
{
|
|
|
|
return (
|
|
|
|
p.X >= 0 && p.X < d.X &&
|
|
|
|
p.Y >= 0 && p.Y < d.Y &&
|
|
|
|
p.Z >= 0 && p.Z < d.Z
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-03-26 19:08:31 +01:00
|
|
|
template <typename T>
|
|
|
|
inline void sortBoxVerticies(core::vector3d<T> &p1, core::vector3d<T> &p2)
|
2025-03-01 11:53:37 +01:00
|
|
|
{
|
2013-06-22 00:29:44 -04:00
|
|
|
if (p1.X > p2.X)
|
2025-03-01 11:53:37 +01:00
|
|
|
std::swap(p1.X, p2.X);
|
2013-06-22 00:29:44 -04:00
|
|
|
if (p1.Y > p2.Y)
|
2025-03-01 11:53:37 +01:00
|
|
|
std::swap(p1.Y, p2.Y);
|
2013-06-22 00:29:44 -04:00
|
|
|
if (p1.Z > p2.Z)
|
2025-03-01 11:53:37 +01:00
|
|
|
std::swap(p1.Z, p2.Z);
|
2013-06-22 00:29:44 -04:00
|
|
|
}
|
|
|
|
|
2025-03-26 19:08:31 +01:00
|
|
|
template <typename T>
|
|
|
|
inline constexpr core::vector3d<T> componentwise_min(const core::vector3d<T> &a,
|
|
|
|
const core::vector3d<T> &b)
|
2017-02-23 16:04:39 +03:00
|
|
|
{
|
2025-03-26 19:08:31 +01:00
|
|
|
return {std::min(a.X, b.X), std::min(a.Y, b.Y), std::min(a.Z, b.Z)};
|
2017-02-23 16:04:39 +03:00
|
|
|
}
|
|
|
|
|
2025-03-26 19:08:31 +01:00
|
|
|
template <typename T>
|
|
|
|
inline constexpr core::vector3d<T> componentwise_max(const core::vector3d<T> &a,
|
|
|
|
const core::vector3d<T> &b)
|
2017-02-23 16:04:39 +03:00
|
|
|
{
|
2025-03-26 19:08:31 +01:00
|
|
|
return {std::max(a.X, b.X), std::max(a.Y, b.Y), std::max(a.Z, b.Z)};
|
2017-02-23 16:04:39 +03:00
|
|
|
}
|
|
|
|
|
2023-02-08 13:42:12 -08:00
|
|
|
/// @brief Describes a grid with given step, oirginating at (0,0,0)
|
|
|
|
struct MeshGrid {
|
|
|
|
u16 cell_size;
|
|
|
|
|
|
|
|
u32 getCellVolume() const { return cell_size * cell_size * cell_size; }
|
|
|
|
|
2023-03-11 14:10:26 +01:00
|
|
|
/// @brief returns coordinate of mesh cell given coordinate of a map block
|
|
|
|
s16 getCellPos(s16 p) const
|
|
|
|
{
|
|
|
|
return (p - (p < 0) * (cell_size - 1)) / cell_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief returns position of mesh cell in the grid given position of a map block
|
|
|
|
v3s16 getCellPos(v3s16 block_pos) const
|
|
|
|
{
|
|
|
|
return v3s16(getCellPos(block_pos.X), getCellPos(block_pos.Y), getCellPos(block_pos.Z));
|
|
|
|
}
|
|
|
|
|
2023-02-08 13:42:12 -08:00
|
|
|
/// @brief returns closest step of the grid smaller than p
|
|
|
|
s16 getMeshPos(s16 p) const
|
|
|
|
{
|
2023-03-11 14:10:26 +01:00
|
|
|
return getCellPos(p) * cell_size;
|
2023-02-08 13:42:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Returns coordinates of the origin of the grid cell containing p
|
|
|
|
v3s16 getMeshPos(v3s16 p) const
|
|
|
|
{
|
|
|
|
return v3s16(getMeshPos(p.X), getMeshPos(p.Y), getMeshPos(p.Z));
|
|
|
|
}
|
2023-05-18 14:34:18 -04:00
|
|
|
|
2023-02-08 13:42:12 -08:00
|
|
|
/// @brief Returns true if p is an origin of a cell in the grid.
|
|
|
|
bool isMeshPos(v3s16 &p) const
|
|
|
|
{
|
2024-08-24 13:29:45 +02:00
|
|
|
return p.X % cell_size == 0
|
|
|
|
&& p.Y % cell_size == 0
|
|
|
|
&& p.Z % cell_size == 0;
|
2023-02-08 13:42:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Returns index of the given offset in a grid cell
|
|
|
|
/// All offset coordinates must be smaller than the size of the cell
|
|
|
|
u16 getOffsetIndex(v3s16 offset) const
|
|
|
|
{
|
|
|
|
return (offset.Z * cell_size + offset.Y) * cell_size + offset.X;
|
|
|
|
}
|
|
|
|
};
|
2012-06-17 01:29:13 +03:00
|
|
|
|
2015-02-23 16:25:14 +10:00
|
|
|
/** Returns \p f wrapped to the range [-360, 360]
|
|
|
|
*
|
|
|
|
* See test.cpp for example cases.
|
|
|
|
*
|
|
|
|
* \note This is also used in cases where degrees wrapped to the range [0, 360]
|
|
|
|
* is innapropriate (e.g. pitch needs negative values)
|
|
|
|
*/
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2015-02-23 16:25:14 +10:00
|
|
|
inline float modulo360f(float f)
|
|
|
|
{
|
2023-11-12 20:08:33 +01:00
|
|
|
return fmodf(f, 360.0f);
|
2015-02-23 16:25:14 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Returns \p f wrapped to the range [0, 360]
|
|
|
|
*/
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2012-06-17 01:29:13 +03:00
|
|
|
inline float wrapDegrees_0_360(float f)
|
|
|
|
{
|
2015-02-23 16:25:14 +10:00
|
|
|
float value = modulo360f(f);
|
|
|
|
return value < 0 ? value + 360 : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-28 03:38:50 -05:00
|
|
|
/** Returns \p v3f wrapped to the range [0, 360]
|
|
|
|
*/
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2018-11-28 03:38:50 -05:00
|
|
|
inline v3f wrapDegrees_0_360_v3f(v3f v)
|
|
|
|
{
|
|
|
|
v3f value_v3f;
|
|
|
|
value_v3f.X = modulo360f(v.X);
|
|
|
|
value_v3f.Y = modulo360f(v.Y);
|
|
|
|
value_v3f.Z = modulo360f(v.Z);
|
|
|
|
|
|
|
|
// Now that values are wrapped, use to get values for certain ranges
|
|
|
|
value_v3f.X = value_v3f.X < 0 ? value_v3f.X + 360 : value_v3f.X;
|
|
|
|
value_v3f.Y = value_v3f.Y < 0 ? value_v3f.Y + 360 : value_v3f.Y;
|
|
|
|
value_v3f.Z = value_v3f.Z < 0 ? value_v3f.Z + 360 : value_v3f.Z;
|
|
|
|
return value_v3f;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-23 16:25:14 +10:00
|
|
|
/** Returns \p f wrapped to the range [-180, 180]
|
|
|
|
*/
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2012-06-17 01:29:13 +03:00
|
|
|
inline float wrapDegrees_180(float f)
|
|
|
|
{
|
2015-02-23 16:25:14 +10:00
|
|
|
float value = modulo360f(f + 180);
|
|
|
|
if (value < 0)
|
|
|
|
value += 360;
|
|
|
|
return value - 180;
|
2012-06-17 01:29:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Pseudo-random (VC++ rand() sucks)
|
|
|
|
*/
|
2015-03-22 00:01:46 -04:00
|
|
|
#define MYRAND_RANGE 0xffffffff
|
|
|
|
u32 myrand();
|
2025-03-30 16:42:26 +02:00
|
|
|
void mysrand(u64 seed);
|
2015-03-22 00:01:46 -04:00
|
|
|
void myrand_bytes(void *out, size_t len);
|
2012-06-17 01:29:13 +03:00
|
|
|
int myrand_range(int min, int max);
|
2022-07-13 11:57:12 +02:00
|
|
|
float myrand_range(float min, float max);
|
|
|
|
float myrand_float();
|
2012-06-17 01:29:13 +03:00
|
|
|
|
2024-03-12 14:13:24 +01:00
|
|
|
// Implements a C++11 UniformRandomBitGenerator using the above functions
|
|
|
|
struct MyRandGenerator {
|
|
|
|
typedef u32 result_type;
|
|
|
|
static constexpr result_type min() { return 0; }
|
|
|
|
static constexpr result_type max() { return MYRAND_RANGE; }
|
|
|
|
inline result_type operator()() {
|
|
|
|
return myrand();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-17 01:29:13 +03:00
|
|
|
/*
|
|
|
|
Miscellaneous functions
|
|
|
|
*/
|
|
|
|
|
2015-03-30 23:40:35 -04:00
|
|
|
inline u32 get_bits(u32 x, u32 pos, u32 len)
|
|
|
|
{
|
|
|
|
u32 mask = (1 << len) - 1;
|
|
|
|
return (x >> pos) & mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void set_bits(u32 *x, u32 pos, u32 len, u32 val)
|
|
|
|
{
|
|
|
|
u32 mask = (1 << len) - 1;
|
2015-03-31 23:30:44 -04:00
|
|
|
*x &= ~(mask << pos);
|
2015-03-30 23:40:35 -04:00
|
|
|
*x |= (val & mask) << pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline u32 calc_parity(u32 v)
|
|
|
|
{
|
|
|
|
v ^= v >> 16;
|
|
|
|
v ^= v >> 8;
|
|
|
|
v ^= v >> 4;
|
|
|
|
v &= 0xf;
|
|
|
|
return (0x6996 >> v) & 1;
|
|
|
|
}
|
|
|
|
|
2025-03-26 19:08:31 +01:00
|
|
|
/**
|
|
|
|
* Calculate MurmurHash64A hash for an arbitrary block of data.
|
|
|
|
* @param key data to hash (does not need to be aligned)
|
|
|
|
* @param len length in bytes
|
|
|
|
* @param seed initial seed value
|
|
|
|
* @return hash value
|
|
|
|
*/
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2025-03-26 19:08:31 +01:00
|
|
|
u64 murmur_hash_64_ua(const void *key, size_t len, unsigned int seed);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param blockpos_b position of block in block coordinates
|
|
|
|
* @param camera_pos position of camera in nodes
|
|
|
|
* @param camera_dir an unit vector pointing to camera direction
|
|
|
|
* @param range viewing range
|
|
|
|
* @param distance_ptr return location for distance from the camera
|
|
|
|
*/
|
2012-06-17 01:29:13 +03:00
|
|
|
bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
|
2025-03-26 21:56:09 +01:00
|
|
|
f32 camera_fov, f32 range, f32 *distance_ptr=nullptr);
|
2012-06-17 01:29:13 +03:00
|
|
|
|
2017-11-15 21:58:23 -08:00
|
|
|
s16 adjustDist(s16 dist, float zoom_fov);
|
|
|
|
|
2012-06-17 01:29:13 +03:00
|
|
|
/*
|
|
|
|
Returns nearest 32-bit integer for given floating point number.
|
|
|
|
<cmath> and <math.h> in VC++ don't provide round().
|
|
|
|
*/
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2012-06-17 01:29:13 +03:00
|
|
|
inline s32 myround(f32 f)
|
|
|
|
{
|
2015-04-29 19:28:25 +02:00
|
|
|
return (s32)(f < 0.f ? (f - 0.5f) : (f + 0.5f));
|
2012-06-17 01:29:13 +03:00
|
|
|
}
|
|
|
|
|
2025-03-01 11:53:37 +01:00
|
|
|
template <typename T>
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2025-03-01 11:53:37 +01:00
|
|
|
inline constexpr T sqr(T f)
|
2018-09-16 19:59:42 +03:00
|
|
|
{
|
|
|
|
return f * f;
|
|
|
|
}
|
|
|
|
|
2012-06-17 01:29:13 +03:00
|
|
|
/*
|
|
|
|
Returns integer position of node in given floating point position
|
|
|
|
*/
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2012-06-17 01:29:13 +03:00
|
|
|
inline v3s16 floatToInt(v3f p, f32 d)
|
2017-12-22 10:00:57 +00:00
|
|
|
{
|
|
|
|
return v3s16(
|
|
|
|
(p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
|
|
|
|
(p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
|
|
|
|
(p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Returns integer position of node in given double precision position
|
|
|
|
*/
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2017-12-22 10:00:57 +00:00
|
|
|
inline v3s16 doubleToInt(v3d p, double d)
|
2012-06-17 01:29:13 +03:00
|
|
|
{
|
2015-07-06 12:53:30 -04:00
|
|
|
return v3s16(
|
|
|
|
(p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
|
|
|
|
(p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
|
|
|
|
(p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
|
2012-06-17 01:29:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Returns floating point position of node in given integer position
|
|
|
|
*/
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2012-06-17 01:29:13 +03:00
|
|
|
inline v3f intToFloat(v3s16 p, f32 d)
|
|
|
|
{
|
2025-03-01 11:53:37 +01:00
|
|
|
return v3f::from(p) * d;
|
2012-06-17 01:29:13 +03:00
|
|
|
}
|
|
|
|
|
2025-03-26 21:56:09 +01:00
|
|
|
// Returns box of a node as in-world box. Usually d=BS
|
|
|
|
[[nodiscard]]
|
2016-02-11 15:21:21 +01:00
|
|
|
inline aabb3f getNodeBox(v3s16 p, float d)
|
2012-06-17 01:29:13 +03:00
|
|
|
{
|
2016-02-11 15:21:21 +01:00
|
|
|
return aabb3f(
|
2025-03-01 11:53:37 +01:00
|
|
|
v3f::from(p) * d - 0.5f * d,
|
|
|
|
v3f::from(p) * d + 0.5f * d
|
2012-06-17 01:29:13 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-07-06 12:53:30 -04:00
|
|
|
|
2012-06-17 01:29:13 +03:00
|
|
|
class IntervalLimiter
|
|
|
|
{
|
|
|
|
public:
|
2017-08-20 13:30:50 +02:00
|
|
|
IntervalLimiter() = default;
|
|
|
|
|
2024-02-26 20:46:57 +01:00
|
|
|
/**
|
|
|
|
@param dtime time from last call to this method
|
|
|
|
@param wanted_interval interval wanted
|
|
|
|
@return true if action should be done
|
2012-06-17 01:29:13 +03:00
|
|
|
*/
|
2025-03-26 21:56:09 +01:00
|
|
|
[[nodiscard]]
|
2018-01-12 23:47:39 -08:00
|
|
|
bool step(float dtime, float wanted_interval)
|
2012-06-17 01:29:13 +03:00
|
|
|
{
|
|
|
|
m_accumulator += dtime;
|
2015-07-06 12:53:30 -04:00
|
|
|
if (m_accumulator < wanted_interval)
|
2012-06-17 01:29:13 +03:00
|
|
|
return false;
|
|
|
|
m_accumulator -= wanted_interval;
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-06 12:53:30 -04:00
|
|
|
|
|
|
|
private:
|
2017-06-19 23:54:58 +02:00
|
|
|
float m_accumulator = 0.0f;
|
2012-06-17 01:29:13 +03:00
|
|
|
};
|
|
|
|
|
2015-07-06 12:53:30 -04:00
|
|
|
|
2012-06-17 01:29:13 +03:00
|
|
|
/*
|
|
|
|
Splits a list into "pages". For example, the list [1,2,3,4,5] split
|
|
|
|
into two pages would be [1,2,3],[4,5]. This function computes the
|
|
|
|
minimum and maximum indices of a single page.
|
|
|
|
|
|
|
|
length: Length of the list that should be split
|
|
|
|
page: Page number, 1 <= page <= pagecount
|
|
|
|
pagecount: The number of pages, >= 1
|
|
|
|
minindex: Receives the minimum index (inclusive).
|
|
|
|
maxindex: Receives the maximum index (exclusive).
|
|
|
|
|
|
|
|
Ensures 0 <= minindex <= maxindex <= length.
|
|
|
|
*/
|
|
|
|
inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
|
|
|
|
{
|
2015-07-06 12:53:30 -04:00
|
|
|
if (length < 1 || pagecount < 1 || page < 1 || page > pagecount) {
|
2012-06-17 01:29:13 +03:00
|
|
|
// Special cases or invalid parameters
|
|
|
|
minindex = maxindex = 0;
|
2015-07-06 12:53:30 -04:00
|
|
|
} else if(pagecount <= length) {
|
2012-06-17 01:29:13 +03:00
|
|
|
// Less pages than entries in the list:
|
|
|
|
// Each page contains at least one entry
|
|
|
|
minindex = (length * (page-1) + (pagecount-1)) / pagecount;
|
|
|
|
maxindex = (length * page + (pagecount-1)) / pagecount;
|
2015-07-06 12:53:30 -04:00
|
|
|
} else {
|
2012-06-17 01:29:13 +03:00
|
|
|
// More pages than entries in the list:
|
|
|
|
// Make sure the empty pages are at the end
|
2015-07-06 12:53:30 -04:00
|
|
|
if (page < length) {
|
2012-06-17 01:29:13 +03:00
|
|
|
minindex = page-1;
|
|
|
|
maxindex = page;
|
2015-07-06 12:53:30 -04:00
|
|
|
} else {
|
2012-06-17 01:29:13 +03:00
|
|
|
minindex = 0;
|
|
|
|
maxindex = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-01 11:53:37 +01:00
|
|
|
constexpr inline bool is_power_of_two(u32 n)
|
2014-11-02 03:47:43 +01:00
|
|
|
{
|
2015-07-06 12:53:30 -04:00
|
|
|
return n != 0 && (n & (n - 1)) == 0;
|
2014-11-02 03:47:43 +01:00
|
|
|
}
|
|
|
|
|
2015-03-09 09:32:11 -04:00
|
|
|
// Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
|
|
|
|
// Public Domain: https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
2025-03-01 11:53:37 +01:00
|
|
|
constexpr inline u32 npot2(u32 orig)
|
|
|
|
{
|
2015-03-09 09:32:11 -04:00
|
|
|
orig--;
|
|
|
|
orig |= orig >> 1;
|
|
|
|
orig |= orig >> 2;
|
|
|
|
orig |= orig >> 4;
|
|
|
|
orig |= orig >> 8;
|
|
|
|
orig |= orig >> 16;
|
|
|
|
return orig + 1;
|
|
|
|
}
|
2018-08-03 00:25:37 +02:00
|
|
|
|
2024-08-12 15:32:18 +02:00
|
|
|
// Distance between two values in a wrapped (circular) system
|
|
|
|
template<typename T>
|
|
|
|
inline unsigned wrappedDifference(T a, T b, const T maximum)
|
|
|
|
{
|
|
|
|
if (a > b)
|
|
|
|
std::swap(a, b);
|
|
|
|
// now b >= a
|
|
|
|
unsigned s = b - a, l = static_cast<unsigned>(maximum - b) + a + 1;
|
|
|
|
return std::min(s, l);
|
|
|
|
}
|
|
|
|
|
2018-08-03 00:25:37 +02:00
|
|
|
// Gradual steps towards the target value in a wrapped (circular) system
|
|
|
|
// using the shorter of both ways
|
|
|
|
template<typename T>
|
|
|
|
inline void wrappedApproachShortest(T ¤t, const T target, const T stepsize,
|
|
|
|
const T maximum)
|
|
|
|
{
|
|
|
|
T delta = target - current;
|
|
|
|
if (delta < 0)
|
|
|
|
delta += maximum;
|
|
|
|
|
|
|
|
if (delta > stepsize && maximum - delta > stepsize) {
|
|
|
|
current += (delta < maximum / 2) ? stepsize : -stepsize;
|
|
|
|
if (current >= maximum)
|
|
|
|
current -= maximum;
|
|
|
|
} else {
|
|
|
|
current = target;
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 16:26:06 -05:00
|
|
|
|
2024-03-12 14:13:24 +01:00
|
|
|
void setPitchYawRollRad(core::matrix4 &m, v3f rot);
|
2019-02-07 16:26:06 -05:00
|
|
|
|
2024-03-12 14:13:24 +01:00
|
|
|
inline void setPitchYawRoll(core::matrix4 &m, v3f rot)
|
2019-02-07 16:26:06 -05:00
|
|
|
{
|
2024-03-12 14:13:24 +01:00
|
|
|
setPitchYawRollRad(m, rot * core::DEGTORAD);
|
2019-02-07 16:26:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
v3f getPitchYawRollRad(const core::matrix4 &m);
|
|
|
|
|
|
|
|
inline v3f getPitchYawRoll(const core::matrix4 &m)
|
|
|
|
{
|
2024-03-12 14:13:24 +01:00
|
|
|
return getPitchYawRollRad(m) * core::RADTODEG;
|
2019-02-07 16:26:06 -05:00
|
|
|
}
|
2020-04-11 16:39:30 -04:00
|
|
|
|
|
|
|
// Muliply the RGB value of a color linearly, and clamp to black/white
|
2025-03-26 19:08:31 +01:00
|
|
|
inline video::SColor multiplyColorValue(const video::SColor &color, float mod)
|
2020-04-11 16:39:30 -04:00
|
|
|
{
|
2025-03-26 19:08:31 +01:00
|
|
|
return video::SColor(color.getAlpha(),
|
2020-04-11 16:39:30 -04:00
|
|
|
core::clamp<u32>(color.getRed() * mod, 0, 255),
|
|
|
|
core::clamp<u32>(color.getGreen() * mod, 0, 255),
|
|
|
|
core::clamp<u32>(color.getBlue() * mod, 0, 255));
|
|
|
|
}
|
2022-07-13 11:57:12 +02:00
|
|
|
|
2025-03-26 21:56:09 +01:00
|
|
|
template <typename T>
|
|
|
|
constexpr inline T numericAbsolute(T v)
|
2025-03-26 19:08:31 +01:00
|
|
|
{
|
|
|
|
return v < 0 ? T(-v) : v;
|
|
|
|
}
|
2022-07-13 11:57:12 +02:00
|
|
|
|
2025-03-26 21:56:09 +01:00
|
|
|
template <typename T>
|
|
|
|
constexpr inline T numericSign(T v)
|
2022-07-13 11:57:12 +02:00
|
|
|
{
|
2025-03-26 19:08:31 +01:00
|
|
|
return T(v < 0 ? -1 : (v == 0 ? 0 : 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline constexpr core::vector3d<T> vecAbsolute(const core::vector3d<T> &v)
|
|
|
|
{
|
|
|
|
return {
|
2022-07-13 11:57:12 +02:00
|
|
|
numericAbsolute(v.X),
|
|
|
|
numericAbsolute(v.Y),
|
|
|
|
numericAbsolute(v.Z)
|
2025-03-26 19:08:31 +01:00
|
|
|
};
|
2022-07-13 11:57:12 +02:00
|
|
|
}
|
|
|
|
|
2025-03-26 19:08:31 +01:00
|
|
|
template <typename T>
|
|
|
|
inline constexpr core::vector3d<T> vecSign(const core::vector3d<T> &v)
|
2022-07-13 11:57:12 +02:00
|
|
|
{
|
2025-03-26 19:08:31 +01:00
|
|
|
return {
|
2022-07-13 11:57:12 +02:00
|
|
|
numericSign(v.X),
|
|
|
|
numericSign(v.Y),
|
|
|
|
numericSign(v.Z)
|
2025-03-26 19:08:31 +01:00
|
|
|
};
|
2022-07-13 11:57:12 +02:00
|
|
|
}
|