Compare commits

...

3 Commits

Author SHA1 Message Date
Stephan Rehfeld
8aeb643e21 Some Macros 2026-06-17 18:50:47 +02:00
Stephan Rehfeld
4f762c7df9 Pre Macro Implementation 2026-06-17 18:24:15 +02:00
Stephan Rehfeld
1b94f8ca7a Make everything generic 2026-06-17 17:47:32 +02:00
3 changed files with 77 additions and 52 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!");
}
@ -120,4 +124,7 @@ fn main() {
);
println!("Reference counter = {}", Rc::strong_count(&tex));
let a = Vector3::new(2.0, 3.0, 1.0);
let b = 10.0 * a;
}

View File

@ -1,17 +1,23 @@
use std::ops::{Add, Div, Mul, Sub};
macro_rules! create_vector {
($name:ident [$($elem:ident),+] ) => {
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Vector3<T> {
x: T,
y: T,
z: T,
pub struct $name<T> {
$($elem: T,)*
}
impl<T> Vector3<T> {
pub fn new(x: T, y: T, z: T) -> Vector3<T> {
Vector3 { x, y, z }
impl<T> $name<T> {
pub fn new($($elem : T,)*) -> $name<T> {
$name { $($elem,)* }
}
}
};
}
create_vector! { Vector4 [x, y, z, w] }
create_vector! { Vector3 [x, y, z] }
create_vector! { Vector2 [x, y] }
impl<T> Add for Vector3<T>
where
@ -46,14 +52,20 @@ where
}
}
/*
impl<T> Mul<Vector3<T>> for f64 {
type Output = Vector3<T>;
macro_rules! impl_mul_for_vector3 {
($($type:ty)+) => {
$(
impl Mul<Vector3<$type>> for $type {
type Output = Vector3<$type>;
fn mul(self, rhs: Vector3<T>) -> Self::Output {
fn mul(self, rhs: Vector3<$type>) -> Self::Output {
Vector3::new(self * rhs.x, self * rhs.y, self * rhs.z)
}
}*/
})*
};
}
impl_mul_for_vector3! { i32 f32 f64 }
impl<T> Mul for Vector3<T>
where
@ -78,30 +90,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)
}
}