Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--geometry/geometry_tests/region_test.cpp45
-rw-r--r--geometry/region2d.hpp12
2 files changed, 56 insertions, 1 deletions
diff --git a/geometry/geometry_tests/region_test.cpp b/geometry/geometry_tests/region_test.cpp
index 46b1887095..a8a79a6b5d 100644
--- a/geometry/geometry_tests/region_test.cpp
+++ b/geometry/geometry_tests/region_test.cpp
@@ -33,6 +33,25 @@ void Test()
}
+UNIT_TEST(Region)
+{
+ typedef m2::PointD P;
+ P p1[] = { P(0.1, 0.2) };
+
+ m2::Region<P> region(p1, p1 + ARRAY_SIZE(p1));
+ TEST(!region.IsValid(), ());
+
+ {
+ P p2[] = { P(1.0, 2.0), P(55.0, 33.0) };
+ region.Assign(p2, p2 + ARRAY_SIZE(p2));
+ }
+ TEST(!region.IsValid(), ());
+
+ region.AddPoint(P(34.4, 33.2));
+ TEST(region.IsValid(), ());
+
+}
+
UNIT_TEST(Region_Contains)
{
Test<m2::PointU>();
@@ -68,3 +87,29 @@ UNIT_TEST(Region_Contains)
TEST(!region.Contains(P(1, 1)), ());
}
}
+
+template <class TPoint>
+struct PointsSummator
+{
+ double m_xSumm;
+ double m_ySumm;
+ PointsSummator() : m_xSumm(0), m_ySumm(0) {}
+ void operator()(TPoint const & pt)
+ {
+ m_xSumm += pt.x;
+ m_ySumm += pt.y;
+ }
+};
+
+UNIT_TEST(Region_ForEachPoint)
+{
+ typedef m2::PointF P;
+ P const points[] = { P(0.0, 1.0), P(1.0, 2.0), P(10.5, 11.5) };
+ m2::Region<P> region(points, points + ARRAY_SIZE(points));
+
+ PointsSummator<P> s;
+ region.ForEachPoint(s);
+
+ TEST_EQUAL(s.m_xSumm, 11.5, ());
+ TEST_EQUAL(s.m_ySumm, 14.5, ());
+}
diff --git a/geometry/region2d.hpp b/geometry/region2d.hpp
index 27b1761ac7..2b34aff741 100644
--- a/geometry/region2d.hpp
+++ b/geometry/region2d.hpp
@@ -10,6 +10,8 @@ namespace m2
template <class TPoint>
class Region
{
+ typedef vector<TPoint> internal_container;
+
public:
typedef TPoint value_type;
typedef typename TPoint::value_type coord_type;
@@ -40,6 +42,14 @@ namespace m2
m_rect.Add(pt);
}
+ template <class TFunctor>
+ void ForEachPoint(TFunctor & f) const
+ {
+ for (typename internal_container::const_iterator it = m_points.begin();
+ it != m_points.end(); ++it)
+ f(*it);
+ }
+
m2::Rect<coord_type> Rect() const { return m_rect; }
bool IsValid() const { return m_points.size() > 2; }
@@ -165,7 +175,7 @@ namespace m2
*/
private:
- vector<TPoint> m_points;
+ internal_container m_points;
m2::Rect<coord_type> m_rect;
};