Skip to content

Commit

Permalink
Speed up E57-to-XYZ tool by using faster float-to-string conversion p…
Browse files Browse the repository at this point in the history
…owered by the ryu crate
  • Loading branch information
cry-inc committed Aug 22, 2023
1 parent 488000e commit 927490a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.
to easily check if it has certain point attributes
- Added simple iterator option to convert Cartesian to spherical coordinates
- Added new E57-to-LAZ example tool
- Faster E57-to-XYZ tool (uses now ryu for float-to-string conversion)
- Added this CHANGELOG.md file

## [0.7.0] - 2023-08-16
Expand Down
1 change: 1 addition & 0 deletions tools/e57-to-xyz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ publish = false
[dependencies]
e57 = { path = "../../" }
anyhow = "1"
ryu = "1"
22 changes: 18 additions & 4 deletions tools/e57-to-xyz/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ fn main() -> Result<()> {
let writer = File::create(out_file).context("Unable to open output file for writing")?;
let mut writer = BufWriter::new(writer);

// Prepare fast floating point to ASCII generation.
// The std implementation is a bit slower compared to the specialized ryu crate.
let mut buffer = ryu::Buffer::new();

// Loop over all point clouds in the E57 file
let pointclouds = file.pointclouds();
for pointcloud in pointclouds {
Expand All @@ -54,9 +58,19 @@ fn main() -> Result<()> {

// Write XYZ data to output file
if let CartesianCoordinate::Valid { x, y, z } = p.cartesian {
writer
.write_fmt(format_args!("{x} {y} {z}"))
.context("Failed to write XYZ coordinates")?;
let space = " ".as_bytes();
let xyz_err = "Failed to write XYZ coordinates";

let str = buffer.format(x);
writer.write_all(str.as_bytes()).context(xyz_err)?;
writer.write_all(space).context(xyz_err)?;

let str = buffer.format(y);
writer.write_all(str.as_bytes()).context(xyz_err)?;
writer.write_all(space).context(xyz_err)?;

let str = buffer.format(z);
writer.write_all(str.as_bytes()).context(xyz_err)?;
} else {
continue;
}
Expand All @@ -75,7 +89,7 @@ fn main() -> Result<()> {

// Write new line before next point
writer
.write_fmt(format_args!("\n"))
.write_all("\n".as_bytes())
.context("Failed to write newline")?;
}
}
Expand Down

0 comments on commit 927490a

Please sign in to comment.