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

drawing_rules.cpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8b94774c05b4523180ce2c2363f17fb8509da5e0 (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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
#include "indexer/drawing_rules.hpp"
#include "indexer/classificator.hpp"
#include "indexer/drules_include.hpp"
#include "indexer/map_style_reader.hpp"
#include "indexer/scales.hpp"

#include "defines.hpp"

#include "platform/platform.hpp"

#include "base/logging.hpp"

#include "std/bind.hpp"
#include "std/iterator_facade.hpp"
#include "std/unordered_map.hpp"

#include <google/protobuf/text_format.h>

namespace
{
  uint32_t const DEFAULT_BG_COLOR = 0xEEEEDD;

  drule::text_type_t GetTextType(string const & text)
  {
    if (text == "addr:housename")
      return drule::text_type_housename;
    else if (text == "addr:housenumber")
      return drule::text_type_housenumber;

    return drule::text_type_name;
  }
} // namespace

namespace drule
{

BaseRule::BaseRule() : m_type(node | way)
{}

BaseRule::~BaseRule()
{}

void BaseRule::CheckCacheSize(size_t s)
{
  m_id1.resize(s);
  MakeEmptyID();
}

uint32_t BaseRule::GetID(size_t threadSlot) const
{
  ASSERT(m_id1.size() > threadSlot, ());
  return m_id1[threadSlot];
}

void BaseRule::SetID(size_t threadSlot, uint32_t id) const
{
  ASSERT(m_id1.size() > threadSlot, ());
  m_id1[threadSlot] = id;
}

void BaseRule::MakeEmptyID(size_t threadSlot)
{
  ASSERT(m_id1.size() > threadSlot, ());
  m_id1[threadSlot] = empty_id;
}

void BaseRule::MakeEmptyID()
{
  for (size_t i = 0; i < m_id1.size(); ++i)
    MakeEmptyID(i);
}

LineDefProto const * BaseRule::GetLine() const
{
  return 0;
}

AreaRuleProto const * BaseRule::GetArea() const
{
  return 0;
}

SymbolRuleProto const * BaseRule::GetSymbol() const
{
  return 0;
}

CaptionDefProto const * BaseRule::GetCaption(int) const
{
  return 0;
}

text_type_t BaseRule::GetCaptionTextType(int) const
{
  return text_type_name;
}

ShieldRuleProto const * BaseRule::GetShield() const
{
  return nullptr;
}

bool BaseRule::TestFeature(FeatureType const & ft, int /* zoom */) const
{
  if (nullptr == m_selector)
    return true;
  return m_selector->Test(ft);
}

void BaseRule::SetSelector(unique_ptr<ISelector> && selector)
{
  m_selector = move(selector);
}

RulesHolder::RulesHolder()
  : m_bgColors(scales::UPPER_STYLE_SCALE+1, DEFAULT_BG_COLOR)
{}

RulesHolder::~RulesHolder()
{
  Clean();
}

void RulesHolder::Clean()
{
  for (size_t i = 0; i < m_container.size(); ++i)
  {
    rule_vec_t & v = m_container[i];
    for (size_t j = 0; j < v.size(); ++j)
      delete v[j];
    v.clear();
  }

  m_rules.clear();
  m_colors.clear();
}

Key RulesHolder::AddRule(int scale, rule_type_t type, BaseRule * p)
{
  ASSERT ( 0 <= scale && scale <= scales::GetUpperStyleScale(), (scale) );
  ASSERT ( 0 <= type && type < count_of_rules, () );

  m_container[type].push_back(p);

  vector<uint32_t> & v = m_rules[scale][type];
  v.push_back(static_cast<uint32_t>(m_container[type].size()-1));

  int const ret = static_cast<int>(v.size() - 1);
  Key k(scale, type, ret);
  ASSERT ( Find(k) == p, (ret) );
  return k;
}

BaseRule const * RulesHolder::Find(Key const & k) const
{
  rules_map_t::const_iterator i = m_rules.find(k.m_scale);
  if (i == m_rules.end()) return 0;

  vector<uint32_t> const & v = (i->second)[k.m_type];

  ASSERT ( k.m_index >= 0, (k.m_index) );
  if (static_cast<size_t>(k.m_index) < v.size())
    return m_container[k.m_type][v[k.m_index]];
  else
    return 0;
}

uint32_t RulesHolder::GetBgColor(int scale) const
{
  ASSERT_LESS(scale, static_cast<int>(m_bgColors.size()), ());
  ASSERT_GREATER_OR_EQUAL(scale, 0, ());
  return m_bgColors[scale];
}

uint32_t RulesHolder::GetColor(std::string const & name) const
{
  auto const it = m_colors.find(name);
  if (it == m_colors.end())
  {
    LOG(LWARNING, ("Requested color '" + name + "' is not found"));
    return 0;
  }
  return it->second;
}

void RulesHolder::ClearCaches()
{
  ForEachRule(bind(static_cast<void (BaseRule::*)()>(&BaseRule::MakeEmptyID), _4));
}

void RulesHolder::ResizeCaches(size_t s)
{
  ForEachRule(bind(&BaseRule::CheckCacheSize, _4, s));
}

namespace
{
RulesHolder & rules(MapStyle mapStyle)
{
  static RulesHolder h[MapStyleCount];
  return h[mapStyle];
}
} // namespace

RulesHolder & rules()
{
  return rules(GetStyleReader().GetCurrentStyle());
}

namespace
{
  namespace proto_rules
  {
    class Line : public BaseRule
    {
      LineDefProto m_line;
    public:
      Line(LineRuleProto const & r)
      {
        m_line.set_color(r.color());
        m_line.set_width(r.width());
        m_line.set_join(r.join());
        m_line.set_cap(r.cap());
        if (r.has_dashdot())
          *(m_line.mutable_dashdot()) = r.dashdot();
        if (r.has_pathsym())
          *(m_line.mutable_pathsym()) = r.pathsym();
      }

      virtual LineDefProto const * GetLine() const { return &m_line; }
    };

    class Area : public BaseRule
    {
      AreaRuleProto m_area;
    public:
      Area(AreaRuleProto const & r) : m_area(r) {}

      virtual AreaRuleProto const * GetArea() const { return &m_area; }
    };

    class Symbol : public BaseRule
    {
      SymbolRuleProto m_symbol;
    public:
      Symbol(SymbolRuleProto const & r) : m_symbol(r)
      {
        SetType(r.apply_for_type());
      }

      virtual SymbolRuleProto const * GetSymbol() const { return &m_symbol; }
    };

    template <class T> class CaptionT : public BaseRule
    {
      T m_caption;

      text_type_t m_textTypePrimary;
      text_type_t m_textTypeSecondary;

    public:
      CaptionT(T const & r) : m_caption(r)
        , m_textTypePrimary(text_type_name)
        , m_textTypeSecondary(text_type_name)
      {
        if (!m_caption.primary().text().empty())
          m_textTypePrimary = GetTextType(m_caption.primary().text());

        if (m_caption.has_secondary() && !m_caption.secondary().text().empty())
          m_textTypeSecondary = GetTextType(m_caption.secondary().text());
      }

      virtual CaptionDefProto const * GetCaption(int i) const
      {
        if (i == 0)
          return &m_caption.primary();
        else
        {
          ASSERT_EQUAL ( i, 1, () );
          if (m_caption.has_secondary())
            return &m_caption.secondary();
          else
            return 0;
        }
      }

      virtual text_type_t GetCaptionTextType(int i) const
      {
        if (i == 0)
          return m_textTypePrimary;
        else
        {
          ASSERT_EQUAL ( i, 1, () );
          return m_textTypeSecondary;
        }
      }
    };

    typedef CaptionT<CaptionRuleProto> Caption;
    typedef CaptionT<PathTextRuleProto> PathText;

    class Shield : public BaseRule
    {
      ShieldRuleProto m_shield;
    public:
      Shield(ShieldRuleProto const & r) : m_shield(r) {}

      virtual ShieldRuleProto const * GetShield() const { return &m_shield; }
    };
  }

  class DoSetIndex
  {
  public:
    ContainerProto m_cont;

  private:
    vector<string> m_names;

    typedef ClassifElementProto ElementT;

    class RandI : public iterator_facade<
        RandI,
        ElementT const,
        random_access_traversal_tag>
    {
      ContainerProto const * m_cont;
    public:
      int m_index;

      RandI() : m_cont(0), m_index(-1) {}
      RandI(ContainerProto const & cont, int ind) : m_cont(&cont), m_index(ind) {}

      ElementT const & dereference() const { return m_cont->cont(m_index); }
      bool equal(RandI const & r) const { return m_index == r.m_index; }
      void increment() { ++m_index; }
      void decrement() { --m_index; }
      void advance(size_t n) { m_index += n; }
      difference_type distance_to(RandI const & r) const { return (r.m_index - m_index); }
    };

    struct less_name
    {
      bool operator() (ElementT const & e1, ElementT const & e2) const
      {
        return (e1.name() < e2.name());
      }
      bool operator() (string const & e1, ElementT const & e2) const
      {
        return (e1 < e2.name());
      }
      bool operator() (ElementT const & e1, string const & e2) const
      {
        return (e1.name() < e2);
      }
    };

    int FindIndex() const
    {
      string name = m_names[0];
      for (size_t i = 1; i < m_names.size(); ++i)
        name = name + "-" + m_names[i];

      int const count = m_cont.cont_size();
      int const i = lower_bound(RandI(m_cont, 0), RandI(m_cont, count), name, less_name()).m_index;
      ASSERT_GREATER_OR_EQUAL(i, 0, ());
      ASSERT_LESS_OR_EQUAL(i, count, ());

      if (i < count && m_cont.cont(i).name() == name)
        return i;
      else
        return -1;
    }

    RulesHolder & m_holder;

    template <class TRule, class TProtoRule>
    void AddRule(ClassifObject * p, int scale, rule_type_t type, TProtoRule const & rule,
                 vector<string> const & apply_if)
    {
      unique_ptr<ISelector> selector;
      if (!apply_if.empty())
      {
        selector = ParseSelector(apply_if);
        if (selector == nullptr)
        {
          LOG(LERROR, ("Runtime selector has not been created:", apply_if));
          return;
        }
      }

      BaseRule * obj = new TRule(rule);
      obj->SetSelector(move(selector));
      Key k = m_holder.AddRule(scale, type, obj);
      p->SetVisibilityOnScale(true, scale);
      k.SetPriority(rule.priority());
      p->AddDrawRule(k);
    }

    static void DrawElementGetApplyIf(DrawElementProto const & de, vector<string> & apply_if)
    {
      apply_if.clear();
      apply_if.reserve(de.apply_if_size());
      for (int i = 0; i < de.apply_if_size(); ++i)
        apply_if.emplace_back(de.apply_if(i));
    }

  public:
    DoSetIndex(RulesHolder & holder)
      : m_holder(holder) {}

    void operator() (ClassifObject * p)
    {
      m_names.push_back(p->GetName());

      int const i = FindIndex();
      if (i != -1)
      {
        vector<string> apply_if;

        ClassifElementProto const & ce = m_cont.cont(i);
        for (int j = 0; j < ce.element_size(); ++j)
        {
          DrawElementProto const & de = ce.element(j);

          using namespace proto_rules;

          DrawElementGetApplyIf(de, apply_if);

          for (int k = 0; k < de.lines_size(); ++k)
            AddRule<Line>(p, de.scale(), line, de.lines(k), apply_if);

          if (de.has_area())
            AddRule<Area>(p, de.scale(), area, de.area(), apply_if);

          if (de.has_symbol())
            AddRule<Symbol>(p, de.scale(), symbol, de.symbol(), apply_if);

          if (de.has_caption())
            AddRule<Caption>(p, de.scale(), caption, de.caption(), apply_if);

          if (de.has_path_text())
            AddRule<PathText>(p, de.scale(), pathtext, de.path_text(), apply_if);

          if (de.has_shield())
            AddRule<Shield>(p, de.scale(), shield, de.shield(), apply_if);
        }
      }

      p->ForEachObject(ref(*this));

      m_names.pop_back();
    }
  };
} // namespace

void RulesHolder::InitBackgroundColors(ContainerProto const & cont)
{
  // WARNING!
  // Background color is not specified in current format.
  // Therefore, we use color of "natural-land" area element as background color.
  // If such element is not present or if the element is not area then we use default background color

  // Default background color is any found color, it is used if color is not specified for scale
  uint32_t bgColorDefault = DEFAULT_BG_COLOR;

  // Background colors specified for scales
  unordered_map<int, uint32_t> bgColorForScale;

  // Find the "natural-land" classification element
  for (int i = 0; i < cont.cont_size(); ++i)
  {
    ClassifElementProto const & ce = cont.cont(i);
    if (ce.name() == "natural-land")
    {
      // Take any area draw element
      for (int j = 0; j < ce.element_size(); ++j)
      {
        DrawElementProto const & de = ce.element(j);
        if (de.has_area())
        {
          // Take the color of the draw element
          AreaRuleProto const & rule = de.area();
          bgColorDefault = rule.color();

          if (de.scale() != 0)
            bgColorForScale.insert(make_pair(de.scale(), rule.color()));
        }
      }
      break;
    }
  }

  ASSERT_EQUAL(m_bgColors.size(), scales::UPPER_STYLE_SCALE+1, ());
  for (int scale = 0; scale <= scales::UPPER_STYLE_SCALE; ++scale)
  {
    auto const i = bgColorForScale.find(scale);
    if (bgColorForScale.end() != i)
      m_bgColors[scale] = i->second;
    else
      m_bgColors[scale] = bgColorDefault;
  }
}

void RulesHolder::InitColors(ContainerProto const & cp)
{
  if (!cp.has_colors())
    return;

  ASSERT_EQUAL(m_colors.size(), 0, ());
  for (int i = 0; i < cp.colors().value_size(); i++)
  {
    ColorElementProto const & proto = cp.colors().value(i);
    m_colors.insert(std::make_pair(proto.name(), proto.color()));
  }
}

void RulesHolder::LoadFromBinaryProto(string const & s)
{
  Clean();

  DoSetIndex doSet(*this);

  CHECK ( doSet.m_cont.ParseFromString(s), ("Error in proto loading!") );

  classif().GetMutableRoot()->ForEachObject(ref(doSet));

  InitBackgroundColors(doSet.m_cont);
  InitColors(doSet.m_cont);
}

void LoadRules()
{
  string buffer;
  GetStyleReader().GetDrawingRulesReader().ReadAsString(buffer);
  rules().LoadFromBinaryProto(buffer);
}

} // namespace drule