Visual Computing Library
Loading...
Searching...
No Matches
bit_flags.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_BIT_FLAGS_H
24#define VCL_MESH_COMPONENTS_BIT_FLAGS_H
25
26#include "bases/component.h"
27
28#include <vclib/concepts/mesh/components/bit_flags.h>
29#include <vclib/space/core/bit_set.h>
30
31namespace vcl::comp {
32
70template<typename ParentElemType = void, bool OPT = false>
71class BitFlags :
72 public Component<
73 BitFlags<ParentElemType, OPT>,
74 CompId::BIT_FLAGS,
75 BitSet<char>,
76 ParentElemType,
77 !std::is_same_v<ParentElemType, void>,
78 OPT>
79{
80 using Base = Component<
82 CompId::BIT_FLAGS,
85 !std::is_same_v<ParentElemType, void>,
86 OPT>;
87
88 using FT = char; // FlagsType, the integral type used for the flags
89
90 // indices of the bits
91 enum {
92 DELETED = 0, // bit 0
93 SELECTED = 1, // bit 1
94 BORDER = 2, // bit 2
95 VISITED = 3 // bit 3
96 };
97
98 static const uint FIRST_USER_BIT = 4;
99
100public:
104 inline static const uint USER_BITS_NUMBER = sizeof(FT) * 8 - FIRST_USER_BIT;
105
106 /* Constructors */
107
112 {
113 if constexpr (!Base::IS_VERTICAL) {
114 init();
115 }
116 }
117
128 void init() { flags().reset(); }
129
130 /* Member functions */
131
136 bool deleted() const { return flags()[DELETED]; }
137
143 BitProxy<FT> selected() { return flags()[SELECTED]; }
144
149 bool selected() const { return flags()[SELECTED]; }
150
156 BitProxy<FT> onBorder() { return flags()[BORDER]; }
157
162 bool onBorder() const { return flags()[BORDER]; }
163
169 BitProxy<FT> visited() { return flags()[VISITED]; }
170
175 bool visited() const { return flags()[VISITED]; }
176
185 bool userBit(uint bit) const
186 {
188 return flags()[bit + FIRST_USER_BIT];
189 }
190
201 {
203 return flags()[bit + FIRST_USER_BIT];
204 }
205
211 {
212 bool isD = deleted();
213 flags().reset();
214 deletedBit() = isD;
215 }
216
224 {
226 if (f & 0x0010)
227 visited() = true;
228 if (f & 0x0020)
229 selected() = true;
230 if (f & 0x0100)
231 onBorder() = true;
232 }
233
242 {
243 int f = 0;
244 if (visited())
245 f &= 0x0010;
246 if (selected())
247 f &= 0x0020;
248 if (onBorder())
249 f &= 0x0100;
250 return f;
251 }
252
253protected:
254 BitProxy<FT> deletedBit() { return flags()[DELETED]; }
255
256 // Component interface functions
257 template<typename Element>
258 void importFrom(const Element& e, bool = true)
259 {
260 if constexpr (HasBitFlags<Element>) {
262 if constexpr (
264 deletedBit() = e.deleted();
265 selected() = e.selected();
266 visited() = e.visited();
267 onBorder() = e.onBorder();
268 const uint UM = std::min(USER_BITS_NUMBER, e.USER_BITS_NUMBER);
269 for (uint i = 0; i < UM; ++i)
270 userBit(i) = e.userBit(i);
271 }
272 else {
273 flags() = e.flags();
274 }
275 }
276 }
277
278 void serialize(std::ostream& os) const { flags().serialize(os); }
279
280 void deserialize(std::istream& is) { flags().deserialize(is); }
281
282private:
283 // members that allow to access the flags, trough data (horizontal) or
284 // trough parent (vertical)
285 BitSet<FT>& flags() { return Base::data(); }
286
287 BitSet<FT> flags() const { return Base::data(); }
288};
289
290} // namespace vcl::comp
291
292#endif // VCL_MESH_COMPONENTS_BIT_FLAGS_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 BitFlags component class represents a collection of 8 bits that will be part of an Element (e....
Definition bit_flags.h:79
void init()
Initializes the bits to false.
Definition bit_flags.h:128
bool onBorder() const
Returns whether the current Element is on border or not.
Definition bit_flags.h:162
void importFlagsFromVCGFormat(int f)
Sets all the flags of this element to the values contained in the integer input parameter,...
Definition bit_flags.h:223
bool deleted() const
Returns whether the current Element is deleted or not.
Definition bit_flags.h:136
BitFlags()
Initializes the bits to false.
Definition bit_flags.h:111
BitProxy< FT > userBit(uint bit)
Returns the boolean value of the user bit of this Element given in input. The bit is checked to be le...
Definition bit_flags.h:200
bool visited() const
Returns whether the current Element has been visited or not.
Definition bit_flags.h:175
BitProxy< FT > onBorder()
Accesses the 'onBorder' bit of this Element, returning a reference to it.
Definition bit_flags.h:156
void resetBitFlags()
Unsets all the flags of this Element and sets them to false, except the deleted flag,...
Definition bit_flags.h:210
BitProxy< FT > selected()
Accesses the 'selected' bit of this Element, returning a reference to it.
Definition bit_flags.h:143
bool selected() const
Returns whether the current Element is selected or not.
Definition bit_flags.h:149
static const uint USER_BITS_NUMBER
Static number of bits that can have custom meanings to the user.
Definition bit_flags.h:104
BitProxy< FT > visited()
Accesses the 'visited' bit of this Element, returning a reference to it.
Definition bit_flags.h:169
bool userBit(uint bit) const
Returns a reference to the value of the user bit of this Element given in input. The bit is checked t...
Definition bit_flags.h:185
int exportFlagsToVCGFormat() const
Returns the bit flags of this element in the format of the VCG library.
Definition bit_flags.h:241