GlobeEngine
CullableSpatialKey.h
Go to the documentation of this file.
1 
9 #ifndef GlobeEngine_CullableSpatialKey_H
10 #define GlobeEngine_CullableSpatialKey_H
11 
12 #include <iostream>
13 #include <cassert>
14 #include <memory>
15 #include <set>
16 #include "SpatialKey.h"
17 
18 namespace geSpatial {
19  /*
20  * A culable spatial key is used if you need to
21  * have the spatial key as cullable object but
22  * you do not have any other geometric properties
23  */
24  template <int D, class T> class CullableSpatialTreeKey : public SpatialTreeKey< D, T>
25  {
26  public:
28  requestable = true;
29  requestabilityTested = false;
30  loaded = false;
31  removed = false;
32  inViewFrustum = false;
33  farFromViewFrustum = false;
34  cameraDistance = 0.0;
35  }
36 
37  void isRequestable(bool _in){ this->requestable = _in; }
38 
39  bool isRequestable() const { return this->requestable; }
40 
41  void isLoaded(bool _in){ this->loaded = _in; }
42 
43  bool isLoaded() const { return this->loaded; }
44 
45  void isRemoved(bool _in){ this->removed = _in; }
46 
47  bool isRemoved() const { return this->removed; }
48 
49  void isRequestabilityTested(bool _in){ this->requestabilityTested = _in; }
50 
51  bool isRequestabilityTested() const { return this->requestabilityTested; }
52 
53  void isInViewFrustum(bool _in) { this->inViewFrustum = _in; }
54 
55  bool isInViewFrustum() const { return this->inViewFrustum; }
56 
57  void isFarFromViewFrustum(bool _in) { this->farFromViewFrustum = _in; }
58 
59  bool isFarFromViewFrustum() const { return this->farFromViewFrustum; }
60 
61  void setDistanceToCamera(double _in) { this->cameraDistance = _in; }
62 
63  double getDistanceToCamera() const { return this->cameraDistance; }
64 
65  protected:
66  /* tells you if the file system was tested already for this key. */
68 
69  /* tells you if a key can exist in this LoD structure. e.g. is avilable in the TMS file structure. */
71 
72  /* tells you if the content of a key is loaded. */
73  bool loaded;
74 
75  /* tells you if the content of this key was deleted. */
76  bool removed;
77 
78  /* tells you if the key is currently in view frustum or not. */
81  /* tells you how far the key is currently from the camera. */
83  };
84 
85  /*
86  * A cullable spatial key for a 2D data strcutures
87  * is used for example in a spatial quadtree
88  * where parts of the quadtree need to be requestable
89  * and cullable based on the key properties. The keys
90  * have a priorisation based on the distance to camera
91  */
92  template <class T> class CullableSpatialKey2 : public CullableSpatialTreeKey< 2, T>
93  {
94  public:
96  this->setInitial(-1, -1, -1);
97  }
98 
99  CullableSpatialKey2(short _lod) {
100  this->setInitial(_lod, -1, -1);
101  }
102 
103  CullableSpatialKey2(short _lod, unsigned int _x, unsigned int _y) {
104  this->setInitial(_lod, _x, _y);
105  }
106 
107  void setInitial(short _lod, unsigned int _x, unsigned int _y) {
108  this->setLod(_lod);
109  this->coord[0] = _x;
110  this->coord[1] = _y;
111  this->clearCullability();
112  }
113 
114  // perform a deep copy of the spatial key
115  CullableSpatialKey2(const std::shared_ptr<CullableSpatialKey2> _copy){
116  this->setLod(_copy->getLod());
117  this->coord[0] = _copy->getX();
118  this->coord[1] = _copy->getY();
119 
120  this->requestabilityTested = _copy->requestabilityTested;
121  this->requestable = _copy->requestable;
122  this->loaded = _copy->loaded;
123  this->removed = _copy->removed;
124  this->parent = _copy->parent;
125  for(int i=0;i<4;i++){
126  this->childs[i] = _copy->childs[i];
127  }
128  }
129 
130  std::shared_ptr< CullableSpatialKey2<T> > getParentKey() {
131  return this->parent;
132  }
133 
134  std::shared_ptr< CullableSpatialKey2<T> > getChildKey(int _idx) {
135  if(!childs[_idx]){
136  switch (_idx) {
137  case 0:{
138  this->childs[_idx] = std::make_shared<CullableSpatialKey2<T> >(this->lod+1,this->coord[0]*2, this->coord[1]*2);
139  break; }
140  case 1:{
141  this->childs[_idx] = std::make_shared<CullableSpatialKey2<T> >(this->lod+1,this->coord[0]*2 + 1, this->coord[1]*2);
142  break; }
143  case 2:{
144  this->childs[_idx] = std::make_shared<CullableSpatialKey2<T> >(this->lod+1,this->coord[0]*2, this->coord[1]*2 + 1);
145  break; }
146  case 3:{
147  this->childs[_idx] = std::make_shared<CullableSpatialKey2<T> >(this->lod+1,this->coord[0]*2 + 1, this->coord[1]*2 + 1);
148  break; }
149  default:
150  std::cout << "Child key request out of range" << std::endl;
151  assert(false);
152  break;
153  }
154  }
155  return childs[_idx];
156  }
157 
158  int getChildKeyInDirectionTo(std::shared_ptr< CullableSpatialKey2<T> > _key) {
160  }
161 
162  int getChildIdxInDirectionTo(std::shared_ptr< CullableSpatialKey2<T> > _key) {
163  double powOfDiff = pow(2.0f, _key->getLod() - this->getLod());
164  int keysProj[2];
165  for (int i = 0; i < 2; i++){
166  keysProj[i] = this->getCoord()[i] * powOfDiff;
167  }
168  int sidelenghtOfChild = (int)(powOfDiff / 2.0f);
169  if (keysProj[0] <= _key->getX() && _key->getX() < keysProj[0] + sidelenghtOfChild) {
170  if (keysProj[1] <= _key->getY() && _key->getY() < keysProj[1] + sidelenghtOfChild) {
171  return 0;
172  }
173  else {
174  return 2;
175  }
176  }
177  else {
178  if (keysProj[1] <= _key->getY() && _key->getY() < keysProj[1] + sidelenghtOfChild) {
179  return 1;
180  }
181  else {
182  return 3;
183  }
184  }
185  }
186 
187  /* smaller with x coordinate priority. And priority for stuff in view frustum.
188  * Shared pointers seem to have problems here.
189  */
190  bool operator<(const CullableSpatialKey2 &_other) const{
191  //bool operator<(const std::shared_ptr< CullableSpatialKey2> _other) const {
192  //bool operator<(const std::shared_ptr< CullableSpatialKey2> _other) const {
193  //if(this->inViewFrustum && !other.inViewFrustum)
194  // return this->inViewFrustum;
195  //if(!this->inViewFrustum && other.inViewFrustum)
196  // return this->inViewFrustum;
197  /*if(this->cameraDistance > 0.0){
198  return this->cameraDistance < _other.cameraDistance;
199  }*/
200 
201  if(this->lod != _other.lod)
202  return this->lod < _other.lod;
203  if(this->coord[0] != _other.coord[0])
204  return this->coord[0] < _other.coord[0];
205  return this->coord[1] < _other.coord[1];
206  }
207 
208  /* is equal is coordinates are equal */
209  bool operator==(const CullableSpatialKey2 &other)const{
210  return this->lod == other.lod && this->coord[0] == other.coord[0] && this->coord[1] == other.coord[1];
211  }
212 
213  unsigned int getX() { return this->coord[0]; };
214  void setX(unsigned int _in) { this->coord[0] = _in; };
215  unsigned int getY() { return this->coord[1]; };
216  void setY(unsigned int _in) { this->coord[1] = _in; };
217 
218  private:
219  std::shared_ptr< CullableSpatialKey2<T> > parent;
220  std::shared_ptr< CullableSpatialKey2<T> > childs[4];
221  };
222 
229  // Spatial Key definitions. Standard ist 2D and unsigned int
230  typedef CullableSpatialKey2ui CullableSpatialKey;
231 
233  template <typename T>
234  bool operator()(const std::shared_ptr<T>& lhs,
235  const std::shared_ptr<T>& rhs) const {
236  return (*lhs) < (*rhs);
237  }
238  };
239 
240  typedef std::set< std::shared_ptr<geSpatial::CullableSpatialKey>,geSpatial::CullableSpatialKeyComp > CullableSpatialKeySet;
241  typedef std::set< std::shared_ptr<geSpatial::CullableSpatialKey>,geSpatial::CullableSpatialKeyComp > ::iterator CullableSpatialKeySetIterator;
242 }
243 #endif
bool isFarFromViewFrustum() const
Definition: CullableSpatialKey.h:59
int getChildKeyInDirectionTo(std::shared_ptr< CullableSpatialKey2< T > > _key)
Definition: CullableSpatialKey.h:158
std::set< std::shared_ptr< geSpatial::CullableSpatialKey >, geSpatial::CullableSpatialKeyComp >::iterator CullableSpatialKeySetIterator
Definition: CullableSpatialKey.h:241
bool operator<(const CullableSpatialKey2 &_other) const
Definition: CullableSpatialKey.h:190
CullableSpatialKey2(short _lod, unsigned int _x, unsigned int _y)
Definition: CullableSpatialKey.h:103
bool operator==(const CullableSpatialKey2 &other) const
Definition: CullableSpatialKey.h:209
void isRemoved(bool _in)
Definition: CullableSpatialKey.h:45
short lod
Definition: SpatialKey.h:57
short getLod() const
Definition: SpatialKey.h:33
void isRequestabilityTested(bool _in)
Definition: CullableSpatialKey.h:49
void isFarFromViewFrustum(bool _in)
Definition: CullableSpatialKey.h:57
void setX(unsigned int _in)
Definition: CullableSpatialKey.h:214
expr pow(half base, half exp)
Definition: Half.h:2231
CullableSpatialKey2< unsigned char > CullableSpatialKey2ub
Definition: CullableSpatialKey.h:224
typedef int(CALL_CONVENTION *func_type_com_asprise_ocr_setup)(bool)
void clearCullability()
Definition: CullableSpatialKey.h:27
Definition: CullableSpatialKey.h:92
std::set< std::shared_ptr< geSpatial::CullableSpatialKey >, geSpatial::CullableSpatialKeyComp > CullableSpatialKeySet
Definition: CullableSpatialKey.h:240
CullableSpatialKey2< unsigned int > CullableSpatialKey2ui
Definition: CullableSpatialKey.h:228
bool isInViewFrustum() const
Definition: CullableSpatialKey.h:55
T coord[D]
Definition: SpatialKey.h:58
bool isRequestabilityTested() const
Definition: CullableSpatialKey.h:51
double cameraDistance
Definition: CullableSpatialKey.h:82
void setLod(short _in)
Definition: SpatialKey.h:34
CullableSpatialKey2(short _lod)
Definition: CullableSpatialKey.h:99
void isLoaded(bool _in)
Definition: CullableSpatialKey.h:41
std::shared_ptr< CullableSpatialKey2< T > > getParentKey()
Definition: CullableSpatialKey.h:130
CullableSpatialKey2< int > CullableSpatialKey2i
Definition: CullableSpatialKey.h:227
bool farFromViewFrustum
Definition: CullableSpatialKey.h:80
bool operator()(const std::shared_ptr< T > &lhs, const std::shared_ptr< T > &rhs) const
Definition: CullableSpatialKey.h:234
CullableSpatialKey2ui CullableSpatialKey
Definition: CullableSpatialKey.h:230
bool loaded
Definition: CullableSpatialKey.h:73
void isRequestable(bool _in)
Definition: CullableSpatialKey.h:37
void setInitial(short _lod, unsigned int _x, unsigned int _y)
Definition: CullableSpatialKey.h:107
std::shared_ptr< CullableSpatialKey2< T > > getChildKey(int _idx)
Definition: CullableSpatialKey.h:134
bool isRequestable() const
Definition: CullableSpatialKey.h:39
CullableSpatialKey2(const std::shared_ptr< CullableSpatialKey2 > _copy)
Definition: CullableSpatialKey.h:115
Definition: SpatialKey.h:17
void isInViewFrustum(bool _in)
Definition: CullableSpatialKey.h:53
Definition: CullableSpatialKey.h:24
bool requestable
Definition: CullableSpatialKey.h:70
const T * getCoord() const
Definition: SpatialKey.h:36
bool isLoaded() const
Definition: CullableSpatialKey.h:43
bool inViewFrustum
Definition: CullableSpatialKey.h:79
void setDistanceToCamera(double _in)
Definition: CullableSpatialKey.h:61
double getDistanceToCamera() const
Definition: CullableSpatialKey.h:63
CullableSpatialKey2()
Definition: CullableSpatialKey.h:95
CullableSpatialKey2< char > CullableSpatialKey2b
Definition: CullableSpatialKey.h:223
bool isRemoved() const
Definition: CullableSpatialKey.h:47
CullableSpatialKey2< short > CullableSpatialKey2s
Definition: CullableSpatialKey.h:225
bool removed
Definition: CullableSpatialKey.h:76
void setY(unsigned int _in)
Definition: CullableSpatialKey.h:216
CullableSpatialKey2< unsigned short > CullableSpatialKey2us
Definition: CullableSpatialKey.h:226
bool requestabilityTested
Definition: CullableSpatialKey.h:67
Definition: CullableSpatialKey.h:232
Definition: Cache.h:17
int getChildIdxInDirectionTo(std::shared_ptr< CullableSpatialKey2< T > > _key)
Definition: CullableSpatialKey.h:162
unsigned int getX()
Definition: CullableSpatialKey.h:213
unsigned int getY()
Definition: CullableSpatialKey.h:215