|
| 1 | +use re_log_types::{DataRow, EntityPath, RowId, TimePoint}; |
| 2 | + |
| 3 | +use crate::{DataLoader, DataLoaderError, LoadedData}; |
| 4 | + |
| 5 | +// --- |
| 6 | + |
| 7 | +/// Loads data from any supported file or in-memory contents as native [`re_types::Archetype`]s. |
| 8 | +/// |
| 9 | +/// This is a simple generic [`DataLoader`] for filetypes that match 1-to-1 with our builtin |
| 10 | +/// archetypes. |
| 11 | +pub struct ArchetypeLoader; |
| 12 | + |
| 13 | +impl DataLoader for ArchetypeLoader { |
| 14 | + #[inline] |
| 15 | + fn name(&self) -> String { |
| 16 | + "rerun.data_loaders.Archetype".into() |
| 17 | + } |
| 18 | + |
| 19 | + #[cfg(not(target_arch = "wasm32"))] |
| 20 | + fn load_from_path( |
| 21 | + &self, |
| 22 | + store_id: re_log_types::StoreId, |
| 23 | + filepath: std::path::PathBuf, |
| 24 | + tx: std::sync::mpsc::Sender<LoadedData>, |
| 25 | + ) -> Result<(), crate::DataLoaderError> { |
| 26 | + use anyhow::Context as _; |
| 27 | + |
| 28 | + if filepath.is_dir() { |
| 29 | + return Ok(()); // simply not interested |
| 30 | + } |
| 31 | + |
| 32 | + re_tracing::profile_function!(filepath.display().to_string()); |
| 33 | + |
| 34 | + let contents = std::fs::read(&filepath) |
| 35 | + .with_context(|| format!("Failed to read file {filepath:?}"))?; |
| 36 | + let contents = std::borrow::Cow::Owned(contents); |
| 37 | + |
| 38 | + self.load_from_file_contents(store_id, filepath, contents, tx) |
| 39 | + } |
| 40 | + |
| 41 | + fn load_from_file_contents( |
| 42 | + &self, |
| 43 | + _store_id: re_log_types::StoreId, |
| 44 | + filepath: std::path::PathBuf, |
| 45 | + contents: std::borrow::Cow<'_, [u8]>, |
| 46 | + tx: std::sync::mpsc::Sender<LoadedData>, |
| 47 | + ) -> Result<(), crate::DataLoaderError> { |
| 48 | + re_tracing::profile_function!(filepath.display().to_string()); |
| 49 | + |
| 50 | + let entity_path = EntityPath::from_file_path(&filepath); |
| 51 | + |
| 52 | + let mut timepoint = TimePoint::timeless(); |
| 53 | + // TODO(cmc): log these once heuristics (I think?) are fixed |
| 54 | + if false { |
| 55 | + if let Ok(metadata) = filepath.metadata() { |
| 56 | + use re_log_types::{Time, Timeline}; |
| 57 | + |
| 58 | + if let Some(created) = metadata.created().ok().and_then(|t| Time::try_from(t).ok()) |
| 59 | + { |
| 60 | + timepoint.insert(Timeline::new_temporal("created_at"), created.into()); |
| 61 | + } |
| 62 | + if let Some(modified) = metadata |
| 63 | + .modified() |
| 64 | + .ok() |
| 65 | + .and_then(|t| Time::try_from(t).ok()) |
| 66 | + { |
| 67 | + timepoint.insert(Timeline::new_temporal("modified_at"), modified.into()); |
| 68 | + } |
| 69 | + if let Some(accessed) = metadata |
| 70 | + .accessed() |
| 71 | + .ok() |
| 72 | + .and_then(|t| Time::try_from(t).ok()) |
| 73 | + { |
| 74 | + timepoint.insert(Timeline::new_temporal("accessed_at"), accessed.into()); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + let extension = crate::extension(&filepath); |
| 80 | + |
| 81 | + let mut rows = Vec::new(); |
| 82 | + |
| 83 | + if crate::SUPPORTED_MESH_EXTENSIONS.contains(&extension.as_str()) { |
| 84 | + re_log::debug!(?filepath, loader = self.name(), "Loading 3D model…",); |
| 85 | + rows.extend(load_mesh( |
| 86 | + filepath, |
| 87 | + timepoint, |
| 88 | + entity_path, |
| 89 | + contents.into_owned(), |
| 90 | + )?); |
| 91 | + } else if crate::SUPPORTED_IMAGE_EXTENSIONS.contains(&extension.as_str()) { |
| 92 | + re_log::debug!(?filepath, loader = self.name(), "Loading image…",); |
| 93 | + rows.extend(load_image( |
| 94 | + &filepath, |
| 95 | + timepoint, |
| 96 | + entity_path, |
| 97 | + contents.into_owned(), |
| 98 | + )?); |
| 99 | + }; |
| 100 | + |
| 101 | + for row in rows { |
| 102 | + if tx.send(row.into()).is_err() { |
| 103 | + break; // The other end has decided to hang up, not our problem. |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + Ok(()) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// --- |
| 112 | + |
| 113 | +fn load_mesh( |
| 114 | + filepath: std::path::PathBuf, |
| 115 | + timepoint: TimePoint, |
| 116 | + entity_path: EntityPath, |
| 117 | + contents: Vec<u8>, |
| 118 | +) -> Result<impl ExactSizeIterator<Item = DataRow>, DataLoaderError> { |
| 119 | + re_tracing::profile_function!(); |
| 120 | + |
| 121 | + let rows = [ |
| 122 | + { |
| 123 | + let arch = re_types::archetypes::Asset3D::from_file_contents( |
| 124 | + contents, |
| 125 | + re_types::components::MediaType::guess_from_path(filepath), |
| 126 | + ); |
| 127 | + DataRow::from_archetype(RowId::new(), timepoint, entity_path, &arch)? |
| 128 | + }, |
| 129 | + // |
| 130 | + ]; |
| 131 | + |
| 132 | + Ok(rows.into_iter()) |
| 133 | +} |
| 134 | + |
| 135 | +fn load_image( |
| 136 | + filepath: &std::path::Path, |
| 137 | + timepoint: TimePoint, |
| 138 | + entity_path: EntityPath, |
| 139 | + contents: Vec<u8>, |
| 140 | +) -> Result<impl ExactSizeIterator<Item = DataRow>, DataLoaderError> { |
| 141 | + re_tracing::profile_function!(); |
| 142 | + |
| 143 | + let rows = [ |
| 144 | + { |
| 145 | + let arch = re_types::archetypes::Image::from_file_contents( |
| 146 | + contents, |
| 147 | + image::ImageFormat::from_path(filepath).ok(), |
| 148 | + )?; |
| 149 | + DataRow::from_archetype(RowId::new(), timepoint, entity_path, &arch)? |
| 150 | + }, |
| 151 | + // |
| 152 | + ]; |
| 153 | + |
| 154 | + Ok(rows.into_iter()) |
| 155 | +} |
0 commit comments