xr_hand_tracker.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**************************************************************************/
  2. /* xr_hand_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_hand_tracker.h"
  31. #include "xr_body_tracker.h"
  32. void XRHandTracker::_bind_methods() {
  33. ClassDB::bind_method(D_METHOD("set_has_tracking_data", "has_data"), &XRHandTracker::set_has_tracking_data);
  34. ClassDB::bind_method(D_METHOD("get_has_tracking_data"), &XRHandTracker::get_has_tracking_data);
  35. ClassDB::bind_method(D_METHOD("set_hand_tracking_source", "source"), &XRHandTracker::set_hand_tracking_source);
  36. ClassDB::bind_method(D_METHOD("get_hand_tracking_source"), &XRHandTracker::get_hand_tracking_source);
  37. ClassDB::bind_method(D_METHOD("set_hand_joint_flags", "joint", "flags"), &XRHandTracker::set_hand_joint_flags);
  38. ClassDB::bind_method(D_METHOD("get_hand_joint_flags", "joint"), &XRHandTracker::get_hand_joint_flags);
  39. ClassDB::bind_method(D_METHOD("set_hand_joint_transform", "joint", "transform"), &XRHandTracker::set_hand_joint_transform);
  40. ClassDB::bind_method(D_METHOD("get_hand_joint_transform", "joint"), &XRHandTracker::get_hand_joint_transform);
  41. ClassDB::bind_method(D_METHOD("set_hand_joint_radius", "joint", "radius"), &XRHandTracker::set_hand_joint_radius);
  42. ClassDB::bind_method(D_METHOD("get_hand_joint_radius", "joint"), &XRHandTracker::get_hand_joint_radius);
  43. ClassDB::bind_method(D_METHOD("set_hand_joint_linear_velocity", "joint", "linear_velocity"), &XRHandTracker::set_hand_joint_linear_velocity);
  44. ClassDB::bind_method(D_METHOD("get_hand_joint_linear_velocity", "joint"), &XRHandTracker::get_hand_joint_linear_velocity);
  45. ClassDB::bind_method(D_METHOD("set_hand_joint_angular_velocity", "joint", "angular_velocity"), &XRHandTracker::set_hand_joint_angular_velocity);
  46. ClassDB::bind_method(D_METHOD("get_hand_joint_angular_velocity", "joint"), &XRHandTracker::get_hand_joint_angular_velocity);
  47. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "has_tracking_data", PROPERTY_HINT_NONE), "set_has_tracking_data", "get_has_tracking_data");
  48. ADD_PROPERTY(PropertyInfo(Variant::INT, "hand_tracking_source", PROPERTY_HINT_ENUM, "Unknown,Unobstructed,Controller"), "set_hand_tracking_source", "get_hand_tracking_source");
  49. BIND_ENUM_CONSTANT(HAND_TRACKING_SOURCE_UNKNOWN);
  50. BIND_ENUM_CONSTANT(HAND_TRACKING_SOURCE_UNOBSTRUCTED);
  51. BIND_ENUM_CONSTANT(HAND_TRACKING_SOURCE_CONTROLLER);
  52. BIND_ENUM_CONSTANT(HAND_TRACKING_SOURCE_NOT_TRACKED);
  53. BIND_ENUM_CONSTANT(HAND_TRACKING_SOURCE_MAX);
  54. BIND_ENUM_CONSTANT(HAND_JOINT_PALM);
  55. BIND_ENUM_CONSTANT(HAND_JOINT_WRIST);
  56. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_METACARPAL);
  57. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_PHALANX_PROXIMAL);
  58. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_PHALANX_DISTAL);
  59. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_TIP);
  60. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_FINGER_METACARPAL);
  61. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_FINGER_PHALANX_PROXIMAL);
  62. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_FINGER_PHALANX_INTERMEDIATE);
  63. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_FINGER_PHALANX_DISTAL);
  64. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_FINGER_TIP);
  65. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_FINGER_METACARPAL);
  66. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_FINGER_PHALANX_PROXIMAL);
  67. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_FINGER_PHALANX_INTERMEDIATE);
  68. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_FINGER_PHALANX_DISTAL);
  69. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_FINGER_TIP);
  70. BIND_ENUM_CONSTANT(HAND_JOINT_RING_FINGER_METACARPAL);
  71. BIND_ENUM_CONSTANT(HAND_JOINT_RING_FINGER_PHALANX_PROXIMAL);
  72. BIND_ENUM_CONSTANT(HAND_JOINT_RING_FINGER_PHALANX_INTERMEDIATE);
  73. BIND_ENUM_CONSTANT(HAND_JOINT_RING_FINGER_PHALANX_DISTAL);
  74. BIND_ENUM_CONSTANT(HAND_JOINT_RING_FINGER_TIP);
  75. BIND_ENUM_CONSTANT(HAND_JOINT_PINKY_FINGER_METACARPAL);
  76. BIND_ENUM_CONSTANT(HAND_JOINT_PINKY_FINGER_PHALANX_PROXIMAL);
  77. BIND_ENUM_CONSTANT(HAND_JOINT_PINKY_FINGER_PHALANX_INTERMEDIATE);
  78. BIND_ENUM_CONSTANT(HAND_JOINT_PINKY_FINGER_PHALANX_DISTAL);
  79. BIND_ENUM_CONSTANT(HAND_JOINT_PINKY_FINGER_TIP);
  80. BIND_ENUM_CONSTANT(HAND_JOINT_MAX);
  81. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_ORIENTATION_VALID);
  82. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_ORIENTATION_TRACKED);
  83. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_POSITION_VALID);
  84. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_POSITION_TRACKED);
  85. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_LINEAR_VELOCITY_VALID);
  86. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_ANGULAR_VELOCITY_VALID);
  87. }
  88. void XRHandTracker::set_tracker_type(XRServer::TrackerType p_type) {
  89. ERR_FAIL_COND_MSG(p_type != XRServer::TRACKER_HAND, "XRHandTracker must be of type TRACKER_HAND.");
  90. }
  91. void XRHandTracker::set_tracker_hand(const XRPositionalTracker::TrackerHand p_hand) {
  92. ERR_FAIL_COND_MSG(p_hand != TRACKER_HAND_LEFT && p_hand != TRACKER_HAND_RIGHT, "XRHandTracker must specify hand.");
  93. tracker_hand = p_hand;
  94. }
  95. void XRHandTracker::set_has_tracking_data(bool p_has_tracking_data) {
  96. has_tracking_data = p_has_tracking_data;
  97. }
  98. bool XRHandTracker::get_has_tracking_data() const {
  99. return has_tracking_data;
  100. }
  101. void XRHandTracker::set_hand_tracking_source(XRHandTracker::HandTrackingSource p_source) {
  102. hand_tracking_source = p_source;
  103. }
  104. XRHandTracker::HandTrackingSource XRHandTracker::get_hand_tracking_source() const {
  105. return hand_tracking_source;
  106. }
  107. void XRHandTracker::set_hand_joint_flags(XRHandTracker::HandJoint p_joint, BitField<XRHandTracker::HandJointFlags> p_flags) {
  108. ERR_FAIL_INDEX(p_joint, HAND_JOINT_MAX);
  109. hand_joint_flags[p_joint] = p_flags;
  110. }
  111. BitField<XRHandTracker::HandJointFlags> XRHandTracker::get_hand_joint_flags(XRHandTracker::HandJoint p_joint) const {
  112. ERR_FAIL_INDEX_V(p_joint, HAND_JOINT_MAX, BitField<HandJointFlags>());
  113. return hand_joint_flags[p_joint];
  114. }
  115. void XRHandTracker::set_hand_joint_transform(XRHandTracker::HandJoint p_joint, const Transform3D &p_transform) {
  116. ERR_FAIL_INDEX(p_joint, HAND_JOINT_MAX);
  117. hand_joint_transforms[p_joint] = p_transform;
  118. }
  119. Transform3D XRHandTracker::get_hand_joint_transform(XRHandTracker::HandJoint p_joint) const {
  120. ERR_FAIL_INDEX_V(p_joint, HAND_JOINT_MAX, Transform3D());
  121. return hand_joint_transforms[p_joint];
  122. }
  123. void XRHandTracker::set_hand_joint_radius(XRHandTracker::HandJoint p_joint, float p_radius) {
  124. ERR_FAIL_INDEX(p_joint, HAND_JOINT_MAX);
  125. hand_joint_radii[p_joint] = p_radius;
  126. }
  127. float XRHandTracker::get_hand_joint_radius(XRHandTracker::HandJoint p_joint) const {
  128. ERR_FAIL_INDEX_V(p_joint, HAND_JOINT_MAX, 0.0);
  129. return hand_joint_radii[p_joint];
  130. }
  131. void XRHandTracker::set_hand_joint_linear_velocity(XRHandTracker::HandJoint p_joint, const Vector3 &p_velocity) {
  132. ERR_FAIL_INDEX(p_joint, HAND_JOINT_MAX);
  133. hand_joint_linear_velocities[p_joint] = p_velocity;
  134. }
  135. Vector3 XRHandTracker::get_hand_joint_linear_velocity(XRHandTracker::HandJoint p_joint) const {
  136. ERR_FAIL_INDEX_V(p_joint, HAND_JOINT_MAX, Vector3());
  137. return hand_joint_linear_velocities[p_joint];
  138. }
  139. void XRHandTracker::set_hand_joint_angular_velocity(XRHandTracker::HandJoint p_joint, const Vector3 &p_velocity) {
  140. ERR_FAIL_INDEX(p_joint, HAND_JOINT_MAX);
  141. hand_joint_angular_velocities[p_joint] = p_velocity;
  142. }
  143. Vector3 XRHandTracker::get_hand_joint_angular_velocity(XRHandTracker::HandJoint p_joint) const {
  144. ERR_FAIL_INDEX_V(p_joint, HAND_JOINT_MAX, Vector3());
  145. return hand_joint_angular_velocities[p_joint];
  146. }
  147. XRHandTracker::XRHandTracker() {
  148. type = XRServer::TRACKER_HAND;
  149. tracker_hand = TRACKER_HAND_LEFT;
  150. }