PICCANTE  0.4
The hottest HDR imaging library!
quadtree.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_ALGORITHMS_QUADTREE_HPP
19 #define PIC_ALGORITHMS_QUADTREE_HPP
20 
21 #include <set>
22 
23 namespace pic {
24 
28 class Quadtree
29 {
30 protected:
31  bool leaf;
32  std::set<int> list;
34 
35  //bounding box
36  int bmax[2], bmin[2];
37 
44  void findAux(int *pos, int radius2, std::set<int> &out)
45  {
46  if(leaf) {
47  out.insert(list.begin(), list.end());
48  } else {
49  for(int i = 0; i < 4; i++) {
50  if(children[i] != NULL) {
51  if(checkCircleBBox(children[i]->bmax, children[i]->bmin, pos, radius2)) {
52  children[i]->findAux(pos, radius2, out);
53  }
54  }
55  }
56  }
57  }
58 
59 public:
60 
66  Quadtree(int *bmax, int *bmin)
67  {
68  for(int i = 0; i < 2; i++) {
69  this->bmax[i] = bmax[i];
70  this->bmin[i] = bmin[i];
71  }
72 
73  leaf = false;
74 
75  for(int i = 0; i < 4; i++) {
76  children[i] = NULL;
77  }
78  }
79 
81  {
82  for(int i = 0; i < 4; i++)
83  if(children[i] != NULL) {
84  delete children[i];
85  }
86  }
87 
95  static bool checkPointBBox(int *p, int *bmin, int *bmax)
96  {
97  return((p[0] >= bmin[0])
98  && (p[1] >= bmin[1])
99  && (p[0] < bmax[0])
100  && (p[1] < bmax[1]));
101  }
102 
111  static bool checkCircleBBox(int *bmax, int *bmin, int *center, int radius2)
112  {
113  int dmin = 0;
114 
115  for(int i = 0; i < 2; i++) {
116  if(center[i] < bmin[i]) {
117  int tmp = center[i] - bmin[i];
118  dmin += tmp * tmp;
119  } else {
120  if(center[i] > bmax[i]) {
121  int tmp = center[i] - bmax[i];
122  dmin += tmp * tmp;
123  }
124  }
125  }
126 
127  return (dmin <= radius2);
128  }
129 
138  static void getQuadrant(int *bmax, int *bmin, int *pMax, int *pMin, int i)
139  {
140  int half[2];
141 
142  for(int j = 0; j < 2; j++) {
143  half[j] = (bmax[j] + bmin[j]);
144 
145  if((half[j] % 2) == 0) {
146  half[j] = half[j] >> 1;
147  } else {
148  half[j] = (half[j] >> 1) + 1;
149  }
150  }
151 
152  switch(i) {
153  case 0: {
154  pMin[0] = bmin[0];
155  pMin[1] = bmin[1];
156 
157  pMax[0] = half[0];
158  pMax[1] = half[1];
159  }
160  break;
161 
162  case 1: {
163  pMin[0] = half[0];
164  pMin[1] = bmin[1];
165 
166  pMax[0] = bmax[0];
167  pMax[1] = half[1];
168  }
169  break;
170 
171  case 2: {
172  pMin[0] = bmin[0];
173  pMin[1] = half[1];
174 
175  pMax[0] = half[0];
176  pMax[1] = bmax[1];
177  }
178  break;
179 
180  case 3: {
181  pMin[0] = half[0];
182  pMin[1] = half[1];
183 
184  pMax[0] = bmax[0];
185  pMax[1] = bmax[1];
186  }
187  break;
188  }
189  }
190 
198  void insert(int *pos, int value, int MAX_OCTREE_LEVEL, int level = 0)
199  {
200  if(level == MAX_OCTREE_LEVEL) {
201  list.insert(value);
202  leaf = true;
203  } else {
204  int pMax[2], pMin[2];
205 
206  for(int i = 0; i < 4; i++) {
207  getQuadrant(bmax, bmin, pMax, pMin, i);
208 
209  if(checkPointBBox(pos, pMin, pMax)) {
210  if(children[i] == NULL) {
211  children[i] = new Quadtree(pMax, pMin);
212  }
213 
214  children[i]->insert(pos, value, MAX_OCTREE_LEVEL, level + 1);
215  break;
216  }
217  }
218  }
219  }
220 
228  void find(float x, float y, float radius, std::set<int> &out)
229  {
230 
231  int pos[2];
232  pos[0] = int(x);
233  pos[1] = int(y);
234  int radius2 = int(ceilf(radius * radius));
235 
236  if(checkPointBBox(pos, bmin, bmax)) {
237  findAux(pos, radius2, out);
238  }
239  }
240 };
241 
242 } // end namespace pic
243 
244 #endif /* PIC_ALGORITHMS_QUADTREE_HPP */
245 
int bmax[2]
Definition: quadtree.hpp:36
The Quadtree class.
Definition: quadtree.hpp:28
static bool checkPointBBox(int *p, int *bmin, int *bmax)
checkPointBBox
Definition: quadtree.hpp:95
void findAux(int *pos, int radius2, std::set< int > &out)
findAux
Definition: quadtree.hpp:44
static void getQuadrant(int *bmax, int *bmin, int *pMax, int *pMin, int i)
getQuadrant
Definition: quadtree.hpp:138
static bool checkCircleBBox(int *bmax, int *bmin, int *center, int radius2)
checkCircleBBox
Definition: quadtree.hpp:111
void find(float x, float y, float radius, std::set< int > &out)
find
Definition: quadtree.hpp:228
Quadtree * children[4]
Definition: quadtree.hpp:33
int bmin[2]
Definition: quadtree.hpp:36
void insert(int *pos, int value, int MAX_OCTREE_LEVEL, int level=0)
insert
Definition: quadtree.hpp:198
~Quadtree()
Definition: quadtree.hpp:80
Quadtree(int *bmax, int *bmin)
Quadtree.
Definition: quadtree.hpp:66
Definition: bilateral_separation.hpp:25
std::set< int > list
Definition: quadtree.hpp:32
bool leaf
Definition: quadtree.hpp:31