mmwcore 0.3.0

Rust kernels for decoding and processing mmWave radar data
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
//! Native sequence-level tracking metrics.

use std::collections::{BTreeMap, HashSet};
use std::fmt;

use super::TrackingBox2D;

/// Packed observations from ordered tracker frames.
#[derive(Clone, Copy, Debug)]
pub struct TrackingMetricsInput<'a> {
    /// Cumulative observation counts, with one entry per frame plus the final count.
    pub frame_offsets: &'a [usize],
    pub track_ids: &'a [i64],
    /// Packed XYZ positions with shape `(observations, 3)`.
    pub positions: &'a [f32],
    /// Packed XYZ velocities with shape `(observations, 3)`.
    pub velocities: &'a [f32],
    /// Native lifecycle status codes aligned with observations.
    pub status_codes: &'a [u8],
    /// `None` means scenery metrics are disabled; `Some([])` means every point is in scenery.
    pub scenery_boxes: Option<&'a [TrackingBox2D]>,
    pub frame_index_offset: usize,
}

/// One track's observed motion and lifecycle summary.
#[derive(Clone, Debug, PartialEq)]
pub struct TrackObservationMetrics {
    pub track_id: i64,
    pub observed_frames: usize,
    pub confirmed_frames: usize,
    pub first_frame_index: usize,
    pub last_frame_index: usize,
    pub first_position_m: [f32; 3],
    pub last_position_m: [f32; 3],
    pub median_position_m: [f32; 3],
    pub displacement_m: f32,
    pub path_length_m: f32,
    pub median_speed_mps: f32,
    pub max_speed_mps: f32,
    pub confirmed_intervals: Vec<[usize; 2]>,
    pub in_scenery_frames: Option<usize>,
    pub outside_scenery_frames: Option<usize>,
}

/// Aggregated tracker coverage over an ordered frame sequence.
#[derive(Clone, Debug, PartialEq)]
pub struct TrackingSequenceMetrics {
    pub num_frames: usize,
    pub frames_with_tracks: usize,
    pub frames_with_confirmed_tracks: usize,
    pub max_concurrent_tracks: usize,
    pub tracks: Vec<TrackObservationMetrics>,
}

/// Summarize ordered tracker reports in a single native call.
pub fn summarize_tracking_metrics(
    input: TrackingMetricsInput<'_>,
) -> Result<TrackingSequenceMetrics, TrackingMetricsError> {
    let num_frames = validate_input(input)?;
    let mut observed = BTreeMap::<i64, Vec<Observation>>::new();
    let mut frames_with_tracks = 0_usize;
    let mut frames_with_confirmed_tracks = 0_usize;
    let mut max_concurrent_tracks = 0_usize;

    for relative_frame_index in 0..num_frames {
        let start = input.frame_offsets[relative_frame_index];
        let stop = input.frame_offsets[relative_frame_index + 1];
        let track_count = stop - start;
        frames_with_tracks += usize::from(track_count > 0);
        max_concurrent_tracks = max_concurrent_tracks.max(track_count);
        let frame_index = input
            .frame_index_offset
            .checked_add(relative_frame_index)
            .ok_or(TrackingMetricsError::FrameIndexOverflow)?;
        let mut seen_track_ids = HashSet::with_capacity(track_count);
        let mut confirmed_in_frame = false;
        for observation_index in start..stop {
            let track_id = input.track_ids[observation_index];
            if !seen_track_ids.insert(track_id) {
                return Err(TrackingMetricsError::DuplicateTrackId {
                    frame_index,
                    track_id,
                });
            }
            let status_code = input.status_codes[observation_index];
            let confirmed = status_code == 1;
            confirmed_in_frame |= confirmed;
            observed.entry(track_id).or_default().push(Observation {
                frame_index,
                position: vector3(input.positions, observation_index),
                velocity: vector3(input.velocities, observation_index),
                confirmed,
            });
        }
        frames_with_confirmed_tracks += usize::from(confirmed_in_frame);
    }

    let tracks = observed
        .into_iter()
        .map(|(track_id, observations)| {
            summarize_track(track_id, &observations, input.scenery_boxes)
        })
        .collect();
    Ok(TrackingSequenceMetrics {
        num_frames,
        frames_with_tracks,
        frames_with_confirmed_tracks,
        max_concurrent_tracks,
        tracks,
    })
}

#[derive(Clone, Copy, Debug)]
struct Observation {
    frame_index: usize,
    position: [f32; 3],
    velocity: [f32; 3],
    confirmed: bool,
}

fn validate_input(input: TrackingMetricsInput<'_>) -> Result<usize, TrackingMetricsError> {
    let Some((&first_offset, remaining_offsets)) = input.frame_offsets.split_first() else {
        return Err(TrackingMetricsError::EmptyFrameOffsets);
    };
    if first_offset != 0 {
        return Err(TrackingMetricsError::InvalidFirstFrameOffset {
            actual: first_offset,
        });
    }
    let observation_count = input.track_ids.len();
    for (index, &offset) in remaining_offsets.iter().enumerate() {
        let previous = input.frame_offsets[index];
        if offset < previous {
            return Err(TrackingMetricsError::NonMonotonicFrameOffset {
                index: index + 1,
                previous,
                actual: offset,
            });
        }
        if offset > observation_count {
            return Err(TrackingMetricsError::FrameOffsetOutOfBounds {
                index: index + 1,
                value: offset,
                observation_count,
            });
        }
    }
    let final_offset = *input.frame_offsets.last().expect("non-empty is checked");
    if final_offset != observation_count {
        return Err(TrackingMetricsError::FinalFrameOffset {
            expected: observation_count,
            actual: final_offset,
        });
    }
    validate_vector_length("positions", input.positions.len(), observation_count)?;
    validate_vector_length("velocities", input.velocities.len(), observation_count)?;
    if input.status_codes.len() != observation_count {
        return Err(TrackingMetricsError::InputLength {
            name: "status codes",
            expected: observation_count,
            actual: input.status_codes.len(),
        });
    }
    for (index, &track_id) in input.track_ids.iter().enumerate() {
        if track_id < 0 {
            return Err(TrackingMetricsError::NegativeTrackId { index, track_id });
        }
    }
    for (index, &status_code) in input.status_codes.iter().enumerate() {
        if status_code > 2 {
            return Err(TrackingMetricsError::InvalidStatusCode { index, status_code });
        }
    }
    for (name, values) in [
        ("positions", input.positions),
        ("velocities", input.velocities),
    ] {
        if let Some((index, _)) = values
            .iter()
            .enumerate()
            .find(|(_, value)| !value.is_finite())
        {
            return Err(TrackingMetricsError::NonFiniteInput { name, index });
        }
    }
    Ok(input.frame_offsets.len() - 1)
}

fn validate_vector_length(
    name: &'static str,
    actual: usize,
    observation_count: usize,
) -> Result<(), TrackingMetricsError> {
    let expected = observation_count
        .checked_mul(3)
        .ok_or(TrackingMetricsError::InputLengthOverflow { name })?;
    if actual != expected {
        return Err(TrackingMetricsError::InputLength {
            name,
            expected,
            actual,
        });
    }
    Ok(())
}

fn vector3(values: &[f32], index: usize) -> [f32; 3] {
    let offset = index * 3;
    [values[offset], values[offset + 1], values[offset + 2]]
}

fn summarize_track(
    track_id: i64,
    observations: &[Observation],
    scenery_boxes: Option<&[TrackingBox2D]>,
) -> TrackObservationMetrics {
    let first = observations
        .first()
        .expect("tracked observations are non-empty");
    let last = observations
        .last()
        .expect("tracked observations are non-empty");
    let confirmed_frames = observations
        .iter()
        .filter_map(|observation| observation.confirmed.then_some(observation.frame_index))
        .collect::<Vec<_>>();
    let median_position_m = [
        median(
            observations
                .iter()
                .map(|observation| observation.position[0])
                .collect(),
        ),
        median(
            observations
                .iter()
                .map(|observation| observation.position[1])
                .collect(),
        ),
        median(
            observations
                .iter()
                .map(|observation| observation.position[2])
                .collect(),
        ),
    ];
    let speeds = observations
        .iter()
        .map(|observation| observation.velocity[0].hypot(observation.velocity[1]))
        .collect::<Vec<_>>();
    let path_length_m = observations
        .windows(2)
        .map(|window| {
            (window[1].position[0] - window[0].position[0])
                .hypot(window[1].position[1] - window[0].position[1])
        })
        .sum();
    let in_scenery_frames = scenery_boxes.map(|boxes| {
        observations
            .iter()
            .filter(|observation| scenery_contains(boxes, observation.position))
            .count()
    });
    TrackObservationMetrics {
        track_id,
        observed_frames: observations.len(),
        confirmed_frames: confirmed_frames.len(),
        first_frame_index: first.frame_index,
        last_frame_index: last.frame_index,
        first_position_m: first.position,
        last_position_m: last.position,
        median_position_m,
        displacement_m: (last.position[0] - first.position[0])
            .hypot(last.position[1] - first.position[1]),
        path_length_m,
        median_speed_mps: median(speeds.clone()),
        max_speed_mps: speeds.into_iter().fold(f32::NEG_INFINITY, f32::max),
        confirmed_intervals: contiguous_intervals(&confirmed_frames),
        in_scenery_frames,
        outside_scenery_frames: in_scenery_frames.map(|count| observations.len() - count),
    }
}

fn median(mut values: Vec<f32>) -> f32 {
    values.sort_by(f32::total_cmp);
    let middle = values.len() / 2;
    if values.len() % 2 == 1 {
        values[middle]
    } else {
        (values[middle - 1] + values[middle]) * 0.5
    }
}

fn scenery_contains(boxes: &[TrackingBox2D], position: [f32; 3]) -> bool {
    boxes.is_empty()
        || boxes
            .iter()
            .copied()
            .any(|boundary| boundary.contains(f64::from(position[0]), f64::from(position[1])))
}

fn contiguous_intervals(indices: &[usize]) -> Vec<[usize; 2]> {
    let Some((&first, remaining)) = indices.split_first() else {
        return Vec::new();
    };
    let mut intervals = Vec::new();
    let mut start = first;
    let mut previous = first;
    for &index in remaining {
        if index != previous + 1 {
            intervals.push([start, previous]);
            start = index;
        }
        previous = index;
    }
    intervals.push([start, previous]);
    intervals
}

/// Native input and numerical validation errors for tracking metrics.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TrackingMetricsError {
    EmptyFrameOffsets,
    InvalidFirstFrameOffset {
        actual: usize,
    },
    NonMonotonicFrameOffset {
        index: usize,
        previous: usize,
        actual: usize,
    },
    FrameOffsetOutOfBounds {
        index: usize,
        value: usize,
        observation_count: usize,
    },
    FinalFrameOffset {
        expected: usize,
        actual: usize,
    },
    InputLengthOverflow {
        name: &'static str,
    },
    InputLength {
        name: &'static str,
        expected: usize,
        actual: usize,
    },
    NegativeTrackId {
        index: usize,
        track_id: i64,
    },
    InvalidStatusCode {
        index: usize,
        status_code: u8,
    },
    NonFiniteInput {
        name: &'static str,
        index: usize,
    },
    DuplicateTrackId {
        frame_index: usize,
        track_id: i64,
    },
    FrameIndexOverflow,
}

impl fmt::Display for TrackingMetricsError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyFrameOffsets => write!(formatter, "Tracking metrics require frame offsets."),
            Self::InvalidFirstFrameOffset { actual } => {
                write!(
                    formatter,
                    "Tracking metrics first frame offset must be zero; got {actual}."
                )
            }
            Self::NonMonotonicFrameOffset {
                index,
                previous,
                actual,
            } => write!(
                formatter,
                "Tracking metrics frame offset {index} must be at least {previous}; got {actual}."
            ),
            Self::FrameOffsetOutOfBounds {
                index,
                value,
                observation_count,
            } => write!(
                formatter,
                "Tracking metrics frame offset {index} is {value}; only {observation_count} observations exist."
            ),
            Self::FinalFrameOffset { expected, actual } => write!(
                formatter,
                "Tracking metrics final frame offset must be {expected}; got {actual}."
            ),
            Self::InputLengthOverflow { name } => {
                write!(formatter, "Tracking metrics {name} length overflows.")
            }
            Self::InputLength {
                name,
                expected,
                actual,
            } => write!(
                formatter,
                "Tracking metrics {name} has {actual} values; expected {expected}."
            ),
            Self::NegativeTrackId { index, track_id } => write!(
                formatter,
                "Tracking metrics track ID at observation {index} must be non-negative; got {track_id}."
            ),
            Self::InvalidStatusCode { index, status_code } => write!(
                formatter,
                "Tracking metrics status at observation {index} is invalid: {status_code}."
            ),
            Self::NonFiniteInput { name, index } => write!(
                formatter,
                "Tracking metrics {name} value {index} contains NaN or Inf."
            ),
            Self::DuplicateTrackId {
                frame_index,
                track_id,
            } => write!(
                formatter,
                "Tracking metrics frame {frame_index} repeats track ID {track_id}."
            ),
            Self::FrameIndexOverflow => {
                write!(formatter, "Tracking metrics frame index overflows.")
            }
        }
    }
}

impl std::error::Error for TrackingMetricsError {}

#[cfg(test)]
mod tests {
    use super::{TrackingMetricsInput, summarize_tracking_metrics};

    #[test]
    fn summarizes_identity_motion_and_confirmed_intervals() {
        let summary = summarize_tracking_metrics(TrackingMetricsInput {
            frame_offsets: &[0, 1, 3, 4],
            track_ids: &[2, 2, 7, 2],
            positions: &[0.0, 1.0, 0.0, 0.3, 1.4, 0.0, 1.0, 2.0, 0.0, 1.2, 1.8, 0.0],
            velocities: &[0.0, 0.0, 0.0, 0.3, 0.4, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0],
            status_codes: &[0, 1, 0, 1],
            scenery_boxes: None,
            frame_index_offset: 10,
        })
        .unwrap();

        let track = &summary.tracks[0];
        assert_eq!(summary.frames_with_tracks, 3);
        assert_eq!(summary.frames_with_confirmed_tracks, 2);
        assert_eq!(track.first_frame_index, 10);
        assert_eq!(track.last_frame_index, 12);
        assert_eq!(track.confirmed_intervals, vec![[11, 12]]);
        assert!((track.path_length_m - (0.5 + 0.9_f32.hypot(0.4))).abs() < 1.0e-6);
    }
}