GameApi 28
Library used by GameApi Builder tool
Loading...
Searching...
No Matches
quantization_utils.h
Go to the documentation of this file.
1// Copyright 2016 The Draco Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// A set of classes for quantizing and dequantizing of floating point values
16// into integers.
17// The quantization works on all floating point numbers within (-range, +range)
18// interval producing integers in range
19// (-max_quantized_value, +max_quantized_value).
20
21#ifndef DRACO_CORE_QUANTIZATION_UTILS_H_
22#define DRACO_CORE_QUANTIZATION_UTILS_H_
23
24#include <stdint.h>
25
26#include <cmath>
27
28#include "draco/core/macros.h"
29
30namespace draco {
31
32// Class for quantizing single precision floating point values. The values
33// should be centered around zero and be within interval (-range, +range), where
34// the range is specified in the Init() method. Alternatively, the quantization
35// can be defined by |delta| that specifies the distance between two quantized
36// values. Note that the quantizer always snaps the values to the nearest
37// integer value. E.g. for |delta| == 1.f, values -0.4f and 0.4f would be
38// both quantized to 0 while value 0.6f would be quantized to 1. If a value
39// lies exactly between two quantized states, it is always rounded up. E.g.,
40// for |delta| == 1.f, value -0.5f would be quantized to 0 while 0.5f would be
41// quantized to 1.
42class Quantizer {
43 public:
44 Quantizer();
45 void Init(float range, int32_t max_quantized_value);
46 void Init(float delta);
47 inline int32_t QuantizeFloat(float val) const {
48 val *= inverse_delta_;
49 return static_cast<int32_t>(floor(val + 0.5f));
50 }
51 inline int32_t operator()(float val) const { return QuantizeFloat(val); }
52
53 private:
54 float inverse_delta_;
55};
56
57// Class for dequantizing values that were previously quantized using the
58// Quantizer class.
60 public:
62
63 // Initializes the dequantizer. Both parameters must correspond to the values
64 // provided to the initializer of the Quantizer class.
65 // Returns false when the initialization fails.
66 bool Init(float range, int32_t max_quantized_value);
67
68 // Initializes the dequantizer using the |delta| between two quantized values.
69 bool Init(float delta);
70
71 inline float DequantizeFloat(int32_t val) const {
72 return static_cast<float>(val) * delta_;
73 }
74 inline float operator()(int32_t val) const { return DequantizeFloat(val); }
75
76 private:
77 float delta_;
78};
79
80} // namespace draco
81
82#endif // DRACO_CORE_QUANTIZATION_UTILS_H_
void floor(const FCube &pos, FLine &l1, FLine &l2, FLine &l3, FLine &l4, FPlane &pl, FaceStore &s)
Definition Functions.cc:95
Dequantizer()
Definition quantization_utils.cc:27
float DequantizeFloat(int32_t val) const
Definition quantization_utils.h:71
float operator()(int32_t val) const
Definition quantization_utils.h:74
bool Init(float range, int32_t max_quantized_value)
Definition quantization_utils.cc:29
void Init(float range, int32_t max_quantized_value)
Definition quantization_utils.cc:21
int32_t operator()(float val) const
Definition quantization_utils.h:51
Quantizer()
Definition quantization_utils.cc:19
int32_t QuantizeFloat(float val) const
Definition quantization_utils.h:47
Definition attribute_octahedron_transform.cc:21