lljoint.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * @file lljoint.h
  3. * @brief Implementation of LLJoint class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLJOINT_H
  33. #define LL_LLJOINT_H
  34. #include <map>
  35. #include <sstream>
  36. #include <string>
  37. #include <vector>
  38. #include "hbfastmap.h"
  39. #include "llxform.h"
  40. class LLAvatarJoint;
  41. class LLViewerJoint;
  42. constexpr S32 LL_CHARACTER_MAX_JOINTS_PER_MESH = 15;
  43. // Need to set this to count of animate-able joints, currently = #bones +
  44. // #collision_volumes + #attachments + 2, rounded to next multiple of 4.
  45. constexpr U32 LL_CHARACTER_MAX_ANIMATED_JOINTS = 216;
  46. constexpr U32 LL_MAX_JOINTS_PER_MESH_OBJECT = 110;
  47. constexpr F32 LL_JOINT_TRESHOLD_POS_OFFSET = 0.0001f; // 0.1 mm
  48. // These should be higher than the joint_num of any other joint, to avoid
  49. // conflicts in updateMotionsByType()
  50. constexpr U32 LL_HAND_JOINT_NUM = LL_CHARACTER_MAX_ANIMATED_JOINTS - 1;
  51. constexpr U32 LL_FACE_JOINT_NUM = LL_CHARACTER_MAX_ANIMATED_JOINTS - 2;
  52. constexpr S32 LL_CHARACTER_MAX_PRIORITY = 7;
  53. constexpr F32 LL_MAX_PELVIS_OFFSET = 5.f;
  54. // These are the indexes of "well known" joints (used in the code) in
  55. // sJointNamesList. If you change this list, be sure to change the static
  56. // LLJoint::getKey() method accordingly !
  57. constexpr U32 LL_JOINT_KEY_SCREEN = 1;
  58. constexpr U32 LL_JOINT_KEY_ROOT = 2;
  59. constexpr U32 LL_JOINT_KEY_PELVIS = 3;
  60. constexpr U32 LL_JOINT_KEY_TORSO = 4;
  61. constexpr U32 LL_JOINT_KEY_CHEST = 5;
  62. constexpr U32 LL_JOINT_KEY_NECK = 6;
  63. constexpr U32 LL_JOINT_KEY_HEAD = 7;
  64. constexpr U32 LL_JOINT_KEY_SKULL = 8;
  65. constexpr U32 LL_JOINT_KEY_HIPLEFT = 9;
  66. constexpr U32 LL_JOINT_KEY_HIPRIGHT = 10;
  67. constexpr U32 LL_JOINT_KEY_KNEELEFT = 11;
  68. constexpr U32 LL_JOINT_KEY_KNEERIGHT = 12;
  69. constexpr U32 LL_JOINT_KEY_ANKLELEFT = 13;
  70. constexpr U32 LL_JOINT_KEY_ANKLERIGHT = 14;
  71. constexpr U32 LL_JOINT_KEY_FOOTLEFT = 15;
  72. constexpr U32 LL_JOINT_KEY_FOOTRIGHT = 16;
  73. constexpr U32 LL_JOINT_KEY_WRISTLEFT = 17;
  74. constexpr U32 LL_JOINT_KEY_WRISTRIGHT = 18;
  75. constexpr U32 LL_JOINT_KEY_EYELEFT = 19;
  76. constexpr U32 LL_JOINT_KEY_EYERIGHT = 20;
  77. constexpr U32 LL_JOINT_KEY_ELBOWLEFT = 21;
  78. constexpr U32 LL_JOINT_KEY_SHOULDERLEFT = 22;
  79. constexpr U32 LL_JOINT_KEY_EYEALTLEFT = 23;
  80. constexpr U32 LL_JOINT_KEY_EYEALTRIGHT = 24;
  81. typedef std::map<std::string, std::string> joint_alias_map_t;
  82. class LLVector3OverrideMap
  83. {
  84. public:
  85. LLVector3OverrideMap() {}
  86. LL_INLINE void add(const LLUUID& mesh_id,
  87. const LLVector3& pos) { mMap[mesh_id] = pos; }
  88. LL_INLINE bool remove(const LLUUID& mesh_id) { return mMap.erase(mesh_id) > 0; }
  89. LL_INLINE U32 count() const { return mMap.size(); }
  90. typedef fast_hmap<LLUUID, LLVector3> map_t;
  91. LL_INLINE const map_t& getMap() const { return mMap; }
  92. LL_INLINE void clear() { mMap.clear(); }
  93. bool findActiveOverride(LLUUID& mesh_id, LLVector3& pos) const;
  94. void showJointVector3Overrides(std::ostringstream& os) const;
  95. private:
  96. map_t mMap;
  97. };
  98. LL_INLINE bool operator==(const LLVector3OverrideMap& a,
  99. const LLVector3OverrideMap& b)
  100. {
  101. return a.getMap() == b.getMap();
  102. }
  103. LL_INLINE bool operator!=(const LLVector3OverrideMap& a,
  104. const LLVector3OverrideMap& b)
  105. {
  106. return a.getMap() != b.getMap();
  107. }
  108. //-----------------------------------------------------------------------------
  109. // class LLJoint
  110. //-----------------------------------------------------------------------------
  111. class LLJoint
  112. {
  113. protected:
  114. LOG_CLASS(LLJoint);
  115. public:
  116. // Priority levels, from highest to lowest
  117. enum JointPriority
  118. {
  119. USE_MOTION_PRIORITY = -1,
  120. LOW_PRIORITY = 0,
  121. MEDIUM_PRIORITY,
  122. HIGH_PRIORITY,
  123. HIGHER_PRIORITY,
  124. HIGHEST_PRIORITY,
  125. ADDITIVE_PRIORITY = LL_CHARACTER_MAX_PRIORITY,
  126. // Extra constant just for clarity.
  127. PUPPET_PRIORITY = LL_CHARACTER_MAX_PRIORITY
  128. };
  129. enum DirtyFlags
  130. {
  131. MATRIX_DIRTY = 0x1 << 0,
  132. ROTATION_DIRTY = 0x1 << 1,
  133. POSITION_DIRTY = 0x1 << 2,
  134. ALL_DIRTY = 0x7
  135. };
  136. enum SupportCategory
  137. {
  138. SUPPORT_BASE,
  139. SUPPORT_EXTENDED
  140. };
  141. LLJoint();
  142. LLJoint(const std::string& name, LLJoint* parent = NULL);
  143. virtual ~LLJoint();
  144. LL_INLINE virtual LLViewerJoint* asViewerJoint() { return NULL; }
  145. LL_INLINE virtual LLAvatarJoint* asAvatarJoint() { return NULL; }
  146. // Sets name and parent
  147. void setup(const std::string& name, LLJoint* parent = NULL);
  148. void touch(U32 flags = ALL_DIRTY);
  149. static U32 getKey(const std::string& name, bool add_if_unknown = true);
  150. static U32 getAliasedJointKey(const std::string& name);
  151. LL_INLINE U32 getKey() const { return mKey; }
  152. static const std::string& getName(U32 key);
  153. LL_INLINE const std::string& getName() const { return sJointNamesList[mKey]; }
  154. LL_INLINE void setName(const std::string& name) { mKey = getKey(name); }
  155. LL_INLINE S32 getJointNum() const { return mJointNum; }
  156. void setJointNum(S32 joint_num);
  157. LL_INLINE bool isBone() const { return mIsBone; }
  158. LL_INLINE void setIsBone(bool is_bone) { mIsBone = is_bone; }
  159. LL_INLINE SupportCategory getSupport() const { return mSupport; }
  160. LL_INLINE void setSupport(const SupportCategory& s) { mSupport = s; }
  161. void setSupport(const std::string& support_string);
  162. LL_INLINE const LLVector3& getEnd() const { return mEnd; }
  163. LL_INLINE void setEnd(const LLVector3& end) { mEnd = end; }
  164. LL_INLINE LLJoint* getParent() { return mParent; }
  165. LLJoint* getRoot();
  166. // Searches for child joint by key
  167. LLJoint* findJoint(U32 key);
  168. // Searches for child joint by name
  169. LLJoint* findJoint(const std::string& name)
  170. {
  171. return findJoint(getKey(name, false));
  172. }
  173. // Searches for child joint by name, with aliases
  174. LL_INLINE LLJoint* findAliasedJoint(const std::string& name)
  175. {
  176. return findJoint(getAliasedJointKey(name));
  177. }
  178. void addChild(LLJoint* joint);
  179. void removeChild(LLJoint* joint);
  180. void removeAllChildren();
  181. // A returned number of 0 indicates an end effector, 1 a normal joint and
  182. // over 1 a leaf.
  183. LL_INLINE U32 getNumChildren() const { return mChildren.size(); }
  184. LL_INLINE const LLVector3& getPosition() const { return mXform.getPosition(); }
  185. void setPosition(const LLVector3& pos, bool do_override = false);
  186. LL_INLINE void setDefaultPosition(const LLVector3& pos)
  187. {
  188. mDefaultPosition = pos;
  189. }
  190. LL_INLINE const LLVector3& getDefaultPosition() const
  191. {
  192. return mDefaultPosition;
  193. }
  194. LL_INLINE void setDefaultScale(const LLVector3& scale)
  195. {
  196. mDefaultScale = scale;
  197. }
  198. LL_INLINE const LLVector3& getDefaultScale() const
  199. {
  200. return mDefaultScale;
  201. }
  202. const LLVector3& getWorldPosition();
  203. const LLVector3& getLastWorldPosition() const;
  204. void setWorldPosition(const LLVector3& pos);
  205. const LLQuaternion& getRotation() const;
  206. void setRotation(const LLQuaternion& rot);
  207. const LLQuaternion& getWorldRotation();
  208. const LLQuaternion& getLastWorldRotation();
  209. void setWorldRotation(const LLQuaternion& rot);
  210. const LLVector3& getScale() const;
  211. void setScale(const LLVector3& scale,
  212. bool apply_attachment_overrides = false);
  213. const LLMatrix4& getWorldMatrix();
  214. void updateWorldMatrixChildren();
  215. void updateWorldMatrixParent();
  216. void updateWorldPRSParent();
  217. void updateWorldMatrix();
  218. LL_INLINE const LLVector3& getSkinOffset() const { return mSkinOffset; }
  219. LL_INLINE void setSkinOffset(const LLVector3& o) { mSkinOffset = o; }
  220. LL_INLINE LLXformMatrix* getXform() { return &mXform; }
  221. LL_INLINE virtual bool isAnimatable() const { return true; }
  222. void addAttachmentPosOverride(const LLVector3& pos,
  223. const LLUUID& mesh_id,
  224. const std::string& av_info,
  225. bool* active_override_changed = NULL);
  226. void addAttachmentScaleOverride(const LLVector3& scale,
  227. const LLUUID& mesh_id,
  228. const std::string& av_info);
  229. void removeAttachmentPosOverride(const LLUUID& mesh_id,
  230. const std::string& av_info,
  231. bool* active_override_changed = NULL);
  232. void removeAttachmentScaleOverride(const LLUUID& mesh_id,
  233. const std::string& av_info);
  234. LL_INLINE bool hasAttachmentPosOverride(LLVector3& pos,
  235. LLUUID& mesh_id) const
  236. {
  237. return mAttachmentPosOverrides.findActiveOverride(mesh_id, pos);
  238. }
  239. LL_INLINE bool hasAttachmentScaleOverride(LLVector3& scale,
  240. LLUUID& mesh_id) const
  241. {
  242. return mAttachmentScaleOverrides.findActiveOverride(mesh_id, scale);
  243. }
  244. void clearAttachmentPosOverrides();
  245. void clearAttachmentScaleOverrides();
  246. S32 getAllAttachmentPosOverrides(std::set<LLVector3>& overrides) const;
  247. S32 getAllAttachmentScaleOverrides(std::set<LLVector3>& overrides) const;
  248. void updatePos(const std::string& av_info);
  249. void updateScale(const std::string& av_info);
  250. // These are used in checks of whether a pos/scale override is considered
  251. // significant.
  252. bool aboveJointPosThreshold(const LLVector3& pos) const;
  253. bool aboveJointScaleThreshold(const LLVector3& scale) const;
  254. private:
  255. void init();
  256. protected:
  257. // Explicit transformation members
  258. LLXformMatrix mXform;
  259. // Parent joint
  260. LLJoint* mParent;
  261. LLVector3 mDefaultPosition;
  262. LLVector3 mDefaultScale;
  263. U32 mKey;
  264. SupportCategory mSupport;
  265. public:
  266. // Describes the skin binding pose
  267. LLVector3 mSkinOffset;
  268. // End point of the bone, if applicable. This is only relevant for external
  269. // programs like Blender, and for diagnostic display.
  270. LLVector3 mEnd;
  271. S32 mJointNum;
  272. U32 mDirtyFlags;
  273. // Child joints
  274. typedef std::vector<LLJoint*> child_list_t;
  275. child_list_t mChildren;
  276. // Position overrides
  277. LLVector3OverrideMap mAttachmentPosOverrides;
  278. LLVector3 mPosBeforeOverrides;
  279. // Scale overrides
  280. LLVector3OverrideMap mAttachmentScaleOverrides;
  281. LLVector3 mScaleBeforeOverrides;
  282. bool mUpdateXform;
  283. bool mIsBone;
  284. // Debug statics
  285. static S32 sNumTouches;
  286. static S32 sNumUpdates;
  287. static std::vector<std::string> sJointNamesList;
  288. static joint_alias_map_t sAvatarJointAliasMap;
  289. };
  290. #endif // LL_LLJOINT_H