mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-22 07:20:57 +01:00
Merge branch 'fix/graph-delete-and-sample-overflow' of https://github.com/SteamSmith/materialcraft into review/pr-73
This commit is contained in:
commit
3dd0705b54
3 files changed
+46
No files matched your search
@@ -8,6 +8,10 @@ use materialcraft_core::{Comment, ExposedParam, Frame, MAX_SIZE_LOG2, MIN_SIZE_L
|
||||
use crate::commands::{CommandSpec, always, bad, boolp, f32p, has_graph, has_selection, i64p, ids_param, point, strp, u64p};
|
||||
use crate::{Result, Session, cmd};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "graph_cmds.test.rs"]
|
||||
mod tests;
|
||||
|
||||
pub(crate) fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
cmd!("graph.open", "Open graph", [], None, "{graph: index or name}", always, open),
|
||||
@@ -124,6 +128,10 @@ fn delete(s: &mut Session, p: &J) -> Result<J> {
|
||||
let i = graph_index(s, p)?;
|
||||
s.edit("Delete graph", |s| {
|
||||
s.pkg.graphs.remove(i);
|
||||
// Removing a graph before the open one shifts it down; keep the same graph open.
|
||||
if i < s.graph {
|
||||
s.graph -= 1;
|
||||
}
|
||||
s.graph = s.graph.min(s.pkg.graphs.len().saturating_sub(1));
|
||||
s.selection.clear();
|
||||
s.view2d.node = None;
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#[cfg(test)]
|
||||
mod delete_tests {
|
||||
use crate::{execute, sample_session};
|
||||
use serde_json::json;
|
||||
|
||||
/// Deleting a graph that sits before the open one must not switch the user to another graph.
|
||||
#[test]
|
||||
fn deleting_an_earlier_graph_keeps_the_open_graph_open() {
|
||||
let mut s = sample_session();
|
||||
let mut run = |s: &mut crate::Session, id: &str, p: serde_json::Value| {
|
||||
execute(s, id, &p).unwrap_or_else(|e| panic!("{id}: {e}"))
|
||||
};
|
||||
run(&mut s, "file.new", json!({"template": "empty", "name": "Mine"}));
|
||||
for _ in 0..3 {
|
||||
run(&mut s, "graph.duplicate", json!({}));
|
||||
}
|
||||
let count = s.pkg.graphs.len();
|
||||
assert!(count >= 4, "need four graphs, got {count}");
|
||||
run(&mut s, "graph.open", json!({"graph": 2}));
|
||||
let open_name = s.pkg.graphs[s.graph].name.clone();
|
||||
run(&mut s, "graph.delete", json!({"graph": 0}));
|
||||
assert_eq!(s.pkg.graphs.len(), count - 1);
|
||||
assert_eq!(s.pkg.graphs[s.graph].name, open_name, "the open graph changed");
|
||||
}
|
||||
}
|
||||
@@ -140,6 +140,8 @@ impl Image {
|
||||
if !fx.is_finite() || !fy.is_finite() {
|
||||
return [0.0, 0.0, 0.0, 1.0];
|
||||
}
|
||||
// Huge but finite coordinates would saturate the i64 cast and overflow at `+ 1`.
|
||||
let (fx, fy) = (fx.clamp(-1.0e9, 1.0e9), fy.clamp(-1.0e9, 1.0e9));
|
||||
let (x0, y0) = (fx.floor(), fy.floor());
|
||||
let (tx, ty) = (fx - x0, fy - y0);
|
||||
let (x0, y0) = (x0 as i64, y0 as i64);
|
||||
@@ -304,6 +306,17 @@ pub fn srgb_to_linear(v: f32) -> f32 {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn sample_survives_huge_coordinates() {
|
||||
let img = Image::gray(8, 8, |x, _| x as f32);
|
||||
for u in [1.0e30_f32, -1.0e30, f32::MAX, f32::MIN] {
|
||||
for tile in [true, false] {
|
||||
let p = img.sample(u, u, tile);
|
||||
assert!(p.iter().all(|c| c.is_finite()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sample_wraps_and_clamps() {
|
||||
let img = Image::gray(4, 1, |x, _| x as f32);
|
||||
|
||||
Reference in new issue
Block a user