xr_body_tracker.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**************************************************************************/
  2. /* xr_body_tracker.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "xr_body_tracker.h"
  31. void XRBodyTracker::_bind_methods() {
  32. ClassDB::bind_method(D_METHOD("set_has_tracking_data", "has_data"), &XRBodyTracker::set_has_tracking_data);
  33. ClassDB::bind_method(D_METHOD("get_has_tracking_data"), &XRBodyTracker::get_has_tracking_data);
  34. ClassDB::bind_method(D_METHOD("set_body_flags", "flags"), &XRBodyTracker::set_body_flags);
  35. ClassDB::bind_method(D_METHOD("get_body_flags"), &XRBodyTracker::get_body_flags);
  36. ClassDB::bind_method(D_METHOD("set_joint_flags", "joint", "flags"), &XRBodyTracker::set_joint_flags);
  37. ClassDB::bind_method(D_METHOD("get_joint_flags", "joint"), &XRBodyTracker::get_joint_flags);
  38. ClassDB::bind_method(D_METHOD("set_joint_transform", "joint", "transform"), &XRBodyTracker::set_joint_transform);
  39. ClassDB::bind_method(D_METHOD("get_joint_transform", "joint"), &XRBodyTracker::get_joint_transform);
  40. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "has_tracking_data", PROPERTY_HINT_NONE), "set_has_tracking_data", "get_has_tracking_data");
  41. ADD_PROPERTY(PropertyInfo(Variant::INT, "body_flags", PROPERTY_HINT_FLAGS, "Upper Body,Lower Body,Hands"), "set_body_flags", "get_body_flags");
  42. BIND_BITFIELD_FLAG(BODY_FLAG_UPPER_BODY_SUPPORTED);
  43. BIND_BITFIELD_FLAG(BODY_FLAG_LOWER_BODY_SUPPORTED);
  44. BIND_BITFIELD_FLAG(BODY_FLAG_HANDS_SUPPORTED);
  45. BIND_ENUM_CONSTANT(JOINT_ROOT);
  46. BIND_ENUM_CONSTANT(JOINT_HIPS);
  47. BIND_ENUM_CONSTANT(JOINT_SPINE);
  48. BIND_ENUM_CONSTANT(JOINT_CHEST);
  49. BIND_ENUM_CONSTANT(JOINT_UPPER_CHEST);
  50. BIND_ENUM_CONSTANT(JOINT_NECK);
  51. BIND_ENUM_CONSTANT(JOINT_HEAD);
  52. BIND_ENUM_CONSTANT(JOINT_HEAD_TIP);
  53. BIND_ENUM_CONSTANT(JOINT_LEFT_SHOULDER);
  54. BIND_ENUM_CONSTANT(JOINT_LEFT_UPPER_ARM);
  55. BIND_ENUM_CONSTANT(JOINT_LEFT_LOWER_ARM);
  56. BIND_ENUM_CONSTANT(JOINT_RIGHT_SHOULDER);
  57. BIND_ENUM_CONSTANT(JOINT_RIGHT_UPPER_ARM);
  58. BIND_ENUM_CONSTANT(JOINT_RIGHT_LOWER_ARM);
  59. BIND_ENUM_CONSTANT(JOINT_LEFT_UPPER_LEG);
  60. BIND_ENUM_CONSTANT(JOINT_LEFT_LOWER_LEG);
  61. BIND_ENUM_CONSTANT(JOINT_LEFT_FOOT);
  62. BIND_ENUM_CONSTANT(JOINT_LEFT_TOES);
  63. BIND_ENUM_CONSTANT(JOINT_RIGHT_UPPER_LEG);
  64. BIND_ENUM_CONSTANT(JOINT_RIGHT_LOWER_LEG);
  65. BIND_ENUM_CONSTANT(JOINT_RIGHT_FOOT);
  66. BIND_ENUM_CONSTANT(JOINT_RIGHT_TOES);
  67. BIND_ENUM_CONSTANT(JOINT_LEFT_HAND);
  68. BIND_ENUM_CONSTANT(JOINT_LEFT_PALM);
  69. BIND_ENUM_CONSTANT(JOINT_LEFT_WRIST);
  70. BIND_ENUM_CONSTANT(JOINT_LEFT_THUMB_METACARPAL);
  71. BIND_ENUM_CONSTANT(JOINT_LEFT_THUMB_PHALANX_PROXIMAL);
  72. BIND_ENUM_CONSTANT(JOINT_LEFT_THUMB_PHALANX_DISTAL);
  73. BIND_ENUM_CONSTANT(JOINT_LEFT_THUMB_TIP);
  74. BIND_ENUM_CONSTANT(JOINT_LEFT_INDEX_FINGER_METACARPAL);
  75. BIND_ENUM_CONSTANT(JOINT_LEFT_INDEX_FINGER_PHALANX_PROXIMAL);
  76. BIND_ENUM_CONSTANT(JOINT_LEFT_INDEX_FINGER_PHALANX_INTERMEDIATE);
  77. BIND_ENUM_CONSTANT(JOINT_LEFT_INDEX_FINGER_PHALANX_DISTAL);
  78. BIND_ENUM_CONSTANT(JOINT_LEFT_INDEX_FINGER_TIP);
  79. BIND_ENUM_CONSTANT(JOINT_LEFT_MIDDLE_FINGER_METACARPAL);
  80. BIND_ENUM_CONSTANT(JOINT_LEFT_MIDDLE_FINGER_PHALANX_PROXIMAL);
  81. BIND_ENUM_CONSTANT(JOINT_LEFT_MIDDLE_FINGER_PHALANX_INTERMEDIATE);
  82. BIND_ENUM_CONSTANT(JOINT_LEFT_MIDDLE_FINGER_PHALANX_DISTAL);
  83. BIND_ENUM_CONSTANT(JOINT_LEFT_MIDDLE_FINGER_TIP);
  84. BIND_ENUM_CONSTANT(JOINT_LEFT_RING_FINGER_METACARPAL);
  85. BIND_ENUM_CONSTANT(JOINT_LEFT_RING_FINGER_PHALANX_PROXIMAL);
  86. BIND_ENUM_CONSTANT(JOINT_LEFT_RING_FINGER_PHALANX_INTERMEDIATE);
  87. BIND_ENUM_CONSTANT(JOINT_LEFT_RING_FINGER_PHALANX_DISTAL);
  88. BIND_ENUM_CONSTANT(JOINT_LEFT_RING_FINGER_TIP);
  89. BIND_ENUM_CONSTANT(JOINT_LEFT_PINKY_FINGER_METACARPAL);
  90. BIND_ENUM_CONSTANT(JOINT_LEFT_PINKY_FINGER_PHALANX_PROXIMAL);
  91. BIND_ENUM_CONSTANT(JOINT_LEFT_PINKY_FINGER_PHALANX_INTERMEDIATE);
  92. BIND_ENUM_CONSTANT(JOINT_LEFT_PINKY_FINGER_PHALANX_DISTAL);
  93. BIND_ENUM_CONSTANT(JOINT_LEFT_PINKY_FINGER_TIP);
  94. BIND_ENUM_CONSTANT(JOINT_RIGHT_HAND);
  95. BIND_ENUM_CONSTANT(JOINT_RIGHT_PALM);
  96. BIND_ENUM_CONSTANT(JOINT_RIGHT_WRIST);
  97. BIND_ENUM_CONSTANT(JOINT_RIGHT_THUMB_METACARPAL);
  98. BIND_ENUM_CONSTANT(JOINT_RIGHT_THUMB_PHALANX_PROXIMAL);
  99. BIND_ENUM_CONSTANT(JOINT_RIGHT_THUMB_PHALANX_DISTAL);
  100. BIND_ENUM_CONSTANT(JOINT_RIGHT_THUMB_TIP);
  101. BIND_ENUM_CONSTANT(JOINT_RIGHT_INDEX_FINGER_METACARPAL);
  102. BIND_ENUM_CONSTANT(JOINT_RIGHT_INDEX_FINGER_PHALANX_PROXIMAL);
  103. BIND_ENUM_CONSTANT(JOINT_RIGHT_INDEX_FINGER_PHALANX_INTERMEDIATE);
  104. BIND_ENUM_CONSTANT(JOINT_RIGHT_INDEX_FINGER_PHALANX_DISTAL);
  105. BIND_ENUM_CONSTANT(JOINT_RIGHT_INDEX_FINGER_TIP);
  106. BIND_ENUM_CONSTANT(JOINT_RIGHT_MIDDLE_FINGER_METACARPAL);
  107. BIND_ENUM_CONSTANT(JOINT_RIGHT_MIDDLE_FINGER_PHALANX_PROXIMAL);
  108. BIND_ENUM_CONSTANT(JOINT_RIGHT_MIDDLE_FINGER_PHALANX_INTERMEDIATE);
  109. BIND_ENUM_CONSTANT(JOINT_RIGHT_MIDDLE_FINGER_PHALANX_DISTAL);
  110. BIND_ENUM_CONSTANT(JOINT_RIGHT_MIDDLE_FINGER_TIP);
  111. BIND_ENUM_CONSTANT(JOINT_RIGHT_RING_FINGER_METACARPAL);
  112. BIND_ENUM_CONSTANT(JOINT_RIGHT_RING_FINGER_PHALANX_PROXIMAL);
  113. BIND_ENUM_CONSTANT(JOINT_RIGHT_RING_FINGER_PHALANX_INTERMEDIATE);
  114. BIND_ENUM_CONSTANT(JOINT_RIGHT_RING_FINGER_PHALANX_DISTAL);
  115. BIND_ENUM_CONSTANT(JOINT_RIGHT_RING_FINGER_TIP);
  116. BIND_ENUM_CONSTANT(JOINT_RIGHT_PINKY_FINGER_METACARPAL);
  117. BIND_ENUM_CONSTANT(JOINT_RIGHT_PINKY_FINGER_PHALANX_PROXIMAL);
  118. BIND_ENUM_CONSTANT(JOINT_RIGHT_PINKY_FINGER_PHALANX_INTERMEDIATE);
  119. BIND_ENUM_CONSTANT(JOINT_RIGHT_PINKY_FINGER_PHALANX_DISTAL);
  120. BIND_ENUM_CONSTANT(JOINT_RIGHT_PINKY_FINGER_TIP);
  121. BIND_ENUM_CONSTANT(JOINT_MAX);
  122. BIND_BITFIELD_FLAG(JOINT_FLAG_ORIENTATION_VALID);
  123. BIND_BITFIELD_FLAG(JOINT_FLAG_ORIENTATION_TRACKED);
  124. BIND_BITFIELD_FLAG(JOINT_FLAG_POSITION_VALID);
  125. BIND_BITFIELD_FLAG(JOINT_FLAG_POSITION_TRACKED);
  126. }
  127. void XRBodyTracker::set_tracker_type(XRServer::TrackerType p_type) {
  128. ERR_FAIL_COND_MSG(p_type != XRServer::TRACKER_BODY, "XRBodyTracker must be of type TRACKER_BODY.");
  129. }
  130. void XRBodyTracker::set_tracker_hand(const XRPositionalTracker::TrackerHand p_hand) {
  131. ERR_FAIL_COND_MSG(p_hand != XRPositionalTracker::TRACKER_HAND_UNKNOWN, "XRBodyTracker cannot specify hand.");
  132. }
  133. void XRBodyTracker::set_has_tracking_data(bool p_has_tracking_data) {
  134. has_tracking_data = p_has_tracking_data;
  135. }
  136. bool XRBodyTracker::get_has_tracking_data() const {
  137. return has_tracking_data;
  138. }
  139. void XRBodyTracker::set_body_flags(BitField<BodyFlags> p_body_flags) {
  140. body_flags = p_body_flags;
  141. }
  142. BitField<XRBodyTracker::BodyFlags> XRBodyTracker::get_body_flags() const {
  143. return body_flags;
  144. }
  145. void XRBodyTracker::set_joint_flags(Joint p_joint, BitField<JointFlags> p_flags) {
  146. ERR_FAIL_INDEX(p_joint, JOINT_MAX);
  147. joint_flags[p_joint] = p_flags;
  148. }
  149. BitField<XRBodyTracker::JointFlags> XRBodyTracker::get_joint_flags(Joint p_joint) const {
  150. ERR_FAIL_INDEX_V(p_joint, JOINT_MAX, BitField<JointFlags>());
  151. return joint_flags[p_joint];
  152. }
  153. void XRBodyTracker::set_joint_transform(Joint p_joint, const Transform3D &p_transform) {
  154. ERR_FAIL_INDEX(p_joint, JOINT_MAX);
  155. joint_transforms[p_joint] = p_transform;
  156. }
  157. Transform3D XRBodyTracker::get_joint_transform(Joint p_joint) const {
  158. ERR_FAIL_INDEX_V(p_joint, JOINT_MAX, Transform3D());
  159. return joint_transforms[p_joint];
  160. }
  161. XRBodyTracker::XRBodyTracker() {
  162. type = XRServer::TRACKER_BODY;
  163. }