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

covering.hpp « geometry - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f7fd6869afdffad576ba860af059320e9940ed5f (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once
#include "../geometry/point2d.hpp"
#include "../base/assert.hpp"
#include "../base/base.hpp"
#include "../base/logging.hpp"
#include "../base/math.hpp"
#include "../std/algorithm.hpp"
#include "../std/utility.hpp"
#include "../std/vector.hpp"

namespace covering
{

enum CellObjectIntersection
{
  CELL_OBJECT_NO_INTERSECTION = 0,
  CELL_OBJECT_INTERSECT = 1,
  CELL_INSIDE_OBJECT = 2,
  OBJECT_INSIDE_CELL = 3
};

template <class CellIdT>
inline CellObjectIntersection IntersectCellWithLine(CellIdT const cell,
                                                    m2::PointD const & a,
                                                    m2::PointD const & b)
{
  pair<uint32_t, uint32_t> const xy = cell.XY();
  uint32_t const r = cell.Radius();
  m2::PointD const cellCorners[4] =
  {
    m2::PointD(xy.first - r, xy.second - r),
    m2::PointD(xy.first - r, xy.second + r),
    m2::PointD(xy.first + r, xy.second + r),
    m2::PointD(xy.first + r, xy.second - r)
  };
  for (int i = 0; i < 4; ++i)
    if (m2::SegmentsIntersect(a, b, cellCorners[i], cellCorners[i == 0 ? 3 : i - 1]))
      return CELL_OBJECT_INTERSECT;
  if (xy.first - r <= a.x && a.x <= xy.first + r && xy.second - r <= a.y && a.y <= xy.second + r)
    return OBJECT_INSIDE_CELL;
  return CELL_OBJECT_NO_INTERSECTION;
}

template <class CellIdT>
CellObjectIntersection IntersectCellWithTriangle(
    CellIdT const cell, m2::PointD const & a, m2::PointD const & b, m2::PointD const & c)
{
  CellObjectIntersection const i1 = IntersectCellWithLine(cell, a, b);
  if (i1 == CELL_OBJECT_INTERSECT)
    return CELL_OBJECT_INTERSECT;
  CellObjectIntersection const i2 = IntersectCellWithLine(cell, b, c);
  if (i2 == CELL_OBJECT_INTERSECT)
    return CELL_OBJECT_INTERSECT;
  CellObjectIntersection const i3 = IntersectCellWithLine(cell, c, a);
  if (i3 == CELL_OBJECT_INTERSECT)
    return CELL_OBJECT_INTERSECT;
  ASSERT_EQUAL(i1, i2, (cell, a, b, c));
  ASSERT_EQUAL(i2, i3, (cell, a, b, c));
  ASSERT_EQUAL(i3, i1, (cell, a, b, c));
  return i1;
}

template <class CellIdT, typename IntersectF>
void CoverObject(IntersectF const & intersect, uint64_t cellPenaltyArea, vector<CellIdT> & out,
                 CellIdT cell = CellIdT::Root())
{
  uint64_t const cellArea = my::sq(uint64_t(1 << (CellIdT::DEPTH_LEVELS - 1 - cell.Level())));
  CellObjectIntersection const intersection = intersect(cell);

  if (intersection == CELL_OBJECT_NO_INTERSECTION)
    return;
  if (intersection == CELL_INSIDE_OBJECT ||
      cell.Level() == CellIdT::DEPTH_LEVELS - 1 ||
      cellPenaltyArea >= cellArea)
  {
    out.push_back(cell);
    return;
  }

  vector<CellIdT> subdiv;
  for (uint8_t i = 0; i < 4; ++i)
    CoverObject(intersect, cellPenaltyArea, subdiv, cell.Child(i));


  uint64_t subdivArea = 0;
  for (size_t i = 0; i < subdiv.size(); ++i)
    subdivArea += my::sq(uint64_t(1 << (CellIdT::DEPTH_LEVELS - 1 - subdiv[i].Level())));

  ASSERT(!subdiv.empty(), (cellPenaltyArea, out, cell));

  if (subdiv.empty() ||
      cellPenaltyArea * (int(subdiv.size()) - 1) >= cellArea - subdivArea)
    out.push_back(cell);
  else
    out.insert(out.end(), subdiv.begin(), subdiv.end());
}


} // namespace covering