wifi-densepose-mat 0.3.2

Mass Casualty Assessment Tool - WiFi-based disaster survivor detection
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
//! Triangulation for 2D/3D position estimation from multiple sensors.

use crate::domain::{Coordinates3D, LocationUncertainty, SensorPosition};

/// Configuration for triangulation
#[derive(Debug, Clone)]
pub struct TriangulationConfig {
    /// Minimum number of sensors required
    pub min_sensors: usize,
    /// Maximum position uncertainty to accept (meters)
    pub max_uncertainty: f64,
    /// Path loss exponent for distance estimation
    pub path_loss_exponent: f64,
    /// Reference distance for path loss model (meters)
    pub reference_distance: f64,
    /// Reference RSSI at reference distance (dBm)
    pub reference_rssi: f64,
    /// Use weighted least squares
    pub weighted: bool,
}

impl Default for TriangulationConfig {
    fn default() -> Self {
        Self {
            min_sensors: 3,
            max_uncertainty: 5.0,
            path_loss_exponent: 3.0, // Indoor with obstacles
            reference_distance: 1.0,
            reference_rssi: -30.0,
            weighted: true,
        }
    }
}

/// Result of a distance estimation
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct DistanceEstimate {
    /// Sensor ID
    pub sensor_id: String,
    /// Estimated distance in meters
    pub distance: f64,
    /// Estimation confidence
    pub confidence: f64,
}

/// Triangulator for position estimation
pub struct Triangulator {
    config: TriangulationConfig,
}

impl Triangulator {
    /// Create a new triangulator
    pub fn new(config: TriangulationConfig) -> Self {
        Self { config }
    }

    /// Create with default configuration
    pub fn with_defaults() -> Self {
        Self::new(TriangulationConfig::default())
    }

    /// Access the triangulation configuration.
    pub fn config(&self) -> &TriangulationConfig {
        &self.config
    }

    /// Estimate position from RSSI measurements
    pub fn estimate_position(
        &self,
        sensors: &[SensorPosition],
        rssi_values: &[(String, f64)], // (sensor_id, rssi)
    ) -> Option<Coordinates3D> {
        // Get distance estimates from RSSI
        let distances: Vec<(SensorPosition, f64)> = rssi_values
            .iter()
            .filter_map(|(id, rssi)| {
                let sensor = sensors.iter().find(|s| &s.id == id)?;
                if !sensor.is_operational {
                    return None;
                }
                let distance = self.rssi_to_distance(*rssi);
                Some((sensor.clone(), distance))
            })
            .collect();

        if distances.len() < self.config.min_sensors {
            return None;
        }

        // Perform trilateration
        self.trilaterate(&distances)
    }

    /// Estimate position from Time of Arrival measurements
    pub fn estimate_from_toa(
        &self,
        sensors: &[SensorPosition],
        toa_values: &[(String, f64)], // (sensor_id, time_of_arrival_ns)
    ) -> Option<Coordinates3D> {
        const SPEED_OF_LIGHT: f64 = 299_792_458.0; // m/s

        let distances: Vec<(SensorPosition, f64)> = toa_values
            .iter()
            .filter_map(|(id, toa)| {
                let sensor = sensors.iter().find(|s| &s.id == id)?;
                if !sensor.is_operational {
                    return None;
                }
                // Convert nanoseconds to distance
                let distance = (*toa * 1e-9) * SPEED_OF_LIGHT / 2.0; // Round trip
                Some((sensor.clone(), distance))
            })
            .collect();

        if distances.len() < self.config.min_sensors {
            return None;
        }

        self.trilaterate(&distances)
    }

    /// Convert RSSI to distance using path loss model
    fn rssi_to_distance(&self, rssi: f64) -> f64 {
        // Log-distance path loss model:
        // RSSI = RSSI_0 - 10 * n * log10(d / d_0)
        // Solving for d:
        // d = d_0 * 10^((RSSI_0 - RSSI) / (10 * n))

        let exponent =
            (self.config.reference_rssi - rssi) / (10.0 * self.config.path_loss_exponent);

        self.config.reference_distance * 10.0_f64.powf(exponent)
    }

    /// Perform trilateration using least squares
    fn trilaterate(&self, distances: &[(SensorPosition, f64)]) -> Option<Coordinates3D> {
        if distances.len() < 3 {
            return None;
        }

        // Use linearized least squares approach
        // Reference: https://en.wikipedia.org/wiki/Trilateration

        // Use first sensor as reference
        let (ref_sensor, ref_dist) = &distances[0];
        let x1 = ref_sensor.x;
        let y1 = ref_sensor.y;
        let r1 = *ref_dist;

        // Build system of linear equations: A * [x, y]^T = b
        let n = distances.len() - 1;
        let mut a_matrix = vec![vec![0.0; 2]; n];
        let mut b_vector = vec![0.0; n];

        for (i, (sensor, dist)) in distances.iter().skip(1).enumerate() {
            let xi = sensor.x;
            let yi = sensor.y;
            let ri = *dist;

            // Linearized equation from difference of squared distances
            a_matrix[i][0] = 2.0 * (xi - x1);
            a_matrix[i][1] = 2.0 * (yi - y1);
            b_vector[i] = r1 * r1 - ri * ri - x1 * x1 + xi * xi - y1 * y1 + yi * yi;
        }

        // Solve using least squares: (A^T * A)^-1 * A^T * b
        let solution = self.solve_least_squares(&a_matrix, &b_vector)?;

        // Calculate uncertainty from residuals
        let uncertainty = self.calculate_uncertainty(&solution, distances);

        if uncertainty.horizontal_error > self.config.max_uncertainty {
            return None;
        }

        Some(Coordinates3D::new(
            solution[0],
            solution[1],
            0.0, // Z estimated separately
            uncertainty,
        ))
    }

    /// Solve linear system using least squares
    #[allow(clippy::needless_range_loop)]
    fn solve_least_squares(&self, a: &[Vec<f64>], b: &[f64]) -> Option<Vec<f64>> {
        let n = a.len();
        if n < 2 || a[0].len() != 2 {
            return None;
        }

        // Calculate A^T * A (dual-index matrix multiplication — range loop required)
        let mut ata = vec![vec![0.0; 2]; 2];
        for i in 0..2 {
            for j in 0..2 {
                for k in 0..n {
                    ata[i][j] += a[k][i] * a[k][j];
                }
            }
        }

        // Calculate A^T * b (dual-index — range loop required)
        let mut atb = [0.0; 2];
        for i in 0..2 {
            for k in 0..n {
                atb[i] += a[k][i] * b[k];
            }
        }

        // Solve 2x2 system using Cramer's rule
        let det = ata[0][0] * ata[1][1] - ata[0][1] * ata[1][0];
        if det.abs() < 1e-10 {
            return None;
        }

        let x = (atb[0] * ata[1][1] - atb[1] * ata[0][1]) / det;
        let y = (ata[0][0] * atb[1] - ata[1][0] * atb[0]) / det;

        Some(vec![x, y])
    }

    /// Calculate position uncertainty from residuals
    fn calculate_uncertainty(
        &self,
        position: &[f64],
        distances: &[(SensorPosition, f64)],
    ) -> LocationUncertainty {
        // Calculate root mean square error
        let mut sum_sq_error = 0.0;

        for (sensor, measured_dist) in distances {
            let dx = position[0] - sensor.x;
            let dy = position[1] - sensor.y;
            let estimated_dist = (dx * dx + dy * dy).sqrt();
            let error = measured_dist - estimated_dist;
            sum_sq_error += error * error;
        }

        let rmse = (sum_sq_error / distances.len() as f64).sqrt();

        // Real, dimensionless GDOP (Geometric Dilution of Precision). Falls back
        // to a unit factor for a degenerate (collinear) geometry where (HᵀH) is
        // singular — that geometry already produces a large residual RMSE.
        let gdop = self
            .compute_gdop(position, distances)
            .unwrap_or(1.0)
            .max(1.0);

        LocationUncertainty {
            horizontal_error: rmse * gdop,
            vertical_error: rmse * gdop * 1.5, // Vertical typically less accurate
            confidence: 0.95,
        }
    }

    /// Compute the real Geometric Dilution of Precision (GDOP).
    ///
    /// GDOP is the dimensionless factor by which measurement (range) noise is
    /// amplified into position error by the sensor geometry. For range-based 2D
    /// positioning the measurement Jacobian `H` has one row per sensor equal to
    /// the unit bearing vector from the target to that sensor,
    /// `[ (xₛ-xₜ)/d , (yₛ-yₜ)/d ]`. The position covariance (per unit measurement
    /// variance) is `(HᵀH)⁻¹`, and
    ///
    /// ```text
    /// GDOP = sqrt( trace( (HᵀH)⁻¹ ) )
    /// ```
    ///
    /// This is the same quantity ADR-156 §2.3 corrected elsewhere — a genuine
    /// dimensionless dilution, not the previous ad-hoc average-angle factor that
    /// was merely *labelled* GDOP. Returns `None` when `HᵀH` is singular
    /// (collinear / coincident geometry), which the caller treats as no
    /// dilution information (factor 1.0).
    fn compute_gdop(&self, position: &[f64], distances: &[(SensorPosition, f64)]) -> Option<f64> {
        let (tx, ty) = (position[0], position[1]);

        // Accumulate HᵀH (2×2, symmetric) from unit bearing vectors.
        let (mut hxx, mut hxy, mut hyy) = (0.0_f64, 0.0_f64, 0.0_f64);
        let mut rows = 0usize;
        for (sensor, _dist) in distances {
            let dx = sensor.x - tx;
            let dy = sensor.y - ty;
            let d = (dx * dx + dy * dy).sqrt();
            if d <= f64::EPSILON {
                continue; // target coincident with sensor: undefined bearing
            }
            let ux = dx / d;
            let uy = dy / d;
            hxx += ux * ux;
            hxy += ux * uy;
            hyy += uy * uy;
            rows += 1;
        }

        if rows < 2 {
            return None;
        }

        // Invert the 2×2 HᵀH. trace((HᵀH)⁻¹) = (hxx + hyy) / det.
        let det = hxx * hyy - hxy * hxy;
        if det.abs() < 1e-12 {
            return None; // singular: collinear geometry
        }
        let trace_inv = (hxx + hyy) / det;
        if trace_inv <= 0.0 {
            return None;
        }
        Some(trace_inv.sqrt())
    }
}

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

    fn create_test_sensors() -> Vec<SensorPosition> {
        vec![
            SensorPosition {
                id: "s1".to_string(),
                x: 0.0,
                y: 0.0,
                z: 1.5,
                sensor_type: SensorType::Transceiver,
                is_operational: true,
                last_rssi: None,
            },
            SensorPosition {
                id: "s2".to_string(),
                x: 10.0,
                y: 0.0,
                z: 1.5,
                sensor_type: SensorType::Transceiver,
                is_operational: true,
                last_rssi: None,
            },
            SensorPosition {
                id: "s3".to_string(),
                x: 5.0,
                y: 10.0,
                z: 1.5,
                sensor_type: SensorType::Transceiver,
                is_operational: true,
                last_rssi: None,
            },
        ]
    }

    #[test]
    fn test_rssi_to_distance() {
        let triangulator = Triangulator::with_defaults();

        // At reference distance, RSSI should equal reference RSSI
        let distance = triangulator.rssi_to_distance(-30.0);
        assert!((distance - 1.0).abs() < 0.1);

        // Weaker signal = further distance
        let distance2 = triangulator.rssi_to_distance(-60.0);
        assert!(distance2 > distance);
    }

    #[test]
    fn test_trilateration() {
        let triangulator = Triangulator::with_defaults();
        let sensors = create_test_sensors();

        // Target at (5, 4) - calculate distances
        let target: (f64, f64) = (5.0, 4.0);
        let distances: Vec<(&str, f64)> = vec![
            (
                "s1",
                ((target.0 - 0.0_f64).powi(2) + (target.1 - 0.0_f64).powi(2)).sqrt(),
            ),
            (
                "s2",
                ((target.0 - 10.0_f64).powi(2) + (target.1 - 0.0_f64).powi(2)).sqrt(),
            ),
            (
                "s3",
                ((target.0 - 5.0_f64).powi(2) + (target.1 - 10.0_f64).powi(2)).sqrt(),
            ),
        ];

        let dist_vec: Vec<(SensorPosition, f64)> = distances
            .iter()
            .filter_map(|(id, d)| {
                let sensor = sensors.iter().find(|s| s.id == *id)?;
                Some((sensor.clone(), *d))
            })
            .collect();

        let result = triangulator.trilaterate(&dist_vec);
        assert!(result.is_some());

        let pos = result.unwrap();
        assert!((pos.x - target.0).abs() < 0.5);
        assert!((pos.y - target.1).abs() < 0.5);
    }

    #[test]
    fn test_insufficient_sensors() {
        let triangulator = Triangulator::with_defaults();
        let sensors = create_test_sensors();

        // Only 2 distance measurements
        let rssi_values = vec![("s1".to_string(), -40.0), ("s2".to_string(), -45.0)];

        let result = triangulator.estimate_position(&sensors, &rssi_values);
        assert!(result.is_none());
    }

    fn sensor_at(id: &str, x: f64, y: f64) -> SensorPosition {
        SensorPosition {
            id: id.to_string(),
            x,
            y,
            z: 1.5,
            sensor_type: SensorType::Transceiver,
            is_operational: true,
            last_rssi: None,
        }
    }

    /// Real GDOP: dimensionless, geometry-dependent, and matches the closed-form
    /// sqrt(trace((HᵀH)⁻¹)). A well-spread (near-orthogonal) array must give a
    /// LOWER GDOP than a near-collinear one. (The old ad-hoc angle factor was not
    /// a true dilution and is replaced.)
    #[test]
    fn test_gdop_is_real_dilution() {
        let t = Triangulator::with_defaults();
        let target = [5.0_f64, 5.0_f64];

        // Well-distributed: an equilateral-ish triangle around the target.
        let good = vec![
            (sensor_at("a", 5.0, 15.0), 10.0),
            (sensor_at("b", -3.66, 0.0), 10.0),
            (sensor_at("c", 13.66, 0.0), 10.0),
        ];
        let gdop_good = t.compute_gdop(&target, &good).expect("good geometry");

        // Near-collinear: bearings nearly all along ±y with a tiny x-spread, so
        // HᵀH is ill-conditioned (invertible but with a small eigenvalue) and the
        // GDOP is large but finite.
        let bad = vec![
            (sensor_at("a", 5.3, 15.0), 10.0),
            (sensor_at("b", 4.7, 15.0), 10.0),
            (sensor_at("c", 5.1, -5.0), 10.0),
        ];
        let gdop_bad = t.compute_gdop(&target, &bad).expect("bad geometry");

        assert!(gdop_good >= 1.0, "GDOP must be >= 1 (dilution, dimensionless)");
        assert!(
            gdop_good < gdop_bad,
            "well-spread GDOP {gdop_good} must be < near-collinear GDOP {gdop_bad}"
        );

        // Closed-form cross-check for the well-spread case: each unit bearing
        // vector contributes to HᵀH; verify trace((HᵀH)⁻¹) explicitly.
        let (mut hxx, mut hxy, mut hyy) = (0.0, 0.0, 0.0);
        for (s, _d) in &good {
            let dx = s.x - target[0];
            let dy = s.y - target[1];
            let d = (dx * dx + dy * dy).sqrt();
            let (ux, uy) = (dx / d, dy / d);
            hxx += ux * ux;
            hxy += ux * uy;
            hyy += uy * uy;
        }
        let det = hxx * hyy - hxy * hxy;
        let expected = ((hxx + hyy) / det).sqrt();
        assert!((gdop_good - expected).abs() < 1e-9);
    }

    /// Collinear geometry makes HᵀH singular -> compute_gdop returns None,
    /// and the uncertainty path falls back to a unit factor (no fabrication).
    #[test]
    fn test_gdop_singular_collinear_is_none() {
        let t = Triangulator::with_defaults();
        let target = [0.0_f64, 0.0_f64];
        // All sensors on the +x axis through the target: bearings all ±x -> rank 1.
        let collinear = vec![
            (sensor_at("a", 1.0, 0.0), 1.0),
            (sensor_at("b", 2.0, 0.0), 2.0),
            (sensor_at("c", 3.0, 0.0), 3.0),
        ];
        assert!(t.compute_gdop(&target, &collinear).is_none());
    }
}

// ---------------------------------------------------------------------------
// Integration 5: Multi-AP TDoA triangulation via NeumannSolver
// ---------------------------------------------------------------------------

#[cfg(feature = "ruvector")]
use ruvector_solver::neumann::NeumannSolver;
#[cfg(feature = "ruvector")]
use ruvector_solver::types::CsrMatrix;

/// Solve multi-AP TDoA survivor localization using NeumannSolver.
///
/// For N access points with TDoA measurements, linearizes the hyperbolic
/// equations and solves the 2×2 normal equations system. Complexity is O(1)
/// in AP count (always solves a 2×2 system regardless of N).
///
/// # Arguments
/// * `tdoa_measurements` - Vec of (ap_i_idx, ap_j_idx, tdoa_seconds)
///   where tdoa = t_i - t_j (positive if closer to AP_i)
/// * `ap_positions` - Vec of (x_metres, y_metres) for each AP
///
/// # Returns
/// Some((x, y)) estimated survivor position in metres, or None if underdetermined
#[cfg(feature = "ruvector")]
pub fn solve_tdoa_triangulation(
    tdoa_measurements: &[(usize, usize, f32)],
    ap_positions: &[(f32, f32)],
) -> Option<(f32, f32)> {
    let n_meas = tdoa_measurements.len();
    if n_meas < 3 || ap_positions.len() < 2 {
        return None;
    }

    const C: f32 = 3e8_f32; // speed of light m/s
    let (x_ref, y_ref) = ap_positions[0];

    // Accumulate (A^T A) and (A^T b) for 2×2 normal equations
    let mut ata = [[0.0_f32; 2]; 2];
    let mut atb = [0.0_f32; 2];

    for &(i, j, tdoa) in tdoa_measurements {
        let (xi, yi) = ap_positions.get(i).copied().unwrap_or((x_ref, y_ref));
        let (xj, yj) = ap_positions.get(j).copied().unwrap_or((x_ref, y_ref));

        // Row of A: [xi - xj, yi - yj] (linearized TDoA)
        let ai0 = xi - xj;
        let ai1 = yi - yj;

        // RHS: C * tdoa / 2 + (xi^2 - xj^2 + yi^2 - yj^2) / 2 - x_ref*(xi-xj) - y_ref*(yi-yj)
        let bi = C * tdoa / 2.0 + ((xi * xi - xj * xj) + (yi * yi - yj * yj)) / 2.0
            - x_ref * ai0
            - y_ref * ai1;

        ata[0][0] += ai0 * ai0;
        ata[0][1] += ai0 * ai1;
        ata[1][0] += ai1 * ai0;
        ata[1][1] += ai1 * ai1;
        atb[0] += ai0 * bi;
        atb[1] += ai1 * bi;
    }

    // Tikhonov regularization
    let lambda = 0.01_f32;
    ata[0][0] += lambda;
    ata[1][1] += lambda;

    let csr = CsrMatrix::<f32>::from_coo(
        2,
        2,
        vec![
            (0, 0, ata[0][0]),
            (0, 1, ata[0][1]),
            (1, 0, ata[1][0]),
            (1, 1, ata[1][1]),
        ],
    );

    // Attempt the Neumann-series solver first; fall back to Cramer's rule for
    // the 2×2 case when the iterative solver cannot converge (e.g. the
    // diagonal is very large relative to f32 precision).
    if let Ok(r) = NeumannSolver::new(1e-5, 500).solve(&csr, &atb) {
        return Some((r.solution[0] + x_ref, r.solution[1] + y_ref));
    }

    // Cramer's rule fallback for the 2×2 normal equations.
    let det = ata[0][0] * ata[1][1] - ata[0][1] * ata[1][0];
    if det.abs() < 1e-10 {
        return None;
    }
    let x_sol = (atb[0] * ata[1][1] - atb[1] * ata[0][1]) / det;
    let y_sol = (ata[0][0] * atb[1] - ata[1][0] * atb[0]) / det;
    Some((x_sol + x_ref, y_sol + y_ref))
}

#[cfg(all(test, feature = "ruvector"))]
mod triangulation_tests {
    use super::*;

    #[test]
    fn tdoa_triangulation_insufficient_data() {
        let result = solve_tdoa_triangulation(&[(0, 1, 1e-9)], &[(0.0, 0.0), (5.0, 0.0)]);
        assert!(result.is_none());
    }

    #[test]
    fn tdoa_triangulation_symmetric_case() {
        // Target at centre (2.5, 2.5), APs at corners of 5m×5m square
        let aps = vec![(0.0_f32, 0.0), (5.0, 0.0), (5.0, 5.0), (0.0, 5.0)];
        // Target equidistant from all APs → TDoA ≈ 0 for all pairs
        let measurements = vec![
            (0_usize, 1_usize, 0.0_f32),
            (1, 2, 0.0),
            (2, 3, 0.0),
            (0, 3, 0.0),
        ];
        let result = solve_tdoa_triangulation(&measurements, &aps);
        assert!(result.is_some(), "should solve symmetric case");
        let (x, y) = result.unwrap();
        assert!(x.is_finite() && y.is_finite());
    }
}