fast image operations
Diffstat (limited to 'src/drawing/tri.rs')
-rw-r--r--src/drawing/tri.rs82
1 files changed, 30 insertions, 52 deletions
diff --git a/src/drawing/tri.rs b/src/drawing/tri.rs
index 7ca13b6..3cc390b 100644
--- a/src/drawing/tri.rs
+++ b/src/drawing/tri.rs
@@ -1,65 +1,43 @@
//! trongle drawing
-use umath::Float;
-use vecto::Vector2;
-
use crate::Image;
-use std::cmp::{max, min};
-impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
+impl<const CHANNELS: usize> Image<&mut [u8], CHANNELS> {
/// Draw a (filled) triangle
+ ///
+ /// # Safety
+ ///
+ /// UB if any point is out of bounds
/// ```
/// # use fimg::*;
/// let mut a = Image::alloc(10, 10);
- /// // draw a triangle
- /// a.as_mut().tri::<f32>(
- /// (3.0, 2.0), // point a
- /// (8.0, 7.0), // point b
- /// (1.0, 8.0), // point c
- /// [255] // white
- /// );
- /// # 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");
+ /// // draw a triangle from point a v point b v point c v
+ /// // with color white
+ /// unsafe { a.as_mut().tri((3.0, 2.0), (8.0, 7.0), (1.0, 8.0), [255]) };
+ /// # 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\xff\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\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<F: Float<f32>>(
+ pub unsafe fn tri(
&mut self,
- a: impl Into<Vector2<F>>,
- b: impl Into<Vector2<F>>,
- c: impl Into<Vector2<F>>,
- col: [u8; CHANNELS],
+ (x1, y1): (f32, f32),
+ (x2, y2): (f32, f32),
+ (x3, y3): (f32, f32),
+ c: [u8; CHANNELS],
) {
- let Vector2 {
- x: mut x1,
- y: mut y1,
- } = a.into();
- let Vector2 {
- x: mut x2,
- y: mut y2,
- } = b.into();
- let Vector2 { x: x3, y: y3 } = c.into();
- // fix winding
- if (x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1) > 0.0 {
- std::mem::swap(&mut x1, &mut x2);
- std::mem::swap(&mut y1, &mut y2);
- }
- 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
- // SAFETY: nNaN
- 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) };
+ // TODO optimize
+ for y in y1.min(y2).min(y3) as u32..y1.max(y2).max(y3) as u32 {
+ for x in x1.min(x2).min(x3) as u32..x1.max(x2).max(x3) as u32 {
+ let s = (x1 - x3) * (y as f32 - y3) - (y1 - y2) * (x as f32 - x3);
+ let t = (x2 - x1) * (y as f32 - y1) - (y2 - y1) * (x as f32 - x1);
+
+ if (s < 0.0) != (t < 0.0) && s != 0.0 && t != 0.0 {
+ continue;
+ }
+
+ let d = (x3 - x2) * (y as f32 - y2) - (y3 - y2) * (x as f32 - x2);
+ if d == 0.0 || (d < 0.0) == (s + t <= 0.0) {
+ // SAFETY:
+ // caller gurantees triangle is in bounds, this loops over the
+ // bounding box of the triangle, therefore this is fine.
+ unsafe { self.set_pixel(x, y, c) };
}
}
}