forked from jerry73204/realsense-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_profile.rs
273 lines (246 loc) · 8.6 KB
/
stream_profile.rs
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
//! Defines the profile type of streams.
use crate::{
base::{Extrinsics, Intrinsics, MotionIntrinsics, Resolution, StreamProfileData},
common::*,
error::{ErrorChecker, Result},
kind::{Format, StreamKind},
stream_profile_kind,
};
/// The enumeration of extended stream profile type returned by [StreamProfile::try_extend](StreamProfile::try_extend).
#[derive(Debug)]
pub enum ExtendedStreamProfile {
Video(VideoStreamProfile),
Motion(MotionStreamProfile),
Pose(PoseStreamProfile),
Other(AnyStreamProfile),
}
/// The profile of stream.
#[derive(Debug)]
pub struct StreamProfile<Kind>
where
Kind: stream_profile_kind::StreamProfileKind,
{
ptr: NonNull<sys::rs2_stream_profile>,
from_clone: bool,
_phantom: PhantomData<Kind>,
}
// type aliases
pub type VideoStreamProfile = StreamProfile<stream_profile_kind::Video>;
pub type MotionStreamProfile = StreamProfile<stream_profile_kind::Motion>;
pub type PoseStreamProfile = StreamProfile<stream_profile_kind::Pose>;
pub type AnyStreamProfile = StreamProfile<stream_profile_kind::Any>;
impl<Kind> StreamProfile<Kind>
where
Kind: stream_profile_kind::StreamProfileKind,
{
/// Check whether the profile is default or not.
pub fn is_default(&self) -> Result<bool> {
unsafe {
let mut checker = ErrorChecker::new();
let val =
sys::rs2_is_stream_profile_default(self.ptr.as_ptr(), checker.inner_mut_ptr());
checker.check()?;
Ok(val != 0)
}
}
/// Gets the attributes of stream.
pub fn get_data(&self) -> Result<StreamProfileData> {
unsafe {
let mut checker = ErrorChecker::new();
let mut stream = MaybeUninit::uninit();
let mut format = MaybeUninit::uninit();
let mut index = MaybeUninit::uninit();
let mut unique_id = MaybeUninit::uninit();
let mut framerate = MaybeUninit::uninit();
sys::rs2_get_stream_profile_data(
self.ptr.as_ptr(),
stream.as_mut_ptr(),
format.as_mut_ptr(),
index.as_mut_ptr(),
unique_id.as_mut_ptr(),
framerate.as_mut_ptr(),
checker.inner_mut_ptr(),
);
checker.check()?;
let data = StreamProfileData {
stream: StreamKind::from_u32(stream.assume_init()).unwrap(),
format: Format::from_u32(format.assume_init()).unwrap(),
index: index.assume_init() as usize,
unique_id: unique_id.assume_init(),
framerate: framerate.assume_init(),
};
Ok(data)
}
}
// /// Sets the attributes of stream.
// pub fn set_data(&mut self) {
// todo!();
// }
/// Gets the extrinsic parameters to another stream.
pub fn get_extrinsics<P, K>(&self, to_stream: P) -> Result<Extrinsics>
where
P: Borrow<StreamProfile<K>>,
K: stream_profile_kind::StreamProfileKind,
{
unsafe {
let mut extrinsics = MaybeUninit::<sys::rs2_extrinsics>::uninit();
let mut checker = ErrorChecker::new();
sys::rs2_get_extrinsics(
self.ptr.as_ptr(),
to_stream.borrow().ptr.as_ptr(),
extrinsics.as_mut_ptr(),
checker.inner_mut_ptr(),
);
checker.check()?;
Ok(Extrinsics(extrinsics.assume_init()))
}
}
/// Sets the extrinsic parameters to another stream.
pub fn set_extrinsics<P, K>(&self, to_stream: P, extrinsics: Extrinsics) -> Result<()>
where
P: Borrow<StreamProfile<K>>,
K: stream_profile_kind::StreamProfileKind,
{
unsafe {
let mut checker = ErrorChecker::new();
sys::rs2_register_extrinsics(
self.ptr.as_ptr(),
to_stream.borrow().ptr.as_ptr(),
*extrinsics,
checker.inner_mut_ptr(),
);
checker.check()?;
Ok(())
}
}
pub fn into_raw_parts(self) -> (*mut sys::rs2_stream_profile, bool) {
let ptr = self.ptr;
let from_clone = self.from_clone;
mem::forget(self);
(ptr.as_ptr(), from_clone)
}
pub unsafe fn from_raw_parts(ptr: *mut sys::rs2_stream_profile, from_clone: bool) -> Self {
Self {
ptr: NonNull::new(ptr).unwrap(),
from_clone,
_phantom: PhantomData,
}
}
}
impl AnyStreamProfile {
/// Check if the stream is extendable to the given extension.
pub fn is_extendable_to<Kind>(&self) -> Result<bool>
where
Kind: stream_profile_kind::NonAnyStreamProfileKind,
{
unsafe {
let mut checker = ErrorChecker::new();
let val = sys::rs2_stream_profile_is(
self.ptr.as_ptr(),
Kind::EXTENSION as sys::rs2_extension,
checker.inner_mut_ptr(),
);
checker.check()?;
Ok(val != 0)
}
}
/// Extends to a specific stream profile subtype.
pub fn try_extend_to<Kind>(self) -> Result<result::Result<StreamProfile<Kind>, Self>>
where
Kind: stream_profile_kind::NonAnyStreamProfileKind,
{
if self.is_extendable_to::<Kind>()? {
let (ptr, from_clone) = self.into_raw_parts();
let profile = StreamProfile {
ptr: NonNull::new(ptr).unwrap(),
from_clone,
_phantom: PhantomData,
};
Ok(Ok(profile))
} else {
Ok(Err(self))
}
}
/// Extends to one of a stream profile subtype.
pub fn try_extend(self) -> Result<ExtendedStreamProfile> {
let profile_any = self;
let profile_any = match profile_any.try_extend_to::<stream_profile_kind::Video>()? {
Ok(profile) => return Ok(ExtendedStreamProfile::Video(profile)),
Err(profile) => profile,
};
let profile_any = match profile_any.try_extend_to::<stream_profile_kind::Motion>()? {
Ok(profile) => return Ok(ExtendedStreamProfile::Motion(profile)),
Err(profile) => profile,
};
let profile_any = match profile_any.try_extend_to::<stream_profile_kind::Pose>()? {
Ok(profile) => return Ok(ExtendedStreamProfile::Pose(profile)),
Err(profile) => profile,
};
Ok(ExtendedStreamProfile::Other(profile_any))
}
}
impl VideoStreamProfile {
/// Gets the resolution of stream.
pub fn resolution(&self) -> Result<Resolution> {
let mut width = MaybeUninit::uninit();
let mut height = MaybeUninit::uninit();
let resolution = unsafe {
let mut checker = ErrorChecker::new();
sys::rs2_get_video_stream_resolution(
self.ptr.as_ptr(),
width.as_mut_ptr(),
height.as_mut_ptr(),
checker.inner_mut_ptr(),
);
checker.check()?;
Resolution {
width: width.assume_init() as usize,
height: height.assume_init() as usize,
}
};
Ok(resolution)
}
/// Gets the intrinsic parameters.
pub fn intrinsics(&self) -> Result<Intrinsics> {
unsafe {
let mut checker = ErrorChecker::new();
let mut intrinsics = MaybeUninit::<sys::rs2_intrinsics>::uninit();
sys::rs2_get_video_stream_intrinsics(
self.ptr.as_ptr(),
intrinsics.as_mut_ptr(),
checker.inner_mut_ptr(),
);
checker.check()?;
Ok(Intrinsics(intrinsics.assume_init()))
}
}
}
impl MotionStreamProfile {
/// Gets the motion intrinsic parameters.
pub fn motion_intrinsics(&self) -> Result<MotionIntrinsics> {
unsafe {
let mut checker = ErrorChecker::new();
let mut intrinsics = MaybeUninit::<sys::rs2_motion_device_intrinsic>::uninit();
sys::rs2_get_motion_intrinsics(
self.ptr.as_ptr(),
intrinsics.as_mut_ptr(),
checker.inner_mut_ptr(),
);
checker.check()?;
Ok(MotionIntrinsics(intrinsics.assume_init()))
}
}
}
impl<Kind> Drop for StreamProfile<Kind>
where
Kind: stream_profile_kind::StreamProfileKind,
{
fn drop(&mut self) {
unsafe {
if self.from_clone {
sys::rs2_delete_stream_profile(self.ptr.as_ptr());
}
}
}
}
unsafe impl<Kind> Send for StreamProfile<Kind> where Kind: stream_profile_kind::StreamProfileKind {}