Skip to content

Commit 829d2f4

Browse files
committed
Simplify some vector type conversions
1 parent 2f46e5b commit 829d2f4

File tree

4 files changed

+18
-30
lines changed

4 files changed

+18
-30
lines changed

splashsurf_lib/src/io/bgeo_format.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::io::io_utils::IteratorExt;
44
use crate::mesh::{AttributeData, MeshAttribute};
5-
use crate::{Real, io::io_utils};
5+
use crate::{Real, RealConvert, io::io_utils};
66
use anyhow::{Context, anyhow};
77
use flate2::Compression;
88
use flate2::read::GzDecoder;
@@ -47,13 +47,7 @@ pub fn particles_from_bgeo_file<R: Real>(
4747
// Convert the array storage into individual vectors
4848
let positions: Vec<_> = position_storage
4949
.chunks(3)
50-
.map(|p| {
51-
Vector3::new(
52-
R::from_f32(p[0]).unwrap(),
53-
R::from_f32(p[1]).unwrap(),
54-
R::from_f32(p[2]).unwrap(),
55-
)
56-
})
50+
.map(|p| Vector3::new(p[0], p[1], p[2]).try_convert().unwrap())
5751
.collect();
5852

5953
Ok(positions)

splashsurf_lib/src/io/obj_format.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::mesh::{
44
AttributeData, CellConnectivity, Mesh3d, MeshAttribute, MeshWithData, TriMesh3d,
55
};
6-
use crate::{Real, io::io_utils, profile};
6+
use crate::{Real, RealConvert, io::io_utils, profile};
77
use anyhow::Context;
88
use nalgebra::Vector3;
99
use std::fs;
@@ -82,10 +82,12 @@ pub fn surface_mesh_from_obj<R: Real, P: AsRef<Path>>(
8282

8383
let buffer_to_vec3 = |buffer: &[&str]| -> Result<Vector3<R>, anyhow::Error> {
8484
Ok(Vector3::new(
85-
R::from_f64(f64::from_str(buffer[0])?).unwrap(),
86-
R::from_f64(f64::from_str(buffer[1])?).unwrap(),
87-
R::from_f64(f64::from_str(buffer[2])?).unwrap(),
88-
))
85+
f64::from_str(buffer[0])?,
86+
f64::from_str(buffer[1])?,
87+
f64::from_str(buffer[2])?,
88+
)
89+
.try_convert()
90+
.unwrap())
8991
};
9092

9193
let mut outer_buffer: Vec<&'static str> = Vec::new();

splashsurf_lib/src/io/ply_format.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::io::io_utils::IteratorExt;
44
use crate::mesh::{
55
AttributeData, CellConnectivity, Mesh3d, MeshAttribute, MeshWithData, TriMesh3d,
66
};
7-
use crate::{Real, profile};
7+
use crate::{Real, RealConvert, profile};
88
use anyhow::{Context, anyhow};
99
use nalgebra::Vector3;
1010
use num_traits::ToPrimitive;
@@ -53,11 +53,9 @@ fn parse_particles_from_ply<R: Real>(
5353
);
5454

5555
let v = match vertex {
56-
(Property::Float(x), Property::Float(y), Property::Float(z)) => Vector3::new(
57-
R::from_f32(*x).unwrap(),
58-
R::from_f32(*y).unwrap(),
59-
R::from_f32(*z).unwrap(),
60-
),
56+
(Property::Float(x), Property::Float(y), Property::Float(z)) => {
57+
Vector3::new(*x, *y, *z).try_convert().unwrap()
58+
}
6159
_ => {
6260
return Err(anyhow!(
6361
"Vertex properties have wrong PLY data type (expected float)"
@@ -102,11 +100,9 @@ fn parse_mesh_from_ply<R: Real>(
102100
let mut vertices = Vec::with_capacity(vertices_normals.len());
103101

104102
let load_vec3_property = |vertex: (&Property, &Property, &Property)| match vertex {
105-
(Property::Float(x), Property::Float(y), Property::Float(z)) => Ok(Vector3::new(
106-
R::from_f32(*x).unwrap(),
107-
R::from_f32(*y).unwrap(),
108-
R::from_f32(*z).unwrap(),
109-
)),
103+
(Property::Float(x), Property::Float(y), Property::Float(z)) => {
104+
Ok(Vector3::new(*x, *y, *z).try_convert().unwrap())
105+
}
110106
_ => Err(anyhow!(
111107
"Vertex properties have wrong PLY data type (expected float)"
112108
)),

splashsurf_lib/src/io/xyz_format.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Helper functions for the binary `.xyz` float coordinate format
22
3-
use crate::Real;
3+
use crate::{Real, RealConvert};
44
use anyhow::Context;
55
use nalgebra::Vector3;
66
use std::fs::File;
@@ -30,11 +30,7 @@ pub fn particles_from_xyz<R: Real, P: AsRef<Path>>(
3030
let x = f32::from_ne_bytes(get_four_bytes(&buffer, 0));
3131
let y = f32::from_ne_bytes(get_four_bytes(&buffer, 4));
3232
let z = f32::from_ne_bytes(get_four_bytes(&buffer, 8));
33-
particles.push(Vector3::new(
34-
R::from_f32(x).unwrap(),
35-
R::from_f32(y).unwrap(),
36-
R::from_f32(z).unwrap(),
37-
));
33+
particles.push(Vector3::new(x, y, z).try_convert().unwrap());
3834
}
3935

4036
Ok(particles)

0 commit comments

Comments
 (0)