ViewDesignEngine  3.1.0
高性能 CAD 计算几何引擎
sdf_optimize.h
浏览该文件的文档.
1 #pragma once
31 #include "vde/sdf/sdf_tree.h"
32 #include "vde/core/point.h"
33 #include <vector>
34 #include <functional>
35 
36 namespace vde::sdf {
37 
38 using core::Point3D;
39 using core::Vector3D;
40 
41 // ────────────────────────────────────────────────
42 // Shape Optimization
43 // ────────────────────────────────────────────────
44 
52  double final_loss;
53  int iterations;
54  bool converged;
55  std::vector<double> loss_history;
56 };
57 
65 using LossFn = std::function<double(const std::function<double(const Point3D&)>&)>;
66 
67 // ── Shape Fitting ───────────────────────────────
68 
100  const SdfNodePtr& initial,
101  const std::vector<Point3D>& target_points,
102  double learning_rate = 0.01,
103  int max_iterations = 100);
104 
120  const SdfNodePtr& initial,
121  const std::vector<Point3D>& surface_points,
122  double learning_rate = 0.01,
123  int max_iterations = 100);
124 
148  const SdfNodePtr& source,
149  const std::function<double(const Point3D&)>& target_sdf,
150  const Point3D& bmin, const Point3D& bmax,
151  int grid_resolution = 16,
152  double learning_rate = 0.01,
153  int max_iterations = 100);
154 
155 // ── Collision Avoidance ─────────────────────────
156 
177 [[nodiscard]] bool resolve_collision(
178  SdfNodePtr& shape_a, SdfNodePtr& shape_b,
179  double step_size = 0.1,
180  int max_iterations = 50);
181 
197 [[nodiscard]] double penetration_depth(
198  const SdfNodePtr& shape_a,
199  const SdfNodePtr& shape_b,
200  const Point3D& bmin, const Point3D& bmax,
201  int samples = 1000);
202 
203 // ── Reachability / Accessibility ─────────────────
204 
221 [[nodiscard]] double accessibility(
222  const SdfNodePtr& shape,
223  const Point3D& p, // surface point
224  const Vector3D& direction, // approach direction
225  int hemisphere_samples = 64);
226 
242  const SdfNodePtr& shape,
243  const Vector3D& approach_dir,
244  const Point3D& bmin, const Point3D& bmax,
245  int grid_res = 32);
246 
247 // ── Symmetry Detection ──────────────────────────
248 
265 [[nodiscard]] double symmetry_score(
266  const SdfNodePtr& shape,
267  const Point3D& bmin, const Point3D& bmax,
268  const Vector3D& plane_normal,
269  double plane_offset = 0.0,
270  int samples = 1000);
271 
276  double score;
278  double offset;
279 };
280 
296  const SdfNodePtr& shape,
297  const Point3D& bmin, const Point3D& bmax,
298  int samples = 1000);
299 
300 // ── SDF Volume Computation ──────────────────────
301 
321 [[nodiscard]] double estimate_volume(
322  const SdfNodePtr& shape,
323  const Point3D& bmin, const Point3D& bmax,
324  int samples = 10000);
325 
341 [[nodiscard]] Point3D center_of_mass(
342  const SdfNodePtr& shape,
343  const Point3D& bmin, const Point3D& bmax,
344  int samples = 10000);
345 
346 // ── Parameter Collection ────────────────────────
347 
354 struct ParamRef {
355  double* value;
356  std::string name;
357 };
358 
380 [[nodiscard]] std::vector<ParamRef> collect_params(SdfNodePtr& root);
381 
405 [[nodiscard]] std::vector<double> numerical_gradient(
406  const std::vector<ParamRef>& params,
407  const std::function<double()>& loss_fn,
408  double eps = 1e-6);
409 
410 // ── Gradient Descent Utility ────────────────────
411 
429 public:
434  explicit GradientDescent(double lr) : lr_(lr) {}
435 
448  double step(std::vector<double>& params,
449  const std::function<double(const std::vector<double>&,
450  std::vector<double>&)>& grad_fn);
451 
453  void set_learning_rate(double lr) { lr_ = lr; }
454 
456  [[nodiscard]] double learning_rate() const { return lr_; }
457 
459  [[nodiscard]] int iteration() const { return iteration_; }
460 
461 private:
462  double lr_;
463  int iteration_ = 0;
464 };
465 
466 } // namespace vde::sdf
固定学习率的简单梯度下降优化器
Definition: sdf_optimize.h:428
int iteration() const
获取当前迭代次数
Definition: sdf_optimize.h:459
GradientDescent(double lr)
构造优化器
Definition: sdf_optimize.h:434
void set_learning_rate(double lr)
设置学习率
Definition: sdf_optimize.h:453
double learning_rate() const
获取学习率
Definition: sdf_optimize.h:456
double step(std::vector< double > &params, const std::function< double(const std::vector< double > &, std::vector< double > &)> &grad_fn)
执行单步梯度下降
foundation::Vector3D Vector3D
双精度三维向量(重新导出)
Definition: point.h:49
foundation::Point3D Point3D
双精度三维点(重新导出)
Definition: point.h:31
SymmetryResult find_symmetry_plane(const SdfNodePtr &shape, const Point3D &bmin, const Point3D &bmax, int samples=1000)
自动找到形状的最佳反射对称平面
Point3D find_accessible_point(const SdfNodePtr &shape, const Vector3D &approach_dir, const Point3D &bmin, const Point3D &bmax, int grid_res=32)
找到给定接近方向下具有最大可及性的表面点
double accessibility(const SdfNodePtr &shape, const Point3D &p, const Vector3D &direction, int hemisphere_samples=64)
计算形状表面上点的可及性分数
OptimizeResult fit_surface_to_points(const SdfNodePtr &initial, const std::vector< Point3D > &surface_points, double learning_rate=0.01, int max_iterations=100)
将形状表面移动到目标采样点
Point3D center_of_mass(const SdfNodePtr &shape, const Point3D &bmin, const Point3D &bmax, int samples=10000)
通过蒙地卡罗法估算形状的质心
std::function< double(const std::function< double(const Point3D &)> &)> LossFn
损失函数类型
Definition: sdf_optimize.h:65
OptimizeResult fit_to_point_cloud(const SdfNodePtr &initial, const std::vector< Point3D > &target_points, double learning_rate=0.01, int max_iterations=100)
将参数化形状拟合到目标点云
OptimizeResult fit_to_sdf(const SdfNodePtr &source, const std::function< double(const Point3D &)> &target_sdf, const Point3D &bmin, const Point3D &bmax, int grid_resolution=16, double learning_rate=0.01, int max_iterations=100)
拟合一个 SDF 形状以匹配另一个目标 SDF
bool resolve_collision(SdfNodePtr &shape_a, SdfNodePtr &shape_b, double step_size=0.1, int max_iterations=50)
通过平移解决两个形状之间的穿透
double symmetry_score(const SdfNodePtr &shape, const Point3D &bmin, const Point3D &bmax, const Vector3D &plane_normal, double plane_offset=0.0, int samples=1000)
检测形状在给定方向上的反射对称性
double penetration_depth(const SdfNodePtr &shape_a, const SdfNodePtr &shape_b, const Point3D &bmin, const Point3D &bmax, int samples=1000)
估算两个形状之间的最小穿透距离
std::shared_ptr< SdfNode > SdfNodePtr
Definition: sdf_tree.h:48
double estimate_volume(const SdfNodePtr &shape, const Point3D &bmin, const Point3D &bmax, int samples=10000)
通过蒙地卡罗法估算隐式形状的体积
std::vector< ParamRef > collect_params(SdfNodePtr &root)
从叶节点收集所有可变数值参数
std::vector< double > numerical_gradient(const std::vector< ParamRef > &params, const std::function< double()> &loss_fn, double eps=1e-6)
计算标量损失对收集到的参数的数值梯度
SDF 表达式树结构
bool converged
是否收敛
Definition: sdf_optimize.h:54
int iterations
实际迭代次数
Definition: sdf_optimize.h:53
SdfNodePtr optimized_shape
优化后的 SDF 树(参数已更新)
Definition: sdf_optimize.h:51
std::vector< double > loss_history
每次迭代的损失记录
Definition: sdf_optimize.h:55
double final_loss
最终损失值
Definition: sdf_optimize.h:52
SDF 树中可变参数的引用
Definition: sdf_optimize.h:354
std::string name
参数名称(调试用)
Definition: sdf_optimize.h:356
double * value
指向参数值的可写指针
Definition: sdf_optimize.h:355
对称检测结果
Definition: sdf_optimize.h:275
double score
对称性分数 [0, 1]
Definition: sdf_optimize.h:276
Vector3D normal
最佳对称平面的法向量
Definition: sdf_optimize.h:277
double offset
最佳对称平面的偏移量
Definition: sdf_optimize.h:278