use super::accuracy::{GateMotionLimits, GateTrajectoryAccuracy};
use crate::structures::{StructureError, StructureInputErrorKind};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GateOpeningKnot {
pub time_s: f64,
pub opening_m: f64,
}
#[derive(Clone, Debug, PartialEq)]
pub struct GateOpeningTrajectory {
knots: Vec<GateOpeningKnot>,
motion_limits: GateMotionLimits,
accuracy: GateTrajectoryAccuracy,
provenance: String,
}
impl GateOpeningTrajectory {
pub fn new(
knots: Vec<GateOpeningKnot>,
motion_limits: GateMotionLimits,
accuracy: GateTrajectoryAccuracy,
provenance: String,
) -> Result<Self, StructureError> {
crate::structures::definition::text(&provenance, "gate trajectory provenance")?;
motion_limits.check()?;
accuracy.check()?;
if knots.len() < 2 {
return Err(StructureError::input(
StructureInputErrorKind::Definition,
"gate trajectory needs at least two knots: supply explicit closed support",
));
}
for (index, knot) in knots.iter().enumerate() {
if !knot.time_s.is_finite()
|| knot.time_s < 0.0
|| !knot.opening_m.is_finite()
|| knot.opening_m < 0.0
{
return Err(StructureError::input(
StructureInputErrorKind::Definition,
format!(
"gate knot={index},value={knot:?}: supply finite nonnegative time and opening"
),
));
}
}
for (index, pair) in knots.windows(2).enumerate() {
if pair[1].time_s <= pair[0].time_s {
return Err(StructureError::input(
StructureInputErrorKind::Definition,
format!(
"gate segment={index},times={:?}/{:?}: supply strictly increasing original times",
pair[0].time_s, pair[1].time_s
),
));
}
}
Ok(Self {
knots,
motion_limits,
accuracy,
provenance,
})
}
pub fn knots(&self) -> &[GateOpeningKnot] {
&self.knots
}
pub fn motion_limits(&self) -> GateMotionLimits {
self.motion_limits
}
pub fn accuracy(&self) -> GateTrajectoryAccuracy {
self.accuracy
}
pub fn provenance(&self) -> &str {
&self.provenance
}
}