18#include <nlohmann/json.hpp>
22#include <unordered_set>
28std::string firstDuplicate(
const LocatorList& locators) {
29 std::unordered_set<std::string> locatorNames;
30 for (
const auto& l : locators) {
31 auto itr = locatorNames.find(l.name);
32 if (itr != locatorNames.end()) {
35 locatorNames.insert(itr, l.name);
41std::pair<size_t, size_t> byteIndexToLineColumn(std::string_view input,
size_t byteIndex) {
45 for (
size_t i = 0; i < byteIndex && i < input.size(); ++i) {
46 if (input[i] ==
'\n') {
54 return {line, column};
60 const filesystem::path& filename,
63 std::ifstream instream(filename, std::ios::binary);
64 MT_THROW_IF(!instream.is_open(),
"Cannot find file {}", filename.string());
65 instream.seekg(0, std::ios::end);
66 auto length = instream.tellg();
67 instream.seekg(0, std::ios::beg);
69 std::vector<char> buffer(length);
70 instream.read(buffer.data(), length);
73 std::span<const std::byte>(
reinterpret_cast<const std::byte*
>(buffer.data()), length),
79 std::span<const std::byte> rawData,
82 const std::string_view input(
reinterpret_cast<const char*
>(rawData.data()), rawData.size());
87 const nlohmann::json j = nlohmann::json::parse(input);
89 if (!j.contains(
"locators") || !j[
"locators"].is_array()) {
95 for (
const auto& locatorJson : j[
"locators"]) {
99 l.
locked.x() = locatorJson.value(
"lockX", 0);
100 l.
locked.y() = locatorJson.value(
"lockY", 0);
101 l.
locked.z() = locatorJson.value(
"lockZ", 0);
104 l.
name = locatorJson.value(
"name", std::string());
108 if (locatorJson.contains(
"parentName")) {
109 const std::string parentName = locatorJson[
"parentName"].get<std::string>();
110 for (
size_t k = 0; k < skeleton.
joints.size(); k++) {
111 if (skeleton.
joints[k].name == parentName) {
112 l.
parent =
static_cast<int>(k);
117 MT_LOGW(
"Invalid parent name: {}", parentName);
124 "Skipping locator '{}' because it has no valid parent",
125 l.
name.empty() ?
"(unnamed)" : l.
name);
130 l.
weight = locatorJson.value(
"weight", 1.0f);
133 const bool hasOffsetX = locatorJson.contains(
"offsetX");
134 const bool hasOffsetY = locatorJson.contains(
"offsetY");
135 const bool hasOffsetZ = locatorJson.contains(
"offsetZ");
136 l.
offset.x() = locatorJson.value(
"offsetX", 0.0f);
137 l.
offset.y() = locatorJson.value(
"offsetY", 0.0f);
138 l.
offset.z() = locatorJson.value(
"offsetZ", 0.0f);
141 const bool hasAnyOffset = hasOffsetX || hasOffsetY || hasOffsetZ;
142 const bool hasAllOffset = hasOffsetX && hasOffsetY && hasOffsetZ;
143 if (hasAnyOffset && !hasAllOffset) {
145 "Locator '{}' has partial offset specification (offsetX: {}, offsetY: {}, offsetZ: {}). "
146 "If any offset component is specified, all three should be provided.",
147 l.
name.empty() ?
"(unnamed)" : l.
name,
154 const bool hasGlobalX = locatorJson.contains(
"globalX");
155 const bool hasGlobalY = locatorJson.contains(
"globalY");
156 const bool hasGlobalZ = locatorJson.contains(
"globalZ");
157 const bool hasAnyGlobal = hasGlobalX || hasGlobalY || hasGlobalZ;
158 const bool hasAllGlobal = hasGlobalX && hasGlobalY && hasGlobalZ;
161 if (hasAnyGlobal && !hasAllGlobal) {
163 "Locator '{}' has partial global specification (globalX: {}, globalY: {}, globalZ: {}). "
164 "If any global component is specified, all three should be provided.",
165 l.
name.empty() ?
"(unnamed)" : l.
name,
172 if (hasAnyGlobal && hasAnyOffset) {
174 "Locator '{}' has both global (globalX/Y/Z) and local (offsetX/Y/Z) offset specification. "
175 "Only one type of offset should be specified. Global offset will take precedence.",
176 l.
name.empty() ?
"(unnamed)" : l.
name);
181 global.x() = locatorJson.value(
"globalX", 0.0f);
182 global.y() = locatorJson.value(
"globalY", 0.0f);
183 global.z() = locatorJson.value(
"globalZ", 0.0f);
188 l.
limitWeight[0] = locatorJson.value(
"limitWeightX", 0.0f);
189 l.
limitWeight[1] = locatorJson.value(
"limitWeightY", 0.0f);
190 l.
limitWeight[2] = locatorJson.value(
"limitWeightZ", 0.0f);
194 l.
skinOffset = locatorJson.value(
"skinOffset", 0.0f);
199 }
catch (
const nlohmann::json::parse_error& e) {
200 const auto [line, column] = byteIndexToLineColumn(input, e.byte);
201 MT_THROW(
"Failed to parse locators JSON at line {}, column {}: {}", line, column, e.what());
204 std::string dup = firstDuplicate(res);
205 MT_THROW_IF(!dup.empty(),
"Duplicated locator {} found", dup);
211 const filesystem::path& filename,
219 j[
"locators"] = nlohmann::json::array();
221 for (
size_t i = 0; i < locators.size(); i++) {
222 nlohmann::json locatorJson;
224 locatorJson[
"name"] = locators[i].name;
227 locatorJson[
"globalX"] = lstate.
position[i].x();
228 locatorJson[
"globalY"] = lstate.
position[i].y();
229 locatorJson[
"globalZ"] = lstate.
position[i].z();
231 locatorJson[
"offsetX"] = locators[i].offset.x();
232 locatorJson[
"offsetY"] = locators[i].offset.y();
233 locatorJson[
"offsetZ"] = locators[i].offset.z();
236 locatorJson[
"lockX"] = locators[i].locked.x();
237 locatorJson[
"lockY"] = locators[i].locked.y();
238 locatorJson[
"lockZ"] = locators[i].locked.z();
239 locatorJson[
"weight"] = locators[i].weight;
241 if (locators[i].limitWeight[0] != 0.0f) {
242 locatorJson[
"limitWeightX"] = locators[i].limitWeight.x();
244 if (locators[i].limitWeight[1] != 0.0f) {
245 locatorJson[
"limitWeightY"] = locators[i].limitWeight.y();
247 if (locators[i].limitWeight[2] != 0.0f) {
248 locatorJson[
"limitWeightZ"] = locators[i].limitWeight.z();
251 if (locators[i].attachedToSkin) {
252 locatorJson[
"attachedToSkin"] = 1;
254 if (locators[i].skinOffset != 0.0f) {
255 locatorJson[
"skinOffset"] = locators[i].skinOffset;
259 0 <= locators[i].parent && locators[i].parent < skeleton.
joints.size(),
260 "Invalid joint index");
261 if (!skeleton.
joints.empty() && locators[i].parent < skeleton.
joints.size()) {
262 locatorJson[
"parentName"] = skeleton.
joints[locators[i].parent].name;
265 j[
"locators"].push_back(locatorJson);
268 std::ofstream o(filename);
269 o << j.dump(1,
'\t') <<
'\n';
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
#define MT_THROW(...)
Throws a std::runtime_error with a formatted message.
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
SkeletonT< float > Skeleton
void saveLocators(const filesystem::path &filename, const LocatorList &locators, const Skeleton &skeleton, const LocatorSpace space)
ParameterTransformT< float > ParameterTransform
SkeletonStateT< float > SkeletonState
constexpr size_t kParametersPerJoint
LocatorList loadLocators(const filesystem::path &filename, const Skeleton &skeleton, const ParameterTransform ¶meterTransform)
LocatorList loadLocatorsFromBuffer(std::span< const std::byte > rawData, const Skeleton &skeleton, const ParameterTransform ¶meterTransform)
constexpr size_t kInvalidIndex
Sentinel value indicating an invalid or unset size_t index.
Vector3< float > Vector3f
std::vector< Locator > LocatorList
A collection of locators attached to a skeleton.
Tracks the current world positions of locators in a skeleton.
std::vector< Vector3f > position
World-space positions of all locators, updated when the skeleton moves.
Represents a point attached to a joint in a skeleton.
float weight
Influence weight when used as a constraint in error functions.
size_t parent
Index of the parent joint in the skeleton.
float skinOffset
Offset from the skin surface, used when solving for body shape using locators.
Vector3f limitWeight
Per-axis strength pulling the locator back toward limitOrigin.
Vector3i locked
Per-axis lock flags (x, y, z): 1 = locked, 0 = free.
bool attachedToSkin
True if the locator is attached to the skin of a person (e.g.
Vector3f offset
Position in the parent joint's local coordinate frame.
Vector3f limitOrigin
Reference position for limit constraints; typically equals offset when loaded.
JointStateListT< T > jointState
Computed joint states for all joints in the skeleton.
JointList joints
The list of joints in this skeleton, ordered such that every joint's parent appears earlier in the li...