/** * @file dl_math.h * @brief 数学相关的通用算法 * * * @version 1.0 * @author lveyou * @date 21-08-15 */ #pragma once #include #include #include #include "dl_type.h" #include "io/dl_log.h" #include "base/dl_concept.h" #include "dl_algorithm.h" #include "dl_range.h" #include "system/dl_platform.h" #if (DL_PLATFORM == DL_PLATFORM_WIN32) #include #endif //! 类似std::numbers的常数 namespace dl::Numbers { constexpr Float EARTH_fADIUS = 6371.393_f;//!< 地球半径km constexpr Float GRAVITY = 9.8_f;//!< 地球重力加速度(赤道最小) } //! 类似numeric头文件的数值操作 namespace dl::Numeric { //! 返回的是最小正数(假设有符号) template void Min(std::array& container) { for (size_t i = 0; i < N; ++i) { container[i] = (std::numeric_limits::min)(); } } template void Max(std::array& container) { for (size_t i = 0; i < N; ++i) { container[i] = (std::numeric_limits::max)(); } } } /** * @brief 2维装箱算法 * @param[in] size 箱子大小 * @param map_size 传入矩形的数组,失败返回剩余的矩形 * @param[out] map_fet 返回装入的矩形信息 */ //void RectBinPack(const Size& size, list<>& map_fect, map& map_fet) //{ // //0按面积排序 //} namespace dl::Math { /** * @brief 限制数值到 闭区间 * @note 注意无符号误区 * @note 请使用Every::Clamp */ template [[deprecated]] constexpr T Limit(const T& v, const T& v_min, const T& v_max) { if (v < v_min) return v_min; if (v_max < v) return v_max; return v; } /** * @brief 判断数值是否越界(前闭后开) */ template constexpr bool IsOutRange(T x, T a, T b) { return x < a || b < x; } /** * @brief 返回x的n次方 * @param[in] x 底数,n64 * @param[in] n 指数,n8[0, 255] */ constexpr uint64_t Pow(uint64_t x, uint8_t n) { uint64_t ret = 1; for (uint8_t i = 0; i < n; ++i) { ret *= x; } return ret; } constexpr uint32_t Pow32(uint32_t x, uint8_t n) { uint32_t ret = 1; for (uint8_t i = 0; i < n; ++i) { ret *= x; } return ret; } /** * @brief 2的整数次幂列表 */ constexpr uint64_t ARRAY_POW2[] = { Pow(2,0),Pow(2,1),Pow(2,2),Pow(2,3),Pow(2,4),Pow(2,5),Pow(2,6),Pow(2,7), Pow(2,8),Pow(2,9),Pow(2,10),Pow(2,11),Pow(2,12),Pow(2,13),Pow(2,14),Pow(2,15), Pow(2,16),Pow(2,17),Pow(2,18),Pow(2,19),Pow(2,20),Pow(2,21),Pow(2,22),Pow(2,23), Pow(2,24),Pow(2,25),Pow(2,26),Pow(2,27),Pow(2,28),Pow(2,29),Pow(2,30),Pow(2,31), Pow(2,32),Pow(2,33),Pow(2,34),Pow(2,35),Pow(2,36),Pow(2,37),Pow(2,38),Pow(2,39), Pow(2,40),Pow(2,41),Pow(2,42),Pow(2,43),Pow(2,44),Pow(2,45),Pow(2,46),Pow(2,47), Pow(2,48),Pow(2,49),Pow(2,50),Pow(2,51),Pow(2,52),Pow(2,53),Pow(2,54),Pow(2,55), Pow(2,56),Pow(2,57),Pow(2,58),Pow(2,59),Pow(2,60),Pow(2,61),Pow(2,62),Pow(2,63) }; constexpr uint32_t ARRAY_POW2_32[] = { Pow32(2,0),Pow32(2,1),Pow32(2,2),Pow32(2,3),Pow32(2,4),Pow32(2,5),Pow32(2,6),Pow32(2,7), Pow32(2,8),Pow32(2,9),Pow32(2,10),Pow32(2,11),Pow32(2,12),Pow32(2,13),Pow32(2,14),Pow32(2,15), Pow32(2,16),Pow32(2,17),Pow32(2,18),Pow32(2,19),Pow32(2,20),Pow32(2,21),Pow32(2,22),Pow32(2,23), Pow32(2,24),Pow32(2,25),Pow32(2,26),Pow32(2,27),Pow32(2,28),Pow32(2,29),Pow32(2,30),Pow32(2,31) }; /** * @brief 向上取2的幂次 * * @note 常量表达式 */ #define DND_ARRAY_POW_LINE(index) if (x <= ARRAY_POW2[index])return ARRAY_POW2[index] constexpr uint64_t CeilToPow2(uint64_t&& x) { DND_ARRAY_POW_LINE(0); DND_ARRAY_POW_LINE(1); DND_ARRAY_POW_LINE(2); DND_ARRAY_POW_LINE(3); DND_ARRAY_POW_LINE(4); DND_ARRAY_POW_LINE(5); DND_ARRAY_POW_LINE(6); DND_ARRAY_POW_LINE(7); DND_ARRAY_POW_LINE(8); DND_ARRAY_POW_LINE(9); DND_ARRAY_POW_LINE(10); DND_ARRAY_POW_LINE(11); DND_ARRAY_POW_LINE(12); DND_ARRAY_POW_LINE(13); DND_ARRAY_POW_LINE(14); DND_ARRAY_POW_LINE(15); DND_ARRAY_POW_LINE(16); DND_ARRAY_POW_LINE(17); DND_ARRAY_POW_LINE(18); DND_ARRAY_POW_LINE(19); DND_ARRAY_POW_LINE(20); DND_ARRAY_POW_LINE(21); DND_ARRAY_POW_LINE(22); DND_ARRAY_POW_LINE(23); DND_ARRAY_POW_LINE(24); DND_ARRAY_POW_LINE(25); DND_ARRAY_POW_LINE(26); DND_ARRAY_POW_LINE(27); DND_ARRAY_POW_LINE(28); DND_ARRAY_POW_LINE(29); DND_ARRAY_POW_LINE(30); DND_ARRAY_POW_LINE(31); DND_ARRAY_POW_LINE(32); DND_ARRAY_POW_LINE(33); DND_ARRAY_POW_LINE(34); DND_ARRAY_POW_LINE(35); DND_ARRAY_POW_LINE(36); DND_ARRAY_POW_LINE(37); DND_ARRAY_POW_LINE(38); DND_ARRAY_POW_LINE(39); DND_ARRAY_POW_LINE(40); DND_ARRAY_POW_LINE(41); DND_ARRAY_POW_LINE(42); DND_ARRAY_POW_LINE(43); DND_ARRAY_POW_LINE(44); DND_ARRAY_POW_LINE(45); DND_ARRAY_POW_LINE(46); DND_ARRAY_POW_LINE(47); DND_ARRAY_POW_LINE(48); DND_ARRAY_POW_LINE(49); DND_ARRAY_POW_LINE(50); DND_ARRAY_POW_LINE(51); DND_ARRAY_POW_LINE(52); DND_ARRAY_POW_LINE(53); DND_ARRAY_POW_LINE(54); DND_ARRAY_POW_LINE(55); DND_ARRAY_POW_LINE(56); DND_ARRAY_POW_LINE(57); DND_ARRAY_POW_LINE(58); DND_ARRAY_POW_LINE(59); DND_ARRAY_POW_LINE(60); DND_ARRAY_POW_LINE(61); DND_ARRAY_POW_LINE(62); DND_ARRAY_POW_LINE(63); log("数值过大,超过2^63:" + std::to_string(x)); return 0; } #define DND_ARRAY_POW_LINE_32(index) if (x <= ARRAY_POW2_32[index])return ARRAY_POW2_32[index] constexpr uint32_t CeilToPow2(uint32_t&& x) { DND_ARRAY_POW_LINE_32(0); DND_ARRAY_POW_LINE_32(1); DND_ARRAY_POW_LINE_32(2); DND_ARRAY_POW_LINE_32(3); DND_ARRAY_POW_LINE_32(4); DND_ARRAY_POW_LINE_32(5); DND_ARRAY_POW_LINE_32(6); DND_ARRAY_POW_LINE_32(7); DND_ARRAY_POW_LINE_32(8); DND_ARRAY_POW_LINE_32(9); DND_ARRAY_POW_LINE_32(10); DND_ARRAY_POW_LINE_32(11); DND_ARRAY_POW_LINE_32(12); DND_ARRAY_POW_LINE_32(13); DND_ARRAY_POW_LINE_32(14); DND_ARRAY_POW_LINE_32(15); DND_ARRAY_POW_LINE_32(16); DND_ARRAY_POW_LINE_32(17); DND_ARRAY_POW_LINE_32(18); DND_ARRAY_POW_LINE_32(19); DND_ARRAY_POW_LINE_32(20); DND_ARRAY_POW_LINE_32(21); DND_ARRAY_POW_LINE_32(22); DND_ARRAY_POW_LINE_32(23); DND_ARRAY_POW_LINE_32(24); DND_ARRAY_POW_LINE_32(25); DND_ARRAY_POW_LINE_32(26); DND_ARRAY_POW_LINE_32(27); DND_ARRAY_POW_LINE_32(28); DND_ARRAY_POW_LINE_32(29); DND_ARRAY_POW_LINE_32(30); DND_ARRAY_POW_LINE_32(31); log("数值过大,超过2^31:" + std::to_string(x)); return 0; } /** * @brief 向上取2的幂次 * * @note bit_ceil很难触发常量表达式,但在计算变量时最快 */ template inline T CeilToPow2(const T& x) { #if (DL_PLATFORM == DL_PLATFORM_WIN32) return std::bit_ceil(x); #else return CeilToPow2(x); #endif } /** * @brief 返回平方根的倒数 * * @note 经测试,此方法并比1.0f / sqrt快那么一点 * @note 类型转换也需要时间,如果是非内联函数也会变慢许多。 */ inline float RSqrt(float x) { long i; float x2, y; const float threehalfs = 1.5f; x2 = x * 0.5f; y = x; i = *(long*)&y; i = 0x5f3759df - (i >> 1); y = *(float*)&i; y = y * (threehalfs - (x2 * y * y)); return y; } /** * @brief 向量单位化,精度低速度快 */ template void NormalizeFast(std::array& v) { float length = 0; for (size_t i = 0; i < N; ++i) { length += (v[i] * v[i]); } length = RSqrt(length); for (size_t i = 0; i < N; ++i) { v[i] *= length; } } //-------------------------------有符号距离----------------------------- /** * @brief 返回 向量 距离的平方 * @note 有符号版本 */ template T DisPow2(const std::array& a) { return a[0] * a[0] + a[1] * a[1]; } template T DisPow2(const std::array& a) { return a[0] * a[0] + a[1] * a[1] + a[2] * a[2]; } template T DisPow2(const std::array& a) { return a[0] * a[0] + a[1] * a[1] + a[2] * a[2] + a[3] * a[3]; } /** * @brief 返回 距离的平方 * @note 有符号版本 */ template T DisPow2(const std::array& a, const std::array& b) { return DisPow2(b - a); } //---------------------------------无符号距离---------------------------------------- /** * @brief 返回无符号类型的差值 */ template T DisU(T a, T b) { return a > b ? a - b : b - a; } /** * @brief 无符号 加 有符号,防止小于0 */ template [[nodiscard]] T0 AddU(T0 a, T1 b) { if (b < 0 && (T0)(-b) > a) return 0; else return a + b; } /** * @brief 返回 距离的平方 * @note 无符号版本 */ template T DisPow2U(const std::array& a, const std::array& b) { unsigned dx = DisU(a[0], b[0]); unsigned dy = DisU(a[1], b[1]); return dx * dx + dy * dy; } /** * @brief 向量单位化 */ template void Normalize(std::array& v) { T length = DisPow2(v); length = sqrt(length); v /= length; } template [[nodiscard]] std::array Normalize(const std::array& v) { std::array ret = v; Normalize(ret); return ret; } /** * @brief 返回圆形范围的节点,考虑越界 * @param[in] rad 传入0返回1个节点、传入1返回4个节点、传入2返回13个节点,依次类推 * @todo 暂未测试 */ std::vector getCircle(const Size& size, const PointU& xy, unsigned rad); /** * @brief 返回圆形范围的节点,不考虑越界 * @param[in] rad 传入0返回1个节点、传入1返回4个节点、传入2返回13个节点,依次类推 */ std::vector getCircle(const Point& xy, unsigned rad); /** * @brief 返回PI的分数 * @param[in] n 分子 * @param[in] d 分母 */ constexpr double PI(unsigned n, unsigned d = 1) { #ifdef __cpp_lib_concepts return std::numbers::pi * n / d; #else return 3.1415926535897932384626433832795 * n / d; #endif } /** * @brief RGP资源下标转方向 */ /*dnd::Direction IndexRpg2Direction(size_t index) { if (index == 0) return Direction::DOWN; if (index == 1) return Direction::LEFT; if (index == 2) return Direction::RIGHT; if (index == 3) return Direction::UP; debug_warn("错误的下标![0, 3]"); return Direction::DOWN; }*/ /** * @brief 向量 转 掩码 */ //DirectionMask::Var Dir2Mask(const Vector2& dir) //{ // if (Every::Zero(dir)) // return DirectionMask::NONE; // double r = atan2(dir[1], dir[0]);//返回(-pi, pi],从正x轴向正y轴旋转 // if (r < 0)//变换到(0, 2pi],x轴不变 // r += (std::numbers::pi * 2.0); // r += (std::numbers::pi / 8.0);//四舍五入 加pi/8 // size_t index = r / (std::numbers::pi / 4.0);//2pi分为8个方向为pi/4 // dnd_assert(index >= 0 && index <= 7); // //x为0 // if (index == 0) // return DirectionMask::RIGHT; // else if (index == 1) // return DirectionMask::RIGHT_DOWN; // else if (index == 2) // return DirectionMask::DOWN; // else if (index == 3) // return DirectionMask::LEFT_DOWN; // else if (index == 4) // return DirectionMask::LEFT; // else if (index == 5) // return DirectionMask::LEFT_UP; // else if (index == 6) // return DirectionMask::UP; // else if (index == 7) // return DirectionMask::RIGHT_UP; // return DirectionMask::NONE; //} /** * @brief 对齐align的倍数,并且x考虑负数 * @note 注意,现在如果x为0,则返回align * @note 负数向右对齐 */ template constexpr T AlignF(T x, T align) { dnd_assert(align > 0); if (x < 0) { T ret = -align; while (x < ret) { ret -= align; } return ret + align; } else { T ret = align; while (x > ret) { ret += align; } return ret; } } /** * @brief 对齐align的倍数 * @note 注意,现在如果x为0,则返回align */ template constexpr T Align(T x, T align) { assert(align != 0); T ret = align; while (x > ret) { ret += align; } return ret; } /** * @brief 对齐align的倍数 * @param[in] align 必须是2的幂,否则返回0 */ constexpr unsigned AlignPow2(unsigned x, unsigned align) { if ((0 == align) || (align & (align - 1))) { return 0; } return ((x + (align - 1)) & ~(align - 1)); } /** * @brief 无符号除法,返回四舍五入结果 * @param[in] n 分子 * @param[in] d 分母 */ constexpr unsigned div_found(unsigned n, unsigned d) { unsigned q = n / d; unsigned r = n % d; return r >= (d >> 1) ? q + 1 : q; } /** * @brief 无符号除法,返回四舍五入结果 * @note 分子n过大会溢出,但效率更高 * @param[in] n 分子 * @param[in] d 分母 */ constexpr unsigned div_found_fast(unsigned n, unsigned d) { return (n + (d >> 1)) / d; } /** * @brief 偏移order * @param[in] order 原本order * @param[in] u 偏移大小,用户使用 * @param[in] v 偏移大小,多个组使用 * @param[in] w 偏移大小,单个元素内使用 * * @note 正数使order变大,更远离屏幕 * @note 如果偏移出 [0, 1]的范围,则限制到范围,并输出警告 * @note 在一组内,优先使用负数靠近屏幕 */ constexpr float OffsetOrder(float order, int u = 1, int v = 0, int w = 0) { constexpr int NUM_ORDER_DT = 32; float ret = order + (w + v * NUM_ORDER_DT + u * NUM_ORDER_DT * NUM_ORDER_DT) * std::numeric_limits::epsilon(); if (!is_saturate(ret)) { log_("order超出范围:{}", ret); return saturate(ret); } return ret; } /** * @brief 叉乘 */ constexpr Float CrossProduct(const Position2& a, const Position2& b) { return a[0] * b[1] - a[1] * b[0]; } constexpr Position3 CrossProduct(const Position3& a, const Position3& b) { return { a[1] * b[2] - b[1] * a[2], a[2] * b[0] - b[2] * a[0], a[0] * b[1] - b[0] * a[1] }; } /** * @brief 点乘 */ template constexpr T DotProduct(const std::array& a, const std::array& b) { T ret = 0; for (size_t i = 0; i < N; ++i) ret += a[i] * b[i]; return ret; } /** * @brief 球面线性插值 * @note 普通线性插值存在于std::lerp */ Position4 Slerp(const Position4& q0, const Position4& q1, const Float& t); /** * @brief 纹理区域转uv */ //constexpr void Rect2Uv(const Size& tex_size, const RectU& rect, floatx2* uv) //{ // floatx4 ltrb; // ltrb[LTRB::LEFT] = float(rect[LTRB::LEFT]) / tex_size[0]; // ltrb[LTRB::TOP] = float(rect[LTRB::TOP]) / tex_size[1]; // ltrb[LTRB::RIGHT] = float(rect[LTRB::RIGHT]) / tex_size[0]; // ltrb[LTRB::BUTTOM] = float(rect[LTRB::BUTTOM]) / tex_size[1]; // // // uv[Corner::LT] = { ltrb[LTRB::LEFT], ltrb[LTRB::TOP] }; // uv[Corner::RT] = { ltrb[LTRB::RIGHT], ltrb[LTRB::TOP] }; // uv[Corner::LB] = { ltrb[LTRB::LEFT], ltrb[LTRB::BUTTOM] }; // uv[Corner::RB] = { ltrb[LTRB::RIGHT], ltrb[LTRB::BUTTOM] }; //} /** * @brief 对角线距离(注意不是真的对角线,而是取宽高较大者) */ template constexpr auto DistanceDiagonal(const C& p0, const C& p1) { C dt = p1 - p0; Every::Abs(dt); return Select::Max(dt); } /** * @brief 线性插值 画线算法 */ std::vector DrawLineLerp(const Point& p0, const Point& p1); /** * @brief bresenham 画线算法 */ std::vector DrawLineBresenham(const Point& p0, const Point& p1); /** * @brief 正交 画线算法 * @note 只能竖着或横着走 */ std::vector DrawLineOrth(const Point& p0, const Point& p1); /** * @brief 覆盖 画线算法 * @note 直线经过的网格都包含(会穿过对角线) */ std::vector DrawLineCover(const Point& p0, const Point& p1); /** * @brief 路径合并(网格路径) * @note 方向相同且相邻的节点合并(最终路径不变) */ void MergePath(std::list& list_pos); /** * @brief 路径合并(网格路径) * @note 两点之间没有障碍物,则删除之间的(路径会发生变化) */ template void MergePathDirect(std::list& list_pos, F func_pass) { if (list_pos.size() < 3) return; auto iter_last = list_pos.end();//最后一个点总是有效的 --iter_last; for (auto iter_0 = list_pos.begin(); iter_0 != iter_last;) {//直到前一个点到 iter_last(最后一个点) //iter_0的下一个点 auto iter_0_next = iter_0; ++iter_0_next; //是否删除了节点 bool b_del = false; for (auto iter_1 = iter_last; iter_1 != iter_0_next; --iter_1) {//后一个点 从后往前遍历,直到iter_0_next if (func_pass(*iter_0, *iter_1)) {//如果可通行 删除中间的节点 for (auto iter = iter_0_next; iter != iter_1;) iter = list_pos.erase(iter); iter_0 = iter_1;//后一个点此时成为起点 b_del = true; break; } } if (!b_del) ++iter_0; } } ////-------------------------------隐藏含义获取-------------------------------- ///** //* @brief 计算返回 矩形 右下角坐标y 减去 左上角坐标y //* @param[in] rect 矩形 //*/ //template //constexpr T GetHeight(const std::array& rect) //{ // return rect[3] - rect[1]; //} // ///** //* @brief 计算返回 矩形 右下角坐标x 减去 左上角坐标x //* @param[in] rect 矩形 //*/ //template //constexpr T GetWidth(const std::array& rect) //{ // return rect[2] - rect[0]; //} // ///** //* @brief 计算返回 矩形 宽和高 //* @param[in] rect 矩形 //*/ //template //constexpr std::array GetWidthAndHeight(const std::array& rect) //{ // return { GetWidth(rect), GetHeight(rect) }; //} /** * @brief 判断 矩形 是否有效 * @param[in] rect 矩形 * @note 空矩形也是有效的 */ template constexpr bool IsValidRect(const std::array& rect) { return rect[0] <= rect[2] && rect[1] <= rect[3]; } /** * @brief 判断 矩形 在 大小 内,首先它必须是有效的矩形 * @param[in] rect 矩形 */ template constexpr bool IsRectInSize(const std::array& rect, const Size& size) { if (!IsValidRect(rect)) return false; return rect[0] >= 0 && rect[1] >= 0 && rect[2] <= size[0] && rect[3] <= size[1]; } /** * @brief 判断 点是否在大小内 */ constexpr bool IsXyInSize(const Point& xy, const Size& size) { if (Each::Less(xy, {})) return false; return unsigned(xy[0]) < size[0] && unsigned(xy[1]) < size[1]; } /** * @brief 判断 点是否在矩形内 * @param[in] rect 矩形 */ constexpr bool IsXyInRect(const Point& xy, const Rect& rect) { return xy[0] >= rect[0] && xy[1] >= rect[1] && xy[0] < rect[2] && xy[1] < rect[3]; } /** * @brief 判断点是否在三角形内 * @param[in] dot 点 * @param[in] triangle 三角形 */ constexpr bool IsDotInTriangle(const Position& dot, const Triangle& triangle) { Position pa = triangle[0] - dot; Position pb = triangle[1] - dot; Position pc = triangle[2] - dot; double t1 = CrossProduct(pa, pb); double t2 = CrossProduct(pb, pc); double t3 = CrossProduct(pc, pa); return t1 * t2 >= 0 && t1 * t3 >= 0 && t2 * t3 >= 0; } /** * @brief 判断点是否在凸四边形内 * @param[in] dot 点 * @param[in] quad 四边形(凸) */ constexpr bool IsDotInQuad(const Position& dot, const Quad& quad) { return Math::IsDotInTriangle(dot, { quad[0], quad[1], quad[2] }) || Math::IsDotInTriangle(dot, { quad[0], quad[2], quad[3] }); } }