Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
trc_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
14
15#include <fstream>
16
17namespace momentum {
18
19MarkerSequence loadTrc(std::istream& stream, UpVector up) {
21 res.fps = 0.f;
22
23 std::string line;
24
25 // parse header
26 GetLineCrossPlatform(stream, line);
27 if (line.find("PathFileType\t4\t(X/Y/Z)") == std::string::npos) {
28 return res;
29 }
30
31 GetLineCrossPlatform(stream, line);
32 if (line.find(
33 "DataRate\tCameraRate\tNumFrames\tNumMarkers\tUnits\tOrigDataRate\tOrigDataStartFrame\tOrigNumFrames") ==
34 std::string::npos) {
35 return res;
36 }
37
38 GetLineCrossPlatform(stream, line);
39 auto tokens = tokenize(line, " \t\r\n");
40 if (tokens.size() != 8) {
41 return res;
42 }
43 const double frameRate = std::stod(tokens[0]);
44 const size_t numMarkers = std::stoi(tokens[3]);
45 const std::string unitStr = tokens[4];
46 res.fps = static_cast<float>(frameRate);
47
48 // get line with marker names and parse it
49 GetLineCrossPlatform(stream, line);
50 tokens = tokenize(line, " \t\r\n");
51 if (tokens.size() != numMarkers + 2) {
52 res.fps = 0.0f;
53 return res;
54 }
55 std::vector<std::string> markerNames;
56 for (size_t i = 2; i < tokens.size(); i++) {
57 markerNames.push_back(tokens[i]);
58 }
59
60 // ignore next line
61 GetLineCrossPlatform(stream, line);
62
63 while (GetLineCrossPlatform(stream, line)) {
64 tokens = tokenize(line, "\t", false);
65 // check for right size
66 if (tokens.size() != numMarkers * 3 + 2) {
67 continue;
68 }
69
70 // create actor structure for every frame
71 std::vector<Marker> markers(numMarkers);
72
73 // go over all markers
74 for (size_t i = 0; i < numMarkers; i++) {
75 const size_t pos = i * 3 + 2;
77 i >= markerNames.size() || pos + 2 >= tokens.size(), "Marker index {} out of bounds", i);
78 markers[i].name = markerNames[i];
79 if (tokens[pos + 0].empty() || tokens[pos + 1].empty() || tokens[pos + 2].empty()) {
80 markers[i].occluded = true;
81 } else {
82 markers[i].occluded = false;
83 Vector3d p{
84 std::stod(tokens[pos + 0]), std::stod(tokens[pos + 1]), std::stod(tokens[pos + 2])};
85 p = toMomentumVector3(p, up, unitStr);
86 markers[i].pos = p;
87 }
88 }
89
90 // store frame
91 res.frames.push_back(markers);
92 }
93
94 return res;
95}
96
97MarkerSequence loadTrc(const std::string& filename, UpVector up) {
98 std::ifstream infile(filename);
99 if (!infile.is_open()) {
100 return MarkerSequence{};
101 }
102 return loadTrc(static_cast<std::istream&>(infile), up);
103}
104
105MarkerSequence loadTrc(std::span<const std::byte> bytes, UpVector up) {
106 ispanstream stream(bytes);
107 return loadTrc(static_cast<std::istream&>(stream), up);
108}
109
110} // namespace momentum
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Definition exception.h:55
Vector3< double > Vector3d
Definition types.h:261
UpVector
This enumeration is used to define the primary up vector directions for various coordinate systems an...
MarkerSequence loadTrc(std::istream &stream, UpVector up)
Loads marker data from a TRC input stream into a MarkerSequence.
Definition trc_io.cpp:19
std::istream & GetLineCrossPlatform(std::istream &is, std::string &line)
Reads a line from the input stream, handling line endings for different platforms.
std::vector< std::string > tokenize(const std::string &inputString, const std::string &delimiters, const bool trim)
Tokenizes a string using the specified single-byte delimiter characters.
Definition string.cpp:42
Vector3< T > toMomentumVector3(const Vector3< T > &vec, UpVector up, Unit unit)
This function converts a 3D vector in the mocap marker coordinate system to the Momentum coordinate s...
All frames from a single motion capture sequence for one subject (actor).
Definition marker.h:37
float fps
Frame rate of the capture, in frames per second.
Definition marker.h:45
std::vector< std::vector< Marker > > frames
All marker observations across the sequence. Layout: [numFrames][numMarkers].
Definition marker.h:42
istream that takes a span of const bytes as input.