From 3fd6d40fafa8d3dad6f63835e02420d95168eae6 Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Mon, 13 Sep 2010 03:24:35 +0000 Subject: [PATCH] comments and some extra tests --- geometry/tests/testTensors.cpp | 93 ++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 10 deletions(-) diff --git a/geometry/tests/testTensors.cpp b/geometry/tests/testTensors.cpp index 0c19eb3c7..7c2a10326 100644 --- a/geometry/tests/testTensors.cpp +++ b/geometry/tests/testTensors.cpp @@ -35,23 +35,32 @@ Index<4, 'B'> B; /* ************************************************************************* */ TEST(Tensor1, Basics) { + // you can create 1-tensors corresponding to 2D homogeneous points + // using the function point2h in projectiveGeometry.* Point2h p = point2h(1, 2, 3), q = point2h(2, 4, 6); + + // equality tests always take tensor expressions, not tensors themselves + // the difference is that a tensor expression has indices CHECK(p(a)==p(a)) CHECK(assert_equality(p(a),p(a))) - CHECK(assert_equivalent(p(a),q(a))) - DOUBLES_EQUAL(sqrt(14),norm(p(a)),1e-9) CHECK(assert_equality(p(a)*2,q(a))) + CHECK(assert_equivalent(p(a),q(a))) // projectively equivalent + + // and you can take a norm, typically for normalization to the sphere + DOUBLES_EQUAL(sqrt(14),norm(p(a)),1e-9) } /* ************************************************************************* */ TEST( Tensor1, Incidence2D) { + // 2D lines are created with line2h Line2h l = line2h(-13, 5, 1); Point2h p = point2h(1, 2, 3), q = point2h(2, 5, 1); - // incidence + // Incidence between a line and a point is checked with simple contraction + // It does not matter which index you use, but it has to be of dimension 3 DOUBLES_EQUAL(l(a)*p(a),0,1e-9) - DOUBLES_EQUAL(l(a)*q(a),0,1e-9) + DOUBLES_EQUAL(l(b)*q(b),0,1e-9) DOUBLES_EQUAL(p(a)*l(a),0,1e-9) DOUBLES_EQUAL(q(a)*l(a),0,1e-9) } @@ -59,10 +68,11 @@ TEST( Tensor1, Incidence2D) /* ************************************************************************* */ TEST( Tensor1, Incidence3D) { + // similar constructs exist for 3D points and planes Plane3h pi = plane3h(0, 1, 0, -2); Point3h P = point3h(0, 2, 0, 1), Q = point3h(1, 2, 0, 1); - // incidence + // Incidence is checked similarly DOUBLES_EQUAL(pi(A)*P(A),0,1e-9) DOUBLES_EQUAL(pi(A)*Q(A),0,1e-9) DOUBLES_EQUAL(P(A)*pi(A),0,1e-9) @@ -72,15 +82,33 @@ TEST( Tensor1, Incidence3D) /* ************************************************************************* */ // Tensor2 /* ************************************************************************* */ -TEST( Tensor2, Outer3) +TEST( Tensor2, Outer33) { Line2h l1 = line2h(1, 2, 3), l2 = line2h(1, 3, 5); - double data[3][3] = { { 1, 2, 3 }, { 2, 4, 6 }, { 3, 6, 9 } }; + // We can also create tensors directly from data + double data[3][3] = { { 1, 2, 3 }, { 3, 6, 9 }, {5, 10, 15} }; Tensor2<3, 3> expected(data); - CHECK(expected(a,b) == expected(a,b)) - CHECK(expected(a,b) == l1(a) * l1(b)) - CHECK(expected(a,b).swap() == l1(b) * l1(a)) + // in this case expected(0) == {1,2,3} + Line2h l0 = expected(a,b)(0); + CHECK(l0(a) == l1(a)) + + // And we create rank 2 tensors from the outer product of two rank 1 tensors + CHECK(expected(a,b) == l1(a) * l2(b)) + + // swap just swaps how you access a tensor, but note the data is the same + CHECK(assert_equality(expected(a,b).swap(), l2(b) * l1(a))); +} + +/* ************************************************************************* */ +TEST( Tensor2, AnotherOuter33) +{ + // first cube point from testFundamental, projected in left and right +// Point2h p = point2h(0, -1, 2), q = point2h(-2, -1, 2); +// print(p(a)*q(b)); +// print(p(b)*q(a)); +// print(q(a)*p(b)); +// print(q(b)*p(a)); } /* ************************************************************************* */ @@ -118,6 +146,51 @@ TEST( Tensor2, ProjectiveCamera) CHECK(assert_equality(p(a),M(a,A)*P(A))) } +/* ************************************************************************* */ +namespace camera { + // to specify the tensor M(a,A), we need to give four 2D points + double data[4][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } }; + ProjectiveCamera M(data); + Matrix matrix = Matrix_(4,3,1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.); + Vector vector = Vector_( 12,1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.); +} + +/* ************************************************************************* */ +TEST( Tensor2, reshape ) +{ + // it is annoying that a camera can only be reshaped to a 4*3 +// print(camera::M(a,A)); + Matrix actual = reshape(camera::M(a,A),4,3); + EQUALITY(camera::matrix,actual); +} + +/* ************************************************************************* */ +TEST( Tensor2, toVector ) +{ + // Vectors are created with the leftmost indices iterating the fastest + Vector actual = toVector(camera::M(a,A)); + CHECK(assert_equal(camera::vector,actual)); +} + +/* ************************************************************************* */ +TEST( Tensor2, reshape2 ) +{ + Tensor2<3,4> actual = reshape2<3,4>(camera::vector); + CHECK(assert_equality(camera::M(a,A),actual(a,A))); +} + +/* ************************************************************************* */ +TEST( Tensor2, reshape_33_to_9 ) +{ + double data[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; + FundamentalMatrix F(data); + Matrix matrix = Matrix_(1,9,1.,2.,3.,4.,5.,6.,7.,8.,9.); + Matrix actual = reshape(F(a,b),1,9); + EQUALITY(matrix,actual); + Vector v = Vector_( 9,1.,2.,3.,4.,5.,6.,7.,8.,9.); + CHECK(assert_equality(F(a,b),reshape2<3, 3> (v)(a,b))); +} + /* ************************************************************************* */ // Tensor3 /* ************************************************************************* */