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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
#include "generator/coastlines_generator.hpp"
#include "generator/feature_builder.hpp"
#include "coding/point_coding.hpp"
#include "geometry/region2d/binary_operators.hpp"
#include "base/string_utils.hpp"
#include "base/logging.hpp"
#include <condition_variable>
#include <functional>
#include <thread>
#include <utility>
using namespace std;
using namespace feature;
using RegionT = m2::RegionI;
using PointT = m2::PointI;
using RectT = m2::RectI;
CoastlineFeaturesGenerator::CoastlineFeaturesGenerator(uint32_t coastType)
: m_merger(kPointCoordBits), m_coastType(coastType)
{
}
namespace
{
m2::RectD GetLimitRect(RegionT const & rgn)
{
RectT r = rgn.GetRect();
return m2::RectD(r.minX(), r.minY(), r.maxX(), r.maxY());
}
inline PointT D2I(m2::PointD const & p)
{
m2::PointU const pu = PointDToPointU(p, kPointCoordBits);
return PointT(static_cast<int32_t>(pu.x), static_cast<int32_t>(pu.y));
}
template <class Tree> class DoCreateRegion
{
Tree & m_tree;
RegionT m_rgn;
m2::PointD m_pt;
bool m_exist;
public:
template <typename T>
DoCreateRegion(T && tree) : m_tree(std::forward<T>(tree)), m_exist(false) {}
bool operator()(m2::PointD const & p)
{
// This logic is to skip last polygon point (that is equal to first).
if (m_exist)
{
// add previous point to region
m_rgn.AddPoint(D2I(m_pt));
}
else
m_exist = true;
// save current point
m_pt = p;
return true;
}
void EndRegion()
{
m_tree.Add(m_rgn, GetLimitRect(m_rgn));
m_rgn = RegionT();
m_exist = false;
}
};
} // namespace
void CoastlineFeaturesGenerator::AddRegionToTree(FeatureBuilder const & fb)
{
ASSERT ( fb.IsGeometryClosed(), () );
DoCreateRegion<TTree> createRgn(m_tree);
fb.ForEachGeometryPointEx(createRgn);
}
void CoastlineFeaturesGenerator::Process(FeatureBuilder const & fb)
{
if (fb.IsGeometryClosed())
AddRegionToTree(fb);
else
m_merger(fb);
}
namespace
{
class DoAddToTree : public FeatureEmitterIFace
{
CoastlineFeaturesGenerator & m_rMain;
size_t m_notMergedCoastsCount;
size_t m_totalNotMergedCoastsPoints;
public:
explicit DoAddToTree(CoastlineFeaturesGenerator & rMain)
: m_rMain(rMain), m_notMergedCoastsCount(0), m_totalNotMergedCoastsPoints(0) {}
virtual void operator() (FeatureBuilder const & fb)
{
if (fb.IsGeometryClosed())
m_rMain.AddRegionToTree(fb);
else
{
base::GeoObjectId const firstWay = fb.GetFirstOsmId();
base::GeoObjectId const lastWay = fb.GetLastOsmId();
if (firstWay == lastWay)
LOG(LINFO, ("Not merged coastline, way", firstWay.GetSerialId(), "(", fb.GetPointsCount(),
"points)"));
else
LOG(LINFO, ("Not merged coastline, ways", firstWay.GetSerialId(), "to",
lastWay.GetSerialId(), "(", fb.GetPointsCount(), "points)"));
++m_notMergedCoastsCount;
m_totalNotMergedCoastsPoints += fb.GetPointsCount();
}
}
bool HasNotMergedCoasts() const
{
return m_notMergedCoastsCount != 0;
}
size_t GetNotMergedCoastsCount() const
{
return m_notMergedCoastsCount;
}
size_t GetNotMergedCoastsPoints() const
{
return m_totalNotMergedCoastsPoints;
}
};
}
bool CoastlineFeaturesGenerator::Finish()
{
DoAddToTree doAdd(*this);
m_merger.DoMerge(doAdd);
if (doAdd.HasNotMergedCoasts())
{
LOG(LINFO, ("Total not merged coasts:", doAdd.GetNotMergedCoastsCount()));
LOG(LINFO, ("Total points in not merged coasts:", doAdd.GetNotMergedCoastsPoints()));
return false;
}
return true;
}
namespace
{
class DoDifference
{
RectT m_src;
vector<RegionT> m_res;
vector<m2::PointD> m_points;
public:
explicit DoDifference(RegionT const & rgn)
{
m_res.push_back(rgn);
m_src = rgn.GetRect();
}
void operator()(RegionT const & r)
{
// if r is fully inside source rect region,
// put it to the result vector without any intersection
if (m_src.IsRectInside(r.GetRect()))
m_res.push_back(r);
else
m2::IntersectRegions(m_res.front(), r, m_res);
}
void operator()(PointT const & p)
{
m_points.push_back(PointUToPointD(
m2::PointU(static_cast<uint32_t>(p.x), static_cast<uint32_t>(p.y)), kPointCoordBits));
}
size_t GetPointsCount() const
{
size_t count = 0;
for (size_t i = 0; i < m_res.size(); ++i)
count += m_res[i].GetPointsCount();
return count;
}
void AssignGeometry(FeatureBuilder & fb)
{
for (size_t i = 0; i < m_res.size(); ++i)
{
m_points.clear();
m_points.reserve(m_res[i].Size() + 1);
m_res[i].ForEachPoint(ref(*this));
fb.AddPolygon(m_points);
}
}
};
}
class RegionInCellSplitter final
{
public:
using TCell = RectId;
using TIndex = m4::Tree<m2::RegionI>;
using TProcessResultFunc = function<void(TCell const &, DoDifference &)>;
static int constexpr kStartLevel = 4;
static int constexpr kHighLevel = 10;
static int constexpr kMaxPoints = 20000;
protected:
struct Context
{
mutex mutexTasks;
list<TCell> listTasks;
condition_variable listCondVar;
size_t inWork = 0;
TProcessResultFunc processResultFunc;
};
Context & m_ctx;
TIndex const & m_index;
RegionInCellSplitter(Context & ctx,TIndex const & index)
: m_ctx(ctx), m_index(index)
{}
public:
static bool Process(size_t numThreads, size_t baseScale, TIndex const & index,
TProcessResultFunc funcResult)
{
Context ctx;
for (size_t i = 0; i < TCell::TotalCellsOnLevel(baseScale); ++i)
ctx.listTasks.push_back(TCell::FromBitsAndLevel(i, static_cast<int>(baseScale)));
ctx.processResultFunc = funcResult;
vector<RegionInCellSplitter> instances;
vector<thread> threads;
for (size_t i = 0; i < numThreads; ++i)
{
instances.emplace_back(RegionInCellSplitter(ctx, index));
threads.emplace_back(instances.back());
}
for (auto & thread : threads)
thread.join();
// return true if listTask has no error cells
return ctx.listTasks.empty();
}
bool ProcessCell(TCell const & cell)
{
// get rect cell
double minX, minY, maxX, maxY;
CellIdConverter<MercatorBounds, TCell>::GetCellBounds(cell, minX, minY, maxX, maxY);
// create rect region
PointT arr[] = {D2I(m2::PointD(minX, minY)), D2I(m2::PointD(minX, maxY)),
D2I(m2::PointD(maxX, maxY)), D2I(m2::PointD(maxX, minY))};
RegionT rectR(arr, arr + ARRAY_SIZE(arr));
// Do 'and' with all regions and accumulate the result, including bound region.
// In 'odd' parts we will have an ocean.
DoDifference doDiff(rectR);
m_index.ForEachInRect(GetLimitRect(rectR), bind<void>(ref(doDiff), placeholders::_1));
// Check if too many points for feature.
if (cell.Level() < kHighLevel && doDiff.GetPointsCount() >= kMaxPoints)
return false;
m_ctx.processResultFunc(cell, doDiff);
return true;
}
void operator()()
{
// thread main loop
while (true)
{
unique_lock<mutex> lock(m_ctx.mutexTasks);
m_ctx.listCondVar.wait(lock, [this]{return (!m_ctx.listTasks.empty() || m_ctx.inWork == 0);});
if (m_ctx.listTasks.empty())
break;
TCell currentCell = m_ctx.listTasks.front();
m_ctx.listTasks.pop_front();
++m_ctx.inWork;
lock.unlock();
bool const done = ProcessCell(currentCell);
lock.lock();
// return to queue not ready cells
if (!done)
{
for (int8_t i = 0; i < TCell::MAX_CHILDREN; ++i)
m_ctx.listTasks.push_back(currentCell.Child(i));
}
--m_ctx.inWork;
m_ctx.listCondVar.notify_all();
}
}
};
void CoastlineFeaturesGenerator::GetFeatures(vector<FeatureBuilder> & features)
{
size_t const maxThreads = thread::hardware_concurrency();
CHECK_GREATER(maxThreads, 0, ("Not supported platform"));
mutex featuresMutex;
RegionInCellSplitter::Process(
maxThreads, RegionInCellSplitter::kStartLevel, m_tree,
[&features, &featuresMutex, this](RegionInCellSplitter::TCell const & cell, DoDifference & cellData)
{
FeatureBuilder fb;
fb.SetCoastCell(cell.ToInt64(RegionInCellSplitter::kHighLevel + 1));
cellData.AssignGeometry(fb);
fb.SetArea();
fb.AddType(m_coastType);
// Should represent non-empty geometry
CHECK_GREATER(fb.GetPolygonsCount(), 0, ());
CHECK_GREATER_OR_EQUAL(fb.GetPointsCount(), 3, ());
// save result
lock_guard<mutex> lock(featuresMutex);
features.emplace_back(move(fb));
});
}
|