mirror of
https://github.com/bend-n/fimg.git
synced 2024-12-22 10:28:21 -06:00
add a madd
function
This commit is contained in:
parent
a0f555439e
commit
853b51fc50
|
@ -1,8 +1,7 @@
|
||||||
//! draw polygons
|
//! draw polygons
|
||||||
use std::{
|
use crate::math::madd;
|
||||||
cmp::{max, min},
|
use std::cmp::{max, min};
|
||||||
f32::consts::TAU,
|
use std::f32::consts::TAU;
|
||||||
};
|
|
||||||
|
|
||||||
use crate::Image;
|
use crate::Image;
|
||||||
|
|
||||||
|
@ -43,7 +42,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let fraction = (y - p0.1) as f32 / (p1.1 - p0.1) as f32;
|
let fraction = (y - p0.1) as f32 / (p1.1 - p0.1) as f32;
|
||||||
let inter = fraction.mul_add((p1.0 - p0.0) as f32, p0.0 as f32);
|
let inter = madd(fraction, (p1.0 - p0.0) as f32, p0.0 as f32);
|
||||||
intersections.push(inter.round() as i32);
|
intersections.push(inter.round() as i32);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,7 +112,7 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
|
||||||
self.tri(
|
self.tri(
|
||||||
add(trans(space + rotation)),
|
add(trans(space + rotation)),
|
||||||
add(trans(rotation)),
|
add(trans(rotation)),
|
||||||
add(trans(space.mul_add(2.0, rotation))),
|
add(trans(madd(space, 2.0, rotation))),
|
||||||
c,
|
c,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -122,9 +121,9 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
|
||||||
for i in (0..sides - 1).step_by(2).map(|i| i as f32) {
|
for i in (0..sides - 1).step_by(2).map(|i| i as f32) {
|
||||||
self.quad(
|
self.quad(
|
||||||
r((x, y)),
|
r((x, y)),
|
||||||
r(add(trans(space.mul_add(i, rotation)))),
|
r(add(trans(madd(space, i, rotation)))),
|
||||||
r(add(trans(space.mul_add(i + 1., rotation)))),
|
r(add(trans(madd(space, i + 1., rotation)))),
|
||||||
r(add(trans(space.mul_add(i + 2., rotation)))),
|
r(add(trans(madd(space, i + 2., rotation)))),
|
||||||
c,
|
c,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -134,8 +133,8 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
|
||||||
// the missing piece
|
// the missing piece
|
||||||
self.tri(
|
self.tri(
|
||||||
(x, y),
|
(x, y),
|
||||||
add(trans(space.mul_add(i, rotation))),
|
add(trans(madd(space, i, rotation))),
|
||||||
add(trans(space.mul_add(i + 1., rotation))),
|
add(trans(madd(space, i + 1., rotation))),
|
||||||
c,
|
c,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
//! trongle drawing
|
//! trongle drawing
|
||||||
|
|
||||||
|
use crate::math::madd;
|
||||||
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
use crate::Image;
|
use crate::Image;
|
||||||
|
|
||||||
|
@ -25,21 +27,22 @@ impl<T: AsMut<[u8]> + AsRef<[u8]>, const CHANNELS: usize> Image<T, CHANNELS> {
|
||||||
c: [u8; CHANNELS],
|
c: [u8; CHANNELS],
|
||||||
) {
|
) {
|
||||||
// TODO optimize
|
// TODO optimize
|
||||||
for y in y1.min(y2).min(y3) as u32..y1.max(y2).max(y3) as u32 {
|
let ymin = max(y1.min(y2).min(y3) as u32, 0);
|
||||||
for x in x1.min(x2).min(x3) as u32..x1.max(x2).max(x3) as u32 {
|
let ymax = min(y1.max(y2).max(y3) as u32, self.height());
|
||||||
let s = (x1 - x3).mul_add(y as f32 - y3, -(y1 - y2) * (x as f32 - x3));
|
let xmin = max(x1.min(x2).min(x3) as u32, 0);
|
||||||
let t = (x2 - x1).mul_add(y as f32 - y1, -(y2 - y1) * (x as f32 - x1));
|
let xmax = min(x1.max(x2).max(x3) as u32, self.width());
|
||||||
|
for y in ymin..ymax {
|
||||||
|
for x in xmin..xmax {
|
||||||
|
let s = madd(x1 - x3, y as f32 - y3, -(y1 - y2) * (x as f32 - x3));
|
||||||
|
let t = madd(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 {
|
if (s < 0.0) != (t < 0.0) && s != 0.0 && t != 0.0 {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let d = (x3 - x2).mul_add(y as f32 - y2, -(y3 - y2) * (x as f32 - x2));
|
let d = madd(x3 - x2, y as f32 - y2, -(y3 - y2) * (x as f32 - x2));
|
||||||
if (d == 0.0 || (d < 0.0) == (s + t <= 0.0))
|
if d == 0.0 || (d < 0.0) == (s + t <= 0.0) {
|
||||||
&& x < self.width()
|
// SAFETY: x, y are bounded
|
||||||
&& y < self.height()
|
|
||||||
{
|
|
||||||
// SAFETY: we just checked the bounds
|
|
||||||
unsafe { self.set_pixel(x, y, c) };
|
unsafe { self.set_pixel(x, y, c) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,13 +25,13 @@
|
||||||
missing_docs
|
missing_docs
|
||||||
)]
|
)]
|
||||||
#![allow(clippy::zero_prefixed_literal, incomplete_features)]
|
#![allow(clippy::zero_prefixed_literal, incomplete_features)]
|
||||||
|
|
||||||
use std::{num::NonZeroU32, slice::SliceIndex};
|
use std::{num::NonZeroU32, slice::SliceIndex};
|
||||||
|
|
||||||
mod affine;
|
mod affine;
|
||||||
pub mod builder;
|
pub mod builder;
|
||||||
pub mod cloner;
|
pub mod cloner;
|
||||||
mod drawing;
|
mod drawing;
|
||||||
|
pub(crate) mod math;
|
||||||
mod overlay;
|
mod overlay;
|
||||||
pub mod scale;
|
pub mod scale;
|
||||||
use cloner::ImageCloner;
|
use cloner::ImageCloner;
|
||||||
|
@ -271,10 +271,10 @@ impl<const CHANNELS: usize> Image<Vec<u8>, CHANNELS> {
|
||||||
macro_rules! make {
|
macro_rules! make {
|
||||||
($channels:literal channels $w:literal x $h: literal) => {
|
($channels:literal channels $w:literal x $h: literal) => {
|
||||||
unsafe {
|
unsafe {
|
||||||
Image::<_, $channels>::new(
|
$crate::Image::<_, $channels>::new(
|
||||||
match ::core::num::NonZeroU32::new($w) {
|
match ::core::num::NonZeroU32::new($w) {
|
||||||
Some(n) => n,
|
::core::option::Option::Some(n) => n,
|
||||||
None => panic!("width is 0"),
|
::core::option::Option::None => panic!("width is 0"),
|
||||||
},
|
},
|
||||||
match ::core::num::NonZeroU32::new($h) {
|
match ::core::num::NonZeroU32::new($h) {
|
||||||
::core::option::Option::Some(n) => n,
|
::core::option::Option::Some(n) => n,
|
||||||
|
|
8
src/math.rs
Normal file
8
src/math.rs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
/// Calculates `a * b + c`, with hardware support if possible.
|
||||||
|
pub fn madd(a: f32, b: f32, c: f32) -> f32 {
|
||||||
|
if cfg!(target_feature = "fma") {
|
||||||
|
a.mul_add(b, c)
|
||||||
|
} else {
|
||||||
|
a * b + c
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue