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

locality_finder.hpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 56d510bbf7e01c2d7fb4eb05968e0e0364ddd564 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#pragma once

#include "search/cities_boundaries_table.hpp"

#include "indexer/feature_utils.hpp"
#include "indexer/mwm_set.hpp"
#include "indexer/rank_table.hpp"

#include "platform/preferred_languages.hpp"

#include "coding/string_utf8_multilang.hpp"

#include "geometry/point2d.hpp"
#include "geometry/rect2d.hpp"
#include "geometry/tree4d.hpp"

#include "base/macros.hpp"

#include <cstdint>
#include <limits>
#include <map>
#include <memory>
#include <unordered_set>
#include <utility>

class DataSource;

namespace search
{
class VillagesCache;

struct LocalityItem
{
  using Boundaries = CitiesBoundariesTable::Boundaries;

  LocalityItem(StringUtf8Multilang const & names, m2::PointD const & center,
               Boundaries const & boundaries, uint64_t population, FeatureID const & id);

  bool GetName(int8_t lang, string & name) const { return m_names.GetString(lang, name); }

  bool GetSpecifiedOrDefaultName(int8_t lang, string & name) const
  {
    return GetName(lang, name) || GetName(StringUtf8Multilang::kDefaultCode, name);
  }

  bool GetReadableName(string & name) const
  {
    auto const mwmInfo = m_id.m_mwmId.GetInfo();

    if (!mwmInfo)
      return false;

    auto const deviceLang = StringUtf8Multilang::GetLangIndex(languages::GetCurrentNorm());
    feature::GetReadableName(mwmInfo->GetRegionData(), m_names, deviceLang,
                             false /* allowTranslit */, name);

    return !name.empty();
  }

  StringUtf8Multilang m_names;
  m2::PointD m_center;
  Boundaries m_boundaries;
  uint64_t m_population;
  FeatureID m_id;
};

string DebugPrint(LocalityItem const & item);

class LocalitySelector
{
public:
  LocalitySelector(m2::PointD const & p);

  void operator()(LocalityItem const & item);

  template <typename Fn>
  bool WithBestLocality(Fn && fn) const
  {
    if (!m_locality)
      return false;
    fn(*m_locality);
    return true;
  }

private:
  m2::PointD const m_p;

  bool m_inside = false;
  double m_score = std::numeric_limits<double>::max();
  LocalityItem const * m_locality = nullptr;
};

class LocalityFinder
{
public:
  class Holder
  {
  public:
    Holder(double radiusMeters);

    bool IsCovered(m2::RectD const & rect) const;
    void SetCovered(m2::PointD const & p);

    void Add(LocalityItem const & item);
    void ForEachInVicinity(m2::RectD const & rect, LocalitySelector & selector) const;

    m2::RectD GetRect(m2::PointD const & p) const;
    m2::RectD GetDRect(m2::PointD const & p) const;

    void Clear();

  private:
    double const m_radiusMeters;
    m4::Tree<bool> m_coverage;
    m4::Tree<LocalityItem> m_localities;

    DISALLOW_COPY_AND_MOVE(Holder);
  };

  LocalityFinder(DataSource const & dataSource, CitiesBoundariesTable const & boundaries,
                 VillagesCache & villagesCache);

  template <typename Fn>
  bool GetLocality(m2::PointD const & p, Fn && fn)
  {
    m2::RectD const crect = m_cities.GetRect(p);
    m2::RectD const vrect = m_villages.GetRect(p);

    LoadVicinity(p, !m_cities.IsCovered(crect) /* loadCities */,
                 !m_villages.IsCovered(vrect) /* loadVillages */);

    LocalitySelector selector(p);
    m_cities.ForEachInVicinity(crect, selector);
    m_villages.ForEachInVicinity(vrect, selector);

    return selector.WithBestLocality(std::forward<Fn>(fn));
  }

  void ClearCache();

private:
  void LoadVicinity(m2::PointD const & p, bool loadCities, bool loadVillages);
  void UpdateMaps();

  DataSource const & m_dataSource;
  CitiesBoundariesTable const & m_boundariesTable;
  VillagesCache & m_villagesCache;

  Holder m_cities;
  Holder m_villages;

  m4::Tree<MwmSet::MwmId> m_maps;
  MwmSet::MwmId m_worldId;
  bool m_mapsLoaded;

  std::unique_ptr<RankTable> m_ranks;

  std::map<MwmSet::MwmId, std::unordered_set<uint32_t>> m_loadedIds;
};
}  // namespace search