|
| 1 | +// Copyright (c) 2013, Open Source Robotics Foundation |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// Software License Agreement (BSD License 2.0) |
| 5 | +// |
| 6 | +// Redistribution and use in source and binary forms, with or without |
| 7 | +// modification, are permitted provided that the following conditions |
| 8 | +// are met: |
| 9 | +// |
| 10 | +// * Redistributions of source code must retain the above copyright |
| 11 | +// notice, this list of conditions and the following disclaimer. |
| 12 | +// * Redistributions in binary form must reproduce the above |
| 13 | +// copyright notice, this list of conditions and the following |
| 14 | +// disclaimer in the documentation and/or other materials provided |
| 15 | +// with the distribution. |
| 16 | +// * Neither the name of the copyright holder nor the names of its |
| 17 | +// contributors may be used to endorse or promote products derived |
| 18 | +// from this software without specific prior written permission. |
| 19 | +// |
| 20 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | +// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | +// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | +// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 26 | +// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 | +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 28 | +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | +// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 30 | +// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | +// POSSIBILITY OF SUCH DAMAGE. |
| 32 | + |
| 33 | +// this file is originally ported from ROS1: |
| 34 | +// https://github.com/ros/common_msgs/blob/4148d041bae4f284819b980ff50577344147cd5b/sensor_msgs/test/main.cpp |
| 35 | + |
| 36 | +#include <gtest/gtest.h> |
| 37 | + |
| 38 | +#include "sensor_msgs/msg/point_cloud2.hpp" |
| 39 | +#include "sensor_msgs/point_cloud2_iterator.hpp" |
| 40 | + |
| 41 | +TEST(sensor_msgs, PointCloud2Iterator) |
| 42 | +{ |
| 43 | + // Create a dummy PointCloud2 |
| 44 | + size_t n_points = 4; |
| 45 | + sensor_msgs::msg::PointCloud2 cloud_msg_1, cloud_msg_2; |
| 46 | + cloud_msg_1.height = n_points; |
| 47 | + cloud_msg_1.width = 1; |
| 48 | + sensor_msgs::PointCloud2Modifier modifier(cloud_msg_1); |
| 49 | + modifier.setPointCloud2FieldsByString(2, "xyz", "rgb"); |
| 50 | + cloud_msg_2 = cloud_msg_1; |
| 51 | + |
| 52 | + // Fill 1 by hand |
| 53 | + float point_data_raw[] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0}; |
| 54 | + std::vector<float> point_data(point_data_raw, point_data_raw + 3*n_points); |
| 55 | + // colors in RGB order |
| 56 | + uint8_t color_data_raw[] = {40, 80, 120, 160, 200, 240, 20, 40, 60, 80, 100, 120}; |
| 57 | + std::vector<uint8_t> color_data(color_data_raw, color_data_raw + 3*n_points); |
| 58 | + |
| 59 | + float *data = reinterpret_cast<float*>(&cloud_msg_1.data.front()); |
| 60 | + for(size_t n=0, i=0; n<n_points; ++n) { |
| 61 | + for(; i<3*(n+1); ++i) |
| 62 | + *(data++) = point_data[i]; |
| 63 | + // Add an extra float of padding |
| 64 | + ++data; |
| 65 | + uint8_t *bgr = reinterpret_cast<uint8_t*>(data++); |
| 66 | + // add the colors in order BGRA like PCL |
| 67 | + size_t j_max = 2; |
| 68 | + for(size_t j = 0; j <= j_max; ++j) |
| 69 | + *(bgr++) = color_data[3*n+(j_max - j)]; |
| 70 | + // Add 3 extra floats of padding |
| 71 | + data += 3; |
| 72 | + } |
| 73 | + |
| 74 | + // Fill 2 using an iterator |
| 75 | + sensor_msgs::PointCloud2Iterator<float> iter_x(cloud_msg_2, "x"); |
| 76 | + sensor_msgs::PointCloud2Iterator<uint8_t> iter_r(cloud_msg_2, "r"); |
| 77 | + sensor_msgs::PointCloud2Iterator<uint8_t> iter_g(cloud_msg_2, "g"); |
| 78 | + sensor_msgs::PointCloud2Iterator<uint8_t> iter_b(cloud_msg_2, "b"); |
| 79 | + for(size_t i=0; i<n_points; ++i, ++iter_x, ++iter_r, ++iter_g, ++iter_b) { |
| 80 | + for(size_t j=0; j<3; ++j) |
| 81 | + iter_x[j] = point_data[j+3*i]; |
| 82 | + *iter_r = color_data[3*i]; |
| 83 | + *iter_g = color_data[3*i+1]; |
| 84 | + *iter_b = color_data[3*i+2]; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + // Check the values using an iterator |
| 89 | + sensor_msgs::PointCloud2ConstIterator<float> iter_const_1_x(cloud_msg_1, "x"), iter_const_2_x(cloud_msg_2, "x"); |
| 90 | + sensor_msgs::PointCloud2ConstIterator<float> iter_const_1_y(cloud_msg_1, "y"), iter_const_2_y(cloud_msg_2, "y"); |
| 91 | + sensor_msgs::PointCloud2ConstIterator<float> iter_const_1_z(cloud_msg_1, "z"), iter_const_2_z(cloud_msg_2, "z"); |
| 92 | + sensor_msgs::PointCloud2ConstIterator<uint8_t> iter_const_1_r(cloud_msg_1, "r"), iter_const_2_r(cloud_msg_2, "r"); |
| 93 | + sensor_msgs::PointCloud2ConstIterator<uint8_t> iter_const_1_g(cloud_msg_1, "g"), iter_const_2_g(cloud_msg_2, "g"); |
| 94 | + sensor_msgs::PointCloud2ConstIterator<uint8_t> iter_const_1_b(cloud_msg_1, "b"), iter_const_2_b(cloud_msg_2, "b"); |
| 95 | + |
| 96 | + size_t i=0; |
| 97 | + for(; iter_const_1_x != iter_const_1_x.end(); ++i, ++iter_const_1_x, ++iter_const_2_x, ++iter_const_1_y, ++iter_const_2_y, |
| 98 | + ++iter_const_1_z, ++iter_const_2_z, ++iter_const_1_r, ++iter_const_1_g, ++iter_const_1_b) { |
| 99 | + EXPECT_EQ(*iter_const_1_x, *iter_const_2_x); |
| 100 | + EXPECT_EQ(*iter_const_1_x, point_data[0+3*i]); |
| 101 | + EXPECT_EQ(*iter_const_1_y, *iter_const_2_y); |
| 102 | + EXPECT_EQ(*iter_const_1_y, point_data[1+3*i]); |
| 103 | + EXPECT_EQ(*iter_const_1_z, *iter_const_2_z); |
| 104 | + EXPECT_EQ(*iter_const_1_z, point_data[2+3*i]); |
| 105 | + EXPECT_EQ(*iter_const_1_r, *iter_const_2_r); |
| 106 | + EXPECT_EQ(*iter_const_1_r, color_data[3*i+0]); |
| 107 | + EXPECT_EQ(*iter_const_1_g, *iter_const_2_g); |
| 108 | + EXPECT_EQ(*iter_const_1_g, color_data[3*i+1]); |
| 109 | + EXPECT_EQ(*iter_const_1_b, *iter_const_2_b); |
| 110 | + EXPECT_EQ(*iter_const_1_b, color_data[3*i+2]); |
| 111 | + // This is to test the different operators |
| 112 | + ++iter_const_2_r; |
| 113 | + iter_const_2_g += 1; |
| 114 | + iter_const_2_b = iter_const_2_b + 1; |
| 115 | + } |
| 116 | + EXPECT_EQ(i, n_points); |
| 117 | +} |
| 118 | + |
0 commit comments