use super::{Hue, Point, Shape};
const SUB: u8 = 4;
const SAMPLES: u32 = 16;
const FULL: u32 = 255;
pub(super) fn rgba(shapes: &[Shape], size: u16) -> Vec<u8> {
let figures: Vec<Figure> = shapes.iter().map(Figure::of).collect();
let edge = f32::from(size);
let mut out = Vec::with_capacity(usize::from(size) * usize::from(size) * 4);
for row in 0..size {
for column in 0..size {
out.extend_from_slice(&pixel(&figures, column, row, edge));
}
}
out
}
#[derive(Clone, Copy)]
struct Bounds {
left: f32,
top: f32,
right: f32,
bottom: f32,
}
impl Bounds {
fn around(points: impl Iterator<Item = Point>, pad: f32) -> Self {
let mut held = Self {
left: f32::INFINITY,
top: f32::INFINITY,
right: f32::NEG_INFINITY,
bottom: f32::NEG_INFINITY,
};
for (x, y) in points {
held.left = held.left.min(x - pad);
held.top = held.top.min(y - pad);
held.right = held.right.max(x + pad);
held.bottom = held.bottom.max(y + pad);
}
held
}
fn holds(self, (x, y): Point) -> bool {
x >= self.left && x <= self.right && y >= self.top && y <= self.bottom
}
}
struct Segment {
from: Point,
to: Point,
bounds: Bounds,
}
enum Figure {
Disc {
centre: Point,
radius: f32,
fill: Hue,
},
Band {
segments: Vec<Segment>,
half: f32,
bounds: Bounds,
fill: Hue,
},
Lens {
outline: Vec<Point>,
bounds: Bounds,
fill: Hue,
},
}
impl Figure {
fn of(shape: &Shape) -> Self {
match shape {
Shape::Disc {
cx,
cy,
radius,
fill,
} => Self::Disc {
centre: (*cx, *cy),
radius: *radius,
fill: *fill,
},
Shape::Trace { path, width, fill } => {
let half = width / 2.0;
Self::Band {
segments: path
.windows(2)
.map(|pair| Segment {
from: pair.first().copied().unwrap_or_default(),
to: pair.last().copied().unwrap_or_default(),
bounds: Bounds::around(pair.iter().copied(), half),
})
.collect(),
half,
bounds: Bounds::around(path.iter().copied(), half),
fill: *fill,
}
}
Shape::Lune { ribs, fill } => {
let outline = Shape::outline(ribs);
Self::Lens {
bounds: Bounds::around(outline.iter().copied(), 0.0),
outline,
fill: *fill,
}
}
}
}
fn fill(&self) -> Hue {
match self {
Self::Disc { fill, .. } | Self::Band { fill, .. } | Self::Lens { fill, .. } => *fill,
}
}
fn holds(&self, point: Point) -> bool {
match self {
Self::Disc { centre, radius, .. } => {
let (dx, dy) = (point.0 - centre.0, point.1 - centre.1);
dx.mul_add(dx, dy * dy) <= radius * radius
}
Self::Band {
segments,
half,
bounds,
..
} => {
bounds.holds(point)
&& segments.iter().any(|segment| {
segment.bounds.holds(point)
&& gap2(segment.from, segment.to, point) <= half * half
})
}
Self::Lens {
outline, bounds, ..
} => bounds.holds(point) && crossings(outline, point) % 2 == 1,
}
}
}
fn gap2(from: Point, to: Point, point: Point) -> f32 {
let (ux, uy) = (to.0 - from.0, to.1 - from.1);
let (px, py) = (point.0 - from.0, point.1 - from.1);
let span = ux.mul_add(ux, uy * uy);
let along = (px.mul_add(ux, py * uy) / span).clamp(0.0, 1.0);
let (dx, dy) = (along.mul_add(-ux, px), along.mul_add(-uy, py));
dx.mul_add(dx, dy * dy)
}
fn crossings(outline: &[Point], (x, y): Point) -> u32 {
let mut count = 0;
for (from, to) in outline.iter().zip(outline.iter().cycle().skip(1)) {
if (from.1 > y) != (to.1 > y) {
let reach = (y - from.1) / (to.1 - from.1);
if x < reach.mul_add(to.0 - from.0, from.0) {
count += 1;
}
}
}
count
}
#[derive(Default)]
struct Seen {
red: u32,
green: u32,
blue: u32,
hits: u32,
}
impl Seen {
fn take(&mut self, fill: Hue) {
self.red += u32::from(fill[0]);
self.green += u32::from(fill[1]);
self.blue += u32::from(fill[2]);
self.hits += 1;
}
fn pixel(&self) -> [u8; 4] {
if self.hits == 0 {
return [0; 4];
}
let mean = |total: u32| byte((total + self.hits / 2) / self.hits);
[
mean(self.red),
mean(self.green),
mean(self.blue),
byte((self.hits * FULL + SAMPLES / 2) / SAMPLES),
]
}
}
fn pixel(figures: &[Figure], column: u16, row: u16, edge: f32) -> [u8; 4] {
let mut seen = Seen::default();
for down in 0..SUB {
for across in 0..SUB {
let point = (sample(column, across, edge), sample(row, down, edge));
if let Some(fill) = topmost(figures, point) {
seen.take(fill);
}
}
}
seen.pixel()
}
fn sample(whole: u16, step: u8, edge: f32) -> f32 {
(f32::from(step) + 0.5).mul_add(1.0 / f32::from(SUB), f32::from(whole)) / edge
}
fn topmost(figures: &[Figure], point: Point) -> Option<Hue> {
figures
.iter()
.rev()
.find(|figure| figure.holds(point))
.map(Figure::fill)
}
fn byte(value: u32) -> u8 {
u8::try_from(value.min(FULL)).unwrap_or(u8::MAX)
}
#[cfg(test)]
pub(super) const fn samples() -> (u8, u32) {
(SUB, SAMPLES)
}