Point Cloud Library (PCL)  1.9.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
pcl_tests.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  * $Id$
37  */
38 
39 
40 #ifndef PCL_TEST_MACROS
41 #define PCL_TEST_MACROS
42 
43 #include <Eigen/Core>
44 
45 /** \file pcl_tests.h
46  * Helper macros for testing equality of various data fields in PCL points */
47 
48 namespace pcl
49 {
50 
51  /** test_macros.h provide helper macros for testing vectors, matrices etc.
52  * We took some liberty with upcasing names to make them look like googletest
53  * macros names so that reader is not confused.
54  *
55  * This file also provides a family of googletest-style macros for asserting
56  * equality or nearness of xyz, normal, and rgba fields.
57  *
58  * \author Nizar Sallem, Sergey Alexandrov
59  */
60 
61  namespace test
62  {
63 
64  template <typename V1, typename V2>
65  void EXPECT_EQ_VECTORS (const V1& v1, const V2& v2)
66  {
67  SCOPED_TRACE("EXPECT_EQ_VECTORS failed");
68  EXPECT_EQ (v1.size (), v2.size ());
69  size_t length = v1.size ();
70  for (size_t i = 0; i < length; ++i)
71  EXPECT_EQ (v1[i], v2[i]);
72  }
73 
74  template <typename V1, typename V2, typename Scalar>
75  void EXPECT_NEAR_VECTORS (const V1& v1, const V2& v2, const Scalar& epsilon)
76  {
77  SCOPED_TRACE("EXPECT_NEAR_VECTORS failed");
78  EXPECT_EQ (v1.size (), v2.size ());
79  size_t length = v1.size ();
80  for (size_t i = 0; i < length; ++i)
81  EXPECT_NEAR (v1[i], v2[i], epsilon);
82  }
83 
84  namespace internal
85  {
86 
87  template <typename Point1T, typename Point2T>
88  ::testing::AssertionResult XYZEQ (const char* expr1,
89  const char* expr2,
90  const Point1T& p1,
91  const Point2T& p2)
92  {
93  if ((p1).getVector3fMap ().cwiseEqual ((p2).getVector3fMap ()).all ())
94  return ::testing::AssertionSuccess ();
95  return ::testing::AssertionFailure ()
96  << "Value of: " << expr2 << ".getVector3fMap ()" << std::endl
97  << " Actual: " << p2.getVector3fMap ().transpose () << std::endl
98  << "Expected: " << expr1 << ".getVector3fMap ()" << std::endl
99  << "Which is: " << p1.getVector3fMap ().transpose ();
100  }
101 
102  template <typename Point1T, typename Point2T>
103  ::testing::AssertionResult XYZNear (const char* expr1,
104  const char* expr2,
105  const char* abs_error_expr,
106  const Point1T& p1,
107  const Point2T& p2,
108  double abs_error)
109  {
110  const Eigen::Vector3f diff = ((p1).getVector3fMap () -
111  (p2).getVector3fMap ()).cwiseAbs ();
112  if ((diff.array () < abs_error).all ())
113  return ::testing::AssertionSuccess ();
114  return ::testing::AssertionFailure ()
115  << "Some of the element-wise differences exceed " << abs_error_expr
116  << " (which evaluates to " << abs_error << ")" << std::endl
117  << "Difference: " << diff.transpose () << std::endl
118  << " Value of: " << expr2 << ".getVector3fMap ()" << std::endl
119  << " Actual: " << p2.getVector3fMap ().transpose () << std::endl
120  << " Expected: " << expr1 << ".getVector3fMap ()" << std::endl
121  << " Which is: " << p1.getVector3fMap ().transpose ();
122  }
123 
124  template <typename Point1T, typename Point2T>
125  ::testing::AssertionResult NormalEQ (const char* expr1,
126  const char* expr2,
127  const Point1T& p1,
128  const Point2T& p2)
129  {
130  if ((p1).getNormalVector3fMap ().cwiseEqual ((p2).getNormalVector3fMap ()).all ())
131  return ::testing::AssertionSuccess ();
132  return ::testing::AssertionFailure ()
133  << "Value of: " << expr2 << ".getNormalVector3fMap ()" << std::endl
134  << " Actual: " << p2.getNormalVector3fMap ().transpose () << std::endl
135  << "Expected: " << expr1 << ".getNormalVector3fMap ()" << std::endl
136  << "Which is: " << p1.getNormalVector3fMap ().transpose ();
137  }
138 
139  template <typename Point1T, typename Point2T>
140  ::testing::AssertionResult NormalNear (const char* expr1,
141  const char* expr2,
142  const char* abs_error_expr,
143  const Point1T& p1,
144  const Point2T& p2,
145  double abs_error)
146  {
147  const Eigen::Vector3f diff = ((p1).getNormalVector3fMap () -
148  (p2).getNormalVector3fMap ()).cwiseAbs ();
149  if ((diff.array () < abs_error).all ())
150  return ::testing::AssertionSuccess ();
151  return ::testing::AssertionFailure ()
152  << "Some of the element-wise differences exceed " << abs_error_expr
153  << " (which evaluates to " << abs_error << ")" << std::endl
154  << "Difference: " << diff.transpose () << std::endl
155  << " Value of: " << expr2 << ".getNormalVector3fMap ()" << std::endl
156  << " Actual: " << p2.getNormalVector3fMap ().transpose () << std::endl
157  << " Expected: " << expr1 << ".getNormalVector3fMap ()" << std::endl
158  << " Which is: " << p1.getNormalVector3fMap ().transpose ();
159  }
160 
161  template <typename Point1T, typename Point2T>
162  ::testing::AssertionResult RGBEQ (const char* expr1,
163  const char* expr2,
164  const Point1T& p1,
165  const Point2T& p2)
166  {
167  if ((p1).getRGBVector3i ().cwiseEqual ((p2).getRGBVector3i ()).all ())
168  return ::testing::AssertionSuccess ();
169  return ::testing::AssertionFailure ()
170  << "Value of: " << expr2 << ".getRGBVector3i ()" << std::endl
171  << " Actual: " << p2.getRGBVector3i ().transpose () << std::endl
172  << "Expected: " << expr1 << ".getRGBVector3i ()" << std::endl
173  << "Which is: " << p1.getRGBVector3i ().transpose ();
174  }
175 
176  template <typename Point1T, typename Point2T>
177  ::testing::AssertionResult RGBAEQ (const char* expr1,
178  const char* expr2,
179  const Point1T& p1,
180  const Point2T& p2)
181  {
182  if ((p1).getRGBAVector4i ().cwiseEqual ((p2).getRGBAVector4i ()).all ())
183  return ::testing::AssertionSuccess ();
184  return ::testing::AssertionFailure ()
185  << "Value of: " << expr2 << ".getRGBAVector4i ()" << std::endl
186  << " Actual: " << p2.getRGBAVector4i ().transpose () << std::endl
187  << "Expected: " << expr1 << ".getRGBAVector4i ()" << std::endl
188  << "Which is: " << p1.getRGBAVector4i ().transpose ();
189  }
190 
191  template <typename PointCloud1T, typename PointCloud2T>
192  ::testing::AssertionResult MetaDataEQ (const char* expr1,
193  const char* expr2,
194  const PointCloud1T& p1,
195  const PointCloud2T& p2)
196  {
197  if (!(p1.header == p2.header))
198  return ::testing::AssertionFailure () << "Headers are different";
199  if (p1.width != p2.width)
200  return ::testing::AssertionFailure ()
201  << "Value of: " << expr2 << ".width" << std::endl
202  << " Actual: " << p2.width << std::endl
203  << "Expected: " << expr1 << ".width" << std::endl
204  << "Which is: " << p1.width << std::endl;
205  if (p1.height != p2.height)
206  return ::testing::AssertionFailure ()
207  << "Value of: " << expr2 << ".height" << std::endl
208  << " Actual: " << p2.height << std::endl
209  << "Expected: " << expr1 << ".height" << std::endl
210  << "Which is: " << p1.height << std::endl;
211  if (p1.is_dense != p2.is_dense)
212  return ::testing::AssertionFailure ()
213  << "Value of: " << expr2 << ".is_dense" << std::endl
214  << " Actual: " << p2.is_dense << std::endl
215  << "Expected: " << expr1 << ".is_dense" << std::endl
216  << "Which is: " << p1.is_dense << std::endl;
217  if (p1.sensor_origin_ != p2.sensor_origin_)
218  return ::testing::AssertionFailure () << "Sensor origins are different";
219  if (p1.sensor_orientation_.coeffs () != p2.sensor_orientation_.coeffs ())
220  return ::testing::AssertionFailure () << "Sensor orientations are different";
221  return ::testing::AssertionSuccess ();
222  }
223 
224  }
225 
226  }
227 
228 }
229 
230 /// Expect that each of x, y, and z fields are equal in
231 /// two points.
232 #define EXPECT_XYZ_EQ(expected, actual) \
233  EXPECT_PRED_FORMAT2(::pcl::test::internal::XYZEQ, \
234  (expected), (actual))
235 
236 /// Assert that each of x, y, and z fields are equal in
237 /// two points.
238 #define ASSERT_XYZ_EQ(expected, actual) \
239  ASSERT_PRED_FORMAT2(::pcl::test::internal::XYZEQ, \
240  (expected), (actual))
241 
242 /// Expect that differences between x, y, and z fields in
243 /// two points are each within abs_error.
244 #define EXPECT_XYZ_NEAR(expected, actual, abs_error) \
245  EXPECT_PRED_FORMAT3(::pcl::test::internal::XYZNear, \
246  (expected), (actual), abs_error)
247 
248 /// Assert that differences between x, y, and z fields in
249 /// two points are each within abs_error.
250 #define ASSERT_XYZ_NEAR(expected, actual, abs_error) \
251  ASSERT_PRED_FORMAT3(::pcl::test::internal::XYZNear, \
252  (expected), (actual), abs_error)
253 
254 /// Expect that each of normal_x, normal_y, and normal_z
255 /// fields are equal in two points.
256 #define EXPECT_NORMAL_EQ(expected, actual) \
257  EXPECT_PRED_FORMAT2(::pcl::test::internal::NormalEQ, \
258  (expected), (actual))
259 
260 /// Assert that each of normal_x, normal_y, and normal_z
261 /// fields are equal in two points.
262 #define ASSERT_NORMAL_EQ(expected, actual) \
263  ASSERT_PRED_FORMAT2(::pcl::test::internal::NormalEQ, \
264  (expected), (actual))
265 
266 /// Expect that differences between normal_x, normal_y,
267 /// and normal_z fields in two points are each within
268 /// abs_error.
269 #define EXPECT_NORMAL_NEAR(expected, actual, abs_error) \
270  EXPECT_PRED_FORMAT3(::pcl::test::internal::NormalNear, \
271  (expected), (actual), abs_error)
272 
273 /// Assert that differences between normal_x, normal_y,
274 /// and normal_z fields in two points are each within
275 /// abs_error.
276 #define ASSERT_NORMAL_NEAR(expected, actual, abs_error) \
277  ASSERT_PRED_FORMAT3(::pcl::test::internal::NormalNear, \
278  (expected), (actual), abs_error)
279 
280 /// Expect that each of r, g, and b fields are equal in
281 /// two points.
282 #define EXPECT_RGB_EQ(expected, actual) \
283  EXPECT_PRED_FORMAT2(::pcl::test::internal::RGBEQ, \
284  (expected), (actual))
285 
286 /// Assert that each of r, g, and b fields are equal in
287 /// two points.
288 #define ASSERT_RGB_EQ(expected, actual) \
289  ASSERT_PRED_FORMAT2(::pcl::test::internal::RGBEQ, \
290  (expected), (actual))
291 
292 /// Expect that each of r, g, b, and a fields are equal
293 /// in two points.
294 #define EXPECT_RGBA_EQ(expected, actual) \
295  EXPECT_PRED_FORMAT2(::pcl::test::internal::RGBAEQ, \
296  (expected), (actual))
297 
298 /// Assert that each of r, g, b, and a fields are equal
299 /// in two points.
300 #define ASSERT_RGBA_EQ(expected, actual) \
301  ASSERT_PRED_FORMAT2(::pcl::test::internal::RGBAEQ, \
302  (expected), (actual))
303 
304 /// Assert that the metadata (header, width, height,
305 /// is_dense, sensor origin and orientation) are equal
306 /// in two point clouds.
307 #define ASSERT_METADATA_EQ(expected, actual) \
308  ASSERT_PRED_FORMAT2(::pcl::test::internal::MetaDataEQ, \
309  expected, actual)
310 
311 /// Expect that the metadata (header, width, height,
312 /// is_dense, sensor origin and orientation) are equal
313 /// in two point clouds.
314 #define EXPECT_METADATA_EQ(expected, actual) \
315  EXPECT_PRED_FORMAT2(::pcl::test::internal::MetaDataEQ, \
316  expected, actual)
317 
318 #endif
::testing::AssertionResult NormalNear(const char *expr1, const char *expr2, const char *abs_error_expr, const Point1T &p1, const Point2T &p2, double abs_error)
Definition: pcl_tests.h:140
::testing::AssertionResult RGBAEQ(const char *expr1, const char *expr2, const Point1T &p1, const Point2T &p2)
Definition: pcl_tests.h:177
void EXPECT_EQ_VECTORS(const V1 &v1, const V2 &v2)
Definition: pcl_tests.h:65
::testing::AssertionResult MetaDataEQ(const char *expr1, const char *expr2, const PointCloud1T &p1, const PointCloud2T &p2)
Definition: pcl_tests.h:192
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
::testing::AssertionResult XYZEQ(const char *expr1, const char *expr2, const Point1T &p1, const Point2T &p2)
Definition: pcl_tests.h:88
void EXPECT_NEAR_VECTORS(const V1 &v1, const V2 &v2, const Scalar &epsilon)
Definition: pcl_tests.h:75
::testing::AssertionResult RGBEQ(const char *expr1, const char *expr2, const Point1T &p1, const Point2T &p2)
Definition: pcl_tests.h:162
::testing::AssertionResult NormalEQ(const char *expr1, const char *expr2, const Point1T &p1, const Point2T &p2)
Definition: pcl_tests.h:125
::testing::AssertionResult XYZNear(const char *expr1, const char *expr2, const char *abs_error_expr, const Point1T &p1, const Point2T &p2, double abs_error)
Definition: pcl_tests.h:103