Files
hm 4ad1690b8b
Build & Test / build-and-test (push) Canceled after 0s
VDE Build & Test / build-and-test (push) Canceled after 0s
Build & Test / python-bindings (push) Canceled after 0s
VDE-404: 补齐材料库 by_category / register_custom / databases / JSON 互操作
在既有 vde_pdm/material_library 上补齐缺失提案 API:
- MaterialDatabase::by_category(静态按类别过滤,按名升序)
- MaterialDatabase::register_custom(写入 custom 目录,可被 get/search/by_category 检索)
- MaterialDatabase::databases(列出 native/custom 目录名,add 默认 native、register_custom 标 custom)
- to_json / from_json(单材料扁平 JSON,17 位精度 + 字符串转义;from_json 容缺键)

(MaterialLibraryTools 为应用层名、databases 为正文词)

test_material_library 23/23 全绿
2026-10-05 20:05:11 +08:00

99 lines
3.5 KiB
C++

#pragma once
/**
* @file material_library.h
* @brief Material library with native database, search, and properties
*
* VDE-404: native_mat_db + material_search + material_properties
*
* @ingroup core
*/
#include <string>
#include <vector>
#include <unordered_map>
#include <optional>
namespace vde::pdm {
/// Material property set
struct MaterialProperties {
std::string name;
std::string category; ///< "metal", "plastic", "composite", "ceramic", "wood"
double density_kgm3 = 7850.0; ///< Density (kg/m³)
double youngs_modulus_gpa = 200.0;
double poisson_ratio = 0.3;
double yield_strength_mpa = 250.0;
double tensile_strength_mpa = 400.0;
double thermal_expansion = 12e-6; ///< /K
double thermal_conductivity = 50.0; ///< W/(m·K)
double specific_heat = 460.0; ///< J/(kg·K)
std::string standard; ///< "ASTM A36", "EN 10025", "JIS G3101", "GB/T 700", "GOST 380"
std::string appearance; ///< "Matte gray", "Polished silver", etc.
bool is_isotropic = true;
};
/// Native material database
class MaterialDatabase {
public:
static MaterialDatabase& instance();
/// Add a material
void add(const MaterialProperties& mat);
/// Get material by name
[[nodiscard]] std::optional<MaterialProperties> get(const std::string& name) const;
/// Get all materials
[[nodiscard]] std::vector<MaterialProperties> all() const;
/// Get material count
[[nodiscard]] size_t count() const;
/// Clear database
void clear();
// ── VDE-404 补齐 ──
/// 按类别筛选(分类名大小写敏感,如 "metal"/"plastic"/…);按名称升序
[[nodiscard]] static std::vector<MaterialProperties> by_category(
const std::string& category);
/// 注册自定义材料:写入 "custom" 目录,可被 get / material_search /
/// by_category 检索;同名覆盖。
static void register_custom(const MaterialProperties& mat);
/// 列出当前已知的材料目录(catalog)名("native" / "custom"),升序
[[nodiscard]] static std::vector<std::string> databases();
private:
MaterialDatabase();
std::unordered_map<std::string, MaterialProperties> materials_;
std::unordered_map<std::string, std::string> catalog_; ///< 材料名 -> 目录名
};
/// Search materials by criteria
struct MaterialSearchQuery {
std::string category; ///< Filter by category (empty = any)
double min_density = 0.0;
double max_density = 0.0; ///< 0 = no limit
double min_yield_strength = 0.0;
double max_yield_strength = 0.0;
std::string standard; ///< Filter by standard
std::string name_pattern; ///< Substring match
int max_results = 50;
};
/// Search materials in database
[[nodiscard]] std::vector<MaterialProperties> material_search(const MaterialSearchQuery& query);
/// Initialize the native material database with standard materials
void init_native_material_db();
/// Get material properties by name (convenience)
[[nodiscard]] std::optional<MaterialProperties> material_properties(const std::string& name);
// ── VDE-404: 材料 JSON 序列化(扁平对象,键与字段同名)──
/// 序列化单个材料为 JSON 对象
[[nodiscard]] std::string to_json(const MaterialProperties& mat);
/// 反序列化单个材料(缺失键保留默认值;解析失败返回 name 为空的对象)
[[nodiscard]] MaterialProperties from_json(const std::string& json);
} // namespace vde::pdm