From 483e3d4aa60bf713c81add4f5d545f0e7da5bf44 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Fri, 19 Mar 2021 02:14:34 -0400 Subject: [PATCH] translation + rotation, but I don't think it's not the right scale Perhaps I need to normalize... --- src/calibration/world_calibration.rs | 92 ++++++++++++++++++--- src/main_zeromq.rs | 9 +- src/pointcloud/cpu_calculate_point_cloud.rs | 14 ++++ src/setup/camera_info.rs | 2 + src/setup/open_devices.rs | 7 +- 5 files changed, 103 insertions(+), 21 deletions(-) diff --git a/src/calibration/world_calibration.rs b/src/calibration/world_calibration.rs index ff2ac15..a6cd576 100644 --- a/src/calibration/world_calibration.rs +++ b/src/calibration/world_calibration.rs @@ -9,6 +9,22 @@ use std::io::{BufReader, BufRead}; /// LiveScan3D can be used to derive calibration files. /// https://github.com/storytold/LiveScan3D pub struct WorldCalibration { + pub translation_x: f32, + pub translation_y: f32, + pub translation_z: f32, + + // TODO(bt): Use a linear algebra package + pub rotation_0_x: f32, + pub rotation_0_y: f32, + pub rotation_0_z: f32, + + pub rotation_1_x: f32, + pub rotation_1_y: f32, + pub rotation_1_z: f32, + + pub rotation_2_x: f32, + pub rotation_2_y: f32, + pub rotation_2_z: f32, } impl WorldCalibration { @@ -24,25 +40,40 @@ impl WorldCalibration { let mut string_buffer = String::new(); reader.read_line(&mut string_buffer)?; - let mut x = 0.0f32; - let mut y = 0.0f32; - let mut z = 0.0f32; + let mut translation_x = 0.0f32; + let mut translation_y = 0.0f32; + let mut translation_z = 0.0f32; if let Some(captures) = translation_regex.captures(&string_buffer) { if captures.len() == 4 { - x = captures[1].parse()?; - y = captures[2].parse()?; - z = captures[3].parse()?; + translation_x = captures[1].parse()?; + translation_y = captures[2].parse()?; + translation_z = captures[3].parse()?; } } - println!("x: {}", x); - println!("y: {}", y); - println!("z: {}", z); + println!("x: {}", translation_x); + println!("y: {}", translation_y); + println!("z: {}", translation_z); let rotation_regex : Regex = Regex::new(r"^(\-?\d\.\d+)(\-?\d\.\d+)(\-?\d\.\d+)\s*$")?; - for _i in 0..3 { + + let mut rotation_0_x = 0.0f32; + let mut rotation_0_y = 0.0f32; + let mut rotation_0_z = 0.0f32; + let mut rotation_1_x = 0.0f32; + let mut rotation_1_y = 0.0f32; + let mut rotation_1_z = 0.0f32; + let mut rotation_2_x = 0.0f32; + let mut rotation_2_y = 0.0f32; + let mut rotation_2_z = 0.0f32; + + for i in 0..3 { + let mut x = 0.0f32; + let mut y = 0.0f32; + let mut z = 0.0f32; + let mut string_buffer = String::new(); reader.read_line(&mut string_buffer)?; @@ -54,15 +85,44 @@ impl WorldCalibration { } } + match i { + 0 => { + rotation_0_x = x; + rotation_0_y = y; + rotation_0_z = z; + }, + 1 => { + rotation_1_x = x; + rotation_1_y = y; + rotation_1_z = z; + }, + 2 => { + rotation_2_x = x; + rotation_2_y = y; + rotation_2_z = z; + }, + _ => {}, + } + println!("x: {}", x); println!("y: {}", y); println!("z: {}", z); println!(""); } - - Ok(Self { + translation_x, + translation_y, + translation_z, + rotation_0_x, + rotation_0_y, + rotation_0_z, + rotation_1_x, + rotation_1_y, + rotation_1_z, + rotation_2_x, + rotation_2_y, + rotation_2_z, }) } @@ -70,6 +130,14 @@ impl WorldCalibration { let serial_number = device.get_serial_number()?; Self::load_from_file_and_serial(&serial_number) } + + pub fn rotate_point(&self, x: f32, y: f32, z: f32) -> (f32, f32, f32) { + let x0 = x * self.rotation_0_x + y * self.rotation_0_y + z * self.rotation_0_z; + let y0 = x * self.rotation_1_x + y * self.rotation_1_y + z * self.rotation_1_z; + let z0 = x * self.rotation_1_x + y * self.rotation_1_y + z * self.rotation_1_z; + + (x0, y0, z0) + } } /* diff --git a/src/main_zeromq.rs b/src/main_zeromq.rs index 80b9c61..55aba3b 100644 --- a/src/main_zeromq.rs +++ b/src/main_zeromq.rs @@ -117,10 +117,6 @@ fn main() -> AnyhowResult<()> { println!("Opening camera {}...", index); let (device, camera_info) = open_and_configure_camera(&args, index as u32)?; - println!("Loading world calibration..."); - let world_calibration = WorldCalibration::load_from_file_and_device(&device)?; - - println!("Starting camera {}...", index); device.start_cameras(&camera_info.config)?; @@ -128,10 +124,6 @@ fn main() -> AnyhowResult<()> { camera_infos.push(camera_info); } - if true { - return Ok(()); - } - let multi_device_capturer = MultiDeviceCapturer::new(devices, true, true); let capture_buffer = multi_device_capturer.get_capture_buffer(); @@ -271,6 +263,7 @@ fn get_point_cloud( calculate_point_cloud3( &depth_image2, &camera_info.xy_table, + Some(&camera_info.world_calibration), //color, &color_image, command_args, diff --git a/src/pointcloud/cpu_calculate_point_cloud.rs b/src/pointcloud/cpu_calculate_point_cloud.rs index c63fc9a..904de55 100644 --- a/src/pointcloud/cpu_calculate_point_cloud.rs +++ b/src/pointcloud/cpu_calculate_point_cloud.rs @@ -8,6 +8,7 @@ use kinect::Image; use kinect::ImageFormat; use num_traits::Float; use std::mem::size_of; +use crate::calibration::world_calibration::WorldCalibration; pub struct PointCloudResult { pub point_cloud_image: Image, @@ -107,6 +108,7 @@ struct ColorCameraPoint { pub fn calculate_point_cloud3( depth_image: &Image, xy_table_image: &Image, + maybe_world_calibration: Option<&WorldCalibration>, color_image: &Image, command_args: &CommandArgs ) -> AnyhowResult> @@ -147,6 +149,18 @@ pub fn calculate_point_cloud3( let mut y = y_lut * ((*depth_data.offset(i)) as f32); let mut z = (*depth_data.offset(i)) as f32; + if let Some(world_calibration) = maybe_world_calibration { + x += world_calibration.translation_x; + y += world_calibration.translation_y; + z += world_calibration.translation_z; + + let (x2, y2, z2) = world_calibration.rotate_point(x, y, z); + + x = x2; + y = y2; + z = z2; + } + let color = (*color_data.offset(i)) as ColorCameraPoint; // The z-direction moves directly towards and away from the camera diff --git a/src/setup/camera_info.rs b/src/setup/camera_info.rs index 841c78b..a1ca45c 100644 --- a/src/setup/camera_info.rs +++ b/src/setup/camera_info.rs @@ -1,5 +1,6 @@ use kinect::{Device, Calibration, Image, DeviceConfiguration}; use crate::pointcloud::cpu_transformation::DepthTransformer; +use crate::calibration::world_calibration::WorldCalibration; /// Extra metadata about a camera. pub struct CameraInfo { @@ -7,4 +8,5 @@ pub struct CameraInfo { pub calibration: Calibration, pub xy_table: Image, pub transformer: DepthTransformer, + pub world_calibration: WorldCalibration, } diff --git a/src/setup/open_devices.rs b/src/setup/open_devices.rs index 10436c3..2b47d05 100644 --- a/src/setup/open_devices.rs +++ b/src/setup/open_devices.rs @@ -5,6 +5,7 @@ use crate::pointcloud::xy_table::create_xy_table_from_color_calibration; use crate::pointcloud::cpu_transformation::DepthTransformer; use crate::AnyhowResult; use k4a_sys_temp as k4a_sys; +use crate::calibration::world_calibration::WorldCalibration; pub fn open_and_configure_camera(args: &CommandArgs, index: u32) -> AnyhowResult<(Device, CameraInfo)> { @@ -37,11 +38,15 @@ pub fn open_and_configure_camera(args: &CommandArgs, index: u32) -> AnyhowResult let transformer = DepthTransformer::new(&calibration); + println!("Loading world calibration..."); + let world_calibration = WorldCalibration::load_from_file_and_device(&device)?; + let camera_info = CameraInfo { config, calibration, xy_table, - transformer + transformer, + world_calibration, }; Ok((device, camera_info))