realsense_rust/processing_blocks/
options.rs1use crate::check_rs2_error;
7use anyhow::Result;
8use realsense_sys as sys;
9use std::ptr::NonNull;
10use thiserror::Error;
11
12#[derive(Error, Debug, Clone, PartialEq)]
14pub enum ProcessingBlockOptionError {
15 #[error("Option not supported: {0}")]
17 OptionNotSupported(String),
18 #[error("Invalid option value: {0}")]
20 InvalidValue(String),
21 #[error("Failed to set option: {0}")]
23 SetOptionFailed(String),
24 #[error("Failed to get option: {0}")]
26 GetOptionFailed(String),
27}
28
29pub trait OptionsExt {
31 fn set_option(
33 &mut self,
34 option: sys::rs2_option,
35 value: f32,
36 ) -> Result<(), ProcessingBlockOptionError>;
37
38 fn get_option(&self, option: sys::rs2_option) -> Result<f32, ProcessingBlockOptionError>;
40
41 fn supports_option(&self, option: sys::rs2_option) -> bool;
43
44 fn get_option_range(
46 &self,
47 option: sys::rs2_option,
48 ) -> Result<(f32, f32, f32, f32), ProcessingBlockOptionError>;
49}
50
51#[derive(Debug, Clone, PartialEq, Default)]
53pub struct SpatialFilterOptions {
54 pub smooth_alpha: Option<f32>,
56 pub smooth_delta: Option<f32>,
58 pub magnitude: Option<f32>,
60 pub holes_fill: Option<f32>,
62}
63
64#[derive(Debug, Clone, PartialEq, Default)]
66pub struct TemporalFilterOptions {
67 pub smooth_alpha: Option<f32>,
69 pub smooth_delta: Option<f32>,
71 pub persistence_control: Option<f32>,
73}
74
75#[derive(Debug, Clone, PartialEq, Default)]
77pub struct HoleFillingOptions {
78 pub holes_fill: Option<f32>,
80}
81
82#[derive(Debug, Clone, PartialEq, Default)]
84pub struct ThresholdOptions {
85 pub min_distance: Option<f32>,
87 pub max_distance: Option<f32>,
89}
90
91#[derive(Debug, Clone, PartialEq, Default)]
93pub struct DecimationOptions {
94 pub filter_magnitude: Option<f32>,
96}
97
98#[derive(Debug, Clone, PartialEq, Default)]
100pub struct ColorizerOptions {
101 pub color_scheme: Option<f32>,
103 pub histogram_equalization: Option<f32>,
105 pub min_distance: Option<f32>,
107 pub max_distance: Option<f32>,
109}
110
111pub 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 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
150pub 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 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
184pub 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}