Skip to content

Commit

Permalink
Removed some duplicated code and remove hacky public access to a stru…
Browse files Browse the repository at this point in the history
…ct member
  • Loading branch information
cry-inc committed Jan 25, 2024
1 parent 20f981b commit 4258191
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
17 changes: 3 additions & 14 deletions src/pc_reader_raw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::error::Converter;
use crate::paged_reader::PagedReader;
use crate::queue_reader::QueueReader;
use crate::PointCloud;
Expand Down Expand Up @@ -26,17 +25,6 @@ impl<'a, T: Read + Seek> PointCloudReaderRaw<'a, T> {
read: 0,
})
}

fn pop_point(&mut self) -> Result<RawValues> {
let mut point = RawValues::with_capacity(self.prototype_len);
for i in 0..self.prototype_len {
let value = self.queue_reader.queues[i]
.pop_front()
.internal_err("Failed to pop value for next point")?;
point.push(value);
}
Ok(point)
}
}

impl<'a, T: Read + Seek> Iterator for PointCloudReaderRaw<'a, T> {
Expand All @@ -58,8 +46,9 @@ impl<'a, T: Read + Seek> Iterator for PointCloudReaderRaw<'a, T> {
}

// Extract next point
match self.pop_point() {
Ok(point) => {
let mut point = RawValues::with_capacity(self.prototype_len);
match self.queue_reader.pop_point(&mut point) {
Ok(()) => {
self.read += 1;
Some(Ok(point))
}
Expand Down
9 changes: 1 addition & 8 deletions src/pc_reader_simple.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::error::Converter;
use crate::paged_reader::PagedReader;
use crate::queue_reader::QueueReader;
use crate::{
Expand Down Expand Up @@ -144,13 +143,7 @@ impl<'a, T: Read + Seek> PointCloudReaderSimple<'a, T> {

fn pop_point(&mut self) -> Result<Point> {
// Read raw values of the point from queue
self.values.clear();
for i in 0..self.pc.prototype.len() {
let value = self.queue_reader.queues[i]
.pop_front()
.internal_err("Failed to pop value for next point")?;
self.values.push(value);
}
self.queue_reader.pop_point(&mut self.values)?;

// Some shortcuts for better readability
let proto = &self.pc.prototype;
Expand Down
17 changes: 15 additions & 2 deletions src/queue_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ use crate::packet::PacketHeader;
use crate::paged_reader::PagedReader;
use crate::Error;
use crate::PointCloud;
use crate::RawValues;
use crate::RecordDataType;
use crate::RecordValue;
use crate::Result;
use std::collections::VecDeque;
use std::io::{Read, Seek};

/// Read compressed vector sections into queues of raw values.
/// There will be once queue for each record defined by the prototype.
pub struct QueueReader<'a, T: Read + Seek> {
pc: PointCloud,
reader: &'a mut PagedReader<T>,
buffer: Vec<u8>,
buffer_sizes: Vec<usize>,
byte_streams: Vec<ByteStreamReadBuffer>,
pub queues: Vec<VecDeque<RecordValue>>,
queues: Vec<VecDeque<RecordValue>>,
}

impl<'a, T: Read + Seek> QueueReader<'a, T> {
Expand Down Expand Up @@ -59,6 +59,19 @@ impl<'a, T: Read + Seek> QueueReader<'a, T> {
av
}

/// Return values for the next point by popping one value from each queue.
/// Use an existing vector with enough capacity to avoid frequent reallocations!
pub fn pop_point(&mut self, output: &mut RawValues) -> Result<()> {
output.clear();
for i in 0..self.pc.prototype.len() {
let value = self.queues[i]
.pop_front()
.internal_err("Failed to pop value for next point")?;
output.push(value);
}
Ok(())
}

/// Reads the next packet from the compressed vector and decodes it into the queues.
pub fn advance(&mut self) -> Result<()> {
let packet_header = PacketHeader::read(self.reader)?;
Expand Down

0 comments on commit 4258191

Please sign in to comment.