mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
Merge pull request #4406 from dotCooCoo/fix/raster3d-crossing-tie
raster3d: a clipped edge crosses at the same corner in both directions when its distances tie
This commit is contained in:
1 file changed
+20
-2
@@ -140,8 +140,16 @@ fn clip_wide(poly: &[Corner], planes: &[usize]) -> Vec<WideCorner> {
|
||||
|
||||
/// Where the edge from `a` (plane distance `da`) to `b` (`db`) crosses the plane, interpolated in
|
||||
/// `f64` from the corner nearer the plane, so both directions of an edge give the same corner.
|
||||
/// When both corners are equally near, the interpolation starts from the corner that sorts first
|
||||
/// by its coordinates and then its weights.
|
||||
fn crossing_f64(a: ([f64; 4], [f64; 3]), b: ([f64; 4], [f64; 3]), da: f64, db: f64) -> ([f64; 4], [f64; 3]) {
|
||||
let (p, q, t) = if da.abs() <= db.abs() { (a, b, da / (da - db)) } else { (b, a, db / (db - da)) };
|
||||
let a_first = if da.abs() == db.abs() {
|
||||
let order = a.0.iter().chain(&a.1).zip(b.0.iter().chain(&b.1)).map(|(x, y)| x.total_cmp(y)).find(|o| o.is_ne());
|
||||
order.is_none_or(std::cmp::Ordering::is_lt)
|
||||
} else {
|
||||
da.abs() < db.abs()
|
||||
};
|
||||
let (p, q, t) = if a_first { (a, b, da / (da - db)) } else { (b, a, db / (db - da)) };
|
||||
let c: [f64; 4] = std::array::from_fn(|k| p.0[k] + (q.0[k] - p.0[k]) * t);
|
||||
let w: [f64; 3] = std::array::from_fn(|k| p.1[k] + (q.1[k] - p.1[k]) * t);
|
||||
(c, w)
|
||||
@@ -758,7 +766,7 @@ fn render(s: &Scene, limits: &Limits, #[cfg(test)] fallback_tests: &AtomicU64) -
|
||||
mod tests {
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
|
||||
use super::{cover, f32_crossing, fan, project, render, try_render};
|
||||
use super::{cover, crossing_f64, f32_crossing, fan, project, render, try_render};
|
||||
use crate::scene::{Material, Scene, Vertex};
|
||||
use crate::{Limits, RenderError};
|
||||
|
||||
@@ -836,4 +844,14 @@ mod tests {
|
||||
assert_eq!(f32_crossing(-3e38, 1e38), (false, 0.75));
|
||||
assert_eq!(f32_crossing(1e38, -3e38), (false, 0.25));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn both_directions_of_an_edge_cross_at_the_same_corner_when_the_distances_tie() {
|
||||
let a = ([3.0, 0.1, 0.5, 1.0], [1.0, 0.0, 0.0]);
|
||||
let b = ([2f64.powi(54), 0.7, 0.5, 1.0], [0.0, 0.3, 0.7]);
|
||||
let ab = crossing_f64(a, b, 1.0, -1.0);
|
||||
let ba = crossing_f64(b, a, -1.0, 1.0);
|
||||
assert_eq!(ab.0.map(f64::to_bits), ba.0.map(f64::to_bits));
|
||||
assert_eq!(ab.1.map(f64::to_bits), ba.1.map(f64::to_bits));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user