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 
52  Transform getParentTransform(unsigned int uid) const;
53  Transform getResultTransform(unsigned int uid) const;
54 
55  bool isLeftHand(unsigned int uid);
56  bool isRightHand(unsigned int uid);
57  bool isMouse(unsigned int uid);
58 
59  void setShouldPickHUDOperator(std::function<bool()> shouldPickHUDOperator) { _shouldPickHUDOperator = shouldPickHUDOperator; }
60  void setCalculatePos2DFromHUDOperator(std::function<glm::vec2(const glm::vec3&)> calculatePos2DFromHUDOperator) { _calculatePos2DFromHUDOperator = calculatePos2DFromHUDOperator; }
61  glm::vec2 calculatePos2DFromHUD(const glm::vec3& intersection) { return _calculatePos2DFromHUDOperator(intersection); }
62 
63  static const unsigned int INVALID_PICK_ID { 0 };
64 
65  unsigned int getPerFrameTimeBudget() const { return _perFrameTimeBudget; }
66  void setPerFrameTimeBudget(unsigned int numUsecs) { _perFrameTimeBudget = numUsecs; }
67 
68  bool getForceCoarsePicking() { return _forceCoarsePicking; }
69 
70  const std::vector<QVector3D>& getUpdatedPickCounts() { return _updatedPickCounts; }
71  const std::vector<int>& getTotalPickCounts() { return _totalPickCounts; }
72 
73 public slots:
74  void setForceCoarsePicking(bool forceCoarsePicking) { _forceCoarsePicking = forceCoarsePicking; }
75 
76 protected:
77  std::vector<QVector3D> _updatedPickCounts { PickQuery::NUM_PICK_TYPES };
78  std::vector<int> _totalPickCounts { 0, 0, 0, 0 };
79 
80  bool _forceCoarsePicking { false };
81  std::function<bool()> _shouldPickHUDOperator;
82  std::function<glm::vec2(const glm::vec3&)> _calculatePos2DFromHUDOperator;
83 
84  std::shared_ptr<PickQuery> findPick(unsigned int uid) const;
85  std::unordered_map<PickQuery::PickType, std::unordered_map<unsigned int, std::shared_ptr<PickQuery>>> _picks;
86  unsigned int _nextPickToUpdate[PickQuery::NUM_PICK_TYPES] { 0, 0, 0, 0 };
87  std::unordered_map<unsigned int, PickQuery::PickType> _typeMap;
88  unsigned int _nextPickID { INVALID_PICK_ID + 1 };
89 
90  PickCacheOptimizer<PickRay> _rayPickCacheOptimizer;
91  PickCacheOptimizer<StylusTip> _stylusPickCacheOptimizer;
92  PickCacheOptimizer<PickParabola> _parabolaPickCacheOptimizer;
93  PickCacheOptimizer<CollisionRegion> _collisionPickCacheOptimizer;
94 
95  static const unsigned int DEFAULT_PER_FRAME_TIME_BUDGET = 3 * USECS_PER_MSEC;
96  unsigned int _perFrameTimeBudget { DEFAULT_PER_FRAME_TIME_BUDGET };
97 };
98 
99 #endif // hifi_PickManager_h