Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SpatialTree.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
20 #ifndef GALOIS_GRAPHS_SPATIALTREE_H
21 #define GALOIS_GRAPHS_SPATIALTREE_H
22 
23 #include "galois/config.h"
24 
25 namespace galois {
26 namespace graphs {
27 
30 template <typename T>
32  struct Box2d {
33  double xmin;
34  double ymin;
35  double xmax;
36  double ymax;
37 
38  double xmid() const { return (xmin + xmax) / 2.0; }
39  double ymid() const { return (ymin + ymax) / 2.0; }
40 
41  void decimate(int quad, double midx, double midy) {
42  if (quad & 1)
43  xmin = midx;
44  else
45  xmax = midx;
46  if (quad & 2)
47  ymin = midy;
48  else
49  ymax = midy;
50  }
51  };
52  struct Node {
53  // existing item
54  T val;
55  double x, y;
56 
57  // center
58  double midx, midy;
59 
60  Node* children[4];
61  // needs c++11: Node(const T& v) :val(v), children({0,0,0,0}) {}
62  Node(const T& v, double _x, double _y) : val(v), x(_x), y(_y) {
63  children[0] = children[1] = children[2] = children[3] = 0;
64  }
65 
66  void setCenter(double cx, double cy) {
67  midx = cx;
68  midy = cy;
69  }
70 
71  int getQuad(double _x, double _y) {
72  int retval = 0;
73  if (_x > midx)
74  retval += 1;
75  if (_y > midy)
76  retval += 2;
77  return retval;
78  }
79  };
80 
82 
83  Node* root;
84  Box2d bounds;
85 
86  // true if x,y is closer to testx, testy than oldx, oldy
87  bool closer(double x, double y, double testx, double testy, double oldx,
88  double oldy) const {
89  double doldx = x - oldx;
90  double doldy = y - oldy;
91  double dtestx = x - testx;
92  double dtesty = y - testy;
93  doldx *= doldx;
94  doldy *= doldy;
95  dtestx *= dtestx;
96  dtesty *= dtesty;
97  return (dtestx + dtesty) < (doldx + doldy);
98  }
99 
100  /*
101  T* recfind(Node* n, T* best, double bestx, double besty, double x, double y,
102  Box2d b) { if (!n) return best; if (!best) { // || closer(x, y, n->x, n->y,
103  bestx, besty)) { best = &n->val; bestx = n->x; besty = n->y;
104  }
105  int quad = b.getQuad(x,y);
106  b.decimate(quad);
107  return recfind(n->children[quad], best, bestx, besty, x, y, b);
108  }
109  */
110 
111  T* recfind(Node* n, double x, double y) {
112  Node* best = 0;
113  while (n) {
114  if (!best || closer(x, y, n->x, n->y, best->x, best->y))
115  best = n;
116  // best = &n->val;
117  int quad = n->getQuad(x, y);
118  n = n->children[quad];
119  }
120  return &best->val;
121  }
122 
123  void recinsert(Node** pos, Box2d b, Node* node) {
124  if (!*pos) {
125  // only do an atomic if it looks empty
126  node->setCenter(b.xmid(), b.ymid());
127  if (__sync_bool_compare_and_swap(pos, 0, node))
128  return; // worked!
129  }
130  // We should recurse
131  int quad = (*pos)->getQuad(node->x, node->y);
132  b.decimate(quad, (*pos)->midx, (*pos)->midy);
133  recinsert(&(*pos)->children[quad], b, node);
134  }
135 
136  Node* mkNode(const T& v, double x, double y) {
137  Node* n = nodeAlloc.allocate(1);
138  nodeAlloc.construct(n, Node(v, x, y));
139  return n;
140  // return new Node(v,x,y);
141  }
142 
143  void delNode(Node* n) {
144  nodeAlloc.destroy(n);
145  nodeAlloc.deallocate(n, 1);
146  // delete n;
147  }
148 
149  void freeTree(Node* n) {
150  if (!n)
151  return;
152  for (int x = 0; x < 4; ++x)
153  freeTree(n->children[x]);
154  delNode(n);
155  }
156 
157 public:
158  SpatialTree2d(double xmin = 0.0, double ymin = 0.0, double xmax = 0.0,
159  double ymax = 0.0)
160  : root(0) {
161  init(xmin, ymin, xmax, ymax);
162  }
163 
165  freeTree(root);
166  root = 0;
167  }
168 
169  void init(double xmin, double ymin, double xmax, double ymax) {
170  bounds.xmin = xmin;
171  bounds.ymin = ymin;
172  bounds.xmax = xmax;
173  bounds.ymax = ymax;
174  }
175 
177  T* find(double x, double y) {
178  assert(root);
179  return recfind(root, x, y);
180  }
181 
184  void insert(double x, double y, const T& v) {
185  recinsert(&root, bounds, mkNode(v, x, y));
186  }
187 };
188 
189 } // namespace graphs
190 } // namespace galois
191 
192 #endif
void insert(double x, double y, const T &v)
Insert an element.
Definition: SpatialTree.h:184
void construct(U *p, Args &&...args) const
Definition: runtime/Mem.h:766
SpatialTree2d(double xmin=0.0, double ymin=0.0, double xmax=0.0, double ymax=0.0)
Definition: SpatialTree.h:158
pointer allocate(size_type size)
Definition: runtime/Mem.h:754
void init(double xmin, double ymin, double xmax, double ymax)
Definition: SpatialTree.h:169
void destroy(pointer ptr) const
Definition: runtime/Mem.h:770
~SpatialTree2d()
Definition: SpatialTree.h:164
void deallocate(pointer ptr, size_type GALOIS_USED_ONLY_IN_DEBUG(len))
Definition: runtime/Mem.h:760
T * find(double x, double y)
Returns null if tree is empty.
Definition: SpatialTree.h:177
Stores sets of objects at specific spatial coordinates in a quad tree.
Definition: SpatialTree.h:31