24#if (DL_PLATFORM == DL_PLATFORM_WIN32)
41 template<
typename T,
size_t N>
42 void Min(std::array<T, N>& container)
44 for (
size_t i = 0; i <
N; ++i)
46 container[i] = (std::numeric_limits<T>::min)();
49 template<
typename T,
size_t N>
50 void Max(std::array<T, N>& container)
52 for (
size_t i = 0; i <
N; ++i)
54 container[i] = (std::numeric_limits<T>::max)();
77constexpr T Limit(
const T& v,
const T& v_min,
const T& v_max)
92 return x < a || b < x;
105 for (
T i = 0; i < n; ++i)
111constexpr uint64_t
Pow64(uint64_t x, uint8_t n)
114 for (uint8_t i = 0; i < n; ++i)
120constexpr uint32_t
Pow32(uint32_t x, uint8_t n)
123 for (uint8_t i = 0; i < n; ++i)
134 Pow64(2,0),
Pow64(2,1),
Pow64(2,2),
Pow64(2,3),
Pow64(2,4),
Pow64(2,5),
Pow64(2,6),
Pow64(2,7),
135 Pow64(2,8),
Pow64(2,9),
Pow64(2,10),
Pow64(2,11),
Pow64(2,12),
Pow64(2,13),
Pow64(2,14),
Pow64(2,15),
136 Pow64(2,16),
Pow64(2,17),
Pow64(2,18),
Pow64(2,19),
Pow64(2,20),
Pow64(2,21),
Pow64(2,22),
Pow64(2,23),
137 Pow64(2,24),
Pow64(2,25),
Pow64(2,26),
Pow64(2,27),
Pow64(2,28),
Pow64(2,29),
Pow64(2,30),
Pow64(2,31),
138 Pow64(2,32),
Pow64(2,33),
Pow64(2,34),
Pow64(2,35),
Pow64(2,36),
Pow64(2,37),
Pow64(2,38),
Pow64(2,39),
139 Pow64(2,40),
Pow64(2,41),
Pow64(2,42),
Pow64(2,43),
Pow64(2,44),
Pow64(2,45),
Pow64(2,46),
Pow64(2,47),
140 Pow64(2,48),
Pow64(2,49),
Pow64(2,50),
Pow64(2,51),
Pow64(2,52),
Pow64(2,53),
Pow64(2,54),
Pow64(2,55),
141 Pow64(2,56),
Pow64(2,57),
Pow64(2,58),
Pow64(2,59),
Pow64(2,60),
Pow64(2,61),
Pow64(2,62),
Pow64(2,63) };
143 Pow32(2,0),
Pow32(2,1),
Pow32(2,2),
Pow32(2,3),
Pow32(2,4),
Pow32(2,5),
Pow32(2,6),
Pow32(2,7),
144 Pow32(2,8),
Pow32(2,9),
Pow32(2,10),
Pow32(2,11),
Pow32(2,12),
Pow32(2,13),
Pow32(2,14),
Pow32(2,15),
145 Pow32(2,16),
Pow32(2,17),
Pow32(2,18),
Pow32(2,19),
Pow32(2,20),
Pow32(2,21),
Pow32(2,22),
Pow32(2,23),
146 Pow32(2,24),
Pow32(2,25),
Pow32(2,26),
Pow32(2,27),
Pow32(2,28),
Pow32(2,29),
Pow32(2,30),
Pow32(2,31) };
153#define DND_ARRAY_POW_LINE(index) if (x <= ARRAY_POW2[index])return ARRAY_POW2[index]
165 log(
"数值过大,超过2^63:" + std::to_string(x));
168#define DND_ARRAY_POW_LINE_32(index) if (x <= ARRAY_POW2_32[index])return ARRAY_POW2_32[index]
176 log(
"数值过大,超过2^31:" + std::to_string(x));
187#if (DL_PLATFORM == DL_PLATFORM_WIN32)
188 return std::bit_ceil(x);
205 const float threehalfs = 1.5f;
210 i = 0x5f3759df - (i >> 1);
212 y = y * (threehalfs - (x2 * y * y));
224 for (
size_t i = 0; i <
N; ++i)
226 length += (v[i] * v[i]);
229 length =
RSqrt(length);
230 for (
size_t i = 0; i <
N; ++i)
244 return a[0] * a[0] + a[1] * a[1];
250 return a[0] * a[0] + a[1] * a[1] + a[2] * a[2];
256 return a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3];
272template <IsUn
signed T>
275 return a > b ? a - b : 0;
281template <IsUn
signed T>
284 return a > b ? a - b : b - a;
290template <IsUn
signed T0, IsSigned T1>
294 if (b < 0 && (T0)(-b) > a)
305template <IsUn
signed T>
308 unsigned dx =
DisU(a[0], b[0]);
309 unsigned dy =
DisU(a[1], b[1]);
310 return dx * dx + dy * dy;
316template <IsSigned T,
size_t N>
320 length = sqrt(length);
325template <IsSigned T,
size_t N>
329 std::array<T, N> ret = v;
353constexpr double PI(
unsigned n = 1,
unsigned d = 1)
356 return std::numbers::pi * n / d;
358 return 3.1415926535897932384626433832795 * n / d;
422 dnd_assert(align > 0);
448template<IsUn
signed T>
467 if ((0 == align) || (align & (align - 1)))
472 return ((x + (align - 1)) & ~(align - 1));
484 return r >= (d >> 1) ? q + 1 : q;
495 return (n + (d >> 1)) / d;
509constexpr float OffsetOrder(
float order,
int u = 1,
int v = 0,
int w = 0)
511 constexpr int NUM_ORDER_DT = 100;
512 float ret = order + (w + v * NUM_ORDER_DT + u * NUM_ORDER_DT * NUM_ORDER_DT)
513 * std::numeric_limits<float>::epsilon();
516 log_(
"order超出范围:{}", ret);
526 return a[0] * b[1] - a[1] * b[0];
532 a[1] * b[2] - b[1] * a[2],
533 a[2] * b[0] - b[2] * a[0],
534 a[0] * b[1] - b[0] * a[1]
541template<
typename T,
size_t N>
542constexpr T DotProduct(
const std::array<T, N>& a,
const std::array<T, N>& b)
545 for (
size_t i = 0; i <
N; ++i)
621 if (list_pos.size() < 3)
623 auto iter_last = list_pos.end();
625 for (
auto iter_0 = list_pos.begin(); iter_0 != iter_last;)
628 auto iter_0_next = iter_0;
632 for (
auto iter_1 = iter_last; iter_1 != iter_0_next; --iter_1)
634 if (func_pass(*iter_0, *iter_1))
636 for (
auto iter = iter_0_next; iter != iter_1;)
637 iter = list_pos.erase(iter);
656 return rect[0] <= rect[2]
657 && rect[1] <= rect[3];
666 return rect[0] == rect[2]
667 && rect[1] == rect[3];
681 && rect[2] <= size[0]
682 && rect[3] <= size[1];
692 return unsigned(xy[0]) < size[0]
693 && unsigned(xy[1]) < size[1];
702 return xy[0] >= rect[0]
721 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 SubU(T a, T b)
无符号类型相减,负数转为0
std::vector< Point > DrawLineOrth(const Point &p0, const Point &p1)
正交 画线算法
constexpr bool IsXyInRect(const Point &xy, const Rect &rect)
判断 点是否在矩形内
constexpr uint64_t Pow64(uint64_t x, uint8_t n)
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 Position CreatePositionByRatioGoldW(Float w)
按宽生成黄金比例大小
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 bool IsRectValid(const std::array< T, 4 > &rect)
判断 矩形 是否有效
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)
constexpr T Pow(T x, T n)
返回x的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)
球面线性插值
constexpr double PI(unsigned n=1, unsigned d=1)
返回PI的分数
void MergePath(std::list< Point > &list_pos)
路径合并(网格路径)
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 bool IsRectEmpty(const std::array< T, 4 > &rect)
判断 矩形 是否空
T DistancePow2(const std::array< T, 2 > &a)
返回 向量 距离的平方
constexpr uint64_t ARRAY_POW2[]
2的整数次幂列表
std::vector< Point > DrawLineBresenham(const Point &p0, const Point &p1)
bresenham 画线算法
constexpr auto DistanceDiagonal(const C &p0, const C &p1)
纹理区域转uv
std::vector< PointU > getCircle(const Size &size, const PointU &xy, unsigned rad)
返回圆形范围的节点,考虑越界
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)
T0 AddU(T0 a, T1 b)
无符号 加 有符号,防止小于0
constexpr bool IsDotInTriangle(const Position &dot, const Triangle &triangle)
判断点是否在三角形内
T DistancePow2U(const std::array< T, 2 > &a, const std::array< T, 2 > &b)
返回 距离的平方
void NormalizeFast(std::array< float, N > &v)
向量单位化,精度低速度快
constexpr Float EULER
自然常数
constexpr Float EARTH_fADIUS
地球半径km
constexpr Float GRAVITY
地球重力加速度(赤道最小)
constexpr Float RATIO_GOLD
黄金比例
void Max(std::array< T, N > &container)
void Min(std::array< T, N > &container)
返回的是最小正数(假设有符号)
constexpr auto Max(const C &container)
返回容器最大值
std::array< Float, 3 > Position3
std::array< Float, 4 > Position4
std::array< Position, 4 > Quad
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< Position, 3 > Triangle
constexpr T saturate(const T &x)
限制数值到 [0, 1]
std::array< Float, 2 > Position2