Compare commits

..
17 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
Stephan Rehfeld 3eca1b1f7b Some changes 2026-06-17 17:34:29 +02:00
Stephan Rehfeld 5a23f49eae Making datatype for color channels of color type and texture generic 2026-05-27 18:47:57 +02:00
Stephan Rehfeld 3ea1a7c5aa Polymorpism by dyn Trait and Boxes 2026-05-27 18:41:29 +02:00
Stephan Rehfeld 65bb4d4897 draw() is a nicer name 2026-05-27 18:29:20 +02:00
Stephan Rehfeld 36c16d053e We can create Polymorphism using enums 2026-05-27 18:27:00 +02:00
Stephan Rehfeld 50eabecfe9 Using Rc reference counter smart pointer, to have shared resources 2026-05-27 18:20:19 +02:00
Stephan Rehfeld 585af00665 Texture needs to be copied all the Time. 2026-05-27 18:15:04 +02:00
Stephan Rehfeld 5065596f96 Use Box<T> to break cycle 2026-05-27 18:00:05 +02:00
Stephan Rehfeld 8cd294c44a Boxes behave similar to references. Auto deference in many situations, but sometimes a * is required. 2026-05-27 17:58:08 +02:00
Stephan Rehfeld 58a9c36d0d We cant build recursive enum type, as the size of that time would be infitite 2026-05-27 17:50:24 +02:00
Stephan Rehfeld c4a794c4f2 Example for seperate tests in tests directory 2026-05-13 18:38:22 +02:00
Stephan Rehfeld 2649558ecd You can also test if a code should panic 2026-05-13 18:26:41 +02:00
Stephan Rehfeld 6adb2e143f Implement some tests 2026-05-13 18:23:12 +02:00
Stephan Rehfeld 29f39a9163 You can use implemented traits 2026-05-13 18:04:38 +02:00
5 changed files with 370 additions and 3 deletions
+72
View File
@@ -0,0 +1,72 @@
use crate::math::{Point3, Vector3};
pub struct Plane<T> {
point: Point3<T>,
normal: Vector3<T>,
}
impl<T> Plane<T> {
pub fn new(point: Point3<T>, normal: Vector3<T>) -> Plane<T> {
Plane { point, normal }
}
}
pub struct Line<T> {
point: Point3<T>,
direction: Vector3<T>,
}
pub struct Sphere<T> {
point: Point3<T>,
radius: T,
}
impl<T> Sphere<T> {
pub fn new(point: Point3<T>, radius: T) -> Sphere<T> {
Sphere { point, radius }
}
}
pub enum Geometry<T> {
Sphere(Sphere<T>),
Plane(Plane<T>),
Line(Line<T>),
}
impl<T> Geometry<T> {
pub fn draw(&self) {
match self {
Geometry::Sphere(_) => {
println!("Drawing Sphere!");
}
Geometry::Line(_) => {
println!("Drawing Line!");
}
Geometry::Plane(_) => {
println!("Drawing Plane!");
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_plane() {
let p = Point3::new(1.0, 2.0, 3.0);
let n = Vector3::new(4.0, 5.0, 6.0);
let plane = Plane::new(p, n);
assert_eq!(plane.point, p);
assert_eq!(plane.normal, n);
}
#[test]
#[should_panic]
fn foo() {
panic!("This code panics!");
}
}
+1 -1
View File
@@ -1,2 +1,2 @@
mod geometry;
pub mod geometry;
pub mod math;
+127 -2
View File
@@ -1,5 +1,130 @@
use hello::math::Point3;
use hello::geometry::{Geometry, Line, Plane, Sphere};
use hello::math::{Mat4x4, Point3, Vector3};
use std::rc::Rc;
#[derive(Clone)]
struct RGB<T> {
red: T,
green: T,
blue: T,
}
#[derive(Clone)]
struct Texture<T> {
width: usize,
height: usize,
pixel: Vec<RGB<T>>,
}
impl Texture<f64> {
fn load_texture() -> Texture<f64> {
let width = 640;
let height = 480;
let mut pixel = Vec::new();
for _ in 0..width {
for _ in 0..height {
pixel.push(RGB {
red: 1.0,
green: 1.0,
blue: 1.0,
});
}
}
Texture {
width,
height,
pixel,
}
}
}
struct RenderableGeometry<T> {
transform: Mat4x4,
geometry: Geometry<T>,
texture: Rc<Texture<f64>>,
}
impl<T> RenderableGeometry<T> {
fn new(
transform: Mat4x4,
geometry: Geometry<T>,
texture: Rc<Texture<f64>>,
) -> RenderableGeometry<T> {
RenderableGeometry {
transform,
geometry,
texture,
}
}
}
trait Renderable {
fn render(&self);
}
impl<T> Renderable for Sphere<T> {
fn render(&self) {
println!("Rendering Sphere!");
}
}
impl<T> Renderable for Plane<T> {
fn render(&self) {
println!("Rendering Plane!");
}
}
impl<T> Renderable for Line<T> {
fn render(&self) {
println!("Rendering Line!");
}
}
fn main() {
let p = Point3::new(0.0, 0.0, 0.0);
let tex = Rc::new(Texture::load_texture());
let sphere = Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0));
let geometries = vec![sphere];
for g in geometries {
g.draw();
}
let mut renderables: Vec<Box<dyn Renderable>> = Vec::new();
renderables.push(Box::new(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0)));
renderables.push(Box::new(Plane::new(
Point3::new(0.0, 0.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
)));
for r in renderables {
r.render();
}
let geo1 = RenderableGeometry::new(
Mat4x4::ident(),
Geometry::Sphere(Sphere::new(Point3::new(0.0, 0.0, 0.0), 1.0)),
tex.clone(),
);
let geo2 = RenderableGeometry::new(
Mat4x4::ident(),
Geometry::Sphere(Sphere::new(Point3::new(-2.0, 0.0, 0.0), 1.0)),
tex.clone(),
);
let geo3 = RenderableGeometry::new(
Mat4x4::ident(),
Geometry::Sphere(Sphere::new(Point3::new(2.0, 0.0, 0.0), 1.0)),
tex.clone(),
);
println!("Reference counter = {}", Rc::strong_count(&tex));
let a = Vector3::new(2.0, 3.0, 1.0);
let b = 10.0 * a;
}
+168
View File
@@ -0,0 +1,168 @@
use std::ops::{Add, Div, Mul, Sub};
macro_rules! create_vector {
($name:ident [$($elem:ident),+] ) => {
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct $name<T> {
$($elem: T,)*
}
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
T: Add<Output = T>,
{
type Output = Vector3<T>;
fn add(self, rhs: Vector3<T>) -> Self::Output {
Vector3::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
}
}
impl<T> Sub for Vector3<T>
where
T: Sub<Output = T>,
{
type Output = Vector3<T>;
fn sub(self, rhs: Vector3<T>) -> Self::Output {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
impl<T> Mul<T> for Vector3<T>
where
T: Mul<Output = T> + Clone + Copy,
{
type Output = Vector3<T>;
fn mul(self, rhs: T) -> Self::Output {
Vector3::new(self.x * rhs, self.y * rhs, self.z * rhs)
}
}
macro_rules! impl_mul_for_vector3 {
($($type:ty)+) => {
$(
impl Mul<Vector3<$type>> for $type {
type Output = Vector3<$type>;
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
T: Mul<Output = T> + Add<Output = T>,
{
type Output = T;
fn mul(self, rhs: Vector3<T>) -> Self::Output {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
}
}
impl<T> Div<T> for Vector3<T>
where
T: Div<Output = T> + Copy + Clone,
{
type Output = Vector3<T>;
fn div(self, rhs: T) -> Self::Output {
Vector3::new(self.x / rhs, self.y / rhs, self.z / rhs)
}
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Point3<T> {
x: T,
y: T,
z: T,
}
impl<T> Point3<T> {
pub fn new(x: T, y: T, z: T) -> Point3<T> {
Point3 { x, y, z }
}
}
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<T> Sub for Point3<T>
where
T: Sub<Output = T>,
{
type Output = Vector3<T>;
fn sub(self, rhs: Point3<T>) -> Self::Output {
Vector3::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
}
}
pub struct Mat4x4 {
v: f64,
}
impl Mat4x4 {
pub fn ident() -> Mat4x4 {
Mat4x4 { v: 0.0 }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_vector3() {
let v = Vector3::new(1.0, 2.0, 3.0);
assert_eq!(v.x, 1.0);
assert_eq!(v.y, 2.0);
assert_eq!(v.z, 3.0);
}
#[test]
fn add_vector3_to_vector3() {
let v1 = Vector3::new(1.0, 2.0, 3.0);
let v2 = Vector3::new(4.0, 5.0, 6.0);
let r = v1 + v2;
assert_eq!(r.x, 5.0);
assert_eq!(r.y, 7.0);
assert_eq!(r.z, 9.0);
}
#[test]
fn new_point3() {
let p = Point3::new(1.0, 2.0, 3.0);
assert_eq!(p.x, 1.0);
assert_eq!(p.y, 2.0);
assert_eq!(p.z, 3.0);
}
}
+2
View File
@@ -0,0 +1,2 @@
#[test]
fn very_long_running_test() {}