2020-03-22 00:42:37 +01:00
|
|
|
//! Various shape implementations
|
|
|
|
|
2020-03-16 15:25:21 +01:00
|
|
|
use super::{Point, Point2D, Vector};
|
2020-03-24 20:31:48 +01:00
|
|
|
use beevee::{
|
2020-03-18 15:24:09 +01:00
|
|
|
aabb::{Bounded, AABB},
|
2020-03-24 21:37:00 +01:00
|
|
|
bvh::Intersected,
|
2020-03-18 15:24:09 +01:00
|
|
|
ray::Ray,
|
|
|
|
};
|
2020-03-24 20:31:48 +01:00
|
|
|
use nalgebra::Unit;
|
2020-03-18 15:42:25 +01:00
|
|
|
use serde::Deserialize;
|
2020-03-18 15:24:09 +01:00
|
|
|
|
|
|
|
/// All the existing `Shape` implementation.
|
2020-03-18 15:42:25 +01:00
|
|
|
#[serde(tag = "type")]
|
|
|
|
#[serde(rename_all = "lowercase")]
|
2020-03-22 00:42:37 +01:00
|
|
|
#[allow(missing_docs)]
|
2020-03-18 15:24:09 +01:00
|
|
|
#[enum_dispatch::enum_dispatch]
|
2020-03-26 20:55:42 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Deserialize)]
|
2020-03-18 15:24:09 +01:00
|
|
|
pub enum ShapeEnum {
|
|
|
|
Sphere,
|
|
|
|
Triangle,
|
2020-03-23 16:33:02 +01:00
|
|
|
InterpolatedTriangle,
|
2020-03-18 15:24:09 +01:00
|
|
|
}
|
2020-03-16 15:25:21 +01:00
|
|
|
|
2020-03-26 01:03:34 +01:00
|
|
|
// FIXME: this has to be written by hand due to a limitation of `enum_dispatch` on super traits
|
|
|
|
impl Bounded for ShapeEnum {
|
2020-03-18 15:24:09 +01:00
|
|
|
fn aabb(&self) -> AABB {
|
2020-03-26 01:03:34 +01:00
|
|
|
match self {
|
|
|
|
ShapeEnum::Sphere(s) => s.aabb(),
|
|
|
|
ShapeEnum::Triangle(s) => s.aabb(),
|
|
|
|
ShapeEnum::InterpolatedTriangle(s) => s.aabb(),
|
|
|
|
}
|
2020-03-18 15:24:09 +01:00
|
|
|
}
|
2020-03-24 20:31:48 +01:00
|
|
|
|
|
|
|
fn centroid(&self) -> Point {
|
2020-03-26 01:03:34 +01:00
|
|
|
match self {
|
|
|
|
ShapeEnum::Sphere(s) => s.centroid(),
|
|
|
|
ShapeEnum::Triangle(s) => s.centroid(),
|
|
|
|
ShapeEnum::InterpolatedTriangle(s) => s.centroid(),
|
|
|
|
}
|
2020-03-24 20:31:48 +01:00
|
|
|
}
|
2020-03-16 15:25:21 +01:00
|
|
|
}
|
2020-03-16 17:26:04 +01:00
|
|
|
|
2020-03-26 01:03:34 +01:00
|
|
|
impl Intersected for ShapeEnum {
|
2020-03-24 21:37:00 +01:00
|
|
|
fn intersect(&self, ray: &Ray) -> Option<f32> {
|
2020-03-26 01:03:34 +01:00
|
|
|
match self {
|
|
|
|
ShapeEnum::Sphere(s) => s.intersect(ray),
|
|
|
|
ShapeEnum::Triangle(s) => s.intersect(ray),
|
|
|
|
ShapeEnum::InterpolatedTriangle(s) => s.intersect(ray),
|
|
|
|
}
|
2020-03-24 21:37:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 01:03:34 +01:00
|
|
|
/// Represent an abstract shape inside the scene.
|
|
|
|
#[enum_dispatch::enum_dispatch(ShapeEnum)]
|
|
|
|
pub trait Shape: std::fmt::Debug + Intersected {
|
|
|
|
/// Return the unit vector corresponding to the normal at this point of the shape.
|
|
|
|
fn normal(&self, point: &Point) -> Unit<Vector>;
|
|
|
|
/// Project the point from the shape's surface to its texel coordinates.
|
|
|
|
fn project_texel(&self, point: &Point) -> Point2D;
|
|
|
|
}
|
|
|
|
|
2020-03-23 16:33:02 +01:00
|
|
|
mod interpolated_triangle;
|
|
|
|
pub use interpolated_triangle::*;
|
|
|
|
|
2020-03-23 12:27:12 +01:00
|
|
|
mod sphere;
|
2020-03-16 17:26:04 +01:00
|
|
|
pub use sphere::*;
|
2020-03-16 19:00:25 +01:00
|
|
|
|
2020-03-23 12:27:12 +01:00
|
|
|
mod triangle;
|
2020-03-16 19:00:25 +01:00
|
|
|
pub use triangle::*;
|