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

polygonizer.hpp « generator - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 36022245626b9400f46a56cd282bd5bc831a55ac (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#pragma once

#include "borders_loader.hpp"
#include "feature_builder.hpp"

#include "../indexer/feature_visibility.hpp"
#include "../indexer/cell_id.hpp"

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

#include "../coding/file_writer.hpp"

#include "../base/base.hpp"
#include "../base/buffer_vector.hpp"
#include "../base/macros.hpp"

#include "../std/scoped_ptr.hpp"
#include "../std/string.hpp"


#ifndef PARALLEL_POLYGONIZER
#define PARALLEL_POLYGONIZER 1
#endif

#if PARALLEL_POLYGONIZER
#include <QSemaphore>
#include <QThreadPool>
#include <QMutex>
#include <QMutexLocker>
#endif


namespace feature
{
  // Groups features according to country polygons
  template <class FeatureOutT>
  class Polygonizer
  {
    string m_prefix;
    string m_suffix;

    vector<FeatureOutT*> m_Buckets;
    vector<string> m_Names;
    borders::CountriesContainerT m_countries;

#if PARALLEL_POLYGONIZER
    QThreadPool m_ThreadPool;
    QSemaphore m_ThreadPoolSemaphore;
    QMutex m_EmitFeatureMutex;
#endif

  public:
    template <class TInfo>
    explicit Polygonizer(TInfo const & info)
      : m_prefix(info.m_datFilePrefix), m_suffix(info.m_datFileSuffix)
#if PARALLEL_POLYGONIZER
    , m_ThreadPoolSemaphore(m_ThreadPool.maxThreadCount() * 8)
#endif
    {
#if PARALLEL_POLYGONIZER
      LOG(LINFO, ("Polygonizer thread pool threads:", m_ThreadPool.maxThreadCount()));
#endif

      if (info.m_splitByPolygons)
      {
        CHECK(borders::LoadCountriesList(info.m_datFilePrefix, m_countries),
            ("Error loading country polygons files"));
      }
      else
      {
        // Insert fake country polygon equal to whole world to
        // create only one output file which contains all features
        m_countries.Add(borders::CountryPolygons(), MercatorBounds::FullRect());
      }
    }
    ~Polygonizer()
    {
      Finish();
      for_each(m_Buckets.begin(), m_Buckets.end(), DeleteFunctor());
    }

    struct PointChecker
    {
      borders::RegionsContainerT const & m_regions;
      bool m_belongs;

      PointChecker(borders::RegionsContainerT const & regions)
        : m_regions(regions), m_belongs(false) {}

      bool operator()(m2::PointD const & pt)
      {
        m_regions.ForEachInRect(m2::RectD(pt, pt), bind<void>(ref(*this), _1, cref(pt)));
        return !m_belongs;
      }

      void operator() (borders::Region const & rgn, borders::Region::ValueT const & point)
      {
        if (!m_belongs)
          m_belongs = rgn.Contains(point);
      }
    };

    class InsertCountriesPtr
    {
      typedef buffer_vector<borders::CountryPolygons const *, 32> vec_type;
      vec_type & m_vec;

    public:
      InsertCountriesPtr(vec_type & vec) : m_vec(vec) {}
      void operator() (borders::CountryPolygons const & c)
      {
        m_vec.push_back(&c);
      }
    };

    void operator () (FeatureBuilder1 const & fb)
    {
      buffer_vector<borders::CountryPolygons const *, 32> vec;
      m_countries.ForEachInRect(fb.GetLimitRect(), InsertCountriesPtr(vec));

      switch (vec.size())
      {
      case 0:
        break;
      case 1:
        EmitFeature(vec[0], fb);
        break;
      default:
        {
#if PARALLEL_POLYGONIZER
          m_ThreadPoolSemaphore.acquire();
          m_ThreadPool.start(new PolygonizerTask(this, vec, fb));
#else
          PolygonizerTask task(this, vec, fb);
          task.RunBase();
#endif
        }
      }
    }

    void Finish()
    {
#if PARALLEL_POLYGONIZER
      m_ThreadPool.waitForDone();
#endif
    }

    void EmitFeature(borders::CountryPolygons const * country, FeatureBuilder1 const & fb)
    {
#if PARALLEL_POLYGONIZER
      QMutexLocker mutexLocker(&m_EmitFeatureMutex);
      UNUSED_VALUE(mutexLocker);
#endif
      if (country->m_index == -1)
      {
        m_Names.push_back(country->m_name);
        m_Buckets.push_back(new FeatureOutT(m_prefix + country->m_name + m_suffix));
        country->m_index = m_Buckets.size()-1;
      }

      (*(m_Buckets[country->m_index]))(fb);
    }

    vector<string> const & Names() const
    {
      return m_Names;
    }

  private:
    friend class PolygonizerTask;

    class PolygonizerTask
#if PARALLEL_POLYGONIZER
      : public QRunnable
#endif
    {
    public:
      PolygonizerTask(Polygonizer * pPolygonizer,
                      buffer_vector<borders::CountryPolygons const *, 32> const & countries,
                      FeatureBuilder1 const & fb)
        : m_pPolygonizer(pPolygonizer), m_Countries(countries), m_FB(fb) {}

      void RunBase()
      {
        for (size_t i = 0; i < m_Countries.size(); ++i)
        {
          PointChecker doCheck(m_Countries[i]->m_regions);
          m_FB.ForEachGeometryPoint(doCheck);

          if (doCheck.m_belongs)
            m_pPolygonizer->EmitFeature(m_Countries[i], m_FB);
        }
      }

#if PARALLEL_POLYGONIZER
      void run()
      {
        RunBase();

        m_pPolygonizer->m_ThreadPoolSemaphore.release();
      }
#endif

    private:
      Polygonizer * m_pPolygonizer;
      buffer_vector<borders::CountryPolygons const *, 32> m_Countries;
      FeatureBuilder1 m_FB;
    };
  };
}