Skip to content

Commit

Permalink
Add configurable wireframe visualization
Browse files Browse the repository at this point in the history
Signed-off-by: Luca Della Vedova <lucadv@intrinsic.ai>
  • Loading branch information
luca-della-vedova committed Sep 5, 2023
1 parent fbc5fbf commit 530ce1b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 18 deletions.
4 changes: 4 additions & 0 deletions rmf_site_editor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ pub mod site_asset_io;
pub mod urdf_loader;
use sdf_loader::*;

pub mod wireframe;
use wireframe::*;

use aabb::AabbUpdatePlugin;
use animate::AnimationPlugin;
use interaction::InteractionPlugin;
Expand Down Expand Up @@ -222,6 +225,7 @@ impl Plugin for SiteEditor {
.add_plugin(InteractionPlugin)
.add_plugin(StandardUiLayout)
.add_plugin(AnimationPlugin)
.add_plugin(SiteWireframePlugin)
.add_plugin(OccupancyPlugin)
.add_plugin(WorkspacePlugin)
// Note order matters, issue and OSMView plugins must be initialized after the UI
Expand Down
92 changes: 92 additions & 0 deletions rmf_site_editor/src/wireframe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (C) 2023 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

use crate::menu_bar::{MenuEvent, MenuItem, ViewMenu};
use bevy::pbr::wireframe::{Wireframe, WireframePlugin};
use bevy::prelude::*;

use rmf_site_format::ModelMarker;

#[derive(Default)]
pub struct SiteWireframePlugin;

/// Keeps track of which entity is associated to the toggle menu button
#[derive(Resource)]
pub struct WireframeMenu {
toggle_wireframe: Entity,
}

impl FromWorld for WireframeMenu {
fn from_world(world: &mut World) -> Self {
// Tools menu
let toggle_wireframe = world
.spawn(MenuItem::CheckBox("View wireframe".to_string(), false))
.id();

let view_header = world.resource::<ViewMenu>().get();
world
.entity_mut(view_header)
.push_children(&[toggle_wireframe]);

WireframeMenu { toggle_wireframe }
}
}

fn handle_wireframe_menu_events(
mut commands: Commands,
mut menu_events: EventReader<MenuEvent>,
mut menu_items: Query<&mut MenuItem>,
wireframe_menu: Res<WireframeMenu>,
meshes: Query<Entity, With<Handle<Mesh>>>,
parents: Query<&Parent>,
children: Query<&Children>,
models: Query<Entity, With<ModelMarker>>,
) {
for event in menu_events.iter() {
if event.clicked() && event.source() == wireframe_menu.toggle_wireframe {
let Ok(mut checkbox) = menu_items.get_mut(wireframe_menu.toggle_wireframe) else {
error!("Wireframe button not found");
return;
};
let MenuItem::CheckBox(_, ref mut enable) = *checkbox else {
error!("Mismatch for wireframe toggle menu type, expected checkbox");
return;
};
*enable = !*enable;
for model in models.iter() {
// Now go through all the model children and toggle wireframe on meshes
for c in DescendantIter::new(&children, model) {
if meshes.get(c).is_ok() {
if *enable {
commands.entity(c).insert(Wireframe);
} else {
commands.entity(c).remove::<Wireframe>();
}
}
}
}
}
}
}

impl Plugin for SiteWireframePlugin {
fn build(&self, app: &mut App) {
app.init_resource::<WireframeMenu>()
.add_plugin(WireframePlugin)
.add_system(handle_wireframe_menu_events);
}
}
18 changes: 0 additions & 18 deletions rmf_site_editor/src/workcell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ pub use workcell::*;
pub mod urdf;
pub use urdf::*;

use bevy::pbr::wireframe::{Wireframe, WireframePlugin};
use bevy::render::{render_resource::WgpuFeatures, settings::WgpuSettings};
use bevy::{prelude::*, render::view::visibility::VisibilitySystems, transform::TransformSystem};
use bevy_infinite_grid::{InfiniteGrid, InfiniteGridBundle, InfiniteGridPlugin};
Expand Down Expand Up @@ -68,29 +67,13 @@ fn delete_grid(mut commands: Commands, grids: Query<Entity, With<InfiniteGrid>>)
}
}

fn add_wireframe_to_meshes(
mut commands: Commands,
new_meshes: Query<Entity, Added<Handle<Mesh>>>,
parents: Query<&Parent>,
models: Query<Entity, With<ModelMarker>>,
) {
for e in new_meshes.iter() {
for ancestor in AncestorIter::new(&parents, e) {
if let Ok(_) = models.get(ancestor) {
commands.entity(e).insert(Wireframe);
}
}
}
}

impl Plugin for WorkcellEditorPlugin {
fn build(&self, app: &mut App) {
app.add_plugin(InfiniteGridPlugin)
.insert_resource(WgpuSettings {
features: WgpuFeatures::POLYGON_MODE_LINE,
..default()
})
.add_plugin(WireframePlugin)
.add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
.add_plugin(RapierDebugRenderPlugin::default())
.add_event::<SaveWorkcell>()
Expand All @@ -100,7 +83,6 @@ impl Plugin for WorkcellEditorPlugin {
.add_system_set(SystemSet::on_exit(AppState::WorkcellEditor).with_system(delete_grid))
.add_system_set(
SystemSet::on_update(AppState::WorkcellEditor)
.with_system(add_wireframe_to_meshes)
.with_system(update_constraint_dependents)
.with_system(handle_model_loaded_events)
.with_system(update_model_scenes)
Expand Down

0 comments on commit 530ce1b

Please sign in to comment.