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

tree4d.hpp « geometry - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4bf7c5a3dc455b4dd902644347fd69a9fba5263e (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
#pragma once

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

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

#include "std/sstream.hpp"
#include "std/kdtree.hpp"


namespace m4
{
  template <typename T>
  struct TraitsDef
  {
    m2::RectD const LimitRect(T const & t) const
    {
      return t.GetLimitRect();
    }
  };

  template <class T, typename Traits = TraitsDef<T> >
  class Tree
  {
    struct ValueT
    {
      T m_val;
      double m_pts[4];

      typedef double value_type;

      ValueT(T const & t, m2::RectD const & r) : m_val(t)
      {
        m_pts[0] = r.minX();
        m_pts[1] = r.minY();
        m_pts[2] = r.maxX();
        m_pts[3] = r.maxY();
      }

      bool IsIntersect(m2::RectD const & r) const
      {
        return !((m_pts[2] <= r.minX()) || (m_pts[0] >= r.maxX()) ||
                 (m_pts[3] <= r.minY()) || (m_pts[1] >= r.maxY()));
      }

      bool operator ==(ValueT const & r) const
      {
        return (m_val == r.m_val);
      }

      string DebugPrint() const
      {
        ostringstream out;

        out << DebugPrint(m_val) << ", ("
            << m_pts[0] << ", "
            << m_pts[1] << ", "
            << m_pts[2] << ", "
            << m_pts[3] << ")";

        return out.str();
      }

      double operator[](size_t i) const { return m_pts[i]; }

      m2::RectD GetRect() const { return m2::RectD(m_pts[0], m_pts[1], m_pts[2], m_pts[3]); }
    };

    typedef KDTree::KDTree<4, ValueT> TreeT;
    TreeT m_tree;

    // Do-class for rect-iteration in tree.
    template <class ToDo> class for_each_helper
    {
      m2::RectD const & m_rect;
      ToDo m_toDo;

    public:
      for_each_helper(m2::RectD const & r, ToDo toDo)
        : m_rect(r), m_toDo(toDo)
      {
      }

      bool ScanLeft(size_t plane, ValueT const & v) const
      {
        switch (plane & 3)    // % 4
        {
        case 2: return m_rect.minX() < v[2];
        case 3: return m_rect.minY() < v[3];
        default: return true;
        }
      }

      bool ScanRight(size_t plane, ValueT const & v) const
      {
        switch (plane & 3)  // % 4
        {
        case 0: return m_rect.maxX() > v[0];
        case 1: return m_rect.maxY() > v[1];
        default: return true;
        }
      }

      void operator() (ValueT const & v) const
      {
        if (v.IsIntersect(m_rect))
          m_toDo(v);
      }
    };

    template <class ToDo> for_each_helper<ToDo> GetFunctor(m2::RectD const & rect, ToDo toDo) const
    {
      return for_each_helper<ToDo>(rect, toDo);
    }

  protected:
    Traits m_traits;
    m2::RectD GetLimitRect(T const & t) const { return m_traits.LimitRect(t); }

  public:
    Tree(Traits const & traits = Traits()) : m_traits(traits) {}

    typedef T elem_t;

    void Add(T const & obj)
    {
      Add(obj, GetLimitRect(obj));
    }

    void Add(T const & obj, m2::RectD const & rect)
    {
      m_tree.insert(ValueT(obj, rect));
    }

  private:
    template <class CompareT>
    void ReplaceImpl(T const & obj, m2::RectD const & rect, CompareT comp)
    {
      bool skip = false;
      vector<ValueT const *> isect;

      m_tree.for_each(GetFunctor(rect, [&] (ValueT const & v)
      {
        if (skip)
          return;

        switch (comp(obj, v.m_val))
        {
        case 1:
          isect.push_back(&v);
          break;
        case -1:
          skip = true;
          break;
        }
      }));

      if (skip)
        return;

      for (ValueT const * v : isect)
        m_tree.erase(*v);

      Add(obj, rect);
    }

  public:
    template <class CompareT>
    void ReplaceAllInRect(T const & obj, CompareT comp)
    {
      ReplaceImpl(obj, GetLimitRect(obj), [&comp] (T const & t1, T const & t2)
      {
        return comp(t1, t2) ? 1 : -1;
      });
    }

    template <class EqualT, class CompareT>
    void ReplaceEqualInRect(T const & obj, EqualT eq, CompareT comp)
    {
      ReplaceImpl(obj, GetLimitRect(obj), [&] (T const & t1, T const & t2)
      {
        if (eq(t1, t2))
          return comp(t1, t2) ? 1 : -1;
        else
          return 0;
      });
    }

    void Erase(T const & obj, m2::RectD const & r)
    {
      ValueT val(obj, r);
      m_tree.erase_exact(val);
    }

    void Erase(T const & obj)
    {
      ValueT val(obj, m_traits.LimitRect(obj));
      m_tree.erase_exact(val);
    }

    template <class ToDo>
    void ForEach(ToDo toDo) const
    {
      for (ValueT const & v : m_tree)
        toDo(v.m_val);
    }

    template <class ToDo>
    void ForEachWithRect(ToDo toDo) const
    {
      for (ValueT const & v : m_tree)
        toDo(v.GetRect(), v.m_val);
    }

    template <class ToDo>
    void ForEachInRect(m2::RectD const & rect, ToDo toDo) const
    {
      m_tree.for_each(GetFunctor(rect, [&toDo] (ValueT const & v) { toDo(v.m_val); }));
    }

    bool IsEmpty() const { return m_tree.empty(); }

    size_t GetSize() const { return m_tree.size(); }

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

    string DebugPrint() const
    {
      ostringstream out;
      for (ValueT const & v : m_tree.begin())
        out << v.DebugPrint() << ", ";
      return out.str();
    }
  };

  template <typename T, typename Traits>
  string DebugPrint(Tree<T, Traits> const & t)
  {
    return t.DebugPrint();
  }
}