23#if (DL_PLATFORM == DL_PLATFORM_WIN32)
38 template<
typename T,
size_t N>
39 void Min(std::array<T, N>& container)
41 for (
size_t i = 0; i <
N; ++i)
43 container[i] = (std::numeric_limits<T>::min)();
46 template<
typename T,
size_t N>
47 void Max(std::array<T, N>& container)
49 for (
size_t i = 0; i <
N; ++i)
51 container[i] = (std::numeric_limits<T>::max)();
74constexpr T Limit(
const T& v,
const T& v_min,
const T& v_max)
87template<
typename T,
size_t N>
89std::array<unsigned, N>
CastU(
const std::array<T, N>& a,
bool show_log =
true)
91 std::array<unsigned, N> ret;
92 for (
int i = 0; i !=
N; ++i)
101 ret[i] = unsigned(a[i]);
109template<
typename T,
size_t N>
113 std::array<unsigned, N> ret;
114 for (
int i = 0; i !=
N; ++i)
115 ret[i] =
unsigned(a[i]);
125 return x < a || b < x;
134constexpr uint64_t
Pow(uint64_t x, uint8_t n)
137 for (uint8_t i = 0; i < n; ++i)
143constexpr uint32_t
Pow32(uint32_t x, uint8_t n)
146 for (uint8_t i = 0; i < n; ++i)
157 Pow(2,0),
Pow(2,1),
Pow(2,2),
Pow(2,3),
Pow(2,4),
Pow(2,5),
Pow(2,6),
Pow(2,7),
158 Pow(2,8),
Pow(2,9),
Pow(2,10),
Pow(2,11),
Pow(2,12),
Pow(2,13),
Pow(2,14),
Pow(2,15),
159 Pow(2,16),
Pow(2,17),
Pow(2,18),
Pow(2,19),
Pow(2,20),
Pow(2,21),
Pow(2,22),
Pow(2,23),
160 Pow(2,24),
Pow(2,25),
Pow(2,26),
Pow(2,27),
Pow(2,28),
Pow(2,29),
Pow(2,30),
Pow(2,31),
161 Pow(2,32),
Pow(2,33),
Pow(2,34),
Pow(2,35),
Pow(2,36),
Pow(2,37),
Pow(2,38),
Pow(2,39),
162 Pow(2,40),
Pow(2,41),
Pow(2,42),
Pow(2,43),
Pow(2,44),
Pow(2,45),
Pow(2,46),
Pow(2,47),
163 Pow(2,48),
Pow(2,49),
Pow(2,50),
Pow(2,51),
Pow(2,52),
Pow(2,53),
Pow(2,54),
Pow(2,55),
164 Pow(2,56),
Pow(2,57),
Pow(2,58),
Pow(2,59),
Pow(2,60),
Pow(2,61),
Pow(2,62),
Pow(2,63) };
166 Pow32(2,0),
Pow32(2,1),
Pow32(2,2),
Pow32(2,3),
Pow32(2,4),
Pow32(2,5),
Pow32(2,6),
Pow32(2,7),
167 Pow32(2,8),
Pow32(2,9),
Pow32(2,10),
Pow32(2,11),
Pow32(2,12),
Pow32(2,13),
Pow32(2,14),
Pow32(2,15),
168 Pow32(2,16),
Pow32(2,17),
Pow32(2,18),
Pow32(2,19),
Pow32(2,20),
Pow32(2,21),
Pow32(2,22),
Pow32(2,23),
169 Pow32(2,24),
Pow32(2,25),
Pow32(2,26),
Pow32(2,27),
Pow32(2,28),
Pow32(2,29),
Pow32(2,30),
Pow32(2,31) };
176#define DND_ARRAY_POW_LINE(index) if (x <= ARRAY_POW2[index])return ARRAY_POW2[index]
188 log(
"数值过大,超过2^63:" + std::to_string(x));
191#define DND_ARRAY_POW_LINE_32(index) if (x <= ARRAY_POW2_32[index])return ARRAY_POW2_32[index]
199 log(
"数值过大,超过2^31:" + std::to_string(x));
210#if (DL_PLATFORM == DL_PLATFORM_WIN32)
211 return std::bit_ceil(x);
228 const float threehalfs = 1.5f;
233 i = 0x5f3759df - (i >> 1);
235 y = y * (threehalfs - (x2 * y * y));
247 for (
size_t i = 0; i <
N; ++i)
249 length += (v[i] * v[i]);
252 length =
RSqrt(length);
253 for (
size_t i = 0; i <
N; ++i)
267 return a[0] * a[0] + a[1] * a[1];
273 return a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
279 return a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3];
287T DisPow2(
const std::array<T, 2>& a,
const std::array<T, 2>& b)
297template <IsUn
signed T>
300 return a > b ? a - b : b - a;
306template <IsUn
signed T0, IsSigned T1>
310 if (b < 0 && (T0)(-b) > a)
321template <IsUn
signed T>
322T DisPow2U(
const std::array<T, 2>& a,
const std::array<T, 2>& b)
324 unsigned dx =
DisU(a[0], b[0]);
325 unsigned dy =
DisU(a[1], b[1]);
326 return dx * dx + dy * dy;
332template <IsSigned T,
size_t N>
336 length = sqrt(length);
341template <IsSigned T,
size_t N>
345 std::array<T, N> ret = v;
369constexpr double PI(
unsigned n,
unsigned d = 1)
371#ifdef __cpp_lib_concepts
372 return std::numbers::pi * n / d;
374 return 3.1415926535897932384626433832795 * n / d;
438 dnd_assert(align > 0);
464template<IsUn
signed T>
483 if ((0 == align) || (align & (align - 1)))
488 return ((x + (align - 1)) & ~(align - 1));
500 return r >= (d >> 1) ? q + 1 : q;
511 return (n + (d >> 1)) / d;
525constexpr float OffsetOrder(
float order,
int u = 1,
int v = 0,
int w = 0)
527 constexpr int NUM_ORDER_DT = 32;
528 float ret = order + (w + v * NUM_ORDER_DT + u * NUM_ORDER_DT * NUM_ORDER_DT)
529 * std::numeric_limits<float>::epsilon();
532 log_(
"order超出范围:{}", ret);
542 return a[0] * b[1] - a[1] * b[0];
548 a[1] * b[2] - b[1] * a[2],
549 a[2] * b[0] - b[2] * a[0],
550 a[0] * b[1] - b[0] * a[1]
557template<
typename T,
size_t N>
558constexpr T DotProduct(
const std::array<T, N>& a,
const std::array<T, N>& b)
561 for (
size_t i = 0; i <
N; ++i)
637 if (list_pos.size() < 3)
639 auto iter_last = list_pos.end();
641 for (
auto iter_0 = list_pos.begin(); iter_0 != iter_last;)
644 auto iter_0_next = iter_0;
648 for (
auto iter_1 = iter_last; iter_1 != iter_0_next; --iter_1)
650 if (func_pass(*iter_0, *iter_1))
652 for (
auto iter = iter_0_next; iter != iter_1;)
653 iter = list_pos.erase(iter);
703 return rect[0] <= rect[2]
704 && rect[1] <= rect[3];
718 && rect[2] <= size[0]
719 && rect[3] <= size[1];
729 return unsigned(xy[0]) < size[0]
730 && unsigned(xy[1]) < size[1];
739 return xy[0] >= rect[0]
758 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)
正交 画线算法
std::array< unsigned, N > CastUDefault(const std::array< T, N > &a)
有符号转无符号,直接转换,负数会直接转为大正数
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)
判断 矩形 在 大小 内,首先它必须是有效的矩形
std::array< unsigned, N > CastU(const std::array< T, N > &a, bool show_log=true)
有符号转无符号,并且判断负数
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 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)
返回容器最大值
void log_warn0(std::string_view str, const std::source_location location=std::source_location::current())
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, 3 > Triangle