diff --git a/rtchallenge/src/shapes.rs b/rtchallenge/src/shapes.rs index 12be159..aa330d3 100644 --- a/rtchallenge/src/shapes.rs +++ b/rtchallenge/src/shapes.rs @@ -44,7 +44,7 @@ impl PartialEq for Geometry { /// many different shapes based on the value of it's geometry field. Users chose the shape by /// calling the appropriate constructor, i.e. [Shape::sphere]. #[derive(Builder, Debug, Clone, PartialEq)] -#[builder(default)] +#[builder(default, pattern = "owned")] pub struct Shape { transform: Matrix4x4, #[builder(private, default = "self.default_inverse_transform()?")] @@ -53,41 +53,93 @@ pub struct Shape { geometry: Geometry, } -/// Short hand for creating a Shape with a plane geometry. +/// Short hand for creating a ShapeBuilder with a plane geometry. /// /// # Examples /// ``` /// use rtchallenge::shapes::{plane, Shape}; -/// -/// assert_eq!(plane(), Shape::plane()); +/// # fn main() -> Result<(), Box> { +/// assert_eq!(plane().build()?, Shape::plane()); +/// # Ok(()) +/// # } /// ``` -pub fn plane() -> Shape { - Shape::plane() +pub fn plane() -> ShapeBuilder { + ShapeBuilder::plane() } -/// Short hand for creating a Shape with a sphere geometry. +/// Short hand for creating a ShapeBuilder with a sphere geometry. /// /// # Examples /// ``` /// use rtchallenge::shapes::{sphere, Shape}; /// -/// assert_eq!(sphere(), Shape::sphere()); +/// # fn main() -> Result<(), Box> { +/// assert_eq!(sphere().build()?, Shape::sphere()); +/// # Ok(()) +/// # } /// ``` -pub fn sphere() -> Shape { - Shape::sphere() +pub fn sphere() -> ShapeBuilder { + ShapeBuilder::sphere() } -/// Short hand for creating a Shape with a test shape geometry. +/// Short hand for creating a ShapeBuilder with a test shape geometry. /// /// # Examples /// ``` /// use rtchallenge::shapes::{test_shape, Shape}; /// -/// assert_eq!(test_shape(), Shape::test_shape()); +/// # fn main() -> Result<(), Box> { +/// assert_eq!(test_shape().build()?, Shape::test_shape()); +/// # Ok(()) +/// # } /// ``` -pub fn test_shape() -> Shape { - Shape::test_shape() +pub fn test_shape() -> ShapeBuilder { + ShapeBuilder::test_shape() } impl ShapeBuilder { + /// Short hand for creating a ShapeBuilder with a plane geometry. + /// + /// # Examples + /// ``` + /// use rtchallenge::shapes::{plane, Shape}; + /// + /// # fn main() -> Result<(), Box> { + /// assert_eq!(plane().build()?, Shape::plane()); + /// # Ok(()) + /// # } + /// ``` + pub fn plane() -> ShapeBuilder { + ShapeBuilder::default().geometry(Geometry::Plane) + } + /// Short hand for creating a ShapeBuilder with a sphere geometry. + /// + /// # Examples + /// ``` + /// use rtchallenge::shapes::{sphere, Shape}; + /// + /// # fn main() -> Result<(), Box> { + /// assert_eq!(sphere().build()?, Shape::sphere()); + /// # Ok(()) + /// # } + /// ``` + pub fn sphere() -> ShapeBuilder { + ShapeBuilder::default().geometry(Geometry::Sphere) + } + /// Short hand for creating a ShapeBuilder with a test shape geometry. + /// + /// # Examples + /// ``` + /// use rtchallenge::shapes::{test_shape, Shape}; + /// + /// # fn main() -> Result<(), Box> { + /// assert_eq!(test_shape().build()?, Shape::test_shape()); + /// # Ok(()) + /// # } + /// ``` + pub fn test_shape() -> ShapeBuilder { + ShapeBuilder::default().geometry(Geometry::TestShape(Arc::new(Mutex::new( + TestData::default(), + )))) + } fn default_inverse_transform(&self) -> Result { Ok(self.transform.unwrap_or(Matrix4x4::identity()).inverse()) }