| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | #nullable enable |
| | | 5 | | |
| | | 6 | | using System; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | |
| | | 10 | | namespace Lattice.Math |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// LUT (查找表) 管理器:运行时加载数学函数查找表 |
| | | 14 | | /// <para>支持文件系统加载和嵌入资源加载</para> |
| | | 15 | | /// </summary> |
| | | 16 | | public static class FPLut |
| | | 17 | | { |
| | | 18 | | #region 常量 |
| | | 19 | | |
| | | 20 | | /// <summary>LUT 精度:16 位小数</summary> |
| | | 21 | | public const int Precision = 16; |
| | | 22 | | |
| | | 23 | | /// <summary>π 的原始值</summary> |
| | | 24 | | public const long PI = 205887L; |
| | | 25 | | |
| | | 26 | | /// <summary>2π 的原始值</summary> |
| | | 27 | | public const long PiTimes2 = 411775L; |
| | | 28 | | |
| | | 29 | | /// <summary>π/2 的原始值</summary> |
| | | 30 | | public const long PiOver2 = 102944L; |
| | | 31 | | |
| | | 32 | | /// <summary>1 的原始值</summary> |
| | | 33 | | public const long ONE = 65536L; |
| | | 34 | | |
| | | 35 | | #endregion |
| | | 36 | | |
| | | 37 | | #region LUT 表(延迟加载) |
| | | 38 | | |
| | | 39 | | /// <summary>Sin/Cos 查找表</summary> |
| | | 40 | | private static long[]? _sinCosTable; |
| | | 41 | | |
| | | 42 | | /// <summary>Tan 查找表</summary> |
| | | 43 | | private static long[]? _tanTable; |
| | | 44 | | |
| | | 45 | | /// <summary>Asin 查找表</summary> |
| | | 46 | | private static long[]? _asinTable; |
| | | 47 | | |
| | | 48 | | /// <summary>Acos 查找表</summary> |
| | | 49 | | private static long[]? _acosTable; |
| | | 50 | | |
| | | 51 | | /// <summary>Atan 查找表</summary> |
| | | 52 | | private static long[]? _atanTable; |
| | | 53 | | |
| | | 54 | | /// <summary>Sqrt 查找表</summary> |
| | | 55 | | private static int[]? _sqrtTable; |
| | | 56 | | |
| | | 57 | | #endregion |
| | | 58 | | |
| | | 59 | | #region 公共访问属性 |
| | | 60 | | |
| | | 61 | | /// <summary>LUT 是否已加载</summary> |
| | 7 | 62 | | public static bool IsLoaded => _sinCosTable != null && _sqrtTable != null; |
| | | 63 | | |
| | | 64 | | /// <summary>LUT 版本</summary> |
| | 5 | 65 | | public static string Version { get; private set; } = "not loaded"; |
| | | 66 | | |
| | | 67 | | /// <summary>Sin/Cos 查找表(自动加载检查)</summary> |
| | 8196 | 68 | | public static long[] SinCosTable => _sinCosTable ?? throw CreateNotLoadedException(); |
| | | 69 | | |
| | | 70 | | /// <summary>Tan 查找表</summary> |
| | 0 | 71 | | public static long[] TanTable => _tanTable ?? throw CreateNotLoadedException(); |
| | | 72 | | |
| | | 73 | | /// <summary>Asin 查找表</summary> |
| | 0 | 74 | | public static long[] AsinTable => _asinTable ?? throw CreateNotLoadedException(); |
| | | 75 | | |
| | | 76 | | /// <summary>Acos 查找表</summary> |
| | 2 | 77 | | public static long[] AcosTable => _acosTable ?? throw CreateNotLoadedException(); |
| | | 78 | | |
| | | 79 | | /// <summary>Atan 查找表</summary> |
| | 1 | 80 | | public static long[] AtanTable => _atanTable ?? throw CreateNotLoadedException(); |
| | | 81 | | |
| | | 82 | | /// <summary>Sqrt 查找表</summary> |
| | 2001 | 83 | | public static int[] SqrtTable => _sqrtTable ?? throw CreateNotLoadedException(); |
| | | 84 | | |
| | | 85 | | #endregion |
| | | 86 | | |
| | | 87 | | #region 初始化方法 |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// 从文件系统加载 LUT |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="lutDirectory">LUT 文件目录路径</param> |
| | | 93 | | /// <remarks> |
| | | 94 | | /// 需要以下文件: |
| | | 95 | | /// - FPSinCos.bin |
| | | 96 | | /// - FPTan.bin |
| | | 97 | | /// - FPAsin.bin |
| | | 98 | | /// - FPAcos.bin |
| | | 99 | | /// - FPAtan.bin |
| | | 100 | | /// - FPSqrt.bin |
| | | 101 | | /// - version.txt(可选) |
| | | 102 | | /// </remarks> |
| | | 103 | | public static void Initialize(string lutDirectory) |
| | | 104 | | { |
| | 0 | 105 | | if (IsLoaded) return; |
| | | 106 | | |
| | 0 | 107 | | if (!Directory.Exists(lutDirectory)) |
| | 0 | 108 | | throw new DirectoryNotFoundException($"LUT directory not found: {lutDirectory}"); |
| | | 109 | | |
| | | 110 | | // 加载版本文件(如果存在) |
| | 0 | 111 | | string versionPath = Path.Combine(lutDirectory, "version.txt"); |
| | 0 | 112 | | if (File.Exists(versionPath)) |
| | | 113 | | { |
| | 0 | 114 | | Version = File.ReadAllText(versionPath).Trim(); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | // 加载各 LUT 表 |
| | 0 | 118 | | _sinCosTable = LoadTable<long>(Path.Combine(lutDirectory, "FPSinCos.bin")); |
| | 0 | 119 | | _tanTable = LoadTable<long>(Path.Combine(lutDirectory, "FPTan.bin")); |
| | 0 | 120 | | _asinTable = LoadTable<long>(Path.Combine(lutDirectory, "FPAsin.bin")); |
| | 0 | 121 | | _acosTable = LoadTable<long>(Path.Combine(lutDirectory, "FPAcos.bin")); |
| | 0 | 122 | | _atanTable = LoadTable<long>(Path.Combine(lutDirectory, "FPAtan.bin")); |
| | 0 | 123 | | _sqrtTable = LoadTable<int>(Path.Combine(lutDirectory, "FPSqrt.bin")); |
| | | 124 | | |
| | 0 | 125 | | ValidateTables(); |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// 从嵌入资源加载 LUT(适用于 Godot 导出版本) |
| | | 130 | | /// </summary> |
| | | 131 | | public static void InitializeFromEmbedded() |
| | | 132 | | { |
| | 0 | 133 | | if (IsLoaded) return; |
| | | 134 | | |
| | 0 | 135 | | var assembly = typeof(FPLut).Assembly; |
| | | 136 | | |
| | 0 | 137 | | _sinCosTable = LoadFromResource<long>(assembly, "Lattice.LUT.FPSinCos.bin"); |
| | 0 | 138 | | _tanTable = LoadFromResource<long>(assembly, "Lattice.LUT.FPTan.bin"); |
| | 0 | 139 | | _asinTable = LoadFromResource<long>(assembly, "Lattice.LUT.FPAsin.bin"); |
| | 0 | 140 | | _acosTable = LoadFromResource<long>(assembly, "Lattice.LUT.FPAcos.bin"); |
| | 0 | 141 | | _atanTable = LoadFromResource<long>(assembly, "Lattice.LUT.FPAtan.bin"); |
| | 0 | 142 | | _sqrtTable = LoadFromResource<int>(assembly, "Lattice.LUT.FPSqrt.bin"); |
| | | 143 | | |
| | 0 | 144 | | Version = "embedded"; |
| | 0 | 145 | | ValidateTables(); |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | /// <summary> |
| | | 149 | | /// 使用预编译的内嵌 LUT(当前实现,兼容性) |
| | | 150 | | /// </summary> |
| | | 151 | | public static void InitializeBuiltIn() |
| | | 152 | | { |
| | 11 | 153 | | if (IsLoaded) return; |
| | | 154 | | |
| | 1 | 155 | | _sinCosTable = FPSinCosLut.SinAccurate; |
| | 1 | 156 | | _acosTable = FPAcosLut.Table; |
| | 1 | 157 | | _atanTable = FPAtanLut.Table; |
| | 1 | 158 | | _sqrtTable = FPSqrtLut.Table; |
| | | 159 | | |
| | | 160 | | // Tan 和 Asin 使用近似计算或从 SinCos 派生 |
| | 1 | 161 | | _tanTable = GenerateTanTable(); |
| | 1 | 162 | | _asinTable = GenerateAsinTable(); |
| | | 163 | | |
| | 1 | 164 | | Version = "builtin"; |
| | 1 | 165 | | } |
| | | 166 | | |
| | | 167 | | #endregion |
| | | 168 | | |
| | | 169 | | #region 辅助方法 |
| | | 170 | | |
| | | 171 | | private static T[] LoadTable<T>(string path) where T : unmanaged |
| | | 172 | | { |
| | 0 | 173 | | if (!File.Exists(path)) |
| | 0 | 174 | | throw new FileNotFoundException($"LUT file not found: {path}"); |
| | | 175 | | |
| | 0 | 176 | | byte[] bytes = File.ReadAllBytes(path); |
| | 0 | 177 | | int elementSize = Unsafe.SizeOf<T>(); |
| | 0 | 178 | | int elementCount = bytes.Length / elementSize; |
| | | 179 | | |
| | 0 | 180 | | T[] result = new T[elementCount]; |
| | 0 | 181 | | Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length); |
| | | 182 | | |
| | 0 | 183 | | return result; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | private static T[] LoadFromResource<T>(System.Reflection.Assembly assembly, string resourceName) where T : unman |
| | | 187 | | { |
| | 0 | 188 | | using var stream = assembly.GetManifestResourceStream(resourceName); |
| | 0 | 189 | | if (stream == null) |
| | 0 | 190 | | throw new InvalidOperationException($"Embedded resource not found: {resourceName}"); |
| | | 191 | | |
| | 0 | 192 | | byte[] bytes = new byte[stream.Length]; |
| | 0 | 193 | | stream.Read(bytes, 0, bytes.Length); |
| | | 194 | | |
| | 0 | 195 | | int elementSize = Unsafe.SizeOf<T>(); |
| | 0 | 196 | | int elementCount = bytes.Length / elementSize; |
| | | 197 | | |
| | 0 | 198 | | T[] result = new T[elementCount]; |
| | 0 | 199 | | Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length); |
| | | 200 | | |
| | 0 | 201 | | return result; |
| | 0 | 202 | | } |
| | | 203 | | |
| | | 204 | | private static void ValidateTables() |
| | | 205 | | { |
| | 0 | 206 | | if (_sinCosTable?.Length < 4096) |
| | 0 | 207 | | throw new InvalidDataException($"SinCos LUT too small: {_sinCosTable?.Length}"); |
| | | 208 | | |
| | 0 | 209 | | if (_sqrtTable?.Length < 65536) |
| | 0 | 210 | | throw new InvalidDataException($"Sqrt LUT too small: {_sqrtTable?.Length}"); |
| | | 211 | | |
| | 0 | 212 | | if (_acosTable?.Length < 65536) |
| | 0 | 213 | | throw new InvalidDataException($"Acos LUT too small: {_acosTable?.Length}"); |
| | 0 | 214 | | } |
| | | 215 | | |
| | | 216 | | private static InvalidOperationException CreateNotLoadedException() |
| | | 217 | | { |
| | 1 | 218 | | return new InvalidOperationException( |
| | 1 | 219 | | "LUT not loaded. Call FPLut.Initialize(path) or FPLut.InitializeFromEmbedded() first."); |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | private static long[] GenerateTanTable() |
| | | 223 | | { |
| | | 224 | | // 简化实现:从 SinCos 表派生 |
| | 1 | 225 | | var table = new long[4096]; |
| | 8194 | 226 | | for (int i = 0; i < 4096; i++) |
| | | 227 | | { |
| | 4096 | 228 | | double angle = i * System.Math.PI * 2 / 4096; |
| | | 229 | | #pragma warning disable RS0030 // LUT 生成是确定性的,允许使用 Math 函数 |
| | 4096 | 230 | | table[i] = (long)(System.Math.Tan(angle) * ONE); |
| | | 231 | | #pragma warning restore RS0030 |
| | | 232 | | } |
| | 1 | 233 | | return table; |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | private static long[] GenerateAsinTable() |
| | | 237 | | { |
| | | 238 | | // 简化实现:线性近似 |
| | 1 | 239 | | var table = new long[65537]; |
| | 131076 | 240 | | for (int i = 0; i <= 65536; i++) |
| | | 241 | | { |
| | 65537 | 242 | | double x = (i - 32768) / 32768.0; // [-1, 1] |
| | 65537 | 243 | | if (x < -1) x = -1; |
| | 65537 | 244 | | if (x > 1) x = 1; |
| | | 245 | | #pragma warning disable RS0030 // LUT 生成是确定性的,允许使用 Math 函数 |
| | 65537 | 246 | | table[i] = (long)(System.Math.Asin(x) * ONE); |
| | | 247 | | #pragma warning restore RS0030 |
| | | 248 | | } |
| | 1 | 249 | | return table; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | #endregion |
| | | 253 | | } |
| | | 254 | | } |