VR-Forces 4.5 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
indexChooserFeatureSet.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2015 MAK Technologies, Inc.
3  * All rights reserved.
4  ******************************************************************************/
5 
9 
10 #pragma once
11 
14 
15 namespace MAKVRinTerra
16 {
17  template <typename FS>
18  class DtIndexChooserFeatureSet : public FS
19  {
20  public:
21  typedef typename FS::Feature Feature;
22  typedef typename FS::ForEachFunction ForEachFunction;
23  typedef typename FS::LoadedCallback LoadedCallback;
24  typedef typename FS::DetachCallback DetachCallback;
25 
26  typedef boost::shared_ptr<DtIndexChooserFeatureSet> Ptr;
27 
28  static FS* Make(FS* input, FS* mainIndex)
29  {
30  return new DtIndexChooserFeatureSet(input, mainIndex);
31  }
32 
33  std::string label() const { return myInput->label() + " index chooser"; }
34 
35  unsigned numberOfFeatures() const
36  {
37  unsigned tot = myMainIndex->numberOfFeatures();
38 
39  Mutex::scoped_lock lock(myData->myMutex, false);
40  for (typename Data::const_iterator i=myData->begin(), end=myData->end(); i!=end; ++i)
41  tot += i->second->numberOfFeatures();
42 
43  return tot;
44  }
45 
46  bool elided() const
47  {
48  if (!myMainIndex->elided())
49  return false;
50 
51  Mutex::scoped_lock lock(myData->myMutex, false);
52  for (typename Data::const_iterator i=myData->begin(), end=myData->end(); i!=end; ++i)
53  if (!i->second->elided())
54  return false;
55 
56  return true;
57  }
58 
59  bool empty() const
60  {
61  if (!myMainIndex->empty())
62  return false;
63 
64  Mutex::scoped_lock lock(myData->myMutex, false);
65  for (typename Data::const_iterator i=myData->begin(), end=myData->end(); i!=end; ++i)
66  if (!i->second->empty())
67  return false;
68 
69  return true;
70  }
71 
72  bool isIndexed() const
73  {
74  return true;
75  }
76 
77  bool loaded(double timeout=0.0) const
78  {
79  tbb::tick_count start = tbb::tick_count::now();
80 
81  if (!myMainIndex->loaded(timeout))
82  return false;
83 
84  Mutex::scoped_lock lock(myData->myMutex, false);
85  for (typename Data::const_iterator i=myData->begin(), end=myData->end(); i!=end; ++i)
86  {
87  double whatsleft = timeout - (tbb::tick_count::now() - start).seconds();
88  if (whatsleft < 0)
89  whatsleft = 0;
90 
91  if (!i->second->loaded(whatsleft))
92  return false;
93  }
94 
95  return true;
96  }
97 
98  FS* clone() const
99  {
100  return new DtIndexChooserFeatureSet(*this);
101  }
102 
104  {
105  return myMainIndex->addLoadedCallback(cb);
106  }
107 
108  struct Caller
109  {
110  void operator()(const Feature& feature) const
111  {
112  if (query.matches(feature))
113  func(feature);
114  }
115 
117  : func(func), query(query)
118  {
119  }
120 
121  private:
124  };
125 
126  void forEachFeatureParallel(const ForEachFunction& func) const
127  {
128  Caller caller(func, myFilteredAttributes);
129  myMainIndex->forEachFeatureParallel(caller);
130  }
131 
133  {
134  Caller caller(func, myFilteredAttributes);
135  return myMainIndex->forEachFeatureAsync(caller);
136  }
137 
138  FS* filter(
139  const DtQuery& query,
140  const boost::optional<DtFeatureGeometry>& geometry,
141  DtLoadType load) const
142  {
143  DtQuery combined = myFilteredAttributes && query;
144 
146  if (geometry)
147  return myMainIndex->filter(combined, geometry, load);
148 
151  if (!query.isTrue())
152  {
153  std::auto_ptr<FS> filterTest(myInput->filter(combined, geometry, DO_NOT_LOAD));
154  if (DtIsElided(filterTest.get()))
155  return 0;
156  }
157 
162  if (load == DO_NOT_LOAD)
163  return new DtIndexChooserFeatureSet(*this, combined);
164 
165  DtWarn << "Query = " << query << std::endl;
166  DtWarn << "Previous query = " << myFilteredAttributes << std::endl;
167  DtWarn << "Combined = " << combined << std::endl;
168 
171  if (const DtHasAttribute* attr = query.trycast<DtHasAttribute>())
172  {
173  Mutex::scoped_lock lock(myData->myMutex, true);
174  typename Data::const_iterator i = myData->myIndexMap.find(attr->key());
175  if (i != myData->myIndexMap.end())
176  {
177  DtInfo << "Using feature attribute index on key " << attr->key() << std::endl;
178  return i->second->filter(combined, geometry, load);
179  }
180 
181  typedef DtAttributeIndexingFeatureSet<FS> AttributeIndex;
182  typedef typename AttributeIndex::Config AttributeConfig;
183 
184  std::auto_ptr<FS> set(AttributeIndex::Make(
185  new AttributeConfig(myInput->filter(true, LOAD), attr->key())));
186 
187  DtInfo << "Creating feature attribute index on key " << attr->key() << std::endl;
188  std::auto_ptr<FS> ret(set->filter(combined, geometry, load));
189 
190  myData->myIndexMap.insert(attr->key(), set.release());
191  return ret.release();
192  }
193 
194  return myMainIndex->filter(combined, geometry, load);
195  }
196 
197  private:
198  explicit DtIndexChooserFeatureSet(FS* input, FS* mainIndex)
199  : myInput(input)
200  , myMainIndex(mainIndex)
201  , myData(new Data())
202  , myFilteredAttributes(true)
203  {
204  }
205 
207  const DtIndexChooserFeatureSet& from, const DtQuery& combined)
208  : myInput(from.myInput)
209  , myMainIndex(from.myMainIndex)
210  , myData(from.myData)
211  , myFilteredAttributes(combined)
212  {
213  }
214 
216  : myInput(from.myInput)
217  , myMainIndex(from.myMainIndex)
218  , myData(from.myData)
220  {
221  }
222 
223  typedef tbb::queuing_rw_mutex Mutex;
224 
225  const boost::shared_ptr<FS> myInput;
226  const boost::shared_ptr<FS> myMainIndex;
227 
228  struct Data
229  {
230  typedef boost::ptr_map<const DtFeature::Key, FS> Map;
231  typedef typename Map::const_iterator const_iterator;
232 
233  const_iterator begin() const { return myIndexMap.begin(); }
234  const_iterator end() const { return myIndexMap.end(); }
235 
236  mutable Mutex myMutex;
238  };
239 
240  const boost::shared_ptr<Data> myData;
242  };
243 }

Document ID: Generated on Thu Mar 23 18:54:12 EDT 2017 from SVN revision 174804
Copyright © 2005-2017 VT MÄK. All Rights Reserved (www.mak.com)