24#if (DL_PLATFORM == DL_PLATFORM_WIN32)
39 template<
typename T,
size_t N>
40 void Min(std::array<T, N>& container)
42 for (
size_t i = 0; i <
N; ++i)
44 container[i] = (std::numeric_limits<T>::min)();
47 template<
typename T,
size_t N>
48 void Max(std::array<T, N>& container)
50 for (
size_t i = 0; i <
N; ++i)
52 container[i] = (std::numeric_limits<T>::max)();
75constexpr T Limit(
const T& v,
const T& v_min,
const T& v_max)
90 return x < a || b < x;
99constexpr uint64_t
Pow(uint64_t x, uint8_t n)
102 for (uint8_t i = 0; i < n; ++i)
108constexpr uint32_t
Pow32(uint32_t x, uint8_t n)
111 for (uint8_t i = 0; i < n; ++i)
122 Pow(2,0),
Pow(2,1),
Pow(2,2),
Pow(2,3),
Pow(2,4),
Pow(2,5),
Pow(2,6),
Pow(2,7),
123 Pow(2,8),
Pow(2,9),
Pow(2,10),
Pow(2,11),
Pow(2,12),
Pow(2,13),
Pow(2,14),
Pow(2,15),
124 Pow(2,16),
Pow(2,17),
Pow(2,18),
Pow(2,19),
Pow(2,20),
Pow(2,21),
Pow(2,22),
Pow(2,23),
125 Pow(2,24),
Pow(2,25),
Pow(2,26),
Pow(2,27),
Pow(2,28),
Pow(2,29),
Pow(2,30),
Pow(2,31),
126 Pow(2,32),
Pow(2,33),
Pow(2,34),
Pow(2,35),
Pow(2,36),
Pow(2,37),
Pow(2,38),
Pow(2,39),
127 Pow(2,40),
Pow(2,41),
Pow(2,42),
Pow(2,43),
Pow(2,44),
Pow(2,45),
Pow(2,46),
Pow(2,47),
128 Pow(2,48),
Pow(2,49),
Pow(2,50),
Pow(2,51),
Pow(2,52),
Pow(2,53),
Pow(2,54),
Pow(2,55),
129 Pow(2,56),
Pow(2,57),
Pow(2,58),
Pow(2,59),
Pow(2,60),
Pow(2,61),
Pow(2,62),
Pow(2,63) };
131 Pow32(2,0),
Pow32(2,1),
Pow32(2,2),
Pow32(2,3),
Pow32(2,4),
Pow32(2,5),
Pow32(2,6),
Pow32(2,7),
132 Pow32(2,8),
Pow32(2,9),
Pow32(2,10),
Pow32(2,11),
Pow32(2,12),
Pow32(2,13),
Pow32(2,14),
Pow32(2,15),
133 Pow32(2,16),
Pow32(2,17),
Pow32(2,18),
Pow32(2,19),
Pow32(2,20),
Pow32(2,21),
Pow32(2,22),
Pow32(2,23),
134 Pow32(2,24),
Pow32(2,25),
Pow32(2,26),
Pow32(2,27),
Pow32(2,28),
Pow32(2,29),
Pow32(2,30),
Pow32(2,31) };
141#define DND_ARRAY_POW_LINE(index) if (x <= ARRAY_POW2[index])return ARRAY_POW2[index]
153 log(
"数值过大,超过2^63:" + std::to_string(x));
156#define DND_ARRAY_POW_LINE_32(index) if (x <= ARRAY_POW2_32[index])return ARRAY_POW2_32[index]
164 log(
"数值过大,超过2^31:" + std::to_string(x));
175#if (DL_PLATFORM == DL_PLATFORM_WIN32)
176 return std::bit_ceil(x);
193 const float threehalfs = 1.5f;
198 i = 0x5f3759df - (i >> 1);
200 y = y * (threehalfs - (x2 * y * y));
212 for (
size_t i = 0; i <
N; ++i)
214 length += (v[i] * v[i]);
217 length =
RSqrt(length);
218 for (
size_t i = 0; i <
N; ++i)
232 return a[0] * a[0] + a[1] * a[1];
238 return a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
244 return a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3];
252T DisPow2(
const std::array<T, 2>& a,
const std::array<T, 2>& b)
262template <IsUn
signed T>
265 return a > b ? a - b : b - a;
271template <IsUn
signed T0, IsSigned T1>
275 if (b < 0 && (T0)(-b) > a)
286template <IsUn
signed T>
287T DisPow2U(
const std::array<T, 2>& a,
const std::array<T, 2>& b)
289 unsigned dx =
DisU(a[0], b[0]);
290 unsigned dy =
DisU(a[1], b[1]);
291 return dx * dx + dy * dy;
297template <IsSigned T,
size_t N>
301 length = sqrt(length);
306template <IsSigned T,
size_t N>
310 std::array<T, N> ret = v;
334constexpr double PI(
unsigned n,
unsigned d = 1)
336#ifdef __cpp_lib_concepts
337 return std::numbers::pi * n / d;
339 return 3.1415926535897932384626433832795 * n / d;
403 dnd_assert(align > 0);
429template<IsUn
signed T>
448 if ((0 == align) || (align & (align - 1)))
453 return ((x + (align - 1)) & ~(align - 1));
465 return r >= (d >> 1) ? q + 1 : q;
476 return (n + (d >> 1)) / d;
490constexpr float OffsetOrder(
float order,
int u = 1,
int v = 0,
int w = 0)
492 constexpr int NUM_ORDER_DT = 32;
493 float ret = order + (w + v * NUM_ORDER_DT + u * NUM_ORDER_DT * NUM_ORDER_DT)
494 * std::numeric_limits<float>::epsilon();
497 log_(
"order超出范围:{}", ret);
507 return a[0] * b[1] - a[1] * b[0];
513 a[1] * b[2] - b[1] * a[2],
514 a[2] * b[0] - b[2] * a[0],
515 a[0] * b[1] - b[0] * a[1]
522template<
typename T,
size_t N>
523constexpr T DotProduct(
const std::array<T, N>& a,
const std::array<T, N>& b)
526 for (
size_t i = 0; i <
N; ++i)
602 if (list_pos.size() < 3)
604 auto iter_last = list_pos.end();
606 for (
auto iter_0 = list_pos.begin(); iter_0 != iter_last;)
609 auto iter_0_next = iter_0;
613 for (
auto iter_1 = iter_last; iter_1 != iter_0_next; --iter_1)
615 if (func_pass(*iter_0, *iter_1))
617 for (
auto iter = iter_0_next; iter != iter_1;)
618 iter = list_pos.erase(iter);
668 return rect[0] <= rect[2]
669 && rect[1] <= rect[3];
683 && rect[2] <= size[0]
684 && rect[3] <= size[1];
694 return unsigned(xy[0]) < size[0]
695 && unsigned(xy[1]) < size[1];
704 return xy[0] >= rect[0]
723 return t1 * t2 >= 0 &&
#define DND_ARRAY_POW_LINE_32(index)
#define DND_ARRAY_POW_LINE(index)
向上取2的幂次
constexpr bool Less(const std::array< T, N > &a, const std::array< T, N > &b)
每个元素都小于返回真
T DisPow2U(const std::array< T, 2 > &a, const std::array< T, 2 > &b)
返回 距离的平方
std::vector< Point > DrawLineOrth(const Point &p0, const Point &p1)
正交 画线算法
constexpr bool IsXyInRect(const Point &xy, const Rect &rect)
判断 点是否在矩形内
constexpr bool IsOutRange(T x, T a, T b)
判断数值是否越界(前闭后开)
constexpr T Align(T x, T align)
对齐align的倍数
constexpr Float CrossProduct(const Position2 &a, const Position2 &b)
叉乘
void Normalize(std::array< T, N > &v)
向量单位化
constexpr unsigned AlignPow2(unsigned x, unsigned align)
对齐align的倍数
constexpr float OffsetOrder(float order, int u=1, int v=0, int w=0)
偏移order
std::vector< Point > DrawLineLerp(const Point &p0, const Point &p1)
线性插值 画线算法
void MergePathDirect(std::list< Point > &list_pos, F func_pass)
路径合并(网格路径)
constexpr uint32_t ARRAY_POW2_32[]
constexpr T AlignF(T x, T align)
RGP资源下标转方向
constexpr unsigned div_found(unsigned n, unsigned d)
无符号除法,返回四舍五入结果
constexpr bool IsXyInSize(const Point &xy, const Size &size)
判断 点是否在大小内
constexpr uint32_t Pow32(uint32_t x, uint8_t n)
std::vector< Point > DrawLineCover(const Point &p0, const Point &p1)
覆盖 画线算法
constexpr unsigned div_found_fast(unsigned n, unsigned d)
无符号除法,返回四舍五入结果
Position4 Slerp(const Position4 &q0, const Position4 &q1, const Float &t)
球面线性插值
void MergePath(std::list< Point > &list_pos)
路径合并(网格路径)
constexpr double PI(unsigned n, unsigned d=1)
返回PI的分数
constexpr T DotProduct(const std::array< T, N > &a, const std::array< T, N > &b)
点乘
float RSqrt(float x)
返回平方根的倒数
constexpr bool IsRectInSize(const std::array< T, 4 > &rect, const Size &size)
判断 矩形 在 大小 内,首先它必须是有效的矩形
T DisU(T a, T b)
返回无符号类型的差值
constexpr uint64_t ARRAY_POW2[]
2的整数次幂列表
std::vector< Point > DrawLineBresenham(const Point &p0, const Point &p1)
bresenham 画线算法
constexpr uint64_t Pow(uint64_t x, uint8_t n)
返回x的n次方
constexpr auto DistanceDiagonal(const C &p0, const C &p1)
纹理区域转uv
std::vector< PointU > getCircle(const Size &size, const PointU &xy, unsigned rad)
返回圆形范围的节点,考虑越界
constexpr bool IsValidRect(const std::array< T, 4 > &rect)
**
constexpr bool IsDotInQuad(const Position &dot, const Quad &quad)
判断点是否在凸四边形内
constexpr T Limit(const T &v, const T &v_min, const T &v_max)
限制数值到 闭区间
constexpr uint64_t CeilToPow2(uint64_t &&x)
T DisPow2(const std::array< T, 2 > &a)
返回 向量 距离的平方
T0 AddU(T0 a, T1 b)
无符号 加 有符号,防止小于0
constexpr bool IsDotInTriangle(const Position &dot, const Triangle &triangle)
判断点是否在三角形内
void NormalizeFast(std::array< float, N > &v)
向量单位化,精度低速度快
constexpr Float EARTH_fADIUS
地球半径km
constexpr Float GRAVITY
地球重力加速度(赤道最小)
void Max(std::array< T, N > &container)
void Min(std::array< T, N > &container)
返回的是最小正数(假设有符号)
constexpr auto Max(const C &container)
返回容器最大值
constexpr T is_saturate(const T &x)
是否数值在[0, 1]
void log(std::string_view str, const std::source_location location=std::source_location::current())
以当前设置输出
std::array< Float, 3 > Position3
std::array< Float, 2 > Position2
std::array< Float, 4 > Position4
constexpr T saturate(const T &x)
限制数值到 [0, 1]
std::array< Position, 4 > Quad
std::array< Position, 3 > Triangle