Visual Computing Library
Loading...
Searching...
No Matches
shuffle.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_SHUFFLE_H
24#define VCL_ALGORITHMS_MESH_SHUFFLE_H
25
26#include <vclib/mesh/requirements.h>
27#include <vclib/misc/shuffle.h>
28
29namespace vcl {
30
31namespace detail {
32
33/*
34 * Generic implementation of fillAndShuffleVertexPointerVector, to avoid code
35 * duplication.
36 *
37 * Templates M and V can be both const and non-const MeshType and VertexType
38 */
39template<typename M, typename V>
40std::vector<V*> genericFASVPV(M m, bool deterministic)
41{
42 std::vector<V*> vec;
43 vec.reserve(m.vertexNumber());
44
45 for (V& v : m.vertices()) {
46 vec.push_back(&v);
47 }
48
49 shuffle(vec, deterministic);
50
51 return vec;
52}
53
54/*
55 * Generic implementation of fillAndShuffleFacePointerVector, to avoid code
56 * duplication.
57 *
58 * Templates M and F can be both const and non-const MeshType and FaceType
59 */
60template<typename M, typename F>
61std::vector<F*> genericFASFPV(M m, bool deterministic)
62{
63 std::vector<F*> vec;
64 vec.reserve(m.faceNumber());
65
66 for (F& f : m.faces()) {
67 vec.push_back(&f);
68 }
69
70 shuffle(vec, deterministic);
71
72 return vec;
73}
74
75} // namespace detail
76
77template<MeshConcept MeshType>
78std::vector<typename MeshType::VertexType*> fillAndShuffleVertexPointerVector(
79 MeshType& m,
80 bool deterministic = false)
81{
82 using VertexType = MeshType::VertexType;
83
84 return detail::genericFASVPV<MeshType&, VertexType>(m, deterministic);
85}
86
87template<MeshConcept MeshType>
88std::vector<const typename MeshType::VertexType*>
89fillAndShuffleVertexPointerVector(const MeshType& m, bool deterministic = false)
90{
91 using VertexType = MeshType::VertexType;
92 return detail::genericFASVPV<const MeshType&, const VertexType>(
93 m, deterministic);
94}
95
96template<MeshConcept MeshType>
97std::vector<uint> fillAndShuffleVertexIndexVector(
98 const MeshType& m,
99 bool deterministic = false)
100{
101 using VertexType = MeshType::VertexType;
102
103 std::vector<uint> vec;
104 vec.reserve(m.vertexNumber());
105
106 for (const VertexType& v : m.vertices()) {
107 vec.push_back(m.index(v));
108 }
109
110 shuffle(vec, deterministic);
111
112 return vec;
113}
114
115template<FaceMeshConcept MeshType>
116std::vector<typename MeshType::FaceType*> fillAndShuffleFacePointerVector(
117 MeshType& m,
118 bool deterministic = false)
119{
120 using FaceType = MeshType::FaceType;
121
122 return detail::genericFASFPV<MeshType&, FaceType>(m, deterministic);
123}
124
125template<FaceMeshConcept MeshType>
126std::vector<const typename MeshType::FaceType*> fillAndShuffleFacePointerVector(
127 const MeshType& m,
128 bool deterministic = false)
129{
130 using FaceType = MeshType::FaceType;
131
132 return detail::genericFASFPV<const MeshType&, const FaceType>(
133 m, deterministic);
134}
135
136template<FaceMeshConcept MeshType>
137std::vector<uint> fillAndShuffleFaceIndexVector(
138 const MeshType& m,
139 bool deterministic = false)
140{
141 using FaceType = MeshType::FaceType;
142
143 std::vector<uint> vec;
144 vec.reserve(m.faceNumber());
145
146 for (const FaceType& f : m.faces()) {
147 vec.push_back(m.index(f));
148 }
149
150 shuffle(vec, deterministic);
151
152 return vec;
153}
154
155} // namespace vcl
156
157#endif // VCL_ALGORITHMS_MESH_SHUFFLE_H
void shuffle(R &&range, bool deterministic=false)
Shuffle the elements of a range.
Definition shuffle.h:43
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