Compare commits

...
45 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
Stephan Rehfeld 0a692c01de You can also make a whole module public, to allow access 2026-05-13 18:03:19 +02:00
Stephan Rehfeld 41eb9a9dc0 You can provide internals of a module via pub use 2026-05-13 18:02:48 +02:00
Stephan Rehfeld 5b4c205234 Some cleanup 2026-05-13 17:55:28 +02:00
Stephan Rehfeld 09ff36dff8 Put some stuff into lib.rs 2026-05-13 17:49:59 +02:00
Stephan Rehfeld 45cad795a7 Use enums 2026-04-29 18:40:36 +02:00
Stephan Rehfeld 5daaf9e1bc Iterating over collection using match 2026-04-29 18:31:53 +02:00
Stephan Rehfeld bb45906753 Important Rust Pattern: Inheritance does not exist, use enum to store multiple different types in same Collection 2026-04-29 18:28:57 +02:00
Stephan Rehfeld b7a1f52360 Putting some Sphere into a Vec 2026-04-29 18:19:56 +02:00
Stephan Rehfeld b1963c7e6a Adding some Geometries 2026-04-29 18:14:00 +02:00
Stephan Rehfeld 9ef8e08936 Impl Trait add for Point ad Vector3 2026-04-29 18:08:30 +02:00
Stephan Rehfeld 31e224f9b0 Implementing Div Trait 2026-04-29 18:02:48 +02:00
Stephan Rehfeld 07b2fc28d8 Implemting multiplication of vector3 with scalar 2026-04-29 17:53:40 +02:00
Stephan Rehfeld 31c9bda5a2 Implementing Sub for our Vector 2026-04-29 17:49:48 +02:00
Stephan Rehfeld 794228dfce Implementing Add for our Vector3 2026-04-29 17:47:42 +02:00
Stephan Rehfeld 28df45da67 Minimal example to show implementation of traits 2026-04-29 17:38:52 +02:00
Stephan Rehfeld 81205e5b29 Example with ownership 2026-04-29 17:35:33 +02:00
Stephan Rehfeld 07d7855f47 CODE DOES NOT Compile
You can't trick the borrow checker, by putting references into another struct
2026-04-22 18:36:00 +02:00
Stephan Rehfeld 352085f8d9 We need lifitime annotation, when struct wants to old a reference. 2026-04-22 18:25:24 +02:00
Stephan Rehfeld b0e998fb28 Either on mutable borrow or inifite immutable borrows are allowed 2026-04-22 18:12:55 +02:00
Stephan Rehfeld 7a1b0975d2 Example with references and mutable references 2026-04-22 17:59:39 +02:00
Stephan Rehfeld 12a54c42da Image example to show Ownership 2026-04-15 18:58:16 +02:00
Stephan Rehfeld bd260f1769 Drop and move behavior 2026-04-15 18:04:15 +02:00
Stephan Rehfeld 823ec0ad00 Move is the default in Rust 2026-04-08 19:03:40 +02:00
Stephan Rehfeld 019fbcf095 use unwrap_or to get default value 2026-04-08 18:46:40 +02:00
Stephan Rehfeld 20c675ef91 You can use unwrap to get value. Panics, if result is Err 2026-04-08 18:43:12 +02:00
Stephan Rehfeld 6e0e031b12 Optional and Result for optional parameters and functions that may fail. 2026-04-08 18:41:31 +02:00
Stephan Rehfeld d5427c96ca Enum values can contain futher values. Use pattern matching to access. 2026-04-08 18:19:44 +02:00
Stephan Rehfeld 092e5693b7 Simple enum with match 2026-04-08 18:16:15 +02:00
5 changed files with 372 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!");
}
}
+2
View File
@@ -0,0 +1,2 @@
pub mod geometry;
pub mod math;
+127 -2
View File
@@ -1,5 +1,130 @@
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() {
for value in (-10..10).rev() {
println!("Value: {}", value);
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() {}