Skip to content

Commit

Permalink
Use site ID to ensure unique file names (#219)
Browse files Browse the repository at this point in the history
* Use site ID to ensure unique file names

Signed-off-by: Michael X. Grey <mxgrey@intrinsic.ai>

* Fix style

Signed-off-by: Michael X. Grey <mxgrey@intrinsic.ai>

* Fix door names for toggle floors plugin

Signed-off-by: Luca Della Vedova <lucadv@intrinsic.ai>

---------

Signed-off-by: Michael X. Grey <mxgrey@intrinsic.ai>
Signed-off-by: Luca Della Vedova <lucadv@intrinsic.ai>
Co-authored-by: Luca Della Vedova <lucadv@intrinsic.ai>
  • Loading branch information
mxgrey and luca-della-vedova authored Jun 14, 2024
1 parent 4f78ab3 commit bb43fcb
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 85 deletions.
21 changes: 13 additions & 8 deletions rmf_site_editor/src/site/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,19 @@ pub fn save_site(world: &mut World) {
}
ExportFormat::Sdf => {
// TODO(luca) reduce code duplication with default exporting

// Make sure to generate the site before anything else, because
// generating the site will ensure that all items are assigned a
// SiteID, and the SDF export process will not work correctly if
// any are unassigned.
let site = match generate_site(world, save_event.site) {
Ok(site) => site,
Err(err) => {
error!("Unable to compile site: {err}");
continue;
}
};

info!("Saving to {}", new_path.display());
let Some(parent_folder) = new_path.parent() else {
error!("Unable to save SDF. Please select a save path that has a parent directory.");
Expand Down Expand Up @@ -1309,14 +1322,6 @@ pub fn save_site(world: &mut World) {
}

migrate_relative_paths(save_event.site, &new_path, world);

let site = match generate_site(world, save_event.site) {
Ok(site) => site,
Err(err) => {
error!("Unable to compile site: {err}");
continue;
}
};
let graphs = legacy::nav_graph::NavGraph::from_site(&site);
let sdf = match site.to_sdf() {
Ok(sdf) => sdf,
Expand Down
80 changes: 61 additions & 19 deletions rmf_site_editor/src/site/sdf_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ pub fn headless_sdf_export(
return;
}
if sites.is_empty() {
warn!("Site loading failed, aborting");
warn!(
"Unable to load site from file [{}] so we cannot export an SDF from it",
export_state.target_path,
);
exit.send(bevy::app::AppExit);
}
if !missing_models.is_empty() {
Expand Down Expand Up @@ -128,6 +131,13 @@ pub fn collect_site_meshes(world: &mut World, site: Entity, folder: &Path) -> Re
std::fs::write(filename, bytes).map_err(|e| e.to_string())
};

let get_site_id = |e: Entity| -> Result<u32, String> {
q_site_ids.get(e).map(|id| id.0).map_err(|_| {
let backtrace = std::backtrace::Backtrace::force_capture();
format!("Site ID was not available for entity {e:?}. Backtrace:\n{backtrace}")
})
};

let get_mesh_and_material = |entity: Entity| -> Option<(&Mesh, &StandardMaterial)> {
let Ok((mesh, material)) = q_pbr.get(entity) else {
return None;
Expand Down Expand Up @@ -241,14 +251,22 @@ pub fn collect_site_meshes(world: &mut World, site: Entity, folder: &Path) -> Re
visual_data.extend(model_visuals);
} else {
// Create a new mesh for it
let filename = format!("{}/{}_collision.glb", folder.display(), **name);
let filename = format!(
"{}/model_{}_collision.glb",
folder.display(),
get_site_id(*child)?,
);
write_meshes_to_file(
model_collisions,
None,
CompressGltfOptions::skip_materials(),
filename,
)?;
let filename = format!("{}/{}_visual.glb", folder.display(), **name);
let filename = format!(
"{}/model_{}_visual.glb",
folder.display(),
get_site_id(*child)?,
);
write_meshes_to_file(
model_visuals,
Some(format!("{}_visual", **name)),
Expand All @@ -270,20 +288,22 @@ pub fn collect_site_meshes(world: &mut World, site: Entity, folder: &Path) -> Re
let Ok(tf) = q_tfs.get(*entity) else {
continue;
};
let Some(door_name) = door_name else {
continue;
};

let data = MeshData {
mesh,
material: Some(material),
transform: Some(tf.clone()),
};
let filename =
format!("{}/{}_{}.glb", folder.display(), **door_name, segment_name);
let filename = format!(
"{}/door_{}_{}.glb",
folder.display(),
get_site_id(*child)?,
segment_name,
);
let door_name = door_name.map(|n| n.0.as_str()).unwrap_or("");
write_meshes_to_file(
vec![data],
None,
Some(format!("door_{}_{}", door_name, segment_name)),
CompressGltfOptions::default(),
filename,
)?;
Expand All @@ -292,17 +312,25 @@ pub fn collect_site_meshes(world: &mut World, site: Entity, folder: &Path) -> Re
continue;
};
}
let filename = format!("{}/level_{}_collision.glb", folder.display(), **level_name);
let filename = format!(
"{}/level_{}_collision.glb",
folder.display(),
get_site_id(*site_child)?,
);
write_meshes_to_file(
collision_data,
None,
Some(format!("level_{}_collision", **level_name)),
CompressGltfOptions::skip_materials(),
filename,
)?;
let filename = format!("{}/level_{}_visual.glb", folder.display(), **level_name);
let filename = format!(
"{}/level_{}_visual.glb",
folder.display(),
get_site_id(*site_child)?,
);
write_meshes_to_file(
visual_data,
Some(format!("level_{}_visuals", **level_name)),
Some(format!("level_{}_visual", **level_name)),
CompressGltfOptions::default(),
filename,
)?;
Expand All @@ -327,8 +355,17 @@ pub fn collect_site_meshes(world: &mut World, site: Entity, folder: &Path) -> Re
transform: None,
});
}
let filename = format!("{}/{}.glb", folder.display(), **lift_name);
write_meshes_to_file(lift_data, None, CompressGltfOptions::default(), filename)?;
let filename = format!(
"{}/lift_{}.glb",
folder.display(),
get_site_id(*site_child)?
);
write_meshes_to_file(
lift_data,
Some(format!("lift_{}", **lift_name)),
CompressGltfOptions::default(),
filename,
)?;
// Now generate the lift doors
let LiftCabin::Rect(cabin) = cabin;
for (face, door) in cabin.doors().iter() {
Expand Down Expand Up @@ -357,15 +394,20 @@ pub fn collect_site_meshes(world: &mut World, site: Entity, folder: &Path) -> Re
transform: Some(tf.clone()),
};
let filename = format!(
"{}/{}_{}_{}.glb",
"{}/lift_{}_{}_{}.glb",
folder.display(),
**lift_name,
get_site_id(*site_child)?,
face.label(),
segment_name
segment_name,
);
write_meshes_to_file(
vec![data],
None,
Some(format!(
"lift_{}_{}_{}",
**lift_name,
face.label(),
segment_name
)),
CompressGltfOptions::default(),
filename,
)?;
Expand Down
2 changes: 2 additions & 0 deletions rmf_site_editor/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ pub fn dispatch_load_workspace_events(
.send(LoadWorkspaceFile(Some(path.clone()), data))
.expect("Failed sending load event");
}
} else {
warn!("Unable to read file [{path:?}] so it cannot be loaded");
}
}
LoadWorkspace::Data(data) => {
Expand Down
Loading

0 comments on commit bb43fcb

Please sign in to comment.