testPoint3.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 
18 #include <gtsam/base/Testable.h>
20 #include <gtsam/geometry/Point3.h>
21 
22 using namespace std::placeholders;
23 using namespace gtsam;
24 
27 
28 static Point3 P(0.2, 0.7, -2);
29 
30 //******************************************************************************
31 TEST(Point3 , Constructor) {
32  Point3 p;
33 }
34 
35 //******************************************************************************
36 TEST(Point3 , Concept) {
38  GTSAM_CONCEPT_ASSERT(IsManifold<Point3>);
40 }
41 
42 //******************************************************************************
43 TEST(Point3 , Invariants) {
44  Point3 p1(1, 2, 3), p2(4, 5, 6);
45  EXPECT(check_group_invariants(p1, p2));
46  EXPECT(check_manifold_invariants(p1, p2));
47 }
48 
49 /* ************************************************************************* */
50 TEST(Point3, Lie) {
51  Point3 p1(1, 2, 3);
52  Point3 p2(4, 5, 6);
53  Matrix H1, H2;
54 
55  EXPECT(assert_equal(Point3(5, 7, 9), traits<Point3>::Compose(p1, p2, H1, H2)));
56  EXPECT(assert_equal(I_3x3, H1));
57  EXPECT(assert_equal(I_3x3, H2));
58 
59  EXPECT(assert_equal(Point3(3, 3, 3), traits<Point3>::Between(p1, p2, H1, H2)));
60  EXPECT(assert_equal(-I_3x3, H1));
61  EXPECT(assert_equal(I_3x3, H2));
62 
65 }
66 
67 /* ************************************************************************* */
69  CHECK(P * 3 == 3 * P);
70  CHECK(assert_equal<Point3>(Point3(-1, -5, -6), -Point3(1, 5, 6)));
71  CHECK(assert_equal<Point3>(Point3(2, 5, 6), Point3(1, 4, 5) + Point3(1, 1, 1)));
72  CHECK(assert_equal<Point3>(Point3(0, 3, 4), Point3(1, 4, 5) - Point3(1, 1, 1)));
73  CHECK(assert_equal<Point3>(Point3(2, 8, 6), Point3(1, 4, 3) * 2));
74  CHECK(assert_equal<Point3>(Point3(2, 2, 6), 2 * Point3(1, 1, 3)));
75  CHECK(assert_equal<Point3>(Point3(1, 2, 3), Point3(2, 4, 6) / 2));
76 }
77 
78 /* ************************************************************************* */
81  Point3 Q(0,0,0);
83 }
84 
85 /* ************************************************************************* */
87  Point3 origin(0,0,0), ones(1, 1, 1);
88  CHECK(origin.dot(Point3(1, 1, 0)) == 0);
89  CHECK(ones.dot(Point3(1, 1, 0)) == 2);
90 
91  Point3 p(1, 0.2, 0.3);
92  Point3 q = p + Point3(0.5, 0.2, -3.0);
93  Point3 r = p + Point3(0.8, 0, 0);
94  Point3 t = p + Point3(0, 0.3, -0.4);
95  EXPECT(assert_equal(1.130000, p.dot(p), 1e-8));
96  EXPECT(assert_equal(0.770000, p.dot(q), 1e-5));
97  EXPECT(assert_equal(1.930000, p.dot(r), 1e-5));
98  EXPECT(assert_equal(1.070000, p.dot(t), 1e-5));
99 
100  // Use numerical derivatives to calculate the expected Jacobians
101  Matrix H1, H2;
102  std::function<double(const Point3&, const Point3&)> f =
103  [](const Point3& p, const Point3& q) { return gtsam::dot(p, q); };
104  {
105  gtsam::dot(p, q, H1, H2);
106  EXPECT(assert_equal(numericalDerivative21<double,Point3>(f, p, q), H1, 1e-9));
107  EXPECT(assert_equal(numericalDerivative22<double,Point3>(f, p, q), H2, 1e-9));
108  }
109  {
110  gtsam::dot(p, r, H1, H2);
111  EXPECT(assert_equal(numericalDerivative21<double,Point3>(f, p, r), H1, 1e-9));
112  EXPECT(assert_equal(numericalDerivative22<double,Point3>(f, p, r), H2, 1e-9));
113  }
114  {
115  gtsam::dot(p, t, H1, H2);
116  EXPECT(assert_equal(numericalDerivative21<double,Point3>(f, p, t), H1, 1e-9));
117  EXPECT(assert_equal(numericalDerivative22<double,Point3>(f, p, t), H2, 1e-9));
118  }
119 }
120 
121 /* ************************************************************************* */
123  Matrix aH1, aH2;
124  std::function<Point3(const Point3&, const Point3&)> f =
125  [](const Point3& p, const Point3& q) { return gtsam::cross(p, q); };
126  const Point3 omega(0, 1, 0), theta(4, 6, 8);
127  cross(omega, theta, aH1, aH2);
130 }
131 
132 /* ************************************************************************* */
133 TEST( Point3, cross2) {
134  Point3 p(1, 0.2, 0.3);
135  Point3 q = p + Point3(0.5, 0.2, -3.0);
136  Point3 r = p + Point3(0.8, 0, 0);
137  EXPECT(assert_equal(Point3(0, 0, 0), p.cross(p), 1e-8));
138  EXPECT(assert_equal(Point3(-0.66, 3.15, 0.1), p.cross(q), 1e-5));
139  EXPECT(assert_equal(Point3(0, 0.24, -0.16), p.cross(r), 1e-5));
140 
141  // Use numerical derivatives to calculate the expected Jacobians
142  Matrix H1, H2;
143  std::function<Point3(const Point3&, const Point3&)> f =
144  [](const Point3& p, const Point3& q) { return gtsam::cross(p, q); };
145  {
146  gtsam::cross(p, q, H1, H2);
147  EXPECT(assert_equal(numericalDerivative21<Point3,Point3>(f, p, q), H1, 1e-9));
148  EXPECT(assert_equal(numericalDerivative22<Point3,Point3>(f, p, q), H2, 1e-9));
149  }
150  {
151  gtsam::cross(p, r, H1, H2);
152  EXPECT(assert_equal(numericalDerivative21<Point3,Point3>(f, p, r), H1, 1e-9));
153  EXPECT(assert_equal(numericalDerivative22<Point3,Point3>(f, p, r), H2, 1e-9));
154  }
155 }
156 
157 //*************************************************************************
159  Matrix actualH;
160  Point3 point(1, -2, 3); // arbitrary point
161  Point3 expected(point / sqrt(14.0));
162  EXPECT(assert_equal(expected, normalize(point, actualH), 1e-8));
163  std::function<Point3(const Point3&)> fn = [](const Point3& p) { return normalize(p); };
164  Matrix expectedH = numericalDerivative11<Point3, Point3>(fn, point);
165  EXPECT(assert_equal(expectedH, actualH, 1e-8));
166 }
167 
168 //*************************************************************************
170  Point3 expected(2, 2, 2);
171  Point3 a1(0, 0, 0), a2(1, 2, 3), a3(5, 4, 3);
172  std::vector<Point3> a_points{a1, a2, a3};
173  Point3 actual = mean(a_points);
174  EXPECT(assert_equal(expected, actual));
175 }
176 
177 TEST(Point3, mean_pair) {
178  Point3 a_mean(2, 2, 2), b_mean(-1, 1, 0);
179  Point3Pair expected = {a_mean, b_mean};
180  Point3 a1(0, 0, 0), a2(1, 2, 3), a3(5, 4, 3);
181  Point3 b1(-1, 0, 0), b2(-2, 4, 0), b3(0, -1, 0);
182  std::vector<Point3Pair> point_pairs{{a1, b1}, {a2, b2}, {a3, b3}};
183  Point3Pair actual = means(point_pairs);
184  EXPECT(assert_equal(expected.first, actual.first));
185  EXPECT(assert_equal(expected.second, actual.second));
186 }
187 
188 //*************************************************************************
189 double norm_proxy(const Point3& point) {
190  return double(point.norm());
191 }
192 
193 TEST (Point3, norm) {
194  Matrix actualH;
195  Point3 point(3,4,5); // arbitrary point
196  double expected = sqrt(50);
197  EXPECT_DOUBLES_EQUAL(expected, norm3(point, actualH), 1e-8);
198  Matrix expectedH = numericalDerivative11<double, Point3>(norm_proxy, point);
199  EXPECT(assert_equal(expectedH, actualH, 1e-8));
200 }
201 
202 /* ************************************************************************* */
203 double testFunc(const Point3& P, const Point3& Q) {
204  return distance3(P, Q);
205 }
206 
208  Point3 P(1., 12.8, -32.), Q(52.7, 4.9, -13.3);
209  Matrix H1, H2;
210  double d = distance3(P, Q, H1, H2);
211  double expectedDistance = 55.542686;
212  Matrix numH1 = numericalDerivative21(testFunc, P, Q);
213  Matrix numH2 = numericalDerivative22(testFunc, P, Q);
214  DOUBLES_EQUAL(expectedDistance, d, 1e-5);
215  EXPECT(assert_equal(numH1, H1, 1e-8));
216  EXPECT(assert_equal(numH2, H2, 1e-8));
217 }
218 
219 /* ************************************************************************* */
220 int main() {
221  TestResult tr;
222  return TestRegistry::runAllTests(tr);
223 }
224 /* ************************************************************************* */
225 
Point2 a1
Definition: testPose2.cpp:769
#define CHECK(condition)
Definition: Test.h:108
Quaternion Q
#define GTSAM_CONCEPT_ASSERT(concept)
Definition: base/concepts.h:22
Concept check for values that can be used in unit tests.
static int runAllTests(TestResult &result)
Eigen::Vector3d Vector3
Definition: Vector.h:43
double dot(const V1 &a, const V2 &b)
Definition: Vector.h:195
Vector3f p1
Point2 a3
Definition: testPose2.cpp:771
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set origin
Matrix expected
Definition: testMatrix.cpp:971
#define DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:141
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:40
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
Vector2 b2(4, -5)
Double_ distance(const OrientedPlane3_ &p)
Some functions to compute numerical derivatives.
Vector Space concept.
Definition: VectorSpace.h:470
#define GTSAM_CONCEPT_LIE_INST(T)
Definition: Lie.h:372
#define EXPECT_DOUBLES_EQUAL(expected, actual, threshold)
Definition: Test.h:161
internal::FixedSizeMatrix< Y, X1 >::type numericalDerivative21(const std::function< Y(const X1 &, const X2 &)> &h, const X1 &x1, const X2 &x2, double delta=1e-5)
static void normalize(Signature::Row &row)
Definition: Signature.cpp:88
static const Vector2 mean(20, 40)
Scalar EIGEN_BLAS_FUNC() dot(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Point3 cross(const Point3 &p, const Point3 &q, OptionalJacobian< 3, 3 > H1, OptionalJacobian< 3, 3 > H2)
cross product
Definition: Point3.cpp:64
#define EXPECT(condition)
Definition: Test.h:150
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
TEST(Point3, Constructor)
Definition: testPoint3.cpp:31
Vector::Scalar omega(const Vector &t, const Vector &s, RealScalar angle)
Definition: IDRS.h:36
Array< double, 1, 3 > e(1./3., 0.5, 2.)
EIGEN_DEVICE_FUNC const Scalar & q
traits
Definition: chartTesting.h:28
Point2 a2
Definition: testPose2.cpp:770
Vector2 b3(3, -6)
Point2Pair means(const std::vector< Point2Pair > &abPointPairs)
Calculate the two means of a set of Point2 pairs.
Definition: Point2.cpp:116
Annotation to mark enums as an arithmetic type.
Definition: attr.h:116
The quaternion class used to represent 3D orientations and rotations.
3D Point
float * p
internal::FixedSizeMatrix< Y, X2 >::type numericalDerivative22(std::function< Y(const X1 &, const X2 &)> h, const X1 &x1, const X2 &x2, double delta=1e-5)
static Point3 p2
double testFunc(const Point3 &P, const Point3 &Q)
Definition: testPoint3.cpp:203
static Point3 P(0.2, 0.7, -2)
double norm3(const Point3 &p, OptionalJacobian< 1, 3 > H)
Distance of the point from the origin, with Jacobian.
Definition: Point3.cpp:41
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
Vector3 Point3
Definition: Point3.h:38
double distance3(const Point3 &p1, const Point3 &q, OptionalJacobian< 1, 3 > H1, OptionalJacobian< 1, 3 > H2)
distance between two points
Definition: Point3.cpp:27
double norm_proxy(const Point3 &point)
Definition: testPoint3.cpp:189
int main()
Definition: testPoint3.cpp:220
#define GTSAM_CONCEPT_TESTABLE_INST(T)
Definition: Testable.h:176
Point2 t(10, 10)
std::pair< Point3, Point3 > Point3Pair
Definition: Point3.h:42
Vector2 b1(2, -1)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:38:55