GlobeEngine
SpatialKey.h
Go to the documentation of this file.
1 
9 #ifndef GlobeEngine_SpatialKey_H
10 #define GlobeEngine_SpatialKey_H
11 
12 #include <iostream>
13 #include <cassert>
14 #include <memory>
15 
16 namespace geSpatial {
17  template <int D, class T> class SpatialTreeKey {
18  public:
20  this->setLod(-1);
21  for (int i=0;i<D;i++) {
22  this->coord[i] = -1;
23  }
24  }
25 
26  SpatialTreeKey(short _lod){
27  this->setLod(_lod);
28  for (int i=0;i<D;i++) {
29  this->coord[i] = -1;
30  }
31  }
32 
33  short getLod() const { return this->lod; };
34  void setLod(short _in) { this->lod = _in; };
35 
36  const T* getCoord() const { return coord; };
37 
38  std::string getKeyAsString() {
39  std::string res = std::to_string(this->lod);
40  for (int i = 0; i<D; i++) {
41  res += "/" + std::to_string(this->coord[i]);
42  }
43  return res;
44  }
45 
46  friend std::ostream& operator<< (std::ostream &out, const SpatialTreeKey<D,T> &key) {
47  out << "Key: (" << key.lod;
48  for (int i=0;i<D;i++) {
49  out << ","<< key.coord[i];
50  }
51  out << ")";
52  //,rq: " << key.requestable <<" lo: " << key.loaded;
53  return out;
54  }
55 
56  protected:
57  short lod;
58  T coord[D];
59  };
60 
61  template <class T> class SpatialTreeKey1 : public SpatialTreeKey< 1, T>
62  {
63  public:
65  this->setLod(-1);
66  this->coord[0] = -1;
67  this->childOrder = 0;
68  }
69 
70  SpatialTreeKey1(short _lod) {
71  this->setLod(_lod);
72  this->coord[0] = -1;
73  this->childOrder = 0;
74  }
75 
76  SpatialTreeKey1(short _lod, unsigned int _x) {
77  this->setLod(_lod);
78  this->coord[0] = _x;
79  this->childOrder = 0;
80  }
81 
82  SpatialTreeKey1(short _lod, unsigned int _x, bool _order) {
83  this->setLod(_lod);
84  this->coord[0] = _x;
85  this->childOrder = _order;
86  }
87 
88  /* smaller with x coordinate priority. */
89  bool operator<(const SpatialTreeKey1 &other)const{
90  if(this->lod != other.lod)
91  return this->lod < other.lod;
92  return this->coord[0] < other.coord[0];
93  }
94  /* is equal is coordinates are equal */
95  bool operator==(const SpatialTreeKey1 &other)const{
96  return this->lod == other.lod && this->coord[0] == other.coord[0];
97  }
98  unsigned int getX() const { return this->coord[0]; };
99  void setX(unsigned int _in) { this->coord[0] = _in; };
100 
101  /* 0 = first child, 1 = second child */
102  bool isFirstChild() const {
103  return !this->childOrder;
104  }
105 
106  void setChildOrder(bool _in) {
107  this->childOrder = _in;
108  }
109 
110  protected:
112  };
113 
122 
123  /*
124  * Spatial keys for 2D data structures
125  * e.g. coordinates start bottom left by default. e.g:
126  *
127  * coordinate structure enum structure
128  * 0/3 - 1/3 - 2/3 - 3/3 UL - UR
129  * | | | | | |
130  * 0/2 - 1/2 - 2/2 - 3/2 BL - BR
131  * | | | | index structure
132  * 0/1 - 1/1 - 2/1 - 3/1 2 - 3
133  * | | | | | |
134  * 0/0 - 1/0 - 2/0 - 3/0 0 - 1
135  *
136  */
137  template <class T> class SpatialTreeKey2 : public SpatialTreeKey< 2, T>
138  {
139  public:
141  this->setInitial(-1, -1, -1);
142  }
143 
144  SpatialTreeKey2(short _lod) {
145  this->setInitial(_lod, -1, -1);
146  }
147 
148  SpatialTreeKey2(short _lod, unsigned int _x, unsigned int _y) {
149  this->setInitial(_lod, _x, _y);
150  }
151 
152  void setInitial(short _lod, unsigned int _x, unsigned int _y) {
153  this->setLod(_lod);
154  this->coord[0] = _x;
155  this->coord[1] = _y;
156  }
157 
158  // perform a deep copy of the spatial key
159  SpatialTreeKey2(const std::shared_ptr<SpatialTreeKey2> _copy){
160  this->setLod(_copy->getLod());
161  this->coord[0] = _copy->getX();
162  this->coord[1] = _copy->getY();
163 
164  this->parent = _copy->parent;
165  for(int i=0;i<4;i++){
166  this->childs[i] = _copy->childs[i];
167  }
168  }
169 
170  std::shared_ptr< SpatialTreeKey2<T> > getParentKey() const {
171  return this->parent;
172  }
173 
174  std::shared_ptr< SpatialTreeKey2<T> > getChildKey(int _idx) const {
175  if(!childs[_idx]){
176  switch (_idx) {
177  case 0:{
178  this->childs[_idx] = std::make_shared<SpatialTreeKey2<T> >(this->lod+1,this->coord[0]*2, this->coord[1]*2);
179  break; }
180  case 1:{
181  this->childs[_idx] = std::make_shared<SpatialTreeKey2<T> >(this->lod+1,this->coord[0]*2 + 1, this->coord[1]*2);
182  break; }
183  case 2:{
184  this->childs[_idx] = std::make_shared<SpatialTreeKey2<T> >(this->lod+1,this->coord[0]*2, this->coord[1]*2 + 1);
185  break; }
186  case 3:{
187  this->childs[_idx] = std::make_shared<SpatialTreeKey2<T> >(this->lod+1,this->coord[0]*2 + 1, this->coord[1]*2 + 1);
188  break; }
189  default:
190  std::cout << "Child key request out of range" << std::endl;
191  assert(false);
192  break;
193  }
194  }
195  return childs[_idx];
196  }
197 
198  int getChildKeyInDirectionTo(std::shared_ptr< SpatialTreeKey2<T> > _key) {
200  }
201 
202  int getChildIdxInDirectionTo(std::shared_ptr< SpatialTreeKey2<T> > _key) {
203  double powOfDiff = pow(2.0f, _key->getLod() - this->getLod());
204  int keysProj[2];
205  for (int i = 0; i < 2; i++){
206  keysProj[i] = this->getCoord()[i] * powOfDiff;
207  }
208  int sidelenghtOfChild = (int)(powOfDiff / 2.0f);
209  if (keysProj[0] <= _key->getX() && _key->getX() < keysProj[0] + sidelenghtOfChild) {
210  if (keysProj[1] <= _key->getY() && _key->getY() < keysProj[1] + sidelenghtOfChild) {
211  return 0;
212  }
213  else {
214  return 2;
215  }
216  }
217  else {
218  if (keysProj[1] <= _key->getY() && _key->getY() < keysProj[1] + sidelenghtOfChild) {
219  return 1;
220  }
221  else {
222  return 3;
223  }
224  }
225  }
226 
227  /* smaller with x coordinate priority. And priority for stuff in view frustum. */
228  bool operator<(const SpatialTreeKey2 &other)const{
229  if(this->inViewFrustum && !other.inViewFrustum)
230  return this->inViewFrustum;
231  if(!this->inViewFrustum && other.inViewFrustum)
232  return this->inViewFrustum;
233 
234  if(this->lod != other.lod)
235  return this->lod < other.lod;
236  if(this->coord[0] != other.coord[0])
237  return this->coord[0] < other.coord[0];
238  return this->coord[1] < other.coord[1];
239  }
240 
241  /* is equal is coordinates are equal */
242  bool operator==(const SpatialTreeKey2 &other)const{
243  return this->lod == other.lod && this->coord[0] == other.coord[0] && this->coord[1] == other.coord[1];
244  }
245 
246  unsigned int getX() { return this->coord[0]; };
247  void setX(unsigned int _in) { this->coord[0] = _in; };
248  unsigned int getY() { return this->coord[1]; };
249  void setY(unsigned int _in) { this->coord[1] = _in; };
250 
251  private:
252  std::shared_ptr< SpatialTreeKey2<T> > parent;
253  std::shared_ptr< SpatialTreeKey2<T> > childs[4];
254  };
255 
262 
263 }
264 #endif
SpatialTreeKey2()
Definition: SpatialKey.h:140
SpatialTreeKey1< short > SpatialKey1s
Definition: SpatialKey.h:118
void setX(unsigned int _in)
Definition: SpatialKey.h:247
void setInitial(short _lod, unsigned int _x, unsigned int _y)
Definition: SpatialKey.h:152
SpatialTreeKey1< int > SpatialKey1i
Definition: SpatialKey.h:120
short lod
Definition: SpatialKey.h:57
short getLod() const
Definition: SpatialKey.h:33
expr pow(half base, half exp)
Definition: Half.h:2231
SpatialTreeKey(short _lod)
Definition: SpatialKey.h:26
SpatialTreeKey2< unsigned char > SpatialKey2ub
Definition: SpatialKey.h:257
SpatialTreeKey2< short > SpatialKey2s
Definition: SpatialKey.h:258
typedef int(CALL_CONVENTION *func_type_com_asprise_ocr_setup)(bool)
unsigned int getY()
Definition: SpatialKey.h:248
T coord[D]
Definition: SpatialKey.h:58
SpatialTreeKey2(short _lod, unsigned int _x, unsigned int _y)
Definition: SpatialKey.h:148
void setLod(short _in)
Definition: SpatialKey.h:34
bool operator==(const SpatialTreeKey2 &other) const
Definition: SpatialKey.h:242
Definition: SpatialKey.h:61
SpatialTreeKey1< unsigned short > SpatialKey1us
Definition: SpatialKey.h:119
SpatialTreeKey1< unsigned char > SpatialKey1ub
Definition: SpatialKey.h:117
SpatialTreeKey2< char > SpatialKey2b
Definition: SpatialKey.h:256
SpatialTreeKey()
Definition: SpatialKey.h:19
bool isFirstChild() const
Definition: SpatialKey.h:102
Definition: SpatialKey.h:17
SpatialTreeKey1()
Definition: SpatialKey.h:64
bool childOrder
Definition: SpatialKey.h:111
SpatialTreeKey1(short _lod)
Definition: SpatialKey.h:70
void setChildOrder(bool _in)
Definition: SpatialKey.h:106
const T * getCoord() const
Definition: SpatialKey.h:36
std::shared_ptr< SpatialTreeKey2< T > > getChildKey(int _idx) const
Definition: SpatialKey.h:174
Definition: SpatialKey.h:137
SpatialTreeKey2(const std::shared_ptr< SpatialTreeKey2 > _copy)
Definition: SpatialKey.h:159
bool operator<(const SpatialTreeKey2 &other) const
Definition: SpatialKey.h:228
bool operator==(const SpatialTreeKey1 &other) const
Definition: SpatialKey.h:95
void setX(unsigned int _in)
Definition: SpatialKey.h:99
SpatialTreeKey1< unsigned int > SpatialKey1ui
Definition: SpatialKey.h:121
SpatialTreeKey1(short _lod, unsigned int _x)
Definition: SpatialKey.h:76
SpatialTreeKey1(short _lod, unsigned int _x, bool _order)
Definition: SpatialKey.h:82
SpatialTreeKey2< int > SpatialKey2i
Definition: SpatialKey.h:260
SpatialTreeKey2< unsigned int > SpatialKey2ui
Definition: SpatialKey.h:261
SpatialTreeKey1< char > SpatialKey1b
Definition: SpatialKey.h:116
void setY(unsigned int _in)
Definition: SpatialKey.h:249
bool operator<(const SpatialTreeKey1 &other) const
Definition: SpatialKey.h:89
int getChildIdxInDirectionTo(std::shared_ptr< SpatialTreeKey2< T > > _key)
Definition: SpatialKey.h:202
std::shared_ptr< SpatialTreeKey2< T > > getParentKey() const
Definition: SpatialKey.h:170
int getChildKeyInDirectionTo(std::shared_ptr< SpatialTreeKey2< T > > _key)
Definition: SpatialKey.h:198
Definition: Cache.h:17
SpatialTreeKey1< unsigned int > BinaryTreeKey
Definition: SpatialKey.h:114
unsigned int getX()
Definition: SpatialKey.h:246
SpatialTreeKey2< unsigned short > SpatialKey2us
Definition: SpatialKey.h:259
std::string getKeyAsString()
Definition: SpatialKey.h:38
unsigned int getX() const
Definition: SpatialKey.h:98
SpatialTreeKey2(short _lod)
Definition: SpatialKey.h:144
SpatialTreeKey1< unsigned int > MultiwayTreeKey
Definition: SpatialKey.h:115