Add physical dimensions to bin editor for correct aspect ratio

When de-perspectiving a bin photo taken at an angle, inferring the
output size from the quadrilateral shape squishes the result. Entering
real-world W×H (mm) lets the server use the correct aspect ratio,
scaled to the same resolution as the inferred size.

- Bin editor dialog: W×H number inputs, pre-filled from saved phys_w/phys_h
- PUT /api/bins/:id/corners: accepts optional phys_w/phys_h; when provided,
  derives bin_w/bin_h from the physical aspect ratio at equivalent area
- phys_w/phys_h stored on the bin record for re-use on next edit
- future-plans.md: bin types note (reusable dimensions per model)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-01 05:13:57 +00:00
parent 1ea14f8953
commit 320c6f1bd9
6 changed files with 60 additions and 6 deletions

View File

@@ -741,19 +741,34 @@ app.post('/api/bins', upload.single('image'), async (req, res) => {
app.put('/api/bins/:id/corners', async (req, res) => {
const bin = get_bin(req.params.id);
if (!bin) return fail(res, 'not found', 404);
const { corners } = req.body;
const { corners, phys_w, phys_h } = req.body;
if (!corners || corners.length !== 4) return fail(res, 'corners must be array of 4 points');
try {
// Delete old processed image if any
if (bin.image_filename) remove_image_file(bin.image_filename);
const source_path = join('./data/images', bin.source_id);
const { bin_w, bin_h } = compute_bin_size(corners);
let bin_w, bin_h;
if (phys_w > 0 && phys_h > 0) {
// Use physical aspect ratio scaled to the same area as computed size
const computed = compute_bin_size(corners);
const area = computed.bin_w * computed.bin_h;
const aspect = phys_w / phys_h;
bin_h = Math.round(Math.sqrt(area / aspect));
bin_w = Math.round(bin_h * aspect);
} else {
({ bin_w, bin_h } = compute_bin_size(corners));
}
const cells = await process_grid_image(source_path, corners, 1, 1, bin_w, bin_h, './data/images');
const image_filename = cells[0][0];
const updated = { ...bin, corners, image_filename, bin_w, bin_h, updated_at: Date.now() };
const updated = {
...bin, corners, image_filename, bin_w, bin_h,
phys_w: phys_w > 0 ? phys_w : (bin.phys_w ?? null),
phys_h: phys_h > 0 ? phys_h : (bin.phys_h ?? null),
updated_at: Date.now(),
};
set_bin(updated);
ok(res, { bin: updated });
} catch (err) {