Visual Computing Library  devel
Loading...
Searching...
No Matches
sort.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_SORT_H
24#define VCL_ALGORITHMS_MESH_SORT_H
25
26#include "utility.h"
27
28#include <vclib/space/complex.h>
29
30#include <algorithm>
31
32namespace vcl {
33
34template<FaceMeshConcept MeshType>
35std::vector<MeshEdgeUtil<MeshType>> fillAndSortMeshEdgeUtilVector(
36 MeshType& m,
37 bool includeFauxEdges = true)
38{
39 using FaceType = MeshType::FaceType;
40
41 std::vector<MeshEdgeUtil<MeshType>> vec;
42
43 int n_edges = 0;
44 for (const FaceType& f : m.faces())
45 n_edges += f.vertexNumber();
46
47 vec.reserve(n_edges);
48
49 for (FaceType& f : m.faces()) { // fill it with face data
50 for (uint j = 0; j < f.vertexNumber(); ++j) {
51 if (includeFauxEdges || !f.edgeFaux(j)) {
52 vec.emplace_back(f, j);
53 }
54 }
55 }
56
57 // Sort it by vertices
58 std::sort(std::execution::par_unseq, vec.begin(), vec.end());
59
60 return vec;
61}
62
63template<FaceMeshConcept MeshType>
64std::vector<ConstMeshEdgeUtil<MeshType>> fillAndSortMeshEdgeUtilVector(
65 const MeshType& m,
66 bool includeFauxEdges = true)
67{
68 using FaceType = MeshType::FaceType;
69
70 std::vector<ConstMeshEdgeUtil<MeshType>> vec;
71
72 int n_edges = 0;
73 for (const FaceType& f : m.faces())
74 n_edges += f.vertexNumber();
75
76 vec.reserve(n_edges);
77
78 for (const FaceType& f : m.faces()) { // Lo riempio con i dati delle facce
79 for (uint j = 0; j < f.vertexNumber(); ++j) {
80 if (includeFauxEdges || !f.edgeFaux(j)) {
81 vec.emplace_back(f, j);
82 }
83 }
84 }
85
86 // Lo ordino per vertici
87 std::sort(std::execution::par_unseq, vec.begin(), vec.end());
88
89 return vec;
90}
91
115template<uint ELEM_ID, MeshConcept MeshType>
116std::vector<uint> sortElemIndicesByFunction(
117 const MeshType& mesh,
118 const std::function<bool(
119 const typename MeshType::template ElementType<ELEM_ID>&,
120 const typename MeshType::template ElementType<ELEM_ID>&)>& func,
121 bool getIndicesAsIfContainerCompact = false)
122{
123 using ElemType = typename MeshType::template ElementType<ELEM_ID>;
124
125 auto compactIndices = detail::elemCompactIndices<ELEM_ID>(
126 mesh, getIndicesAsIfContainerCompact);
127
128 std::vector<uint> indices;
129
130 // Initialize indices with sequential values
131 indices.resize(mesh.template number<ELEM_ID>());
132 std::iota(indices.begin(), indices.end(), 0u);
133
134 std::sort(indices.begin(), indices.end(), [&](uint a, uint b) {
135 return func(
136 mesh.template element<ELEM_ID>(a),
137 mesh.template element<ELEM_ID>(b));
138 });
139
140 if (!compactIndices.empty()) {
141 for (auto& idx : indices) {
142 idx = compactIndices[idx];
143 }
144 }
145
146 return indices;
147}
148
170template<MeshConcept MeshType>
171std::vector<uint> sortVertexIndicesByFunction(
172 const MeshType& mesh,
173 const std::function<bool(
174 const typename MeshType::VertexType&,
175 const typename MeshType::VertexType&)>& func,
176 bool getIndicesAsIfContainerCompact = false)
177{
178 return sortElemIndicesByFunction<ElemId::VERTEX>(
179 mesh, func, getIndicesAsIfContainerCompact);
180}
181
203template<FaceMeshConcept MeshType>
204std::vector<uint> sortFaceIndicesByFunction(
205 const MeshType& mesh,
206 const std::function<bool(
207 const typename MeshType::FaceType&,
208 const typename MeshType::FaceType&)>& func,
209 bool getIndicesAsIfContainerCompact = false)
210{
211 return sortElemIndicesByFunction<ElemId::FACE>(
212 mesh, func, getIndicesAsIfContainerCompact);
213}
214
236template<EdgeMeshConcept MeshType>
237std::vector<uint> sortEdgeIndicesByFunction(
238 const MeshType& mesh,
239 const std::function<bool(
240 const typename MeshType::EdgeType&,
241 const typename MeshType::EdgeType&)>& func,
242 bool getIndicesAsIfContainerCompact = false)
243{
244 return sortElemIndicesByFunction<ElemId::EDGE>(
245 mesh, func, getIndicesAsIfContainerCompact);
246}
247
248} // namespace vcl
249
250#endif // VCL_ALGORITHMS_MESH_SORT_H
constexpr detail::FacesView faces
A view that allows to iterate overt the Face elements of an object.
Definition face.h:84