GlobeEngine
ArrayTree.h
Go to the documentation of this file.
1 //
2 // Created by Matthias Thöny on 1/11/12.
3 // Copyright (c) 2012 University of Zuerich. All rights reserved.
4 //
5 #ifndef VMML_ArrayTree_h
6 #define VMML_ArrayTree_h
7 
8 #include <vector>
9 #include <iostream>
10 
11 namespace geData {
12  template <class NODETYPE>
13  class ArrayTree
14  {
15  public:
16  ArrayTree() {}
18  nodes.clear();
19  }
20 
21  void setRootnode(NODETYPE _node){
22  this->rootnode = std::move(_node);
23  this->addNode(rootnode);
24  }
25 
26  int addNode(NODETYPE _data)
27  {
28  _data->setUID(nodes.size());
29  nodes.push_back(std::move(_data));
30  return nodes.size()-1;
31  }
32 
33  void removeNode(NODETYPE const& _data)
34  {
35  // search parent node id
36  int parentid = 0;
37  int nodeid = _data->getUID();
38  NODETYPE const& _parentkey = _data->getKey()->getParentKey();
39  //std::shared_ptr<geSpatial::SpatialKey> _parentkey = _data->getKey()->getParentKey();
40  if(_parentkey->getLod() > -1){
41  parentid = this->locateKey(_parentkey,0);
42  }
43 
44  if(parentid != -1){
45  nodes[parentid]->setChildByKey(_data->getKey(), -1);
46  }
47 
48  if(_data->isParent()) {
49  // remove children as well?
50  }
51  // remove node from list and repair all UIDs because they are now not valid anymore
52  if(_data->getKey()->isLoaded()){
53  nodes[nodeid]->clear();
54  //nodes.erase(nodes.begin()+_data->getUID());
55  nodes.erase(nodes.begin() + nodeid);
56  this->repairTreeUIDs(nodeid);
57  }
58  }
59 
60  void repairTreeUIDs(int _nodeid) {
61  // loop over the tree from bottom to top to correct all UIDs bigger than _nodeid for -1.
62  for (int i = 0; i < nodes.size();i++) {
63  // 1.) correct this UID if it is wrong
64  if(nodes[i]->getUID() != i) {
65  nodes[i]->setUID(i);
66  }
67  // check parent for debug
68  if(nodes[i]->getParentID() == _nodeid) {
69  std::cout << "there is a problem with the nodeids while delete" << std::endl;
70  }
71  // 2.) correct the UID in Parent
72  if(nodes[i]->getParentID() > _nodeid) {
73  nodes[i]->setParentID(nodes[i]->getParentID()-1);
74  }
75  // 3.) correct the UID off children
76  // 4.) correct this UID in children
77  nodes[i]->repairChildIdx(_nodeid);
78  }
79  //return -1;
80  }
81 
82 
83  /*int insertNode(NODETYPE _data)
84  {
85  // search parent node id
86  int parentid = 0;
87  int nodeid = -1;//std::numeric_limits<int>::max();
88  std::shared_ptr< geSpatial::SpatialKey> _parentkey = _data->getKey()->getParentKey();
89  //&& _data->getKey().getLod() > 0) {
90  if(_parentkey->getLod() > -1){
91  parentid = this->locateKey(_parentkey,0);
92  }
93  // if key is available add it to the tree
94  //if(_data->isAvailable()){
95  nodeid = this->addNode(_data);
96  nodes[nodeid]->setParentID(parentid);
97  //}
98  if(parentid != -1){
99  nodes[parentid]->setChildByKey(_data->getKey(), nodeid);
100  }else{
101  //std::cout << "this key cannot be inserted" << std::endl;
102  }
103  return nodeid;
104  };*/
105 
106  //int locateKey(std::shared_ptr<geSpatial::SpatialKey> _key, int _id) {
107  int locateKey(NODETYPE const& _key, int _id) {
108  //std::cout << _key.getLod() << " " << _key.getX()<< " " << _key.getY() << std::endl;
109  if (_key->getLod() > nodes[_id]->getKey()->getLod()) {
110  int childId = nodes[_id]->locateKeyInChild(_key);
111  if(childId != -1){
112  return this->locateKey(_key, childId);
113  }else{
114  return -1;//std::numeric_limits<int>::max();
115  }
116  }else{
117  return nodes[_id]->getUID();
118  }
119  }
120 
121  size_t getSize() {
122  return nodes.size();
123  }
124 
125  void print() {
126  for (int i = 0; i < nodes.size();i++) {
127  nodes[i]->print();
128  std::cout << std::endl;
129  };
130  }
131 
132  NODETYPE const& getNodeAtIndex(int _index) const { return nodes[_index]; }
133  NODETYPE const& getRoot() const { return rootnode; }
134 
135  /* returns the LoD which was defined as maximum for this tree */
136  int getMaxLod() const { return maxLoD; }
137  void setMaxLod(int _inLod) {
138  maxLoD = _inLod;
139  }
140 
141  /* returns the LoD which was defined as minimum for this tree */
142  int getMinLod() const { return minLoD; }
143  void setMinLod(int _inLod) { minLoD = _inLod; }
144 
145  /* returns the highest LoD which was loaded at this tree */
146  int getMaxDynamicLod() const { return maxDynamicLoD; }
147  void setMaxDynamicLod(int _inLod) { maxDynamicLoD = _inLod; }
148 
149  protected:
150  std::vector< NODETYPE > nodes;
151  NODETYPE rootnode;
153  short maxLoD;
154  short minLoD;
155  };
156 }
157 #endif
int addNode(NODETYPE _data)
Definition: ArrayTree.h:26
short minLoD
Definition: ArrayTree.h:154
NODETYPE const & getRoot() const
Definition: ArrayTree.h:133
NODETYPE const & getNodeAtIndex(int _index) const
Definition: ArrayTree.h:132
short maxDynamicLoD
Definition: ArrayTree.h:152
int getMaxLod() const
Definition: ArrayTree.h:136
Definition: AvalancheTrainingSimulationEngine.h:39
Definition: ArrayTree.h:13
short maxLoD
Definition: ArrayTree.h:153
ArrayTree()
Definition: ArrayTree.h:16
void setRootnode(NODETYPE _node)
Definition: ArrayTree.h:21
int getMinLod() const
Definition: ArrayTree.h:142
void setMinLod(int _inLod)
Definition: ArrayTree.h:143
size_t getSize()
Definition: ArrayTree.h:121
void removeNode(NODETYPE const &_data)
Definition: ArrayTree.h:33
void setMaxDynamicLod(int _inLod)
Definition: ArrayTree.h:147
void repairTreeUIDs(int _nodeid)
Definition: ArrayTree.h:60
int locateKey(NODETYPE const &_key, int _id)
Definition: ArrayTree.h:107
NODETYPE rootnode
Definition: ArrayTree.h:151
void print()
Definition: ArrayTree.h:125
int getMaxDynamicLod() const
Definition: ArrayTree.h:146
std::vector< NODETYPE > nodes
Definition: ArrayTree.h:150
~ArrayTree()
Definition: ArrayTree.h:17
void setMaxLod(int _inLod)
Definition: ArrayTree.h:137