Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API that allows reading many points #68

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/compression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,36 @@ impl<'a, R: Read + Seek + Send> PointReader for CompressedPointReader<'a, R> {
.decompress_one(&mut self.decompressor_output.get_mut());
if let Err(e) = res {
Some(Err(e.into()))
} else if let Err(e) = self.decompressor_output.seek(SeekFrom::Start(0)) {
Some(Err(e.into()))
} else {
self.decompressor_output.set_position(0);
Some(read_point_from(&mut self.decompressor_output, &self.header))
}
} else {
None
}
}

fn read_into_vec(&mut self, points: &mut Vec<Point>, n: u64) -> Result<u64> {
let points_left = self.header().number_of_points() - self.last_point_idx;
let num_points_to_read = points_left.min(n);

self.decompressor_output.get_mut().resize(
num_points_to_read as usize * self.header.point_format().len() as usize,
0u8,
);
self.decompressor
.decompress_many(self.decompressor_output.get_mut())?;
self.decompressor_output.set_position(0);
points.reserve(num_points_to_read as usize);

for _ in 0..num_points_to_read {
let point = read_point_from(&mut self.decompressor_output, &self.header)?;
self.last_point_idx += 1;
points.push(point);
}
Ok(num_points_to_read)
}

fn seek(&mut self, position: u64) -> Result<()> {
self.last_point_idx = position;
self.decompressor.seek(position)?;
Expand Down
42 changes: 41 additions & 1 deletion src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ pub(crate) fn read_point_from<R: std::io::Read>(
point
}

/// Trait to specify behaviour a a PointReader
/// Trait to specify behaviour of a PointReader
pub(crate) trait PointReader: Debug + Send {
fn read_next(&mut self) -> Option<Result<Point>>;
fn read_into_vec(&mut self, points: &mut Vec<Point>, n: u64) -> Result<u64>;
fn read_next_points(&mut self, n: u64) -> Result<Vec<Point>> {
let mut points = Vec::with_capacity(n as usize);
self.read_into_vec(&mut points, n)?;
Ok(points)
}
fn seek(&mut self, position: u64) -> Result<()>;
fn header(&self) -> &Header;
}
Expand Down Expand Up @@ -116,6 +122,18 @@ impl<R: std::io::Read + Seek + Debug + Send> PointReader for UncompressedPointRe
}
}

fn read_into_vec(&mut self, points: &mut Vec<Point>, n: u64) -> Result<u64> {
let points_left = self.header().number_of_points() - self.last_point_idx;
let num_points_to_read = points_left.min(n);
points.reserve(num_points_to_read as usize);
for _ in 0..num_points_to_read {
let point = read_point_from(&mut self.source, &self.header)?;
self.last_point_idx += 1;
points.push(point);
}
Ok(num_points_to_read)
}

fn seek(&mut self, position: u64) -> Result<()> {
self.last_point_idx = position;
self.source.seek(SeekFrom::Start(
Expand Down Expand Up @@ -153,6 +171,15 @@ pub trait Read {
/// ```
fn read(&mut self) -> Option<Result<Point>>;

/// Reads n points.
fn read_n(&mut self, n: u64) -> Result<Vec<Point>>;

/// Reads n points into the vec
fn read_n_into(&mut self, n: u64, points: &mut Vec<Point>) -> Result<u64>;

/// Reads all points left into the vec
fn read_all_points(&mut self, points: &mut Vec<Point>) -> Result<u64>;

/// Seeks to the given point number, zero-indexed.
///
/// Note that seeking on compressed (LAZ) data can be expensive as the reader
Expand Down Expand Up @@ -301,6 +328,19 @@ impl<'a> Read for Reader<'a> {
self.point_reader.read_next()
}

fn read_n(&mut self, n: u64) -> Result<Vec<Point>> {
self.point_reader.read_next_points(n)
}

fn read_n_into(&mut self, n: u64, points: &mut Vec<Point>) -> Result<u64> {
self.point_reader.read_into_vec(points, n)
}

fn read_all_points(&mut self, points: &mut Vec<Point>) -> Result<u64> {
let point_count = self.point_reader.header().number_of_points();
self.point_reader.read_into_vec(points, point_count)
}

/// Seeks to the given point number, zero-indexed.
fn seek(&mut self, position: u64) -> Result<()> {
self.point_reader.seek(position)
Expand Down
97 changes: 97 additions & 0 deletions tests/autzen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,100 @@ fn test_seek_0_works_on_las() {
fn test_seek_0_works_on_laz() {
test_seek_0_works_on("tests/data/autzen.laz");
}

fn test_read_n_on(path: &str) {
use las::{Point, Read, Reader};

let ground_truth_points = {
let mut ground_truth_reader = Reader::from_path(path).unwrap();
ground_truth_reader
.points()
.collect::<las::Result<Vec<Point>>>()
.unwrap()
};

let mut reader = Reader::from_path(path).unwrap();
let n = 7;
let mut all_points = Vec::with_capacity(reader.header().number_of_points() as usize);
loop {
let mut points = reader.read_n(n).unwrap();
if points.len() == 0 {
break;
}
all_points.append(&mut points);
}

assert_eq!(all_points, ground_truth_points);
}

fn test_read_n_into_on(path: &str) {
use las::{Point, Read, Reader};

let ground_truth_points = {
let mut ground_truth_reader = Reader::from_path(path).unwrap();
ground_truth_reader
.points()
.collect::<las::Result<Vec<Point>>>()
.unwrap()
};

let mut reader = Reader::from_path(path).unwrap();
let n = 7;
let mut all_points = Vec::with_capacity(reader.header().number_of_points() as usize);
let mut points_buffer = Vec::with_capacity(n as usize);
while reader.read_n_into(n, &mut points_buffer).unwrap() != 0 {
all_points.append(&mut points_buffer);
}

assert_eq!(all_points, ground_truth_points);
}

#[test]
fn test_las_read_n() {
test_read_n_on("tests/data/autzen.las");
}

#[cfg(feature = "laz")]
#[test]
fn test_laz_read_n() {
test_read_n_on("tests/data/autzen.laz");
}

#[test]
fn test_las_read_n_into() {
test_read_n_into_on("tests/data/autzen.las");
}

#[cfg(feature = "laz")]
#[test]
fn test_laz_read_n_into() {
test_read_n_into_on("tests/data/autzen.laz");
}

fn test_read_all_points_on(path: &str) {
use las::{Point, Read, Reader};

let ground_truth_points = {
let mut ground_truth_reader = Reader::from_path(path).unwrap();
ground_truth_reader
.points()
.collect::<las::Result<Vec<Point>>>()
.unwrap()
};

let mut reader = Reader::from_path(path).unwrap();
let mut all_points = vec![];
reader.read_all_points(&mut all_points).unwrap();
assert_eq!(all_points, ground_truth_points);
}

#[test]
fn test_las_read_all_points() {
test_read_all_points_on("tests/data/autzen.las");
}

#[cfg(feature = "laz")]
#[test]
fn test_laz_read_all_points() {
test_read_all_points_on("tests/data/autzen.laz");
}