#pragma once #ifndef GRID_CACHE_IO_H #define GRID_CACHE_IO_H #include #include #include #include "pch.h" class GridCacheIO { public: // 保存网格(写入 header + gridOutput2) // 注意:会自动“清洗”一些不稳定的 n 字段,避免写入垃圾值 static bool saveGrid(const std::string& filename, HX_NWTM_GRID_OUTPUT2 grid, // 这里用值传递:方便内部清洗 const HX_NWTM_GRID_INPUT& gridInput); // 读取网格(读取后会检查 sceneKey 是否匹配) // 注意:读取后也会做一次“清洗”,保证 n 字段稳定 static bool loadGrid(const std::string& filename, HX_NWTM_GRID_OUTPUT2& grid, const HX_NWTM_GRID_INPUT& expectedGridInput); // 只检查 header 的 sceneKey 是否匹配(不读完整网格) static bool isCacheMatch(const std::string& filename, const HX_NWTM_GRID_INPUT& expectedGridInput); static bool fileExists(const std::string& filename); private: // ---------------- 文件头 ---------------- struct GridCacheHeader { unsigned int magic; // 'GCHX' unsigned int version; // 格式版本 unsigned int reserved; // 保留 unsigned long long sceneKey; // 串场景指纹 unsigned int nCells; // 快速检查 }; enum { MAGIC = 0x58484347 }; // 'GCHX' little-endian enum { VERSION = 1 }; private: // 计算串场景 key static unsigned long long computeSceneKey64(const HX_NWTM_GRID_INPUT& in); // FNV-1a 64 static unsigned long long fnv1a64_init(); static void fnv1a64_update(unsigned long long& h, const void* data, size_t len); // hash 辅助 static void hashInt(unsigned long long& h, int v); static void hashUInt(unsigned long long& h, unsigned int v); static void hashDouble(unsigned long long& h, double v); static void hashVec1D(unsigned long long& h, const dVec1& v); static void hashIVec1D(unsigned long long& h, const iVec1& v); static void hashVec2D(unsigned long long& h, const dVec2& v); static void hashIVec2D(unsigned long long& h, const iVec2& v); static void hashVec3D(unsigned long long& h, const dVec3& v); // IO 基元 static bool writeUInt(std::ofstream& fs, unsigned int v); static bool writeULL(std::ofstream& fs, unsigned long long v); static bool writeInt(std::ofstream& fs, int v); static bool writeDouble(std::ofstream& fs, double v); static bool readUInt(std::ifstream& fs, unsigned int& v); static bool readULL(std::ifstream& fs, unsigned long long& v); static bool readInt(std::ifstream& fs, int& v); static bool readDouble(std::ifstream& fs, double& v); static bool writeSafeSize(std::ofstream& fs, size_t sz); static bool readSafeSize(std::ifstream& fs, size_t& sz); static bool writeVec1D(std::ofstream& fs, const dVec1& v); static bool writeIVec1D(std::ofstream& fs, const iVec1& v); static bool writeVec2D(std::ofstream& fs, const dVec2& v); static bool writeIVec2D(std::ofstream& fs, const iVec2& v); static bool writeVec3D(std::ofstream& fs, const dVec3& v); static bool readVec1D(std::ifstream& fs, dVec1& v); static bool readIVec1D(std::ifstream& fs, iVec1& v); static bool readVec2D(std::ifstream& fs, dVec2& v); static bool readIVec2D(std::ifstream& fs, iVec2& v); static bool readVec3D(std::ifstream& fs, dVec3& v); // gridOutput2 序列化 static bool writeGridOutput2(std::ofstream& fs, const HX_NWTM_GRID_OUTPUT2& g); static bool readGridOutput2(std::ifstream& fs, HX_NWTM_GRID_OUTPUT2& g); static bool readHeader(std::ifstream& fs, GridCacheHeader& hdr); static bool writeHeader(std::ofstream& fs, const GridCacheHeader& hdr); // “清洗”不稳定字段:用 vector size 统一覆盖 n,避免 DLL 未初始化导致的随机值 static void sanitizeGridOutput2(HX_NWTM_GRID_OUTPUT2& g); }; #endif // GRID_CACHE_IO_H