Visual Computing Library
Loading...
Searching...
No Matches
index_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_INDEX_CONTAINER_COMPONENT_H
24#define VCL_MESH_COMPONENTS_BASES_INDEX_CONTAINER_COMPONENT_H
25
26#include "container_component.h"
27
28#include <vclib/mesh/iterators/components/pointer_from_index_iterator.h>
29#include <vclib/space/core/vector.h>
30
31namespace vcl::comp {
32
74template<
75 typename DerivedComponent, // CRTP pattern, derived class
76 uint COMP_ID, // component id
77 typename Elem, // element type for which the indices are stored
78 int N, // container size
79 typename ParentElemType, // parent element type
80 bool VERT, // true if component vertical
81 bool OPT, // true if component vertical and optional
82 bool TTVN> // true if container size tied to vertex number
84 public ContainerComponent<
85 DerivedComponent,
86 COMP_ID,
87 uint,
88 N,
89 void,
90 ParentElemType,
91 VERT,
92 OPT,
93 TTVN,
94 Elem>
95{
98 COMP_ID,
99 uint,
100 N,
101 void,
103 VERT,
104 OPT,
105 TTVN,
106 Elem>;
107
108public:
110 {
111 if constexpr (!Base::IS_VERTICAL) {
112 init();
113 }
114 }
115
116 void init()
117 {
118 if constexpr (N >= 0) {
119 container().fill(UINT_NULL);
120 }
121 }
122
131 template<typename T>
132 const auto& indices() const requires std::is_same_v<T, Elem>
133 {
134 return container();
135 }
136
137protected:
138 using Base::container;
139
140 using ConstIndexIterator = typename Base::ConstIterator;
141 using Iterator =
143 using ConstIterator =
145
146 /*
147 * This member function is called when we need to update the indices in
148 * this container after a reallocation (the pointer of the first element of
149 * the container of Elem is changed).
150 *
151 * With respect to the updateReferences function of the
152 * PointerContainerComponent, we need to do something only when offset is
153 * different from 0, because in this case the indices of the elements have
154 * changed (append case).
155 *
156 * When we append elements, this function is called only on the elements
157 * that have been appended, and the offset is the number of referenced
158 * elements that were in the container before the append operation.
159 */
160 void updateReferences(const Elem*, std::size_t offset = 0)
161 {
162 if (offset != 0) {
163 auto& baseContainer = Base::container();
164
165 for (uint j = 0; j < baseContainer.size(); ++j) {
166 if (baseContainer.at(j) != UINT_NULL) {
167 baseContainer.at(j) += offset;
168 }
169 }
170 }
171 }
172
173 /*
174 * This member function is called when we need to update the pointers or
175 * indices in this containers, usually after a compaction of the container
176 * (but not always).
177 *
178 * In this case, indices must be updated, because the indices of the
179 * elements have changed.
180 */
181 void updateReferences(const std::vector<uint>& newIndices)
182 {
183 auto& baseContainer = Base::container();
184
185 for (uint j = 0; j < baseContainer.size(); ++j) {
186 if (baseContainer.at(j) != UINT_NULL) {
187 baseContainer.at(j) = newIndices.at(baseContainer.at(j));
188 }
189 }
190 }
191};
192
193} // namespace vcl::comp
194
195#endif // VCL_MESH_COMPONENTS_BASES_INDEX_CONTAINER_COMPONENT_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The ContainerComponent class is the base class for all the components of VCLib that store a container...
Definition container_component.h:130
The IndexContainerComponent is the base class for all the components of VCLib that store a container ...
Definition index_container_component.h:95
const auto & indices() const
Exposes the indices of the container.
Definition index_container_component.h:132
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