Files
kinect/core/mapping/mapping.h
T

92 lines
2.3 KiB
C++

#ifndef NUI_CORE_MAPPING_H
#define NUI_CORE_MAPPING_H
#include <array>
#include <cstddef>
#include <cstdint>
#include "contract/contract.h"
namespace nui::mapping {
constexpr std::size_t kMediaPipeLandmarkCount = 33;
constexpr int kDefaultDepthWidth = 640;
constexpr int kDefaultDepthHeight = 480;
struct DepthImagePoint {
int x;
int y;
uint16_t packed_depth;
};
struct MediaPipeLandmark {
float u;
float v;
float visibility;
float world_z;
};
struct MappingConfig {
int depth_width = kDefaultDepthWidth;
int depth_height = kDefaultDepthHeight;
float z0_meters = 2.5f;
float depth_gain = 0.0f;
bool mirror = true;
};
struct SmoothingParameters {
// Tuned for a stable menu hand-cursor: the game maps HAND relative to the
// SHOULDER, so reference jitter (MediaPipe shoulder drift during arm motion)
// makes the cursor jump. Higher smoothing + a wider jitter dead-zone steady
// it; lower prediction stops velocity overshoot from amplifying jitter.
float smoothing = 0.7f;
float correction = 0.3f;
float prediction = 0.2f;
float jitter_radius = 0.09f;
float max_deviation_radius = 0.13f;
};
contract::Vector4 depth_image_to_skeleton(
const DepthImagePoint& point,
int depth_width = kDefaultDepthWidth,
int depth_height = kDefaultDepthHeight);
DepthImagePoint skeleton_to_depth_image(
const contract::Vector4& point,
int depth_width = kDefaultDepthWidth,
int depth_height = kDefaultDepthHeight);
class HoltSmoothingFilter {
public:
explicit HoltSmoothingFilter(SmoothingParameters parameters = {});
contract::Vector4 update(const contract::Vector4& raw);
bool initialized() const;
const contract::Vector4& output() const;
private:
SmoothingParameters parameters_;
contract::Vector4 filtered_{};
contract::Vector4 trend_{};
contract::Vector4 output_{};
bool initialized_ = false;
};
class SkeletonMapper {
public:
explicit SkeletonMapper(
MappingConfig config = {},
SmoothingParameters smoothing_parameters = {});
contract::SkeletonFrame map(
const std::array<MediaPipeLandmark, kMediaPipeLandmarkCount>& landmarks);
private:
MappingConfig config_;
std::array<HoltSmoothingFilter, contract::kJointCount> filters_;
};
} // namespace nui::mapping
#endif // NUI_CORE_MAPPING_H