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
use crate::channel_info::{ChannelId, ChannelInfo};
use crate::error::Error;
use crate::error::Error::{InvalidChannelId, TransformsNotSorted};
use crate::frame_info::{FrameId, FrameInfo};
use crate::isometry_graph::IsometryGraph;
use crate::ops::filter::filter_by_channel;
use crate::transform::TransformId;
use crate::transform_info::TransformInfo;
use crate::utils::transforms_interpolation::interpolate_transforms;
use crate::Error::{InvalidTransformId, MissingTransforms, NoChannels};
use crate::{InterpolationMethod, Transform};
use chrono::{DateTime, Utc};
use itertools::Itertools;
use nalgebra::Isometry3;
use std::collections::{HashMap, HashSet};
use std::vec;
/// Represents a list of transforms for representing different coordinate frames.
///
#[derive(Debug, Default, Clone, PartialEq)]
pub struct ReferenceFrames {
pub(crate) transforms: HashMap<(ChannelId, TransformId), Vec<Transform>>,
pub(crate) frame_info: HashMap<FrameId, FrameInfo>,
pub(crate) channel_info: HashMap<ChannelId, ChannelInfo>,
pub(crate) transform_info: HashMap<TransformId, TransformInfo>,
}
impl ReferenceFrames {
pub fn new(
transforms: HashMap<(ChannelId, TransformId), Vec<Transform>>,
frame_info: HashMap<FrameId, FrameInfo>,
channel_info: HashMap<ChannelId, ChannelInfo>,
transform_info: HashMap<TransformId, TransformInfo>,
) -> Result<Self, Error> {
if !transforms.is_empty() {
// check if all frames are referenced by a transforms
for frame in frame_info.keys() {
let contained_in_transforms = transforms.keys().any(|(_, transform_id)| {
&transform_id.frame_id == frame || &transform_id.child_frame_id == frame
});
assert!(
contained_in_transforms,
"No transform is referencing child or parent frame: {frame}"
);
}
for (current_id, current_transform) in &transforms {
if !current_transform
.windows(2)
.all(|t| t[0].timestamp.timestamp_nanos() < t[1].timestamp.timestamp_nanos())
{
return Err(TransformsNotSorted {
channel_id: current_id.0.clone(),
transform_id: current_id.1.clone(),
});
}
}
}
// sorting the transform vectors by time
let mut sorted_transforms: HashMap<(ChannelId, TransformId), Vec<Transform>> =
HashMap::new();
for (current_key, current_transforms) in transforms {
let mut current_sorted_transforms = current_transforms.clone();
current_sorted_transforms.sort_by_key(|t| t.timestamp);
sorted_transforms.insert(current_key, current_sorted_transforms);
}
//.all(|transforms| transforms.sort_by_key(|t| t.timestamp));
/*for transform in transforms.iter() {
assert!(
frame_info.keys().contains(&transform.frame_id),
"Frame with id '{}' is missing",
transform.frame_id
);
assert!(
frame_info.keys().contains(&transform.child_frame_id),
"Frame with id '{}' is missing",
transform.child_frame_id
);
}*/
//for (a, b) in transform.iter().tuple_windows() {
// assert_eq!(a.child_frame_id, b.parent_frame_id, "Child frame '{}' does not fit to parent frame '{}' of the following transform", a.child_frame_id, b.parent_frame_id);
//}
Ok(Self {
transforms: sorted_transforms,
frame_info,
channel_info,
transform_info,
})
}
pub fn is_empty(&self) -> bool {
self.transforms.is_empty()
}
pub fn frame_info(&self) -> &HashMap<FrameId, FrameInfo> {
&self.frame_info
}
pub fn channel_info(&self) -> &HashMap<ChannelId, ChannelInfo> {
&self.channel_info
}
pub fn transform_info(&self) -> &HashMap<TransformId, TransformInfo> {
&self.transform_info
}
/// Creates a subset of.
pub fn get_subset(&self, selected_channel_ids: &[ChannelId]) -> Result<ReferenceFrames, Error> {
/*let invalid_channel_ids: Vec<&ChannelId> = selected_channel_ids
.iter()
.filter(|c| !self.get_channel_ids().contains(c))
.collect();
if !invalid_channel_ids.is_empty() {
let invalid_channel_ids = invalid_channel_ids.into_iter().cloned().collect();
return Err(InvalidChannelIds(invalid_channel_ids));
}*/
let all_transforms: HashMap<(ChannelId, TransformId), Vec<Transform>> = self
.transforms
.iter()
.filter(|((channel_id, _), _)| selected_channel_ids.contains(channel_id))
.map(|((channel_id, transform_id), transforms)| {
(
(channel_id.clone(), transform_id.clone()),
transforms.clone(),
)
})
.collect();
let selected_transform_ids: HashSet<TransformId> =
all_transforms.keys().map(|(_, t)| t.clone()).collect();
let selected_frame_ids: HashSet<FrameId> = selected_transform_ids
.iter()
.flat_map(|t| [t.frame_id.clone(), t.child_frame_id.clone()])
.collect();
let all_frame_info: HashMap<FrameId, FrameInfo> = self
.frame_info
.iter()
.filter(|(i, _)| selected_frame_ids.contains(i))
.map(|(i, f)| (i.clone(), f.clone()))
.collect();
let all_channel_info: HashMap<ChannelId, ChannelInfo> = self
.channel_info
.iter()
.filter(|(channel_id, _)| selected_channel_ids.contains(channel_id))
.map(|(i, c)| (i.clone(), c.clone()))
.collect();
let all_transform_info: HashMap<TransformId, TransformInfo> = self
.transform_info
.iter()
.filter(|(i, _)| selected_transform_ids.contains(i))
.map(|(i, c)| (i.clone(), c.clone()))
.collect();
let reference_frame = ReferenceFrames::new(
all_transforms,
all_frame_info,
all_channel_info,
all_transform_info,
)?;
Ok(reference_frame)
}
pub fn get_timed_subset(&self, timestamp: &DateTime<Utc>) -> Result<ReferenceFrames, Error> {
let all_transforms: HashMap<(ChannelId, TransformId), Vec<Transform>> = self
.transforms
.iter()
.map(|((channel_id, transform_id), transforms)| {
let interpolation_method = self
.get_interpolation_method(transform_id)
.unwrap_or_default();
let isometry =
interpolate_transforms(transforms, &Some(*timestamp), interpolation_method);
isometry.map(|i| {
(
(channel_id.clone(), transform_id.clone()),
vec![Transform::from(*timestamp, i)],
)
})
})
.collect::<Result<HashMap<(ChannelId, TransformId), Vec<Transform>>, _>>()?;
let all_transform_info = self
.transform_info
.keys()
.map(|k| {
(
k.clone(),
TransformInfo::new(Some(InterpolationMethod::Step)),
)
})
.collect();
let reference_frame = ReferenceFrames::new(
all_transforms,
self.frame_info.clone(),
self.channel_info.clone(),
all_transform_info,
)?;
Ok(reference_frame)
}
pub fn set_interpolation_method(
&mut self,
transform_id: TransformId,
method: Option<InterpolationMethod>,
) {
self.transform_info
.entry(transform_id)
.or_default()
.interpolation_method = method;
}
/*pub fn get_transforms_with_validity_time_frames(
&self,
channel_id: &ChannelId,
transform_id: &TransformId,
) -> Result<Vec<(DateTime<Utc>, Duration)>, Error> {
if !self.contains_channel(channel_id) {
return Err(InvalidChannelId(channel_id.clone()));
}
let transforms = self
.transforms
.get(&(channel_id.clone(), transform_id.clone()))
.ok_or_else(|| InvalidTransformId(channel_id.clone(), transform_id.clone()))?;
let interpolation_method = self
.get_interpolation_method(transform_id)
.unwrap_or_default();
let a = transforms.windows(2).map(|t| t[0].);
Ok(vec![])
}*/
pub fn transforms(&self) -> &HashMap<(ChannelId, TransformId), Vec<Transform>> {
&self.transforms
}
/// Returns the transforms valid at a specific timestamp.
pub fn get_valid_transform(
&self,
channel_id: &ChannelId,
transform_id: &TransformId,
timestamp: &Option<DateTime<Utc>>,
) -> Result<Vec<&Transform>, Error> {
if !self.contains_channel(channel_id) {
return Err(InvalidChannelId(channel_id.clone()));
}
let all_transforms: Vec<&Transform> = self
.transforms
.get(&(channel_id.clone(), transform_id.clone()))
.ok_or_else(|| InvalidTransformId(channel_id.clone(), transform_id.clone()))?
.iter()
.collect();
if timestamp.is_none() {
return Ok(all_transforms);
}
let timestamp = timestamp.unwrap();
let mut time_based_filtered_transforms: Vec<&Transform> = all_transforms
.clone()
.windows(2)
.filter(|t| {
t[0].timestamp.timestamp_nanos() <= timestamp.timestamp_nanos()
&& timestamp.timestamp_nanos() < t[1].timestamp.timestamp_nanos()
})
/*.filter(|t| {
t[0].duration
.map_or(false, |d| timestamp <= t[0].timestamp + d)
|| timestamp.timestamp_nanos() < t[1].timestamp.timestamp_nanos()
})*/
.map(|t| t[0])
.collect();
if all_transforms.last().unwrap().timestamp <= timestamp {
time_based_filtered_transforms.push(all_transforms.last().unwrap());
}
Ok(time_based_filtered_transforms)
}
pub fn get_channel_ids(&self) -> HashSet<ChannelId> {
self.transforms
.keys()
.map(|(channel_id, _)| channel_id.clone())
.collect()
}
pub fn get_frame_ids(&self) -> HashSet<FrameId> {
self.transforms
.keys()
.fold(Vec::<FrameId>::new(), |mut acc, x| {
acc.push(x.1.frame_id.clone());
acc.push(x.1.child_frame_id.clone());
acc
})
.into_iter()
.collect()
}
/*pub fn get_channel_names(&self) -> HashSet<ChannelId> {
self.transforms
.keys()
.map(|(channel_id, _)| channel_id.clone())
.collect()
}*/
pub fn get_channel_priority(&self, channel_id: &ChannelId) -> i32 {
assert!(self.contains_channel(channel_id));
self.channel_info
.get(channel_id)
.and_then(|x| x.priority)
.unwrap_or(0)
}
pub fn contains_channel(&self, channel_id: &ChannelId) -> bool {
self.get_channel_ids().iter().any(|x| x == channel_id)
}
pub fn contains_frame(&self, frame_id: &FrameId) -> bool {
self.get_frame_ids().iter().any(|f| f == frame_id)
}
pub fn contains_transform(&self, channel_id: &ChannelId, transform_id: &TransformId) -> bool {
self.transforms
.keys()
.any(|(current_channel_id, current_transform_id)| {
current_channel_id == channel_id && current_transform_id == transform_id
})
}
pub fn add_transform(
&mut self,
channel_id: ChannelId,
transform_id: TransformId,
transforms: Vec<Transform>,
channel_info: Option<ChannelInfo>,
transform_info: Option<TransformInfo>,
) -> Result<(), Error> {
if transforms.is_empty() {
return Err(MissingTransforms());
}
self.transforms
.insert((channel_id.clone(), transform_id.clone()), transforms);
if let Some(channel_info) = channel_info {
self.channel_info.insert(channel_id, channel_info);
}
if let Some(transform_info) = transform_info {
self.transform_info.insert(transform_id, transform_info);
}
Ok(())
}
pub fn get_interpolation_method(
&self,
transform_id: &TransformId,
) -> Option<InterpolationMethod> {
self.transform_info
.get(transform_id)
.and_then(|o| o.interpolation_method)
}
/// Derive a concrete transform graph for a specific timestamp and selected channels.
///
/// * `selected_channel_ids` - Selected channels for building the transform graph.
/// * `selected_timestamp` - Timestamp to choose for interpolating time-dependent transforms.
pub fn derive_transform_graph(
&self,
selected_channel_ids: &Option<HashSet<ChannelId>>,
selected_timestamp: &Option<DateTime<Utc>>,
) -> Result<IsometryGraph, Error> {
if let Some(channel_names) = &selected_channel_ids {
if channel_names.is_empty() {
return Err(NoChannels());
}
}
let mut selected_isometries: HashMap<TransformId, Isometry3<f64>> = HashMap::new();
//if let Some(selected_channel_ids) = selected_channel_ids {
// let a: HashMap<&(ChannelId, TransformId), &Transform> = self.transforms.iter().filter(|&(k, v)| selected_channel_ids.contains(&k.0)).collect();
// println!("x is None")
//}
let selected_transforms: HashMap<(ChannelId, TransformId), Vec<Transform>> =
match &selected_channel_ids {
Some(channel_names) => filter_by_channel(&self.transforms, channel_names),
None => self.transforms.clone(),
};
let mut prioritized_selected_transforms: HashMap<TransformId, Vec<Transform>> =
HashMap::new();
for (_, group) in &selected_transforms
.iter()
.sorted_by_key(|k| &k.0 .1)
.chunk_by(|k| k.0 .1.clone())
{
//group.into_iter().for_each(|x| println!("{}, {}", x.0.0, self.get_channel_priority(&x.0.0)));
let highest_priority = group
.into_iter()
.max_by_key(|k| self.get_channel_priority(&k.0 .0))
.unwrap();
//println!("{}", highest_priority.0 .0);
prioritized_selected_transforms
.insert(highest_priority.0 .1.clone(), highest_priority.1.clone());
}
//let test = selected_transforms.iter().group_by(|k| k.1);
for (current_transform_id, current_transforms) in prioritized_selected_transforms {
// let key = (current_channel_id.clone(), current_transform_id.clone());
let interpolation_method = self
.get_interpolation_method(¤t_transform_id)
.unwrap_or_default();
let interpolated_transform = interpolate_transforms(
¤t_transforms,
selected_timestamp,
interpolation_method,
)?;
selected_isometries.insert(current_transform_id.clone(), interpolated_transform);
// .entry(current_transform_id.clone())
// .insert_entry(Vec::new)
// .push(interpolated_transform);
/*
#[cfg(debug_assertions)]
{
let min_max_result = current_transforms.iter().minmax_by_key(|t| t.timestamp);
match min_max_result {
MinMaxResult::NoElements => {
println!(
"transform line frame_id={} child_frame_id={}: no elements",
¤t_transform_id.frame_id, ¤t_transform_id.child_frame_id
);
}
MinMaxResult::OneElement(e) => {
println!(
"transform frame_id={} child_frame_id={}: one element={}",
¤t_transform_id.frame_id,
¤t_transform_id.child_frame_id,
e.timestamp
);
}
MinMaxResult::MinMax(min, max) => {
println!(
"transform frame_id={} child_frame_id={}: min={}, max={}",
¤t_transform_id.frame_id,
¤t_transform_id.child_frame_id,
min.timestamp,
max.timestamp
);
}
}
}
*/
}
IsometryGraph::new(selected_isometries)
}
//
// pub fn get_start_timestamp(
// &self,
// selected_channel_ids: Option<&HashSet<ChannelId>>,
// ) -> DateTime<Utc> {
// assert!(selected_channel_ids.is_none(), "Not supported yet.");
// self.transforms
// .iter()
// .flat_map(|t| t.1)
// .map(|t| t.timestamp)
// .min()
// .unwrap()
// }
}