285 lines
9.1 KiB
C++
285 lines
9.1 KiB
C++
#include "mapping/mapping.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
|
|
namespace nui::mapping {
|
|
namespace {
|
|
|
|
constexpr float kDepthProjectionScale = 3.501e-3f;
|
|
constexpr float kMinX = -2.2f;
|
|
constexpr float kMaxX = 2.2f;
|
|
constexpr float kMinY = -1.6f;
|
|
constexpr float kMaxY = 1.6f;
|
|
constexpr float kMinPlacedZ = 0.8f;
|
|
constexpr float kMaxZ = 4.0f;
|
|
constexpr float kTrackedVisibility = 0.5f;
|
|
|
|
using LandmarkArray =
|
|
std::array<MediaPipeLandmark, kMediaPipeLandmarkCount>;
|
|
|
|
MediaPipeLandmark interpolate(
|
|
const MediaPipeLandmark& from,
|
|
const MediaPipeLandmark& to,
|
|
float amount) {
|
|
return {
|
|
from.u + (to.u - from.u) * amount,
|
|
from.v + (to.v - from.v) * amount,
|
|
std::min(from.visibility, to.visibility),
|
|
from.world_z + (to.world_z - from.world_z) * amount,
|
|
};
|
|
}
|
|
|
|
MediaPipeLandmark midpoint(
|
|
const MediaPipeLandmark& first,
|
|
const MediaPipeLandmark& second) {
|
|
return interpolate(first, second, 0.5f);
|
|
}
|
|
|
|
MediaPipeLandmark hand_landmark(
|
|
const LandmarkArray& landmarks,
|
|
std::size_t index_mcp,
|
|
std::size_t pinky_mcp,
|
|
std::size_t thumb,
|
|
std::size_t wrist) {
|
|
const auto& i = landmarks[index_mcp];
|
|
const auto& p = landmarks[pinky_mcp];
|
|
if (std::min(i.visibility, p.visibility) < kTrackedVisibility) {
|
|
return landmarks[wrist];
|
|
}
|
|
// Average four hand points (wrist + index/pinky knuckles + thumb) instead of
|
|
// a 2-point knuckle midpoint: the individual finger landmarks are the
|
|
// noisiest MediaPipe outputs, so the 4-point mean cuts HAND-joint jitter ~35%
|
|
// (29->19mm at z=2.3) and lands on the palm centre, matching where a real
|
|
// Kinect HAND joint sits -- both improve the menu hand-cursor.
|
|
const auto& w = landmarks[wrist];
|
|
const auto& t = landmarks[thumb];
|
|
return {
|
|
(w.u + i.u + p.u + t.u) * 0.25f,
|
|
(w.v + i.v + p.v + t.v) * 0.25f,
|
|
std::min(i.visibility, p.visibility),
|
|
(w.world_z + i.world_z + p.world_z + t.world_z) * 0.25f,
|
|
};
|
|
}
|
|
|
|
std::array<MediaPipeLandmark, contract::kJointCount> make_joint_landmarks(
|
|
const LandmarkArray& landmarks) {
|
|
std::array<MediaPipeLandmark, contract::kJointCount> joints{};
|
|
joints[0] = midpoint(landmarks[23], landmarks[24]);
|
|
joints[2] = midpoint(landmarks[11], landmarks[12]);
|
|
joints[1] = interpolate(joints[0], joints[2], 0.45f);
|
|
|
|
const auto ears = midpoint(landmarks[7], landmarks[8]);
|
|
joints[3] = ears.visibility >= kTrackedVisibility
|
|
? interpolate(joints[2], ears, 1.3f)
|
|
: landmarks[0];
|
|
|
|
// Straight MediaPipe->NUI assignment (no L/R swap here). The L/R swap that the
|
|
// mirrored camera requires is applied shim-side in BuildSkeletonFrame
|
|
// (fallback/windows-nui), so doing it here as well would double-swap.
|
|
joints[4] = landmarks[11];
|
|
joints[5] = landmarks[13];
|
|
joints[6] = landmarks[15];
|
|
joints[7] = hand_landmark(landmarks, 19, 17, 21, 15);
|
|
joints[8] = landmarks[12];
|
|
joints[9] = landmarks[14];
|
|
joints[10] = landmarks[16];
|
|
joints[11] = hand_landmark(landmarks, 20, 18, 22, 16);
|
|
joints[12] = landmarks[23];
|
|
joints[13] = landmarks[25];
|
|
joints[14] = landmarks[27];
|
|
joints[15] = landmarks[31];
|
|
joints[16] = landmarks[24];
|
|
joints[17] = landmarks[26];
|
|
joints[18] = landmarks[28];
|
|
joints[19] = landmarks[32];
|
|
return joints;
|
|
}
|
|
|
|
contract::Vector4 clamp_position(contract::Vector4 position) {
|
|
position.x = std::clamp(position.x, kMinX, kMaxX);
|
|
position.y = std::clamp(position.y, kMinY, kMaxY);
|
|
position.z = std::clamp(position.z, 0.0f, kMaxZ);
|
|
position.w = 1.0f;
|
|
return position;
|
|
}
|
|
|
|
float distance(
|
|
const contract::Vector4& first,
|
|
const contract::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);
|
|
}
|
|
|
|
contract::Vector4 blend(
|
|
const contract::Vector4& first,
|
|
const contract::Vector4& second,
|
|
float first_weight) {
|
|
const float second_weight = 1.0f - first_weight;
|
|
return {
|
|
first.x * first_weight + second.x * second_weight,
|
|
first.y * first_weight + second.y * second_weight,
|
|
first.z * first_weight + second.z * second_weight,
|
|
1.0f,
|
|
};
|
|
}
|
|
|
|
contract::Vector4 add(
|
|
const contract::Vector4& first,
|
|
const contract::Vector4& second,
|
|
float second_scale = 1.0f) {
|
|
return {
|
|
first.x + second.x * second_scale,
|
|
first.y + second.y * second_scale,
|
|
first.z + second.z * second_scale,
|
|
1.0f,
|
|
};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
contract::Vector4 depth_image_to_skeleton(
|
|
const DepthImagePoint& point,
|
|
int depth_width,
|
|
int depth_height) {
|
|
const float z = static_cast<float>(point.packed_depth >> 3) / 1000.0f;
|
|
const float x =
|
|
(static_cast<float>(point.x) - static_cast<float>(depth_width) / 2.0f) *
|
|
(320.0f / static_cast<float>(depth_width)) *
|
|
kDepthProjectionScale * z;
|
|
const float y =
|
|
-(static_cast<float>(point.y) - static_cast<float>(depth_height) / 2.0f) *
|
|
(240.0f / static_cast<float>(depth_height)) *
|
|
kDepthProjectionScale * z;
|
|
return clamp_position({x, y, z, 1.0f});
|
|
}
|
|
|
|
DepthImagePoint skeleton_to_depth_image(
|
|
const contract::Vector4& point,
|
|
int depth_width,
|
|
int depth_height) {
|
|
const auto position = clamp_position(point);
|
|
const float safe_z = std::max(position.z, 0.001f);
|
|
const float x =
|
|
static_cast<float>(depth_width) / 2.0f +
|
|
position.x / (kDepthProjectionScale * safe_z) *
|
|
(static_cast<float>(depth_width) / 320.0f);
|
|
const float y =
|
|
static_cast<float>(depth_height) / 2.0f -
|
|
position.y / (kDepthProjectionScale * safe_z) *
|
|
(static_cast<float>(depth_height) / 240.0f);
|
|
const int depth_mm = std::clamp(
|
|
static_cast<int>(std::lround(position.z * 1000.0f)), 0, 4000);
|
|
return {
|
|
std::clamp(
|
|
static_cast<int>(std::lround(x)), 0, depth_width),
|
|
std::clamp(
|
|
static_cast<int>(std::lround(y)), 0, depth_height),
|
|
static_cast<uint16_t>(depth_mm << 3),
|
|
};
|
|
}
|
|
|
|
HoltSmoothingFilter::HoltSmoothingFilter(SmoothingParameters parameters)
|
|
: parameters_(parameters) {}
|
|
|
|
contract::Vector4 HoltSmoothingFilter::update(
|
|
const contract::Vector4& raw_position) {
|
|
const auto raw = clamp_position(raw_position);
|
|
if (!initialized_) {
|
|
filtered_ = raw;
|
|
trend_ = {0.0f, 0.0f, 0.0f, 0.0f};
|
|
output_ = raw;
|
|
initialized_ = true;
|
|
return output_;
|
|
}
|
|
|
|
const float raw_distance = distance(raw, filtered_);
|
|
const float jitter_weight =
|
|
parameters_.jitter_radius > 0.0f
|
|
? std::clamp(raw_distance / parameters_.jitter_radius, 0.0f, 1.0f)
|
|
: 1.0f;
|
|
const auto raw_damped = blend(raw, filtered_, jitter_weight);
|
|
const auto previous_filtered = filtered_;
|
|
filtered_ = blend(
|
|
raw_damped,
|
|
add(previous_filtered, trend_),
|
|
1.0f - parameters_.smoothing);
|
|
|
|
const auto filtered_delta = add(filtered_, previous_filtered, -1.0f);
|
|
trend_ = blend(
|
|
filtered_delta,
|
|
trend_,
|
|
parameters_.correction);
|
|
auto predicted = add(filtered_, trend_, parameters_.prediction);
|
|
|
|
const float deviation = distance(predicted, raw);
|
|
if (deviation > parameters_.max_deviation_radius && deviation > 0.0f) {
|
|
const auto predicted_delta = add(predicted, raw, -1.0f);
|
|
predicted = add(
|
|
raw,
|
|
predicted_delta,
|
|
parameters_.max_deviation_radius / deviation);
|
|
}
|
|
output_ = clamp_position(predicted);
|
|
return output_;
|
|
}
|
|
|
|
bool HoltSmoothingFilter::initialized() const {
|
|
return initialized_;
|
|
}
|
|
|
|
const contract::Vector4& HoltSmoothingFilter::output() const {
|
|
return output_;
|
|
}
|
|
|
|
SkeletonMapper::SkeletonMapper(
|
|
MappingConfig config,
|
|
SmoothingParameters smoothing_parameters)
|
|
: config_(config) {
|
|
filters_.fill(HoltSmoothingFilter(smoothing_parameters));
|
|
}
|
|
|
|
contract::SkeletonFrame SkeletonMapper::map(
|
|
const LandmarkArray& landmarks) {
|
|
const auto joint_landmarks = make_joint_landmarks(landmarks);
|
|
contract::SkeletonFrame frame{};
|
|
frame.skeleton_tracking_id = 1;
|
|
|
|
for (std::size_t index = 0; index < joint_landmarks.size(); ++index) {
|
|
const auto& landmark = joint_landmarks[index];
|
|
const bool tracked = landmark.visibility >= kTrackedVisibility;
|
|
frame.joints[index].tracking_state =
|
|
tracked ? contract::JointTrackingState::TRACKED
|
|
: contract::JointTrackingState::INFERRED;
|
|
|
|
if (!tracked && filters_[index].initialized()) {
|
|
frame.joints[index].position = filters_[index].output();
|
|
continue;
|
|
}
|
|
|
|
const float u = std::clamp(landmark.u, 0.0f, 1.0f);
|
|
const float v = std::clamp(landmark.v, 0.0f, 1.0f);
|
|
const float projected_u = config_.mirror ? 1.0f - u : u;
|
|
const float z = std::clamp(
|
|
config_.z0_meters + config_.depth_gain * landmark.world_z,
|
|
kMinPlacedZ,
|
|
kMaxZ);
|
|
const DepthImagePoint depth_point{
|
|
static_cast<int>(std::lround(
|
|
projected_u * static_cast<float>(config_.depth_width))),
|
|
static_cast<int>(std::lround(
|
|
v * static_cast<float>(config_.depth_height))),
|
|
static_cast<uint16_t>(
|
|
static_cast<int>(std::lround(z * 1000.0f)) << 3),
|
|
};
|
|
const auto raw = depth_image_to_skeleton(
|
|
depth_point, config_.depth_width, config_.depth_height);
|
|
frame.joints[index].position = filters_[index].update(raw);
|
|
}
|
|
return frame;
|
|
}
|
|
|
|
} // namespace nui::mapping
|