33std::span<const GloveFrameData> validateAndSliceGloveData(
34 std::span<const GloveFrameData> gloveData,
40 if (gloveData.empty()) {
44 gloveData.size() != markerFrames,
45 "{} glove data has {} frames but marker data has {} frames",
52 for (
size_t frameIndex = firstFrame; frameIndex < lastFrame; ++frameIndex) {
53 const auto& frame = gloveData[frameIndex];
54 for (
size_t obsIndex = 0; obsIndex < frame.size(); ++obsIndex) {
55 const auto& obs = frame[obsIndex];
60 obs.jointName.empty(),
61 "{} glove observation has empty joint name at frame {}, observation {}",
66 character.skeleton.getJointIdByName(obs.jointName) ==
kInvalidIndex,
67 "{} glove observation uses unknown joint '{}' at frame {}, observation {}",
73 !obs.position.allFinite(),
74 "{} glove observation '{}' has non-finite position at frame {}, observation {}",
80 !obs.orientation.coeffs().allFinite(),
81 "{} glove observation '{}' has non-finite orientation at frame {}, observation {}",
87 obs.orientation.norm() <= 1e-6f,
88 "{} glove observation '{}' has zero orientation at frame {}, observation {}",
95 return gloveData.subspan(firstFrame, lastFrame - firstFrame);
99 std::span<const GloveFrameData> leftGloveData,
100 std::span<const GloveFrameData> rightGloveData) {
101 return !leftGloveData.empty() || !rightGloveData.empty();
107std::vector<CameraKeypointData> sliceCameraKeypointData(
108 std::span<const CameraKeypointData> data,
111 std::vector<CameraKeypointData> result;
115 result.reserve(data.size());
116 for (
const auto& camData : data) {
118 sliced.
camera = camData.camera;
119 if (camData.frameData.size() >= lastFrame) {
120 sliced.frameData.assign(
121 camData.frameData.begin() + firstFrame, camData.frameData.begin() + lastFrame);
122 }
else if (camData.frameData.size() > firstFrame) {
123 sliced.frameData.assign(camData.frameData.begin() + firstFrame, camData.frameData.end());
125 result.push_back(std::move(sliced));
135 std::span<
const std::vector<momentum::Marker>> markerData,
139 std::span<const GloveFrameData> leftGloveData,
140 std::span<const GloveFrameData> rightGloveData,
141 const std::optional<GloveConfig>& gloveConfig,
142 std::span<const CameraKeypointData> cameraKeypointData,
143 std::vector<size_t>* selectedFrameIndices,
144 Eigen::MatrixXf* selectedFrameMotion) {
146 firstFrame > markerData.size(),
147 "First frame {} can't exceed total frames {}",
150 const size_t lastFrame =
151 maxFrames > 0 ? std::min(firstFrame + maxFrames, markerData.size()) : markerData.size();
152 const std::span<const std::vector<momentum::Marker>> inputData(
153 markerData.data() + firstFrame, lastFrame - firstFrame);
155 inputData.size() < 2,
156 "Calibration requires at least 2 frames, got {} (from {} total with firstFrame={}, maxFrames={}).",
166 hasAnyGloveData(leftGloveData, rightGloveData) && !gloveConfig,
167 "glove_config must be provided when left_glove_data or right_glove_data is non-empty.");
169 const auto leftGloveSlice = validateAndSliceGloveData(
170 leftGloveData, character, markerData.size(), firstFrame, lastFrame,
"Left");
171 const auto rightGloveSlice = validateAndSliceGloveData(
172 rightGloveData, character, markerData.size(), firstFrame, lastFrame,
"Right");
176 "globalScaleOnly and locatorsOnly are exclusive; they cannot both be true.");
178 const auto slicedKeypointData =
179 sliceCameraKeypointData(cameraKeypointData, firstFrame, lastFrame);
197 selectedFrameIndices,
198 selectedFrameMotion);
205 const std::vector<std::vector<momentum::Marker>>& markerData,
211 std::span<const GloveFrameData> leftGloveData,
212 std::span<const GloveFrameData> rightGloveData,
213 const std::optional<GloveConfig>& gloveConfig,
214 std::span<const CameraKeypointData> cameraKeypointData) {
216 firstFrame > markerData.size(),
217 "First frame {} can't exceed total frames {}",
220 const size_t lastFrame =
221 maxFrames > 0 ? std::min(firstFrame + maxFrames, markerData.size()) : markerData.size();
222 const std::span<const std::vector<momentum::Marker>> inputData(
223 markerData.data() + firstFrame, lastFrame - firstFrame);
226 "Input marker data is empty (firstFrame={}, maxFrames={}).",
234 hasAnyGloveData(leftGloveData, rightGloveData) && !gloveConfig,
235 "glove_config must be provided when left_glove_data or right_glove_data is non-empty.");
237 const auto leftGloveSlice = validateAndSliceGloveData(
238 leftGloveData, character, markerData.size(), firstFrame, lastFrame,
"Left");
239 const auto rightGloveSlice = validateAndSliceGloveData(
240 rightGloveData, character, markerData.size(), firstFrame, lastFrame,
"Right");
242 const auto slicedKeypointData =
243 sliceCameraKeypointData(cameraKeypointData, firstFrame, lastFrame);
249 "globalScaleOnly and locatorsOnly are exclusive; they cannot both be true.");
284 if (trackingConfig.
debug) {
286 MT_LOGI(
"Average marker error: {}", errors.first);
287 MT_LOGI(
"Max marker error: {}", errors.second);
293 const std::string& inputMarkerFile,
294 const std::string& outputFile,
302 const auto outExtension = filesystem::path(outputFile).extension();
303 if (outExtension !=
".fbx" && outExtension !=
".gltf" && outExtension !=
".glb") {
305 "Invalid output file type {}; supported types are glb, gltf, and fbx",
306 outExtension.string());
314 std::vector<std::string> validMarkerNames;
315 for (
const auto& locator : character.locators) {
316 validMarkerNames.push_back(locator.name);
321 std::optional<MarkerSequence> actor;
322 for (
const auto& a : actors) {
323 if (a.name == character.name) {
333 actor = actors[index];
339 MT_LOGE(
"No marker sequence found in the marker file: {}", inputMarkerFile);
354 character.name = actor->name;
357 const size_t lastFrame = maxFrames > 0 ? std::min(firstFrame + maxFrames, actor->frames.size())
358 : actor->frames.size();
364 std::span<
const std::vector<momentum::Marker>>(
365 actor->frames.data() + firstFrame, actor->frames.data() + lastFrame),
367 MT_LOGI(
"{} saved", outputFile);
368 }
catch (std::exception& e) {
369 MT_LOGE(
"Failed to track the markers from file {}, exception: {}", inputMarkerFile, e.what());
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
void processMarkerFile(const std::string &inputMarkerFile, const std::string &outputFile, const TrackingConfig &trackingConfig, const CalibrationConfig &calibrationConfig, const ModelOptions &modelOptions, bool calibrate, size_t firstFrame, size_t maxFrames)
Runs marker tracking on an input marker file (e.g.
std::pair< float, float > getLocatorError(std::span< const std::vector< momentum::Marker > > markerData, const MatrixXf &motion, momentum::Character &character)
Compute average and maximum marker tracking errors across all frames.
void calibrateMarkers(momentum::Character &character, momentum::ModelParameters &identity, std::span< const std::vector< momentum::Marker > > markerData, const CalibrationConfig &calibrationConfig, size_t firstFrame, size_t maxFrames, std::span< const GloveFrameData > leftGloveData, std::span< const GloveFrameData > rightGloveData, const std::optional< GloveConfig > &gloveConfig, std::span< const CameraKeypointData > cameraKeypointData, std::vector< size_t > *selectedFrameIndices, Eigen::MatrixXf *selectedFrameMotion)
Calibrates a character model using marker data without running full tracking.
std::vector< MarkerSequence > loadMarkers(const std::string &filename, UpVector up, std::span< const std::string > validMarkerNames)
Loads all actor sequences from a marker file (c3d, trc, glb) and converts the positions to the target...
void saveMotion(const std::string &outFile, const momentum::Character &character, const momentum::ModelParameters &identity, Eigen::MatrixXf &finalMotion, std::span< const std::vector< momentum::Marker > > markerData, const double fps, const bool saveMarkerMesh, std::span< const int64_t > timestamps)
Save the given character and motion to a GLB or FBX file.
Eigen::MatrixXf processMarkers(momentum::Character &character, momentum::ModelParameters &identity, const std::vector< std::vector< momentum::Marker > > &markerData, const TrackingConfig &trackingConfig, const CalibrationConfig &calibrationConfig, bool calibrate, size_t firstFrame, size_t maxFrames, std::span< const GloveFrameData > leftGloveData, std::span< const GloveFrameData > rightGloveData, const std::optional< GloveConfig > &gloveConfig, std::span< const CameraKeypointData > cameraKeypointData)
Processes marker data for a character model.
Eigen::MatrixXf trackPosesPerframe(const std::span< const std::vector< Marker > > markerData, const Character &character, const ModelParameters &globalParams, const TrackingConfig &config, const size_t frameStride, std::span< const GloveFrameData > leftGloveData, std::span< const GloveFrameData > rightGloveData, const std::optional< GloveConfig > &gloveConfig)
Track poses independently per frame with fixed character identity.
int findMainSubjectIndex(std::span< const MarkerSequence > markerSequences)
Finds the "main subject" from a vector of MarkerSequences.
void calibrateLocators(const std::span< const std::vector< Marker > > markerData, const CalibrationConfig &config, const ModelParameters &identity, Character &character)
Calibrate only locator positions with fixed character identity parameters.
void calibrateModel(const std::span< const std::vector< Marker > > markerData, const CalibrationConfig &config, Character &character, ModelParameters &identity, const std::array< float, 3 > ®ularizerWeights, std::span< const GloveFrameData > leftGloveData, std::span< const GloveFrameData > rightGloveData, const std::optional< GloveConfig > &gloveConfig, std::span< const CameraKeypointData > cameraKeypointData, std::vector< size_t > *selectedFrameIndices, Eigen::MatrixXf *selectedFrameMotion)
ModelParametersT< float > ModelParameters
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
std::tuple< momentum::Character, momentum::ModelParameters > loadCharacterWithIdentity(const ModelOptions &modelFiles)
bool debug
True to print and save debug information.
Configuration for running body and/or locator calibration.
bool locatorsOnly
True to calibrate only the locators and not the body.
bool globalScaleOnly
True to only solve for a global body scale without changing individual bone length.
Per-camera 2D keypoint data for all frames.
Camera camera
Camera intrinsics and extrinsics (world-space).
A character model with skeletal structure, mesh, and optional components.
Configuration for pose tracking given a calibrated body and locators.