Overte C++ Documentation
Grab.h
1 //
2 // Grab.h
3 // libraries/avatars/src
4 //
5 // Created by Seth Alves on 2018-9-1.
6 // Copyright 2018 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_Grab_h
13 #define hifi_Grab_h
14 
15 #include <QUuid>
16 #include <QByteArray>
17 #include "GLMHelpers.h"
18 #include "StreamUtils.h"
19 
20 class Grab;
21 using GrabPointer = std::shared_ptr<Grab>;
22 using GrabWeakPointer = std::weak_ptr<Grab>;
23 
24 class GrabLocationAccumulator {
25 public:
26  void accumulate(glm::vec3 position, glm::quat orientation) {
27  _position += position;
28  _orientation = orientation; // XXX
29  _count++;
30  }
31 
32  glm::vec3 finalizePosition() { return _count > 0 ? _position * (1.0f / _count) : glm::vec3(0.0f); }
33  glm::quat finalizeOrientation() { return _orientation; } // XXX
34 
35 protected:
36  glm::vec3 _position;
37  glm::quat _orientation;
38  int _count { 0 };
39 };
40 
41 class Grab {
42 public:
43  Grab() {};
44  Grab(const QUuid& newOwnerID, const QUuid& newTargetID, int newParentJointIndex, const QString& newHand,
45  glm::vec3 newPositionalOffset, glm::quat newRotationalOffset) :
46  _ownerID(newOwnerID),
47  _targetID(newTargetID),
48  _parentJointIndex(newParentJointIndex),
49  _hand(newHand),
50  _positionalOffset(newPositionalOffset),
51  _rotationalOffset(newRotationalOffset),
52  _released(false) {}
53 
54  QByteArray toByteArray();
55  bool fromByteArray(const QByteArray& grabData);
56 
57  Grab& operator=(const GrabPointer& other) {
58  _ownerID = other->_ownerID;
59  _targetID = other->_targetID;
60  _parentJointIndex = other->_parentJointIndex;
61  _hand = other->_hand;
62  _positionalOffset = other->_positionalOffset;
63  _rotationalOffset = other->_rotationalOffset;
64  _actionID = other->_actionID;
65  _released = other->_released;
66  return *this;
67  }
68 
69  QUuid getActionID() const { return _actionID; }
70  void setActionID(const QUuid& actionID) { _actionID = actionID; }
71 
72  QUuid getOwnerID() const { return _ownerID; }
73  void setOwnerID(QUuid ownerID) { _ownerID = ownerID; }
74 
75  QUuid getTargetID() const { return _targetID; }
76  void setTargetID(QUuid targetID) { _targetID = targetID; }
77 
78  int getParentJointIndex() const { return _parentJointIndex; }
79  void setParentJointIndex(int parentJointIndex) { _parentJointIndex = parentJointIndex; }
80 
81  QString getHand() const { return _hand; }
82  void setHand(QString hand) { _hand = hand; }
83 
84  glm::vec3 getPositionalOffset() const { return _positionalOffset; }
85  void setPositionalOffset(glm::vec3 positionalOffset) { _positionalOffset = positionalOffset; }
86 
87  glm::quat getRotationalOffset() const { return _rotationalOffset; }
88  void setRotationalOffset(glm::quat rotationalOffset) { _rotationalOffset = rotationalOffset; }
89 
90  bool getReleased() const { return _released; }
91  void setReleased(bool value) { _released = value; }
92 
93 protected:
94  QUuid _actionID; // if an action is created in bullet for this grab, this is the ID
95  QUuid _ownerID; // avatar ID of grabber
96  QUuid _targetID; // SpatiallyNestable ID of grabbed
97  int _parentJointIndex { -1 }; // which avatar joint is being used to grab
98  QString _hand; // "left" or "right"
99  glm::vec3 _positionalOffset; // relative to joint
100  glm::quat _rotationalOffset; // relative to joint
101  bool _released { false }; // released and scheduled for deletion
102 };
103 
104 
105 #endif // hifi_Grab_h