pose_controller_test.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "render/humanoid/humanoid_specs.h"
  2. #include "render/humanoid/pose_controller.h"
  3. #include "render/humanoid/rig.h"
  4. #include <QVector3D>
  5. #include <cmath>
  6. #include <gtest/gtest.h>
  7. using namespace Render::GL;
  8. class HumanoidPoseControllerTest : public ::testing::Test {
  9. protected:
  10. void SetUp() override {
  11. // Initialize a default pose with basic standing configuration
  12. pose = HumanoidPose{};
  13. pose.headPos = QVector3D(0.0F, 1.70F, 0.0F);
  14. pose.headR = 0.10F;
  15. pose.neck_base = QVector3D(0.0F, 1.49F, 0.0F);
  16. pose.shoulderL = QVector3D(-0.21F, 1.45F, 0.0F);
  17. pose.shoulderR = QVector3D(0.21F, 1.45F, 0.0F);
  18. pose.pelvisPos = QVector3D(0.0F, 0.95F, 0.0F);
  19. pose.handL = QVector3D(-0.05F, 1.50F, 0.55F);
  20. pose.hand_r = QVector3D(0.15F, 1.60F, 0.20F);
  21. pose.elbowL = QVector3D(-0.15F, 1.30F, 0.25F);
  22. pose.elbowR = QVector3D(0.25F, 1.35F, 0.10F);
  23. pose.knee_l = QVector3D(-0.10F, 0.44F, 0.05F);
  24. pose.knee_r = QVector3D(0.10F, 0.44F, -0.05F);
  25. pose.footL = QVector3D(-0.14F, 0.022F, 0.06F);
  26. pose.foot_r = QVector3D(0.14F, 0.022F, -0.06F);
  27. pose.footYOffset = 0.022F;
  28. // Initialize animation context with default idle state
  29. anim_ctx = HumanoidAnimationContext{};
  30. anim_ctx.inputs.time = 0.0F;
  31. anim_ctx.inputs.isMoving = false;
  32. anim_ctx.inputs.is_attacking = false;
  33. anim_ctx.variation = VariationParams::fromSeed(12345);
  34. anim_ctx.gait.state = HumanoidMotionState::Idle;
  35. }
  36. HumanoidPose pose;
  37. HumanoidAnimationContext anim_ctx;
  38. // Helper to check if a position is approximately equal
  39. bool approxEqual(const QVector3D &a, const QVector3D &b,
  40. float epsilon = 0.01F) {
  41. return std::abs(a.x() - b.x()) < epsilon &&
  42. std::abs(a.y() - b.y()) < epsilon &&
  43. std::abs(a.z() - b.z()) < epsilon;
  44. }
  45. };
  46. TEST_F(HumanoidPoseControllerTest, ConstructorInitializesCorrectly) {
  47. HumanoidPoseController controller(pose, anim_ctx);
  48. // Constructor should not modify the pose
  49. EXPECT_FLOAT_EQ(pose.headPos.y(), 1.70F);
  50. EXPECT_FLOAT_EQ(pose.pelvisPos.y(), 0.95F);
  51. }
  52. TEST_F(HumanoidPoseControllerTest, StandIdleDoesNotModifyPose) {
  53. HumanoidPoseController controller(pose, anim_ctx);
  54. QVector3D const original_pelvis = pose.pelvisPos;
  55. QVector3D const original_shoulder_l = pose.shoulderL;
  56. controller.standIdle();
  57. // standIdle should be a no-op, keeping pose unchanged
  58. EXPECT_TRUE(approxEqual(pose.pelvisPos, original_pelvis));
  59. EXPECT_TRUE(approxEqual(pose.shoulderL, original_shoulder_l));
  60. }
  61. TEST_F(HumanoidPoseControllerTest, KneelLowersPelvis) {
  62. HumanoidPoseController controller(pose, anim_ctx);
  63. float const original_pelvis_y = pose.pelvisPos.y();
  64. controller.kneel(0.5F);
  65. // Kneeling should lower the pelvis
  66. EXPECT_LT(pose.pelvisPos.y(), original_pelvis_y);
  67. // Pelvis should be lowered by approximately depth * 0.40F
  68. float const expected_offset = 0.5F * 0.40F;
  69. EXPECT_NEAR(pose.pelvisPos.y(), HumanProportions::WAIST_Y - expected_offset,
  70. 0.05F);
  71. }
  72. TEST_F(HumanoidPoseControllerTest, KneelFullDepthTouchesGroundWithKnee) {
  73. HumanoidPoseController controller(pose, anim_ctx);
  74. controller.kneel(1.0F);
  75. // At full kneel, left knee should be very close to ground
  76. EXPECT_NEAR(pose.knee_l.y(), HumanProportions::GROUND_Y + 0.07F, 0.02F);
  77. // Pelvis should be lowered significantly
  78. EXPECT_LT(pose.pelvisPos.y(), HumanProportions::WAIST_Y - 0.35F);
  79. }
  80. TEST_F(HumanoidPoseControllerTest, KneelZeroDepthKeepsStanding) {
  81. HumanoidPoseController controller(pose, anim_ctx);
  82. float const original_pelvis_y = pose.pelvisPos.y();
  83. controller.kneel(0.0F);
  84. // Zero depth should keep pelvis at original height
  85. EXPECT_NEAR(pose.pelvisPos.y(), original_pelvis_y, 0.01F);
  86. }
  87. TEST_F(HumanoidPoseControllerTest, LeanMovesUpperBody) {
  88. HumanoidPoseController controller(pose, anim_ctx);
  89. QVector3D const original_shoulder_l = pose.shoulderL;
  90. QVector3D const original_shoulder_r = pose.shoulderR;
  91. QVector3D const lean_direction(0.0F, 0.0F, 1.0F); // Forward
  92. controller.lean(lean_direction, 0.5F);
  93. // Shoulders should move forward when leaning forward
  94. EXPECT_GT(pose.shoulderL.z(), original_shoulder_l.z());
  95. EXPECT_GT(pose.shoulderR.z(), original_shoulder_r.z());
  96. }
  97. TEST_F(HumanoidPoseControllerTest, LeanZeroAmountNoChange) {
  98. HumanoidPoseController controller(pose, anim_ctx);
  99. QVector3D const original_shoulder_l = pose.shoulderL;
  100. QVector3D const lean_direction(1.0F, 0.0F, 0.0F); // Right
  101. controller.lean(lean_direction, 0.0F);
  102. // Zero amount should keep shoulders unchanged
  103. EXPECT_TRUE(approxEqual(pose.shoulderL, original_shoulder_l));
  104. }
  105. TEST_F(HumanoidPoseControllerTest, PlaceHandAtSetsHandPosition) {
  106. HumanoidPoseController controller(pose, anim_ctx);
  107. QVector3D const target_position(0.30F, 1.20F, 0.80F);
  108. controller.placeHandAt(false, target_position); // Right hand
  109. // Hand should be at target position
  110. EXPECT_TRUE(approxEqual(pose.hand_r, target_position));
  111. }
  112. TEST_F(HumanoidPoseControllerTest, PlaceHandAtComputesElbow) {
  113. HumanoidPoseController controller(pose, anim_ctx);
  114. QVector3D const target_position(0.30F, 1.20F, 0.80F);
  115. QVector3D const original_elbow = pose.elbowR;
  116. controller.placeHandAt(false, target_position); // Right hand
  117. // Elbow should be recomputed (different from original)
  118. EXPECT_FALSE(approxEqual(pose.elbowR, original_elbow));
  119. // Elbow should be between shoulder and hand
  120. float const shoulder_to_elbow_dist = (pose.elbowR - pose.shoulderR).length();
  121. float const elbow_to_hand_dist = (target_position - pose.elbowR).length();
  122. EXPECT_GT(shoulder_to_elbow_dist, 0.0F);
  123. EXPECT_GT(elbow_to_hand_dist, 0.0F);
  124. }
  125. TEST_F(HumanoidPoseControllerTest, SolveElbowIKReturnsValidPosition) {
  126. HumanoidPoseController controller(pose, anim_ctx);
  127. QVector3D const shoulder = pose.shoulderR;
  128. QVector3D const hand(0.35F, 1.15F, 0.75F);
  129. QVector3D const outward_dir(1.0F, 0.0F, 0.0F);
  130. QVector3D const elbow = controller.solveElbowIK(
  131. false, shoulder, hand, outward_dir, 0.45F, 0.15F, 0.0F, 1.0F);
  132. // Elbow should be somewhere between shoulder and hand
  133. EXPECT_GT(elbow.length(), 0.0F);
  134. // Distance from shoulder to elbow should be reasonable
  135. float const shoulder_elbow_dist = (elbow - shoulder).length();
  136. EXPECT_GT(shoulder_elbow_dist, 0.05F);
  137. EXPECT_LT(shoulder_elbow_dist, 0.50F);
  138. }
  139. TEST_F(HumanoidPoseControllerTest, SolveKneeIKReturnsValidPosition) {
  140. HumanoidPoseController controller(pose, anim_ctx);
  141. QVector3D const hip(0.10F, 0.93F, 0.0F);
  142. QVector3D const foot(0.10F, 0.0F, 0.05F);
  143. float const height_scale = 1.0F;
  144. QVector3D const knee = controller.solveKneeIK(false, hip, foot, height_scale);
  145. // Knee should be between hip and foot (in Y)
  146. EXPECT_LT(knee.y(), hip.y());
  147. EXPECT_GT(knee.y(), foot.y());
  148. // Knee should not be below ground
  149. EXPECT_GE(knee.y(), HumanProportions::GROUND_Y);
  150. }
  151. TEST_F(HumanoidPoseControllerTest, SolveKneeIKPreventsGroundPenetration) {
  152. HumanoidPoseController controller(pose, anim_ctx);
  153. // Set up a scenario where IK would put knee below ground
  154. QVector3D const hip(0.0F, 0.30F, 0.0F); // Very low hip
  155. QVector3D const foot(0.50F, 0.0F, 0.50F); // Far foot
  156. float const height_scale = 1.0F;
  157. QVector3D const knee = controller.solveKneeIK(true, hip, foot, height_scale);
  158. // Knee should be at or above the floor threshold
  159. float const min_knee_y = HumanProportions::GROUND_Y + pose.footYOffset * 0.5F;
  160. EXPECT_GE(knee.y(), min_knee_y - 0.001F); // Small epsilon for floating point
  161. }
  162. TEST_F(HumanoidPoseControllerTest, PlaceHandAtLeftHandWorks) {
  163. HumanoidPoseController controller(pose, anim_ctx);
  164. QVector3D const target_position(-0.40F, 1.30F, 0.60F);
  165. controller.placeHandAt(true, target_position); // Left hand
  166. // Left hand should be at target position
  167. EXPECT_TRUE(approxEqual(pose.handL, target_position));
  168. // Left elbow should be computed
  169. EXPECT_GT((pose.elbowL - pose.shoulderL).length(), 0.0F);
  170. }
  171. TEST_F(HumanoidPoseControllerTest, KneelClampsBounds) {
  172. HumanoidPoseController controller(pose, anim_ctx);
  173. // Test clamping of depth > 1.0
  174. controller.kneel(1.5F);
  175. float const max_kneel_pelvis_y = pose.pelvisPos.y();
  176. // Reset pose
  177. SetUp();
  178. HumanoidPoseController controller2(pose, anim_ctx);
  179. // Test depth = 1.0
  180. controller2.kneel(1.0F);
  181. // Should be same as clamped 1.5F
  182. EXPECT_NEAR(pose.pelvisPos.y(), max_kneel_pelvis_y, 0.001F);
  183. }
  184. TEST_F(HumanoidPoseControllerTest, LeanClampsBounds) {
  185. HumanoidPoseController controller(pose, anim_ctx);
  186. QVector3D const lean_direction(0.0F, 0.0F, 1.0F);
  187. // Test clamping of amount > 1.0
  188. controller.lean(lean_direction, 1.5F);
  189. float const max_lean_z = pose.shoulderL.z();
  190. // Reset pose
  191. SetUp();
  192. HumanoidPoseController controller2(pose, anim_ctx);
  193. // Test amount = 1.0
  194. controller2.lean(lean_direction, 1.0F);
  195. // Should be same as clamped 1.5F
  196. EXPECT_NEAR(pose.shoulderL.z(), max_lean_z, 0.001F);
  197. }