diff options
author | Julian T <julian@jtle.dk> | 2021-02-02 20:39:32 +0100 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2021-02-03 14:30:21 +0100 |
commit | 32e719a517a6fea113f3e66e350c9aa60ddd98b9 (patch) | |
tree | 3db7367b59a981dd51135122254d72dc9299eb74 /src/core | |
parent | df6a266e5823a7fc4cca3060ec86d35f2125cd0d (diff) |
Added documentation for many of the modules
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/bound.rs | 33 | ||||
-rw-r--r-- | src/core/mod.rs | 4 | ||||
-rw-r--r-- | src/core/ray.rs | 1 | ||||
-rw-r--r-- | src/core/spectrum.rs | 3 | ||||
-rw-r--r-- | src/core/vector2.rs | 3 | ||||
-rw-r--r-- | src/core/vector3.rs | 16 |
6 files changed, 56 insertions, 4 deletions
diff --git a/src/core/bound.rs b/src/core/bound.rs index ea9d990..635218a 100644 --- a/src/core/bound.rs +++ b/src/core/bound.rs @@ -1,7 +1,9 @@ +//! Implements a 2d region use crate::{Number, Float}; use super::vector2::Vector2; use crate::core; +/// Implements a region between min and max #[derive(Clone)] pub struct Bound2<T: Number> { pub min: Vector2<T>, @@ -26,6 +28,9 @@ fn max<T: Number> (a: T, b: T) -> T { } impl<T: Number> Bound2<T> { + /// Creates a new bound from two points + /// + /// p0 does not have to be smaller than p1 pub fn new(p0: &Vector2<T>, p1: &Vector2<T>) -> Self { let min = Vector2::new_xy(min(p0.x, p1.x), min(p0.y, p1.y)); let max = Vector2::new_xy(max(p0.x, p1.x), max(p0.y, p1.y)); @@ -40,18 +45,37 @@ impl<T: Number> Bound2<T> { ) } + /// Calculates the diagonal vector + /// + /// Can be used to calculate the size of the bound + /// + /// # Examples + /// + /// ``` + /// use pathtrace::core::Bound2i; + /// let b = Bound2i::new_xyxy(2, 2, 6, 7); + /// let diag = b.diagonal(); + /// + /// assert!(diag.x == 4 && diag.y == 5); + /// ``` pub fn diagonal(&self) -> Vector2<T> { self.max - self.min } + /// Calculates the area of of the bounded region + /// + /// # Examples + /// + /// ``` + /// use pathtrace::core::Bound2i; + /// let b = Bound2i::new_xyxy(10, 10, 20, 20); + /// + /// assert!(b.area() == 100); + /// ``` pub fn area(&self) -> T { let diag = self.diagonal(); return diag.x * diag.y; } - - pub fn width(&self) -> T { - self.diagonal().x - } } impl From<&Bound2i> for Bound2f { @@ -72,6 +96,7 @@ impl From<&Bound2f> for Bound2i { } } +/// Finds the intersected area between two bounds pub fn intersect<T: Number>(a: &Bound2<T>, b: &Bound2<T>) -> Bound2<T> { Bound2::new( &Vector2::new_xy(max(a.min.x, b.min.x), max(a.min.y, b.min.y)), diff --git a/src/core/mod.rs b/src/core/mod.rs index 938a16e..5723b2d 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,3 +1,7 @@ +//! Contains a collection of core modules used by other modules +//! +//! Also creates a shortcut for some common types + pub mod vector2; pub mod vector3; pub mod bound; diff --git a/src/core/ray.rs b/src/core/ray.rs index 54af639..2368315 100644 --- a/src/core/ray.rs +++ b/src/core/ray.rs @@ -1,3 +1,4 @@ +//! The ray class used when probing the 3d scene use crate::core::Vector3f; pub struct Ray { diff --git a/src/core/spectrum.rs b/src/core/spectrum.rs index 604c8c0..c72a251 100644 --- a/src/core/spectrum.rs +++ b/src/core/spectrum.rs @@ -1,3 +1,6 @@ +//! Used to represent color +//! +//! Currently only implements RGB colors use crate::Float; use std::ops; diff --git a/src/core/vector2.rs b/src/core/vector2.rs index ac70947..5afa0f2 100644 --- a/src/core/vector2.rs +++ b/src/core/vector2.rs @@ -1,3 +1,6 @@ +//! Implements 2d vectors +//! +//! This is implemented generictly with types that fit in the Number trait use crate::{Float, Number}; use std::ops::{Sub, Add}; diff --git a/src/core/vector3.rs b/src/core/vector3.rs index e3aa9a6..915765d 100644 --- a/src/core/vector3.rs +++ b/src/core/vector3.rs @@ -1,3 +1,6 @@ +//! Implements 3d vectors +//! +//! Also add more 3d math things needed for shading and 3d calculations. use crate::{Float, Number}; use std::ops::{Sub, Add, DivAssign}; @@ -55,6 +58,9 @@ impl<T: Number> DivAssign<T> for Vector3<T> { } impl Vector3f { + /// Calculates the length times itself + /// + /// This is faster than using len * len as the square is ommited pub fn len_squared(&self) -> Float { self.x * self.x + self.y * self.y + self.z * self.z } @@ -67,6 +73,16 @@ impl Vector3f { self.x * op.x + self.y * op.y + self.z * op.z } + /// Inplace normal instead of creating a new vector + /// + /// # Example + /// + /// ``` + /// use pathtrace::core::Vector3f; + /// let mut v = Vector3f::new_xyz(10.0, 0.0, 0.0); + /// v.norm_in(); + /// assert!(v.x == 1.0); + /// ``` pub fn norm_in(&mut self) { let len = self.len(); if len == 0.0 { |