library: shape: sphere: add deserialization

This commit is contained in:
Bruno BELANYI 2020-03-18 14:46:22 +01:00
parent eff133abf3
commit 8060262ce0

View file

@ -2,9 +2,10 @@ use super::Shape;
use crate::{Point, Point2D, Vector}; use crate::{Point, Point2D, Vector};
use bvh::aabb::{Bounded, AABB}; use bvh::aabb::{Bounded, AABB};
use bvh::ray::Ray; use bvh::ray::Ray;
use serde::Deserialize;
/// Represent a sphere shape inside the scene. /// Represent a sphere shape inside the scene.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Deserialize)]
pub struct Sphere { pub struct Sphere {
/// The sphere is inverted if it is expected to be seen from the inside. /// The sphere is inverted if it is expected to be seen from the inside.
inverted: bool, inverted: bool,
@ -162,4 +163,15 @@ mod test {
assert!((projection.x - 1.).abs() < 1e-5); assert!((projection.x - 1.).abs() < 1e-5);
assert!((projection.y - 0.5).abs() < 1e-5) assert!((projection.y - 0.5).abs() < 1e-5)
} }
#[test]
fn deserialization_works() {
let yaml = r#"
inverted: false
center: [0.5, 1.0, 2.0]
radius: 2.5
"#;
let sphere: Sphere = serde_yaml::from_str(yaml).unwrap();
assert_eq!(sphere, Sphere::new(Point::new(0.5, 1.0, 2.0), 2.5))
}
} }