Point3Test.cpp
Go to the documentation of this file.
1 
2 #include "UnitTestUtils.hpp"
3 #include <gtest/gtest.h>
4 
5 using namespace RobotDynamics;
6 using namespace RobotDynamics::Math;
7 
8 class Point3Test : public ::testing::Test
9 {
10  protected:
11  virtual void SetUp()
12  {
13  std::srand(time(NULL));
14  }
15 
16  virtual void TearDown()
17  {
18  }
19 
20  int nTests = 1000;
21 };
22 
23 TEST_F(Point3Test, testSimpleConstructors)
24 {
25  RobotDynamics::Math::Vector3d v(-1.2, 3.1, 9.1);
27 
28  EXPECT_EQ(v.x(), p.x());
29  EXPECT_EQ(v.y(), p.y());
30  EXPECT_EQ(v.z(), p.z());
31 }
32 
33 TEST_F(Point3Test, testConstCorrectness)
34 {
35  RobotDynamics::Math::Point3d p(1.1, 2.2, 3.3);
36 
37  p.x() = 10.2;
38 
39  EXPECT_EQ(10.2, p.x());
40 
41  double& Y = p.y();
42 
43  Y = 8.2;
44 
45  EXPECT_EQ(p.y(), 8.2);
46 
47  EXPECT_EQ(p.z(), 3.3);
48 
49  double* ptr = p.data();
50 
51  EXPECT_EQ(ptr[0], p.x());
52  EXPECT_EQ(ptr[1], p.y());
53  EXPECT_EQ(ptr[2], p.z());
54 }
55 
56 TEST_F(Point3Test, testCross)
57 {
58  Point3d p(1., 0., 0.);
59  Vector3d v_p(1., 0., 0.);
60  Vector3d v(0., 1., 0.);
61 
63 }
64 
65 TEST_F(Point3Test, testDistanceSquared)
66 {
67  for (int i = 0; i < nTests; i++)
68  {
69  double x = unit_test_utils::getRandomNumber<double>();
70  double y = unit_test_utils::getRandomNumber<double>();
71  double z = unit_test_utils::getRandomNumber<double>();
72 
73  Point3d point(x, y, z);
74 
75  double x2 = unit_test_utils::getRandomNumber<double>();
76  double y2 = unit_test_utils::getRandomNumber<double>();
77  double z2 = unit_test_utils::getRandomNumber<double>();
78 
79  Point3d point2(x2, y2, z2);
80 
81  double distanceSquared = pow((x2 - x), 2) + pow((y2 - y), 2) + pow((z2 - z), 2);
82 
83  EXPECT_NEAR(distanceSquared, point.distanceSquared(point2), unit_test_utils::TEST_PREC);
84  }
85 
86  for (int i = 0; i < nTests; i++)
87  {
88  double x = unit_test_utils::getRandomNumber<double>();
89  double y = unit_test_utils::getRandomNumber<double>();
90  double z = unit_test_utils::getRandomNumber<double>();
91 
92  Point3d point(x, y, z);
93 
94  double x2 = unit_test_utils::getRandomNumber<double>();
95  double y2 = unit_test_utils::getRandomNumber<double>();
96  double z2 = unit_test_utils::getRandomNumber<double>();
97 
98  Point3d point2(x2, y2, z2);
99 
100  double dx, dy, dz;
101  dx = x - x2;
102  dy = y - y2;
103  dz = z - z2;
104 
105  double distanceSquared = dx * dx + dy * dy + dz * dz;
106 
107  EXPECT_NEAR(distanceSquared, point.distanceSquared(point2), unit_test_utils::TEST_PREC);
108  }
109 }
110 
111 TEST_F(Point3Test, testDistance)
112 {
113  for (int i = 0; i < nTests; i++)
114  {
115  double x = unit_test_utils::getRandomNumber<double>();
116  double y = unit_test_utils::getRandomNumber<double>();
117  double z = unit_test_utils::getRandomNumber<double>();
118 
119  Point3d point(x, y, z);
120 
121  double x2 = unit_test_utils::getRandomNumber<double>();
122  double y2 = unit_test_utils::getRandomNumber<double>();
123  double z2 = unit_test_utils::getRandomNumber<double>();
124 
125  Point3d point2(x2, y2, z2);
126 
127  double distance = sqrt(pow((x2 - x), 2) + pow((y2 - y), 2) + pow((z2 - z), 2));
128 
129  EXPECT_NEAR(distance, point.distance(point2), unit_test_utils::TEST_PREC);
130  }
131 
132  for (int i = 0; i < nTests; i++)
133  {
134  double x = unit_test_utils::getRandomNumber<double>();
135  double y = unit_test_utils::getRandomNumber<double>();
136  double z = unit_test_utils::getRandomNumber<double>();
137 
138  Point3d point(x, y, z);
139 
140  double x2 = unit_test_utils::getRandomNumber<double>();
141  double y2 = unit_test_utils::getRandomNumber<double>();
142  double z2 = unit_test_utils::getRandomNumber<double>();
143 
144  Point3d point2(x2, y2, z2);
145 
146  double dx, dy, dz;
147  dx = x - x2;
148  dy = y - y2;
149  dz = z - z2;
150 
151  double distance = sqrt(dx * dx + dy * dy + dz * dz);
152 
153  EXPECT_NEAR(distance, point.distance(point2), unit_test_utils::TEST_PREC);
154  }
155 }
156 
157 TEST_F(Point3Test, testDistanceL1)
158 {
159  for (int i = 0; i < nTests; i++)
160  {
161  double x = unit_test_utils::getRandomNumber<double>();
162  double y = unit_test_utils::getRandomNumber<double>();
163  double z = unit_test_utils::getRandomNumber<double>();
164 
165  Point3d point(x, y, z);
166 
167  double x2 = unit_test_utils::getRandomNumber<double>();
168  double y2 = unit_test_utils::getRandomNumber<double>();
169  double z2 = unit_test_utils::getRandomNumber<double>();
170 
171  Point3d point2(x2, y2, z2);
172 
173  double distanceL1 = fabs(x2 - x) + fabs(y2 - y) + fabs(z2 - z);
174 
175  EXPECT_NEAR(distanceL1, point.distanceL1(point2), unit_test_utils::TEST_PREC);
176  }
177 
178  for (int i = 0; i < nTests; i++)
179  {
180  double x = unit_test_utils::getRandomNumber<double>();
181  double y = unit_test_utils::getRandomNumber<double>();
182  double z = unit_test_utils::getRandomNumber<double>();
183 
184  Point3d point(x, y, z);
185 
186  double x2 = unit_test_utils::getRandomNumber<double>();
187  double y2 = unit_test_utils::getRandomNumber<double>();
188  double z2 = unit_test_utils::getRandomNumber<double>();
189 
190  Point3d point2(x2, y2, z2);
191 
192  double distanceL1 = fabs(x2 - x) + fabs(y2 - y) + fabs(z2 - z);
193 
194  EXPECT_NEAR(distanceL1, point.distanceL1(point2), unit_test_utils::TEST_PREC);
195  }
196 }
197 
198 TEST_F(Point3Test, testDistanceLInf)
199 {
200  for (int i = 0; i < nTests; i++)
201  {
202  double x = unit_test_utils::getRandomNumber<double>();
203  double y = unit_test_utils::getRandomNumber<double>();
204  double z = unit_test_utils::getRandomNumber<double>();
205 
206  Point3d point(x, y, z);
207 
208  double x2 = unit_test_utils::getRandomNumber<double>();
209  double y2 = unit_test_utils::getRandomNumber<double>();
210  double z2 = unit_test_utils::getRandomNumber<double>();
211 
212  Point3d point2(x2, y2, z2);
213 
214  double distanceLInf = std::max(fabs(x2 - x), fabs(y2 - y));
215  distanceLInf = std::max(distanceLInf, fabs(z2 - z));
216 
217  EXPECT_NEAR(distanceLInf, point.distanceLinf(point2), unit_test_utils::TEST_PREC);
218  }
219 
220  for (int i = 0; i < nTests; i++)
221  {
222  double x = unit_test_utils::getRandomNumber<double>();
223  double y = unit_test_utils::getRandomNumber<double>();
224  double z = unit_test_utils::getRandomNumber<double>();
225 
226  Point3d point(x, y, z);
227 
228  double x2 = unit_test_utils::getRandomNumber<double>();
229  double y2 = unit_test_utils::getRandomNumber<double>();
230  double z2 = unit_test_utils::getRandomNumber<double>();
231 
232  Point3d point2(x2, y2, z2);
233 
234  double distanceLInf = std::max(fabs(x2 - x), fabs(y2 - y));
235  distanceLInf = std::max(distanceLInf, fabs(z2 - z));
236 
237  EXPECT_NEAR(distanceLInf, point.distanceLinf(point2), unit_test_utils::TEST_PREC);
238  }
239 }
240 
241 TEST_F(Point3Test, testSubtract1)
242 {
245  Vector3d v(point2.x() - point1.x(), point2.y() - point1.y(), point2.z() - point1.z());
246  Vector3d v2 = point2 - point1;
247 
248  EXPECT_TRUE(v.isApprox(v2, 1e-12));
249 }
250 
251 TEST_F(Point3Test, pointPlusMinusVector)
252 {
253  Point3d p(-0.132, 0.6135, 1.111);
254  Vector3d v(-8.711, 332.11, -9.8111);
255 
256  Point3d p_out = p - v;
257  EXPECT_NEAR(p_out.x(), p.x() - v.x(), 1.e-12);
258  EXPECT_NEAR(p_out.y(), p.y() - v.y(), 1.e-12);
259  EXPECT_NEAR(p_out.z(), p.z() - v.z(), 1.e-12);
260 
261  p_out = p + v;
262  EXPECT_NEAR(p_out.x(), p.x() + v.x(), 1.e-12);
263  EXPECT_NEAR(p_out.y(), p.y() + v.y(), 1.e-12);
264  EXPECT_NEAR(p_out.z(), p.z() + v.z(), 1.e-12);
265 }
266 
267 TEST_F(Point3Test, testScale1)
268 {
269  for (int i = 0; i < 1000; i++)
270  {
272  Point3d point2 = point1;
273  double scale = rand() % 100 - 50;
274 
275  point1 *= scale;
276 
277  EXPECT_NEAR(point1.x(), point2.x() * scale, unit_test_utils::TEST_PREC);
278  EXPECT_NEAR(point1.y(), point2.y() * scale, unit_test_utils::TEST_PREC);
279  EXPECT_NEAR(point1.z(), point2.z() * scale, unit_test_utils::TEST_PREC);
280 
281  EXPECT_NEAR(point1.x(), scale * point2.x(), unit_test_utils::TEST_PREC);
282  EXPECT_NEAR(point1.y(), scale * point2.y(), unit_test_utils::TEST_PREC);
283  EXPECT_NEAR(point1.z(), scale * point2.z(), unit_test_utils::TEST_PREC);
284  }
285 }
286 
287 TEST_F(Point3Test, testAbsoluteValue1)
288 {
289  for (int i = 0; i < 1000; i++)
290  {
292  Point3d point2 = point1;
293  point1.absoluteValue();
294 
295  EXPECT_NEAR(point1.x(), fabs(point2.x()), unit_test_utils::TEST_PREC);
296  EXPECT_NEAR(point1.y(), fabs(point2.y()), unit_test_utils::TEST_PREC);
297  EXPECT_NEAR(point1.z(), fabs(point2.z()), unit_test_utils::TEST_PREC);
298  }
299 
300  for (int i = 0; i < 1000; i++)
301  {
303  Point3d point2 = point1;
304  point1.absoluteValue();
305 
306  EXPECT_NEAR(point1.x(), fabs(point2.x()), unit_test_utils::TEST_PREC);
307  EXPECT_NEAR(point1.y(), fabs(point2.y()), unit_test_utils::TEST_PREC);
308  EXPECT_NEAR(point1.z(), fabs(point2.z()), unit_test_utils::TEST_PREC);
309  }
310 }
311 
312 TEST_F(Point3Test, testClampMinMax1)
313 {
314  {
315  Point3d point1(100., 200., 300.);
316  Point3d point2 = point1;
317  point2.clampMinMax(-100., -50.);
318 
319  EXPECT_NEAR(point2.x(), -50., unit_test_utils::TEST_PREC);
320  EXPECT_NEAR(point2.y(), -50., unit_test_utils::TEST_PREC);
321  EXPECT_NEAR(point2.z(), -50., unit_test_utils::TEST_PREC);
322  }
323 
324  {
325  Point3d point1(100., 200., 300.);
326  Point3d point2 = point1;
327  point2.clampMinMax(-100., -50.);
328 
329  EXPECT_NEAR(point2.x(), -50., unit_test_utils::TEST_PREC);
330  EXPECT_NEAR(point2.y(), -50., unit_test_utils::TEST_PREC);
331  EXPECT_NEAR(point2.z(), -50., unit_test_utils::TEST_PREC);
332  }
333 }
334 
335 TEST_F(Point3Test, testClampMinMax2)
336 {
337  {
338  Point3d point1(100, 200, 300);
339  Point3d point2 = point1;
340  point2.clampMinMax(-100, 200);
341 
342  EXPECT_NEAR(point2.x(), 100, unit_test_utils::TEST_PREC);
343  EXPECT_NEAR(point2.y(), 200, unit_test_utils::TEST_PREC);
344  EXPECT_NEAR(point2.z(), 200, unit_test_utils::TEST_PREC);
345  }
346 
347  {
348  Point3d point1(100, 200, 300);
349  Point3d point2 = point1;
350  point2.clampMinMax(-100, 200);
351 
352  EXPECT_NEAR(point2.x(), 100, unit_test_utils::TEST_PREC);
353  EXPECT_NEAR(point2.y(), 200, unit_test_utils::TEST_PREC);
354  EXPECT_NEAR(point2.z(), 200, unit_test_utils::TEST_PREC);
355  }
356 }
357 
358 TEST_F(Point3Test, testClampMinMax3)
359 {
360  {
361  Point3d point1(100, 200, 300);
362  Point3d point2 = point1;
363  point2.clampMinMax(201, 220);
364 
365  EXPECT_NEAR(point2.x(), 201, unit_test_utils::TEST_PREC);
366  EXPECT_NEAR(point2.y(), 201, unit_test_utils::TEST_PREC);
367  EXPECT_NEAR(point2.z(), 220, unit_test_utils::TEST_PREC);
368  }
369 
370  {
371  Point3d point1(100, 200, 300);
372  Point3d point2 = point1;
373  point2.clampMinMax(201, 220);
374 
375  EXPECT_NEAR(point2.x(), 201, unit_test_utils::TEST_PREC);
376  EXPECT_NEAR(point2.y(), 201, unit_test_utils::TEST_PREC);
377  EXPECT_NEAR(point2.z(), 220, unit_test_utils::TEST_PREC);
378  }
379 }
380 
381 TEST_F(Point3Test, testClampMin)
382 {
383  {
384  Point3d point1(100, 200, 300);
385  Point3d point2 = point1;
386  point2.clampMin(201);
387 
388  EXPECT_NEAR(point2.x(), 201, unit_test_utils::TEST_PREC);
389  EXPECT_NEAR(point2.y(), 201, unit_test_utils::TEST_PREC);
390  EXPECT_NEAR(point2.z(), 300, unit_test_utils::TEST_PREC);
391  }
392 
393  {
394  Point3d point1(100, 200, 300);
395  Point3d point2 = point1;
396  point2.clampMin(201);
397 
398  EXPECT_NEAR(point2.x(), 201, unit_test_utils::TEST_PREC);
399  EXPECT_NEAR(point2.y(), 201, unit_test_utils::TEST_PREC);
400  EXPECT_NEAR(point2.z(), 300, unit_test_utils::TEST_PREC);
401  }
402 
403  {
404  Point3d point1(100, 200, 199);
405  Point3d point2 = point1;
406  point2.clampMin(201);
407 
408  EXPECT_NEAR(point2.x(), 201, unit_test_utils::TEST_PREC);
409  EXPECT_NEAR(point2.y(), 201, unit_test_utils::TEST_PREC);
410  EXPECT_NEAR(point2.z(), 201, unit_test_utils::TEST_PREC);
411  }
412 }
413 
414 TEST_F(Point3Test, testClampMax)
415 {
416  {
417  Point3d point1(100, 200, 300);
418  Point3d point2 = point1;
419  point2.clampMax(201);
420 
421  EXPECT_NEAR(point2.x(), 100, unit_test_utils::TEST_PREC);
422  EXPECT_NEAR(point2.y(), 200, unit_test_utils::TEST_PREC);
423  EXPECT_NEAR(point2.z(), 201, unit_test_utils::TEST_PREC);
424  }
425 
426  {
427  Point3d point1(100, 200, 300);
428  Point3d point2 = point1;
429  point2.clampMax(201);
430 
431  EXPECT_NEAR(point2.x(), 100, unit_test_utils::TEST_PREC);
432  EXPECT_NEAR(point2.y(), 200, unit_test_utils::TEST_PREC);
433  EXPECT_NEAR(point2.z(), 201, unit_test_utils::TEST_PREC);
434  }
435 }
436 
437 TEST_F(Point3Test, testEqualsEquals)
438 {
439  {
440  double x = unit_test_utils::getRandomNumber<double>();
441  double y = unit_test_utils::getRandomNumber<double>();
442  double z = unit_test_utils::getRandomNumber<double>();
443  Point3d point1(x, y, z);
444  Point3d point2 = point1;
445 
446  EXPECT_TRUE(point2 == point1);
447  EXPECT_FALSE(point2 != point1);
448 
449  point2.x() = point2.x() + 0.1;
450  EXPECT_FALSE(point2 == point1);
451  }
452 
453  {
454  float x = unit_test_utils::getRandomNumber<float>();
455  float y = unit_test_utils::getRandomNumber<float>();
456  float z = unit_test_utils::getRandomNumber<float>();
457  Point3d point1(x, y, z);
458  Point3d point2 = point1;
459 
460  EXPECT_TRUE(point2 == point1);
461  EXPECT_FALSE(point2 != point1);
462  }
463 }
464 
465 TEST_F(Point3Test, testTemplateTypeInference)
466 {
467  double x = unit_test_utils::getRandomNumber<double>();
468  double y = unit_test_utils::getRandomNumber<double>();
469  double z = unit_test_utils::getRandomNumber<double>();
471  Point3d point1(v);
472  Point3d point2(x, y, z);
473 
474  EXPECT_NEAR(point1.x(), point2.x(), unit_test_utils::TEST_PREC);
475  EXPECT_NEAR(point1.y(), point2.y(), unit_test_utils::TEST_PREC);
476  EXPECT_NEAR(point1.z(), point2.z(), unit_test_utils::TEST_PREC);
477 
479 
480  point1.set(point3);
481 
482  EXPECT_NEAR(point3.x(), point1.x(), unit_test_utils::TEST_PREC);
483  EXPECT_NEAR(point3.y(), point1.y(), unit_test_utils::TEST_PREC);
484  EXPECT_NEAR(point3.z(), point1.z(), unit_test_utils::TEST_PREC);
485 
486  Point3d point4(point3);
487 
488  EXPECT_NEAR(point3.x(), point4.x(), unit_test_utils::TEST_PREC);
489  EXPECT_NEAR(point3.y(), point4.y(), unit_test_utils::TEST_PREC);
490  EXPECT_NEAR(point3.z(), point4.z(), unit_test_utils::TEST_PREC);
491 }
492 
493 TEST_F(Point3Test, SpatialTransformApplyOnPoint3)
494 {
496 
497  double th = M_PI_2;
500 
503 
504  Point3d p(3., -1., 2.);
505 
508 
509  p.transform(X_1);
510 
511  EXPECT_TRUE(p.epsilonEquals(Point3d(-3., -1., 6.), unit_test_utils::TEST_PREC));
512 
513  p.transform(X_2);
514 
515  Point3d pTest(3., -1., 2.);
516 
518 
519  pTest.transform(X_3);
520 
521  EXPECT_TRUE(pTest.epsilonEquals(Point3d(-2., 5., -6.), unit_test_utils::TEST_PREC));
522 }
523 
524 TEST_F(Point3Test, transform)
525 {
526  Point3d p1(0.1, 0.2, -1.4);
527  Point3d p2 = p1;
528 
531  p1.transform(X);
532  Point3d p3 = p2.transform_copy(X);
533 
534  EXPECT_NEAR(p3.x(), p1.x(), unit_test_utils::TEST_PREC);
535  EXPECT_NEAR(p3.y(), p1.y(), unit_test_utils::TEST_PREC);
536  EXPECT_NEAR(p3.z(), p1.z(), unit_test_utils::TEST_PREC);
537 
538  EXPECT_NEAR(p2.x(), 0.1, unit_test_utils::TEST_PREC);
539  EXPECT_NEAR(p2.y(), 0.2, unit_test_utils::TEST_PREC);
540  EXPECT_NEAR(p2.z(), -1.4, unit_test_utils::TEST_PREC);
541 }
542 
543 int main(int argc, char** argv)
544 {
545  ::testing::InitGoogleTest(&argc, argv);
546  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
547  return RUN_ALL_TESTS();
548 }
EIGEN_STRONG_INLINE double & x()
Definition: Point3.hpp:234
void transform(const Math::SpatialTransform &X)
Performs in place point transform. Given a point, , this performs .
Definition: Point3.hpp:59
EIGEN_STRONG_INLINE void set(const std::vector< double > &vector)
Definition: Point3.hpp:71
void absoluteValue()
Set each element to the absolute value.
Definition: Point3.hpp:167
SpatialTransform Xroty(const double &yrot)
Get transform with zero translation and pure rotation about y axis.
void clampMax(const double max)
clamp any values that are greater than make to max
Definition: Point3.hpp:135
const double TEST_PREC
SpatialTransform Xtrans(const Vector3d &r)
Get pure translation transform.
TEST_F(Point3Test, testSimpleConstructors)
Definition: Point3Test.cpp:23
static Point3d getRandomPoint3()
SpatialTransform Xrotz(const double &zrot)
Get transform with zero translation and pure rotation about z axis.
EIGEN_STRONG_INLINE double * data()
Definition: Point3.hpp:264
Compact representation of spatial transformations.
SpatialTransform Xrotx(const double &xrot)
Get transform with zero translation and pure rotation about x axis.
virtual void TearDown()
Definition: Point3Test.cpp:16
int main(int argc, char **argv)
Definition: Point3Test.cpp:543
Math types such as vectors and matrices and utility functions.
Definition: ForceVector.hpp:21
EIGEN_STRONG_INLINE double & y()
Definition: Point3.hpp:244
Vector3d cross(const Vector3d &v)
Cross product between a point and vector.
Definition: Point3.hpp:213
static bool checkVector3dEpsilonClose(const RobotDynamics::Math::Vector3d &v1, const RobotDynamics::Math::Vector3d &v2, const double epsilon)
virtual void SetUp()
Definition: Point3Test.cpp:11
EIGEN_STRONG_INLINE bool epsilonEquals(const Point3d &point, const double epsilon) const
Definition: Point3.hpp:104
void clampMinMax(const double min, const double max)
clamp any values greater than max to max, and any value less than min to min
Definition: Point3.hpp:158
EIGEN_STRONG_INLINE double & z()
Definition: Point3.hpp:254
void clampMin(const double min)
clamp any values that are less than min to min
Definition: Point3.hpp:113
Point3d transform_copy(const Math::SpatialTransform &X)
Definition: Point3.hpp:64
Namespace for all structures of the RobotDynamics library.
Definition: Body.h:21


rdl_dynamics
Author(s):
autogenerated on Tue Apr 20 2021 02:25:27