Skip to main content

slint_node_editor/
hit_test.rs

1use crate::path::{distance_to_bezier, CubicBezier};
2
3/// Trait for link geometry data needed for hit-testing
4pub trait LinkGeometry {
5    fn id(&self) -> i32;
6    fn start(&self) -> (f32, f32);
7    fn end(&self) -> (f32, f32);
8}
9
10/// A resolved link route that can measure a point in the same coordinate space.
11///
12/// Rendering and picking should construct the same route representation. The
13/// picker supplies `hit_samples` so curved implementations can choose their
14/// approximation quality; exact polyline implementations can ignore it.
15pub trait LinkRoute {
16    /// Stable application link identifier returned after a hit.
17    fn id(&self) -> i32;
18    /// Distance from `point` to this route in their shared coordinate space.
19    fn distance_to(&self, point: (f32, f32), hit_samples: usize) -> f32;
20}
21
22/// A link route backed by the same cubic Bézier used for rendering.
23#[derive(Clone, Copy, Debug)]
24pub struct BezierLinkRoute {
25    /// Stable application link identifier.
26    pub id: i32,
27    /// The curve used to render the link.
28    pub curve: CubicBezier,
29}
30
31impl LinkRoute for BezierLinkRoute {
32    fn id(&self) -> i32 {
33        self.id
34    }
35
36    fn distance_to(&self, point: (f32, f32), hit_samples: usize) -> f32 {
37        distance_to_bezier(point, &self.curve, hit_samples)
38    }
39}
40
41/// An owned polyline route for orthogonal and other segmented custom links.
42#[derive(Clone, Copy, Debug)]
43pub struct PolylineLinkRoute<const N: usize> {
44    /// Stable application link identifier.
45    pub id: i32,
46    /// Ordered vertices of the rendered route.
47    pub points: [(f32, f32); N],
48}
49
50impl<const N: usize> LinkRoute for PolylineLinkRoute<N> {
51    fn id(&self) -> i32 {
52        self.id
53    }
54
55    fn distance_to(&self, point: (f32, f32), _hit_samples: usize) -> f32 {
56        distance_to_polyline(point, &self.points)
57    }
58}
59
60/// Trait for pin geometry data needed for hit-testing
61pub trait PinGeometry {
62    fn id(&self) -> i32;
63    fn position(&self) -> (f32, f32);
64}
65
66/// Trait for node geometry data needed for selection
67pub trait NodeGeometry {
68    fn id(&self) -> i32;
69    fn rect(&self) -> (f32, f32, f32, f32); // x, y, width, height
70}
71
72// === Standard Implementations ===
73
74/// Simple implementation of LinkGeometry
75#[derive(Debug, Clone, Copy)]
76pub struct SimpleLinkGeometry {
77    pub id: i32,
78    pub start_x: f32,
79    pub start_y: f32,
80    pub end_x: f32,
81    pub end_y: f32,
82}
83
84impl LinkGeometry for SimpleLinkGeometry {
85    fn id(&self) -> i32 {
86        self.id
87    }
88    fn start(&self) -> (f32, f32) {
89        (self.start_x, self.start_y)
90    }
91    fn end(&self) -> (f32, f32) {
92        (self.end_x, self.end_y)
93    }
94}
95
96/// Simple implementation of PinGeometry
97#[derive(Debug, Clone, Copy)]
98pub struct SimplePinGeometry {
99    pub id: i32,
100    pub x: f32,
101    pub y: f32,
102}
103
104impl PinGeometry for SimplePinGeometry {
105    fn id(&self) -> i32 {
106        self.id
107    }
108    fn position(&self) -> (f32, f32) {
109        (self.x, self.y)
110    }
111}
112
113/// Simple implementation of NodeGeometry
114#[derive(Debug, Clone, Copy)]
115pub struct SimpleNodeGeometry {
116    pub id: i32,
117    pub x: f32,
118    pub y: f32,
119    pub width: f32,
120    pub height: f32,
121}
122
123impl NodeGeometry for SimpleNodeGeometry {
124    fn id(&self) -> i32 {
125        self.id
126    }
127    fn rect(&self) -> (f32, f32, f32, f32) {
128        (self.x, self.y, self.width, self.height)
129    }
130}
131
132/// Find the closest resolved link route at a point.
133///
134/// `point`, every route, and `hover_distance` must use the same coordinate
135/// space. For a world-space route and a screen-pixel tolerance, divide the
136/// tolerance by the viewport zoom before calling this function.
137pub fn find_link_route_at<R, I>(
138    point: (f32, f32),
139    routes: I,
140    hover_distance: f32,
141    hit_samples: usize,
142) -> i32
143where
144    R: LinkRoute,
145    I: IntoIterator<Item = R>,
146{
147    let mut closest_link_id = -1;
148    let mut closest_distance = hover_distance;
149
150    for route in routes {
151        let distance = route.distance_to(point, hit_samples);
152        if distance < closest_distance {
153            closest_distance = distance;
154            closest_link_id = route.id();
155        }
156    }
157
158    closest_link_id
159}
160
161/// Distance from a point to a polyline in their shared coordinate space.
162pub fn distance_to_polyline(point: (f32, f32), points: &[(f32, f32)]) -> f32 {
163    match points {
164        [] => f32::INFINITY,
165        [only] => squared_distance(point, *only).sqrt(),
166        _ => points
167            .windows(2)
168            .map(|segment| distance_to_segment(point, segment[0], segment[1]))
169            .fold(f32::INFINITY, f32::min),
170    }
171}
172
173fn squared_distance(a: (f32, f32), b: (f32, f32)) -> f32 {
174    let dx = a.0 - b.0;
175    let dy = a.1 - b.1;
176    dx * dx + dy * dy
177}
178
179fn distance_to_segment(point: (f32, f32), start: (f32, f32), end: (f32, f32)) -> f32 {
180    let segment = (end.0 - start.0, end.1 - start.1);
181    let length_sq = segment.0 * segment.0 + segment.1 * segment.1;
182    if length_sq == 0.0 {
183        return squared_distance(point, start).sqrt();
184    }
185    let from_start = (point.0 - start.0, point.1 - start.1);
186    let t = ((from_start.0 * segment.0 + from_start.1 * segment.1) / length_sq).clamp(0.0, 1.0);
187    let closest = (start.0 + t * segment.0, start.1 + t * segment.1);
188    squared_distance(point, closest).sqrt()
189}
190
191/// Find a default Bézier link from endpoint geometry.
192///
193/// All inputs use one coordinate space. `zoom` affects curve construction; it
194/// must be the same value used when the route was rendered. World-space links
195/// rendered inside a scaled container use `zoom = 1.0` here.
196pub fn find_link_at<L, I>(
197    mouse_x: f32,
198    mouse_y: f32,
199    links: I,
200    hover_distance: f32,
201    zoom: f32,
202    bezier_min_offset: f32,
203    hit_samples: usize,
204) -> i32
205where
206    L: LinkGeometry,
207    I: IntoIterator<Item = L>,
208{
209    let routes = links.into_iter().map(|link| {
210        let (start_x, start_y) = link.start();
211        let (end_x, end_y) = link.end();
212        BezierLinkRoute {
213            id: link.id(),
214            curve: CubicBezier::from_endpoints(
215                start_x,
216                start_y,
217                end_x,
218                end_y,
219                zoom,
220                bezier_min_offset,
221            ),
222        }
223    });
224    find_link_route_at((mouse_x, mouse_y), routes, hover_distance, hit_samples)
225}
226
227/// Find the nearest pin at the given position.
228///
229/// Returns the ID of the closest pin within `hit_radius`, or 0 if none. When
230/// pins are exactly equidistant, the lowest pin ID wins. The result therefore
231/// does not depend on iterator or cache insertion order.
232pub fn find_pin_at<P, I>(mouse_x: f32, mouse_y: f32, pins: I, hit_radius: f32) -> i32
233where
234    P: PinGeometry,
235    I: IntoIterator<Item = P>,
236{
237    let hit_radius_sq = hit_radius * hit_radius;
238    let mut closest: Option<(f32, i32)> = None;
239
240    for pin in pins {
241        let (pin_x, pin_y) = pin.position();
242        let dx = mouse_x - pin_x;
243        let dy = mouse_y - pin_y;
244        let distance_sq = dx * dx + dy * dy;
245        let pin_id = pin.id();
246        if distance_sq <= hit_radius_sq
247            && closest.is_none_or(|(best_distance_sq, best_id)| {
248                distance_sq < best_distance_sq
249                    || (distance_sq == best_distance_sq && pin_id < best_id)
250            })
251        {
252            closest = Some((distance_sq, pin_id));
253        }
254    }
255
256    closest.map_or(0, |(_, pin_id)| pin_id)
257}
258
259/// Find all nodes that intersect with a selection box
260pub fn nodes_in_selection_box<N, I>(
261    sel_x: f32,
262    sel_y: f32,
263    sel_width: f32,
264    sel_height: f32,
265    nodes: I,
266) -> Vec<i32>
267where
268    N: NodeGeometry,
269    I: IntoIterator<Item = N>,
270{
271    nodes
272        .into_iter()
273        .filter(|node| {
274            let (x, y, w, h) = node.rect();
275            x < sel_x + sel_width && x + w > sel_x && y < sel_y + sel_height && y + h > sel_y
276        })
277        .map(|node| node.id())
278        .collect()
279}
280
281/// Find links with at least one endpoint inside the selection box (inclusive).
282/// A curve crossing the box with both endpoints outside is not selected.
283pub fn links_in_selection_box<L, I>(
284    sel_x: f32,
285    sel_y: f32,
286    sel_width: f32,
287    sel_height: f32,
288    links: I,
289) -> Vec<i32>
290where
291    L: LinkGeometry,
292    I: IntoIterator<Item = L>,
293{
294    links
295        .into_iter()
296        .filter(|link| {
297            let (start_x, start_y) = link.start();
298            let (end_x, end_y) = link.end();
299
300            let start_in_box = start_x >= sel_x
301                && start_x <= sel_x + sel_width
302                && start_y >= sel_y
303                && start_y <= sel_y + sel_height;
304
305            let end_in_box = end_x >= sel_x
306                && end_x <= sel_x + sel_width
307                && end_y >= sel_y
308                && end_y <= sel_y + sel_height;
309
310            start_in_box || end_in_box
311        })
312        .map(|link| link.id())
313        .collect()
314}
315
316#[cfg(test)]
317mod tests {
318    use super::*;
319
320    // ========================================================================
321    // find_pin_at() - Pin Hit Testing
322    // ========================================================================
323
324    #[test]
325    fn test_find_pin_at() {
326        let pins = vec![
327            SimplePinGeometry {
328                id: 1001,
329                x: 10.0,
330                y: 10.0,
331            },
332            SimplePinGeometry {
333                id: 2001,
334                x: 50.0,
335                y: 50.0,
336            },
337        ];
338
339        // Pass vector (IntoIterator)
340        assert_eq!(find_pin_at(12.0, 12.0, pins.clone(), 10.0), 1001);
341        assert_eq!(find_pin_at(52.0, 52.0, pins.clone(), 10.0), 2001);
342        assert_eq!(find_pin_at(100.0, 100.0, pins, 10.0), 0);
343    }
344
345    #[test]
346    fn test_find_pin_at_exact_position() {
347        let pins = vec![SimplePinGeometry {
348            id: 1001,
349            x: 50.0,
350            y: 50.0,
351        }];
352        assert_eq!(find_pin_at(50.0, 50.0, pins, 10.0), 1001);
353    }
354
355    #[test]
356    fn test_find_pin_at_boundary_radius() {
357        let pins = vec![SimplePinGeometry {
358            id: 1001,
359            x: 50.0,
360            y: 50.0,
361        }];
362
363        // Exactly at radius distance
364        assert_eq!(find_pin_at(60.0, 50.0, pins.clone(), 10.0), 1001);
365
366        // Just outside radius
367        assert_eq!(find_pin_at(60.1, 50.0, pins, 10.0), 0);
368    }
369
370    #[test]
371    fn test_find_pin_at_empty_list() {
372        let pins: Vec<SimplePinGeometry> = vec![];
373        assert_eq!(find_pin_at(50.0, 50.0, pins, 10.0), 0);
374    }
375
376    #[test]
377    fn test_find_pin_at_picks_nearest_regardless_of_iteration_order() {
378        let farther = SimplePinGeometry {
379            id: 1001,
380            x: 59.0,
381            y: 50.0,
382        };
383        let exact = SimplePinGeometry {
384            id: 2001,
385            x: 50.0,
386            y: 50.0,
387        };
388
389        assert_eq!(find_pin_at(50.0, 50.0, [farther, exact], 10.0), 2001);
390        assert_eq!(find_pin_at(50.0, 50.0, [exact, farther], 10.0), 2001);
391    }
392
393    #[test]
394    fn test_find_pin_at_breaks_distance_ties_by_lowest_id() {
395        let lower_id = SimplePinGeometry {
396            id: 1001,
397            x: 45.0,
398            y: 50.0,
399        };
400        let higher_id = SimplePinGeometry {
401            id: 2001,
402            x: 55.0,
403            y: 50.0,
404        };
405
406        assert_eq!(find_pin_at(50.0, 50.0, [higher_id, lower_id], 10.0), 1001);
407        assert_eq!(find_pin_at(50.0, 50.0, [lower_id, higher_id], 10.0), 1001);
408    }
409
410    #[test]
411    fn test_find_pin_at_zero_radius() {
412        let pins = vec![SimplePinGeometry {
413            id: 1001,
414            x: 50.0,
415            y: 50.0,
416        }];
417
418        // Exact match with zero radius
419        assert_eq!(find_pin_at(50.0, 50.0, pins.clone(), 0.0), 1001);
420
421        // Any offset with zero radius should miss
422        assert_eq!(find_pin_at(50.1, 50.0, pins, 0.0), 0);
423    }
424
425    // ========================================================================
426    // find_link_at() - Link Hit Testing (Core function)
427    // ========================================================================
428
429    #[test]
430    fn test_find_link_at_single_link() {
431        let links = vec![SimpleLinkGeometry {
432            id: 1,
433            start_x: 0.0,
434            start_y: 50.0,
435            end_x: 100.0,
436            end_y: 50.0,
437        }];
438
439        // Click on the middle of a horizontal link
440        let result = find_link_at(50.0, 50.0, links, 10.0, 1.0, 50.0, 20);
441        assert_eq!(result, 1);
442    }
443
444    #[test]
445    fn test_find_link_at_near_start() {
446        let links = vec![SimpleLinkGeometry {
447            id: 1,
448            start_x: 0.0,
449            start_y: 50.0,
450            end_x: 100.0,
451            end_y: 50.0,
452        }];
453
454        // Click near the start point
455        let result = find_link_at(5.0, 50.0, links, 10.0, 1.0, 50.0, 20);
456        assert_eq!(result, 1);
457    }
458
459    #[test]
460    fn test_find_link_at_near_end() {
461        let links = vec![SimpleLinkGeometry {
462            id: 1,
463            start_x: 0.0,
464            start_y: 50.0,
465            end_x: 100.0,
466            end_y: 50.0,
467        }];
468
469        // Click near the end point
470        let result = find_link_at(95.0, 50.0, links, 10.0, 1.0, 50.0, 20);
471        assert_eq!(result, 1);
472    }
473
474    #[test]
475    fn test_find_link_at_miss() {
476        let links = vec![SimpleLinkGeometry {
477            id: 1,
478            start_x: 0.0,
479            start_y: 50.0,
480            end_x: 100.0,
481            end_y: 50.0,
482        }];
483
484        // Click far away from the link
485        let result = find_link_at(50.0, 200.0, links, 10.0, 1.0, 50.0, 20);
486        assert_eq!(result, -1);
487    }
488
489    #[test]
490    fn test_find_link_at_empty_list() {
491        let links: Vec<SimpleLinkGeometry> = vec![];
492        let result = find_link_at(50.0, 50.0, links, 10.0, 1.0, 50.0, 20);
493        assert_eq!(result, -1);
494    }
495
496    #[test]
497    fn test_find_link_at_closest_wins() {
498        // Two links at different distances
499        let links = vec![
500            SimpleLinkGeometry {
501                id: 1,
502                start_x: 0.0,
503                start_y: 50.0, // At y=50
504                end_x: 100.0,
505                end_y: 50.0,
506            },
507            SimpleLinkGeometry {
508                id: 2,
509                start_x: 0.0,
510                start_y: 55.0, // At y=55, closer to y=53
511                end_x: 100.0,
512                end_y: 55.0,
513            },
514        ];
515
516        // Click at y=53 - link 2 should be closer
517        let result = find_link_at(50.0, 53.0, links, 10.0, 1.0, 50.0, 20);
518        assert_eq!(result, 2);
519    }
520
521    #[test]
522    fn test_find_link_at_first_wins_on_tie() {
523        // Two links at exactly the same position
524        let links = vec![
525            SimpleLinkGeometry {
526                id: 1,
527                start_x: 0.0,
528                start_y: 50.0,
529                end_x: 100.0,
530                end_y: 50.0,
531            },
532            SimpleLinkGeometry {
533                id: 2,
534                start_x: 0.0,
535                start_y: 50.0,
536                end_x: 100.0,
537                end_y: 50.0,
538            },
539        ];
540
541        // First link should win on exact tie
542        let result = find_link_at(50.0, 50.0, links, 10.0, 1.0, 50.0, 20);
543        assert_eq!(result, 1);
544    }
545
546    #[test]
547    fn test_find_link_at_hover_distance_threshold() {
548        let links = vec![SimpleLinkGeometry {
549            id: 1,
550            start_x: 0.0,
551            start_y: 50.0,
552            end_x: 100.0,
553            end_y: 50.0,
554        }];
555
556        // Just within hover distance
557        let result1 = find_link_at(50.0, 59.0, links.clone(), 10.0, 1.0, 50.0, 20);
558        assert_eq!(result1, 1);
559
560        // Just outside hover distance
561        let result2 = find_link_at(50.0, 70.0, links, 10.0, 1.0, 50.0, 20);
562        assert_eq!(result2, -1);
563    }
564
565    #[test]
566    fn test_find_link_at_different_zoom() {
567        let links = vec![SimpleLinkGeometry {
568            id: 1,
569            start_x: 0.0,
570            start_y: 50.0,
571            end_x: 100.0,
572            end_y: 50.0,
573        }];
574
575        // Same click position with different zoom levels
576        // The bezier shape changes with zoom, which may affect hit detection
577        let result1 = find_link_at(50.0, 50.0, links.clone(), 10.0, 1.0, 50.0, 20);
578        let result2 = find_link_at(50.0, 50.0, links, 10.0, 2.0, 50.0, 20);
579
580        // Both should hit since we're clicking right on the curve
581        assert_eq!(result1, 1);
582        assert_eq!(result2, 1);
583    }
584
585    #[test]
586    fn test_find_link_at_very_short_link() {
587        // Link where start ≈ end
588        let links = vec![SimpleLinkGeometry {
589            id: 1,
590            start_x: 50.0,
591            start_y: 50.0,
592            end_x: 51.0,
593            end_y: 50.0,
594        }];
595
596        // Click on the point
597        let result = find_link_at(50.5, 50.0, links, 10.0, 1.0, 50.0, 20);
598        assert_eq!(result, 1);
599    }
600
601    #[test]
602    fn test_find_link_at_diagonal_link() {
603        let links = vec![SimpleLinkGeometry {
604            id: 1,
605            start_x: 0.0,
606            start_y: 0.0,
607            end_x: 100.0,
608            end_y: 100.0,
609        }];
610
611        // Click near the middle of a diagonal bezier
612        let result = find_link_at(50.0, 50.0, links, 20.0, 1.0, 50.0, 20);
613        assert_eq!(result, 1);
614    }
615
616    #[test]
617    fn test_find_link_at_multiple_links_one_hit() {
618        let links = vec![
619            SimpleLinkGeometry {
620                id: 1,
621                start_x: 0.0,
622                start_y: 0.0,
623                end_x: 100.0,
624                end_y: 0.0,
625            },
626            SimpleLinkGeometry {
627                id: 2,
628                start_x: 0.0,
629                start_y: 100.0,
630                end_x: 100.0,
631                end_y: 100.0,
632            },
633            SimpleLinkGeometry {
634                id: 3,
635                start_x: 0.0,
636                start_y: 200.0,
637                end_x: 100.0,
638                end_y: 200.0,
639            },
640        ];
641
642        // Click on the middle link
643        let result = find_link_at(50.0, 100.0, links, 10.0, 1.0, 50.0, 20);
644        assert_eq!(result, 2);
645    }
646
647    // ========================================================================
648    // nodes_in_selection_box() - Box Selection
649    // ========================================================================
650
651    #[test]
652    fn test_nodes_in_selection_box() {
653        let nodes = vec![
654            SimpleNodeGeometry {
655                id: 1,
656                x: 0.0,
657                y: 0.0,
658                width: 100.0,
659                height: 80.0,
660            },
661            SimpleNodeGeometry {
662                id: 2,
663                x: 200.0,
664                y: 0.0,
665                width: 100.0,
666                height: 80.0,
667            },
668            SimpleNodeGeometry {
669                id: 3,
670                x: 50.0,
671                y: 100.0,
672                width: 100.0,
673                height: 80.0,
674            },
675        ];
676
677        let selected = nodes_in_selection_box(0.0, 0.0, 150.0, 200.0, nodes);
678        assert!(selected.contains(&1));
679        assert!(selected.contains(&3));
680        assert!(!selected.contains(&2));
681    }
682
683    #[test]
684    fn test_nodes_in_selection_box_empty() {
685        let nodes: Vec<SimpleNodeGeometry> = vec![];
686        let selected = nodes_in_selection_box(0.0, 0.0, 100.0, 100.0, nodes);
687        assert!(selected.is_empty());
688    }
689
690    #[test]
691    fn test_nodes_in_selection_box_partial_overlap() {
692        let nodes = vec![SimpleNodeGeometry {
693            id: 1,
694            x: 50.0,
695            y: 50.0,
696            width: 100.0,
697            height: 100.0,
698        }];
699
700        // Selection box partially overlaps the node
701        let selected = nodes_in_selection_box(0.0, 0.0, 60.0, 60.0, nodes);
702        assert!(selected.contains(&1));
703    }
704
705    #[test]
706    fn test_nodes_in_selection_box_no_overlap() {
707        let nodes = vec![SimpleNodeGeometry {
708            id: 1,
709            x: 200.0,
710            y: 200.0,
711            width: 100.0,
712            height: 100.0,
713        }];
714
715        let selected = nodes_in_selection_box(0.0, 0.0, 100.0, 100.0, nodes);
716        assert!(selected.is_empty());
717    }
718
719    #[test]
720    fn test_nodes_in_selection_box_touching_edge() {
721        let nodes = vec![SimpleNodeGeometry {
722            id: 1,
723            x: 100.0,
724            y: 0.0,
725            width: 100.0,
726            height: 100.0,
727        }];
728
729        // Selection box ends exactly where node starts - no overlap
730        let selected = nodes_in_selection_box(0.0, 0.0, 100.0, 100.0, nodes);
731        assert!(selected.is_empty());
732    }
733
734    // ========================================================================
735    // links_in_selection_box() - Link Box Selection
736    // ========================================================================
737
738    #[test]
739    fn test_links_in_selection_box() {
740        let links = vec![
741            SimpleLinkGeometry {
742                id: 1,
743                start_x: 10.0,
744                start_y: 10.0,
745                end_x: 200.0,
746                end_y: 10.0,
747            },
748            SimpleLinkGeometry {
749                id: 2,
750                start_x: 200.0,
751                start_y: 10.0,
752                end_x: 10.0,
753                end_y: 10.0,
754            },
755            SimpleLinkGeometry {
756                id: 3,
757                start_x: 200.0,
758                start_y: 10.0,
759                end_x: 300.0,
760                end_y: 10.0,
761            },
762        ];
763
764        let selected = links_in_selection_box(0.0, 0.0, 100.0, 100.0, links);
765        assert!(selected.contains(&1));
766        assert!(selected.contains(&2));
767        assert!(!selected.contains(&3));
768    }
769
770    #[test]
771    fn test_links_in_selection_box_end_inside() {
772        let links = vec![SimpleLinkGeometry {
773            id: 1,
774            start_x: 200.0, // Outside box
775            start_y: 50.0,
776            end_x: 50.0, // Inside box
777            end_y: 50.0,
778        }];
779
780        let selected = links_in_selection_box(0.0, 0.0, 100.0, 100.0, links);
781        assert!(selected.contains(&1));
782    }
783
784    #[test]
785    fn test_links_in_selection_box_neither_inside() {
786        let links = vec![SimpleLinkGeometry {
787            id: 1,
788            start_x: 200.0,
789            start_y: 50.0,
790            end_x: 300.0,
791            end_y: 50.0,
792        }];
793
794        let selected = links_in_selection_box(0.0, 0.0, 100.0, 100.0, links);
795        assert!(selected.is_empty());
796    }
797
798    #[test]
799    fn test_links_in_selection_box_empty() {
800        let links: Vec<SimpleLinkGeometry> = vec![];
801        let selected = links_in_selection_box(0.0, 0.0, 100.0, 100.0, links);
802        assert!(selected.is_empty());
803    }
804
805    // ========================================================================
806    // Trait implementations
807    // ========================================================================
808
809    #[test]
810    fn test_simple_link_geometry_trait() {
811        let link = SimpleLinkGeometry {
812            id: 42,
813            start_x: 10.0,
814            start_y: 20.0,
815            end_x: 30.0,
816            end_y: 40.0,
817        };
818
819        assert_eq!(link.id(), 42);
820        assert_eq!(link.start(), (10.0, 20.0));
821        assert_eq!(link.end(), (30.0, 40.0));
822    }
823
824    #[test]
825    fn test_simple_pin_geometry_trait() {
826        let pin = SimplePinGeometry {
827            id: 1001,
828            x: 50.0,
829            y: 75.0,
830        };
831
832        assert_eq!(pin.id(), 1001);
833        assert_eq!(pin.position(), (50.0, 75.0));
834    }
835
836    #[test]
837    fn test_simple_node_geometry_trait() {
838        let node = SimpleNodeGeometry {
839            id: 1,
840            x: 10.0,
841            y: 20.0,
842            width: 100.0,
843            height: 80.0,
844        };
845
846        assert_eq!(node.id(), 1);
847        assert_eq!(node.rect(), (10.0, 20.0, 100.0, 80.0));
848    }
849}