From 0c490c00056cdfb398cc0565e3be62e92bcfeed3 Mon Sep 17 00:00:00 2001 From: bendn Date: Wed, 1 Nov 2023 18:57:42 +0700 Subject: [PATCH] make tri() take ff32's --- benches/drawing.rs | 10 +++++++++- src/drawing/tri.rs | 42 ++++++++++++++++++++++++------------------ 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/benches/drawing.rs b/benches/drawing.rs index 61bf4d9..fede930 100644 --- a/benches/drawing.rs +++ b/benches/drawing.rs @@ -1,7 +1,15 @@ use fimg::*; +use umath::{generic_float::Constructors, FF32}; fn tri() { let mut i: Image<_, 4> = fimg::make!(4 channels 1000 x 1000).boxed(); - i.tri((0., 0.), (1000., 500.), (0., 999.), [255, 255, 255, 255]); + unsafe { + i.tri::( + (FF32::zero(), FF32::zero()), + (FF32::new(1000.), FF32::new(500.)), + (FF32::zero(), FF32::new(999.)), + [255, 255, 255, 255], + ) + }; iai::black_box(i); } fn line() { diff --git a/src/drawing/tri.rs b/src/drawing/tri.rs index 5fb90bb..538d5ed 100644 --- a/src/drawing/tri.rs +++ b/src/drawing/tri.rs @@ -1,7 +1,7 @@ //! trongle drawing -use vecto::Vec2; +use umath::Float; +use vecto::Vector2; -use crate::math::madd; use crate::Image; use std::cmp::{max, min}; @@ -11,7 +11,7 @@ impl + AsRef<[u8]>, const CHANNELS: usize> Image { /// # use fimg::*; /// let mut a = Image::alloc(10, 10); /// // draw a triangle - /// a.as_mut().tri( + /// a.as_mut().tri::( /// (3.0, 2.0), // point a /// (8.0, 7.0), // point b /// (1.0, 8.0), // point c @@ -19,27 +19,33 @@ impl + AsRef<[u8]>, const CHANNELS: usize> Image { /// ); /// # assert_eq!(a.buffer(), b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); /// ``` - pub fn tri( + pub fn tri>( &mut self, - b: impl Into, - a: impl Into, - c: impl Into, + b: impl Into>, + a: impl Into>, + c: impl Into>, col: [u8; CHANNELS], ) { - let Vec2 { x: x1, y: y1 } = a.into(); - let Vec2 { x: x2, y: y2 } = b.into(); - let Vec2 { x: x3, y: y3 } = c.into(); - let ymin = max(y1.min(y2).min(y3) as u32, 0); - let ymax = min(y1.max(y2).max(y3) as u32, self.height()); - let xmin = max(x1.min(x2).min(x3) as u32, 0); - let xmax = min(x1.max(x2).max(x3) as u32, self.width()); + let Vector2 { x: x1, y: y1 } = a.into(); + let Vector2 { x: x2, y: y2 } = b.into(); + let Vector2 { x: x3, y: y3 } = c.into(); + let ymin = max(y1.min(y2).min(y3).take() as u32, 0); + let ymax = min(y1.max(y2).max(y3).take() as u32, self.height()); + let xmin = max(x1.min(x2).min(x3).take() as u32, 0); + let xmax = min(x1.max(x2).max(x3).take() as u32, self.width()); for y in ymin..ymax { for x in xmin..xmax { // algorithm from https://web.archive.org/web/20050408192410/http://sw-shader.sourceforge.net/rasterizer.html, but im too dumb to implement the faster ones - if madd(x1 - x2, y as f32 - y1, -(y1 - y2) * (x as f32 - x1)) > 0. - && madd(x2 - x3, y as f32 - y2, -(y2 - y3) * (x as f32 - x2)) > 0. - && madd(x3 - x1, y as f32 - y3, -(y3 - y1) * (x as f32 - x3)) > 0. - { + if unsafe { + (x1 - x2) * (F::new(y as f32) - y1) + (-(y1 - y2) * (F::new(x as f32) - x1)) + > 0. + && (x2 - x3) * (F::new(y as f32) - y2) + + (-(y2 - y3) * (F::new(x as f32) - x2)) + > 0. + && (x3 - x1) * (F::new(y as f32) - y3) + + (-(y3 - y1) * (F::new(x as f32) - x3)) + > 0. + } { // SAFETY: x, y are bounded unsafe { self.set_pixel(x, y, col) }; }