工厂原仅接收 height 且把 radius 硬绑等于 height。改为 hex_prism(radius,height,sides=6),SdfParams 增 sides;新增通用 regular_polygon_prism 精确 SDF(扇区折叠,sides=6 与旧 hex_prism 几何一致),evaluate 经其求值;bounds 半径随 radius/height 计算。python 绑定同步。 判别性测试 +7:test_sdf_tree 半径/高度独立、边数控截面、非法边数夹取;test_sdf_primitives 顶点共享、面法向精确距离。sdf 6 套全绿(tree 34、primitives 69、to_mesh 6、optimize 21、operations 33、gradient 51)。
539 lines
20 KiB
C++
539 lines
20 KiB
C++
#include "vde/sdf/sdf_tree.h"
|
|
#include <gtest/gtest.h>
|
|
#include <cmath>
|
|
|
|
using namespace vde::sdf;
|
|
using vde::core::Point3D;
|
|
using vde::core::Vector3D;
|
|
|
|
// ── Null/empty tree ──────────────────────────────────────────────
|
|
|
|
TEST(SdfTree, NullNodeReturnsLargeValue) {
|
|
SdfNodePtr null_node = nullptr;
|
|
double d = evaluate(null_node, Point3D(0, 0, 0));
|
|
EXPECT_GT(d, 1e10);
|
|
}
|
|
|
|
// ── Primitive evaluation ─────────────────────────────────────────
|
|
|
|
TEST(SdfTree, SphereInsideOutsideBoundary) {
|
|
auto s = SdfNode::sphere(1.0);
|
|
|
|
// Origin = inside
|
|
double d_in = evaluate(s, Point3D(0, 0, 0));
|
|
EXPECT_LT(d_in, 0.0);
|
|
EXPECT_NEAR(d_in, -1.0, 1e-6);
|
|
|
|
// Surface (r=1 on X axis)
|
|
double d_surf = evaluate(s, Point3D(1, 0, 0));
|
|
EXPECT_NEAR(d_surf, 0.0, 1e-6);
|
|
|
|
// Outside
|
|
double d_out = evaluate(s, Point3D(2, 0, 0));
|
|
EXPECT_GT(d_out, 0.0);
|
|
EXPECT_NEAR(d_out, 1.0, 1e-6);
|
|
}
|
|
|
|
TEST(SdfTree, BoxEvaluation) {
|
|
auto b = SdfNode::box(Point3D(1, 1, 1));
|
|
|
|
// Center = inside
|
|
EXPECT_LT(evaluate(b, Point3D(0, 0, 0)), 0.0);
|
|
// Surface
|
|
EXPECT_NEAR(evaluate(b, Point3D(1, 0, 0)), 0.0, 1e-6);
|
|
// Outside
|
|
EXPECT_GT(evaluate(b, Point3D(2, 0, 0)), 0.0);
|
|
}
|
|
|
|
TEST(SdfTree, TorusEvaluation) {
|
|
auto t = SdfNode::torus(1.0, 0.3);
|
|
|
|
// Point on the ring centerline (should be -minor_radius = inside)
|
|
double d = evaluate(t, Point3D(1, 0, 0));
|
|
EXPECT_LT(d, 0.0);
|
|
EXPECT_NEAR(d, -0.3, 1e-6);
|
|
}
|
|
|
|
TEST(SdfTree, CylinderEvaluation) {
|
|
auto c = SdfNode::cylinder(0.5, 1.0);
|
|
|
|
EXPECT_LT(evaluate(c, Point3D(0, 0, 0)), 0.0); // center
|
|
EXPECT_NEAR(evaluate(c, Point3D(0.5, 0, 0)), 0.0, 1e-6); // surface
|
|
EXPECT_GT(evaluate(c, Point3D(1, 0, 0)), 0.0); // outside radius
|
|
EXPECT_GT(evaluate(c, Point3D(0, 2, 0)), 0.0); // above cap
|
|
}
|
|
|
|
TEST(SdfTree, PlaneEvaluation) {
|
|
auto p = SdfNode::plane(Vector3D(0, 1, 0), 0.0); // horizontal at y=0
|
|
|
|
EXPECT_GT(evaluate(p, Point3D(0, 1, 0)), 0.0); // above
|
|
EXPECT_LT(evaluate(p, Point3D(0, -1, 0)), 0.0); // below
|
|
EXPECT_NEAR(evaluate(p, Point3D(0, 0, 0)), 0.0, 1e-6);
|
|
}
|
|
|
|
// ── CSG boolean operations ───────────────────────────────────────
|
|
|
|
TEST(SdfTree, UnionSphereBox) {
|
|
auto tree = SdfNode::op_union(
|
|
SdfNode::sphere(1.0),
|
|
SdfNode::box(Point3D(1, 1, 1))
|
|
);
|
|
|
|
// Point inside sphere
|
|
EXPECT_LT(evaluate(tree, Point3D(0, 0, 0)), 0.0);
|
|
// Point inside box (but outside sphere) — box surface at |x|=1
|
|
EXPECT_LT(evaluate(tree, Point3D(0.5, 0, 0)), 0.0);
|
|
// Point outside both
|
|
EXPECT_GT(evaluate(tree, Point3D(3, 0, 0)), 0.0);
|
|
}
|
|
|
|
TEST(SdfTree, IntersectionSphereBox) {
|
|
auto tree = SdfNode::op_intersection(
|
|
SdfNode::sphere(1.0),
|
|
SdfNode::box(Point3D(0.5, 0.5, 0.5))
|
|
);
|
|
|
|
// Inside intersection region
|
|
EXPECT_LT(evaluate(tree, Point3D(0, 0, 0)), 0.0);
|
|
// Inside sphere but outside box
|
|
EXPECT_GT(evaluate(tree, Point3D(0.8, 0, 0)), 0.0);
|
|
}
|
|
|
|
TEST(SdfTree, DifferenceSphereBox) {
|
|
auto tree = SdfNode::op_difference(
|
|
SdfNode::sphere(2.0),
|
|
SdfNode::box(Point3D(1, 1, 1))
|
|
);
|
|
|
|
// Inside sphere, outside box → inside
|
|
EXPECT_LT(evaluate(tree, Point3D(1.5, 0, 0)), 0.0);
|
|
// Inside box (removed region) → outside
|
|
EXPECT_GT(evaluate(tree, Point3D(0, 0, 0)), 0.0);
|
|
}
|
|
|
|
// ── Smooth boolean operations ────────────────────────────────────
|
|
|
|
TEST(SdfTree, SmoothUnion) {
|
|
auto tree = SdfNode::smooth_union(
|
|
SdfNode::sphere(1.0),
|
|
SdfNode::sphere(1.0),
|
|
0.3
|
|
);
|
|
|
|
// Both spheres at origin → should be equivalent
|
|
EXPECT_LT(evaluate(tree, Point3D(0, 0, 0)), 0.0);
|
|
}
|
|
|
|
// ── Domain transforms ────────────────────────────────────────────
|
|
|
|
TEST(SdfTree, TranslatedSphere) {
|
|
auto tree = SdfNode::translate(
|
|
SdfNode::sphere(1.0),
|
|
Point3D(2, 0, 0)
|
|
);
|
|
|
|
// Original origin should be far away
|
|
EXPECT_GT(evaluate(tree, Point3D(0, 0, 0)), 0.0);
|
|
// New center should be inside
|
|
EXPECT_LT(evaluate(tree, Point3D(2, 0, 0)), 0.0);
|
|
EXPECT_NEAR(evaluate(tree, Point3D(2, 0, 0)), -1.0, 1e-6);
|
|
// Surface at new center + radius
|
|
EXPECT_NEAR(evaluate(tree, Point3D(3, 0, 0)), 0.0, 1e-6);
|
|
}
|
|
|
|
TEST(SdfTree, ScaledSphere) {
|
|
auto tree = SdfNode::scale(
|
|
SdfNode::sphere(1.0),
|
|
Point3D(2, 1, 1)
|
|
);
|
|
|
|
// At (2,0,0) in world = (1,0,0) in local → on surface
|
|
EXPECT_NEAR(evaluate(tree, Point3D(2, 0, 0)), 0.0, 1e-6);
|
|
// At (0,0,0) in world = inside
|
|
EXPECT_LT(evaluate(tree, Point3D(0, 0, 0)), 0.0);
|
|
}
|
|
|
|
TEST(SdfTree, RepeatedSphere) {
|
|
auto tree = SdfNode::repeat(
|
|
SdfNode::sphere(0.3),
|
|
Point3D(2, 2, 2)
|
|
);
|
|
|
|
// Origin → mapped to (0,0,0) inside
|
|
EXPECT_LT(evaluate(tree, Point3D(0, 0, 0)), 0.0);
|
|
// (2,0,0) → mapped to (0,0,0) inside
|
|
EXPECT_LT(evaluate(tree, Point3D(2, 0, 0)), 0.0);
|
|
// Between copies → outside
|
|
EXPECT_GT(evaluate(tree, Point3D(1, 1, 0)), 0.0);
|
|
}
|
|
|
|
TEST(SdfTree, MirroredX) {
|
|
auto tree = SdfNode::mirror_x(
|
|
SdfNode::sphere(1.0)
|
|
);
|
|
|
|
// Both sides should have the sphere
|
|
EXPECT_LT(evaluate(tree, Point3D(0, 0, 0)), 0.0);
|
|
EXPECT_LT(evaluate(tree, Point3D(-0.5, 0, 0)), 0.0);
|
|
// Surface at ±1
|
|
EXPECT_NEAR(evaluate(tree, Point3D(1, 0, 0)), 0.0, 1e-6);
|
|
EXPECT_NEAR(evaluate(tree, Point3D(-1, 0, 0)), 0.0, 1e-6);
|
|
}
|
|
|
|
// ── Nested operations ────────────────────────────────────────────
|
|
|
|
TEST(SdfTree, NestedUnionTwist) {
|
|
auto inner = SdfNode::op_union(
|
|
SdfNode::sphere(1.0),
|
|
SdfNode::box(Point3D(0.6, 0.6, 0.6))
|
|
);
|
|
auto tree = SdfNode::twist(std::move(inner), 0.5);
|
|
|
|
// Should still evaluate successfully
|
|
double d = evaluate(tree, Point3D(0, 0.5, 0));
|
|
EXPECT_LT(d, 0.0); // interior of union
|
|
}
|
|
|
|
TEST(SdfTree, DeepNesting) {
|
|
auto a = SdfNode::sphere(1.0);
|
|
auto b = SdfNode::box(Point3D(0.5, 0.5, 0.5));
|
|
auto uni = SdfNode::op_union(std::move(a), std::move(b));
|
|
auto scaled = SdfNode::scale(std::move(uni), Point3D(2, 2, 2));
|
|
auto translated = SdfNode::translate(std::move(scaled), Point3D(3, 0, 0));
|
|
|
|
// Original origin → should be inside after inverse transform + union
|
|
double d = evaluate(translated, Point3D(3, 0, 0));
|
|
EXPECT_LT(d, 0.0);
|
|
}
|
|
|
|
// ── Modifiers ────────────────────────────────────────────────────
|
|
|
|
TEST(SdfTree, RoundModifier) {
|
|
auto tree = SdfNode::round(
|
|
SdfNode::box(Point3D(1, 1, 1)),
|
|
0.2
|
|
);
|
|
|
|
// Center still inside
|
|
EXPECT_LT(evaluate(tree, Point3D(0, 0, 0)), 0.0);
|
|
// Original corner point (1,1,0) should now be outside (rounded away)
|
|
double d_corner = evaluate(tree, Point3D(1, 0, 0));
|
|
// Rounded box should have slightly larger radius at corners
|
|
// The exact value depends on implementation, just verify it's farther
|
|
double d_box = evaluate(SdfNode::box(Point3D(1, 1, 1)), Point3D(1, 0, 0));
|
|
EXPECT_NEAR(d_corner, d_box - 0.2, 1e-6);
|
|
}
|
|
|
|
TEST(SdfTree, OnionModifier) {
|
|
auto tree = SdfNode::onion(
|
|
SdfNode::sphere(2.0),
|
|
0.3
|
|
);
|
|
|
|
// Center should be outside (hollowed out)
|
|
EXPECT_GT(evaluate(tree, Point3D(0, 0, 0)), 0.0);
|
|
// Shell surface should exist
|
|
EXPECT_NEAR(evaluate(tree, Point3D(1.7, 0, 0)), 0.0, 1e-6);
|
|
}
|
|
|
|
// ── Bounds estimation ────────────────────────────────────────────
|
|
|
|
TEST(SdfTree, EstimateBoundsSphere) {
|
|
auto s = SdfNode::sphere(2.0);
|
|
Point3D b = estimate_bounds(s, 0.0);
|
|
EXPECT_NEAR(b.x(), 2.0, 1e-6);
|
|
EXPECT_NEAR(b.y(), 2.0, 1e-6);
|
|
EXPECT_NEAR(b.z(), 2.0, 1e-6);
|
|
}
|
|
|
|
TEST(SdfTree, EstimateBoundsWithMargin) {
|
|
auto s = SdfNode::sphere(2.0);
|
|
Point3D b = estimate_bounds(s, 1.0);
|
|
EXPECT_NEAR(b.x(), 3.0, 1e-6);
|
|
}
|
|
|
|
TEST(SdfTree, EstimateBoundsUnion) {
|
|
auto tree = SdfNode::op_union(
|
|
SdfNode::sphere(2.0),
|
|
SdfNode::box(Point3D(3, 3, 3))
|
|
);
|
|
Point3D b = estimate_bounds(tree, 0.0);
|
|
// Box half-extent norm = sqrt(27) ≈ 5.196
|
|
EXPECT_GT(b.norm(), 4.0);
|
|
}
|
|
|
|
TEST(SdfTree, EstimateBoundsTranslated) {
|
|
auto tree = SdfNode::translate(
|
|
SdfNode::sphere(1.0),
|
|
Point3D(5, 0, 0)
|
|
);
|
|
Point3D b = estimate_bounds(tree, 0.0);
|
|
EXPECT_GT(b.norm(), 5.0);
|
|
}
|
|
|
|
// ── Tree traversal ───────────────────────────────────────────────
|
|
|
|
TEST(SdfTree, VisitCountsNodes) {
|
|
auto tree = SdfNode::op_union(
|
|
SdfNode::sphere(1.0),
|
|
SdfNode::box(Point3D(1, 1, 1))
|
|
);
|
|
|
|
int count = 0;
|
|
tree->visit([&count](const SdfNode&) { ++count; });
|
|
EXPECT_EQ(count, 3); // union + sphere + box
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════════════
|
|
// VDE-893: domain-transform & smooth-CSG factory discriminance tests
|
|
// Each factory's evaluate() is cross-checked point-by-point against an
|
|
// independently hand-coded reference transform / blend polynomial.
|
|
// ═══════════════════════════════════════════════════════════════════
|
|
|
|
namespace {
|
|
|
|
// Independent references (hand-coded; do NOT call the library op_* helpers).
|
|
Point3D ref_bend(const Point3D& p, double k) {
|
|
double c = std::cos(k * p.x()), s = std::sin(k * p.x());
|
|
return Point3D(c * p.x() - s * p.y(), s * p.x() + c * p.y(), p.z());
|
|
}
|
|
Point3D ref_cheap_bend(const Point3D& p, double k) {
|
|
double c = std::cos(k * p.x()), s = std::sin(k * p.x());
|
|
return Point3D(p.x(), p.y() * c - p.z() * s, p.y() * s + p.z() * c);
|
|
}
|
|
Point3D ref_elongate(const Point3D& p, const Point3D& h) {
|
|
auto comp = [](double v, double hi) {
|
|
return (std::abs(v) - hi > 0.0) ? v - std::copysign(hi, v) : 0.0;
|
|
};
|
|
return Point3D(comp(p.x(), h.x()), comp(p.y(), h.y()), comp(p.z(), h.z()));
|
|
}
|
|
|
|
std::vector<Point3D> probe_points() {
|
|
return {
|
|
Point3D(0.3, 0.2, 0.1), Point3D(1.0, 0.0, 0.0),
|
|
Point3D(-0.7, 0.4, 0.9), Point3D(1.2, -1.1, 0.5),
|
|
Point3D(-1.5, -0.6, -1.3), Point3D(0.05, -0.02, 0.03),
|
|
Point3D(2.0, 1.0, -2.0), Point3D(-0.25, 1.75, 0.35),
|
|
};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(SdfTreeFactory, BendMatchesIndependentTransform) {
|
|
const double k = 0.3;
|
|
auto child = SdfNode::box(Point3D(0.3, 1.5, 0.3));
|
|
auto tree = SdfNode::bend(child, k);
|
|
|
|
EXPECT_EQ(static_cast<int>(tree->op), static_cast<int>(SdfOp::Bend));
|
|
EXPECT_DOUBLE_EQ(tree->params.amount, k);
|
|
ASSERT_EQ(tree->children.size(), 1u);
|
|
|
|
for (const auto& p : probe_points())
|
|
EXPECT_NEAR(evaluate(tree, p), evaluate(child, ref_bend(p, k)), 1e-12);
|
|
|
|
// Discriminance: bend (xy plane) and cheap_bend (yz plane) must not be
|
|
// interchangeable — they give different distances for a non-radial child.
|
|
auto cheap = SdfNode::cheap_bend(child, k);
|
|
Point3D q(1.0, 1.0, 1.0);
|
|
EXPECT_GT(std::abs(evaluate(tree, q) - evaluate(cheap, q)), 1e-6);
|
|
}
|
|
|
|
TEST(SdfTreeFactory, CheapBendMatchesIndependentTransform) {
|
|
const double k = 0.4;
|
|
auto child = SdfNode::box(Point3D(0.3, 1.5, 0.3));
|
|
auto tree = SdfNode::cheap_bend(child, k);
|
|
|
|
EXPECT_EQ(static_cast<int>(tree->op), static_cast<int>(SdfOp::CheapBend));
|
|
EXPECT_DOUBLE_EQ(tree->params.amount, k);
|
|
|
|
for (const auto& p : probe_points())
|
|
EXPECT_NEAR(evaluate(tree, p), evaluate(child, ref_cheap_bend(p, k)), 1e-12);
|
|
|
|
// At x=0 the cheap bend is the identity (cos0=1, sin0=0).
|
|
Point3D on_axis(0.0, 0.7, -0.4);
|
|
EXPECT_NEAR(evaluate(tree, on_axis), evaluate(child, on_axis), 1e-12);
|
|
}
|
|
|
|
TEST(SdfTreeFactory, ElongateMatchesIndependentTransform) {
|
|
const Point3D h(0.5, 0.25, 0.75);
|
|
auto child = SdfNode::sphere(1.0);
|
|
auto tree = SdfNode::elongate(child, h);
|
|
|
|
EXPECT_EQ(static_cast<int>(tree->op), static_cast<int>(SdfOp::Elongate));
|
|
EXPECT_DOUBLE_EQ(tree->params.extents.x(), h.x());
|
|
EXPECT_DOUBLE_EQ(tree->params.extents.y(), h.y());
|
|
EXPECT_DOUBLE_EQ(tree->params.extents.z(), h.z());
|
|
|
|
for (const auto& p : probe_points())
|
|
EXPECT_NEAR(evaluate(tree, p), evaluate(child, ref_elongate(p, h)), 1e-12);
|
|
|
|
// Elongation pulls the surface outward: (2,0,0) maps to (1.5,0,0) → d = 0.5
|
|
// (vs. 1.0 for the un-elongated sphere).
|
|
EXPECT_NEAR(evaluate(tree, Point3D(2.0, 0.0, 0.0)), 0.5, 1e-12);
|
|
}
|
|
|
|
TEST(SdfTreeFactory, DisplaceMatchesIndependentNoise) {
|
|
const double amp = 0.4, freq = 2.0;
|
|
auto child = SdfNode::sphere(1.0);
|
|
auto tree = SdfNode::displace(child, amp, freq);
|
|
|
|
EXPECT_EQ(static_cast<int>(tree->op), static_cast<int>(SdfOp::Displace));
|
|
EXPECT_DOUBLE_EQ(tree->params.amplitude, amp);
|
|
EXPECT_DOUBLE_EQ(tree->params.frequency, freq);
|
|
|
|
for (const auto& p : probe_points()) {
|
|
double want = evaluate(child, p)
|
|
+ amp * std::sin(p.x() * freq) * std::sin(p.y() * freq) * std::sin(p.z() * freq);
|
|
EXPECT_NEAR(evaluate(tree, p), want, 1e-12);
|
|
}
|
|
|
|
// The perturbation vanishes where any sine factor is zero (origin).
|
|
EXPECT_NEAR(evaluate(tree, Point3D(0, 0, 0)), evaluate(child, Point3D(0, 0, 0)), 1e-12);
|
|
}
|
|
|
|
TEST(SdfTreeFactory, MirrorYMatchesIndependentFold) {
|
|
auto child = SdfNode::translate(SdfNode::sphere(0.5), Point3D(0.0, 1.0, 0.0));
|
|
auto tree = SdfNode::mirror_y(child);
|
|
|
|
EXPECT_EQ(static_cast<int>(tree->op), static_cast<int>(SdfOp::MirrorY));
|
|
for (const auto& p : probe_points())
|
|
EXPECT_NEAR(evaluate(tree, p),
|
|
evaluate(child, Point3D(p.x(), std::abs(p.y()), p.z())), 1e-12);
|
|
|
|
// The sphere centred at y=+1 is mirrored to y=-1: both copies are solid.
|
|
EXPECT_LT(evaluate(tree, Point3D(0, 1.0, 0)), 0.0);
|
|
EXPECT_LT(evaluate(tree, Point3D(0, -1.0, 0)), 0.0);
|
|
EXPECT_GT(evaluate(tree, Point3D(0, 0.0, 0)), 0.0); // gap between the copies
|
|
}
|
|
|
|
TEST(SdfTreeFactory, MirrorZMatchesIndependentFold) {
|
|
auto child = SdfNode::translate(SdfNode::sphere(0.5), Point3D(0.0, 0.0, 1.0));
|
|
auto tree = SdfNode::mirror_z(child);
|
|
|
|
EXPECT_EQ(static_cast<int>(tree->op), static_cast<int>(SdfOp::MirrorZ));
|
|
for (const auto& p : probe_points())
|
|
EXPECT_NEAR(evaluate(tree, p),
|
|
evaluate(child, Point3D(p.x(), p.y(), std::abs(p.z()))), 1e-12);
|
|
|
|
EXPECT_LT(evaluate(tree, Point3D(0, 0, 1.0)), 0.0);
|
|
EXPECT_LT(evaluate(tree, Point3D(0, 0, -1.0)), 0.0);
|
|
EXPECT_GT(evaluate(tree, Point3D(0, 0, 0.0)), 0.0);
|
|
}
|
|
|
|
TEST(SdfTreeFactory, SmoothIntersectionMatchesIndependentBlend) {
|
|
const double k = 0.3;
|
|
auto a = SdfNode::sphere(1.0);
|
|
auto b = SdfNode::translate(SdfNode::sphere(1.0), Point3D(1.0, 0.0, 0.0));
|
|
auto tree = SdfNode::smooth_intersection(a, b, k);
|
|
|
|
EXPECT_EQ(static_cast<int>(tree->op), static_cast<int>(SdfOp::SmoothIntersection));
|
|
EXPECT_DOUBLE_EQ(tree->params.blend_k, k);
|
|
|
|
auto ref = [&](const Point3D& p) {
|
|
double d1 = evaluate(a, p), d2 = evaluate(b, p);
|
|
double h = std::clamp(0.5 - 0.5 * (d1 - d2) / k, 0.0, 1.0);
|
|
return d1 * (1.0 - h) + d2 * h + k * h * (1.0 - h);
|
|
};
|
|
for (const auto& p : probe_points())
|
|
EXPECT_NEAR(evaluate(tree, p), ref(p), 1e-12);
|
|
|
|
// Blend probe: at (0.5,0,0) both children evaluate to -0.5 → h=0.5, so the
|
|
// smooth result is -0.5 + k/4, strictly above the sharp intersection (-0.5).
|
|
Point3D bs(0.5, 0.0, 0.0);
|
|
EXPECT_NEAR(ref(bs), -0.5 + k * 0.25, 1e-12);
|
|
EXPECT_NEAR(evaluate(tree, bs), ref(bs), 1e-12);
|
|
EXPECT_GT(evaluate(tree, bs), -0.5);
|
|
}
|
|
|
|
TEST(SdfTreeFactory, SmoothDifferenceMatchesIndependentBlend) {
|
|
const double k = 0.3;
|
|
auto a = SdfNode::sphere(1.0);
|
|
auto b = SdfNode::translate(SdfNode::sphere(1.0), Point3D(2.0, 0.0, 0.0));
|
|
auto tree = SdfNode::smooth_difference(a, b, k);
|
|
|
|
EXPECT_EQ(static_cast<int>(tree->op), static_cast<int>(SdfOp::SmoothDifference));
|
|
EXPECT_DOUBLE_EQ(tree->params.blend_k, k);
|
|
|
|
auto ref = [&](const Point3D& p) {
|
|
double d1 = evaluate(a, p), d2 = evaluate(b, p);
|
|
double h = std::clamp(0.5 - 0.5 * (d2 + d1) / k, 0.0, 1.0);
|
|
return d1 * (1.0 - h) + (-d2) * h + k * h * (1.0 - h);
|
|
};
|
|
for (const auto& p : probe_points())
|
|
EXPECT_NEAR(evaluate(tree, p), ref(p), 1e-12);
|
|
|
|
// Blend probe: at (1,0,0) both children evaluate to 0 → h=0.5 → f = k/4,
|
|
// strictly above the sharp difference result (0).
|
|
Point3D bs(1.0, 0.0, 0.0);
|
|
EXPECT_NEAR(ref(bs), k * 0.25, 1e-12);
|
|
EXPECT_NEAR(evaluate(tree, bs), ref(bs), 1e-12);
|
|
EXPECT_GT(evaluate(tree, bs), 0.0);
|
|
}
|
|
|
|
// ═══════════════════════════════════════════════════════════════════
|
|
// VDE-141: SdfNode::hex_prism(radius, height, sides)
|
|
// The factory used to take only a height and hard-bound the cross-section
|
|
// circumradius to it. These tests pin the cross-section radius and the
|
|
// polygon side count as independent parameters.
|
|
// ═══════════════════════════════════════════════════════════════════
|
|
|
|
TEST(SdfTreeFactory, HexPrismRadiusIndependentOfHeight) {
|
|
const double R = 2.0;
|
|
auto short_p = SdfNode::hex_prism(R, 4.0);
|
|
auto tall_p = SdfNode::hex_prism(R, 20.0);
|
|
|
|
// Factory stores radius/height as distinct fields (previously radius==height).
|
|
EXPECT_DOUBLE_EQ(short_p->params.radius, R);
|
|
EXPECT_DOUBLE_EQ(short_p->params.height, 4.0);
|
|
EXPECT_EQ(short_p->params.sides, 6);
|
|
EXPECT_DOUBLE_EQ(tall_p->params.height, 20.0);
|
|
|
|
// Same circumradius → identical in-plane extent: (3,0,0) is 1.0 outside
|
|
// the +X vertex for both nodes, regardless of height.
|
|
EXPECT_NEAR(evaluate(short_p, Point3D(3, 0, 0)), 1.0, 1e-12);
|
|
EXPECT_NEAR(evaluate(tall_p, Point3D(3, 0, 0)), 1.0, 1e-12);
|
|
|
|
// Different height → the Y caps differ: a point above the short prism is
|
|
// inside the tall one.
|
|
EXPECT_GT(evaluate(short_p, Point3D(0, 6, 0)), 0.0); // 6 > 4/2
|
|
EXPECT_LT(evaluate(tall_p, Point3D(0, 6, 0)), 0.0); // 6 < 20/2
|
|
|
|
// Independent radius: same height, halved radius moves the surface inward.
|
|
auto narrow = SdfNode::hex_prism(1.0, 4.0);
|
|
EXPECT_NEAR(evaluate(narrow, Point3D(1.5, 0, 0)), 0.5, 1e-12); // just outside
|
|
EXPECT_LT(evaluate(short_p, Point3D(1.5, 0, 0)), 0.0); // deep inside
|
|
}
|
|
|
|
TEST(SdfTreeFactory, HexPrismSidesControlsCrossSection) {
|
|
// Same circumradius (1.0) → every polygon shares the +X vertex exactly.
|
|
auto tri = SdfNode::hex_prism(1.0, 4.0, 3);
|
|
auto sq = SdfNode::hex_prism(1.0, 4.0, 4);
|
|
auto hex = SdfNode::hex_prism(1.0, 4.0, 6);
|
|
|
|
EXPECT_EQ(tri->params.sides, 3);
|
|
EXPECT_EQ(sq->params.sides, 4);
|
|
EXPECT_EQ(hex->params.sides, 6);
|
|
|
|
for (const auto& node : {tri, sq, hex})
|
|
EXPECT_NEAR(evaluate(node, Point3D(1.0, 0, 0)), 0.0, 1e-12); // shared vertex
|
|
|
|
// Probe (0,0,0.9) lies on the +Z face-normal axis. Its signed distance
|
|
// depends on the side count: outside the triangle, inside the square,
|
|
// outside the hexagon (whose apothem is cos30°≈0.866 < 0.9).
|
|
Point3D q(0.0, 0.0, 0.9);
|
|
EXPECT_GT(evaluate(tri, q), 0.0);
|
|
EXPECT_LT(evaluate(sq, q), 0.0);
|
|
EXPECT_GT(evaluate(hex, q), 0.0);
|
|
|
|
// The triangle's +Z extent is smallest (apothem 0.5) → largest positive
|
|
// distance; the hexagon's is largest (apothem 0.866) → smallest positive.
|
|
EXPECT_GT(evaluate(tri, q), evaluate(hex, q));
|
|
}
|
|
|
|
TEST(SdfTreeFactory, HexPrismClampsInvalidSideCount) {
|
|
// sides < 3 is meaningless; the factory clamps to a triangle.
|
|
auto bad = SdfNode::hex_prism(1.0, 2.0, 2);
|
|
EXPECT_EQ(bad->params.sides, 3);
|
|
EXPECT_NEAR(evaluate(bad, Point3D(0, 0, 0.6)), evaluate(SdfNode::hex_prism(1.0, 2.0, 3), Point3D(0, 0, 0.6)), 1e-12);
|
|
}
|