Skip to content

Work in progress using the new resource retriever apis #1262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions rviz_common/src/rviz_common/load_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,11 @@
namespace rviz_common
{

resource_retriever::MemoryResource getResource(const std::string & resource_path)
resource_retriever::ResourceSharedPtr getResource(const std::string & resource_path)
{
RVIZ_COMMON_LOG_DEBUG("rviz_common::getResource() loading resource: " + resource_path);
resource_retriever::Retriever retriever;
resource_retriever::MemoryResource res;
try {
res = retriever.get(resource_path);
} catch (resource_retriever::Exception & e) {
RVIZ_COMMON_LOG_DEBUG(e.what());
return resource_retriever::MemoryResource();
}

return res;
return retriever.get_shared(resource_path);
}

QPixmap loadPixmap(QString url, bool fill_cache)
Expand All @@ -73,8 +66,8 @@ QPixmap loadPixmap(QString url, bool fill_cache)
RVIZ_COMMON_LOG_DEBUG("Load pixmap at " + url.toStdString());

auto image = getResource(url.toStdString());
if (image.size != 0) {
if (!pixmap.loadFromData(image.data.get(), static_cast<uint32_t>(image.size))) {
if (image != nullptr && !image->data.empty()) {
if (!pixmap.loadFromData(image->data.data(), static_cast<uint32_t>(image->data.size()))) {
RVIZ_COMMON_LOG_ERROR("Could not load pixmap " + url.toStdString());
}
}
Expand Down
5 changes: 4 additions & 1 deletion rviz_default_plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ find_package(pluginlib REQUIRED)
find_package(point_cloud_transport REQUIRED)
find_package(rclcpp REQUIRED)
find_package(resource_retriever REQUIRED)
find_package(rviz_resource_interfaces REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(tf2 REQUIRED)
find_package(tf2_geometry_msgs REQUIRED)
Expand Down Expand Up @@ -230,6 +231,7 @@ set(rviz_default_plugins_source_files
src/rviz_default_plugins/view_controllers/orbit/orbit_view_controller.cpp
src/rviz_default_plugins/view_controllers/ortho/fixed_orientation_ortho_view_controller.cpp
src/rviz_default_plugins/view_controllers/xy_orbit/xy_orbit_view_controller.cpp
src/rviz_default_plugins/ros_resource_retriever.cpp
)

add_library(rviz_default_plugins SHARED
Expand Down Expand Up @@ -262,11 +264,12 @@ target_link_libraries(rviz_default_plugins PUBLIC
tf2_ros::tf2_ros
urdf::urdf
${visualization_msgs_TARGETS}
resource_retriever::resource_retriever
${rviz_resource_interfaces_TARGETS}
)

target_link_libraries(rviz_default_plugins PRIVATE
gz-math::core
resource_retriever::resource_retriever
)

# Causes the visibility macros to use dllexport rather than dllimport,
Expand Down
6 changes: 6 additions & 0 deletions rviz_default_plugins/icons/classes/AxesColor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions rviz_default_plugins/icons/classes/AxisColor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions rviz_default_plugins/icons/classes/FlatColor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions rviz_default_plugins/icons/classes/Intensity.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions rviz_default_plugins/icons/classes/RGB8.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions rviz_default_plugins/icons/classes/RGBF32.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions rviz_default_plugins/icons/classes/XYZ.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

#include <QString> // NOLINT: cpplint is unable to handle the include order here

#include "resource_retriever/retriever.hpp"
#include "visualization_msgs/msg/marker.hpp"
#include "visualization_msgs/msg/marker_array.hpp"

Expand Down Expand Up @@ -122,6 +123,8 @@ class RVIZ_DEFAULT_PLUGINS_PUBLIC MarkerCommon
void setMarkerStatus(MarkerID id, StatusLevel level, const std::string & text);
void deleteMarkerStatus(MarkerID id);

resource_retriever::Retriever * getResourceRetriever();

private:
/** @brief Delete all the markers within the given namespace. */
void deleteMarkersInNamespace(const std::string & ns);
Expand Down Expand Up @@ -175,6 +178,8 @@ class RVIZ_DEFAULT_PLUGINS_PUBLIC MarkerCommon
rviz_common::DisplayContext * context_;
Ogre::SceneNode * scene_node_;

resource_retriever::Retriever retriever_;

friend class MarkerNamespace;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@

#include <urdf/model.hpp> // can be replaced later by urdf_model/types.h

#include "rviz_rendering/objects/object.hpp"
#include "resource_retriever/retriever.hpp"
#include "rviz_common/interaction/forwards.hpp"
#include "rviz_rendering/objects/object.hpp"

#include "rviz_default_plugins/robot/robot_element_base_class.hpp"
#include "rviz_default_plugins/visibility_control.hpp"
Expand Down Expand Up @@ -235,6 +236,8 @@ private Q_SLOTS:
Ogre::SceneManager * scene_manager_;
rviz_common::DisplayContext * context_;

mutable resource_retriever::Retriever retriever_;

std::string parent_joint_name_;
std::vector<std::string> child_joint_names_;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2025 Open Source Robotics Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef RVIZ_DEFAULT_PLUGINS__ROS_RESOURCE_RETRIEVER_HPP_
#define RVIZ_DEFAULT_PLUGINS__ROS_RESOURCE_RETRIEVER_HPP_

#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>

#include <rclcpp/callback_group.hpp>
#include <rclcpp/client.hpp>
#include <rclcpp/executors/single_threaded_executor.hpp>
#include <rclcpp/logger.hpp>
#include <rclcpp/node.hpp>
#include <resource_retriever/memory_resource.hpp>
#include <resource_retriever/plugins/retriever_plugin.hpp>
#include <rviz_common/ros_integration/ros_node_abstraction_iface.hpp>
#include <rviz_resource_interfaces/srv/get_resource.hpp>

/// Plugin for resource_retriever that loads resources from a ROS interface.
class RosResourceRetriever : public resource_retriever::plugins::RetrieverPlugin
{
using GetResource = rviz_resource_interfaces::srv::GetResource;

RosResourceRetriever() = delete;

static constexpr std::string_view service_name = "/rviz/get_resource";

public:
explicit RosResourceRetriever(
rviz_common::ros_integration::RosNodeAbstractionIface::WeakPtr weak_ros_iface);

~RosResourceRetriever() override = default;

std::string name() override;

bool can_handle(const std::string & url) override;

resource_retriever::ResourceSharedPtr get_shared(const std::string & url) override;

private:
// It should be safe to keep a shared pointer to the node here, because this
// plugin will be destroyed with the resource retriever in the marker display,
// which should be destroyed along before the node abstraction is destroyed.
// Also, since we're keeping callback groups and clients around, we need to
// ensure the node stays around too.
rclcpp::Node::SharedPtr ros_node_;
rclcpp::CallbackGroup::SharedPtr callback_group_;
rclcpp::Client<GetResource>::SharedPtr client_;
rclcpp::executors::SingleThreadedExecutor executor_;
rclcpp::Logger logger_;

// Map of the resource path to a pair with the etag value and the memory resource that is cached.
std::unordered_map<
std::string,
std::pair<std::string, resource_retriever::ResourceSharedPtr>
> cached_resources_;
};

#endif // RVIZ_DEFAULT_PLUGINS__ROS_RESOURCE_RETRIEVER_HPP_
1 change: 1 addition & 0 deletions rviz_default_plugins/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<depend>point_cloud_transport</depend>
<depend>rclcpp</depend>
<depend>resource_retriever</depend>
<depend>rviz_resource_interfaces</depend>
<depend>rviz_common</depend>
<depend>rviz_rendering</depend>
<depend>tf2</depend>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.


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

#include <cinttypes>
#include <memory>
#include <set>
#include <sstream>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include <QString> // NOLINT: cpplint is unable to handle the include order here
#include "QString"

#include "rclcpp/duration.hpp"

Expand All @@ -49,6 +50,8 @@

#include "rviz_default_plugins/displays/marker/markers/marker_factory.hpp"

#include "rviz_default_plugins/ros_resource_retriever.hpp"

namespace rviz_default_plugins
{
namespace displays
Expand All @@ -72,6 +75,13 @@ void MarkerCommon::initialize(rviz_common::DisplayContext * context, Ogre::Scene
context_ = context;
scene_node_ = scene_node;

resource_retriever::RetrieverVec plugins;
plugins.push_back(std::make_shared<RosResourceRetriever>(context_->getRosNodeAbstraction()));
for (const auto & plugin : resource_retriever::default_plugins()) {
plugins.push_back(plugin);
}
retriever_ = resource_retriever::Retriever(plugins);

namespace_config_enabled_state_.clear();

marker_factory_->initialize(this, context_, scene_node_);
Expand Down Expand Up @@ -148,6 +158,11 @@ void MarkerCommon::deleteMarkerStatus(MarkerID id)
display_->deleteStatusStd(marker_name);
}

resource_retriever::Retriever * MarkerCommon::getResourceRetriever()
{
return &this->retriever_;
}

void MarkerCommon::addMessage(const visualization_msgs::msg::Marker::ConstSharedPtr marker)
{
std::unique_lock<std::mutex> lock(queue_mutex_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ void MeshResourceMarker::onNewMessage(
return;
}

if (!rviz_rendering::loadMeshFromResource(new_message->mesh_resource)) {
if (
!rviz_rendering::loadMeshFromResource(
owner_->getResourceRetriever(),
new_message->mesh_resource))
{
printMeshLoadingError(new_message);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
#include <OgreTextureManager.h>
#include <OgreTechnique.h>

#include "resource_retriever/retriever.hpp"
#include "rviz_rendering/mesh_loader.hpp"
#include "rviz_rendering/material_manager.hpp"
#include "rviz_common/display_context.hpp"
Expand Down
Loading