Skip to main content

viewport_lib/interaction/
sub_object.rs

1//! Typed sub-object reference and sub-object selection set.
2//!
3//! [`SubObjectRef`] is the single canonical way to identify a face, vertex,
4//! edge, or point-cloud point relative to its parent object. It is carried
5//! inside [`PickHit::sub_object`](crate::interaction::picking::PickHit::sub_object)
6//! and used as the key type in [`SubSelection`].
7//!
8//! [`SubSelection`] is the sub-object counterpart to
9//! [`crate::interaction::selection::Selection`]. Typically an app holds both:
10//! `Selection` for which objects are selected, `SubSelection` for which faces
11//! or points within those objects are selected.
12
13use std::collections::HashSet;
14
15use crate::interaction::selection::NodeId;
16
17// ---------------------------------------------------------------------------
18// SubObjectRef
19// ---------------------------------------------------------------------------
20
21/// A typed reference to a sub-object within a parent scene object.
22///
23/// Produced by all pick functions when a specific surface feature is hit, and
24/// stored in [`PickHit::sub_object`](crate::interaction::picking::PickHit::sub_object).
25///
26/// # Variants
27///
28/// - [`Face`](SubObjectRef::Face) : triangular face, by index in the triangle list.
29///   Index `i` addresses vertices `indices[3i..3i+3]`.
30/// - [`Vertex`](SubObjectRef::Vertex) : mesh vertex, by position in the vertex buffer.
31/// - [`Edge`](SubObjectRef::Edge) : mesh edge (from parry3d `FeatureId::Edge`; rarely
32///   produced by TriMesh ray casts in practice).
33/// - [`Point`](SubObjectRef::Point) : point in a point-cloud object, by index in the
34///   positions slice.
35/// - [`Voxel`](SubObjectRef::Voxel) : voxel in a structured scalar volume.
36/// - [`Cell`](SubObjectRef::Cell) : cell in an unstructured volume mesh (`VolumeMeshData`).
37/// - [`Splat`](SubObjectRef::Splat) : gaussian splat, by index in the splat buffer.
38/// - [`Instance`](SubObjectRef::Instance) : glyph, tensor glyph, or sprite instance,
39///   by instance index.
40/// - [`Segment`](SubObjectRef::Segment) : polyline, tube, or ribbon segment, by index.
41/// - [`Strip`](SubObjectRef::Strip) : connected curve strip within a multi-strip item,
42///   by strip index.
43#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
44#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
45#[non_exhaustive]
46pub enum SubObjectRef {
47    /// A triangular face identified by its index in the triangle list.
48    Face(u32),
49    /// A mesh vertex identified by its position in the vertex buffer.
50    Vertex(u32),
51    /// A mesh edge identified by its edge index (`parry3d::shape::FeatureId::Edge`).
52    ///
53    /// Rarely produced in practice; included for completeness.
54    Edge(u32),
55    /// A point within a point-cloud object, by its index in the positions slice.
56    Point(u32),
57    /// A voxel within a ray-marched volume, by its flat grid index.
58    ///
59    /// The flat index encodes `(ix, iy, iz)` as `ix + iy * nx + iz * nx * ny`.
60    /// Recover the 3-D indices using the grid dimensions from
61    /// [`VolumeData`](crate::geometry::marching_cubes::VolumeData).
62    Voxel(u32),
63    /// A cell within an unstructured volume mesh, by its index in
64    /// [`VolumeMeshData::cells`](crate::resources::volume_mesh::VolumeMeshData::cells).
65    ///
66    /// Produced by [`pick_transparent_volume_mesh_cpu`](crate::interaction::picking::pick_transparent_volume_mesh_cpu)
67    /// and [`pick_transparent_volume_mesh_rect`](crate::interaction::picking::pick_transparent_volume_mesh_rect).
68    Cell(u32),
69    /// A gaussian splat identified by its index in the splat buffer.
70    Splat(u32),
71    /// A glyph, tensor glyph, or sprite instance identified by its instance index.
72    Instance(u32),
73    /// A polyline, tube, or ribbon segment identified by its segment index.
74    Segment(u32),
75    /// A connected curve strip within a multi-strip item, identified by its strip index.
76    Strip(u32),
77}
78
79impl SubObjectRef {
80    /// Returns `true` if this is a [`Face`](SubObjectRef::Face).
81    pub fn is_face(&self) -> bool {
82        matches!(self, Self::Face(_))
83    }
84
85    /// Returns `true` if this is a [`Point`](SubObjectRef::Point).
86    pub fn is_point(&self) -> bool {
87        matches!(self, Self::Point(_))
88    }
89
90    /// Returns `true` if this is a [`Vertex`](SubObjectRef::Vertex).
91    pub fn is_vertex(&self) -> bool {
92        matches!(self, Self::Vertex(_))
93    }
94
95    /// Returns `true` if this is an [`Edge`](SubObjectRef::Edge).
96    pub fn is_edge(&self) -> bool {
97        matches!(self, Self::Edge(_))
98    }
99
100    /// Returns `true` if this is a [`Voxel`](SubObjectRef::Voxel).
101    pub fn is_voxel(&self) -> bool {
102        matches!(self, Self::Voxel(_))
103    }
104
105    /// Returns `true` if this is a [`Cell`](SubObjectRef::Cell).
106    pub fn is_cell(&self) -> bool {
107        matches!(self, Self::Cell(_))
108    }
109
110    /// Returns the raw index regardless of variant.
111    pub fn index(&self) -> u32 {
112        match *self {
113            Self::Face(i) | Self::Vertex(i) | Self::Edge(i) | Self::Point(i)
114            | Self::Voxel(i) | Self::Cell(i) | Self::Splat(i) | Self::Instance(i)
115            | Self::Segment(i) | Self::Strip(i) => i,
116        }
117    }
118
119    /// Convert from a parry3d [`FeatureId`](parry3d::shape::FeatureId).
120    ///
121    /// Returns `None` for `FeatureId::Unknown` (not expected from TriMesh ray casts).
122    pub fn from_feature_id(f: parry3d::shape::FeatureId) -> Option<Self> {
123        match f {
124            parry3d::shape::FeatureId::Face(i) => Some(Self::Face(i)),
125            parry3d::shape::FeatureId::Vertex(i) => Some(Self::Vertex(i)),
126            parry3d::shape::FeatureId::Edge(i) => Some(Self::Edge(i)),
127            _ => None,
128        }
129    }
130}
131
132// ---------------------------------------------------------------------------
133// SubSelection
134// ---------------------------------------------------------------------------
135
136/// A set of selected sub-objects (faces, vertices, edges, or points) across
137/// one or more parent objects.
138///
139/// Parallel to [`crate::interaction::selection::Selection`] but operates at
140/// sub-object granularity. Each entry pairs a parent `object_id` with a
141/// [`SubObjectRef`]. No ordering is maintained beyond the tracked `primary`.
142///
143/// # Typical usage
144///
145/// Hold a `SubSelection` alongside a `Selection`. Use `Selection` to track
146/// which objects are selected at object level; use `SubSelection` to track
147/// which specific faces or points within those objects are highlighted.
148///
149/// ```rust,ignore
150/// // On rect-pick:
151/// let rect_result = pick_rect(...);
152/// sub_sel.clear();
153/// sub_sel.extend_from_rect_pick(&rect_result);
154///
155/// // On ray-pick (face highlight):
156/// if let Some(sub) = hit.sub_object {
157///     sub_sel.select_one(hit.id, sub);
158/// }
159/// ```
160#[derive(Debug, Clone, Default)]
161pub struct SubSelection {
162    selected: HashSet<(NodeId, SubObjectRef)>,
163    primary: Option<(NodeId, SubObjectRef)>,
164    version: u64,
165}
166
167impl SubSelection {
168    /// Create an empty sub-selection.
169    pub fn new() -> Self {
170        Self::default()
171    }
172
173    /// Monotonically increasing generation counter.
174    ///
175    /// Incremented by `wrapping_add(1)` on every mutation. Compare against a
176    /// cached value to detect changes without re-hashing.
177    pub fn version(&self) -> u64 {
178        self.version
179    }
180
181    /// Clear and select exactly one sub-object.
182    pub fn select_one(&mut self, object_id: NodeId, sub: SubObjectRef) {
183        self.selected.clear();
184        self.selected.insert((object_id, sub));
185        self.primary = Some((object_id, sub));
186        self.version = self.version.wrapping_add(1);
187    }
188
189    /// Toggle a sub-object in or out of the selection.
190    ///
191    /// If added, it becomes the primary. If removed, primary is cleared or set
192    /// to an arbitrary remaining entry.
193    pub fn toggle(&mut self, object_id: NodeId, sub: SubObjectRef) {
194        let key = (object_id, sub);
195        if self.selected.contains(&key) {
196            self.selected.remove(&key);
197            if self.primary == Some(key) {
198                self.primary = self.selected.iter().next().copied();
199            }
200        } else {
201            self.selected.insert(key);
202            self.primary = Some(key);
203        }
204        self.version = self.version.wrapping_add(1);
205    }
206
207    /// Add a sub-object without clearing others.
208    pub fn add(&mut self, object_id: NodeId, sub: SubObjectRef) {
209        self.selected.insert((object_id, sub));
210        self.primary = Some((object_id, sub));
211        self.version = self.version.wrapping_add(1);
212    }
213
214    /// Remove a sub-object from the selection.
215    pub fn remove(&mut self, object_id: NodeId, sub: SubObjectRef) {
216        let key = (object_id, sub);
217        self.selected.remove(&key);
218        if self.primary == Some(key) {
219            self.primary = self.selected.iter().next().copied();
220        }
221        self.version = self.version.wrapping_add(1);
222    }
223
224    /// Clear the entire sub-selection.
225    pub fn clear(&mut self) {
226        self.selected.clear();
227        self.primary = None;
228        self.version = self.version.wrapping_add(1);
229    }
230
231    /// Extend from an iterator of `(object_id, SubObjectRef)` pairs.
232    ///
233    /// The last pair becomes primary.
234    pub fn extend(&mut self, items: impl IntoIterator<Item = (NodeId, SubObjectRef)>) {
235        let mut last = None;
236        for item in items {
237            self.selected.insert(item);
238            last = Some(item);
239        }
240        if let Some(item) = last {
241            self.primary = Some(item);
242        }
243        self.version = self.version.wrapping_add(1);
244    }
245
246    /// Populate from a [`RectPickResult`](crate::interaction::picking::RectPickResult).
247    ///
248    /// Adds all sub-objects from the rect pick without clearing the current
249    /// selection. Call [`clear`](Self::clear) first if you want a fresh selection.
250    pub fn extend_from_rect_pick(&mut self, result: &crate::interaction::picking::RectPickResult) {
251        for (&object_id, subs) in &result.hits {
252            for &sub in subs {
253                self.selected.insert((object_id, sub));
254                self.primary = Some((object_id, sub));
255            }
256        }
257        self.version = self.version.wrapping_add(1);
258    }
259
260    /// Whether a specific sub-object is selected.
261    pub fn contains(&self, object_id: NodeId, sub: SubObjectRef) -> bool {
262        self.selected.contains(&(object_id, sub))
263    }
264
265    /// The most recently selected `(object_id, SubObjectRef)` pair.
266    pub fn primary(&self) -> Option<(NodeId, SubObjectRef)> {
267        self.primary
268    }
269
270    /// Iterate over all selected `(object_id, SubObjectRef)` pairs.
271    pub fn iter(&self) -> impl Iterator<Item = &(NodeId, SubObjectRef)> {
272        self.selected.iter()
273    }
274
275    /// All sub-object refs for a specific parent object.
276    pub fn for_object(&self, object_id: NodeId) -> impl Iterator<Item = SubObjectRef> + '_ {
277        self.selected
278            .iter()
279            .filter(move |(id, _)| *id == object_id)
280            .map(|(_, sub)| *sub)
281    }
282
283    /// Number of selected sub-objects.
284    pub fn len(&self) -> usize {
285        self.selected.len()
286    }
287
288    /// Whether the sub-selection is empty.
289    pub fn is_empty(&self) -> bool {
290        self.selected.is_empty()
291    }
292
293    /// Count of selected faces across all objects.
294    pub fn face_count(&self) -> usize {
295        self.selected.iter().filter(|(_, s)| s.is_face()).count()
296    }
297
298    /// Count of selected points across all objects.
299    pub fn point_count(&self) -> usize {
300        self.selected.iter().filter(|(_, s)| s.is_point()).count()
301    }
302
303    /// Count of selected vertices across all objects.
304    pub fn vertex_count(&self) -> usize {
305        self.selected.iter().filter(|(_, s)| s.is_vertex()).count()
306    }
307
308    /// Count of selected voxels across all objects.
309    pub fn voxel_count(&self) -> usize {
310        self.selected.iter().filter(|(_, s)| s.is_voxel()).count()
311    }
312
313    /// Count of selected cells across all objects.
314    pub fn cell_count(&self) -> usize {
315        self.selected.iter().filter(|(_, s)| s.is_cell()).count()
316    }
317}
318
319// ---------------------------------------------------------------------------
320// SubSelectionRef
321// ---------------------------------------------------------------------------
322
323/// Geometry info needed to decode a [`SubObjectRef::Voxel`] flat index into
324/// world-space AABB corners for highlight rendering.
325///
326/// Pass one entry per volume object via [`SubSelectionRef::with_voxels`].
327pub struct VolumeSelectionInfo {
328    /// Grid dimensions `[nx, ny, nz]` — same as [`VolumeData::dims`].
329    pub dims: [u32; 3],
330    /// Local-space bounding-box minimum corner (matches [`VolumeItem::bbox_min`]).
331    pub bbox_min: [f32; 3],
332    /// Local-space bounding-box maximum corner (matches [`VolumeItem::bbox_max`]).
333    pub bbox_max: [f32; 3],
334    /// World-space transform (matches [`VolumeItem::model`]).
335    pub model: [[f32; 4]; 4],
336}
337
338/// Geometry info needed to highlight [`SubObjectRef::Point`], [`SubObjectRef::Segment`],
339/// and [`SubObjectRef::Strip`] selections on a polyline item.
340///
341/// Pass one entry per polyline object via [`SubSelectionRef::with_polylines`].
342pub struct PolylineSelectionInfo {
343    /// World-space vertex positions. Each entry is one polyline node.
344    pub positions: Vec<[f32; 3]>,
345    /// Strip lengths. Same encoding as [`PolylineItem::strip_lengths`](crate::renderer::types::items::PolylineItem::strip_lengths):
346    /// each entry is the number of nodes in that strip. If empty, all positions
347    /// belong to a single strip.
348    pub strip_lengths: Vec<u32>,
349}
350
351/// Geometry info needed to highlight a [`SubObjectRef::Cell`] selection.
352///
353/// Contains the vertex positions and cell connectivity from the host's
354/// [`VolumeMeshData`](crate::resources::volume_mesh::VolumeMeshData). Pass one
355/// entry per volume mesh object via [`SubSelectionRef::with_cells`].
356pub struct CellSelectionInfo {
357    /// World-space vertex positions. Indexed by cell connectivity entries.
358    pub positions: Vec<[f32; 3]>,
359    /// Cell connectivity. Each entry is `[u32; 8]` with
360    /// `u32::MAX` padding for cells with fewer than 8 vertices (same encoding
361    /// as [`VolumeMeshData::cells`](crate::resources::volume_mesh::VolumeMeshData::cells)).
362    pub cells: Vec<[u32; 8]>,
363}
364
365/// A renderer-owned snapshot of a [`SubSelection`] taken at frame submission time.
366///
367/// Bundles the selection items with the CPU-side mesh and point cloud data the
368/// renderer needs to build highlight geometry. The renderer does not hold a
369/// reference to any app-owned data between frames.
370///
371/// # Usage
372///
373/// ```ignore
374/// fd.interaction.sub_selection = Some(SubSelectionRef::new(
375///     &self.sub_selection,
376///     mesh_lookup,
377///     model_matrices,
378///     point_positions,
379/// ));
380/// ```
381pub struct SubSelectionRef {
382    /// Snapshot of all selected (node_id, sub_object) pairs.
383    pub(crate) items: Vec<(NodeId, SubObjectRef)>,
384    /// CPU-side vertex positions and triangle indices keyed by node id.
385    ///
386    /// Same format as the `mesh_lookup` parameter to
387    /// [`pick_scene_cpu`](crate::interaction::picking::pick_scene_cpu):
388    /// the value is `(positions, indices)` where every three consecutive
389    /// indices form one triangle.
390    pub(crate) mesh_lookup:
391        std::collections::HashMap<u64, (Vec<[f32; 3]>, Vec<u32>)>,
392    /// World-space model matrix for each node, keyed by node id.
393    ///
394    /// Used to transform local-space mesh positions into world space when
395    /// building fill and edge geometry. Nodes absent from the map are treated
396    /// as having an identity transform.
397    pub(crate) model_matrices: std::collections::HashMap<u64, glam::Mat4>,
398    /// World-space point cloud positions keyed by node id.
399    ///
400    /// Required for [`SubObjectRef::Point`] highlights. The index carried by
401    /// `Point(i)` addresses `point_positions[node_id][i]`.
402    pub(crate) point_positions: std::collections::HashMap<u64, Vec<[f32; 3]>>,
403    /// Volume geometry info keyed by node id.
404    ///
405    /// Required for [`SubObjectRef::Voxel`] highlights. Each entry provides the
406    /// grid dimensions and bounding box so the renderer can decode flat voxel
407    /// indices into world-space AABB wireframes.
408    pub(crate) voxel_lookup: std::collections::HashMap<u64, VolumeSelectionInfo>,
409    /// Unstructured volume mesh geometry keyed by node id.
410    ///
411    /// Required for [`SubObjectRef::Cell`] highlights. Each entry provides the
412    /// vertex positions and cell connectivity so the renderer can draw edge
413    /// outlines around selected cells.
414    pub(crate) cell_lookup: std::collections::HashMap<u64, CellSelectionInfo>,
415    /// Polyline geometry keyed by node id.
416    ///
417    /// Required for [`SubObjectRef::Point`], [`SubObjectRef::Segment`], and
418    /// [`SubObjectRef::Strip`] highlights on polyline items. Each entry provides
419    /// the positions and strip lengths so the renderer can draw node sprites and
420    /// segment edge lines.
421    pub(crate) polyline_lookup: std::collections::HashMap<u64, PolylineSelectionInfo>,
422    /// Version counter copied from the source [`SubSelection::version()`].
423    ///
424    /// The renderer uses this to skip GPU buffer rebuilds when the selection
425    /// has not changed since the previous frame.
426    pub version: u64,
427}
428
429impl SubSelectionRef {
430    /// Create a snapshot from a live [`SubSelection`].
431    ///
432    /// - `mesh_lookup` : CPU positions + indices per node id (same type as the
433    ///   `mesh_lookup` argument to the CPU pick functions).
434    /// - `model_matrices` : world transform per node id.
435    /// - `point_positions` : point cloud positions per node id (for
436    ///   [`SubObjectRef::Point`] entries).
437    pub fn new(
438        sub_selection: &SubSelection,
439        mesh_lookup: std::collections::HashMap<u64, (Vec<[f32; 3]>, Vec<u32>)>,
440        model_matrices: std::collections::HashMap<u64, glam::Mat4>,
441        point_positions: std::collections::HashMap<u64, Vec<[f32; 3]>>,
442    ) -> Self {
443        Self {
444            items: sub_selection
445                .iter()
446                .map(|(n, s)| (*n, *s))
447                .collect(),
448            mesh_lookup,
449            model_matrices,
450            point_positions,
451            voxel_lookup: std::collections::HashMap::new(),
452            cell_lookup: std::collections::HashMap::new(),
453            polyline_lookup: std::collections::HashMap::new(),
454            version: sub_selection.version(),
455        }
456    }
457
458    /// Attach volume geometry info for [`SubObjectRef::Voxel`] highlight rendering.
459    ///
460    /// `lookup` maps each volume's node id to its [`VolumeSelectionInfo`]. Without
461    /// this, selected voxels are silently skipped during highlight geometry build.
462    pub fn with_voxels(
463        mut self,
464        lookup: std::collections::HashMap<u64, VolumeSelectionInfo>,
465    ) -> Self {
466        self.voxel_lookup = lookup;
467        self
468    }
469
470    /// Attach unstructured volume mesh geometry for [`SubObjectRef::Cell`] highlight rendering.
471    ///
472    /// `lookup` maps each volume mesh's node id to its [`CellSelectionInfo`]. Without
473    /// this, selected cells are silently skipped during highlight geometry build.
474    pub fn with_cells(
475        mut self,
476        lookup: std::collections::HashMap<u64, CellSelectionInfo>,
477    ) -> Self {
478        self.cell_lookup = lookup;
479        self
480    }
481
482    /// Attach polyline geometry for [`SubObjectRef::Point`], [`SubObjectRef::Segment`],
483    /// and [`SubObjectRef::Strip`] highlight rendering.
484    ///
485    /// `lookup` maps each polyline item's node id to its [`PolylineSelectionInfo`].
486    /// Without this, selected polyline nodes and segments are silently skipped during
487    /// highlight geometry build.
488    pub fn with_polylines(
489        mut self,
490        lookup: std::collections::HashMap<u64, PolylineSelectionInfo>,
491    ) -> Self {
492        self.polyline_lookup = lookup;
493        self
494    }
495
496    /// Returns `true` if the snapshot contains no selected sub-objects.
497    pub fn is_empty(&self) -> bool {
498        self.items.is_empty()
499    }
500}
501
502// ---------------------------------------------------------------------------
503// Tests
504// ---------------------------------------------------------------------------
505
506#[cfg(test)]
507mod tests {
508    use super::*;
509    use crate::interaction::picking::RectPickResult;
510
511    // --- SubObjectRef ---
512
513    #[test]
514    fn sub_object_ref_kind_checks() {
515        assert!(SubObjectRef::Face(0).is_face());
516        assert!(!SubObjectRef::Face(0).is_point());
517        assert!(!SubObjectRef::Face(0).is_vertex());
518        assert!(!SubObjectRef::Face(0).is_edge());
519
520        assert!(SubObjectRef::Point(1).is_point());
521        assert!(SubObjectRef::Vertex(2).is_vertex());
522        assert!(SubObjectRef::Edge(3).is_edge());
523    }
524
525    #[test]
526    fn sub_object_ref_index() {
527        assert_eq!(SubObjectRef::Face(7).index(), 7);
528        assert_eq!(SubObjectRef::Vertex(42).index(), 42);
529        assert_eq!(SubObjectRef::Edge(0).index(), 0);
530        assert_eq!(SubObjectRef::Point(99).index(), 99);
531    }
532
533    #[test]
534    fn sub_object_ref_from_feature_id() {
535        use parry3d::shape::FeatureId;
536        assert_eq!(
537            SubObjectRef::from_feature_id(FeatureId::Face(3)),
538            Some(SubObjectRef::Face(3))
539        );
540        assert_eq!(
541            SubObjectRef::from_feature_id(FeatureId::Vertex(1)),
542            Some(SubObjectRef::Vertex(1))
543        );
544        assert_eq!(
545            SubObjectRef::from_feature_id(FeatureId::Edge(2)),
546            Some(SubObjectRef::Edge(2))
547        );
548        assert_eq!(SubObjectRef::from_feature_id(FeatureId::Unknown), None);
549    }
550
551    #[test]
552    fn sub_object_ref_hashable() {
553        let mut set = std::collections::HashSet::new();
554        set.insert(SubObjectRef::Face(0));
555        set.insert(SubObjectRef::Face(0)); // duplicate
556        set.insert(SubObjectRef::Face(1));
557        set.insert(SubObjectRef::Point(0)); // same index, different variant
558        assert_eq!(set.len(), 3);
559    }
560
561    // --- SubSelection ---
562
563    #[test]
564    fn sub_selection_select_one_clears_others() {
565        let mut sel = SubSelection::new();
566        sel.add(1, SubObjectRef::Face(0));
567        sel.add(1, SubObjectRef::Face(1));
568        sel.select_one(1, SubObjectRef::Face(5));
569        assert_eq!(sel.len(), 1);
570        assert!(sel.contains(1, SubObjectRef::Face(5)));
571        assert!(!sel.contains(1, SubObjectRef::Face(0)));
572    }
573
574    #[test]
575    fn sub_selection_toggle() {
576        let mut sel = SubSelection::new();
577        sel.toggle(1, SubObjectRef::Face(0));
578        assert!(sel.contains(1, SubObjectRef::Face(0)));
579        sel.toggle(1, SubObjectRef::Face(0));
580        assert!(!sel.contains(1, SubObjectRef::Face(0)));
581        assert!(sel.is_empty());
582    }
583
584    #[test]
585    fn sub_selection_add_preserves_others() {
586        let mut sel = SubSelection::new();
587        sel.add(1, SubObjectRef::Face(0));
588        sel.add(1, SubObjectRef::Face(1));
589        assert_eq!(sel.len(), 2);
590        assert!(sel.contains(1, SubObjectRef::Face(0)));
591        assert!(sel.contains(1, SubObjectRef::Face(1)));
592    }
593
594    #[test]
595    fn sub_selection_remove() {
596        let mut sel = SubSelection::new();
597        sel.add(1, SubObjectRef::Face(0));
598        sel.add(1, SubObjectRef::Face(1));
599        sel.remove(1, SubObjectRef::Face(0));
600        assert!(!sel.contains(1, SubObjectRef::Face(0)));
601        assert_eq!(sel.len(), 1);
602    }
603
604    #[test]
605    fn sub_selection_clear() {
606        let mut sel = SubSelection::new();
607        sel.add(1, SubObjectRef::Face(0));
608        sel.add(2, SubObjectRef::Point(3));
609        sel.clear();
610        assert!(sel.is_empty());
611        assert_eq!(sel.primary(), None);
612    }
613
614    #[test]
615    fn sub_selection_primary_tracks_last() {
616        let mut sel = SubSelection::new();
617        sel.add(1, SubObjectRef::Face(0));
618        assert_eq!(sel.primary(), Some((1, SubObjectRef::Face(0))));
619        sel.add(2, SubObjectRef::Point(5));
620        assert_eq!(sel.primary(), Some((2, SubObjectRef::Point(5))));
621    }
622
623    #[test]
624    fn sub_selection_contains() {
625        let mut sel = SubSelection::new();
626        sel.add(10, SubObjectRef::Face(3));
627        assert!(sel.contains(10, SubObjectRef::Face(3)));
628        assert!(!sel.contains(10, SubObjectRef::Face(4)));
629        assert!(!sel.contains(99, SubObjectRef::Face(3)));
630    }
631
632    #[test]
633    fn sub_selection_for_object() {
634        let mut sel = SubSelection::new();
635        sel.add(1, SubObjectRef::Face(0));
636        sel.add(1, SubObjectRef::Face(1));
637        sel.add(2, SubObjectRef::Face(0));
638        let obj1: Vec<SubObjectRef> = {
639            let mut v: Vec<_> = sel.for_object(1).collect();
640            v.sort_by_key(|s| s.index());
641            v
642        };
643        assert_eq!(obj1, vec![SubObjectRef::Face(0), SubObjectRef::Face(1)]);
644        let obj2: Vec<SubObjectRef> = sel.for_object(2).collect();
645        assert_eq!(obj2, vec![SubObjectRef::Face(0)]);
646        assert_eq!(sel.for_object(99).count(), 0);
647    }
648
649    #[test]
650    fn sub_selection_version_increments() {
651        let mut sel = SubSelection::new();
652        let v0 = sel.version();
653        sel.add(1, SubObjectRef::Face(0));
654        assert!(sel.version() > v0);
655        let v1 = sel.version();
656        sel.clear();
657        assert!(sel.version() > v1);
658    }
659
660    #[test]
661    fn sub_selection_kind_counts() {
662        let mut sel = SubSelection::new();
663        sel.add(1, SubObjectRef::Face(0));
664        sel.add(1, SubObjectRef::Face(1));
665        sel.add(2, SubObjectRef::Point(0));
666        sel.add(3, SubObjectRef::Vertex(0));
667        assert_eq!(sel.face_count(), 2);
668        assert_eq!(sel.point_count(), 1);
669        assert_eq!(sel.vertex_count(), 1);
670    }
671
672    #[test]
673    fn sub_selection_extend() {
674        let mut sel = SubSelection::new();
675        sel.extend([
676            (1, SubObjectRef::Face(0)),
677            (1, SubObjectRef::Face(1)),
678            (2, SubObjectRef::Point(3)),
679        ]);
680        assert_eq!(sel.len(), 3);
681        assert_eq!(sel.primary(), Some((2, SubObjectRef::Point(3))));
682    }
683
684    #[test]
685    fn sub_selection_extend_from_rect_pick() {
686        let mut result = RectPickResult::default();
687        result
688            .hits
689            .insert(10, vec![SubObjectRef::Face(0), SubObjectRef::Face(1)]);
690        result.hits.insert(20, vec![SubObjectRef::Point(5)]);
691
692        let mut sel = SubSelection::new();
693        sel.extend_from_rect_pick(&result);
694
695        assert_eq!(sel.len(), 3);
696        assert!(sel.contains(10, SubObjectRef::Face(0)));
697        assert!(sel.contains(10, SubObjectRef::Face(1)));
698        assert!(sel.contains(20, SubObjectRef::Point(5)));
699        assert_eq!(sel.face_count(), 2);
700        assert_eq!(sel.point_count(), 1);
701    }
702}