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

convex_hull.cpp « geometry - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0daef2b97997e7d0ef221636e4dec5a3a8f84e96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "geometry/convex_hull.hpp"

#include "geometry/robust_orientation.hpp"

#include "base/stl_helpers.hpp"

#include <algorithm>

using namespace std;

namespace m2
{
namespace
{
// Checks whether (p1 - p) x (p2 - p) > 0.
bool IsCCW(PointD const & p1, PointD const & p2, PointD const & p, double eps)
{
  return robust::OrientedS(p1, p2, p) > eps;
}

bool IsContinuedBy(vector<PointD> const & hull, PointD const & p, double eps)
{
  auto const n = hull.size();
  if (n < 2)
    return true;

  auto const & p1 = hull[n - 2];
  auto const & p2 = hull[n - 1];

  // Checks whether (p2 - p1) x (p - p2) > 0.
  return IsCCW(p, p1, p2, eps);
}

vector<PointD> BuildConvexHull(vector<PointD> points, double eps)
{
  base::SortUnique(points);

  auto const n = points.size();

  if (n < 2)
    return points;

  iter_swap(points.begin(), min_element(points.begin(), points.end()));

  auto const pivot = points[0];

  sort(points.begin() + 1, points.end(), [&pivot, &eps](PointD const & lhs, PointD const & rhs) {
    if (IsCCW(lhs, rhs, pivot, eps))
      return true;
    if (IsCCW(rhs, lhs, pivot, eps))
      return false;
    return lhs.SquaredLength(pivot) < rhs.SquaredLength(pivot);
  });

  vector<PointD> hull;

  for (auto const & p : points)
  {
    while (!IsContinuedBy(hull, p, eps))
      hull.pop_back();
    hull.push_back(p);
  }

  return hull;
}
}  // namespace

ConvexHull::ConvexHull(vector<PointD> const & points, double eps)
  : m_hull(BuildConvexHull(points, eps))
{
}
}  // namespace m2