realsense_rust/processing_blocks/
hole_filling.rs

1//! Processing block for filling holes in depth data
2//!
3//! The filter replaces empty pixels with data from adjacent pixels based on the method selected
4
5use crate::{
6    check_rs2_error,
7    frame::{DepthFrame, FrameEx},
8    processing_blocks::{
9        errors::{ProcessFrameError, ProcessingBlockConstructionError},
10        options::{
11            get_processing_block_option, get_processing_block_option_range,
12            set_processing_block_option, HoleFillingOptions, OptionsExt,
13            ProcessingBlockOptionError,
14        },
15    },
16};
17use anyhow::Result;
18use realsense_sys as sys;
19use std::{convert::TryFrom, ptr::NonNull, task::Poll, time::Duration};
20
21/// Creates Depth post-processing hole filling block. The filter replaces empty pixels
22/// with data from adjacent pixels based on the method selected
23#[derive(Debug, Clone)]
24pub struct HoleFillingFilter {
25    /// The processing block for hole filling
26    processing_block: NonNull<sys::rs2_processing_block>,
27    /// The frame queue upon which the processing block will deposit filtered frames
28    processing_queue: NonNull<sys::rs2_frame_queue>,
29}
30
31impl Drop for HoleFillingFilter {
32    fn drop(&mut self) {
33        unsafe {
34            sys::rs2_delete_frame_queue(self.processing_queue.as_ptr());
35            sys::rs2_delete_processing_block(self.processing_block.as_ptr());
36        }
37    }
38}
39
40impl HoleFillingFilter {
41    /// Create a new HoleFillingFilter processing block
42    pub fn new(processing_queue_size: i32) -> Result<Self, ProcessingBlockConstructionError> {
43        let (processing_block, processing_queue) = unsafe {
44            let mut err = std::ptr::null_mut::<sys::rs2_error>();
45
46            let ptr = sys::rs2_create_hole_filling_filter_block(&mut err);
47            check_rs2_error!(
48                err,
49                ProcessingBlockConstructionError::CouldNotCreateProcessingBlock
50            )?;
51
52            let queue_ptr = sys::rs2_create_frame_queue(processing_queue_size, &mut err);
53            check_rs2_error!(
54                err,
55                ProcessingBlockConstructionError::CouldNotCreateProcessingQueue
56            )?;
57
58            sys::rs2_start_processing_queue(ptr, queue_ptr, &mut err);
59            check_rs2_error!(
60                err,
61                ProcessingBlockConstructionError::CouldNotStartProcessingQueue
62            )?;
63            (NonNull::new(ptr).unwrap(), NonNull::new(queue_ptr).unwrap())
64        };
65
66        Ok(Self {
67            processing_block,
68            processing_queue,
69        })
70    }
71
72    /// Process a depth frame with hole filling
73    pub fn queue(&mut self, frame: DepthFrame) -> Result<(), ProcessFrameError> {
74        unsafe {
75            let mut err = std::ptr::null_mut::<sys::rs2_error>();
76            sys::rs2_process_frame(
77                self.processing_block.as_ptr(),
78                frame.get_owned_raw().as_ptr(),
79                &mut err,
80            );
81            check_rs2_error!(err, |kind, context| { ProcessFrameError { kind, context } })?;
82            Ok(())
83        }
84    }
85
86    /// Wait to receive the hole-filled results
87    pub fn wait(&mut self, timeout: Duration) -> Result<DepthFrame, ProcessFrameError> {
88        unsafe {
89            let mut err = std::ptr::null_mut::<sys::rs2_error>();
90            let timeout_millis = u32::try_from(timeout.as_millis()).unwrap_or(u32::MAX);
91
92            let filtered_frame =
93                sys::rs2_wait_for_frame(self.processing_queue.as_ptr(), timeout_millis, &mut err);
94            check_rs2_error!(err, |kind, context| { ProcessFrameError { kind, context } })?;
95            Ok(DepthFrame::try_from(NonNull::new(filtered_frame).unwrap()).unwrap())
96        }
97    }
98
99    /// Poll to receive the hole-filled results
100    pub fn poll(&mut self) -> Result<Poll<DepthFrame>, ProcessFrameError> {
101        unsafe {
102            let mut err = std::ptr::null_mut::<sys::rs2_error>();
103            let mut frame = std::ptr::null_mut::<sys::rs2_frame>();
104            let is_ready =
105                sys::rs2_poll_for_frame(self.processing_queue.as_ptr(), &mut frame, &mut err);
106
107            // Check for errors
108            check_rs2_error!(err, |kind, context| { ProcessFrameError { kind, context } })?;
109
110            // Check for queue readiness
111            if is_ready == 0 {
112                Ok(Poll::Pending)
113            } else {
114                Ok(Poll::Ready(
115                    DepthFrame::try_from(NonNull::new(frame).unwrap()).unwrap(),
116                ))
117            }
118        }
119    }
120
121    /// Apply options to configure the hole filling filter
122    pub fn apply_options(
123        &mut self,
124        options: &HoleFillingOptions,
125    ) -> Result<(), ProcessingBlockOptionError> {
126        if let Some(holes_fill) = options.holes_fill {
127            self.set_option(sys::rs2_option_RS2_OPTION_HOLES_FILL, holes_fill)?;
128        }
129        Ok(())
130    }
131}
132
133impl OptionsExt for HoleFillingFilter {
134    fn set_option(
135        &mut self,
136        option: sys::rs2_option,
137        value: f32,
138    ) -> Result<(), ProcessingBlockOptionError> {
139        set_processing_block_option(self.processing_block, option, value)
140    }
141
142    fn get_option(&self, option: sys::rs2_option) -> Result<f32, ProcessingBlockOptionError> {
143        get_processing_block_option(self.processing_block, option)
144    }
145
146    fn supports_option(&self, option: sys::rs2_option) -> bool {
147        unsafe {
148            let mut err = std::ptr::null_mut::<sys::rs2_error>();
149            let is_supported = sys::rs2_supports_option(
150                self.processing_block.as_ptr() as *const sys::rs2_options,
151                option,
152                &mut err,
153            );
154            err.is_null() && is_supported != 0
155        }
156    }
157
158    fn get_option_range(
159        &self,
160        option: sys::rs2_option,
161    ) -> Result<(f32, f32, f32, f32), ProcessingBlockOptionError> {
162        get_processing_block_option_range(self.processing_block, option)
163    }
164}