hydr_structures/structures/gate/trajectory/
events.rs1use super::GateTrajectoryAssignment;
4use crate::structures::{StructureError, StructureInputErrorKind};
5
6#[derive(Clone, Copy, Debug, PartialEq)]
8pub struct GateTrajectorySupport {
9 pub start_s: f64,
11 pub end_s: f64,
13}
14#[derive(Clone, Debug, PartialEq)]
16pub struct GateTrajectoryEvent {
17 pub time_s: f64,
19 pub structure_ids: Vec<String>,
21}
22#[derive(Clone, Debug, PartialEq)]
24pub struct GateTrajectoryInterval {
25 pub start_s: f64,
27 pub end_s: f64,
29 pub interior_events: Vec<GateTrajectoryEvent>,
31 pub end_event: Option<GateTrajectoryEvent>,
33}
34pub(super) fn build(
35 assignments: &[GateTrajectoryAssignment],
36 allow_point: bool,
37) -> Result<(GateTrajectorySupport, Vec<GateTrajectoryEvent>), StructureError> {
38 let mut start_s: f64 = 0.0;
39 let mut end_s = f64::MAX;
40 for assignment in assignments {
41 let knots = assignment.trajectory.knots();
42 let first = knots
43 .first()
44 .ok_or_else(|| StructureError::numerical("checked trajectory lacks first knot"))?;
45 let last = knots
46 .last()
47 .ok_or_else(|| StructureError::numerical("checked trajectory lacks last knot"))?;
48 start_s = start_s.max(first.time_s);
49 end_s = end_s.min(last.time_s);
50 }
51 if end_s < start_s || (!allow_point && end_s == start_s) {
52 return Err(StructureError::input(
53 StructureInputErrorKind::Definition,
54 if allow_point {
55 format!(
56 "common support=[{start_s},{end_s}]: supply a nonempty closed trajectory intersection"
57 )
58 } else {
59 format!(
60 "common support=[{start_s},{end_s}]: supply positive overlapping trajectory duration"
61 )
62 },
63 ));
64 }
65 let mut events = std::collections::BTreeMap::<u64, GateTrajectoryEvent>::new();
66 for assignment in assignments {
67 for knot in assignment.trajectory.knots() {
68 if knot.time_s >= start_s && knot.time_s <= end_s {
69 let key = if knot.time_s == 0.0 {
70 0
71 } else {
72 knot.time_s.to_bits()
73 };
74 events
75 .entry(key)
76 .or_insert_with(|| GateTrajectoryEvent {
77 time_s: knot.time_s,
78 structure_ids: Vec::new(),
79 })
80 .structure_ids
81 .push(assignment.structure_id.clone());
82 }
83 }
84 }
85 let events = events.into_values().collect();
86 Ok((GateTrajectorySupport { start_s, end_s }, events))
87}
88pub(super) fn check_query(
89 support: GateTrajectorySupport,
90 time_s: f64,
91) -> Result<(), StructureError> {
92 if !time_s.is_finite() {
93 return Err(StructureError::input(
94 StructureInputErrorKind::NonfiniteInput,
95 format!("trajectory query={time_s}: supply finite original time"),
96 ));
97 }
98 if time_s < support.start_s || time_s > support.end_s {
99 return Err(StructureError::input(
100 StructureInputErrorKind::UnsupportedRegime,
101 format!(
102 "trajectory query={time_s},support={support:?}: query within original closed support"
103 ),
104 ));
105 }
106 Ok(())
107}