# Terrain modelling and the edit-measure loop

Every `jifto_terrain` action evaluates the terrain, hands its mesh to the analyses and measures it
before it returns. A loop of edits reads each edit's own result; there is nothing to wait for.
An edit plus its cut and fill takes tens of milliseconds.

## 1. Existing ground

```json
jifto_geometry {"action": "add", "items": [{"kind": "mesh_grid", "origin": [-80, -80], "size": [160, 160],
  "heights": [[...], ...], "name": "Existing ground", "layer": "Site"}]}
jifto_terrain {"action": "create", "object_id": "<mesh id>", "name": "Site"}
```

The object becomes the terrain's **Base**: cut and fill are measured against it. A closed curve makes
a flat terrain inside it; a mesh or surface keeps its shape.

A terrain can stand on another: `{"action": "create", "on_terrain": "<terrain id>"}`. Its Base is
that terrain's surface, and it follows that terrain's edits in the same call (a design option on the
existing site, measured against the site as modelled).

## 2. Modifiers, by what they follow

```json
jifto_terrain {"action": "add", "terrain": "Site", "kind": "path", "sources": ["<curve id>"]}
jifto_terrain {"action": "add", "terrain": "Site", "kind": "pad", "sources": ["<slab id>"],
  "settings": {"Cut Slope": 30, "Fill Slope": 26}}
jifto_terrain {"action": "add", "terrain": "Site", "kind": "pond", "sources": ["<closed rim curve id>"],
  "settings": {"Depth": 1.5, "Water Level": 5}}
```

Kinds: pad, path, pond, stream, wall, stairs, void, point, breakline, boundary, hole, material, remesh.
`describe` lists each modifier's id (m1, m2 ...), settings with their ranges, and sources.

Every reply has `metrics`:

```json
"metrics": {"elevation_min_m": -2.9, "elevation_max_m": 4.1, "triangles": 31840,
  "water": [{"volume_m3": 412.5, "at": [30, -25, -1.2]}],
  "cut_fill": {"cut_m3": 318.4, "fill_m3": 951.2, "net_m3": 632.8, "moved_m3": 1269.6,
    "cut_area_m2": 410.2, "fill_area_m2": 822.0, "deepest_cut_m": -1.42, "highest_fill_m": 2.31,
    "resolution_m": 0.25, "bands": [...]}}
```

`net_m3` is fill minus cut (positive: earth to bring in); `moved_m3` is cut plus fill.

## 3. The loop

To change what a modifier follows, edit its source object; the terrain follows in the same call.
Batch the edit and the measurement to save round trips:

```json
jifto_batch {"calls": [
  {"tool": "jifto_geometry", "arguments": {"action": "update", "id": "<path curve id>", "points": [[...], ...]}},
  {"tool": "jifto_terrain", "arguments": {"action": "measure", "terrain": "Site"}}]}
```

Setting changes measure in the same call:

```json
jifto_terrain {"action": "set", "terrain": "Site", "modifier": "m3", "settings": {"Depth": 2.1}}
```

To raise a pad, move its slab: `jifto_geometry {"action": "move", "ids": ["<slab id>"], "by": [0, 0, 0.1]}`.

## 4. Showing it

`jifto_terrain {"action": "set", "terrain": "Site", "surface": "cut_fill"}` colours the terrain by cut
and fill (the reply carries its legend); then `jifto_capture`. `"surface": "material"` goes back.
