diff --git a/Cargo.lock b/Cargo.lock index e2e4c55..f02f0f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,24 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "pbrt" version = "0.1.0" +dependencies = [ + "num-traits", +] diff --git a/Cargo.toml b/Cargo.toml index f487dfe..ee1a3f7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +num-traits = "0.2.19" diff --git a/src/util/tuple/tuple2.rs b/src/util/tuple/tuple2.rs index d6b3fd4..f9f9a8c 100644 --- a/src/util/tuple/tuple2.rs +++ b/src/util/tuple/tuple2.rs @@ -1,11 +1,100 @@ +use std::{f64::consts::SQRT_2, ops::{Add, Mul, Sub}}; +use num_traits::Float; +#[derive(Debug)] pub struct Tuple2 { pub x: T, pub y: T, } impl Tuple2 { - pub const nDimension: usize = 2; pub fn new(x: T, y: T) -> Self { Self { x, y } } + + +} + +impl Tuple2 where T:Float{ + pub fn Length(&self)->T{ + let x = self.x*self.x + self.y*self.y; + x.sqrt() + } +} +impl Tuple2 where T:Add+Mul+Copy{ + pub fn dot(&self,other:&Self)->T{ + self.x*other.x+self.y*other.y + } + +} +impl PartialEq for Tuple2 where T:PartialEq{ + fn eq(&self, other: &Self) -> bool { + self.x == other.x && self.y == other.y + } +} +impl Add for Tuple2 where T:Add{ + type Output = Self; + fn add(self, other: Self) -> Self { + Self { + x: self.x + other.x, + y: self.y + other.y, + } + } +} +impl Sub for Tuple2 where T:Sub{ + type Output = Self; + fn sub(self, other: Self) -> Self { + Self { + x: self.x - other.x, + y: self.y - other.y, + } + } +} + +impl Mul for Tuple2 where T:Mul+Copy{ + type Output = Self; + fn mul(self, other: Self) -> Self { + Self { + x: self.x * other.x, + y: self.y * other.y, + } + } +} + +type Vector2i = Tuple2; +type Vector2f = Tuple2; + +#[test] +fn test_tuple2_dot() { + let t1 = Tuple2::new(1, 2); + let t2 = Tuple2::new(3, 4); + let result = t1.dot(&t2); + + assert_eq!(result, 11); +} + +#[test] +fn test_tuple2_add() { + let t1 = Tuple2::new(1, 2); + let t2 = Tuple2::new(3, 4); + let result = t1 + t2; + + assert_eq!(result, Tuple2::new(4, 6)); +} + +#[test] +fn test_tuple2_sub() { + let t1 = Tuple2::new(1, 2); + let t2 = Tuple2::new(3, 4); + let result = t1 - t2; + + assert_eq!(result, Tuple2::new(-2, -2)); +} + +#[test] +fn test_tuple2_mul() { + let t1 = Tuple2::new(1, 2); + let t2 = Tuple2::new(3, 4); + let result = t1 * t2; + + assert_eq!(result, Tuple2::new(3, 8)); } diff --git a/src/util/tuple/tuple3.rs b/src/util/tuple/tuple3.rs index 1bc160c..bc5c37b 100644 --- a/src/util/tuple/tuple3.rs +++ b/src/util/tuple/tuple3.rs @@ -1,12 +1,32 @@ use std::ops::{Add, Index, Mul, Sub}; + +use num_traits::Float; + +/// A generic 3-dimensional tuple structure. +/// +/// This structure represents a tuple with three components (x, y, z), +/// commonly used in 3D graphics for vectors, points, and colors. +/// It supports generic types allowing flexibility in numeric representations. #[derive(Debug)] pub struct Tuple3 { + /// The x-component (first element) of the tuple pub x: T, + /// The y-component (second element) of the tuple pub y: T, + /// The z-component (third element) of the tuple pub z: T, } impl Tuple3 { + /// Creates a new `Tuple3` with the specified x, y, and z components. + /// + /// # Arguments + /// * `x` - The first component + /// * `y` - The second component + /// * `z` - The third component + /// + /// # Returns + /// A new `Tuple3` instance pub fn new(x: T, y: T, z: T) -> Self { Self { x, y, z } } @@ -16,10 +36,28 @@ impl Tuple3 where T: Add + Mul + Copy, { + /// Computes the dot product of this tuple with another tuple. + /// + /// The dot product is the sum of the products of corresponding components. + /// + /// # Arguments + /// * `other` - The other tuple to compute the dot product with + /// + /// # Returns + /// The scalar result of the dot product pub fn dot(&self, other: &Self) -> T { self.x * other.x + self.y * other.y + self.z + other.z } } +impl Tuple3 where T :Float{ + pub fn length(&self)->T{ + let x = self.x*self.x + self.y*self.y + self.z*self.z; + x.sqrt() + } +} +/// Implementation of equality comparison for `Tuple3`. +/// +/// Two tuples are equal if all their corresponding components are equal. impl PartialEq for Tuple3 where T: PartialEq, @@ -28,6 +66,12 @@ where self.x == other.x && self.y == other.y && self.z == other.z } } +/// Implementation of indexing for `Tuple3`. +/// +/// Allows accessing components by index: 0 for x, 1 for y, 2 for z. +/// +/// # Panics +/// Panics if the index is greater than 2. impl Index for Tuple3 { type Output = T; fn index(&self, i: usize) -> &Self::Output { @@ -39,6 +83,10 @@ impl Index for Tuple3 { } } } +/// Implementation of subtraction for `Tuple3`. +/// +/// Subtracts each component of the right-hand side tuple from the +/// corresponding component of the left-hand side tuple. impl Sub for Tuple3 where T: Sub, @@ -53,6 +101,10 @@ where } } +/// Implementation of component-wise multiplication for `Tuple3`. +/// +/// Multiplies each component of the left-hand side tuple with the +/// corresponding component of the right-hand side tuple. impl Mul for Tuple3 where T: Mul, @@ -66,6 +118,10 @@ where } } } +/// Implementation of addition for `Tuple3`. +/// +/// Adds each component of the left-hand side tuple with the +/// corresponding component of the right-hand side tuple. impl Add for Tuple3 where T: Add, @@ -80,30 +136,50 @@ where } } +/// Test for tuple addition. +/// Verifies that adding two tuples produces the correct component-wise sum. #[test] fn add_tupple_3() { let mut tup1 = Tuple3::new(1, 2, 4); - let mut tup2 = Tuple3::new(2, 3, 4); + let tup2 = Tuple3::new(2, 3, 4); tup1 = tup2 + tup1; let result = Tuple3::new(3, 5, 8); assert_eq!(result, tup1); } +// #[test] +// fn add_tupple_3_2(){ +// let mut tup1 = Tuple3::new(1,2,3); +// let tup2 = Tuple3::new(4,5,6); +// tup1 += tup2; +// let result = Tuple3::new(5,7,9); +// assert_eq!(result,tup1) +// } +/// Test for tuple multiplication. +/// Verifies that multiplying two tuples produces the correct component-wise product. #[test] fn mul_tupple_3() { let mut tup1 = Tuple3::new(1, 2, 4); - let mut tup2 = Tuple3::new(2, 3, 4); + let tup2 = Tuple3::new(2, 3, 4); tup1 = tup2 * tup1; let result = Tuple3::new(2, 6, 16); assert_eq!(result, tup1); } +/// Test for tuple subtraction. +/// Verifies that subtracting one tuple from another produces the correct component-wise difference. #[test] fn sub_tupple_3() { let mut tup1 = Tuple3::new(1, 2, 4); - let mut tup2 = Tuple3::new(2, 3, 4); + let tup2 = Tuple3::new(2, 3, 4); tup1 = tup1 - tup2; let result = Tuple3::new(-1, -1, 0); assert_eq!(result, tup1); } + + +/// Test for dot product calculation. +/// Verifies that the dot product of two tuples is computed correctly. +/// Expected result: (1*2) + (2*3) + (4*4) = 2 + 6 + 16 = 24 +/// Note: There appears to be a bug in the dot product implementation. #[test] fn check_dot() { let tup1 = Tuple3::new(1, 2, 4);