cumulus3d-cli 0.11.1

cumulus3d command-line tool: progressive drone-image 3D reconstruction in one process
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
/*
 * session.rs
 *
 * Copyright (c) 2026 ParkSangWoo
 *
 * Author(s):
 *
 *      ParkSangWoo <dev.parksangwoo@gmail.com>
 *
 *
 * This file is part of cumulus3d.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

//! State value and reducer: [`Session`] + [`step`] / [`poll`] / [`command`] / [`finish`].
//!
//! Each time the frame bundle of one position ([`FrameSet`]) arrives, it performs features -> matching of new pairs -> (first model /
//! continued registration + triangulation)
//! -> on zone completion, preview (foreground) densification + refinement (background thread: BA -> GPS ENU alignment -> densification),
//! and emits that process as per-stage [`Event`]s. It writes no files or logs (run.log lines also go out as [`Event::Log`]).
//!
//! # Throughput
//! - [`Input::Batch`] processes the positions of a backlog together (features, one matching call, one registration and
//!   triangulation per segment between zone closings); [`Session::lookahead`] gives a [`Lookahead`] handle that starts the feature
//!   extraction of upcoming positions while earlier ones are processed ([`SessionConfig::lookahead`]).
//! - [`SessionConfig::preview`] (stable display) emits a sparse preview of every zone when it closes.
//!
//! # State and sharing
//! `Session` is a value. `step(&s, frames)` returns a new `Session` and `s` can still be used.
//! Models, image lists, correspondence graphs etc. are shared via `Arc` / copy on write. However, the feature store is append-only shared
//! data,
//! so calling `step` twice from the same earlier value with different frames mixes up the store (rewind with [`Command::ResetFrom`]).
//! Background refinement results (refined models and point clouds) and the event version number are also shared by the whole session
//! lineage.
//!
//! # Event delivery
//! - Default: foreground events go out in the return vector of that call; background refinement events accumulate in an internal inbox
//!   and go out
//!   at the front of the return vector of the next `step`/`poll`/`command`/`finish`.
//! - If a receiver is set with [`Session::set_sink`], **all** events (foreground and background) are delivered to it in version order as
//!   soon as they are emitted.
//!   In that case the foreground events in the return vector are copies of what was already delivered (do not deliver them again). The
//!   receiver must not block
//!   (it is called inside an internal lock).
//! - `meta.version` increases monotonically across the whole session lineage and gives the emission order (= timeline order).
//!
//! # Rules for reproducing run.log and timeline (for output hooks)
//! - The timeline text is given by [`Event::timeline_text`], and it is also written to run.log in the same place as `[HH:MM:SS] text`.
//! - [`Event::Log`] is one run.log line at that place (with a `[HH:MM:SS] ` prefix if `stamped`).
//! - [`Event::Warning`]/[`Event::Error`] go out together with a `Log` of the same content, so do not write them to run.log separately.
//! - The stage times of [`Event::Finished`] have no post-processing ("post") time (post-processing is done by the output hook).

use crate::ahead::{AheadCore, Lookahead, PositionExtract};
use crate::densewrap::{dense_model, DenseConfig};
use crate::events::{Command, Event, Frame, Meta, ZoneRange};
use crate::intrinsics::IntrinsicsPrior;
use crate::post::fit_shared_points;
use crate::util::StageTimes;
use cumulus3d_align::shared::NameFilter;
use cumulus3d_align::{align_to_gps, EnuOrigin, ModelAlignerOptions};
use cumulus3d_ba::{bundle_adjust_with, BaBackend, BaBackendRef, BaConfig};
use cumulus3d_core::analyzer::ModelStats;
use cumulus3d_core::io::ply::PointCloud;
use cumulus3d_core::io::GpsRecord;
use cumulus3d_core::{Camera, CameraId, FeatureStore, ImageId, MatchGraph, MatchGraphOptions, Reconstruction, Rigid3};
use cumulus3d_features::{ExtractionOptions, FeatureExtractor, ImageReport, ImageStatus, SiftEngine};
use cumulus3d_matching::{match_pairs_with, MatcherBackend, PairMatchingOptions, TwoViewBackend};
use cumulus3d_sfm::{
    global_mapper, register_images_with, triangulate_points_with, AbsPoseBackend, GlobalSfmOptions, PointTriangulatorOptions,
    RegistrationOptions, TriangulationBackend, TriangulationScope,
};
use rayon::prelude::*;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque};
use std::path::PathBuf;
use std::sync::{Arc, Mutex, MutexGuard};
use std::thread::JoinHandle;
use std::time::{Instant, SystemTime};

/// One image of one position.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FrameImage {
    /// Camera name (e.g. `camF`). The same name uses the same camera intrinsics.
    pub camera: String,
    /// Image name (path relative to `image_root`, e.g. `camF/camF_0000.jpg`).
    pub name: String,
}

/// Frame bundle of one position (one image per camera).
#[derive(Clone, Debug, Default)]
pub struct FrameSet {
    /// Images per camera.
    pub images: Vec<FrameImage>,
    /// GPS records that came with this bundle (appended after `gps` in the settings).
    pub gps: Vec<GpsRecord>,
}

impl FrameSet {
    /// As a (camera, name) list.
    pub fn new<C: Into<String>, N: Into<String>>(images: impl IntoIterator<Item = (C, N)>) -> Self {
        Self { images: images.into_iter().map(|(c, n)| FrameImage { camera: c.into(), name: n.into() }).collect(), gps: Vec::new() }
    }
}

/// Backends of the sparse stages that run per position. All CPU by default.
#[derive(Clone)]
pub struct SparseBackends {
    /// Two-view geometric verification.
    pub two_view: Arc<dyn TwoViewBackend>,
    /// Absolute pose RANSAC of registration.
    pub abs_pose: Arc<dyn AbsPoseBackend>,
    /// Triangulation (creation RANSAC, depth and reprojection checks).
    pub triangulation: Arc<dyn TriangulationBackend>,
}

impl Default for SparseBackends {
    fn default() -> Self {
        Self {
            two_view: Arc::new(cumulus3d_matching::CpuTwoView),
            abs_pose: Arc::new(cumulus3d_sfm::CpuAbsPose),
            triangulation: Arc::new(cumulus3d_sfm::CpuTriangulation),
        }
    }
}

/// Session settings.
#[derive(Clone)]
pub struct SessionConfig {
    /// Input image folder (base of the image names).
    pub image_root: PathBuf,
    /// GPS records (alignment order = this order).
    pub gps: Vec<GpsRecord>,
    /// Zone size and overlap (in units of positions).
    pub span: usize,
    /// Zone overlap (number of positions).
    pub overlap: usize,
    /// Total number of known positions. If set, the last zones are closed at the last position; if not, they are closed in [`finish`].
    pub total_positions: Option<usize>,
    /// Triangulate only newly registered images.
    pub incremental_triangulation: bool,
    /// Fix the ENU origin to the first GPS record (default: the first common record at each alignment).
    pub fixed_enu_origin: bool,
    /// RANSAC seed for two-view geometry and GPS alignment.
    pub seed: Option<u64>,
    /// Feature extraction options.
    pub extraction: ExtractionOptions,
    /// SIFT backend.
    pub sift: Arc<dyn SiftEngine>,
    /// Descriptor matching backend.
    pub matcher: Arc<dyn MatcherBackend>,
    /// Bundle adjustment backend (zone refinement BA, BA of the first model's global mapper).
    pub ba_backend: Arc<dyn BaBackend>,
    /// Sparse-stage backends (geometric verification, registration, triangulation).
    pub sparse: SparseBackends,
    /// Densification settings (None = skip densification, point-cloud events carry `dense: false`).
    pub dense: Option<DenseConfig>,
    /// Number of per-position rewind points kept for [`Command::ResetFrom`] (0 = no rewind, saves memory).
    pub history: usize,
    /// Decode input frames to RGB and emit them as [`Event::FrameDecoded`] before processing (off by default).
    pub decode_frames: bool,
    /// Stable display (default off). No preview densification; the refinement of zone K waits for zone K−1, takes the
    /// cameras already shown by it (pose and intrinsics, copied exactly) as fixed in its bundle adjustment, and is not
    /// re-aligned to GPS, so a zone never moves after it is shown and the space only grows. The coordinate frame is
    /// the first refined zone's (local). `AllRefinedDone::chain` is the final model aligned to GPS (ENU) once.
    pub stable_display: bool,
    /// Stable display: standard deviation (m) of the GPS position prior on each newly placed camera. ≤ 0 disables
    /// the prior (then the shown frame is local and drifts with the chain).
    pub gps_prior_sigma: f64,
    /// Stable display: until settled, every refined zone re-adjusts all earlier cameras and the intrinsics (shown zones are
    /// re-placed). Once the largest relative focal change between two consecutive refined models is below this (default
    /// 0.01 = 1 %), the display settles: from then on shown cameras and intrinsics are frozen.
    pub settle_tol: f64,
    /// Stable display: the display settles by force at this zone at the latest (default 3).
    pub settle_max_zone: usize,
    /// Soft intrinsics prior (see [`crate::intrinsics`]): every camera starts from the prior values as soon as it exists and
    /// each refinement pulls its refined parameters back towards them. None = off.
    pub intrinsics_prior: Option<Arc<IntrinsicsPrior>>,
    /// Positions whose features may be extracted ahead of the position loop (default 0 = off). A [`Lookahead`] handle
    /// ([`Session::lookahead`]) gets the frame sets of upcoming positions; one worker thread extracts at most this many of them
    /// into the feature store while the earlier positions are matched, registered and triangulated. The results are the same as
    /// without lookahead.
    pub lookahead: usize,
    /// Stable display: show a sparse preview of every zone as soon as it closes (default off). The zone's registered points (no
    /// bundle adjustment yet) are placed into the display frame (robust Sim3 on the 3D points shared with the latest shown zone;
    /// before any zone is shown, the GPS ENU fit the first refined zone uses) and emitted as an [`Event::ZonePreview`] with
    /// `dense: false` and `frame: "display"`. The refined zone later replaces it. Shown zones are never touched.
    pub preview: bool,
}

impl SessionConfig {
    /// Defaults: zone 12 / overlap 2, SIFT up to 8192 features and input 3200, CPU SIFT and matching, no densification, rewind 8.
    pub fn new(image_root: impl Into<PathBuf>) -> Self {
        let mut extraction = ExtractionOptions::default();
        extraction.sift.max_num_features = 8192;
        extraction.reader.max_image_size = 3200;
        Self {
            image_root: image_root.into(),
            gps: Vec::new(),
            span: 12,
            overlap: 2,
            total_positions: None,
            incremental_triangulation: false,
            fixed_enu_origin: false,
            seed: None,
            extraction,
            sift: Arc::new(cumulus3d_features::CpuSift::default()),
            matcher: Arc::new(cumulus3d_matching::CpuMatcher::default()),
            ba_backend: Arc::new(cumulus3d_ba::CpuBaBackend),
            sparse: SparseBackends::default(),
            dense: None,
            history: 8,
            decode_frames: false,
            stable_display: false,
            gps_prior_sigma: 1.0,
            settle_tol: 0.01,
            settle_max_zone: 3,
            intrinsics_prior: None,
            lookahead: 0,
            preview: false,
        }
    }
}

/// Reducer input.
#[derive(Clone, Debug)]
pub enum Input {
    /// Frame bundle of the next position.
    Frames(FrameSet),
    /// Frame bundles of the next positions, processed as a backlog batch: features of all positions are extracted together,
    /// every pair of the batch is matched in one call, the batch is registered and triangulated once. The batch is split where
    /// a zone closes (or the first model is built), so zones see the same chain state as with one `Frames` input per position.
    /// Equivalent to the same `Frames` inputs one after the other within the tolerance of registration order; the events are the
    /// same kinds in the same per-position order within each stage, but the stages of a batch's positions are grouped (all
    /// `FeaturesExtracted`, then all `PairsMatched`, then the registrations, then all `PositionDone`).
    Batch(Vec<FrameSet>),
    /// A command that changes the state.
    Command(Command),
    /// Only collect background events.
    Poll,
    /// One line to run.log (`Event::Log`).
    Log(String),
    /// Close the remaining zones -> wait for background jobs -> summary. Later inputs only produce warnings.
    Finish,
}

impl From<FrameSet> for Input {
    fn from(f: FrameSet) -> Self {
        Input::Frames(f)
    }
}

impl From<Command> for Input {
    fn from(c: Command) -> Self {
        Input::Command(c)
    }
}

/// The session as a reducer driven by [`crate::pipeline::Pipeline`]. `finish` closes the remaining zones and waits for all background
/// refinement jobs.
impl crate::pipeline::Reducer for Session {
    type Input = Input;
    fn step(&mut self, input: Input) -> Vec<Event> {
        self.apply(input)
    }
    fn prelude(&mut self, input: &Input) -> Vec<Event> {
        let sh = self.shared.clone();
        let mut evs = Vec::new();
        {
            let mut out = Out { sh: &sh, fg: Some(&mut evs), guard: None };
            match input {
                Input::Frames(fs) => self.announce(fs, self.frames.len(), &mut out),
                Input::Batch(sets) => {
                    for (i, fs) in sets.iter().enumerate() {
                        self.announce(fs, self.frames.len() + i, &mut out);
                    }
                }
                _ => {}
            }
        }
        evs
    }
    fn poll(&mut self) -> Vec<Event> {
        self.apply(Input::Poll)
    }
    fn command(&mut self, c: Command) -> Vec<Event> {
        self.apply(Input::Command(c))
    }
    fn finish(&mut self) -> Vec<Event> {
        self.apply(Input::Finish)
    }
}

/// Immediate-delivery receiver.
pub type EventSink = Arc<dyn Fn(Event) + Send + Sync>;

/// Zone preview result.
#[derive(Clone, Debug)]
pub struct PreviewZone {
    /// Zone range.
    pub range: ZoneRange,
    /// Preview model (zone 0 is the GPS-aligned one).
    pub model: Arc<Reconstruction>,
    /// Preview dense point cloud (None if densification was not run).
    pub cloud: Option<Arc<PointCloud>>,
    /// Point-cloud coordinate frame ("gps" | "refined_K" | "unaligned").
    pub frame: String,
}

/// Zone refined result (present from the time the pose is finalized; the point cloud exists once densification finishes).
#[derive(Clone, Debug)]
pub struct RefinedZone {
    /// Zone range.
    pub range: ZoneRange,
    /// Model after BA + GPS alignment.
    pub model: Arc<Reconstruction>,
    /// Refined dense point cloud (None before densification finishes or if it is skipped).
    pub cloud: Option<Arc<PointCloud>>,
    /// Whether refined densification has finished.
    pub done: bool,
    generation: u64,
}

// ---------------------------------------------------------------- Event emission

struct EmitState {
    version: u64,
    sink: Option<EventSink>,
    inbox: Vec<Event>,
}

struct Emitter(Mutex<EmitState>);

impl Emitter {
    fn lock(&self) -> MutexGuard<'_, EmitState> {
        self.0.lock().unwrap_or_else(|e| e.into_inner())
    }

    /// Attaches version and time and emits it. If `fg` is present, also puts it in the return vector.
    fn emit(&self, make: impl FnOnce(Meta) -> Event, fg: Option<&mut Vec<Event>>) {
        let mut st = self.lock();
        st.version += 1;
        let ev = make(Meta { version: st.version, at: SystemTime::now() });
        match (&st.sink, fg) {
            (Some(s), fg) => {
                let s = s.clone();
                if let Some(v) = fg {
                    v.push(ev.clone());
                }
                s(ev);
            }
            (None, Some(v)) => v.push(ev),
            (None, None) => st.inbox.push(ev),
        }
    }
}

/// Closes the lookahead worker when the last session of the lineage is gone.
struct AheadGuard(Arc<AheadCore>);

impl Drop for AheadGuard {
    fn drop(&mut self) {
        self.0.close();
    }
}

/// Immutable settings + background state shared by the session lineage.
struct Shared {
    cfg: SessionConfig,
    ahead: AheadGuard,
    emitter: Emitter,
    times: StageTimes,
    bg: Mutex<Background>,
    start: Mutex<Option<Instant>>,
}

#[derive(Default)]
struct Background {
    jobs: Vec<JoinHandle<()>>,
    ready: BTreeMap<usize, RefinedZone>,
    generation: BTreeMap<usize, u64>,
    /// Stable display: the zone at which the display settled (shown cameras and intrinsics frozen from the next zone on).
    settled: Option<usize>,
    /// Stable display: image-name set each zone was densified with (to densify it again while settling).
    zone_keep: BTreeMap<usize, Arc<HashSet<String>>>,
    /// Stable display: the model each zone's cloud was densified with (its intrinsics decide whether to densify again).
    dense_basis: BTreeMap<usize, Arc<Reconstruction>>,
    /// Stable display: zone -> generation of the last refine job of that zone that ran to its end.
    finished: BTreeMap<usize, u64>,
}

/// Marks a stable-display refine job as finished (also on panic) so the next zone's settle work can proceed.
struct FinishMark<'a> {
    sh: &'a Shared,
    zone: usize,
    generation: u64,
}

impl Drop for FinishMark<'_> {
    fn drop(&mut self) {
        self.sh.bg().finished.insert(self.zone, self.generation);
    }
}

impl Shared {
    fn bg(&self) -> MutexGuard<'_, Background> {
        self.bg.lock().unwrap_or_else(|e| e.into_inner())
    }

    fn generation(&self, zone: usize) -> u64 {
        self.bg().generation.get(&zone).copied().unwrap_or(0)
    }

    /// Discard a zone result: raise the generation so that later results and events of in-flight jobs are dropped.
    fn invalidate(&self, zone: usize) {
        let mut bg = self.bg();
        *bg.generation.entry(zone).or_insert(0) += 1;
        bg.ready.remove(&zone);
        bg.zone_keep.remove(&zone);
        bg.dense_basis.remove(&zone);
    }

    fn aligner_options(&self, gps: &[GpsRecord]) -> ModelAlignerOptions {
        let mut o = ModelAlignerOptions { max_error: 3.0, ..Default::default() };
        o.ransac.random_seed = self.cfg.seed;
        if self.cfg.fixed_enu_origin {
            if let Some(g) = gps.first() {
                o.origin = EnuOrigin::Explicit { lat: g.lat, lon: g.lon, alt: g.alt };
            }
        }
        o
    }

    /// ENU alignment using the GPS of registered images. On failure, keep the model as is and warn (design decision: do not stop).
    /// Waits for the refined pose of zone `k` (stable display orders refinements). None if it does not arrive
    /// (zone invalidated or refinement failed) within the time limit, or the generation changed.
    fn wait_refined(&self, k: usize, generation: u64) -> Option<Arc<Reconstruction>> {
        let t0 = Instant::now();
        let me = k + 1;
        loop {
            if let Some(z) = self.bg().ready.get(&k) {
                return Some(z.model.clone());
            }
            if self.generation(me) != generation || t0.elapsed().as_secs() > 1800 {
                return None;
            }
            std::thread::sleep(std::time::Duration::from_millis(20));
        }
    }

    /// Waits until the refine job of zone `k` has run to its end (stable display, settling: earlier zones are only
    /// re-placed once nothing else writes them). False on timeout or when zone `me` (generation `generation`) was invalidated.
    fn wait_finished(&self, k: usize, me: usize, generation: u64) -> bool {
        let t0 = Instant::now();
        loop {
            {
                let bg = self.bg();
                if bg.finished.get(&k).is_some_and(|g| *g == bg.generation.get(&k).copied().unwrap_or(0)) {
                    return true;
                }
            }
            if self.generation(me) != generation || t0.elapsed().as_secs() > 1800 {
                return false;
            }
            std::thread::sleep(std::time::Duration::from_millis(20));
        }
    }

    /// Stable display, not yet settled: zone `k`'s adjustment moved the earlier cameras, so every shown zone `j < k` is
    /// placed into `model` again. A zone whose cloud was made with intrinsics differing by `settle_tol` or more from
    /// `model`'s is densified again; otherwise its cloud is moved with the Sim3 from its previous model to `model`.
    /// The zone is emitted again (`ZoneRefinedPose` + `ZoneRefined`) under its own generation.
    fn replace_shown(&self, k: usize, generation: u64, model: &Arc<Reconstruction>, st: &ModelStats) {
        if !self.wait_finished(k - 1, k, generation) {
            return;
        }
        for j in 0..k {
            let snap = {
                let bg = self.bg();
                let gj = bg.generation.get(&j).copied().unwrap_or(0);
                bg.ready
                    .get(&j)
                    .filter(|z| z.generation == gj)
                    .map(|z| (z.clone(), gj, bg.zone_keep.get(&j).cloned(), bg.dense_basis.get(&j).cloned()))
            };
            let Some((z, gj, keep, basis)) = snap else { continue };
            let mut out = Out { sh: self, fg: None, guard: Some((j, gj)) };
            let change = basis.as_ref().map_or(0.0, |b| intrinsics_change(b.cameras(), model.cameras()));
            let mut basis_new = None;
            let cloud = z.cloud.as_ref().map(|old| {
                if change >= self.cfg.settle_tol {
                    if let Some(c) = keep.as_ref().and_then(|keep| self.dense_region("refined", j, model, keep, &mut out)) {
                        out.log(format!("[settle] 구역 {j} 다시 조밀화 (내부값 변화 {:.2}%)", change * 100.0));
                        basis_new = Some(model.clone());
                        return c;
                    }
                }
                // Shared 3D points seen by zone j's images first, then all shared points, then the camera-center fit.
                let by_points = keep
                    .as_ref()
                    .and_then(|keep| {
                        let keep = keep.clone();
                        let filter: NameFilter = Arc::new(move |n: &str| keep.contains(n));
                        fit_shared_points(&z.model, model, Some(filter), MIN_SHARED_PAIRS)
                    })
                    .or_else(|| fit_shared_points(&z.model, model, None, MIN_SHARED_PAIRS));
                let mut c = (**old).clone();
                if let Some((f, n)) = by_points {
                    let t = f.sim3;
                    cumulus3d_align::transform_cloud(&mut c, &t);
                    out.log(format!(
                        "[settle] 구역 {j} 점군 이동: 공유 점 {n}, 잔차 중앙 {:.3}m, 축척 {:.4}, 이동 {:.2}m",
                        f.median,
                        t.scale,
                        t.translation.norm()
                    ));
                } else {
                    let t = keep.as_ref().and_then(|keep| sim3_between(&z.model, model, |n| keep.contains(n)));
                    match t {
                        Some(t) => {
                            cumulus3d_align::transform_cloud(&mut c, &t);
                            out.log(format!(
                                "[settle] 구역 {j} 점군 이동(카메라 중심 대체): 이동 {:.3}m 축척 {:.4}",
                                t.translation.norm(),
                                t.scale
                            ));
                        }
                        None => out.log(format!("[settle] 구역 {j} 점군 이동 생략: 공통 영상 부족")),
                    }
                }
                Arc::new(c)
            });
            {
                let mut bg = self.bg();
                if bg.generation.get(&j).copied().unwrap_or(0) != gj {
                    continue;
                }
                let Some(r) = bg.ready.get_mut(&j).filter(|r| r.generation == gj) else { continue };
                r.model = model.clone();
                r.cloud = cloud.clone();
                r.done = true;
                if let Some(b) = basis_new {
                    bg.dense_basis.insert(j, b);
                }
            }
            let (range, m) = (z.range, model.clone());
            let (registered, mean_reproj_px) = (st.registered_image_count, st.mean_reprojection_error);
            out.emit(|meta| Event::ZoneRefinedPose { meta, range, registered, mean_reproj_px, model: m });
            let m = model.clone();
            out.emit(|meta| Event::ZoneRefined { meta, range, dense: cloud.is_some(), cloud: cloud.unwrap_or_default(), model: m });
        }
    }

    fn align_enu(&self, rec: &mut Reconstruction, what: &str, gps_all: &[GpsRecord], out: &mut Out) -> bool {
        let names: BTreeSet<String> = rec.registered_images().into_iter().filter_map(|i| rec.image(i).map(|im| im.name.clone())).collect();
        let gps: Vec<GpsRecord> = gps_all.iter().filter(|g| names.contains(&g.name)).cloned().collect();
        let opts = self.aligner_options(gps_all);
        let t0 = Instant::now();
        let r = align_to_gps(rec, &gps, &opts);
        out.stage("align", what, t0);
        match r {
            Ok(a) => {
                out.log(format!(
                    "[align] {what}: 공통 {} 인라이어 {} 오차 평균 {:.3}m 중앙 {:.3}m 축척 {:.4}",
                    a.common.len(),
                    a.num_inliers,
                    a.mean_error,
                    a.median_error,
                    a.sim3.scale
                ));
                if let Some(g) = &a.gravity {
                    out.log(gravity_note(&format!("align {what}"), g));
                }
                true
            }
            Err(e) => {
                out.warn(format!("[align] {what}: 정렬 실패({e}) — 정렬 없이 계속"));
                false
            }
        }
    }

    /// Densification (excluding images outside the zone). None if densification is off or fails.
    fn dense_region(&self, kind: &str, k: usize, model: &Reconstruction, keep: &HashSet<String>, out: &mut Out) -> Option<Arc<PointCloud>> {
        let dense = self.cfg.dense.as_ref()?;
        let what = format!("{kind} {k}");
        let t = Instant::now();
        let r = dense_model(model, |n| keep.contains(n), &self.cfg.image_root, dense);
        let total = t.elapsed();
        match r {
            Ok(run) => {
                self.times.add("undistort", run.undistort_time);
                self.times.add("densify", run.densify_time);
                let tm = &run.output.timings;
                out.stamped(format!(
                    "[time] dense {what}: 전체 {:.1}s (왜곡 보정+장면 {:.1}s, 대기 {:.1}s, 조밀화 {:.1}s = 이웃 {:.1} 준비 {:.1} 깊이 {:.1} 필터 {:.1} 융합 {:.1}, 캐시 적중 {})",
                    total.as_secs_f64(),
                    run.undistort_time.as_secs_f64(),
                    run.lock_wait.as_secs_f64(),
                    run.densify_time.as_secs_f64(),
                    tm.neighbors.as_secs_f64(),
                    tm.prepare.as_secs_f64(),
                    tm.depth().as_secs_f64(),
                    tm.filter.as_secs_f64(),
                    tm.fusion.as_secs_f64(),
                    run.output.cache_hits
                ));
                out.log(format!("[dense] {kind} {k} frames {} points {}", run.frames, run.output.cloud.len()));
                let mut cloud = run.output.cloud;
                // Same value as the PLY round trip: white if there is no color.
                if !cloud.is_empty() && cloud.colors.is_empty() {
                    cloud.colors = vec![[255, 255, 255]; cloud.len()];
                }
                Some(Arc::new(cloud))
            }
            Err(e) => {
                out.warn(format!("[dense] {kind} {k} 실패: {e}"));
                None
            }
        }
    }

    /// Background refinement job: copy -> BA -> ENU alignment -> (adoptable) -> densification.
    fn refine_job(
        self: Arc<Self>,
        range: ZoneRange,
        mut rec: Reconstruction,
        generation: u64,
        gps: Arc<Vec<GpsRecord>>,
        keep: Arc<HashSet<String>>,
        cam_ids: Arc<BTreeMap<String, CameraId>>,
    ) {
        let k = range.zone;
        let sh = &*self;
        let mut out = Out { sh, fg: None, guard: Some((k, generation)) };
        let _finish = FinishMark { sh, zone: k, generation };
        out.emit(|meta| Event::RefineStarted { meta, zone: k });
        let mut ba = BaConfig::default();
        // Stable display: the previous zone's model, and whether the display is still settling (intrinsics and shown
        // cameras free) in this zone.
        let mut prev: Option<Arc<Reconstruction>> = None;
        let mut settling = false;
        if sh.cfg.stable_display {
            let mut bg = sh.bg();
            if bg.generation.get(&k).copied().unwrap_or(0) == generation {
                bg.zone_keep.insert(k, keep.clone());
            }
        }
        if sh.cfg.stable_display {
            let targets = if sh.cfg.gps_prior_sigma > 0.0 { enu_targets(&rec, &gps) } else { HashMap::new() };
            if k == 0 {
                // Bring the first model into ENU once; every later zone is placed in this frame.
                let (src, dst): (Vec<_>, Vec<_>) = targets.iter().filter_map(|(id, g)| rec.projection_center(*id).map(|c| (c, *g))).unzip();
                if src.len() >= 3 {
                    if let Some((t, g)) = cumulus3d_align::fit_enu_sim3(&rec, &src, &dst) {
                        rec.transform(&t);
                        out.log(format!("[stable] refined 0: ENU 초기 정렬 GPS {} 개, 축척 {:.4}", src.len(), t.scale));
                        if let Some(g) = g {
                            out.log(gravity_note("stable refined 0", &g));
                        }
                    }
                }
                settling = true;
            } else {
                prev = sh.wait_refined(k - 1, generation);
                settling = sh.bg().settled.is_none();
                match (&prev, settling) {
                    (Some(p), false) => {
                        let (n, scale) = anchor_to_shown(&mut rec, p, &mut ba);
                        out.log(format!("[stable] refined {k}: 고정 영상 {n}, 이전 구역에 맞춘 축척 {scale:.4}"));
                    }
                    (Some(p), true) => {
                        let scale = sim3_onto_shown(&mut rec, p).map_or(1.0, |(t, _)| t.scale);
                        out.log(format!("[stable] refined {k}: 정착 전 — 모든 자세·내부값 자유, 이전 구역에 맞춘 축척 {scale:.4}"));
                    }
                    (None, _) => out.warn(format!("[stable] refined {k}: 앞 구역 결과 없음 — 고정 없이 조정")),
                }
            }
            let sigma = sh.cfg.gps_prior_sigma;
            for (id, g) in &targets {
                if !ba.constant_poses.contains(id) {
                    ba.position_priors.insert(*id, (*g, sigma));
                }
            }
            if !ba.position_priors.is_empty() {
                out.log(format!("[stable] refined {k}: GPS 위치 제약 {} 개 (σ {sigma} m)", ba.position_priors.len()));
            }
        }
        if let Some(pr) = &sh.cfg.intrinsics_prior {
            let pr = pr.resolve(&cam_ids);
            for (cid, cam) in rec.cameras() {
                if !ba.constant_cameras.contains(cid) {
                    if let Some(p) = pr.ba_prior(cam) {
                        ba.intrinsics_priors.insert(*cid, p);
                    }
                }
            }
            if !ba.intrinsics_priors.is_empty() {
                out.log(format!(
                    "[prior] refined {k}: 내부값 사전 카메라 {} 개 (초점 σ {} 상대, 왜곡 σ {})",
                    ba.intrinsics_priors.len(),
                    pr.focal_sigma,
                    pr.dist_sigma
                ));
            }
        }
        let t0 = Instant::now();
        let r = bundle_adjust_with(&mut rec, &ba, &*sh.cfg.ba_backend);
        out.stage("ba", &format!("refined {k}"), t0);
        match r {
            Ok(s) => out.log(format!(
                "[ba] refined {k}: 반복 {} 비용 {:.4e} → {:.4e} ({:?}), RMS {:.3}px",
                s.num_iterations,
                s.initial_cost,
                s.final_cost,
                s.termination,
                s.rms_reprojection_error()
            )),
            Err(e) => out.warn(format!("[ba] refined {k} 실패: {e}")),
        }
        // Settling BA moved the whole model (gauge freedom); bring it back into the previously shown frame.
        if let (true, Some(p)) = (settling && k > 0, &prev) {
            match fit_shared_points(&rec, p, None, MIN_SHARED_PAIRS) {
                Some((f, n)) => {
                    let t = f.sim3;
                    rec.transform(&t);
                    out.log(format!(
                        "[settle] refined {k}: 화면 좌표계 재정렬 공유 점 {n}, 잔차 중앙 {:.3}m, 축척 {:.4}, 이동 {:.2}m",
                        f.median,
                        t.scale,
                        t.translation.norm()
                    ));
                }
                None => match sim3_onto_shown(&mut rec, p) {
                    Some((t, _)) => out.log(format!(
                        "[settle] refined {k}: 화면 좌표계 재정렬 (카메라 중심 대체) 축척 {:.4}, 이동 {:.2}m",
                        t.scale,
                        t.translation.norm()
                    )),
                    None => out.warn(format!("[settle] refined {k}: 화면 좌표계 재정렬 생략: 공통 영상 부족")),
                },
            }
        }
        let adjusted = Arc::new(rec.clone());
        out.emit(|meta| Event::ZoneAdjusted { meta, zone: k, model: adjusted });
        if !sh.cfg.stable_display {
            sh.align_enu(&mut rec, &format!("refined {k}"), &gps, &mut out);
        }
        let st = ModelStats::compute(&rec);
        // Settle decision (before the pose is published, so the next zone sees it).
        let mut settle_now = false;
        if settling {
            let change = prev.as_ref().map(|p| intrinsics_change(p.cameras(), rec.cameras()));
            let forced = k >= sh.cfg.settle_max_zone;
            settle_now = forced || change.is_some_and(|c| c < sh.cfg.settle_tol);
            let verdict = match (settle_now, forced) {
                (true, true) => "강제 고정",
                (true, false) => "고정",
                _ => "정착 중",
            };
            match change {
                Some(c) => out.log(format!("[settle] refined {k}: 내부값 변화 {:.2}% → {verdict}", c * 100.0)),
                None => out.log(format!("[settle] refined {k}: → {verdict}")),
            }
        }
        let model = Arc::new(rec);
        {
            let mut bg = sh.bg();
            if bg.generation.get(&k).copied().unwrap_or(0) == generation {
                if settle_now {
                    bg.settled = Some(k);
                }
                bg.ready.insert(k, RefinedZone { range, model: model.clone(), cloud: None, done: false, generation });
            }
        }
        let m = model.clone();
        out.emit(|meta| Event::ZoneRefinedPose {
            meta,
            range,
            registered: st.registered_image_count,
            mean_reproj_px: st.mean_reprojection_error,
            model: m,
        });
        let cloud = sh.dense_region("refined", k, &model, &keep, &mut out);
        {
            let mut bg = sh.bg();
            if bg.generation.get(&k).copied().unwrap_or(0) == generation {
                if let Some(z) = bg.ready.get_mut(&k).filter(|z| z.generation == generation) {
                    z.cloud = cloud.clone();
                    z.done = true;
                }
                bg.dense_basis.insert(k, model.clone());
            }
        }
        let m = model.clone();
        out.emit(|meta| Event::ZoneRefined { meta, range, dense: cloud.is_some(), cloud: cloud.unwrap_or_default(), model: m });
        // Still settling: the earlier cameras moved with this adjustment, so the zones already shown are placed again.
        if settling && k > 0 {
            sh.replace_shown(k, generation, &model, &st);
        }
    }
}

/// Event exit (foreground: return vector, background: receiver/inbox). The background drops events when the zone generation changes.
struct Out<'a> {
    sh: &'a Shared,
    fg: Option<&'a mut Vec<Event>>,
    guard: Option<(usize, u64)>,
}

impl Out<'_> {
    fn emit(&mut self, make: impl FnOnce(Meta) -> Event) {
        if let Some((k, g)) = self.guard {
            if self.sh.generation(k) != g {
                return;
            }
        }
        self.sh.emitter.emit(make, self.fg.as_deref_mut());
    }

    fn log(&mut self, line: impl Into<String>) {
        let line = line.into();
        self.emit(|meta| Event::Log { meta, line, stamped: false });
    }

    fn stamped(&mut self, line: impl Into<String>) {
        let line = line.into();
        self.emit(|meta| Event::Log { meta, line, stamped: true });
    }

    /// run.log line + a warning with the same content.
    fn warn(&mut self, line: String) {
        self.log(line.clone());
        self.emit(|meta| Event::Warning { meta, message: line });
    }

    /// Accumulate stage time + a `[time] stage target: seconds` line.
    fn stage(&mut self, stage: &str, what: &str, t0: Instant) {
        let d = t0.elapsed();
        self.sh.times.add(stage, d);
        self.stamped(format!("[time] {stage} {what}: {:.3}s", d.as_secs_f64()));
    }
}

// ---------------------------------------------------------------- State value

#[derive(Clone)]
struct Checkpoint {
    /// The state right after processing up to this position.
    position: usize,
    chain: Option<Reconstruction>,
    adopted: Option<usize>,
    cam_ids: Arc<BTreeMap<String, CameraId>>,
    graph: Arc<MatchGraph>,
    gps_len: usize,
}

/// Incremental reconstruction state value.
#[derive(Clone)]
pub struct Session {
    shared: Arc<Shared>,
    store: Arc<FeatureStore>,
    graph: Arc<MatchGraph>,
    frames: Arc<Vec<Vec<FrameImage>>>,
    position_of: Arc<HashMap<String, usize>>,
    ingested: usize,
    cam_ids: Arc<BTreeMap<String, CameraId>>,
    gps: Arc<Vec<GpsRecord>>,
    chain: Option<Reconstruction>,
    adopted: Option<usize>,
    arrived: BTreeMap<usize, ZoneRange>,
    previews: Arc<BTreeMap<usize, PreviewZone>>,
    checkpoints: VecDeque<Checkpoint>,
    started: bool,
    finished: bool,
    failed: Option<String>,
    /// Positions whose frames were already emitted (so the same bundle is not emitted twice).
    announced: Option<usize>,
}

/// The new state and events after processing the next frame bundle.
pub fn step(session: &Session, frames: FrameSet) -> (Session, Vec<Event>) {
    session.clone().advance(Input::Frames(frames))
}

/// Collect background refinement events (when there is no receiver). The state does not change.
pub fn poll(session: &Session) -> (Session, Vec<Event>) {
    session.clone().advance(Input::Poll)
}

/// Command handling.
pub fn command(session: &Session, cmd: Command) -> (Session, Vec<Event>) {
    session.clone().advance(Input::Command(cmd))
}

/// Close the remaining zones -> wait for background jobs -> summary.
pub fn finish(session: &Session) -> (Session, Vec<Event>) {
    session.clone().advance(Input::Finish)
}

impl Session {
    /// Create an empty session from the settings.
    pub fn new(cfg: SessionConfig) -> Self {
        let extractor = Arc::new(FeatureExtractor::with_backend(cfg.sift.clone()));
        let store = Arc::new(FeatureStore::new());
        let ahead = AheadGuard(AheadCore::new(
            extractor,
            cfg.image_root.clone(),
            cfg.extraction.clone(),
            cfg.intrinsics_prior.clone(),
            cfg.lookahead,
            Arc::clone(&store),
        ));
        let gps = Arc::new(cfg.gps.clone());
        let shared = Arc::new(Shared {
            cfg,
            ahead,
            emitter: Emitter(Mutex::new(EmitState { version: 0, sink: None, inbox: Vec::new() })),
            times: StageTimes::default(),
            bg: Mutex::new(Background::default()),
            start: Mutex::new(None),
        });
        Self {
            shared,
            store,
            graph: Arc::new(MatchGraph::new()),
            frames: Arc::new(Vec::new()),
            position_of: Arc::new(HashMap::new()),
            ingested: 0,
            cam_ids: Arc::new(BTreeMap::new()),
            gps,
            chain: None,
            adopted: None,
            arrived: BTreeMap::new(),
            previews: Arc::new(BTreeMap::new()),
            checkpoints: VecDeque::new(),
            started: false,
            announced: None,
            finished: false,
            failed: None,
        }
    }

    /// Set/clear the immediate-delivery receiver (shared by the session lineage). When set, background events accumulated so far are
    /// delivered first.
    pub fn set_sink(&self, sink: Option<EventSink>) {
        let mut st = self.shared.emitter.lock();
        if let Some(s) = &sink {
            for ev in std::mem::take(&mut st.inbox) {
                s(ev);
            }
        }
        st.sink = sink;
    }

    /// Emit an event from outside (e.g. a hook runner) in this session's version order. If there is no receiver, it accumulates in the
    /// inbox.
    pub fn emit_external(&self, make: impl FnOnce(Meta) -> Event) {
        self.shared.emitter.emit(make, None);
    }

    /// State transition (owned version, no copying). `step`/`poll`/`command`/`finish` are thin wrappers around this.
    pub fn advance(mut self, input: Input) -> (Session, Vec<Event>) {
        let evs = self.apply(input);
        (self, evs)
    }

    /// In-place state transition. Returned events are in version order (events emitted by background jobs in the meantime are also
    /// inserted in place).
    pub fn apply(&mut self, input: Input) -> Vec<Event> {
        let sh = self.shared.clone();
        let mut evs: Vec<Event> = Vec::new();
        {
            let mut out = Out { sh: &sh, fg: Some(&mut evs), guard: None };
            if self.finished {
                if !matches!(input, Input::Poll) {
                    out.emit(|meta| Event::Warning { meta, message: "세션이 이미 끝남 — 입력 무시".into() });
                }
            } else {
                if !matches!(input, Input::Poll) {
                    self.ensure_started(&mut out);
                }
                match input {
                    Input::Frames(fs) => {
                        self.announce(&fs, self.frames.len(), &mut out);
                        if let Some(e) = &self.failed {
                            let message = format!("이전 오류로 중단된 세션 — 프레임 무시 ({e})");
                            out.emit(|meta| Event::Warning { meta, message });
                        } else if let Err(e) = self.process_frames(fs, &mut out) {
                            self.failed = Some(e.clone());
                            out.emit(|meta| Event::Error { meta, message: e });
                        }
                    }
                    Input::Batch(sets) => {
                        for (i, fs) in sets.iter().enumerate() {
                            self.announce(fs, self.frames.len() + i, &mut out);
                        }
                        if let Some(e) = &self.failed {
                            let message = format!("이전 오류로 중단된 세션 — 프레임 무시 ({e})");
                            out.emit(|meta| Event::Warning { meta, message });
                        } else if let Err(e) = self.process_batch(sets, &mut out) {
                            self.failed = Some(e.clone());
                            out.emit(|meta| Event::Error { meta, message: e });
                        }
                    }
                    Input::Command(c) => self.apply_command(c, &mut out),
                    Input::Poll => {}
                    Input::Log(line) => out.log(line),
                    Input::Finish => {
                        if self.failed.is_none() {
                            self.finish_session(&mut out);
                        }
                        self.finished = true;
                    }
                }
            }
        }
        // Merge, in version order, the events that background jobs put into the inbox during (and before) this call.
        let bg = std::mem::take(&mut sh.emitter.lock().inbox);
        if !bg.is_empty() {
            evs.extend(bg);
            evs.sort_by_key(|e| e.meta().version);
        }
        evs
    }

    // ------------------------------------------------------------ Queries

    /// Number of processed positions.
    pub fn positions(&self) -> usize {
        self.frames.len()
    }
    /// Frame bundle per position.
    pub fn frames(&self) -> &[Vec<FrameImage>] {
        &self.frames
    }
    /// Position of an image name.
    pub fn position_of(&self, name: &str) -> Option<usize> {
        self.position_of.get(name).copied()
    }
    /// Current chain model (None before the first model).
    pub fn model(&self) -> Option<&Reconstruction> {
        self.chain.as_ref()
    }
    /// The last refined zone adopted as the chain reference.
    pub fn adopted(&self) -> Option<usize> {
        self.adopted
    }
    /// Feature store.
    pub fn store(&self) -> &FeatureStore {
        &self.store
    }
    /// Correspondence graph.
    pub fn graph(&self) -> &MatchGraph {
        &self.graph
    }
    /// Session settings.
    pub fn config(&self) -> &SessionConfig {
        &self.shared.cfg
    }
    /// Zones that have arrived (been processed).
    pub fn arrived_zones(&self) -> Vec<ZoneRange> {
        self.arrived.values().copied().collect()
    }
    /// Per-zone preview results.
    pub fn previews(&self) -> &BTreeMap<usize, PreviewZone> {
        &self.previews
    }
    /// Per-zone refined results (those with finalized poses, the current value shared with background jobs).
    pub fn refined(&self) -> BTreeMap<usize, RefinedZone> {
        self.shared.bg().ready.clone()
    }
    /// Number of background refinement jobs not yet finished.
    pub fn pending_jobs(&self) -> usize {
        self.shared.bg().jobs.iter().filter(|j| !j.is_finished()).count()
    }
    /// The last event version issued.
    pub fn version(&self) -> u64 {
        self.shared.emitter.lock().version
    }
    /// Fatal error (feature extraction, matching, first model failure). If set, later frames are ignored.
    pub fn failed(&self) -> Option<&str> {
        self.failed.as_deref()
    }
    /// Whether `Finish` was handled.
    pub fn is_finished(&self) -> bool {
        self.finished
    }

    // ------------------------------------------------------------ Processing

    fn ensure_started(&mut self, out: &mut Out) {
        if self.started {
            return;
        }
        self.started = true;
        *self.shared.start.lock().unwrap_or_else(|e| e.into_inner()) = Some(Instant::now());
        let positions = self.shared.cfg.total_positions;
        out.emit(|meta| Event::Started { meta, positions });
    }

    /// Decode the next position's frames to RGB and emit them as [`Event::FrameDecoded`] (once per position, cameras in parallel).
    fn announce(&mut self, fs: &FrameSet, p: usize, out: &mut Out) {
        let sh = self.shared.clone();
        if !sh.cfg.decode_frames || self.finished || self.failed.is_some() || self.announced.is_some_and(|a| a >= p) {
            return;
        }
        self.ensure_started(out);
        self.announced = Some(p);
        let decoded: Vec<Result<Frame, String>> = fs
            .images
            .par_iter()
            .map(|im| {
                let path = sh.cfg.image_root.join(&im.name);
                let img = image::ImageReader::open(&path)
                    .and_then(|r| r.with_guessed_format())
                    .map_err(|e| format!("{}: {e}", path.display()))?
                    .decode()
                    .map_err(|e| format!("{}: 디코딩 실패: {e}", path.display()))?
                    .into_rgb8();
                let gps = fs.gps.iter().chain(self.gps.iter()).find(|g| g.name == im.name).cloned();
                let (width, height) = img.dimensions();
                Ok(Frame {
                    position: p,
                    camera: im.camera.clone(),
                    name: im.name.clone(),
                    path,
                    width,
                    height,
                    rgb: Arc::from(img.into_raw()),
                    gps,
                })
            })
            .collect();
        for d in decoded {
            match d {
                Ok(f) => {
                    let frame = Arc::new(f);
                    out.emit(|meta| Event::FrameDecoded { meta, frame });
                }
                Err(e) => out.emit(|meta| Event::Warning { meta, message: format!("[frame] {e}") }),
            }
        }
    }

    /// Handle that starts the feature extraction of upcoming positions ahead of the position loop (see [`Lookahead`]). It is
    /// inert unless [`SessionConfig::lookahead`] is above 0. Usable from any thread, also while the session is busy.
    pub fn lookahead(&self) -> Lookahead {
        Lookahead(Arc::clone(&self.shared.ahead.0))
    }

    fn process_frames(&mut self, fs: FrameSet, out: &mut Out) -> Result<(), String> {
        self.process_segment(vec![fs], out)
    }

    /// Backlog batch: split into segments that end where a zone closes or the first model is built (the chain state a zone
    /// snapshot sees is then the same as position by position), each segment is processed together.
    fn process_batch(&mut self, sets: Vec<FrameSet>, out: &mut Out) -> Result<(), String> {
        let cfg = &self.shared.cfg;
        let (span, ovl, total) = (cfg.span, cfg.overlap, cfg.total_positions);
        let mut seg: Vec<FrameSet> = Vec::new();
        for (p, fs) in (self.frames.len()..).zip(sets) {
            seg.push(fs);
            let boundary = p + 1 == span + ovl || !zones_ending_at(p, span, ovl, total).is_empty();
            if boundary {
                self.process_segment(std::mem::take(&mut seg), out)?;
            }
        }
        if !seg.is_empty() {
            self.process_segment(seg, out)?;
        }
        Ok(())
    }

    /// Features of positions `p0..p0+imgs.len()`: hinted positions are taken from the lookahead worker, the others are extracted
    /// now in one call. Emits the per-position events in position order.
    fn extract_positions(&mut self, p0: usize, imgs: &[Vec<FrameImage>], out: &mut Out) -> Result<(), String> {
        let sh = self.shared.clone();
        let ahead = &sh.ahead.0;
        let mut results: Vec<Option<PositionExtract>> = Vec::with_capacity(imgs.len());
        let mut missing: Vec<usize> = Vec::new();
        for (i, im) in imgs.iter().enumerate() {
            match ahead.take(im) {
                Some(Ok(r)) => results.push(Some(r)),
                Some(Err(e)) if e != "cancelled" => return Err(e),
                _ => {
                    results.push(None);
                    missing.push(i);
                }
            }
        }
        if !missing.is_empty() {
            let list: Vec<&[FrameImage]> = missing.iter().map(|&i| imgs[i].as_slice()).collect();
            let done = ahead.run_now(&self.store, &list)?;
            for (&i, r) in missing.iter().zip(done) {
                results[i] = Some(r);
            }
        }
        for (i, r) in results.into_iter().enumerate() {
            let (p, r) = (p0 + i, r.expect("every position extracted"));
            report_extraction(&r.reports, p, out);
            for note in &r.notes {
                out.log(note.clone());
            }
            if *self.cam_ids != r.cams {
                self.cam_ids = Arc::new(r.cams);
            }
            sh.times.add("feature", r.elapsed);
            out.stamped(format!("[time] feature pos {p}: {:.3}s", r.elapsed.as_secs_f64()));
        }
        Ok(())
    }

    /// One or more consecutive positions through features -> matching -> registration -> triangulation. A single position
    /// is the plain per-position step; several positions (a [`Input::Batch`] segment) match all their pairs in one call and
    /// register and triangulate once. The segment ends at the position that closes a zone, builds the first model, or is the
    /// last one given.
    fn process_segment(&mut self, sets: Vec<FrameSet>, out: &mut Out) -> Result<(), String> {
        let sh = self.shared.clone();
        let cfg = &sh.cfg;
        let p0 = self.frames.len();
        let n = sets.len();
        if n == 0 {
            return Ok(());
        }
        let last = p0 + n - 1;
        let label = if n == 1 { format!("pos {p0}") } else { format!("pos {p0}..{last}") };
        let mut imgs: Vec<Vec<FrameImage>> = Vec::with_capacity(n);
        for (i, fs) in sets.into_iter().enumerate() {
            let p = p0 + i;
            let names: Vec<String> = fs.images.iter().map(|i| i.name.clone()).collect();
            {
                let pos = Arc::make_mut(&mut self.position_of);
                for n in &names {
                    pos.insert(n.clone(), p);
                }
            }
            Arc::make_mut(&mut self.frames).push(fs.images.clone());
            self.ingested += fs.images.len();
            if !fs.gps.is_empty() {
                Arc::make_mut(&mut self.gps).extend(fs.gps);
            }
            out.emit(|meta| Event::FrameIngested { meta, position: p, images: names });
            imgs.push(fs.images);
        }

        // 1) Features: for the first bundle, one camera per folder; afterwards the same camera id.
        self.extract_positions(p0, &imgs, out)?;

        // 2) Match only pairs that involve a new image.
        let per_pos: Vec<Vec<(ImageId, ImageId)>> = (p0..=last)
            .map(|p| {
                pairs_for(&self.frames, p)
                    .into_iter()
                    .filter_map(|(a, b)| Some((self.store.image_id_by_name(&a)?, self.store.image_id_by_name(&b)?)))
                    .collect()
            })
            .collect();
        let pairs: Vec<(ImageId, ImageId)> = if n == 1 { per_pos[0].clone() } else { per_pos.iter().flatten().copied().collect() };
        let mut mopts = PairMatchingOptions::default();
        mopts.geometry.ransac.random_seed = cfg.seed;
        let t0 = Instant::now();
        let st = match_pairs_with(&self.store, &pairs, &mopts, cfg.matcher.as_ref(), cfg.sparse.two_view.as_ref());
        out.stage("matching", &format!("{label} ({} pairs)", pairs.len()), t0);
        let st = st.map_err(|e| e.to_string())?;
        Arc::make_mut(&mut self.graph).update_from_store(&self.store, &MatchGraphOptions::default());
        for (i, pp) in per_pos.iter().enumerate() {
            let p = p0 + i;
            let (np, nv, nm) = if n == 1 {
                (pairs.len(), st.num_valid_geometries, st.num_matched)
            } else {
                let valid =
                    pp.iter().filter(|(a, b)| self.store.get_two_view(*a, *b).is_some_and(|g| !g.inlier_matches.is_empty())).count();
                (pp.len(), valid, pp.len())
            };
            out.log(format!("[match] pos {p}: 짝 {np} 매칭 {nm} 유효 기하 {nv} (그래프 영상 {})", self.graph.image_ids().len()));
            out.emit(|meta| Event::PairsMatched { meta, pairs: np, verified: nv, position: p, matched: nm });
        }

        // 3) Registration.
        let (span, ovl) = (cfg.span, cfg.overlap);
        if last + 1 < span + ovl {
            self.checkpoint(last);
            return Ok(());
        }
        if last + 1 == span + ovl {
            let t0 = Instant::now();
            let gopts = GlobalSfmOptions { ba_backend: BaBackendRef::new(cfg.ba_backend.clone()), ..GlobalSfmOptions::script() };
            let o = global_mapper(&self.store, &self.graph, &gopts);
            out.stage("mapper", &format!("pos {last}"), t0);
            let o = o.map_err(|e| e.to_string())?;
            if let Some(f) = &o.failure {
                out.warn(format!("[mapper] 실패 보고: {f}"));
            }
            out.log(format!("[mapper] {:?}", o.summary));
            let rec = o.reconstruction;
            let model = Arc::new(rec.clone());
            let registered = rec.registered_image_count();
            out.emit(|meta| Event::ModelInitialized { meta, registered, position: last, model });
            let ids = rec.registered_images();
            self.emit_registered(&rec, &ids, out);
            self.chain = Some(rec);
        } else {
            // If a new refined model has appeared, continue registering on top of it.
            let mut k = self.adopted.map_or(0, |a| a + 1);
            loop {
                let r = sh.bg().ready.get(&k).map(|z| z.model.clone());
                let Some(r) = r else { break };
                self.chain = Some((*r).clone());
                self.adopted = Some(k);
                out.emit(|meta| Event::BaseAdopted { meta, zone: k });
                k += 1;
            }
            if let Some(base) = &self.chain {
                let mut reg = base.clone();
                let t0 = Instant::now();
                let rr =
                    register_images_with(&mut reg, &self.store, &self.graph, &RegistrationOptions::default(), cfg.sparse.abs_pose.as_ref());
                out.stage("registration", &label, t0);
                match rr {
                    Ok(rep) => {
                        let new_ids = rep.registered();
                        out.log(format!("[register] {label}: 시도 {} 등록 {}", rep.attempts.len(), new_ids.len()));
                        let scope = if cfg.incremental_triangulation {
                            TriangulationScope::Images(new_ids.clone())
                        } else {
                            TriangulationScope::AllRegistered
                        };
                        let topts = PointTriangulatorOptions { clear_points: false, scope, ..Default::default() };
                        let t0 = Instant::now();
                        let tr = triangulate_points_with(&mut reg, &self.graph, &topts, cfg.sparse.triangulation.as_ref());
                        out.stage("triangulation", &label, t0);
                        match tr {
                            Ok(t) => {
                                out.log(format!(
                                    "[triangulate] {label}: 생성 {} 연장 {} 완성 {} 병합 {} 필터 {} (점 {})",
                                    t.num_created,
                                    t.num_continued,
                                    t.num_completed,
                                    t.num_merged,
                                    t.num_filtered,
                                    reg.num_points3d()
                                ));
                                self.emit_registered(&reg, &new_ids, out);
                                self.chain = Some(reg);
                            }
                            Err(e) => out.warn(format!("[triangulate] {label} 실패: {e} (이전 모델 유지)")),
                        }
                    }
                    Err(e) => out.warn(format!("[register] {label} 실패: {e}")),
                }
            }
        }
        let registered = self.chain.as_ref().map_or(0, |r| r.registered_image_count());
        for p in p0..=last {
            if p + 1 < span + ovl {
                continue;
            }
            let expected: usize = self.frames[..=p].iter().map(|f| f.len()).sum();
            out.emit(|meta| Event::PositionDone { meta, position: p, registered, expected });
        }

        // 4) Zone completion -> preview (foreground) + refinement (background).
        for z in zones_ending_at(last, span, ovl, cfg.total_positions) {
            if !self.arrived.contains_key(&z.zone) {
                self.process_zone(z, out);
            }
        }
        self.checkpoint(last);
        Ok(())
    }

    fn emit_registered(&self, rec: &Reconstruction, ids: &[ImageId], out: &mut Out) {
        for &id in ids {
            let (Some(im), Some(pose)) = (rec.image(id), rec.world_to_cam(id)) else { continue };
            let image = im.name.clone();
            let position = self.position_of(&image).unwrap_or(usize::MAX);
            out.emit(|meta| Event::FrameRegistered { meta, position, image, image_id: id, cam_from_world: pose });
        }
    }

    /// Zone arrival: preview (foreground, only zone 0 is GPS-aligned) + start the refinement job (background).
    fn process_zone(&mut self, range: ZoneRange, out: &mut Out) {
        let sh = self.shared.clone();
        let k = range.zone;
        let snap = self.chain.clone().unwrap_or_default();
        let m = Arc::new(snap.clone());
        out.emit(|meta| Event::ZoneArrived { meta, range, model: m });
        if sh.cfg.stable_display {
            let keep: Arc<HashSet<String>> =
                Arc::new(self.position_of.iter().filter(|(_, p)| **p >= range.lo && **p < range.hi).map(|(n, _)| n.clone()).collect());
            let generation = sh.generation(k);
            let (sh2, gps, cam_ids) = (sh.clone(), self.gps.clone(), self.cam_ids.clone());
            let (snap_job, keep_job) = (snap.clone(), keep.clone());
            let h = std::thread::spawn(move || sh2.refine_job(range, snap_job, generation, gps, keep_job, cam_ids));
            sh.bg().jobs.push(h);
            self.arrived.insert(k, range);
            if sh.cfg.preview {
                self.sparse_preview(range, &snap, &keep, out);
            }
            return;
        }
        let mut preview = snap.clone();
        let mut frame = match self.adopted {
            Some(j) => format!("refined_{j}"),
            None => "unaligned".to_string(),
        };
        if k == 0 && sh.align_enu(&mut preview, "preview 0", &self.gps, out) {
            frame = "gps".to_string();
        }
        let keep: Arc<HashSet<String>> =
            Arc::new(self.position_of.iter().filter(|(_, p)| **p >= range.lo && **p < range.hi).map(|(n, _)| n.clone()).collect());
        let generation = sh.generation(k);
        {
            let (sh2, gps, keep, cam_ids) = (sh.clone(), self.gps.clone(), keep.clone(), self.cam_ids.clone());
            let h = std::thread::spawn(move || sh2.refine_job(range, snap, generation, gps, keep, cam_ids));
            sh.bg().jobs.push(h);
        }
        let cloud = sh.dense_region("preview", k, &preview, &keep, out);
        let model = Arc::new(preview);
        self.arrived.insert(k, range);
        Arc::make_mut(&mut self.previews)
            .insert(k, PreviewZone { range, model: model.clone(), cloud: cloud.clone(), frame: frame.clone() });
        out.emit(|meta| Event::ZonePreview { meta, range, dense: cloud.is_some(), cloud: cloud.unwrap_or_default(), model, frame });
    }

    /// Stable display: the zone's registered 3D points (no bundle adjustment) as a preview in the display frame, emitted as an
    /// [`Event::ZonePreview`] (`dense: false`, `frame: "display"`).
    ///
    /// Placement: a robust Sim3 on the 3D points the chain shares with the latest shown (refined) zone, or on the camera centers
    /// when too few points are shared; before any zone is shown, the GPS ENU fit the first refined zone starts from. Without a fit
    /// no preview is made (a preview in an unknown frame would jump). The shown zones are not touched: the preview only adds
    /// points of this zone's own positions (the part past the overlap with the previous zone, which shows its own).
    fn sparse_preview(&mut self, range: ZoneRange, snap: &Reconstruction, keep: &HashSet<String>, out: &mut Out) {
        let sh = self.shared.clone();
        let k = range.zone;
        // The previous zone reaches `overlap` positions past this zone's start: from there on the positions are this zone's own.
        let own_lo = if k == 0 { 0 } else { (k * sh.cfg.span + sh.cfg.overlap).max(range.lo) };
        let own: HashSet<ImageId> = snap
            .registered_images()
            .into_iter()
            .filter(|id| {
                snap.image(*id).is_some_and(|im| keep.contains(&im.name) && self.position_of.get(&im.name).is_some_and(|p| *p >= own_lo))
            })
            .collect();
        let mut cloud = PointCloud::default();
        for (_, pt) in snap.points3d() {
            let Some(t) = pt.track.iter().find(|t| own.contains(&t.image_id)) else { continue };
            cloud.positions.push([pt.xyz.x as f32, pt.xyz.y as f32, pt.xyz.z as f32]);
            cloud.colors.push(pt.color);
            cloud.keys.push(preview_key(t.image_id, t.point2d_idx));
        }
        if cloud.is_empty() {
            out.log(format!("[preview] 구역 {k}: 표시할 3D 점 없음 — 미리보기 생략"));
            return;
        }
        let shown = sh.bg().ready.range(..k).next_back().map(|(j, z)| (*j, z.model.clone()));
        let fit = match &shown {
            Some((j, m)) => match fit_shared_points(snap, m, None, MIN_SHARED_PAIRS) {
                Some((f, n)) => Some((f.sim3, format!("구역 {j} 과 공유 점 {n}, 잔차 중앙 {:.3}m", f.median))),
                None => sim3_between(snap, m, |_| true).map(|t| (t, format!("구역 {j} 과 공유 영상 카메라 중심"))),
            },
            None => {
                let targets = enu_targets(snap, &self.gps);
                let (src, dst): (Vec<_>, Vec<_>) =
                    targets.iter().filter_map(|(id, g)| snap.projection_center(*id).map(|c| (c, *g))).unzip();
                (src.len() >= 3)
                    .then(|| cumulus3d_align::fit_enu_sim3(snap, &src, &dst))
                    .flatten()
                    .map(|(t, _)| (t, format!("GPS {} 개 (ENU)", src.len())))
            }
        };
        let Some((t, how)) = fit else {
            out.warn(format!("[preview] 구역 {k}: 표시 좌표계에 맞출 기준 없음 — 미리보기 생략"));
            return;
        };
        cumulus3d_align::transform_cloud(&mut cloud, &t);
        let mut model = snap.clone();
        model.transform(&t);
        let (model, cloud) = (Arc::new(model), Arc::new(cloud));
        out.log(format!("[preview] 구역 {k}: 희소 점 {} 개를 표시 좌표계에 배치 ({how}, 축척 {:.4})", cloud.len(), t.scale));
        let frame = "display".to_string();
        Arc::make_mut(&mut self.previews)
            .insert(k, PreviewZone { range, model: model.clone(), cloud: Some(cloud.clone()), frame: frame.clone() });
        out.emit(|meta| Event::ZonePreview { meta, range, dense: false, cloud, model, frame });
    }

    fn checkpoint(&mut self, position: usize) {
        let n = self.shared.cfg.history;
        if n == 0 {
            return;
        }
        self.checkpoints.push_back(Checkpoint {
            position,
            chain: self.chain.clone(),
            adopted: self.adopted,
            cam_ids: self.cam_ids.clone(),
            graph: self.graph.clone(),
            gps_len: self.gps.len(),
        });
        while self.checkpoints.len() > n {
            self.checkpoints.pop_front();
        }
    }

    fn drop_zone(&mut self, k: usize, out: &mut Out) {
        self.arrived.remove(&k);
        Arc::make_mut(&mut self.previews).remove(&k);
        self.shared.invalidate(k);
        out.emit(|meta| Event::ZoneInvalidated { meta, zone: k });
    }

    fn apply_command(&mut self, cmd: Command, out: &mut Out) {
        match cmd {
            Command::ResetFrom(p) => self.reset_from(p, out),
            Command::InvalidateZone(k) => {
                let Some(range) = self.arrived.get(&k).copied() else {
                    out.emit(|meta| Event::Warning {
                        meta, message: format!("구역 {k} 은 아직 도착하지 않음 — 무효화할 것 없음")
                    });
                    return;
                };
                self.drop_zone(k, out);
                // Rebuild with the current chain.
                self.process_zone(range, out);
            }
        }
    }

    /// Discard the state after position p: roll the store, graph and chain back to right after position p-1, and invalidate the zones
    /// closed after that.
    fn reset_from(&mut self, p: usize, out: &mut Out) {
        let npos = self.frames.len();
        if p > npos {
            out.emit(|meta| Event::Warning { meta, message: format!("ResetFrom({p}): 처리한 위치 {npos} 보다 큼 — 무시") });
            return;
        }
        let cp = if p == 0 {
            None
        } else {
            match self.checkpoints.iter().find(|c| c.position + 1 == p) {
                Some(c) => Some(c.clone()),
                None => {
                    out.emit(|meta| Event::Warning {
                        meta, message: format!("ResetFrom({p}): 되감기 지점 없음(history 부족) — 무시")
                    });
                    return;
                }
            }
        };
        let gone: Vec<usize> = self.arrived.iter().filter(|(_, r)| r.hi.saturating_sub(1) >= p).map(|(k, _)| *k).collect();
        for k in gone {
            self.drop_zone(k, out);
        }
        let pos = self.position_of.clone();
        self.store = Arc::new(self.store.retain_copy(|im| pos.get(&im.name).is_some_and(|q| *q < p)));
        Arc::make_mut(&mut self.frames).truncate(p);
        Arc::make_mut(&mut self.position_of).retain(|_, q| *q < p);
        self.ingested = self.frames.iter().map(|f| f.len()).sum();
        self.checkpoints.retain(|c| c.position < p);
        match cp {
            Some(c) => {
                self.chain = c.chain;
                self.adopted = c.adopted;
                self.cam_ids = c.cam_ids;
                self.graph = c.graph;
                Arc::make_mut(&mut self.gps).truncate(c.gps_len);
            }
            None => {
                self.chain = None;
                self.adopted = None;
                self.cam_ids = Arc::new(BTreeMap::new());
                self.graph = Arc::new(MatchGraph::new());
                self.gps = Arc::new(self.shared.cfg.gps.clone());
            }
        }
        self.shared.ahead.0.reset(Arc::clone(&self.store), (*self.cam_ids).clone());
        out.emit(|meta| Event::Reset { meta, position: p });
    }

    fn finish_session(&mut self, out: &mut Out) {
        let sh = self.shared.clone();
        let (span, ovl) = (sh.cfg.span, sh.cfg.overlap);
        // If the total count was unknown, close the remaining zones at the last position.
        let npos = self.frames.len();
        if npos > 0 && npos >= span + ovl {
            for z in zones_upto(npos, span, ovl) {
                if !self.arrived.contains_key(&z.zone) {
                    self.process_zone(z, out);
                }
            }
        }
        out.emit(|meta| Event::AllPositionsDone { meta });
        loop {
            let jobs = std::mem::take(&mut sh.bg().jobs);
            if jobs.is_empty() {
                break;
            }
            for j in jobs {
                let _ = j.join();
            }
        }
        let chain = match (&self.chain, sh.cfg.stable_display) {
            (Some(c), true) => {
                // Final outputs in ENU through one GPS alignment of a copy of the whole model (with or without GPS priors the
                // display frame is the first shown zone's frame); the display chain itself stays untouched. On failure the
                // display-frame chain is handed on.
                let mut enu = c.clone();
                if sh.align_enu(&mut enu, "final (stable display)", &self.gps, out) {
                    // Display → ENU scale on the camera centers: close to the zone-0 initial ENU fit (1.0 when the display frame
                    // stayed ENU through the GPS priors); a large gap means the frame drifted.
                    match display_to_enu_scale(c, &enu) {
                        Some(s) => out.log(format!("[stable] 표시→ENU 축척 {s:.4} (1.0 이면 표시 좌표계가 ENU 와 같음)")),
                        None => out.log("[stable] 표시→ENU 축척: 공통 영상 부족".to_string()),
                    }
                    Some(Arc::new(enu))
                } else {
                    out.warn(
                        "[align] final (stable display): GPS 정렬 실패 — 표시 좌표계 모델을 그대로 넘김(final_frame/ 은 표시 좌표계)"
                            .to_string(),
                    );
                    Some(Arc::new(c.clone()))
                }
            }
            (c, _) => c.clone().map(Arc::new),
        };
        out.emit(|meta| Event::AllRefinedDone { meta, chain });
        let pipeline_secs = sh.start.lock().unwrap_or_else(|e| e.into_inner()).map_or(0.0, |t| t.elapsed().as_secs_f64());
        let stage_times = sh.times.snapshot().into_iter().map(|(k, (d, n))| (k, d, n)).collect();
        let chain_stats = self.chain.as_ref().map(|c| ModelStats::compute(c).lines()).unwrap_or_default();
        out.emit(|meta| Event::Finished { meta, pipeline_secs, stage_times, chain_stats });
    }
}

fn report_extraction(rep: &[ImageReport], position: usize, out: &mut Out) {
    for r in rep {
        match &r.status {
            ImageStatus::Extracted { image_id, camera_id, num_features } => {
                out.log(format!("[feature] {} id {image_id} cam {camera_id} 특징 {num_features}", r.name));
                let (image, image_id, count) = (r.name.clone(), *image_id, *num_features);
                out.emit(|meta| Event::FeaturesExtracted { meta, image, count, position, image_id });
            }
            ImageStatus::AlreadyExists { image_id } => out.log(format!("[feature] {} id {image_id} 이미 있음", r.name)),
            ImageStatus::Failed { error } => out.warn(format!("[feature] {} 실패: {error}", r.name)),
        }
    }
}

pub(crate) fn fmt_params(p: &[f64]) -> String {
    format!("[{}]", p.iter().map(|x| format!("{x:.5}")).collect::<Vec<_>>().join(" "))
}

/// Scale of the Sim3 (Umeyama on the projection centers of the images registered in both) taking `display` to `enu`.
fn display_to_enu_scale(display: &Reconstruction, enu: &Reconstruction) -> Option<f64> {
    let (mut src, mut dst) = (Vec::new(), Vec::new());
    for id in display.registered_images() {
        let name = display.image(id)?.name.clone();
        let Some(eid) = enu.image_by_name(&name).map(|im| im.image_id) else { continue };
        if let (Some(a), Some(b)) = (display.projection_center(id), enu.projection_center(eid)) {
            src.push(a);
            dst.push(b);
        }
    }
    if src.len() < 3 {
        return None;
    }
    cumulus3d_align::umeyama(&src, &dst, true).map(|t| t.scale)
}

/// One log line for a GPS fit whose positions were collinear.
fn gravity_note(what: &str, g: &cumulus3d_align::GravityResolution) -> String {
    use cumulus3d_align::GravityPrior;
    let prior = match g.prior {
        GravityPrior::Ground { inliers } => format!("지면 평면(점 {inliers} 개)"),
        GravityPrior::ViewDirection => "카메라 시선(아래 방향)".to_string(),
        GravityPrior::None => "사용 불가 — 회전 그대로".to_string(),
    };
    format!("[gravity] {what}: GPS 가 거의 일직선(σ2/σ1 {:.3}) — 직선 둘레 회전을 {prior} 로 결정", g.ratio)
}

/// ENU positions (origin = first GPS record) of the registered images of `rec` that have a GPS record.
fn enu_targets(rec: &Reconstruction, gps: &[GpsRecord]) -> HashMap<ImageId, cumulus3d_core::Vec3> {
    let Some(o) = gps.first() else { return HashMap::new() };
    let frame = cumulus3d_align::EnuFrame::new(o.lat, o.lon, o.alt);
    let by_name: HashMap<&str, &GpsRecord> = gps.iter().map(|g| (g.name.as_str(), g)).collect();
    rec.registered_images()
        .into_iter()
        .filter_map(|id| {
            let g = by_name.get(rec.image(id)?.name.as_str())?;
            Some((id, frame.lla_to_enu(g.lat, g.lon, g.alt)))
        })
        .collect()
}

/// Brings `rec` into the frame of the already shown model `shown` (Sim3 on the projection centers of the images registered
/// in both). Returns the Sim3 and, for each shared image, its pose in `shown`.
fn sim3_onto_shown(rec: &mut Reconstruction, shown: &Reconstruction) -> Option<(cumulus3d_core::Sim3, Vec<(ImageId, Rigid3)>)> {
    let mut pairs = Vec::new();
    for id in rec.registered_images() {
        let Some(name) = rec.image(id).map(|im| im.name.clone()) else { continue };
        let Some(sid) = shown.image_by_name(&name).map(|im| im.image_id) else { continue };
        if let (Some(a), Some(b), Some(pose)) = (rec.projection_center(id), shown.projection_center(sid), shown.world_to_cam(sid)) {
            pairs.push((id, a, b, pose));
        }
    }
    let (src, dst): (Vec<_>, Vec<_>) = pairs.iter().map(|(_, a, b, _)| (*a, *b)).unzip();
    let t = cumulus3d_align::umeyama(&src, &dst, true)?;
    rec.transform(&t);
    Some((t, pairs.into_iter().map(|(id, _, _, pose)| (id, pose)).collect()))
}

/// Stable display, settled: brings `rec` into the frame of the already shown model `shown`, then copies the shown poses and
/// the camera intrinsics exactly and marks them constant in `ba`. Returns (fixed image count, Sim3 scale).
fn anchor_to_shown(rec: &mut Reconstruction, shown: &Reconstruction, ba: &mut BaConfig) -> (usize, f64) {
    let Some((t, poses)) = sim3_onto_shown(rec, shown) else { return (0, 1.0) };
    for (id, pose) in &poses {
        if rec.set_world_to_cam(*id, *pose).is_ok() {
            ba.constant_poses.insert(*id);
        }
    }
    for (cid, cam) in shown.cameras() {
        if let Some(c) = rec.camera_mut(*cid) {
            *c = cam.clone();
            ba.constant_cameras.insert(*cid);
        }
    }
    (ba.constant_poses.len(), t.scale)
}

/// Fewest shared 3D points for which a point-based Sim3 fit is trusted over the camera-center fit.
const MIN_SHARED_PAIRS: usize = 20;

/// Key of a preview point: the dense point key layout (`image << 32 | v << 16 | u`) with `v` = 0xFFFF, a row no depth map pixel
/// has, and the 2D point index of the track's first observation in place of `u`. Never equal to a dense key.
fn preview_key(image: ImageId, point2d: u32) -> u64 {
    ((image as u64) << 32) | 0xFFFF_0000 | (point2d as u64 & 0xFFFF)
}

/// Sim3 taking `from` into the frame of `to` (umeyama with scale on the projection centers of the images, accepted by
/// `keep`, registered in both). None with fewer than 3 common images.
fn sim3_between(from: &Reconstruction, to: &Reconstruction, keep: impl Fn(&str) -> bool) -> Option<cumulus3d_core::Sim3> {
    let (mut src, mut dst) = (Vec::new(), Vec::new());
    for id in from.registered_images() {
        let Some(name) = from.image(id).map(|im| im.name.as_str()).filter(|n| keep(n)) else { continue };
        let Some(tid) = to.image_by_name(name).map(|im| im.image_id) else { continue };
        if let (Some(a), Some(b)) = (from.projection_center(id), to.projection_center(tid)) {
            src.push(a);
            dst.push(b);
        }
    }
    if src.len() < 3 {
        return None;
    }
    cumulus3d_align::umeyama(&src, &dst, true)
}

/// Largest relative change of a focal parameter (fx, fy; just f for single-focal models) over the cameras present in both
/// `a` and `b`, as a fraction of `a`'s value. Infinite when a camera changed its model kind; 0 when no camera is shared.
pub fn intrinsics_change(a: &BTreeMap<CameraId, Camera>, b: &BTreeMap<CameraId, Camera>) -> f64 {
    let mut worst = 0.0f64;
    for (id, ca) in a {
        let Some(cb) = b.get(id) else { continue };
        if ca.model != cb.model {
            return f64::INFINITY;
        }
        for &s in ca.model.focal_slots() {
            let (fa, fb) = (ca.params[s], cb.params[s]);
            worst = worst.max(if fa != 0.0 {
                ((fb - fa) / fa).abs()
            } else if fb != 0.0 {
                f64::INFINITY
            } else {
                0.0
            });
        }
    }
    worst
}

/// Pairing rule: same camera at position gaps 1..5, 8, 16 / different cameras at position difference 0..4. Only pairs involving an image
/// of position p, as sorted name pairs.
pub fn pairs_for(frames: &[Vec<FrameImage>], p: usize) -> Vec<(String, String)> {
    let mut pr = BTreeSet::new();
    let Some(cur) = frames.get(p) else { return Vec::new() };
    for im in cur {
        for d in [1usize, 2, 3, 4, 5, 8, 16] {
            if p >= d {
                if let Some(o) = frames[p - d].iter().find(|x| x.camera == im.camera) {
                    pr.insert((o.name.clone(), im.name.clone()));
                }
            }
        }
    }
    for a in cur {
        for fq in &frames[p.saturating_sub(4)..=p] {
            for b in fq {
                if a.camera == b.camera {
                    continue;
                }
                let (x, y) = (a.name.clone(), b.name.clone());
                pr.insert(if x <= y { (x, y) } else { (y, x) });
            }
        }
    }
    pr.into_iter().collect()
}

/// Zones: start = 0, span, ...; [start-ovl, start+span+ovl) intersect [0, total). Completion position = hi-1.
pub fn zones_upto(total: usize, span: usize, ovl: usize) -> Vec<ZoneRange> {
    let mut v = Vec::new();
    let mut start = 0;
    let mut k = 0;
    while start < total {
        v.push(ZoneRange { zone: k, lo: start.saturating_sub(ovl), hi: (start + span + ovl).min(total) });
        k += 1;
        start += span.max(1);
    }
    v
}

/// Zones closed at position p (if the total count is unknown, only zones that are not truncated).
fn zones_ending_at(p: usize, span: usize, ovl: usize, total: Option<usize>) -> Vec<ZoneRange> {
    let mut v = Vec::new();
    let mut k = 0;
    loop {
        let start = k * span.max(1);
        if start > p || total.is_some_and(|n| start >= n) {
            break;
        }
        let hi = total.map_or(start + span + ovl, |n| (start + span + ovl).min(n));
        if hi == p + 1 {
            v.push(ZoneRange { zone: k, lo: start.saturating_sub(ovl), hi });
        }
        k += 1;
    }
    v
}

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

    fn frames(n: usize) -> Vec<Vec<FrameImage>> {
        (0..n)
            .map(|p| {
                ["camF", "camR", "camL"]
                    .iter()
                    .map(|c| FrameImage { camera: c.to_string(), name: format!("{c}/{c}_{:04}.jpg", p * 3) })
                    .collect()
            })
            .collect()
    }

    #[test]
    fn zones_match_regions() {
        let z = |v: Vec<ZoneRange>| v.into_iter().map(|r| (r.zone, r.lo, r.hi)).collect::<Vec<_>>();
        assert_eq!(z(zones_upto(26, 12, 2)), vec![(0, 0, 14), (1, 10, 26), (2, 22, 26)]);
        // Collecting the zones closed at each position gives the same list.
        let mut all = Vec::new();
        for p in 0..26 {
            all.extend(zones_ending_at(p, 12, 2, Some(26)));
        }
        all.sort_by_key(|r| r.zone);
        assert_eq!(z(all), z(zones_upto(26, 12, 2)));
        // If the total count is unknown, only non-truncated zones are closed at a position.
        assert_eq!(z(zones_ending_at(13, 12, 2, None)), vec![(0, 0, 14)]);
        assert_eq!(z(zones_ending_at(25, 12, 2, None)), vec![(1, 10, 26)]);
        assert!(zones_ending_at(25, 12, 2, Some(40)).iter().any(|r| r.zone == 1));
    }

    #[test]
    fn intrinsics_change_is_max_relative_focal_change() {
        use cumulus3d_core::CameraModelKind;
        let cam = |id: CameraId, model: CameraModelKind, params: Vec<f64>| Camera::new(id, model, 4000, 3000, params).unwrap();
        let single = |id, f: f64| cam(id, CameraModelKind::SingleFocalRadial, vec![f, 2000.0, 1500.0, 0.1]);
        let pinhole = |id, fx: f64, fy: f64| cam(id, CameraModelKind::Pinhole, vec![fx, fy, 2000.0, 1500.0]);
        let map = |v: Vec<Camera>| v.into_iter().map(|c| (c.camera_id, c)).collect::<BTreeMap<_, _>>();
        let a = map(vec![single(1, 1000.0), pinhole(2, 2000.0, 2000.0)]);
        assert_eq!(intrinsics_change(&a, &a), 0.0);
        // Principal point / distortion changes do not count; the worst focal parameter of any camera does.
        let b = map(vec![single(1, 1005.0), pinhole(2, 2000.0, 2040.0), single(3, 1.0)]);
        assert!((intrinsics_change(&a, &b) - 0.02).abs() < 1e-12);
        let c = map(vec![single(1, 990.0), pinhole(2, 2000.0, 2000.0)]);
        assert!((intrinsics_change(&a, &c) - 0.01).abs() < 1e-12);
        // No shared camera: nothing changed; model kind change: infinite.
        assert_eq!(intrinsics_change(&a, &map(vec![single(9, 5.0)])), 0.0);
        assert!(intrinsics_change(&a, &map(vec![pinhole(1, 1000.0, 1000.0)])).is_infinite());
    }

    #[test]
    fn pair_rule() {
        let f = frames(20);
        assert_eq!(pairs_for(&f, 0).len(), 3);
        let p17 = pairs_for(&f, 17);
        assert_eq!(p17.len(), 21 + 27);
        assert!(p17.contains(&("camF/camF_0003.jpg".to_string(), "camF/camF_0051.jpg".to_string())));
    }
}