Point Cloud Library (PCL)  1.8.0
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  }
192 
193  }
194 
195 }
196 
197 /// Expect that each of x, y, and z fields are equal in
198 /// two points.
199 #define EXPECT_XYZ_EQ(expected, actual) \
200  EXPECT_PRED_FORMAT2(::pcl::test::internal::XYZEQ, \
201  (expected), (actual))
202 
203 /// Assert that each of x, y, and z fields are equal in
204 /// two points.
205 #define ASSERT_XYZ_EQ(expected, actual) \
206  ASSERT_PRED_FORMAT2(::pcl::test::internal::XYZEQ, \
207  (expected), (actual))
208 
209 /// Expect that differences between x, y, and z fields in
210 /// two points are each within abs_error.
211 #define EXPECT_XYZ_NEAR(expected, actual, abs_error) \
212  EXPECT_PRED_FORMAT3(::pcl::test::internal::XYZNear, \
213  (expected), (actual), abs_error)
214 
215 /// Assert that differences between x, y, and z fields in
216 /// two points are each within abs_error.
217 #define ASSERT_XYZ_NEAR(expected, actual, abs_error) \
218  EXPECT_PRED_FORMAT3(::pcl::test::internal::XYZNear, \
219  (expected), (actual), abs_error)
220 
221 /// Expect that each of normal_x, normal_y, and normal_z
222 /// fields are equal in two points.
223 #define EXPECT_NORMAL_EQ(expected, actual) \
224  EXPECT_PRED_FORMAT2(::pcl::test::internal::NormalEQ, \
225  (expected), (actual))
226 
227 /// Assert that each of normal_x, normal_y, and normal_z
228 /// fields are equal in two points.
229 #define ASSERT_NORMAL_EQ(expected, actual) \
230  ASSERT_PRED_FORMAT2(::pcl::test::internal::NormalEQ, \
231  (expected), (actual))
232 
233 /// Expect that differences between normal_x, normal_y,
234 /// and normal_z fields in two points are each within
235 /// abs_error.
236 #define EXPECT_NORMAL_NEAR(expected, actual, abs_error) \
237  EXPECT_PRED_FORMAT3(::pcl::test::internal::NormalNear, \
238  (expected), (actual), abs_error)
239 
240 /// Assert that differences between normal_x, normal_y,
241 /// and normal_z fields in two points are each within
242 /// abs_error.
243 #define ASSERT_NORMAL_NEAR(expected, actual, abs_error) \
244  EXPECT_PRED_FORMAT3(::pcl::test::internal::NormalNear, \
245  (expected), (actual), abs_error)
246 
247 /// Expect that each of r, g, and b fields are equal in
248 /// two points.
249 #define EXPECT_RGB_EQ(expected, actual) \
250  EXPECT_PRED_FORMAT2(::pcl::test::internal::RGBEQ, \
251  (expected), (actual))
252 
253 /// Assert that each of r, g, and b fields are equal in
254 /// two points.
255 #define ASSERT_RGB_EQ(expected, actual) \
256  ASSERT_PRED_FORMAT2(::pcl::test::internal::RGBEQ, \
257  (expected), (actual))
258 
259 /// Expect that each of r, g, b, and a fields are equal
260 /// in two points.
261 #define EXPECT_RGBA_EQ(expected, actual) \
262  EXPECT_PRED_FORMAT2(::pcl::test::internal::RGBAEQ, \
263  (expected), (actual))
264 
265 /// Assert that each of r, g, b, and a fields are equal
266 /// in two points.
267 #define ASSERT_RGBA_EQ(expected, actual) \
268  ASSERT_PRED_FORMAT2(::pcl::test::internal::RGBAEQ, \
269  (expected), (actual))
270 
271 #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 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