<!-- https://docs.dseams.info/user/tutorials/voronoi.md -->

# Measure periodic Voronoi cells {#measure-periodic-voronoi-cells}

A Voronoi cell contains the points closest to one site. In a periodic
box, each site competes with all periodic images. The site coordinates
define an ordinary, equally weighted tessellation. Use the cell volumes
to measure how much space the selected sites occupy.

## Inspect a cell and its periodic neighbours {#inspect-a-cell-and-its-periodic-neighbours}

Install `pydseamslib` and import `pydseams`. Put one site in a periodic
cube with edges of 3 Å. The cell fills the box and each face meets a
periodic image of the same site.

```python
import numpy as np
import pydseams as ds

frame = ds.Frame.from_arrays([[0.3, 0.0, 2.8]], [3.0, 3.0, 3.0])
cells = frame.voronoi()
faces = cells.cell_faces(0)

np.testing.assert_allclose(cells.volumes, [27.0])
np.testing.assert_allclose(cells.surface_areas, [54.0])
np.testing.assert_allclose(cells.face_areas[faces], 9.0)
np.testing.assert_array_equal(cells.neighbours[faces], np.zeros(6, dtype=int))
np.testing.assert_array_equal(np.abs(cells.shifts[faces]).sum(axis=1), np.ones(6))
print("volume", cells.volumes[0], "faces", faces.stop - faces.start)
```

The result has volume 27 Å³ and six faces with area 9 Å² each. Every
neighbour index is zero. The shift triples distinguish the six images.
Add the site coordinate to `cells.face_vertices(faces.start)` to
place that face’s vertices in the original Cartesian coordinates.

When analysing a trajectory, select the sites whose cells you want.
For example, a frame containing only water oxygens partitions the box
among those oxygens. Including framework atoms changes that partition.

## Use the same result in C++ {#use-the-same-result-in-c}

Include `seams.hpp` and link against `libseams`. The `Voronoi` result
owns its arrays. Keep `cells` alive while using the view.

```cpp
#include <seams.hpp>
#include <array>
#include <cmath>

int main() {
  const std::array xyz{0.3, 0.0, 2.8};
  seams::System frame;
  frame.set_ortho(xyz, 3.0, 3.0, 3.0);
  const auto cells = frame.voronoi();
  const auto view = cells.view();
  return view.n_faces != 6 || std::abs(view.volumes[0] - 27.0) > 1e-10;
}
```

The C API uses the same layout. Call `seams_voronoi_find` to create a
result and `seams_voronoi_get_view` to borrow its arrays. Release the
result with `seams_voronoi_destroy`. The compiled code certifies that no
periodic image outside the search can reach the returned cells.

## How the engine handles sparse regions {#how-the-engine-handles-sparse-regions}

LinkCell builds the initial list of nearby sites. The search radius
starts from the site density. For a cell entirely contained within
half that radius, every distant bisector misses the cell. The engine
grows the search for other cells and asks LinkCell for the new shell of
periodic images. A partial cell without artificial faces
can reuse its supporting planes on a retry. All subsequent cuts can
only shrink it.
