66 const std::function<
double(
const Point3D&)>& f,
67 const Point3D& p,
double h = 1e-6)
70 double inv_2h = 0.5 / h;
71 g.x() = (f(
Point3D(p.x() + h, p.y(), p.z())) - f(
Point3D(p.x() - h, p.y(), p.z()))) * inv_2h;
72 g.y() = (f(
Point3D(p.x(), p.y() + h, p.z())) - f(
Point3D(p.x(), p.y() - h, p.z()))) * inv_2h;
73 g.z() = (f(
Point3D(p.x(), p.y(), p.z() + h)) - f(
Point3D(p.x(), p.y(), p.z() - h))) * inv_2h;
103 const std::function<
double(
const Point3D&)>& f,
104 const Point3D& p,
double h = 1e-6)
129 return (
sphere(p, radius + h) -
sphere(p, radius - h)) / (2.0 * h);
144 int axis,
double h = 1e-6)
149 ext_minus[axis] -= h;
150 return (
box(p, ext_plus) -
box(p, ext_minus)) / (2.0 * h);
165 return (
torus(p, major_r + h, minor_r) -
torus(p, major_r - h, minor_r)) / (2.0 * h);
176 return (
torus(p, major_r, minor_r + h) -
torus(p, major_r, minor_r - h)) / (2.0 * h);
191 return (
cylinder(p, radius, height + h) -
cylinder(p, radius, height - h)) / (2.0 * h);
202 return (
cylinder(p, radius + h, height) -
cylinder(p, radius - h, height)) / (2.0 * h);
226 if (n < 1e-30)
return Vector3D::Zero();
246 if (std::abs(p.x()) > half_extents.x()) g.x() = (p.x() > 0.0 ? 1.0 : -1.0);
247 if (std::abs(p.y()) > half_extents.y()) g.y() = (p.y() > 0.0 ? 1.0 : -1.0);
248 if (std::abs(p.z()) > half_extents.z()) g.z() = (p.z() > 0.0 ? 1.0 : -1.0);
251 if (g.norm() < 1e-30 && half_extents.norm() > 1e-30) {
254 half_extents.x() - std::abs(p.x()),
255 half_extents.y() - std::abs(p.y()),
256 half_extents.z() - std::abs(p.z())
259 if (q.x() <= q.y() && q.x() <= q.z()) g.x() = (p.x() >= 0.0 ? 1.0 : -1.0);
260 else if (q.y() <= q.x() && q.y() <= q.z()) g.y() = (p.y() >= 0.0 ? 1.0 : -1.0);
261 else g.z() = (p.z() >= 0.0 ? 1.0 : -1.0);
282 double rho = std::sqrt(p.x() * p.x() + p.z() * p.z());
286 double inner = std::sqrt(major_r * major_r + p.y() * p.y());
287 if (inner < 1e-30)
return Vector3D::Zero();
288 return Vector3D(0.0, p.y() / inner, 0.0);
290 double inner = std::sqrt((rho - major_r) * (rho - major_r) + p.y() * p.y());
291 if (inner < 1e-30)
return Vector3D::Zero();
292 double d_rho = (rho - major_r) / inner;
314 double rho = std::sqrt(p.x() * p.x() + p.z() * p.z());
315 if (rho < 1e-30)
return Vector3D::UnitY();
316 return Vector3D(p.x() / rho, 0.0, p.z() / rho);
360 double ba_sq = ba.squaredNorm();
363 double n = pa.norm();
364 if (n < 1e-30)
return Vector3D::Zero();
367 double h = std::clamp(pa.dot(ba) / ba_sq, 0.0, 1.0);
370 double d = delta.norm();
371 if (d < 1e-30)
return Vector3D::Zero();
395 double d1,
double d2)
397 return (d1 < d2) ? grad_a : grad_b;
408 double d1,
double d2)
410 return (d1 > d2) ? grad_a : grad_b;
425 double d1,
double d2)
427 return (d1 > -d2) ? grad_a :
Vector3D(-grad_b.x(), -grad_b.y(), -grad_b.z());
453 double d1,
double d2,
double k)
455 if (k <= 0.0)
return chain_union(grad_a, grad_b, d1, d2);
456 double h = std::clamp(0.5 + 0.5 * (d1 - d2) / k, 0.0, 1.0);
457 if (h <= 0.0)
return grad_a;
458 if (h >= 1.0)
return grad_b;
462 double w1 = 2.0 - 3.0 * h;
463 double w2 = 3.0 * h - 1.0;
464 return grad_a * w1 + grad_b * w2;
501 double c = std::cos(angle_rad);
502 double s = std::sin(angle_rad);
505 c * child_result.
grad.x() - s * child_result.
grad.z(),
506 child_result.
grad.y(),
507 s * child_result.
grad.x() + c * child_result.
grad.z()
529 child_result.
grad.x() / factors.x(),
530 child_result.
grad.y() / factors.y(),
531 child_result.
grad.z() / factors.z()
559 double amount,
const Point3D& p)
561 double angle = amount * p.y();
562 double c = std::cos(angle);
563 double s = std::sin(angle);
566 double tp_x = p.x() * c - p.z() * s;
567 double tp_z = p.x() * s + p.z() * c;
572 double gx = c * child_result.
grad.x() + s * child_result.
grad.z();
573 double gz = -s * child_result.
grad.x() + c * child_result.
grad.z();
577 double gy = child_result.
grad.y() + amount * (tp_x * child_result.
grad.z()
578 - tp_z * child_result.
grad.x());
foundation::Vector3D Vector3D
双精度三维向量(重新导出)
foundation::Point3D Point3D
双精度三维点(重新导出)
Vector3D sphere_gradient_analytic(const Point3D &p, double)
球体 SDF 的解析梯度
Vector3D chain_smooth_union(const Vector3D &grad_a, const Vector3D &grad_b, double d1, double d2, double k)
平滑并集操作的梯度传递(含完整链式法则)
Vector3D cylinder_gradient_analytic(const Point3D &p, double)
无限圆柱(沿 Y 轴)的解析梯度
GradResult chain_twist(const GradResult &child_result, double amount, const Point3D &p)
扭曲变换后的梯度传递(含完整雅可比)
Vector3D capsule_gradient_analytic(const Point3D &p, const Point3D &a, const Point3D &b, double radius)
胶囊体 SDF 的解析梯度
ParamGrad sphere_param_grad(const Point3D &p, double radius, double h=1e-6)
球体所有参数的梯度(当前仅 radius 有意义)
ParamGrad box_param_grad(const Point3D &p, const Point3D &half_extents, double h=1e-6)
长方体所有参数的梯度
double cylinder_radius_gradient(const Point3D &p, double radius, double height, double h=1e-6)
圆柱 SDF 对半径的参数梯度 ∂f/∂radius
Vector3D chain_difference(const Vector3D &grad_a, const Vector3D &grad_b, double d1, double d2)
差集操作后的梯度传递
double box(const Point3D &p, const Point3D &half_extents)
轴对齐立方体的 SDF
double cylinder(const Point3D &p, double radius, double height)
带平端盖的圆柱体 SDF
Vector3D chain_union(const Vector3D &grad_a, const Vector3D &grad_b, double d1, double d2)
并集操作后的梯度传递
GradResult chain_rotate(const GradResult &child_result, double angle_rad)
绕 Y 轴旋转后的梯度传递
double box_extent_gradient(const Point3D &p, const Point3D &extents, int axis, double h=1e-6)
长方体 SDF 对各轴半尺寸的参数梯度 ∂f/∂extent[i]
double cylinder_height_gradient(const Point3D &p, double radius, double height, double h=1e-6)
圆柱 SDF 对高度的参数梯度 ∂f/∂height
double torus(const Point3D &p, double major_radius, double minor_radius)
圆环面的 SDF
GradResult chain_repeat(const GradResult &child_result, const Point3D &, const Point3D &)
周期重复后的梯度传递(梯度不变)
double sphere_radius_gradient(const Point3D &p, double radius, double h=1e-6)
球体 SDF 对半径的参数梯度 ∂f/∂r
double torus_minor_gradient(const Point3D &p, double major_r, double minor_r, double h=1e-6)
圆环 SDF 对副半径的参数梯度 ∂f/∂minor_r
Vector3D plane_gradient_analytic(const Vector3D &normal)
平面的解析梯度
Vector3D box_gradient_analytic(const Point3D &p, const Point3D &half_extents)
长方体 SDF 的解析梯度
Vector3D torus_gradient_analytic(const Point3D &p, double major_r, double minor_r)
圆环 SDF 的解析梯度
Vector3D chain_intersection(const Vector3D &grad_a, const Vector3D &grad_b, double d1, double d2)
交集操作后的梯度传递
GradResult chain_scale(const GradResult &child_result, const Point3D &factors)
非均匀缩放后的梯度传递
double sphere(const Point3D &p, double radius)
球体的 SDF
double torus_major_gradient(const Point3D &p, double major_r, double minor_r, double h=1e-6)
圆环 SDF 对主半径的参数梯度 ∂f/∂major_r
GradResult chain_translate(const GradResult &child_result, const Point3D &)
平移变换后的梯度传递(梯度不变)
ParamGrad cylinder_param_grad(const Point3D &p, double radius, double height, double h=1e-6)
圆柱所有参数的梯度
Vector3D gradient(const std::function< double(const Point3D &)> &f, const Point3D &p, double h=1e-6)
SDF 的空间梯度(中心差分近似)
GradResult evaluate_with_gradient(const std::function< double(const Point3D &)> &f, const Point3D &p, double h=1e-6)
同时计算 SDF 值和梯度的便捷函数
Vector3D grad
空间梯度 ∇f(p) = (df/dx, df/dy, df/dz)
Point3D d_translate
∂f/∂translate_offset
double d_height
∂f/∂height
double d_minor
∂f/∂minor_radius
double d_rotate
∂f/∂rotation_angle
double d_offset
∂f/∂offset
Vector3D d_normal
∂f/∂normal (无效分量清零)
double d_major
∂f/∂major_radius
Point3D d_scale
∂f/∂scale_factors
double d_radius
∂f/∂radius
Point3D d_extents
∂f/∂(extent.x, extent.y, extent.z)
double d_angle
∂f/∂angle_rad