134 lines
4.4 KiB
C++
134 lines
4.4 KiB
C++
#include "calibration/calibration.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <fstream>
|
|
#include <iomanip>
|
|
|
|
namespace nui::calibration {
|
|
namespace {
|
|
|
|
constexpr float kProjectionScale = 3.501e-3f;
|
|
constexpr float kMinZ = 0.8f;
|
|
constexpr float kMaxZ = 4.0f;
|
|
|
|
bool finite(float value) {
|
|
return std::isfinite(value);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool confirm_mirror_raise_right_arm(
|
|
const RaiseRightArmObservation& observation,
|
|
bool& mirror,
|
|
std::string& error) {
|
|
const bool right_raised =
|
|
observation.right_wrist_v < observation.right_shoulder_v;
|
|
const bool left_lowered =
|
|
observation.left_wrist_v >= observation.left_shoulder_v;
|
|
if (!observation.person_detected || !right_raised || !left_lowered) {
|
|
error = "mirror confirmation requires only the right arm raised";
|
|
return false;
|
|
}
|
|
mirror = observation.right_wrist_u < observation.right_shoulder_u;
|
|
error.clear();
|
|
return true;
|
|
}
|
|
|
|
bool calibrate_tpose(
|
|
const TposeObservation& observation,
|
|
CalibrationConfig& output,
|
|
std::string& error) {
|
|
if (!observation.person_detected) {
|
|
error = "T-pose calibration requires one detected person";
|
|
return false;
|
|
}
|
|
if (!finite(observation.root_depth_meters) ||
|
|
!finite(observation.normalized_shoulder_width) ||
|
|
observation.normalized_shoulder_width <= 0.0f) {
|
|
error = "T-pose calibration observation is invalid";
|
|
return false;
|
|
}
|
|
|
|
CalibrationConfig candidate{};
|
|
candidate.mapping.z0_meters =
|
|
std::clamp(observation.root_depth_meters, kMinZ, kMaxZ);
|
|
candidate.mapping.depth_gain = 1.0f;
|
|
candidate.mapping.mirror = observation.mirror;
|
|
candidate.player_scale = kTargetShoulderWidthMeters /
|
|
(observation.normalized_shoulder_width * 320.0f *
|
|
kProjectionScale * candidate.mapping.z0_meters);
|
|
candidate.floor_plane = observation.floor_plane;
|
|
for (const float value : candidate.floor_plane) {
|
|
if (!finite(value)) {
|
|
error = "T-pose floor plane is invalid";
|
|
return false;
|
|
}
|
|
}
|
|
output = candidate;
|
|
error.clear();
|
|
return true;
|
|
}
|
|
|
|
bool save_config(
|
|
const CalibrationConfig& config,
|
|
const std::string& path,
|
|
std::string& error) {
|
|
std::ofstream file(path, std::ios::trunc);
|
|
if (!file) {
|
|
error = "could not open calibration config for writing";
|
|
return false;
|
|
}
|
|
file << std::setprecision(9)
|
|
<< "depth_width " << config.mapping.depth_width << '\n'
|
|
<< "depth_height " << config.mapping.depth_height << '\n'
|
|
<< "z0_meters " << config.mapping.z0_meters << '\n'
|
|
<< "depth_gain " << config.mapping.depth_gain << '\n'
|
|
<< "mirror " << config.mapping.mirror << '\n'
|
|
<< "player_scale " << config.player_scale << '\n'
|
|
<< "smoothing " << config.smoothing.smoothing << ' '
|
|
<< config.smoothing.correction << ' ' << config.smoothing.prediction
|
|
<< ' ' << config.smoothing.jitter_radius << ' '
|
|
<< config.smoothing.max_deviation_radius << '\n'
|
|
<< "floor_plane " << config.floor_plane[0] << ' '
|
|
<< config.floor_plane[1] << ' ' << config.floor_plane[2] << ' '
|
|
<< config.floor_plane[3] << '\n';
|
|
if (!file) {
|
|
error = "could not write calibration config";
|
|
return false;
|
|
}
|
|
error.clear();
|
|
return true;
|
|
}
|
|
|
|
bool load_config(
|
|
const std::string& path,
|
|
CalibrationConfig& output,
|
|
std::string& error) {
|
|
std::ifstream file(path);
|
|
CalibrationConfig candidate{};
|
|
std::string key;
|
|
if (!file ||
|
|
!(file >> key >> candidate.mapping.depth_width) || key != "depth_width" ||
|
|
!(file >> key >> candidate.mapping.depth_height) || key != "depth_height" ||
|
|
!(file >> key >> candidate.mapping.z0_meters) || key != "z0_meters" ||
|
|
!(file >> key >> candidate.mapping.depth_gain) || key != "depth_gain" ||
|
|
!(file >> key >> candidate.mapping.mirror) || key != "mirror" ||
|
|
!(file >> key >> candidate.player_scale) || key != "player_scale" ||
|
|
!(file >> key >> candidate.smoothing.smoothing >>
|
|
candidate.smoothing.correction >> candidate.smoothing.prediction >>
|
|
candidate.smoothing.jitter_radius >>
|
|
candidate.smoothing.max_deviation_radius) || key != "smoothing" ||
|
|
!(file >> key >> candidate.floor_plane[0] >> candidate.floor_plane[1] >>
|
|
candidate.floor_plane[2] >> candidate.floor_plane[3]) ||
|
|
key != "floor_plane") {
|
|
error = "could not parse calibration config";
|
|
return false;
|
|
}
|
|
output = candidate;
|
|
error.clear();
|
|
return true;
|
|
}
|
|
|
|
} // namespace nui::calibration
|