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