Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
polygon_data.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
11#include "momentum/common/log.h"
12
13namespace momentum {
14
15inline constexpr uint32_t kInvalidTexCoord = static_cast<uint32_t>(-1);
16
17std::vector<Eigen::Vector3i> triangulate(
18 std::span<const uint32_t> indices,
19 std::span<const uint32_t> offsets) {
20 if (indices.empty()) {
21 return {};
22 }
23
24 const auto nFaces = offsets.size() - 1;
25
26 std::vector<Eigen::Vector3i> result;
27 result.reserve(2 * nFaces);
28
29 for (size_t iFace = 0; iFace < nFaces; ++iFace) {
30 const auto faceStart = offsets[iFace];
31 const auto faceEnd = offsets[iFace + 1];
32 const auto nv = faceEnd - faceStart;
33 MT_THROW_IF(nv < 3, "Invalid face with {} indices; expected at least 3.", nv);
34
35 for (size_t j = 1; j < (nv - 1); ++j) {
36 result.emplace_back(
37 indices[faceStart + 0], indices[faceStart + j], indices[faceStart + j + 1]);
38 }
39 }
40
41 return result;
42}
43
45 MT_CHECK(!offsets.empty());
46 return offsets.size() - 1;
47}
48
49std::string PolygonData::errorMessage(const size_t numVertices) const {
50 if (offsets.empty()) {
51 return "Empty offsets array";
52 }
53
54 if (offsets.front() != 0) {
55 return "Invalid first offset (expected 0)";
56 }
57
58 if (!textureIndices.empty() && textureIndices.size() != indices.size()) {
59 return "Mismatch between indices and textureIndices";
60 }
61
62 const auto nPoly = numPolygons();
63
64 for (size_t iFace = 0; iFace < nPoly; ++iFace) {
65 const auto faceBegin = offsets[iFace];
66 const auto faceEnd = offsets[iFace + 1];
67
68 if (faceBegin >= faceEnd) {
69 return "Non-monotonic offsets array";
70 }
71
72 if (faceEnd > indices.size()) {
73 return "Too-large offset";
74 }
75
76 const auto nv = faceEnd - faceBegin;
77 if (nv < 3) {
78 return "Too few vertices in face";
79 }
80 }
81
82 for (const auto& i : indices) {
83 if (i >= numVertices) {
84 return "Invalid too-large vertex";
85 }
86 }
87
88 if (offsets.back() != indices.size()) {
89 return "Unused trailing vertices";
90 }
91
92 return {};
93}
94
95std::string PolygonData::warnMessage(const size_t numTexVertices) const {
96 for (const auto& i : textureIndices) {
97 if (i != kInvalidTexCoord && i >= numTexVertices) {
98 return "Invalid too-large texture vertex";
99 }
100 }
101
102 return {};
103}
104
105} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
#define MT_THROW_IF(Condition,...)
Conditionally throws a std::runtime_error with a formatted message if the condition is true.
Definition exception.h:55
constexpr uint32_t kInvalidTexCoord
std::vector< Eigen::Vector3i > triangulate(std::span< const uint32_t > indices, std::span< const uint32_t > offsets)
std::string errorMessage(size_t numVertices) const
std::vector< uint32_t > offsets
std::vector< uint32_t > indices
std::vector< uint32_t > textureIndices
std::string warnMessage(size_t numTexVertices) const
size_t numPolygons() const