Visual Computing Library
Loading...
Searching...
No Matches
element.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_ELEMENTS_ELEMENT_H
24#define VCL_MESH_ELEMENTS_ELEMENT_H
25
26#include <vclib/concepts/mesh/elements/element.h>
27
28#include <vclib/concepts/mesh/components/component.h>
29#include <vclib/mesh/components/bases/component.h>
30#include <vclib/mesh/components/parent_mesh_pointer.h>
31#include <vclib/types.h>
32
41namespace vcl::mesh {
42
43template<ElementConcept>
45
46} // namespace vcl::mesh
47
48namespace vcl {
49
55template<uint ELEM_ID, typename MeshType, typename... Comps>
56class Element : public comp::ParentMeshPointer<MeshType>, public Comps...
57{
58 template<ElementConcept>
59 friend class mesh::ElementContainer;
60
61public:
62 using ParentMeshType = MeshType;
63
69 using Components =
70 FilterTypesByCondition<comp::IsComponentPred, TypeWrapper<Comps...>>::
71 type;
72
73 static const uint ELEMENT_ID = ELEM_ID;
74
75 uint index() const
76 {
79 ->template elementIndex<ELEM_ID>(this);
80 }
81
82 template<uint COMP_ID>
83 auto& component()
84 {
85 using Comp = GetComponentFromID<COMP_ID>::type;
86 return *static_cast<Comp*>(this);
87 }
88
89 template<uint COMP_ID>
90 const auto& component() const
91 {
92 using Comp = GetComponentFromID<COMP_ID>::type;
93 return *static_cast<const Comp*>(this);
94 }
95
96 template<typename ElType>
97 void importFrom(const ElType& v, bool importRefs = true)
98 {
99 // we need to call importFrom for each component of the Element,
100 // but importFrom must be called only on components that are
101 // available (e.g. optional and not enabled)
102 (importComponent<Comps>(v, importRefs), ...);
103 }
104
105 void serialize(std::ostream& out) const
106 {
107 // we need to call serialize for each component of the Element,
108 // but serialize must be called only on components that are
109 // available (e.g. optional and not enabled)
110 (serializeComponent<Comps>(out), ...);
111 }
112
113 void deserialize(std::istream& in)
114 {
115 // we need to call deserialize for each component of the Element,
116 // but deserialize must be called only on components that are
117 // available (e.g. optional and not enabled)
118 (deserializeComponent<Comps>(in), ...);
119 }
120
121private:
122 // hide init and isAvailable members
123 void init() {}
124
125 bool isAvailable() const { return true; }
126
127 // init to call after set parent mesh
128 void initVerticalComponents() { (construct<Comps>(), ...); }
129
130 template<typename Comp>
131 void construct()
132 {
133 if constexpr (
134 comp::IsVerticalComponent<Comp> &&
135 comp::HasInitMemberFunction<Comp>) {
136 if constexpr (comp::HasIsAvailableMemberFunction<Comp>) {
137 if (Comp::isAvailable()) {
138 Comp::init();
139 }
140 }
141 // no possibility to check if is available, it means that is always
142 // available
143 else {
144 Comp::init();
145 }
146 }
147 }
148
149 template<typename Comp, typename ElType>
150 void importComponent(const ElType& v, bool importRefs)
151 {
152 if constexpr (!comp::IsOptionalComponent<Comp>) {
153 Comp::importFrom(v, importRefs); // safe to call importFrom
154 }
155 else { // it is optional...
156 if (Comp::isAvailable()) { // check if it is available
157 Comp::importFrom(v, importRefs);
158 }
159 }
160 }
161
162 template<typename Comp>
163 void serializeComponent(std::ostream& out) const
164 {
165 if constexpr (!comp::IsOptionalComponent<Comp>) {
166 Comp::serialize(out); // safe to call serialize
167 }
168 else { // it is optional...
169 if (Comp::isAvailable()) { // check if it is available
170 Comp::serialize(out);
171 }
172 }
173 }
174
175 template<typename Comp>
176 void deserializeComponent(std::istream& in)
177 {
178 if constexpr (!comp::IsOptionalComponent<Comp>) {
179 Comp::deserialize(in); // safe to call deserialize
180 }
181 else { // it is optional...
182 if (Comp::isAvailable()) { // check if it is available
183 Comp::deserialize(in);
184 }
185 }
186 }
187
188 // Predicate structures
189
190 // Components can be individuated with their ID, which is an unsigned int.
191 // This struct sets its bool `value` to true if this Element has a Component
192 // with the given unsigned integer COMP_ID. Sets also `type` with a
193 // TypeWrapper containing the Component that satisfied the condition. The
194 // TypeWrapper will be empty if no Components were found.
195 template<uint COMP_ID>
197 {
198 private:
199 template<typename Cont>
201 {
202 static constexpr bool value = Cont::COMPONENT_ID == COMP_ID;
203 };
204
205 public:
206 // TypeWrapper of the found component, if any
208 static constexpr bool value = NumberOfTypes<type>::value == 1;
209 };
210
211 template<uint COMP_ID>
213 {
214 private:
215 template<typename>
217 {
218 };
219
220 template<typename C>
222 {
223 using type = C;
224 };
225
226 public:
227 using type =
229 };
230};
231
232} // namespace vcl
233
234#endif // VCL_MESH_ELEMENTS_ELEMENT_H
The Element class.
Definition element.h:57
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The ParentMeshPointer class.
Definition parent_mesh_pointer.h:34
Definition element.h:44
Definition element.h:197
Definition element.h:213
Removes all types that do not satisfy a condition, and get them as a tuple.
Definition filter_types.h:72
Definition variadic_templates.h:143
A simple structure that wraps a list of variadic templates, without instantiating anything....
Definition type_wrapper.h:41