ViewDesignEngine  3.1.0
高性能 CAD 计算几何引擎
sdf_operations.h
浏览该文件的文档.
1 #pragma once
17 #include "vde/core/point.h"
18 #include <cmath>
19 #include <algorithm>
20 
21 namespace vde::sdf {
22 
23 using core::Point3D;
24 using core::Vector3D;
25 
26 // ── Boolean operations on distance values ──
27 
48 [[nodiscard]] inline double op_union(double d1, double d2) {
49  return std::min(d1, d2);
50 }
51 
70 [[nodiscard]] inline double op_intersection(double d1, double d2) {
71  return std::max(d1, d2);
72 }
73 
94 [[nodiscard]] inline double op_difference(double d1, double d2) {
95  return std::max(d1, -d2);
96 }
97 
126 [[nodiscard]] inline double op_smooth_union(double d1, double d2, double k) {
127  if (k <= 0.0) return std::min(d1, d2);
128  double h = std::clamp(0.5 + 0.5 * (d1 - d2) / k, 0.0, 1.0);
129  return d1 * (1.0 - h) + d2 * h - k * h * (1.0 - h);
130 }
131 
149 [[nodiscard]] inline double op_smooth_intersection(double d1, double d2, double k) {
150  if (k <= 0.0) return std::max(d1, d2);
151  double h = std::clamp(0.5 - 0.5 * (d1 - d2) / k, 0.0, 1.0);
152  return d1 * (1.0 - h) + d2 * h + k * h * (1.0 - h);
153 }
154 
172 [[nodiscard]] inline double op_smooth_difference(double d1, double d2, double k) {
173  if (k <= 0.0) return std::max(d1, -d2);
174  double h = std::clamp(0.5 - 0.5 * (d2 + d1) / k, 0.0, 1.0);
175  return d1 * (1.0 - h) + (-d2) * h + k * h * (1.0 - h);
176 }
177 
178 // ── Modifiers (operate on a distance value) ──
179 
200 [[nodiscard]] inline double op_round(double d, double r) {
201  return d - r;
202 }
203 
223 [[nodiscard]] inline double op_onion(double d, double thickness) {
224  return std::abs(d) - thickness;
225 }
226 
227 // ── Domain deformation operators (transform point space) ──
228 
251 [[nodiscard]] inline Point3D op_repeat(const Point3D& p, const Point3D& cell) {
252  auto wrap = [](double v, double c) {
253  return c > 0.0 ? v - c * std::round(v / c) : v;
254  };
255  return Point3D(wrap(p.x(), cell.x()),
256  wrap(p.y(), cell.y()),
257  wrap(p.z(), cell.z()));
258 }
259 
279 [[nodiscard]] inline Point3D op_mirror_x(const Point3D& p, double offset = 0.0) {
280  return Point3D(offset + std::abs(p.x() - offset), p.y(), p.z());
281 }
282 
299 [[nodiscard]] inline Point3D op_mirror_y(const Point3D& p) {
300  return Point3D(p.x(), std::abs(p.y()), p.z());
301 }
302 
313 [[nodiscard]] inline Point3D op_mirror_z(const Point3D& p) {
314  return Point3D(p.x(), p.y(), std::abs(p.z()));
315 }
316 
339 [[nodiscard]] inline Point3D op_translate(const Point3D& p, const Point3D& offset) {
340  return p - offset;
341 }
342 
368 [[nodiscard]] inline Point3D op_rotate(const Point3D& p, double angle_rad) {
369  double c = std::cos(-angle_rad);
370  double s = std::sin(-angle_rad);
371  return Point3D(p.x() * c - p.z() * s, p.y(), p.x() * s + p.z() * c);
372 }
373 
397 [[nodiscard]] inline Point3D op_scale(const Point3D& p, const Point3D& s) {
398  return Point3D(p.x() / s.x(), p.y() / s.y(), p.z() / s.z());
399 }
400 
426 [[nodiscard]] inline Point3D op_twist(const Point3D& p, double amount) {
427  double c = std::cos(amount * p.y());
428  double s = std::sin(amount * p.y());
429  return Point3D(p.x() * c - p.z() * s, p.y(), p.x() * s + p.z() * c);
430 }
431 
457 [[nodiscard]] inline Point3D op_bend(const Point3D& p, double k) {
458  double c = std::cos(k * p.x());
459  double s = std::sin(k * p.x());
460  return Point3D(c * p.x() - s * p.y(), s * p.x() + c * p.y(), p.z());
461 }
462 
486 [[nodiscard]] inline Point3D op_elongate(const Point3D& p, const Point3D& h) {
487  Point3D q = p.cwiseAbs() - h;
488  return Point3D(
489  q.x() > 0.0 ? p.x() - std::copysign(h.x(), p.x()) : 0.0,
490  q.y() > 0.0 ? p.y() - std::copysign(h.y(), p.y()) : 0.0,
491  q.z() > 0.0 ? p.z() - std::copysign(h.z(), p.z()) : 0.0
492  );
493 }
494 
515 [[nodiscard]] inline Point3D op_cheap_bend(const Point3D& p, double k) {
516  double c = std::cos(k * p.x());
517  double s = std::sin(k * p.x());
518  return Point3D(p.x(), p.y() * c - p.z() * s, p.y() * s + p.z() * c);
519 }
520 
545 [[nodiscard]] inline double op_displace(double d, const Point3D& p,
546  double amplitude, double frequency) {
547  double noise = std::sin(p.x() * frequency) *
548  std::sin(p.y() * frequency) *
549  std::sin(p.z() * frequency);
550  return d + amplitude * noise;
551 }
552 
553 } // namespace vde::sdf
foundation::Vector3D Vector3D
双精度三维向量(重新导出)
Definition: point.h:49
foundation::Point3D Point3D
双精度三维点(重新导出)
Definition: point.h:31
double op_intersection(double d1, double d2)
布尔交集: A ∩ B
Point3D op_rotate(const Point3D &p, double angle_rad)
绕 Y 轴旋转(逆变换)
Point3D op_scale(const Point3D &p, const Point3D &s)
非均匀缩放(逆变换)
double op_difference(double d1, double d2)
布尔差集: A \ B
Point3D op_twist(const Point3D &p, double amount)
绕 Y 轴的扭曲变换
Point3D op_cheap_bend(const Point3D &p, double k)
简化弯曲变换(绕 X 轴弯曲 YZ 平面)
double op_onion(double d, double thickness)
洋葱皮/壳层修饰: 取绝对值后减去厚度
Point3D op_bend(const Point3D &p, double k)
弯曲变换(绕 Z 轴弯曲 XZ 平面)
Point3D op_mirror_y(const Point3D &p)
镜像对称(Y=0 平面)
Point3D op_repeat(const Point3D &p, const Point3D &cell)
无限重复(周期复制)
double op_union(double d1, double d2)
布尔并集: A ∪ B
double op_smooth_difference(double d1, double d2, double k)
平滑差集(带混合过渡)
Point3D op_elongate(const Point3D &p, const Point3D &h)
拉伸/压扁域变形(消除拉伸方向上的内部体积)
Point3D op_mirror_z(const Point3D &p)
镜像对称(Z=0 平面)
Point3D op_mirror_x(const Point3D &p, double offset=0.0)
镜像对称(X=0 平面),可选偏移
Point3D op_translate(const Point3D &p, const Point3D &offset)
空间平移(逆变换)
double op_displace(double d, const Point3D &p, double amplitude, double frequency)
正弦波位移扰动
double op_round(double d, double r)
圆角偏移: 将等值面向外扩展 r 单位
double op_smooth_union(double d1, double d2, double k)
平滑并集(带混合过渡)
double op_smooth_intersection(double d1, double d2, double k)
平滑交集(带混合过渡)