Overte C++ Documentation
PickManager.h
1 //
2 // Created by Sam Gondelman 10/16/2017
3 // Copyright 2017 High Fidelity, Inc.
4 //
5 // Distributed under the Apache License, Version 2.0.
6 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
7 //
8 #ifndef hifi_PickManager_h
9 #define hifi_PickManager_h
10 
11 #include <DependencyManager.h>
12 #include "RegisteredMetaTypes.h"
13 
14 #include "Pick.h"
15 #include "PickCacheOptimizer.h"
16 
17 #include <NumericalConstants.h>
18 
19 #include <QObject>
20 
21 class PickManager : public QObject, public Dependency, protected ReadWriteLockable {
22  Q_OBJECT
23  SINGLETON_DEPENDENCY
24 
25 public:
26  PickManager();
27 
28  void update();
29 
30  unsigned int addPick(PickQuery::PickType type, const std::shared_ptr<PickQuery> pick);
31  void removePick(unsigned int uid);
32  void enablePick(unsigned int uid) const;
33  void disablePick(unsigned int uid) const;
34  bool isPickEnabled(unsigned int uid) const;
35  QVector<unsigned int> getPicks() const;
36 
37  PickResultPointer getPrevPickResult(unsigned int uid) const;
38  // The actual current properties of the pick
39  QVariantMap getPickProperties(unsigned int uid) const;
40  // The properties that were passed in to create the pick (may be empty if the pick was created by invoking the constructor)
41  QVariantMap getPickScriptParameters(unsigned int uid) const;
42 
43  template <typename T>
44  std::shared_ptr<T> getPrevPickResultTyped(unsigned int uid) const {
45  return std::static_pointer_cast<T>(getPrevPickResult(uid));
46  }
47 
48  void setPrecisionPicking(unsigned int uid, bool precisionPicking) const;
49  void setIgnoreItems(unsigned int uid, const QVector<QUuid>& ignore) const;
50  void setIncludeItems(unsigned int uid, const QVector<QUuid>& include) const;
51  void setDelay(unsigned int uid, float delay) const;
52 
53  Transform getParentTransform(unsigned int uid) const;
54  Transform getResultTransform(unsigned int uid) const;
55 
56  bool isLeftHand(unsigned int uid);
57  bool isRightHand(unsigned int uid);
58  bool isMouse(unsigned int uid);
59 
60  void setShouldPickHUDOperator(std::function<bool()> shouldPickHUDOperator) { _shouldPickHUDOperator = shouldPickHUDOperator; }
61  void setCalculatePos2DFromHUDOperator(std::function<glm::vec2(const glm::vec3&)> calculatePos2DFromHUDOperator) { _calculatePos2DFromHUDOperator = calculatePos2DFromHUDOperator; }
62  glm::vec2 calculatePos2DFromHUD(const glm::vec3& intersection) { return _calculatePos2DFromHUDOperator(intersection); }
63 
64  static const unsigned int INVALID_PICK_ID { 0 };
65 
66  unsigned int getPerFrameTimeBudget() const { return _perFrameTimeBudget; }
67  void setPerFrameTimeBudget(unsigned int numUsecs) { _perFrameTimeBudget = numUsecs; }
68 
69  bool getForceCoarsePicking() { return _forceCoarsePicking; }
70 
71  const std::vector<QVector3D>& getUpdatedPickCounts() { return _updatedPickCounts; }
72  const std::vector<int>& getTotalPickCounts() { return _totalPickCounts; }
73 
74 public slots:
75  void setForceCoarsePicking(bool forceCoarsePicking) { _forceCoarsePicking = forceCoarsePicking; }
76 
77 protected:
78  std::vector<QVector3D> _updatedPickCounts { PickQuery::NUM_PICK_TYPES };
79  std::vector<int> _totalPickCounts { 0, 0, 0, 0 };
80 
81  bool _forceCoarsePicking { false };
82  std::function<bool()> _shouldPickHUDOperator;
83  std::function<glm::vec2(const glm::vec3&)> _calculatePos2DFromHUDOperator;
84 
85  std::shared_ptr<PickQuery> findPick(unsigned int uid) const;
86  std::unordered_map<PickQuery::PickType, std::unordered_map<unsigned int, std::shared_ptr<PickQuery>>> _picks;
87  unsigned int _nextPickToUpdate[PickQuery::NUM_PICK_TYPES] { 0, 0, 0, 0 };
88  std::unordered_map<unsigned int, PickQuery::PickType> _typeMap;
89  unsigned int _nextPickID { INVALID_PICK_ID + 1 };
90 
91  PickCacheOptimizer<PickRay> _rayPickCacheOptimizer;
92  PickCacheOptimizer<StylusTip> _stylusPickCacheOptimizer;
93  PickCacheOptimizer<PickParabola> _parabolaPickCacheOptimizer;
94  PickCacheOptimizer<CollisionRegion> _collisionPickCacheOptimizer;
95 
96  static const unsigned int DEFAULT_PER_FRAME_TIME_BUDGET = 3 * USECS_PER_MSEC;
97  unsigned int _perFrameTimeBudget { DEFAULT_PER_FRAME_TIME_BUDGET };
98 };
99 
100 #endif // hifi_PickManager_h