Wildmeshing Toolkit
Loading...
Searching...
No Matches
SurfaceTagAttributes.h
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cstddef>
6#include <cstdint>
7
8namespace wmtk {
9
21using InputSet = uint64_t;
22
24{
25 // Eleven bytes. The values are tiny -- a flag, a bbox side in [-1, 5], a class in {0, 1}, an
26 // orientation count that is +-1 or 0 almost everywhere, a 64-bit input set -- but as int the
27 // struct was 12 bytes, and the 3D meshes carry four of these per tet slot (the 2D ones three
28 // per triangle slot) in storage preallocated several times over: on the largest tetwild
29 // inputs that alone was most of a gigabyte. The class and the orientation share a byte as
30 // bitfields; both are int8_t so every compiler packs them together. The input set took it
31 // from 3 to 5 bytes, 8 more per tet slot: on Thingi10K 46024, the largest tetwild input of
32 // the challenging set (156k tets after insertion, two iterations), peak RSS went from 1006
33 // to 1017 MB (+1.1%), the simplification's per-face input id included. 64 bits rather than 16
34 // so that simwild can give every label its own bit -- each tet tag's region as well as each
35 // face tag -- and hold every surface face to its own labels' envelopes: segmented images
36 // carry more than 16 labels. That is 6 bytes more per face record: on the same
37 // 46024, one input, serial, peak RSS 1001 MB on main (3 bytes) and 1007 MB here (11 bytes).
38public:
39 // Bitfields cannot take default member initializers before C++20.
42 , m_orientation(0)
43 {}
44
46 bool m_is_surface_fs = false;
48 int8_t m_is_bbox_fs = -1;
49
65 int8_t m_surface_class : 3;
66
96 int8_t m_orientation : 5;
97 static constexpr int max_orientation = 15;
98
119 InputSet input_set() const
120 {
121 InputSet s = 0;
122 for (size_t i = 0; i < sizeof(InputSet); ++i) s |= InputSet(m_input_set[i]) << (8 * i);
123 return s;
124 }
125 void set_input_set(InputSet s)
126 {
127 for (size_t i = 0; i < sizeof(InputSet); ++i) m_input_set[i] = uint8_t(s >> (8 * i));
128 }
130 static constexpr int max_inputs = int(8 * sizeof(InputSet));
131 uint8_t m_input_set[sizeof(InputSet)] = {};
132
134 template <size_t N>
135 static int ascending_parity(const std::array<size_t, N>& v)
136 {
137 int inversions = 0;
138 for (size_t i = 0; i < N; ++i)
139 for (size_t j = i + 1; j < N; ++j)
140 if (v[i] > v[j]) ++inversions;
141 return (inversions % 2 == 0) ? 1 : -1;
142 }
143
145 void set_orientation(int o)
146 {
147 m_orientation = int8_t(std::clamp(o, -max_orientation, max_orientation));
148 }
149
151 template <size_t N>
152 int orientation_along(const std::array<size_t, N>& v) const
153 {
155 }
156
158 template <size_t N>
159 void set_orientation_along(const std::array<size_t, N>& v, int o)
160 {
162 }
163
164 void reset()
165 {
166 m_is_surface_fs = false;
167 m_is_bbox_fs = -1;
168 m_surface_class = 0;
169 m_orientation = 0;
170 set_input_set(0);
171 }
172
175 void merge(const SurfaceTagAttributes& attr)
176 {
178 if (attr.m_is_bbox_fs >= 0) m_is_bbox_fs = attr.m_is_bbox_fs;
179 if (attr.m_surface_class != 0) m_surface_class = attr.m_surface_class;
180 set_input_set(input_set() | attr.input_set());
181 }
182};
183
184static_assert(
185 sizeof(SurfaceTagAttributes) == 3 + sizeof(InputSet),
186 "keep the surface tags byte-sized");
187
188} // namespace wmtk
Definition SurfaceTagAttributes.h:24
int8_t m_is_bbox_fs
Which bbox side this simplex is on; -1 for none.
Definition SurfaceTagAttributes.h:48
int8_t m_orientation
The input's orientation on this simplex: a signed count of input sheets lying on it,...
Definition SurfaceTagAttributes.h:96
int orientation_along(const std::array< size_t, N > &v) const
The orientation measured against the simplex ordered as v (which must be its vertices).
Definition SurfaceTagAttributes.h:152
InputSet input_set() const
The INPUT SET: which inputs this simplex covers, bit i for input i. At most max_inputs,...
Definition SurfaceTagAttributes.h:119
static int ascending_parity(const std::array< size_t, N > &v)
+1 if v is an even permutation of its ascending sort, -1 if odd. N is 2 or 3.
Definition SurfaceTagAttributes.h:135
static constexpr int max_inputs
One input per bit of InputSet: the width is set there, and only there.
Definition SurfaceTagAttributes.h:130
void set_orientation(int o)
Store an orientation already measured against the ascending vertex order, clamped.
Definition SurfaceTagAttributes.h:145
bool m_is_surface_fs
Is this simplex part of the tracked surface.
Definition SurfaceTagAttributes.h:46
void set_orientation_along(const std::array< size_t, N > &v, int o)
Store orientation o, measured against the simplex ordered as v.
Definition SurfaceTagAttributes.h:159
int8_t m_surface_class
Which tracked surface this simplex belongs to, for applications that track more than one....
Definition SurfaceTagAttributes.h:65
void merge(const SurfaceTagAttributes &attr)
Definition SurfaceTagAttributes.h:175