ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
vectorbasedvolumetree.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2020 German Aerospace Center (DLR).
3  * Eclipse ADORe, Automated Driving Open Research https://eclipse.org/adore
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License 2.0 which is available at
7  * http://www.eclipse.org/legal/epl-2.0.
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * Daniel Heß - initial implementation and API
13  ********************************************************************************/
14 #pragma once
15 #include <vector>
16 #include <queue>
17 
18 namespace adore
19 {
20  namespace mad
21  {
28  template<typename VolumeType, typename BoundingFunctor>
30  {
31  public:
32  typedef typename std::pair<std::pair<int,int>,VolumeType> IndexedVolumeType;
33  typedef typename std::vector<IndexedVolumeType> VolumeVector;
34 
35  private:
36  BoundingFunctor bound_;
37  std::vector<VolumeVector> levels_;
39  bool levels_ok_;
42  {
43  int lvl_,i0_,i1_;
44  SearchPosition(int lvl,int i0,int i1):lvl_(lvl),i0_(i0),i1_(i1){}
45  };
46  struct SearchState
47  {
49  double value_;
50  SearchState(double value,SearchPosition first,SearchPosition second)
51  :first_(first),second_(second),value_(value){}
52  };
53 
54  public:
56  {
57  levels_.push_back(VolumeVector());
58  }
59 
61  {
62  return levels_[i];
63  }
64 
65  const VolumeVector& getLevel(int i)const
66  {
67  return levels_[i];
68  }
73  {
74  return levels_[0].size();
75  }
76  const int getOccupancyCount()const
77  {
78  return levels_[0].size();
79  }
84  {
85  branching_factor_ = (std::max)(1,f);
86  }
90  void insert(const VolumeType& volume)
91  {
92  IndexedVolumeType ivolume;
93  ivolume.second = volume;
94  ivolume.first.first = levels_[0].size();
95  ivolume.first.second = levels_[0].size();
96  levels_[0].push_back(ivolume);
97  if(levels_.size()>1)levels_ok_=false;
98  }
102  int getLevelCount()const
103  {
104  return levels_.size();
105  }
110  {
111  while(levels_.size()>1)levels_.pop_back();
112  levels_ok_ = true;
113  }
118  {
119  setLevelCount(1000);
120  }
125  {
126  if(!levels_ok_)
127  {
130  }
131  }
132 
137  void setLevelCount(int count)
138  {
140  while(levels_.size()>(std::max)(1,count))levels_.pop_back();
141  while(levels_.size()<count && levels_.back().size()>1)
142  {
143  levels_.push_back(VolumeVector());
144  const VolumeVector& lower = levels_[levels_.size()-2];
145  VolumeVector& higher = levels_[levels_.size()-1];
146  for(int i0=0;i0<lower.size();i0+=branching_factor_)
147  {
148  const int i1 = (std::min)((int)lower.size()-1,i0+branching_factor_-1);
149  higher.push_back(bound_(lower,i0,i1));
150  }
151  }
152  }
160  template<typename MetricFunctor,typename OtherVolumeType,typename OtherFunctorType>
161  bool compute_min(const VectorBasedVolumeTree<OtherVolumeType,OtherFunctorType>& other,double cutoff,double& result_value,const MetricFunctor& f)const
162  {
163  const double guard = -1.e99;
164  //heap
165  auto cmp = [](const SearchState& left, const SearchState& right) { return left.value_ > right.value_; };
166  std::priority_queue<SearchState, std::vector<SearchState>, decltype(cmp)> open(cmp);
167  //initial state of search
168  SearchState s0(guard,//lower bound on metric value (not computed yet)
169  SearchPosition(levels_.size()-1,0,levels_.back().size()-1),//complete this range
170  SearchPosition(other.levels_.size()-1,0,other.levels_.back().size()-1)
171  );
172  open.push(s0);
173  while(!open.empty())
174  {
175  SearchState s = open.top();
176  open.pop();
177  //goal condition: 1-to-1 relation on lvl 0 with minimum value
178  if( s.first_.lvl_==0 && s.first_.i0_==s.first_.i1_
179  && s.second_.lvl_==0 && s.second_.i0_==s.second_.i1_ )
180  {
181  result_value = s.value_;
182  return true;
183  }
184  //partition the higher level
185  if( s.first_.lvl_ > s.second_.lvl_ )
186  {
187  if( s.first_.i0_ == s.first_.i1_ )
188  {
189  //unfold range
190  auto& data = levels_[s.first_.lvl_][s.first_.i0_];
191  s.first_.lvl_ = s.first_.lvl_-1;
192  s.first_.i0_ = data.first.first;
193  s.first_.i1_ = data.first.second;
194  }
195  }
196  else
197  {
198  if( s.second_.lvl_>0 && s.second_.i0_ == s.second_.i1_ )
199  {
200  //unfold range
201  auto& data = other.levels_[s.second_.lvl_][s.second_.i0_];
202  s.second_.lvl_ = s.second_.lvl_-1;
203  s.second_.i0_ = data.first.first;
204  s.second_.i1_ = data.first.second;
205  }
206  }
207  for(int i=s.first_.i0_;i<=s.first_.i1_;i++)
208  {
209  for(int j=s.second_.i0_;j<=s.second_.i1_;j++)
210  {
211  SearchState child(
212  //compute metric between i and j
213  f(levels_[s.first_.lvl_][i].second,other.levels_[s.second_.lvl_][j].second),
214  SearchPosition(s.first_.lvl_,i,i),
216  );
217  if(child.value_<cutoff)
218  {
219  open.push(child);
220  }
221  }
222  }
223 
224 
225  }
226  return false;
227  }
228 
229  };
230 
231  }
232 }
Definition: vectorbasedvolumetree.h:30
void compute_all_levels()
Definition: vectorbasedvolumetree.h:117
std::pair< std::pair< int, int >, VolumeType > IndexedVolumeType
Definition: vectorbasedvolumetree.h:32
std::vector< VolumeVector > levels_
Definition: vectorbasedvolumetree.h:37
int getOccupancyCount()
Definition: vectorbasedvolumetree.h:72
int branching_factor_
Definition: vectorbasedvolumetree.h:38
void setPreferredBranchingFactor(int f)
Definition: vectorbasedvolumetree.h:83
void insert(const VolumeType &volume)
Definition: vectorbasedvolumetree.h:90
void setLevelCount(int count)
Definition: vectorbasedvolumetree.h:137
std::vector< IndexedVolumeType > VolumeVector
Definition: vectorbasedvolumetree.h:33
VolumeVector & getLevel(int i)
Definition: vectorbasedvolumetree.h:60
const VolumeVector & getLevel(int i) const
Definition: vectorbasedvolumetree.h:65
BoundingFunctor bound_
Definition: vectorbasedvolumetree.h:36
const int getOccupancyCount() const
Definition: vectorbasedvolumetree.h:76
int getLevelCount() const
Definition: vectorbasedvolumetree.h:102
void recompute_levels()
Definition: vectorbasedvolumetree.h:124
bool levels_ok_
Definition: vectorbasedvolumetree.h:39
void remove_all_levels()
Definition: vectorbasedvolumetree.h:109
VectorBasedVolumeTree()
Definition: vectorbasedvolumetree.h:55
bool compute_min(const VectorBasedVolumeTree< OtherVolumeType, OtherFunctorType > &other, double cutoff, double &result_value, const MetricFunctor &f) const
Definition: vectorbasedvolumetree.h:161
@ right
Definition: indicator_hint.h:36
@ left
Definition: indicator_hint.h:35
T min(T a, T b, T c, T d)
Definition: adoremath.h:663
adoreMatrix< T, N, M > max(adoreMatrix< T, N, M > a, const adoreMatrix< T, N, M > &b)
Definition: adoremath.h:686
Definition: areaofeffectconverter.h:20
Definition: vectorbasedvolumetree.h:42
SearchPosition(int lvl, int i0, int i1)
Definition: vectorbasedvolumetree.h:44
int lvl_
Definition: vectorbasedvolumetree.h:43
int i0_
Definition: vectorbasedvolumetree.h:43
int i1_
Definition: vectorbasedvolumetree.h:43
Definition: vectorbasedvolumetree.h:47
SearchState(double value, SearchPosition first, SearchPosition second)
Definition: vectorbasedvolumetree.h:50
SearchPosition first_
Definition: vectorbasedvolumetree.h:48
SearchPosition second_
Definition: vectorbasedvolumetree.h:48
double value_
Definition: vectorbasedvolumetree.h:49