use crate::native::linkage::Linkage;
use crate::text::StringEncoding;
pub type UorPoint = [f64; 3];
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ElementHeader {
pub type_code: u16,
pub level: u32,
pub id: Option<u64>,
pub complex_header: bool,
pub complex_component: bool,
pub deleted: bool,
pub is_3d: bool,
pub has_display_header: bool,
pub graphic_group: u32,
pub properties: u32,
pub style: u32,
pub weight: u32,
pub color: u32,
pub range: Option<[UorPoint; 2]>,
pub type_flags: u16,
pub modified_ms: Option<f64>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Rotation {
None,
Angle(f64),
Quaternion([f64; 4]),
Matrix([f64; 9]),
}
impl Rotation {
pub fn matrix(&self) -> [f64; 9] {
match *self {
Rotation::None => [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0],
Rotation::Angle(a) => {
let (s, c) = a.sin_cos();
[c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0]
}
Rotation::Quaternion([w, x, y, z]) => {
let n = (w * w + x * x + y * y + z * z).sqrt();
if !n.is_finite() || n < 1e-12 {
return Rotation::None.matrix();
}
let (w, x, y, z) = (w / n, x / n, y / n, z / n);
[
x * x - y * y - z * z + w * w,
2.0 * (z * w + x * y),
2.0 * (x * z - y * w),
2.0 * (x * y - z * w),
-x * x + y * y - z * z + w * w,
2.0 * (x * w + y * z),
2.0 * (x * z + y * w),
2.0 * (y * z - x * w),
-x * x - y * y + z * z + w * w,
]
}
Rotation::Matrix(m) => m,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TextData {
pub font: u32,
pub justification: u16,
pub width: f64,
pub height: f64,
pub rotation: Rotation,
pub origin: UorPoint,
pub text: String,
pub encoding: StringEncoding,
pub raw_text: Vec<u8>,
pub measured_length: Option<f64>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TextNodeData {
pub children: u32,
pub node_number: u32,
pub font: u32,
pub justification: u16,
pub line_spacing: f64,
pub width: f64,
pub height: f64,
pub rotation: Rotation,
pub origin: UorPoint,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CellData {
pub name: Option<String>,
pub description: Option<String>,
pub children: u32,
pub range: Option<[UorPoint; 2]>,
pub matrix: [f64; 9],
pub origin: UorPoint,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TagValue {
Text(String),
Int(i64),
Float(f64),
Binary(Vec<u8>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct TagData {
pub set_id: Option<u64>,
pub set_number: Option<u32>,
pub tag_index: u16,
pub value_type: u16,
pub value: TagValue,
pub target: Option<u64>,
pub origin: UorPoint,
pub offset: UorPoint,
pub size: [f64; 2],
pub font: u32,
pub justification: u16,
pub quaternion: [f64; 4],
}
impl TagData {
pub fn display_rotation(&self, is_3d: bool) -> Rotation {
let [w, x, y, z] = self.quaternion;
let legacy_quaternion = [w, x, y, z] == [-1.0, 0.0, 0.0, 0.0]
|| (x == 0.0 && y == 0.0 && z != 0.0 && (w * w + z * z - 1.0).abs() < 1e-9);
if is_3d || legacy_quaternion {
Rotation::Quaternion(self.quaternion)
} else {
Rotation::Angle(w)
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct TagDef {
pub id: u16,
pub name: String,
pub prompt: String,
pub value_type: u16,
pub default: TagValue,
pub flags: [u8; 5],
}
#[derive(Debug, Clone, PartialEq)]
pub struct TagSetData {
pub name: Option<String>,
pub number: Option<u32>,
pub tags: Vec<TagDef>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct LevelData {
pub id: u32,
pub name: Option<String>,
pub description: Option<String>,
pub parent: u32,
pub flags: u32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct TcbData {
pub is_3d: bool,
pub subunits_per_master: i32,
pub uor_per_subunit: i32,
pub master_label: String,
pub sub_label: String,
pub origin: UorPoint,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ElementData {
Line {
start: UorPoint,
end: UorPoint,
},
LineString {
points: Vec<UorPoint>,
},
Shape {
points: Vec<UorPoint>,
},
Curve {
points: Vec<UorPoint>,
},
PointString {
points: Vec<UorPoint>,
orientations: Vec<[f64; 4]>,
},
BSplinePoles {
points: Vec<UorPoint>,
},
BSplineKnots {
values: Vec<f64>,
},
BSplineWeights {
values: Vec<f64>,
},
BSplineCurve {
children: u32,
order: u8,
flags: u8,
curve_type: u8,
closed: bool,
num_poles: u32,
num_knots: u32,
},
Ellipse {
primary: f64,
secondary: f64,
rotation: Rotation,
center: UorPoint,
},
Arc {
start: f64,
sweep: f64,
primary: f64,
secondary: f64,
rotation: Rotation,
center: UorPoint,
},
Text(TextData),
TextNode(TextNodeData),
Complex {
children: u32,
},
Cell(CellData),
SharedCellDefinition(CellData),
SharedCellInstance(CellData),
Tag(TagData),
TagSet(TagSetData),
RasterFrame {
matrix: [f64; 16],
corner: Option<[f64; 2]>,
},
RasterReference {
file_name: Option<String>,
full_path: Option<String>,
},
ColorTable {
colors: Vec<[u8; 3]>,
},
Tcb(TcbData),
TableHeader {
table: u32,
children: u32,
},
Level(LevelData),
Font {
number: u32,
name: String,
},
Unknown,
}
impl ElementData {
pub fn child_count(&self) -> Option<u32> {
match self {
ElementData::BSplineCurve { children, .. }
| ElementData::Complex { children }
| ElementData::TableHeader { children, .. } => Some(*children),
ElementData::TextNode(t) => Some(t.children),
ElementData::Cell(c) | ElementData::SharedCellDefinition(c) => Some(c.children),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Element {
pub header: ElementHeader,
pub data: ElementData,
pub linkages: Vec<Linkage>,
pub problem: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
fn close(a: [f64; 9], b: [f64; 9]) -> bool {
a.iter().zip(b.iter()).all(|(x, y)| (x - y).abs() < 1e-12)
}
#[test]
fn quaternion_convention_matches_planar_rotation() {
let theta: f64 = 0.7;
let q = Rotation::Quaternion([(-theta / 2.0).cos(), 0.0, 0.0, (-theta / 2.0).sin()]);
assert!(close(q.matrix(), Rotation::Angle(theta).matrix()));
assert!(close(
Rotation::Quaternion([1.0, 0.0, 0.0, 0.0]).matrix(),
Rotation::None.matrix()
));
assert!(close(
Rotation::Quaternion([0.0; 4]).matrix(),
Rotation::None.matrix()
));
}
}