Overte C++ Documentation
DiffTraversal.h
1 //
2 // DiffTraversal.h
3 // assignment-client/src/entities
4 //
5 // Created by Andrew Meadows 2017.08.08
6 // Copyright 2017 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_DiffTraversal_h
13 #define hifi_DiffTraversal_h
14 
15 #include <shared/ConicalViewFrustum.h>
16 
17 #include "EntityTreeElement.h"
18 
19 // DiffTraversal traverses the tree and applies _scanElementCallback on elements it finds
20 class DiffTraversal {
21 public:
22  // VisibleElement is a struct identifying an element and how it intersected the view.
23  // The intersection is used to optimize culling entities from the sendQueue.
24  class VisibleElement {
25  public:
26  EntityTreeElementPointer element;
27  };
28 
29  // View is a struct with a ViewFrustum and LOD parameters
30  class View {
31  public:
32  bool usesViewFrustums() const;
33  bool isVerySimilar(const View& view) const;
34 
35  bool shouldTraverseElement(const EntityTreeElement& element) const;
36  float computePriority(const EntityItemPointer& entity) const;
37 
38  ConicalViewFrustums viewFrustums;
39  uint64_t startTime { 0 };
40  float lodScaleFactor { 1.0f };
41  };
42 
43  // Waypoint is an bookmark in a "path" of waypoints during a traversal.
44  class Waypoint {
45  public:
46  Waypoint(EntityTreeElementPointer& element);
47 
48  void getNextVisibleElementFirstTime(VisibleElement& next, const View& view);
49  void getNextVisibleElementRepeat(VisibleElement& next, const View& view, uint64_t lastTime);
50  void getNextVisibleElementDifferential(VisibleElement& next, const View& view, const View& lastView);
51 
52  int8_t getNextIndex() const { return _nextIndex; }
53  void initRootNextIndex() { _nextIndex = -1; }
54 
55  protected:
56  EntityTreeElementWeakPointer _weakElement;
57  int8_t _nextIndex;
58  };
59 
60  typedef enum { First, Repeat, Differential } Type;
61 
62  DiffTraversal();
63 
64  Type prepareNewTraversal(const DiffTraversal::View& view, EntityTreeElementPointer root, bool forceFirstPass = false);
65 
66  const View& getCurrentView() const { return _currentView; }
67 
68  uint64_t getStartOfCompletedTraversal() const { return _completedView.startTime; }
69  bool finished() const { return _path.empty(); }
70 
71  void setScanCallback(std::function<void (VisibleElement&)> cb);
72  void traverse(uint64_t timeBudget);
73 
74  void reset() { _path.clear(); _completedView.startTime = 0; } // resets our state to force a new "First" traversal
75 
76 private:
77  void getNextVisibleElement(VisibleElement& next);
78 
79  View _currentView;
80  View _completedView;
81  std::vector<Waypoint> _path;
82  std::function<void (VisibleElement&)> _getNextVisibleElementCallback { nullptr };
83  std::function<void (VisibleElement&)> _scanElementCallback { [](VisibleElement& e){} };
84 };
85 
86 #endif // hifi_EntityPriorityQueue_h