adk-vision 3.0.0

Local-first perception for ADK-Rust agents: artifact-backed images, regions in source pixels with their transform chains, model manifests pinned by revision and SHA-256, perception traits with conformance suites, routing with crop escalation, and a governed egress boundary
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! [`NimDocumentReader`]: a page read by NVIDIA's OCR, page-element, table-structure and
//! chart-element endpoints, composed into one [`DocumentPage`].

use async_trait::async_trait;
use base64::Engine;

use super::client::CvClient;
use super::config::NimCvConfig;
use super::wire::{self, NormBox, NormPoint, TextDetection};
use crate::backend::{BackendDescriptor, InputLimits, Locality};
use crate::cx::VisionCx;
use crate::edit::ops;
use crate::egress::ReleasedImage;
use crate::error::{Result, VisionError};
use crate::geometry::{AxisOrder, BBox, CoordFrame, Polygon, RawRegion, Shape};
use crate::reading::{self, Piece};
use crate::traits::{Block, BlockKind, DocumentPage, DocumentReader, ReadOptions, TableCell};

/// The page-element model.
pub const PAGE_ELEMENTS_MODEL: &str = "nvidia/nemotron-page-elements-v3";
/// The table-structure model, called on each table's crop.
pub const TABLE_STRUCTURE_MODEL: &str = "nvidia/nemotron-table-structure-v1";
/// The chart-element model, called on each chart's crop.
pub const GRAPHIC_ELEMENTS_MODEL: &str = "nvidia/nemotron-graphic-elements-v1";

/// The OCR model a [`NimDocumentReader`] reads text with.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NimOcrModel {
    /// `nvidia/nemotron-ocr-v1`, asked for words, which the reader groups into lines.
    NemotronOcrV1,
    /// `nvidia/nemoretriever-ocr`, the same model under its retriever name.
    NemoretrieverOcr,
    /// `baidu/paddleocr`, which answers in lines.
    PaddleOcr,
}

impl NimOcrModel {
    /// The model's `org/name`.
    pub fn id(self) -> &'static str {
        match self {
            Self::NemotronOcrV1 => "nvidia/nemotron-ocr-v1",
            Self::NemoretrieverOcr => "nvidia/nemoretriever-ocr",
            Self::PaddleOcr => "baidu/paddleocr",
        }
    }

    fn merge_levels(self) -> Option<&'static [&'static str]> {
        match self {
            Self::NemotronOcrV1 | Self::NemoretrieverOcr => Some(&["word"]),
            Self::PaddleOcr => None,
        }
    }
}

/// A [`DocumentReader`] over NVIDIA's CV endpoints: OCR for text, page elements for blocks,
/// table structure for cells and chart elements for chart labels.
///
/// Remote: it implements the trait for [`ReleasedImage`] only, so a page reaches it only
/// through the egress gate. It sends the released PNG inline; table and chart crops are cut
/// from the released pixels, so nothing outside the released image leaves. Answers are in
/// 0–1 of the image sent (`CoordFrame::Normalized { scale: 1, order: Xy }`), and the runtime
/// maps them to source pixels. NVIDIA publishes no per-call price, so the descriptor
/// carries none and calls are capped by units (images and pages).
///
/// | Step | Endpoint | Calls per page |
/// |------|----------|----------------|
/// | Text | the [`NimOcrModel`] | 1 |
/// | Blocks | [`PAGE_ELEMENTS_MODEL`] | 1, when layout is on |
/// | Table cells | [`TABLE_STRUCTURE_MODEL`] | 1 per table, when tables are on |
/// | Chart labels | [`GRAPHIC_ELEMENTS_MODEL`] | 1 per chart, when charts are on |
///
/// # Example
///
/// ```
/// use adk_vision::{CoordFrame, NimCvConfig, NimDocumentReader, NimOcrModel, DocumentReader, ReleasedImage};
///
/// let reader = NimDocumentReader::new(
///     NimCvConfig::new("https://ai.api.nvidia.com/v1/cv").with_api_key_from_env(),
/// )
/// .unwrap()
/// .with_ocr(NimOcrModel::NemotronOcrV1)
/// .with_charts(false);
/// let descriptor = DocumentReader::<ReleasedImage>::descriptor(&reader);
/// assert!(descriptor.locality.is_remote());
/// assert!(descriptor.price.is_none());
/// assert!(matches!(descriptor.frame, CoordFrame::Normalized { scale: 1, .. }));
/// ```
#[derive(Debug, Clone)]
pub struct NimDocumentReader {
    descriptor: BackendDescriptor,
    client: CvClient,
    ocr: NimOcrModel,
    layout: bool,
    min_layout_confidence: f32,
    tables: bool,
    charts: bool,
}

/// The default floor below which a page element is ignored: the page-element model also
/// answers faint boxes (0.18 around a chart's axis label in the probe) that would split a
/// block.
pub const DEFAULT_MIN_LAYOUT_CONFIDENCE: f32 = 0.3;

/// The default backend id.
pub const NIM_READER_ID: &str = "nim-document-reader";

impl NimDocumentReader {
    /// A reader with `nemotron-ocr-v1`, page elements, table structure and chart elements.
    ///
    /// # Errors
    ///
    /// [`VisionError::InvalidInput`] when a configured URL does not parse or names no host,
    /// or the HTTP client cannot start.
    pub fn new(config: NimCvConfig) -> Result<Self> {
        let client = CvClient::new(config)?;
        let mut reader = Self {
            descriptor: BackendDescriptor::new(
                NIM_READER_ID,
                Locality::Remote { hosts: Vec::new() },
                CoordFrame::Normalized { scale: 1, order: AxisOrder::Xy },
            )
            .with_provider("nvidia")
            // The CV endpoints answered 1920×1080 inline in the probe; larger pages are
            // downscaled and the resize recorded.
            .with_limits(InputLimits::long_edge(2048)),
            client,
            ocr: NimOcrModel::NemotronOcrV1,
            layout: true,
            min_layout_confidence: DEFAULT_MIN_LAYOUT_CONFIDENCE,
            tables: true,
            charts: true,
        };
        // Every model the reader may call is checked now, so later builder calls cannot fail.
        for model in [
            NimOcrModel::NemotronOcrV1.id(),
            NimOcrModel::NemoretrieverOcr.id(),
            NimOcrModel::PaddleOcr.id(),
            PAGE_ELEMENTS_MODEL,
            TABLE_STRUCTURE_MODEL,
            GRAPHIC_ELEMENTS_MODEL,
        ] {
            let url = reader.client.config().url_for(model);
            let parsed = reqwest::Url::parse(&url).map_err(|e| {
                VisionError::InvalidInput(format!("NIM URL `{url}` for `{model}` is invalid: {e}"))
            })?;
            if parsed.host_str().is_none_or(str::is_empty) {
                return Err(VisionError::InvalidInput(format!(
                    "NIM URL `{url}` for `{model}` names no host"
                )));
            }
        }
        reader.refresh_hosts();
        Ok(reader)
    }

    /// Sets the backend id the runtime registers it under.
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.descriptor.id = crate::backend::BackendId::new(id);
        self
    }

    /// Sets the input limits.
    pub fn with_limits(mut self, limits: InputLimits) -> Self {
        self.descriptor.limits = limits;
        self
    }

    /// Reads text with `model`.
    pub fn with_ocr(mut self, model: NimOcrModel) -> Self {
        self.ocr = model;
        self.refresh_hosts();
        self
    }

    /// Detects blocks with the page-element model; off, every line is its own block.
    pub fn with_layout(mut self, on: bool) -> Self {
        self.layout = on;
        self.refresh_hosts();
        self
    }

    /// Ignores page elements whose confidence is below `floor`.
    pub fn with_min_layout_confidence(mut self, floor: f32) -> Self {
        self.min_layout_confidence = floor;
        self
    }

    /// Reads table cells from each table's crop.
    pub fn with_tables(mut self, on: bool) -> Self {
        self.tables = on;
        self.refresh_hosts();
        self
    }

    /// Labels chart text from each chart's crop.
    pub fn with_charts(mut self, on: bool) -> Self {
        self.charts = on;
        self.refresh_hosts();
        self
    }

    fn models(&self) -> Vec<&'static str> {
        let mut models = vec![self.ocr.id()];
        if self.layout {
            models.push(PAGE_ELEMENTS_MODEL);
            if self.tables {
                models.push(TABLE_STRUCTURE_MODEL);
            }
            if self.charts {
                models.push(GRAPHIC_ELEMENTS_MODEL);
            }
        }
        models
    }

    /// The descriptor's hosts are those of every URL the reader calls, so the egress
    /// policy decides each one. `new` checked that every URL parses and names a host.
    fn refresh_hosts(&mut self) {
        let mut hosts: Vec<String> = Vec::new();
        for model in self.models() {
            let url = self.client.config().url_for(model);
            let host = reqwest::Url::parse(&url)
                .ok()
                .and_then(|u| u.host_str().map(str::to_string))
                .unwrap_or_default();
            if !hosts.contains(&host) {
                hosts.push(host);
            }
        }
        self.descriptor.locality = Locality::Remote { hosts };
    }

    fn data_uri(&self, png: &[u8]) -> Result<String> {
        let max = self.client.config().max_inline_bytes();
        if png.len() > max {
            return Err(VisionError::InputTooLarge {
                backend: self.descriptor.id.to_string(),
                limit: format!("{max} bytes inline"),
                actual: format!("{} bytes", png.len()),
            });
        }
        Ok(format!(
            "data:image/png;base64,{}",
            base64::engine::general_purpose::STANDARD.encode(png)
        ))
    }

    async fn call(
        &self,
        model: &str,
        data_uri: &str,
        merge: Option<&[&str]>,
    ) -> Result<serde_json::Value> {
        let body = wire::body(data_uri, merge);
        self.client.infer(self.descriptor.id.as_str(), model, &body).await
    }

    /// Calls `model` on the crop of `released` at `crop` (sent pixels) and returns its boxes
    /// in 0–1 of the whole sent image, or nothing when the crop is degenerate.
    async fn on_crop(
        &self,
        model: &str,
        released: &ReleasedImage,
        crop: BBox,
    ) -> Result<Vec<(String, NormBox)>> {
        let (w, h) = released.image().size();
        let (x, y, cw, ch) = crop.pixel_bounds(w, h);
        if cw < 8 || ch < 8 {
            return Ok(Vec::new());
        }
        let pixels = ops::crop(released.pixels(), x, y, cw, ch);
        let uri = self.data_uri(&pixels.encode_png()?)?;
        let value = self.call(model, &uri, None).await?;
        let found = wire::elements(self.descriptor.id.as_str(), model, value)?;
        let (fw, fh, fx, fy) = (w as f32, h as f32, x as f32, y as f32);
        let (cw, ch) = (cw as f32, ch as f32);
        Ok(found
            .into_iter()
            .map(|(class, b)| {
                let to_page = NormBox {
                    x_min: (fx + b.x_min * cw) / fw,
                    y_min: (fy + b.y_min * ch) / fh,
                    x_max: (fx + b.x_max * cw) / fw,
                    y_max: (fy + b.y_max * ch) / fh,
                    confidence: b.confidence,
                };
                (class, to_page)
            })
            .collect())
    }
}

/// A block of the page-element model, in 0–1 and in sent pixels.
struct Element {
    class: String,
    norm: BBox,
    px: BBox,
    confidence: Option<f32>,
}

fn kind_of(class: &str) -> BlockKind {
    match class {
        "title" => BlockKind::Title,
        "table" => BlockKind::Table,
        "chart" | "infographic" => BlockKind::Figure,
        _ => BlockKind::Text,
    }
}

fn norm_box(b: &NormBox) -> BBox {
    BBox::new(b.x_min, b.y_min, b.x_max, b.y_max)
}

fn to_px(b: &BBox, (w, h): (u32, u32)) -> BBox {
    BBox::new(b.x0 * w as f32, b.y0 * h as f32, b.x1 * w as f32, b.y1 * h as f32)
}

fn piece(detection: TextDetection, size: (u32, u32)) -> Piece {
    let points: Vec<[f32; 2]> = detection.points.iter().map(|p: &NormPoint| [p.x, p.y]).collect();
    let norm = BBox::covering(points.iter().copied()).unwrap_or(BBox::new(0.0, 0.0, 0.0, 0.0));
    Piece {
        raw: RawRegion {
            shape: Shape::Polygon(Polygon { points }),
            label: None,
            score: detection.confidence,
            track: None,
        },
        px: to_px(&norm, size),
        text: detection.text,
        confidence: detection.confidence,
    }
}

fn boxed(norm: BBox, label: Option<String>, score: Option<f32>) -> RawRegion {
    RawRegion { shape: Shape::Box(norm), label, score, track: None }
}

/// The index of the smallest element whose box holds `point`.
fn holder(elements: &[Element], point: [f32; 2]) -> Option<usize> {
    elements
        .iter()
        .enumerate()
        .filter(|(_, e)| e.px.contains(point))
        .min_by(|(_, a), (_, b)| a.px.area().total_cmp(&b.px.area()))
        .map(|(i, _)| i)
}

/// The label of the smallest chart element near `word`: one whose box, grown by half the
/// word's height, holds the word's centre. The element model reads a crop and the OCR the
/// page, so their boxes for one label differ by a few pixels.
fn label_of(labels: &[Element], word: &BBox) -> Option<String> {
    let reach = 0.5 * word.height();
    labels
        .iter()
        .filter(|e| e.px.expanded(reach).contains(word.center()))
        .min_by(|a, b| a.px.area().total_cmp(&b.px.area()))
        .map(|e| e.class.clone())
}

/// Rows and columns of a table: each cell's index range among the sorted rows (columns)
/// it overlaps by at least half its own height (width).
fn spans(cell: &BBox, bands: &[BBox], vertical: bool) -> Option<(u32, u32)> {
    let hits: Vec<u32> = bands
        .iter()
        .enumerate()
        .filter(|(_, band)| {
            let (a0, a1, b0, b1, len) = if vertical {
                (cell.y0, cell.y1, band.y0, band.y1, cell.height())
            } else {
                (cell.x0, cell.x1, band.x0, band.x1, cell.width())
            };
            (a1.min(b1) - a0.max(b0)) >= 0.5 * len.max(f32::EPSILON)
        })
        .map(|(i, _)| i as u32)
        .collect();
    let first = *hits.first()?;
    Some((first, hits.len() as u32))
}

/// Cells of one table from the structure model's `cell`, `row` and `column` boxes, with the
/// text of the words inside each cell.
fn cells(
    found: Vec<(String, NormBox)>,
    words: &[Piece],
    size: (u32, u32),
) -> Vec<TableCell<RawRegion>> {
    let mut rows: Vec<BBox> = Vec::new();
    let mut cols: Vec<BBox> = Vec::new();
    let mut raw_cells: Vec<(BBox, Option<f32>)> = Vec::new();
    for (class, b) in found {
        let px = to_px(&norm_box(&b), size);
        match class.as_str() {
            "row" => rows.push(px),
            "column" => cols.push(px),
            "cell" => raw_cells.push((norm_box(&b), b.confidence)),
            _ => {}
        }
    }
    rows.sort_by(|a, b| a.center()[1].total_cmp(&b.center()[1]));
    cols.sort_by(|a, b| a.center()[0].total_cmp(&b.center()[0]));
    let mut out: Vec<(TableCell<RawRegion>, Option<f32>)> = Vec::new();
    for (norm, confidence) in raw_cells {
        let px = to_px(&norm, size);
        let (Some((row, row_span)), Some((col, col_span))) =
            (spans(&px, &rows, true), spans(&px, &cols, false))
        else {
            continue;
        };
        let inside: Vec<Piece> =
            words.iter().filter(|w| px.contains(w.px.center())).cloned().collect();
        let text = reading::lines(inside)
            .iter()
            .map(|line| reading::line_text(line))
            .collect::<Vec<_>>()
            .join(" ");
        let cell = TableCell {
            region: boxed(norm, Some("cell".into()), confidence),
            row,
            col,
            row_span,
            col_span,
            text,
        };
        // Two boxes for one grid position keep the more confident.
        match out.iter_mut().find(|(c, _)| c.row == row && c.col == col) {
            Some(existing) if existing.1.unwrap_or(0.0) >= confidence.unwrap_or(0.0) => {}
            Some(existing) => *existing = (cell, confidence),
            None => out.push((cell, confidence)),
        }
    }
    let mut cells: Vec<TableCell<RawRegion>> = out.into_iter().map(|(c, _)| c).collect();
    cells.sort_by_key(|c| (c.row, c.col));
    cells
}

#[async_trait]
impl DocumentReader<ReleasedImage> for NimDocumentReader {
    fn descriptor(&self) -> &BackendDescriptor {
        &self.descriptor
    }

    async fn read(
        &self,
        cx: &VisionCx,
        page: &ReleasedImage,
        options: &ReadOptions,
    ) -> Result<DocumentPage<RawRegion>> {
        cx.check()?;
        if !page.is_for(&self.descriptor.id) {
            return Err(VisionError::NotReleased { backend: self.descriptor.id.to_string() });
        }
        let size = page.image().size();
        let backend = self.descriptor.id.as_str();
        let uri = self.data_uri(page.bytes())?;
        let ocr = self.call(self.ocr.id(), &uri, self.ocr.merge_levels());
        let layout = async {
            if self.layout {
                self.call(PAGE_ELEMENTS_MODEL, &uri, None).await.map(Some)
            } else {
                Ok(None)
            }
        };
        let (ocr, layout) = tokio::try_join!(ocr, layout)?;
        let pieces: Vec<Piece> = wire::text_detections(backend, self.ocr.id(), ocr)?
            .into_iter()
            .filter(|d| !d.text.trim().is_empty())
            .map(|d| piece(d, size))
            .collect();
        let elements: Vec<Element> = match layout {
            Some(value) => wire::elements(backend, PAGE_ELEMENTS_MODEL, value)?
                .into_iter()
                .map(|(class, b)| {
                    let norm = norm_box(&b);
                    Element { px: to_px(&norm, size), norm, class, confidence: b.confidence }
                })
                .filter(|e| {
                    !e.px.is_empty() && e.confidence.is_none_or(|c| c >= self.min_layout_confidence)
                })
                .collect(),
            None => Vec::new(),
        };

        // Each piece belongs to the smallest element holding its centre, or to none.
        let mut owned: Vec<Vec<Piece>> = vec![Vec::new(); elements.len()];
        let mut free: Vec<Piece> = Vec::new();
        for piece in pieces {
            match holder(&elements, piece.px.center()) {
                Some(i) => owned[i].push(piece),
                None => free.push(piece),
            }
        }

        let mut blocks: Vec<Block<RawRegion>> = Vec::new();
        let mut boxes: Vec<BBox> = Vec::new();
        for (element, words) in elements.iter().zip(owned) {
            let kind = kind_of(&element.class);
            let lines = reading::lines(words);
            let text = lines.iter().map(|l| reading::line_text(l)).collect::<Vec<_>>().join("\n");
            let flat: Vec<Piece> = lines.into_iter().flatten().collect();
            let confidence = reading::min_confidence(&flat).or(element.confidence);
            let mut block = reading::block(
                kind,
                boxed(element.norm, Some(element.class.clone()), element.confidence),
                text,
                confidence,
            );
            match kind {
                BlockKind::Table if self.tables => {
                    let found =
                        self.on_crop(TABLE_STRUCTURE_MODEL, page, element.px.expanded(4.0)).await?;
                    block.cells = cells(found, &flat, size);
                    if !block.cells.is_empty() {
                        block.text = reading::table_text(&block.cells);
                    }
                }
                BlockKind::Figure if self.charts && element.class == "chart" => {
                    let found = self
                        .on_crop(GRAPHIC_ELEMENTS_MODEL, page, element.px.expanded(4.0))
                        .await?;
                    let labels: Vec<Element> = found
                        .into_iter()
                        .map(|(class, b)| {
                            let norm = norm_box(&b);
                            Element {
                                px: to_px(&norm, size),
                                norm,
                                class,
                                confidence: b.confidence,
                            }
                        })
                        .collect();
                    block.words = flat
                        .iter()
                        .map(|p| {
                            let mut word = p.word();
                            word.region.label = label_of(&labels, &p.px);
                            word
                        })
                        .collect();
                }
                _ => {}
            }
            if block.words.is_empty() {
                block.words = flat.iter().map(Piece::word).collect();
            }
            boxes.push(element.px);
            blocks.push(block);
        }
        // Text outside every element: one block per line.
        for line in reading::lines(free) {
            let norm = BBox::covering(
                line.iter()
                    .filter_map(|p| p.raw.shape.bbox())
                    .flat_map(|b| [[b.x0, b.y0], [b.x1, b.y1]]),
            )
            .unwrap_or(BBox::new(0.0, 0.0, 0.0, 0.0));
            let confidence = reading::min_confidence(&line);
            let mut block = reading::block(
                BlockKind::Text,
                boxed(norm, None, confidence),
                reading::line_text(&line),
                confidence,
            );
            block.words = line.iter().map(Piece::word).collect();
            boxes.push(reading::span(&line));
            blocks.push(block);
        }
        let reading_order = reading::reading_order(&boxes);
        let mut out = DocumentPage { blocks, reading_order, markdown: None };
        if options.markdown {
            out.markdown = Some(reading::markdown(&out));
        }
        tracing::debug!(
            vision.backend = backend,
            vision.blocks = out.blocks.len(),
            "nim page read"
        );
        Ok(out)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn nb(x0: f32, y0: f32, x1: f32, y1: f32) -> NormBox {
        NormBox { x_min: x0, y_min: y0, x_max: x1, y_max: y1, confidence: Some(0.9) }
    }

    #[test]
    fn structure_boxes_become_a_cell_grid_with_text() {
        let size = (100, 100);
        let found = vec![
            ("row".to_string(), nb(0.0, 0.0, 1.0, 0.5)),
            ("row".to_string(), nb(0.0, 0.5, 1.0, 1.0)),
            ("column".to_string(), nb(0.0, 0.0, 0.5, 1.0)),
            ("column".to_string(), nb(0.5, 0.0, 1.0, 1.0)),
            ("cell".to_string(), nb(0.5, 0.5, 1.0, 1.0)),
            ("cell".to_string(), nb(0.0, 0.0, 0.5, 0.5)),
            // A header spanning both columns.
            ("cell".to_string(), nb(0.0, 0.0, 1.0, 0.5)),
        ];
        let word = |text: &str, x0: f32, y0: f32| {
            let px = BBox::new(x0, y0, x0 + 10.0, y0 + 10.0);
            Piece {
                raw: RawRegion::shape(Shape::Box(px)),
                px,
                text: text.into(),
                confidence: Some(0.9),
            }
        };
        let words = vec![word("4.2", 70.0, 70.0), word("Part", 10.0, 10.0)];
        let cells = cells(found, &words, size);
        let grid: Vec<(u32, u32, u32, u32, &str)> =
            cells.iter().map(|c| (c.row, c.col, c.row_span, c.col_span, c.text.as_str())).collect();
        // The two cells at (0, 0) keep the first seen at equal confidence.
        assert_eq!(grid, vec![(0, 0, 1, 1, "Part"), (1, 1, 1, 1, "4.2")]);
    }

    #[test]
    fn pieces_belong_to_the_smallest_holding_element() {
        let element =
            |class: &str, px: BBox| Element { class: class.into(), norm: px, px, confidence: None };
        let elements = vec![
            element("chart", BBox::new(0.0, 0.0, 100.0, 100.0)),
            element("title", BBox::new(10.0, 10.0, 50.0, 20.0)),
        ];
        assert_eq!(holder(&elements, [20.0, 15.0]), Some(1));
        assert_eq!(holder(&elements, [80.0, 80.0]), Some(0));
        assert_eq!(holder(&elements, [180.0, 80.0]), None);
    }
}