cumulus3d-cli 0.6.0

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
/*
 * events.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
 */

//! Pipeline events. Each time a new frame bundle is processed, the result decomposed by stage is emitted.
//! This file is the shared contract of three modules (session, pipeline, sinks). Adding fields is allowed; changing or removing existing
//! fields is forbidden.

use cumulus3d_core::io::ply::PointCloud;
use cumulus3d_core::{ImageId, Reconstruction, Rigid3, Sim3};
use std::sync::Arc;
use std::time::{Duration, SystemTime};

/// Header attached to every event.
#[derive(Clone, Debug)]
pub struct Meta {
    /// Session version (a monotonically increasing number of the state that emitted the event).
    pub version: u64,
    /// Time of occurrence.
    pub at: SystemTime,
}

/// Zone index and position range [lo, hi).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ZoneRange {
    /// Zone index.
    pub zone: usize,
    /// First position (inclusive).
    pub lo: usize,
    /// End position (exclusive).
    pub hi: usize,
}

/// A decoded input frame (one camera image). It is delivered first as [`Event::FrameDecoded`], before processing (features, matching,
/// registration).
#[derive(Clone, Debug)]
pub struct Frame {
    /// Position index.
    pub position: usize,
    /// Camera name (e.g. `camF`).
    pub camera: String,
    /// Image name (path relative to `image_root`).
    pub name: String,
    /// Image file path.
    pub path: std::path::PathBuf,
    /// Width in pixels.
    pub width: u32,
    /// Height in pixels.
    pub height: u32,
    /// RGB8 pixels (row-major, length `width * height * 3`). Shared between hooks without copying.
    pub rgb: Arc<[u8]>,
    /// GPS record of this image (if any).
    pub gps: Option<cumulus3d_core::io::GpsRecord>,
}

/// Pipeline events (per-stage results). Every variant carries a [`Meta`].
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Event {
    /// The frame bundle of one position has arrived.
    FrameIngested {
        /// Common header.
        meta: Meta,
        /// Position index.
        position: usize,
        /// Image names of this position.
        images: Vec<String>,
    },
    /// Decoding of one input frame is complete. Comes first, before the position is processed (only when `SessionConfig::decode_frames`
    /// is on).
    FrameDecoded {
        /// Common header.
        meta: Meta,
        /// Decoded frame.
        frame: Arc<Frame>,
    },
    /// Session start (once, at the first input). `positions` = total number of known positions. (added by session)
    Started {
        /// Common header.
        meta: Meta,
        /// Total number of known positions (None if unknown).
        positions: Option<usize>,
    },
    /// One run.log line. If `stamped`, it is written with a `[HH:MM:SS] ` prefix (meta.at local time). (added by session)
    Log {
        /// Common header.
        meta: Meta,
        /// Line to record.
        line: String,
        /// Whether to add the `[HH:MM:SS] ` prefix.
        stamped: bool,
    },
    /// Feature extraction of one image is complete.
    FeaturesExtracted {
        /// Common header.
        meta: Meta,
        /// Image name.
        image: String,
        /// Number of extracted features.
        count: usize,
        /// Position index.
        position: usize,
        /// Image id.
        image_id: ImageId,
    },
    /// Result of matching new pairs.
    /// `pairs` = pairs matched at this position, `verified` = pairs with valid geometry, `matched` = pairs for which descriptor matching
    /// was performed.
    PairsMatched {
        /// Common header.
        meta: Meta,
        /// Pairs matched at this position.
        pairs: usize,
        /// Pairs with valid geometry.
        verified: usize,
        /// Position index.
        position: usize,
        /// Pairs for which descriptor matching was performed.
        matched: usize,
    },
    /// The first model has been built.
    ModelInitialized {
        /// Common header.
        meta: Meta,
        /// Number of registered images.
        registered: usize,
        /// Position index.
        position: usize,
        /// Copy of the relevant model.
        model: Arc<Reconstruction>,
    },
    /// An image has been registered to the model.
    FrameRegistered {
        /// Common header.
        meta: Meta,
        /// Position index.
        position: usize,
        /// Image name.
        image: String,
        /// Image id.
        image_id: ImageId,
        /// Registered pose (world -> camera).
        cam_from_world: Rigid3,
    },
    /// Registration and triangulation of one position are finished.
    PositionDone {
        /// Common header.
        meta: Meta,
        /// Position index.
        position: usize,
        /// Number of registered images.
        registered: usize,
        /// Number of images expected up to this position.
        expected: usize,
    },
    /// The last position of a zone has arrived.
    /// `model` = copy of the chain model at that point (before alignment).
    ZoneArrived {
        /// Common header.
        meta: Meta,
        /// Zone range.
        range: ZoneRange,
        /// Copy of the relevant model.
        model: Arc<Reconstruction>,
    },
    /// Zone preview point cloud (no BA).
    /// `model` = preview model (zone 0 is the GPS-aligned one), `dense` = false means densification was not run or failed and `cloud` is
    /// an empty placeholder.
    /// `frame` = point-cloud coordinate frame ("gps" | "refined_K" | "unaligned").
    ZonePreview {
        /// Common header.
        meta: Meta,
        /// Zone range.
        range: ZoneRange,
        /// Point cloud (an empty placeholder if there is no densification).
        cloud: Arc<PointCloud>,
        /// Copy of the relevant model.
        model: Arc<Reconstruction>,
        /// Whether densification was actually performed.
        dense: bool,
        /// Point-cloud coordinate frame ("gps" | "refined_K" | "unaligned").
        frame: String,
    },
    /// Refinement job start (background). (added by session)
    RefineStarted {
        /// Common header.
        meta: Meta,
        /// Zone index.
        zone: usize,
    },
    /// Model right after BA of the refinement job (before GPS alignment). (added by session)
    ZoneAdjusted {
        /// Common header.
        meta: Meta,
        /// Zone index.
        zone: usize,
        /// Copy of the relevant model.
        model: Arc<Reconstruction>,
    },
    /// Zone refined pose finalized (BA + GPS alignment).
    /// `model` = refined model after BA + GPS alignment.
    ZoneRefinedPose {
        /// Common header.
        meta: Meta,
        /// Zone range.
        range: ZoneRange,
        /// Number of registered images.
        registered: usize,
        /// Mean reprojection error (pixels).
        mean_reproj_px: f64,
        /// Copy of the relevant model.
        model: Arc<Reconstruction>,
    },
    /// Zone refined point cloud. It replaces the preview of the same zone.
    /// `dense` = false means `cloud` is an empty placeholder.
    ZoneRefined {
        /// Common header.
        meta: Meta,
        /// Zone range.
        range: ZoneRange,
        /// Point cloud (an empty placeholder if there is no densification).
        cloud: Arc<PointCloud>,
        /// Copy of the relevant model.
        model: Arc<Reconstruction>,
        /// Whether densification was actually performed.
        dense: bool,
    },
    /// The refined model was adopted as the reference for subsequent registration.
    BaseAdopted {
        /// Common header.
        meta: Meta,
        /// Zone index.
        zone: usize,
    },
    /// Coordinate-frame transform of an already sent zone (the point cloud is not sent again): new_from_old.
    Reanchored {
        /// Common header.
        meta: Meta,
        /// Zone index.
        zone: usize,
        /// new_from_old transform.
        transform: Sim3,
    },
    /// Snapshot for display (event sequence number and composite point cloud).
    Snapshot {
        /// Common header.
        meta: Meta,
        /// Event sequence number.
        index: usize,
        /// Point cloud (an empty placeholder if there is no densification).
        cloud: Arc<PointCloud>,
    },
    /// All positions processed.
    AllPositionsDone {
        /// Common header.
        meta: Meta,
    },
    /// All refinement jobs complete.
    /// `chain` = final chain model.
    AllRefinedDone {
        /// Common header.
        meta: Meta,
        /// Final chain model.
        chain: Option<Arc<Reconstruction>>,
    },
    /// Session end summary (the last of all events). `stage_times` = (stage, accumulated time, count) in stage-name order,
    /// `pipeline_secs` = start to all refinements complete, `chain_stats` = statistics line of the final chain model. (added by session)
    Finished {
        /// Common header.
        meta: Meta,
        /// Start to all refinements complete (seconds).
        pipeline_secs: f64,
        /// (stage, accumulated time, count) in stage-name order.
        stage_times: Vec<(String, Duration, usize)>,
        /// Statistics line of the final chain model.
        chain_stats: Vec<String>,
    },
    /// `Command::ResetFrom` was handled. (added by session)
    Reset {
        /// Common header.
        meta: Meta,
        /// Position index.
        position: usize,
    },
    /// `Command::InvalidateZone` was handled. (added by session)
    ZoneInvalidated {
        /// Common header.
        meta: Meta,
        /// Zone index.
        zone: usize,
    },
    /// Incremental display update computed by the library (opt-in, see `NodeBuilder::display_deltas` and
    /// [`crate::live::DisplayFeed`]): the point-keyed difference between the previous and the new composed display cloud after
    /// a zone result. Emitted right after the event that caused it.
    DisplayDelta {
        /// Common header (of the causing event).
        meta: Meta,
        /// Adds, moves and removes with dense slot ids (apply: removes, adds, moves).
        delta: Arc<crate::live::DisplayDelta>,
        /// Zone whose result caused the delta.
        zone: usize,
        /// Why it was emitted.
        kind: crate::live::DeltaKind,
        /// Seconds since the session start.
        t: f64,
        /// Coordinate frame of the display points.
        frame: crate::live::DisplayFrame,
    },
    /// Warning.
    Warning {
        /// Common header.
        meta: Meta,
        /// Message.
        message: String,
    },
    /// Error (including hook panics).
    Error {
        /// Common header.
        meta: Meta,
        /// Message.
        message: String,
    },
}

/// Event kind (for hook registration and filtering).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum EventKind {
    /// [`Event::FrameIngested`].
    FrameIngested,
    /// [`Event::FrameDecoded`].
    FrameDecoded,
    /// [`Event::Started`].
    Started,
    /// [`Event::Log`].
    Log,
    /// [`Event::RefineStarted`].
    RefineStarted,
    /// [`Event::ZoneAdjusted`].
    ZoneAdjusted,
    /// [`Event::Finished`].
    Finished,
    /// [`Event::Reset`].
    Reset,
    /// [`Event::ZoneInvalidated`].
    ZoneInvalidated,
    /// [`Event::FeaturesExtracted`].
    FeaturesExtracted,
    /// [`Event::PairsMatched`].
    PairsMatched,
    /// [`Event::ModelInitialized`].
    ModelInitialized,
    /// [`Event::FrameRegistered`].
    FrameRegistered,
    /// [`Event::PositionDone`].
    PositionDone,
    /// [`Event::ZoneArrived`].
    ZoneArrived,
    /// [`Event::ZonePreview`].
    ZonePreview,
    /// [`Event::ZoneRefinedPose`].
    ZoneRefinedPose,
    /// [`Event::ZoneRefined`].
    ZoneRefined,
    /// [`Event::BaseAdopted`].
    BaseAdopted,
    /// [`Event::Reanchored`].
    Reanchored,
    /// [`Event::Snapshot`].
    Snapshot,
    /// [`Event::AllPositionsDone`].
    AllPositionsDone,
    /// [`Event::AllRefinedDone`].
    AllRefinedDone,
    /// [`Event::DisplayDelta`].
    DisplayDelta,
    /// [`Event::Warning`].
    Warning,
    /// [`Event::Error`].
    Error,
}

impl Event {
    /// Event kind.
    pub fn kind(&self) -> EventKind {
        use Event::*;
        match self {
            FrameIngested { .. } => EventKind::FrameIngested,
            FrameDecoded { .. } => EventKind::FrameDecoded,
            Started { .. } => EventKind::Started,
            Log { .. } => EventKind::Log,
            RefineStarted { .. } => EventKind::RefineStarted,
            ZoneAdjusted { .. } => EventKind::ZoneAdjusted,
            Finished { .. } => EventKind::Finished,
            Reset { .. } => EventKind::Reset,
            ZoneInvalidated { .. } => EventKind::ZoneInvalidated,
            FeaturesExtracted { .. } => EventKind::FeaturesExtracted,
            PairsMatched { .. } => EventKind::PairsMatched,
            ModelInitialized { .. } => EventKind::ModelInitialized,
            FrameRegistered { .. } => EventKind::FrameRegistered,
            PositionDone { .. } => EventKind::PositionDone,
            ZoneArrived { .. } => EventKind::ZoneArrived,
            ZonePreview { .. } => EventKind::ZonePreview,
            ZoneRefinedPose { .. } => EventKind::ZoneRefinedPose,
            ZoneRefined { .. } => EventKind::ZoneRefined,
            BaseAdopted { .. } => EventKind::BaseAdopted,
            Reanchored { .. } => EventKind::Reanchored,
            Snapshot { .. } => EventKind::Snapshot,
            AllPositionsDone { .. } => EventKind::AllPositionsDone,
            AllRefinedDone { .. } => EventKind::AllRefinedDone,
            DisplayDelta { .. } => EventKind::DisplayDelta,
            Warning { .. } => EventKind::Warning,
            Error { .. } => EventKind::Error,
        }
    }
    /// Common header.
    pub fn meta(&self) -> &Meta {
        use Event::*;
        match self {
            FrameIngested { meta, .. }
            | FeaturesExtracted { meta, .. }
            | PairsMatched { meta, .. }
            | ModelInitialized { meta, .. }
            | FrameRegistered { meta, .. }
            | PositionDone { meta, .. }
            | ZoneArrived { meta, .. }
            | ZonePreview { meta, .. }
            | ZoneRefinedPose { meta, .. }
            | ZoneRefined { meta, .. }
            | BaseAdopted { meta, .. }
            | Reanchored { meta, .. }
            | Snapshot { meta, .. }
            | AllPositionsDone { meta }
            | AllRefinedDone { meta, .. }
            | FrameDecoded { meta, .. }
            | Started { meta, .. }
            | Log { meta, .. }
            | RefineStarted { meta, .. }
            | ZoneAdjusted { meta, .. }
            | Finished { meta, .. }
            | Reset { meta, .. }
            | ZoneInvalidated { meta, .. }
            | DisplayDelta { meta, .. }
            | Warning { meta, .. }
            | Error { meta, .. } => meta,
        }
    }
}

impl Event {
    /// Event text for timeline.txt (None if there is none). The wording and format are the same as the existing record.
    pub fn timeline_text(&self) -> Option<String> {
        use Event::*;
        Some(match self {
            Started { positions, .. } => match positions {
                Some(n) => format!("start NPOS={n}"),
                None => "start".to_string(),
            },
            ModelInitialized { registered, .. } => format!("init_model Registered images: {registered}"),
            BaseAdopted { zone, .. } => format!("adopt refined {zone} as base"),
            PositionDone { position, registered, expected, .. } => format!("pos {position} registered {registered}/{expected}"),
            ZoneArrived { range, .. } => format!("region {} arrived pos[{},{})", range.zone, range.lo, range.hi),
            ZonePreview { range, .. } => format!("preview {} ready", range.zone),
            RefineStarted { zone, .. } => format!("refined {zone} ba_start"),
            ZoneRefinedPose { range, registered, mean_reproj_px, .. } => {
                format!("refined {} pose_ready Registered images: {registered} Mean reprojection error: {mean_reproj_px:.6}px ", range.zone)
            }
            ZoneRefined { range, .. } => format!("refined {} ready", range.zone),
            AllPositionsDone { .. } => "all positions done".to_string(),
            AllRefinedDone { .. } => "all refined done".to_string(),
            _ => return None,
        })
    }
}

/// Commands fed into the reducer.
#[derive(Clone, Debug)]
pub enum Command {
    /// Reprocess from this position (discard later state).
    ResetFrom(usize),
    /// Invalidate the zone result and have it rebuilt.
    InvalidateZone(usize),
}