testOrientedPlane3.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
12 /*
13  * @file testOrientedPlane3.cpp
14  * @date Dec 19, 2012
15  * @author Alex Trevor
16  * @author Zhaoyang Lv
17  * @brief Tests the OrientedPlane3 class
18  */
19 
23 #include <boost/assign/std/vector.hpp>
24 
25 using namespace boost::assign;
26 using namespace gtsam;
27 using namespace std;
28 using boost::none;
29 
32 
33 //*******************************************************************************
34 TEST(OrientedPlane3, getMethods) {
35  Vector4 c;
36  c << -1, 0, 0, 5;
37  OrientedPlane3 plane1(c);
38  OrientedPlane3 plane2(c[0], c[1], c[2], c[3]);
39  Vector4 coefficient1 = plane1.planeCoefficients();
40  double distance1 = plane1.distance();
41  EXPECT(assert_equal(coefficient1, c, 1e-8));
42  EXPECT(assert_equal(Unit3(-1,0,0).unitVector(), plane1.normal().unitVector()));
43  EXPECT_DOUBLES_EQUAL(distance1, 5, 1e-8);
44  Vector4 coefficient2 = plane2.planeCoefficients();
45  double distance2 = plane2.distance();
46  EXPECT(assert_equal(coefficient2, c, 1e-8));
47  EXPECT_DOUBLES_EQUAL(distance2, 5, 1e-8);
48  EXPECT(assert_equal(Unit3(-1,0,0).unitVector(), plane2.normal().unitVector()));
49 }
50 
51 
52 //*******************************************************************************
53 OrientedPlane3 transform_(const OrientedPlane3& plane, const Pose3& xr) {
54  return plane.transform(xr);
55 }
56 
58  gtsam::Pose3 pose(gtsam::Rot3::Ypr(-M_PI / 4.0, 0.0, 0.0),
59  gtsam::Point3(2.0, 3.0, 4.0));
60  OrientedPlane3 plane(-1, 0, 0, 5);
61  OrientedPlane3 expectedPlane(-sqrt(2.0) / 2.0, -sqrt(2.0) / 2.0, 0.0, 3);
62  OrientedPlane3 transformedPlane = plane.transform(pose, none, none);
63  EXPECT(assert_equal(expectedPlane, transformedPlane, 1e-5));
64 
65  // Test the jacobians of transform
66  Matrix actualH1, expectedH1, actualH2, expectedH2;
67  expectedH1 = numericalDerivative21(transform_, plane, pose);
68  plane.transform(pose, actualH1, none);
69  EXPECT(assert_equal(expectedH1, actualH1, 1e-5));
70 
71  expectedH2 = numericalDerivative22(transform_, plane, pose);
72  plane.transform(pose, none, actualH2);
73  EXPECT(assert_equal(expectedH2, actualH2, 1e-5));
74 }
75 
76 //*******************************************************************************
77 // Returns a any size random vector
78 inline static Vector randomVector(const Vector& minLimits,
79  const Vector& maxLimits) {
80 
81  // Get the number of dimensions and create the return vector
82  size_t numDims = minLimits.size();
83  Vector vector = Vector::Zero(numDims);
84 
85  // Create the random vector
86  for (size_t i = 0; i < numDims; i++) {
87  double range = maxLimits(i) - minLimits(i);
88  vector(i) = (((double) rand()) / RAND_MAX) * range + minLimits(i);
89  }
90  return vector;
91 }
92 
93 //*******************************************************************************
94 TEST(OrientedPlane3, localCoordinates_retract) {
95  size_t numIterations = 10000;
96  Vector4 minPlaneLimit, maxPlaneLimit;
97  minPlaneLimit << -1.0, -1.0, -1.0, 0.01;
98  maxPlaneLimit << 1.0, 1.0, 1.0, 10.0;
99 
100  Vector3 minXiLimit, maxXiLimit;
101  minXiLimit << -M_PI, -M_PI, -10.0;
102  maxXiLimit << M_PI, M_PI, 10.0;
103  for (size_t i = 0; i < numIterations; i++) {
104  // Create a Plane
105  OrientedPlane3 p1(randomVector(minPlaneLimit, maxPlaneLimit));
106  Vector v12 = randomVector(minXiLimit, maxXiLimit);
107 
108  // Magnitude of the rotation can be at most pi
109  if (v12.head<3>().norm() > M_PI)
110  v12.head<3>() = v12.head<3>() / M_PI;
111  OrientedPlane3 p2 = p1.retract(v12);
112 
113  // Check if the local coordinates and retract return the same results.
114  Vector actual_v12 = p1.localCoordinates(p2);
115  EXPECT(assert_equal(v12, actual_v12, 1e-6));
116  OrientedPlane3 actual_p2 = p1.retract(actual_v12);
117  EXPECT(assert_equal(p2, actual_p2, 1e-6));
118  }
119 }
120 
121 //*******************************************************************************
122 TEST(OrientedPlane3, errorVector) {
123  OrientedPlane3 plane1(-1, 0.1, 0.2, 5);
124  OrientedPlane3 plane2(-1.1, 0.2, 0.3, 5.4);
125 
126  // Hard-coded regression values, to ensure the result doesn't change.
127  EXPECT(assert_equal((Vector) Z_3x1, plane1.errorVector(plane1), 1e-8));
128  EXPECT(assert_equal(Vector3(-0.0677674148, -0.0760543588, -0.4),
129  plane1.errorVector(plane2), 1e-5));
130 
131  // Test the jacobians of transform
132  Matrix33 actualH1, expectedH1, actualH2, expectedH2;
133 
134  Vector3 actual = plane1.errorVector(plane2, actualH1, actualH2);
135  EXPECT(assert_equal(plane1.normal().errorVector(plane2.normal()),
136  Vector2(actual[0], actual[1])));
137  EXPECT(assert_equal(plane1.distance() - plane2.distance(), actual[2]));
138 
139  boost::function<Vector3(const OrientedPlane3&, const OrientedPlane3&)> f =
140  boost::bind(&OrientedPlane3::errorVector, _1, _2, boost::none, boost::none);
141  expectedH1 = numericalDerivative21(f, plane1, plane2);
142  expectedH2 = numericalDerivative22(f, plane1, plane2);
143  EXPECT(assert_equal(expectedH1, actualH1, 1e-5));
144  EXPECT(assert_equal(expectedH2, actualH2, 1e-5));
145 }
146 
147 //*******************************************************************************
148 TEST(OrientedPlane3, jacobian_retract) {
149  OrientedPlane3 plane(-1, 0.1, 0.2, 5);
150  Matrix33 H_actual;
151  boost::function<OrientedPlane3(const Vector3&)> f =
152  boost::bind(&OrientedPlane3::retract, plane, _1, boost::none);
153  {
154  Vector3 v(-0.1, 0.2, 0.3);
155  plane.retract(v, H_actual);
156  Matrix H_expected_numerical = numericalDerivative11(f, v);
157  EXPECT(assert_equal(H_expected_numerical, H_actual, 1e-5));
158  }
159  {
160  Vector3 v(0, 0, 0);
161  plane.retract(v, H_actual);
162  Matrix H_expected_numerical = numericalDerivative11(f, v);
163  EXPECT(assert_equal(H_expected_numerical, H_actual, 1e-5));
164  }
165 }
166 
167 /* ************************************************************************* */
168 int main() {
169  srand(time(nullptr));
170  TestResult tr;
171  return TestRegistry::runAllTests(tr);
172 }
173 /* ************************************************************************* */
static const Eigen::MatrixBase< Vector3 >::ConstantReturnType Z_3x1
Definition: Vector.h:46
static int runAllTests(TestResult &result)
Eigen::Vector3d Vector3
Definition: Vector.h:43
Vector3f p1
int main()
static Vector randomVector(const Vector &minLimits, const Vector &maxLimits)
ArrayXcf v
Definition: Cwise_arg.cpp:1
double distance2(const Point2 &p, const Point2 &q, OptionalJacobian< 1, 2 > H1, OptionalJacobian< 1, 2 > H2)
distance between two points
Definition: Point2.cpp:39
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
#define M_PI
Definition: main.h:78
TEST(OrientedPlane3, getMethods)
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
GTSAM_EXPORT Vector3 unitVector(OptionalJacobian< 3, 2 > H=boost::none) const
Return unit-norm Vector.
Definition: Unit3.cpp:143
Definition: Half.h:150
Some functions to compute numerical derivatives.
internal::FixedSizeMatrix< Y, X1 >::type numericalDerivative21(const boost::function< Y(const X1 &, const X2 &)> &h, const X1 &x1, const X2 &x2, double delta=1e-5)
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:162
static Rot3 Ypr(double y, double p, double r, OptionalJacobian< 3, 1 > Hy=boost::none, OptionalJacobian< 3, 1 > Hp=boost::none, OptionalJacobian< 3, 1 > Hr=boost::none)
Definition: Rot3.h:199
Represents an infinite plane in 3D, which is composed of a planar normal and its perpendicular distan...
Represents a 3D point on a unit sphere.
Definition: Unit3.h:42
OrientedPlane3 retract(const Vector3 &v, OptionalJacobian< 3, 3 > H=boost::none) const
The retract function.
Eigen::VectorXd Vector
Definition: Vector.h:38
OrientedPlane3 transform(const Pose3 &xr, OptionalJacobian< 3, 3 > Hp=boost::none, OptionalJacobian< 3, 6 > Hr=boost::none) const
internal::FixedSizeMatrix< Y, X2 >::type numericalDerivative22(boost::function< Y(const X1 &, const X2 &)> h, const X1 &x1, const X2 &x2, double delta=1e-5)
static const Pose3 pose(Rot3(Vector3(1,-1,-1).asDiagonal()), Point3(0, 0, 0.5))
GTSAM_EXPORT Vector2 errorVector(const Unit3 &q, OptionalJacobian< 2, 2 > H_p=boost::none, OptionalJacobian< 2, 2 > H_q=boost::none) const
Definition: Unit3.cpp:201
#define EXPECT(condition)
Definition: Test.h:151
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
#define time
Array< double, 1, 3 > e(1./3., 0.5, 2.)
OrientedPlane3 transform_(const OrientedPlane3 &plane, const Pose3 &xr)
EIGEN_DONT_INLINE void transform(const Transformation &t, Data &data)
Definition: geometry.cpp:25
double distance() const
Return the perpendicular distance to the origin.
traits
Definition: chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
#define GTSAM_CONCEPT_MANIFOLD_INST(T)
Definition: Manifold.h:180
Eigen::Vector2d Vector2
Definition: Vector.h:42
Vector3 errorVector(const OrientedPlane3 &other, OptionalJacobian< 3, 3 > H1=boost::none, OptionalJacobian< 3, 3 > H2=boost::none) const
static Point3 p2
Vector4 planeCoefficients() const
Returns the plane coefficients.
Vector3 Point3
Definition: Point3.h:35
internal::FixedSizeMatrix< Y, X >::type numericalDerivative11(boost::function< Y(const X &)> h, const X &x, double delta=1e-5)
New-style numerical derivatives using manifold_traits.
Unit3 normal() const
Return the normal.
Vector3 localCoordinates(const OrientedPlane3 &s) const
The local coordinates function.
#define GTSAM_CONCEPT_TESTABLE_INST(T)
Definition: Testable.h:174


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:48:26