makepad/libs/model/tests/selections.rs
Admin d2130e6550 libs: model, mesh_edit, csg, scene, sim, render, xr
Squash of 25 work commits (Sep 1–12):
  237da90  render: the sprite lane hands the screen draw back the way it found it
  895b9a7  particles: an emitter can ride a body's own frame
  914471f  ai-hub: a feed session whose last socket left ends on its idle timeout; skin: parent, skinned centroid and a nodes-only rig for retargets
  3fcccf3  sim: the whole world is implicitly editable, and one seam says where the ground is
  5692fab  sim: the landform world proves itself — walker through the tunnel included
  f4af6df  sim: terrain knows who changed it — a plan layer over player history
  adbb078  render: a water volume can be physics without a picture
  c17480f  render: a non-rigid body may carry its own orientation
  acad401  sim: an agent with no route holds and retries instead of walking into the wall
  22b2bf9  sim + chat: the composed world surface takes a map floor; the chat gets plan tools
  faf8112  web path: the tessellator's lap timer, the trace span and the fusion cycle timer have no clock on the web
  d3472eb  tsdf: the clock-taking XR helpers are native-only — the browser has no depth camera and no Instant
  4946c9e  sim + render: a repaint is not a world edit — colour and glow restyles never rebake the lightmap
  4317f58  sim + render + chat: walk decks as a surface, the filmed body is no obstruction, the brief never asks
  9791279  render: support rigged models and custom materials across viewers
  3ce2792  sim: use deck geometry for collision and sensing
  c8b79ff  Add portable PBR, rig and soft-body authoring support
  108d423  Add transactional polygon modeling and editable asset documents
  38f4d3b  Refine editable modeling and firm yarn character behavior
  08a2637  sim: add entity-owned lights and vehicle headlights
  00d96a7  render: add clustered lights, local shadows and incremental GI
  cf916af  render: import glTF asset extensions and wire clustered GI
  c6d2cca  model: cut transaction memory and raise capacity limits
  41af47a  raytrace: add a CPU probe-ray BVH budget example
  c963d0a  libs: the game sim splits into makepad-scene and makepad-soft-body; render, model, fab and the asset importer retarget

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-15 13:40:31 +02:00

68 lines
5.9 KiB
Rust

use makepad_model::*;
fn apply(d:&mut Document,id:&str,ops:Vec<Operation>)->Result<Applied>{d.apply(Transaction{request_id:id.into(),expected:d.head(),operations:ops},None)}
fn parsed(s:&str,d:&Document)->Vec<Operation>{parse_operations(&json::parse_depth(s.as_bytes(),24).unwrap(),d.limits()).unwrap()}
#[test]
fn replacing_an_object_in_one_batch_discards_its_old_selection_identity() {
for smaller in [false, true] {
let mut d = Document::new(Limits::default()).unwrap();
let seed = parsed(r#"[
{"op":"cube","object":"head","size":[1,1,1]},
{"op":"select","object":"head","name":"old","element":"faces","min":6,"max":6,"query":{"kind":"all"}}
]"#, &d);
apply(&mut d,"seed",seed).unwrap();
let before = d.to_bytes(None).unwrap();
let replacement = if smaller {
r#"{"op":"plane","object":"head","size":[1,1]}"#
} else {
r#"{"op":"cube","object":"head","size":[2,2,2]}"#
};
let ops = parsed(&format!(r#"[
{{"op":"delete_object","object":"head"}}, {replacement},
{{"op":"select","object":"head","name":"fresh","element":"faces","min":1,"max":6,"query":{{"kind":"all"}}}},
{{"op":"use_selection","object":"head","name":"fresh","element":"faces","operation":{{"op":"assign_material","material":0}}}}
]"#), &d);
apply(&mut d,"replace",ops).unwrap();
assert_eq!(d.selections().groups.len(), 1);
assert!(!d.selections().groups.contains_key(&("head".into(),"old".into())));
let bytes=d.to_bytes(None).unwrap();
assert_eq!(Document::from_bytes(&bytes,Limits::default(),None).unwrap().head(),d.head());
d.undo(d.head(),None).unwrap();
assert_eq!(d.selections(),Document::from_bytes(&before,Limits::default(),None).unwrap().selections());
}
}
#[test]
fn reselect_after_topology_change_marks_other_groups_stale_in_same_batch() {
let mut d=Document::new(Limits::default()).unwrap();
let seed=parsed(r#"[
{"op":"cube","object":"head","size":[1,1,1]},
{"op":"select","object":"head","name":"old","element":"faces","min":6,"max":6,"query":{"kind":"all"}}
]"#,&d);
apply(&mut d,"seed",seed).unwrap();
let face=d.selections().groups[&("head".into(),"old".into())].faces[0].0;
let ops=parsed(&format!(r#"[
{{"op":"delete_faces","object":"head","faces":["{face}"]}},
{{"op":"select","object":"head","name":"fresh","element":"faces","min":5,"max":5,"query":{{"kind":"all"}}}}
]"#),&d);
apply(&mut d,"reselect",ops).unwrap();
assert!(d.selections().groups[&("head".into(),"old".into())].stale);
assert!(!d.selections().groups[&("head".into(),"fresh".into())].stale);
let before=d.to_bytes(None).unwrap();
let ops=parsed(r#"[{"op":"use_selection","object":"head","name":"old","element":"faces","operation":{"op":"delete_faces"}}]"#,&d);
assert!(apply(&mut d,"stale-use",ops).is_err());
assert_eq!(d.to_bytes(None).unwrap(),before);
}
#[test]fn world_query_and_named_group_edit_roundtrip_then_stale_failure(){
let mut d=Document::new(Limits::default()).unwrap();apply(&mut d,"body",vec![Operation::Cube{object:"body".into(),size:[2.;3]},Operation::Scene(SceneOperation::Node{object:"body".into(),node:SceneNode{transform:Transform{translation:[10.,0.,0.],..Default::default()},..Default::default()}})]).unwrap();
let ops=parsed(r#"[{"op":"select","object":"body","name":"right","element":"vertices","min":4,"max":4,"query":{"kind":"bounds","min":[10.5,-2,-2],"max":[12,2,2],"space":"world"}},{"op":"use_selection","object":"body","name":"right","element":"vertices","operation":{"op":"transform","matrix":[[1,0,0,1],[0,1,0,0],[0,0,1,0],[0,0,0,1]]}}]"#,&d);apply(&mut d,"region",ops).unwrap();
let group=&d.selections().groups[&("body".into(),"right".into())];assert_eq!(group.vertices.len(),4);assert!(group.vertices.iter().all(|id|d.object("body").unwrap().vertex(*id).unwrap().position[0]==2.));let face=group.faces[0];
let bytes=d.to_bytes(None).unwrap();let restored=Document::from_bytes(&bytes,Limits::default(),None).unwrap();assert_eq!(restored.head(),d.head());assert_eq!(restored.selections(),d.selections());
apply(&mut d,"delete",vec![Operation::DeleteFaces{object:"body".into(),faces:vec![face]}]).unwrap();assert!(d.selections().groups[&("body".into(),"right".into())].stale);
let before=d.to_bytes(None).unwrap();let ops=parsed(r#"[{"op":"use_selection","object":"body","name":"right","element":"faces","operation":{"op":"delete_faces"}}]"#,&d);assert!(apply(&mut d,"stale",ops).is_err());assert_eq!(before,d.to_bytes(None).unwrap());
}
#[test]fn ray_cardinality_connected_and_cancelled_query_are_atomic(){
let mut d=Document::new(Limits::default()).unwrap();apply(&mut d,"body",vec![Operation::Cube{object:"body".into(),size:[2.;3]}]).unwrap();let ops=parsed(r#"[{"op":"select","object":"body","name":"hit","element":"faces","min":1,"max":1,"query":{"kind":"ray","origin":[0,0,5],"direction":[0,0,-1],"space":"local"}}]"#,&d);apply(&mut d,"ray",ops).unwrap();let face=d.selections().groups[&("body".into(),"hit".into())].faces[0];
apply(&mut d,"connected",vec![Operation::Selection(SelectionOperation::Query{object:"body".into(),name:"shell".into(),query:SelectionQuery::Connected{face,stop_seams:false},element:"faces".into(),min:6,max:6})]).unwrap();assert_eq!(d.selections().groups[&("body".into(),"shell".into())].vertices.len(),8);
let before=d.to_bytes(None).unwrap();let op=Operation::Selection(SelectionOperation::Query{object:"body".into(),name:"bad".into(),query:SelectionQuery::All,element:"faces".into(),min:1,max:1});assert!(apply(&mut d,"ambiguous",vec![op]).is_err());assert_eq!(d.to_bytes(None).unwrap(),before);
let ops=parsed(r#"[{"op":"select","object":"body","name":"all","element":"vertices","min":8,"max":8,"query":{"kind":"all"}}]"#,&d);assert!(d.apply(Transaction{request_id:"cancel".into(),expected:d.head(),operations:ops},Some(&||true)).is_err());assert_eq!(before,d.to_bytes(None).unwrap());
}