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

overlay.cpp « graphics - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5a8f341829b487dd7aa928a22b839204a44ecac3 (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
#include "../base/SRC_FIRST.hpp"

#include "overlay.hpp"
#include "overlay_renderer.hpp"
#include "text_element.hpp"

#include "../base/logging.hpp"
#include "../base/stl_add.hpp"

#include "../std/bind.hpp"
#include "../std/vector.hpp"


namespace graphics
{
  bool betterOverlayElement(shared_ptr<OverlayElement> const & l,
                            shared_ptr<OverlayElement> const & r)
  {
    /// "frozen" object shouldn't be popped out.
    if (r->isFrozen())
      return false;

    /// for the composite elements, collected in OverlayRenderer to replace the part elements
    return l->priority() > r->priority();
  }

  m2::RectD const OverlayElementTraits::LimitRect(shared_ptr<OverlayElement> const & elem)
  {
    return elem->roughBoundRect();
  }

  void DrawIfNotCancelled(OverlayRenderer * r,
                          shared_ptr<OverlayElement> const & e,
                          math::Matrix<double, 3, 3> const & m)
  {
    if (!r->isCancelled())
      e->draw(r, m);
  }

  void Overlay::draw(OverlayRenderer * r, math::Matrix<double, 3, 3> const & m)
  {
    m_tree.ForEach(bind(&DrawIfNotCancelled, r, _1, cref(m)));
  }

  Overlay::Overlay()
    : m_couldOverlap(true)
  {}

  void Overlay::setCouldOverlap(bool flag)
  {
    m_couldOverlap = flag;
  }

  template <typename Tree>
  void offsetTree(Tree & tree, m2::PointD const & offs, m2::RectD const & r)
  {
    m2::AnyRectD AnyRect(r);
    typedef typename Tree::elem_t elem_t;
    vector<elem_t> elems;
    tree.ForEach(MakeBackInsertFunctor(elems));
    tree.Clear();

    for (typename vector<elem_t>::iterator it = elems.begin(); it != elems.end(); ++it)
    {
      (*it)->offset(offs);
      vector<m2::AnyRectD> const & aaLimitRects = (*it)->boundRects();
      bool doAppend = false;

      (*it)->setIsNeedRedraw(false);
      (*it)->setIsFrozen(true);

      bool hasInside = false;
      bool hasOutside = false;

      for (int i = 0; i < aaLimitRects.size(); ++i)
      {
        bool isPartInsideRect = AnyRect.IsRectInside(aaLimitRects[i]);

        if (isPartInsideRect)
        {
          if (hasOutside)
          {
            /// intersecting
            (*it)->setIsNeedRedraw(true);
            doAppend = true;
            break;
          }
          else
          {
            hasInside = true;
            doAppend = true;
            continue;
          }
        }

        bool isRectInsidePart = aaLimitRects[i].IsRectInside(AnyRect);

        if (isRectInsidePart)
        {
          doAppend = true;
          break;
        }

        bool isPartIntersectRect = AnyRect.IsIntersect(aaLimitRects[i]);

        if (isPartIntersectRect)
        {
          /// intersecting
          (*it)->setIsNeedRedraw(true);
          doAppend = true;
          break;
        }

        /// the last case remains here - part rect is outside big rect
        if (hasInside)
        {
          (*it)->setIsNeedRedraw(true);
          doAppend = true;
          break;
        }
        else
          hasOutside = true;
      }

      if (doAppend)
        tree.Add(*it);
    }
  }

  void Overlay::offset(m2::PointD const & offs, m2::RectD const & rect)
  {
    offsetTree(m_tree, offs, rect);
  }

  size_t Overlay::getElementsCount() const
  {
    return m_tree.GetSize();
  }

  void Overlay::lock()
  {
    m_mutex.Lock();
  }

  void Overlay::unlock()
  {
    m_mutex.Unlock();
  }

  void Overlay::clear()
  {
    m_tree.Clear();
  }

  void Overlay::addOverlayElement(shared_ptr<OverlayElement> const & oe)
  {
    m_tree.Add(oe);
  }

  struct DoPreciseSelectByPoint
  {
    m2::PointD m_pt;
    list<shared_ptr<OverlayElement> > * m_elements;

    DoPreciseSelectByPoint(m2::PointD const & pt, list<shared_ptr<OverlayElement> > * elements)
      : m_pt(pt), m_elements(elements)
    {}

    void operator()(shared_ptr<OverlayElement> const & e)
    {
      if (e->hitTest(m_pt))
        m_elements->push_back(e);
    }
  };

  struct DoPreciseSelectByRect
  {
    m2::AnyRectD m_rect;
    list<shared_ptr<OverlayElement> > * m_elements;

    DoPreciseSelectByRect(m2::RectD const & rect,
                          list<shared_ptr<OverlayElement> > * elements)
      : m_rect(rect),
        m_elements(elements)
    {}

    void operator()(shared_ptr<OverlayElement> const & e)
    {
      vector<m2::AnyRectD> const & rects = e->boundRects();

      for (vector<m2::AnyRectD>::const_iterator it = rects.begin();
           it != rects.end();
           ++it)
      {
        m2::AnyRectD const & rect = *it;

        if (m_rect.IsIntersect(rect))
        {
          m_elements->push_back(e);
          break;
        }
      }
    }
  };

  struct DoPreciseIntersect
  {
    shared_ptr<OverlayElement> m_oe;
    bool * m_isIntersect;

    DoPreciseIntersect(shared_ptr<OverlayElement> const & oe, bool * isIntersect)
      : m_oe(oe),
        m_isIntersect(isIntersect)
    {}

    void operator()(shared_ptr<OverlayElement> const & e)
    {
      if (*m_isIntersect)
        return;

      if (m_oe->m_userInfo == e->m_userInfo)
        return;

      vector<m2::AnyRectD> const & lr = m_oe->boundRects();
      vector<m2::AnyRectD> const & rr = e->boundRects();

      for (vector<m2::AnyRectD>::const_iterator lit = lr.begin(); lit != lr.end(); ++lit)
      {
        for (vector<m2::AnyRectD>::const_iterator rit = rr.begin(); rit != rr.end(); ++rit)
        {
          *m_isIntersect = lit->IsIntersect(*rit);
          if (*m_isIntersect)
            return;
        }
      }
    }
  };

  void Overlay::selectOverlayElements(m2::RectD const & rect, list<shared_ptr<OverlayElement> > & res) const
  {
    DoPreciseSelectByRect fn(rect, &res);
    m_tree.ForEachInRect(rect, fn);
  }

  void Overlay::replaceOverlayElement(shared_ptr<OverlayElement> const & oe)
  {
    bool isIntersect = false;
    DoPreciseIntersect fn(oe, &isIntersect);
    m_tree.ForEachInRect(oe->roughBoundRect(), fn);
    if (isIntersect)
      m_tree.ReplaceIf(oe, &betterOverlayElement);
    else
      m_tree.Add(oe);
  }

  void Overlay::removeOverlayElement(shared_ptr<OverlayElement> const & oe, m2::RectD const & r)
  {
    m_tree.Erase(oe, r);
  }

  void Overlay::processOverlayElement(shared_ptr<OverlayElement> const & oe, math::Matrix<double, 3, 3> const & m)
  {
    oe->setTransformation(m);
    if (oe->isValid())
      processOverlayElement(oe);
  }

  void Overlay::processOverlayElement(shared_ptr<OverlayElement> const & oe)
  {
    if (oe->isValid())
    {
      if (m_couldOverlap)
        addOverlayElement(oe);
      else
        replaceOverlayElement(oe);
    }
  }

  bool greater_priority(shared_ptr<OverlayElement> const & l,
                        shared_ptr<OverlayElement> const & r)
  {
    return l->priority() > r->priority();
  }

  void Overlay::merge(Overlay const & layer, math::Matrix<double, 3, 3> const & m)
  {
    vector<shared_ptr<OverlayElement> > v;

    /// 1. collecting all elements from tree
    layer.m_tree.ForEach(MakeBackInsertFunctor(v));

    /// 2. sorting by priority, so the more important ones comes first
    sort(v.begin(), v.end(), &greater_priority);

    /// 3. merging them into the infoLayer starting from most
    /// important one to optimize the space usage.
    for_each(v.begin(), v.end(), bind(&Overlay::processOverlayElement, this, _1, cref(m)));
  }

  void Overlay::merge(Overlay const & infoLayer)
  {
    vector<shared_ptr<OverlayElement> > v;

    /// 1. collecting all elements from tree
    infoLayer.m_tree.ForEach(MakeBackInsertFunctor(v));

    /// 2. sorting by priority, so the more important ones comes first
    sort(v.begin(), v.end(), &greater_priority);

    /// 3. merging them into the infoLayer starting from most
    /// important one to optimize the space usage.
    for_each(v.begin(), v.end(), bind(&Overlay::processOverlayElement, this, _1));
  }

  void Overlay::clip(m2::RectI const & r)
  {
    vector<shared_ptr<OverlayElement> > v;
    m_tree.ForEach(MakeBackInsertFunctor(v));
    m_tree.Clear();

    //int clippedCnt = 0;
    //int elemCnt = v.size();

    m2::RectD rd(r);
    m2::AnyRectD ard(rd);

    for (unsigned i = 0; i < v.size(); ++i)
    {
      shared_ptr<OverlayElement> const & e = v[i];

      if (!e->isVisible())
      {
        //clippedCnt++;
        continue;
      }

      if (e->roughBoundRect().IsIntersect(rd))
      {
        bool hasIntersection = false;
        for (unsigned j = 0; j < e->boundRects().size(); ++j)
        {
          if (ard.IsIntersect(e->boundRects()[j]))
          {
            hasIntersection = true;
            break;
          }
        }

        if (hasIntersection)
          processOverlayElement(e);
      }
      //else
      //  clippedCnt++;
    }

//    LOG(LINFO, ("clipped out", clippedCnt, "elements,", elemCnt, "elements total"));
  }

  bool Overlay::checkHasEquals(Overlay const * l) const
  {
    vector<shared_ptr<OverlayElement> > v0;
    m_tree.ForEach(MakeBackInsertFunctor(v0));

    sort(v0.begin(), v0.end());

    vector<shared_ptr<OverlayElement> > v1;
    l->m_tree.ForEach(MakeBackInsertFunctor(v1));

    sort(v1.begin(), v1.end());

    vector<shared_ptr<OverlayElement> > res;

    set_intersection(v0.begin(), v0.end(), v1.begin(), v1.end(), back_inserter(res));

    return !res.empty();
  }
}