realsense_rust/processing_blocks/
options.rs

1//! Processing block option definitions and management
2//!
3//! This module provides strongly-typed options for configuring processing blocks
4//! at runtime with proper validation and error handling.
5
6use crate::check_rs2_error;
7use anyhow::Result;
8use realsense_sys as sys;
9use std::ptr::NonNull;
10use thiserror::Error;
11
12/// Errors that can occur when working with processing block options
13#[derive(Error, Debug, Clone, PartialEq)]
14pub enum ProcessingBlockOptionError {
15    /// The requested option is not supported by this processing block
16    #[error("Option not supported: {0}")]
17    OptionNotSupported(String),
18    /// The provided value is not valid for this option
19    #[error("Invalid option value: {0}")]
20    InvalidValue(String),
21    /// Failed to set the option value
22    #[error("Failed to set option: {0}")]
23    SetOptionFailed(String),
24    /// Failed to get the option value
25    #[error("Failed to get option: {0}")]
26    GetOptionFailed(String),
27}
28
29/// Trait for processing blocks that support configurable options
30pub trait OptionsExt {
31    /// Set an option value on the processing block
32    fn set_option(
33        &mut self,
34        option: sys::rs2_option,
35        value: f32,
36    ) -> Result<(), ProcessingBlockOptionError>;
37
38    /// Get the current value of an option
39    fn get_option(&self, option: sys::rs2_option) -> Result<f32, ProcessingBlockOptionError>;
40
41    /// Check if an option is supported
42    fn supports_option(&self, option: sys::rs2_option) -> bool;
43
44    /// Get the range of valid values for an option
45    fn get_option_range(
46        &self,
47        option: sys::rs2_option,
48    ) -> Result<(f32, f32, f32, f32), ProcessingBlockOptionError>;
49}
50
51/// Options for the Spatial Filter processing block
52#[derive(Debug, Clone, PartialEq, Default)]
53pub struct SpatialFilterOptions {
54    /// Controls the weight with respect to the current pixel. Range: 0.25-1.0
55    pub smooth_alpha: Option<f32>,
56    /// Controls the decrease in preservation of edge textures. Range: 1-50
57    pub smooth_delta: Option<f32>,
58    /// Controls the filter strength. Range: 1-5
59    pub magnitude: Option<f32>,
60    /// Number of iterations for hole filling. Range: 0-5
61    pub holes_fill: Option<f32>,
62}
63
64/// Options for the Temporal Filter processing block
65#[derive(Debug, Clone, PartialEq, Default)]
66pub struct TemporalFilterOptions {
67    /// Controls the temporal alpha factor. Range: 0.0-1.0
68    pub smooth_alpha: Option<f32>,
69    /// Controls the temporal sensitivity. Range: 1-100
70    pub smooth_delta: Option<f32>,
71    /// Controls persistence of the filter. Range: 1-8
72    pub persistence_control: Option<f32>,
73}
74
75/// Options for the Hole Filling Filter processing block
76#[derive(Debug, Clone, PartialEq, Default)]
77pub struct HoleFillingOptions {
78    /// Hole filling mode. 0=Fill from left, 1=Farthest from around, 2=Nearest from around
79    pub holes_fill: Option<f32>,
80}
81
82/// Options for the Threshold Filter processing block
83#[derive(Debug, Clone, PartialEq, Default)]
84pub struct ThresholdOptions {
85    /// Minimum distance threshold in meters
86    pub min_distance: Option<f32>,
87    /// Maximum distance threshold in meters
88    pub max_distance: Option<f32>,
89}
90
91/// Options for the Decimation Filter processing block
92#[derive(Debug, Clone, PartialEq, Default)]
93pub struct DecimationOptions {
94    /// Decimation filter magnitude. Range: 2-8
95    pub filter_magnitude: Option<f32>,
96}
97
98/// Options for the Colorizer processing block
99#[derive(Debug, Clone, PartialEq, Default)]
100pub struct ColorizerOptions {
101    /// Color scheme for visualization. Range: 0-9
102    pub color_scheme: Option<f32>,
103    /// Histogram equalization. 0=disabled, 1=enabled
104    pub histogram_equalization: Option<f32>,
105    /// Minimum distance for color mapping
106    pub min_distance: Option<f32>,
107    /// Maximum distance for color mapping
108    pub max_distance: Option<f32>,
109}
110
111/// Helper function to set an option on a processing block
112pub fn set_processing_block_option(
113    block: NonNull<sys::rs2_processing_block>,
114    option: sys::rs2_option,
115    value: f32,
116) -> Result<(), ProcessingBlockOptionError> {
117    unsafe {
118        let mut err = std::ptr::null_mut::<sys::rs2_error>();
119
120        // Check if option is supported
121        let is_supported =
122            sys::rs2_supports_option(block.as_ptr() as *const sys::rs2_options, option, &mut err);
123        if !err.is_null() {
124            check_rs2_error!(err, |kind, context| {
125                ProcessingBlockOptionError::SetOptionFailed(format!("{:?}: {}", kind, context))
126            })?;
127        }
128
129        if is_supported == 0 {
130            return Err(ProcessingBlockOptionError::OptionNotSupported(format!(
131                "Option {:?} not supported",
132                option
133            )));
134        }
135
136        sys::rs2_set_option(
137            block.as_ptr() as *const sys::rs2_options,
138            option,
139            value,
140            &mut err,
141        );
142        check_rs2_error!(err, |kind, context| {
143            ProcessingBlockOptionError::SetOptionFailed(format!("{:?}: {}", kind, context))
144        })?;
145
146        Ok(())
147    }
148}
149
150/// Helper function to get an option value from a processing block
151pub fn get_processing_block_option(
152    block: NonNull<sys::rs2_processing_block>,
153    option: sys::rs2_option,
154) -> Result<f32, ProcessingBlockOptionError> {
155    unsafe {
156        let mut err = std::ptr::null_mut::<sys::rs2_error>();
157
158        // Check if option is supported
159        let is_supported =
160            sys::rs2_supports_option(block.as_ptr() as *const sys::rs2_options, option, &mut err);
161        if !err.is_null() {
162            check_rs2_error!(err, |kind, context| {
163                ProcessingBlockOptionError::GetOptionFailed(format!("{:?}: {}", kind, context))
164            })?;
165        }
166
167        if is_supported == 0 {
168            return Err(ProcessingBlockOptionError::OptionNotSupported(format!(
169                "Option {:?} not supported",
170                option
171            )));
172        }
173
174        let value =
175            sys::rs2_get_option(block.as_ptr() as *const sys::rs2_options, option, &mut err);
176        check_rs2_error!(err, |kind, context| {
177            ProcessingBlockOptionError::GetOptionFailed(format!("{:?}: {}", kind, context))
178        })?;
179
180        Ok(value)
181    }
182}
183
184/// Helper function to get option range from a processing block
185pub fn get_processing_block_option_range(
186    block: NonNull<sys::rs2_processing_block>,
187    option: sys::rs2_option,
188) -> Result<(f32, f32, f32, f32), ProcessingBlockOptionError> {
189    unsafe {
190        let mut err = std::ptr::null_mut::<sys::rs2_error>();
191        let mut min = 0.0f32;
192        let mut max = 0.0f32;
193        let mut step = 0.0f32;
194        let mut default = 0.0f32;
195
196        sys::rs2_get_option_range(
197            block.as_ptr() as *const sys::rs2_options,
198            option,
199            &mut min,
200            &mut max,
201            &mut step,
202            &mut default,
203            &mut err,
204        );
205        check_rs2_error!(err, |kind, context| {
206            ProcessingBlockOptionError::GetOptionFailed(format!("{:?}: {}", kind, context))
207        })?;
208
209        Ok((min, max, step, default))
210    }
211}