oximedia-virtual 0.1.7

Virtual production and LED wall tools for OxiMedia
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
#![allow(dead_code)]
//! Stage zone management for virtual production environments.
//!
//! Defines zones within an LED stage (talent area, background wall, ceiling,
//! side panels, etc.) and provides a manager for coordinating them.

/// Zones that make up a virtual production stage.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StageZone {
    /// Primary background LED wall.
    BackWall,
    /// Left side LED panel.
    SideLeft,
    /// Right side LED panel.
    SideRight,
    /// Ceiling LED canopy.
    Ceiling,
    /// Floor projection surface.
    Floor,
    /// Performance area for talent (no display surface).
    TalentArea,
}

impl StageZone {
    /// Returns true if this zone is an active display surface.
    #[must_use]
    pub fn is_display_surface(&self) -> bool {
        !matches!(self, StageZone::TalentArea)
    }

    /// Returns the nominal fill fraction relative to a full 360-degree
    /// stage enclosure (rough approximation, sums to ≤ 1.0).
    #[allow(clippy::cast_precision_loss)]
    #[must_use]
    pub fn coverage_fraction(&self) -> f32 {
        match self {
            StageZone::BackWall => 0.30,
            StageZone::SideLeft => 0.15,
            StageZone::SideRight => 0.15,
            StageZone::Ceiling => 0.20,
            StageZone::Floor => 0.10,
            StageZone::TalentArea => 0.10,
        }
    }
}

impl std::fmt::Display for StageZone {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            StageZone::BackWall => write!(f, "BackWall"),
            StageZone::SideLeft => write!(f, "SideLeft"),
            StageZone::SideRight => write!(f, "SideRight"),
            StageZone::Ceiling => write!(f, "Ceiling"),
            StageZone::Floor => write!(f, "Floor"),
            StageZone::TalentArea => write!(f, "TalentArea"),
        }
    }
}

/// Physical dimensions of a stage zone in metres.
#[derive(Debug, Clone, Copy)]
pub struct ZoneDimensions {
    /// Width in metres.
    pub width_m: f32,
    /// Height in metres.
    pub height_m: f32,
    /// Depth in metres (relevant for wrap-around surfaces).
    pub depth_m: f32,
}

impl ZoneDimensions {
    /// Create dimensions.
    #[must_use]
    pub fn new(width_m: f32, height_m: f32, depth_m: f32) -> Self {
        Self {
            width_m,
            height_m,
            depth_m,
        }
    }

    /// Surface area in square metres (width × height).
    #[allow(clippy::cast_precision_loss)]
    #[must_use]
    pub fn surface_area_m2(&self) -> f32 {
        self.width_m * self.height_m
    }
}

/// Activation state of a stage zone.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZoneState {
    /// Zone is online and receiving content.
    Online,
    /// Zone is configured but not currently showing content.
    Standby,
    /// Zone has encountered an error.
    Faulted,
}

impl std::fmt::Display for ZoneState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ZoneState::Online => write!(f, "Online"),
            ZoneState::Standby => write!(f, "Standby"),
            ZoneState::Faulted => write!(f, "Faulted"),
        }
    }
}

/// A stage layout entry binding a zone to its dimensions and state.
#[derive(Debug, Clone)]
pub struct StageLayout {
    /// Zone identifier.
    pub zone: StageZone,
    /// Physical dimensions.
    pub dimensions: ZoneDimensions,
    /// Current operational state.
    pub state: ZoneState,
    /// LED panel pixel pitch in millimetres (None if not a display surface).
    pub pixel_pitch_mm: Option<f32>,
}

impl StageLayout {
    /// Create a new stage layout entry in `Standby` state.
    #[must_use]
    pub fn new(zone: StageZone, dimensions: ZoneDimensions, pixel_pitch_mm: Option<f32>) -> Self {
        Self {
            zone,
            dimensions,
            state: ZoneState::Standby,
            pixel_pitch_mm,
        }
    }

    /// Bring the zone online.
    pub fn bring_online(&mut self) {
        if self.state != ZoneState::Faulted {
            self.state = ZoneState::Online;
        }
    }

    /// Put the zone into standby.
    pub fn put_standby(&mut self) {
        self.state = ZoneState::Standby;
    }

    /// Mark the zone as faulted.
    pub fn fault(&mut self) {
        self.state = ZoneState::Faulted;
    }

    /// Estimated horizontal pixel resolution given the width and pixel pitch.
    #[must_use]
    pub fn pixel_width(&self) -> Option<u32> {
        self.pixel_pitch_mm.map(|pitch_mm| {
            #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
            let w = ((self.dimensions.width_m * 1000.0) / pitch_mm) as u32;
            w
        })
    }

    /// Estimated vertical pixel resolution given the height and pixel pitch.
    #[must_use]
    pub fn pixel_height(&self) -> Option<u32> {
        self.pixel_pitch_mm.map(|pitch_mm| {
            #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
            let h = ((self.dimensions.height_m * 1000.0) / pitch_mm) as u32;
            h
        })
    }

    /// Returns true if this zone is online and showing content.
    #[must_use]
    pub fn is_active(&self) -> bool {
        self.state == ZoneState::Online
    }
}

/// Manages all stage zones for a virtual production set.
#[derive(Debug, Default)]
pub struct StageLayoutManager {
    zones: Vec<StageLayout>,
}

impl StageLayoutManager {
    /// Create a new, empty manager.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a zone layout.
    pub fn add_zone(&mut self, layout: StageLayout) {
        self.zones.push(layout);
    }

    /// Return an immutable slice of all zones.
    #[must_use]
    pub fn zones(&self) -> &[StageLayout] {
        &self.zones
    }

    /// Find a zone by type.
    #[must_use]
    pub fn find_zone(&self, zone: StageZone) -> Option<&StageLayout> {
        self.zones.iter().find(|z| z.zone == zone)
    }

    /// Find a mutable zone by type.
    pub fn find_zone_mut(&mut self, zone: StageZone) -> Option<&mut StageLayout> {
        self.zones.iter_mut().find(|z| z.zone == zone)
    }

    /// Bring all registered display surface zones online.
    pub fn bring_all_online(&mut self) {
        for z in &mut self.zones {
            if z.zone.is_display_surface() {
                z.bring_online();
            }
        }
    }

    /// Returns the count of zones currently online.
    #[must_use]
    pub fn online_count(&self) -> usize {
        self.zones.iter().filter(|z| z.is_active()).count()
    }

    /// Returns the total surface area of all online display zones in m².
    #[allow(clippy::cast_precision_loss)]
    #[must_use]
    pub fn total_online_area_m2(&self) -> f32 {
        self.zones
            .iter()
            .filter(|z| z.is_active() && z.zone.is_display_surface())
            .map(|z| z.dimensions.surface_area_m2())
            .sum()
    }

    /// Returns the count of faulted zones.
    #[must_use]
    pub fn faulted_count(&self) -> usize {
        self.zones
            .iter()
            .filter(|z| z.state == ZoneState::Faulted)
            .count()
    }

    /// Returns total number of registered zones.
    #[must_use]
    pub fn zone_count(&self) -> usize {
        self.zones.len()
    }
}

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

    fn make_back_wall() -> StageLayout {
        StageLayout::new(
            StageZone::BackWall,
            ZoneDimensions::new(12.0, 4.5, 0.1),
            Some(2.84),
        )
    }

    fn make_talent_area() -> StageLayout {
        StageLayout::new(
            StageZone::TalentArea,
            ZoneDimensions::new(8.0, 4.0, 6.0),
            None,
        )
    }

    #[test]
    fn test_stage_zone_is_display_surface() {
        assert!(StageZone::BackWall.is_display_surface());
        assert!(StageZone::Ceiling.is_display_surface());
        assert!(!StageZone::TalentArea.is_display_surface());
    }

    #[test]
    fn test_stage_zone_coverage_fractions_sum() {
        let zones = [
            StageZone::BackWall,
            StageZone::SideLeft,
            StageZone::SideRight,
            StageZone::Ceiling,
            StageZone::Floor,
            StageZone::TalentArea,
        ];
        let total: f32 = zones.iter().map(super::StageZone::coverage_fraction).sum();
        // Should sum to exactly 1.0
        assert!((total - 1.0).abs() < f32::EPSILON * 10.0);
    }

    #[test]
    fn test_stage_zone_display() {
        assert_eq!(StageZone::BackWall.to_string(), "BackWall");
        assert_eq!(StageZone::TalentArea.to_string(), "TalentArea");
        assert_eq!(StageZone::SideLeft.to_string(), "SideLeft");
    }

    #[test]
    fn test_zone_dimensions_surface_area() {
        let d = ZoneDimensions::new(10.0, 4.0, 0.5);
        assert!((d.surface_area_m2() - 40.0).abs() < 0.001);
    }

    #[test]
    fn test_zone_state_display() {
        assert_eq!(ZoneState::Online.to_string(), "Online");
        assert_eq!(ZoneState::Standby.to_string(), "Standby");
        assert_eq!(ZoneState::Faulted.to_string(), "Faulted");
    }

    #[test]
    fn test_stage_layout_bring_online() {
        let mut layout = make_back_wall();
        assert_eq!(layout.state, ZoneState::Standby);
        layout.bring_online();
        assert_eq!(layout.state, ZoneState::Online);
        assert!(layout.is_active());
    }

    #[test]
    fn test_stage_layout_fault_prevents_online() {
        let mut layout = make_back_wall();
        layout.fault();
        layout.bring_online(); // should be blocked
        assert_eq!(layout.state, ZoneState::Faulted);
    }

    #[test]
    fn test_stage_layout_put_standby() {
        let mut layout = make_back_wall();
        layout.bring_online();
        layout.put_standby();
        assert_eq!(layout.state, ZoneState::Standby);
    }

    #[test]
    fn test_stage_layout_pixel_dimensions() {
        let layout = make_back_wall();
        // 12 000 mm / 2.84 mm = ~4225
        let pw = layout.pixel_width().expect("should succeed in test");
        assert!(pw > 4000 && pw < 5000);
        let ph = layout.pixel_height().expect("should succeed in test");
        assert!(ph > 1000 && ph < 2000);
    }

    #[test]
    fn test_stage_layout_no_pixel_pitch() {
        let layout = make_talent_area();
        assert!(layout.pixel_width().is_none());
        assert!(layout.pixel_height().is_none());
    }

    #[test]
    fn test_manager_bring_all_online() {
        let mut mgr = StageLayoutManager::new();
        mgr.add_zone(make_back_wall());
        mgr.add_zone(make_talent_area());
        mgr.bring_all_online();
        // Only display surfaces go online
        assert_eq!(mgr.online_count(), 1);
    }

    #[test]
    fn test_manager_total_online_area() {
        let mut mgr = StageLayoutManager::new();
        mgr.add_zone(make_back_wall());
        mgr.bring_all_online();
        let area = mgr.total_online_area_m2();
        // 12 × 4.5 = 54 m²
        assert!((area - 54.0).abs() < 0.001);
    }

    #[test]
    fn test_manager_faulted_count() {
        let mut mgr = StageLayoutManager::new();
        let mut zone = make_back_wall();
        zone.fault();
        mgr.add_zone(zone);
        assert_eq!(mgr.faulted_count(), 1);
    }

    #[test]
    fn test_manager_find_zone_mut() {
        let mut mgr = StageLayoutManager::new();
        mgr.add_zone(make_back_wall());
        let z = mgr
            .find_zone_mut(StageZone::BackWall)
            .expect("should succeed in test");
        z.bring_online();
        assert!(mgr
            .find_zone(StageZone::BackWall)
            .expect("should succeed in test")
            .is_active());
    }

    #[test]
    fn test_manager_zone_count() {
        let mut mgr = StageLayoutManager::new();
        assert_eq!(mgr.zone_count(), 0);
        mgr.add_zone(make_back_wall());
        assert_eq!(mgr.zone_count(), 1);
    }
}