Geometry

Voxel boxes, subvolume sample templates, shape functions and iDVC-style point grids. See Correlation method.

zvdvc.geometry.box

Integer voxel boxes, half-open, in array order (z, y, x).

class zvdvc.geometry.box.Box(lo: 'Int3', hi: 'Int3')[source]

Bases: object

Parameters:
lo: tuple[int, int, int]
hi: tuple[int, int, int]
property shape: tuple[int, int, int]
voxels()[source]
Return type:

int

grow(margin)[source]

Expand by margin voxels on every side (rounded outward). A 3-tuple margin is (z, y, x).

Parameters:

margin (float | tuple[int, int, int])

Return type:

Box

shift_xyz(offset_xyz)[source]

Translate by a displacement given in point order (x, y, z), rounded outward.

Parameters:

offset_xyz (tuple[float, float, float])

Return type:

Box

intersect(other)[source]
Parameters:

other (Box)

Return type:

Box | None

slices(origin=(0, 0, 0))[source]

Index expression selecting this box from an array whose [0, 0, 0] sits at origin.

Parameters:

origin (tuple[int, int, int])

Return type:

tuple[slice, slice, slice]

classmethod around_points(xyz_min, xyz_max)[source]

Smallest box holding every voxel whose coordinate lies in [xyz_min, xyz_max].

Parameters:
Return type:

Box

zvdvc.geometry.templates

Subvolume sample templates.

As in CCPi, samples are not voxel-centred and their number is independent of subvolume size. Interpolation supplies values at arbitrary positions.

Unlike CCPi, which builds a FloatingCloud per point, zvDVC builds one template per run and shares it across all points. On the GPU the template offsets sit in constant/shared memory, so a batch is described by its centres and parameters alone and per-point sample coordinates are never stored.

  • cube: a k x k x k grid with k = ceil(n_samples ** (1/3)), spanning [-r*aspect, +r*aspect] per axis (CCPi Search rounds the count up the same way).

  • sphere: n_samples points uniform inside the ellipsoid of radius r*aspect, by rejection from the bounding cube with a seeded Mersenne Twister. CCPi does the same with std::mt19937. The streams differ, so parity with CCPi is statistical (docs/MVP_PLAN.md, M1).

Offsets are stored in z, y, x order (the cube is built that way; sphere samples are sorted). A kernel’s neighbouring threads take neighbouring samples, so this order keeps their volume reads in the same cache lines: 6.8x faster sums on the GPU on real data, and the same sums up to rounding.

class zvdvc.geometry.templates.Template(offsets: 'np.ndarray', spec: 'SubvolumeSpec')[source]

Bases: object

Parameters:
offsets: ndarray
spec: SubvolumeSpec
property n_samples: int
extent()[source]

Largest |offset|. Rotations can bring any sample this far along any axis, so it sizes the halo.

Return type:

float

digest()[source]

Hash of the offsets, recorded with results so a resumed run cannot mix templates.

Return type:

str

zvdvc.geometry.templates.cube_side(n_samples)[source]

Smallest k with k**3 >= n_samples (exact in integers; ceil(n ** (1/3)) is not).

Parameters:

n_samples (int)

Return type:

int

zvdvc.geometry.templates.make_template(spec)[source]
Parameters:

spec (SubvolumeSpec)

Return type:

Template

zvdvc.geometry.warp

Shape functions: 3, 6 and 12 degrees of freedom, in CCPi’s parameterisation.

Parameter vector (CCPi SearchParams), angles in radians, strain dimensionless:

index  0  1  2   3    4    5    6    7    8    9    10   11
name   u  v  w   phi  the  psi  exx  eyy  ezz  exy  eyz  exz

A template offset d around centre c maps to

x’ = c + t + (I + E) R d

where t = (u, v, w), R is CCPi’s roll-pitch-yaw matrix (SearchParams::set_param_vect) and E is the symmetric strain tensor. This matches FloatingCloud::affine_to: translate, then rotate_ptvect (d -> R d), then strain_ptvect (d' -> d' + E d'), both relative to the moved reference point. ndof = 3 gives R = I, E = 0; ndof = 6 gives E = 0.

The forward-additive solver (CCPi parity) needs dx'/dp at the current p. That is nonlinear in the angles and is computed analytically per point and iteration: 3 x ndof values per sample, cheap next to interpolation. The inverse-compositional solver (M5) needs only dx'/dp at p = 0, precomputed once per run from the template.

zvdvc.geometry.warp.rotation_matrix(angles)[source]

(B, 3) -> (B, 3, 3), CCPi roll-pitch-yaw.

angles = (phi, the, psi): yaw about z, pitch about y, roll about x, composed as R = Rx(psi) Ry(the) Rz(phi) (the aerospace 3-2-1 direction-cosine matrix).

Parameters:

angles (Any)

Return type:

Any

zvdvc.geometry.warp.strain_tensor(strains)[source]

(B, 6) (exx, eyy, ezz, exy, eyz, exz) -> (B, 3, 3) symmetric.

Parameters:

strains (Any)

Return type:

Any

zvdvc.geometry.warp.deformation_matrix(params)[source]

(B, ndof) -> (I + E) R as (B, 3, 3).

Parameters:

params (Any)

Return type:

Any

zvdvc.geometry.warp.warp(centres, params, offsets)[source]

(B, 3), (B, ndof), (M, 3) -> (B, M, 3) warped sample positions.

Parameters:
Return type:

Any

zvdvc.geometry.warp.warp_linear_jacobians(params)[source]

dx'/dp_k = A_k d for the non-translation parameters: (B, ndof) -> (B, ndof - 3, 3, 3).

Translations have dx'/dt = I. Solvers contract A_k with the image gradient and the template, so the (B, M, 3, ndof) array of warp_jacobian() is never formed.

Parameters:

params (Any)

Return type:

Any

zvdvc.geometry.warp.warp_jacobian(params, offsets)[source]

dx'/dp: (B, ndof), (M, 3) -> (B, M, 3, ndof). Forward-additive solver.

Parameters:
Return type:

Any

zvdvc.geometry.warp.warp_jacobian_at_identity(offsets, ndof)[source]

dx'/dp at p = 0: (M, 3, ndof). Inverse-compositional solver.

Parameters:
Return type:

Any

zvdvc.geometry.warp.compose_inverse(params, delta)[source]

IC-GN update W(p) <- W(p) o W(delta)^-1, returned in CCPi parameters.

Parameters:
Return type:

Any

zvdvc.geometry.pointgrid

Point-cloud generation, following iDVC’s Point Cloud panel.

iDVC places points on a regular lattice inside a mask. The spacing comes from subvolume size and overlap, with an optional lattice rotation. The mask can optionally be eroded so whole subvolumes stay inside it. zvDVC does the same in chunks, so a 4096^3 mask never has to be in memory at once, and writes the result straight into a zarr-vectors store (zvdvc.io.pointcloud.write_pointcloud_store()).

zvdvc.geometry.pointgrid.lattice_spacing(subvolume, overlap)[source]

Point spacing (x, y, z) for a given fractional overlap, as iDVC computes it.

Neighbouring subvolumes overlap by overlap of their extent along each axis, so the spacing is size * aspect * (1 - overlap).

Parameters:
Return type:

tuple[float, float, float]

zvdvc.geometry.pointgrid.grid_in_mask(shape_zyx, spacing_xyz, *, mask=None, rotation_deg=(0.0, 0.0, 0.0), erode_radius=0.0, origin_xyz=None)[source]

Lattice points (N, 3) inside mask (a (z, y, x) array or a zarr array read slab by slab).

The lattice passes through origin_xyz (default: the volume centre) and is rotated about it. Points are kept if they lie inside the volume and, with a mask, if the nearest mask voxel is set and (erode_radius > 0) at least erode_radius voxels from the nearest unset voxel. Points come out sorted by (z, y, x).

Parameters:
Return type:

ndarray