use super::Renderer;
use crate::{Color, Rect};
#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) struct PlanarTransform {
a: f32,
b: f32,
c: f32,
d: f32,
e: f32,
f: f32,
g: f32,
h: f32,
i: f32,
}
impl PlanarTransform {
pub(super) const IDENTITY: Self = Self {
a: 1.0,
b: 0.0,
c: 0.0,
d: 1.0,
e: 0.0,
f: 0.0,
g: 0.0,
h: 0.0,
i: 1.0,
};
pub(super) fn is_identity(self) -> bool {
self == Self::IDENTITY
}
pub(super) fn is_axis_aligned(self) -> bool {
self.b == 0.0 && self.c == 0.0 && self.g == 0.0 && self.h == 0.0 && self.i == 1.0
}
pub(super) fn is_positive_axis_aligned(self) -> bool {
self.is_axis_aligned() && self.a > 0.0 && self.d > 0.0
}
pub(super) fn from_parts(
origin: [f32; 2],
translate: [f32; 2],
scale: [f32; 2],
rotation_rad: f32,
) -> Self {
let (sin, cos) = rotation_rad.sin_cos();
let a = scale[0] * cos;
let b = scale[0] * sin;
let c = -scale[1] * sin;
let d = scale[1] * cos;
let e = origin[0] + translate[0] - (a * origin[0] + c * origin[1]);
let f = origin[1] + translate[1] - (b * origin[0] + d * origin[1]);
Self { a, b, c, d, e, f, g: 0.0, h: 0.0, i: 1.0 }
}
pub(super) fn scale_y(origin: f32, scale: f32) -> Self {
Self {
a: 1.0,
b: 0.0,
c: 0.0,
d: scale,
e: 0.0,
f: origin * (1.0 - scale),
g: 0.0,
h: 0.0,
i: 1.0,
}
}
pub(super) fn from_matrix([[a, c, e], [b, d, f], [g, h, i]]: [[f32; 3]; 3]) -> Self {
Self { a, b, c, d, e, f, g, h, i }
}
pub(super) fn then(self, local: Self) -> Self {
Self {
a: self.a * local.a + self.c * local.b + self.e * local.g,
b: self.b * local.a + self.d * local.b + self.f * local.g,
c: self.a * local.c + self.c * local.d + self.e * local.h,
d: self.b * local.c + self.d * local.d + self.f * local.h,
e: self.a * local.e + self.c * local.f + self.e * local.i,
f: self.b * local.e + self.d * local.f + self.f * local.i,
g: self.g * local.a + self.h * local.b + self.i * local.g,
h: self.g * local.c + self.h * local.d + self.i * local.h,
i: self.g * local.e + self.h * local.f + self.i * local.i,
}
}
#[cfg(test)]
pub(super) fn apply(self, p: [f32; 2]) -> [f32; 2] {
let (position, w) = self.homogeneous(p);
[position[0] / w, position[1] / w]
}
pub(super) fn homogeneous(self, p: [f32; 2]) -> ([f32; 2], f32) {
let w = self.g * p[0] + self.h * p[1] + self.i;
([self.a * p[0] + self.c * p[1] + self.e,
self.b * p[0] + self.d * p[1] + self.f], w)
}
pub(super) fn inverse_axis_aligned_rect(self, rect: Rect) -> Option<Rect> {
if self.g != 0.0 || self.h != 0.0 || self.i != 1.0 {
return None;
}
let tolerance = f32::EPSILON * 4.0 * self.a.abs().max(self.b.abs()).max(self.c.abs()).max(self.d.abs()).max(1.0);
if self.a.abs() <= tolerance && self.d.abs() <= tolerance && self.b.abs() > tolerance && self.c.abs() > tolerance {
let x0=(rect.y-self.f)/self.b; let x1=(rect.y+rect.height-self.f)/self.b;
let y0=(rect.x-self.e)/self.c; let y1=(rect.x+rect.width-self.e)/self.c;
if ![x0,x1,y0,y1].iter().all(|v| v.is_finite()) { return None; }
return Some(Rect {x:x0.min(x1),y:y0.min(y1),width:(x1-x0).abs(),height:(y1-y0).abs()});
}
if self.b != 0.0 || self.c != 0.0 { return None; }
if self.a == 0.0 || self.d == 0.0 { return Some(Rect::default()); }
let x0=(rect.x-self.e)/self.a; let x1=(rect.x+rect.width-self.e)/self.a;
let y0=(rect.y-self.f)/self.d; let y1=(rect.y+rect.height-self.f)/self.d;
if ![x0,x1,y0,y1].iter().all(|v| v.is_finite()) { return None; }
Some(Rect { x:x0.min(x1),y:y0.min(y1),width:(x1-x0).abs(),height:(y1-y0).abs() })
}
pub(super) fn rect_clip_planes(self, rect: Rect) -> Option<[[f32; 3]; 4]> {
if rect.width <= 0.0 || rect.height <= 0.0 { return None; }
let matrix=crate::projective::ProjectiveTransform::new([
[self.a as f64,self.c as f64,self.e as f64],
[self.b as f64,self.d as f64,self.f as f64],
[self.g as f64,self.h as f64,self.i as f64],
])?.inverse()?.coefficients();
let edge=|a: usize, k: f64, sign: f64| -> [f32; 3] {
std::array::from_fn(|i| ((matrix[a][i]-k*matrix[2][i])*sign) as f32)
};
let planes=[edge(0,rect.x as f64,1.0),edge(0,(rect.x+rect.width) as f64,-1.0),
edge(1,rect.y as f64,1.0),edge(1,(rect.y+rect.height) as f64,-1.0)];
planes.iter().flatten().all(|v| v.is_finite()).then_some(planes)
}
pub(super) fn transform_rect_aabb(self, rect: Rect, unbounded: Rect) -> Rect {
if rect.width <= 0.0 || rect.height <= 0.0 {
return Rect::default();
}
if self.is_identity() {
return rect;
}
let corners = [[rect.x, rect.y], [rect.x + rect.width, rect.y],
[rect.x + rect.width, rect.y + rect.height], [rect.x, rect.y + rect.height]]
.map(|point| self.homogeneous(point));
if corners.iter().all(|(_, w)| *w <= 0.0) { return Rect::default(); }
if corners.iter().any(|(point, w)| *w <= 0.0 || !w.is_finite() || !point.iter().all(|x| x.is_finite())) {
return unbounded;
}
let mut min_x = f32::INFINITY;
let mut min_y = f32::INFINITY;
let mut max_x = f32::NEG_INFINITY;
let mut max_y = f32::NEG_INFINITY;
for ([x, y], w) in corners {
let (x, y) = (x / w, y / w);
if !x.is_finite() || !y.is_finite() { return unbounded; }
min_x = min_x.min(x);
min_y = min_y.min(y);
max_x = max_x.max(x);
max_y = max_y.max(y);
}
if !(max_x - min_x).is_finite() || !(max_y - min_y).is_finite() { return unbounded; }
Rect {
x: min_x,
y: min_y,
width: (max_x - min_x).max(0.0),
height: (max_y - min_y).max(0.0),
}
}
}
#[cfg(test)]
mod projective_tests {
use super::PlanarTransform;
use crate::Rect;
#[test]
fn axis_aligned_child_inverse_maps_parent_clip() {
let clip=Rect{x:10.0,y:20.0,width:40.0,height:30.0};
let transform=PlanarTransform::from_parts([0.0;2],[5.0,10.0],[-2.0,0.5],0.0);
assert_eq!(transform.inverse_axis_aligned_rect(clip),Some(Rect{x:-22.5,y:20.0,width:20.0,height:60.0}));
let rotate=PlanarTransform::from_parts([0.0;2],[0.0;2],[1.0;2],0.2);
assert_eq!(rotate.inverse_axis_aligned_rect(clip),None);
let quarter=PlanarTransform::from_parts([0.0;2],[0.0;2],[1.0;2],std::f32::consts::FRAC_PI_2);
let mapped=quarter.inverse_axis_aligned_rect(clip).unwrap();
assert!((mapped.x-20.0).abs()<0.0001 && (mapped.y+50.0).abs()<0.0001);
assert!((mapped.width-30.0).abs()<0.0001 && (mapped.height-40.0).abs()<0.0001);
}
#[test]
fn projected_clip_planes_contain_quad_for_both_windings() {
let rect=Rect{x:0.0,y:0.0,width:100.0,height:100.0};
for reflection in [1.0,-1.0] {
let t=PlanarTransform::from_matrix([[reflection,0.0,0.0],[0.0,1.0,0.0],[0.005,0.0,1.0]]);
let planes=t.rect_clip_planes(rect).unwrap();
let point=t.apply([50.0,50.0]);
assert!(planes.iter().all(|p| p[0]*point[0]+p[1]*point[1]+p[2]>=0.0));
let outside=t.apply([50.0,120.0]);
assert!(planes.iter().any(|p| p[0]*outside[0]+p[1]*outside[1]+p[2]<0.0));
}
let horizon=PlanarTransform::from_matrix([[1.0,0.0,0.0],[0.0,1.0,0.0],[-0.02,0.0,1.0]]);
let planes=horizon.rect_clip_planes(rect).unwrap();
let inside=horizon.apply([25.0,50.0]);
let outside=horizon.apply([25.0,120.0]);
assert!(planes.iter().all(|p| p[0]*inside[0]+p[1]*inside[1]+p[2]>=0.0));
assert!(planes.iter().any(|p| p[0]*outside[0]+p[1]*outside[1]+p[2]<0.0));
}
#[test]
fn perspective_coordinates_and_w_survive_composition() {
let perspective = PlanarTransform { g: 0.01, ..PlanarTransform::IDENTITY };
let parent = PlanarTransform::from_parts([0.0, 0.0], [5.0, -3.0], [1.0, 1.0], 0.0);
let combined = parent.then(perspective);
let (point, w) = combined.homogeneous([100.0, 20.0]);
assert!((point[0] / w - 55.0).abs() < 0.0001 && (point[1] / w - 7.0).abs() < 0.0001);
assert!((w - 2.0).abs() < 0.0001);
let (horizon, w) = perspective.homogeneous([-100.0, 20.0]);
assert_eq!(w, 0.0);
assert_eq!(horizon, [-100.0, 20.0]);
}
#[test]
fn perspective_bounds_conservatively_handle_the_camera_plane() {
let transform = PlanarTransform { g: 0.01, ..PlanarTransform::IDENTITY };
let screen = Rect { x: 0.0, y: 0.0, width: 400.0, height: 300.0 };
let crossing = Rect { x: -110.0, y: 0.0, width: 20.0, height: 10.0 };
assert_eq!(transform.transform_rect_aabb(crossing, screen), screen);
let behind = Rect { x: -150.0, y: 0.0, width: 20.0, height: 10.0 };
assert_eq!(transform.transform_rect_aabb(behind, screen), Rect::default());
let visible = Rect { x: 0.0, y: 0.0, width: 100.0, height: 20.0 };
let mapped = transform.transform_rect_aabb(visible, screen);
assert!((mapped.width - 50.0).abs() < 0.0001 && (mapped.height - 20.0).abs() < 0.0001);
}
}
impl Renderer {
pub(super) fn is_rect_visible(&self, rect: Rect) -> bool {
if rect.width <= 0.0 || rect.height <= 0.0 {
return false;
}
let clipped = match self.active_local_clip() {
Some(clip) => rect.intersect(clip),
None => rect,
};
if clipped.width <= 0.0 || clipped.height <= 0.0 {
return false;
}
if !self.transform.is_identity() {
let screen = self
.transform
.transform_rect_aabb(clipped, self.screen_clip)
.intersect(self.screen_clip);
if screen.width <= 0.0 || screen.height <= 0.0 {
return false;
}
}
true
}
pub(super) fn glyph_quad(&mut self, rect: Rect, uv: [f32; 4], color: Color, baseline: f32, shear: f32) {
if rect.width <= 0.0 || rect.height <= 0.0 {
return;
}
let half = [rect.width * 0.5, rect.height * 0.5];
let mut min_x = f32::INFINITY;
let mut min_y = f32::INFINITY;
let mut max_x = f32::NEG_INFINITY;
let mut max_y = f32::NEG_INFINITY;
let mut finite = true;
for [corner_x, corner_y] in [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]] {
let py = rect.y + corner_y * rect.height;
let px = rect.x + corner_x * rect.width + (baseline - py) * shear;
let (pos, w) = self.transform.homogeneous([px, py]);
if w > 0.0 && w.is_finite() {
let sx = pos[0] / w;
let sy = pos[1] / w;
min_x = min_x.min(sx);
min_y = min_y.min(sy);
max_x = max_x.max(sx);
max_y = max_y.max(sy);
} else {
finite = false;
}
self.vertices.extend_from_slice(&[
pos[0],
pos[1],
(corner_x * 2.0 - 1.0) * half[0],
(corner_y * 2.0 - 1.0) * half[1],
uv[0] + corner_x * (uv[2] - uv[0]),
uv[1] + corner_y * (uv[3] - uv[1]),
color.0 as f32 / 255.0,
color.1 as f32 / 255.0,
color.2 as f32 / 255.0,
color.3 as f32 / 255.0,
half[0],
half[1],
0.0,
1.0,
w,
]);
}
if !self.active_layers.is_empty() {
if finite && min_x.is_finite() && max_x.is_finite() {
self.note_drawn_rect(Rect {
x: min_x,
y: min_y,
width: (max_x - min_x).max(0.0),
height: (max_y - min_y).max(0.0),
});
} else {
self.note_drawn_rect(self.screen_clip);
}
}
}
pub(super) fn quad(&mut self, rect: Rect, uv: [f32; 4], color: Color, radius: f32, mode: f32) {
self.quad_sub(rect, rect, uv, color, radius, mode);
}
pub(super) fn quad_sub(
&mut self,
rect: Rect,
sub: Rect,
uv: [f32; 4],
color: Color,
radius: f32,
mode: f32,
) {
if rect.width <= 0.0 || rect.height <= 0.0 || sub.width <= 0.0 || sub.height <= 0.0 {
return;
}
let sub_clipped = rect.intersect(sub);
if sub_clipped.width <= 0.0 || sub_clipped.height <= 0.0 {
return;
}
let clipped = match self.active_local_clip() {
Some(clip) => sub_clipped.intersect(clip),
None => sub_clipped,
};
if clipped.width <= 0.0 || clipped.height <= 0.0 {
return;
}
let screen_aabb = if !self.transform.is_identity() {
let screen = self
.transform
.transform_rect_aabb(clipped, self.screen_clip)
.intersect(self.screen_clip);
if screen.width <= 0.0 || screen.height <= 0.0 {
return;
}
screen
} else {
clipped
};
if !self.active_layers.is_empty() {
self.note_drawn_rect(screen_aabb);
}
let half = [rect.width * 0.5, rect.height * 0.5];
for [corner_x, corner_y] in [[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0]] {
let u = (clipped.x + corner_x * clipped.width - rect.x) / rect.width;
let v = (clipped.y + corner_y * clipped.height - rect.y) / rect.height;
let (pos, w) = self
.transform
.homogeneous([rect.x + u * rect.width, rect.y + v * rect.height]);
self.vertices.extend_from_slice(&[
pos[0],
pos[1],
(u * 2.0 - 1.0) * half[0],
(v * 2.0 - 1.0) * half[1],
uv[0] + u * (uv[2] - uv[0]),
uv[1] + v * (uv[3] - uv[1]),
color.0 as f32 / 255.0,
color.1 as f32 / 255.0,
color.2 as f32 / 255.0,
color.3 as f32 / 255.0,
half[0],
half[1],
radius.min(half[0]).min(half[1]).max(0.0),
mode,
w,
]);
}
}
pub(super) fn static_shape(&mut self, bounds: Rect, index: usize, color: Color, rotation: f32) {
let Some(shape) = crate::shape_data(index) else {
return;
};
let rotated_clip = crate::shape_clip_bounds(bounds, rotation);
let screen = self
.transform
.transform_rect_aabb(rotated_clip, self.screen_clip)
.intersect(self.screen_clip);
if screen.width <= 0.0 || screen.height <= 0.0 {
return;
}
let previous_screen_clip = self.screen_clip;
let previous_local_clip = self.local_clip;
if self.transform.is_identity() {
self.screen_clip = self.screen_clip.intersect(rotated_clip);
} else {
self.local_clip = Some(
self.local_clip
.map_or(rotated_clip, |c| c.intersect(rotated_clip)),
);
}
let size = bounds.width.min(bounds.height);
let x = bounds.x + (bounds.width - size) / 2.0;
let y = bounds.y + (bounds.height - size) / 2.0;
let (sin, cos) = rotation.sin_cos();
let transform = |p: [f32; 2]| {
let (px, py) = (p[0] - 0.5, p[1] - 0.5);
[
x + (0.5 + px * cos - py * sin) * size,
y + (0.5 + px * sin + py * cos) * size,
]
};
let point = |i: u16| {
let p = transform(shape.points[i as usize]);
[p[0], p[1], 1.0]
};
for tri in shape.triangles.as_chunks::<3>().0 {
self.triangle([point(tri[0]), point(tri[1]), point(tri[2])], color);
}
for contour in shape.contours.windows(2) {
let points = &shape.points[contour[0] as usize..contour[1] as usize];
let area: f32 = points
.iter()
.zip(points.iter().cycle().skip(1))
.take(points.len())
.map(|(a, b)| a[0] * b[1] - a[1] * b[0])
.sum();
let winding = if area >= 0.0 { 1.0 } else { -1.0 };
for (a, b) in points
.iter()
.zip(points.iter().cycle().skip(1))
.take(points.len())
{
let (a, b) = (transform(*a), transform(*b));
let d = [b[0] - a[0], b[1] - a[1]];
let length = d[0].hypot(d[1]);
if length > 0.00001 {
let n = [
winding * d[1] / length / self.scale,
-winding * d[0] / length / self.scale,
];
self.mesh_quad(
[
[a[0], a[1], 1.0],
[b[0], b[1], 1.0],
[b[0] + n[0], b[1] + n[1], 0.0],
[a[0] + n[0], a[1] + n[1], 0.0],
],
color,
);
}
}
}
self.screen_clip = previous_screen_clip;
self.local_clip = previous_local_clip;
}
pub(super) fn loading(&mut self, bounds: Rect, sample: crate::loading::LoadingSample, color: Color) {
let screen = self
.transform
.transform_rect_aabb(bounds, self.screen_clip)
.intersect(self.screen_clip);
if screen.width <= 0.0 || screen.height <= 0.0 {
return;
}
let mut points = std::mem::take(&mut self.path_points);
points.clear();
crate::loading::loading_cubics(bounds, sample, |c| {
if points.is_empty() {
points.push([c[0], c[1]]);
}
for step in 1..=12 {
let t = step as f32 / 12.0;
let u = 1.0 - t;
points.push(std::array::from_fn(|axis| {
u * u * u * c[axis]
+ 3.0 * u * u * t * c[axis + 2]
+ 3.0 * u * t * t * c[axis + 4]
+ t * t * t * c[axis + 6]
}));
}
});
let center = [
bounds.x + bounds.width * 0.5,
bounds.y + bounds.height * 0.5,
1.0,
];
for i in (1..points.len()).step_by(2) {
let a = points[i - 1];
let b = points[i];
let c = points[(i + 1).min(points.len() - 1)];
self.mesh_quad(
[
center,
[a[0], a[1], 1.0],
[b[0], b[1], 1.0],
[c[0], c[1], 1.0],
],
color,
);
}
for edge in points.windows(2) {
let (a, b) = (edge[0], edge[1]);
let delta = [b[0] - a[0], b[1] - a[1]];
let length = delta[0].hypot(delta[1]);
if length > 0.00001 {
let n = [
delta[1] / length / self.scale,
-delta[0] / length / self.scale,
];
let a0 = [a[0], a[1], 1.0];
let b0 = [b[0], b[1], 1.0];
let a1 = [a[0] + n[0], a[1] + n[1], 0.0];
let b1 = [b[0] + n[0], b[1] + n[1], 0.0];
self.mesh_quad([a0, b0, b1, a1], color);
}
}
self.path_points = points;
}
pub(super) fn emit_quad(&mut self, points: [[f32; 3]; 4], color: Color) {
let track = !self.active_layers.is_empty();
let mut min_x = f32::INFINITY;
let mut min_y = f32::INFINITY;
let mut max_x = f32::NEG_INFINITY;
let mut max_y = f32::NEG_INFINITY;
let mut finite = true;
for p in points {
let (pos, w) = self.transform.homogeneous([p[0], p[1]]);
if track {
if w > 0.0 && w.is_finite() {
let sx = pos[0] / w;
let sy = pos[1] / w;
min_x = min_x.min(sx);
min_y = min_y.min(sy);
max_x = max_x.max(sx);
max_y = max_y.max(sy);
} else {
finite = false;
}
}
self.vertices.extend_from_slice(&[
pos[0],
pos[1],
0.0,
0.0,
0.0,
0.0,
color.0 as f32 / 255.0,
color.1 as f32 / 255.0,
color.2 as f32 / 255.0,
color.3 as f32 / 255.0 * p[2],
1.0,
1.0,
0.0,
-1.0,
w,
]);
}
if track {
if finite && min_x.is_finite() && max_x.is_finite() {
self.note_drawn_rect(Rect {
x: min_x,
y: min_y,
width: (max_x - min_x).max(0.0),
height: (max_y - min_y).max(0.0),
});
} else {
self.note_drawn_rect(self.screen_clip);
}
}
}
pub(super) fn mesh_quad(&mut self, points: [[f32; 3]; 4], color: Color) {
match self.active_local_clip() {
None => self.emit_quad(points, color),
Some(clip) => {
if points.iter().all(|p| clip.contains([p[0], p[1]])) {
self.emit_quad(points, color);
} else {
self.triangle([points[0], points[1], points[2]], color);
self.triangle([points[0], points[2], points[3]], color);
}
}
}
}
pub(super) fn polyline(&mut self, points: [[f32; 2]; 3], count: u8, width: f32, color: Color) {
let n = usize::from(count).clamp(2, 3);
let normal = |a: [f32; 2], b: [f32; 2]| {
let dx = b[0] - a[0];
let dy = b[1] - a[1];
let len = dx.hypot(dy).max(0.00001);
[-dy / len, dx / len]
};
let first = normal(points[0], points[1]);
let last = if n == 3 {
normal(points[1], points[2])
} else {
first
};
let half = width * 0.5;
let middle = if n == 3 {
let sum = [first[0] + last[0], first[1] + last[1]];
let dot = (sum[0] * last[0] + sum[1] * last[1]).max(0.01);
[sum[0] * half / dot, sum[1] * half / dot]
} else {
[first[0] * half, first[1] * half]
};
let offsets = [
[first[0] * half, first[1] * half],
middle,
[last[0] * half, last[1] * half],
];
for i in 0..n - 1 {
let p = points[i];
let q = points[i + 1];
let a = offsets[i];
let b = offsets[i + 1];
self.mesh_quad(
[
[p[0] + a[0], p[1] + a[1], 1.0],
[q[0] + b[0], q[1] + b[1], 1.0],
[q[0] - b[0], q[1] - b[1], 1.0],
[p[0] - a[0], p[1] - a[1], 1.0],
],
color,
);
}
let mut perimeter = [[0.0; 2]; 6];
for i in 0..n {
perimeter[i] = [points[i][0] + offsets[i][0], points[i][1] + offsets[i][1]];
perimeter[2 * n - 1 - i] = [points[i][0] - offsets[i][0], points[i][1] - offsets[i][1]];
}
let perimeter = &perimeter[..2 * n];
let area: f32 = perimeter
.iter()
.zip(perimeter.iter().cycle().skip(1))
.take(perimeter.len())
.map(|(a, b)| a[0] * b[1] - a[1] * b[0])
.sum();
let winding = if area >= 0.0 { 1.0 } else { -1.0 };
for (&a, &b) in perimeter
.iter()
.zip(perimeter.iter().cycle().skip(1))
.take(perimeter.len())
{
let dx = b[0] - a[0];
let dy = b[1] - a[1];
let length = dx.hypot(dy);
if length > 0.00001 {
let offset = [
winding * dy / length / self.scale,
-winding * dx / length / self.scale,
];
self.mesh_quad(
[
[a[0], a[1], 1.0],
[b[0], b[1], 1.0],
[b[0] + offset[0], b[1] + offset[1], 0.0],
[a[0] + offset[0], a[1] + offset[1], 0.0],
],
color,
);
}
}
}
pub(super) fn triangle(&mut self, triangle: [[f32; 3]; 3], color: Color) {
let Some(clip) = self.active_local_clip() else {
self.emit_quad([triangle[0], triangle[1], triangle[2], triangle[0]], color);
return;
};
let mut a = [[0.0; 3]; 12];
let mut b = [[0.0; 3]; 12];
a[..3].copy_from_slice(&triangle);
let mut count = 3;
for (axis, edge, greater) in [
(0, clip.x, true),
(0, clip.x + clip.width, false),
(1, clip.y, true),
(1, clip.y + clip.height, false),
] {
if count == 0 {
return;
}
let mut next = 0;
for i in 0..count {
let p = a[i];
let q = a[(i + 1) % count];
let inside = |v: [f32; 3]| {
if greater {
v[axis] >= edge
} else {
v[axis] <= edge
}
};
let pi = inside(p);
let qi = inside(q);
if pi {
b[next] = p;
next += 1;
}
if pi != qi {
let t = (edge - p[axis]) / (q[axis] - p[axis]);
b[next] = std::array::from_fn(|j| p[j] + t * (q[j] - p[j]));
next += 1;
}
}
count = next;
std::mem::swap(&mut a, &mut b);
}
if count < 3 {
return;
}
for i in 1..count - 1 {
self.emit_quad([a[0], a[i], a[i + 1], a[0]], color);
}
}
}