17#ifndef DRACO_COMPRESSION_POINT_CLOUD_ALGORITHMS_DYNAMIC_INTEGER_POINTS_KD_TREE_DECODER_H_
18#define DRACO_COMPRESSION_POINT_CLOUD_ALGORITHMS_DYNAMIC_INTEGER_POINTS_KD_TREE_DECODER_H_
37template <
int compression_level_t>
40 compression_level_t - 1> {};
70template <
int compression_level_t>
72 static_assert(compression_level_t >= 0,
"Compression level must in [0..6].");
73 static_assert(compression_level_t <= 6,
"Compression level must in [0..6].");
78 typedef typename Policy::NumbersDecoder NumbersDecoder;
79 typedef typename Policy::AxisDecoder AxisDecoder;
80 typedef typename Policy::HalfDecoder HalfDecoder;
81 typedef typename Policy::RemainingBitsDecoder RemainingBitsDecoder;
82 typedef std::vector<uint32_t> VectorUint32;
88 num_decoded_points_(0),
100 template <
class OutputIteratorT>
103 template <
class OutputIteratorT>
105 uint32_t oit_max_points);
108 template <
class OutputIteratorT>
110 template <
class OutputIteratorT>
112 uint32_t oit_max_points);
121 uint32_t GetAxis(uint32_t num_remaining_points,
const VectorUint32 &levels,
124 template <
class OutputIteratorT>
125 bool DecodeInternal(uint32_t num_points, OutputIteratorT &oit);
127 void DecodeNumber(
int nbits, uint32_t *value) {
128 numbers_decoder_.DecodeLeastSignificantBits32(nbits, value);
131 struct DecodingStatus {
132 DecodingStatus(uint32_t num_remaining_points_, uint32_t last_axis_,
134 : num_remaining_points(num_remaining_points_),
135 last_axis(last_axis_),
136 stack_pos(stack_pos_) {}
138 uint32_t num_remaining_points;
143 uint32_t bit_length_;
144 uint32_t num_points_;
145 uint32_t num_decoded_points_;
147 NumbersDecoder numbers_decoder_;
148 RemainingBitsDecoder remaining_bits_decoder_;
149 AxisDecoder axis_decoder_;
150 HalfDecoder half_decoder_;
153 std::vector<VectorUint32> base_stack_;
154 std::vector<VectorUint32> levels_stack_;
159template <
int compression_level_t>
160template <
class OutputIteratorT>
163 return DecodePoints(buffer, oit, std::numeric_limits<uint32_t>::max());
166template <
int compression_level_t>
167template <
class OutputIteratorT>
169 DecoderBuffer *buffer, OutputIteratorT &&oit, uint32_t oit_max_points) {
170 OutputIteratorT local = std::forward<OutputIteratorT>(oit);
175template <
int compression_level_t>
176template <
class OutputIteratorT>
179 return DecodePoints(buffer, oit, std::numeric_limits<uint32_t>::max());
182template <
int compression_level_t>
183template <
class OutputIteratorT>
185 DecoderBuffer *buffer, OutputIteratorT &oit, uint32_t oit_max_points) {
186 if (!buffer->
Decode(&bit_length_)) {
189 if (bit_length_ > 32) {
192 if (!buffer->
Decode(&num_points_)) {
195 if (num_points_ == 0) {
198 if (num_points_ > oit_max_points) {
201 num_decoded_points_ = 0;
203 if (!numbers_decoder_.StartDecoding(buffer)) {
206 if (!remaining_bits_decoder_.StartDecoding(buffer)) {
209 if (!axis_decoder_.StartDecoding(buffer)) {
212 if (!half_decoder_.StartDecoding(buffer)) {
216 if (!DecodeInternal(num_points_, oit)) {
220 numbers_decoder_.EndDecoding();
221 remaining_bits_decoder_.EndDecoding();
222 axis_decoder_.EndDecoding();
223 half_decoder_.EndDecoding();
228template <
int compression_level_t>
229uint32_t DynamicIntegerPointsKdTreeDecoder<compression_level_t>::GetAxis(
230 uint32_t num_remaining_points,
const VectorUint32 &levels,
231 uint32_t last_axis) {
232 if (!Policy::select_axis) {
236 uint32_t best_axis = 0;
237 if (num_remaining_points < 64) {
238 for (uint32_t axis = 1; axis < dimension_; ++axis) {
239 if (levels[best_axis] > levels[axis]) {
244 axis_decoder_.DecodeLeastSignificantBits32(4, &best_axis);
250template <
int compression_level_t>
251template <
class OutputIteratorT>
252bool DynamicIntegerPointsKdTreeDecoder<compression_level_t>::DecodeInternal(
253 uint32_t num_points, OutputIteratorT &oit) {
254 typedef DecodingStatus
Status;
255 base_stack_[0] = VectorUint32(dimension_, 0);
256 levels_stack_[0] = VectorUint32(dimension_, 0);
257 DecodingStatus init_status(num_points, 0, 0);
258 std::stack<Status> status_stack;
259 status_stack.push(init_status);
262 while (!status_stack.empty()) {
263 const DecodingStatus status = status_stack.top();
266 const uint32_t num_remaining_points = status.num_remaining_points;
267 const uint32_t last_axis = status.last_axis;
268 const uint32_t stack_pos = status.stack_pos;
269 const VectorUint32 &old_base = base_stack_[stack_pos];
270 const VectorUint32 &levels = levels_stack_[stack_pos];
272 if (num_remaining_points > num_points) {
276 const uint32_t axis = GetAxis(num_remaining_points, levels, last_axis);
277 if (axis >= dimension_) {
281 const uint32_t level = levels[axis];
284 if ((bit_length_ - level) == 0) {
285 for (uint32_t i = 0; i < num_remaining_points; i++) {
288 ++num_decoded_points_;
296 if (num_remaining_points <= 2) {
300 for (uint32_t i = 1; i < dimension_; i++) {
303 for (uint32_t i = 0; i < num_remaining_points; ++i) {
304 for (uint32_t j = 0; j < dimension_; j++) {
306 const uint32_t num_remaining_bits = bit_length_ - levels[axes_[j]];
307 if (num_remaining_bits) {
308 if (!remaining_bits_decoder_.DecodeLeastSignificantBits32(
309 num_remaining_bits, &p_[axes_[j]])) {
313 p_[axes_[j]] = old_base[axes_[j]] | p_[axes_[j]];
317 ++num_decoded_points_;
322 if (num_decoded_points_ > num_points_) {
326 const int num_remaining_bits = bit_length_ - level;
327 const uint32_t modifier = 1 << (num_remaining_bits - 1);
328 base_stack_[stack_pos + 1] = old_base;
329 base_stack_[stack_pos + 1][axis] += modifier;
334 DecodeNumber(incoming_bits, &number);
336 uint32_t first_half = num_remaining_points / 2;
337 if (first_half < number) {
341 first_half -= number;
342 uint32_t second_half = num_remaining_points - first_half;
344 if (first_half != second_half) {
345 if (!half_decoder_.DecodeNextBit()) {
346 std::swap(first_half, second_half);
350 levels_stack_[stack_pos][axis] += 1;
351 levels_stack_[stack_pos + 1] = levels_stack_[stack_pos];
353 status_stack.push(DecodingStatus(first_half, axis, stack_pos));
356 status_stack.push(DecodingStatus(second_half, axis, stack_pos + 1));
Definition decoder_buffer.h:30
bool Decode(T *out_val)
Definition decoder_buffer.h:68
Definition direct_bit_decoder.h:25
Definition dynamic_integer_points_kd_tree_decoder.h:71
bool DecodePoints(DecoderBuffer *buffer, OutputIteratorT &oit)
Definition dynamic_integer_points_kd_tree_decoder.h:177
DynamicIntegerPointsKdTreeDecoder(uint32_t dimension)
Definition dynamic_integer_points_kd_tree_decoder.h:85
const uint32_t dimension() const
Definition dynamic_integer_points_kd_tree_decoder.h:115
uint32_t num_decoded_points() const
Definition dynamic_integer_points_kd_tree_decoder.h:118
Definition folded_integer_bit_decoder.h:27
Definition rans_bit_decoder.h:28
#define DRACO_DCHECK_EQ(a, b)
Definition macros.h:81
#define DRACO_INCREMENT_MOD(I, M)
Definition math_utils.h:24
Definition attribute_octahedron_transform.cc:21
int MostSignificantBit(uint32_t n)
Definition bit_utils.h:58
static constexpr bool select_axis
Definition dynamic_integer_points_kd_tree_decoder.h:48
DirectBitDecoder HalfDecoder
Definition dynamic_integer_points_kd_tree_decoder.h:46
DirectBitDecoder RemainingBitsDecoder
Definition dynamic_integer_points_kd_tree_decoder.h:47
DirectBitDecoder NumbersDecoder
Definition dynamic_integer_points_kd_tree_decoder.h:44
DirectBitDecoder AxisDecoder
Definition dynamic_integer_points_kd_tree_decoder.h:45
RAnsBitDecoder NumbersDecoder
Definition dynamic_integer_points_kd_tree_decoder.h:54
FoldedBit32Decoder< RAnsBitDecoder > NumbersDecoder
Definition dynamic_integer_points_kd_tree_decoder.h:60
static constexpr bool select_axis
Definition dynamic_integer_points_kd_tree_decoder.h:66
Definition dynamic_integer_points_kd_tree_decoder.h:40