From d0e7b955bc93423808990c3e86214340bbb28600 Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Sun, 2 Mar 2025 14:42:16 +0700 Subject: refactor renaming global to common --- src/camera.rs | 2 +- src/common.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/global.rs | 79 ----------------------------------------------------------- src/main.rs | 4 +-- src/view.rs | 4 +-- 5 files changed, 84 insertions(+), 84 deletions(-) create mode 100644 src/common.rs delete mode 100644 src/global.rs diff --git a/src/camera.rs b/src/camera.rs index 87ba7c3..a082f34 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,5 +1,5 @@ use crate::calculus::calculus::{sample_square, Point3, Ray, Vec3}; -use crate::global::{get_image_height, Color, DisplayBuffer, Pixel, ASPECT_RATIO, CAMERA_CENTER, FOCAL_LENGTH, IMG_WIDTH, SAMPLES_PER_PIXEL, VIEWPORT_HEIGHT}; +use crate::common::{get_image_height, Color, DisplayBuffer, Pixel, ASPECT_RATIO, CAMERA_CENTER, FOCAL_LENGTH, IMG_WIDTH, SAMPLES_PER_PIXEL, VIEWPORT_HEIGHT}; use crate::interval::Interval; use crate::object::{HitRecord, Hittable, HittableList}; diff --git a/src/common.rs b/src/common.rs new file mode 100644 index 0000000..3975609 --- /dev/null +++ b/src/common.rs @@ -0,0 +1,79 @@ +use crate::calculus::calculus::Point3; +use crate::interval::Interval; + +// "Ideal" aspect ratio, allowing fraction +pub const ASPECT_RATIO: f32 = 16.0f32 / 9.0f32; +pub const IMG_WIDTH: usize = 400; +pub const IMG_HEIGHT: usize = get_image_height(IMG_WIDTH, ASPECT_RATIO); + +pub const VIEWPORT_HEIGHT: f32 = 2.0; +pub const FOCAL_LENGTH: f32 = 1.0; +pub const CAMERA_CENTER: Point3 = Point3 { x: 0f32, y: 0f32, z: 0f32 }; + +pub const SAMPLES_PER_PIXEL: usize = 100; + +pub const fn get_image_height(image_width: usize, aspect_ratio: f32) -> usize { + let image_height = (image_width as f32 / aspect_ratio) as usize; + if image_width < 1 { 1 } else { image_height } +} + +pub struct Color { + pub r: f32, + pub g: f32, + pub b: f32, +} + +impl Color { + pub fn new(r: f32, g: f32, b: f32) -> Self { + Self { r, g, b } + } + + pub fn mul_scalar(&self, scalar: f32) -> Color { + Self { + r: self.r * scalar, + g: self.g * scalar, + b: self.b * scalar, + } + } + + pub fn add(&self, other: &Self) -> Color { + Self { + r: self.r + other.r, + g: self.g + other.g, + b: self.b + other.b + } + } +} + + +#[derive(Debug, Copy, Clone)] +pub struct Pixel { + pub r: u8, + pub g: u8, + pub b: u8, +} + +impl Pixel { + pub fn from_frac(r: f32, g: f32, b: f32) -> Self { + let intensity = Interval::new(0.0, 0.999); + Self { + r: (256.0 * intensity.clamp(r)) as u8, + g: (256.0 * intensity.clamp(g)) as u8, + b: (256.0 * intensity.clamp(b)) as u8, + } + } + + pub fn from_color(color: &Color) -> Self { + let (r, g, b) = (color.r, color.g, color.b); + Self::from_frac(r, g, b) + } +} + +impl Default for Pixel { + fn default() -> Self { + Pixel { r:0, g:0, b:0 } + } +} + +pub type DisplayBuffer = [[Pixel; IMG_WIDTH]; IMG_HEIGHT]; + diff --git a/src/global.rs b/src/global.rs deleted file mode 100644 index 3975609..0000000 --- a/src/global.rs +++ /dev/null @@ -1,79 +0,0 @@ -use crate::calculus::calculus::Point3; -use crate::interval::Interval; - -// "Ideal" aspect ratio, allowing fraction -pub const ASPECT_RATIO: f32 = 16.0f32 / 9.0f32; -pub const IMG_WIDTH: usize = 400; -pub const IMG_HEIGHT: usize = get_image_height(IMG_WIDTH, ASPECT_RATIO); - -pub const VIEWPORT_HEIGHT: f32 = 2.0; -pub const FOCAL_LENGTH: f32 = 1.0; -pub const CAMERA_CENTER: Point3 = Point3 { x: 0f32, y: 0f32, z: 0f32 }; - -pub const SAMPLES_PER_PIXEL: usize = 100; - -pub const fn get_image_height(image_width: usize, aspect_ratio: f32) -> usize { - let image_height = (image_width as f32 / aspect_ratio) as usize; - if image_width < 1 { 1 } else { image_height } -} - -pub struct Color { - pub r: f32, - pub g: f32, - pub b: f32, -} - -impl Color { - pub fn new(r: f32, g: f32, b: f32) -> Self { - Self { r, g, b } - } - - pub fn mul_scalar(&self, scalar: f32) -> Color { - Self { - r: self.r * scalar, - g: self.g * scalar, - b: self.b * scalar, - } - } - - pub fn add(&self, other: &Self) -> Color { - Self { - r: self.r + other.r, - g: self.g + other.g, - b: self.b + other.b - } - } -} - - -#[derive(Debug, Copy, Clone)] -pub struct Pixel { - pub r: u8, - pub g: u8, - pub b: u8, -} - -impl Pixel { - pub fn from_frac(r: f32, g: f32, b: f32) -> Self { - let intensity = Interval::new(0.0, 0.999); - Self { - r: (256.0 * intensity.clamp(r)) as u8, - g: (256.0 * intensity.clamp(g)) as u8, - b: (256.0 * intensity.clamp(b)) as u8, - } - } - - pub fn from_color(color: &Color) -> Self { - let (r, g, b) = (color.r, color.g, color.b); - Self::from_frac(r, g, b) - } -} - -impl Default for Pixel { - fn default() -> Self { - Pixel { r:0, g:0, b:0 } - } -} - -pub type DisplayBuffer = [[Pixel; IMG_WIDTH]; IMG_HEIGHT]; - diff --git a/src/main.rs b/src/main.rs index 02efc49..73888c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,12 @@ mod view; -mod global; +mod common; mod calculus; mod camera; mod object; mod interval; use crate::calculus::calculus::Point3; -use crate::global::*; +use crate::common::*; use crate::camera::Camera; use crate::object::{HittableList, Sphere}; use crate::view::{render_viewer, View}; diff --git a/src/view.rs b/src/view.rs index 0ed5d43..896eb0a 100644 --- a/src/view.rs +++ b/src/view.rs @@ -1,6 +1,6 @@ use std::fs::File; use std::io::Write; -use crate::global::{DisplayBuffer, IMG_HEIGHT, IMG_WIDTH}; +use crate::common::{DisplayBuffer, IMG_HEIGHT, IMG_WIDTH}; pub struct View<'a> { pub display_buffer: &'a DisplayBuffer, @@ -23,7 +23,7 @@ impl<'a> View<'a> { pub mod render_viewer { use image::ImageFormat; - use crate::global::Pixel; + use crate::common::Pixel; use super::*; pub fn ppm_exporter(data: &DisplayBuffer) -> Result { -- cgit v1.2.3-70-g09d2