Skip to content

Commit be12020

Browse files
committed
Handle some panics with errors instead
1 parent 526c700 commit be12020

1 file changed

Lines changed: 34 additions & 26 deletions

File tree

splashsurf/src/io.rs

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -111,39 +111,47 @@ pub fn particles_from_xyz<R: Real, P: AsRef<Path>>(
111111
Ok(particles)
112112
}
113113

114-
pub fn particles_from_ply<R:Real, P:AsRef<Path>>(
115-
ply_file: P
114+
pub fn particles_from_ply<R: Real, P: AsRef<Path>>(
115+
ply_file: P,
116116
) -> Result<Vec<Vector3<R>>, anyhow::Error> {
117117
let mut ply_file = std::fs::File::open(ply_file).unwrap();
118118
let parser = ply::parser::Parser::<ply::ply::DefaultElement>::new();
119119

120-
let ply = parser.read_ply(&mut ply_file);
120+
let ply = parser
121+
.read_ply(&mut ply_file)
122+
.context("Failed to read PLY file")?;
123+
let elements = ply
124+
.payload
125+
.get("vertex")
126+
.ok_or(anyhow!("PLY file is missing a 'vertex' element"))?;
121127

122-
assert!(ply.is_ok());
123-
let ply = ply.unwrap();
124-
125-
let elements = ply.payload.get("vertex").unwrap();
126-
127-
let points: Vec<Vector3<R>> = elements
128+
let particles = elements
128129
.into_iter()
129-
.map(|e| (e.get("x").unwrap(), e.get("y").unwrap(), e.get("z").unwrap()))
130-
.map(|point| {
131-
let vector: Vector3<R> = match point {
132-
(Property::Float(x), Property::Float(y), Property::Float(z)) =>
133-
Vector3::new(
134-
R::from_f32(*x).unwrap(),
135-
R::from_f32(*y).unwrap(),
136-
R::from_f32(*z).unwrap()),
137-
_ => panic!("Couldnt load point from ply file")
138-
// _ => Vector3::new(R::from_f32(0.).unwrap(), R::from_f32(0.).unwrap(), R::from_f32(0.).unwrap())
139-
130+
.map(|e| {
131+
let vertex = (
132+
e.get("x").unwrap(),
133+
e.get("y").unwrap(),
134+
e.get("z").unwrap(),
135+
);
136+
137+
let v = match vertex {
138+
(Property::Float(x), Property::Float(y), Property::Float(z)) => Vector3::new(
139+
R::from_f32(*x).unwrap(),
140+
R::from_f32(*y).unwrap(),
141+
R::from_f32(*z).unwrap(),
142+
),
143+
_ => {
144+
return Err(anyhow!(
145+
"Vertex properties have wrong PLY data type (expected float)"
146+
))
147+
}
140148
};
141-
vector
142-
149+
150+
Ok(v)
143151
})
144-
.collect();
145-
146-
Ok(points)
152+
.collect::<Result<Vec<_>, anyhow::Error>>()?;
153+
154+
Ok(particles)
147155
}
148156

149157
#[allow(dead_code)]
@@ -158,4 +166,4 @@ pub fn to_binary_f32<R: Real, P: AsRef<Path>>(file: P, values: &[R]) -> Result<(
158166
}
159167

160168
Ok(())
161-
}
169+
}

0 commit comments

Comments
 (0)