Skip to content

Commit

Permalink
Added ZSTD plugin (#12) (#16)
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Hernández Cordero <[email protected]>
  • Loading branch information
ahcorde authored Aug 28, 2023
1 parent 5472ea0 commit 156b65e
Show file tree
Hide file tree
Showing 9 changed files with 494 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Currently provided are:

- [draco_point_cloud_transport](https://github.com/ros-perception/point_cloud_transport_plugins/tree/rolling/draco_point_cloud_transport) - A library using Google Draco to compress the pointclouds.

- [zlib_point_cloud_transport](https://github.com/ros-perception/point_cloud_transport_plugins/tree/rolling/zlib_point_cloud_transport) - A library using zlib to compress the pointclouds.
- [zlib_point_cloud_transport](https://github.com/ros-perception/point_cloud_transport_plugins/tree/rolling/zlib_point_cloud_transport) - A libraory using zlib to compress the pointclouds.

- [zstd_point_cloud_transport](https://github.com/ros-perception/point_cloud_transport_plugins/tree/master/zstd_point_cloud_transport) - A library using ZSTD to compress the pointclouds.

More transports can be added. Pull requests are welcome!
54 changes: 54 additions & 0 deletions zstd_point_cloud_transport/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
cmake_minimum_required(VERSION 3.10.2)

set(CMAKE_CXX_STANDARD 17)

project(zstd_point_cloud_transport)

find_package(ament_cmake REQUIRED)
find_package(pluginlib REQUIRED)
find_package(point_cloud_interfaces REQUIRED)
find_package(point_cloud_transport REQUIRED)
find_package(rclcpp REQUIRED)

set(dependencies
pluginlib
point_cloud_interfaces
point_cloud_transport
rclcpp
)

include_directories(include)

add_library(${PROJECT_NAME}
SHARED
src/zstd_publisher.cpp
src/zstd_subscriber.cpp
src/manifest.cpp
)

target_link_libraries(${PROJECT_NAME} zstd)

ament_target_dependencies(${PROJECT_NAME} ${dependencies})

install(TARGETS ${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION lib/${PROJECT_NAME}
)

install(
DIRECTORY include/${PROJECT_NAME}/
DESTINATION include/${PROJECT_NAME}
)

pluginlib_export_plugin_description_file(point_cloud_transport zstd_plugins.xml)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

ament_export_include_directories(include)
ament_export_libraries(${PROJECT_NAME})
ament_export_dependencies(${dependencies})
ament_package()
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2023, 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 ZSTD_POINT_CLOUD_TRANSPORT__ZSTD_PUBLISHER_HPP_
#define ZSTD_POINT_CLOUD_TRANSPORT__ZSTD_PUBLISHER_HPP_

#include <memory>
#include <string>

#include <sensor_msgs/msg/point_cloud2.hpp>

#include <point_cloud_transport/point_cloud_transport.hpp>

#include <point_cloud_transport/simple_publisher_plugin.hpp>
#include <point_cloud_interfaces/msg/compressed_point_cloud2.hpp>


namespace zstd_point_cloud_transport
{

class ZstdPublisher
: public point_cloud_transport::SimplePublisherPlugin<
point_cloud_interfaces::msg::CompressedPointCloud2>
{
public:
std::string getTransportName() const override;

void declareParameters(const std::string & base_topic) override;

std::string getDataType() const override;

TypedEncodeResult encodeTyped(const sensor_msgs::msg::PointCloud2 & raw) const override;

private:
int encode_level_{7};
};
} // namespace zstd_point_cloud_transport

#endif // ZSTD_POINT_CLOUD_TRANSPORT__ZSTD_PUBLISHER_HPP_
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2023, 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 ZSTD_POINT_CLOUD_TRANSPORT__ZSTD_SUBSCRIBER_HPP_
#define ZSTD_POINT_CLOUD_TRANSPORT__ZSTD_SUBSCRIBER_HPP_

#include <string>

#include <point_cloud_interfaces/msg/compressed_point_cloud2.hpp>

#include <point_cloud_transport/simple_subscriber_plugin.hpp>
#include <point_cloud_transport/transport_hints.hpp>

namespace zstd_point_cloud_transport
{

class ZstdSubscriber
: public point_cloud_transport::SimpleSubscriberPlugin<
point_cloud_interfaces::msg::CompressedPointCloud2>
{
public:
std::string getTransportName() const override;

void declareParameters() override;

std::string getDataType() const override;

DecodeResult decodeTyped(const point_cloud_interfaces::msg::CompressedPointCloud2 & compressed)
const override;
};
} // namespace zstd_point_cloud_transport

#endif // ZSTD_POINT_CLOUD_TRANSPORT__ZSTD_SUBSCRIBER_HPP_
33 changes: 33 additions & 0 deletions zstd_point_cloud_transport/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<package format="3">
<name>zstd_point_cloud_transport</name>
<version>1.0.5</version>
<description>
zstd_point_cloud_transport provides a plugin to point_cloud_transport for sending point clouds
encoded with lib
</description>
<author>Alejandro Hernandez Cordero</author>
<maintainer email="[email protected]">Alejandro Hernandez Cordero</maintainer>
<license>BSD</license>

<url type="repository">https://github.com/ctu-vras/draco_point_cloud_transport</url>
<url type="website">https://wiki.ros.org/draco_point_cloud_transport</url>
<url type="bugtracker">https://github.com/ctu-vras/draco_point_cloud_transport/issues</url>

<buildtool_depend>ament_cmake</buildtool_depend>

<build_depend>pluginlib</build_depend>

<exec_depend>pluginlib</exec_depend>

<depend>point_cloud_interfaces</depend>
<depend>point_cloud_transport</depend>
<depend>rclcpp</depend>
<depend>libzstd-dev</depend>

<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
46 changes: 46 additions & 0 deletions zstd_point_cloud_transport/src/manifest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2023, Czech Technical University in Prague
* Copyright (c) 2019, paplhjak
* 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.
*/

#include <pluginlib/class_list_macros.hpp>

#include <point_cloud_transport/publisher_plugin.hpp>
#include <point_cloud_transport/subscriber_plugin.hpp>

#include <zstd_point_cloud_transport/zstd_publisher.hpp>
#include <zstd_point_cloud_transport/zstd_subscriber.hpp>

PLUGINLIB_EXPORT_CLASS(
zstd_point_cloud_transport::ZstdPublisher,
point_cloud_transport::PublisherPlugin)
PLUGINLIB_EXPORT_CLASS(
zstd_point_cloud_transport::ZstdSubscriber,
point_cloud_transport::SubscriberPlugin)
122 changes: 122 additions & 0 deletions zstd_point_cloud_transport/src/zstd_publisher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright (c) 2023, 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.
*/

#include <zstd.h>

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include <zstd_point_cloud_transport/zstd_publisher.hpp>

namespace zstd_point_cloud_transport
{

void ZstdPublisher::declareParameters(const std::string & base_topic)
{
rcl_interfaces::msg::ParameterDescriptor encode_level_paramDescriptor;
encode_level_paramDescriptor.name = "zstd_encode_level";
encode_level_paramDescriptor.type = rcl_interfaces::msg::ParameterType::PARAMETER_INTEGER;
encode_level_paramDescriptor.description =
"0 = minimum compression, but the maximum compression 10";
encode_level_paramDescriptor.set__integer_range(
{rcl_interfaces::msg::IntegerRange()
.set__from_value(-1)
.set__to_value(9)
.set__step(1)});
declareParam<int>(
encode_level_paramDescriptor.name, this->encode_level_,
encode_level_paramDescriptor);

auto param_change_callback =
[this](const std::vector<rclcpp::Parameter> & parameters) -> rcl_interfaces::msg::
SetParametersResult
{
auto result = rcl_interfaces::msg::SetParametersResult();
result.successful = true;
for (auto parameter : parameters) {
if (parameter.get_name() == "zstd_encode_level") {
this->encode_level_ = static_cast<int>(parameter.as_int());
if (!(this->encode_level_ >= -1 && this->encode_level_ <= 9)) {
RCLCPP_ERROR_STREAM(
getLogger(), "encode_level value range should be between [0, 10] ");
}
return result;
}
}
return result;
};
setParamCallback(param_change_callback);
}

std::string ZstdPublisher::getDataType() const
{
return "point_cloud_interfaces/msg/CompressedPointCloud2";
}

std::string ZstdPublisher::getTransportName() const
{
return "zstd";
}

ZstdPublisher::TypedEncodeResult ZstdPublisher::encodeTyped(
const sensor_msgs::msg::PointCloud2 & raw) const
{
size_t est_compress_size = ZSTD_compressBound(raw.data.size());

point_cloud_interfaces::msg::CompressedPointCloud2 compressed;
compressed.compressed_data.resize(est_compress_size);

auto compress_size =
ZSTD_compress(
static_cast<void *>(&compressed.compressed_data[0]),
est_compress_size,
&raw.data[0],
raw.data.size(),
this->encode_level_);

compressed.compressed_data.resize(compress_size);

compressed.width = raw.width;
compressed.height = raw.height;
compressed.row_step = raw.row_step;
compressed.point_step = raw.point_step;
compressed.is_bigendian = raw.is_bigendian;
compressed.is_dense = raw.is_dense;
compressed.header = raw.header;
compressed.fields = raw.fields;
compressed.format = getTransportName();

return compressed;
}

} // namespace zstd_point_cloud_transport
Loading

0 comments on commit 156b65e

Please sign in to comment.