Skip to content

Commit d2aa265

Browse files
committed
demo: Add a unit test for computeSphereBounds
We validate that the points and radii are used correctly and produce reasonable results. Note that the axis aligned tetrahedron is a suboptimal input for Ritter's algorithm but slightly adjusting the radii makes it pick a good estimate.
1 parent 4006da2 commit d2aa265

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

demo/tests.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,36 @@ static void clusterBoundsDegenerate()
10591059
assert(bounds2.center[2] - bounds2.radius <= 0 && bounds2.center[2] + bounds2.radius >= 1);
10601060
}
10611061

1062+
static void sphereBounds()
1063+
{
1064+
const float vbr[] = {
1065+
0, 0, 0, 0,
1066+
0, 1, 0, 1,
1067+
0, 0, 1, 2,
1068+
1, 0, 1, 3, // clang-format
1069+
};
1070+
1071+
// without the radius, the center is somewhere inside the tetrahedron
1072+
// note that we currently compute a somewhat suboptimal sphere here due to the tetrahedron being perfecly axis aligned
1073+
meshopt_Bounds bounds = meshopt_computeSphereBounds(vbr, 4, sizeof(float) * 4, NULL, 0);
1074+
assert(bounds.radius < 0.97f);
1075+
1076+
// forcing a better initial guess for the center by using different radii produces a close to optimal sphere
1077+
float eps[4] = {1e-3f, 2e-3f, 3e-3f, 4e-3f};
1078+
meshopt_Bounds boundse = meshopt_computeSphereBounds(vbr, 4, sizeof(float) * 4, eps, sizeof(float));
1079+
assert(fabsf(boundse.center[0] - 0.5f) < 1e-2f);
1080+
assert(fabsf(boundse.center[1] - 0.5f) < 1e-2f);
1081+
assert(fabsf(boundse.center[2] - 0.5f) < 1e-2f);
1082+
assert(boundse.radius < 0.87f);
1083+
1084+
// when using the radius, the last sphere envelops the entire set
1085+
meshopt_Bounds boundsr = meshopt_computeSphereBounds(vbr, 4, sizeof(float) * 4, vbr + 3, sizeof(float) * 4);
1086+
assert(fabsf(boundsr.center[0] - 1.f) < 1e-2f);
1087+
assert(fabsf(boundsr.center[1] - 0.f) < 1e-2f);
1088+
assert(fabsf(boundsr.center[2] - 1.f) < 1e-2f);
1089+
assert(fabsf(boundsr.radius - 3.f) < 1e-2f);
1090+
}
1091+
10621092
static void meshletsEmpty()
10631093
{
10641094
const float vbd[4 * 3] = {};
@@ -2256,6 +2286,7 @@ void runTests()
22562286
encodeFilterExpClamp();
22572287

22582288
clusterBoundsDegenerate();
2289+
sphereBounds();
22592290

22602291
meshletsEmpty();
22612292
meshletsDense();

0 commit comments

Comments
 (0)