draco_core/encoder_options.rs
1use std::collections::HashMap;
2use std::sync::Arc;
3
4/// Integer option bag used to configure Draco encoding.
5///
6/// Options mirror the C++ Draco encoder style: global options apply to the
7/// whole geometry, while attribute options override a value for one attribute
8/// id and fall back to the global value when unset.
9///
10/// Common keys include `quantization_bits` (per-attribute precision),
11/// `encoding_speed`/`decoding_speed`, `encoding_method`, and
12/// `prediction_scheme`. Keys without an explicit setter are read and written
13/// with [`get_global_int`](EncoderOptions::get_global_int) /
14/// [`set_global_int`](EncoderOptions::set_global_int).
15///
16/// # Examples
17///
18/// ```
19/// use draco_core::EncoderOptions;
20///
21/// let mut options = EncoderOptions::new();
22/// options.set_global_int("quantization_bits", 14); // default for all attributes
23/// options.set_attribute_int(0, "quantization_bits", 10); // override attribute 0
24///
25/// assert_eq!(options.get_attribute_int(0, "quantization_bits", 0), 10);
26/// // Attribute 1 has no override, so it falls back to the global value.
27/// assert_eq!(options.get_attribute_int(1, "quantization_bits", 0), 14);
28/// ```
29#[derive(Debug, Clone, Default)]
30pub struct EncoderOptions {
31 inner: Arc<Inner>,
32}
33
34/// The option maps, shared until a setter needs its own copy.
35///
36/// The encoders clone the whole bag on every `encode` call and keep the copy
37/// for the duration. A deep clone paid one allocation per map and per `String`
38/// key each time -- pure bookkeeping, and on a tiny mesh a visible share of
39/// the per-call floor. Behind an `Arc` the clone is a reference count, and a
40/// setter on a bag that is shared at that moment copies the maps once for
41/// itself (`Arc::make_mut`); a bag nobody else holds is mutated in place.
42#[derive(Debug, Clone, Default)]
43struct Inner {
44 global_options: HashMap<String, i32>,
45 attribute_options: HashMap<i32, HashMap<String, i32>>,
46}
47
48impl EncoderOptions {
49 /// Creates options with Draco-compatible defaults.
50 pub fn new() -> Self {
51 Self::default()
52 }
53
54 /// Returns the configured encoding speed, defaulting to 5.
55 pub fn get_encoding_speed(&self) -> i32 {
56 self.get_global_int("encoding_speed", 5)
57 }
58
59 /// Returns the configured decoding speed target, defaulting to 5.
60 pub fn get_decoding_speed(&self) -> i32 {
61 self.get_global_int("decoding_speed", 5)
62 }
63
64 /// Returns the maximum speed for both encoding/decoding.
65 /// Matches C++ ExpertEncoder::GetSpeed() behavior.
66 pub fn get_speed(&self) -> i32 {
67 let encoding_speed = self
68 .inner
69 .global_options
70 .get("encoding_speed")
71 .copied()
72 .unwrap_or(-1);
73 let decoding_speed = self
74 .inner
75 .global_options
76 .get("decoding_speed")
77 .copied()
78 .unwrap_or(-1);
79 let max_speed = encoding_speed.max(decoding_speed);
80 if max_speed == -1 {
81 5 // Default value
82 } else {
83 max_speed
84 }
85 }
86
87 /// Sets both speeds from a `draco_encoder`-style compression level.
88 ///
89 /// The CLI's `-cl` runs 0 (least compression) to 10 (most), the opposite
90 /// sense of `encoding_speed`/`decoding_speed`, and converts with
91 /// `speed = 10 - compression_level` before calling the same
92 /// `SetSpeedOptions` this crate mirrors; this method does the same
93 /// conversion and nothing else. `level` is not range-checked, matching
94 /// the CLI, which passes an out-of-range `-cl` straight through the same
95 /// subtraction rather than rejecting it.
96 pub fn set_compression_level(&mut self, level: i32) {
97 let speed = 10 - level;
98 self.set_global_int("encoding_speed", speed);
99 self.set_global_int("decoding_speed", speed);
100 }
101
102 /// Returns `10 - `[`get_speed`](Self::get_speed)`()`, the CLI's compression
103 /// level for the speed this instance currently carries.
104 ///
105 /// A fresh `EncoderOptions` reports 5 here, not the CLI's own default of
106 /// 7 — the two tools default to different speeds (5 here, 3 there), and
107 /// this getter reads what is actually set rather than what the CLI would
108 /// have chosen.
109 pub fn get_compression_level(&self) -> i32 {
110 10 - self.get_speed()
111 }
112
113 /// Sets `quantization_bits` for one attribute id.
114 ///
115 /// Equivalent to `ExpertEncoder::SetAttributeQuantization` and to what the
116 /// CLI's `-qp`/`-qt`/`-qn`/`-qg` resolve to once they have picked an
117 /// attribute id for POSITION/TEX_COORD/NORMAL/GENERIC; this method takes
118 /// the id directly rather than a geometry attribute type.
119 pub fn set_attribute_quantization(&mut self, att_id: i32, quantization_bits: i32) {
120 self.set_attribute_int(att_id, "quantization_bits", quantization_bits);
121 }
122
123 /// Returns the `quantization_bits` set for one attribute id, or -1 if
124 /// none was set for it or globally.
125 pub fn get_attribute_quantization(&self, att_id: i32) -> i32 {
126 self.get_attribute_int(att_id, "quantization_bits", -1)
127 }
128
129 /// Returns the forced prediction scheme, or -1 for the encoder default.
130 pub fn get_prediction_scheme(&self) -> i32 {
131 self.get_global_int("prediction_scheme", -1)
132 }
133
134 /// Returns the prediction scheme forced for one attribute, falling back to
135 /// the global setting and then to -1 for the encoder default.
136 ///
137 /// Upstream reads this per attribute (`GetPredictionMethodFromOptions`), and
138 /// `get_attribute_int` itself falls back to the global option, so a value
139 /// set either way is honoured. Crate-internal: callers outside can already
140 /// express both halves with `set_attribute_int` and `set_prediction_scheme`,
141 /// and a getter with no matching per-attribute setter would be a half API.
142 pub(crate) fn get_attribute_prediction_scheme(&self, att_id: i32) -> i32 {
143 self.get_attribute_int(att_id, "prediction_scheme", -1)
144 }
145
146 /// Forces a prediction scheme by numeric Draco method id.
147 pub fn set_prediction_scheme(&mut self, value: i32) {
148 self.set_global_int("prediction_scheme", value);
149 }
150
151 /// Returns the forced encoding method, if one was set.
152 pub fn get_encoding_method(&self) -> Option<i32> {
153 self.inner.global_options.get("encoding_method").cloned()
154 }
155
156 /// Forces an encoding method by numeric Draco method id.
157 pub fn set_encoding_method(&mut self, value: i32) {
158 self.set_global_int("encoding_method", value);
159 }
160
161 /// Sets the target Draco bitstream version.
162 pub fn set_version(&mut self, major: u8, minor: u8) {
163 self.set_global_int("version_major", major as i32);
164 self.set_global_int("version_minor", minor as i32);
165 }
166
167 /// Returns the target Draco bitstream version, or `(0, 0)` for default.
168 pub fn get_version(&self) -> (u8, u8) {
169 let major = self.get_global_int("version_major", -1);
170 let minor = self.get_global_int("version_minor", -1);
171 if major == -1 || minor == -1 {
172 // Default version depends on the encoder type and method,
173 // but we'll return (0, 0) to indicate "use default".
174 (0, 0)
175 } else {
176 (major as u8, minor as u8)
177 }
178 }
179
180 /// Sets a global integer option.
181 pub fn set_global_int(&mut self, key: &str, value: i32) {
182 Arc::make_mut(&mut self.inner)
183 .global_options
184 .insert(key.to_string(), value);
185 }
186
187 /// Returns a global integer option or the supplied default.
188 pub fn get_global_int(&self, key: &str, default_val: i32) -> i32 {
189 *self.inner.global_options.get(key).unwrap_or(&default_val)
190 }
191
192 /// Sets an integer option for one attribute id.
193 pub fn set_attribute_int(&mut self, att_id: i32, key: &str, value: i32) {
194 Arc::make_mut(&mut self.inner)
195 .attribute_options
196 .entry(att_id)
197 .or_default()
198 .insert(key.to_string(), value);
199 }
200
201 /// Returns an attribute integer option, falling back to the global value.
202 pub fn get_attribute_int(&self, att_id: i32, key: &str, default_val: i32) -> i32 {
203 if let Some(opts) = self.inner.attribute_options.get(&att_id) {
204 if let Some(val) = opts.get(key) {
205 return *val;
206 }
207 }
208 // Falls back to the global value, as upstream does.
209 self.get_global_int(key, default_val)
210 }
211}
212
213#[cfg(test)]
214mod tests {
215 use super::*;
216
217 /// `draco_encoder`'s own default is `-cl 7`; this crate's is speed 5.
218 /// Verified against the CLI: `EncoderOptions::new()` and
219 /// `draco_encoder -cl 5 -qp 0` produce byte-identical output.
220 #[test]
221 fn compression_level_defaults_to_5_not_the_cli_default_of_7() {
222 let options = EncoderOptions::new();
223 assert_eq!(options.get_compression_level(), 5);
224 }
225
226 /// `speed = 10 - compression_level`, matching `draco_encoder.cc`'s own
227 /// conversion before it calls `SetSpeedOptions`.
228 #[test]
229 fn set_compression_level_matches_the_cli_conversion() {
230 let mut options = EncoderOptions::new();
231 options.set_compression_level(7);
232 assert_eq!(options.get_speed(), 3);
233 assert_eq!(options.get_encoding_speed(), 3);
234 assert_eq!(options.get_decoding_speed(), 3);
235 assert_eq!(options.get_compression_level(), 7);
236 }
237
238 #[test]
239 fn set_compression_level_round_trips_the_full_cli_range() {
240 for level in 0..=10 {
241 let mut options = EncoderOptions::new();
242 options.set_compression_level(level);
243 assert_eq!(options.get_compression_level(), level);
244 }
245 }
246
247 /// A clone shares the maps, and a setter on either side afterwards
248 /// changes only the side it was called on.
249 #[test]
250 fn a_clone_shares_the_maps_until_one_side_writes() {
251 let mut options = EncoderOptions::new();
252 options.set_global_int("quantization_bits", 14);
253 options.set_attribute_int(1, "quantization_bits", 10);
254 let mut cloned = options.clone();
255 assert!(Arc::ptr_eq(&options.inner, &cloned.inner));
256
257 cloned.set_attribute_int(1, "quantization_bits", 8);
258 options.set_global_int("quantization_bits", 12);
259 assert!(!Arc::ptr_eq(&options.inner, &cloned.inner));
260 assert_eq!(options.get_attribute_int(1, "quantization_bits", 0), 10);
261 assert_eq!(options.get_global_int("quantization_bits", 0), 12);
262 assert_eq!(cloned.get_attribute_int(1, "quantization_bits", 0), 8);
263 assert_eq!(cloned.get_global_int("quantization_bits", 0), 14);
264 }
265
266 #[test]
267 fn attribute_quantization_defaults_to_unset() {
268 let options = EncoderOptions::new();
269 assert_eq!(options.get_attribute_quantization(0), -1);
270 }
271
272 #[test]
273 fn set_attribute_quantization_round_trips() {
274 let mut options = EncoderOptions::new();
275 options.set_attribute_quantization(0, 14);
276 options.set_attribute_quantization(1, 10);
277
278 assert_eq!(options.get_attribute_quantization(0), 14);
279 assert_eq!(options.get_attribute_quantization(1), 10);
280 // Unset attributes fall back to the global value, same as
281 // `get_attribute_int` -- there is none here, so -1.
282 assert_eq!(options.get_attribute_quantization(2), -1);
283 }
284}