ViewDesignEngine  3.1.0
高性能 CAD 计算几何引擎
sdf_tree.h
浏览该文件的文档.
1 #pragma once
35 #include "vde/sdf/sdf_primitives.h"
36 #include "vde/sdf/sdf_operations.h"
37 #include "vde/core/point.h"
38 #include <memory>
39 #include <string>
40 #include <vector>
41 
42 namespace vde::sdf {
43 using core::Point3D;
44 using core::Vector3D;
45 
46 // Forward declaration
47 class SdfNode;
48 using SdfNodePtr = std::shared_ptr<SdfNode>;
49 
60 enum class SdfOp : uint8_t {
61  // Primitives
64  // Boolean CSG
67  // Modifiers
68  Round, Onion,
69  // Domain transforms
72  // Displacement
73  Displace
74 };
75 
100 struct SdfParams {
101  // ── Primitive parameters ──
102  double radius = 1.0;
103  Point3D extents = Point3D(1, 1, 1);
104  Point3D pt_a = Point3D(0, -1, 0);
105  Point3D pt_b = Point3D(0, 1, 0);
106  double major_radius = 1.0;
107  double minor_radius = 0.3;
108  double angle_rad = 0.5;
109  double height = 2.0;
110  double thickness = 0.1;
111  Vector3D normal = Vector3D(0, 1, 0);
112  double offset = 0.0;
113 
114  // ── Operation parameters ──
115  double blend_k = 0.2;
116  double amount = 0.5;
117  double amplitude = 0.1;
118  double frequency = 1.0;
122 };
123 
138 class SdfNode {
139 public:
142  std::vector<SdfNodePtr> children;
143  std::string name;
144 
145  // ── Primitive factories ──
146 
148  static SdfNodePtr sphere(double r);
149 
151  static SdfNodePtr box(const Point3D& half_extents);
152 
154  static SdfNodePtr round_box(const Point3D& half_extents, double r);
155 
157  static SdfNodePtr cylinder(double r, double h);
158 
160  static SdfNodePtr torus(double major_r, double minor_r);
161 
163  static SdfNodePtr capsule(const Point3D& a, const Point3D& b, double r);
164 
166  static SdfNodePtr cone(double angle_rad, double h);
167 
169  static SdfNodePtr plane(const Vector3D& normal, double offset);
170 
172  static SdfNodePtr ellipsoid(const Point3D& radii);
173 
175  static SdfNodePtr triangular_prism(double h);
176 
178  static SdfNodePtr hex_prism(double h);
179 
181  static SdfNodePtr link(double r, double length, double thickness);
182 
184  static SdfNodePtr wedge(const Point3D& extents);
185 
186  // ── Boolean CSG factories (two children) ──
187 
190 
193 
196 
199 
202 
205 
206  // ── Modifier factories (one child) ──
207 
209  static SdfNodePtr round(SdfNodePtr child, double r);
210 
212  static SdfNodePtr onion(SdfNodePtr child, double thickness);
213 
214  // ── Domain transform factories (one child) ──
215 
217  static SdfNodePtr repeat(SdfNodePtr child, const Point3D& cell);
218 
221 
224 
227 
229  static SdfNodePtr translate(SdfNodePtr child, const Point3D& offset);
230 
232  static SdfNodePtr rotate(SdfNodePtr child, double angle_rad);
233 
235  static SdfNodePtr scale(SdfNodePtr child, const Point3D& factors);
236 
238  static SdfNodePtr twist(SdfNodePtr child, double amount);
239 
241  static SdfNodePtr bend(SdfNodePtr child, double amount);
242 
244  static SdfNodePtr elongate(SdfNodePtr child, const Point3D& h);
245 
247  static SdfNodePtr cheap_bend(SdfNodePtr child, double amount);
248 
250  static SdfNodePtr displace(SdfNodePtr child, double amplitude, double frequency);
251 
266  template <typename Fn>
267  void visit(Fn&& fn) {
268  fn(*this);
269  for (auto& c : children) c->visit(std::forward<Fn>(fn));
270  }
271 
278  template <typename Fn>
279  void visit(Fn&& fn) const {
280  fn(*this);
281  for (auto& c : children) c->visit(std::forward<Fn>(fn));
282  }
283 
284 public:
289  explicit SdfNode(SdfOp o) : op(o) {}
290 };
291 
314 [[nodiscard]] double evaluate(const SdfNodePtr& root, const Point3D& p);
315 
330 [[nodiscard]] Point3D estimate_bounds(const SdfNodePtr& root, double margin = 1.0);
331 
337 struct SdfBBox {
340 };
341 
357 [[nodiscard]] SdfBBox estimate_bbox(const SdfNodePtr& root, double margin = 1.0);
358 
359 } // namespace vde::sdf
SDF 表达式树节点
Definition: sdf_tree.h:138
SdfNode(SdfOp o)
构造 SDF 节点
Definition: sdf_tree.h:289
static SdfNodePtr bend(SdfNodePtr child, double amount)
创建弯曲节点
static SdfNodePtr box(const Point3D &half_extents)
创建轴对齐长方体节点
static SdfNodePtr scale(SdfNodePtr child, const Point3D &factors)
创建缩放节点
static SdfNodePtr sphere(double r)
创建球体节点
static SdfNodePtr round_box(const Point3D &half_extents, double r)
创建圆角长方体节点
SdfOp op
操作类型
Definition: sdf_tree.h:140
static SdfNodePtr smooth_intersection(SdfNodePtr a, SdfNodePtr b, double k)
创建平滑交集节点
static SdfNodePtr rotate(SdfNodePtr child, double angle_rad)
创建旋转节点(绕 Y 轴)
static SdfNodePtr mirror_y(SdfNodePtr child)
创建 Y 镜像节点
static SdfNodePtr op_intersection(SdfNodePtr a, SdfNodePtr b)
创建交集节点: A ∩ B
static SdfNodePtr op_difference(SdfNodePtr a, SdfNodePtr b)
创建差集节点: A \ B
static SdfNodePtr cylinder(double r, double h)
创建圆柱体节点
static SdfNodePtr translate(SdfNodePtr child, const Point3D &offset)
创建平移节点
std::string name
节点名(调试用)
Definition: sdf_tree.h:143
static SdfNodePtr mirror_z(SdfNodePtr child)
创建 Z 镜像节点
static SdfNodePtr triangular_prism(double h)
创建三角棱柱节点
static SdfNodePtr cheap_bend(SdfNodePtr child, double amount)
创建简化弯曲节点
static SdfNodePtr hex_prism(double h)
创建六棱柱节点
static SdfNodePtr link(double r, double length, double thickness)
创建双环链接节点
static SdfNodePtr capsule(const Point3D &a, const Point3D &b, double r)
创建胶囊体节点
static SdfNodePtr displace(SdfNodePtr child, double amplitude, double frequency)
创建位移扰动节点
static SdfNodePtr plane(const Vector3D &normal, double offset)
创建平面节点
static SdfNodePtr wedge(const Point3D &extents)
创建楔形体节点
void visit(Fn &&fn) const
前序遍历(const 版本)
Definition: sdf_tree.h:279
static SdfNodePtr round(SdfNodePtr child, double r)
创建圆角偏移节点
static SdfNodePtr op_union(SdfNodePtr a, SdfNodePtr b)
创建并集节点: A ∪ B
static SdfNodePtr smooth_union(SdfNodePtr a, SdfNodePtr b, double k)
创建平滑并集节点
static SdfNodePtr cone(double angle_rad, double h)
创建圆锥台节点
static SdfNodePtr mirror_x(SdfNodePtr child)
创建 X 镜像节点
static SdfNodePtr elongate(SdfNodePtr child, const Point3D &h)
创建拉伸节点
static SdfNodePtr repeat(SdfNodePtr child, const Point3D &cell)
创建周期重复节点
void visit(Fn &&fn)
前序遍历所有节点
Definition: sdf_tree.h:267
static SdfNodePtr twist(SdfNodePtr child, double amount)
创建扭曲节点
std::vector< SdfNodePtr > children
子节点
Definition: sdf_tree.h:142
static SdfNodePtr smooth_difference(SdfNodePtr a, SdfNodePtr b, double k)
创建平滑差集节点
static SdfNodePtr torus(double major_r, double minor_r)
创建圆环体节点
SdfParams params
参数值
Definition: sdf_tree.h:141
static SdfNodePtr onion(SdfNodePtr child, double thickness)
创建壳层修饰节点
static SdfNodePtr ellipsoid(const Point3D &radii)
创建椭球体节点
foundation::Vector3D Vector3D
双精度三维向量(重新导出)
Definition: point.h:49
foundation::Point3D Point3D
双精度三维点(重新导出)
Definition: point.h:31
SdfBBox estimate_bbox(const SdfNodePtr &root, double margin=1.0)
估算 SDF 树的完整轴对齐包围盒
double evaluate(const SdfNodePtr &root, const Point3D &p)
在空间点 p 处求值 SDF 树
std::shared_ptr< SdfNode > SdfNodePtr
Definition: sdf_tree.h:48
SdfOp
CSG 树的所有节点类型
Definition: sdf_tree.h:60
Point3D estimate_bounds(const SdfNodePtr &root, double margin=1.0)
估算 SDF 树的包围半尺寸
SDF 布尔运算、修饰算子与域变形
有符号距离函数(SDF)基础图元
SDF 树的包围盒(AABB)
Definition: sdf_tree.h:337
Point3D max
最大角点
Definition: sdf_tree.h:339
Point3D min
最小角点
Definition: sdf_tree.h:338
SDF 节点参数联合体
Definition: sdf_tree.h:100
double angle_rad
锥体半角(弧度)
Definition: sdf_tree.h:108
Vector3D normal
平面法向量
Definition: sdf_tree.h:111
Point3D pt_a
胶囊端点 A
Definition: sdf_tree.h:104
double minor_radius
圆环副半径
Definition: sdf_tree.h:107
Point3D pt_b
胶囊端点 B
Definition: sdf_tree.h:105
Point3D extents
盒子的半边长
Definition: sdf_tree.h:103
double blend_k
平滑 CSG 混合参数
Definition: sdf_tree.h:115
double major_radius
圆环主半径
Definition: sdf_tree.h:106
double thickness
壳层厚度(Onion)
Definition: sdf_tree.h:110
Point3D translate_offset
平移量
Definition: sdf_tree.h:120
double radius
球体/圆柱/胶囊/圆角半径
Definition: sdf_tree.h:102
double height
柱体/锥体/挤出高度
Definition: sdf_tree.h:109
Point3D repeat_cell
重复晶格尺寸
Definition: sdf_tree.h:119
double amplitude
位移扰动幅值
Definition: sdf_tree.h:117
double offset
平面偏移 / 通用偏移
Definition: sdf_tree.h:112
Point3D scale_factors
缩放因子
Definition: sdf_tree.h:121
double frequency
位移扰动频率
Definition: sdf_tree.h:118
double amount
扭曲/弯曲量
Definition: sdf_tree.h:116