Files
ViewDesignEngine/include/vde/ai/generative_design.h
T
茂之钳 69888621cd
CI / Build & Test (push) Failing after 30s
CI / Release Build (push) Failing after 38s
Build & Test / build-and-test (push) Has been cancelled
Build & Test / python-bindings (push) Has been cancelled
feat(v9): distributed computing + cloud-native + KBE + WASM + digital twin
v9.1 — Distributed Computing (超越 Parasolid):
- cluster_engine: ClusterManager, TaskScheduler(DAG+Kahn), 4 load-balance strategies
- distributed_boolean, distributed_marching_cubes, distributed_ray_tracing
- grpc_service: BrepOps/MeshOps/SdfOps RPC, streaming, TLS, connection pool
- ~750 lines

v9.2 — Cloud-Native + KBE + WASM + Digital Twin (34/34 tests passing):
- cloud_native: CloudSession, OperationalTransform, DeltaSync, Serverless, ObjectStorage
- knowledge_engine: CheckMate(13 rules), RuleEngine, DesignTable, GA+Adam optimizer
- vde_wasm: WasmBridge, WebWorkerPool, SharedArrayBuffer, IndexedDB
- dt_engine: DigitalTwin, MQTT/OPC-UA, RealTimeSync, PredictiveMaintenance(RUL)
- 3950 lines, 34 tests all passing

Pending: AI/ML integration (retrying)

18 files, ~4700 lines
2026-07-26 23:44:24 +08:00

298 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
/**
* @file generative_design.h
* @brief 生成式设计:拓扑优化 + 晶格结构生成 + GAN 3D 生成
*
* ## 主要功能
*
* | 功能 | 说明 |
* |-------------------------|------------------------------------------------|
* | topology_optimization | SIMP 方法密度场拓扑优化 → 等值面 → B-Rep |
* | lattice_generation | Gyroid/Diamond/BCC/FCC 晶格 + 变密度晶格 |
* | generative_adversarial_3d | 条件 GAN 3D 形状生成(载荷/约束 → 形状) |
*
* @ingroup ai
*/
#include "vde/brep/brep.h"
#include "vde/core/point.h"
#include "vde/core/aabb.h"
#include "vde/mesh/halfedge_mesh.h"
#include <vector>
#include <string>
#include <functional>
#include <memory>
namespace vde::ai {
using core::Point3D;
using core::Vector3D;
using core::AABB3D;
using mesh::HalfedgeMesh;
// ═══════════════════════════════════════════════════════════
// 拓扑优化 (Topology Optimization)
// ═══════════════════════════════════════════════════════════
/** @brief 载荷条件 */
struct LoadCondition {
Point3D position; ///< 载荷施加点
Vector3D force; ///< 力向量 (N)
double magnitude = 0.0; ///< 力的大小
};
/** @brief 固定约束 */
struct FixedConstraint {
Point3D position; ///< 约束点/面
bool fix_x = false; ///< 固定 X 方向
bool fix_y = false; ///< 固定 Y 方向
bool fix_z = false; ///< 固定 Z 方向
};
/** @brief 优化约束参数 */
struct OptimizationConstraints {
double volume_fraction = 0.3; ///< 目标体积分数 (0, 1],默认保留 30%
double penalization_power = 3.0; ///< SIMP 惩罚因子 p,典型值 3.0
double filter_radius = 1.5; ///< 密度过滤半径(单元数)
int max_iterations = 100; ///< 最大迭代次数
double convergence_tolerance = 0.01;///< 收敛容差
int grid_resolution = 64; ///< 密度场网格分辨率(每轴)
};
/** @brief 拓扑优化结果 */
struct TopologyOptimizationResult {
std::vector<double> density_field; ///< 体素密度值 [0,1],线性排列
HalfedgeMesh optimized_mesh; ///< 等值面提取的三角网格
brep::BrepModel reconstructed_body; ///< B-Rep 重建结果(简化)
int grid_resolution = 0; ///< 实际使用的网格分辨率
double final_volume_fraction = 0.0; ///< 最终体积分数
int iterations = 0; ///< 实际迭代次数
bool converged = false; ///< 是否收敛
std::string status_message; ///< 状态信息
};
/**
* @brief SIMP 方法拓扑优化
*
* Solid Isotropic Material with Penalization (SIMP) 是最常用的
* 连续体拓扑优化方法。对设计空间的每个体素赋予密度变量 ρ∈[0,1],
* 通过有限元分析计算柔度,迭代更新密度场。
*
* ## 算法概要
*
* 1. 初始化密度场为均匀 volume_fraction
* 2. 循环:
* a. 滤波密度场(密度过滤:加权平均邻域密度)
* b. 有限元分析(刚度矩阵 K(ρ) = ρ^p * K_e
* c. 计算目标函数和灵敏度(∂c/∂ρ = -p·ρ^(p-1)·u^T·K_e·u
* d. OC (Optimality Criteria) 更新密度
* e. 检查收敛
* 3. 等值面提取(Marching Cubes)→ 三角网格
* 4. B-Rep 曲面重建
*
* @param design_space 设计空间包围盒
* @param loads 载荷条件列表
* @param constraints 固定约束列表
* @param opt_constraints 优化约束参数
* @return 拓扑优化结果
*/
[[nodiscard]] TopologyOptimizationResult topology_optimization(
const AABB3D& design_space,
const std::vector<LoadCondition>& loads,
const std::vector<FixedConstraint>& constraints,
const OptimizationConstraints& opt_constraints = OptimizationConstraints{});
/**
* @brief 密度场 → 等值面提取
*
* 使用 Marching Cubes 算法从 3D 密度场中提取等值面。
*
* @param density_field 体素密度值 [0,1]
* @param resolution 网格分辨率
* @param bounds 包围盒
* @param iso_level 等值面阈值(默认 0.5
* @return 三角网格
*/
[[nodiscard]] HalfedgeMesh extract_isosurface(
const std::vector<double>& density_field,
int resolution,
const AABB3D& bounds,
double iso_level = 0.5);
/**
* @brief 三角网格 → B-Rep 重建(简化版)
*
* 将密度场等值面网格简化为 B-Rep 实体。
* 使用面片合并 + 边界识别策略。
*
* @param mesh 输入三角网格
* @param simplification_angle 面片合并的角度阈值(度)
* @return B-Rep 模型
*/
[[nodiscard]] brep::BrepModel mesh_to_brep_reconstruct(
const HalfedgeMesh& mesh, double simplification_angle = 15.0);
// ═══════════════════════════════════════════════════════════
// 晶格结构生成 (Lattice Generation)
// ═══════════════════════════════════════════════════════════
/** @brief 晶格单元类型 */
enum class LatticeCellType {
Gyroid, ///< 三周期极小曲面 Gyroid: sin(x)cos(y)+sin(y)cos(z)+sin(z)cos(x)=0
Diamond, ///< Diamond: sin(x)sin(y)sin(z)+sin(x)cos(y)cos(z)+cos(x)sin(y)cos(z)+cos(x)cos(y)sin(z)=0
BCC, ///< Body-Centered Cubic: 体心立方晶格
FCC, ///< Face-Centered Cubic: 面心立方晶格
Octet, ///< Octet Truss: 八面体桁架晶格
Cubic ///< 简单立方晶格
};
/** @brief 晶格参数 */
struct LatticeParams {
double cell_size = 1.0; ///< 晶格单元尺寸
double strut_thickness = 0.15; ///< 支柱厚度(相对于 cell_size 的比例)
double min_density = 0.1; ///< 最小密度(不可见区域的填充率)
bool variable_density = false; ///< 是否启用变密度晶格
std::vector<double> density_map; ///< 变密度映射(驱动密度场的体素值,用于应力驱动)
int density_map_resolution = 0; ///< 密度映射网格分辨率
bool smooth_transition = true; ///< 变密度区域间是否平滑过渡
double surface_thickness = 0.5; ///< 表面壳层厚度(晶格仅在内部生成)
};
/** @brief 晶格生成结果 */
struct LatticeGenerationResult {
HalfedgeMesh lattice_mesh; ///< 晶格三角网格
brep::BrepModel lattice_body; ///< 晶格 B-Rep 体
int cell_count = 0; ///< 晶格单元总数
double porosity = 0.0; ///< 孔隙率
bool success = false;
std::string error_message;
};
/**
* @brief 在 B-Rep 体内部生成晶格结构
*
* 将输入实体作为外部轮廓,在其内部空间填充晶格结构。
* 晶格由三周期极小曲面(TPMS)或桁架晶格定义。
*
* @param body 外部轮廓 B-Rep 体
* @param cell_type 晶格类型
* @param params 晶格参数
* @return 晶格生成结果
*/
[[nodiscard]] LatticeGenerationResult lattice_generation(
const brep::BrepModel& body,
LatticeCellType cell_type,
const LatticeParams& params = LatticeParams{});
/**
* @brief 评估特定晶格在点 (x,y,z) 处的 SDF 值
*
* @param x X 坐标
* @param y Y 坐标
* @param z Z 坐标
* @param cell_type 晶格类型
* @param cell_size 单元尺寸
* @param strut_thickness 支柱厚度
* @return SDF 值(内部为负,表面为 0,外部为正)
*/
[[nodiscard]] double lattice_sdf(
double x, double y, double z,
LatticeCellType cell_type,
double cell_size,
double strut_thickness);
/**
* @brief 生成变密度晶格的密度映射
*
* 基于应力场或用户指定的密度场,为每个晶格单元计算局部密度。
*
* @param body B-Rep 体
* @param loads 载荷条件(用于应力分析)
* @param cell_size 晶格单元尺寸
* @param stress_threshold 应力阈值
* @return (density_map, resolution) pair
*/
[[nodiscard]] std::pair<std::vector<double>, int> compute_variable_density_map(
const brep::BrepModel& body,
const std::vector<LoadCondition>& loads,
double cell_size,
double stress_threshold = 1.0);
// ═══════════════════════════════════════════════════════════
// GAN 3D 生成 (Generative Adversarial 3D)
// ═══════════════════════════════════════════════════════════
/** @brief GAN 生成条件 */
struct GANCondition {
std::vector<LoadCondition> loads; ///< 载荷条件
std::vector<FixedConstraint> constraints; ///< 固定约束
double target_volume = 0.0; ///< 目标体积 (0=自动)
double target_stiffness = 0.0; ///< 目标刚度 (0=自动)
std::string style_tag; ///< 风格标签(如 "aerospace", "automotive"
};
/** @brief GAN 生成结果 */
struct GANGenerationResult {
HalfedgeMesh generated_mesh; ///< 生成的三角网格
brep::BrepModel generated_body; ///< 生成的 B-Rep 体
double generation_time_ms = 0.0; ///< 生成耗时
double latent_z_score = 0.0; ///< 潜在空间得分
bool success = false;
std::string error_message;
std::vector<std::string> candidate_tags; ///< 可选的风格标签
};
/**
* @brief 基于条件 GAN 的 3D 形状生成
*
* 使用预训练的 cGAN 模型,根据工程条件(载荷、约束、目标体积等)
* 生成符合要求的 3D 形状。
*
* ## 算法流程
*
* 1. 编码条件(载荷/约束 → 条件向量 c)
* 2. 采样潜在向量 z ~ N(0, 1)
* 3. Generator(z, c) → 3D SDF 体素网格
* 4. Marching Cubes → 三角网格
* 5. B-Rep 重建
*
* @param conditions 工程条件
* @param model_path 预训练 GAN 模型路径(ONNX 格式)
* @param resolution 输出分辨率
* @return 生成结果
*/
[[nodiscard]] GANGenerationResult generative_adversarial_3d(
const GANCondition& conditions,
const std::string& model_path = "models/gan3d_generator.onnx",
int resolution = 64);
/**
* @brief 编码为条件向量
*
* 将工程条件(载荷、约束等)编码为网络可用的固定长度向量。
*
* @param conditions 工程条件
* @return 条件向量
*/
[[nodiscard]] std::vector<double> encode_conditions(const GANCondition& conditions);
/**
* @brief 从潜在空间插值生成形状序列
*
* 在两个潜在向量之间线性插值,生成形状过渡序列。
*
* @param z0 起始潜在向量
* @param z1 终止潜在向量
* @param steps 插值步数
* @param conditions 共享条件
* @param model_path GAN 模型路径
* @return 形状序列
*/
[[nodiscard]] std::vector<GANGenerationResult> latent_interpolation(
const std::vector<double>& z0,
const std::vector<double>& z1,
int steps,
const GANCondition& conditions,
const std::string& model_path = "models/gan3d_generator.onnx");
} // namespace vde::ai