Visual Computing Library
Loading...
Searching...
No Matches
triangle_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_TRIANGLE_BIT_FLAGS_H
24#define VCL_MESH_COMPONENTS_TRIANGLE_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
80template<typename ParentElemType = void, bool OPT = false>
82 public Component<
83 TriangleBitFlags<ParentElemType, OPT>,
84 CompId::BIT_FLAGS,
85 BitSet<short>,
86 ParentElemType,
87 !std::is_same_v<ParentElemType, void>,
88 OPT>
89{
90 using Base = Component<
92 CompId::BIT_FLAGS,
95 !std::is_same_v<ParentElemType, void>,
96 OPT>;
97
98 using FT = short; // FlagsType, the integral type used for the flags
99
100 static const uint FIRST_USER_BIT = 15;
101
102 // indices of the bits
103 enum {
104 DELETED = 0, // bit 0
105 SELECTED = 1, // bit 1
106 VISITED = 2, // bit 2
107 // Edge border
108 BORDER0 = 3, // bits [3, 5]
109 // Edge selection
110 EDGESEL0 = 6, // bits [6, 8]
111 EDGEVIS0 = 9, // bits [9, 11]
112 // Faux edges: when representing polygonal meshes on triangle meshes,
113 // some triangle edges can be marked as "faux", meaning that they are
114 // internal on the polygon
115 FAUX0 = 12 // bits [12, 14]
116 };
117
118public:
122 inline static const uint USER_BITS_NUMBER = sizeof(FT) * 8 - FIRST_USER_BIT;
123
124 /* Constructors */
125
130 {
131 if constexpr (!Base::IS_VERTICAL) {
132 init();
133 }
134 }
135
146 void init() { flags().reset(); }
147
148 /* Member functions */
149
154 bool deleted() const { return flags()[DELETED]; }
155
161 BitProxy<FT> selected() { return flags()[SELECTED]; }
162
167 bool selected() const { return flags()[SELECTED]; }
168
174 BitProxy<FT> visited() { return flags()[VISITED]; }
175
180 bool visited() const { return flags()[VISITED]; }
181
188 bool onBorder() const
189 {
190 return edgeOnBorder(0) || edgeOnBorder(1) || edgeOnBorder(2);
191 }
192
201 {
202 assert(i < 3);
203 return flags()[BORDER0 + i];
204 }
205
213 bool edgeOnBorder(uint i) const
214 {
215 assert(i < 3);
216 return flags()[BORDER0 + i];
217 }
218
227 {
228 assert(i < 3);
229 return flags()[EDGESEL0 + i];
230 }
231
239 bool edgeSelected(uint i) const
240 {
241 assert(i < 3);
242 return flags()[EDGESEL0 + i];
243 }
244
253 {
254 assert(i < 3);
255 return flags()[EDGEVIS0 + i];
256 }
257
265 bool edgeVisited(uint i) const
266 {
267 assert(i < 3);
268 return flags()[EDGEVIS0 + i];
269 }
270
278 {
279 assert(i < 3);
280 return flags()[FAUX0 + i];
281 }
282
289 bool edgeFaux(uint i) const
290 {
291 assert(i < 3);
292 return flags()[FAUX0 + i];
293 }
294
303 bool userBit(uint bit) const
304 {
306 return flags()[bit + FIRST_USER_BIT];
307 }
308
319 {
321 return flags()[bit + FIRST_USER_BIT];
322 }
323
329 {
330 bool isD = deleted();
331 flags().reset();
332 deletedBit() = isD;
333 }
334
342 {
343 if (f & 0x00000010)
344 visited() = true;
345 if (f & 0x00000020)
346 selected() = true;
347 if (f & 0x00000040)
348 edgeOnBorder(0) = true;
349 if (f & 0x00000080)
350 edgeOnBorder(1) = true;
351 if (f & 0x00000100)
352 edgeOnBorder(2) = true;
353 if (f & 0x00008000)
354 edgeSelected(0) = true;
355 if (f & 0x00010000)
356 edgeSelected(1) = true;
357 if (f & 0x00020000)
358 edgeSelected(2) = true;
359 if (f & 0x00040000)
360 edgeFaux(0) = true;
361 if (f & 0x00080000)
362 edgeFaux(1) = true;
363 if (f & 0x00100000)
364 edgeFaux(2) = true;
365 }
366
375 {
376 int f = 0;
377 if (visited())
378 f &= 0x00000010;
379 if (selected())
380 f &= 0x00000020;
381 if (edgeOnBorder(0))
382 f &= 0x00000040;
383 if (edgeOnBorder(1))
384 f &= 0x00000080;
385 if (edgeOnBorder(2))
386 f &= 0x00000100;
387 if (edgeSelected(0))
388 f &= 0x00008000;
389 if (edgeSelected(1))
390 f &= 0x00010000;
391 if (edgeSelected(2))
392 f &= 0x00020000;
393 if (edgeFaux(0))
394 f &= 0x00040000;
395 if (edgeFaux(1))
396 f &= 0x00080000;
397 if (edgeFaux(2))
398 f &= 0x00100000;
399 return f;
400 }
401
402 // dummy member to discriminate between triangle and non-triangle bit flags
403 void __triangleBitFlags() const {}
404
405protected:
406 BitProxy<FT> deletedBit() { return flags()[DELETED]; }
407
408 // Component interface functions
409 template<typename Element>
410 void importFrom(const Element& e, bool = true)
411 {
412 if constexpr (HasBitFlags<Element>) {
414 if constexpr (HasTriangleBitFlags<Element>) {
415 flags() = e.flags();
416 }
417 else {
418 // BitFlags
419 deletedBit() = e.deleted();
420 selected() = e.selected();
421 visited() = e.visited();
422 const uint UM = std::min(USER_BITS_NUMBER, e.USER_BITS_NUMBER);
423 for (uint i = 0; i < UM; ++i)
424 userBit(i) = e.userBit(i);
425 if constexpr (HasPolygonBitFlags<Element>) {
426 // PolygonBitFlags
427 for (uint i = 0; i < 3; ++i) {
428 edgeOnBorder(i) = e.edgeOnBorder(i);
429 edgeSelected(i) = e.edgeSelected(i);
430 edgeFaux(i) = e.edgeFaux(i);
431 }
432 }
433 }
434 }
435 }
436
437 void serialize(std::ostream& os) const { flags().serialize(os); }
438
439 void deserialize(std::istream& is) { flags().deserialize(is); }
440
441private:
442 // members that allow to access the flags, trough data (horizontal) or
443 // trough parent (vertical)
444 BitSet<FT>& flags() { return Base::data(); }
445
446 BitSet<FT> flags() const { return Base::data(); }
447};
448
449} // namespace vcl::comp
450
451#endif // VCL_MESH_COMPONENTS_TRIANGLE_BIT_FLAGS_H
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The TriangleBitFlags class represents a collection of 16 bits that will be part of a Triangle of a Me...
Definition triangle_bit_flags.h:89
BitProxy< FT > edgeOnBorder(uint i)
Accesses the 'onBorder' bit of the i-th edge of the triangle, returning a reference to it.
Definition triangle_bit_flags.h:200
int exportFlagsToVCGFormat() const
Returns the bit flags of this element in the format of the VCG library.
Definition triangle_bit_flags.h:374
void importFlagsFromVCGFormat(int f)
Sets all the flags of this element to the values contained in the integer input parameter,...
Definition triangle_bit_flags.h:341
bool onBorder() const
Returns whether the current Triangle is on border or not, by checking whether at least one of its thr...
Definition triangle_bit_flags.h:188
bool visited() const
Returns whether the current Triangle has been visited or not.
Definition triangle_bit_flags.h:180
bool userBit(uint bit) const
Returns a reference to the value of the user bit of this Triangle given in input. The bit is checked ...
Definition triangle_bit_flags.h:303
bool edgeVisited(uint i) const
Returns whether the i-th edge of the current triangle is visited or not.
Definition triangle_bit_flags.h:265
bool edgeFaux(uint i) const
Returns whether the i-th edge of the current triangle is faux or not.
Definition triangle_bit_flags.h:289
static const uint USER_BITS_NUMBER
Static number of bits that can have custom meanings to the user.
Definition triangle_bit_flags.h:122
bool selected() const
Returns whether the current Triangle is selected or not.
Definition triangle_bit_flags.h:167
void init()
Initializes the bits to false.
Definition triangle_bit_flags.h:146
bool edgeSelected(uint i) const
Returns whether the i-th edge of the current triangle is selected or not.
Definition triangle_bit_flags.h:239
BitProxy< FT > selected()
Accesses the 'selected' bit of this Triangle, returning a reference to it.
Definition triangle_bit_flags.h:161
BitProxy< FT > edgeVisited(uint i)
Accesses the 'visited' bit of the i-th edge of the triangle, returning a reference to it.
Definition triangle_bit_flags.h:252
BitProxy< FT > visited()
Accesses the 'visited' bit of this Triangle, returning a reference to it.
Definition triangle_bit_flags.h:174
BitProxy< FT > userBit(uint bit)
Returns the boolean value of the user bit of this Triangle given in input. The bit is checked to be l...
Definition triangle_bit_flags.h:318
void resetBitFlags()
Unsets all the flags of this Triangle and sets them to false, except the deleted flag,...
Definition triangle_bit_flags.h:328
BitProxy< FT > edgeSelected(uint i)
Accesses the 'selected' bit of the i-th edge of the triangle, returning a reference to it.
Definition triangle_bit_flags.h:226
bool deleted() const
Returns whether the current Triangle is deleted or not.
Definition triangle_bit_flags.h:154
BitProxy< FT > edgeFaux(uint i)
Accesses the 'faux' bit of the i-th edge of the triangle, returning a reference to it.
Definition triangle_bit_flags.h:277
TriangleBitFlags()
Initializes the bits to false.
Definition triangle_bit_flags.h:129
bool edgeOnBorder(uint i) const
Returns whether the i-th edge of the current triangle is on border or not.
Definition triangle_bit_flags.h:213