use super::*;
use crate::OwnedData;
use nsi_trait::Type;
fn strength(value: i32) -> OwnedArgument {
OwnedArgument {
name: "strength".to_string(),
..priority(value)
}
}
fn priority(value: i32) -> OwnedArgument {
OwnedArgument {
name: "priority".to_string(),
type_tag: Type::I32,
array_length: 1,
flags: 0,
data: OwnedData::I32(vec![value]),
}
}
fn arg(name: &str, value: f32) -> OwnedArgument {
OwnedArgument {
name: name.to_string(),
type_tag: Type::F32,
array_length: 1,
flags: 0,
data: OwnedData::F32(vec![value]),
}
}
#[test]
fn creates_and_finds_a_node() {
let mut scene = Scene::default();
scene.create("cam", "perspectivecamera").unwrap();
assert_eq!(scene.node("cam").unwrap().node_type(), "perspectivecamera");
}
#[test]
fn set_attribute_overwrites_by_name() {
let mut scene = Scene::default();
scene.create("cam", "perspectivecamera").unwrap();
scene.set_attribute("cam", vec![arg("fov", 45.0)]).unwrap();
scene.set_attribute("cam", vec![arg("fov", 60.0)]).unwrap();
assert_eq!(scene.node("cam").unwrap().attributes().count(), 1);
assert_eq!(
scene.node("cam").unwrap().attribute("fov").unwrap().data,
OwnedData::F32(vec![60.0])
);
}
#[test]
fn time_samples_are_kept_separately_and_sorted() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
scene
.set_attribute_at_time("xf", 1.0, vec![arg("t", 1.0)])
.unwrap();
scene
.set_attribute_at_time("xf", 0.0, vec![arg("t", 0.0)])
.unwrap();
assert_eq!(
scene.attribute_times("xf", "t").unwrap(),
vec![0.0, 1.0],
"recorded later-time-first and reported in time order",
);
assert_eq!(
scene
.node("xf")
.unwrap()
.sample_calls("t")
.unwrap()
.iter()
.map(|(time, _)| *time)
.collect::<Vec<_>>(),
vec![1.0, 0.0],
"while the log keeps the order they were set in",
);
assert_eq!(scene.node("xf").unwrap().attributes().count(), 0);
}
#[test]
fn delete_removes_the_node_and_its_edges() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
scene.create("mesh", "mesh").unwrap();
scene
.connect("mesh", None, "xf", "objects")
.expect("known attribute");
scene.delete("xf").unwrap();
assert!(scene.node("xf").is_none());
assert!(scene.edges.is_empty());
}
#[test]
fn delete_attribute_removes_one_key() {
let mut scene = Scene::default();
scene.create("cam", "perspectivecamera").unwrap();
scene
.set_attribute("cam", vec![arg("fov", 45.0), arg("fs", 1.0)])
.unwrap();
scene.delete_attribute("cam", "fov");
assert!(scene.node("cam").unwrap().attribute("fov").is_none());
assert!(scene.node("cam").unwrap().attribute("fs").is_some());
}
#[test]
fn node_order_is_insertion_order() {
let mut scene = Scene::default();
for handle in ["z", "a", "m"] {
scene.create(handle, "transform").unwrap();
}
let order: Vec<&str> = scene.nodes().map(|(handle, _)| handle).collect();
assert_eq!(order, vec!["z", "a", "m"]);
}
#[test]
fn connect_carries_an_unlisted_destination() {
let mut scene = Scene::default();
scene.create("a", "transform").unwrap();
scene.create("b", "transform").unwrap();
scene.connect("a", None, "b", "nonsense").unwrap();
assert_eq!(
scene.edges[0].kind,
EdgeKind::Other {
to_attribute: "nonsense".to_string()
}
);
}
#[test]
fn delete_attribute_removes_from_every_time_sample() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
scene.set_attribute("xf", vec![arg("t", 9.0)]).unwrap();
scene
.set_attribute_at_time("xf", 0.0, vec![arg("t", 0.0)])
.unwrap();
scene
.set_attribute_at_time("xf", 1.0, vec![arg("t", 1.0), arg("keep", 2.0)])
.unwrap();
scene.delete_attribute("xf", "t");
let node = &scene.node("xf").unwrap();
assert!(node.attribute("t").is_none(), "static copy removed");
assert!(node.sample_calls("t").is_none(), "every sample of it too");
assert!(node.sample_calls("keep").is_some());
}
#[test]
fn disconnect_removes_only_the_named_edge() {
let mut scene = Scene::default();
for handle in ["a", "b"] {
scene.create(handle, "transform").unwrap();
scene.connect(handle, None, ".root", "objects").unwrap();
}
scene.disconnect("a", None, ".root", "objects").unwrap();
assert_eq!(scene.edges.len(), 1);
assert_eq!(scene.edges[0].from, "b");
}
#[test]
fn disconnect_removes_an_unlisted_destination() {
let mut scene = Scene::default();
scene.create("a", "transform").unwrap();
scene.create("b", "transform").unwrap();
scene.connect("a", None, "b", "nonsense").unwrap();
scene.disconnect("a", None, "b", "nonsense").unwrap();
assert!(scene.edges.is_empty());
}
#[test]
fn a_static_set_clears_the_motion_samples_of_that_name() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
scene
.set_attribute_at_time("xf", 0.0, vec![arg("t", 0.0)])
.unwrap();
scene
.set_attribute_at_time("xf", 1.0, vec![arg("t", 1.0), arg("keep", 9.0)])
.unwrap();
scene.set_attribute("xf", vec![arg("t", 5.0)]).unwrap();
let node = &scene.node("xf").unwrap();
assert_eq!(node.attribute("t").unwrap().data, OwnedData::F32(vec![5.0]));
assert!(node.sample_calls("t").is_none(), "its samples went with it");
assert!(
node.sample_calls("keep").is_some(),
"an unrelated sampled attribute is untouched"
);
}
#[test]
fn a_sampled_set_clears_the_static_value_of_that_name() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
scene
.set_attribute("xf", vec![arg("t", 5.0), arg("keep", 9.0)])
.unwrap();
scene
.set_attribute_at_time("xf", 0.0, vec![arg("t", 0.0)])
.unwrap();
let node = &scene.node("xf").unwrap();
assert!(node.attribute("t").is_none(), "static value replaced");
assert!(node.attribute("keep").is_some(), "others untouched");
}
#[test]
fn a_repeated_connect_updates_rather_than_duplicates() {
let mut scene = Scene::default();
scene.create("grp", "transform").unwrap();
scene.connect("grp", None, ".root", "objects").unwrap();
scene
.connect_with_arguments(
"grp",
None,
".root",
"objects",
vec![priority(4)],
)
.unwrap();
assert_eq!(scene.edges.len(), 1, "one edge, not two parents");
assert_eq!(scene.edges[0].priority(), 4, "arguments updated");
}
#[test]
fn disconnect_all_matches_every_source() {
let mut scene = Scene::default();
for handle in ["a", "b", "c", "other"] {
scene.create(handle, "transform").unwrap();
}
scene.connect("a", None, ".root", "objects").unwrap();
scene.connect("b", None, ".root", "objects").unwrap();
scene.connect("c", None, "other", "objects").unwrap();
scene
.disconnect(crate::ALL, None, ".root", "objects")
.unwrap();
assert_eq!(scene.edges.len(), 1);
assert_eq!(scene.edges[0].to, "other");
}
#[test]
fn disconnect_all_matches_destinations_and_attributes() {
let mut scene = Scene::default();
for handle in ["a", "b", "x", "y", "z"] {
scene.create(handle, "transform").unwrap();
}
scene.connect("a", None, "x", "objects").unwrap();
scene.connect("a", None, "y", "geometryattributes").unwrap();
scene.connect("b", None, "z", "objects").unwrap();
scene.disconnect("a", None, crate::ALL, crate::ALL).unwrap();
assert_eq!(scene.edges.len(), 1);
assert_eq!(scene.edges[0].from, "b");
}
#[test]
fn disconnect_with_an_all_attribute_is_not_a_classify_error() {
let mut scene = Scene::default();
scene.create("a", "screen").unwrap();
scene.create("x", "perspectivecamera").unwrap();
scene.connect("a", None, "x", "screens").unwrap();
assert!(scene.disconnect("a", None, "x", crate::ALL).is_ok());
assert!(scene.edges.is_empty());
}
#[test]
fn setting_an_attribute_on_an_uncreated_handle_is_an_error() {
let mut scene = Scene::default();
assert_eq!(
scene.set_attribute("ghost", vec![arg("fov", 45.0)]),
Err(RecordError::UnknownHandle {
handle: "ghost".to_string()
})
);
assert_eq!(
scene.set_attribute_at_time("ghost", 0.0, vec![arg("t", 1.0)]),
Err(RecordError::UnknownHandle {
handle: "ghost".to_string()
})
);
assert!(scene.node("ghost").is_none(), "nothing fabricated");
scene.create("real", "transform").unwrap();
assert!(scene.connect("real", None, "ghost", "objects").is_err());
}
#[test]
fn the_reserved_handles_take_attributes_without_a_create() {
let mut scene = Scene::default();
assert!(
scene
.set_attribute(crate::GLOBAL, vec![arg("quality", 1.0)])
.is_ok()
);
assert!(scene.node(crate::GLOBAL).is_some());
}
#[test]
fn a_recursive_delete_takes_the_network_with_it() {
let mut scene = Scene::default();
for handle in ["attr", "surface", "texture"] {
scene.create(handle, "shader").unwrap();
}
scene.create("mesh", "mesh").unwrap();
scene.connect("mesh", None, ".root", "objects").unwrap();
scene
.connect("attr", None, "mesh", "geometryattributes")
.unwrap();
scene
.connect("surface", None, "attr", "surfaceshader")
.unwrap();
scene
.connect("texture", Some("out"), "surface", "color")
.unwrap();
scene.delete_recursive("attr").unwrap();
assert!(scene.node("attr").is_none());
assert!(scene.node("surface").is_none(), "network followed");
assert!(scene.node("texture").is_none(), "and transitively");
assert!(scene.node("mesh").is_some(), "the geometry survives");
}
#[test]
fn a_recursive_delete_spares_a_node_used_elsewhere() {
let mut scene = Scene::default();
scene.create("shared", "shader").unwrap();
scene.create("a", "attributes").unwrap();
scene.create("b", "attributes").unwrap();
scene.connect("shared", None, "a", "surfaceshader").unwrap();
scene.connect("shared", None, "b", "surfaceshader").unwrap();
scene.delete_recursive("a").unwrap();
assert!(scene.node("a").is_none());
assert!(scene.node("shared").is_some(), "still feeds `b`");
assert!(scene.node("b").is_some());
}
#[test]
fn strength_blocks_a_recursive_delete() {
let mut scene = Scene::default();
scene.create("keep", "shader").unwrap();
scene.create("go", "shader").unwrap();
scene.create("attr", "attributes").unwrap();
scene
.connect_with_arguments(
"keep",
None,
"attr",
"surfaceshader",
vec![strength(1)],
)
.unwrap();
scene
.connect("go", None, "attr", "displacementshader")
.unwrap();
scene.delete_recursive("attr").unwrap();
assert!(scene.node("attr").is_none());
assert!(scene.node("keep").is_some(), "strength blocked it");
assert!(scene.node("go").is_none(), "the weak connection did not");
}
#[test]
fn strength_blocks_a_recursive_delete_through_a_second_path() {
let mut scene = Scene::default();
scene.create("attr", "attributes").unwrap();
scene.create("relay", "attributes").unwrap();
scene.create("keep", "shader").unwrap();
scene
.connect("relay", None, "attr", "surfaceshader")
.unwrap();
scene
.connect_with_arguments(
"keep",
None,
"attr",
"displacementshader",
vec![strength(1)],
)
.unwrap();
scene
.connect("keep", None, "relay", "surfaceshader")
.unwrap();
scene.delete_recursive("attr").unwrap();
assert!(scene.node("attr").is_none());
assert!(scene.node("relay").is_none(), "only fed the deleted node");
assert!(
scene.node("keep").is_some(),
"its strong connection spares it, whichever path found it"
);
}
#[test]
fn a_plain_delete_is_not_recursive() {
let mut scene = Scene::default();
scene.create("attr", "attributes").unwrap();
scene.create("shader", "shader").unwrap();
scene
.connect("shader", None, "attr", "surfaceshader")
.unwrap();
scene.delete("attr").unwrap();
assert!(scene.node("shader").is_some());
}
#[test]
fn a_recursive_delete_still_refuses_the_reserved_nodes() {
let mut scene = Scene::default();
assert!(scene.delete_recursive(crate::ROOT).is_err());
}
#[test]
fn connecting_an_uncreated_handle_is_an_error() {
let mut scene = Scene::default();
scene.create("real", "transform").unwrap();
assert_eq!(
scene.connect("ghost", None, "real", "objects"),
Err(RecordError::UnknownHandle {
handle: "ghost".to_string()
})
);
assert_eq!(
scene.connect("real", None, "ghost", "objects"),
Err(RecordError::UnknownHandle {
handle: "ghost".to_string()
})
);
assert!(scene.edges.is_empty(), "nothing recorded");
}
#[test]
fn the_reserved_handles_need_no_create() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
assert!(scene.connect("xf", None, crate::ROOT, "objects").is_ok());
assert!(scene.connect("xf", None, crate::GLOBAL, "objects").is_ok());
}
#[test]
fn disconnect_all_matches_every_source_attribute() {
let mut scene = Scene::default();
scene.create("s1", "shader").unwrap();
scene.create("s2", "shader").unwrap();
scene
.connect("s1", Some("outColor"), "s2", "inColor")
.unwrap();
scene
.connect("s1", Some("outAlpha"), "s2", "inColor")
.unwrap();
scene
.disconnect("s1", Some(crate::ALL), "s2", "inColor")
.unwrap();
assert!(scene.edges.is_empty(), "every source port matched");
}
#[test]
fn the_reserved_nodes_cannot_be_deleted() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
scene.connect("xf", None, crate::ROOT, "objects").unwrap();
assert_eq!(
scene.delete(crate::ROOT),
Err(RecordError::Reserved {
handle: crate::ROOT.to_string()
})
);
assert_eq!(scene.edges.len(), 1, "the scene is intact");
assert!(scene.delete(crate::GLOBAL).is_err());
}
#[test]
fn recreating_with_a_different_type_is_an_error() {
let mut scene = Scene::default();
scene.create("x", "mesh").unwrap();
assert_eq!(
scene.create("x", "transform"),
Err(RecordError::TypeMismatch {
handle: "x".to_string(),
existing: "mesh".to_string(),
requested: "transform".to_string(),
})
);
assert_eq!(
scene.node("x").unwrap().node_type(),
"mesh",
"type unchanged"
);
}
#[test]
fn recreating_with_the_same_type_is_a_no_op() {
let mut scene = Scene::default();
scene.create("x", "mesh").unwrap();
scene.set_attribute("x", vec![arg("fov", 45.0)]).unwrap();
scene.create("x", "mesh").unwrap();
assert_eq!(
scene.node("x").unwrap().attributes().count(),
1,
"attributes survive"
);
}
#[test]
fn a_non_finite_sample_time_is_refused() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
for bad in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
assert_eq!(
scene.set_attribute_at_time("xf", bad, vec![arg("t", 1.0)]),
Err(RecordError::InvalidTime {
handle: "xf".to_string()
})
);
}
assert!(scene.node("xf").unwrap().samples().next().is_none());
}
#[test]
fn negative_zero_is_the_same_sample_time_as_zero() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
scene
.set_attribute_at_time("xf", -0.0, vec![arg("t", 5.0)])
.unwrap();
scene
.set_attribute_at_time("xf", 0.0, vec![arg("t", 9.0)])
.unwrap();
let samples = scene.attribute_samples("xf", "t").unwrap();
assert_eq!(samples.len(), 1, "one sample, as the renderer has");
assert!(!samples[0].0.is_sign_negative(), "normalised to +0");
assert_eq!(samples[0].1.data, OwnedData::F32(vec![9.0]));
}
#[test]
fn the_reserved_handles_cannot_be_created() {
let mut scene = Scene::default();
for handle in [crate::ROOT, crate::GLOBAL] {
assert_eq!(
scene.create(handle, "transform"),
Err(RecordError::Reserved {
handle: handle.to_string()
})
);
assert!(scene.node(handle).is_none(), "nothing recorded");
}
}
#[test]
fn effective_takes_the_last_call_not_the_greatest_time() {
for (first, second, expected) in
[(0.0, 1.0, 1.0), (1.0, 0.0, 0.0), (2.0, 3.0, 3.0)]
{
let mut scene = Scene::default();
scene.create("a", "attributes").unwrap();
scene
.set_attribute_at_time("a", 1.0, vec![arg("visibility", first)])
.unwrap();
scene
.set_attribute_at_time("a", 0.0, vec![arg("visibility", second)])
.unwrap();
let node = scene.node("a").expect("created");
assert_eq!(
node.effective("visibility").expect("set at a time").data,
OwnedData::F32(vec![expected]),
"the t=0 call came last",
);
}
}
#[test]
fn a_re_set_time_is_another_call_and_the_later_one_stands() {
let mut scene = Scene::default();
scene.create("a", "attributes").unwrap();
for (time, value) in [(0.0, 1.0), (1.0, 2.0), (0.0, 3.0)] {
scene
.set_attribute_at_time("a", time, vec![arg("visibility", value)])
.unwrap();
}
let node = scene.node("a").expect("created");
assert_eq!(
node.sample_calls("visibility").unwrap().len(),
3,
"three calls"
);
assert_eq!(
node.effective("visibility").expect("set at a time").data,
OwnedData::F32(vec![3.0]),
"the last of them",
);
assert_eq!(
scene.attribute_times("a", "visibility").unwrap(),
vec![0.0, 1.0],
"but two samples",
);
}
#[test]
fn a_hand_built_node_does_not_panic_the_readers() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
scene.create("q", "mesh").unwrap();
scene.connect("xf", None, ".root", "objects").unwrap();
scene.connect("q", None, "xf", "objects").unwrap();
crate::handle::map_get_mut(&mut scene.nodes, "xf")
.expect("created")
.sample_table_mut()
.insert(crate::handle::handle("transformationmatrix"), Vec::new());
let node = scene.node("xf").expect("created");
assert!(node.effective("transformationmatrix").is_none());
assert!(scene.motion_times("q").unwrap().is_empty());
assert!(scene.world_transform("q").is_ok());
assert!(
scene
.attribute_samples("xf", "transformationmatrix")
.unwrap()
.is_empty()
);
let mut out = Vec::new();
crate::write_stream(&scene, &mut out).expect("writes");
assert!(
!String::from_utf8(out)
.unwrap()
.contains("SetAttributeAtTime"),
"a sample that is not there is not invented",
);
}
#[test]
fn a_static_call_clears_the_call_order() {
let mut scene = Scene::default();
scene.create("a", "attributes").unwrap();
scene
.set_attribute_at_time("a", 1.0, vec![arg("visibility", 1.0)])
.unwrap();
scene
.set_attribute("a", vec![arg("visibility", 2.0)])
.unwrap();
let node = scene.node("a").expect("created");
assert!(node.sample_calls("visibility").is_none());
assert_eq!(
node.effective("visibility").expect("static").data,
OwnedData::F32(vec![2.0]),
);
}
#[test]
fn delete_attribute_clears_the_log() {
let mut scene = Scene::default();
scene.create("a", "attributes").unwrap();
scene
.set_attribute_at_time("a", 1.0, vec![arg("visibility", 1.0)])
.unwrap();
scene.delete_attribute("a", "visibility");
let node = scene.node("a").expect("created");
assert!(node.sample_calls("visibility").is_none());
assert!(node.effective("visibility").is_none());
}
#[test]
fn the_record_is_net_not_a_log() {
let mut scene = Scene::default();
scene.create("a", "attributes").unwrap();
for value in 0..40u8 {
scene
.set_attribute("a", vec![arg("visibility", f32::from(value))])
.unwrap();
}
scene.create("gone", "mesh").unwrap();
scene.delete("gone").unwrap();
let changes = scene.take_changes();
assert_eq!(changes.attributes().count(), 1, "one name, forty calls");
assert!(changes.attributes().any(|pair| pair == ("a", "visibility")));
assert!(
!changes.was_created("gone"),
"created and deleted in one interval: the create is undone, and \
a consumer that never saw the node has nothing to undo",
);
assert_eq!(
changes.deleted_type("gone"),
Some("mesh"),
"only the delete stands, with the type the handle no longer has",
);
assert_eq!(
scene.take_changes(),
Changes::default(),
"taking clears, so the next synchronise starts empty",
);
}
#[test]
fn a_connect_rearmed_in_place_is_recorded() {
let mut scene = Scene::default();
scene.create("attr", "attributes").unwrap();
scene.create("shader", "shader").unwrap();
scene
.connect("shader", None, "attr", "surfaceshader")
.unwrap();
scene.take_changes();
scene
.connect_with_arguments(
"shader",
None,
"attr",
"surfaceshader",
vec![OwnedArgument::new(
"priority",
Type::I32,
1,
0,
OwnedData::I32(vec![10]),
)],
)
.unwrap();
let changes = scene.take_changes();
assert!(changes.edges_added.is_empty(), "no edge appeared");
assert!(changes.edges_removed.is_empty(), "none disappeared");
assert_eq!(changes.edges_rearmed.len(), 1);
assert_eq!(changes.edges_rearmed[0].from, "shader");
}
#[test]
fn re_arming_one_edge_repeatedly_is_one_entry() {
let mut scene = Scene::default();
scene.create("attr", "attributes").unwrap();
scene.create("shader", "shader").unwrap();
scene
.connect("shader", None, "attr", "surfaceshader")
.unwrap();
scene.take_changes();
for priority in 0..40 {
scene
.connect_with_arguments(
"shader",
None,
"attr",
"surfaceshader",
vec![OwnedArgument::new(
"priority",
Type::I32,
1,
0,
OwnedData::I32(vec![priority]),
)],
)
.unwrap();
}
let changes = scene.take_changes();
assert_eq!(changes.edges_rearmed.len(), 1, "forty calls, one edge");
assert_eq!(
changes.edges_rearmed[0].args[0].as_i32(),
Some(39),
"and it carries the arguments of the last call",
);
}
#[test]
fn a_wildcard_disconnect_records_what_it_removed() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
for handle in ["a", "b"] {
scene.create(handle, "mesh").unwrap();
scene.connect(handle, None, "xf", "objects").unwrap();
}
scene.take_changes();
scene.disconnect(crate::ALL, None, "xf", "objects").unwrap();
let changes = scene.take_changes();
let removed: Vec<&str> = changes
.edges_removed
.iter()
.map(|e| e.from.as_str())
.collect();
assert_eq!(removed, vec!["a", "b"], "both, by name, not the pattern");
}
#[test]
fn a_delete_records_the_edges_it_took_with_it() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
scene.create("q", "mesh").unwrap();
scene.connect("xf", None, ".root", "objects").unwrap();
scene.connect("q", None, "xf", "objects").unwrap();
scene.take_changes();
scene.delete("xf").unwrap();
let changes = scene.take_changes();
assert_eq!(changes.deleted().count(), 1);
let pairs: Vec<(&str, &str)> = changes
.edges_removed
.iter()
.map(|e| (e.from.as_str(), e.to.as_str()))
.collect();
assert!(pairs.contains(&("q", "xf")), "the child's edge, {pairs:?}");
assert!(pairs.contains(&("xf", ".root")), "and its own");
}
#[test]
fn changes_are_not_part_of_scene_equality() {
let build = || {
let mut scene = Scene::default();
scene.create("a", "attributes").unwrap();
scene
.set_attribute("a", vec![arg("visibility", 1.0)])
.unwrap();
scene
};
let mut synchronised = build();
synchronised.take_changes();
assert_eq!(synchronised, build());
}
fn affected_nodes<'a>(
scene: &'a Scene,
affected: &Affected<'a>,
) -> IndexSet<&'a str> {
let mut all = IndexSet::new();
for root in &affected.roots {
all.extend(scene.descendants(root));
}
all
}
#[test]
fn a_moved_transform_dirties_its_subtree() {
let mut scene = Scene::default();
scene.create("outer", "transform").unwrap();
scene.create("inner", "transform").unwrap();
scene.create("q", "mesh").unwrap();
scene.create("elsewhere", "mesh").unwrap();
scene.connect("outer", None, ".root", "objects").unwrap();
scene.connect("inner", None, "outer", "objects").unwrap();
scene.connect("q", None, "inner", "objects").unwrap();
scene
.connect("elsewhere", None, ".root", "objects")
.unwrap();
scene.take_changes();
scene
.set_attribute("outer", vec![arg("transformationmatrix", 1.0)])
.unwrap();
let changes = scene.take_changes();
let affected = scene.affected(&changes);
assert!(
affected_nodes(&scene, &affected).contains("q"),
"two levels down"
);
assert!(affected_nodes(&scene, &affected).contains("inner"));
assert!(
!affected_nodes(&scene, &affected).contains("elsewhere"),
"and nothing on another branch",
);
}
#[test]
fn a_shader_edit_reaches_the_geometry_bound_through_it() {
let mut scene = Scene::default();
scene.create("attr", "attributes").unwrap();
scene.create("shader", "shader").unwrap();
scene.create("group", "set").unwrap();
scene.create("q", "mesh").unwrap();
scene.create("other", "mesh").unwrap();
scene.connect("q", None, ".root", "objects").unwrap();
scene.connect("other", None, ".root", "objects").unwrap();
scene.connect("q", None, "group", "members").unwrap();
scene
.connect("attr", None, "group", "geometryattributes")
.unwrap();
scene
.connect("shader", None, "attr", "surfaceshader")
.unwrap();
scene.take_changes();
scene
.connect_with_arguments(
"shader",
None,
"attr",
"surfaceshader",
vec![OwnedArgument::new(
"priority",
Type::I32,
1,
0,
OwnedData::I32(vec![10]),
)],
)
.unwrap();
let changes = scene.take_changes();
let affected = scene.affected(&changes);
assert!(
affected_nodes(&scene, &affected).contains("q"),
"through the set's members"
);
assert!(
!affected_nodes(&scene, &affected).contains("other"),
"not the whole scene"
);
assert!(affected.shaders.contains("shader"));
}
#[test]
fn a_shader_parameter_edit_is_not_geometry_work() {
let mut scene = Scene::default();
scene.create("attr", "attributes").unwrap();
scene.create("shader", "shader").unwrap();
scene.create("q", "mesh").unwrap();
scene.connect("q", None, ".root", "objects").unwrap();
scene
.connect("attr", None, "q", "geometryattributes")
.unwrap();
scene
.connect("shader", None, "attr", "surfaceshader")
.unwrap();
scene.take_changes();
scene
.set_attribute("shader", vec![arg("roughness", 0.5)])
.unwrap();
let changes = scene.take_changes();
let affected = scene.affected(&changes);
assert!(affected.shaders.contains("shader"));
assert!(
affected_nodes(&scene, &affected).is_empty(),
"a material parameter costs no geometry work: {:?}",
affected_nodes(&scene, &affected),
);
}
#[test]
fn a_prototypes_ancestor_reaches_the_instancer() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
scene.create("proto", "mesh").unwrap();
scene.create("inst", "instances").unwrap();
scene.connect("inst", None, ".root", "objects").unwrap();
scene.connect("proto", None, "xf", "objects").unwrap();
scene
.connect("proto", None, "inst", "sourcemodels")
.unwrap();
scene.take_changes();
scene
.set_attribute("xf", vec![arg("transformationmatrix", 1.0)])
.unwrap();
let changes = scene.take_changes();
let affected = scene.affected(&changes);
assert!(affected_nodes(&scene, &affected).contains("proto"));
assert!(
affected_nodes(&scene, &affected).contains("inst"),
"the instancer draws it, and is reached the other way round",
);
}
#[test]
fn a_global_edit_dirties_everything() {
let mut scene = Scene::default();
scene.create("q", "mesh").unwrap();
scene.connect("q", None, ".root", "objects").unwrap();
scene.take_changes();
scene
.set_attribute(crate::GLOBAL, vec![arg("quality.shadingsamples", 8.0)])
.unwrap();
let changes = scene.take_changes();
let affected = scene.affected(&changes);
assert!(affected.everything);
assert!(
affected_nodes(&scene, &affected).is_empty(),
"not a copy of the scene"
);
}
#[test]
fn a_wildcard_disconnect_dirties_every_child_it_severed() {
let mut scene = Scene::default();
scene.create("xf", "transform").unwrap();
scene.connect("xf", None, ".root", "objects").unwrap();
for handle in ["a", "b"] {
scene.create(handle, "mesh").unwrap();
scene.connect(handle, None, "xf", "objects").unwrap();
}
scene.take_changes();
scene.disconnect(crate::ALL, None, "xf", "objects").unwrap();
let changes = scene.take_changes();
let affected = scene.affected(&changes);
assert!(affected_nodes(&scene, &affected).contains("a"));
assert!(affected_nodes(&scene, &affected).contains("b"));
}
#[test]
fn every_changed_answer_is_named_in_the_affected_set() {
fn answers(scene: &Scene, handle: &str) -> String {
format!(
"{:?}|{:?}|{:?}|{:?}",
scene.world_transform(handle),
scene.geometry_binding(handle),
scene.attribute_value(handle, "visibility"),
scene.placements(handle).map(|placements| placements
.iter()
.map(|placement| (placement.path.clone(), placement.transform))
.collect::<Vec<_>>()),
) + &format!(
"|{:?}|{:?}|{:?}",
scene.instance_transforms(handle),
scene.attribute_value(handle, "visibility.camera"),
scene.instance_sources(handle),
)
}
fn fixture() -> Scene {
let mut scene = Scene::default();
for (handle, node_type) in [
("outer", "transform"),
("inner", "transform"),
("protoxf", "transform"),
("q", "mesh"),
("r", "mesh"),
("proto", "mesh"),
("inst", "instances"),
("group", "set"),
("near", "attributes"),
("far", "attributes"),
("surface", "shader"),
("surface2", "shader"),
("texture", "shader"),
("cam", "perspectivecamera"),
("screen", "screen"),
("layer", "outputlayer"),
("driver", "outputdriver"),
] {
scene.create(handle, node_type).unwrap();
}
for (from, to) in [
("outer", ".root"),
("inner", "outer"),
("q", "inner"),
("r", "outer"),
("protoxf", ".root"),
("proto", "protoxf"),
("inst", ".root"),
] {
scene.connect(from, None, to, "objects").unwrap();
}
scene
.connect("proto", None, "inst", "sourcemodels")
.unwrap();
scene
.set_attribute(
"inst",
vec![OwnedArgument::new(
"transformationmatrices",
Type::MatrixF64,
1,
0,
OwnedData::F64(
[
[
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, -1.0, 0.0, 0.0, 1.0,
],
[
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0,
],
]
.concat(),
),
)],
)
.unwrap();
scene.connect("cam", None, ".root", "objects").unwrap();
scene.connect("screen", None, "cam", "screens").unwrap();
scene
.connect("layer", None, "screen", "outputlayers")
.unwrap();
scene
.connect("driver", None, "layer", "outputdrivers")
.unwrap();
scene.connect("q", None, "group", "members").unwrap();
scene
.connect("near", None, "q", "geometryattributes")
.unwrap();
scene
.connect("far", None, "group", "geometryattributes")
.unwrap();
scene
.connect("surface", None, "near", "surfaceshader")
.unwrap();
scene
.connect("surface2", None, "near", "surfaceshader")
.unwrap();
scene
.connect("texture", Some("out"), "surface", "colour")
.unwrap();
scene
.set_attribute(
"outer",
vec![OwnedArgument::new(
"transformationmatrix",
Type::MatrixF64,
1,
0,
OwnedData::F64(vec![
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 0.0, 1.0,
]),
)],
)
.unwrap();
scene
.set_attribute("near", vec![arg("visibility", 1.0)])
.unwrap();
scene
.set_attribute(
"far",
vec![
arg("visibility", 0.0),
OwnedArgument::new(
"visibility.priority",
Type::I32,
1,
0,
OwnedData::I32(vec![10]),
),
],
)
.unwrap();
scene
}
let handles = [
"outer", "inner", "protoxf", "q", "r", "proto", "inst", "group",
"near", "far", "surface", "surface2", "texture", "cam", "screen",
"layer", "driver",
"fresh",
];
fn matrix(x: f64) -> OwnedArgument {
OwnedArgument::new(
"transformationmatrix",
Type::MatrixF64,
1,
0,
OwnedData::F64(vec![
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, x,
0.0, 0.0, 1.0,
]),
)
}
fn edit(scene: &mut Scene, handle: &str, op: usize) {
match op {
0 => {
let _ = scene.set_attribute(handle, vec![matrix(2.0)]);
}
1 => {
let _ =
scene.set_attribute(handle, vec![arg("visibility", 0.0)]);
}
2 => {
let _ =
scene.set_attribute_at_time(handle, 0.5, vec![matrix(3.0)]);
}
3 => scene.delete_attribute(handle, "visibility"),
4 => {
let _ = scene.delete(handle);
}
5 => {
let _ = scene.disconnect(crate::ALL, None, handle, "objects");
}
6 => {
let _ = scene.connect(handle, None, ".root", "objects");
}
7 => {
let _ = scene.connect_with_arguments(
if handle.len().is_multiple_of(2) {
"surface"
} else {
"surface2"
},
None,
"near",
"surfaceshader",
vec![OwnedArgument::new(
"priority",
Type::I32,
1,
0,
OwnedData::I32(vec![7]),
)],
);
}
9 => {
let _ = scene.create("fresh", "mesh");
}
10 => {
let _ =
scene.disconnect(crate::ALL, None, handle, "sourcemodels");
}
_ => {
let _ = scene.set_attribute(
handle,
vec![OwnedArgument::new(
"visibility.priority",
Type::I32,
1,
0,
OwnedData::I32(vec![20]),
)],
);
}
}
}
let check =
|scene: &Scene, before: &Scene, affected: &Affected, what: &str| {
if affected.everything {
return;
}
assert!(
format!("{:?}", before.render_outputs())
== format!("{:?}", scene.render_outputs())
|| affected.outputs,
"{what}: the outputs changed and `outputs` is false",
);
for handle in handles {
let was = answers(before, handle);
let now = answers(scene, handle);
assert!(
was == now
|| affected_nodes(scene, affected).contains(handle),
"{what}: `{handle}` answers differently and is not in the \
affected set\n before: {was}\n after: {now}",
);
}
};
for handle in handles {
for op in 0..11 {
let mut scene = fixture();
let before = scene.clone();
scene.take_changes();
edit(&mut scene, handle, op);
let changes = scene.take_changes();
let affected = scene.affected(&changes);
check(&scene, &before, &affected, &format!("{handle}/{op}"));
}
}
for seed in 0..256u64 {
let mut scene = fixture();
let before = scene.clone();
scene.take_changes();
let mut state =
seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1);
for _ in 0..2 {
state = state
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
let pick = (state >> 33) as usize;
edit(&mut scene, handles[pick % handles.len()], pick % 11);
}
let changes = scene.take_changes();
let affected = scene.affected(&changes);
check(&scene, &before, &affected, &format!("seed {seed}"));
}
}
#[test]
fn effective_reads_a_sampled_attribute() {
let mut scene = Scene::default();
scene.create("a", "attributes").unwrap();
scene
.set_attribute_at_time("a", 0.0, vec![arg("visibility", 0.0)])
.unwrap();
let node = scene.node("a").expect("created");
assert!(
node.attribute("visibility").is_none(),
"it is not a static attribute",
);
assert!(
node.effective("visibility").is_some(),
"but it is the node's effective value",
);
}
#[test]
fn an_emptied_attribute_table_equals_an_absent_one() {
let mut set_then_deleted = Scene::default();
set_then_deleted.create("a", "attributes").unwrap();
set_then_deleted
.set_attribute("a", vec![arg("visibility", 0.0)])
.unwrap();
set_then_deleted.delete_attribute("a", "visibility");
let mut never_set = Scene::default();
never_set.create("a", "attributes").unwrap();
assert_eq!(
set_then_deleted.node("a").expect("created"),
never_set.node("a").expect("created"),
);
}
#[test]
fn an_emptied_sample_table_equals_an_absent_one() {
let mut sampled_then_static = Scene::default();
sampled_then_static.create("a", "attributes").unwrap();
sampled_then_static
.set_attribute_at_time("a", 0.0, vec![arg("visibility", 0.0)])
.unwrap();
sampled_then_static
.set_attribute("a", vec![arg("visibility", 0.0)])
.unwrap();
let mut static_only = Scene::default();
static_only.create("a", "attributes").unwrap();
static_only
.set_attribute("a", vec![arg("visibility", 0.0)])
.unwrap();
assert_eq!(
sampled_then_static.node("a").expect("created"),
static_only.node("a").expect("created"),
);
}