Visual Computing Library
Loading...
Searching...
No Matches
topology.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_ALGORITHMS_MESH_UPDATE_TOPOLOGY_H
24#define VCL_ALGORITHMS_MESH_UPDATE_TOPOLOGY_H
25
26#include <vclib/algorithms/mesh/sort.h>
27#include <vclib/mesh/requirements.h>
28
29namespace vcl {
30
44template<MeshConcept MeshType>
45void clearPerVertexAdjacentFaces(MeshType& m)
46{
47 requirePerVertexAdjacentFaces(m);
48
49 using VertexType = MeshType::VertexType;
50
51 for (VertexType& v : m.vertices()) {
52 v.clearAdjFaces();
53 }
54}
55
67template<FaceMeshConcept MeshType>
68void updatePerVertexAdjacentFaces(MeshType& m)
69{
70 clearPerVertexAdjacentFaces(m);
71
72 using VertexType = MeshType::VertexType;
73 using FaceType = MeshType::FaceType;
74
75 for (VertexType& v : m.vertices()) {
76 v.clearAdjFaces();
77 }
78
79 for (FaceType& f : m.faces()) {
80 for (VertexType* v : f.vertices()) {
81 v->pushAdjFace(&f);
82 }
83 }
84}
85
99template<MeshConcept MeshType>
100void clearPerVertexAdjacentVertices(MeshType& m)
101{
102 requirePerVertexAdjacentVertices(m);
103
104 using VertexType = MeshType::VertexType;
105
106 for (VertexType& v : m.vertices()) {
107 v.clearAdjVertices();
108 }
109}
110
122template<FaceMeshConcept MeshType>
123void updatePerVertexAdjacentVertices(MeshType& m)
124{
125 clearPerVertexAdjacentVertices(m);
126
127 using VertexType = MeshType::VertexType;
128
129 // vector that contains edges sorted trough unordered vertex pointers
130 // it contains clusters of "same" edges, but each one of them has its face
131 // pointer note that in case on non-manifold mesh, clusters may be of size
132 // >= 2
133 std::vector<MeshEdgeUtil<MeshType>> vec = fillAndSortMeshEdgeUtilVector(m);
134
135 // store the last pair of vertices
136 VertexType* v1 = nullptr;
137 VertexType* v2 = nullptr;
138 for (uint i = 0; i < vec.size(); ++i) {
139 // if this pair is different from the last pair
140 if (vec[i].v[0] != v1 || vec[i].v[1] != v2) {
141 // update last pair
142 v1 = vec[i].v[0];
143 v2 = vec[i].v[1];
144 v1->pushAdjVertex(v2); // set the pair as adjacent
145 v2->pushAdjVertex(v1);
146 }
147 }
148}
149
164template<FaceMeshConcept MeshType>
165void clearPerFaceAdjacentFaces(MeshType& m)
166{
168
169 using FaceType = MeshType::FaceType;
170
171 for (FaceType& f : m.faces()) {
172 for (uint i = 0; i < f.adjFacesNumber(); ++i) {
173 f.setAdjFace(i, nullptr);
174 }
175 }
176}
177
222template<FaceMeshConcept MeshType>
223void updatePerFaceAdjacentFaces(MeshType& m)
224{
226
227 // vector that contains edges sorted trough unordered vertex pointers
228 // it contains clusters of "same" edges, but each one of them has its face
229 // pointer note that in case on non-manifold mesh, clusters may be of size
230 // >= 2
231 std::vector<MeshEdgeUtil<MeshType>> vec = fillAndSortMeshEdgeUtilVector(m);
232
233 if (vec.size() > 0) {
234 // in this loop, base will point to the first element of a cluster of
235 // edges
236 // increment of clusters into loop
237 for (auto base = vec.begin(); base != vec.end();) {
238 auto first = base; // remember the first to set adj to the last
239
240 // i and j will increment together, and if i == j, i will be adj to
241 // j, but j will not be adj to i (to manage non manifold edges and
242 // make cyclic adj on the same edge)
243 auto i = base;
244 auto j = i + 1;
245 if (j != vec.end()) {
246 // case of cluster composed of one element. adj of i is nullptr
247 if (*i != *j) {
248 i->f->setAdjFace(i->e, nullptr);
249 }
250 else { // at least two edges in the cluster
251 while (j != vec.end() && *i == *j) {
252 i->f->setAdjFace(i->e, j->f);
253 ++i;
254 ++j;
255 }
256 // i now is the last element that was equal to first
257 i->f->setAdjFace(i->e, first->f);
258 }
259 }
260
261 // j is the first different edge from first (or it is vec.end()!)
262 base = j;
263 }
264 }
265}
266
267} // namespace vcl
268
269#endif // VCL_ALGORITHMS_MESH_UPDATE_TOPOLOGY_H
void requirePerFaceAdjacentFaces(const MeshType &m)
This function asserts that a Mesh has a FaceContainer, the Face has a AdjacentFaces Component,...
Definition face_requirements.h:698
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:52
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:60