orbbec-sdk 0.1.0

High-level bindings to Orbbec SDK v2
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Filter module
use crate::{
    ConvertType, HoleFillMode, StreamType,
    error::{OrbbecError, OrbbecErrorData},
    frame::Frame,
    stream::VideoStreamProfile,
    sys::filter::OBFilter,
};

/// Filter trait
pub trait Filter<F: Frame>: AsRef<OBFilter> {
    /// Process a frame with the filter
    fn process(&self, frame: &F) -> Result<F, OrbbecError> {
        self.as_ref()
            .process(frame.as_ref())
            .map(|f| F::from(f))
            .map_err(OrbbecError::from)
    }
}

/// Decimation Filter
///
/// This filter reduces the resolution of the depth frame by an integer factor.
pub struct DecimationFilter {
    inner: OBFilter,
}

impl DecimationFilter {
    /// Create a new decimation filter.
    pub fn new() -> Result<Self, OrbbecError> {
        // Unwrap is safe because Decimation filter is always available (public filter)
        // Ref: https://github.com/orbbec/OrbbecSDK_v2/blob/815ae047cc977a1f7edd2b97b69ff6cd29f510b3/src/filter/publicfilters/publicFilterLoader.cpp#L32
        let filter = OBFilter::new(c"DecimationFilter")?.unwrap();
        Ok(DecimationFilter { inner: filter })
    }

    /// Set the decimation factor.
    ///
    /// ### Arguments
    /// * `factor` - The decimation factor. Usually between 1 and 8.
    pub fn set_factor(&mut self, factor: u8) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"decimate", factor as f64)
            .map_err(OrbbecError::from)
    }
}

impl AsRef<OBFilter> for DecimationFilter {
    fn as_ref(&self) -> &OBFilter {
        &self.inner
    }
}

impl<F: Frame> Filter<F> for DecimationFilter {}

/// Format Convert Filter
///
/// This filter converts the format of the input frame to a different format.
pub struct FormatConvertFilter {
    inner: OBFilter,
}

impl FormatConvertFilter {
    /// Create a new format convert filter.
    pub fn new() -> Result<Self, OrbbecError> {
        // Unwrap is safe because FormatConvert filter is always available (public filter)
        // Ref: https://github.com/orbbec/OrbbecSDK_v2/blob/815ae047cc977a1f7edd2b97b69ff6cd29f510b3/src/filter/publicfilters/publicFilterLoader.cpp#L32
        let filter = OBFilter::new(c"FormatConverter")?.unwrap();
        Ok(FormatConvertFilter { inner: filter })
    }

    /// Set the format conversion type
    /// ### Arguments
    /// * `convert_type` - The desired format conversion type.
    pub fn set_convert_type(&mut self, convert_type: ConvertType) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"convertType", convert_type as u32 as f64)
            .map_err(OrbbecError::from)
    }
}

impl AsRef<OBFilter> for FormatConvertFilter {
    fn as_ref(&self) -> &OBFilter {
        &self.inner
    }
}

impl<F: Frame> Filter<F> for FormatConvertFilter {}

/// Hole Filling Filter
///
/// This filter fills holes in the depth frame using a specified hole filling mode.
pub struct HoleFillingFilter {
    inner: OBFilter,
}

impl HoleFillingFilter {
    /// Create a new hole filling filter.
    pub fn new() -> Result<Self, OrbbecError> {
        match OBFilter::new(c"HoleFillingFilter")? {
            Some(f) => Ok(HoleFillingFilter { inner: f }),
            None => {
                let err_data = OrbbecErrorData {
                    message: "HoleFillingFilter is not available".to_string(),
                    function: "HoleFillingFilter::new".to_string(),
                    args: "".to_string(),
                };

                Err(OrbbecError::NotImplemented(err_data))
            }
        }
    }

    /// Set the hole filling mode.
    ///
    /// ### Arguments
    /// * `mode` - The hole filling mode.
    pub fn set_mode(&mut self, mode: HoleFillMode) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"hole_filling_mode", mode as u32 as f64)
            .map_err(OrbbecError::from)
    }
}

impl AsRef<OBFilter> for HoleFillingFilter {
    fn as_ref(&self) -> &OBFilter {
        &self.inner
    }
}

impl<F: Frame> Filter<F> for HoleFillingFilter {}

/// Temporal Filter
///
/// This filter smooths the depth frame over time to reduce noise and temporal artifacts.
pub struct TemporalFilter {
    inner: OBFilter,
}

impl TemporalFilter {
    /// Create a new temporal filter.
    pub fn new() -> Result<Self, OrbbecError> {
        match OBFilter::new(c"TemporalFilter")? {
            Some(f) => Ok(TemporalFilter { inner: f }),
            None => {
                let err_data = OrbbecErrorData {
                    message: "TemporalFilter is not available".to_string(),
                    function: "TemporalFilter::new".to_string(),
                    args: "".to_string(),
                };

                Err(OrbbecError::NotImplemented(err_data))
            }
        }
    }

    /// Set the threshold parameter.
    ///
    /// Threshold is the maximum change (in percentage) for a pixel to still be considered static (without motion).
    /// ### Arguments
    /// * `threshold` - The threshold value. Usually between 0.1 and 1.0.
    pub fn set_threshold(&mut self, threshold: f32) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"diff_scale", threshold as f64)
            .map_err(OrbbecError::from)
    }

    /// Set the weight parameter.
    ///
    /// Weight is how much the current frame affects the output frame.
    /// ### Arguments
    /// * `weight` - The weight value. Usually between 0.1 and 1.0.
    pub fn set_weight(&mut self, weight: f32) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"weight", weight as f64)
            .map_err(OrbbecError::from)
    }
}

impl AsRef<OBFilter> for TemporalFilter {
    fn as_ref(&self) -> &OBFilter {
        &self.inner
    }
}

impl<F: Frame> Filter<F> for TemporalFilter {}

/// Spatial Fast Filter
///
/// This filter smooths the depth frame while preserving edges.
/// It uses an enhanced median smoothing algorithm and is the simplest spatial filter available.
pub struct SpatialFastFilter {
    inner: OBFilter,
}

impl SpatialFastFilter {
    /// Create a new spatial fast filter.
    pub fn new() -> Result<Self, OrbbecError> {
        match OBFilter::new(c"SpatialFastFilter")? {
            Some(f) => Ok(SpatialFastFilter { inner: f }),
            None => {
                let err_data = OrbbecErrorData {
                    message: "SpatialFastFilter is not available".to_string(),
                    function: "SpatialFastFilter::new".to_string(),
                    args: "".to_string(),
                };

                Err(OrbbecError::NotImplemented(err_data))
            }
        }
    }

    /// Set the filter radius parameter.
    ///
    /// Radius is how many neighbors will be considered for smoothing.
    /// ### Arguments
    /// * `radius` - The radius value. Usually between 3 and 5.
    pub fn set_radius(&mut self, radius: u16) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"radius", radius as f64)
            .map_err(OrbbecError::from)
    }
}

impl AsRef<OBFilter> for SpatialFastFilter {
    fn as_ref(&self) -> &OBFilter {
        &self.inner
    }
}

impl<F: Frame> Filter<F> for SpatialFastFilter {}

/// Spatial Moderate Filter
///
/// This filter smooths the depth frame while preserving edges.
/// It uses an optimized average smoothing algorithm and is a balance between speed and quality.
pub struct SpatialModerateFilter {
    inner: OBFilter,
}

impl SpatialModerateFilter {
    /// Create a new spatial moderate filter.
    pub fn new() -> Result<Self, OrbbecError> {
        match OBFilter::new(c"SpatialModerateFilter")? {
            Some(f) => Ok(SpatialModerateFilter { inner: f }),
            None => {
                let err_data = OrbbecErrorData {
                    message: "SpatialModerateFilter is not available".to_string(),
                    function: "SpatialModerateFilter::new".to_string(),
                    args: "".to_string(),
                };

                Err(OrbbecError::NotImplemented(err_data))
            }
        }
    }

    /// Set the filter radius parameter.
    ///
    /// Radius is how many neighbors will be considered for smoothing.
    /// ### Arguments
    /// * `radius` - The radius value. Usually between 3 and 7.
    pub fn set_radius(&mut self, radius: u16) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"radius", radius as f64)
            .map_err(OrbbecError::from)
    }

    /// Set the filter magnitude parameter.
    ///
    /// Magnitude is the strength of the filter.
    /// ### Arguments
    /// * `magnitude` - The magnitude value. Usually between 1 and 3.
    pub fn set_magnitude(&mut self, magnitude: u8) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"magnitude", magnitude as f64)
            .map_err(OrbbecError::from)
    }

    /// Set the filter threshold parameter.
    ///
    /// Threshold is the maximum difference between pixels to be considered part of the same surface.
    /// ### Arguments
    /// * `threshold` - The threshold value. Usually between 1 and 10000.
    pub fn set_threshold(&mut self, threshold: u16) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"disp_diff", threshold as f64)
            .map_err(OrbbecError::from)
    }
}

impl AsRef<OBFilter> for SpatialModerateFilter {
    fn as_ref(&self) -> &OBFilter {
        &self.inner
    }
}

impl<F: Frame> Filter<F> for SpatialModerateFilter {}

///  Spatial Advanced Filter
///
/// This filter smooths the depth frame while preserving edges.
/// It uses a more advanced algorithm with more parameters to tune.
pub struct SpatialAdvancedFilter {
    inner: OBFilter,
}

impl SpatialAdvancedFilter {
    /// Create a new spatial advanced filter.
    pub fn new() -> Result<Self, OrbbecError> {
        match OBFilter::new(c"SpatialAdvancedFilter")? {
            Some(f) => Ok(SpatialAdvancedFilter { inner: f }),
            None => {
                let err_data = OrbbecErrorData {
                    message: "SpatialAdvancedFilter is not available".to_string(),
                    function: "SpatialAdvancedFilter::new".to_string(),
                    args: "".to_string(),
                };

                Err(OrbbecError::NotImplemented(err_data))
            }
        }
    }

    /// Set the filter alpha parameter.
    ///
    /// Alpha is the weight of a pixel vs neighbors of the same surface.
    /// ### Arguments
    /// * `alpha` - The alpha value. Usually between 0.1 and 1.0.
    pub fn set_alpha(&mut self, alpha: f32) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"alpha", alpha as f64)
            .map_err(OrbbecError::from)
    }

    /// Set the filter threshold parameter.
    ///
    /// Threshold is the maximum difference between pixels to be considered part of the same surface.
    /// ### Arguments
    /// * `threshold` - The threshold value. Usually between 1 and 10000.
    pub fn set_threshold(&mut self, threshold: u16) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"disp_diff", threshold as f64)
            .map_err(OrbbecError::from)
    }

    /// Set the filter radius parameter.
    ///
    /// Radius is how many missing pixels will be filled in.
    /// ### Arguments
    /// * `radius` - The radius value. Usually between 0 and 8.
    pub fn set_radius(&mut self, radius: u16) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"radius", radius as f64)
            .map_err(OrbbecError::from)
    }

    /// Set the filter magnitude parameter.
    ///
    /// Magnitude is the strength of the filter.
    /// ### Arguments
    /// * `magnitude` - The magnitude value. Usually between 1 and 5.
    pub fn set_magnitude(&mut self, magnitude: u8) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"magnitude", magnitude as f64)
            .map_err(OrbbecError::from)
    }
}

impl AsRef<OBFilter> for SpatialAdvancedFilter {
    fn as_ref(&self) -> &OBFilter {
        &self.inner
    }
}

impl<F: Frame> Filter<F> for SpatialAdvancedFilter {}

/// Threshold filter
///
/// This filter removes depth values outside the specified range.
pub struct ThresholdFilter {
    inner: OBFilter,
}

impl ThresholdFilter {
    /// Create a new threshold filter.
    pub fn new() -> Result<Self, OrbbecError> {
        // Unwrap is safe because Threshold filter is always available (public filter)
        // Ref: https://github.com/orbbec/OrbbecSDK_v2/blob/815ae047cc977a1f7edd2b97b69ff6cd29f510b3/src/filter/publicfilters/publicFilterLoader.cpp#L32
        let filter = OBFilter::new(c"ThresholdFilter")?.unwrap();
        Ok(ThresholdFilter { inner: filter })
    }

    /// Set the minimum depth value (in millimeters).
    /// ### Arguments
    /// * `min_depth` - The minimum depth value to keep, must be between 0 and 16000.
    pub fn set_min_depth(&mut self, min_depth: u16) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"min", min_depth as f64)
            .map_err(OrbbecError::from)
    }

    /// Set the maximum depth value (in millimeters).
    /// ### Arguments
    /// * `max_depth` - The maximum depth value to keep, must be between 0 and 16000.
    pub fn set_max_depth(&mut self, max_depth: u16) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"max", max_depth as f64)
            .map_err(OrbbecError::from)
    }
}

impl AsRef<OBFilter> for ThresholdFilter {
    fn as_ref(&self) -> &OBFilter {
        &self.inner
    }
}

impl<F: Frame> Filter<F> for ThresholdFilter {}

/// Align filter
///
/// This filter aligns a stream (usually depth) to another stream (usually color).
pub struct AlignFilter {
    inner: OBFilter,
}

impl AlignFilter {
    /// Create a new align filter.
    /// By default, it aligns to the color stream and resizes the aligned frame to match the target stream resolution.
    pub fn new() -> Result<Self, OrbbecError> {
        // Unwrap is safe because Align filter is always available (public filter)
        // Ref: https://github.com/orbbec/OrbbecSDK_v2/blob/815ae047cc977a1f7edd2b97b69ff6cd29f510b3/src/filter/publicfilters/publicFilterLoader.cpp#L32
        let filter = OBFilter::new(c"Align")?.unwrap();
        Ok(AlignFilter { inner: filter })
    }

    /// Align to the specified stream type.
    /// ### Arguments
    /// * `stream_type` - The stream type to align to.
    pub fn set_align_to_stream_type(&mut self, stream_type: StreamType) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"AlignType", stream_type as u32 as f64)
            .map_err(OrbbecError::from)
    }

    /// Add target stream distortion to the aligned frame.
    /// ### Arguments
    /// * `enable` - Whether to enable distortion.
    pub fn set_add_distortion(&mut self, enable: bool) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"TargetDistortion", if enable { 1.0 } else { 0.0 })
            .map_err(OrbbecError::from)
    }

    /// Set gap fill mode.
    /// ### Arguments
    /// * `enable` - Whether to enable gap fill.
    pub fn set_gap_fill(&mut self, enable: bool) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"GapFillCopy", if enable { 1.0 } else { 0.0 })
            .map_err(OrbbecError::from)
    }

    /// Set match resolution mode. When enabled, the aligned frame will have the same resolution as the target stream.
    /// ### Arguments
    /// * `enable` - Whether to enable match resolution.
    pub fn set_match_resolution(&mut self, enable: bool) -> Result<(), OrbbecError> {
        self.inner
            .set_config_value(c"MatchTargetRes", if enable { 1.0 } else { 0.0 })
            .map_err(OrbbecError::from)
    }

    /// Align to the specified stream profile.
    /// It is useful when the align target stream has not started (without any frame to get intrinsics and extrinsics).
    /// ### Arguments
    /// * `profile` - The video stream profile to align to.
    pub fn set_align_to_stream_profile(
        &mut self,
        profile: &VideoStreamProfile,
    ) -> Result<(), OrbbecError> {
        self.inner
            .set_align_to(profile.inner())
            .map_err(OrbbecError::from)
    }
}

impl AsRef<OBFilter> for AlignFilter {
    fn as_ref(&self) -> &OBFilter {
        &self.inner
    }
}

impl<F: Frame> Filter<F> for AlignFilter {}