20 #ifndef GALOIS_GRAPHS_SPATIALTREE_H
21 #define GALOIS_GRAPHS_SPATIALTREE_H
23 #include "galois/config.h"
38 double xmid()
const {
return (xmin + xmax) / 2.0; }
39 double ymid()
const {
return (ymin + ymax) / 2.0; }
41 void decimate(
int quad,
double midx,
double midy) {
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;
66 void setCenter(
double cx,
double cy) {
71 int getQuad(
double _x,
double _y) {
87 bool closer(
double x,
double y,
double testx,
double testy,
double oldx,
89 double doldx = x - oldx;
90 double doldy = y - oldy;
91 double dtestx = x - testx;
92 double dtesty = y - testy;
97 return (dtestx + dtesty) < (doldx + doldy);
111 T* recfind(Node* n,
double x,
double y) {
114 if (!best || closer(x, y, n->x, n->y, best->x, best->y))
117 int quad = n->getQuad(x, y);
118 n = n->children[quad];
123 void recinsert(Node** pos, Box2d b, Node* node) {
126 node->setCenter(b.xmid(), b.ymid());
127 if (__sync_bool_compare_and_swap(pos, 0, node))
131 int quad = (*pos)->getQuad(node->x, node->y);
132 b.decimate(quad, (*pos)->midx, (*pos)->midy);
133 recinsert(&(*pos)->children[quad], b, node);
136 Node* mkNode(
const T& v,
double x,
double y) {
143 void delNode(Node* n) {
149 void freeTree(Node* n) {
152 for (
int x = 0; x < 4; ++x)
153 freeTree(n->children[x]);
161 init(xmin, ymin, xmax, ymax);
169 void init(
double xmin,
double ymin,
double xmax,
double ymax) {
179 return recfind(root, x, y);
184 void insert(
double x,
double y,
const T& v) {
185 recinsert(&root, bounds, mkNode(v, x, y));
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