Visual Computing Library  devel
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
28#include <vclib/mesh.h>
29
30namespace vcl {
31
45template<MeshConcept MeshType>
46void clearPerVertexAdjacentFaces(MeshType& m)
47{
48 requirePerVertexAdjacentFaces(m);
49
50 using VertexType = MeshType::VertexType;
51
52 for (VertexType& v : m.vertices()) {
53 v.clearAdjFaces();
54 }
55}
56
68template<FaceMeshConcept MeshType>
69void updatePerVertexAdjacentFaces(MeshType& m)
70{
71 clearPerVertexAdjacentFaces(m);
72
73 using VertexType = MeshType::VertexType;
74 using FaceType = MeshType::FaceType;
75
76 for (VertexType& v : m.vertices()) {
77 v.clearAdjFaces();
78 }
79
80 for (FaceType& f : m.faces()) {
81 for (VertexType* v : f.vertices()) {
82 v->pushAdjFace(&f);
83 }
84 }
85}
86
100template<MeshConcept MeshType>
101void clearPerVertexAdjacentVertices(MeshType& m)
102{
103 requirePerVertexAdjacentVertices(m);
104
105 using VertexType = MeshType::VertexType;
106
107 for (VertexType& v : m.vertices()) {
108 v.clearAdjVertices();
109 }
110}
111
123template<FaceMeshConcept MeshType>
124void updatePerVertexAdjacentVertices(MeshType& m)
125{
126 clearPerVertexAdjacentVertices(m);
127
128 using VertexType = MeshType::VertexType;
129
130 // vector that contains edges sorted trough unordered vertex pointers
131 // it contains clusters of "same" edges, but each one of them has its face
132 // pointer note that in case on non-manifold mesh, clusters may be of size
133 // >= 2
134 std::vector<MeshEdgeUtil<MeshType>> vec = fillAndSortMeshEdgeUtilVector(m);
135
136 // store the last pair of vertices
137 VertexType* v1 = nullptr;
138 VertexType* v2 = nullptr;
139 for (uint i = 0; i < vec.size(); ++i) {
140 // if this pair is different from the last pair
141 if (vec[i].v[0] != v1 || vec[i].v[1] != v2) {
142 // update last pair
143 v1 = vec[i].v[0];
144 v2 = vec[i].v[1];
145 v1->pushAdjVertex(v2); // set the pair as adjacent
146 v2->pushAdjVertex(v1);
147 }
148 }
149}
150
165template<FaceMeshConcept MeshType>
166void clearPerFaceAdjacentFaces(MeshType& m)
167{
169
170 using FaceType = MeshType::FaceType;
171
172 for (FaceType& f : m.faces()) {
173 for (uint i = 0; i < f.adjFacesNumber(); ++i) {
174 f.setAdjFace(i, nullptr);
175 }
176 }
177}
178
223template<FaceMeshConcept MeshType>
224void updatePerFaceAdjacentFaces(MeshType& m)
225{
227
228 // vector that contains edges sorted trough unordered vertex pointers
229 // it contains clusters of "same" edges, but each one of them has its face
230 // pointer note that in case on non-manifold mesh, clusters may be of size
231 // >= 2
232 std::vector<MeshEdgeUtil<MeshType>> vec = fillAndSortMeshEdgeUtilVector(m);
233
234 if (vec.size() > 0) {
235 // in this loop, base will point to the first element of a cluster of
236 // edges
237 // increment of clusters into loop
238 for (auto base = vec.begin(); base != vec.end();) {
239 auto first = base; // remember the first to set adj to the last
240
241 // i and j will increment together, and if i == j, i will be adj to
242 // j, but j will not be adj to i (to manage non manifold edges and
243 // make cyclic adj on the same edge)
244 auto i = base;
245 auto j = i + 1;
246 if (j != vec.end()) {
247 // case of cluster composed of one element. adj of i is nullptr
248 if (*i != *j) {
249 i->f->setAdjFace(i->e, nullptr);
250 }
251 else { // at least two edges in the cluster
252 while (j != vec.end() && *i == *j) {
253 i->f->setAdjFace(i->e, j->f);
254 ++i;
255 ++j;
256 }
257 // i now is the last element that was equal to first
258 i->f->setAdjFace(i->e, first->f);
259 }
260 }
261
262 // j is the first different edge from first (or it is vec.end()!)
263 base = j;
264 }
265 }
266}
267
268} // namespace vcl
269
270#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:960
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:84
constexpr detail::VerticesView vertices
A view that allows to iterate over the Vertex elements of an object.
Definition vertex.h:92