diff --git a/gtsam/geometry/Point2.h b/gtsam/geometry/Point2.h index 3d7dc4d78..025d0c1f5 100644 --- a/gtsam/geometry/Point2.h +++ b/gtsam/geometry/Point2.h @@ -110,6 +110,9 @@ namespace gtsam { /** norm of point */ double norm() const; + /** creates a unit vector */ + Point2 unit() const { return *this/norm(); } + /** distance between two points */ inline double dist(const Point2& p2) const { return (p2 - *this).norm(); diff --git a/gtsam/geometry/tests/testPoint2.cpp b/gtsam/geometry/tests/testPoint2.cpp index 30e2cf83e..e8fbb2201 100644 --- a/gtsam/geometry/tests/testPoint2.cpp +++ b/gtsam/geometry/tests/testPoint2.cpp @@ -28,18 +28,18 @@ TEST( Point2, expmap) d(0) = 1; d(1) = -1; Point2 a(4, 5), b = a.expmap(d), c(5, 4); - CHECK(assert_equal(b,c)); + EXPECT(assert_equal(b,c)); } /* ************************************************************************* */ TEST( Point2, arithmetic) { - CHECK(assert_equal( Point2(-5,-6), -Point2(5,6) )); - CHECK(assert_equal( Point2(5,6), Point2(4,5)+Point2(1,1))); - CHECK(assert_equal( Point2(3,4), Point2(4,5)-Point2(1,1) )); - CHECK(assert_equal( Point2(8,6), Point2(4,3)*2)); - CHECK(assert_equal( Point2(4,6), 2*Point2(2,3))); - CHECK(assert_equal( Point2(2,3), Point2(4,6)/2)); + EXPECT(assert_equal( Point2(-5,-6), -Point2(5,6) )); + EXPECT(assert_equal( Point2(5,6), Point2(4,5)+Point2(1,1))); + EXPECT(assert_equal( Point2(3,4), Point2(4,5)-Point2(1,1) )); + EXPECT(assert_equal( Point2(8,6), Point2(4,3)*2)); + EXPECT(assert_equal( Point2(4,6), 2*Point2(2,3))); + EXPECT(assert_equal( Point2(2,3), Point2(4,6)/2)); } /* ************************************************************************* */ @@ -52,6 +52,15 @@ TEST( Point2, norm) DOUBLES_EQUAL( 5,(p2-p1).norm(),1e-6); } +/* ************************************************************************* */ +TEST( Point2, unit) +{ + Point2 p0(10.0, 0.0), p1(0.0,-10.0), p2(10.0, 10.0); + EXPECT(assert_equal(Point2(1.0, 0.0), p0.unit(), 1e-6)); + EXPECT(assert_equal(Point2(0.0,-1.0), p1.unit(), 1e-6)); + EXPECT(assert_equal(Point2(sqrt(2)/2.0, sqrt(2)/2.0), p2.unit(), 1e-6)); +} + /* ************************************************************************* */ int main() { TestResult tr; return TestRegistry::runAllTests(tr); } /* ************************************************************************* */