VR-Forces Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
indexingFeatureSet.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 
13 #include <features/featureSet.h>
16 #include <features/tileFeature.h>
17 
19 
20 #include <atomic>
21 
22 namespace MAKVRinTerra
23 {
24  template <typename FS, typename S = typename FS::Sink>
26  : public FS
27  , public std::enable_shared_from_this<DtIndexingFeatureSet<FS,S> >
28  {
29  public:
30  typedef std::shared_ptr<DtIndexingFeatureSet> Ptr;
31  typedef std::shared_ptr<const DtIndexingFeatureSet> CPtr;
32  typedef std::weak_ptr<DtIndexingFeatureSet> WPtr;
33 
34  typedef typename FS::Feature Feature;
35  typedef typename FS::ForEachFunction ForEachFunction;
36  typedef typename FS::LoadedCallback LoadedCallback;
37  typedef typename FS::DetachCallback DetachCallback;
38 
39  typedef S Sink;
40  typedef typename Sink::Ptr SinkPtr;
41 
44  {
45  public:
46  typedef std::shared_ptr<Config> Ptr;
47 
48  explicit Config(FS* input)
49  : inputSink(Sink::Make(input))
50  {
51  }
52 
53  explicit Config(const SinkPtr& input)
54  : inputSink(input ? input : Sink::Make(0))
55  {
56  }
57 
58  explicit Config(const Config& cfg)
60  , inputSink(cfg.inputSink)
61  {
62  static_cast<DtTerrainInterfaceConfig&>(*this) = cfg;
63  }
64 
65  void addTile(const DtFeature& feature)
66  {
67  if (tileFeatures)
68  tileFeatures->addFeature(feature);
69  }
70 
71  std::shared_ptr<DtFeatureSet::Source> tileFeatures;
72 
74  };
75 
76  static FS* Make(const Config& cfg, const DtProj::Bounds& bounds)
77  {
78  return Make(new Config(cfg), bounds);
79  }
80 
81  static FS* Make(Config* cfg, const DtProj::Bounds& bounds)
82  {
83  return Make(std::shared_ptr<Config>(cfg), bounds);
84  }
85 
86  static FS* Make(const std::shared_ptr<Config>& cfg, const DtProj::Bounds& bounds)
87  {
88  Ptr set(new DtIndexingFeatureSet(typename Config::Ptr(cfg), bounds));
89  set->init();
90 
91  return FS::Wrap(set);
92  }
93 
95  {
96  //myTileFeature.setDeleted();
97  }
98 
99  FS* clone() const
100  {
101  return FS::Wrap(this->shared_from_this());
102  }
103 
104  std::string label() const
105  {
106  return myConfig->inputSink->label() + " index level "
107  + boost::lexical_cast<std::string>(myLevel);
108  }
109 
110  bool loaded(double timeout) const
111  {
112  return myFeatures->loaded(timeout);
113  }
114 
116  {
117  return myFeatures->addLoadedCallback(cb);
118  }
119 
120  unsigned numberOfFeatures() const
121  {
122  return myFeatures->numberOfFeatures();
123  }
124 
125  bool elided() const
126  {
127  return myFeatures->elided();
128  }
129 
130  bool empty() const
131  {
132  return myFeatures->empty();
133  }
134 
136  const DtQuery& query,
137  const boost::optional<DtFeatureGeometry>& geometry,
138  DtLoadType load) const
139  {
141  if (!geometry)
142  return myFeatures->filter(query, boost::none_t(), load);
143 
145  if (myState != ACTIVE)
146  return myFeatures->filter(query, geometry, load);
147 
148  bool covers[4] = { false, false, false, false };
149  bool intersections[4] = { false, false, false, false };
150  size_t lastIntersection = 0;
151  unsigned numIntersects = 0;
152 
155  switch (geometry->type())
156  {
158  for (unsigned i=0; i != 4; ++i)
159  {
160  if (!geometry->intersects(this->getSubtile(i).geometry()))
161  continue;
162 
163  intersections[i] = true;
164  ++numIntersects;
165  lastIntersection = i;
166  break;
167  }
168 
169  break;
170 
172  for (unsigned i=0; i != 4; ++i)
173  {
174  if (!geometry->intersects(this->getSubtile(i).geometry()))
175  continue;
176 
177  intersections[i] = true;
178  ++numIntersects;
179  lastIntersection = i;
180  }
181 
182  break;
183 
184  default:
185  for (unsigned i=0; i != 4; ++i)
186  {
187  std::unique_ptr<DtFeatureGeometry::RelationshipMatrix> matrix
188  = geometry->relate(this->getSubtile(i).geometry());
189 
190  if (!matrix->intersects())
191  continue;
192 
194  if (matrix->touches())
195  continue;
196 
197  intersections[i] = true;
198  ++numIntersects;
199  lastIntersection = i;
200 
202  if (matrix->covers())
203  covers[i] = true;
204 
206  if (matrix->coveredBy())
207  break;
208  }
209  }
210 
213  switch (numIntersects)
214  {
215  case 0: return 0;
216  case 1: return getSubtile(lastIntersection).indexedFeatures().filter(
217  query,
218  !covers[lastIntersection] ? geometry : boost::none_t(),
219  load);
220  }
221 
223  std::unique_ptr< DtFeatureSetList<FS> > list(new DtFeatureSetList<FS>(4));
224  for (size_t i=0; i != 4; ++i)
225  {
226  if (!intersections[i])
227  continue;
228 
229  list->add(getSubtile(i).indexedFeatures().filter(
230  query,
231  !covers[i] ? geometry : boost::none_t(),
232  load));
233  }
234 
235  return list.release();
236  }
237 
238  void forEachFeatureParallel(const ForEachFunction& func) const
239  {
240  myFeatures->forEachFeatureParallel(func);
241  }
242 
244  {
245  return myFeatures->forEachFeatureAsync(func);
246  }
247 
248  protected:
249  class Subtile;
250 
251  DtIndexingFeatureSet(const Ptr& parent, const Subtile& subtile)
252  : myConfig(parent->myConfig)
253  , myLevel(parent->myLevel + 1)
254  , myParent(parent)
255  , myTileGeometry(subtile.geometry())
256  , myFeatures(subtile.sink())
257  , mySubtile0(*myFeatures, subtile.bounds(), 0)
258  , mySubtile1(*myFeatures, subtile.bounds(), 1)
259  , mySubtile2(*myFeatures, subtile.bounds(), 2)
260  , mySubtile3(*myFeatures, subtile.bounds(), 3)
261  //, myTileFeature(myTileGeometry.boundary(), 0xAA00FF, 1)
262  {
263  myState = START;
264  //myConfig->addTile(myTileFeature);
265  }
266 
268  const std::shared_ptr<Config>& cfg,
269  const DtProj::Bounds& bounds)
270  : myConfig(cfg)
271  , myLevel(0)
272  , myTileGeometry(DtFeatureGeometry::MakeBox(bounds))
273  , myFeatures(cfg->inputSink)
274  , mySubtile0(*myFeatures, bounds, 0)
275  , mySubtile1(*myFeatures, bounds, 1)
276  , mySubtile2(*myFeatures, bounds, 2)
277  , mySubtile3(*myFeatures, bounds, 3)
278  //, myTileFeature(myTileGeometry.boundary(), 0xAA00FF, 1)
279  {
280  myState = START;
281  }
282 
283  const Subtile& getSubtile(unsigned i) const
284  {
285  switch (i)
286  {
287  case 0: return mySubtile0;
288  case 1: return mySubtile1;
289  case 2: return mySubtile2;
290  case 3: return mySubtile3;
291  }
292 
293  assert(false);
294  DtTHROW_NEW(DtException, "Tile index out of bounds");
295  }
296 
298  void init()
299  {
300  Ptr This(this->shared_from_this());
301 
302  mySubtile0.init(This);
303  mySubtile1.init(This);
304  mySubtile2.init(This);
305  mySubtile3.init(This);
306  }
307 
310  {
313  auto expected = START;
314  if (!myState.compare_exchange_strong(expected, ACTIVATING))
315  return false;
316 
317  try
318  {
319  Ptr This(this->shared_from_this());
320  mySubtile0.activate(This);
321  mySubtile1.activate(This);
322  mySubtile2.activate(This);
323  mySubtile3.activate(This);
324  }
325  catch (...)
326  {
327  myState = FAULT;
328  throw;
329  }
330 
331  expected = ACTIVATING;
332  if (!myState.compare_exchange_strong(expected, ACTIVE))
333  {
334  DtWarn << "Error activating subtile" << std::endl;
335  myState = FAULT;
336  return false;
337  }
338 
339  return true;
340  }
341 
343  const std::shared_ptr<Config> myConfig;
344 
346  const int myLevel;
347 
349  const WPtr myParent;
350 
353 
356 
358  class Subtile
359  {
363  std::shared_ptr<DtIndexingFeatureSet> myIndexingSet;
364 
365  public:
366  const DtFeatureGeometry& geometry() const { return myGeometry; }
367  const DtProj::Bounds& bounds() const { return myBounds; }
368  const SinkPtr& sink() const { return mySink; }
369  const FS& indexedFeatures() const { return *myIndexingSet; }
370 
371  Subtile(const Sink& parentFeatures, const DtProj::Bounds& pbounds, unsigned i)
372  : myBounds(pbounds.makeSubBounds(i,2,2))
373  , myGeometry(DtFeatureGeometry::MakeBox(bounds()))
374  , mySink(Sink::Make(parentFeatures, geometry()))
375  {
376  }
377 
378  void init(const Ptr& parent)
379  {
380  Caller caller(parent, *this);
381  mySink->addLoadedCallback(caller);
382 
383  checkIndex(*parent);
384  }
385 
386  void activate(const Ptr& parent)
387  {
388  myIndexingSet.reset(new DtIndexingFeatureSet(parent, *this));
389  myIndexingSet->init();
390  }
391 
392  private:
394  {
395  try
396  {
397  if (!mySink->loaded())
398  return;
399  }
401  {
402  return;
403  }
404 
405  if (parent.myLevel >= parent.myConfig->featuresMaxIndexLOD)
406  return;
407 
408  size_t pc = parent.numberOfFeatures();
409  size_t c = mySink->numberOfFeatures();
410 
411  assert(pc >= c);
412 
414  if (pc <= parent.myConfig->minFeaturesToIndexTile)
415  return;
416 
417  if (c > 0)
418  {
419  if (pc/(double)c >= parent.myConfig->featuresIndexRatio)
420  parent.activateSubtiles();
421  }
422  else
423  {
425  //if (pc > 0)
427  }
428  }
429 
430  class Caller
431  {
432  public:
433  explicit Caller(const Ptr& set, Subtile& subtile)
434  : set(set), subtile(subtile)
435  {
436  }
437 
438  void operator()() const
439  {
440  Ptr p(set.lock());
441  if (p)
442  subtile.checkIndex(*p);
443  }
444 
445  private:
448  };
449  };
450 
451  friend class Subtile;
452 
457 
458  enum State
459  {
461  };
462 
463  std::atomic<State> myState;
464 
465  //DtTileFeature myTileFeature;
466  };
467 }
Definition: indexingFeatureSet.h:460
std::shared_ptr< DtFeatureSet::Source > tileFeatures
Definition: indexingFeatureSet.h:71
friend class Subtile
Definition: indexingFeatureSet.h:451
Class DtTerrainInterfaceConfig is a readable-writable class holding terrain interface configuration o...
Definition: terrainInterfaceConfig.h:33
static const int PathType
Definition: featureGeometry.h:58
Definition: indexingFeatureSet.h:25
const std::shared_ptr< Config > myConfig
Configuration data for this node.
Definition: indexingFeatureSet.h:343
Sink::Ptr SinkPtr
Definition: indexingFeatureSet.h:40
static FS * Make(const Config &cfg, const DtProj::Bounds &bounds)
Definition: indexingFeatureSet.h:76
void init()
Delayed init needed b/c we need a shared_ptr to ourself.
Definition: indexingFeatureSet.h:298
void operator()() const
Definition: indexingFeatureSet.h:438
Definition: indexingFeatureSet.h:460
FS * filter(const DtQuery &query, const boost::optional< DtFeatureGeometry > &geometry, DtLoadType load) const
Definition: indexingFeatureSet.h:135
Subtile mySubtile3
Definition: indexingFeatureSet.h:456
void init(const Ptr &parent)
Definition: indexingFeatureSet.h:378
const int myLevel
Level in hierarchy.
Definition: indexingFeatureSet.h:346
std::shared_ptr< DtIndexingFeatureSet > Ptr
Definition: indexingFeatureSet.h:30
FS::DetachCallback DetachCallback
Definition: indexingFeatureSet.h:37
const Subtile & getSubtile(unsigned i) const
Definition: indexingFeatureSet.h:283
Definition: featureGeometry.h:61
Subtile mySubtile2
Definition: indexingFeatureSet.h:455
Definition: proj.h:76
FS::ForEachFunction ForEachFunction
Definition: indexingFeatureSet.h:35
bool elided() const
Definition: indexingFeatureSet.h:125
Subtile(const Sink &parentFeatures, const DtProj::Bounds &pbounds, unsigned i)
Definition: indexingFeatureSet.h:371
Definition: indexingFeatureSet.h:460
Subtile mySubtile1
Definition: indexingFeatureSet.h:454
const FS & indexedFeatures() const
Definition: indexingFeatureSet.h:369
bool empty() const
Definition: indexingFeatureSet.h:130
Subtile & subtile
Definition: indexingFeatureSet.h:447
DetachCallback forEachFeatureAsync(const ForEachFunction &func) const
Definition: indexingFeatureSet.h:243
const WPtr myParent
Weak pointer to parent node.
Definition: indexingFeatureSet.h:349
SinkPtr myFeatures
Features in this tile.
Definition: indexingFeatureSet.h:355
Represents a set of characeters a DtFeature object can have. DtQuery objects are the way to communica...
Definition: query.h:37
const DtFeatureGeometry & geometry() const
Definition: indexingFeatureSet.h:366
static FS * Make(const std::shared_ptr< Config > &cfg, const DtProj::Bounds &bounds)
Definition: indexingFeatureSet.h:86
Subtile mySubtile0
Definition: indexingFeatureSet.h:453
std::atomic< State > myState
Definition: indexingFeatureSet.h:463
void checkIndex(DtIndexingFeatureSet &parent)
Definition: indexingFeatureSet.h:393
FS::LoadedCallback LoadedCallback
Definition: indexingFeatureSet.h:36
static const int PointType
Definition: featureGeometry.h:57
Definition: indexingFeatureSet.h:430
Set of subtiles.
Definition: indexingFeatureSet.h:358
const SinkPtr inputSink
Definition: indexingFeatureSet.h:73
std::shared_ptr< DtIndexingFeatureSet > myIndexingSet
Definition: indexingFeatureSet.h:363
State
Definition: indexingFeatureSet.h:458
bool loaded(double timeout) const
Definition: indexingFeatureSet.h:110
static FS * Make(Config *cfg, const DtProj::Bounds &bounds)
Definition: indexingFeatureSet.h:81
Caller(const Ptr &set, Subtile &subtile)
Definition: indexingFeatureSet.h:433
std::weak_ptr< DtIndexingFeatureSet > WPtr
Definition: indexingFeatureSet.h:32
DtIndexingFeatureSet(const std::shared_ptr< Config > &cfg, const DtProj::Bounds &bounds)
Definition: indexingFeatureSet.h:267
~DtIndexingFeatureSet()
Definition: indexingFeatureSet.h:94
std::shared_ptr< Config > Ptr
Definition: indexingFeatureSet.h:46
void forEachFeatureParallel(const ForEachFunction &func) const
Definition: indexingFeatureSet.h:238
std::string label() const
Definition: indexingFeatureSet.h:104
FS * clone() const
Definition: indexingFeatureSet.h:99
S Sink
Definition: indexingFeatureSet.h:39
DtProj::Bounds myBounds
Definition: indexingFeatureSet.h:360
Configuration information.
Definition: indexingFeatureSet.h:43
Config(FS *input)
Definition: indexingFeatureSet.h:48
DtLoadType
Used to indicate whether to load features. See DtFeatureSetTemplate::filter.
Definition: featureSet.h:33
const DtFeatureGeometry::Area myTileGeometry
Geometry object representing this tile.
Definition: indexingFeatureSet.h:352
const DtProj::Bounds & bounds() const
Definition: indexingFeatureSet.h:367
DtIndexingFeatureSet(const Ptr &parent, const Subtile &subtile)
Definition: indexingFeatureSet.h:251
WPtr set
Definition: indexingFeatureSet.h:446
Config(const Config &cfg)
Definition: indexingFeatureSet.h:58
Definition: indexingFeatureSet.h:460
DtFeatureSet used to combine multiple feature sets into one. This class allows all feature set algori...
Definition: featureSetList.h:26
void addTile(const DtFeature &feature)
Definition: indexingFeatureSet.h:65
SinkPtr mySink
Definition: indexingFeatureSet.h:362
std::shared_ptr< const DtIndexingFeatureSet > CPtr
Definition: indexingFeatureSet.h:31
void activate(const Ptr &parent)
Definition: indexingFeatureSet.h:386
Represents a GIS feature. Features consist of a DtFeatureGeometry (2.5D geometric vector data) and a ...
Definition: feature.h:36
DetachCallback addLoadedCallback(const LoadedCallback &cb) const
Definition: indexingFeatureSet.h:115
FS::Feature Feature
Definition: indexingFeatureSet.h:34
Config(const SinkPtr &input)
Definition: indexingFeatureSet.h:53
DtFeatureGeometry myGeometry
Definition: indexingFeatureSet.h:361
Represents a feature geometry.
Definition: featureGeometry.h:40
unsigned numberOfFeatures() const
Definition: indexingFeatureSet.h:120
bool activateSubtiles()
Mark subtiles and read to use.
Definition: indexingFeatureSet.h:309
const SinkPtr & sink() const
Definition: indexingFeatureSet.h:368

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)