Visual Computing Library  devel
Loading...
Searching...
No Matches
vertical_components_vector_tuple.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_MESH_CONTAINERS_DETAIL_VERTICAL_COMPONENTS_VECTOR_TUPLE_H
24#define VCL_MESH_CONTAINERS_DETAIL_VERTICAL_COMPONENTS_VECTOR_TUPLE_H
25
26#include <vclib/mesh/components/concepts/component.h>
27
28#include <vclib/base.h>
29
30#include <array>
31#include <tuple>
32#include <vector>
33
34namespace vcl::mesh::detail {
35
36template<typename... Comp>
37class VerticalComponentsVectorTuple
38{
39 using ComponentTypes = std::tuple<Comp...>;
40
41 static constexpr uint COMP_NUMBER =
42 std::tuple_size_v<std::tuple<std::vector<Comp>...>>;
43
44 std::tuple<std::vector<typename Comp::DataValueType>...> mVecTuple;
45
46 std::array<bool, COMP_NUMBER> mVecEnabled;
47 std::size_t mSize = 0;
48
49public:
50 VerticalComponentsVectorTuple()
51 {
52 (setComponentEnabled<Comp, !comp::IsOptionalComponent<Comp>>(), ...);
53 }
54
55 static constexpr uint componentsNumber() { return COMP_NUMBER; }
56
57 template<typename C>
58 constexpr bool hasComponent() const
59 {
60 return indexOfType<C>() != UINT_NULL;
61 }
62
63 template<typename C>
64 constexpr std::vector<typename C::DataValueType>& vector()
65 {
66 constexpr uint ind = indexOfType<C>();
67 return std::get<ind>(mVecTuple);
68 }
69
70 template<typename C>
71 constexpr const std::vector<typename C::DataValueType>& vector() const
72 {
73 constexpr uint ind = indexOfType<C>();
74 return std::get<ind>(mVecTuple);
75 }
76
77 std::size_t size() const { return mSize; }
78
79 void resize(std::size_t size)
80 {
81 if constexpr (componentsNumber() > 0) {
82 vectorResize<componentsNumber() - 1>(size);
83 }
84 mSize = size;
85 }
86
87 void reserve(std::size_t size)
88 {
89 if constexpr (componentsNumber() > 0) {
90 vectorReserve<componentsNumber() - 1>(size);
91 }
92 }
93
94 void compact(const std::vector<uint>& newIndices)
95 {
96 if constexpr (componentsNumber() > 0) {
97 vectorCompact<componentsNumber() - 1>(newIndices);
98 }
99 }
100
101 void clear()
102 {
103 auto function = [](auto&... args) {
104 ((args.clear()), ...);
105 };
106
107 std::apply(function, mVecTuple);
108 mSize = 0;
109 }
110
111 void enableAllOptionalComponents()
112 {
113 (setComponentEnabledIfOptional<Comp, true>(), ...);
114 }
115
116 void disableAllOptionalComponents()
117 {
118 (setComponentEnabledIfOptional<Comp, false>(), ...);
119 }
120
121 template<typename C>
122 bool isComponentEnabled() const
123 {
124 constexpr uint ind = indexOfType<C>();
125 return mVecEnabled[ind];
126 }
127
128 template<uint COMP_ID>
129 bool isComponentEnabled() const
130 {
131 using C = comp::ComponentOfType<COMP_ID, Comp...>;
132 return isComponentEnabled<C>();
133 }
134
135 template<typename C>
136 void enableComponent()
137 {
138 constexpr uint ind = indexOfType<C>();
139 mVecEnabled[ind] = true;
140 vector<C>().resize(mSize);
141 }
142
143 template<uint COMP_ID>
144 void enableComponent()
145 {
146 using C = comp::ComponentOfType<COMP_ID, Comp...>;
147 enableComponent<C>();
148 }
149
150 template<typename C>
151 void disableComponent()
152 {
153 constexpr uint ind = indexOfType<C>();
154 mVecEnabled[ind] = false;
155 vector<C>().clear();
156 }
157
158 template<uint COMP_ID>
159 void disableComponent()
160 {
161 using C = comp::ComponentOfType<COMP_ID, Comp...>;
162 disableComponent<C>();
163 }
164
165private:
166 template<typename C>
167 static constexpr uint indexOfType()
168 {
169 return IndexInTypes<C, Comp...>::value;
170 }
171
172 template<std::size_t N>
173 void vectorResize(std::size_t size)
174 {
175 if (mVecEnabled[N]) {
176 std::get<N>(mVecTuple).resize(size);
177 }
178 if constexpr (N != 0)
179 vectorResize<N - 1>(size);
180 }
181
182 template<std::size_t N>
183 void vectorReserve(std::size_t size)
184 {
185 if (mVecEnabled[N]) {
186 std::get<N>(mVecTuple).reserve(size);
187 }
188 if constexpr (N != 0)
189 vectorReserve<N - 1>(size);
190 }
191
192 template<std::size_t N>
193 void vectorCompact(const std::vector<uint>& newIndices)
194 {
195 if (mVecEnabled[N]) {
196 compactVector(std::get<N>(mVecTuple), newIndices);
197 }
198 if constexpr (N != 0)
199 vectorCompact<N - 1>(newIndices);
200 }
201
202 template<typename C, bool E>
203 void setComponentEnabled()
204 {
205 if constexpr (E) {
206 enableComponent<C>();
207 }
208 else {
209 disableComponent<C>();
210 }
211 }
212
213 template<typename C, bool E>
214 void setComponentEnabledIfOptional()
215 {
216 if constexpr (comp::IsOptionalComponent<C>) {
217 setComponentEnabled<C, E>();
218 }
219 }
220};
221
222/*
223 * Crucial specialization - allows to catch components that are passed with a
224 * TypeWrapper
225 */
226template<typename... Comp>
227class VerticalComponentsVectorTuple<TypeWrapper<Comp...>> :
228 public VerticalComponentsVectorTuple<Comp...>
229{
230};
231
232} // namespace vcl::mesh::detail
233
234#endif // VCL_MESH_CONTAINERS_DETAIL_VERTICAL_COMPONENTS_VECTOR_TUPLE_H
constexpr uint UINT_NULL
The UINT_NULL value represent a null value of uint that is the maximum value that can be represented ...
Definition base.h:48
FirstTypeT< typename detail::ComponentOfTypePred< COMP_ID, Components... >::type > ComponentOfType
Given the ID of a Component and a list of Component types (or a TypeWrapper), this alias sets its typ...
Definition base.h:194