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>(m, seed);
94}
95
96template<MeshConcept MeshType>
97std::vector<uint> fillAndShuffleVertexIndexVector(
98 const MeshType& m,
99 std::optional<uint> seed = std::nullopt)
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, seed);
111
112 return vec;
113}
114
115template<FaceMeshConcept MeshType>
116std::vector<typename MeshType::FaceType*> fillAndShuffleFacePointerVector(
117 MeshType& m,
118 std::optional<uint> seed = std::nullopt)
119{
120 using FaceType = MeshType::FaceType;
121
122 return detail::genericFASFPV<MeshType&, FaceType>(m, seed);
123}
124
125template<FaceMeshConcept MeshType>
126std::vector<const typename MeshType::FaceType*> fillAndShuffleFacePointerVector(
127 const MeshType& m,
128 std::optional<uint> seed = std::nullopt)
129{
130 using FaceType = MeshType::FaceType;
131
132 return detail::genericFASFPV<const MeshType&, const FaceType>(m, seed);
133}
134
135template<FaceMeshConcept MeshType>
136std::vector<uint> fillAndShuffleFaceIndexVector(
137 const MeshType& m,
138 std::optional<uint> seed = std::nullopt)
139{
140 using FaceType = MeshType::FaceType;
141
142 std::vector<uint> vec;
143 vec.reserve(m.faceNumber());
144
145 for (const FaceType& f : m.faces()) {
146 vec.push_back(m.index(f));
147 }
148
149 shuffle(vec, seed);
150
151 return vec;
152}
153
154} // namespace vcl
155
156#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:71
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