Stacking rules
checkPlacement() answers one question for every algorithm: is this placement allowed? Its checks
run cheapest first and the first failure returns immediately (fail fast), because a packer calls
it thousands of times per pallet.
| Order | Check | Cost |
|---|---|---|
| 1 | Inside the pallet and under max height | O(1) |
| 2 | Total load under max | O(1) |
| 3 | No overlap; nothing above the spot; who supports us | O(n) single pass |
| 4 | Support ratio ≥ 75% | O(1) after the pass |
| 5 | Center inside the support polygon | O(k log k) |
| 6 | Not heavier than any supporter | O(supporters) |
Support ratio
Supported area = the sum of footprint overlaps with cartons whose top equals our bottom (or the whole base on the deck). Supporters cannot overlap each other (they would collide), so summing is safe.
The support polygon
Something stays standing when its center of mass, projected straight down, falls inside the outline of its contacts: the support polygon, which is the convex hull of the contact areas. A bridge across two pillars has only 50% of its area supported but is perfectly stable, because its centre is inside the hull. The live model shows a carton sliding across two pillars: blue is the hull, the carton turns orange when its centre leaves it.
Convex hull: Andrew's monotone chain
Sort the points by x then y; sweep left to right building the lower hull, then right to left for the upper hull. Whenever the last three points don't turn left, pop the middle one. O(k log k), the sort dominating; each point is pushed and popped at most once per sweep.
The turn test is the 2D cross product:
int64_t cross(P2 o, P2 a, P2 b) {
return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x); // > 0: left turn
}
Box centres can land on half millimetres, so the code doubles every coordinate and the maths stays in exact integers.
Load bearing and reachability
- Load bearing: a carton may not rest on a lighter one. Production systems sum the load down the whole "rests on" graph against each carton's crush strength.
- Reachable from above: a top-down gripper cannot slide a carton under an overhang, even into free space. Physically valid is not the same as robot-executable.