Visual Computing Library
Loading...
Searching...
No Matches
container_component.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_COMPONENTS_BASES_CONTAINER_COMPONENT_H
24#define VCL_MESH_COMPONENTS_BASES_CONTAINER_COMPONENT_H
25
26#include "component.h"
27
28#include <vclib/space/core/vector.h>
29
30namespace vcl::comp {
31
32namespace detail {
33
34// small alias to differentiate between components having only a container or
35// components having a container and an additional data type:
36// - if AD is void, no additional data and the component will have only a Vector
37// - if AD is not void, there is additional data, and the component will have
38// a tuple containing a Vector and the additional data
39template<
40 typename DC,
41 uint CT,
42 typename T,
43 int N,
44 typename AD,
45 typename El,
46 bool v,
47 bool o,
48 bool TT,
49 typename... PT>
50using ContCompBase = std::conditional_t<
51 std::is_same_v<AD, void>,
52 Component<DC, CT, Vector<T, N>, El, v, o, PT...>,
53 Component<DC, CT, std::tuple<Vector<T, N>, AD>, El, v, o, PT...>>;
54
55} // namespace detail
56
107template<
108 typename DerivedComponent, // CRTP pattern, derived class
109 uint COMP_ID, // component id
110 typename T, // data stored in container
111 int N, // container size
112 typename AdditionalData, // additional data outside container
113 typename ParentElemType, // parent element type
114 bool VERT, // true if component vertical
115 bool OPT, // true if component vertical and optional
116 bool TTVN, // true if container size tied to vertex number
117 typename... PointedTypes> // types of pointers stored by the component
119 public detail::ContCompBase<
120 DerivedComponent,
121 COMP_ID,
122 T,
123 N,
124 AdditionalData,
125 ParentElemType,
126 VERT,
127 OPT,
128 TTVN,
129 PointedTypes...>
130{
131 static constexpr bool HAS_ADDITIONAL_DATA =
132 !std::is_same_v<AdditionalData, void>;
133
134 using Base = detail::ContCompBase<
136 COMP_ID,
137 T,
138 N,
141 VERT,
142 OPT,
143 TTVN,
144 PointedTypes...>;
145
146public:
157 static const bool TIED_TO_VERTEX_NUMBER = TTVN;
158
159 static const int SIZE = N;
160
161protected:
162 /* Iterator Types declaration */
163
164 using Iterator = Vector<T, N>::Iterator;
165 using ConstIterator = Vector<T, N>::ConstIterator;
166
167 /* Constructor */
168
169 /*
170 * Create a container of T objects.
171 * If this Container is a static array, all its element will be initialized
172 * to T(). If this Container is a dynamic vector, it will be an empty
173 * container.
174 */
176 {
177 if constexpr (!Base::IS_VERTICAL) {
178 if constexpr (N >= 0) {
179 Base::data().fill(T());
180 }
181 }
182 }
183
184 /*
185 * Create a container of Objects.
186 * If this Container is a static array, all its element will be initialized
187 * to T(). If this Container is a dynamic vector, it will be an empty
188 * container.
189 */
190 void init()
191 {
192 if constexpr (N >= 0) {
193 // I'll use the array, N is >= 0.
194 // There will be a static number of objects.
195 container().fill(T());
196 }
197 else {
198 // I'll use the vector, because N is < 0.
199 // There will be a dynamic number of objects.
200 container().clear();
201 }
202 }
203
204 Vector<T, N>& container()
205 {
206 if constexpr (HAS_ADDITIONAL_DATA) {
207 return std::get<0>(Base::data());
208 }
209 else {
210 return Base::data();
211 }
212 }
213
214 const Vector<T, N>& container() const
215 {
216 if constexpr (HAS_ADDITIONAL_DATA) {
217 return std::get<0>(Base::data());
218 }
219 else {
220 return Base::data();
221 }
222 }
223
224 template<typename AdDt = AdditionalData>
225 AdDt& additionalData() requires (HAS_ADDITIONAL_DATA)
226 {
227 return std::get<1>(Base::data());
228 }
229
230 template<typename AdDt = AdditionalData>
231 const AdDt& additionalData() const requires (HAS_ADDITIONAL_DATA)
232 {
233 return std::get<1>(Base::data());
234 }
235};
236
237} // namespace vcl::comp
238
239#endif // VCL_MESH_COMPONENTS_BASES_CONTAINER_COMPONENT_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Container::iterator Iterator
An iterator to the elements of the Vector.
Definition vector.h:102
Container::const_iterator ConstIterator
A const iterator to the elements of the Vector.
Definition vector.h:105
The ContainerComponent class is the base class for all the components of VCLib that store a container...
Definition container_component.h:130
static const bool TIED_TO_VERTEX_NUMBER
Boolean that tells if this component stores a container having its size tied to the number of the ver...
Definition container_component.h:157