Overte C++ Documentation
AvatarData.h
1 //
2 // AvatarData.h
3 // libraries/avatars/src
4 //
5 // Created by Stephen Birarda on 4/9/13.
6 // Copyright 2013 High Fidelity, Inc.
7 // Copyright 2021 Vircadia contributors.
8 // Copyright 2022-2023 Overte e.V.
9 //
10 // Distributed under the Apache License, Version 2.0.
11 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
12 // SPDX-License-Identifier: Apache-2.0
13 //
14 
15 #ifndef hifi_AvatarData_h
16 #define hifi_AvatarData_h
17 
18 #include <string>
19 #include <memory>
20 #include <queue>
21 #include <inttypes.h>
22 #include <vector>
23 
24 #include <glm/glm.hpp>
25 #include <glm/gtc/quaternion.hpp>
26 
27 #include <QByteArray>
28 #include <QElapsedTimer>
29 #include <QHash>
30 #include <QObject>
31 #include <QRect>
32 #include <QStringList>
33 #include <QUrl>
34 #include <QUuid>
35 #include <QVariantMap>
36 #include <QVector>
37 #include <QReadWriteLock>
38 
39 #include <AvatarConstants.h>
40 #include <JointData.h>
41 #include <NLPacket.h>
42 #include <Node.h>
43 #include <NumericalConstants.h>
44 #include <Packed.h>
45 #include <RegisteredMetaTypes.h>
46 #include <SharedUtil.h>
47 #include <SimpleMovingAverage.h>
48 #include <SpatiallyNestable.h>
49 #include <ThreadSafeValueCache.h>
50 #include <ViewFrustum.h>
51 #include <shared/RateCounter.h>
52 #include <udt/SequenceNumber.h>
53 #include <Scriptable.h>
54 #include <ScriptValue.h>
55 
56 #include "AABox.h"
57 #include "AvatarTraits.h"
58 #include "HeadData.h"
59 #include "PathUtils.h"
60 
61 class ScriptEngine;
62 
63 using AvatarSharedPointer = std::shared_ptr<AvatarData>;
64 using AvatarWeakPointer = std::weak_ptr<AvatarData>;
65 using AvatarHash = QHash<QUuid, AvatarSharedPointer>;
66 
67 using AvatarEntityMap = QMap<QUuid, QByteArray>;
68 using PackedAvatarEntityMap = QMap<QUuid, QByteArray>; // similar to AvatarEntityMap, but different internal format
69 using AvatarEntityIDs = QSet<QUuid>;
70 
71 using AvatarGrabDataMap = QMap<QUuid, QByteArray>;
72 
73 using AvatarDataSequenceNumber = uint16_t;
74 
75 const int MAX_NUM_AVATAR_ENTITIES = 42;
76 
77 // avatar motion behaviors
78 const quint32 AVATAR_MOTION_ACTION_MOTOR_ENABLED = 1U << 0;
79 const quint32 AVATAR_MOTION_SCRIPTED_MOTOR_ENABLED = 1U << 1;
80 
81 const quint32 AVATAR_MOTION_DEFAULTS =
82  AVATAR_MOTION_ACTION_MOTOR_ENABLED |
83  AVATAR_MOTION_SCRIPTED_MOTOR_ENABLED;
84 
85 // these bits will be expanded as features are exposed
86 const quint32 AVATAR_MOTION_SCRIPTABLE_BITS =
87  AVATAR_MOTION_SCRIPTED_MOTOR_ENABLED;
88 
89 // Bitset of state flags - we store the key state, hand state, Faceshift, eye tracking, and existence of
90 // referential data in this bit set. The hand state is an octal, but is split into two sections to maintain
91 // backward compatibility. The bits are ordered as such (0-7 left to right).
92 // AA 6/1/18 added three more flags bits 8,9, and 10 for procedural audio, blink, and eye saccade enabled
93 //
94 // +-----+-----+-+-+-+--+--+--+--+-----+
95 // |K0,K1|H0,H1|F|E|R|H2|Au|Bl|Ey|xxxxx|
96 // +-----+-----+-+-+-+--+--+--+--+-----+
97 //
98 // Key state - K0,K1 is found in the 1st and 2nd bits
99 // Hand state - H0,H1,H2 is found in the 3rd, 4th, and 8th bits
100 // Face tracker - F is found in the 5th bit
101 // Eye tracker - E is found in the 6th bit
102 // Referential Data - R is found in the 7th bit
103 // Procedural audio to mouth movement is enabled 8th bit
104 // Procedural Blink is enabled 9th bit
105 // Procedural Eyelid is enabled 10th bit
106 // Procedural PROCEDURAL_BLINK_FACE_MOVEMENT is enabled 11th bit
107 // Procedural Collide with other avatars is enabled 12th bit
108 // Procedural Has Hero Priority is enabled 13th bit
109 
110 const int KEY_STATE_START_BIT = 0; // 1st and 2nd bits (UNUSED)
111 const int HAND_STATE_START_BIT = 2; // 3rd and 4th bits (UNUSED)
112 const int HAS_SCRIPTED_BLENDSHAPES = 4; // 5th bit
113 const int HAS_PROCEDURAL_EYE_MOVEMENT = 5; // 6th bit
114 const int HAS_REFERENTIAL = 6; // 7th bit
115 const int HAND_STATE_FINGER_POINTING_BIT = 7; // 8th bit (UNUSED)
116 const int AUDIO_ENABLED_FACE_MOVEMENT = 8; // 9th bit
117 const int PROCEDURAL_EYE_FACE_MOVEMENT = 9; // 10th bit
118 const int PROCEDURAL_BLINK_FACE_MOVEMENT = 10; // 11th bit
119 const int COLLIDE_WITH_OTHER_AVATARS = 11; // 12th bit
120 const int HAS_HERO_PRIORITY = 12; // 13th bit (be scared)
121 
122 /*@jsdoc
123  * <p>The pointing state of the hands is specified by the following values:</p>
124  * <table>
125  * <thead>
126  * <tr><th>Value</th><th>Description</th>
127  * </thead>
128  * <tbody>
129  * <tr><td><code>0</code></td><td>No hand is pointing.</td></tr>
130  * <tr><td><code>1</code></td><td>The left hand is pointing.</td></tr>
131  * <tr><td><code>2</code></td><td>The right hand is pointing.</td></tr>
132  * <tr><td><code>4</code></td><td>It is the index finger that is pointing.</td></tr>
133  * </tbody>
134  * </table>
135  * <p>The values for the hand states are added together to give the <code>HandState</code> value. For example, if the left
136  * hand's finger is pointing, the value is <code>1 + 4 == 5</code>.
137  * @typedef {number} HandState
138  */
139 const char HAND_STATE_NULL = 0;
140 const char LEFT_HAND_POINTING_FLAG = 1;
141 const char RIGHT_HAND_POINTING_FLAG = 2;
142 const char IS_FINGER_POINTING_FLAG = 4;
143 
144 // AvatarData state flags - we store the details about the packet encoding in the first byte,
145 // before the "header" structure
146 const char AVATARDATA_FLAGS_MINIMUM = 0;
147 
148 using SmallFloat = uint16_t; // a compressed float with less precision, user defined radix
149 
150 namespace AvatarSkeletonTrait {
151  enum BoneType {
152  SkeletonRoot = 0,
153  SkeletonChild,
154  NonSkeletonRoot,
155  NonSkeletonChild
156  };
157 
158  PACKED_BEGIN struct Header {
159  float maxTranslationDimension;
160  float maxScaleDimension;
161  uint8_t numJoints;
162  uint16_t stringTableLength;
163  } PACKED_END;
164 
165  PACKED_BEGIN struct JointData {
166  uint16_t stringStart;
167  uint8_t stringLength;
168  uint8_t boneType;
169  uint8_t defaultTranslation[6];
170  uint8_t defaultRotation[6];
171  uint16_t defaultScale;
172  uint16_t jointIndex;
173  uint16_t parentIndex;
174  } PACKED_END;
175 
176  struct UnpackedJointData {
177  int stringStart;
178  int stringLength;
179  int boneType;
180  glm::vec3 defaultTranslation;
181  glm::quat defaultRotation;
182  float defaultScale;
183  int jointIndex;
184  int parentIndex;
185  QString jointName;
186  };
187 }
188 
189 namespace AvatarDataPacket {
190 
191  // NOTE: every time AvatarData is sent from mixer to client, it also includes the GUIID for the session
192  // this is 16bytes of data at 45hz that's 5.76kbps
193  // it might be nice to use a dictionary to compress that
194 
195  // Packet State Flags - we store the details about the existence of other records in this bitset:
196  // AvatarGlobalPosition, Avatar face tracker, eye tracking, and existence of
197  using HasFlags = uint16_t;
198  const HasFlags PACKET_HAS_AVATAR_GLOBAL_POSITION = 1U << 0;
199  const HasFlags PACKET_HAS_AVATAR_BOUNDING_BOX = 1U << 1;
200  const HasFlags PACKET_HAS_AVATAR_ORIENTATION = 1U << 2;
201  const HasFlags PACKET_HAS_AVATAR_SCALE = 1U << 3;
202  const HasFlags PACKET_HAS_LOOK_AT_POSITION = 1U << 4;
203  const HasFlags PACKET_HAS_AUDIO_LOUDNESS = 1U << 5;
204  const HasFlags PACKET_HAS_SENSOR_TO_WORLD_MATRIX = 1U << 6;
205  const HasFlags PACKET_HAS_ADDITIONAL_FLAGS = 1U << 7;
206  const HasFlags PACKET_HAS_PARENT_INFO = 1U << 8;
207  const HasFlags PACKET_HAS_AVATAR_LOCAL_POSITION = 1U << 9;
208  const HasFlags PACKET_HAS_HAND_CONTROLLERS = 1U << 10;
209  const HasFlags PACKET_HAS_FACE_TRACKER_INFO = 1U << 11;
210  const HasFlags PACKET_HAS_JOINT_DATA = 1U << 12;
211  const HasFlags PACKET_HAS_JOINT_DEFAULT_POSE_FLAGS = 1U << 13;
212  const HasFlags PACKET_HAS_GRAB_JOINTS = 1U << 14;
213  const size_t AVATAR_HAS_FLAGS_SIZE = 2;
214 
215  using SixByteQuat = uint8_t[6];
216  using SixByteTrans = uint8_t[6];
217 
218  // NOTE: AvatarDataPackets start with a uint16_t sequence number that is not reflected in the Header structure.
219 
220  PACKED_BEGIN struct Header {
221  HasFlags packetHasFlags; // state flags, indicated which additional records are included in the packet
222  } PACKED_END;
223  const size_t HEADER_SIZE = 2;
224  static_assert(sizeof(Header) == HEADER_SIZE, "AvatarDataPacket::Header size doesn't match.");
225 
226  PACKED_BEGIN struct AvatarGlobalPosition {
227  float globalPosition[3]; // avatar's position
228  } PACKED_END;
229  const size_t AVATAR_GLOBAL_POSITION_SIZE = 12;
230  static_assert(sizeof(AvatarGlobalPosition) == AVATAR_GLOBAL_POSITION_SIZE, "AvatarDataPacket::AvatarGlobalPosition size doesn't match.");
231 
232  PACKED_BEGIN struct AvatarBoundingBox {
233  float avatarDimensions[3]; // avatar's bounding box in world space units, but relative to the position.
234  float boundOriginOffset[3]; // offset from the position of the avatar to the origin of the bounding box
235  } PACKED_END;
236  const size_t AVATAR_BOUNDING_BOX_SIZE = 24;
237  static_assert(sizeof(AvatarBoundingBox) == AVATAR_BOUNDING_BOX_SIZE, "AvatarDataPacket::AvatarBoundingBox size doesn't match.");
238 
239  PACKED_BEGIN struct AvatarOrientation {
240  SixByteQuat avatarOrientation; // encodeded and compressed by packOrientationQuatToSixBytes()
241  } PACKED_END;
242  const size_t AVATAR_ORIENTATION_SIZE = 6;
243  static_assert(sizeof(AvatarOrientation) == AVATAR_ORIENTATION_SIZE, "AvatarDataPacket::AvatarOrientation size doesn't match.");
244 
245  PACKED_BEGIN struct AvatarScale {
246  SmallFloat scale; // avatar's scale, compressed by packFloatRatioToTwoByte()
247  } PACKED_END;
248  const size_t AVATAR_SCALE_SIZE = 2;
249  static_assert(sizeof(AvatarScale) == AVATAR_SCALE_SIZE, "AvatarDataPacket::AvatarScale size doesn't match.");
250 
251  PACKED_BEGIN struct LookAtPosition {
252  float lookAtPosition[3]; // world space position that eyes are focusing on.
253  // FIXME - unless the person has an eye tracker, this is simulated...
254  // a) maybe we can just have the client calculate this
255  // b) at distance this will be hard to discern and can likely be
256  // descimated or dropped completely
257  //
258  // POTENTIAL SAVINGS - 12 bytes
259  } PACKED_END;
260  const size_t LOOK_AT_POSITION_SIZE = 12;
261  static_assert(sizeof(LookAtPosition) == LOOK_AT_POSITION_SIZE, "AvatarDataPacket::LookAtPosition size doesn't match.");
262 
263  PACKED_BEGIN struct AudioLoudness {
264  uint8_t audioLoudness; // current loudness of microphone compressed with packFloatGainToByte()
265  } PACKED_END;
266  const size_t AUDIO_LOUDNESS_SIZE = 1;
267  static_assert(sizeof(AudioLoudness) == AUDIO_LOUDNESS_SIZE, "AvatarDataPacket::AudioLoudness size doesn't match.");
268 
269  PACKED_BEGIN struct SensorToWorldMatrix {
270  // FIXME - these 20 bytes are only used by viewers if my avatar has "attachments"
271  // we could save these bytes if no attachments are active.
272  //
273  // POTENTIAL SAVINGS - 20 bytes
274 
275  SixByteQuat sensorToWorldQuat; // 6 byte compressed quaternion part of sensor to world matrix
276  uint16_t sensorToWorldScale; // uniform scale of sensor to world matrix
277  float sensorToWorldTrans[3]; // fourth column of sensor to world matrix
278  // FIXME - sensorToWorldTrans might be able to be better compressed if it was
279  // relative to the avatar position.
280  } PACKED_END;
281  const size_t SENSOR_TO_WORLD_SIZE = 20;
282  static_assert(sizeof(SensorToWorldMatrix) == SENSOR_TO_WORLD_SIZE, "AvatarDataPacket::SensorToWorldMatrix size doesn't match.");
283 
284  PACKED_BEGIN struct AdditionalFlags {
285  uint16_t flags; // additional flags: hand state, key state, eye tracking
286  } PACKED_END;
287  const size_t ADDITIONAL_FLAGS_SIZE = 2;
288  static_assert(sizeof(AdditionalFlags) == ADDITIONAL_FLAGS_SIZE, "AvatarDataPacket::AdditionalFlags size doesn't match.");
289 
290  // only present if HAS_REFERENTIAL flag is set in AvatarInfo.flags
291  PACKED_BEGIN struct ParentInfo {
292  uint8_t parentUUID[16]; // rfc 4122 encoded
293  uint16_t parentJointIndex;
294  } PACKED_END;
295  const size_t PARENT_INFO_SIZE = 18;
296  static_assert(sizeof(ParentInfo) == PARENT_INFO_SIZE, "AvatarDataPacket::ParentInfo size doesn't match.");
297 
298  // will only ever be included if the avatar has a parent but can change independent of changes to parent info
299  // and so we keep it a separate record
300  PACKED_BEGIN struct AvatarLocalPosition {
301  float localPosition[3]; // parent frame translation of the avatar
302  } PACKED_END;
303 
304  const size_t AVATAR_LOCAL_POSITION_SIZE = 12;
305  static_assert(sizeof(AvatarLocalPosition) == AVATAR_LOCAL_POSITION_SIZE, "AvatarDataPacket::AvatarLocalPosition size doesn't match.");
306 
307  PACKED_BEGIN struct HandControllers {
308  SixByteQuat leftHandRotation;
309  SixByteTrans leftHandTranslation;
310  SixByteQuat rightHandRotation;
311  SixByteTrans rightHandTranslation;
312  } PACKED_END;
313  static const size_t HAND_CONTROLLERS_SIZE = 24;
314  static_assert(sizeof(HandControllers) == HAND_CONTROLLERS_SIZE, "AvatarDataPacket::HandControllers size doesn't match.");
315 
316  const size_t MAX_CONSTANT_HEADER_SIZE = HEADER_SIZE +
317  AVATAR_GLOBAL_POSITION_SIZE +
318  AVATAR_BOUNDING_BOX_SIZE +
319  AVATAR_ORIENTATION_SIZE +
320  AVATAR_SCALE_SIZE +
321  LOOK_AT_POSITION_SIZE +
322  AUDIO_LOUDNESS_SIZE +
323  SENSOR_TO_WORLD_SIZE +
324  ADDITIONAL_FLAGS_SIZE +
325  PARENT_INFO_SIZE +
326  AVATAR_LOCAL_POSITION_SIZE +
327  HAND_CONTROLLERS_SIZE;
328 
329  // variable length structure follows
330 
331  // only present if HAS_SCRIPTED_BLENDSHAPES flag is set in AvatarInfo.flags
332  PACKED_BEGIN struct FaceTrackerInfo {
333  float leftEyeBlink;
334  float rightEyeBlink;
335  float averageLoudness;
336  float browAudioLift;
337  uint8_t numBlendshapeCoefficients;
338  // float blendshapeCoefficients[numBlendshapeCoefficients];
339  } PACKED_END;
340  const size_t FACE_TRACKER_INFO_SIZE = 17;
341  static_assert(sizeof(FaceTrackerInfo) == FACE_TRACKER_INFO_SIZE, "AvatarDataPacket::FaceTrackerInfo size doesn't match.");
342  size_t maxFaceTrackerInfoSize(size_t numBlendshapeCoefficients);
343 
344  /*
345  struct JointData {
346  uint8_t numJoints;
347  uint8_t rotationValidityBits[ceil(numJoints / 8)]; // one bit per joint, if true then a compressed rotation follows.
348  SixByteQuat rotation[numValidRotations]; // encodeded and compressed by packOrientationQuatToSixBytes()
349  uint8_t translationValidityBits[ceil(numJoints / 8)]; // one bit per joint, if true then a compressed translation follows.
350  float maxTranslationDimension; // used to normalize fixed point translation values.
351  SixByteTrans translation[numValidTranslations]; // normalized and compressed by packFloatVec3ToSignedTwoByteFixed()
352  SixByteQuat leftHandControllerRotation;
353  SixByteTrans leftHandControllerTranslation;
354  SixByteQuat rightHandControllerRotation;
355  SixByteTrans rightHandControllerTranslation;
356  };
357  */
358  size_t maxJointDataSize(size_t numJoints);
359  size_t minJointDataSize(size_t numJoints);
360 
361  /*
362  struct JointDefaultPoseFlags {
363  uint8_t numJoints;
364  uint8_t rotationIsDefaultPoseBits[ceil(numJoints / 8)];
365  uint8_t translationIsDefaultPoseBits[ceil(numJoints / 8)];
366  };
367  */
368  size_t maxJointDefaultPoseFlagsSize(size_t numJoints);
369 
370  PACKED_BEGIN struct FarGrabJoints {
371  float leftFarGrabPosition[3]; // left controller far-grab joint position
372  float leftFarGrabRotation[4]; // left controller far-grab joint rotation
373  float rightFarGrabPosition[3]; // right controller far-grab joint position
374  float rightFarGrabRotation[4]; // right controller far-grab joint rotation
375  float mouseFarGrabPosition[3]; // mouse far-grab joint position
376  float mouseFarGrabRotation[4]; // mouse far-grab joint rotation
377  } PACKED_END;
378  const size_t FAR_GRAB_JOINTS_SIZE = 84;
379  static_assert(sizeof(FarGrabJoints) == FAR_GRAB_JOINTS_SIZE, "AvatarDataPacket::FarGrabJoints size doesn't match.");
380 
381  static const size_t MIN_BULK_PACKET_SIZE = NUM_BYTES_RFC4122_UUID + HEADER_SIZE;
382 
383  // AvatarIdentity packet:
384  enum class IdentityFlag: quint32 {none, isReplicated = 0x1, lookAtSnapping = 0x2, verificationFailed = 0x4};
385  Q_DECLARE_FLAGS(IdentityFlags, IdentityFlag)
386 
387  struct SendStatus {
388  HasFlags itemFlags { 0 };
389  bool sendUUID { false };
390  int rotationsSent { 0 }; // ie: index of next unsent joint
391  int translationsSent { 0 };
392  operator bool() { return itemFlags == 0; }
393  };
394 }
395 
396 const float MAX_AUDIO_LOUDNESS = 1000.0f; // close enough for mouth animation
397 
398 // See also static AvatarData::defaultFullAvatarModelUrl().
399 const QString DEFAULT_FULL_AVATAR_MODEL_NAME = QString("Default");
400 
401 // how often should we send a full report about joint rotations, even if they haven't changed?
402 const float AVATAR_SEND_FULL_UPDATE_RATIO = 0.02f;
403 // this controls how large a change in joint-rotation must be before the interface sends it to the avatar mixer
404 const float AVATAR_MIN_ROTATION_DOT = 0.9999999f;
405 const float AVATAR_MIN_TRANSLATION = 0.0001f;
406 
407 // quaternion dot products
408 const float ROTATION_CHANGE_2D = 0.99984770f; // 2 degrees
409 const float ROTATION_CHANGE_4D = 0.99939083f; // 4 degrees
410 const float ROTATION_CHANGE_6D = 0.99862953f; // 6 degrees
411 const float ROTATION_CHANGE_15D = 0.99144486f; // 15 degrees
412 const float ROTATION_CHANGE_179D = 0.00872653f; // 179 degrees
413 
414 // rotation culling distance thresholds
415 const float AVATAR_DISTANCE_LEVEL_1 = 12.5f; // meters
416 const float AVATAR_DISTANCE_LEVEL_2 = 16.6f; // meters
417 const float AVATAR_DISTANCE_LEVEL_3 = 25.0f; // meters
418 const float AVATAR_DISTANCE_LEVEL_4 = 50.0f; // meters
419 const float AVATAR_DISTANCE_LEVEL_5 = 200.0f; // meters
420 
421 // Where one's own Avatar begins in the world (will be overwritten if avatar data file is found).
422 // This is the start location in the Sandbox (xyz: 6270, 211, 6000).
423 const glm::vec3 START_LOCATION(6270, 211, 6000);
424 
425 // Avatar Transit Constants
426 const float AVATAR_TRANSIT_MIN_TRIGGER_DISTANCE = 1.0f;
427 const float AVATAR_TRANSIT_MAX_TRIGGER_DISTANCE = 30.0f;
428 const int AVATAR_TRANSIT_FRAME_COUNT = 5;
429 const float AVATAR_TRANSIT_FRAMES_PER_METER = 0.5f;
430 const float AVATAR_TRANSIT_ABORT_DISTANCE = 0.1f;
431 const bool AVATAR_TRANSIT_DISTANCE_BASED = false;
432 const float AVATAR_TRANSIT_FRAMES_PER_SECOND = 30.0f;
433 const float AVATAR_PRE_TRANSIT_FRAME_COUNT = 10.0f;
434 const float AVATAR_POST_TRANSIT_FRAME_COUNT = 27.0f;
435 
436 enum KeyState {
437  NO_KEY_DOWN = 0,
438  INSERT_KEY_DOWN,
439  DELETE_KEY_DOWN
440 };
441 
442 enum KillAvatarReason : uint8_t {
443  NoReason = 0,
444  AvatarDisconnected,
445  AvatarIgnored,
446  TheirAvatarEnteredYourBubble,
447  YourAvatarEnteredTheirBubble
448 };
449 
450 Q_DECLARE_METATYPE(KillAvatarReason);
451 
452 class QDataStream;
453 
454 class Transform;
455 using TransformPointer = std::shared_ptr<Transform>;
456 
457 class AvatarDataRate {
458 public:
459  RateCounter<> globalPositionRate;
460  RateCounter<> localPositionRate;
461  RateCounter<> handControllersRate;
462  RateCounter<> avatarBoundingBoxRate;
463  RateCounter<> avatarOrientationRate;
464  RateCounter<> avatarScaleRate;
465  RateCounter<> lookAtPositionRate;
466  RateCounter<> audioLoudnessRate;
467  RateCounter<> sensorToWorldRate;
468  RateCounter<> additionalFlagsRate;
469  RateCounter<> parentInfoRate;
470  RateCounter<> faceTrackerRate;
471  RateCounter<> jointDataRate;
472  RateCounter<> jointDefaultPoseFlagsRate;
473  RateCounter<> farGrabJointRate;
474 };
475 
476 class AvatarPriority {
477 public:
478  AvatarPriority(AvatarSharedPointer a, float p) : avatar(a), priority(p) {}
479  AvatarSharedPointer avatar;
480  float priority;
481  // NOTE: we invert the less-than operator to sort high priorities to front
482  bool operator<(const AvatarPriority& other) const { return priority < other.priority; }
483 };
484 
485 class ClientTraitsHandler;
486 
487 class AvatarData : public QObject, public SpatiallyNestable {
488  Q_OBJECT
489 
490  // IMPORTANT: The JSDoc for the following properties should be copied to MyAvatar.h and ScriptableAvatar.h.
491  /*
492  * @property {Vec3} position - The position of the avatar.
493  * @property {number} scale=1.0 - The scale of the avatar. The value can be set to anything between <code>0.005</code> and
494  * <code>1000.0</code>. When the scale value is fetched, it may temporarily be further limited by the domain's settings.
495  * @property {number} density - The density of the avatar in kg/m<sup>3</sup>. The density is used to work out its mass in
496  * the application of physics. <em>Read-only.</em>
497  * @property {Vec3} handPosition - A user-defined hand position, in world coordinates. The position moves with the avatar
498  * but is otherwise not used or changed by Interface.
499  * @property {number} bodyYaw - The left or right rotation about an axis running from the head to the feet of the avatar.
500  * Yaw is sometimes called "heading".
501  * @property {number} bodyPitch - The rotation about an axis running from shoulder to shoulder of the avatar. Pitch is
502  * sometimes called "elevation".
503  * @property {number} bodyRoll - The rotation about an axis running from the chest to the back of the avatar. Roll is
504  * sometimes called "bank".
505  * @property {Quat} orientation - The orientation of the avatar.
506  * @property {Quat} headOrientation - The orientation of the avatar's head.
507  * @property {number} headPitch - The rotation about an axis running from ear to ear of the avatar's head. Pitch is
508  * sometimes called "elevation".
509  * @property {number} headYaw - The rotation left or right about an axis running from the base to the crown of the avatar's
510  * head. Yaw is sometimes called "heading".
511  * @property {number} headRoll - The rotation about an axis running from the nose to the back of the avatar's head. Roll is
512  * sometimes called "bank".
513  * @property {Vec3} velocity - The current velocity of the avatar.
514  * @property {Vec3} angularVelocity - The current angular velocity of the avatar.
515  * @property {number} audioLoudness - The instantaneous loudness of the audio input that the avatar is injecting into the
516  * domain.
517  * @property {number} audioAverageLoudness - The rolling average loudness of the audio input that the avatar is injecting
518  * into the domain.
519  * @property {string} displayName - The avatar's display name.
520  * @property {string} sessionDisplayName - <code>displayName's</code> sanitized and default version defined by the avatar
521  * mixer rather than Interface clients. The result is unique among all avatars present in the domain at the time.
522  * @property {boolean} lookAtSnappingEnabled=true - <code>true</code> if the avatar's eyes snap to look at another avatar's
523  * eyes when the other avatar is in the line of sight and also has <code>lookAtSnappingEnabled == true</code>.
524  * @property {string} skeletonModelURL - The avatar's FST file.
525  * @property {string[]} jointNames - The list of joints in the current avatar model. <em>Read-only.</em>
526  * @property {Uuid} sessionUUID - Unique ID of the avatar in the domain. <em>Read-only.</em>
527  * @property {Mat4} sensorToWorldMatrix - The scale, rotation, and translation transform from the user's real world to the
528  * avatar's size, orientation, and position in the virtual world. <em>Read-only.</em>
529  * @property {Mat4} controllerLeftHandMatrix - The rotation and translation of the left hand controller relative to the
530  * avatar. <em>Read-only.</em>
531  * @property {Mat4} controllerRightHandMatrix - The rotation and translation of the right hand controller relative to the
532  * avatar. <em>Read-only.</em>
533  * @property {number} sensorToWorldScale - The scale that transforms dimensions in the user's real world to the avatar's
534  * size in the virtual world. <em>Read-only.</em>
535  * @property {boolean} hasPriority - <code>true</code> if the avatar is in a "hero" zone, <code>false</code> if it isn't.
536  * <em>Read-only.</em>
537  * @property {boolean} hasScriptedBlendshapes=false - <code>true</code> if blend shapes are controlled by scripted actions,
538  * otherwise <code>false</code>. Set this to <code>true</code> before using the {@link Avatar.setBlendshape} method,
539  * and set back to <code>false</code> after you no longer want scripted control over the blend shapes.
540  * <p><strong>Note:</strong> This property will automatically be set to <code>true</code> if the controller system has
541  * valid facial blend shape actions.</p>
542  * @property {boolean} hasProceduralBlinkFaceMovement=true - <code>true</code> if avatars blink automatically by animating
543  * facial blend shapes, <code>false</code> if automatic blinking is disabled. Set to <code>false</code> to fully control
544  * the blink facial blend shapes via the {@link Avatar.setBlendshape} method.
545  * @property {boolean} hasProceduralEyeFaceMovement=true - <code>true</code> if the facial blend shapes for an avatar's eyes
546  * adjust automatically as the eyes move, <code>false</code> if this automatic movement is disabled. Set this property
547  * to <code>true</code> to prevent the iris from being obscured by the upper or lower lids. Set to <code>false</code> to
548  * fully control the eye blend shapes via the {@link Avatar.setBlendshape} method.
549  * @property {boolean} hasAudioEnabledFaceMovement=true - <code>true</code> if the avatar's mouth blend shapes animate
550  * automatically based on detected microphone input, <code>false</code> if this automatic movement is disabled. Set
551  * this property to <code>false</code> to fully control the mouth facial blend shapes via the
552  * {@link Avatar.setBlendshape} method.
553  */
554  Q_PROPERTY(glm::vec3 position READ getWorldPosition WRITE setPositionViaScript)
555  Q_PROPERTY(float scale READ getDomainLimitedScale WRITE setTargetScale)
556  Q_PROPERTY(float density READ getDensity)
557  Q_PROPERTY(glm::vec3 handPosition READ getHandPosition WRITE setHandPosition)
558  Q_PROPERTY(float bodyYaw READ getBodyYaw WRITE setBodyYaw)
559  Q_PROPERTY(float bodyPitch READ getBodyPitch WRITE setBodyPitch)
560  Q_PROPERTY(float bodyRoll READ getBodyRoll WRITE setBodyRoll)
561 
562  Q_PROPERTY(glm::quat orientation READ getWorldOrientation WRITE setOrientationViaScript)
563  Q_PROPERTY(glm::quat headOrientation READ getHeadOrientation WRITE setHeadOrientation)
564  Q_PROPERTY(float headPitch READ getHeadPitch WRITE setHeadPitch)
565  Q_PROPERTY(float headYaw READ getHeadYaw WRITE setHeadYaw)
566  Q_PROPERTY(float headRoll READ getHeadRoll WRITE setHeadRoll)
567 
568  Q_PROPERTY(glm::vec3 velocity READ getWorldVelocity WRITE setWorldVelocity)
569  Q_PROPERTY(glm::vec3 angularVelocity READ getWorldAngularVelocity WRITE setWorldAngularVelocity)
570 
571  Q_PROPERTY(float audioLoudness READ getAudioLoudness WRITE setAudioLoudness)
572  Q_PROPERTY(float audioAverageLoudness READ getAudioAverageLoudness WRITE setAudioAverageLoudness)
573 
574  Q_PROPERTY(QString displayName READ getDisplayName WRITE setDisplayName NOTIFY displayNameChanged)
575  // sessionDisplayName is sanitized, defaulted version displayName that is defined by the AvatarMixer rather than by Interface clients.
576  // The result is unique among all avatars present at the time.
577  Q_PROPERTY(QString sessionDisplayName READ getSessionDisplayName WRITE setSessionDisplayName NOTIFY sessionDisplayNameChanged)
578  Q_PROPERTY(bool lookAtSnappingEnabled MEMBER _lookAtSnappingEnabled NOTIFY lookAtSnappingChanged)
579  Q_PROPERTY(QString skeletonModelURL READ getSkeletonModelURLFromScript WRITE setSkeletonModelURLFromScript NOTIFY skeletonModelURLChanged)
580 
581  Q_PROPERTY(QStringList jointNames READ getJointNames)
582 
583  Q_PROPERTY(QUuid sessionUUID READ getSessionUUID NOTIFY sessionUUIDChanged)
584 
585  Q_PROPERTY(glm::mat4 sensorToWorldMatrix READ getSensorToWorldMatrix)
586  Q_PROPERTY(glm::mat4 controllerLeftHandMatrix READ getControllerLeftHandMatrix)
587  Q_PROPERTY(glm::mat4 controllerRightHandMatrix READ getControllerRightHandMatrix)
588 
589  Q_PROPERTY(float sensorToWorldScale READ getSensorToWorldScale)
590 
591  Q_PROPERTY(bool hasPriority READ getHasPriority)
592 
593  Q_PROPERTY(bool hasScriptedBlendshapes READ getHasScriptedBlendshapes WRITE setHasScriptedBlendshapes)
594  Q_PROPERTY(bool hasProceduralBlinkFaceMovement READ getHasProceduralBlinkFaceMovement WRITE setHasProceduralBlinkFaceMovement)
595  Q_PROPERTY(bool hasProceduralEyeFaceMovement READ getHasProceduralEyeFaceMovement WRITE setHasProceduralEyeFaceMovement)
596  Q_PROPERTY(bool hasAudioEnabledFaceMovement READ getHasAudioEnabledFaceMovement WRITE setHasAudioEnabledFaceMovement)
597 
598 public:
599  virtual QString getName() const override { return QString("Avatar:") + _displayName; }
600 
601  static const QString FRAME_NAME;
602 
603  static void fromFrame(const QByteArray& frameData, AvatarData& avatar, bool useFrameSkeleton = true);
604  static QByteArray toFrame(const AvatarData& avatar);
605 
606  AvatarData();
607  virtual ~AvatarData();
608 
609  virtual bool isMyAvatarURLProtected() const { return false; } // This needs to be here because both MyAvatar and AvatarData inherit from MyAvatar
610 
611  static const QUrl& defaultFullAvatarModelUrl();
612 
613  const QUuid getSessionUUID() const { return getID(); }
614 
615  glm::vec3 getHandPosition() const;
616  void setHandPosition(const glm::vec3& handPosition);
617 
618  typedef enum {
619  NoData,
620  PALMinimum,
621  MinimumData,
622  CullSmallData,
623  IncludeSmallData,
624  SendAllData
625  } AvatarDataDetail;
626 
627  virtual QByteArray toByteArrayStateful(AvatarDataDetail dataDetail, bool dropFaceTracking = false);
628 
629  virtual QByteArray toByteArray(AvatarDataDetail dataDetail, quint64 lastSentTime, const QVector<JointData>& lastSentJointData,
630  AvatarDataPacket::SendStatus& sendStatus, bool dropFaceTracking, bool distanceAdjust, glm::vec3 viewerPosition,
631  QVector<JointData>* sentJointDataOut, int maxDataSize = 0, AvatarDataRate* outboundDataRateOut = nullptr) const;
632 
633  virtual void doneEncoding(bool cullSmallChanges);
634 
636  bool shouldLogError(const quint64& now);
637 
641  virtual int parseDataFromBuffer(const QByteArray& buffer);
642 
643  virtual void setCollisionWithOtherAvatarsFlags() {};
644 
645  // Body Rotation (degrees)
646  float getBodyYaw() const;
647  void setBodyYaw(float bodyYaw);
648  float getBodyPitch() const;
649  void setBodyPitch(float bodyPitch);
650  float getBodyRoll() const;
651  void setBodyRoll(float bodyRoll);
652 
653  virtual void setPositionViaScript(const glm::vec3& position);
654  virtual void setOrientationViaScript(const glm::quat& orientation);
655 
656  virtual void updateAttitude(const glm::quat& orientation) {}
657 
658  glm::quat getHeadOrientation() const {
659  lazyInitHeadData();
660  return _headData->getOrientation();
661  }
662  void setHeadOrientation(const glm::quat& orientation) {
663  if (_headData) {
664  _headData->setOrientation(orientation);
665  }
666  }
667 
668  void setLookAtPosition(const glm::vec3& lookAtPosition) {
669  if (_headData) {
670  _headData->setLookAtPosition(lookAtPosition);
671  }
672  }
673 
674  void setBlendshapeCoefficients(const QVector<float>& blendshapeCoefficients) {
675  if (_headData) {
676  _headData->setBlendshapeCoefficients(blendshapeCoefficients);
677  }
678  }
679 
680  // access to Head().set/getMousePitch (degrees)
681  float getHeadPitch() const { return _headData->getBasePitch(); }
682  void setHeadPitch(float value) { _headData->setBasePitch(value); }
683 
684  float getHeadYaw() const { return _headData->getBaseYaw(); }
685  void setHeadYaw(float value) { _headData->setBaseYaw(value); }
686 
687  float getHeadRoll() const { return _headData->getBaseRoll(); }
688  void setHeadRoll(float value) { _headData->setBaseRoll(value); }
689 
690  // access to Head().set/getAverageLoudness
691 
692  float getAudioLoudness() const { return _audioLoudness; }
693  void setAudioLoudness(float audioLoudness) {
694  if (audioLoudness != _audioLoudness) {
695  _audioLoudnessChanged = usecTimestampNow();
696  }
697  _audioLoudness = audioLoudness;
698  }
699  bool audioLoudnessChangedSince(quint64 time) const { return _audioLoudnessChanged >= time; }
700 
701  float getAudioAverageLoudness() const { return _audioAverageLoudness; }
702  void setAudioAverageLoudness(float audioAverageLoudness) { _audioAverageLoudness = audioAverageLoudness; }
703 
704  // Scale
705  virtual void setTargetScale(float targetScale);
706 
707  float getDomainLimitedScale() const;
708 
709  void setHasScriptedBlendshapes(bool hasScriptedBlendshapes);
710  bool getHasScriptedBlendshapes() const;
711  void setHasProceduralBlinkFaceMovement(bool hasProceduralBlinkFaceMovement);
712  bool getHasProceduralBlinkFaceMovement() const;
713  void setHasProceduralEyeFaceMovement(bool hasProceduralEyeFaceMovement);
714  bool getHasProceduralEyeFaceMovement() const;
715  void setHasAudioEnabledFaceMovement(bool hasAudioEnabledFaceMovement);
716  bool getHasAudioEnabledFaceMovement() const;
717 
718  /*@jsdoc
719  * Gets the minimum scale allowed for this avatar in the current domain.
720  * This value can change as the user changes avatars or when changing domains.
721  * @function Avatar.getDomainMinScale
722  * @returns {number} The minimum scale allowed for this avatar in the current domain.
723  */
724  Q_INVOKABLE float getDomainMinScale() const;
725 
726  /*@jsdoc
727  * Gets the maximum scale allowed for this avatar in the current domain.
728  * This value can change as the user changes avatars or when changing domains.
729  * @function Avatar.getDomainMaxScale
730  * @returns {number} The maximum scale allowed for this avatar in the current domain.
731  */
732  Q_INVOKABLE float getDomainMaxScale() const;
733 
734  // Returns eye height of avatar in meters, ignoring avatar scale.
735  // if _targetScale is 1 then this will be identical to getEyeHeight;
736  virtual float getUnscaledEyeHeight() const { return DEFAULT_AVATAR_EYE_HEIGHT; }
737 
738  // returns true, if an acurate eye height estimage can be obtained by inspecting the avatar model skeleton and geometry,
739  // not all subclasses of AvatarData have access to this data.
740  virtual bool canMeasureEyeHeight() const { return false; }
741 
742  /*@jsdoc
743  * Gets the current eye height of the avatar.
744  * This height is only an estimate and might be incorrect for avatars that are missing standard joints.
745  * @function Avatar.getEyeHeight
746  * @returns {number} The eye height of the avatar.
747  */
748  Q_INVOKABLE virtual float getEyeHeight() const { return _targetScale * getUnscaledEyeHeight(); }
749 
750  /*@jsdoc
751  * Gets the current height of the avatar.
752  * This height is only an estimate and might be incorrect for avatars that are missing standard joints.
753  * @function Avatar.getHeight
754  * @returns {number} The height of the avatar.
755  */
756  Q_INVOKABLE virtual float getHeight() const;
757 
758  float getUnscaledHeight() const;
759 
760  void setDomainMinimumHeight(float domainMinimumHeight);
761  void setDomainMaximumHeight(float domainMaximumHeight);
762 
763  /*@jsdoc
764  * Sets the pointing state of the hands to control where the laser emanates from. If the right index finger is pointing, the
765  * laser emanates from the tip of that finger, otherwise it emanates from the palm.
766  * @function Avatar.setHandState
767  * @param {HandState} state - The pointing state of the hand.
768  */
769  Q_INVOKABLE void setHandState(char s) { _handState = s; }
770 
771  /*@jsdoc
772  * Gets the pointing state of the hands to control where the laser emanates from. If the right index finger is pointing, the
773  * laser emanates from the tip of that finger, otherwise it emanates from the palm.
774  * @function Avatar.getHandState
775  * @returns {HandState} The pointing state of the hand.
776  */
777  Q_INVOKABLE char getHandState() const { return _handState; }
778 
779  /*@jsdoc
780  * Sets a specific joint's rotation and position relative to its parent, in model coordinates.
781  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
782  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
783  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
784  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
785  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
786  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
787  * @function Avatar.setJointData
788  * @param {number} index - The index of the joint.
789  * @param {Quat} rotation - The rotation of the joint relative to its parent.
790  * @param {Vec3} translation - The translation of the joint relative to its parent, in model coordinates.
791  * @example <caption>Set your avatar to it's default T-pose for a while.<br />
792  * <img alt="Avatar in T-pose" src="https://apidocs.overte.org/examples/t-pose.png" /></caption>
793  * // Set all joint translations and rotations to defaults.
794  * var i, length, rotation, translation;
795  * for (i = 0, length = MyAvatar.getJointNames().length; i < length; i++) {
796  * rotation = MyAvatar.getDefaultJointRotation(i);
797  * translation = MyAvatar.getDefaultJointTranslation(i);
798  * MyAvatar.setJointData(i, rotation, translation);
799  * }
800  *
801  * // Restore your avatar's motion after 5s.
802  * Script.setTimeout(function () {
803  * MyAvatar.clearJointsData();
804  * }, 5000);
805  *
806  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
807  */
808  Q_INVOKABLE virtual void setJointData(int index, const glm::quat& rotation, const glm::vec3& translation);
809 
810  /*@jsdoc
811  * Sets a specific joint's rotation relative to its parent.
812  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
813  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
814  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
815  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
816  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
817  * @function Avatar.setJointRotation
818  * @param {number} index - The index of the joint.
819  * @param {Quat} rotation - The rotation of the joint relative to its parent.
820  */
821  Q_INVOKABLE virtual void setJointRotation(int index, const glm::quat& rotation);
822 
823  /*@jsdoc
824  * Sets a specific joint's translation relative to its parent, in model coordinates.
825  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
826  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
827  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
828  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
829  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
830  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
831  * @function Avatar.setJointTranslation
832  * @param {number} index - The index of the joint.
833  * @param {Vec3} translation - The translation of the joint relative to its parent, in model coordinates.
834  */
835  Q_INVOKABLE virtual void setJointTranslation(int index, const glm::vec3& translation);
836 
837  /*@jsdoc
838  * Clears joint translations and rotations set by script for a specific joint. This restores all motion from the default
839  * animation system including inverse kinematics for that joint.
840  * <p>Note: This is slightly faster than the function variation that specifies the joint name.</p>
841  * @function Avatar.clearJointData
842  * @param {number} index - The index of the joint.
843  */
844  Q_INVOKABLE virtual void clearJointData(int index);
845 
846  /*@jsdoc
847  * Checks that the data for a joint are valid.
848  * @function Avatar.isJointDataValid
849  * @param {number} index - The index of the joint.
850  * @returns {boolean} <code>true</code> if the joint data are valid, <code>false</code> if not.
851  */
852  Q_INVOKABLE bool isJointDataValid(int index) const;
853 
854  /*@jsdoc
855  * Gets the rotation of a joint relative to its parent. For information on the joint hierarchy used, see
856  * <a href="https://docs.overte.org/create/avatars/avatar-standards.html">Avatar Standards</a>.
857  * @function Avatar.getJointRotation
858  * @param {number} index - The index of the joint.
859  * @returns {Quat} The rotation of the joint relative to its parent.
860  */
861  Q_INVOKABLE virtual glm::quat getJointRotation(int index) const;
862 
863  /*@jsdoc
864  * Gets the translation of a joint relative to its parent, in model coordinates.
865  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
866  * <p>For information on the joint hierarchy used, see
867  * <a href="https://docs.overte.org/create/avatars/avatar-standards.html">Avatar Standards</a>.</p>
868  * @function Avatar.getJointTranslation
869  * @param {number} index - The index of the joint.
870  * @returns {Vec3} The translation of the joint relative to its parent, in model coordinates.
871  */
872  Q_INVOKABLE virtual glm::vec3 getJointTranslation(int index) const;
873 
874  /*@jsdoc
875  * Sets a specific joint's rotation and position relative to its parent, in model coordinates.
876  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
877  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
878  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
879  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
880  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
881  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
882  * @function Avatar.setJointData
883  * @param {string} name - The name of the joint.
884  * @param {Quat} rotation - The rotation of the joint relative to its parent.
885  * @param {Vec3} translation - The translation of the joint relative to its parent, in model coordinates.
886  */
887  Q_INVOKABLE virtual void setJointData(const QString& name, const glm::quat& rotation, const glm::vec3& translation);
888 
889  /*@jsdoc
890  * Sets a specific joint's rotation relative to its parent.
891  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
892  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
893  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
894  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
895  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
896  * @function Avatar.setJointRotation
897  * @param {string} name - The name of the joint.
898  * @param {Quat} rotation - The rotation of the joint relative to its parent.
899  * @example <caption>Set your avatar to its default T-pose then rotate its right arm.<br />
900  * <img alt="Avatar in T-pose with arm rotated" src="https://apidocs.overte.org/examples/armpose.png" /></caption>
901  * // Set all joint translations and rotations to defaults.
902  * var i, length, rotation, translation;
903  * for (i = 0, length = MyAvatar.getJointNames().length; i < length; i++) {
904  * rotation = MyAvatar.getDefaultJointRotation(i);
905  * translation = MyAvatar.getDefaultJointTranslation(i);
906  * MyAvatar.setJointData(i, rotation, translation);
907  * }
908  *
909  * // Rotate the right arm.
910  * var newArmRotation = { x: 0.47, y: 0.22, z: -0.02, w: 0.87 };
911  * MyAvatar.setJointRotation("RightArm", newArmRotation);
912  *
913  * // Restore your avatar's motion after 5s.
914  * Script.setTimeout(function () {
915  * MyAvatar.clearJointsData();
916  * }, 5000);
917  *
918  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
919  */
920  Q_INVOKABLE virtual void setJointRotation(const QString& name, const glm::quat& rotation);
921 
922  /*@jsdoc
923  * Sets a specific joint's translation relative to its parent, in model coordinates.
924  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
925  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
926  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
927  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
928  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
929  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
930  * @function Avatar.setJointTranslation
931  * @param {string} name - The name of the joint.
932  * @param {Vec3} translation - The translation of the joint relative to its parent, in model coordinates.
933  * @example <caption>Stretch your avatar's neck. Depending on the avatar you are using, you will either see a gap between
934  * the head and body or you will see the neck stretched.<br />
935  * <img alt="Avatar with neck stretched" src="https://apidocs.overte.org/examples/stretched-neck.png" /></caption>
936  * // Stretch your avatar's neck.
937  * MyAvatar.setJointTranslation("Neck", Vec3.multiply(2, MyAvatar.getJointTranslation("Neck")));
938  *
939  * // Restore your avatar's neck after 5s.
940  * Script.setTimeout(function () {
941  * MyAvatar.clearJointData("Neck");
942  * }, 5000);
943  *
944  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
945  */
946  Q_INVOKABLE virtual void setJointTranslation(const QString& name, const glm::vec3& translation);
947 
948  /*@jsdoc
949  * Clears joint translations and rotations set by script for a specific joint. This restores all motion from the default
950  * animation system including inverse kinematics for that joint.
951  * <p>Note: This is slightly slower than the function variation that specifies the joint index.</p>
952  * @function Avatar.clearJointData
953  * @param {string} name - The name of the joint.
954  * @example <caption>Offset and restore the position of your avatar's head.</caption>
955  * // Stretch your avatar's neck.
956  * MyAvatar.setJointTranslation("Neck", Vec3.multiply(2, MyAvatar.getJointTranslation("Neck")));
957  *
958  * // Restore your avatar's neck after 5s.
959  * Script.setTimeout(function () {
960  * MyAvatar.clearJointData("Neck");
961  * }, 5000);
962  *
963  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
964  */
965  Q_INVOKABLE virtual void clearJointData(const QString& name);
966 
967  /*@jsdoc
968  * Checks if the data for a joint are valid.
969  * @function Avatar.isJointDataValid
970  * @param {string} name - The name of the joint.
971  * @returns {boolean} <code>true</code> if the joint data are valid, <code>false</code> if not.
972  */
973  Q_INVOKABLE virtual bool isJointDataValid(const QString& name) const;
974 
975  /*@jsdoc
976  * Gets the rotation of a joint relative to its parent. For information on the joint hierarchy used, see
977  * <a href="https://docs.overte.org/create/avatars/avatar-standards.html">Avatar Standards</a>.
978  * @function Avatar.getJointRotation
979  * @param {string} name - The name of the joint.
980  * @returns {Quat} The rotation of the joint relative to its parent.
981  * @example <caption>Report the rotation of your avatar's hips joint.</caption>
982  * print(JSON.stringify(MyAvatar.getJointRotation("Hips")));
983  *
984  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
985  */
986  Q_INVOKABLE virtual glm::quat getJointRotation(const QString& name) const;
987 
988  /*@jsdoc
989  * Gets the translation of a joint relative to its parent, in model coordinates.
990  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
991  * <p>For information on the joint hierarchy used, see
992  * <a href="https://docs.overte.org/create/avatars/avatar-standards.html">Avatar Standards</a>.</p>
993  * @function Avatar.getJointTranslation
994  * @param {number} name - The name of the joint.
995  * @returns {Vec3} The translation of the joint relative to its parent, in model coordinates.
996  * @example <caption>Report the translation of your avatar's hips joint.</caption>
997  * print(JSON.stringify(MyAvatar.getJointRotation("Hips")));
998  *
999  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1000  */
1001  Q_INVOKABLE virtual glm::vec3 getJointTranslation(const QString& name) const;
1002 
1003  /*@jsdoc
1004  * Gets the rotations of all joints in the current avatar. Each joint's rotation is relative to its parent joint.
1005  * @function Avatar.getJointRotations
1006  * @returns {Quat[]} The rotations of all joints relative to each's parent. The values are in the same order as the array
1007  * returned by {@link MyAvatar.getJointNames}, or {@link Avatar.getJointNames} if using the <code>Avatar</code> API.
1008  * @example <caption>Report the rotations of all your avatar's joints.</caption>
1009  * print(JSON.stringify(MyAvatar.getJointRotations()));
1010  *
1011  * // Note: If using from the Avatar API, replace all "MyAvatar" with "Avatar".
1012  */
1013  Q_INVOKABLE virtual QVector<glm::quat> getJointRotations() const;
1014 
1015  /*@jsdoc
1016  * Gets the translations of all joints in the current avatar. Each joint's translation is relative to its parent joint, in
1017  * model coordinates.
1018  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
1019  * @function Avatar.getJointTranslations
1020  * @returns {Vec3[]} The translations of all joints relative to each's parent, in model coordinates. The values are in the
1021  * same order as the array returned by {@link MyAvatar.getJointNames}, or {@link Avatar.getJointNames} if using the
1022  * <code>Avatar</code> API.
1023  */
1024  Q_INVOKABLE virtual QVector<glm::vec3> getJointTranslations() const;
1025 
1026  /*@jsdoc
1027  * Sets the rotations of all joints in the current avatar. Each joint's rotation is relative to its parent joint.
1028  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
1029  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
1030  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
1031  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
1032  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
1033  * @function Avatar.setJointRotations
1034  * @param {Quat[]} jointRotations - The rotations for all joints in the avatar. The values are in the same order as the
1035  * array returned by {@link MyAvatar.getJointNames}, or {@link Avatar.getJointNames} if using the <code>Avatar</code> API.
1036  * @example <caption>Set your avatar to its default T-pose then rotate its right arm.<br />
1037  * <img alt="Avatar in T-pose" src="https://apidocs.overte.org/examples/armpose.png" /></caption>
1038  * // Set all joint translations and rotations to defaults.
1039  * var i, length, rotation, translation;
1040  * for (i = 0, length = MyAvatar.getJointNames().length; i < length; i++) {
1041  * rotation = MyAvatar.getDefaultJointRotation(i);
1042  * translation = MyAvatar.getDefaultJointTranslation(i);
1043  * MyAvatar.setJointData(i, rotation, translation);
1044  * }
1045  *
1046  * // Get all join rotations.
1047  * var jointRotations = MyAvatar.getJointRotations();
1048  *
1049  * // Update the rotation of the right arm in the array.
1050  * jointRotations[MyAvatar.getJointIndex("RightArm")] = { x: 0.47, y: 0.22, z: -0.02, w: 0.87 };
1051  *
1052  * // Update all joint rotations.
1053  * MyAvatar.setJointRotations(jointRotations);
1054  *
1055  * // Restore your avatar's motion after 5s.
1056  * Script.setTimeout(function () {
1057  * MyAvatar.clearJointsData();
1058  * }, 5000);
1059  *
1060  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
1061  */
1062  Q_INVOKABLE virtual void setJointRotations(const QVector<glm::quat>& jointRotations);
1063 
1064  /*@jsdoc
1065  * Sets the translations of all joints in the current avatar. Each joint's translation is relative to its parent joint, in
1066  * model coordinates.
1067  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
1068  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
1069  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
1070  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
1071  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
1072  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
1073  * @function Avatar.setJointTranslations
1074  * @param {Vec3[]} translations - The translations for all joints in the avatar, in model coordinates. The values are in
1075  * the same order as the array returned by {@link MyAvatar.getJointNames}, or {@link Avatar.getJointNames} if using the
1076  * <code>Avatar</code> API.
1077  */
1078  Q_INVOKABLE virtual void setJointTranslations(const QVector<glm::vec3>& jointTranslations);
1079 
1080  /*@jsdoc
1081  * Clears all joint translations and rotations that have been set by script. This restores all motion from the default
1082  * animation system including inverse kinematics for all joints.
1083  * @function Avatar.clearJointsData
1084  * @example <caption>Set your avatar to it's default T-pose for a while.</caption>
1085  * // Set all joint translations and rotations to defaults.
1086  * var i, length, rotation, translation;
1087  * for (i = 0, length = MyAvatar.getJointNames().length; i < length; i++) {
1088  * rotation = MyAvatar.getDefaultJointRotation(i);
1089  * translation = MyAvatar.getDefaultJointTranslation(i);
1090  * MyAvatar.setJointData(i, rotation, translation);
1091  * }
1092  *
1093  * // Restore your avatar's motion after 5s.
1094  * Script.setTimeout(function () {
1095  * MyAvatar.clearJointsData();
1096  * }, 5000);
1097  *
1098  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
1099  */
1100  Q_INVOKABLE virtual void clearJointsData();
1101 
1102  /*@jsdoc
1103  * Gets the joint index for a named joint. The joint index value is the position of the joint in the array returned by
1104  * {@link MyAvatar.getJointNames}, or {@link Avatar.getJointNames} if using the <code>Avatar</code> API.
1105  * @function Avatar.getJointIndex
1106  * @param {string} name - The name of the joint.
1107  * @returns {number} The index of the joint if valid, otherwise <code>-1</code>.
1108  * @example <caption>Report the index of your avatar's left arm joint.</caption>
1109  * print(JSON.stringify(MyAvatar.getJointIndex("LeftArm")));
1110  *
1111  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1112  */
1114  Q_INVOKABLE virtual int getJointIndex(const QString& name) const;
1115 
1116  /*@jsdoc
1117  * Gets the names of all the joints in the current avatar.
1118  * @function Avatar.getJointNames
1119  * @returns {string[]} The joint names.
1120  * @example <caption>Report the names of all the joints in your current avatar.</caption>
1121  * print(JSON.stringify(MyAvatar.getJointNames()));
1122  *
1123  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1124  */
1125  Q_INVOKABLE virtual QStringList getJointNames() const;
1126 
1127 
1128  /*@jsdoc
1129  * Sets the value of a blend shape to animate your avatar's face. In order for other users to see the resulting animations
1130  * on your avatar's face, set <code>hasScriptedBlendshapes</code> to <code>true</code>. When you are done using this API,
1131  * set <code>hasScriptedBlendshapes</code> back to <code>false</code> when the animation is complete.
1132  * @function Avatar.setBlendshape
1133  * @param {string} name - The name of the blendshape, per the
1134  * {@link https://docs.overte.org/create/avatars/avatar-standards.html#blendshapes Avatar Standards}.
1135  * @param {number} value - A value between <code>0.0</code> and <code>1.0</code>.
1136  * @example <caption>Open your avatar's mouth wide.</caption>
1137  * MyAvatar.hasScriptedBlendshapes = true;
1138  * MyAvatar.setBlendshape("JawOpen", 1.0);
1139  *
1140  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1141  */
1142  Q_INVOKABLE void setBlendshape(QString name, float val) { _headData->setBlendshape(name, val); }
1143 
1144  virtual void storeAvatarEntityDataPayload(const QUuid& entityID, const QByteArray& payload);
1145 
1146  /*@jsdoc
1147  * @function Avatar.updateAvatarEntity
1148  * @param {Uuid} entityID - The entity ID.
1149  * @param {ArrayBuffer} entityData - Entity data.
1150  * @deprecated This function is deprecated and will be removed.
1151  */
1152  Q_INVOKABLE virtual void updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData);
1153 
1154  /*@jsdoc
1155  * @function Avatar.clearAvatarEntity
1156  * @param {Uuid} entityID - The entity ID.
1157  * @param {boolean} [requiresRemovalFromTree=true] - unused
1158  * @deprecated This function is deprecated and will be removed.
1159  */
1160  Q_INVOKABLE virtual void clearAvatarEntity(const QUuid& entityID, bool requiresRemovalFromTree = true);
1161 
1162  // FIXME: Rename to clearAvatarEntity() once the API call is removed.
1163  virtual void clearAvatarEntityInternal(const QUuid& entityID);
1164 
1165  void clearAvatarEntities();
1166 
1167  QList<QUuid> getAvatarEntityIDs() const;
1168 
1169  /*@jsdoc
1170  * Enables blend shapes set using {@link Avatar.setBlendshape} or {@link MyAvatar.setBlendshape} to be transmitted to other
1171  * users so that they can see the animation of your avatar's face.
1172  * <p class="important">Deprecated: This method is deprecated and will be removed. Use the
1173  * <code>Avatar.hasScriptedBlendshapes</code> or <code>MyAvatar.hasScriptedBlendshapes</code> property instead.</p>
1174  * @function Avatar.setForceFaceTrackerConnected
1175  * @param {boolean} connected - <code>true</code> to enable blend shape changes to be transmitted to other users,
1176  * <code>false</code> to disable.
1177  */
1178  Q_INVOKABLE void setForceFaceTrackerConnected(bool connected) { setHasScriptedBlendshapes(connected); }
1179 
1180  // key state
1181  void setKeyState(KeyState s) { _keyState = s; }
1182  KeyState keyState() const { return _keyState; }
1183 
1184  const HeadData* getHeadData() const { return _headData; }
1185 
1186  struct Identity {
1187  QString displayName;
1188  QString sessionDisplayName;
1189  bool isReplicated;
1190  bool lookAtSnappingEnabled;
1191  AvatarDataPacket::IdentityFlags identityFlags;
1192  };
1193 
1194  // identityChanged returns true if identity has changed, false otherwise.
1195  // identityChanged returns true if identity has changed, false otherwise. Similarly for displayNameChanged and skeletonModelUrlChange.
1196  void processAvatarIdentity(QDataStream& packetStream, bool& identityChanged, bool& displayNameChanged);
1197 
1198  QByteArray packTrait(AvatarTraits::TraitType traitType) const;
1199  QByteArray packTraitInstance(AvatarTraits::TraitType traitType, AvatarTraits::TraitInstanceID instanceID);
1200 
1201  void processTrait(AvatarTraits::TraitType traitType, QByteArray traitBinaryData);
1202  void processTraitInstance(AvatarTraits::TraitType traitType,
1203  AvatarTraits::TraitInstanceID instanceID, QByteArray traitBinaryData);
1204  void processDeletedTraitInstance(AvatarTraits::TraitType traitType, AvatarTraits::TraitInstanceID instanceID);
1205 
1206  void prepareResetTraitInstances();
1207 
1208  QByteArray identityByteArray(bool setIsReplicated = false) const;
1209 
1210  QUrl getWireSafeSkeletonModelURL() const;
1211  virtual const QUrl& getSkeletonModelURL() const;
1212 
1213  const QString& getDisplayName() const { return _displayName; }
1214  const QString& getSessionDisplayName() const { return _sessionDisplayName; }
1215  bool getLookAtSnappingEnabled() const { return _lookAtSnappingEnabled; }
1216 
1217  /*@jsdoc
1218  * Sets the avatar's skeleton model.
1219  * @function Avatar.setSkeletonModelURL
1220  * @param {string} url - The avatar's FST file.
1221  */
1222  Q_INVOKABLE virtual void setSkeletonModelURL(const QUrl& skeletonModelURL);
1223 
1224  virtual void setDisplayName(const QString& displayName);
1225  virtual void setSessionDisplayName(const QString& sessionDisplayName) {
1226  _sessionDisplayName = sessionDisplayName;
1227  markIdentityDataChanged();
1228  }
1229  virtual bool isCertifyFailed() const { return _verificationFailed; }
1230 
1231  QString getSkeletonModelURLFromScript() const;
1232  void setSkeletonModelURLFromScript(const QString& skeletonModelString) { setSkeletonModelURL(QUrl(skeletonModelString)); }
1233 
1234  void setOwningAvatarMixer(const QWeakPointer<Node>& owningAvatarMixer) { _owningAvatarMixer = owningAvatarMixer; }
1235 
1236  int getAverageBytesReceivedPerSecond() const;
1237  int getReceiveRate() const;
1238 
1239  // An Avatar can be set Priority from the AvatarMixer side.
1240  bool getHasPriority() const { return _hasPriority; }
1241  // regular setHasPriority does a check of state changed and if true reset 'additionalFlagsChanged' timestamp
1242  void setHasPriority(bool hasPriority) {
1243  if (_hasPriority != hasPriority) {
1244  _additionalFlagsChanged = usecTimestampNow();
1245  _hasPriority = hasPriority;
1246  }
1247  }
1248  // In some cases, we want to assign the hasPRiority flag without reseting timestamp
1249  void setHasPriorityWithoutTimestampReset(bool hasPriority) { _hasPriority = hasPriority; }
1250 
1251  const glm::vec3& getTargetVelocity() const { return _targetVelocity; }
1252 
1253  void clearRecordingBasis();
1254  TransformPointer getRecordingBasis() const;
1255  void setRecordingBasis(TransformPointer recordingBasis = TransformPointer());
1256  void createRecordingIDs();
1257  virtual void avatarEntityDataToJson(QJsonObject& root) const;
1258  QJsonObject toJson() const;
1259  void fromJson(const QJsonObject& json, bool useFrameSkeleton = true);
1260 
1261  glm::vec3 getClientGlobalPosition() const { return _globalPosition; }
1262  AABox getGlobalBoundingBox() const { return AABox(_globalPosition + _globalBoundingBoxOffset - _globalBoundingBoxDimensions, _globalBoundingBoxDimensions); }
1263  AABox getDefaultBubbleBox() const;
1264 
1265  /*@jsdoc
1266  * @comment Documented in derived classes' JSDoc because implementations are different.
1267  */
1268  // Get avatar entity data with all property values. Used in API.
1269  Q_INVOKABLE virtual AvatarEntityMap getAvatarEntityData() const;
1270 
1271  // Get avatar entity data with non-default property values. Used internally.
1272  virtual AvatarEntityMap getAvatarEntityDataNonDefault() const;
1273 
1274  /*@jsdoc
1275  * @comment Documented in derived classes' JSDoc because implementations are different.
1276  */
1277  Q_INVOKABLE virtual void setAvatarEntityData(const AvatarEntityMap& avatarEntityData);
1278 
1279  AvatarEntityIDs getAndClearRecentlyRemovedIDs();
1280 
1281  /*@jsdoc
1282  * Gets the transform from the user's real world to the avatar's size, orientation, and position in the virtual world.
1283  * @function Avatar.getSensorToWorldMatrix
1284  * @returns {Mat4} The scale, rotation, and translation transform from the user's real world to the avatar's size,
1285  * orientation, and position in the virtual world.
1286  * @example <caption>Report the sensor to world matrix.</caption>
1287  * var sensorToWorldMatrix = MyAvatar.getSensorToWorldMatrix();
1288  * print("Sensor to woprld matrix: " + JSON.stringify(sensorToWorldMatrix));
1289  * print("Rotation: " + JSON.stringify(Mat4.extractRotation(sensorToWorldMatrix)));
1290  * print("Translation: " + JSON.stringify(Mat4.extractTranslation(sensorToWorldMatrix)));
1291  * print("Scale: " + JSON.stringify(Mat4.extractScale(sensorToWorldMatrix)));
1292  *
1293  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1294  */
1295  // thread safe
1296  Q_INVOKABLE glm::mat4 getSensorToWorldMatrix() const;
1297 
1298  /*@jsdoc
1299  * Gets the scale that transforms dimensions in the user's real world to the avatar's size in the virtual world.
1300  * @function Avatar.getSensorToWorldScale
1301  * @returns {number} The scale that transforms dimensions in the user's real world to the avatar's size in the virtual
1302  * world.
1303  */
1304  // thread safe
1305  Q_INVOKABLE float getSensorToWorldScale() const;
1306 
1307  /*@jsdoc
1308  * Gets the rotation and translation of the left hand controller relative to the avatar.
1309  * @function Avatar.getControllerLeftHandMatrix
1310  * @returns {Mat4} The rotation and translation of the left hand controller relative to the avatar.
1311  * @example <caption>Report the left hand controller matrix.</caption>
1312  * var leftHandMatrix = MyAvatar.getControllerLeftHandMatrix();
1313  * print("Controller left hand matrix: " + JSON.stringify(leftHandMatrix));
1314  * print("Rotation: " + JSON.stringify(Mat4.extractRotation(leftHandMatrix)));
1315  * print("Translation: " + JSON.stringify(Mat4.extractTranslation(leftHandMatrix)));
1316  * print("Scale: " + JSON.stringify(Mat4.extractScale(leftHandMatrix))); // Always 1,1,1.
1317  *
1318  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1319  */
1320  // thread safe
1321  Q_INVOKABLE glm::mat4 getControllerLeftHandMatrix() const;
1322 
1323  /*@jsdoc
1324  * Gets the rotation and translation of the right hand controller relative to the avatar.
1325  * @function Avatar.getControllerRightHandMatrix
1326  * @returns {Mat4} The rotation and translation of the right hand controller relative to the avatar.
1327  */
1328  // thread safe
1329  Q_INVOKABLE glm::mat4 getControllerRightHandMatrix() const;
1330 
1331 
1332  /*@jsdoc
1333  * Gets the amount of avatar mixer data being generated by the avatar.
1334  * @function Avatar.getDataRate
1335  * @param {AvatarDataRate} [rateName=""] - The type of avatar mixer data to get the data rate of.
1336  * @returns {number} The data rate in kbps.
1337  */
1338  Q_INVOKABLE float getDataRate(const QString& rateName = QString("")) const;
1339 
1340  /*@jsdoc
1341  * Gets the update rate of avatar mixer data being generated by the avatar.
1342  * @function Avatar.getUpdateRate
1343  * @param {AvatarUpdateRate} [rateName=""] - The type of avatar mixer data to get the update rate of.
1344  * @returns {number} The update rate in Hz.
1345  */
1346  Q_INVOKABLE float getUpdateRate(const QString& rateName = QString("")) const;
1347 
1348  int getJointCount() const { return _jointData.size(); }
1349 
1350  QVector<JointData> getLastSentJointData() {
1351  QReadLocker readLock(&_jointDataLock);
1352  _lastSentJointData.resize(_jointData.size());
1353  return _lastSentJointData;
1354  }
1355 
1356  // A method intended to be overriden by MyAvatar for polling orientation for network transmission.
1357  virtual glm::quat getOrientationOutbound() const;
1358 
1359  // TODO: remove this HACK once we settle on optimal sort coefficients
1360  // These coefficients exposed for fine tuning the sort priority for transfering new _jointData to the render pipeline.
1361  static float _avatarSortCoefficientSize;
1362  static float _avatarSortCoefficientCenter;
1363  static float _avatarSortCoefficientAge;
1364 
1365  bool getIdentityDataChanged() const { return _identityDataChanged; } // has the identity data changed since the last time sendIdentityPacket() was called
1366  void markIdentityDataChanged() { _identityDataChanged = true; }
1367 
1368  void pushIdentitySequenceNumber() { ++_identitySequenceNumber; };
1369  bool hasProcessedFirstIdentity() const { return _hasProcessedFirstIdentity; }
1370 
1371  float getDensity() const { return _density; }
1372 
1373  bool getIsReplicated() const { return _isReplicated; }
1374 
1375  void setReplicaIndex(int replicaIndex) { _replicaIndex = replicaIndex; }
1376  int getReplicaIndex() { return _replicaIndex; }
1377 
1378  static const float DEFAULT_BUBBLE_SCALE; /* = 2.4 */
1379  AABox computeBubbleBox(float bubbleScale = DEFAULT_BUBBLE_SCALE) const;
1380 
1381  void setIsNewAvatar(bool isNewAvatar) { _isNewAvatar = isNewAvatar; }
1382  bool getIsNewAvatar() { return _isNewAvatar; }
1383  void setIsClientAvatar(bool isClientAvatar) { _isClientAvatar = isClientAvatar; }
1384  void setSkeletonData(const std::vector<AvatarSkeletonTrait::UnpackedJointData>& skeletonData);
1385  std::vector<AvatarSkeletonTrait::UnpackedJointData> getSkeletonData() const;
1386  void sendSkeletonData() const;
1387  QVector<JointData> getJointData() const;
1388  glm::vec3 getHeadJointFrontVector() const;
1389 
1390 signals:
1391 
1392  /*@jsdoc
1393  * Triggered when the avatar's <code>displayName</code> property value changes.
1394  * @function Avatar.displayNameChanged
1395  * @returns {Signal}
1396  * @example <caption>Report when your avatar display name changes.</caption>
1397  * MyAvatar.displayNameChanged.connect(function () {
1398  * print("Avatar display name changed to: " + MyAvatar.displayName);
1399  * });
1400  *
1401  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1402  */
1403  void displayNameChanged();
1404 
1405  /*@jsdoc
1406  * Triggered when the avatar's <code>sessionDisplayName</code> property value changes.
1407  * @function Avatar.sessionDisplayNameChanged
1408  * @returns {Signal}
1409  * @example <caption>Report when your avatar's session display name changes.</caption>
1410  * MyAvatar.sessionDisplayNameChanged.connect(function () {
1411  * print("Avatar session display name changed to: " + MyAvatar.sessionDisplayName);
1412  * });
1413  *
1414  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1415  */
1416  void sessionDisplayNameChanged();
1417 
1418  /*@jsdoc
1419  * Triggered when the avatar's model (i.e., <code>skeletonModelURL</code> property value) changes.
1420  * @function Avatar.skeletonModelURLChanged
1421  * @returns {Signal}
1422  * @example <caption>Report when your avatar's skeleton model changes.</caption>
1423  * MyAvatar.skeletonModelURLChanged.connect(function () {
1424  * print("Skeleton model changed to: " + MyAvatar.skeletonModelURL);
1425  * });
1426  *
1427  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1428  */
1429  void skeletonModelURLChanged();
1430 
1431  /*@jsdoc
1432  * Triggered when the avatar's <code>lookAtSnappingEnabled</code> property value changes.
1433  * @function Avatar.lookAtSnappingChanged
1434  * @param {boolean} enabled - <code>true</code> if look-at snapping is enabled, <code>false</code> if not.
1435  * @returns {Signal}
1436  * @example <caption>Report when your look-at snapping setting changes.</caption>
1437  * MyAvatar.lookAtSnappingChanged.connect(function () {
1438  * print("Avatar look-at snapping changed to: " + MyAvatar.lookAtSnappingEnabled);
1439  * });
1440  *
1441  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1442  */
1443  void lookAtSnappingChanged(bool enabled);
1444 
1445  /*@jsdoc
1446  * Triggered when the avatar's <code>sessionUUID</code> property value changes.
1447  * @function Avatar.sessionUUIDChanged
1448  * @returns {Signal}
1449  * @example <caption>Report when your avatar's session UUID changes.</caption>
1450  * MyAvatar.sessionUUIDChanged.connect(function () {
1451  * print("Avatar session UUID changed to: " + MyAvatar.sessionUUID);
1452  * });
1453  *
1454  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1455  */
1456  void sessionUUIDChanged();
1457 
1458 public slots:
1459 
1460 /*@jsdoc
1461  * @function Avatar.sendAvatarDataPacket
1462  * @param {boolean} [sendAll=false] - Send all.
1463  * @returns {number}
1464  * @deprecated This function is deprecated and will be removed.
1465  */
1466  virtual int sendAvatarDataPacket(bool sendAll = false);
1467 
1468  /*@jsdoc
1469  * @function Avatar.sendIdentityPacket
1470  * @returns {number}
1471  * @deprecated This function is deprecated and will be removed.
1472  */
1473  int sendIdentityPacket();
1474 
1475  /*@jsdoc
1476  * @function Avatar.setSessionUUID
1477  * @param {Uuid} sessionUUID - Session UUID.
1478  * @deprecated This function is deprecated and will be removed.
1479  */
1480  virtual void setSessionUUID(const QUuid& sessionUUID) {
1481  if (sessionUUID != getID()) {
1482  if (sessionUUID == QUuid()) {
1483  setID(AVATAR_SELF_ID);
1484  } else {
1485  setID(sessionUUID);
1486  }
1487  emit sessionUUIDChanged();
1488  }
1489  }
1490 
1491 
1492  /*@jsdoc
1493  * Gets the rotation of a joint relative to the avatar.
1494  * <p><strong>Warning:</strong> Not able to be used in the <code>Avatar</code> API.</p>
1495  * @function Avatar.getAbsoluteJointRotationInObjectFrame
1496  * @param {number} index - The index of the joint. <em>Not used.</em>
1497  * @returns {Quat} <code>Quat.IDENTITY</code>.
1498  */
1499  virtual glm::quat getAbsoluteJointRotationInObjectFrame(int index) const override;
1500 
1501  /*@jsdoc
1502  * Gets the translation of a joint relative to the avatar.
1503  * <p><strong>Warning:</strong> Not able to be used in the <code>Avatar</code> API.</p>
1504  * @function Avatar.getAbsoluteJointTranslationInObjectFrame
1505  * @param {number} index - The index of the joint. <em>Not used.</em>
1506  * @returns {Vec3} <code>Vec3.ZERO</code>.
1507  */
1508  virtual glm::vec3 getAbsoluteJointTranslationInObjectFrame(int index) const override;
1509 
1510  /*@jsdoc
1511  * Sets the rotation of a joint relative to the avatar.
1512  * <p><strong>Warning:</strong> Not able to be used in the <code>Avatar</code> API.</p>
1513  * @function Avatar.setAbsoluteJointRotationInObjectFrame
1514  * @param {number} index - The index of the joint. <em>Not used.</em>
1515  * @param {Quat} rotation - The rotation of the joint relative to the avatar. <em>Not used.</em>
1516  * @returns {boolean} <code>false</code>.
1517  */
1518  virtual bool setAbsoluteJointRotationInObjectFrame(int index, const glm::quat& rotation) override { return false; }
1519 
1520  /*@jsdoc
1521  * Sets the translation of a joint relative to the avatar.
1522  * <p><strong>Warning:</strong> Not able to be used in the <code>Avatar</code> API.</p>
1523  * @function Avatar.setAbsoluteJointTranslationInObjectFrame
1524  * @param {number} index - The index of the joint. <em>Not used.</em>
1525  * @param {Vec3} translation - The translation of the joint relative to the avatar. <em>Not used.</em>
1526  * @returns {boolean} <code>false</code>.
1527  */
1528  virtual bool setAbsoluteJointTranslationInObjectFrame(int index, const glm::vec3& translation) override { return false; }
1529 
1530  /*@jsdoc
1531  * Gets the target scale of the avatar without any restrictions on permissible values imposed by the domain. In contrast, the
1532  * <code>scale</code> property's value may be limited by the domain's settings.
1533  * @function Avatar.getTargetScale
1534  * @returns {number} The target scale of the avatar.
1535  * @example <caption>Compare the target and current avatar scales.</caption>
1536  * print("Current avatar scale: " + MyAvatar.scale);
1537  * print("Target avatar scale: " + MyAvatar.getTargetScale());
1538  *
1539  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
1540  */
1541  float getTargetScale() const { return _targetScale; } // why is this a slot?
1542 
1543  /*@jsdoc
1544  * @function Avatar.resetLastSent
1545  * @deprecated This function is deprecated and will be removed.
1546  */
1547  void resetLastSent() { _lastToByteArray = 0; }
1548 
1549 protected:
1550  void insertRemovedEntityID(const QUuid entityID);
1551  void lazyInitHeadData() const;
1552 
1553  float getDistanceBasedMinRotationDOT(glm::vec3 viewerPosition) const;
1554  float getDistanceBasedMinTranslationDistance(glm::vec3 viewerPosition) const;
1555 
1556  bool avatarBoundingBoxChangedSince(quint64 time) const { return _avatarBoundingBoxChanged >= time; }
1557  bool avatarScaleChangedSince(quint64 time) const { return _avatarScaleChanged >= time; }
1558  bool lookAtPositionChangedSince(quint64 time) const { return _headData->lookAtPositionChangedSince(time); }
1559  bool sensorToWorldMatrixChangedSince(quint64 time) const { return _sensorToWorldMatrixChanged >= time; }
1560  bool additionalFlagsChangedSince(quint64 time) const { return _additionalFlagsChanged >= time; }
1561  bool parentInfoChangedSince(quint64 time) const { return _parentChanged >= time; }
1562  bool faceTrackerInfoChangedSince(quint64 time) const { return true; } // FIXME
1563 
1564  bool hasParent() const { return !getParentID().isNull(); }
1565 
1566  QByteArray packSkeletonData() const;
1567  QByteArray packSkeletonModelURL() const;
1568  QByteArray packAvatarEntityTraitInstance(AvatarTraits::TraitInstanceID traitInstanceID);
1569  QByteArray packGrabTraitInstance(AvatarTraits::TraitInstanceID traitInstanceID);
1570 
1571  const QVector<JointData>& getRawJointData() const { return _jointData; }
1572  void setRawJointData(QVector<JointData> data);
1573 
1574  void unpackSkeletonModelURL(const QByteArray& data);
1575  void unpackSkeletonData(const QByteArray& data);
1576 
1577  // isReplicated will be true on downstream Avatar Mixers and their clients, but false on the upstream "primary"
1578  // Audio Mixer that the replicated avatar is connected to.
1579  bool _isReplicated{ false };
1580 
1581  glm::vec3 _handPosition;
1582  virtual const QString& getSessionDisplayNameForTransport() const { return _sessionDisplayName; }
1583  virtual void maybeUpdateSessionDisplayNameFromTransport(const QString& sessionDisplayName) { } // No-op in AvatarMixer
1584 
1585  virtual void onIdentityRecieved() {}
1586 
1587  // Body scale
1588  float _targetScale;
1589  float _domainMinimumHeight { MIN_AVATAR_HEIGHT };
1590  float _domainMaximumHeight { MAX_AVATAR_HEIGHT };
1591 
1592  // Hand state (are we grabbing something or not)
1593  char _handState;
1594 
1595  QVector<JointData> _jointData;
1596  QVector<JointData> _lastSentJointData;
1597  mutable QReadWriteLock _jointDataLock;
1598 
1599  // key state
1600  KeyState _keyState;
1601 
1602  bool _hasNewJointData { true }; // set in AvatarData, cleared in Avatar
1603 
1604  mutable HeadData* _headData { nullptr };
1605 
1606  QUrl _skeletonModelURL;
1607  QString _displayName;
1608  QString _sessionDisplayName { };
1609  bool _lookAtSnappingEnabled { true };
1610  bool _verificationFailed { false };
1611 
1612  quint64 _errorLogExpiry;
1613 
1614  QWeakPointer<Node> _owningAvatarMixer;
1615 
1616  glm::vec3 _targetVelocity;
1617 
1618  SimpleMovingAverage _averageBytesReceived;
1619 
1620  // During recording, this holds the starting position, orientation & scale of the recorded avatar
1621  // During playback, it holds the origin from which to play the relative positions in the clip
1622  TransformPointer _recordingBasis;
1623 
1624  // _globalPosition is sent along with localPosition + parent because the avatar-mixer doesn't know
1625  // where Entities are located. This is currently only used by the mixer to decide how often to send
1626  // updates about one avatar to another.
1627  glm::vec3 _globalPosition { 0, 0, 0 };
1628  glm::vec3 _serverPosition { 0, 0, 0 };
1629 
1630  quint64 _globalPositionChanged { 0 };
1631  quint64 _avatarBoundingBoxChanged { 0 };
1632  quint64 _avatarScaleChanged { 0 };
1633  quint64 _sensorToWorldMatrixChanged { 0 };
1634  quint64 _additionalFlagsChanged { 0 };
1635  quint64 _parentChanged { 0 };
1636 
1637  quint64 _lastToByteArray { 0 }; // tracks the last time we did a toByteArray
1638 
1639  // Some rate data for incoming data in bytes
1640  RateCounter<> _parseBufferRate;
1641  RateCounter<> _globalPositionRate;
1642  RateCounter<> _localPositionRate;
1643  RateCounter<> _handControllersRate;
1644  RateCounter<> _avatarBoundingBoxRate;
1645  RateCounter<> _avatarOrientationRate;
1646  RateCounter<> _avatarScaleRate;
1647  RateCounter<> _lookAtPositionRate;
1648  RateCounter<> _audioLoudnessRate;
1649  RateCounter<> _sensorToWorldRate;
1650  RateCounter<> _additionalFlagsRate;
1651  RateCounter<> _parentInfoRate;
1652  RateCounter<> _faceTrackerRate;
1653  RateCounter<> _jointDataRate;
1654  RateCounter<> _jointDefaultPoseFlagsRate;
1655  RateCounter<> _farGrabJointRate;
1656 
1657  // Some rate data for incoming data updates
1658  RateCounter<> _parseBufferUpdateRate;
1659  RateCounter<> _globalPositionUpdateRate;
1660  RateCounter<> _localPositionUpdateRate;
1661  RateCounter<> _handControllersUpdateRate;
1662  RateCounter<> _avatarBoundingBoxUpdateRate;
1663  RateCounter<> _avatarOrientationUpdateRate;
1664  RateCounter<> _avatarScaleUpdateRate;
1665  RateCounter<> _lookAtPositionUpdateRate;
1666  RateCounter<> _audioLoudnessUpdateRate;
1667  RateCounter<> _sensorToWorldUpdateRate;
1668  RateCounter<> _additionalFlagsUpdateRate;
1669  RateCounter<> _parentInfoUpdateRate;
1670  RateCounter<> _faceTrackerUpdateRate;
1671  RateCounter<> _jointDataUpdateRate;
1672  RateCounter<> _jointDefaultPoseFlagsUpdateRate;
1673  RateCounter<> _farGrabJointUpdateRate;
1674 
1675  // Some rate data for outgoing data
1676  AvatarDataRate _outboundDataRate;
1677 
1678  glm::vec3 _globalBoundingBoxDimensions;
1679  glm::vec3 _globalBoundingBoxOffset;
1680 
1681  AABox _defaultBubbleBox;
1682  AABox _fitBoundingBox;
1683 
1684  mutable ReadWriteLockable _avatarEntitiesLock;
1685  AvatarEntityIDs _avatarEntityRemoved; // recently removed AvatarEntity ids
1686  AvatarEntityIDs _avatarEntityForRecording; // create new entities id for avatar recording
1687  PackedAvatarEntityMap _packedAvatarEntityData;
1688  bool _avatarEntityDataChanged { false };
1689 
1690  mutable ReadWriteLockable _avatarGrabsLock;
1691  AvatarGrabDataMap _avatarGrabData;
1692  bool _avatarGrabDataChanged { false }; // by network
1693 
1694  mutable ReadWriteLockable _avatarSkeletonDataLock;
1695  std::vector<AvatarSkeletonTrait::UnpackedJointData> _avatarSkeletonData;
1696 
1697  // used to transform any sensor into world space, including the _hmdSensorMat, or hand controllers.
1698  ThreadSafeValueCache<glm::mat4> _sensorToWorldMatrixCache { glm::mat4() };
1699  ThreadSafeValueCache<glm::mat4> _controllerLeftHandMatrixCache { glm::mat4() };
1700  ThreadSafeValueCache<glm::mat4> _controllerRightHandMatrixCache { glm::mat4() };
1701 
1702  ThreadSafeValueCache<glm::mat4> _farGrabRightMatrixCache { glm::mat4() };
1703  ThreadSafeValueCache<glm::mat4> _farGrabLeftMatrixCache { glm::mat4() };
1704  ThreadSafeValueCache<glm::mat4> _farGrabMouseMatrixCache { glm::mat4() };
1705 
1706  ThreadSafeValueCache<QVariantMap> _collisionCapsuleCache{ QVariantMap() };
1707 
1708  int getFauxJointIndex(const QString& name) const;
1709 
1710  float _audioLoudness { 0.0f };
1711  quint64 _audioLoudnessChanged { 0 };
1712  float _audioAverageLoudness { 0.0f };
1713 
1714  bool _identityDataChanged { false };
1715  udt::SequenceNumber _identitySequenceNumber { 0 };
1716  bool _hasProcessedFirstIdentity { false };
1717  float _density;
1718  int _replicaIndex { 0 };
1719  bool _isNewAvatar { true };
1720  bool _isClientAvatar { false };
1721  bool _collideWithOtherAvatars { true };
1722  bool _hasPriority{ false };
1723 
1724  // null unless MyAvatar or ScriptableAvatar sending traits data to mixer
1725  std::unique_ptr<ClientTraitsHandler, LaterDeleter> _clientTraitsHandler;
1726 
1727  template <typename T, typename F>
1728  T readLockWithNamedJointIndex(const QString& name, const T& defaultValue, F f) const {
1729  QReadLocker readLock(&_jointDataLock);
1730  int index = getJointIndex(name);
1731  if (index == -1) {
1732  return defaultValue;
1733  }
1734  return f(index);
1735  }
1736 
1737  template <typename T, typename F>
1738  T readLockWithNamedJointIndex(const QString& name, F f) const {
1739  return readLockWithNamedJointIndex(name, T(), f);
1740  }
1741 
1742  template <typename F>
1743  void writeLockWithNamedJointIndex(const QString& name, F f) {
1744  QWriteLocker writeLock(&_jointDataLock);
1745  int index = getJointIndex(name);
1746  if (index == -1) {
1747  return;
1748  }
1749  if (_jointData.size() <= index) {
1750  _jointData.resize(index + 1);
1751  }
1752  f(index);
1753  }
1754 
1755  bool updateAvatarGrabData(const QUuid& grabID, const QByteArray& grabData);
1756  virtual void clearAvatarGrabData(const QUuid& grabID);
1757 
1758 private:
1759  Q_DISABLE_COPY(AvatarData)
1760 
1761  friend void avatarStateFromFrame(const QByteArray& frameData, AvatarData* _avatar);
1762  // Required for setRawJointData. Making setRawJointData public would expose it to scripting interface.
1763  friend class SkeletonModel;
1764  friend class MyAvatar;
1765  static QUrl _defaultFullAvatarModelUrl;
1766 };
1767 Q_DECLARE_METATYPE(AvatarData*)
1768 
1769 QJsonValue toJsonValue(const JointData& joint);
1770 JointData jointDataFromJsonValue(const QJsonValue& q);
1771 
1772 class RayToAvatarIntersectionResult {
1773 public:
1774  bool intersects { false };
1775  QUuid avatarID;
1776  float distance { FLT_MAX };
1777  BoxFace face { UNKNOWN_FACE };
1778  glm::vec3 intersection { glm::vec3(0.0f, 0.0f, 0.0f) };
1779  glm::vec3 surfaceNormal { glm::vec3(0.0f, 1.0f, 0.0f) };
1780  int jointIndex { -1 };
1781  QVariantMap extraInfo;
1782 };
1783 Q_DECLARE_METATYPE(RayToAvatarIntersectionResult)
1784 ScriptValue RayToAvatarIntersectionResultToScriptValue(ScriptEngine* engine, const RayToAvatarIntersectionResult& results);
1785 bool RayToAvatarIntersectionResultFromScriptValue(const ScriptValue& object, RayToAvatarIntersectionResult& results);
1786 
1787 // No JSDoc because it's not provided as a type to the script engine.
1788 class ParabolaToAvatarIntersectionResult {
1789 public:
1790  bool intersects { false };
1791  QUuid avatarID;
1792  float distance { 0.0f };
1793  float parabolicDistance { 0.0f };
1794  BoxFace face;
1795  glm::vec3 intersection;
1796  glm::vec3 surfaceNormal;
1797  QVariantMap extraInfo;
1798 };
1799 
1800 Q_DECLARE_METATYPE(AvatarEntityMap)
1801 
1802 ScriptValue AvatarEntityMapToScriptValue(ScriptEngine* engine, const AvatarEntityMap& value);
1803 bool AvatarEntityMapFromScriptValue(const ScriptValue& object, AvatarEntityMap& value);
1804 
1805 // faux joint indexes (-1 means invalid)
1806 const int NO_JOINT_INDEX = 65535; // -1
1807 const int SENSOR_TO_WORLD_MATRIX_INDEX = 65534; // -2
1808 const int CONTROLLER_RIGHTHAND_INDEX = 65533; // -3
1809 const int CONTROLLER_LEFTHAND_INDEX = 65532; // -4
1810 const int CAMERA_RELATIVE_CONTROLLER_RIGHTHAND_INDEX = 65531; // -5
1811 const int CAMERA_RELATIVE_CONTROLLER_LEFTHAND_INDEX = 65530; // -6
1812 const int CAMERA_MATRIX_INDEX = 65529; // -7
1813 const int FARGRAB_RIGHTHAND_INDEX = 65528; // -8
1814 const int FARGRAB_LEFTHAND_INDEX = 65527; // -9
1815 const int FARGRAB_MOUSE_INDEX = 65526; // -10
1816 
1817 const int LOWEST_PSEUDO_JOINT_INDEX = 65526;
1818 
1819 const int MAX_NUM_AVATAR_GRABS = 6;
1820 
1821 #endif // hifi_AvatarData_h
Provides an engine-independent interface for a scripting engine.
Definition: ScriptEngine.h:93
[ScriptInterface] Provides an engine-independent interface for QScriptValue
Definition: ScriptValue.h:40
A skeleton loaded from a model.
Definition: SkeletonModel.h:25