MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
mesh_info.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#include <algorithm>
11#include <list>
12#include <set>
13
14#include "mve/mesh_info.h"
15
17
18void
19MeshInfo::initialize (TriangleMesh::ConstPtr mesh)
20{
21 TriangleMesh::VertexList const& verts = mesh->get_vertices();
22 TriangleMesh::FaceList const& faces = mesh->get_faces();
23 std::size_t face_amount = faces.size() / 3;
24
25 this->vertex_info.clear();
26 this->vertex_info.resize(verts.size());
27
28 /* Add faces to their three vertices. */
29 for (std::size_t i = 0, i3 = 0; i < face_amount; ++i)
30 for (std::size_t j = 0; j < 3; ++j, ++i3)
31 this->vertex_info[faces[i3]].faces.push_back(i);
32
33 /* Classify each vertex and compute adjacenty info. */
34 for (std::size_t i = 0; i < this->vertex_info.size(); ++i)
35 this->update_vertex(*mesh, i);
36}
37
38/* ---------------------------------------------------------------- */
39
40namespace
41{
42 /* Adjacent face representation for the ordering algorithm. */
43 struct AdjacentFace
44 {
45 std::size_t face_id;
46 std::size_t first;
47 std::size_t second;
48 };
49
50 typedef std::list<AdjacentFace> AdjacentFaceList;
51}
52
53/* ---------------------------------------------------------------- */
54
55void
56MeshInfo::update_vertex (TriangleMesh const& mesh, std::size_t vertex_id)
57{
58 TriangleMesh::FaceList const& faces = mesh.get_faces();
59 VertexInfo& vinfo = this->vertex_info[vertex_id];
60
61 /* Build new, temporary adjacent faces representation for ordering. */
62 AdjacentFaceList adj_temp;
63 for (std::size_t i = 0; i < vinfo.faces.size(); ++i)
64 {
65 std::size_t face_off = vinfo.faces[i] * 3;
66 for (std::size_t j = 0; j < 3; ++j)
67 if (faces[face_off + j] == vertex_id)
68 {
69 adj_temp.push_back(AdjacentFace());
70 adj_temp.back().face_id = vinfo.faces[i];
71 adj_temp.back().first = faces[face_off + (j + 1) % 3];
72 adj_temp.back().second = faces[face_off + (j + 2) % 3];
73 break;
74 }
75 }
76
77 /* If there are no adjacent faces, the vertex is unreferenced. */
78 if (adj_temp.empty())
79 {
80 vinfo = VertexInfo();
81 vinfo.vclass = VERTEX_CLASS_UNREF;
82 return;
83 }
84
85 /* Sort adjacent faces by chaining them. */
86 AdjacentFaceList adj_sorted;
87 adj_sorted.push_back(adj_temp.front());
88 adj_temp.pop_front();
89 while (!adj_temp.empty())
90 {
91 std::size_t const front_id = adj_sorted.front().first;
92 std::size_t const back_id = adj_sorted.back().second;
93
94 /* Find a faces that fits the back or front of sorted list. */
95 bool found_face = false;
96 for (AdjacentFaceList::iterator iter = adj_temp.begin();
97 iter != adj_temp.end(); ++iter)
98 {
99 if (front_id == iter->second)
100 {
101 adj_sorted.push_front(*iter);
102 adj_temp.erase(iter);
103 found_face = true;
104 break;
105 }
106 if (back_id == iter->first)
107 {
108 adj_sorted.push_back(*iter);
109 adj_temp.erase(iter);
110 found_face = true;
111 break;
112 }
113 }
114
115 /* If there is no next face, the vertex is complex. */
116 if (!found_face)
117 break;
118 }
119
120 /* If the vertex is complex, add unsorted adjacency information. */
121 if (!adj_temp.empty())
122 {
123 /* Transfer remaining adjacent faces. */
124 adj_sorted.insert(adj_sorted.end(), adj_temp.begin(), adj_temp.end());
125 adj_temp.clear();
126
127 /* Create unique list of all adjacent vertices. */
128 std::set<std::size_t> vset;
129 for (AdjacentFaceList::iterator iter = adj_sorted.begin();
130 iter != adj_sorted.end(); ++iter)
131 {
132 vset.insert(iter->first);
133 vset.insert(iter->second);
134 }
135 vinfo.verts.insert(vinfo.verts.end(), vset.begin(), vset.end());
136 vinfo.vclass = VERTEX_CLASS_COMPLEX;
137 return;
138 }
139
140 /* If the vertex is not on the mesh boundary, the list is circular. */
141 if (adj_sorted.front().first == adj_sorted.back().second)
142 vinfo.vclass = VERTEX_CLASS_SIMPLE;
143 else
144 vinfo.vclass = VERTEX_CLASS_BORDER;
145
146 /* Insert the face IDs in the adjacent faces list. */
147 vinfo.faces.clear();
148 for (AdjacentFaceList::iterator iter = adj_sorted.begin();
149 iter != adj_sorted.end(); ++iter)
150 vinfo.faces.push_back(iter->face_id);
151
152 /* Insert vertex IDs in adjacent vertex list. */
153 for (AdjacentFaceList::const_iterator iter = adj_sorted.begin();
154 iter != adj_sorted.end(); ++iter)
155 vinfo.verts.push_back(iter->first);
156 if (vinfo.vclass == VERTEX_CLASS_BORDER)
157 vinfo.verts.push_back(adj_sorted.back().second);
158}
159
160/* ---------------------------------------------------------------- */
161
162bool
163MeshInfo::is_mesh_edge (std::size_t v1, std::size_t v2) const
164{
165 AdjacentVertices const& verts = this->vertex_info[v1].verts;
166 return std::find(verts.begin(), verts.end(), v2) != verts.end();
167}
168
169/* ---------------------------------------------------------------- */
170
171void
172MeshInfo::get_faces_for_edge (std::size_t v1, std::size_t v2,
173 std::vector<std::size_t>* adjacent_faces) const
174{
175 AdjacentFaces const& faces1 = this->vertex_info[v1].faces;
176 AdjacentFaces const& faces2 = this->vertex_info[v2].faces;
177 std::set<std::size_t> faces2_set(faces2.begin(), faces2.end());
178 for (std::size_t i = 0; i < faces1.size(); ++i)
179 if (faces2_set.find(faces1[i]) != faces2_set.end())
180 adjacent_faces->push_back(faces1[i]);
181}
182
std::vector< math::Vec3f > VertexList
Definition mesh.h:33
std::vector< std::size_t > AdjacentVertices
Definition mesh_info.h:38
std::vector< std::size_t > AdjacentFaces
Definition mesh_info.h:39
Triangle mesh representation.
Definition mesh.h:90
std::vector< VertexID > FaceList
Definition mesh.h:97
std::shared_ptr< TriangleMesh const > ConstPtr
Definition mesh.h:93
FaceList const & get_faces(void) const
Returns the triangle indices.
Definition mesh.h:331
std::size_t second
Definition mesh_info.cc:47
std::size_t first
Definition mesh_info.cc:46
std::size_t face_id
Definition mesh_info.cc:45
unsigned int vertex_id
#define MVE_NAMESPACE_BEGIN
Definition defines.h:13
#define MVE_NAMESPACE_END
Definition defines.h:14
Per-vertex classification and adjacency information.
Definition mesh_info.h:43
AdjacentVertices verts
Definition mesh_info.h:45