iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-PointCloud2Serialization.C
4 
5 #include <nrt/External/cereal/archives/binary.hpp>
6 
7 #include <random>
8 
9 #define BOOST_TEST_DYN_LINK
10 #define BOOST_TEST_MODULE PointCloud2SerializationTest
11 #include <boost/test/unit_test.hpp>
12 
13 nrt::PointCloud2 createRandomCloud( int size )
14 {
15  std::random_device rd;
16  std::mt19937 gen(rd());
17 
18  std::uniform_real_distribution<float> posdist(-3.0F, 3.0F);
19  auto posgen = [&]() { return posdist(gen); };
20 
21  std::uniform_int_distribution<uint8_t> coldist(0, 255);
22  auto colgen = [&]() { return coldist(gen); };
23 
24  nrt::PointCloud2 cloud = nrt::PointCloud2::create<nrt::PixGray<uint8_t>>();
25  cloud.resize( size );
26 
27  for(auto p : cloud.range<nrt::PixGray<uint8_t>>())
28  p = {{posgen(), posgen(), posgen()}, colgen()};
29 
30  return cloud;
31 }
32 
33 BOOST_AUTO_TEST_CASE( BasicSerializationTest )
34 {
35  nrt::PointCloud2 cloud1;
36  cloud1.insert({10, 10, 10});
37  cloud1.insert({11, 11, 11});
38  cloud1.insert({12, 12, 12});
39  cloud1.insert({13, 13, 13});
40 
41  BOOST_CHECK_EQUAL( cloud1[0], nrt::PointCloud2::Geometry(10) );
42  BOOST_CHECK_EQUAL( cloud1[1], nrt::PointCloud2::Geometry(11) );
43  BOOST_CHECK_EQUAL( cloud1[2], nrt::PointCloud2::Geometry(12) );
44  BOOST_CHECK_EQUAL( cloud1[3], nrt::PointCloud2::Geometry(13) );
45 
46  std::stringstream ostream;
47 
48  nrt::Message<nrt::PointCloud2> cloud1Message(cloud1);
49  cereal::BinaryOutputArchive outAr(ostream);
50  outAr( cloud1Message );
51 
52  nrt::Message<nrt::PointCloud2> cloud2Message;
53  cereal::BinaryInputArchive inAr(ostream);
54  inAr( cloud2Message );
55 
56  nrt::PointCloud2 cloud2 = cloud2Message.value();
57  std::cout << "Cloud size: " << cloud2.size() << std::endl;
58 
59  BOOST_CHECK_EQUAL( cloud2[0], nrt::PointCloud2::Geometry(10) );
60  BOOST_CHECK_EQUAL( cloud2[1], nrt::PointCloud2::Geometry(11) );
61  BOOST_CHECK_EQUAL( cloud2[2], nrt::PointCloud2::Geometry(12) );
62  BOOST_CHECK_EQUAL( cloud2[3], nrt::PointCloud2::Geometry(13) );
63 }
64 
65 BOOST_AUTO_TEST_CASE( BigBasicSerializationTest )
66 {
67  nrt::PointCloud2 cloud1 = createRandomCloud(70000);
68 
69  std::stringstream ostream;
70 
71  nrt::Message<nrt::PointCloud2> cloud1Message(cloud1);
72  auto const outstarttime = std::chrono::steady_clock::now();
73  {
74  cereal::BinaryOutputArchive outAr(ostream);
75  outAr( cloud1Message );
76  }
77  auto const outendtime = std::chrono::steady_clock::now();
78  NRT_INFO(
79  "Serializing point cloud out message took " <<
80  std::chrono::duration_cast<std::chrono::milliseconds>(outendtime-outstarttime).count() << "ms");
81 
82  nrt::Message<nrt::PointCloud2> cloud2Message;
83 
84  auto const instarttime = std::chrono::steady_clock::now();
85  {
86  cereal::BinaryInputArchive inAr(ostream);
87  inAr( cloud2Message );
88  }
89  auto const inendtime = std::chrono::steady_clock::now();
90  NRT_INFO(
91  "Serializing point cloud in message took " <<
92  std::chrono::duration_cast<std::chrono::milliseconds>(inendtime-instarttime).count() << "ms");
93 
94  nrt::PointCloud2 cloud2 = cloud2Message.value();
95 
96  BOOST_CHECK_EQUAL(cloud1.size(), cloud2.size());
97 
98  for(size_t i=0; i<cloud1.size(); ++i)
99  {
100  BOOST_CHECK_EQUAL(cloud1[i], cloud2[i]);
101  BOOST_CHECK_EQUAL(cloud1.at<nrt::PixGray<uint8_t>>(i).get<nrt::PixGray<uint8_t>>(),
103  }
104 }
105 
106 BOOST_AUTO_TEST_CASE( RGBSerializationTest )
107 {
108  nrt::PointCloud2 cloud1;
110  cloud1.insert<nrt::PixRGB<nrt::byte>>({{10, 10, 10}, nrt::PixRGB<nrt::byte>(10)});
111  cloud1.insert<nrt::PixRGB<nrt::byte>>({{11, 11, 11}, nrt::PixRGB<nrt::byte>(11)});
112  cloud1.insert<nrt::PixRGB<nrt::byte>>({{12, 12, 12}, nrt::PixRGB<nrt::byte>(12)});
113  cloud1.insert<nrt::PixRGB<nrt::byte>>({{13, 13, 13}, nrt::PixRGB<nrt::byte>(13)});
114 
115  BOOST_CHECK_EQUAL( cloud1[0], nrt::PointCloud2::Geometry(10) );
116  BOOST_CHECK_EQUAL( cloud1[1], nrt::PointCloud2::Geometry(11) );
117  BOOST_CHECK_EQUAL( cloud1[2], nrt::PointCloud2::Geometry(12) );
118  BOOST_CHECK_EQUAL( cloud1[3], nrt::PointCloud2::Geometry(13) );
119 
120  BOOST_CHECK_EQUAL( cloud1.get<nrt::PixRGB<nrt::byte>>(0).get<nrt::PixRGB<nrt::byte>>(), nrt::PixRGB<nrt::byte>(10) );
121  BOOST_CHECK_EQUAL( cloud1.get<nrt::PixRGB<nrt::byte>>(1).get<nrt::PixRGB<nrt::byte>>(), nrt::PixRGB<nrt::byte>(11) );
122  BOOST_CHECK_EQUAL( cloud1.get<nrt::PixRGB<nrt::byte>>(2).get<nrt::PixRGB<nrt::byte>>(), nrt::PixRGB<nrt::byte>(12) );
123  BOOST_CHECK_EQUAL( cloud1.get<nrt::PixRGB<nrt::byte>>(3).get<nrt::PixRGB<nrt::byte>>(), nrt::PixRGB<nrt::byte>(13) );
124 
125  std::stringstream ostream;
126 
127  nrt::Message<nrt::PointCloud2> cloud1Message(cloud1);
128  cereal::BinaryOutputArchive outAr(ostream);
129  outAr( cloud1Message );
130 
131  nrt::Message<nrt::PointCloud2> cloud2Message;
132  cereal::BinaryInputArchive inAr(ostream);
133  inAr( cloud2Message );
134 
135  nrt::PointCloud2 cloud2 = cloud2Message.value();
136 
137  BOOST_CHECK_EQUAL( cloud2[0], nrt::PointCloud2::Geometry(10) );
138  BOOST_CHECK_EQUAL( cloud2[1], nrt::PointCloud2::Geometry(11) );
139  BOOST_CHECK_EQUAL( cloud2[2], nrt::PointCloud2::Geometry(12) );
140  BOOST_CHECK_EQUAL( cloud2[3], nrt::PointCloud2::Geometry(13) );
141 
142  BOOST_CHECK_EQUAL( cloud2.get<nrt::PixRGB<nrt::byte>>(0).get<nrt::PixRGB<nrt::byte>>(), nrt::PixRGB<nrt::byte>(10) );
143  BOOST_CHECK_EQUAL( cloud2.get<nrt::PixRGB<nrt::byte>>(1).get<nrt::PixRGB<nrt::byte>>(), nrt::PixRGB<nrt::byte>(11) );
144  BOOST_CHECK_EQUAL( cloud2.get<nrt::PixRGB<nrt::byte>>(2).get<nrt::PixRGB<nrt::byte>>(), nrt::PixRGB<nrt::byte>(12) );
145  BOOST_CHECK_EQUAL( cloud2.get<nrt::PixRGB<nrt::byte>>(3).get<nrt::PixRGB<nrt::byte>>(), nrt::PixRGB<nrt::byte>(13) );
146 }