Brokkr/surfaces/blender/docs/reference.md
Meta Vens 7c95538ea6 docs/reference.md: the refusals of undecidable marks, and the band's step as its resolution
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-12-02 19:40:42 +01:00

130 lines
9.4 KiB
Markdown

# Reference views: the camera a picture was drawn through
`surfaces/blender/brokkr_reference` is a Blender add-on that fits a scene to a reference image. A concept sheet,
a render or a photograph draws its subject through a camera nobody wrote down; given points whose place in the
world is known and the pixel where the image draws each, `solve_camera` finds that camera, so that a render through
it lays over the image pixel for pixel and a line traced on the image can be lifted into the scene.
It is a host add-on in Brokkr's sense (`install.md`, "Host add-on actions"), a sibling of `brokkr_mesh_polish`: it
imports that package's operator base and `Refused`, and nothing from `brokkr_bridge`. Enable `brokkr_mesh_polish`
first; `brokkr_reference` refuses to register without it. `brokkr_actions.json` beside its `__init__.py` lists the
one operator agents may run; the arithmetic is `core.py` (numpy only, tested without Blender), which a script may
import directly. The action writes its JSON report to `brokkr_mesh_polish_result`, returned in the receipt's
`hostResult`.
The package takes points and gives cameras. The images, the marks on them, traces and masks belong to whoever
made them; nothing here opens an image.
## Conventions
- World: x right, y forward, z up, metres.
- Pixels: the image's, in whatever unit the points were marked in, u right and v down. `box` is the view's
rectangle on the image, `u0,v0,u1,v1`; for an image that is one view it is `0,0,width,height`.
- Camera record: `C` is where the camera stands, `R` turns world to camera (rows), the camera looks down its own
-z with y up (Blender's), `f` is the focal length in pixels, `c` the principal point in pixels. `lens` is `f`
in millimetres on a 36 mm sensor across the box's width.
- Misses are weighed by a pseudo-Huber loss of 10 px: a build poses some points elsewhere than the image draws
them, and one such point pulls the camera as its distance, not its square.
The record is the truth. A Blender camera stood at it (`core.blender`) is derived, and `core.camera_of` reads one
back into a record.
## The action
`brokkr_reference.solve_camera`. Host action fields are scalar, so points go in as text:
| field | |
| --- | --- |
| `world` | `name=x,y,z; name=x,y,z ...` |
| `pixels` | `name=u,v; name=u,v ...`; six or more names shared with `world`, the rest ignored |
| `box` | `u0,v0,u1,v1` |
| `model` | `auto` (default), `upright`, `rolled`, `free`, `square` |
| `lens` | mm; 0 (default) solves it, 4 to 4000 holds it |
| `turned` | names, spaces between, of points that turn as one about the vertical through `pivot` |
| `pivot` | `x,y`; absent is `0,0` |
| `camera_name` | a camera object to create or update at the solved camera; absent only computes |
| `width` | pixels across the frame the box becomes (default 1000) |
The models differ in what the camera may do beyond standing somewhere, turning and zooming. `upright` holds it
level with its centre at the middle of the box. `rolled` lets it roll about its line of sight. `free` also solves
the centre: a view cropped off-axis. `auto` solves all three and takes the one that predicts left-out points best
(each point predicted by the camera solved without it), since a freer model always fits the marked points closer.
`square` is for an elevation drawn square-on from far off: the points lie nearly in one plane across the line of
sight, where a small turn of the camera and a narrower subject draw the same picture, so the camera is held looking
straight along a world axis, whichever of the four fits best.
`turned` is for one articulated group (a torso on its waist ring): its turn about the vertical through `pivot` is
solved with the camera and reported as `twist`, in degrees from where the points were given.
With `camera_name` the action creates that camera, or updates it if it exists (an object of that name that is not
a camera is refused): its matrix, lens, shifts, a 36 mm sensor fitted horizontally, `clip_end` raised to four
times the camera's distance if it was nearer, and the scene's render resolution set to `width` by the box's
proportion. It does not make it the scene's active camera and does not touch `resolution_percentage`. A pixel of a
render through it at 100% is then the image's pixel less the box's corner, times `width` over the box's width
(`core.in_frame`; the frame's height is a whole number of pixels, so it can stand half a pixel off the box's).
The report is the camera record: `C`, `R`, `f`, `c`, `box`, `lens`, `lens_band`, `lens_at_bound`, `rms`, `median`
and `worst` (`[name, pixels]`), `left_out` (the leave-one-out rms), `model`, `tried` (each model's `rms`,
`left_out` and `lens`), `azimuth`, `elevation` and `distance` (where the camera stands round the points' middle),
`roll`, `twist`, and `residuals` (`{name: [du, dv]}`, to a hundredth of a pixel in a receipt); plus `camera` (the
object's name or null) and, when a camera was stood, `blender` (what `core.blender` gave it).
Refusals name their cause: fewer than six shared names, a point that is not finite numbers, a name given twice, a
box of no width or height, a lens outside 4 to 4000 mm. Marks that cannot decide a camera are refused before any
solve: fewer than six different world points or six different pixels among the shared names (the message names two
that share one), pixels all on one line, world points all on one line, and world points all in one plane unless
`lens` is held and the model is `upright`, `rolled` or `square`. These are exact cases; points nearly in a line or
a plane are solved, and the band says what they leave open. A report never holds a number that is not finite.
## How far to trust the lens
Six or more points that are not all in one plane decide a camera. What they decide well is the picture: where each
point is drawn. What a long or square-on view decides badly is how far off the camera stands, because distance
and focal length trade against each other (a dolly zoom) and the picture barely changes. So:
- The focal length is held inside 4 to 4000 mm. Without a bound the solve ran the focal length to overflow on long
views; past 4000 mm a view is orthographic to any marking. `lens_at_bound` is `"long"` or `"short"` when the
answer sits at an end: read that as "at least this long", not as a lens.
- `lens_band` is `[low, high]`: the lenses that, held and refitted, come within 1.1 times the best rms, walked out
from the best in steps of a tenth in log f until one misses or the bound is reached. It is an **equal-error
band, not a confidence interval**: it says which lenses the marks cannot tell apart, not where the true lens
lies, and on a probe one such band excluded the true lens. A band that ends at 4000 was stopped by the bound.
The walk's step is the band's resolution: each end is the last step that still fitted, so the true end lies up
to one step (about 10%) further out, and a band of `[x, x]` means the first step each way already missed, that
is, a band narrower than one 10% step each way, not a lens known exactly.
- There is no prior. Where the data cannot decide, the answer is a wide band rather than a plausible-looking 50 mm.
- Hold `lens` only when views must agree: two views of one subject solved alone may each sit anywhere in their
bands, and a fit rendered from elsewhere than the reference view depends on the lens the band does not fix.
What this buys and costs, measured on a generated hull rendered at known cameras with 10 to 16 marked points
(Blender 5.3 on Starfire): a 50 mm oblique view with 1 px of marking noise came back at 49.9 mm; a 135 mm view on a
build 2% of its length out came back anywhere from 94 mm to the bound, with the camera's place off by eleven hull
lengths, and a crease lifted through that camera onto the hull's mirror plane was still within 1.4% of the hull's
length of the truth, moving at most 0.6% between the band's ends. A wide band is a warning about the camera's
place, not about a lift made in the view's own frame.
`rms` is the plain root mean square of the misses, in pixels at the marking unit, though the solve minimises the
pseudo-Huber loss; `median` beside it shows when a few points carry the error. A solve with `auto` on 13 points
takes several seconds (three models, each from 16 starts, plus a left-out solve per point per model).
## What it cannot do
It solves one pinhole camera for one view: no lens distortion, no two views solved together, one turned group and
no other pose. It does not find the points; marking them is the caller's. Points on posed joints fit worse than
points on rigid features of one body, because the build's pose is not the drawing's. It does not say whether the
subject is the right size: a build twice too large is the same picture from twice as far.
## From a script
```python
from brokkr_reference import core
cam = core.solve(world, pixels, box, model="auto") # dicts by name; the record
u_v = core.project(cam, points) # where it draws world points
origin, direction = core.ray(cam, (u, v)) # a pixel's line of sight
point = core.lift(cam, (u, v), plane_point, plane_normal) # a traced pixel on a plane
params = core.blender(cam, 1000) # matrix_world, lens, shifts, resolution
cam2 = core.camera_of(params, box) # and back
```
The tests are `tests/test_reference.py` (no Blender) and `tests/live/reference.py` (a known scene rendered,
solved through the action and rendered again; `reference.result.json` is its last run).