Skip to content

Commit cfa75db

Browse files
Changed Marker Displays to allow toggling visibility of namespaces (#1402)
Co-authored-by: Alejandro Hernandez Cordero <[email protected]>
1 parent d3eabab commit cfa75db

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

rviz_default_plugins/include/rviz_default_plugins/displays/marker/marker_common.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class RVIZ_DEFAULT_PLUGINS_PUBLIC MarkerCommon
126126
resource_retriever::Retriever * getResourceRetriever();
127127

128128
private:
129+
/** @brief Change the visibility for all markers in the given namespace. */
130+
void setVisibilityForMarkersInNamespace(const std::string & ns, bool visible);
129131
/** @brief Delete all the markers within the given namespace. */
130132
void deleteMarkersInNamespace(const std::string & ns);
131133

@@ -167,7 +169,7 @@ class RVIZ_DEFAULT_PLUGINS_PUBLIC MarkerCommon
167169
typedef QHash<QString, MarkerNamespace *> M_Namespace;
168170
M_Namespace namespaces_;
169171

170-
rviz_common::properties::Property * namespaces_category_;
172+
rviz_common::properties::BoolProperty * namespaces_category_;
171173

172174
typedef std::map<QString, bool> M_EnabledState;
173175
M_EnabledState namespace_config_enabled_state_;
@@ -180,6 +182,8 @@ class RVIZ_DEFAULT_PLUGINS_PUBLIC MarkerCommon
180182

181183
resource_retriever::Retriever retriever_;
182184

185+
bool all_namespaces_enabled_ = true;
186+
183187
friend class MarkerNamespace;
184188
};
185189

rviz_default_plugins/include/rviz_default_plugins/displays/marker/markers/marker_base.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ class RVIZ_DEFAULT_PLUGINS_PUBLIC MarkerBase
122122

123123
virtual S_MaterialPtr getMaterials() {return S_MaterialPtr();}
124124

125+
bool isVisible() const;
126+
127+
void setVisible(bool visible);
128+
125129
protected:
126130
bool transform(
127131
const MarkerConstSharedPtr & message,
@@ -144,6 +148,8 @@ class RVIZ_DEFAULT_PLUGINS_PUBLIC MarkerBase
144148
rclcpp::Time expiration_;
145149

146150
std::shared_ptr<MarkerSelectionHandler> handler_;
151+
152+
bool visible_ = true;
147153
};
148154

149155
} // namespace markers

rviz_default_plugins/src/rviz_default_plugins/displays/marker/marker_common.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ namespace displays
6060
MarkerCommon::MarkerCommon(rviz_common::Display * display)
6161
: display_(display)
6262
{
63-
namespaces_category_ = new rviz_common::properties::Property(
64-
"Namespaces", QVariant(), "", display_);
63+
namespaces_category_ = new rviz_common::properties::BoolProperty(
64+
"Namespaces", true, "Toggle to toggle all namespaces", display_);
6565
marker_factory_ = std::make_unique<markers::MarkerFactory>();
6666
}
6767

@@ -118,6 +118,15 @@ void MarkerCommon::deleteMarker(MarkerID id)
118118
}
119119
}
120120

121+
void MarkerCommon::setVisibilityForMarkersInNamespace(const std::string & ns, bool visible)
122+
{
123+
for (auto const & marker : markers_) {
124+
if (marker.first.first == ns) {
125+
marker.second->setVisible(visible);
126+
}
127+
}
128+
}
129+
121130
void MarkerCommon::deleteMarkersInNamespace(const std::string & ns)
122131
{
123132
std::vector<MarkerID> to_delete;
@@ -273,12 +282,6 @@ QHash<QString, MarkerNamespace *>::const_iterator MarkerCommon::getMarkerNamespa
273282

274283
void MarkerCommon::processAdd(const visualization_msgs::msg::Marker::ConstSharedPtr message)
275284
{
276-
auto ns_it = getMarkerNamespace(message);
277-
278-
if (!ns_it.value()->isEnabled() ) {
279-
return;
280-
}
281-
282285
deleteMarkerStatus(MarkerID(message->ns, message->id));
283286

284287
MarkerBasePtr marker = createOrGetOldMarker(message);
@@ -320,6 +323,10 @@ void MarkerCommon::configureMarker(
320323
{
321324
marker->setMessage(message);
322325

326+
// Visibility needs to be set after changes to the marker as they might make it visible again
327+
auto ns_it = getMarkerNamespace(message);
328+
marker->setVisible(ns_it.value()->isEnabled());
329+
323330
if (rclcpp::Duration(message->lifetime).nanoseconds() > 100000) {
324331
markers_with_expiration_.insert(marker);
325332
}
@@ -347,6 +354,14 @@ void MarkerCommon::update(float wall_dt, float ros_dt)
347354
processNewMessages(local_queue);
348355
removeExpiredMarkers();
349356
updateMarkersWithLockedFrame();
357+
358+
// Workaround because this is not a QObject
359+
if (all_namespaces_enabled_ != namespaces_category_->getBool()) {
360+
all_namespaces_enabled_ = namespaces_category_->getBool();
361+
for (auto const & ns : namespaces_) {
362+
ns->setValue(all_namespaces_enabled_);
363+
}
364+
}
350365
}
351366

352367
MarkerCommon::V_MarkerMessage MarkerCommon::takeSnapshotOfMessageQueue()
@@ -401,9 +416,7 @@ MarkerNamespace::MarkerNamespace(
401416

402417
void MarkerNamespace::onEnableChanged()
403418
{
404-
if (!isEnabled()) {
405-
owner_->deleteMarkersInNamespace(getName().toStdString());
406-
}
419+
owner_->setVisibilityForMarkersInNamespace(getName().toStdString(), isEnabled());
407420

408421
// Update the configuration that stores the enabled state of all markers
409422
owner_->namespace_config_enabled_state_[getName()] = isEnabled();

rviz_default_plugins/src/rviz_default_plugins/displays/marker/markers/marker_base.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ const Ogre::Quaternion & MarkerBase::getOrientation()
155155
return scene_node_->getOrientation();
156156
}
157157

158+
bool MarkerBase::isVisible() const
159+
{
160+
return visible_;
161+
}
162+
163+
void MarkerBase::setVisible(bool visible)
164+
{
165+
visible_ = visible;
166+
scene_node_->setVisible(visible);
167+
}
168+
158169
void MarkerBase::extractMaterials(Ogre::Entity * entity, S_MaterialPtr & materials)
159170
{
160171
uint64_t num_sub_entities = entity->getNumSubEntities();

rviz_default_plugins/test/rviz_default_plugins/displays/marker/marker_common_test.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,19 @@ TEST_F(MarkerCommonFixture, onEnableChanged_in_namespace_removes_all_markers_in_
498498
marker->ns = "new_ns";
499499
common_->processMessage(marker);
500500

501-
EXPECT_TRUE(rviz_default_plugins::findOnePointCloud(scene_manager_->getRootSceneNode()));
501+
auto pointcloud = rviz_default_plugins::findOnePointCloud(scene_manager_->getRootSceneNode());
502+
ASSERT_TRUE(pointcloud);
503+
EXPECT_TRUE(pointcloud->getVisible());
502504
EXPECT_TRUE(rviz_default_plugins::findOneMovableText(scene_manager_->getRootSceneNode()));
503505

504506
auto namespace_property = dynamic_cast<rviz_default_plugins::displays::MarkerNamespace *>(
505507
display_->findProperty("Namespaces")->childAt(0));
506508
namespace_property->setValue(false);
507509

508-
EXPECT_FALSE(rviz_default_plugins::findOnePointCloud(scene_manager_->getRootSceneNode()));
510+
// Pointcloud should still exist but not visible
511+
pointcloud = rviz_default_plugins::findOnePointCloud(scene_manager_->getRootSceneNode());
512+
ASSERT_TRUE(pointcloud != nullptr);
513+
EXPECT_FALSE(pointcloud->getVisible());
509514
EXPECT_TRUE(rviz_default_plugins::findOneMovableText(scene_manager_->getRootSceneNode()));
510515
}
511516

@@ -518,15 +523,18 @@ TEST_F(MarkerCommonFixture, processMessage_does_not_add_message_with_disabled_na
518523
// this is necessary to initialize namespace as we don't load a config
519524
common_->processMessage(marker);
520525

521-
EXPECT_TRUE(rviz_default_plugins::findOnePointCloud(scene_manager_->getRootSceneNode()));
526+
auto pointclouds = rviz_default_plugins::findAllPointClouds(scene_manager_->getRootSceneNode());
527+
ASSERT_EQ(pointclouds.size(), 1);
528+
EXPECT_TRUE(pointclouds[0]->getVisible());
522529

523530
auto namespace_property = dynamic_cast<rviz_default_plugins::displays::MarkerNamespace *>(
524531
display_->findProperty("Namespaces")->childAt(0));
525532
namespace_property->setValue(false);
526533

527-
marker->type = visualization_msgs::msg::Marker::TEXT_VIEW_FACING;
528534
common_->processMessage(marker);
529535

530-
EXPECT_FALSE(rviz_default_plugins::findOnePointCloud(scene_manager_->getRootSceneNode()));
531-
EXPECT_FALSE(rviz_default_plugins::findOneMovableText(scene_manager_->getRootSceneNode()));
536+
// Pointcloud should still exist but not visible
537+
pointclouds = rviz_default_plugins::findAllPointClouds(scene_manager_->getRootSceneNode());
538+
ASSERT_EQ(pointclouds.size(), 1);
539+
EXPECT_FALSE(pointclouds[0]->getVisible());
532540
}

0 commit comments

Comments
 (0)