From 2c490b7e83da412778169ff0d1623d410fcac7c7 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Sat, 28 Jan 2023 11:29:19 -0800 Subject: [PATCH] vec3: inline many methods for major performance improvement. --- rtiow/vec3/src/vec3.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/rtiow/vec3/src/vec3.rs b/rtiow/vec3/src/vec3.rs index bd1cdb5..8e5158a 100644 --- a/rtiow/vec3/src/vec3.rs +++ b/rtiow/vec3/src/vec3.rs @@ -16,6 +16,7 @@ pub struct Vec3 { } // Return a `Vec3` with the lowest of each component in v1 or v2. +#[inline] pub fn min(v1: Vec3, v2: Vec3) -> Vec3 { Vec3 { x: v1.x.min(v2.x), @@ -24,6 +25,7 @@ pub fn min(v1: Vec3, v2: Vec3) -> Vec3 { } } // Return a `Vec3` with the greatest of each component in v1 or v2. +#[inline] pub fn max(v1: Vec3, v2: Vec3) -> Vec3 { Vec3 { x: v1.x.max(v2.x), @@ -32,6 +34,7 @@ pub fn max(v1: Vec3, v2: Vec3) -> Vec3 { } } +#[inline] pub fn cross(v1: Vec3, v2: Vec3) -> Vec3 { Vec3 { x: v1.y * v2.z - v1.z * v2.y, @@ -40,6 +43,7 @@ pub fn cross(v1: Vec3, v2: Vec3) -> Vec3 { } } +#[inline] pub fn dot(v1: Vec3, v2: Vec3) -> f32 { v1.x * v2.x + v1.y * v2.y + v1.z * v2.z } @@ -49,26 +53,32 @@ impl Vec3 { Vec3 { x, y, z } } + #[inline] pub fn min(self) -> f32 { self.x.min(self.y).min(self.z) } + #[inline] pub fn max(self) -> f32 { self.x.max(self.y).max(self.z) } + #[inline] pub fn length(self) -> f32 { self.squared_length().sqrt() } + #[inline] pub fn squared_length(self) -> f32 { self.x * self.x + self.y * self.y + self.z * self.z } + #[inline] pub fn unit_vector(self) -> Vec3 { self / self.length() } + #[inline] pub fn make_unit_vector(&mut self) { *self = self.unit_vector(); } @@ -111,6 +121,7 @@ impl str::FromStr for Vec3 { impl Add for Vec3 { type Output = Vec3; + #[inline] fn add(self, r: f32) -> Vec3 { Vec3 { x: self.x + r, @@ -123,6 +134,7 @@ impl Add for Vec3 { impl Add for Vec3 { type Output = Vec3; + #[inline] fn add(self, r: Vec3) -> Vec3 { Vec3 { x: self.x + r.x, @@ -135,6 +147,7 @@ impl Add for Vec3 { impl Div for Vec3 { type Output = Vec3; + #[inline] fn div(self, r: Vec3) -> Vec3 { Vec3 { x: self.x / r.x, @@ -147,6 +160,7 @@ impl Div for Vec3 { impl Div for f32 { type Output = Vec3; + #[inline] fn div(self, r: Vec3) -> Vec3 { Vec3 { x: self / r.x, @@ -159,6 +173,7 @@ impl Div for f32 { impl Div for Vec3 { type Output = Vec3; + #[inline] fn div(self, r: f32) -> Vec3 { Vec3 { x: self.x / r, @@ -170,6 +185,7 @@ impl Div for Vec3 { impl Index for Vec3 { type Output = f32; + #[inline] fn index(&self, idx: usize) -> &f32 { match idx { 0 => &self.x, @@ -183,6 +199,7 @@ impl Index for Vec3 { impl Mul for Vec3 { type Output = Vec3; + #[inline] fn mul(self, r: Vec3) -> Vec3 { Vec3 { x: self.x * r.x, @@ -195,6 +212,7 @@ impl Mul for Vec3 { impl Mul for f32 { type Output = Vec3; + #[inline] fn mul(self, v: Vec3) -> Vec3 { Vec3 { x: v.x * self, @@ -207,6 +225,7 @@ impl Mul for f32 { impl Mul for Vec3 { type Output = Vec3; + #[inline] fn mul(self, r: f32) -> Vec3 { Vec3 { x: self.x * r, @@ -219,6 +238,7 @@ impl Mul for Vec3 { impl Neg for Vec3 { type Output = Vec3; + #[inline] fn neg(self) -> Vec3 { -1. * self } @@ -227,6 +247,7 @@ impl Neg for Vec3 { impl Sub for Vec3 { type Output = Vec3; + #[inline] fn sub(self, r: Vec3) -> Vec3 { Vec3 { x: self.x - r.x,