-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclosest_point.rs
39 lines (34 loc) · 1.36 KB
/
closest_point.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
// Rust implementation of the OpenNI2 C++ sample
// https://github.com/OpenNI/OpenNI2/blob/master/Samples/MWClosestPoint/MWClosestPoint.cpp
// https://github.com/OpenNI/OpenNI2/blob/master/Samples/MWClosestPointApp/main.cpp
extern crate openni2;
use openni2::{Status, Stream};
use std::{thread, time};
fn callback(stream: &Stream) {
// The whole idea of a stream listener is that its callback fires when a frame
// is ready to read, thus the `expect` should be fine.
let frame = stream.read_frame::<openni2::OniDepthPixel>().expect("Frame somehow not available for read.");
let px = frame.pixels();
let closest = px.iter()
.enumerate()
.fold((0u16, 0u16, ::std::u16::MAX), |closest, (n, &depth)| {
let (x, y) = (n as u16 % frame.width(), n as u16 / frame.width());
if depth < closest.2 && depth != 0 {
(x, y, depth)
} else {
closest
}
});
println!("[{:-6} {:-6} {:-6}]", closest.0, closest.1, closest.2);
}
fn main() -> Result<(), Status> {
openni2::init()?;
let device = openni2::Device::open_default()?;
let stream = device.create_stream(openni2::SensorType::DEPTH)?;
let _listener = stream.listener(callback)?;
stream.start()?;
let one_second = time::Duration::from_millis(1000);
loop {
thread::sleep(one_second);
}
}