< Summary

Information
Class: Lattice.Math.FPLut
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/Math/FPLut.cs
Line coverage
35%
Covered lines: 29
Uncovered lines: 53
Coverable lines: 82
Total lines: 254
Line coverage: 35.3%
Branch coverage
27%
Covered branches: 15
Total branches: 54
Branch coverage: 27.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_IsLoaded()100%22100%
get_Version()100%11100%
get_SinCosTable()100%22100%
get_TanTable()0%620%
get_AsinTable()0%620%
get_AcosTable()50%22100%
get_AtanTable()50%22100%
get_SqrtTable()50%22100%
Initialize(...)0%4260%
InitializeFromEmbedded()0%620%
InitializeBuiltIn()100%22100%
LoadTable(...)0%620%
LoadFromResource(...)0%620%
ValidateTables()0%342180%
CreateNotLoadedException()100%11100%
GenerateTanTable()100%22100%
GenerateAsinTable()66.66%66100%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/Math/FPLut.cs

#LineLine coverage
 1// Copyright (c) 2026 Sideline Authors. All rights reserved.
 2// Licensed under GPL-3.0.
 3
 4#nullable enable
 5
 6using System;
 7using System.IO;
 8using System.Runtime.CompilerServices;
 9
 10namespace 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>
 762        public static bool IsLoaded => _sinCosTable != null && _sqrtTable != null;
 63
 64        /// <summary>LUT 版本</summary>
 565        public static string Version { get; private set; } = "not loaded";
 66
 67        /// <summary>Sin/Cos 查找表(自动加载检查)</summary>
 819668        public static long[] SinCosTable => _sinCosTable ?? throw CreateNotLoadedException();
 69
 70        /// <summary>Tan 查找表</summary>
 071        public static long[] TanTable => _tanTable ?? throw CreateNotLoadedException();
 72
 73        /// <summary>Asin 查找表</summary>
 074        public static long[] AsinTable => _asinTable ?? throw CreateNotLoadedException();
 75
 76        /// <summary>Acos 查找表</summary>
 277        public static long[] AcosTable => _acosTable ?? throw CreateNotLoadedException();
 78
 79        /// <summary>Atan 查找表</summary>
 180        public static long[] AtanTable => _atanTable ?? throw CreateNotLoadedException();
 81
 82        /// <summary>Sqrt 查找表</summary>
 200183        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        {
 0105            if (IsLoaded) return;
 106
 0107            if (!Directory.Exists(lutDirectory))
 0108                throw new DirectoryNotFoundException($"LUT directory not found: {lutDirectory}");
 109
 110            // 加载版本文件(如果存在)
 0111            string versionPath = Path.Combine(lutDirectory, "version.txt");
 0112            if (File.Exists(versionPath))
 113            {
 0114                Version = File.ReadAllText(versionPath).Trim();
 115            }
 116
 117            // 加载各 LUT 表
 0118            _sinCosTable = LoadTable<long>(Path.Combine(lutDirectory, "FPSinCos.bin"));
 0119            _tanTable = LoadTable<long>(Path.Combine(lutDirectory, "FPTan.bin"));
 0120            _asinTable = LoadTable<long>(Path.Combine(lutDirectory, "FPAsin.bin"));
 0121            _acosTable = LoadTable<long>(Path.Combine(lutDirectory, "FPAcos.bin"));
 0122            _atanTable = LoadTable<long>(Path.Combine(lutDirectory, "FPAtan.bin"));
 0123            _sqrtTable = LoadTable<int>(Path.Combine(lutDirectory, "FPSqrt.bin"));
 124
 0125            ValidateTables();
 0126        }
 127
 128        /// <summary>
 129        /// 从嵌入资源加载 LUT(适用于 Godot 导出版本)
 130        /// </summary>
 131        public static void InitializeFromEmbedded()
 132        {
 0133            if (IsLoaded) return;
 134
 0135            var assembly = typeof(FPLut).Assembly;
 136
 0137            _sinCosTable = LoadFromResource<long>(assembly, "Lattice.LUT.FPSinCos.bin");
 0138            _tanTable = LoadFromResource<long>(assembly, "Lattice.LUT.FPTan.bin");
 0139            _asinTable = LoadFromResource<long>(assembly, "Lattice.LUT.FPAsin.bin");
 0140            _acosTable = LoadFromResource<long>(assembly, "Lattice.LUT.FPAcos.bin");
 0141            _atanTable = LoadFromResource<long>(assembly, "Lattice.LUT.FPAtan.bin");
 0142            _sqrtTable = LoadFromResource<int>(assembly, "Lattice.LUT.FPSqrt.bin");
 143
 0144            Version = "embedded";
 0145            ValidateTables();
 0146        }
 147
 148        /// <summary>
 149        /// 使用预编译的内嵌 LUT(当前实现,兼容性)
 150        /// </summary>
 151        public static void InitializeBuiltIn()
 152        {
 11153            if (IsLoaded) return;
 154
 1155            _sinCosTable = FPSinCosLut.SinAccurate;
 1156            _acosTable = FPAcosLut.Table;
 1157            _atanTable = FPAtanLut.Table;
 1158            _sqrtTable = FPSqrtLut.Table;
 159
 160            // Tan 和 Asin 使用近似计算或从 SinCos 派生
 1161            _tanTable = GenerateTanTable();
 1162            _asinTable = GenerateAsinTable();
 163
 1164            Version = "builtin";
 1165        }
 166
 167        #endregion
 168
 169        #region 辅助方法
 170
 171        private static T[] LoadTable<T>(string path) where T : unmanaged
 172        {
 0173            if (!File.Exists(path))
 0174                throw new FileNotFoundException($"LUT file not found: {path}");
 175
 0176            byte[] bytes = File.ReadAllBytes(path);
 0177            int elementSize = Unsafe.SizeOf<T>();
 0178            int elementCount = bytes.Length / elementSize;
 179
 0180            T[] result = new T[elementCount];
 0181            Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
 182
 0183            return result;
 184        }
 185
 186        private static T[] LoadFromResource<T>(System.Reflection.Assembly assembly, string resourceName) where T : unman
 187        {
 0188            using var stream = assembly.GetManifestResourceStream(resourceName);
 0189            if (stream == null)
 0190                throw new InvalidOperationException($"Embedded resource not found: {resourceName}");
 191
 0192            byte[] bytes = new byte[stream.Length];
 0193            stream.Read(bytes, 0, bytes.Length);
 194
 0195            int elementSize = Unsafe.SizeOf<T>();
 0196            int elementCount = bytes.Length / elementSize;
 197
 0198            T[] result = new T[elementCount];
 0199            Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
 200
 0201            return result;
 0202        }
 203
 204        private static void ValidateTables()
 205        {
 0206            if (_sinCosTable?.Length < 4096)
 0207                throw new InvalidDataException($"SinCos LUT too small: {_sinCosTable?.Length}");
 208
 0209            if (_sqrtTable?.Length < 65536)
 0210                throw new InvalidDataException($"Sqrt LUT too small: {_sqrtTable?.Length}");
 211
 0212            if (_acosTable?.Length < 65536)
 0213                throw new InvalidDataException($"Acos LUT too small: {_acosTable?.Length}");
 0214        }
 215
 216        private static InvalidOperationException CreateNotLoadedException()
 217        {
 1218            return new InvalidOperationException(
 1219                "LUT not loaded. Call FPLut.Initialize(path) or FPLut.InitializeFromEmbedded() first.");
 220        }
 221
 222        private static long[] GenerateTanTable()
 223        {
 224            // 简化实现:从 SinCos 表派生
 1225            var table = new long[4096];
 8194226            for (int i = 0; i < 4096; i++)
 227            {
 4096228                double angle = i * System.Math.PI * 2 / 4096;
 229#pragma warning disable RS0030 // LUT 生成是确定性的,允许使用 Math 函数
 4096230                table[i] = (long)(System.Math.Tan(angle) * ONE);
 231#pragma warning restore RS0030
 232            }
 1233            return table;
 234        }
 235
 236        private static long[] GenerateAsinTable()
 237        {
 238            // 简化实现:线性近似
 1239            var table = new long[65537];
 131076240            for (int i = 0; i <= 65536; i++)
 241            {
 65537242                double x = (i - 32768) / 32768.0; // [-1, 1]
 65537243                if (x < -1) x = -1;
 65537244                if (x > 1) x = 1;
 245#pragma warning disable RS0030 // LUT 生成是确定性的,允许使用 Math 函数
 65537246                table[i] = (long)(System.Math.Asin(x) * ONE);
 247#pragma warning restore RS0030
 248            }
 1249            return table;
 250        }
 251
 252        #endregion
 253    }
 254}