Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
mesh.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
12#include <algorithm>
13
14namespace momentum {
15
16template <typename T>
18 normals.resize(vertices.size());
19 std::fill(normals.begin(), normals.end(), Eigen::Vector3<T>::Zero());
20 const auto verticesNum = static_cast<int>(vertices.size());
21 for (const auto& face : faces) {
22 // Skip faces with out-of-bounds vertex indices.
23 if (std::any_of(face.begin(), face.end(), [verticesNum](int idx) {
24 return idx < 0 || idx >= verticesNum;
25 })) {
26 continue;
27 }
28 // The unnormalized cross product has magnitude equal to 2 * triangle area, so accumulating it
29 // across faces produces area-weighted vertex normals (larger faces contribute more). This is
30 // the standard Gouraud-style averaging and tends to produce better results than uniform
31 // averaging for meshes with mixed triangle sizes.
32 const Eigen::Vector3<T> normal =
33 (vertices[face[1]] - vertices[face[0]]).cross(vertices[face[2]] - vertices[face[0]]);
34 // Skip degenerate faces that produced NaN (e.g., from non-finite vertex coordinates).
35 // Collinear/zero-area triangles produce a zero vector, which contributes nothing and is safe.
36 // Inspect all components so a NaN limited to y or z is also caught.
37 if (IsNanNoOpt(normal[0]) || IsNanNoOpt(normal[1]) || IsNanNoOpt(normal[2])) {
38 continue;
39 }
40 for (const auto& faceIdx : face) {
41 normals[faceIdx] += normal;
42 }
43 }
44 for (size_t i = 0; i < normals.size(); i++) {
45 const T len = normals[i].norm();
46 // Leave isolated/unreferenced vertices with a zero normal rather than dividing by zero.
47 if (len != 0) {
48 normals[i] /= len;
49 }
50 }
51}
52
53template <typename T, typename T2, int N>
54std::vector<Eigen::Vector<T2, N>> castVectors(const std::vector<Eigen::Vector<T, N>>& vec_in) {
55 std::vector<Eigen::Vector<T2, N>> result;
56 result.reserve(vec_in.size());
57 for (const auto& v : vec_in) {
58 result.push_back(v.template cast<T2>());
59 }
60 return result;
61}
62
63template <typename T>
64template <typename T2>
66 MeshT<T2> result;
69 result.faces = this->faces;
70 result.lines = this->lines;
71 result.colors = this->colors;
72 result.confidence = std::vector<T2>(this->confidence.begin(), this->confidence.end());
73 result.texcoords = this->texcoords;
74 result.texcoord_faces = this->texcoord_faces;
75 result.texcoord_lines = this->texcoord_lines;
76 result.polyFaces = this->polyFaces;
77 result.polyFaceSizes = this->polyFaceSizes;
79 return result;
80}
81
82template <typename T>
84 vertices.clear();
85 normals.clear();
86 faces.clear();
87 lines.clear();
88 colors.clear();
89 confidence.clear();
90 texcoords.clear();
91 texcoord_faces.clear();
92 texcoord_lines.clear();
93 polyFaces.clear();
94 polyFaceSizes.clear();
95 polyTexcoordFaces.clear();
96}
97
102
103template struct MeshT<float>;
104template struct MeshT<double>;
105
106} // namespace momentum
T normal(const T &mean, const T &sigma)
Normally distributed value via the global Random singleton.
Definition random-inl.h:382
bool IsNanNoOpt(float f)
NaN check function that isn't optimized away by fastmath.
Definition utility.h:25
std::vector< Eigen::Vector< T2, N > > castVectors(const std::vector< Eigen::Vector< T, N > > &vec_in)
Definition mesh.cpp:54
Vector3P< S > cross(const Eigen::MatrixBase< Derived > &v1, const Vector3P< S > &v2)
Computes cross product of Eigen::Vector3f and Vector3P.
Definition simd.h:309
decltype(auto) cast(const ContainerType< ObjectType > &originalContainer)
Casts the scalar type of objects in a container that support the cast<>() method.
Definition types.h:466
A generic mesh representation with support for vertices, faces, lines, and texture coordinates.
Definition mesh.h:20
std::vector< uint32_t > polyFaces
Packed polygon face vertex indices (all polygons concatenated back-to-back).
Definition mesh.h:67
std::vector< Eigen::Vector3i > texcoord_faces
List of texture coordinate indices per face Maps each face to its corresponding texture coordinates.
Definition mesh.h:55
std::vector< std::vector< int32_t > > texcoord_lines
List of texture coordinate indices per line Maps each line to its corresponding texture coordinates.
Definition mesh.h:59
std::vector< uint32_t > polyFaceSizes
Number of vertices in each polygon face.
Definition mesh.h:73
std::vector< uint32_t > polyTexcoordFaces
Packed polygon face texture coordinate indices (same layout as polyFaces).
Definition mesh.h:79
std::vector< Eigen::Vector2f > texcoords
List of texture coordinates (UV coordinates).
Definition mesh.h:51
void updateNormals()
Compute vertex normals by averaging connected face normals.
Definition mesh.cpp:17
std::vector< T > confidence
List of per-vertex confidence values Higher values typically indicate higher confidence in the vertex...
Definition mesh.h:39
std::vector< Eigen::Vector3< T > > vertices
List of mesh vertices in 3D space.
Definition mesh.h:22
std::vector< Eigen::Vector3< T > > normals
List of vertex normals (same size as vertices when computed).
Definition mesh.h:25
std::vector< std::vector< int32_t > > lines
List of polylines defined by vertex indices Each inner vector represents a single polyline.
Definition mesh.h:32
void reset()
Reset the mesh by clearing all data.
Definition mesh.cpp:83
std::vector< Eigen::Vector3i > faces
List of triangular faces defined by vertex indices.
Definition mesh.h:28
std::vector< Eigen::Vector3b > colors
List of per-vertex RGB colors (0-255 for each channel).
Definition mesh.h:35
MeshT< T2 > cast() const
Cast the mesh to a different scalar type.
Definition mesh.cpp:65