MCPcopy Create free account
hub / github.com/RozDavid/UnScene3D / segment_mesh

Function segment_mesh

utils/cpp_utils/segmentator.cpp:47–154  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45
46
47vector<int> segment_mesh(MeshDataf mesh, const float kthr, const int segMinVerts, std::map<std::pair<int, int>, int>& connectivity) {
48
49 // Pass mesh file to C++ function
50 // vertices should be of c++ float type with size (num_verts, 3)
51 // faces should be of c++ int type with size (num_faces, 3)
52 // Normals can be initialized here as cleared anyway originally
53
54 size_t edgesCount = mesh.m_FaceIndicesVertices.size() * 3;
55 edge* edges = new edge[edgesCount];
56 vector<int> counts(mesh.m_Vertices.size(), 0);
57 // initialize with zeros
58 mesh.m_Normals.clear();
59 mesh.m_Normals = std::vector<vec3f>(mesh.m_Vertices.size());
60
61 // Compute face normals and smooth into vertex normals
62 for (int i = 0; i < mesh.m_FaceIndicesVertices.size(); i++) {
63 const uint32_t i1 = mesh.m_FaceIndicesVertices[i][0];
64 const uint32_t i2 = mesh.m_FaceIndicesVertices[i][1];
65 const uint32_t i3 = mesh.m_FaceIndicesVertices[i][2];
66
67 vec3f p1 = mesh.m_Vertices[i1];
68 vec3f p2 = mesh.m_Vertices[i2];
69 vec3f p3 = mesh.m_Vertices[i3];
70
71 const int ebase = 3 * i;
72
73 edges[ebase].a = i1; edges[ebase].b = i2;
74 edges[ebase + 1].a = i1; edges[ebase + 1].b = i3;
75 edges[ebase + 2].a = i3; edges[ebase + 2].b = i2;
76
77 // smoothly blend face normals into vertex normals
78 vec3f normal = cross(p2 - p1, p3 - p1);
79 mesh.m_Normals[i1] = lerp(mesh.m_Normals[i1], normal, 1.0f / (counts[i1] + 1.0f));
80 mesh.m_Normals[i2] = lerp(mesh.m_Normals[i2], normal, 1.0f / (counts[i2] + 1.0f));
81 mesh.m_Normals[i3] = lerp(mesh.m_Normals[i3], normal, 1.0f / (counts[i3] + 1.0f));
82 counts[i1]++; counts[i2]++; counts[i3]++;
83 }
84
85 // std::cout << "Constructing edge graph based on mesh connectivity..." << std::endl;
86 for (int i = 0; i < edgesCount; i++) {
87 int a = edges[i].a;
88 int b = edges[i].b;
89
90 vec3f& n1 = mesh.m_Normals[a];
91 vec3f& n2 = mesh.m_Normals[b];
92 vec3f& p1 = mesh.m_Vertices[a];
93 vec3f& p2 = mesh.m_Vertices[b];
94
95 // get the edge as a vector
96 float dx = p2.x - p1.x;
97 float dy = p2.y - p1.y;
98 float dz = p2.z - p1.z;
99 // normalize its length
100 float dd = sqrtf(dx * dx + dy * dy + dz * dz);
101 dx /= dd; dy /= dd; dz /= dd;
102 // similarity of vertex normals
103 float dot = n1.x * n2.x + n1.y * n2.y + n1.z * n2.z;
104 // distance between normals

Callers 1

segment_mesh_wrapperFunction · 0.85

Calls 7

segment_graphFunction · 0.85
sizeMethod · 0.80
findMethod · 0.80
joinMethod · 0.80
crossFunction · 0.50
lerpFunction · 0.50
absFunction · 0.50

Tested by

no test coverage detected