Visual Computing Library
Loading...
Searching...
No Matches
polygon_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_POLYGON_BIT_FLAGS_H
24#define VCL_MESH_COMPONENTS_POLYGON_BIT_FLAGS_H
25
26#include "bases/container_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
97template<int N, typename ParentElemType = void, bool OPT = false>
99 public ContainerComponent<
100 PolygonBitFlags<N, ParentElemType, OPT>,
101 CompId::BIT_FLAGS,
102 BitSet<char>,
103 N,
104 BitSet<char>,
105 ParentElemType,
106 !std::is_same_v<ParentElemType, void>,
107 OPT,
108 true>
109{
110 using FT = char; // FlagsType, the integral type used for the flags
111
112 using Base = ContainerComponent<
114 CompId::BIT_FLAGS,
116 N,
119 !std::is_same_v<ParentElemType, void>,
120 OPT,
121 true>;
122
123 static const uint FIRST_USER_BIT = 6;
124 static const uint FIRST_EDGE_USER_BIT = 3;
125
126 // indices of the bits
127 enum {
128 DELETED = 0, // bit 0
129 SELECTED = 1, // bit 1
130 VISITED = 2, // bit 2
131 // Faux edges, for portability with TriangleBits
132 FAUX0 = 3 // bits [3, 5]
133 };
134
135 // indices of the bits used for each edge of the polygon
136 enum { EDGEBORD = 0, EDGESEL = 1, EDGEVIS = 2 };
137
138public:
142 inline static const uint USER_BITS_NUMBER = sizeof(FT) * 8 - FIRST_USER_BIT;
143
148 static const uint EDGE_USER_BITS_NUMBER =
149 sizeof(FT) * 8 - FIRST_EDGE_USER_BIT;
150
151 /* Constructors */
152
160 {
161 if constexpr (!Base::IS_VERTICAL) {
162 init();
163 }
164 }
165
176 void init() { flags().reset(); }
177
178 /* Member functions */
179
184 bool deleted() const { return flags()[DELETED]; }
185
191 BitProxy<FT> selected() { return flags()[SELECTED]; }
192
197 bool selected() const { return flags()[SELECTED]; }
198
204 BitProxy<FT> visited() { return flags()[VISITED]; }
205
210 bool visited() const { return flags()[VISITED]; }
211
218 bool onBorder() const
219 {
220 for (uint i = 0; i < 12; ++i)
221 if (edgeOnBorder(i))
222 return true;
223 return false;
224 }
225
234 {
235 assert(i < edgeFlags().size());
236 return edgeFlags()[i][EDGEBORD];
237 }
238
246 bool edgeOnBorder(uint i) const
247 {
248 assert(i < edgeFlags().size());
249 return edgeFlags()[i][EDGEBORD];
250 }
251
260 {
261 assert(i < edgeFlags().size());
262 return edgeFlags()[i][EDGESEL];
263 }
264
272 bool edgeSelected(uint i) const
273 {
274 assert(i < edgeFlags().size());
275 return edgeFlags()[i][EDGESEL];
276 }
277
285 {
286 assert(i < edgeFlags().size());
287 return edgeFlags()[i][EDGEVIS];
288 }
289
297 bool edgeVisited(uint i) const
298 {
299 assert(i < edgeFlags().size());
300 return edgeFlags()[i][EDGEVIS];
301 }
302
316 {
317 assert(i < 3);
318 return flags()[FAUX0 + i];
319 }
320
333 bool edgeFaux(uint i) const
334 {
335 assert(i < 3);
336 return flags()[FAUX0 + i];
337 }
338
348 bool userBit(uint bit) const
349 {
351 return flags()[bit + FIRST_USER_BIT];
352 }
353
363 {
365 return flags()[bit + FIRST_USER_BIT];
366 }
367
377 bool edgeUserBit(uint i, uint bit) const
378 {
380 return edgeFlags()[i][bit + FIRST_EDGE_USER_BIT];
381 }
382
393 {
395 return edgeFlags()[i][bit + FIRST_EDGE_USER_BIT];
396 }
397
403 {
404 bool isD = deleted();
405 flags().reset();
406 for (uint i = 0; i < edgeFlags().size(); ++i)
407 edgeFlags()[i].reset();
408 deletedBit() = isD;
409 }
410
418 {
419 if (f & 0x00000010)
420 visited() = true;
421 if (f & 0x00000020)
422 selected() = true;
423 if (f & 0x00000040)
424 edgeOnBorder(0) = true;
425 if (f & 0x00000080)
426 edgeOnBorder(1) = true;
427 if (f & 0x00000100)
428 edgeOnBorder(2) = true;
429 if (f & 0x00008000)
430 edgeSelected(0) = true;
431 if (f & 0x00010000)
432 edgeSelected(1) = true;
433 if (f & 0x00020000)
434 edgeSelected(2) = true;
435 if (f & 0x00040000)
436 edgeFaux(0) = true;
437 if (f & 0x00080000)
438 edgeFaux(1) = true;
439 if (f & 0x00100000)
440 edgeFaux(2) = true;
441 }
442
451 {
452 int f = 0;
453 if (visited())
454 f &= 0x00000010;
455 if (selected())
456 f &= 0x00000020;
457 if (edgeOnBorder(0))
458 f &= 0x00000040;
459 if (edgeOnBorder(1))
460 f &= 0x00000080;
461 if (edgeOnBorder(2))
462 f &= 0x00000100;
463 if (edgeSelected(0))
464 f &= 0x00008000;
465 if (edgeSelected(1))
466 f &= 0x00010000;
467 if (edgeSelected(2))
468 f &= 0x00020000;
469 if (edgeFaux(0))
470 f &= 0x00040000;
471 if (edgeFaux(1))
472 f &= 0x00080000;
473 if (edgeFaux(2))
474 f &= 0x00100000;
475 return f;
476 }
477
478 // dummy member to discriminate between triangle and non-triangle bit flags
479 void __polygonBitFlags() const {}
480
481protected:
482 BitProxy<FT> deletedBit() { return flags()[DELETED]; }
483
484 // Component interface functions
485 template<typename Element>
486 void importFrom(const Element& e, bool = true)
487 {
488 if constexpr (HasBitFlags<Element>) {
490 if constexpr (HasPolygonBitFlags<Element>) {
491 flags() = e.flags();
492 edgeFlags() = e.edgeFlags();
493 }
494 else {
495 // BitFlags
496 deletedBit() = e.deleted();
497 selected() = e.selected();
498 visited() = e.visited();
499 const uint UM = std::min(USER_BITS_NUMBER, e.USER_BITS_NUMBER);
500 for (uint i = 0; i < UM; ++i)
501 userBit(i) = e.userBit(i);
502 if constexpr (HasTriangleBitFlags<Element>) {
503 // TriangleBitFlags
504 for (uint i = 0; i < 3; ++i) {
505 edgeOnBorder(i) = e.edgeOnBorder(i);
506 edgeSelected(i) = e.edgeSelected(i);
507 edgeVisited(i) = e.edgeVisited(i);
508 edgeFaux(i) = e.edgeFaux(i);
509 }
510 }
511 }
512 }
513 }
514
515 void serialize(std::ostream& os) const
516 {
517 flags().serialize(os);
518 edgeFlags().serialize(os);
519 }
520
521 void deserialize(std::istream& is)
522 {
523 flags().deserialize(is);
524 edgeFlags().deserialize(is);
525 }
526
527 // ContainerComponent interface functions
528 void resize(uint n) requires (N < 0) { edgeFlags().resize(n); }
529
530 void pushBack(BitSet<FT> f = BitSet<FT>()) requires (N < 0)
531 {
532 edgeFlags().pushBack(f);
533 }
534
535 void insert(uint i, BitSet<FT> f = BitSet<FT>()) requires (N < 0)
536 {
537 edgeFlags().insert(i, f);
538 }
539
540 void erase(uint i) requires (N < 0) { edgeFlags().erase(i); }
541
542 void clear() requires (N < 0) { edgeFlags().clear(); }
543
544private:
545 // members that allow to access the flags, trough data (horizontal) or
546 // trough parent (vertical)
547
548 BitSet<FT>& flags() { return Base::additionalData(); }
549
550 const BitSet<FT>& flags() const { return Base::additionalData(); }
551
552 Vector<BitSet<FT>, -1>& edgeFlags() { return Base::container(); }
553
554 const Vector<BitSet<FT>, -1>& edgeFlags() const
555 {
556 return Base::container();
557 }
558};
559
560} // namespace vcl::comp
561
562#endif // VCL_MESH_COMPONENTS_POLYGON_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 ContainerComponent class is the base class for all the components of VCLib that store a container...
Definition container_component.h:130
The PolygonBitFlags class represents a collection of 8 bits plus 8 bits for each edge that will be pa...
Definition polygon_bit_flags.h:109
bool onBorder() const
Returns whether the current Polygon is on border or not, by checking whether at least one of its edge...
Definition polygon_bit_flags.h:218
BitProxy< FT > edgeUserBit(uint i, uint bit)
Returns a reference to the value of the user bit of the i-th edge of this Polygon given in input....
Definition polygon_bit_flags.h:392
BitProxy< FT > edgeFaux(uint i)
Accesses the 'faux' bit of the i-th edge of the polygon, returning a reference to it.
Definition polygon_bit_flags.h:315
void importFlagsFromVCGFormat(int f)
Sets all the flags of this element to the values contained in the integer input parameter,...
Definition polygon_bit_flags.h:417
PolygonBitFlags()
Initializes all the bits to false.
Definition polygon_bit_flags.h:159
void init()
Initializes the bits to false.
Definition polygon_bit_flags.h:176
bool edgeOnBorder(uint i) const
Returns whether the i-th edge of the current Polygon is on border or not.
Definition polygon_bit_flags.h:246
bool edgeSelected(uint i) const
Returns whether the i-th edge of the current Polygon is selected or not.
Definition polygon_bit_flags.h:272
void resetBitFlags()
Unsets all the flags of this Polygon and sets them to false, except the deleted flag,...
Definition polygon_bit_flags.h:402
bool edgeVisited(uint i) const
Returns whether the i-th edge of the current Polygon has been visited or not.
Definition polygon_bit_flags.h:297
BitProxy< FT > visited()
Accesses the 'visited' bit of this Polygon, returning a reference to it.
Definition polygon_bit_flags.h:204
bool visited() const
Returns whether the current Polygon has been visited or not.
Definition polygon_bit_flags.h:210
bool edgeFaux(uint i) const
Returns whether the i-th edge of the current Polygon is faux or not.
Definition polygon_bit_flags.h:333
bool deleted() const
Returns whether the current Polygon is deleted or not.
Definition polygon_bit_flags.h:184
BitProxy< FT > edgeOnBorder(uint i)
Accesses the 'onBorder' bit of the i-th edge of the polygon, returning a reference to it.
Definition polygon_bit_flags.h:233
static const uint USER_BITS_NUMBER
Static number of bits that can have custom meanings to the user.
Definition polygon_bit_flags.h:142
BitProxy< FT > edgeVisited(uint i)
Accesses the 'visited' bit of the i-th edge of the polygon, returning a reference to it.
Definition polygon_bit_flags.h:284
static const uint EDGE_USER_BITS_NUMBER
Static number of bits for each edge that can have custom meanings to the user.
Definition polygon_bit_flags.h:148
int exportFlagsToVCGFormat() const
Returns the bit flags of this element in the format of the VCG library.
Definition polygon_bit_flags.h:450
BitProxy< FT > selected()
Accesses the 'selected' bit of this Polygon, returning a reference to it.
Definition polygon_bit_flags.h:191
bool edgeUserBit(uint i, uint bit) const
Returns the boolean value of the user bit of the i-th edge of this Polygon given in input....
Definition polygon_bit_flags.h:377
BitProxy< FT > edgeSelected(uint i)
Accesses the 'selected' bit of the i-th edge of the polygon, returning a reference to it.
Definition polygon_bit_flags.h:259
BitProxy< FT > userBit(uint bit)
Returns a reference to the value of the user bit of this Polygon given in input. The bit is checked t...
Definition polygon_bit_flags.h:362
bool userBit(uint bit) const
Returns the boolean value of the user bit of this Polygon given in input. The bit is checked to be le...
Definition polygon_bit_flags.h:348
bool selected() const
Returns whether the current Polygon is selected or not.
Definition polygon_bit_flags.h:197