From 3f563a079709c72c7159714e9686e5407275332f Mon Sep 17 00:00:00 2001 From: Luca Della Vedova Date: Mon, 4 Sep 2023 11:42:11 +0800 Subject: [PATCH 1/4] Migrate all add_children except site loading ones Signed-off-by: Luca Della Vedova --- rmf_site_editor/src/interaction/anchor.rs | 23 ++- rmf_site_editor/src/interaction/assets.rs | 44 +++--- rmf_site_editor/src/interaction/light.rs | 171 +++++++++++----------- rmf_site_editor/src/occupancy.rs | 2 +- rmf_site_editor/src/site/door.rs | 69 +++++---- rmf_site_editor/src/site/drawing.rs | 8 +- rmf_site_editor/src/site/floor.rs | 32 ++-- rmf_site_editor/src/site/lane.rs | 102 ++++++------- rmf_site_editor/src/site/lift.rs | 10 +- rmf_site_editor/src/site/model.rs | 98 ++++++------- rmf_site_editor/src/site/site.rs | 20 ++- rmf_site_editor/src/workcell/workcell.rs | 2 +- 12 files changed, 273 insertions(+), 308 deletions(-) diff --git a/rmf_site_editor/src/interaction/anchor.rs b/rmf_site_editor/src/interaction/anchor.rs index 90160806..5f841198 100644 --- a/rmf_site_editor/src/interaction/anchor.rs +++ b/rmf_site_editor/src/interaction/anchor.rs @@ -46,25 +46,22 @@ pub fn add_anchor_visual_cues( _ => site_assets.site_anchor_mesh.clone(), }; - let mut entity_commands = commands.entity(e); - let body = entity_commands.add_children(|parent| { - let mut body = parent.spawn(PbrBundle { + let body = commands.spawn(PbrBundle { mesh: body_mesh, material: site_assets.passive_anchor_material.clone(), ..default() - }); - body.insert(Selectable::new(e)); - if subordinate.is_none() { - body.insert(DragPlaneBundle::new(e, Vec3::Z)); - } - let body = body.id(); - - body - }); + }) + .insert(Selectable::new(e)) + .id(); + if subordinate.is_none() { + commands.entity(body).insert(DragPlaneBundle::new(e, Vec3::Z)); + } + let mut entity_commands = commands.entity(e); entity_commands .insert(AnchorVisualization { body, drag: None }) - .insert(OutlineVisualization::Anchor { body }); + .insert(OutlineVisualization::Anchor { body }) + .add_child(body); // 3D anchors should always be visible with arrow cue meshes if anchor.is_3D() { diff --git a/rmf_site_editor/src/interaction/assets.rs b/rmf_site_editor/src/interaction/assets.rs index c66f3fa2..9112cbfa 100644 --- a/rmf_site_editor/src/interaction/assets.rs +++ b/rmf_site_editor/src/interaction/assets.rs @@ -63,7 +63,7 @@ impl InteractionAssets { pub fn make_axis( &self, - command: &mut Commands, + commands: &mut Commands, // What entity will be moved when this gizmo is dragged for_entity_opt: Option, // What entity should be the parent frame of this gizmo @@ -73,22 +73,22 @@ impl InteractionAssets { rotation: Quat, scale: f32, ) -> Entity { - return command.entity(parent).add_children(|parent| { - let mut child_entity = parent.spawn(PbrBundle { + let child_entity = commands.spawn(PbrBundle { transform: Transform::from_rotation(rotation) .with_translation(offset) .with_scale(Vec3::splat(scale)), mesh: self.arrow_mesh.clone(), material: material_set.passive.clone(), ..default() - }); + }) + .id(); - if let Some(for_entity) = for_entity_opt { - child_entity - .insert(DragAxisBundle::new(for_entity, Vec3::Z).with_materials(material_set)); - } - child_entity.id() - }); + if let Some(for_entity) = for_entity_opt { + commands.entity(child_entity) + .insert(DragAxisBundle::new(for_entity, Vec3::Z).with_materials(material_set)); + } + commands.entity(parent).add_child(child_entity); + child_entity } pub fn make_draggable_axis( @@ -121,12 +121,11 @@ impl InteractionAssets { anchor: Entity, cue: &mut AnchorVisualization, ) { - let drag_parent = commands.entity(anchor).add_children(|parent| { - parent - .spawn(SpatialBundle::default()) - .insert(VisualCue::no_outline().irregular().always_xray()) - .id() - }); + let drag_parent = commands + .spawn(SpatialBundle::default()) + .insert(VisualCue::no_outline().irregular().always_xray()) + .id(); + commands.entity(anchor).add_child(drag_parent); let height = 0.0; let scale = 0.2; @@ -167,12 +166,11 @@ impl InteractionAssets { cue: &mut AnchorVisualization, draggable: bool, ) { - let drag_parent = commands.entity(anchor).add_children(|parent| { - parent - .spawn(SpatialBundle::default()) - .insert(VisualCue::no_outline().irregular().always_xray()) - .id() - }); + let drag_parent = commands + .spawn(SpatialBundle::default()) + .insert(VisualCue::no_outline().irregular().always_xray()) + .id(); + commands.entity(anchor).add_child(drag_parent); let for_entity = if draggable { Some(anchor) } else { None }; let scale = 0.2; @@ -197,7 +195,7 @@ impl InteractionAssets { self.make_axis(commands, for_entity, drag_parent, m, p, r, scale); } - commands.entity(drag_parent).add_children(|parent| { + commands.entity(drag_parent).with_children(|parent| { for (polyline, material) in &self.centimeter_finite_grid { parent.spawn(PolylineBundle { polyline: polyline.clone(), diff --git a/rmf_site_editor/src/interaction/light.rs b/rmf_site_editor/src/interaction/light.rs index 1c04e6d6..f0992ee9 100644 --- a/rmf_site_editor/src/interaction/light.rs +++ b/rmf_site_editor/src/interaction/light.rs @@ -65,98 +65,95 @@ pub fn add_physical_light_visual_cues( headlight_toggle.0 = false; } - let bodies = commands - .entity(e) - .insert(light_material.clone()) - .add_children(|parent| { - let point = parent - .spawn(SpatialBundle { - visibility: Visibility { - is_visible: kind.is_point(), - }, - ..default() - }) - .with_children(|point| { - point - .spawn(PbrBundle { - mesh: assets.point_light_socket_mesh.clone(), - material: assets.physical_light_cover_material.clone(), - ..default() - }) - .insert(Selectable::new(e)) - .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); - - point - .spawn(PbrBundle { - mesh: assets.point_light_shine_mesh.clone(), - material: light_material.clone(), - ..default() - }) - .insert(Selectable::new(e)) - .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); - }) - .id(); - - let spot = parent - .spawn(SpatialBundle { - visibility: Visibility { - is_visible: kind.is_spot(), - }, + let point = commands + .spawn(SpatialBundle { + visibility: Visibility { + is_visible: kind.is_point(), + }, + ..default() + }) + .with_children(|point| { + point + .spawn(PbrBundle { + mesh: assets.point_light_socket_mesh.clone(), + material: assets.physical_light_cover_material.clone(), ..default() }) - .with_children(|spot| { - spot.spawn(PbrBundle { - mesh: assets.spot_light_cover_mesh.clone(), - material: assets.physical_light_cover_material.clone(), - ..default() - }) - .insert(Selectable::new(e)) - .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); - - spot.spawn(PbrBundle { - mesh: assets.spot_light_shine_mesh.clone(), - material: light_material.clone(), - ..default() - }) - .insert(Selectable::new(e)) - .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); - }) - .id(); + .insert(Selectable::new(e)) + .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); - let directional = parent - .spawn(SpatialBundle { - visibility: Visibility { - is_visible: kind.is_directional(), - }, + point + .spawn(PbrBundle { + mesh: assets.point_light_shine_mesh.clone(), + material: light_material.clone(), ..default() }) - .with_children(|dir| { - dir.spawn(PbrBundle { - mesh: assets.directional_light_cover_mesh.clone(), - material: assets.direction_light_cover_material.clone(), - ..default() - }) - .insert(Selectable::new(e)) - .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); - - dir.spawn(PbrBundle { - mesh: assets.directional_light_shine_mesh.clone(), - material: light_material.clone(), - ..default() - }) - .insert(Selectable::new(e)) - .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); - }) - .id(); - - return LightBodies { - point, - spot, - directional, - }; - }); - - commands.entity(e).insert(bodies); + .insert(Selectable::new(e)) + .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); + }) + .id(); + + let spot = commands + .spawn(SpatialBundle { + visibility: Visibility { + is_visible: kind.is_spot(), + }, + ..default() + }) + .with_children(|spot| { + spot.spawn(PbrBundle { + mesh: assets.spot_light_cover_mesh.clone(), + material: assets.physical_light_cover_material.clone(), + ..default() + }) + .insert(Selectable::new(e)) + .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); + + spot.spawn(PbrBundle { + mesh: assets.spot_light_shine_mesh.clone(), + material: light_material.clone(), + ..default() + }) + .insert(Selectable::new(e)) + .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); + }) + .id(); + + let directional = commands + .spawn(SpatialBundle { + visibility: Visibility { + is_visible: kind.is_directional(), + }, + ..default() + }) + .with_children(|dir| { + dir.spawn(PbrBundle { + mesh: assets.directional_light_cover_mesh.clone(), + material: assets.direction_light_cover_material.clone(), + ..default() + }) + .insert(Selectable::new(e)) + .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); + + dir.spawn(PbrBundle { + mesh: assets.directional_light_shine_mesh.clone(), + material: light_material.clone(), + ..default() + }) + .insert(Selectable::new(e)) + .insert(DragPlaneBundle::new(e, Vec3::Z).globally()); + }) + .id(); + + commands + .entity(e) + .insert(light_material.clone()) + .insert(LightBodies { + point, + spot, + directional, + }) + .push_children(&[point, spot, directional]); } } diff --git a/rmf_site_editor/src/occupancy.rs b/rmf_site_editor/src/occupancy.rs index 877fe3c0..34779b7b 100644 --- a/rmf_site_editor/src/occupancy.rs +++ b/rmf_site_editor/src/occupancy.rs @@ -276,7 +276,7 @@ fn calculate_grid( ); } - commands.entity(level).add_children(|level| { + commands.entity(level).with_children(|level| { level .spawn(PbrBundle { mesh: meshes.add(mesh.into()), diff --git a/rmf_site_editor/src/site/door.rs b/rmf_site_editor/src/site/door.rs index 1135bb5f..9a2404fc 100644 --- a/rmf_site_editor/src/site/door.rs +++ b/rmf_site_editor/src/site/door.rs @@ -275,42 +275,36 @@ pub fn add_door_visuals( let (pose_tf, door_tfs, cue_inner_mesh, cue_outline_mesh) = make_door_visuals(e, edge, &anchors, kind); - let mut commands = commands.entity(e); - let (body, cue_inner, cue_outline) = commands.add_children(|parent| { - let bodies = door_tfs - .iter() - .map(|tf| { - parent - .spawn(PbrBundle { - mesh: assets.box_mesh.clone(), - material: assets.door_body_material.clone(), - transform: *tf, - ..default() - }) - .insert(Selectable::new(e)) - .id() - }) - .collect::>(); - let body = DoorBodyType::from_door_type(kind, &bodies); - - let cue_inner = parent - .spawn(PbrBundle { - mesh: meshes.add(cue_inner_mesh), - material: assets.translucent_white.clone(), - ..default() - }) - .id(); - - let cue_outline = parent - .spawn(PbrBundle { - mesh: meshes.add(cue_outline_mesh), - material: assets.translucent_black.clone(), - ..default() - }) - .id(); + let bodies = door_tfs + .iter() + .map(|tf| { + commands + .spawn(PbrBundle { + mesh: assets.box_mesh.clone(), + material: assets.door_body_material.clone(), + transform: *tf, + ..default() + }) + .insert(Selectable::new(e)) + .id() + }) + .collect::>(); + let body = DoorBodyType::from_door_type(kind, &bodies); + let cue_inner = commands + .spawn(PbrBundle { + mesh: meshes.add(cue_inner_mesh), + material: assets.translucent_white.clone(), + ..default() + }) + .id(); - (body, cue_inner, cue_outline) - }); + let cue_outline = commands + .spawn(PbrBundle { + mesh: meshes.add(cue_outline_mesh), + material: assets.translucent_black.clone(), + ..default() + }) + .id(); // Level doors for lifts may have already been given a Visibility // component upon creation, in which case we should respect whatever @@ -322,6 +316,7 @@ pub fn add_door_visuals( }; commands + .entity(e) .insert(SpatialBundle { transform: pose_tf, visibility: Visibility { is_visible }, @@ -333,7 +328,9 @@ pub fn add_door_visuals( cue_outline, }) .insert(Category::Door) - .insert(EdgeLabels::LeftRight); + .insert(EdgeLabels::LeftRight) + .push_children(&[cue_inner, cue_outline]) + .push_children(&bodies); for anchor in edge.array() { if let Ok(mut deps) = dependents.get_mut(anchor) { diff --git a/rmf_site_editor/src/site/drawing.rs b/rmf_site_editor/src/site/drawing.rs index 2d1f0711..2f0072a2 100644 --- a/rmf_site_editor/src/site/drawing.rs +++ b/rmf_site_editor/src/site/drawing.rs @@ -157,14 +157,14 @@ pub fn handle_loaded_drawing( // We can ignore the layer height here since that update // will be handled by another system. } else { - let mut cmd = commands.entity(entity); - let leaf = cmd.add_children(|p| p.spawn_empty().id()); + let leaf = commands.spawn_empty().id(); - cmd.insert(DrawingSegments { leaf }) + commands.entity(entity).insert(DrawingSegments { leaf }) .insert(SpatialBundle::from_transform(pose.transform().with_scale( Vec3::new(1.0 / pixels_per_meter.0, 1.0 / pixels_per_meter.0, 1.), ))) - .insert(Selectable::new(entity)); + .insert(Selectable::new(entity)) + .push_children(&[leaf]); leaf }; let z = drawing_layer_height(rank); diff --git a/rmf_site_editor/src/site/floor.rs b/rmf_site_editor/src/site/floor.rs index cc966383..ed12591a 100644 --- a/rmf_site_editor/src/site/floor.rs +++ b/rmf_site_editor/src/site/floor.rs @@ -229,7 +229,6 @@ pub fn add_floor_visuals( let (base_color_texture, texture) = from_texture_source(texture_source, &textures); let mesh = make_floor_mesh(e, new_floor, &texture, &anchors); - let mut cmd = commands.entity(e); let height = floor_height(rank); let default_vis = parent .map(|p| default_floor_vis.get(p.get()).ok()) @@ -242,26 +241,27 @@ pub fn add_floor_visuals( ..default() }); - let mesh_entity_id = cmd + let mesh_entity_id = + commands.spawn(PbrBundle { + mesh: meshes.add(mesh), + material, + ..default() + }) + .insert(Selectable::new(e)) + .id(); + + commands.entity(e) .insert(SpatialBundle { transform: Transform::from_xyz(0.0, 0.0, height), ..default() }) - .add_children(|p| { - p.spawn(PbrBundle { - mesh: meshes.add(mesh), - material, - ..default() - }) - .insert(Selectable::new(e)) - .id() - }); + .insert(FloorSegments { + mesh: mesh_entity_id, + }) + .insert(Category::Floor) + .insert(PathBehavior::for_floor()) + .add_child(mesh_entity_id); - cmd.insert(FloorSegments { - mesh: mesh_entity_id, - }) - .insert(Category::Floor) - .insert(PathBehavior::for_floor()); for anchor in &new_floor.0 { let mut deps = dependents.get_mut(*anchor).unwrap(); diff --git a/rmf_site_editor/src/site/lane.rs b/rmf_site_editor/src/site/lane.rs index 5f0fb1f7..2b443c55 100644 --- a/rmf_site_editor/src/site/lane.rs +++ b/rmf_site_editor/src/site/lane.rs @@ -117,81 +117,60 @@ pub fn add_lane_visuals( let end_anchor = anchors .point_in_parent_frame_of(edge.end(), Category::Lane, e) .unwrap(); - let mut commands = commands.entity(e); - let (layer, start, mid, end, outlines) = commands.add_children(|parent| { - // Create a "layer" entity that manages the height of the lane, - // determined by the DisplayHeight of the graph. - let mut layer_cmd = parent.spawn(SpatialBundle { - transform: Transform::from_xyz(0.0, 0.0, height), - ..default() - }); - let (start, mid, end, outlines) = layer_cmd.add_children(|parent| { - let mut start = parent.spawn(PbrBundle { - mesh: assets.lane_end_mesh.clone(), - material: lane_material.clone(), - transform: Transform::from_translation(start_anchor), - ..default() - }); - let start_outline = start.add_children(|start| { - start - .spawn(PbrBundle { - mesh: assets.lane_end_outline.clone(), - transform: Transform::from_translation(-0.000_5 * Vec3::Z), - visibility: Visibility { is_visible: false }, - ..default() - }) - .id() - }); - let start = start.id(); + // Create a "layer" entity that manages the height of the lane, + // determined by the DisplayHeight of the graph. + let layer = commands.spawn(SpatialBundle { + transform: Transform::from_xyz(0.0, 0.0, height), + ..default() + }).id(); - let mut mid = parent.spawn(PbrBundle { - mesh: assets.lane_mid_mesh.clone(), - material: lane_material.clone(), - transform: line_stroke_transform(&start_anchor, &end_anchor, LANE_WIDTH), - ..default() - }); - let mid_outline = mid.add_children(|mid| { - mid.spawn(PbrBundle { - mesh: assets.lane_mid_outline.clone(), + let mut spawn_lane_mesh_and_outline = |lane_tf, lane_mesh, outline_mesh| + { + let outline = commands + .spawn(PbrBundle { + mesh: outline_mesh, transform: Transform::from_translation(-0.000_5 * Vec3::Z), visibility: Visibility { is_visible: false }, ..default() }) - .id() - }); - let mid = mid.id(); + .id(); - let mut end = parent.spawn(PbrBundle { - mesh: assets.lane_end_mesh.clone(), - material: lane_material.clone(), - transform: Transform::from_translation(end_anchor), - ..default() - }); - let end_outline = end.add_children(|end| { - end.spawn(PbrBundle { - mesh: assets.lane_end_outline.clone(), - transform: Transform::from_translation(-0.000_5 * Vec3::Z), - visibility: Visibility { is_visible: false }, - ..default() - }) - .id() - }); - let end = end.id(); + let mesh = commands.spawn(PbrBundle { + mesh: lane_mesh, + material: lane_material.clone(), + transform: lane_tf, + ..default() + }) + .add_child(outline) + .id(); + + commands.entity(layer).add_child(mesh); + (mesh, outline) + }; - (start, mid, end, [start_outline, mid_outline, end_outline]) - }); + let (start, start_outline) = spawn_lane_mesh_and_outline( + Transform::from_translation(start_anchor), + assets.lane_end_mesh.clone(), + assets.lane_end_outline.clone()); - (layer_cmd.id(), start, mid, end, outlines) - }); + let (mid, mid_outline) = spawn_lane_mesh_and_outline( + line_stroke_transform(&start_anchor, &end_anchor, LANE_WIDTH), + assets.lane_mid_mesh.clone(), + assets.lane_mid_outline.clone()); - commands + let (end, end_outline) = spawn_lane_mesh_and_outline( + Transform::from_translation(end_anchor), + assets.lane_end_mesh.clone(), + assets.lane_end_outline.clone()); + + commands.entity(e) .insert(LaneSegments { layer, start, mid, end, - outlines, + outlines: [start_outline, mid_outline, end_outline], }) .insert(SpatialBundle { transform: Transform::from_translation([0., 0., LANE_LAYER_START].into()), @@ -199,7 +178,8 @@ pub fn add_lane_visuals( ..default() }) .insert(Category::Lane) - .insert(EdgeLabels::StartEnd); + .insert(EdgeLabels::StartEnd) + .add_child(layer); } } diff --git a/rmf_site_editor/src/site/lift.rs b/rmf_site_editor/src/site/lift.rs index 5064697f..d6deca34 100644 --- a/rmf_site_editor/src/site/lift.rs +++ b/rmf_site_editor/src/site/lift.rs @@ -297,12 +297,10 @@ pub fn update_lift_cabin( *cabin_anchor_groups.get_mut(group).unwrap() = cabin_tf; } None => { - let group = commands.entity(e).add_children(|p| { - p.spawn(SpatialBundle::from_transform(cabin_tf)) - .insert(CabinAnchorGroupBundle::default()) - .id() - }); - commands.entity(e).insert(ChildCabinAnchorGroup(group)); + let group = commands.spawn(SpatialBundle::from_transform(cabin_tf)) + .insert(CabinAnchorGroupBundle::default()) + .id(); + commands.entity(e).insert(ChildCabinAnchorGroup(group)).add_child(group); } }; } diff --git a/rmf_site_editor/src/site/model.rs b/rmf_site_editor/src/site/model.rs index 7c20c524..9dd05c25 100644 --- a/rmf_site_editor/src/site/model.rs +++ b/rmf_site_editor/src/site/model.rs @@ -102,59 +102,59 @@ pub fn handle_model_loaded_events( for (e, h, scale, render_layer) in loading_models.iter() { if asset_server.get_load_state(&h.0) == LoadState::Loaded { let model_id = if let Some(gltf) = gltfs.get(&h.typed_weak::()) { - Some(commands.entity(e).add_children(|parent| { - // Get default scene if present, otherwise index 0 - let scene = gltf - .default_scene - .as_ref() - .map(|s| s.clone()) - .unwrap_or(gltf.scenes.get(0).unwrap().clone()); - parent - .spawn(SceneBundle { - scene, - transform: Transform::from_scale(**scale), - ..default() - }) - .id() - })) + // Get default scene if present, otherwise index 0 + let scene = gltf + .default_scene + .as_ref() + .map(|s| s.clone()) + .unwrap_or(gltf.scenes.get(0).unwrap().clone()); + let scene_id = commands + .spawn(SceneBundle { + scene, + transform: Transform::from_scale(**scale), + ..default() + }) + .id(); + commands.entity(e).add_child(scene_id); + Some(scene_id) } else if scenes.contains(&h.typed_weak::()) { - Some(commands.entity(e).add_children(|parent| { - let h_typed = h.0.clone().typed::(); - parent - .spawn(SceneBundle { - scene: h_typed, - transform: Transform::from_scale(**scale), - ..default() - }) - .id() - })) + let h_typed = h.0.clone().typed::(); + let scene_id = commands + .spawn(SceneBundle { + scene: h_typed, + transform: Transform::from_scale(**scale), + ..default() + }) + .id(); + commands.entity(e).add_child(scene_id); + Some(scene_id) } else if meshes.contains(&h.typed_weak::()) { - Some(commands.entity(e).add_children(|parent| { - let h_typed = h.0.clone().typed::(); - parent - .spawn(PbrBundle { - mesh: h_typed, - material: site_assets.default_mesh_grey_material.clone(), - transform: Transform::from_scale(**scale), - ..default() - }) - .id() - })) + let h_typed = h.0.clone().typed::(); + let mesh_id = commands + .spawn(PbrBundle { + mesh: h_typed, + material: site_assets.default_mesh_grey_material.clone(), + transform: Transform::from_scale(**scale), + ..default() + }) + .id(); + commands.entity(e).add_child(mesh_id); + Some(mesh_id) } else if let Some(urdf) = urdfs.get(&h.typed_weak::()) { - Some(commands.entity(e).add_children(|parent| { - parent - .spawn(SpatialBundle::VISIBLE_IDENTITY) - .insert(urdf.clone()) - .insert(Category::Workcell) - .id() - })) + let urdf_id = commands + .spawn(SpatialBundle::VISIBLE_IDENTITY) + .insert(urdf.clone()) + .insert(Category::Workcell) + .id(); + commands.entity(e).add_child(urdf_id); + Some(urdf_id) } else if let Some(sdf) = sdfs.get(&h.typed_weak::()) { - Some(commands.entity(e).add_children(|parent| { - parent - .spawn(SpatialBundle::VISIBLE_IDENTITY) - .insert(sdf.clone()) - .id() - })) + let sdf_id = commands + .spawn(SpatialBundle::VISIBLE_IDENTITY) + .insert(sdf.clone()) + .id(); + commands.entity(e).add_child(sdf_id); + Some(sdf_id) } else { None }; diff --git a/rmf_site_editor/src/site/site.rs b/rmf_site_editor/src/site/site.rs index df7d32d5..2801fa03 100644 --- a/rmf_site_editor/src/site/site.rs +++ b/rmf_site_editor/src/site/site.rs @@ -116,18 +116,16 @@ pub fn change_site( if !found_level { // Create a new blank level for the user - let new_level = commands.entity(cmd.site).add_children(|site| { - site.spawn(SpatialBundle::default()) - .insert(LevelProperties { - name: NameInSite("".to_owned()), - elevation: LevelElevation(0.), - global_floor_visibility: default(), - global_drawing_visibility: default(), - }) - .id() - }); + let new_level = commands.spawn(SpatialBundle::default()) + .insert(LevelProperties { + name: NameInSite("".to_owned()), + elevation: LevelElevation(0.), + global_floor_visibility: default(), + global_drawing_visibility: default(), + }) + .id(); - commands.entity(cmd.site).insert(CachedLevel(new_level)); + commands.entity(cmd.site).insert(CachedLevel(new_level)).add_child(new_level); current_level.0 = Some(new_level); } } diff --git a/rmf_site_editor/src/workcell/workcell.rs b/rmf_site_editor/src/workcell/workcell.rs index 218aec07..3b851298 100644 --- a/rmf_site_editor/src/workcell/workcell.rs +++ b/rmf_site_editor/src/workcell/workcell.rs @@ -56,7 +56,7 @@ pub fn add_workcell_visualization( for e in new_workcells.iter() { let body_mesh = site_assets.site_anchor_mesh.clone(); let mut entity_commands = commands.entity(e); - entity_commands.add_children(|parent| { + entity_commands.with_children(|parent| { let mut body = parent.spawn(PbrBundle { mesh: body_mesh, material: site_assets.passive_anchor_material.clone(), From b662c415390a28bbb2f2b1af6464aaf27d25d924 Mon Sep 17 00:00:00 2001 From: Luca Della Vedova Date: Mon, 4 Sep 2023 13:04:38 +0800 Subject: [PATCH 2/4] Refactor site load to remove add_children Signed-off-by: Luca Della Vedova --- rmf_site_editor/src/site/load.rs | 436 ++++++++++++++++--------------- 1 file changed, 227 insertions(+), 209 deletions(-) diff --git a/rmf_site_editor/src/site/load.rs b/rmf_site_editor/src/site/load.rs index 09eed4a8..ce2beb97 100644 --- a/rmf_site_editor/src/site/load.rs +++ b/rmf_site_editor/src/site/load.rs @@ -76,232 +76,250 @@ fn generate_site_entities( } }; - let mut site_cmd = commands.spawn(SpatialBundle::INVISIBLE_IDENTITY); - let site_id = site_cmd.id(); - site_cmd + let site_id = commands.spawn(SpatialBundle::INVISIBLE_IDENTITY) .insert(Category::Site) .insert(site_data.properties.clone()) .insert(WorkspaceMarker) - .add_children(|site| { - for (anchor_id, anchor) in &site_data.anchors { - let anchor_entity = site + .id(); + + for (anchor_id, anchor) in &site_data.anchors { + let anchor_entity = commands + .spawn(AnchorBundle::new(anchor.clone())) + .insert(SiteID(*anchor_id)) + .id(); + id_to_entity.insert(*anchor_id, anchor_entity); + consider_id(*anchor_id); + commands.entity(site_id).add_child(anchor_entity); + } + + for (group_id, group) in &site_data.fiducial_groups { + let group_entity = commands.spawn(group.clone()).insert(SiteID(*group_id)).id(); + id_to_entity.insert(*group_id, group_entity); + consider_id(*group_id); + commands.entity(site_id).add_child(group_entity); + } + + for (group_id, group) in &site_data.textures { + let group_entity = commands.spawn(group.clone()).insert(SiteID(*group_id)).id(); + id_to_entity.insert(*group_id, group_entity); + consider_id(*group_id); + commands.entity(site_id).add_child(group_entity); + } + + for (level_id, level_data) in &site_data.levels { + let level_entity = commands.spawn(SiteID(*level_id)).id(); + + for (anchor_id, anchor) in &level_data.anchors { + let anchor_entity = commands + .spawn(AnchorBundle::new(anchor.clone())) + .insert(SiteID(*anchor_id)) + .id(); + id_to_entity.insert(*anchor_id, anchor_entity); + consider_id(*anchor_id); + commands.entity(level_entity).add_child(anchor_entity); + } + + for (door_id, door) in &level_data.doors { + let door_entity = commands + .spawn(door.convert(&id_to_entity).for_site(site_id)?) + .insert(SiteID(*door_id)) + .id(); + id_to_entity.insert(*door_id, door_entity); + consider_id(*door_id); + commands.entity(level_entity).add_child(door_entity); + } + + for (drawing_id, drawing) in &level_data.drawings { + let drawing_entity = commands + .spawn(DrawingBundle::new(drawing.properties.clone())) + .insert(SiteID(*drawing_id)) + .id(); + + for (anchor_id, anchor) in &drawing.anchors { + let anchor_entity = commands .spawn(AnchorBundle::new(anchor.clone())) .insert(SiteID(*anchor_id)) .id(); id_to_entity.insert(*anchor_id, anchor_entity); consider_id(*anchor_id); + commands.entity(drawing_entity).add_child(anchor_entity); } - for (group_id, group) in &site_data.fiducial_groups { - let group_entity = site.spawn(group.clone()).insert(SiteID(*group_id)).id(); - id_to_entity.insert(*group_id, group_entity); - consider_id(*group_id); - } - - for (group_id, group) in &site_data.textures { - let group_entity = site.spawn(group.clone()).insert(SiteID(*group_id)).id(); - id_to_entity.insert(*group_id, group_entity); - consider_id(*group_id); - } - - for (level_id, level_data) in &site_data.levels { - let mut level_cmd = site.spawn(SiteID(*level_id)); - - level_cmd - .insert(SpatialBundle::INVISIBLE_IDENTITY) - .insert(level_data.properties.clone()) - .insert(Category::Level) - .add_children(|level| { - for (anchor_id, anchor) in &level_data.anchors { - let anchor_entity = level - .spawn(AnchorBundle::new(anchor.clone())) - .insert(SiteID(*anchor_id)) - .id(); - id_to_entity.insert(*anchor_id, anchor_entity); - consider_id(*anchor_id); - } - - for (door_id, door) in &level_data.doors { - let door_entity = level - .spawn(door.convert(&id_to_entity).for_site(site_id)?) - .insert(SiteID(*door_id)) - .id(); - id_to_entity.insert(*door_id, door_entity); - consider_id(*door_id); - } - - for (drawing_id, drawing) in &level_data.drawings { - level - .spawn(DrawingBundle::new(drawing.properties.clone())) - .insert(SiteID(*drawing_id)) - .add_children(|drawing_parent| { - for (anchor_id, anchor) in &drawing.anchors { - let anchor_entity = drawing_parent - .spawn(AnchorBundle::new(anchor.clone())) - .insert(SiteID(*anchor_id)) - .id(); - id_to_entity.insert(*anchor_id, anchor_entity); - consider_id(*anchor_id); - } - for (fiducial_id, fiducial) in &drawing.fiducials { - drawing_parent - .spawn( - fiducial - .convert(&id_to_entity) - .for_site(site_id)?, - ) - .insert(SiteID(*fiducial_id)); - consider_id(*fiducial_id); - } - for (measurement_id, measurement) in &drawing.measurements { - drawing_parent - .spawn( - measurement - .convert(&id_to_entity) - .for_site(site_id)?, - ) - .insert(SiteID(*measurement_id)); - consider_id(*measurement_id); - } - Ok(()) - })?; - consider_id(*drawing_id); - } - - for (floor_id, floor) in &level_data.floors { - level - .spawn(floor.convert(&id_to_entity).for_site(site_id)?) - .insert(SiteID(*floor_id)); - consider_id(*floor_id); - } - - for (light_id, light) in &level_data.lights { - level.spawn(light.clone()).insert(SiteID(*light_id)); - consider_id(*light_id); - } - - for (model_id, model) in &level_data.models { - level.spawn(model.clone()).insert(SiteID(*model_id)); - consider_id(*model_id); - } - - for (physical_camera_id, physical_camera) in &level_data.physical_cameras { - level - .spawn(physical_camera.clone()) - .insert(SiteID(*physical_camera_id)); - consider_id(*physical_camera_id); - } - - for (wall_id, wall) in &level_data.walls { - level - .spawn(wall.convert(&id_to_entity).for_site(site_id)?) - .insert(SiteID(*wall_id)); - consider_id(*wall_id); - } - Ok(()) - })?; - - // TODO(MXG): Log when a RecencyRanking fails to load correctly. - let level_entity = level_cmd - .insert( - RecencyRanking::::from_u32( - &level_data.rankings.floors, - &id_to_entity, - ) - .unwrap_or(RecencyRanking::new()), - ) - .insert( - RecencyRanking::::from_u32( - &level_data.rankings.drawings, - &id_to_entity, - ) - .unwrap_or(RecencyRanking::new()), + for (fiducial_id, fiducial) in &drawing.fiducials { + let fiducial_entity = commands + .spawn( + fiducial + .convert(&id_to_entity) + .for_site(site_id)?, ) + .insert(SiteID(*fiducial_id)) .id(); - id_to_entity.insert(*level_id, level_entity); - consider_id(*level_id); + consider_id(*fiducial_id); + commands.entity(drawing_entity).add_child(fiducial_entity); } - for (lift_id, lift_data) in &site_data.lifts { - let mut lift = site.spawn(SiteID(*lift_id)); - lift.add_children(|lift| { - let lift_entity = lift.parent_entity(); - lift.spawn(SpatialBundle::default()) - .insert(CabinAnchorGroupBundle::default()) - .with_children(|anchor_group| { - for (anchor_id, anchor) in &lift_data.cabin_anchors { - let anchor_entity = anchor_group - .spawn(AnchorBundle::new(anchor.clone())) - .insert(SiteID(*anchor_id)) - .id(); - id_to_entity.insert(*anchor_id, anchor_entity); - consider_id(*anchor_id); - } - }); - - for (door_id, door) in &lift_data.cabin_doors { - let door_entity = lift - .spawn(door.convert(&id_to_entity).for_site(site_id)?) - .insert(Dependents::single(lift_entity)) - .id(); - id_to_entity.insert(*door_id, door_entity); - consider_id(*door_id); - } - Ok(()) - })?; - - let lift = lift - .insert(Category::Lift) - .insert( - lift_data - .properties + for (measurement_id, measurement) in &drawing.measurements { + let measurement_entity = commands + .spawn( + measurement .convert(&id_to_entity) .for_site(site_id)?, ) + .insert(SiteID(*measurement_id)) .id(); - id_to_entity.insert(*lift_id, lift); - consider_id(*lift_id); + consider_id(*measurement_id); + commands.entity(drawing_entity).add_child(measurement_entity); } - for (fiducial_id, fiducial) in &site_data.fiducials { - let fiducial_entity = site - .spawn(fiducial.convert(&id_to_entity).for_site(site_id)?) - .insert(SiteID(*fiducial_id)) - .id(); - id_to_entity.insert(*fiducial_id, fiducial_entity); - consider_id(*fiducial_id); - } + consider_id(*drawing_id); + } - for (nav_graph_id, nav_graph_data) in &site_data.navigation.guided.graphs { - let nav_graph = site - .spawn(SpatialBundle::default()) - .insert(nav_graph_data.clone()) - .insert(SiteID(*nav_graph_id)) - .id(); - id_to_entity.insert(*nav_graph_id, nav_graph); - consider_id(*nav_graph_id); - } + for (floor_id, floor) in &level_data.floors { + let floor_entity = commands + .spawn(floor.convert(&id_to_entity).for_site(site_id)?) + .insert(SiteID(*floor_id)) + .id(); + consider_id(*floor_id); + commands.entity(level_entity).add_child(floor_entity); + } - for (lane_id, lane_data) in &site_data.navigation.guided.lanes { - let lane = site - .spawn(lane_data.convert(&id_to_entity).for_site(site_id)?) - .insert(SiteID(*lane_id)) - .id(); - id_to_entity.insert(*lane_id, lane); - consider_id(*lane_id); - } + for (wall_id, wall) in &level_data.walls { + let wall_entity = commands + .spawn(wall.convert(&id_to_entity).for_site(site_id)?) + .insert(SiteID(*wall_id)) + .id(); + consider_id(*wall_id); + commands.entity(level_entity).add_child(wall_entity); + } - for (location_id, location_data) in &site_data.navigation.guided.locations { - let location = site - .spawn(location_data.convert(&id_to_entity).for_site(site_id)?) - .insert(SiteID(*location_id)) - .id(); - id_to_entity.insert(*location_id, location); - consider_id(*location_id); - } + commands.entity(level_entity) + .insert(SpatialBundle::INVISIBLE_IDENTITY) + .insert(level_data.properties.clone()) + .insert(Category::Level) + .with_children(|level| { + // These don't need a return value so can be wrapped in a with_children + for (light_id, light) in &level_data.lights { + level.spawn(light.clone()).insert(SiteID(*light_id)); + consider_id(*light_id); + } + + for (model_id, model) in &level_data.models { + level.spawn(model.clone()).insert(SiteID(*model_id)); + consider_id(*model_id); + } + + for (physical_camera_id, physical_camera) in &level_data.physical_cameras { + level + .spawn(physical_camera.clone()) + .insert(SiteID(*physical_camera_id)); + consider_id(*physical_camera_id); + } + }); + + // TODO(MXG): Log when a RecencyRanking fails to load correctly. + commands.entity(level_entity) + .insert( + RecencyRanking::::from_u32( + &level_data.rankings.floors, + &id_to_entity, + ) + .unwrap_or(RecencyRanking::new()), + ) + .insert( + RecencyRanking::::from_u32( + &level_data.rankings.drawings, + &id_to_entity, + ) + .unwrap_or(RecencyRanking::new()), + ); + id_to_entity.insert(*level_id, level_entity); + consider_id(*level_id); + commands.entity(site_id).add_child(level_entity); + } + + for (lift_id, lift_data) in &site_data.lifts { + let lift_entity = commands.spawn(SiteID(*lift_id)).id(); + + commands.entity(lift_entity).with_children(|lift| { + lift.spawn(SpatialBundle::default()) + .insert(CabinAnchorGroupBundle::default()) + .with_children(|anchor_group| { + for (anchor_id, anchor) in &lift_data.cabin_anchors { + let anchor_entity = anchor_group + .spawn(AnchorBundle::new(anchor.clone())) + .insert(SiteID(*anchor_id)) + .id(); + id_to_entity.insert(*anchor_id, anchor_entity); + consider_id(*anchor_id); + } + }); + }); + + for (door_id, door) in &lift_data.cabin_doors { + let door_entity = commands + .spawn(door.convert(&id_to_entity).for_site(site_id)?) + .insert(Dependents::single(lift_entity)) + .id(); + id_to_entity.insert(*door_id, door_entity); + consider_id(*door_id); + commands.entity(lift_entity).add_child(door_entity); + } + + commands.entity(lift_entity) + .insert(Category::Lift) + .insert( + lift_data + .properties + .convert(&id_to_entity) + .for_site(site_id)?, + ); + + id_to_entity.insert(*lift_id, lift_entity); + consider_id(*lift_id); + commands.entity(site_id).add_child(lift_entity); + } + + for (fiducial_id, fiducial) in &site_data.fiducials { + let fiducial_entity = commands + .spawn(fiducial.convert(&id_to_entity).for_site(site_id)?) + .insert(SiteID(*fiducial_id)) + .id(); + id_to_entity.insert(*fiducial_id, fiducial_entity); + consider_id(*fiducial_id); + commands.entity(site_id).add_child(fiducial_entity); + } + + for (nav_graph_id, nav_graph_data) in &site_data.navigation.guided.graphs { + let nav_graph = commands + .spawn(SpatialBundle::default()) + .insert(nav_graph_data.clone()) + .insert(SiteID(*nav_graph_id)) + .id(); + id_to_entity.insert(*nav_graph_id, nav_graph); + consider_id(*nav_graph_id); + commands.entity(site_id).add_child(nav_graph); + } + + for (lane_id, lane_data) in &site_data.navigation.guided.lanes { + let lane = commands + .spawn(lane_data.convert(&id_to_entity).for_site(site_id)?) + .insert(SiteID(*lane_id)) + .id(); + id_to_entity.insert(*lane_id, lane); + consider_id(*lane_id); + commands.entity(site_id).add_child(lane); + } + + for (location_id, location_data) in &site_data.navigation.guided.locations { + let location = commands + .spawn(location_data.convert(&id_to_entity).for_site(site_id)?) + .insert(SiteID(*location_id)) + .id(); + id_to_entity.insert(*location_id, location); + consider_id(*location_id); + commands.entity(site_id).add_child(location); + } - Ok(()) - })?; let nav_graph_rankings = match RecencyRanking::::from_u32( &site_data.navigation.guided.ranking, @@ -317,7 +335,7 @@ fn generate_site_entities( } }; - site_cmd + commands.entity(site_id) .insert(nav_graph_rankings) .insert(NextSiteID(highest_id + 1)); @@ -510,7 +528,7 @@ fn generate_imported_nav_graphs( } } if !already_existing { - params.commands.entity(anchor_group).add_children(|group| { + params.commands.entity(anchor_group).with_children(|group| { let e_anchor = group.spawn(AnchorBundle::new(anchor.clone())).id(); id_to_entity.insert(*anchor_id, e_anchor); }); @@ -538,7 +556,7 @@ fn generate_imported_nav_graphs( } } if !already_existing { - params.commands.entity(level_e).add_children(|level| { + params.commands.entity(level_e).with_children(|level| { let e_anchor = level.spawn(AnchorBundle::new(anchor.clone())).id(); id_to_entity.insert(*anchor_id, e_anchor); }); @@ -561,7 +579,7 @@ fn generate_imported_nav_graphs( } } if !already_existing { - params.commands.entity(into_site).add_children(|site| { + params.commands.entity(into_site).with_children(|site| { let e_anchor = site.spawn(AnchorBundle::new(anchor.clone())).id(); id_to_entity.insert(*anchor_id, e_anchor); }); @@ -570,7 +588,7 @@ fn generate_imported_nav_graphs( } for (nav_graph_id, nav_graph_data) in &from_site_data.navigation.guided.graphs { - params.commands.entity(into_site).add_children(|site| { + params.commands.entity(into_site).with_children(|site| { let e = site .spawn(SpatialBundle::default()) .insert(nav_graph_data.clone()) @@ -583,7 +601,7 @@ fn generate_imported_nav_graphs( let lane_data = lane_data .convert(&id_to_entity) .map_err(ImportNavGraphError::BrokenInternalReference)?; - params.commands.entity(into_site).add_children(|site| { + params.commands.entity(into_site).with_children(|site| { let e = site.spawn(lane_data).id(); id_to_entity.insert(*lane_id, e); }); @@ -593,7 +611,7 @@ fn generate_imported_nav_graphs( let location_data = location_data .convert(&id_to_entity) .map_err(ImportNavGraphError::BrokenInternalReference)?; - params.commands.entity(into_site).add_children(|site| { + params.commands.entity(into_site).with_children(|site| { let e = site.spawn(location_data).id(); id_to_entity.insert(*location_id, e); }); From 9c93a337ebc0b619749d78bc0f61e5ee6dff611c Mon Sep 17 00:00:00 2001 From: Luca Della Vedova Date: Mon, 4 Sep 2023 13:06:42 +0800 Subject: [PATCH 3/4] Style Signed-off-by: Luca Della Vedova --- rmf_site_editor/src/interaction/anchor.rs | 7 ++- rmf_site_editor/src/interaction/assets.rs | 6 ++- rmf_site_editor/src/interaction/light.rs | 2 +- rmf_site_editor/src/site/drawing.rs | 4 +- rmf_site_editor/src/site/floor.rs | 8 ++-- rmf_site_editor/src/site/lane.rs | 54 +++++++++++++---------- rmf_site_editor/src/site/lift.rs | 8 +++- rmf_site_editor/src/site/load.rs | 50 +++++++++------------ rmf_site_editor/src/site/site.rs | 8 +++- 9 files changed, 80 insertions(+), 67 deletions(-) diff --git a/rmf_site_editor/src/interaction/anchor.rs b/rmf_site_editor/src/interaction/anchor.rs index 5f841198..570bb789 100644 --- a/rmf_site_editor/src/interaction/anchor.rs +++ b/rmf_site_editor/src/interaction/anchor.rs @@ -46,7 +46,8 @@ pub fn add_anchor_visual_cues( _ => site_assets.site_anchor_mesh.clone(), }; - let body = commands.spawn(PbrBundle { + let body = commands + .spawn(PbrBundle { mesh: body_mesh, material: site_assets.passive_anchor_material.clone(), ..default() @@ -54,7 +55,9 @@ pub fn add_anchor_visual_cues( .insert(Selectable::new(e)) .id(); if subordinate.is_none() { - commands.entity(body).insert(DragPlaneBundle::new(e, Vec3::Z)); + commands + .entity(body) + .insert(DragPlaneBundle::new(e, Vec3::Z)); } let mut entity_commands = commands.entity(e); diff --git a/rmf_site_editor/src/interaction/assets.rs b/rmf_site_editor/src/interaction/assets.rs index 9112cbfa..12bb8c12 100644 --- a/rmf_site_editor/src/interaction/assets.rs +++ b/rmf_site_editor/src/interaction/assets.rs @@ -73,7 +73,8 @@ impl InteractionAssets { rotation: Quat, scale: f32, ) -> Entity { - let child_entity = commands.spawn(PbrBundle { + let child_entity = commands + .spawn(PbrBundle { transform: Transform::from_rotation(rotation) .with_translation(offset) .with_scale(Vec3::splat(scale)), @@ -84,7 +85,8 @@ impl InteractionAssets { .id(); if let Some(for_entity) = for_entity_opt { - commands.entity(child_entity) + commands + .entity(child_entity) .insert(DragAxisBundle::new(for_entity, Vec3::Z).with_materials(material_set)); } commands.entity(parent).add_child(child_entity); diff --git a/rmf_site_editor/src/interaction/light.rs b/rmf_site_editor/src/interaction/light.rs index f0992ee9..60f8d875 100644 --- a/rmf_site_editor/src/interaction/light.rs +++ b/rmf_site_editor/src/interaction/light.rs @@ -65,7 +65,7 @@ pub fn add_physical_light_visual_cues( headlight_toggle.0 = false; } - let point = commands + let point = commands .spawn(SpatialBundle { visibility: Visibility { is_visible: kind.is_point(), diff --git a/rmf_site_editor/src/site/drawing.rs b/rmf_site_editor/src/site/drawing.rs index 2f0072a2..00c9d41f 100644 --- a/rmf_site_editor/src/site/drawing.rs +++ b/rmf_site_editor/src/site/drawing.rs @@ -159,7 +159,9 @@ pub fn handle_loaded_drawing( } else { let leaf = commands.spawn_empty().id(); - commands.entity(entity).insert(DrawingSegments { leaf }) + commands + .entity(entity) + .insert(DrawingSegments { leaf }) .insert(SpatialBundle::from_transform(pose.transform().with_scale( Vec3::new(1.0 / pixels_per_meter.0, 1.0 / pixels_per_meter.0, 1.), ))) diff --git a/rmf_site_editor/src/site/floor.rs b/rmf_site_editor/src/site/floor.rs index ed12591a..61d6d2f3 100644 --- a/rmf_site_editor/src/site/floor.rs +++ b/rmf_site_editor/src/site/floor.rs @@ -241,8 +241,8 @@ pub fn add_floor_visuals( ..default() }); - let mesh_entity_id = - commands.spawn(PbrBundle { + let mesh_entity_id = commands + .spawn(PbrBundle { mesh: meshes.add(mesh), material, ..default() @@ -250,7 +250,8 @@ pub fn add_floor_visuals( .insert(Selectable::new(e)) .id(); - commands.entity(e) + commands + .entity(e) .insert(SpatialBundle { transform: Transform::from_xyz(0.0, 0.0, height), ..default() @@ -262,7 +263,6 @@ pub fn add_floor_visuals( .insert(PathBehavior::for_floor()) .add_child(mesh_entity_id); - for anchor in &new_floor.0 { let mut deps = dependents.get_mut(*anchor).unwrap(); deps.insert(e); diff --git a/rmf_site_editor/src/site/lane.rs b/rmf_site_editor/src/site/lane.rs index 2b443c55..42aedced 100644 --- a/rmf_site_editor/src/site/lane.rs +++ b/rmf_site_editor/src/site/lane.rs @@ -120,27 +120,29 @@ pub fn add_lane_visuals( // Create a "layer" entity that manages the height of the lane, // determined by the DisplayHeight of the graph. - let layer = commands.spawn(SpatialBundle { - transform: Transform::from_xyz(0.0, 0.0, height), - ..default() - }).id(); + let layer = commands + .spawn(SpatialBundle { + transform: Transform::from_xyz(0.0, 0.0, height), + ..default() + }) + .id(); - let mut spawn_lane_mesh_and_outline = |lane_tf, lane_mesh, outline_mesh| - { + let mut spawn_lane_mesh_and_outline = |lane_tf, lane_mesh, outline_mesh| { let outline = commands - .spawn(PbrBundle { - mesh: outline_mesh, - transform: Transform::from_translation(-0.000_5 * Vec3::Z), - visibility: Visibility { is_visible: false }, - ..default() - }) - .id(); - - let mesh = commands.spawn(PbrBundle { - mesh: lane_mesh, - material: lane_material.clone(), - transform: lane_tf, - ..default() + .spawn(PbrBundle { + mesh: outline_mesh, + transform: Transform::from_translation(-0.000_5 * Vec3::Z), + visibility: Visibility { is_visible: false }, + ..default() + }) + .id(); + + let mesh = commands + .spawn(PbrBundle { + mesh: lane_mesh, + material: lane_material.clone(), + transform: lane_tf, + ..default() }) .add_child(outline) .id(); @@ -152,19 +154,23 @@ pub fn add_lane_visuals( let (start, start_outline) = spawn_lane_mesh_and_outline( Transform::from_translation(start_anchor), assets.lane_end_mesh.clone(), - assets.lane_end_outline.clone()); + assets.lane_end_outline.clone(), + ); let (mid, mid_outline) = spawn_lane_mesh_and_outline( line_stroke_transform(&start_anchor, &end_anchor, LANE_WIDTH), assets.lane_mid_mesh.clone(), - assets.lane_mid_outline.clone()); + assets.lane_mid_outline.clone(), + ); let (end, end_outline) = spawn_lane_mesh_and_outline( Transform::from_translation(end_anchor), assets.lane_end_mesh.clone(), - assets.lane_end_outline.clone()); - - commands.entity(e) + assets.lane_end_outline.clone(), + ); + + commands + .entity(e) .insert(LaneSegments { layer, start, diff --git a/rmf_site_editor/src/site/lift.rs b/rmf_site_editor/src/site/lift.rs index d6deca34..4b876969 100644 --- a/rmf_site_editor/src/site/lift.rs +++ b/rmf_site_editor/src/site/lift.rs @@ -297,10 +297,14 @@ pub fn update_lift_cabin( *cabin_anchor_groups.get_mut(group).unwrap() = cabin_tf; } None => { - let group = commands.spawn(SpatialBundle::from_transform(cabin_tf)) + let group = commands + .spawn(SpatialBundle::from_transform(cabin_tf)) .insert(CabinAnchorGroupBundle::default()) .id(); - commands.entity(e).insert(ChildCabinAnchorGroup(group)).add_child(group); + commands + .entity(e) + .insert(ChildCabinAnchorGroup(group)) + .add_child(group); } }; } diff --git a/rmf_site_editor/src/site/load.rs b/rmf_site_editor/src/site/load.rs index ce2beb97..3b8dc767 100644 --- a/rmf_site_editor/src/site/load.rs +++ b/rmf_site_editor/src/site/load.rs @@ -76,7 +76,8 @@ fn generate_site_entities( } }; - let site_id = commands.spawn(SpatialBundle::INVISIBLE_IDENTITY) + let site_id = commands + .spawn(SpatialBundle::INVISIBLE_IDENTITY) .insert(Category::Site) .insert(site_data.properties.clone()) .insert(WorkspaceMarker) @@ -147,11 +148,7 @@ fn generate_site_entities( for (fiducial_id, fiducial) in &drawing.fiducials { let fiducial_entity = commands - .spawn( - fiducial - .convert(&id_to_entity) - .for_site(site_id)?, - ) + .spawn(fiducial.convert(&id_to_entity).for_site(site_id)?) .insert(SiteID(*fiducial_id)) .id(); consider_id(*fiducial_id); @@ -160,15 +157,13 @@ fn generate_site_entities( for (measurement_id, measurement) in &drawing.measurements { let measurement_entity = commands - .spawn( - measurement - .convert(&id_to_entity) - .for_site(site_id)?, - ) + .spawn(measurement.convert(&id_to_entity).for_site(site_id)?) .insert(SiteID(*measurement_id)) .id(); consider_id(*measurement_id); - commands.entity(drawing_entity).add_child(measurement_entity); + commands + .entity(drawing_entity) + .add_child(measurement_entity); } consider_id(*drawing_id); @@ -192,7 +187,8 @@ fn generate_site_entities( commands.entity(level_entity).add_child(wall_entity); } - commands.entity(level_entity) + commands + .entity(level_entity) .insert(SpatialBundle::INVISIBLE_IDENTITY) .insert(level_data.properties.clone()) .insert(Category::Level) @@ -217,13 +213,11 @@ fn generate_site_entities( }); // TODO(MXG): Log when a RecencyRanking fails to load correctly. - commands.entity(level_entity) + commands + .entity(level_entity) .insert( - RecencyRanking::::from_u32( - &level_data.rankings.floors, - &id_to_entity, - ) - .unwrap_or(RecencyRanking::new()), + RecencyRanking::::from_u32(&level_data.rankings.floors, &id_to_entity) + .unwrap_or(RecencyRanking::new()), ) .insert( RecencyRanking::::from_u32( @@ -265,14 +259,12 @@ fn generate_site_entities( commands.entity(lift_entity).add_child(door_entity); } - commands.entity(lift_entity) - .insert(Category::Lift) - .insert( - lift_data - .properties - .convert(&id_to_entity) - .for_site(site_id)?, - ); + commands.entity(lift_entity).insert(Category::Lift).insert( + lift_data + .properties + .convert(&id_to_entity) + .for_site(site_id)?, + ); id_to_entity.insert(*lift_id, lift_entity); consider_id(*lift_id); @@ -320,7 +312,6 @@ fn generate_site_entities( commands.entity(site_id).add_child(location); } - let nav_graph_rankings = match RecencyRanking::::from_u32( &site_data.navigation.guided.ranking, &id_to_entity, @@ -335,7 +326,8 @@ fn generate_site_entities( } }; - commands.entity(site_id) + commands + .entity(site_id) .insert(nav_graph_rankings) .insert(NextSiteID(highest_id + 1)); diff --git a/rmf_site_editor/src/site/site.rs b/rmf_site_editor/src/site/site.rs index 2801fa03..2ab5ddcd 100644 --- a/rmf_site_editor/src/site/site.rs +++ b/rmf_site_editor/src/site/site.rs @@ -116,7 +116,8 @@ pub fn change_site( if !found_level { // Create a new blank level for the user - let new_level = commands.spawn(SpatialBundle::default()) + let new_level = commands + .spawn(SpatialBundle::default()) .insert(LevelProperties { name: NameInSite("".to_owned()), elevation: LevelElevation(0.), @@ -125,7 +126,10 @@ pub fn change_site( }) .id(); - commands.entity(cmd.site).insert(CachedLevel(new_level)).add_child(new_level); + commands + .entity(cmd.site) + .insert(CachedLevel(new_level)) + .add_child(new_level); current_level.0 = Some(new_level); } } From dea6af9d5b324c33297e62993f702ff2ab1ee30b Mon Sep 17 00:00:00 2001 From: Luca Della Vedova Date: Mon, 4 Sep 2023 14:56:55 +0800 Subject: [PATCH 4/4] Change to set_parent when simpler Signed-off-by: Luca Della Vedova --- rmf_site_editor/src/interaction/assets.rs | 4 +- rmf_site_editor/src/site/lane.rs | 24 +++---- rmf_site_editor/src/site/load.rs | 57 ++++++++------- rmf_site_editor/src/site/model.rs | 87 ++++++++++++----------- rmf_site_editor/src/site/site.rs | 6 +- 5 files changed, 92 insertions(+), 86 deletions(-) diff --git a/rmf_site_editor/src/interaction/assets.rs b/rmf_site_editor/src/interaction/assets.rs index 12bb8c12..a6c452dd 100644 --- a/rmf_site_editor/src/interaction/assets.rs +++ b/rmf_site_editor/src/interaction/assets.rs @@ -82,6 +82,7 @@ impl InteractionAssets { material: material_set.passive.clone(), ..default() }) + .set_parent(parent) .id(); if let Some(for_entity) = for_entity_opt { @@ -89,7 +90,6 @@ impl InteractionAssets { .entity(child_entity) .insert(DragAxisBundle::new(for_entity, Vec3::Z).with_materials(material_set)); } - commands.entity(parent).add_child(child_entity); child_entity } @@ -126,8 +126,8 @@ impl InteractionAssets { let drag_parent = commands .spawn(SpatialBundle::default()) .insert(VisualCue::no_outline().irregular().always_xray()) + .set_parent(anchor) .id(); - commands.entity(anchor).add_child(drag_parent); let height = 0.0; let scale = 0.2; diff --git a/rmf_site_editor/src/site/lane.rs b/rmf_site_editor/src/site/lane.rs index 42aedced..aec379ea 100644 --- a/rmf_site_editor/src/site/lane.rs +++ b/rmf_site_editor/src/site/lane.rs @@ -125,29 +125,30 @@ pub fn add_lane_visuals( transform: Transform::from_xyz(0.0, 0.0, height), ..default() }) + .set_parent(e) .id(); let mut spawn_lane_mesh_and_outline = |lane_tf, lane_mesh, outline_mesh| { - let outline = commands + let mesh = commands .spawn(PbrBundle { - mesh: outline_mesh, - transform: Transform::from_translation(-0.000_5 * Vec3::Z), - visibility: Visibility { is_visible: false }, + mesh: lane_mesh, + material: lane_material.clone(), + transform: lane_tf, ..default() }) + .set_parent(layer) .id(); - let mesh = commands + let outline = commands .spawn(PbrBundle { - mesh: lane_mesh, - material: lane_material.clone(), - transform: lane_tf, + mesh: outline_mesh, + transform: Transform::from_translation(-0.000_5 * Vec3::Z), + visibility: Visibility { is_visible: false }, ..default() }) - .add_child(outline) + .set_parent(mesh) .id(); - commands.entity(layer).add_child(mesh); (mesh, outline) }; @@ -184,8 +185,7 @@ pub fn add_lane_visuals( ..default() }) .insert(Category::Lane) - .insert(EdgeLabels::StartEnd) - .add_child(layer); + .insert(EdgeLabels::StartEnd); } } diff --git a/rmf_site_editor/src/site/load.rs b/rmf_site_editor/src/site/load.rs index 3b8dc767..7523144f 100644 --- a/rmf_site_editor/src/site/load.rs +++ b/rmf_site_editor/src/site/load.rs @@ -87,104 +87,109 @@ fn generate_site_entities( let anchor_entity = commands .spawn(AnchorBundle::new(anchor.clone())) .insert(SiteID(*anchor_id)) + .set_parent(site_id) .id(); id_to_entity.insert(*anchor_id, anchor_entity); consider_id(*anchor_id); - commands.entity(site_id).add_child(anchor_entity); } for (group_id, group) in &site_data.fiducial_groups { - let group_entity = commands.spawn(group.clone()).insert(SiteID(*group_id)).id(); + let group_entity = commands + .spawn(group.clone()) + .insert(SiteID(*group_id)) + .set_parent(site_id) + .id(); id_to_entity.insert(*group_id, group_entity); consider_id(*group_id); - commands.entity(site_id).add_child(group_entity); } for (group_id, group) in &site_data.textures { - let group_entity = commands.spawn(group.clone()).insert(SiteID(*group_id)).id(); + let group_entity = commands + .spawn(group.clone()) + .insert(SiteID(*group_id)) + .set_parent(site_id) + .id(); id_to_entity.insert(*group_id, group_entity); consider_id(*group_id); - commands.entity(site_id).add_child(group_entity); } for (level_id, level_data) in &site_data.levels { - let level_entity = commands.spawn(SiteID(*level_id)).id(); + let level_entity = commands.spawn(SiteID(*level_id)).set_parent(site_id).id(); for (anchor_id, anchor) in &level_data.anchors { let anchor_entity = commands .spawn(AnchorBundle::new(anchor.clone())) .insert(SiteID(*anchor_id)) + .set_parent(level_entity) .id(); id_to_entity.insert(*anchor_id, anchor_entity); consider_id(*anchor_id); - commands.entity(level_entity).add_child(anchor_entity); } for (door_id, door) in &level_data.doors { let door_entity = commands .spawn(door.convert(&id_to_entity).for_site(site_id)?) .insert(SiteID(*door_id)) + .set_parent(level_entity) .id(); id_to_entity.insert(*door_id, door_entity); consider_id(*door_id); - commands.entity(level_entity).add_child(door_entity); } for (drawing_id, drawing) in &level_data.drawings { let drawing_entity = commands .spawn(DrawingBundle::new(drawing.properties.clone())) .insert(SiteID(*drawing_id)) + .set_parent(level_entity) .id(); for (anchor_id, anchor) in &drawing.anchors { let anchor_entity = commands .spawn(AnchorBundle::new(anchor.clone())) .insert(SiteID(*anchor_id)) + .set_parent(drawing_entity) .id(); id_to_entity.insert(*anchor_id, anchor_entity); consider_id(*anchor_id); - commands.entity(drawing_entity).add_child(anchor_entity); } for (fiducial_id, fiducial) in &drawing.fiducials { - let fiducial_entity = commands + commands .spawn(fiducial.convert(&id_to_entity).for_site(site_id)?) .insert(SiteID(*fiducial_id)) + .set_parent(drawing_entity) .id(); consider_id(*fiducial_id); - commands.entity(drawing_entity).add_child(fiducial_entity); } for (measurement_id, measurement) in &drawing.measurements { - let measurement_entity = commands + commands .spawn(measurement.convert(&id_to_entity).for_site(site_id)?) .insert(SiteID(*measurement_id)) + .set_parent(drawing_entity) .id(); consider_id(*measurement_id); - commands - .entity(drawing_entity) - .add_child(measurement_entity); } consider_id(*drawing_id); } for (floor_id, floor) in &level_data.floors { - let floor_entity = commands + commands .spawn(floor.convert(&id_to_entity).for_site(site_id)?) .insert(SiteID(*floor_id)) + .set_parent(level_entity) .id(); consider_id(*floor_id); - commands.entity(level_entity).add_child(floor_entity); } for (wall_id, wall) in &level_data.walls { - let wall_entity = commands + commands .spawn(wall.convert(&id_to_entity).for_site(site_id)?) .insert(SiteID(*wall_id)) + .set_parent(level_entity) .id(); consider_id(*wall_id); - commands.entity(level_entity).add_child(wall_entity); } commands @@ -228,11 +233,10 @@ fn generate_site_entities( ); id_to_entity.insert(*level_id, level_entity); consider_id(*level_id); - commands.entity(site_id).add_child(level_entity); } for (lift_id, lift_data) in &site_data.lifts { - let lift_entity = commands.spawn(SiteID(*lift_id)).id(); + let lift_entity = commands.spawn(SiteID(*lift_id)).set_parent(site_id).id(); commands.entity(lift_entity).with_children(|lift| { lift.spawn(SpatialBundle::default()) @@ -253,10 +257,10 @@ fn generate_site_entities( let door_entity = commands .spawn(door.convert(&id_to_entity).for_site(site_id)?) .insert(Dependents::single(lift_entity)) + .set_parent(lift_entity) .id(); id_to_entity.insert(*door_id, door_entity); consider_id(*door_id); - commands.entity(lift_entity).add_child(door_entity); } commands.entity(lift_entity).insert(Category::Lift).insert( @@ -268,17 +272,16 @@ fn generate_site_entities( id_to_entity.insert(*lift_id, lift_entity); consider_id(*lift_id); - commands.entity(site_id).add_child(lift_entity); } for (fiducial_id, fiducial) in &site_data.fiducials { let fiducial_entity = commands .spawn(fiducial.convert(&id_to_entity).for_site(site_id)?) .insert(SiteID(*fiducial_id)) + .set_parent(site_id) .id(); id_to_entity.insert(*fiducial_id, fiducial_entity); consider_id(*fiducial_id); - commands.entity(site_id).add_child(fiducial_entity); } for (nav_graph_id, nav_graph_data) in &site_data.navigation.guided.graphs { @@ -286,30 +289,30 @@ fn generate_site_entities( .spawn(SpatialBundle::default()) .insert(nav_graph_data.clone()) .insert(SiteID(*nav_graph_id)) + .set_parent(site_id) .id(); id_to_entity.insert(*nav_graph_id, nav_graph); consider_id(*nav_graph_id); - commands.entity(site_id).add_child(nav_graph); } for (lane_id, lane_data) in &site_data.navigation.guided.lanes { let lane = commands .spawn(lane_data.convert(&id_to_entity).for_site(site_id)?) .insert(SiteID(*lane_id)) + .set_parent(site_id) .id(); id_to_entity.insert(*lane_id, lane); consider_id(*lane_id); - commands.entity(site_id).add_child(lane); } for (location_id, location_data) in &site_data.navigation.guided.locations { let location = commands .spawn(location_data.convert(&id_to_entity).for_site(site_id)?) .insert(SiteID(*location_id)) + .set_parent(site_id) .id(); id_to_entity.insert(*location_id, location); consider_id(*location_id); - commands.entity(site_id).add_child(location); } let nav_graph_rankings = match RecencyRanking::::from_u32( diff --git a/rmf_site_editor/src/site/model.rs b/rmf_site_editor/src/site/model.rs index 9dd05c25..6279ded1 100644 --- a/rmf_site_editor/src/site/model.rs +++ b/rmf_site_editor/src/site/model.rs @@ -108,53 +108,58 @@ pub fn handle_model_loaded_events( .as_ref() .map(|s| s.clone()) .unwrap_or(gltf.scenes.get(0).unwrap().clone()); - let scene_id = commands - .spawn(SceneBundle { - scene, - transform: Transform::from_scale(**scale), - ..default() - }) - .id(); - commands.entity(e).add_child(scene_id); - Some(scene_id) + Some( + commands + .spawn(SceneBundle { + scene, + transform: Transform::from_scale(**scale), + ..default() + }) + .set_parent(e) + .id(), + ) } else if scenes.contains(&h.typed_weak::()) { let h_typed = h.0.clone().typed::(); - let scene_id = commands - .spawn(SceneBundle { - scene: h_typed, - transform: Transform::from_scale(**scale), - ..default() - }) - .id(); - commands.entity(e).add_child(scene_id); - Some(scene_id) + Some( + commands + .spawn(SceneBundle { + scene: h_typed, + transform: Transform::from_scale(**scale), + ..default() + }) + .set_parent(e) + .id(), + ) } else if meshes.contains(&h.typed_weak::()) { let h_typed = h.0.clone().typed::(); - let mesh_id = commands - .spawn(PbrBundle { - mesh: h_typed, - material: site_assets.default_mesh_grey_material.clone(), - transform: Transform::from_scale(**scale), - ..default() - }) - .id(); - commands.entity(e).add_child(mesh_id); - Some(mesh_id) + Some( + commands + .spawn(PbrBundle { + mesh: h_typed, + material: site_assets.default_mesh_grey_material.clone(), + transform: Transform::from_scale(**scale), + ..default() + }) + .set_parent(e) + .id(), + ) } else if let Some(urdf) = urdfs.get(&h.typed_weak::()) { - let urdf_id = commands - .spawn(SpatialBundle::VISIBLE_IDENTITY) - .insert(urdf.clone()) - .insert(Category::Workcell) - .id(); - commands.entity(e).add_child(urdf_id); - Some(urdf_id) + Some( + commands + .spawn(SpatialBundle::VISIBLE_IDENTITY) + .insert(urdf.clone()) + .insert(Category::Workcell) + .set_parent(e) + .id(), + ) } else if let Some(sdf) = sdfs.get(&h.typed_weak::()) { - let sdf_id = commands - .spawn(SpatialBundle::VISIBLE_IDENTITY) - .insert(sdf.clone()) - .id(); - commands.entity(e).add_child(sdf_id); - Some(sdf_id) + Some( + commands + .spawn(SpatialBundle::VISIBLE_IDENTITY) + .insert(sdf.clone()) + .set_parent(e) + .id(), + ) } else { None }; diff --git a/rmf_site_editor/src/site/site.rs b/rmf_site_editor/src/site/site.rs index 2ab5ddcd..0e3e9f6e 100644 --- a/rmf_site_editor/src/site/site.rs +++ b/rmf_site_editor/src/site/site.rs @@ -124,12 +124,10 @@ pub fn change_site( global_floor_visibility: default(), global_drawing_visibility: default(), }) + .set_parent(cmd.site) .id(); - commands - .entity(cmd.site) - .insert(CachedLevel(new_level)) - .add_child(new_level); + commands.entity(cmd.site).insert(CachedLevel(new_level)); current_level.0 = Some(new_level); } }