167 lines
5.3 KiB
C++
167 lines
5.3 KiB
C++
#include "synth/synth.h"
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <cmath>
|
|
|
|
#include "mapping/mapping.h"
|
|
|
|
namespace nui::synth {
|
|
namespace {
|
|
|
|
constexpr uint16_t kMinBodyDepthMm = 800;
|
|
constexpr uint16_t kMaxBodyDepthMm = 4000;
|
|
|
|
struct ProjectedJoint {
|
|
float x;
|
|
float y;
|
|
float depth_mm;
|
|
};
|
|
|
|
std::array<ProjectedJoint, contract::kJointCount> project_joints(
|
|
const contract::SkeletonFrame& skeleton,
|
|
ImageSize size) {
|
|
std::array<ProjectedJoint, contract::kJointCount> projected{};
|
|
for (std::size_t index = 0; index < projected.size(); ++index) {
|
|
const auto point = mapping::skeleton_to_depth_image(
|
|
skeleton.joints[index].position,
|
|
static_cast<int>(size.width),
|
|
static_cast<int>(size.height));
|
|
projected[index] = {
|
|
static_cast<float>(std::min(point.x, static_cast<int>(size.width) - 1)),
|
|
static_cast<float>(std::min(point.y, static_cast<int>(size.height) - 1)),
|
|
static_cast<float>(std::clamp(
|
|
NuiDepthPixelToDepth(point.packed_depth),
|
|
kMinBodyDepthMm,
|
|
kMaxBodyDepthMm)),
|
|
};
|
|
}
|
|
return projected;
|
|
}
|
|
|
|
uint16_t interpolated_depth(
|
|
uint32_t x,
|
|
uint32_t y,
|
|
const std::array<ProjectedJoint, contract::kJointCount>& joints) {
|
|
float weighted_depth = 0.0f;
|
|
float total_weight = 0.0f;
|
|
for (const auto& joint : joints) {
|
|
const float delta_x = static_cast<float>(x) - joint.x;
|
|
const float delta_y = static_cast<float>(y) - joint.y;
|
|
const float weight = 1.0f / (delta_x * delta_x + delta_y * delta_y + 1.0f);
|
|
weighted_depth += joint.depth_mm * weight;
|
|
total_weight += weight;
|
|
}
|
|
return static_cast<uint16_t>(std::clamp(
|
|
static_cast<int>(std::lround(weighted_depth / total_weight)),
|
|
static_cast<int>(kMinBodyDepthMm),
|
|
static_cast<int>(kMaxBodyDepthMm)));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
uint16_t pack_depth(uint16_t depth_mm, uint8_t player_index) {
|
|
return static_cast<uint16_t>(
|
|
((depth_mm & 0x1FFF) << 3) | (player_index & 0x7));
|
|
}
|
|
|
|
uint16_t NuiDepthPixelToDepth(uint16_t packed_depth) {
|
|
return static_cast<uint16_t>(packed_depth >> 3);
|
|
}
|
|
|
|
uint8_t NuiDepthPixelToPlayerIndex(uint16_t packed_depth) {
|
|
return static_cast<uint8_t>(packed_depth & 0x7);
|
|
}
|
|
|
|
DepthRasterizer::DepthRasterizer(std::size_t temporal_hold_frames)
|
|
: stabilizer_(temporal_hold_frames) {}
|
|
|
|
DepthFrame DepthRasterizer::rasterize(
|
|
const SegmentationMask& mask,
|
|
const contract::SkeletonFrame& skeleton,
|
|
DepthRasterRequest request) {
|
|
stable_mask_ = stabilizer_.update(mask, request.size);
|
|
DepthFrame frame{
|
|
request.size,
|
|
request.size.width * static_cast<uint32_t>(sizeof(uint16_t)),
|
|
std::vector<uint16_t>(pixel_count(request.size), 0),
|
|
};
|
|
if (!has_body(stable_mask_) ||
|
|
request.size.width == 0 || request.size.height == 0) {
|
|
return frame;
|
|
}
|
|
|
|
const auto joints = project_joints(skeleton, request.size);
|
|
const uint8_t player_index =
|
|
static_cast<uint8_t>(std::min<uint8_t>(request.skeleton_slot, 5) + 1);
|
|
for (uint32_t y = 0; y < request.size.height; ++y) {
|
|
for (uint32_t x = 0; x < request.size.width; ++x) {
|
|
const std::size_t index =
|
|
static_cast<std::size_t>(y) * request.size.width + x;
|
|
if (stable_mask_.pixels[index] != 0) {
|
|
frame.pixels[index] =
|
|
pack_depth(interpolated_depth(x, y, joints), player_index);
|
|
}
|
|
}
|
|
}
|
|
return frame;
|
|
}
|
|
|
|
const BinaryMask& DepthRasterizer::stable_mask() const {
|
|
return stable_mask_;
|
|
}
|
|
|
|
BgraFrame passthrough_color(
|
|
const BgraFrame& source,
|
|
ImageSize requested_size) {
|
|
BgraFrame result{
|
|
requested_size,
|
|
requested_size.width * 4,
|
|
std::vector<uint8_t>(pixel_count(requested_size) * 4, 0),
|
|
};
|
|
if (source.size.width == 0 || source.size.height == 0 ||
|
|
source.pixels.size() != pixel_count(source.size) * 4 ||
|
|
requested_size.width == 0 || requested_size.height == 0) {
|
|
return result;
|
|
}
|
|
for (uint32_t y = 0; y < requested_size.height; ++y) {
|
|
const uint32_t source_y =
|
|
static_cast<uint64_t>(y) * source.size.height / requested_size.height;
|
|
for (uint32_t x = 0; x < requested_size.width; ++x) {
|
|
const uint32_t source_x =
|
|
static_cast<uint64_t>(x) * source.size.width / requested_size.width;
|
|
const std::size_t source_index =
|
|
(static_cast<std::size_t>(source_y) * source.size.width + source_x) * 4;
|
|
const std::size_t target_index =
|
|
(static_cast<std::size_t>(y) * requested_size.width + x) * 4;
|
|
std::copy_n(
|
|
source.pixels.begin() + static_cast<std::ptrdiff_t>(source_index),
|
|
4,
|
|
result.pixels.begin() + static_cast<std::ptrdiff_t>(target_index));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
InfraredFrame synthesize_infrared(
|
|
const BgraFrame& source,
|
|
ImageSize requested_size) {
|
|
const auto color = passthrough_color(source, requested_size);
|
|
InfraredFrame result{
|
|
requested_size,
|
|
requested_size.width * static_cast<uint32_t>(sizeof(uint16_t)),
|
|
std::vector<uint16_t>(pixel_count(requested_size), 0),
|
|
};
|
|
for (std::size_t index = 0; index < result.pixels.size(); ++index) {
|
|
const std::size_t color_index = index * 4;
|
|
const uint32_t luminance =
|
|
77u * color.pixels[color_index + 2] +
|
|
150u * color.pixels[color_index + 1] +
|
|
29u * color.pixels[color_index];
|
|
result.pixels[index] = static_cast<uint16_t>(luminance);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} // namespace nui::synth
|