-
Notifications
You must be signed in to change notification settings - Fork 37
ITEP-89462 Refactor and add UT for controller analytics functions #1299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tdorauintc
wants to merge
12
commits into
main
Choose a base branch
from
refactor/controller-analytics-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4c71bc1
First approach to refactor tripwires
tdorauintc e22209e
Further tripwire refactor
tdorauintc 274646e
Refactor region analytics
tdorauintc c33d1c3
Add doc strings to new static functions
tdorauintc b08d1db
Use keys instead of indices for regions and tripwires
tdorauintc 8e2b967
Move the functions to geometry.py
tdorauintc 48997d5
Merge branch 'main' into refactor/controller-analytics-functions
tdorauintc e926094
Merge branch 'main' into refactor/controller-analytics-functions
tdorauintc eed9a66
Add UT for region and tripwire event detection geometry.py functions
tdorauintc b35d736
Merge branch 'main' into refactor/controller-analytics-functions
tdorauintc 4432d0c
Moved corner case to positive UT to align naming
tdorauintc b4fba21
Merge branch 'main' into refactor/controller-analytics-functions
tdorauintc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| # SPDX-FileCopyrightText: (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import math | ||
|
|
||
| import pytest | ||
|
|
||
| from scene_common.geometry import get_region_events, Region, Point | ||
|
|
||
| UUID = "39bd9698-8603-43fb-9cb9-06d9a14e6a24" | ||
|
|
||
|
|
||
| def _regular_polygon(n, cx=10, cy=10, r=10): | ||
| """! Generate vertices for a regular n-gon centered at (cx, cy) with radius r. """ | ||
| return [ | ||
| [cx + r * math.cos(2 * math.pi * i / n), | ||
| cy + r * math.sin(2 * math.pi * i / n)] | ||
| for i in range(n) | ||
| ] | ||
|
|
||
|
|
||
| # --- Positive tests: object inside various region types --- | ||
|
|
||
| @pytest.mark.parametrize("n_vertices", [3, 4, 5, 10], | ||
| ids=["triangle", "quad", "pentagon", "decagon"]) | ||
| def test_object_inside_polygon(n_vertices): | ||
| """! Verify object at polygon center is detected inside for varying vertex counts. """ | ||
| pts = _regular_polygon(n_vertices) | ||
| region = Region(UUID, "poly", {"points": pts}) | ||
| result = get_region_events({"r": region}, [Point(10, 10)]) | ||
| assert result["r"] == [0] | ||
|
|
||
|
|
||
| def test_object_inside_circle(): | ||
| """! Verify object at circle center is detected inside. """ | ||
| region = Region(UUID, "circle", {"area": "circle", "center": [5, 5], "radius": 3}) | ||
| result = get_region_events({"r": region}, [Point(5, 5)]) | ||
| assert result["r"] == [0] | ||
|
|
||
|
|
||
| def test_object_inside_scene(): | ||
| """! Verify any object is inside a scene-wide region. """ | ||
| region = Region(UUID, "scene", {"area": "scene"}) | ||
| result = get_region_events({"r": region}, [Point(100, 200)]) | ||
| assert result["r"] == [0] | ||
|
|
||
|
|
||
| # --- No-match tests: object outside all regions --- | ||
|
|
||
| def test_no_match_outside_polygon(): | ||
| """! Verify object outside polygon is not detected. """ | ||
| region = Region(UUID, "poly", {"points": [[0, 0], [10, 0], [10, 10], [0, 10]]}) | ||
| result = get_region_events({"r": region}, [Point(50, 50)]) | ||
| assert result["r"] == [] | ||
|
|
||
|
|
||
| def test_no_match_outside_circle(): | ||
| """! Verify object outside circle is not detected. """ | ||
| region = Region(UUID, "circle", {"area": "circle", "center": [5, 5], "radius": 3}) | ||
| result = get_region_events({"r": region}, [Point(50, 50)]) | ||
| assert result["r"] == [] | ||
|
|
||
|
|
||
| # --- Length variations --- | ||
|
|
||
| def test_single_region_single_object(): | ||
| """! Verify single region with one matching object. """ | ||
| region = Region(UUID, "poly", {"points": [[0, 0], [10, 0], [10, 10], [0, 10]]}) | ||
| result = get_region_events({"r": region}, [Point(5, 5)]) | ||
| assert result["r"] == [0] | ||
|
|
||
|
|
||
| def test_multiple_regions_multiple_objects(): | ||
| """! Verify correct mapping with overlapping regions and multiple objects. """ | ||
| region_a = Region(UUID, "a", {"points": [[0, 0], [10, 0], [10, 10], [0, 10]]}) | ||
| region_b = Region(UUID, "b", {"points": [[5, 5], [15, 5], [15, 15], [5, 15]]}) | ||
| region_c = Region(UUID, "c", {"area": "circle", "center": [20, 20], "radius": 2}) | ||
| regions = {"a": region_a, "b": region_b, "c": region_c} | ||
| objects = [Point(7, 7), Point(2, 2), Point(20, 20), Point(50, 50)] | ||
| result = get_region_events(regions, objects) | ||
| assert sorted(result["a"]) == [0, 1] | ||
| assert result["b"] == [0] | ||
| assert result["c"] == [2] | ||
|
|
||
|
|
||
| # --- Empty inputs --- | ||
|
|
||
| def test_empty_object_list(): | ||
| """! Verify regions with no objects returns empty lists per key. """ | ||
| region = Region(UUID, "poly", {"points": [[0, 0], [10, 0], [10, 10], [0, 10]]}) | ||
| result = get_region_events({"r": region}, []) | ||
| assert result["r"] == [] | ||
|
|
||
|
|
||
| def test_empty_region_dict(): | ||
| """! Verify empty region dict returns empty result. """ | ||
| result = get_region_events({}, [Point(5, 5)]) | ||
| assert result == {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # SPDX-FileCopyrightText: (C) 2026 Intel Corporation | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import pytest | ||
|
|
||
| from scene_common.geometry import get_tripwire_events, Tripwire, Point | ||
|
|
||
| UUID = "39bd9698-8603-43fb-9cb9-06d9a14e6a24" | ||
|
|
||
|
|
||
| # --- Positive tests: crossing in both directions for four orientations --- | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "tripwire_pts, current, previous, expected_dir", | ||
| [ | ||
| # Horizontal tripwire [(0,0) -> (10,0)] | ||
| ([[0, 0], [10, 0]], Point(5, 1), Point(5, -1), -1), | ||
| ([[0, 0], [10, 0]], Point(5, -1), Point(5, 1), 1), | ||
| # Vertical tripwire [(5,0) -> (5,10)] | ||
| ([[5, 0], [5, 10]], Point(6, 5), Point(4, 5), 1), | ||
| ([[5, 0], [5, 10]], Point(4, 5), Point(6, 5), -1), | ||
| # Acute angle (~45 deg) tripwire [(0,0) -> (10,10)] | ||
| ([[0, 0], [10, 10]], Point(10, 0), Point(0, 10), 1), | ||
| ([[0, 0], [10, 10]], Point(0, 10), Point(10, 0), -1), | ||
| # Obtuse angle (~135 deg) tripwire [(10,0) -> (0,10)] | ||
| ([[10, 0], [0, 10]], Point(0, 0), Point(10, 10), -1), | ||
| ([[10, 0], [0, 10]], Point(10, 10), Point(0, 0), 1), | ||
| ], | ||
| ids=[ | ||
| "horizontal-south-to-north", | ||
| "horizontal-north-to-south", | ||
| "vertical-left-to-right", | ||
| "vertical-right-to-left", | ||
| "acute-cross-a", | ||
| "acute-cross-b", | ||
| "obtuse-cross-a", | ||
| "obtuse-cross-b", | ||
| ]) | ||
| def test_tripwire_crossing(tripwire_pts, current, previous, expected_dir): | ||
| """! Verify tripwire crossing detection for various orientations and directions. """ | ||
| tripwire = Tripwire(UUID, "tw", {"points": tripwire_pts}) | ||
| result = get_tripwire_events({"tw": tripwire}, [(current, previous)]) | ||
| assert len(result["tw"]) == 1 | ||
| assert result["tw"][0] == (0, expected_dir) | ||
|
|
||
|
|
||
| # --- No-match tests --- | ||
|
|
||
| def test_no_crossing_parallel_offset(): | ||
| """! Verify no crossing when movement is parallel to tripwire but offset. """ | ||
| tripwire = Tripwire(UUID, "tw", {"points": [[0, 0], [10, 0]]}) | ||
| result = get_tripwire_events({"tw": tripwire}, [(Point(15, 2), Point(5, 2))]) | ||
| assert result["tw"] == [] | ||
|
|
||
|
|
||
| def test_no_crossing_collinear(): | ||
| """! Verify no crossing when movement is along the tripwire (collinear). """ | ||
| tripwire = Tripwire(UUID, "tw", {"points": [[0, 0], [10, 0]]}) | ||
| result = get_tripwire_events({"tw": tripwire}, [(Point(8, 0), Point(2, 0))]) | ||
| assert result["tw"] == [] | ||
|
|
||
|
|
||
|
|
||
|
|
||
| # --- Positive tests: endpoint on tripwire --- | ||
|
|
||
| def test_crossing_when_endpoint_on_tripwire(): | ||
| """! Verify that endpoint landing exactly on the tripwire counts as a crossing. """ | ||
| tripwire = Tripwire(UUID, "tw", {"points": [[0, 0], [10, 0]]}) | ||
| result = get_tripwire_events({"tw": tripwire}, [(Point(5, 0), Point(5, -5))]) | ||
|
tdorauintc marked this conversation as resolved.
|
||
| assert len(result["tw"]) == 1 | ||
| assert result["tw"][0][0] == 0 | ||
|
|
||
|
|
||
| # --- Length variations --- | ||
|
|
||
| def test_single_tripwire_single_object(): | ||
| """! Verify single tripwire with one crossing object. """ | ||
| tripwire = Tripwire(UUID, "tw", {"points": [[0, 0], [10, 0]]}) | ||
| result = get_tripwire_events({"tw": tripwire}, [(Point(5, 1), Point(5, -1))]) | ||
| assert len(result["tw"]) == 1 | ||
| assert result["tw"][0][0] == 0 | ||
|
|
||
|
|
||
| def test_multiple_tripwires_multiple_objects(): | ||
| """! Verify detection with multiple tripwires and objects, one crossing both. """ | ||
| tw_h = Tripwire(UUID, "h", {"points": [[0, 0], [10, 0]]}) | ||
| tw_v = Tripwire(UUID, "v", {"points": [[5, -5], [5, 5]]}) | ||
| tripwires = {"h": tw_h, "v": tw_v} | ||
| objects = [ | ||
| (Point(6, 1), Point(4, -1)), # crosses both | ||
| (Point(1, 5), Point(1, 4)), # crosses neither | ||
| ] | ||
| result = get_tripwire_events(tripwires, objects) | ||
| assert len(result["h"]) == 1 | ||
| assert result["h"][0][0] == 0 | ||
| assert len(result["v"]) == 1 | ||
| assert result["v"][0][0] == 0 | ||
|
|
||
|
|
||
| # --- Empty inputs --- | ||
|
|
||
| def test_empty_object_list(): | ||
| """! Verify tripwires with no objects returns empty lists per key. """ | ||
| tripwire = Tripwire(UUID, "tw", {"points": [[0, 0], [10, 0]]}) | ||
| result = get_tripwire_events({"tw": tripwire}, []) | ||
| assert result["tw"] == [] | ||
|
|
||
|
|
||
| def test_empty_tripwire_dict(): | ||
| """! Verify empty tripwire dict returns empty result. """ | ||
| result = get_tripwire_events({}, [(Point(5, 1), Point(5, -1))]) | ||
| assert result == {} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.