use crate::physics::basic::{PhysicalQuantity, Vector3, VectorQuantity};
use crate::spatial_geometry::cos_matrix::CosMatrix;
use crate::spatial_geometry::euler_angles::EulerAngles;
use crate::spatial_geometry::quaternion::Quaternion;
pub const MAX_TMR_WORDS: usize = 9;
#[derive(Clone, Copy, Debug)]
pub struct TmrBits {
words: [u64; MAX_TMR_WORDS],
len: u8,
}
impl TmrBits {
pub const fn new() -> Self {
Self {
words: [0; MAX_TMR_WORDS],
len: 0,
}
}
pub fn push(&mut self, word: u64) {
assert!(
(self.len as usize) < MAX_TMR_WORDS,
"TmrBits 容量 {} 不足,参与表决的类型过大",
MAX_TMR_WORDS
);
self.words[self.len as usize] = word;
self.len += 1;
}
pub fn push_f64(&mut self, value: f64) {
self.push(value.to_bits());
}
pub fn len(&self) -> usize {
self.len as usize
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn as_slice(&self) -> &[u64] {
&self.words[..self.len as usize]
}
}
impl Default for TmrBits {
fn default() -> Self {
Self::new()
}
}
impl PartialEq for TmrBits {
fn eq(&self, other: &Self) -> bool {
self.as_slice() == other.as_slice()
}
}
impl Eq for TmrBits {}
impl std::hash::Hash for TmrBits {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_slice().hash(state);
}
}
pub trait TmrValue {
fn tmr_bits(&self) -> TmrBits;
}
impl<T: PhysicalQuantity> TmrValue for T {
fn tmr_bits(&self) -> TmrBits {
let mut bits = TmrBits::new();
bits.push_f64(self.raw_value());
bits
}
}
impl<T: VectorQuantity + Default> TmrValue for Vector3<T> {
fn tmr_bits(&self) -> TmrBits {
let mut bits = TmrBits::new();
bits.push_f64(self.x.raw_value());
bits.push_f64(self.y.raw_value());
bits.push_f64(self.z.raw_value());
bits
}
}
impl TmrValue for Quaternion {
fn tmr_bits(&self) -> TmrBits {
let mut bits = TmrBits::new();
bits.push_f64(self.q0);
bits.push_f64(self.q1);
bits.push_f64(self.q2);
bits.push_f64(self.q3);
bits
}
}
impl TmrValue for CosMatrix {
fn tmr_bits(&self) -> TmrBits {
let mut bits = TmrBits::new();
let rows = self.to_array();
for row in rows.iter() {
for v in row.iter() {
bits.push_f64(*v);
}
}
bits
}
}
impl TmrValue for EulerAngles {
fn tmr_bits(&self) -> TmrBits {
let mut bits = TmrBits::new();
bits.push_f64(self.yaw.raw_value());
bits.push_f64(self.pitch.raw_value());
bits.push_f64(self.roll.raw_value());
bits
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::physics::basic::{Angular, Distance, Mass, Velocity};
fn vote3(a: TmrBits, b: TmrBits, c: TmrBits) -> Option<TmrBits> {
if a == b {
Some(a)
} else if a == c {
Some(a)
} else if b == c {
Some(b)
} else {
None
}
}
#[test]
fn test_tmr_bits_new_is_empty() {
let bits = TmrBits::new();
assert!(bits.is_empty());
assert_eq!(bits.len(), 0);
assert_eq!(bits.as_slice(), &[] as &[u64]);
}
#[test]
fn test_tmr_bits_push_and_slice() {
let mut bits = TmrBits::new();
bits.push(0xDEAD_BEEF);
bits.push_f64(1.0);
assert_eq!(bits.len(), 2);
assert_eq!(bits.as_slice()[0], 0xDEAD_BEEF);
assert_eq!(bits.as_slice()[1], 1.0f64.to_bits());
}
#[test]
#[should_panic(expected = "容量")]
fn test_tmr_bits_overflow_panics() {
let mut bits = TmrBits::new();
for i in 0..=MAX_TMR_WORDS {
bits.push(i as u64);
}
}
#[test]
fn test_tmr_bits_equality_uses_only_valid_words() {
let mut a = TmrBits::new();
a.push(1);
a.push(2);
let mut b = TmrBits::new();
b.push(1);
b.push(2);
let mut c = TmrBits::new();
c.push(1);
assert_eq!(a, b);
assert_ne!(a, c);
}
#[test]
fn test_scalar_bits_equal_for_same_value() {
let a = Distance::from_m(5.0);
let b = Distance::from_m(5.0);
assert_eq!(a.tmr_bits(), b.tmr_bits());
}
#[test]
fn test_scalar_bits_differ_for_different_value() {
let a = Distance::from_m(5.0);
let b = Distance::from_m(5.000_000_000_000_001);
assert_ne!(a.tmr_bits(), b.tmr_bits());
}
#[test]
fn test_scalar_bits_ignore_padding_across_construction_paths() {
let a = Distance::from_m(5.0);
let b = Distance::from_m(2.5 + 2.5);
let c = Distance::default() + Distance::from_m(5.0);
assert_eq!(a, b);
assert_eq!(a, c);
assert_eq!(a.tmr_bits(), b.tmr_bits(), "同语义值位型必须相同");
assert_eq!(a.tmr_bits(), c.tmr_bits());
}
#[test]
fn test_units_are_not_normalized() {
let in_grams = Mass::from_g(500.0);
let in_kg = Mass::from_kg(0.5);
assert_eq!(
in_grams.default_unit_value(),
in_kg.default_unit_value(),
"default_unit_value 做归一化,两者相同"
);
assert_ne!(
in_grams.raw_value(),
in_kg.raw_value(),
"raw_value 保留原始单位"
);
assert_ne!(
in_grams.tmr_bits(),
in_kg.tmr_bits(),
"不同单位表示同一物理量,表决必须判为分歧"
);
}
#[test]
fn test_nan_bits_equal_though_value_not_equal() {
let a = Distance::from_m(f64::NAN);
let b = Distance::from_m(f64::NAN);
assert_ne!(a, b, "NaN != NaN,数值比较判不等");
assert_eq!(a.tmr_bits(), b.tmr_bits(), "同位型 NaN 的表决键必须相同");
}
#[test]
fn test_nan_with_different_payload_differs() {
let other_nan = f64::from_bits(0x7ff8_0000_0000_0001);
let a = Distance::from_m(f64::NAN);
let b = Distance::from_m(other_nan);
assert_ne!(
a.tmr_bits(),
b.tmr_bits(),
"不同 payload 的 NaN 位型不同,应判为分歧"
);
}
#[test]
fn test_signed_zero_bits_differ_though_value_equal() {
let pos = Distance::from_m(0.0);
let neg = Distance::from_m(-0.0);
assert_eq!(pos, neg, "0.0 == -0.0,数值比较判等");
assert_ne!(
pos.tmr_bits(),
neg.tmr_bits(),
"+0.0 与 -0.0 位型不同,表决判为分歧(这是有意为之)"
);
assert_eq!(pos.raw_value().to_bits(), 0x0000_0000_0000_0000);
assert_eq!(neg.raw_value().to_bits(), 0x8000_0000_0000_0000);
}
#[test]
fn test_vector3_bits() {
let a = Vector3::new(
Distance::from_m(1.0),
Distance::from_m(2.0),
Distance::from_m(3.0),
);
let b = a;
assert_eq!(a.tmr_bits(), b.tmr_bits());
assert_eq!(a.tmr_bits().len(), 3);
let c = Vector3::new(
Distance::from_m(1.0),
Distance::from_m(2.0),
Distance::from_m(4.0),
);
assert_ne!(a.tmr_bits(), c.tmr_bits());
}
#[test]
fn test_vector3_bits_are_order_sensitive() {
let a = Vector3::new(
Distance::from_m(1.0),
Distance::from_m(2.0),
Distance::from_m(3.0),
);
let b = Vector3::new(
Distance::from_m(1.0),
Distance::from_m(3.0),
Distance::from_m(2.0),
);
assert_ne!(a.tmr_bits(), b.tmr_bits());
}
#[test]
fn test_quaternion_bits() {
let a = Quaternion::new(1.0, 0.0, 0.0, 0.0);
let b = Quaternion::new(1.0, 0.0, 0.0, 0.0);
assert_eq!(a.tmr_bits(), b.tmr_bits());
assert_eq!(a.tmr_bits().len(), 4);
let c = Quaternion::new(1.0, 0.0, 0.0, 1e-16);
assert_ne!(a.tmr_bits(), c.tmr_bits());
}
#[test]
fn test_quaternion_q_and_negative_q_differ() {
let a = Quaternion::new(1.0, 0.0, 0.0, 0.0);
let neg = Quaternion::new(-1.0, 0.0, 0.0, 0.0);
assert_ne!(a.tmr_bits(), neg.tmr_bits());
}
#[test]
fn test_cos_matrix_bits() {
let a = CosMatrix::default();
let b = CosMatrix::default();
assert_eq!(a.tmr_bits(), b.tmr_bits());
assert_eq!(a.tmr_bits().len(), 9);
assert_eq!(a.tmr_bits().len(), MAX_TMR_WORDS, "恰好用满容量");
}
#[test]
fn test_cos_matrix_bits_differ_on_single_element() {
let a = CosMatrix::default();
let b = CosMatrix::new([
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0 + f64::EPSILON],
]);
assert_ne!(
a.to_array(),
b.to_array(),
"构造出的矩阵必须真的不同,否则本测试无意义"
);
assert_ne!(a.tmr_bits(), b.tmr_bits());
}
#[test]
fn test_majority_vote_three_units() {
let good1 = Distance::from_m(5.0);
let good2 = Distance::from_m(5.0);
let faulty = Distance::from_m(5.1);
let result = vote3(good1.tmr_bits(), good2.tmr_bits(), faulty.tmr_bits());
assert_eq!(result, Some(good1.tmr_bits()), "应选出健康值");
}
#[test]
fn test_vote_all_branches() {
let x = Distance::from_m(1.0).tmr_bits();
let y = Distance::from_m(2.0).tmr_bits();
let z = Distance::from_m(3.0).tmr_bits();
assert_eq!(vote3(x.clone(), x.clone(), y.clone()), Some(x.clone()));
assert_eq!(vote3(x.clone(), y.clone(), x.clone()), Some(x.clone()));
assert_eq!(vote3(y.clone(), x.clone(), x.clone()), Some(x.clone()));
assert_eq!(vote3(x, y, z), None);
}
#[test]
fn test_majority_passes_when_all_nan() {
let a = Angular::from_rad(f64::NAN);
let b = Angular::from_rad(f64::NAN);
let c = Angular::from_rad(f64::NAN);
assert_eq!(a.tmr_bits(), b.tmr_bits());
assert_eq!(b.tmr_bits(), c.tmr_bits());
}
#[test]
fn test_tmr_bits_hashable() {
use std::collections::HashSet;
let mut set = HashSet::new();
set.insert(Distance::from_m(1.0).tmr_bits());
set.insert(Distance::from_m(1.0).tmr_bits()); assert_eq!(set.len(), 1, "相同键应去重");
set.insert(Distance::from_m(2.0).tmr_bits());
assert_eq!(set.len(), 2);
assert!(set.contains(&Distance::from_m(1.0).tmr_bits()));
assert!(!set.contains(&Distance::from_m(3.0).tmr_bits()));
}
#[test]
fn test_tmr_bits_default_is_empty() {
let d = TmrBits::default();
assert!(d.is_empty());
assert_eq!(d, TmrBits::new());
}
#[test]
fn test_vote_tolerates_padding_differences() {
let unit1 = Velocity::from_m_per_sec(7.5);
let unit2 = Velocity::from_m_per_sec(15.0 / 2.0);
let unit3 = Velocity::from_m_per_sec(5.0 + 2.5);
assert_eq!(unit1.tmr_bits(), unit2.tmr_bits());
assert_eq!(unit2.tmr_bits(), unit3.tmr_bits());
}
#[test]
fn test_no_padding_type_coef() {
use crate::physics::basic::Coef;
use std::mem::size_of;
assert_eq!(
size_of::<Coef>(),
size_of::<f64>(),
"Coef 应与裸 f64 同宽,没有 padding 空间"
);
let a = Coef::new(1.5);
let b = Coef::new(1.5);
let c = Coef::new(1.5 + f64::EPSILON);
let bytes = |v: &Coef| unsafe {
std::slice::from_raw_parts(v as *const Coef as *const u8, size_of::<Coef>()).to_vec()
};
assert_eq!(bytes(&a), bytes(&b));
assert_ne!(bytes(&a), bytes(&c));
assert_eq!(a.tmr_bits(), b.tmr_bits());
assert_ne!(a.tmr_bits(), c.tmr_bits());
}
#[test]
fn test_padded_newtype_cannot_be_memcmped() {
use std::mem::size_of;
assert_eq!(size_of::<Distance>(), 16, "标量物理量占 16 字节");
assert_eq!(size_of::<f64>(), 8);
assert!(
size_of::<Distance>() > size_of::<f64>(),
"多出来的字节就是 tag + padding,字节比较会把它们算进去"
);
assert_eq!(
Distance::from_m(5.0).tmr_bits().len(),
1,
"只应取 f64 一个字段,padding 不参与"
);
let paths = [
Distance::from_m(5.0),
Distance::from_m(2.5 + 2.5),
Distance::default() + Distance::from_m(5.0),
Distance::from_m(10.0) / 2.0,
];
let first = paths[0].tmr_bits();
for (i, p) in paths.iter().enumerate() {
assert_eq!(p, &paths[0], "第 {} 个构造路径的语义值应相同", i);
assert_eq!(p.tmr_bits(), first, "第 {} 个构造路径的键应相同", i);
}
}
#[test]
fn test_nan_sign_bit_participates() {
let pos_nan = f64::from_bits(0x7ff8_0000_0000_0000); let neg_nan = f64::from_bits(0xfff8_0000_0000_0000);
assert_ne!(
pos_nan.to_bits(),
neg_nan.to_bits(),
"符号位不同则位型不同"
);
let a = Distance::from_m(pos_nan);
let b = Distance::from_m(neg_nan);
assert_ne!(a.tmr_bits(), b.tmr_bits());
}
#[test]
fn test_all_nan_units_agree_under_vote() {
let units = [
Distance::from_m(f64::NAN),
Distance::from_m(f64::NAN),
Distance::from_m(f64::NAN),
];
let bits: Vec<_> = units.iter().map(|u| u.tmr_bits()).collect();
assert_eq!(bits[0], bits[1]);
assert_eq!(bits[1], bits[2]);
assert_ne!(units[0], units[1]);
}
#[test]
fn test_signed_zero_across_complex_types() {
let v_pos = Vector3::new(
Distance::from_m(0.0),
Distance::from_m(0.0),
Distance::from_m(0.0),
);
let v_neg = Vector3::new(
Distance::from_m(-0.0),
Distance::from_m(0.0),
Distance::from_m(0.0),
);
assert_ne!(v_pos.tmr_bits(), v_neg.tmr_bits());
let q_pos = Quaternion::new(1.0, 0.0, 0.0, 0.0);
let q_neg = Quaternion::new(1.0, -0.0, 0.0, 0.0);
assert_ne!(
q_pos.tmr_bits(),
q_neg.tmr_bits(),
"同一姿态的 ±0 表示在位型上不同"
);
}
#[test]
fn test_euler_angles_bits() {
let a = EulerAngles::new(
Angular::from_rad(0.1),
Angular::from_rad(0.2),
Angular::from_rad(0.3),
);
let b = a;
let _ = a;
assert_eq!(a.tmr_bits(), b.tmr_bits());
assert_eq!(a.tmr_bits().len(), 3);
let c = EulerAngles::new(
Angular::from_rad(0.1),
Angular::from_rad(0.2),
Angular::from_rad(0.4),
);
assert_ne!(a.tmr_bits(), c.tmr_bits());
}
#[test]
fn test_euler_angles_derive() {
let a = EulerAngles::new(
Angular::from_rad(0.1),
Angular::from_rad(0.2),
Angular::from_rad(0.3),
);
let s = format!("{:?}", a);
assert!(s.contains("EulerAngles"));
let b = a.clone();
let c = a;
assert_eq!(a, b);
assert_eq!(a, c);
}
}