Infer parallel, perpendicular, equal radius and tangent while drawing

Port of snaporca 2d36d28770. Parity OK: 17 files identical, 8 diverging at their
expected counts.

infer_axis_constraint returned only Horizontal or Vertical. On the
CAD-1000-hours corpus the top two transitions are sketch_dim -> sketch_draw
(5896) and back (5756): the signature of geometry that does not self-constrain
as it is drawn.

Every rule requires the relation to be ALREADY TRUE within tolerance, so nothing
the user drew is moved; parallel/perpendicular and tangent additionally require a
shared endpoint.

TWO LIMITS THE CORPUS RUNG FORCED, neither visible to the unit tests:

1. At most ONE constraint per rule per new entity, not one per PAIR, and no
   one-at-a-time fallback for the relations batch. EqualRadius has no locality
   restriction, so 200 equal holes produced ~20000 candidates; the rejected batch
   then cost a solve per constraint and pinned the app at 95% of a core with the
   MCP socket unresponsive.

2. Relations only for gesture-sized batches. "A scripted add is not a drawn
   gesture" is already this file's rule at its bulk call site (snaporca-8xg1), and
   EqualRadius also couples geometrically distant entities, merging independent
   connected components and defeating the partitioning that makes large sketches
   solvable (snaporca-yww4). With the cap alone geometry stayed correct (32/32
   sheets clean) but seven of the largest timed out, including MPD681 -- the sheet
   that call site's own comment names.

Also fixes the tolerance leak behind 2: the bulk path asks for exact inference
with ang_tol_rad = 0 but len_tol_frac kept its 0.01 default.

ALSO independent of this feature: run-kernel-tests.sh defaulted to
TAGS=[CadDocument] while four CAD test files carry their own tags and nothing
selected them (2624 assertions / 206 cases reported, 7648 / 264 actual). All 58
dark cases were passing; the coverage was never exercised.

VERIFICATION LIMIT, as with the previous three commits: this fork's kernel suite
still cannot run (find_package(assimp) at configure time, snaporca-w80c). Shared
sources are byte-identical to snaporca's, where kernel is 7651 assertions / 265
cases and ALL LADDERS HELD 7/7.
This commit is contained in:
Tommaso Bianchi
2026-08-31 03:43:08 +02:00
parent 45494c6035
commit fac3cf44df
5 changed files with 259 additions and 1 deletions
+96
View File
@@ -76,3 +76,99 @@ TEST_CASE("inference: axis inference flags horizontal / vertical segments", "[in
CHECK_FALSE(infer_axis_constraint({0, 0}, {10, 10}).has_value()); // 45 deg
CHECK_FALSE(infer_axis_constraint({0, 0}, {0, 0}).has_value()); // degenerate
}
TEST_CASE("inference: perpendicular inferred for a connected square corner", "[inference]")
{
std::vector<SketchEntity> ents = { line({0, 0}, {10, 0}), line({10, 0}, {10, 7}) };
auto r = infer_relations(ents, 1);
REQUIRE(r.size() == 1);
CHECK(r[0].type == SketchConstraintType::Perpendicular);
CHECK(r[0].ea == 0);
CHECK(r[0].eb == 1);
}
TEST_CASE("inference: parallel inferred for connected collinear-ish lines", "[inference]")
{
std::vector<SketchEntity> ents = { line({0, 0}, {10, 0}), line({10, 0}, {21, 0.1}) };
auto r = infer_relations(ents, 1);
REQUIRE(r.size() == 1);
CHECK(r[0].type == SketchConstraintType::Parallel);
CHECK(r[0].ea == 0);
CHECK(r[0].eb == 1);
}
TEST_CASE("inference: two unconnected parallel lines infer nothing", "[inference]")
{
std::vector<SketchEntity> ents = { line({0, 0}, {10, 0}), line({0, 5}, {10, 5}) };
auto r = infer_relations(ents, 1);
CHECK(r.empty());
}
TEST_CASE("inference: a corner outside tolerance infers nothing", "[inference]")
{
std::vector<SketchEntity> ents = { line({0, 0}, {10, 0}), line({10, 0}, {15, 7}) };
auto r = infer_relations(ents, 1);
CHECK(r.empty());
}
TEST_CASE("inference: equal radius inferred for near-equal circles", "[inference]")
{
auto r = infer_relations({ circle({0, 0}, 5.0), circle({30, 0}, 5.02) }, 1);
REQUIRE(r.size() == 1);
CHECK(r[0].type == SketchConstraintType::EqualRadius);
CHECK(r[0].ea == 0);
CHECK(r[0].eb == 1);
auto r2 = infer_relations({ circle({0, 0}, 5.0), circle({30, 0}, 6.0) }, 1);
CHECK(r2.empty());
}
TEST_CASE("inference: tangent inferred for a line meeting a circle tangentially", "[inference]")
{
std::vector<SketchEntity> ents = { circle({0, 0}, 5.0), line({0, 5}, {10, 5}) };
auto r = infer_relations(ents, 1);
REQUIRE(r.size() == 1);
CHECK(r[0].type == SketchConstraintType::Tangent);
CHECK(r[0].ea == 0);
CHECK(r[0].eb == 1);
std::vector<SketchEntity> off = { circle({0, 0}, 5.0), line({0, 5}, {10, 9}) };
CHECK(infer_relations(off, 1).empty());
}
TEST_CASE("inference: nothing inferred against a higher index", "[inference]")
{
std::vector<SketchEntity> ents = { line({0, 0}, {10, 0}), line({10, 0}, {10, 7}) };
auto r = infer_relations(ents, 0);
CHECK(r.empty());
}
TEST_CASE("inference: degenerate entities are ignored", "[inference]")
{
std::vector<SketchEntity> ents = { line({0, 0}, {10, 0}), line({10, 0}, {10, 0}) };
auto r = infer_relations(ents, 1);
CHECK(r.empty());
}
// The cap that keeps infer_relations linear rather than quadratic. Without it a drawing with
// many equal holes yields a constraint per PAIR: 200 equal circles produced ~20000 candidates,
// the batch was rejected as over-constrained, and the caller's one-at-a-time fallback then ran
// a solve per constraint -- which pinned the app at 95% of a core with the MCP socket
// unresponsive, and is what the corpus rung caught.
TEST_CASE("inference: at most one relation per rule per new entity", "[inference]")
{
// 40 circles of the same radius; the 41st must not produce 40 EqualRadius constraints.
std::vector<SketchEntity> ents;
for (int i = 0; i < 41; ++i) {
SketchEntity c;
c.type = SketchEntity::Type::Circle;
c.center = Vec2d(i * 20.0, 0.0);
c.p0 = c.center;
c.radius = 5.0;
ents.push_back(c);
}
auto rels = infer_relations(ents, 40);
CHECK(rels.size() == 1);
CHECK(rels[0].type == SketchConstraintType::EqualRadius);
CHECK(rels[0].eb == 40);
}