xr_hand_tracker.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_MAX);
  53. BIND_ENUM_CONSTANT(HAND_JOINT_PALM);
  54. BIND_ENUM_CONSTANT(HAND_JOINT_WRIST);
  55. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_METACARPAL);
  56. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_PHALANX_PROXIMAL);
  57. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_PHALANX_DISTAL);
  58. BIND_ENUM_CONSTANT(HAND_JOINT_THUMB_TIP);
  59. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_FINGER_METACARPAL);
  60. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_FINGER_PHALANX_PROXIMAL);
  61. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_FINGER_PHALANX_INTERMEDIATE);
  62. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_FINGER_PHALANX_DISTAL);
  63. BIND_ENUM_CONSTANT(HAND_JOINT_INDEX_FINGER_TIP);
  64. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_FINGER_METACARPAL);
  65. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_FINGER_PHALANX_PROXIMAL);
  66. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_FINGER_PHALANX_INTERMEDIATE);
  67. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_FINGER_PHALANX_DISTAL);
  68. BIND_ENUM_CONSTANT(HAND_JOINT_MIDDLE_FINGER_TIP);
  69. BIND_ENUM_CONSTANT(HAND_JOINT_RING_FINGER_METACARPAL);
  70. BIND_ENUM_CONSTANT(HAND_JOINT_RING_FINGER_PHALANX_PROXIMAL);
  71. BIND_ENUM_CONSTANT(HAND_JOINT_RING_FINGER_PHALANX_INTERMEDIATE);
  72. BIND_ENUM_CONSTANT(HAND_JOINT_RING_FINGER_PHALANX_DISTAL);
  73. BIND_ENUM_CONSTANT(HAND_JOINT_RING_FINGER_TIP);
  74. BIND_ENUM_CONSTANT(HAND_JOINT_PINKY_FINGER_METACARPAL);
  75. BIND_ENUM_CONSTANT(HAND_JOINT_PINKY_FINGER_PHALANX_PROXIMAL);
  76. BIND_ENUM_CONSTANT(HAND_JOINT_PINKY_FINGER_PHALANX_INTERMEDIATE);
  77. BIND_ENUM_CONSTANT(HAND_JOINT_PINKY_FINGER_PHALANX_DISTAL);
  78. BIND_ENUM_CONSTANT(HAND_JOINT_PINKY_FINGER_TIP);
  79. BIND_ENUM_CONSTANT(HAND_JOINT_MAX);
  80. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_ORIENTATION_VALID);
  81. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_ORIENTATION_TRACKED);
  82. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_POSITION_VALID);
  83. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_POSITION_TRACKED);
  84. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_LINEAR_VELOCITY_VALID);
  85. BIND_BITFIELD_FLAG(HAND_JOINT_FLAG_ANGULAR_VELOCITY_VALID);
  86. }
  87. void XRHandTracker::set_tracker_type(XRServer::TrackerType p_type) {
  88. ERR_FAIL_COND_MSG(p_type != XRServer::TRACKER_HAND, "XRHandTracker must be of type TRACKER_HAND.");
  89. }
  90. void XRHandTracker::set_tracker_hand(const XRPositionalTracker::TrackerHand p_hand) {
  91. ERR_FAIL_COND_MSG(p_hand != TRACKER_HAND_LEFT && p_hand != TRACKER_HAND_RIGHT, "XRHandTracker must specify hand.");
  92. tracker_hand = p_hand;
  93. }
  94. void XRHandTracker::set_has_tracking_data(bool p_has_tracking_data) {
  95. has_tracking_data = p_has_tracking_data;
  96. }
  97. bool XRHandTracker::get_has_tracking_data() const {
  98. return has_tracking_data;
  99. }
  100. void XRHandTracker::set_hand_tracking_source(XRHandTracker::HandTrackingSource p_source) {
  101. hand_tracking_source = p_source;
  102. }
  103. XRHandTracker::HandTrackingSource XRHandTracker::get_hand_tracking_source() const {
  104. return hand_tracking_source;
  105. }
  106. void XRHandTracker::set_hand_joint_flags(XRHandTracker::HandJoint p_joint, BitField<XRHandTracker::HandJointFlags> p_flags) {
  107. ERR_FAIL_INDEX(p_joint, HAND_JOINT_MAX);
  108. hand_joint_flags[p_joint] = p_flags;
  109. }
  110. BitField<XRHandTracker::HandJointFlags> XRHandTracker::get_hand_joint_flags(XRHandTracker::HandJoint p_joint) const {
  111. ERR_FAIL_INDEX_V(p_joint, HAND_JOINT_MAX, BitField<HandJointFlags>());
  112. return hand_joint_flags[p_joint];
  113. }
  114. void XRHandTracker::set_hand_joint_transform(XRHandTracker::HandJoint p_joint, const Transform3D &p_transform) {
  115. ERR_FAIL_INDEX(p_joint, HAND_JOINT_MAX);
  116. hand_joint_transforms[p_joint] = p_transform;
  117. }
  118. Transform3D XRHandTracker::get_hand_joint_transform(XRHandTracker::HandJoint p_joint) const {
  119. ERR_FAIL_INDEX_V(p_joint, HAND_JOINT_MAX, Transform3D());
  120. return hand_joint_transforms[p_joint];
  121. }
  122. void XRHandTracker::set_hand_joint_radius(XRHandTracker::HandJoint p_joint, float p_radius) {
  123. ERR_FAIL_INDEX(p_joint, HAND_JOINT_MAX);
  124. hand_joint_radii[p_joint] = p_radius;
  125. }
  126. float XRHandTracker::get_hand_joint_radius(XRHandTracker::HandJoint p_joint) const {
  127. ERR_FAIL_INDEX_V(p_joint, HAND_JOINT_MAX, 0.0);
  128. return hand_joint_radii[p_joint];
  129. }
  130. void XRHandTracker::set_hand_joint_linear_velocity(XRHandTracker::HandJoint p_joint, const Vector3 &p_velocity) {
  131. ERR_FAIL_INDEX(p_joint, HAND_JOINT_MAX);
  132. hand_joint_linear_velocities[p_joint] = p_velocity;
  133. }
  134. Vector3 XRHandTracker::get_hand_joint_linear_velocity(XRHandTracker::HandJoint p_joint) const {
  135. ERR_FAIL_INDEX_V(p_joint, HAND_JOINT_MAX, Vector3());
  136. return hand_joint_linear_velocities[p_joint];
  137. }
  138. void XRHandTracker::set_hand_joint_angular_velocity(XRHandTracker::HandJoint p_joint, const Vector3 &p_velocity) {
  139. ERR_FAIL_INDEX(p_joint, HAND_JOINT_MAX);
  140. hand_joint_angular_velocities[p_joint] = p_velocity;
  141. }
  142. Vector3 XRHandTracker::get_hand_joint_angular_velocity(XRHandTracker::HandJoint p_joint) const {
  143. ERR_FAIL_INDEX_V(p_joint, HAND_JOINT_MAX, Vector3());
  144. return hand_joint_angular_velocities[p_joint];
  145. }
  146. XRHandTracker::XRHandTracker() {
  147. type = XRServer::TRACKER_HAND;
  148. tracker_hand = TRACKER_HAND_LEFT;
  149. }