From 2317f55b5a9460654f0916f6b85d7fa23837d3be Mon Sep 17 00:00:00 2001 From: Sergey Magidovich Date: Sun, 7 Feb 2016 16:39:15 +0300 Subject: Edits migration. --- geometry/geometry_tests/algorithm_test.cpp | 166 +++++++++++++++++++++++++++++ geometry/geometry_tests/geometry_tests.pro | 1 + 2 files changed, 167 insertions(+) create mode 100644 geometry/geometry_tests/algorithm_test.cpp (limited to 'geometry/geometry_tests') diff --git a/geometry/geometry_tests/algorithm_test.cpp b/geometry/geometry_tests/algorithm_test.cpp new file mode 100644 index 0000000000..3f349f9791 --- /dev/null +++ b/geometry/geometry_tests/algorithm_test.cpp @@ -0,0 +1,166 @@ +#include "testing/testing.hpp" + +#include "geometry/algorithm.hpp" + +#include "std/vector.hpp" + +using m2::PointD; +using m2::RectD; +using m2::CalculatePolyLineCenter; +using m2::CalculatePointOnSurface; +using m2::CalculateBoundingBox; + +namespace +{ +PointD GetPolyLineCenter(vector const & points) +{ + CalculatePolyLineCenter doCalc; + for (auto const & p : points) + doCalc(p); + return doCalc.GetCenter(); +} + +RectD GetBoundingBox(vector const & points) +{ + CalculateBoundingBox doCalc; + for (auto const p : points) + doCalc(p); + return doCalc.GetBoundingBox(); +} + +PointD GetPointOnSurface(vector const & points) +{ + ASSERT(!points.empty() && points.size() % 3 == 0, ()); + CalculatePointOnSurface doCalc(GetBoundingBox(points)); + for (auto i = 0; i < points.size() - 3; i += 3) + doCalc(points[i], points[i + 1], points[i + 2]); + return doCalc.GetCenter(); + +} +} // namespace + +UNIT_TEST(CalculatePolyLineCenter) +{ + { + vector const points { + {0, 0}, + {1, 1}, + {2, 2} + }; + TEST_EQUAL(GetPolyLineCenter(points), PointD(1, 1), ()); + } + { + vector const points { + {0, 2}, + {1, 1}, + {2, 2} + }; + TEST_EQUAL(GetPolyLineCenter(points), PointD(1, 1), ()); + } + { + vector const points { + {1, 1}, + {2, 2}, + {4, 4}, + }; + TEST_EQUAL(GetPolyLineCenter(points), PointD(2.5, 2.5), ()); + } + { + vector const points { + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0} + }; + // Also logs a warning message. + TEST_EQUAL(GetPolyLineCenter(points), PointD(0, 0), ()); + } + { + vector const points { + {0, 0}, + {0, 0}, + {0, 0}, + {0, 0.000001}, + {0, 0.000001}, + {0, 0.000001}, + {0, 0.000001} + }; + // Also logs a warning message. + TEST(GetPolyLineCenter(points).EqualDxDy(PointD(0, .0000005), 1e-7), ()); + } +} + +UNIT_TEST(CalculateCenter) +{ + { + vector const points { + {2, 2} + }; + TEST_EQUAL(GetBoundingBox(points), RectD({2, 2}, {2, 2}), ()); + } + { + vector const points { + {0, 0}, + {1, 1}, + {2, 2} + }; + TEST_EQUAL(GetBoundingBox(points), RectD({0, 0}, {2, 2}), ()); + } + { + vector const points { + {0, 2}, + {1, 1}, + {2, 2}, + {1, 2}, + {2, 2}, + {2, 0} + }; + TEST_EQUAL(GetBoundingBox(points), RectD({0, 0}, {2, 2}), ()); + } +} + +UNIT_TEST(CalculatePointOnSurface) +{ + { + vector const points { + {0, 0}, + {1, 1}, + {2, 2} + }; + TEST_EQUAL(GetPointOnSurface(points), PointD(1, 1), ()); + } + { + vector const points { + {0, 2}, + {1, 1}, + {2, 2}, + {1, 2}, + {2, 2}, + {2, 0} + }; + TEST_EQUAL(GetPointOnSurface(points), PointD(1, 1), ()); + } + { + vector const points { + {0, 0}, {1, 1}, {2, 0}, + {1, 1}, {2, 0}, {3, 1}, + {2, 0}, {3, 1}, {4, 0}, + + {4, 0}, {3, 1}, {4, 2}, + {3, 1}, {4, 2}, {3, 3}, + {4, 2}, {3, 3}, {4, 4}, + + {3, 3}, {4, 4}, {2, 4}, + {3, 3}, {2, 4}, {1, 3}, + {1, 3}, {2, 4}, {0, 4}, + + {0, 4}, {1, 3}, {0, 2}, + {1, 3}, {0, 2}, {1, 1}, // Center of this triangle is used as a result. + {0, 2}, {1, 1}, {0, 0}, + }; + TEST_EQUAL(GetPointOnSurface(points), PointD(2.0 / 3.0, 2), ()); + } +} diff --git a/geometry/geometry_tests/geometry_tests.pro b/geometry/geometry_tests/geometry_tests.pro index e9c04c136c..050968f793 100644 --- a/geometry/geometry_tests/geometry_tests.pro +++ b/geometry/geometry_tests/geometry_tests.pro @@ -19,6 +19,7 @@ HEADERS += \ SOURCES += \ ../../testing/testingmain.cpp \ + algorithm_test.cpp \ angle_test.cpp \ anyrect_test.cpp \ cellid_test.cpp \ -- cgit v1.2.3