205 lines
7.2 KiB
C++
205 lines
7.2 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <array>
|
|
#include <cmath>
|
|
#include <string>
|
|
|
|
#include "mapping/mapping.h"
|
|
|
|
namespace {
|
|
|
|
using nui::contract::JointTrackingState;
|
|
using nui::contract::Vector4;
|
|
using nui::mapping::DepthImagePoint;
|
|
using nui::mapping::MediaPipeLandmark;
|
|
|
|
float distance(const Vector4& first, const Vector4& second) {
|
|
const float x = first.x - second.x;
|
|
const float y = first.y - second.y;
|
|
const float z = first.z - second.z;
|
|
return std::sqrt(x * x + y * y + z * z);
|
|
}
|
|
|
|
std::array<MediaPipeLandmark, nui::mapping::kMediaPipeLandmarkCount>
|
|
golden_landmarks() {
|
|
std::array<MediaPipeLandmark, nui::mapping::kMediaPipeLandmarkCount>
|
|
landmarks{};
|
|
for (std::size_t index = 0; index < landmarks.size(); ++index) {
|
|
landmarks[index] = {
|
|
0.2f + static_cast<float>(index) * 0.015f,
|
|
0.1f + static_cast<float>(index) * 0.02f,
|
|
1.0f,
|
|
0.0f,
|
|
};
|
|
}
|
|
return landmarks;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(mapping, ForwardInverseRoundTripStaysBelowOneMillimeter) {
|
|
const std::array<DepthImagePoint, 5> depth_points{{
|
|
{0, 0, static_cast<uint16_t>(800 << 3)},
|
|
{137, 91, static_cast<uint16_t>(1250 << 3)},
|
|
{320, 240, static_cast<uint16_t>(2500 << 3)},
|
|
{434, 171, static_cast<uint16_t>(2500 << 3)},
|
|
{630, 468, static_cast<uint16_t>(4000 << 3)},
|
|
}};
|
|
|
|
for (const auto& depth_point : depth_points) {
|
|
const auto skeleton =
|
|
nui::mapping::depth_image_to_skeleton(depth_point);
|
|
const auto projected =
|
|
nui::mapping::skeleton_to_depth_image(skeleton);
|
|
const auto round_tripped =
|
|
nui::mapping::depth_image_to_skeleton(projected);
|
|
|
|
SCOPED_TRACE(
|
|
"depth point " + std::to_string(depth_point.x) + "," +
|
|
std::to_string(depth_point.y));
|
|
EXPECT_LT(distance(skeleton, round_tripped), 0.001f);
|
|
EXPECT_EQ(projected.x, depth_point.x);
|
|
EXPECT_EQ(projected.y, depth_point.y);
|
|
EXPECT_EQ(projected.packed_depth, depth_point.packed_depth);
|
|
}
|
|
}
|
|
|
|
TEST(mapping, LandmarkFixtureMapsAllTwentyJointsToGoldenPositions) {
|
|
// L/R joints are swapped vs MediaPipe labels (see make_joint_landmarks): the
|
|
// tracker mirrors the camera, so the player's right side comes from
|
|
// MediaPipe's left-side landmarks. NUI LEFT joints (4-7,12-15) therefore take
|
|
// the MediaPipe right-side fixture values and NUI RIGHT joints (8-11,16-19)
|
|
// take the left-side values.
|
|
const std::array<Vector4, nui::contract::kJointCount> expected{{
|
|
{0.14879250f, -0.14879250f, 2.5f, 1.0f},
|
|
{-0.07877250f, 0.07877250f, 2.5f, 1.0f},
|
|
{-0.35885250f, 0.35885250f, 2.5f, 1.0f},
|
|
{-0.57766500f, 0.57766500f, 2.5f, 1.0f},
|
|
{-0.33697125f, 0.33697125f, 2.5f, 1.0f},
|
|
{-0.25382250f, 0.25382250f, 2.5f, 1.0f},
|
|
{-0.16629750f, 0.16629750f, 2.5f, 1.0f},
|
|
{-0.04376250f, 0.04376250f, 2.5f, 1.0f},
|
|
{-0.37635750f, 0.37635750f, 2.5f, 1.0f},
|
|
{-0.29320875f, 0.29320875f, 2.5f, 1.0f},
|
|
{-0.21006000f, 0.21006000f, 2.5f, 1.0f},
|
|
{-0.08314875f, 0.08314875f, 2.5f, 1.0f},
|
|
{0.16629750f, -0.16629750f, 2.5f, 1.0f},
|
|
{0.25382250f, -0.25382250f, 2.5f, 1.0f},
|
|
{0.33697125f, -0.33697125f, 2.5f, 1.0f},
|
|
{0.50326875f, -0.50326875f, 2.5f, 1.0f},
|
|
{0.12691125f, -0.12691125f, 2.5f, 1.0f},
|
|
{0.21006000f, -0.21006000f, 2.5f, 1.0f},
|
|
{0.29320875f, -0.29320875f, 2.5f, 1.0f},
|
|
{0.46388250f, -0.46388250f, 2.5f, 1.0f},
|
|
}};
|
|
nui::mapping::MappingConfig config{};
|
|
config.mirror = false;
|
|
nui::mapping::SkeletonMapper mapper(config);
|
|
|
|
const auto frame = mapper.map(golden_landmarks());
|
|
|
|
for (std::size_t index = 0; index < expected.size(); ++index) {
|
|
SCOPED_TRACE("joint " + std::to_string(index));
|
|
EXPECT_NEAR(frame.joints[index].position.x, expected[index].x, 1e-6f);
|
|
EXPECT_NEAR(frame.joints[index].position.y, expected[index].y, 1e-6f);
|
|
EXPECT_FLOAT_EQ(frame.joints[index].position.z, expected[index].z);
|
|
EXPECT_EQ(
|
|
frame.joints[index].tracking_state,
|
|
JointTrackingState::TRACKED);
|
|
}
|
|
}
|
|
|
|
TEST(mapping, GlobalMirrorIsAppliedExactlyOnce) {
|
|
auto landmarks = golden_landmarks();
|
|
nui::mapping::MappingConfig unmirrored_config{};
|
|
unmirrored_config.mirror = false;
|
|
nui::mapping::SkeletonMapper unmirrored(unmirrored_config);
|
|
nui::mapping::SkeletonMapper mirrored;
|
|
|
|
const auto unmirrored_frame = unmirrored.map(landmarks);
|
|
const auto mirrored_frame = mirrored.map(landmarks);
|
|
|
|
for (std::size_t index = 0; index < nui::contract::kJointCount; ++index) {
|
|
SCOPED_TRACE("joint " + std::to_string(index));
|
|
EXPECT_NEAR(
|
|
mirrored_frame.joints[index].position.x,
|
|
-unmirrored_frame.joints[index].position.x,
|
|
0.0044f);
|
|
EXPECT_FLOAT_EQ(
|
|
mirrored_frame.joints[index].position.y,
|
|
unmirrored_frame.joints[index].position.y);
|
|
}
|
|
}
|
|
|
|
TEST(mapping, OutOfRangeLandmarksAndDepthAreClamped) {
|
|
auto landmarks = golden_landmarks();
|
|
for (auto& landmark : landmarks) {
|
|
landmark.u = 20.0f;
|
|
landmark.v = -20.0f;
|
|
landmark.world_z = 100.0f;
|
|
}
|
|
nui::mapping::MappingConfig config{};
|
|
config.depth_gain = 10.0f;
|
|
config.mirror = false;
|
|
nui::mapping::SkeletonMapper mapper(config);
|
|
|
|
const auto frame = mapper.map(landmarks);
|
|
|
|
for (const auto& joint : frame.joints) {
|
|
EXPECT_GE(joint.position.x, -2.2f);
|
|
EXPECT_LE(joint.position.x, 2.2f);
|
|
EXPECT_GE(joint.position.y, -1.6f);
|
|
EXPECT_LE(joint.position.y, 1.6f);
|
|
EXPECT_GE(joint.position.z, 0.0f);
|
|
EXPECT_LE(joint.position.z, 4.0f);
|
|
EXPECT_FLOAT_EQ(joint.position.z, 4.0f);
|
|
}
|
|
}
|
|
|
|
TEST(mapping, HoltFilterMatchesPrecomputedGoldenSequence) {
|
|
// Explicit params the golden sequence was computed with, so tuning the
|
|
// SmoothingParameters defaults (for cursor stability) does not break this
|
|
// test of the filter math.
|
|
nui::mapping::HoltSmoothingFilter filter(
|
|
nui::mapping::SmoothingParameters{0.6f, 0.4f, 0.4f, 0.04f, 0.07f});
|
|
const std::array<float, 5> raw_x{{0.0f, 0.02f, 0.04f, 0.10f, 0.12f}};
|
|
const std::array<float, 5> expected_x{{
|
|
0.000000000f,
|
|
0.004640000f,
|
|
0.020531200f,
|
|
0.062115328f,
|
|
0.102407352f,
|
|
}};
|
|
|
|
for (std::size_t index = 0; index < raw_x.size(); ++index) {
|
|
const auto output = filter.update({raw_x[index], 0.0f, 2.5f, 1.0f});
|
|
SCOPED_TRACE("sample " + std::to_string(index));
|
|
EXPECT_NEAR(output.x, expected_x[index], 1e-7f);
|
|
EXPECT_FLOAT_EQ(output.y, 0.0f);
|
|
EXPECT_FLOAT_EQ(output.z, 2.5f);
|
|
EXPECT_FLOAT_EQ(output.w, 1.0f);
|
|
}
|
|
}
|
|
|
|
TEST(mapping, OccludedJointIsInferredAndHoldsLastSmoothedPosition) {
|
|
auto landmarks = golden_landmarks();
|
|
nui::mapping::MappingConfig config{};
|
|
config.mirror = false;
|
|
nui::mapping::SkeletonMapper mapper(config);
|
|
const auto visible_frame = mapper.map(landmarks);
|
|
const auto last_wrist = visible_frame.joints[6].position;
|
|
// After the L/R swap, NUI WRIST_LEFT (joint 6) is fed by MediaPipe's
|
|
// RIGHT_WRIST (landmark 16), so occlude 16 to occlude joint 6.
|
|
landmarks[16] = {0.0f, 0.0f, 0.0f, -100.0f};
|
|
|
|
const auto occluded_frame = mapper.map(landmarks);
|
|
|
|
EXPECT_EQ(
|
|
occluded_frame.joints[6].tracking_state,
|
|
JointTrackingState::INFERRED);
|
|
EXPECT_FLOAT_EQ(occluded_frame.joints[6].position.x, last_wrist.x);
|
|
EXPECT_FLOAT_EQ(occluded_frame.joints[6].position.y, last_wrist.y);
|
|
EXPECT_FLOAT_EQ(occluded_frame.joints[6].position.z, last_wrist.z);
|
|
EXPECT_FLOAT_EQ(occluded_frame.joints[6].position.w, 1.0f);
|
|
}
|