Make everything generic

This commit is contained in:
Stephan Rehfeld 2026-06-17 17:47:32 +02:00
parent 3eca1b1f7b
commit 1b94f8ca7a
3 changed files with 45 additions and 35 deletions

View File

@ -1,39 +1,39 @@
use crate::math::{Point3, Vector3};
pub struct Plane {
point: Point3,
normal: Vector3,
pub struct Plane<T> {
point: Point3<T>,
normal: Vector3<T>,
}
impl Plane {
pub fn new(point: Point3, normal: Vector3) -> Plane {
impl<T> Plane<T> {
pub fn new(point: Point3<T>, normal: Vector3<T>) -> Plane<T> {
Plane { point, normal }
}
}
pub struct Line {
point: Point3,
direction: Vector3,
pub struct Line<T> {
point: Point3<T>,
direction: Vector3<T>,
}
pub struct Sphere {
point: Point3,
radius: f64,
pub struct Sphere<T> {
point: Point3<T>,
radius: T,
}
impl Sphere {
pub fn new(point: Point3, radius: f64) -> Sphere {
impl<T> Sphere<T> {
pub fn new(point: Point3<T>, radius: T) -> Sphere<T> {
Sphere { point, radius }
}
}
pub enum Geometry {
Sphere(Sphere),
Plane(Plane),
Line(Line),
pub enum Geometry<T> {
Sphere(Sphere<T>),
Plane(Plane<T>),
Line(Line<T>),
}
impl Geometry {
impl<T> Geometry<T> {
pub fn draw(&self) {
match self {
Geometry::Sphere(_) => {

View File

@ -42,14 +42,18 @@ impl Texture<f64> {
}
}
struct RenderableGeometry {
struct RenderableGeometry<T> {
transform: Mat4x4,
geometry: Geometry,
geometry: Geometry<T>,
texture: Rc<Texture<f64>>,
}
impl RenderableGeometry {
fn new(transform: Mat4x4, geometry: Geometry, texture: Rc<Texture<f64>>) -> RenderableGeometry {
impl<T> RenderableGeometry<T> {
fn new(
transform: Mat4x4,
geometry: Geometry<T>,
texture: Rc<Texture<f64>>,
) -> RenderableGeometry<T> {
RenderableGeometry {
transform,
geometry,
@ -62,19 +66,19 @@ trait Renderable {
fn render(&self);
}
impl Renderable for Sphere {
impl<T> Renderable for Sphere<T> {
fn render(&self) {
println!("Rendering Sphere!");
}
}
impl Renderable for Plane {
impl<T> Renderable for Plane<T> {
fn render(&self) {
println!("Rendering Plane!");
}
}
impl Renderable for Line {
impl<T> Renderable for Line<T> {
fn render(&self) {
println!("Rendering Line!");
}

View File

@ -78,30 +78,36 @@ where
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Point3 {
x: f64,
y: f64,
z: f64,
pub struct Point3<T> {
x: T,
y: T,
z: T,
}
impl Point3 {
pub fn new(x: f64, y: f64, z: f64) -> Point3 {
impl<T> Point3<T> {
pub fn new(x: T, y: T, z: T) -> Point3<T> {
Point3 { x, y, z }
}
}
impl Add<Vector3<T>> for Point3 {
type Output = Point3;
impl<T> Add<Vector3<T>> for Point3<T>
where
T: Add<Output = T>,
{
type Output = Point3<T>;
fn add(self, rhs: Vector3<T>) -> Self::Output {
Point3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
impl Sub for Point3 {
impl<T> Sub for Point3<T>
where
T: Sub<Output = T>,
{
type Output = Vector3<T>;
fn sub(self, rhs: Point3) -> Self::Output {
fn sub(self, rhs: Point3<T>) -> Self::Output {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}