Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
marker_io.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
12#include "momentum/common/log.h"
17
18namespace momentum {
19
20namespace {
21size_t markerCount(const std::vector<std::vector<Marker>>& frames) {
22 size_t maxCountMarkers = 0;
23 for (const auto& fr : frames) {
24 const size_t markerCount =
25 std::count_if(fr.begin(), fr.end(), [](const Marker& marker) { return !marker.occluded; });
26 maxCountMarkers = std::max(markerCount, maxCountMarkers);
27 }
28 return maxCountMarkers;
29}
30} // namespace
31
32std::vector<MarkerSequence> loadMarkers(
33 const std::string& filename,
34 UpVector up,
35 std::span<const std::string> validMarkerNames) {
36 const std::string ext = filesystem::path(filename).extension().string();
37 try {
38 if (ext == ".c3d") {
39 return loadC3d(filename, up, validMarkerNames);
40 } else if (ext == ".trc") {
41 return {loadTrc(filename, up)};
42 } else if (ext == ".glb") {
43 return {loadMarkerSequence(filename)};
44 } else if (ext == ".fbx") {
45 return {loadFbxMarkerSequence(filename)};
46 } else {
47 MT_LOGE("{} Unknown marker file type {}", __func__, filename);
48 return {};
49 }
50 } catch (...) {
51 MT_LOGE("{}: Unknown error in {}", __func__, filename);
52 return {};
53 }
54}
55
56std::optional<MarkerSequence> loadMarkersForMainSubject(
57 const std::string& filename,
58 UpVector up,
59 std::span<const std::string> validMarkerNames) {
60 const std::vector<MarkerSequence> markerSequences = loadMarkers(filename, up, validMarkerNames);
61 const int subjectID = findMainSubjectIndex(markerSequences);
62 if (subjectID < 0) {
63 return {};
64 } else {
65 return {markerSequences.at(subjectID)};
66 }
67}
68
69std::vector<MarkerSequence> loadMarkers(
70 std::span<const std::byte> bytes,
71 const std::string& format,
72 UpVector up,
73 std::span<const std::string> validMarkerNames) {
74 try {
75 if (format == ".trc") {
76 return {loadTrc(bytes, up)};
77 } else if (format == ".glb") {
78 return {loadMarkerSequence(bytes)};
79 } else if (format == ".fbx") {
80 return {loadFbxMarkerSequence(bytes)};
81 } else if (format == ".c3d") {
82#ifdef MOMENTUM_WITH_EZC3D_ISTREAM
83 return loadC3d(bytes, up, validMarkerNames);
84#else
85 // ezc3d does not support reading from a stream; load c3d files by path instead.
86 MT_LOGE("{}: in-memory c3d loading is not supported", __func__);
87 return {};
88#endif
89 } else {
90 MT_LOGE("{}: Unknown marker format '{}' for {}-byte buffer", __func__, format, bytes.size());
91 return {};
92 }
93 } catch (...) {
94 MT_LOGE("{}: Unknown error reading {} buffer", __func__, format);
95 return {};
96 }
97}
98
99std::optional<MarkerSequence> loadMarkersForMainSubject(
100 std::span<const std::byte> bytes,
101 const std::string& format,
102 UpVector up,
103 std::span<const std::string> validMarkerNames) {
104 const std::vector<MarkerSequence> markerSequences =
105 loadMarkers(bytes, format, up, validMarkerNames);
106 const int subjectID = findMainSubjectIndex(markerSequences);
107 if (subjectID < 0) {
108 return {};
109 } else {
110 return {markerSequences.at(subjectID)};
111 }
112}
113
114int findMainSubjectIndex(std::span<const MarkerSequence> markerSequences) {
115 if (markerSequences.empty()) {
116 return -1;
117 }
118
119 // We define the main subject as one with the most markers. Ideally, the main subject is the one
120 // with a proper name, because unlabeled markers are usually the largest number, except when there
121 // is only one actor.
122 // XXX Note that the logic could still break, eg. when a person is unnamed but there are named
123 // objects.
124 //
125 // We will go through the sequence to find the max number of visible markers. This is quite slow
126 // but more robust.
127 const size_t numActors = markerSequences.size();
128 std::vector<size_t> maxMarkers(numActors, 0);
129 // count markers for each actor
130 for (size_t iActor = 0; iActor < numActors; ++iActor) {
131 maxMarkers.at(iActor) = markerCount(markerSequences[iActor].frames);
132 }
133
134 // Special case when there's only one actor.
135 // Return the actor even if it has no markers (empty sequence is valid).
136 if (numActors == 1) {
137 return 0;
138 }
139
140 // find the max marker count with a name
141 size_t maxCount = 0;
142 size_t actorID = 0; // default to be the first actor
143 for (size_t iActor = 0; iActor < numActors; ++iActor) {
144 const size_t count = maxMarkers.at(iActor);
145 auto actorName = markerSequences[iActor].name;
146 std::transform(actorName.begin(), actorName.end(), actorName.begin(), [](unsigned char c) {
147 return std::tolower(c);
148 });
149 if (count > maxCount && !actorName.empty() && actorName != "unlabeled") {
150 maxCount = count;
151 actorID = iActor;
152 }
153 }
154
155 if (maxCount > 0) {
156 return static_cast<int>(actorID);
157 } else {
158 return -1;
159 }
160}
161} // namespace momentum
#define MT_LOGE(...)
Definition log.h:181
std::vector< MarkerSequence > loadC3d(const std::string &filename, UpVector up, std::span< const std::string > validMarkerNames)
Loads all the subjects from a C3D file into a vector of MarkerSequences.
Definition c3d_io.cpp:292
UpVector
This enumeration is used to define the primary up vector directions for various coordinate systems an...
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...
Definition marker_io.cpp:32
MarkerSequence loadTrc(std::istream &stream, UpVector up)
Loads marker data from a TRC input stream into a MarkerSequence.
Definition trc_io.cpp:19
int findMainSubjectIndex(std::span< const MarkerSequence > markerSequences)
Finds the "main subject" from a vector of MarkerSequences.
MarkerSequence loadMarkerSequence(const filesystem::path &filename)
Load a marker sequence from a glTF file.
Definition gltf_io.cpp:485
MarkerSequence loadFbxMarkerSequence(const filesystem::path &filename, bool stripNamespaces)
Loads a MarkerSequence from an FBX file.
Definition fbx_io.cpp:218
std::optional< MarkerSequence > loadMarkersForMainSubject(const std::string &filename, UpVector up, std::span< const std::string > validMarkerNames)
Loads the main subject's marker data from a marker file (c3d, trc, glb) and converts the positions to...
Definition marker_io.cpp:56
A single motion capture marker observation (position, occlusion, confidence).
Definition marker.h:18