use crate::PointCloud;
use anyhow::Result;
use na::Point3;
use nalgebra as na;
use std::convert::TryInto;
use std::fs::File;
use std::io::{BufRead, BufReader, Read};
pub fn read_kitti_bin_file(pointcloud_file: String) -> Result<PointCloud> {
let mut file = File::open(pointcloud_file)?;
let mut raw_data = vec![];
file.read_to_end(&mut raw_data)?;
let mut pcl_data: PointCloud = PointCloud::new();
for num in raw_data.chunks_exact(16) {
let x = f32::from_ne_bytes(num[0..4].try_into().expect("not the good size for the x coordinate"));
let y = f32::from_ne_bytes(num[4..8].try_into().expect("not the good size for the y coordinate"));
let z = f32::from_ne_bytes(num[8..12].try_into().expect("not the good size for the z coordinate"));
let _ = f32::from_ne_bytes(num[12..16].try_into().expect("not the good size for intensity"));
let point = Point3::from_slice(&[z, x, y]);
let color = Point3::new(1.0, 1.0, 1.0);
pcl_data.push(point);
pcl_data.push(color);
}
Ok(pcl_data)
}
#[allow(dead_code)]
pub fn parse_pointcloud_txt(file_path: String) -> Result<PointCloud> {
let file = File::open(file_path)?;
let reader = BufReader::new(&file);
let mut pcl_data: PointCloud = PointCloud::new();
for line in reader.lines() {
let data = line?;
let data_point: Vec<f32> = data
.split_whitespace()
.map(|word| word.parse::<f32>().unwrap())
.collect();
let point = Point3::from_slice(&[data_point[2], data_point[0], data_point[1]]);
let color = Point3::new(1.0, 1.0, 1.0);
pcl_data.push(point);
pcl_data.push(color);
}
Ok(pcl_data)
}