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