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 AttachmentData;
455 class Transform;
456 using TransformPointer = std::shared_ptr<Transform>;
457 
458 class AvatarDataRate {
459 public:
460  RateCounter<> globalPositionRate;
461  RateCounter<> localPositionRate;
462  RateCounter<> handControllersRate;
463  RateCounter<> avatarBoundingBoxRate;
464  RateCounter<> avatarOrientationRate;
465  RateCounter<> avatarScaleRate;
466  RateCounter<> lookAtPositionRate;
467  RateCounter<> audioLoudnessRate;
468  RateCounter<> sensorToWorldRate;
469  RateCounter<> additionalFlagsRate;
470  RateCounter<> parentInfoRate;
471  RateCounter<> faceTrackerRate;
472  RateCounter<> jointDataRate;
473  RateCounter<> jointDefaultPoseFlagsRate;
474  RateCounter<> farGrabJointRate;
475 };
476 
477 class AvatarPriority {
478 public:
479  AvatarPriority(AvatarSharedPointer a, float p) : avatar(a), priority(p) {}
480  AvatarSharedPointer avatar;
481  float priority;
482  // NOTE: we invert the less-than operator to sort high priorities to front
483  bool operator<(const AvatarPriority& other) const { return priority < other.priority; }
484 };
485 
486 class ClientTraitsHandler;
487 
488 class AvatarData : public QObject, public SpatiallyNestable {
489  Q_OBJECT
490 
491  // IMPORTANT: The JSDoc for the following properties should be copied to MyAvatar.h and ScriptableAvatar.h.
492  /*
493  * @property {Vec3} position - The position of the avatar.
494  * @property {number} scale=1.0 - The scale of the avatar. The value can be set to anything between <code>0.005</code> and
495  * <code>1000.0</code>. When the scale value is fetched, it may temporarily be further limited by the domain's settings.
496  * @property {number} density - The density of the avatar in kg/m<sup>3</sup>. The density is used to work out its mass in
497  * the application of physics. <em>Read-only.</em>
498  * @property {Vec3} handPosition - A user-defined hand position, in world coordinates. The position moves with the avatar
499  * but is otherwise not used or changed by Interface.
500  * @property {number} bodyYaw - The left or right rotation about an axis running from the head to the feet of the avatar.
501  * Yaw is sometimes called "heading".
502  * @property {number} bodyPitch - The rotation about an axis running from shoulder to shoulder of the avatar. Pitch is
503  * sometimes called "elevation".
504  * @property {number} bodyRoll - The rotation about an axis running from the chest to the back of the avatar. Roll is
505  * sometimes called "bank".
506  * @property {Quat} orientation - The orientation of the avatar.
507  * @property {Quat} headOrientation - The orientation of the avatar's head.
508  * @property {number} headPitch - The rotation about an axis running from ear to ear of the avatar's head. Pitch is
509  * sometimes called "elevation".
510  * @property {number} headYaw - The rotation left or right about an axis running from the base to the crown of the avatar's
511  * head. Yaw is sometimes called "heading".
512  * @property {number} headRoll - The rotation about an axis running from the nose to the back of the avatar's head. Roll is
513  * sometimes called "bank".
514  * @property {Vec3} velocity - The current velocity of the avatar.
515  * @property {Vec3} angularVelocity - The current angular velocity of the avatar.
516  * @property {number} audioLoudness - The instantaneous loudness of the audio input that the avatar is injecting into the
517  * domain.
518  * @property {number} audioAverageLoudness - The rolling average loudness of the audio input that the avatar is injecting
519  * into the domain.
520  * @property {string} displayName - The avatar's display name.
521  * @property {string} sessionDisplayName - <code>displayName's</code> sanitized and default version defined by the avatar
522  * mixer rather than Interface clients. The result is unique among all avatars present in the domain at the time.
523  * @property {boolean} lookAtSnappingEnabled=true - <code>true</code> if the avatar's eyes snap to look at another avatar's
524  * eyes when the other avatar is in the line of sight and also has <code>lookAtSnappingEnabled == true</code>.
525  * @property {string} skeletonModelURL - The avatar's FST file.
526  * @property {AttachmentData[]} attachmentData - Information on the avatar's attachments.
527  * <p class="important">Deprecated: This property is deprecated and will be removed. Use avatar entities instead.</p>
528  * @property {string[]} jointNames - The list of joints in the current avatar model. <em>Read-only.</em>
529  * @property {Uuid} sessionUUID - Unique ID of the avatar in the domain. <em>Read-only.</em>
530  * @property {Mat4} sensorToWorldMatrix - The scale, rotation, and translation transform from the user's real world to the
531  * avatar's size, orientation, and position in the virtual world. <em>Read-only.</em>
532  * @property {Mat4} controllerLeftHandMatrix - The rotation and translation of the left hand controller relative to the
533  * avatar. <em>Read-only.</em>
534  * @property {Mat4} controllerRightHandMatrix - The rotation and translation of the right hand controller relative to the
535  * avatar. <em>Read-only.</em>
536  * @property {number} sensorToWorldScale - The scale that transforms dimensions in the user's real world to the avatar's
537  * size in the virtual world. <em>Read-only.</em>
538  * @property {boolean} hasPriority - <code>true</code> if the avatar is in a "hero" zone, <code>false</code> if it isn't.
539  * <em>Read-only.</em>
540  * @property {boolean} hasScriptedBlendshapes=false - <code>true</code> if blend shapes are controlled by scripted actions,
541  * otherwise <code>false</code>. Set this to <code>true</code> before using the {@link Avatar.setBlendshape} method,
542  * and set back to <code>false</code> after you no longer want scripted control over the blend shapes.
543  * <p><strong>Note:</strong> This property will automatically be set to <code>true</code> if the controller system has
544  * valid facial blend shape actions.</p>
545  * @property {boolean} hasProceduralBlinkFaceMovement=true - <code>true</code> if avatars blink automatically by animating
546  * facial blend shapes, <code>false</code> if automatic blinking is disabled. Set to <code>false</code> to fully control
547  * the blink facial blend shapes via the {@link Avatar.setBlendshape} method.
548  * @property {boolean} hasProceduralEyeFaceMovement=true - <code>true</code> if the facial blend shapes for an avatar's eyes
549  * adjust automatically as the eyes move, <code>false</code> if this automatic movement is disabled. Set this property
550  * to <code>true</code> to prevent the iris from being obscured by the upper or lower lids. Set to <code>false</code> to
551  * fully control the eye blend shapes via the {@link Avatar.setBlendshape} method.
552  * @property {boolean} hasAudioEnabledFaceMovement=true - <code>true</code> if the avatar's mouth blend shapes animate
553  * automatically based on detected microphone input, <code>false</code> if this automatic movement is disabled. Set
554  * this property to <code>false</code> to fully control the mouth facial blend shapes via the
555  * {@link Avatar.setBlendshape} method.
556  */
557  Q_PROPERTY(glm::vec3 position READ getWorldPosition WRITE setPositionViaScript)
558  Q_PROPERTY(float scale READ getDomainLimitedScale WRITE setTargetScale)
559  Q_PROPERTY(float density READ getDensity)
560  Q_PROPERTY(glm::vec3 handPosition READ getHandPosition WRITE setHandPosition)
561  Q_PROPERTY(float bodyYaw READ getBodyYaw WRITE setBodyYaw)
562  Q_PROPERTY(float bodyPitch READ getBodyPitch WRITE setBodyPitch)
563  Q_PROPERTY(float bodyRoll READ getBodyRoll WRITE setBodyRoll)
564 
565  Q_PROPERTY(glm::quat orientation READ getWorldOrientation WRITE setOrientationViaScript)
566  Q_PROPERTY(glm::quat headOrientation READ getHeadOrientation WRITE setHeadOrientation)
567  Q_PROPERTY(float headPitch READ getHeadPitch WRITE setHeadPitch)
568  Q_PROPERTY(float headYaw READ getHeadYaw WRITE setHeadYaw)
569  Q_PROPERTY(float headRoll READ getHeadRoll WRITE setHeadRoll)
570 
571  Q_PROPERTY(glm::vec3 velocity READ getWorldVelocity WRITE setWorldVelocity)
572  Q_PROPERTY(glm::vec3 angularVelocity READ getWorldAngularVelocity WRITE setWorldAngularVelocity)
573 
574  Q_PROPERTY(float audioLoudness READ getAudioLoudness WRITE setAudioLoudness)
575  Q_PROPERTY(float audioAverageLoudness READ getAudioAverageLoudness WRITE setAudioAverageLoudness)
576 
577  Q_PROPERTY(QString displayName READ getDisplayName WRITE setDisplayName NOTIFY displayNameChanged)
578  // sessionDisplayName is sanitized, defaulted version displayName that is defined by the AvatarMixer rather than by Interface clients.
579  // The result is unique among all avatars present at the time.
580  Q_PROPERTY(QString sessionDisplayName READ getSessionDisplayName WRITE setSessionDisplayName NOTIFY sessionDisplayNameChanged)
581  Q_PROPERTY(bool lookAtSnappingEnabled MEMBER _lookAtSnappingEnabled NOTIFY lookAtSnappingChanged)
582  Q_PROPERTY(QString skeletonModelURL READ getSkeletonModelURLFromScript WRITE setSkeletonModelURLFromScript NOTIFY skeletonModelURLChanged)
583  Q_PROPERTY(QVector<AttachmentData> attachmentData READ getAttachmentData WRITE setAttachmentData)
584 
585  Q_PROPERTY(QStringList jointNames READ getJointNames)
586 
587  Q_PROPERTY(QUuid sessionUUID READ getSessionUUID NOTIFY sessionUUIDChanged)
588 
589  Q_PROPERTY(glm::mat4 sensorToWorldMatrix READ getSensorToWorldMatrix)
590  Q_PROPERTY(glm::mat4 controllerLeftHandMatrix READ getControllerLeftHandMatrix)
591  Q_PROPERTY(glm::mat4 controllerRightHandMatrix READ getControllerRightHandMatrix)
592 
593  Q_PROPERTY(float sensorToWorldScale READ getSensorToWorldScale)
594 
595  Q_PROPERTY(bool hasPriority READ getHasPriority)
596 
597  Q_PROPERTY(bool hasScriptedBlendshapes READ getHasScriptedBlendshapes WRITE setHasScriptedBlendshapes)
598  Q_PROPERTY(bool hasProceduralBlinkFaceMovement READ getHasProceduralBlinkFaceMovement WRITE setHasProceduralBlinkFaceMovement)
599  Q_PROPERTY(bool hasProceduralEyeFaceMovement READ getHasProceduralEyeFaceMovement WRITE setHasProceduralEyeFaceMovement)
600  Q_PROPERTY(bool hasAudioEnabledFaceMovement READ getHasAudioEnabledFaceMovement WRITE setHasAudioEnabledFaceMovement)
601 
602 public:
603  virtual QString getName() const override { return QString("Avatar:") + _displayName; }
604 
605  static const QString FRAME_NAME;
606 
607  static void fromFrame(const QByteArray& frameData, AvatarData& avatar, bool useFrameSkeleton = true);
608  static QByteArray toFrame(const AvatarData& avatar);
609 
610  AvatarData();
611  virtual ~AvatarData();
612 
613  virtual bool isMyAvatarURLProtected() const { return false; } // This needs to be here because both MyAvatar and AvatarData inherit from MyAvatar
614 
615  static const QUrl& defaultFullAvatarModelUrl();
616 
617  const QUuid getSessionUUID() const { return getID(); }
618 
619  glm::vec3 getHandPosition() const;
620  void setHandPosition(const glm::vec3& handPosition);
621 
622  typedef enum {
623  NoData,
624  PALMinimum,
625  MinimumData,
626  CullSmallData,
627  IncludeSmallData,
628  SendAllData
629  } AvatarDataDetail;
630 
631  virtual QByteArray toByteArrayStateful(AvatarDataDetail dataDetail, bool dropFaceTracking = false);
632 
633  virtual QByteArray toByteArray(AvatarDataDetail dataDetail, quint64 lastSentTime, const QVector<JointData>& lastSentJointData,
634  AvatarDataPacket::SendStatus& sendStatus, bool dropFaceTracking, bool distanceAdjust, glm::vec3 viewerPosition,
635  QVector<JointData>* sentJointDataOut, int maxDataSize = 0, AvatarDataRate* outboundDataRateOut = nullptr) const;
636 
637  virtual void doneEncoding(bool cullSmallChanges);
638 
640  bool shouldLogError(const quint64& now);
641 
645  virtual int parseDataFromBuffer(const QByteArray& buffer);
646 
647  virtual void setCollisionWithOtherAvatarsFlags() {};
648 
649  // Body Rotation (degrees)
650  float getBodyYaw() const;
651  void setBodyYaw(float bodyYaw);
652  float getBodyPitch() const;
653  void setBodyPitch(float bodyPitch);
654  float getBodyRoll() const;
655  void setBodyRoll(float bodyRoll);
656 
657  virtual void setPositionViaScript(const glm::vec3& position);
658  virtual void setOrientationViaScript(const glm::quat& orientation);
659 
660  virtual void updateAttitude(const glm::quat& orientation) {}
661 
662  glm::quat getHeadOrientation() const {
663  lazyInitHeadData();
664  return _headData->getOrientation();
665  }
666  void setHeadOrientation(const glm::quat& orientation) {
667  if (_headData) {
668  _headData->setOrientation(orientation);
669  }
670  }
671 
672  void setLookAtPosition(const glm::vec3& lookAtPosition) {
673  if (_headData) {
674  _headData->setLookAtPosition(lookAtPosition);
675  }
676  }
677 
678  void setBlendshapeCoefficients(const QVector<float>& blendshapeCoefficients) {
679  if (_headData) {
680  _headData->setBlendshapeCoefficients(blendshapeCoefficients);
681  }
682  }
683 
684  // access to Head().set/getMousePitch (degrees)
685  float getHeadPitch() const { return _headData->getBasePitch(); }
686  void setHeadPitch(float value) { _headData->setBasePitch(value); }
687 
688  float getHeadYaw() const { return _headData->getBaseYaw(); }
689  void setHeadYaw(float value) { _headData->setBaseYaw(value); }
690 
691  float getHeadRoll() const { return _headData->getBaseRoll(); }
692  void setHeadRoll(float value) { _headData->setBaseRoll(value); }
693 
694  // access to Head().set/getAverageLoudness
695 
696  float getAudioLoudness() const { return _audioLoudness; }
697  void setAudioLoudness(float audioLoudness) {
698  if (audioLoudness != _audioLoudness) {
699  _audioLoudnessChanged = usecTimestampNow();
700  }
701  _audioLoudness = audioLoudness;
702  }
703  bool audioLoudnessChangedSince(quint64 time) const { return _audioLoudnessChanged >= time; }
704 
705  float getAudioAverageLoudness() const { return _audioAverageLoudness; }
706  void setAudioAverageLoudness(float audioAverageLoudness) { _audioAverageLoudness = audioAverageLoudness; }
707 
708  // Scale
709  virtual void setTargetScale(float targetScale);
710 
711  float getDomainLimitedScale() const;
712 
713  void setHasScriptedBlendshapes(bool hasScriptedBlendshapes);
714  bool getHasScriptedBlendshapes() const;
715  void setHasProceduralBlinkFaceMovement(bool hasProceduralBlinkFaceMovement);
716  bool getHasProceduralBlinkFaceMovement() const;
717  void setHasProceduralEyeFaceMovement(bool hasProceduralEyeFaceMovement);
718  bool getHasProceduralEyeFaceMovement() const;
719  void setHasAudioEnabledFaceMovement(bool hasAudioEnabledFaceMovement);
720  bool getHasAudioEnabledFaceMovement() const;
721 
722  /*@jsdoc
723  * Gets the minimum scale allowed for this avatar in the current domain.
724  * This value can change as the user changes avatars or when changing domains.
725  * @function Avatar.getDomainMinScale
726  * @returns {number} The minimum scale allowed for this avatar in the current domain.
727  */
728  Q_INVOKABLE float getDomainMinScale() const;
729 
730  /*@jsdoc
731  * Gets the maximum scale allowed for this avatar in the current domain.
732  * This value can change as the user changes avatars or when changing domains.
733  * @function Avatar.getDomainMaxScale
734  * @returns {number} The maximum scale allowed for this avatar in the current domain.
735  */
736  Q_INVOKABLE float getDomainMaxScale() const;
737 
738  // Returns eye height of avatar in meters, ignoring avatar scale.
739  // if _targetScale is 1 then this will be identical to getEyeHeight;
740  virtual float getUnscaledEyeHeight() const { return DEFAULT_AVATAR_EYE_HEIGHT; }
741 
742  // returns true, if an acurate eye height estimage can be obtained by inspecting the avatar model skeleton and geometry,
743  // not all subclasses of AvatarData have access to this data.
744  virtual bool canMeasureEyeHeight() const { return false; }
745 
746  /*@jsdoc
747  * Gets the current eye height of the avatar.
748  * This height is only an estimate and might be incorrect for avatars that are missing standard joints.
749  * @function Avatar.getEyeHeight
750  * @returns {number} The eye height of the avatar.
751  */
752  Q_INVOKABLE virtual float getEyeHeight() const { return _targetScale * getUnscaledEyeHeight(); }
753 
754  /*@jsdoc
755  * Gets the current height of the avatar.
756  * This height is only an estimate and might be incorrect for avatars that are missing standard joints.
757  * @function Avatar.getHeight
758  * @returns {number} The height of the avatar.
759  */
760  Q_INVOKABLE virtual float getHeight() const;
761 
762  float getUnscaledHeight() const;
763 
764  void setDomainMinimumHeight(float domainMinimumHeight);
765  void setDomainMaximumHeight(float domainMaximumHeight);
766 
767  /*@jsdoc
768  * Sets the pointing state of the hands to control where the laser emanates from. If the right index finger is pointing, the
769  * laser emanates from the tip of that finger, otherwise it emanates from the palm.
770  * @function Avatar.setHandState
771  * @param {HandState} state - The pointing state of the hand.
772  */
773  Q_INVOKABLE void setHandState(char s) { _handState = s; }
774 
775  /*@jsdoc
776  * Gets the pointing state of the hands to control where the laser emanates from. If the right index finger is pointing, the
777  * laser emanates from the tip of that finger, otherwise it emanates from the palm.
778  * @function Avatar.getHandState
779  * @returns {HandState} The pointing state of the hand.
780  */
781  Q_INVOKABLE char getHandState() const { return _handState; }
782 
783  /*@jsdoc
784  * Sets a specific joint's rotation and position relative to its parent, in model coordinates.
785  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
786  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
787  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
788  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
789  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
790  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
791  * @function Avatar.setJointData
792  * @param {number} index - The index of the joint.
793  * @param {Quat} rotation - The rotation of the joint relative to its parent.
794  * @param {Vec3} translation - The translation of the joint relative to its parent, in model coordinates.
795  * @example <caption>Set your avatar to it's default T-pose for a while.<br />
796  * <img alt="Avatar in T-pose" src="https://apidocs.overte.org/examples/t-pose.png" /></caption>
797  * // Set all joint translations and rotations to defaults.
798  * var i, length, rotation, translation;
799  * for (i = 0, length = MyAvatar.getJointNames().length; i < length; i++) {
800  * rotation = MyAvatar.getDefaultJointRotation(i);
801  * translation = MyAvatar.getDefaultJointTranslation(i);
802  * MyAvatar.setJointData(i, rotation, translation);
803  * }
804  *
805  * // Restore your avatar's motion after 5s.
806  * Script.setTimeout(function () {
807  * MyAvatar.clearJointsData();
808  * }, 5000);
809  *
810  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
811  */
812  Q_INVOKABLE virtual void setJointData(int index, const glm::quat& rotation, const glm::vec3& translation);
813 
814  /*@jsdoc
815  * Sets a specific joint's rotation relative to its parent.
816  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
817  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
818  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
819  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
820  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
821  * @function Avatar.setJointRotation
822  * @param {number} index - The index of the joint.
823  * @param {Quat} rotation - The rotation of the joint relative to its parent.
824  */
825  Q_INVOKABLE virtual void setJointRotation(int index, const glm::quat& rotation);
826 
827  /*@jsdoc
828  * Sets a specific joint's translation relative to its parent, in model coordinates.
829  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
830  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
831  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
832  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
833  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
834  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
835  * @function Avatar.setJointTranslation
836  * @param {number} index - The index of the joint.
837  * @param {Vec3} translation - The translation of the joint relative to its parent, in model coordinates.
838  */
839  Q_INVOKABLE virtual void setJointTranslation(int index, const glm::vec3& translation);
840 
841  /*@jsdoc
842  * Clears joint translations and rotations set by script for a specific joint. This restores all motion from the default
843  * animation system including inverse kinematics for that joint.
844  * <p>Note: This is slightly faster than the function variation that specifies the joint name.</p>
845  * @function Avatar.clearJointData
846  * @param {number} index - The index of the joint.
847  */
848  Q_INVOKABLE virtual void clearJointData(int index);
849 
850  /*@jsdoc
851  * Checks that the data for a joint are valid.
852  * @function Avatar.isJointDataValid
853  * @param {number} index - The index of the joint.
854  * @returns {boolean} <code>true</code> if the joint data are valid, <code>false</code> if not.
855  */
856  Q_INVOKABLE bool isJointDataValid(int index) const;
857 
858  /*@jsdoc
859  * Gets the rotation of a joint relative to its parent. For information on the joint hierarchy used, see
860  * <a href="https://docs.overte.org/create/avatars/avatar-standards.html">Avatar Standards</a>.
861  * @function Avatar.getJointRotation
862  * @param {number} index - The index of the joint.
863  * @returns {Quat} The rotation of the joint relative to its parent.
864  */
865  Q_INVOKABLE virtual glm::quat getJointRotation(int index) const;
866 
867  /*@jsdoc
868  * Gets the translation of a joint relative to its parent, in model coordinates.
869  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
870  * <p>For information on the joint hierarchy used, see
871  * <a href="https://docs.overte.org/create/avatars/avatar-standards.html">Avatar Standards</a>.</p>
872  * @function Avatar.getJointTranslation
873  * @param {number} index - The index of the joint.
874  * @returns {Vec3} The translation of the joint relative to its parent, in model coordinates.
875  */
876  Q_INVOKABLE virtual glm::vec3 getJointTranslation(int index) const;
877 
878  /*@jsdoc
879  * Sets a specific joint's rotation and position relative to its parent, in model coordinates.
880  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
881  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
882  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
883  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
884  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
885  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
886  * @function Avatar.setJointData
887  * @param {string} name - The name of the joint.
888  * @param {Quat} rotation - The rotation of the joint relative to its parent.
889  * @param {Vec3} translation - The translation of the joint relative to its parent, in model coordinates.
890  */
891  Q_INVOKABLE virtual void setJointData(const QString& name, const glm::quat& rotation, const glm::vec3& translation);
892 
893  /*@jsdoc
894  * Sets a specific joint's rotation relative to its parent.
895  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
896  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
897  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
898  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
899  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
900  * @function Avatar.setJointRotation
901  * @param {string} name - The name of the joint.
902  * @param {Quat} rotation - The rotation of the joint relative to its parent.
903  * @example <caption>Set your avatar to its default T-pose then rotate its right arm.<br />
904  * <img alt="Avatar in T-pose with arm rotated" src="https://apidocs.overte.org/examples/armpose.png" /></caption>
905  * // Set all joint translations and rotations to defaults.
906  * var i, length, rotation, translation;
907  * for (i = 0, length = MyAvatar.getJointNames().length; i < length; i++) {
908  * rotation = MyAvatar.getDefaultJointRotation(i);
909  * translation = MyAvatar.getDefaultJointTranslation(i);
910  * MyAvatar.setJointData(i, rotation, translation);
911  * }
912  *
913  * // Rotate the right arm.
914  * var newArmRotation = { x: 0.47, y: 0.22, z: -0.02, w: 0.87 };
915  * MyAvatar.setJointRotation("RightArm", newArmRotation);
916  *
917  * // Restore your avatar's motion after 5s.
918  * Script.setTimeout(function () {
919  * MyAvatar.clearJointsData();
920  * }, 5000);
921  *
922  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
923  */
924  Q_INVOKABLE virtual void setJointRotation(const QString& name, const glm::quat& rotation);
925 
926  /*@jsdoc
927  * Sets a specific joint's translation relative to its parent, in model coordinates.
928  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
929  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
930  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
931  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
932  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
933  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
934  * @function Avatar.setJointTranslation
935  * @param {string} name - The name of the joint.
936  * @param {Vec3} translation - The translation of the joint relative to its parent, in model coordinates.
937  * @example <caption>Stretch your avatar's neck. Depending on the avatar you are using, you will either see a gap between
938  * the head and body or you will see the neck stretched.<br />
939  * <img alt="Avatar with neck stretched" src="https://apidocs.overte.org/examples/stretched-neck.png" /></caption>
940  * // Stretch your avatar's neck.
941  * MyAvatar.setJointTranslation("Neck", Vec3.multiply(2, MyAvatar.getJointTranslation("Neck")));
942  *
943  * // Restore your avatar's neck after 5s.
944  * Script.setTimeout(function () {
945  * MyAvatar.clearJointData("Neck");
946  * }, 5000);
947  *
948  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
949  */
950  Q_INVOKABLE virtual void setJointTranslation(const QString& name, const glm::vec3& translation);
951 
952  /*@jsdoc
953  * Clears joint translations and rotations set by script for a specific joint. This restores all motion from the default
954  * animation system including inverse kinematics for that joint.
955  * <p>Note: This is slightly slower than the function variation that specifies the joint index.</p>
956  * @function Avatar.clearJointData
957  * @param {string} name - The name of the joint.
958  * @example <caption>Offset and restore the position of your avatar's head.</caption>
959  * // Stretch your avatar's neck.
960  * MyAvatar.setJointTranslation("Neck", Vec3.multiply(2, MyAvatar.getJointTranslation("Neck")));
961  *
962  * // Restore your avatar's neck after 5s.
963  * Script.setTimeout(function () {
964  * MyAvatar.clearJointData("Neck");
965  * }, 5000);
966  *
967  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
968  */
969  Q_INVOKABLE virtual void clearJointData(const QString& name);
970 
971  /*@jsdoc
972  * Checks if the data for a joint are valid.
973  * @function Avatar.isJointDataValid
974  * @param {string} name - The name of the joint.
975  * @returns {boolean} <code>true</code> if the joint data are valid, <code>false</code> if not.
976  */
977  Q_INVOKABLE virtual bool isJointDataValid(const QString& name) const;
978 
979  /*@jsdoc
980  * Gets the rotation of a joint relative to its parent. For information on the joint hierarchy used, see
981  * <a href="https://docs.overte.org/create/avatars/avatar-standards.html">Avatar Standards</a>.
982  * @function Avatar.getJointRotation
983  * @param {string} name - The name of the joint.
984  * @returns {Quat} The rotation of the joint relative to its parent.
985  * @example <caption>Report the rotation of your avatar's hips joint.</caption>
986  * print(JSON.stringify(MyAvatar.getJointRotation("Hips")));
987  *
988  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
989  */
990  Q_INVOKABLE virtual glm::quat getJointRotation(const QString& name) const;
991 
992  /*@jsdoc
993  * Gets the translation of a joint relative to its parent, in model coordinates.
994  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
995  * <p>For information on the joint hierarchy used, see
996  * <a href="https://docs.overte.org/create/avatars/avatar-standards.html">Avatar Standards</a>.</p>
997  * @function Avatar.getJointTranslation
998  * @param {number} name - The name of the joint.
999  * @returns {Vec3} The translation of the joint relative to its parent, in model coordinates.
1000  * @example <caption>Report the translation of your avatar's hips joint.</caption>
1001  * print(JSON.stringify(MyAvatar.getJointRotation("Hips")));
1002  *
1003  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1004  */
1005  Q_INVOKABLE virtual glm::vec3 getJointTranslation(const QString& name) const;
1006 
1007  /*@jsdoc
1008  * Gets the rotations of all joints in the current avatar. Each joint's rotation is relative to its parent joint.
1009  * @function Avatar.getJointRotations
1010  * @returns {Quat[]} The rotations of all joints relative to each's parent. The values are in the same order as the array
1011  * returned by {@link MyAvatar.getJointNames}, or {@link Avatar.getJointNames} if using the <code>Avatar</code> API.
1012  * @example <caption>Report the rotations of all your avatar's joints.</caption>
1013  * print(JSON.stringify(MyAvatar.getJointRotations()));
1014  *
1015  * // Note: If using from the Avatar API, replace all "MyAvatar" with "Avatar".
1016  */
1017  Q_INVOKABLE virtual QVector<glm::quat> getJointRotations() const;
1018 
1019  /*@jsdoc
1020  * Gets the translations of all joints in the current avatar. Each joint's translation is relative to its parent joint, in
1021  * model coordinates.
1022  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
1023  * @function Avatar.getJointTranslations
1024  * @returns {Vec3[]} The translations of all joints relative to each's parent, in model coordinates. The values are in the
1025  * same order as the array returned by {@link MyAvatar.getJointNames}, or {@link Avatar.getJointNames} if using the
1026  * <code>Avatar</code> API.
1027  */
1028  Q_INVOKABLE virtual QVector<glm::vec3> getJointTranslations() const;
1029 
1030  /*@jsdoc
1031  * Sets the rotations of all joints in the current avatar. Each joint's rotation is relative to its parent joint.
1032  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
1033  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
1034  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
1035  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
1036  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
1037  * @function Avatar.setJointRotations
1038  * @param {Quat[]} jointRotations - The rotations for all joints in the avatar. The values are in the same order as the
1039  * array returned by {@link MyAvatar.getJointNames}, or {@link Avatar.getJointNames} if using the <code>Avatar</code> API.
1040  * @example <caption>Set your avatar to its default T-pose then rotate its right arm.<br />
1041  * <img alt="Avatar in T-pose" src="https://apidocs.overte.org/examples/armpose.png" /></caption>
1042  * // Set all joint translations and rotations to defaults.
1043  * var i, length, rotation, translation;
1044  * for (i = 0, length = MyAvatar.getJointNames().length; i < length; i++) {
1045  * rotation = MyAvatar.getDefaultJointRotation(i);
1046  * translation = MyAvatar.getDefaultJointTranslation(i);
1047  * MyAvatar.setJointData(i, rotation, translation);
1048  * }
1049  *
1050  * // Get all join rotations.
1051  * var jointRotations = MyAvatar.getJointRotations();
1052  *
1053  * // Update the rotation of the right arm in the array.
1054  * jointRotations[MyAvatar.getJointIndex("RightArm")] = { x: 0.47, y: 0.22, z: -0.02, w: 0.87 };
1055  *
1056  * // Update all joint rotations.
1057  * MyAvatar.setJointRotations(jointRotations);
1058  *
1059  * // Restore your avatar's motion after 5s.
1060  * Script.setTimeout(function () {
1061  * MyAvatar.clearJointsData();
1062  * }, 5000);
1063  *
1064  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
1065  */
1066  Q_INVOKABLE virtual void setJointRotations(const QVector<glm::quat>& jointRotations);
1067 
1068  /*@jsdoc
1069  * Sets the translations of all joints in the current avatar. Each joint's translation is relative to its parent joint, in
1070  * model coordinates.
1071  * <p><strong>Warning:</strong> These coordinates are not necessarily in meters.</p>
1072  * <p>Setting joint data completely overrides/replaces all motion from the default animation system including inverse
1073  * kinematics, but just for the specified joint. So for example, if you were to procedurally manipulate the finger joints,
1074  * the avatar's hand and head would still do inverse kinematics properly. However, as soon as you start to manipulate
1075  * joints in the inverse kinematics chain, the inverse kinematics might not function as you expect. For example, if you set
1076  * the rotation of the elbow, the hand inverse kinematics position won't end up in the right place.</p>
1077  * @function Avatar.setJointTranslations
1078  * @param {Vec3[]} translations - The translations for all joints in the avatar, in model coordinates. The values are in
1079  * the same order as the array returned by {@link MyAvatar.getJointNames}, or {@link Avatar.getJointNames} if using the
1080  * <code>Avatar</code> API.
1081  */
1082  Q_INVOKABLE virtual void setJointTranslations(const QVector<glm::vec3>& jointTranslations);
1083 
1084  /*@jsdoc
1085  * Clears all joint translations and rotations that have been set by script. This restores all motion from the default
1086  * animation system including inverse kinematics for all joints.
1087  * @function Avatar.clearJointsData
1088  * @example <caption>Set your avatar to it's default T-pose for a while.</caption>
1089  * // Set all joint translations and rotations to defaults.
1090  * var i, length, rotation, translation;
1091  * for (i = 0, length = MyAvatar.getJointNames().length; i < length; i++) {
1092  * rotation = MyAvatar.getDefaultJointRotation(i);
1093  * translation = MyAvatar.getDefaultJointTranslation(i);
1094  * MyAvatar.setJointData(i, rotation, translation);
1095  * }
1096  *
1097  * // Restore your avatar's motion after 5s.
1098  * Script.setTimeout(function () {
1099  * MyAvatar.clearJointsData();
1100  * }, 5000);
1101  *
1102  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
1103  */
1104  Q_INVOKABLE virtual void clearJointsData();
1105 
1106  /*@jsdoc
1107  * Gets the joint index for a named joint. The joint index value is the position of the joint in the array returned by
1108  * {@link MyAvatar.getJointNames}, or {@link Avatar.getJointNames} if using the <code>Avatar</code> API.
1109  * @function Avatar.getJointIndex
1110  * @param {string} name - The name of the joint.
1111  * @returns {number} The index of the joint if valid, otherwise <code>-1</code>.
1112  * @example <caption>Report the index of your avatar's left arm joint.</caption>
1113  * print(JSON.stringify(MyAvatar.getJointIndex("LeftArm")));
1114  *
1115  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1116  */
1118  Q_INVOKABLE virtual int getJointIndex(const QString& name) const;
1119 
1120  /*@jsdoc
1121  * Gets the names of all the joints in the current avatar.
1122  * @function Avatar.getJointNames
1123  * @returns {string[]} The joint names.
1124  * @example <caption>Report the names of all the joints in your current avatar.</caption>
1125  * print(JSON.stringify(MyAvatar.getJointNames()));
1126  *
1127  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1128  */
1129  Q_INVOKABLE virtual QStringList getJointNames() const;
1130 
1131 
1132  /*@jsdoc
1133  * Sets the value of a blend shape to animate your avatar's face. In order for other users to see the resulting animations
1134  * on your avatar's face, set <code>hasScriptedBlendshapes</code> to <code>true</code>. When you are done using this API,
1135  * set <code>hasScriptedBlendshapes</code> back to <code>false</code> when the animation is complete.
1136  * @function Avatar.setBlendshape
1137  * @param {string} name - The name of the blendshape, per the
1138  * {@link https://docs.overte.org/create/avatars/avatar-standards.html#blendshapes Avatar Standards}.
1139  * @param {number} value - A value between <code>0.0</code> and <code>1.0</code>.
1140  * @example <caption>Open your avatar's mouth wide.</caption>
1141  * MyAvatar.hasScriptedBlendshapes = true;
1142  * MyAvatar.setBlendshape("JawOpen", 1.0);
1143  *
1144  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1145  */
1146  Q_INVOKABLE void setBlendshape(QString name, float val) { _headData->setBlendshape(name, val); }
1147 
1148 
1149  /*@jsdoc
1150  * Gets information about the models currently attached to your avatar.
1151  * @function Avatar.getAttachmentsVariant
1152  * @returns {AttachmentData[]} Information about all models attached to your avatar.
1153  * @deprecated This function is deprecated and will be removed. Use avatar entities instead.
1154  */
1155  // FIXME: Can this name be improved? Can it be deprecated?
1156  Q_INVOKABLE virtual QVariantList getAttachmentsVariant() const;
1157 
1158  /*@jsdoc
1159  * Sets all models currently attached to your avatar. For example, if you retrieve attachment data using
1160  * {@link MyAvatar.getAttachmentsVariant} or {@link Avatar.getAttachmentsVariant}, make changes to it, and then want to
1161  * update your avatar's attachments per the changed data.
1162  * @function Avatar.setAttachmentsVariant
1163  * @param {AttachmentData[]} variant - The attachment data defining the models to have attached to your avatar.
1164  * @deprecated This function is deprecated and will be removed. Use avatar entities instead.
1165  */
1166  // FIXME: Can this name be improved? Can it be deprecated?
1167  Q_INVOKABLE virtual void setAttachmentsVariant(const QVariantList& variant);
1168 
1169  virtual void storeAvatarEntityDataPayload(const QUuid& entityID, const QByteArray& payload);
1170 
1171  /*@jsdoc
1172  * @function Avatar.updateAvatarEntity
1173  * @param {Uuid} entityID - The entity ID.
1174  * @param {ArrayBuffer} entityData - Entity data.
1175  * @deprecated This function is deprecated and will be removed.
1176  */
1177  Q_INVOKABLE virtual void updateAvatarEntity(const QUuid& entityID, const QByteArray& entityData);
1178 
1179  /*@jsdoc
1180  * @function Avatar.clearAvatarEntity
1181  * @param {Uuid} entityID - The entity ID.
1182  * @param {boolean} [requiresRemovalFromTree=true] - unused
1183  * @deprecated This function is deprecated and will be removed.
1184  */
1185  Q_INVOKABLE virtual void clearAvatarEntity(const QUuid& entityID, bool requiresRemovalFromTree = true);
1186 
1187  // FIXME: Rename to clearAvatarEntity() once the API call is removed.
1188  virtual void clearAvatarEntityInternal(const QUuid& entityID);
1189 
1190  void clearAvatarEntities();
1191 
1192  QList<QUuid> getAvatarEntityIDs() const;
1193 
1194  /*@jsdoc
1195  * Enables blend shapes set using {@link Avatar.setBlendshape} or {@link MyAvatar.setBlendshape} to be transmitted to other
1196  * users so that they can see the animation of your avatar's face.
1197  * <p class="important">Deprecated: This method is deprecated and will be removed. Use the
1198  * <code>Avatar.hasScriptedBlendshapes</code> or <code>MyAvatar.hasScriptedBlendshapes</code> property instead.</p>
1199  * @function Avatar.setForceFaceTrackerConnected
1200  * @param {boolean} connected - <code>true</code> to enable blend shape changes to be transmitted to other users,
1201  * <code>false</code> to disable.
1202  */
1203  Q_INVOKABLE void setForceFaceTrackerConnected(bool connected) { setHasScriptedBlendshapes(connected); }
1204 
1205  // key state
1206  void setKeyState(KeyState s) { _keyState = s; }
1207  KeyState keyState() const { return _keyState; }
1208 
1209  const HeadData* getHeadData() const { return _headData; }
1210 
1211  struct Identity {
1212  QVector<AttachmentData> attachmentData;
1213  QString displayName;
1214  QString sessionDisplayName;
1215  bool isReplicated;
1216  bool lookAtSnappingEnabled;
1217  AvatarDataPacket::IdentityFlags identityFlags;
1218  };
1219 
1220  // identityChanged returns true if identity has changed, false otherwise.
1221  // identityChanged returns true if identity has changed, false otherwise. Similarly for displayNameChanged and skeletonModelUrlChange.
1222  void processAvatarIdentity(QDataStream& packetStream, bool& identityChanged, bool& displayNameChanged);
1223 
1224  QByteArray packTrait(AvatarTraits::TraitType traitType) const;
1225  QByteArray packTraitInstance(AvatarTraits::TraitType traitType, AvatarTraits::TraitInstanceID instanceID);
1226 
1227  void processTrait(AvatarTraits::TraitType traitType, QByteArray traitBinaryData);
1228  void processTraitInstance(AvatarTraits::TraitType traitType,
1229  AvatarTraits::TraitInstanceID instanceID, QByteArray traitBinaryData);
1230  void processDeletedTraitInstance(AvatarTraits::TraitType traitType, AvatarTraits::TraitInstanceID instanceID);
1231 
1232  void prepareResetTraitInstances();
1233 
1234  QByteArray identityByteArray(bool setIsReplicated = false) const;
1235 
1236  QUrl getWireSafeSkeletonModelURL() const;
1237  virtual const QUrl& getSkeletonModelURL() const;
1238 
1239  const QString& getDisplayName() const { return _displayName; }
1240  const QString& getSessionDisplayName() const { return _sessionDisplayName; }
1241  bool getLookAtSnappingEnabled() const { return _lookAtSnappingEnabled; }
1242 
1243  /*@jsdoc
1244  * Sets the avatar's skeleton model.
1245  * @function Avatar.setSkeletonModelURL
1246  * @param {string} url - The avatar's FST file.
1247  */
1248  Q_INVOKABLE virtual void setSkeletonModelURL(const QUrl& skeletonModelURL);
1249 
1250  virtual void setDisplayName(const QString& displayName);
1251  virtual void setSessionDisplayName(const QString& sessionDisplayName) {
1252  _sessionDisplayName = sessionDisplayName;
1253  markIdentityDataChanged();
1254  }
1255  virtual bool isCertifyFailed() const { return _verificationFailed; }
1256 
1257  /*@jsdoc
1258  * Gets information about the models currently attached to your avatar.
1259  * @function Avatar.getAttachmentData
1260  * @returns {AttachmentData[]} Information about all models attached to your avatar.
1261  * @deprecated This function is deprecated and will be removed. Use avatar entities instead.
1262  * @example <caption>Report the URLs of all current attachments.</caption>
1263  * var attachments = MyAvatar.getaAttachmentData();
1264  * for (var i = 0; i < attachments.length; i++) {
1265  * print(attachments[i].modelURL);
1266  * }
1267  *
1268  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1269  */
1270  Q_INVOKABLE virtual QVector<AttachmentData> getAttachmentData() const;
1271 
1272  /*@jsdoc
1273  * Sets all models currently attached to your avatar. For example, if you retrieve attachment data using
1274  * {@link MyAvatar.getAttachmentData} or {@link Avatar.getAttachmentData}, make changes to it, and then want to update your avatar's attachments per the
1275  * changed data. You can also remove all attachments by using setting <code>attachmentData</code> to <code>null</code>.
1276  * @function Avatar.setAttachmentData
1277  * @param {AttachmentData[]} attachmentData - The attachment data defining the models to have attached to your avatar. Use
1278  * <code>null</code> to remove all attachments.
1279  * @deprecated This function is deprecated and will be removed. Use avatar entities instead.
1280  * @example <caption>Remove a hat attachment if your avatar is wearing it.</caption>
1281  * var hatURL = "https://apidocs.overte.org/examples/cowboy-hat.fbx";
1282  * var attachments = MyAvatar.getAttachmentData();
1283  *
1284  * for (var i = 0; i < attachments.length; i++) {
1285  * if (attachments[i].modelURL === hatURL) {
1286  * attachments.splice(i, 1);
1287  * MyAvatar.setAttachmentData(attachments);
1288  * break;
1289  * }
1290  * }
1291  *
1292  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
1293  */
1294  Q_INVOKABLE virtual void setAttachmentData(const QVector<AttachmentData>& attachmentData);
1295 
1296  /*@jsdoc
1297  * Attaches a model to your avatar. For example, you can give your avatar a hat to wear, a guitar to hold, or a surfboard to
1298  * stand on.
1299  * @function Avatar.attach
1300  * @param {string} modelURL - The URL of the glTF, FBX, or OBJ model to attach. glTF models may be in JSON or binary format
1301  * (".gltf" or ".glb" URLs respectively).
1302  * @param {string} [jointName=""] - The name of the avatar joint (see {@link MyAvatar.getJointNames} or
1303  * {@link Avatar.getJointNames}) to attach the model to.
1304  * @param {Vec3} [translation=Vec3.ZERO] - The offset to apply to the model relative to the joint position.
1305  * @param {Quat} [rotation=Quat.IDENTITY] - The rotation to apply to the model relative to the joint orientation.
1306  * @param {number} [scale=1.0] - The scale to apply to the model.
1307  * @param {boolean} [isSoft=false] - If the model has a skeleton, set this to <code>true</code> so that the bones of the
1308  * attached model's skeleton are rotated to fit the avatar's current pose. <code>isSoft</code> is used, for example,
1309  * to have clothing that moves with the avatar.
1310  * <p>If <code>true</code>, the <code>translation</code>, <code>rotation</code>, and <code>scale</code> parameters are
1311  * ignored.</p>
1312  * @param {boolean} [allowDuplicates=false] - If <code>true</code> then more than one copy of any particular model may be
1313  * attached to the same joint; if <code>false</code> then the same model cannot be attached to the same joint.
1314  * @param {boolean} [useSaved=true] - <em>Not used.</em>
1315  * @deprecated This function is deprecated and will be removed. Use avatar entities instead.
1316  * @example <caption>Attach a cowboy hat to your avatar's head.</caption>
1317  * var attachment = {
1318  * modelURL: "https://apidocs.overte.org/examples/cowboy-hat.fbx",
1319  * jointName: "Head",
1320  * translation: {"x": 0, "y": 0.25, "z": 0},
1321  * rotation: {"x": 0, "y": 0, "z": 0, "w": 1},
1322  * scale: 0.01,
1323  * isSoft: false
1324  * };
1325  *
1326  * MyAvatar.attach(attachment.modelURL,
1327  * attachment.jointName,
1328  * attachment.translation,
1329  * attachment.rotation,
1330  * attachment.scale,
1331  * attachment.isSoft);
1332  *
1333  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1334  */
1335  Q_INVOKABLE virtual void attach(const QString& modelURL, const QString& jointName = QString(),
1336  const glm::vec3& translation = glm::vec3(), const glm::quat& rotation = glm::quat(),
1337  float scale = 1.0f, bool isSoft = false,
1338  bool allowDuplicates = false, bool useSaved = true);
1339 
1340  /*@jsdoc
1341  * Detaches the most recently attached instance of a particular model from either a specific joint or any joint.
1342  * @function Avatar.detachOne
1343  * @param {string} modelURL - The URL of the model to detach.
1344  * @param {string} [jointName=""] - The name of the joint to detach the model from. If <code>""</code>, then the most
1345  * recently attached model is removed from which ever joint it was attached to.
1346  * @deprecated This function is deprecated and will be removed. Use avatar entities instead.
1347  */
1348  Q_INVOKABLE virtual void detachOne(const QString& modelURL, const QString& jointName = QString());
1349 
1350  /*@jsdoc
1351  * Detaches all instances of a particular model from either a specific joint or all joints.
1352  * @function Avatar.detachAll
1353  * @param {string} modelURL - The URL of the model to detach.
1354  * @param {string} [jointName=""] - The name of the joint to detach the model from. If <code>""</code>, then the model is
1355  * detached from all joints.
1356  * @deprecated This function is deprecated and will be removed. Use avatar entities instead.
1357  */
1358  Q_INVOKABLE virtual void detachAll(const QString& modelURL, const QString& jointName = QString());
1359 
1360  QString getSkeletonModelURLFromScript() const;
1361  void setSkeletonModelURLFromScript(const QString& skeletonModelString) { setSkeletonModelURL(QUrl(skeletonModelString)); }
1362 
1363  void setOwningAvatarMixer(const QWeakPointer<Node>& owningAvatarMixer) { _owningAvatarMixer = owningAvatarMixer; }
1364 
1365  int getAverageBytesReceivedPerSecond() const;
1366  int getReceiveRate() const;
1367 
1368  // An Avatar can be set Priority from the AvatarMixer side.
1369  bool getHasPriority() const { return _hasPriority; }
1370  // regular setHasPriority does a check of state changed and if true reset 'additionalFlagsChanged' timestamp
1371  void setHasPriority(bool hasPriority) {
1372  if (_hasPriority != hasPriority) {
1373  _additionalFlagsChanged = usecTimestampNow();
1374  _hasPriority = hasPriority;
1375  }
1376  }
1377  // In some cases, we want to assign the hasPRiority flag without reseting timestamp
1378  void setHasPriorityWithoutTimestampReset(bool hasPriority) { _hasPriority = hasPriority; }
1379 
1380  const glm::vec3& getTargetVelocity() const { return _targetVelocity; }
1381 
1382  void clearRecordingBasis();
1383  TransformPointer getRecordingBasis() const;
1384  void setRecordingBasis(TransformPointer recordingBasis = TransformPointer());
1385  void createRecordingIDs();
1386  virtual void avatarEntityDataToJson(QJsonObject& root) const;
1387  QJsonObject toJson() const;
1388  void fromJson(const QJsonObject& json, bool useFrameSkeleton = true);
1389 
1390  glm::vec3 getClientGlobalPosition() const { return _globalPosition; }
1391  AABox getGlobalBoundingBox() const { return AABox(_globalPosition + _globalBoundingBoxOffset - _globalBoundingBoxDimensions, _globalBoundingBoxDimensions); }
1392  AABox getDefaultBubbleBox() const;
1393 
1394  /*@jsdoc
1395  * @comment Documented in derived classes' JSDoc because implementations are different.
1396  */
1397  // Get avatar entity data with all property values. Used in API.
1398  Q_INVOKABLE virtual AvatarEntityMap getAvatarEntityData() const;
1399 
1400  // Get avatar entity data with non-default property values. Used internally.
1401  virtual AvatarEntityMap getAvatarEntityDataNonDefault() const;
1402 
1403  /*@jsdoc
1404  * @comment Documented in derived classes' JSDoc because implementations are different.
1405  */
1406  Q_INVOKABLE virtual void setAvatarEntityData(const AvatarEntityMap& avatarEntityData);
1407 
1408  void setAvatarEntityDataChanged(bool value) { _avatarEntityDataChanged = value; }
1409  AvatarEntityIDs getAndClearRecentlyRemovedIDs();
1410 
1411  /*@jsdoc
1412  * Gets the transform from the user's real world to the avatar's size, orientation, and position in the virtual world.
1413  * @function Avatar.getSensorToWorldMatrix
1414  * @returns {Mat4} The scale, rotation, and translation transform from the user's real world to the avatar's size,
1415  * orientation, and position in the virtual world.
1416  * @example <caption>Report the sensor to world matrix.</caption>
1417  * var sensorToWorldMatrix = MyAvatar.getSensorToWorldMatrix();
1418  * print("Sensor to woprld matrix: " + JSON.stringify(sensorToWorldMatrix));
1419  * print("Rotation: " + JSON.stringify(Mat4.extractRotation(sensorToWorldMatrix)));
1420  * print("Translation: " + JSON.stringify(Mat4.extractTranslation(sensorToWorldMatrix)));
1421  * print("Scale: " + JSON.stringify(Mat4.extractScale(sensorToWorldMatrix)));
1422  *
1423  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1424  */
1425  // thread safe
1426  Q_INVOKABLE glm::mat4 getSensorToWorldMatrix() const;
1427 
1428  /*@jsdoc
1429  * Gets the scale that transforms dimensions in the user's real world to the avatar's size in the virtual world.
1430  * @function Avatar.getSensorToWorldScale
1431  * @returns {number} The scale that transforms dimensions in the user's real world to the avatar's size in the virtual
1432  * world.
1433  */
1434  // thread safe
1435  Q_INVOKABLE float getSensorToWorldScale() const;
1436 
1437  /*@jsdoc
1438  * Gets the rotation and translation of the left hand controller relative to the avatar.
1439  * @function Avatar.getControllerLeftHandMatrix
1440  * @returns {Mat4} The rotation and translation of the left hand controller relative to the avatar.
1441  * @example <caption>Report the left hand controller matrix.</caption>
1442  * var leftHandMatrix = MyAvatar.getControllerLeftHandMatrix();
1443  * print("Controller left hand matrix: " + JSON.stringify(leftHandMatrix));
1444  * print("Rotation: " + JSON.stringify(Mat4.extractRotation(leftHandMatrix)));
1445  * print("Translation: " + JSON.stringify(Mat4.extractTranslation(leftHandMatrix)));
1446  * print("Scale: " + JSON.stringify(Mat4.extractScale(leftHandMatrix))); // Always 1,1,1.
1447  *
1448  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1449  */
1450  // thread safe
1451  Q_INVOKABLE glm::mat4 getControllerLeftHandMatrix() const;
1452 
1453  /*@jsdoc
1454  * Gets the rotation and translation of the right hand controller relative to the avatar.
1455  * @function Avatar.getControllerRightHandMatrix
1456  * @returns {Mat4} The rotation and translation of the right hand controller relative to the avatar.
1457  */
1458  // thread safe
1459  Q_INVOKABLE glm::mat4 getControllerRightHandMatrix() const;
1460 
1461 
1462  /*@jsdoc
1463  * Gets the amount of avatar mixer data being generated by the avatar.
1464  * @function Avatar.getDataRate
1465  * @param {AvatarDataRate} [rateName=""] - The type of avatar mixer data to get the data rate of.
1466  * @returns {number} The data rate in kbps.
1467  */
1468  Q_INVOKABLE float getDataRate(const QString& rateName = QString("")) const;
1469 
1470  /*@jsdoc
1471  * Gets the update rate of avatar mixer data being generated by the avatar.
1472  * @function Avatar.getUpdateRate
1473  * @param {AvatarUpdateRate} [rateName=""] - The type of avatar mixer data to get the update rate of.
1474  * @returns {number} The update rate in Hz.
1475  */
1476  Q_INVOKABLE float getUpdateRate(const QString& rateName = QString("")) const;
1477 
1478  int getJointCount() const { return _jointData.size(); }
1479 
1480  QVector<JointData> getLastSentJointData() {
1481  QReadLocker readLock(&_jointDataLock);
1482  _lastSentJointData.resize(_jointData.size());
1483  return _lastSentJointData;
1484  }
1485 
1486  // A method intended to be overriden by MyAvatar for polling orientation for network transmission.
1487  virtual glm::quat getOrientationOutbound() const;
1488 
1489  // TODO: remove this HACK once we settle on optimal sort coefficients
1490  // These coefficients exposed for fine tuning the sort priority for transfering new _jointData to the render pipeline.
1491  static float _avatarSortCoefficientSize;
1492  static float _avatarSortCoefficientCenter;
1493  static float _avatarSortCoefficientAge;
1494 
1495  bool getIdentityDataChanged() const { return _identityDataChanged; } // has the identity data changed since the last time sendIdentityPacket() was called
1496  void markIdentityDataChanged() { _identityDataChanged = true; }
1497 
1498  void pushIdentitySequenceNumber() { ++_identitySequenceNumber; };
1499  bool hasProcessedFirstIdentity() const { return _hasProcessedFirstIdentity; }
1500 
1501  float getDensity() const { return _density; }
1502 
1503  bool getIsReplicated() const { return _isReplicated; }
1504 
1505  void setReplicaIndex(int replicaIndex) { _replicaIndex = replicaIndex; }
1506  int getReplicaIndex() { return _replicaIndex; }
1507 
1508  static const float DEFAULT_BUBBLE_SCALE; /* = 2.4 */
1509  AABox computeBubbleBox(float bubbleScale = DEFAULT_BUBBLE_SCALE) const;
1510 
1511  void setIsNewAvatar(bool isNewAvatar) { _isNewAvatar = isNewAvatar; }
1512  bool getIsNewAvatar() { return _isNewAvatar; }
1513  void setIsClientAvatar(bool isClientAvatar) { _isClientAvatar = isClientAvatar; }
1514  void setSkeletonData(const std::vector<AvatarSkeletonTrait::UnpackedJointData>& skeletonData);
1515  std::vector<AvatarSkeletonTrait::UnpackedJointData> getSkeletonData() const;
1516  void sendSkeletonData() const;
1517  QVector<JointData> getJointData() const;
1518  glm::vec3 getHeadJointFrontVector() const;
1519 
1520 signals:
1521 
1522  /*@jsdoc
1523  * Triggered when the avatar's <code>displayName</code> property value changes.
1524  * @function Avatar.displayNameChanged
1525  * @returns {Signal}
1526  * @example <caption>Report when your avatar display name changes.</caption>
1527  * MyAvatar.displayNameChanged.connect(function () {
1528  * print("Avatar display name changed to: " + MyAvatar.displayName);
1529  * });
1530  *
1531  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1532  */
1533  void displayNameChanged();
1534 
1535  /*@jsdoc
1536  * Triggered when the avatar's <code>sessionDisplayName</code> property value changes.
1537  * @function Avatar.sessionDisplayNameChanged
1538  * @returns {Signal}
1539  * @example <caption>Report when your avatar's session display name changes.</caption>
1540  * MyAvatar.sessionDisplayNameChanged.connect(function () {
1541  * print("Avatar session display name changed to: " + MyAvatar.sessionDisplayName);
1542  * });
1543  *
1544  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1545  */
1546  void sessionDisplayNameChanged();
1547 
1548  /*@jsdoc
1549  * Triggered when the avatar's model (i.e., <code>skeletonModelURL</code> property value) changes.
1550  * @function Avatar.skeletonModelURLChanged
1551  * @returns {Signal}
1552  * @example <caption>Report when your avatar's skeleton model changes.</caption>
1553  * MyAvatar.skeletonModelURLChanged.connect(function () {
1554  * print("Skeleton model changed to: " + MyAvatar.skeletonModelURL);
1555  * });
1556  *
1557  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1558  */
1559  void skeletonModelURLChanged();
1560 
1561  /*@jsdoc
1562  * Triggered when the avatar's <code>lookAtSnappingEnabled</code> property value changes.
1563  * @function Avatar.lookAtSnappingChanged
1564  * @param {boolean} enabled - <code>true</code> if look-at snapping is enabled, <code>false</code> if not.
1565  * @returns {Signal}
1566  * @example <caption>Report when your look-at snapping setting changes.</caption>
1567  * MyAvatar.lookAtSnappingChanged.connect(function () {
1568  * print("Avatar look-at snapping changed to: " + MyAvatar.lookAtSnappingEnabled);
1569  * });
1570  *
1571  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1572  */
1573  void lookAtSnappingChanged(bool enabled);
1574 
1575  /*@jsdoc
1576  * Triggered when the avatar's <code>sessionUUID</code> property value changes.
1577  * @function Avatar.sessionUUIDChanged
1578  * @returns {Signal}
1579  * @example <caption>Report when your avatar's session UUID changes.</caption>
1580  * MyAvatar.sessionUUIDChanged.connect(function () {
1581  * print("Avatar session UUID changed to: " + MyAvatar.sessionUUID);
1582  * });
1583  *
1584  * // Note: If using from the Avatar API, replace "MyAvatar" with "Avatar".
1585  */
1586  void sessionUUIDChanged();
1587 
1588 public slots:
1589 
1590 /*@jsdoc
1591  * @function Avatar.sendAvatarDataPacket
1592  * @param {boolean} [sendAll=false] - Send all.
1593  * @returns {number}
1594  * @deprecated This function is deprecated and will be removed.
1595  */
1596  virtual int sendAvatarDataPacket(bool sendAll = false);
1597 
1598  /*@jsdoc
1599  * @function Avatar.sendIdentityPacket
1600  * @returns {number}
1601  * @deprecated This function is deprecated and will be removed.
1602  */
1603  int sendIdentityPacket();
1604 
1605  /*@jsdoc
1606  * @function Avatar.setSessionUUID
1607  * @param {Uuid} sessionUUID - Session UUID.
1608  * @deprecated This function is deprecated and will be removed.
1609  */
1610  virtual void setSessionUUID(const QUuid& sessionUUID) {
1611  if (sessionUUID != getID()) {
1612  if (sessionUUID == QUuid()) {
1613  setID(AVATAR_SELF_ID);
1614  } else {
1615  setID(sessionUUID);
1616  }
1617  emit sessionUUIDChanged();
1618  }
1619  }
1620 
1621 
1622  /*@jsdoc
1623  * Gets the rotation of a joint relative to the avatar.
1624  * <p><strong>Warning:</strong> Not able to be used in the <code>Avatar</code> API.</p>
1625  * @function Avatar.getAbsoluteJointRotationInObjectFrame
1626  * @param {number} index - The index of the joint. <em>Not used.</em>
1627  * @returns {Quat} <code>Quat.IDENTITY</code>.
1628  */
1629  virtual glm::quat getAbsoluteJointRotationInObjectFrame(int index) const override;
1630 
1631  /*@jsdoc
1632  * Gets the translation of a joint relative to the avatar.
1633  * <p><strong>Warning:</strong> Not able to be used in the <code>Avatar</code> API.</p>
1634  * @function Avatar.getAbsoluteJointTranslationInObjectFrame
1635  * @param {number} index - The index of the joint. <em>Not used.</em>
1636  * @returns {Vec3} <code>Vec3.ZERO</code>.
1637  */
1638  virtual glm::vec3 getAbsoluteJointTranslationInObjectFrame(int index) const override;
1639 
1640  /*@jsdoc
1641  * Sets the rotation of a joint relative to the avatar.
1642  * <p><strong>Warning:</strong> Not able to be used in the <code>Avatar</code> API.</p>
1643  * @function Avatar.setAbsoluteJointRotationInObjectFrame
1644  * @param {number} index - The index of the joint. <em>Not used.</em>
1645  * @param {Quat} rotation - The rotation of the joint relative to the avatar. <em>Not used.</em>
1646  * @returns {boolean} <code>false</code>.
1647  */
1648  virtual bool setAbsoluteJointRotationInObjectFrame(int index, const glm::quat& rotation) override { return false; }
1649 
1650  /*@jsdoc
1651  * Sets the translation of a joint relative to the avatar.
1652  * <p><strong>Warning:</strong> Not able to be used in the <code>Avatar</code> API.</p>
1653  * @function Avatar.setAbsoluteJointTranslationInObjectFrame
1654  * @param {number} index - The index of the joint. <em>Not used.</em>
1655  * @param {Vec3} translation - The translation of the joint relative to the avatar. <em>Not used.</em>
1656  * @returns {boolean} <code>false</code>.
1657  */
1658  virtual bool setAbsoluteJointTranslationInObjectFrame(int index, const glm::vec3& translation) override { return false; }
1659 
1660  /*@jsdoc
1661  * Gets the target scale of the avatar without any restrictions on permissible values imposed by the domain. In contrast, the
1662  * <code>scale</code> property's value may be limited by the domain's settings.
1663  * @function Avatar.getTargetScale
1664  * @returns {number} The target scale of the avatar.
1665  * @example <caption>Compare the target and current avatar scales.</caption>
1666  * print("Current avatar scale: " + MyAvatar.scale);
1667  * print("Target avatar scale: " + MyAvatar.getTargetScale());
1668  *
1669  * // Note: If using from the Avatar API, replace all occurrences of "MyAvatar" with "Avatar".
1670  */
1671  float getTargetScale() const { return _targetScale; } // why is this a slot?
1672 
1673  /*@jsdoc
1674  * @function Avatar.resetLastSent
1675  * @deprecated This function is deprecated and will be removed.
1676  */
1677  void resetLastSent() { _lastToByteArray = 0; }
1678 
1679 protected:
1680  void insertRemovedEntityID(const QUuid entityID);
1681  void lazyInitHeadData() const;
1682 
1683  float getDistanceBasedMinRotationDOT(glm::vec3 viewerPosition) const;
1684  float getDistanceBasedMinTranslationDistance(glm::vec3 viewerPosition) const;
1685 
1686  bool avatarBoundingBoxChangedSince(quint64 time) const { return _avatarBoundingBoxChanged >= time; }
1687  bool avatarScaleChangedSince(quint64 time) const { return _avatarScaleChanged >= time; }
1688  bool lookAtPositionChangedSince(quint64 time) const { return _headData->lookAtPositionChangedSince(time); }
1689  bool sensorToWorldMatrixChangedSince(quint64 time) const { return _sensorToWorldMatrixChanged >= time; }
1690  bool additionalFlagsChangedSince(quint64 time) const { return _additionalFlagsChanged >= time; }
1691  bool parentInfoChangedSince(quint64 time) const { return _parentChanged >= time; }
1692  bool faceTrackerInfoChangedSince(quint64 time) const { return true; } // FIXME
1693 
1694  bool hasParent() const { return !getParentID().isNull(); }
1695 
1696  QByteArray packSkeletonData() const;
1697  QByteArray packSkeletonModelURL() const;
1698  QByteArray packAvatarEntityTraitInstance(AvatarTraits::TraitInstanceID traitInstanceID);
1699  QByteArray packGrabTraitInstance(AvatarTraits::TraitInstanceID traitInstanceID);
1700 
1701  const QVector<JointData>& getRawJointData() const { return _jointData; }
1702  void setRawJointData(QVector<JointData> data);
1703 
1704  void unpackSkeletonModelURL(const QByteArray& data);
1705  void unpackSkeletonData(const QByteArray& data);
1706 
1707  // isReplicated will be true on downstream Avatar Mixers and their clients, but false on the upstream "master"
1708  // Audio Mixer that the replicated avatar is connected to.
1709  bool _isReplicated{ false };
1710 
1711  glm::vec3 _handPosition;
1712  virtual const QString& getSessionDisplayNameForTransport() const { return _sessionDisplayName; }
1713  virtual void maybeUpdateSessionDisplayNameFromTransport(const QString& sessionDisplayName) { } // No-op in AvatarMixer
1714 
1715  // Body scale
1716  float _targetScale;
1717  float _domainMinimumHeight { MIN_AVATAR_HEIGHT };
1718  float _domainMaximumHeight { MAX_AVATAR_HEIGHT };
1719 
1720  // Hand state (are we grabbing something or not)
1721  char _handState;
1722 
1723  QVector<JointData> _jointData;
1724  QVector<JointData> _lastSentJointData;
1725  mutable QReadWriteLock _jointDataLock;
1726 
1727  // key state
1728  KeyState _keyState;
1729 
1730  bool _hasNewJointData { true }; // set in AvatarData, cleared in Avatar
1731 
1732  mutable HeadData* _headData { nullptr };
1733 
1734  QUrl _skeletonModelURL;
1735  QVector<AttachmentData> _attachmentData;
1736  QVector<AttachmentData> _oldAttachmentData;
1737  QString _displayName;
1738  QString _sessionDisplayName { };
1739  bool _lookAtSnappingEnabled { true };
1740  bool _verificationFailed { false };
1741 
1742  quint64 _errorLogExpiry;
1743 
1744  QWeakPointer<Node> _owningAvatarMixer;
1745 
1746  glm::vec3 _targetVelocity;
1747 
1748  SimpleMovingAverage _averageBytesReceived;
1749 
1750  // During recording, this holds the starting position, orientation & scale of the recorded avatar
1751  // During playback, it holds the origin from which to play the relative positions in the clip
1752  TransformPointer _recordingBasis;
1753 
1754  // _globalPosition is sent along with localPosition + parent because the avatar-mixer doesn't know
1755  // where Entities are located. This is currently only used by the mixer to decide how often to send
1756  // updates about one avatar to another.
1757  glm::vec3 _globalPosition { 0, 0, 0 };
1758  glm::vec3 _serverPosition { 0, 0, 0 };
1759 
1760  quint64 _globalPositionChanged { 0 };
1761  quint64 _avatarBoundingBoxChanged { 0 };
1762  quint64 _avatarScaleChanged { 0 };
1763  quint64 _sensorToWorldMatrixChanged { 0 };
1764  quint64 _additionalFlagsChanged { 0 };
1765  quint64 _parentChanged { 0 };
1766 
1767  quint64 _lastToByteArray { 0 }; // tracks the last time we did a toByteArray
1768 
1769  // Some rate data for incoming data in bytes
1770  RateCounter<> _parseBufferRate;
1771  RateCounter<> _globalPositionRate;
1772  RateCounter<> _localPositionRate;
1773  RateCounter<> _handControllersRate;
1774  RateCounter<> _avatarBoundingBoxRate;
1775  RateCounter<> _avatarOrientationRate;
1776  RateCounter<> _avatarScaleRate;
1777  RateCounter<> _lookAtPositionRate;
1778  RateCounter<> _audioLoudnessRate;
1779  RateCounter<> _sensorToWorldRate;
1780  RateCounter<> _additionalFlagsRate;
1781  RateCounter<> _parentInfoRate;
1782  RateCounter<> _faceTrackerRate;
1783  RateCounter<> _jointDataRate;
1784  RateCounter<> _jointDefaultPoseFlagsRate;
1785  RateCounter<> _farGrabJointRate;
1786 
1787  // Some rate data for incoming data updates
1788  RateCounter<> _parseBufferUpdateRate;
1789  RateCounter<> _globalPositionUpdateRate;
1790  RateCounter<> _localPositionUpdateRate;
1791  RateCounter<> _handControllersUpdateRate;
1792  RateCounter<> _avatarBoundingBoxUpdateRate;
1793  RateCounter<> _avatarOrientationUpdateRate;
1794  RateCounter<> _avatarScaleUpdateRate;
1795  RateCounter<> _lookAtPositionUpdateRate;
1796  RateCounter<> _audioLoudnessUpdateRate;
1797  RateCounter<> _sensorToWorldUpdateRate;
1798  RateCounter<> _additionalFlagsUpdateRate;
1799  RateCounter<> _parentInfoUpdateRate;
1800  RateCounter<> _faceTrackerUpdateRate;
1801  RateCounter<> _jointDataUpdateRate;
1802  RateCounter<> _jointDefaultPoseFlagsUpdateRate;
1803  RateCounter<> _farGrabJointUpdateRate;
1804 
1805  // Some rate data for outgoing data
1806  AvatarDataRate _outboundDataRate;
1807 
1808  glm::vec3 _globalBoundingBoxDimensions;
1809  glm::vec3 _globalBoundingBoxOffset;
1810 
1811  AABox _defaultBubbleBox;
1812  AABox _fitBoundingBox;
1813 
1814  mutable ReadWriteLockable _avatarEntitiesLock;
1815  AvatarEntityIDs _avatarEntityRemoved; // recently removed AvatarEntity ids
1816  AvatarEntityIDs _avatarEntityForRecording; // create new entities id for avatar recording
1817  PackedAvatarEntityMap _packedAvatarEntityData;
1818  bool _avatarEntityDataChanged { false };
1819 
1820  mutable ReadWriteLockable _avatarGrabsLock;
1821  AvatarGrabDataMap _avatarGrabData;
1822  bool _avatarGrabDataChanged { false }; // by network
1823 
1824  mutable ReadWriteLockable _avatarSkeletonDataLock;
1825  std::vector<AvatarSkeletonTrait::UnpackedJointData> _avatarSkeletonData;
1826 
1827  // used to transform any sensor into world space, including the _hmdSensorMat, or hand controllers.
1828  ThreadSafeValueCache<glm::mat4> _sensorToWorldMatrixCache { glm::mat4() };
1829  ThreadSafeValueCache<glm::mat4> _controllerLeftHandMatrixCache { glm::mat4() };
1830  ThreadSafeValueCache<glm::mat4> _controllerRightHandMatrixCache { glm::mat4() };
1831 
1832  ThreadSafeValueCache<glm::mat4> _farGrabRightMatrixCache { glm::mat4() };
1833  ThreadSafeValueCache<glm::mat4> _farGrabLeftMatrixCache { glm::mat4() };
1834  ThreadSafeValueCache<glm::mat4> _farGrabMouseMatrixCache { glm::mat4() };
1835 
1836  ThreadSafeValueCache<QVariantMap> _collisionCapsuleCache{ QVariantMap() };
1837 
1838  int getFauxJointIndex(const QString& name) const;
1839 
1840  float _audioLoudness { 0.0f };
1841  quint64 _audioLoudnessChanged { 0 };
1842  float _audioAverageLoudness { 0.0f };
1843 
1844  bool _identityDataChanged { false };
1845  udt::SequenceNumber _identitySequenceNumber { 0 };
1846  bool _hasProcessedFirstIdentity { false };
1847  float _density;
1848  int _replicaIndex { 0 };
1849  bool _isNewAvatar { true };
1850  bool _isClientAvatar { false };
1851  bool _collideWithOtherAvatars { true };
1852  bool _hasPriority{ false };
1853 
1854  // null unless MyAvatar or ScriptableAvatar sending traits data to mixer
1855  std::unique_ptr<ClientTraitsHandler, LaterDeleter> _clientTraitsHandler;
1856 
1857  template <typename T, typename F>
1858  T readLockWithNamedJointIndex(const QString& name, const T& defaultValue, F f) const {
1859  QReadLocker readLock(&_jointDataLock);
1860  int index = getJointIndex(name);
1861  if (index == -1) {
1862  return defaultValue;
1863  }
1864  return f(index);
1865  }
1866 
1867  template <typename T, typename F>
1868  T readLockWithNamedJointIndex(const QString& name, F f) const {
1869  return readLockWithNamedJointIndex(name, T(), f);
1870  }
1871 
1872  template <typename F>
1873  void writeLockWithNamedJointIndex(const QString& name, F f) {
1874  QWriteLocker writeLock(&_jointDataLock);
1875  int index = getJointIndex(name);
1876  if (index == -1) {
1877  return;
1878  }
1879  if (_jointData.size() <= index) {
1880  _jointData.resize(index + 1);
1881  }
1882  f(index);
1883  }
1884 
1885  bool updateAvatarGrabData(const QUuid& grabID, const QByteArray& grabData);
1886  virtual void clearAvatarGrabData(const QUuid& grabID);
1887 
1888 private:
1889  Q_DISABLE_COPY(AvatarData)
1890 
1891  friend void avatarStateFromFrame(const QByteArray& frameData, AvatarData* _avatar);
1892  // Required for setRawJointData. Making setRawJointData public would expose it to scripting interface.
1893  friend class SkeletonModel;
1894  friend class MyAvatar;
1895  static QUrl _defaultFullAvatarModelUrl;
1896 };
1897 Q_DECLARE_METATYPE(AvatarData*)
1898 
1899 QJsonValue toJsonValue(const JointData& joint);
1900 JointData jointDataFromJsonValue(const QJsonValue& q);
1901 
1902 class AttachmentData {
1903 public:
1904  QUrl modelURL;
1905  QString jointName;
1906  glm::vec3 translation;
1907  glm::quat rotation;
1908  float scale { 1.0f };
1909  bool isSoft { false };
1910 
1911  bool isValid() const { return modelURL.isValid(); }
1912 
1913  bool operator==(const AttachmentData& other) const;
1914 
1915  QJsonObject toJson() const;
1916  void fromJson(const QJsonObject& json);
1917 
1918  QVariant toVariant() const;
1919  bool fromVariant(const QVariant& variant);
1920 };
1921 
1922 QDataStream& operator<<(QDataStream& out, const AttachmentData& attachment);
1923 QDataStream& operator>>(QDataStream& in, AttachmentData& attachment);
1924 
1925 Q_DECLARE_METATYPE(AttachmentData)
1926 Q_DECLARE_METATYPE(QVector<AttachmentData>)
1927 
1928 class AttachmentDataObject : public QObject, protected Scriptable {
1930  Q_OBJECT
1931  Q_PROPERTY(QString modelURL READ getModelURL WRITE setModelURL)
1932  Q_PROPERTY(QString jointName READ getJointName WRITE setJointName)
1933  Q_PROPERTY(glm::vec3 translation READ getTranslation WRITE setTranslation)
1934  Q_PROPERTY(glm::quat rotation READ getRotation WRITE setRotation)
1935  Q_PROPERTY(float scale READ getScale WRITE setScale)
1936  Q_PROPERTY(bool isSoft READ getIsSoft WRITE setIsSoft)
1937 
1938 public:
1939 
1940  Q_INVOKABLE void setModelURL(const QString& modelURL);
1941  Q_INVOKABLE QString getModelURL() const;
1942 
1943  Q_INVOKABLE void setJointName(const QString& jointName);
1944  Q_INVOKABLE QString getJointName() const;
1945 
1946  Q_INVOKABLE void setTranslation(const glm::vec3& translation);
1947  Q_INVOKABLE glm::vec3 getTranslation() const;
1948 
1949  Q_INVOKABLE void setRotation(const glm::quat& rotation);
1950  Q_INVOKABLE glm::quat getRotation() const;
1951 
1952  Q_INVOKABLE void setScale(float scale);
1953  Q_INVOKABLE float getScale() const;
1954 
1955  Q_INVOKABLE void setIsSoft(bool scale);
1956  Q_INVOKABLE bool getIsSoft() const;
1957 };
1958 
1959 void registerAvatarTypes(ScriptEngine* engine);
1960 void registerAvatarPrototypes(ScriptEngine* engine);
1961 
1962 class RayToAvatarIntersectionResult {
1963 public:
1964  bool intersects { false };
1965  QUuid avatarID;
1966  float distance { FLT_MAX };
1967  BoxFace face { UNKNOWN_FACE };
1968  glm::vec3 intersection { glm::vec3(0.0f, 0.0f, 0.0f) };
1969  glm::vec3 surfaceNormal { glm::vec3(0.0f, 1.0f, 0.0f) };
1970  int jointIndex { -1 };
1971  QVariantMap extraInfo;
1972 };
1973 Q_DECLARE_METATYPE(RayToAvatarIntersectionResult)
1974 ScriptValue RayToAvatarIntersectionResultToScriptValue(ScriptEngine* engine, const RayToAvatarIntersectionResult& results);
1975 bool RayToAvatarIntersectionResultFromScriptValue(const ScriptValue& object, RayToAvatarIntersectionResult& results);
1976 
1977 // No JSDoc because it's not provided as a type to the script engine.
1978 class ParabolaToAvatarIntersectionResult {
1979 public:
1980  bool intersects { false };
1981  QUuid avatarID;
1982  float distance { 0.0f };
1983  float parabolicDistance { 0.0f };
1984  BoxFace face;
1985  glm::vec3 intersection;
1986  glm::vec3 surfaceNormal;
1987  QVariantMap extraInfo;
1988 };
1989 
1990 Q_DECLARE_METATYPE(AvatarEntityMap)
1991 
1992 ScriptValue AvatarEntityMapToScriptValue(ScriptEngine* engine, const AvatarEntityMap& value);
1993 bool AvatarEntityMapFromScriptValue(const ScriptValue& object, AvatarEntityMap& value);
1994 
1995 // faux joint indexes (-1 means invalid)
1996 const int NO_JOINT_INDEX = 65535; // -1
1997 const int SENSOR_TO_WORLD_MATRIX_INDEX = 65534; // -2
1998 const int CONTROLLER_RIGHTHAND_INDEX = 65533; // -3
1999 const int CONTROLLER_LEFTHAND_INDEX = 65532; // -4
2000 const int CAMERA_RELATIVE_CONTROLLER_RIGHTHAND_INDEX = 65531; // -5
2001 const int CAMERA_RELATIVE_CONTROLLER_LEFTHAND_INDEX = 65530; // -6
2002 const int CAMERA_MATRIX_INDEX = 65529; // -7
2003 const int FARGRAB_RIGHTHAND_INDEX = 65528; // -8
2004 const int FARGRAB_LEFTHAND_INDEX = 65527; // -9
2005 const int FARGRAB_MOUSE_INDEX = 65526; // -10
2006 
2007 const int LOWEST_PSEUDO_JOINT_INDEX = 65526;
2008 
2009 const int MAX_NUM_AVATAR_GRABS = 6;
2010 
2011 #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
[ScriptInterface] Provides an engine-independent interface for QScriptable
Definition: Scriptable.h:29
A skeleton loaded from a model.
Definition: SkeletonModel.h:25