rvt 0.4.0

Apache-2.0 clean-room reader for Autodesk Revit files (.rvt, .rfa, .rte, .rft) without Revit: metadata, previews and the embedded schema of 2016-2026 files, and typed elements with measured geometry from Revit 2024 and 2025 projects, exported as IFC4, glTF and plan SVG.
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Host types' layers and which side of a wall is its exterior (RE-53).
//!
//! # Layers
//!
//! A wall, floor, roof or ceiling type's own serialised data (its
//! element-data header and ElementId, [`crate::partition_names`]) carries
//! its compound structure: a `u32` layer count and one
//! [`LAYER_RECORD_LEN`]-byte record per layer, exterior (or top) first:
//!
//! ```text
//! +0   f64  width, feet (0 only on a membrane)
//! +8   u64  material ElementId, ff × 8 = by category
//! +16  u64  deck profile ElementId, ff × 8 = none
//! +24  u32  function: 1 structure, 2 substrate, 3 thermal / air,
//!           4 finish 1, 5 finish 2, 100 membrane, 200 structural deck
//! +28  9 bytes not read
//! ```
//!
//! On Revit 2023 (RE-112) a record is 29 bytes and its ids are `u32`
//! ([`layer_layout`]):
//!
//! ```text
//! +0   f64  width, feet
//! +8   u32  function
//! +12  u32  not read (ff × 4 or 0)
//! +16  u32  material ElementId, ff × 4 = by category
//! +20  u32  deck profile ElementId, ff × 4 = none
//! +24  5 bytes not read
//! ```
//!
//! The count is framed one of two ways ([`find_layers`]):
//! - `ff ff ff ff` and a per-release tag, `0x10a6` on Revit 2024 and
//!   `0x110e` on Revit 2025 and `0x106f` on Revit 2023
//!   ([`layer_frame_tag`]). On 2024 and 2023 this follows a wall type's
//!   name directly.
//! - the type's name (`u32 k`, `k` UTF-16 code units), then `u32 0`:
//!   Revit 2025 floors and ceilings, whose types on RE1 are named "-",
//!   Revit 2024 roofs (RE-56), and Revit 2023 floors and roofs.
//!
//! Against the materials Revit's own IFC4 export gives the same types:
//! - Snowdon Towers (2024): 42 of 42 types give Revit's constituent
//!   sequence, once the 0-width membranes Revit leaves out are dropped;
//! - Core Interior (2024): 4 of 4, all by category;
//! - RE1 Architecture (2025): the wall, both floors and the ceiling give
//!   Revit's layer widths and materials.
//!
//! # Wall orientation
//!
//! A wall's data carries, after `ff ff ff ff 01 00 00 00`, three `u32`: its
//! location line (0 to 5), a word of 0 to 2, and a flip flag
//! ([`wall_orientation`]). With the flag set, the wall's exterior (its first
//! layer) lies to the right of its location line's direction; clear, to
//! the left. On Snowdon Towers this holds on all 416 walls whose first and
//! last layers Revit's IFC4 export places measurably apart. The one other
//! wall's two outer layers are 0.03 ft apart, too close to tell.
//!
//! The wall's stored location line (RE-49's bounded line) is its
//! centreline whatever its location-line setting: Revit's IFC4 body spans
//! exactly half the type's thickness either side of it on 964 of Snowdon's
//! 1,054 walls, all of them with word 1 (RE-54).

use crate::{Result, RevitFile};
use std::collections::{BTreeMap, BTreeSet};

/// Releases these layouts are measured on.
pub const COMPOUND_STRUCTURE_SUPPORTED_REVIT_VERSIONS: &[u32] = &[2024, 2025];

/// Releases a wall's location line (RE-49's bounded line) is read on. On
/// 2025 it is measured by the same house saved in 2024 and 2025, whose 50
/// walls read the same line, orientation and layers from both (RE-55); on
/// 2023 by the walls of two projects, whose lines are Revit's axes before
/// joins (RE-114).
pub const WALL_LINE_SUPPORTED_REVIT_VERSIONS: &[u32] = &[2023, 2024, 2025];

/// Each wall's location line, by ElementId, on a release in
/// [`WALL_LINE_SUPPORTED_REVIT_VERSIONS`]; empty elsewhere.
pub fn scan_wall_lines(
    rf: &mut RevitFile,
    revit_version: u32,
    walls: &BTreeSet<u32>,
) -> Result<BTreeMap<u32, crate::partition_beam_axes::BoundedLine>> {
    if !WALL_LINE_SUPPORTED_REVIT_VERSIONS.contains(&revit_version) {
        return Ok(BTreeMap::new());
    }
    crate::partition_beam_axes::scan_first_bounded_lines(rf, revit_version, walls)
}

/// Each curved wall's location arc, by ElementId, where its data's first
/// curve record is an arc (RE-75), on the releases [`scan_wall_lines`]
/// reads. Like the line, it is the wall's centreline: Revit's IFC4 body lies
/// half the type's thickness either side of it on 21 of Snowdon Towers' 24
/// curved walls.
pub fn scan_wall_arcs(
    rf: &mut RevitFile,
    revit_version: u32,
    walls: &BTreeSet<u32>,
) -> Result<BTreeMap<u32, crate::partition_beam_axes::BoundedArc>> {
    if !WALL_LINE_SUPPORTED_REVIT_VERSIONS.contains(&revit_version) {
        return Ok(BTreeMap::new());
    }
    crate::partition_beam_axes::scan_first_bounded_arcs(rf, revit_version, walls)
}

/// Bytes per layer record, on Revit 2024 and 2025.
pub const LAYER_RECORD_LEN: usize = 37;

/// Where a layer record's fields are on a release.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LayerLayout {
    /// Bytes per record.
    pub record_len: usize,
    /// The `u32` function.
    pub function_at: usize,
    /// The material ElementId.
    pub material_at: usize,
    /// The deck profile ElementId.
    pub deck_at: usize,
    /// Whether the ids are `u64` (Revit 2024 and later) rather than `u32`.
    pub wide_ids: bool,
}

/// The layer record layout of `revit_version`, where it is measured.
pub fn layer_layout(revit_version: u32) -> Option<LayerLayout> {
    match revit_version {
        2023 => Some(LayerLayout {
            record_len: 29,
            function_at: 8,
            material_at: 16,
            deck_at: 20,
            wide_ids: false,
        }),
        2024 | 2025 => Some(LayerLayout {
            record_len: LAYER_RECORD_LEN,
            function_at: 24,
            material_at: 8,
            deck_at: 16,
            wide_ids: true,
        }),
        _ => None,
    }
}

/// Most layers a type is taken to have.
pub const MAX_LAYERS: usize = 32;

/// How far into a type's data the layers are looked for.
pub const LAYER_WINDOW: usize = 0x2000;

/// Longest type name, in UTF-16 code units, a name-framed layer count is
/// looked for behind.
pub const MAX_NAME_UNITS: usize = 256;

/// The bytes a wall's location line, a word and its flip flag follow.
pub const WALL_FLIP_ANCHOR: [u8; 8] = [0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00];

/// The tag framing a type's layer count on `revit_version`.
pub fn layer_frame_tag(revit_version: u32) -> Option<[u8; 2]> {
    match revit_version {
        2023 => Some([0x6f, 0x10]),
        2024 => Some([0xa6, 0x10]),
        2025 => Some([0x0e, 0x11]),
        _ => None,
    }
}

/// One layer of a type's compound structure.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CompoundLayer {
    /// Feet.
    pub width_feet: f64,
    /// The layer's material; `None` where the layer takes its category's.
    pub material: Option<u32>,
    /// The layer's function (see the module docs).
    pub function: u32,
}

fn u32_at(buf: &[u8], at: usize) -> Option<u32> {
    buf.get(at..at.checked_add(4)?)
        .map(|s| u32::from_le_bytes(s.try_into().expect("4 bytes")))
}

fn u64_at(buf: &[u8], at: usize) -> Option<u64> {
    buf.get(at..at.checked_add(8)?)
        .map(|s| u64::from_le_bytes(s.try_into().expect("8 bytes")))
}

/// The layers whose count starts at `count_at`, or `None` unless every
/// record is one a type can have: a width under 10 ft that is positive (0
/// only on a membrane or a structural deck), a material in `materials` or by category, a deck
/// profile that is none or a declared id, and a known function.
pub fn layers_at(
    buf: &[u8],
    count_at: usize,
    layout: LayerLayout,
    materials: &BTreeSet<u32>,
    declared: &BTreeSet<u32>,
) -> Option<Vec<CompoundLayer>> {
    let id_at = |at: usize| -> Option<u64> {
        if layout.wide_ids {
            u64_at(buf, at)
        } else {
            u32_at(buf, at).map(|id| match id {
                u32::MAX => u64::MAX,
                id => u64::from(id),
            })
        }
    };
    let count = usize::try_from(u32_at(buf, count_at)?).ok()?;
    if !(1..=MAX_LAYERS).contains(&count) {
        return None;
    }
    let mut layers = Vec::with_capacity(count);
    for index in 0..count {
        let at = count_at + 4 + layout.record_len * index;
        let width = f64::from_le_bytes(buf.get(at..at + 8)?.try_into().ok()?);
        let material = id_at(at + layout.material_at)?;
        let deck = id_at(at + layout.deck_at)?;
        let function = u32_at(buf, at + layout.function_at)?;
        // A membrane has no width, and nor has a structural deck whose
        // profile is carried by the layer above it (RE-112).
        let widthless = matches!(function, 100 | 200);
        let width_ok =
            width.is_finite() && width < 10.0 && (width >= 1e-3 || (widthless && width == 0.0));
        let material = match material {
            u64::MAX => None,
            id => Some(u32::try_from(id).ok().filter(|id| materials.contains(id))?),
        };
        let deck_ok = deck == u64::MAX
            || u32::try_from(deck)
                .ok()
                .is_some_and(|id| declared.contains(&id));
        if !width_ok || !deck_ok || !matches!(function, 0..=5 | 100 | 200) {
            return None;
        }
        layers.push(CompoundLayer {
            width_feet: width,
            material,
            function,
        });
    }
    Some(layers)
}

/// Whether the layer count at `count_at` is framed as a type's layers are.
fn framed(buf: &[u8], count_at: usize, tag: [u8; 2]) -> bool {
    let tagged = count_at >= 6
        && buf.get(count_at - 6..count_at - 2) == Some(&[0xff; 4][..])
        && buf.get(count_at - 2..count_at) == Some(&tag[..]);
    if tagged {
        return true;
    }
    // The type's name (u32 k, k UTF-16 code units), u32 0.
    if count_at < 4 || u32_at(buf, count_at - 4) != Some(0) {
        return false;
    }
    (1..=MAX_NAME_UNITS).any(|k: usize| {
        let Some(start) = count_at.checked_sub(4 + 2 * k + 4) else {
            return false;
        };
        u32_at(buf, start) == Some(k as u32)
            && (0..k).all(|i| {
                buf.get(start + 4 + 2 * i..start + 6 + 2 * i)
                    .map(|unit| u16::from_le_bytes([unit[0], unit[1]]))
                    .is_some_and(|unit| unit >= 0x20)
            })
    })
}

/// The first framed layer list in `data`.
pub fn find_layers(
    data: &[u8],
    tag: [u8; 2],
    layout: LayerLayout,
    materials: &BTreeSet<u32>,
    declared: &BTreeSet<u32>,
) -> Option<Vec<CompoundLayer>> {
    (6..data.len().saturating_sub(4))
        .filter(|&at| framed(data, at, tag))
        .find_map(|at| layers_at(data, at, layout, materials, declared))
}

/// The layers of each type in `types`, by ElementId, from each type's own
/// data. A type whose copies disagree is dropped; empty for a release the
/// layout is not measured on.
pub fn scan_type_layers(
    rf: &mut RevitFile,
    revit_version: u32,
    types: &BTreeSet<u32>,
    materials: &BTreeSet<u32>,
    declared: &BTreeSet<u32>,
) -> Result<BTreeMap<u32, Vec<CompoundLayer>>> {
    // Revit 2023's data header frames a `u32` id (RE-111).
    let header = match revit_version {
        2023 => Some(crate::partition_names::ELEMENT_DATA_HEADER_2023),
        _ => crate::partition_names::element_data_header(revit_version),
    };
    let (Some(header), Some(tag), Some(layout)) = (
        header,
        layer_frame_tag(revit_version),
        layer_layout(revit_version),
    ) else {
        return Ok(BTreeMap::new());
    };
    let mut found: BTreeMap<u32, Option<Vec<CompoundLayer>>> = BTreeMap::new();
    for stream in rf.partition_stream_names() {
        let Ok(inflated) = rf.inflated_partition(&stream) else {
            continue;
        };
        let buf = inflated.bytes();
        let hits: Vec<usize> = memchr::memmem::find_iter(buf, &header).collect();
        for (index, &hit) in hits.iter().enumerate() {
            let id_at = hit + header.len();
            let id = if layout.wide_ids {
                u64_at(buf, id_at).and_then(|id| u32::try_from(id).ok())
            } else {
                u32_at(buf, id_at)
            };
            let Some(id) = id.filter(|id| types.contains(id)) else {
                continue;
            };
            let end = hits
                .get(index + 1)
                .copied()
                .unwrap_or(buf.len())
                .min(hit.saturating_add(LAYER_WINDOW))
                .min(buf.len());
            let Some(layers) = buf
                .get(id_at + 8..end)
                .and_then(|data| find_layers(data, tag, layout, materials, declared))
            else {
                continue;
            };
            match found.get_mut(&id) {
                None => {
                    found.insert(id, Some(layers));
                }
                Some(held) => {
                    if held.as_ref() != Some(&layers) {
                        *held = None;
                    }
                }
            }
        }
    }
    Ok(found
        .into_iter()
        .filter_map(|(id, layers)| layers.map(|l| (id, l)))
        .collect())
}

/// What a wall's data says about its location line and its sides.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WallOrientation {
    /// Revit's location-line setting, 0 to 5. The stored line is the
    /// wall's centreline whatever it is (RE-54).
    pub location_line: u32,
    /// A word of 0 to 2. Every Snowdon Towers wall whose body is centred on
    /// its line at its type's thickness carries 1 (RE-54).
    pub word: u32,
    /// Set puts the wall's exterior to the right of its location line's
    /// direction; clear, to the left.
    pub flip: bool,
}

/// A wall's orientation, from its data. `None` without the anchor or with
/// values a wall does not take.
pub fn wall_orientation(data: &[u8]) -> Option<WallOrientation> {
    let at = memchr::memmem::find(data, &WALL_FLIP_ANCHOR)? + WALL_FLIP_ANCHOR.len();
    let location_line = u32_at(data, at)?;
    let word = u32_at(data, at + 4)?;
    let flip = match u32_at(data, at + 8)? {
        0 => false,
        1 => true,
        _ => return None,
    };
    (location_line <= 5 && word <= 2).then_some(WallOrientation {
        location_line,
        word,
        flip,
    })
}

/// Each wall's orientation, by ElementId, from its own data; a wall whose
/// copies disagree is dropped.
pub fn scan_wall_orientations(
    rf: &mut RevitFile,
    revit_version: u32,
    walls: &BTreeSet<u32>,
) -> Result<BTreeMap<u32, WallOrientation>> {
    let Some(layout) = crate::partition_names::element_data_layout(revit_version) else {
        return Ok(BTreeMap::new());
    };
    let header = layout.header;
    let mut found: BTreeMap<u32, Option<WallOrientation>> = BTreeMap::new();
    for stream in rf.partition_stream_names() {
        let Ok(inflated) = rf.inflated_partition(&stream) else {
            continue;
        };
        let buf = inflated.bytes();
        let hits: Vec<usize> = memchr::memmem::find_iter(buf, &header).collect();
        for (index, &hit) in hits.iter().enumerate() {
            let id_at = hit + header.len();
            let Some(id) = layout.id_at(buf, id_at).filter(|id| walls.contains(id)) else {
                continue;
            };
            let end = hits
                .get(index + 1)
                .copied()
                .unwrap_or(buf.len())
                .min(hit.saturating_add(LAYER_WINDOW))
                .min(buf.len());
            let Some(orientation) = buf.get(id_at + 8..end).and_then(wall_orientation) else {
                continue;
            };
            match found.get_mut(&id) {
                None => {
                    found.insert(id, Some(orientation));
                }
                Some(held) => {
                    if *held != Some(orientation) {
                        *held = None;
                    }
                }
            }
        }
    }
    Ok(found
        .into_iter()
        .filter_map(|(id, orientation)| orientation.map(|o| (id, o)))
        .collect())
}

/// Releases a wall type's face angles are read on (RE-86). Only Snowdon
/// Towers (Revit 2024) stores them; no 2025 file measured holds the record.
pub const WALL_FACE_ANGLES_SUPPORTED_REVIT_VERSIONS: &[u32] = &[2024];

/// The bytes a wall type's three face angles follow (RE-86): `u32 2`, eight
/// zero bytes, an `f64` 0.7 and a count of 3. The word before them varies
/// by document.
pub const WALL_FACE_ANGLES_FRAME: [u8; 24] = [
    0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x66, 0x66, 0x66, 0x66,
    0x66, 0x66, 0xe6, 0x3f, 0x03, 0x00, 0x00, 0x00,
];

/// The three `f64` a wall type's data stores after
/// [`WALL_FACE_ANGLES_FRAME`], radians. On Snowdon Towers all three are 0
/// on 40 of its 43 exported wall types. The three "Solar Wall" types store
/// 10 degrees in the middle one, and each of their 21 exported walls is
/// tapered: its exterior face leans 10.000 degrees from vertical, wider at
/// the base, and its interior face is vertical (RE-86). What the first and
/// third angle set is not measured.
///
/// `None` without the frame, with it more than once, or with an angle that
/// is neither 0 nor between 1e-9 radians and a right angle either way (the
/// frame's bytes also occur by chance in other elements' data).
pub fn wall_type_face_angles(data: &[u8]) -> Option<[f64; 3]> {
    let mut frames = memchr::memmem::find_iter(data, &WALL_FACE_ANGLES_FRAME);
    let at = frames.next()? + WALL_FACE_ANGLES_FRAME.len();
    if frames.next().is_some() {
        return None;
    }
    let angle = |index: usize| {
        data.get(at + 8 * index..at + 8 * index + 8)
            .map(|b| f64::from_le_bytes(b.try_into().expect("8 bytes")))
            .filter(|value| {
                *value == 0.0 || (1e-9..std::f64::consts::FRAC_PI_2).contains(&value.abs())
            })
    };
    Some([angle(0)?, angle(1)?, angle(2)?])
}

/// Each type in `types` whose data stores [`wall_type_face_angles`], by
/// ElementId; a type whose copies disagree is dropped. Empty on a release
/// outside [`WALL_FACE_ANGLES_SUPPORTED_REVIT_VERSIONS`].
pub fn scan_wall_type_face_angles(
    rf: &mut RevitFile,
    revit_version: u32,
    types: &BTreeSet<u32>,
) -> Result<BTreeMap<u32, [f64; 3]>> {
    let Some(header) = crate::partition_names::element_data_header(revit_version)
        .filter(|_| WALL_FACE_ANGLES_SUPPORTED_REVIT_VERSIONS.contains(&revit_version))
    else {
        return Ok(BTreeMap::new());
    };
    let mut found: BTreeMap<u32, Option<[f64; 3]>> = BTreeMap::new();
    for stream in rf.partition_stream_names() {
        let Ok(inflated) = rf.inflated_partition(&stream) else {
            continue;
        };
        let buf = inflated.bytes();
        let hits: Vec<usize> = memchr::memmem::find_iter(buf, &header).collect();
        for (index, &hit) in hits.iter().enumerate() {
            let id_at = hit + header.len();
            let Some(id) = u64_at(buf, id_at)
                .and_then(|id| u32::try_from(id).ok())
                .filter(|id| types.contains(id))
            else {
                continue;
            };
            let end = hits
                .get(index + 1)
                .copied()
                .unwrap_or(buf.len())
                .min(hit.saturating_add(LAYER_WINDOW))
                .min(buf.len());
            let Some(angles) = buf.get(id_at + 8..end).and_then(wall_type_face_angles) else {
                continue;
            };
            match found.get_mut(&id) {
                None => {
                    found.insert(id, Some(angles));
                }
                Some(held) => {
                    if *held != Some(angles) {
                        *held = None;
                    }
                }
            }
        }
    }
    Ok(found
        .into_iter()
        .filter_map(|(id, angles)| angles.map(|a| (id, a)))
        .collect())
}

/// Where a join list's count sits past its opening word on
/// `revit_version`: Revit 2025 puts a zero `u32` before it (RE-70).
/// `None` where the layout is not measured.
pub fn wall_join_count_offset(revit_version: u32) -> Option<usize> {
    match revit_version {
        2024 => Some(12),
        2025 => Some(16),
        _ => None,
    }
}

/// The word that opens a wall's join list (RE-70).
pub const WALL_JOIN_LIST_WORD: [u8; 4] = [0x07, 0x00, 0x00, 0x00];

/// Bytes per join-list entry: `u32 · u64 ElementId · u32`.
pub const WALL_JOIN_ENTRY_LEN: usize = 16;

/// Most entries a join list is taken to have.
pub const MAX_WALL_JOIN_ENTRIES: usize = 64;

/// How far into a wall's data its join lists are looked for. They sit up
/// to 12 KB in on Snowdon Towers.
pub const WALL_JOIN_WINDOW: usize = 0x1_0000;

/// A wall's join lists, each as its tag and the ElementIds it names.
pub type WallJoinLists = Vec<(u32, Vec<u32>)>;

/// Every join list in a wall's `data` that names at least one wall and
/// only walls in `walls`, as its tag and the ElementIds it names (RE-70).
///
/// A list is `07 00 00 00 · u32 tag · 02 00 00 00`, a zero `u32` on Revit
/// 2025 ([`wall_join_count_offset`]), a `u32` count and that many
/// [`WALL_JOIN_ENTRY_LEN`]-byte entries whose `u64` is a wall's ElementId.
pub fn wall_join_lists(
    data: &[u8],
    count_at: usize,
    walls: &BTreeSet<u32>,
) -> Vec<(u32, Vec<u32>)> {
    let mut out = Vec::new();
    for at in memchr::memmem::find_iter(data, &WALL_JOIN_LIST_WORD) {
        let (Some(tag), Some(2), Some(count)) = (
            u32_at(data, at + 4),
            u32_at(data, at + 8),
            u32_at(data, at + count_at),
        ) else {
            continue;
        };
        let count = count as usize;
        if count == 0
            || count > MAX_WALL_JOIN_ENTRIES
            || (count_at > 12 && u32_at(data, at + 12) != Some(0))
        {
            continue;
        }
        let named: Option<Vec<u32>> = (0..count)
            .map(|index| {
                u64_at(data, at + count_at + 8 + index * WALL_JOIN_ENTRY_LEN)
                    .and_then(|id| u32::try_from(id).ok())
                    .filter(|id| walls.contains(id))
            })
            .collect();
        if let Some(named) = named {
            out.push((tag, named));
        }
    }
    out
}

/// The walls each wall in `read` names in its join lists, by ElementId
/// (RE-70). `walls` is every recovered wall; a list naming anything else is
/// not a join list.
///
/// The lists' tag is one value per document (`0x0b9f7d` on Snowdon Towers,
/// Core Interior and RE1, `0x039f3d` on the MIT tutorial house) that no
/// schema class carries, so it is read off the data: the one tag whose
/// lists name walls. A file where two tags do gives nothing, and so does a
/// wall whose copies disagree.
pub fn scan_wall_join_partners(
    rf: &mut RevitFile,
    revit_version: u32,
    walls: &BTreeSet<u32>,
    read: &BTreeSet<u32>,
) -> Result<BTreeMap<u32, BTreeSet<u32>>> {
    let (Some(header), Some(count_at)) = (
        crate::partition_names::element_data_header(revit_version),
        wall_join_count_offset(revit_version),
    ) else {
        return Ok(BTreeMap::new());
    };
    let mut found: BTreeMap<u32, Option<WallJoinLists>> = BTreeMap::new();
    for stream in rf.partition_stream_names() {
        let Ok(inflated) = rf.inflated_partition(&stream) else {
            continue;
        };
        let buf = inflated.bytes();
        let hits: Vec<usize> = memchr::memmem::find_iter(buf, &header).collect();
        for (index, &hit) in hits.iter().enumerate() {
            let id_at = hit + header.len();
            let Some(id) = u64_at(buf, id_at)
                .and_then(|id| u32::try_from(id).ok())
                .filter(|id| read.contains(id))
            else {
                continue;
            };
            let end = hits
                .get(index + 1)
                .copied()
                .unwrap_or(buf.len())
                .min(hit.saturating_add(WALL_JOIN_WINDOW))
                .min(buf.len());
            let Some(data) = buf.get(id_at + 8..end) else {
                continue;
            };
            let lists = wall_join_lists(data, count_at, walls);
            match found.get_mut(&id) {
                None => {
                    found.insert(id, Some(lists));
                }
                Some(held) => {
                    if held.as_ref() != Some(&lists) {
                        *held = None;
                    }
                }
            }
        }
    }
    let tags: BTreeSet<u32> = found
        .values()
        .flatten()
        .flatten()
        .map(|(tag, _)| *tag)
        .collect();
    let [tag] = tags.into_iter().collect::<Vec<_>>()[..] else {
        return Ok(BTreeMap::new());
    };
    Ok(found
        .into_iter()
        .filter_map(|(id, lists)| {
            let named: BTreeSet<u32> = lists?
                .into_iter()
                .filter(|(list_tag, _)| *list_tag == tag)
                .flat_map(|(_, named)| named)
                .filter(|other| *other != id)
                .collect();
            Some((id, named))
        })
        .collect())
}