From 8060262ce0c64283f0a72092fc2afda634bffea8 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 18 Mar 2020 14:46:22 +0100 Subject: [PATCH] library: shape: sphere: add deserialization --- src/shape/sphere.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/shape/sphere.rs b/src/shape/sphere.rs index 48a5c27..2eab7d0 100644 --- a/src/shape/sphere.rs +++ b/src/shape/sphere.rs @@ -2,9 +2,10 @@ use super::Shape; use crate::{Point, Point2D, Vector}; use bvh::aabb::{Bounded, AABB}; use bvh::ray::Ray; +use serde::Deserialize; /// Represent a sphere shape inside the scene. -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Deserialize)] pub struct Sphere { /// The sphere is inverted if it is expected to be seen from the inside. inverted: bool, @@ -162,4 +163,15 @@ mod test { assert!((projection.x - 1.).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)) + } }