use crate::geom::WfVertex;
use std::cmp::Ordering;
const SUB: i64 = 256;
const HALF: i64 = SUB / 2;
const GUARD_PX: f32 = 16384.0;
#[inline]
fn viewport_axis(c: f32, size: f32) -> f32 {
let half = f64::from(size) * 0.5;
let hi = f64::from(c) * half;
let s = hi + half;
let bb = s - hi;
let err = (hi - (s - bb)) + (half - bb);
let t = s as f32;
if !t.is_finite() {
return t;
}
let d = (f64::from(t) - s) - err; if (t > 0.0 && d > 0.0) || (t < 0.0 && d < 0.0) {
f32::from_bits(t.to_bits() - 1)
} else {
t
}
}
#[inline]
fn snap(v: f32) -> i64 {
(v * SUB as f32).round_ties_even() as i64
}
#[inline]
fn snap_pos(p: [f32; 2], w: f32, h: f32) -> (i64, i64) {
(
snap(viewport_axis(p[0], w)),
SUB * h as i64 - snap(viewport_axis(p[1], h)),
)
}
#[inline]
fn window(p: [f32; 2], w: f32, h: f32) -> Option<(i64, i64)> {
let x = viewport_axis(p[0], w);
let y = viewport_axis(p[1], h);
if !(x.is_finite() && y.is_finite())
|| (x - w * 0.5).abs() > GUARD_PX
|| (y - h * 0.5).abs() > GUARD_PX
{
return None;
}
Some(snap_pos(p, w, h))
}
fn window_segment(a: [f32; 2], b: [f32; 2], w: f32, h: f32) -> Option<([i64; 4], [f32; 2], bool)> {
let win = |p: [f32; 2]| {
(
(f64::from(p[0]) * 0.5 + 0.5) * f64::from(w),
(0.5 - f64::from(p[1]) * 0.5) * f64::from(h),
)
};
let ((x0, y0), (x1, y1)) = (win(a), win(b));
if !(x0.is_finite() && y0.is_finite() && x1.is_finite() && y1.is_finite()) {
return None;
}
let g = f64::from(GUARD_PX);
let (cx, cy) = (f64::from(w) * 0.5, f64::from(h) * 0.5);
if [x0 - cx, y0 - cy, x1 - cx, y1 - cy]
.iter()
.all(|v| v.abs() <= g)
{
let (p, q) = (snap_pos(a, w, h), snap_pos(b, w, h));
return Some(([p.0, p.1, q.0, q.1], [0.0, 1.0], false));
}
let (dx, dy) = (x1 - x0, y1 - y0);
let (mut t0, mut t1) = (0.0f64, 1.0f64);
for (p, q) in [
(-dx, x0 - cx + g),
(dx, g - (x0 - cx)),
(-dy, y0 - cy + g),
(dy, g - (y0 - cy)),
] {
if p == 0.0 {
if q < 0.0 {
return None;
}
} else {
let r = q / p;
if p < 0.0 {
t0 = t0.max(r);
} else {
t1 = t1.min(r);
}
}
}
if t0 > t1 {
return None;
}
let s = |v: f64| (v * SUB as f64).round_ties_even() as i64;
Some((
[
s(x0 + t0 * dx),
s(y0 + t0 * dy),
s(x0 + t1 * dx),
s(y0 + t1 * dy),
],
[t0 as f32, t1 as f32],
true,
))
}
#[inline]
fn div_floor(a: i64, b: i64) -> i64 {
let q = a / b;
if a % b != 0 && ((a < 0) != (b < 0)) {
q - 1
} else {
q
}
}
#[inline]
fn div_ceil(a: i64, b: i64) -> i64 {
-div_floor(-a, b)
}
#[inline]
fn pixel_vertex(i: i64, j: i64, w: u32, h: u32, color: [f32; 4]) -> Option<WfVertex> {
if i < 0 || j < 0 || i >= i64::from(w) || j >= i64::from(h) {
return None;
}
Some(WfVertex {
pos: [
(i as f32 + 0.5) / w as f32 * 2.0 - 1.0,
1.0 - (j as f32 + 0.5) / h as f32 * 2.0,
],
color,
})
}
#[inline]
fn lerp_color(
c0: [f32; 4],
c1: [f32; 4],
x0: i64,
y0: i64,
x1: i64,
y1: i64,
px: i64,
py: i64,
) -> [f32; 4] {
if c0 == c1 {
return c0;
}
let (dx, dy) = ((x1 - x0) as f64, (y1 - y0) as f64);
let t = (((px - x0) as f64) * dx + ((py - y0) as f64) * dy) / (dx * dx + dy * dy);
let t = t as f32;
let mut c = [0.0; 4];
for k in 0..4 {
c[k] = (c0[k] + (c1[k] - c0[k]) * t).clamp(0.0, 1.0);
}
c
}
fn strip_segment(
x0: i64,
y0: i64,
x1: i64,
y1: i64,
w: i64,
h: i64,
mut emit: impl FnMut(i64, i64),
) {
let (dx, dy) = (x1 - x0, y1 - y0);
if dx == 0 && dy == 0 {
return;
}
if dx.abs() >= dy.abs() {
let (lo, hi) = if x0 < x1 { (x0, x1) } else { (x1, x0) };
let i0 = div_ceil(lo - HALF, SUB).max(0);
let i1 = (div_ceil(hi - HALF, SUB) - 1).min(w - 1);
let rising = dy != 0 && (dx > 0) != (dy > 0);
for i in i0..=i1 {
let c = i * SUB + HALF;
let num = y0 * dx + (c - x0) * dy;
let den = dx * SUB;
let j = if rising {
div_ceil(num, den) - 1
} else {
div_floor(num, den)
};
if (0..h).contains(&j) {
emit(i, j);
}
}
} else {
let (lo, hi) = if y0 < y1 { (y0, y1) } else { (y1, y0) };
let j0 = (div_floor(lo - HALF, SUB) + 1).max(0);
let j1 = div_floor(hi - HALF, SUB).min(h - 1);
for j in j0..=j1 {
let c = j * SUB + HALF;
let num = x0 * dy + (c - y0) * dx;
let i = div_ceil(num, dy * SUB) - 1;
if (0..w).contains(&i) {
emit(i, j);
}
}
}
}
#[derive(Clone, Copy)]
struct Frac {
n: i128,
d: i128,
}
impl Frac {
fn new(n: i64, d: i64) -> Frac {
debug_assert!(d != 0);
if d > 0 {
Frac {
n: n as i128,
d: d as i128,
}
} else {
Frac {
n: -(n as i128),
d: -(d as i128),
}
}
}
fn cmp(&self, o: &Frac) -> Ordering {
(self.n * o.d).cmp(&(o.n * self.d))
}
fn lt(&self, o: &Frac) -> bool {
self.cmp(o) == Ordering::Less
}
fn gt(&self, o: &Frac) -> bool {
self.cmp(o) == Ordering::Greater
}
}
#[inline]
fn in_diamond(ox: i64, oy: i64) -> bool {
let d = ox.abs() + oy.abs();
d < HALF || (d == HALF && (oy < 0 || (oy == 0 && ox > 0)))
}
fn diamond_lit(x0: i64, y0: i64, x1: i64, y1: i64, i: i64, j: i64) -> bool {
let (dx, dy) = (x1 - x0, y1 - y0);
let cx = i * SUB + HALF;
let cy = j * SUB + HALF;
let mut lo: Option<Frac> = None;
let mut hi: Option<Frac> = None;
let mut edge: Option<(i64, i64)> = None; for (s1, s2) in [(1i64, 1i64), (1, -1), (-1, 1), (-1, -1)] {
let a = s1 * dx + s2 * dy;
let b = HALF - (s1 * (x0 - cx) + s2 * (y0 - cy));
if a == 0 {
if b < 0 {
return false;
}
if b == 0 {
edge = Some((s1, s2));
}
} else {
let t = Frac::new(b, a);
if a > 0 {
if hi.is_none_or(|h| t.lt(&h)) {
hi = Some(t);
}
} else if lo.is_none_or(|l| t.gt(&l)) {
lo = Some(t);
}
}
}
let (Some(lo), Some(hi)) = (lo, hi) else {
return false; };
if lo.gt(&hi) {
return false;
}
let zero = Frac { n: 0, d: 1 };
let one = Frac { n: 1, d: 1 };
let tlo = if lo.gt(&zero) { lo } else { zero };
let thi = if hi.lt(&one) { hi } else { one };
if tlo.gt(&thi) {
return false;
}
let intersects = if let Some((s1, s2)) = edge {
(s1, s2) == (-1, -1) || (s1, s2) == (1, -1)
} else if lo.lt(&hi) && hi.gt(&zero) && lo.lt(&one) {
true } else {
let ex = (x0 - cx) as i128 * tlo.d + tlo.n * dx as i128; let ey = (y0 - cy) as i128 * tlo.d + tlo.n * dy as i128;
let on_boundary = ex.abs() + ey.abs() == HALF as i128 * tlo.d;
on_boundary && (ey < 0 || (ey == 0 && ex > 0))
};
intersects && !in_diamond(x1 - cx, y1 - cy)
}
fn diamond_segment(
x0: i64,
y0: i64,
x1: i64,
y1: i64,
w: i64,
h: i64,
mut emit: impl FnMut(i64, i64),
) {
let (dx, dy) = (x1 - x0, y1 - y0);
if dx == 0 && dy == 0 {
return;
}
let (xlo, xhi) = (x0.min(x1), x0.max(x1));
let (ylo, yhi) = (y0.min(y1), y0.max(y1));
if dx.abs() >= dy.abs() {
let i0 = (div_floor(xlo, SUB) - 1).max(-1);
let i1 = (div_floor(xhi, SUB) + 1).min(w);
for i in i0..=i1 {
let xa = (i * SUB).clamp(xlo, xhi);
let xb = ((i + 1) * SUB).clamp(xlo, xhi);
let ya = div_floor(y0 * dx + (xa - x0) * dy, dx);
let yb = div_floor(y0 * dx + (xb - x0) * dy, dx);
let (ja, jb) = (ya.min(yb), ya.max(yb));
for j in (div_floor(ja, SUB) - 1).max(-1)..=(div_floor(jb, SUB) + 1).min(h) {
if (0..w).contains(&i) && (0..h).contains(&j) && diamond_lit(x0, y0, x1, y1, i, j) {
emit(i, j);
}
}
}
} else {
let j0 = (div_floor(ylo, SUB) - 1).max(-1);
let j1 = (div_floor(yhi, SUB) + 1).min(h);
for j in j0..=j1 {
let ya = (j * SUB).clamp(ylo, yhi);
let yb = ((j + 1) * SUB).clamp(ylo, yhi);
let xa = div_floor(x0 * dy + (ya - y0) * dx, dy);
let xb = div_floor(x0 * dy + (yb - y0) * dx, dy);
let (ia, ib) = (xa.min(xb), xa.max(xb));
for i in (div_floor(ia, SUB) - 1).max(-1)..=(div_floor(ib, SUB) + 1).min(w) {
if (0..w).contains(&i) && (0..h).contains(&j) && diamond_lit(x0, y0, x1, y1, i, j) {
emit(i, j);
}
}
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LineRule {
Strip,
Diamond,
}
pub fn raster_segments(segs: &[WfVertex], w: u32, h: u32, rule: LineRule, out: &mut Vec<WfVertex>) {
let (wf, hf) = (w as f32, h as f32);
for s in segs.as_chunks::<2>().0 {
let Some(([mut x0, mut y0, mut x1, mut y1], [t0, t1], clipped)) =
window_segment(s[0].pos, s[1].pos, wf, hf)
else {
continue;
};
let lerp = |t: f32| {
std::array::from_fn::<f32, 4, _>(|k| {
s[0].color[k] + (s[1].color[k] - s[0].color[k]) * t
})
};
let (mut c0, mut c1) = if t0 == 0.0 && t1 == 1.0 {
(s[0].color, s[1].color)
} else {
(lerp(t0), lerp(t1))
};
if clipped && matches!(rule, LineRule::Diamond) {
std::mem::swap(&mut x0, &mut x1);
std::mem::swap(&mut y0, &mut y1);
std::mem::swap(&mut c0, &mut c1);
}
let mut emit = |i: i64, j: i64| {
let col = lerp_color(c0, c1, x0, y0, x1, y1, i * SUB + HALF, j * SUB + HALF);
if let Some(v) = pixel_vertex(i, j, w, h, col) {
out.push(v);
}
};
match rule {
LineRule::Strip => strip_segment(x0, y0, x1, y1, i64::from(w), i64::from(h), &mut emit),
LineRule::Diamond => {
diamond_segment(x0, y0, x1, y1, i64::from(w), i64::from(h), &mut emit)
}
}
}
}
pub fn raster_points(pts: &[WfVertex], w: u32, h: u32, out: &mut Vec<WfVertex>) {
let (wf, hf) = (w as f32, h as f32);
for p in pts {
let Some((x, y)) = window(p.pos, wf, hf) else {
continue;
};
if let Some(v) = pixel_vertex(div_floor(x, SUB), div_floor(y, SUB), w, h, p.color) {
out.push(v);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const W: u32 = 64;
const H: u32 = 64;
fn clip(x: f32, y: f32) -> [f32; 2] {
[x / W as f32 * 2.0 - 1.0, 1.0 - y / H as f32 * 2.0]
}
fn seg(a: (f32, f32), b: (f32, f32)) -> Vec<WfVertex> {
let c = [1.0; 4];
vec![
WfVertex {
pos: clip(a.0, a.1),
color: c,
},
WfVertex {
pos: clip(b.0, b.1),
color: c,
},
]
}
fn pixels(v: &[WfVertex]) -> Vec<(i64, i64)> {
let mut p: Vec<(i64, i64)> = v
.iter()
.map(|v| {
(
((v.pos[0] * 0.5 + 0.5) * W as f32).floor() as i64,
((0.5 - v.pos[1] * 0.5) * H as f32).floor() as i64,
)
})
.collect();
p.sort_unstable();
p
}
fn run(rule: LineRule, a: (f32, f32), b: (f32, f32)) -> Vec<(i64, i64)> {
let mut out = Vec::new();
raster_segments(&seg(a, b), W, H, rule, &mut out);
pixels(&out)
}
#[test]
fn points_are_floor_of_snapped_position() {
let mut out = Vec::new();
let pts: Vec<WfVertex> = [(10.0, 10.0), (10.99, 3.5), (0.0, 0.0), (63.99, 63.5)]
.iter()
.map(|&(x, y)| WfVertex {
pos: clip(x, y),
color: [1.0; 4],
})
.collect();
raster_points(&pts, W, H, &mut out);
assert_eq!(pixels(&out), vec![(0, 0), (10, 3), (10, 10), (63, 63)]);
}
#[test]
fn strip_minor_axis_tie_goes_to_the_higher_row() {
let mut out = Vec::new();
let c = [1.0; 4];
let clip2 = |x: f32, y: f32| [x / 64.0 * 2.0 - 1.0, 1.0 - y / 128.0 * 2.0];
let s = vec![
WfVertex {
pos: clip2(10.0, 65.0),
color: c,
},
WfVertex {
pos: clip2(20.0, 65.0),
color: c,
},
];
raster_segments(&s, 64, 128, LineRule::Strip, &mut out);
let rows: Vec<i64> = out
.iter()
.map(|v| ((0.5 - v.pos[1] * 0.5) * 128.0).floor() as i64)
.collect();
assert!(rows.iter().all(|&r| r == 65), "{rows:?}");
assert_eq!(rows.len(), 10); }
fn strip320(a: (f32, f32), b: (f32, f32)) -> Vec<(i64, i64)> {
let c = [1.0; 4];
let clip = |p: (f32, f32)| [p.0 / 320.0 * 2.0 - 1.0, 1.0 - p.1 / 180.0 * 2.0];
let mut out = Vec::new();
raster_segments(
&[
WfVertex {
pos: clip(a),
color: c,
},
WfVertex {
pos: clip(b),
color: c,
},
],
320,
180,
LineRule::Strip,
&mut out,
);
let mut p: Vec<(i64, i64)> = out
.iter()
.map(|v| {
(
((v.pos[0] * 0.5 + 0.5) * 320.0).floor() as i64,
((0.5 - v.pos[1] * 0.5) * 180.0).floor() as i64,
)
})
.collect();
p.sort_unstable();
p
}
#[test]
fn strip_x_major_tie_depends_on_the_slope_sign() {
let (p0, p1) = ((219.3984375, 92.00390625), (221.6015625, 91.99609375)); let rising = vec![(219, 92), (220, 91), (221, 91)];
assert_eq!(strip320(p0, p1), rising);
assert_eq!(strip320(p1, p0), rising);
let (q0, q1) = ((219.3984375, 91.99609375), (221.6015625, 92.00390625));
let falling = vec![(219, 91), (220, 92), (221, 92)];
assert_eq!(strip320(q0, q1), falling);
assert_eq!(strip320(q1, q0), falling);
}
#[test]
fn strip_y_major_tie_goes_to_the_left_column() {
assert_eq!(strip320((0.0, 10.25), (0.0, 13.75)), vec![]);
assert_eq!(
strip320((320.0, 10.25), (320.0, 12.75)),
vec![(319, 10), (319, 11), (319, 12)]
);
assert_eq!(
strip320((160.0, 10.25), (160.0, 12.75)),
vec![(159, 10), (159, 11), (159, 12)]
);
assert_eq!(
strip320((159.5, 10.5), (160.5, 12.5)),
vec![(159, 11), (160, 12)]
);
}
#[test]
fn strip_extents_are_half_open_per_axis() {
assert_eq!(
run(LineRule::Strip, (10.5, 5.5), (13.5, 5.5)),
vec![(10, 5), (11, 5), (12, 5)]
);
assert_eq!(
run(LineRule::Strip, (30.5, 5.5), (30.5, 8.5)),
vec![(30, 6), (30, 7), (30, 8)]
);
assert_eq!(
run(LineRule::Strip, (10.75, 5.5), (13.5, 5.5)),
vec![(11, 5), (12, 5)]
);
}
#[test]
fn diamond_includes_the_start_pixel_behind_the_centre() {
let (a, b) = ((2.53125, 7.62109375), (5.078125, 6.18359375));
assert_eq!(run(LineRule::Diamond, a, b), vec![(2, 7), (3, 7), (4, 6)]);
assert_eq!(run(LineRule::Strip, a, b), vec![(3, 7), (4, 6)]);
}
#[test]
fn diamond_end_on_boundary_is_lit_only_on_the_lower_edges() {
assert_eq!(
run(LineRule::Diamond, (8.0, 10.75), (10.75, 10.75)),
vec![(8, 10), (9, 10), (10, 10)]
);
assert_eq!(
run(LineRule::Diamond, (8.0, 10.25), (10.75, 10.25)),
vec![(8, 10), (9, 10)]
);
}
#[test]
fn diamond_start_on_upper_edge_leaving_is_lit() {
let r = run(LineRule::Diamond, (10.25, 10.25), (6.0, 6.0));
assert!(r.contains(&(10, 10)), "{r:?}");
let r = run(LineRule::Diamond, (10.25, 10.75), (6.0, 14.0));
assert!(!r.contains(&(10, 10)), "{r:?}");
}
#[test]
fn diamond_45_degree_line_along_a_diamond_edge() {
let r = run(LineRule::Diamond, (9.0, 8.5), (14.0, 13.5));
assert!(r.contains(&(10, 10)), "{r:?}");
let r = run(LineRule::Diamond, (9.0, 9.5), (14.0, 14.5));
assert!(!r.contains(&(10, 10)), "{r:?}");
}
#[test]
fn colours_interpolate_along_the_segment() {
let mut out = Vec::new();
let v = vec![
WfVertex {
pos: clip(0.5, 5.5),
color: [0.0, 0.0, 0.0, 1.0],
},
WfVertex {
pos: clip(10.5, 5.5),
color: [1.0, 0.5, 0.0, 1.0],
},
];
raster_segments(&v, W, H, LineRule::Strip, &mut out);
assert_eq!(out.len(), 10);
assert!(out[0].color[0] < 0.06 && out[9].color[0] > 0.89);
assert!((out[5].color[0] - 0.5).abs() < 0.06 && (out[5].color[1] - 0.25).abs() < 0.03);
}
#[test]
fn long_and_far_away_segments_do_not_stall() {
let mut out = Vec::new();
let v = vec![
WfVertex {
pos: [-1.0e4, 0.3],
color: [1.0; 4],
},
WfVertex {
pos: [1.0e4, -0.2],
color: [1.0; 4],
},
];
raster_segments(&v, W, H, LineRule::Strip, &mut out);
raster_segments(&v, W, H, LineRule::Diamond, &mut out);
assert!(!out.is_empty());
let v = vec![
WfVertex {
pos: [f32::NAN, 0.0],
color: [1.0; 4],
},
WfVertex {
pos: [1.0, 1.0e30],
color: [1.0; 4],
},
];
let n = out.len();
raster_segments(&v, W, H, LineRule::Diamond, &mut out);
assert_eq!(out.len(), n);
}
#[test]
fn segments_beyond_the_guard_band_are_clipped_not_dropped() {
let v = vec![
WfVertex {
pos: clip(32.5, 10.5),
color: [1.0; 4],
},
WfVertex {
pos: clip(32.5, 1.0e7),
color: [1.0; 4],
},
];
for rule in [LineRule::Strip, LineRule::Diamond] {
let mut out = Vec::new();
raster_segments(&v, W, H, rule, &mut out);
let mut p = pixels(&out);
p.retain(|&(x, _)| x == 32);
let (n, first) = (53, 11);
assert_eq!(p.len(), n, "{rule:?} {p:?}");
assert_eq!(p.first(), Some(&(32, first)));
}
let far = vec![
WfVertex {
pos: clip(32.5, 2.0e4),
color: [1.0; 4],
},
WfVertex {
pos: clip(40.5, 1.0e7),
color: [1.0; 4],
},
];
let mut out = Vec::new();
raster_segments(&far, W, H, LineRule::Diamond, &mut out);
assert!(out.is_empty());
}
#[test]
fn guard_band_is_centred_on_the_viewport() {
let inside = vec![
WfVertex {
pos: clip(32.5, 10.5),
color: [1.0; 4],
},
WfVertex {
pos: clip(32.5 + 16300.0, 10.5),
color: [1.0; 4],
},
];
let mut out = Vec::new();
raster_segments(&inside, W, H, LineRule::Diamond, &mut out);
assert!(pixels(&out).contains(&(32, 10)));
let beyond = vec![
WfVertex {
pos: clip(32.5, 10.5),
color: [1.0; 4],
},
WfVertex {
pos: clip(32.5 + 16400.0, 10.5),
color: [1.0; 4],
},
];
let mut out = Vec::new();
raster_segments(&beyond, W, H, LineRule::Diamond, &mut out);
assert!(!pixels(&out).contains(&(32, 10)));
assert!(pixels(&out).contains(&(33, 10)));
}
#[test]
fn clipped_diamond_line_ending_inside_lights_its_end_pixel() {
let v = vec![
WfVertex {
pos: clip(40.5 - 1.0e5, 20.5),
color: [1.0; 4],
},
WfVertex {
pos: clip(40.5, 20.5),
color: [1.0; 4],
},
];
let mut out = Vec::new();
raster_segments(&v, W, H, LineRule::Diamond, &mut out);
assert!(pixels(&out).contains(&(40, 20)));
let u = vec![
WfVertex {
pos: clip(10.5, 20.5),
color: [1.0; 4],
},
WfVertex {
pos: clip(40.5, 20.5),
color: [1.0; 4],
},
];
let mut out = Vec::new();
raster_segments(&u, W, H, LineRule::Diamond, &mut out);
assert!(!pixels(&out).contains(&(40, 20)));
}
#[test]
fn viewport_snap_matches_the_gpu_at_pixel_boundaries() {
let x: [(u32, i64); 14] = [
(0xbf74cc00, 6),
(0xbf74cbff, 7),
(0xbf6198cd, 18),
(0xbf6198cc, 19),
(0xbf44cc00, 36),
(0xbf44cbff, 37),
(0xbf14cc00, 66),
(0xbf14cbff, 67),
(0xbebffe65, 99),
(0xbebffe64, 100),
(0xbd7ff31a, 149),
(0xbd7ff319, 150),
(0x3d8cd33f, 170),
(0x3d8cd340, 171),
];
for (bits, col) in x {
let (sx, _) = snap_pos([f32::from_bits(bits), 0.0], 320.0, 180.0);
assert_eq!(div_ceil(sx, SUB) - 1, col, "x {bits:08x}");
}
let y: [(u32, i64); 14] = [
(0xbf6c1556, 6),
(0xbf6c1555, 7),
(0xbf49f333, 18),
(0xbf49f332, 19),
(0xbf16c000, 36),
(0xbf16bfff, 37),
(0xbe82d553, 66),
(0xbe82d552, 67),
(0x3de399a4, 99),
(0x3de399a5, 100),
(0x3f2aac19, 149),
(0x3f2aac1a, 150),
(0x3f6667d5, 170),
(0x3f6667d6, 171),
];
for (bits, row_gl) in y {
let (_, sy) = snap_pos([0.0, f32::from_bits(bits)], 320.0, 180.0);
assert_eq!(div_floor(sy, SUB), 179 - row_gl, "y {bits:08x}");
}
}
#[test]
fn motion_vector_tip_tie_uses_the_gpu_snap() {
let v = vec![
WfVertex {
pos: [f32::from_bits(0xbf0d0cd8), f32::from_bits(0x3f30d9b6)],
color: [1.0; 4],
},
WfVertex {
pos: [f32::from_bits(0xbf916eaf), f32::from_bits(0x3eff5830)],
color: [1.0; 4],
},
];
let mut out = Vec::new();
raster_segments(&v, 320, 180, LineRule::Diamond, &mut out);
let p: Vec<(i64, i64)> = out
.iter()
.map(|v| {
(
((v.pos[0] * 0.5 + 0.5) * 320.0).floor() as i64,
((0.5 - v.pos[1] * 0.5) * 180.0).floor() as i64,
)
})
.collect();
assert!(p.contains(&(0, 41)) && !p.contains(&(0, 40)), "{p:?}");
}
}