Skip to content

Commit 19391a6

Browse files
committed
Add bounds to map config
1 parent 1a14dd0 commit 19391a6

File tree

5 files changed

+160
-0
lines changed

5 files changed

+160
-0
lines changed

src/alitra/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@
4545
Translation,
4646
)
4747
from alitra.frame_transform import FrameTransform
48+
from alitra.models.bounds import Bounds
4849
from alitra.models.map_config import MapConfig, load_map_config

src/alitra/models/bounds.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from dataclasses import dataclass
2+
3+
from alitra.frame_dataclasses import Point
4+
5+
6+
# Point 1 and 2 should be on the diagonal corners of the bounding cube
7+
@dataclass
8+
class Bounds:
9+
point1: Point
10+
point2: Point
11+
12+
def __post_init__(self):
13+
if self.point1.frame == self.point2.frame:
14+
self.frame = self.point1.frame
15+
else:
16+
raise FrameException("The frames of the bounding points are not the same")
17+
points: list = [self.point1, self.point2]
18+
self.x_max = max(point.x for point in points)
19+
self.x_min = min(point.x for point in points)
20+
self.y_max = max(point.y for point in points)
21+
self.y_min = min(point.y for point in points)
22+
self.z_max = max(point.z for point in points)
23+
self.z_min = min(point.z for point in points)
24+
25+
def point_within_bounds(self, point: Point) -> bool:
26+
if not point.frame == self.frame:
27+
raise FrameException(
28+
f"The point is in {point.frame} frame and the bounds are in {self.frame} frame"
29+
)
30+
if point.x < self.x_min or point.x > self.x_max:
31+
return False
32+
if point.y < self.y_min or point.y > self.y_max:
33+
return False
34+
if point.z < self.z_min or point.z > self.z_max:
35+
return False
36+
return True
37+
38+
39+
class FrameException(Exception):
40+
pass

src/alitra/models/map_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
from dacite import from_dict
66

77
from alitra.frame_dataclasses import PointList
8+
from alitra.models.bounds import Bounds
89

910

1011
@dataclass
1112
class MapConfig:
1213
map_name: str
1314
robot_reference_points: PointList
1415
asset_reference_points: PointList
16+
bounds: Bounds = None
1517

1618

1719
def load_map_config(map_config_path: Path) -> MapConfig:

tests/models/test_map_config.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from cmath import exp
12
from pathlib import Path
23

34
import pytest
45

56
from alitra.frame_dataclasses import Point, PointList
7+
from alitra.models.bounds import Bounds, FrameException
68
from alitra.models.map_config import MapConfig, load_map_config
79

810
expected_map_config = MapConfig(
@@ -24,6 +26,29 @@
2426
frame="asset",
2527
),
2628
)
29+
expected_map_config_bounds = MapConfig(
30+
map_name="test_map",
31+
robot_reference_points=PointList(
32+
points=[
33+
Point(x=10, y=20, z=30, frame="robot"),
34+
Point(x=40, y=50, z=60, frame="robot"),
35+
Point(x=70, y=80, z=90, frame="robot"),
36+
],
37+
frame="robot",
38+
),
39+
asset_reference_points=PointList(
40+
points=[
41+
Point(x=11, y=21, z=31, frame="asset"),
42+
Point(x=41, y=51, z=61, frame="asset"),
43+
Point(x=71, y=81, z=91, frame="asset"),
44+
],
45+
frame="asset",
46+
),
47+
bounds=Bounds(
48+
point1=Point(x=5, y=15, z=30, frame="asset"),
49+
point2=Point(x=80, y=90, z=100, frame="asset"),
50+
),
51+
)
2752

2853

2954
def test_load_map_config():
@@ -36,3 +61,32 @@ def test_invalid_file_path():
3661
map_config_path = Path("./tests/test_data/test_map_config/no_file.json")
3762
with pytest.raises(Exception):
3863
load_map_config(map_config_path)
64+
65+
66+
def test_load_map_bounds():
67+
map_config_path = Path(
68+
"./tests/test_data/test_map_config/test_map_config_bounds.json"
69+
)
70+
map_config: MapConfig = load_map_config(map_config_path)
71+
assert map_config == expected_map_config_bounds
72+
assert map_config.bounds.x_min == expected_map_config_bounds.bounds.point1.x
73+
assert map_config.bounds.x_min == expected_map_config_bounds.bounds.point1.x
74+
assert map_config.bounds.y_min == expected_map_config_bounds.bounds.point1.y
75+
assert map_config.bounds.y_max == expected_map_config_bounds.bounds.point2.y
76+
assert map_config.bounds.z_max == expected_map_config_bounds.bounds.point2.z
77+
assert map_config.bounds.z_max == expected_map_config_bounds.bounds.point2.z
78+
79+
80+
def test_points_within_bounds():
81+
map_config_path = Path(
82+
"./tests/test_data/test_map_config/test_map_config_bounds.json"
83+
)
84+
map_config: MapConfig = load_map_config(map_config_path)
85+
point_within_bounds: Point = Point(x=10, y=40, z=50, frame="asset")
86+
point_outside_bounds: Point = Point(x=2, y=40, z=50, frame="asset")
87+
point_with_wrong_frame: Point = Point(x=2, y=40, z=50, frame="robot")
88+
89+
assert map_config.bounds.point_within_bounds(point=point_within_bounds) == True
90+
assert map_config.bounds.point_within_bounds(point=point_outside_bounds) == False
91+
with pytest.raises(FrameException):
92+
map_config.bounds.point_within_bounds(point=point_with_wrong_frame)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"map_name": "test_map",
3+
"robot_reference_points": {
4+
"points": [
5+
{
6+
"x": 10,
7+
"y": 20,
8+
"z": 30,
9+
"frame": "robot"
10+
},
11+
{
12+
"x": 40,
13+
"y": 50,
14+
"z": 60,
15+
"frame": "robot"
16+
},
17+
{
18+
"x": 70,
19+
"y": 80,
20+
"z": 90,
21+
"frame": "robot"
22+
}
23+
],
24+
"frame": "robot"
25+
},
26+
"asset_reference_points": {
27+
"points": [
28+
{
29+
"x": 11,
30+
"y": 21,
31+
"z": 31,
32+
"frame": "asset"
33+
},
34+
{
35+
"x": 41,
36+
"y": 51,
37+
"z": 61,
38+
"frame": "asset"
39+
},
40+
{
41+
"x": 71,
42+
"y": 81,
43+
"z": 91,
44+
"frame": "asset"
45+
}
46+
],
47+
"frame": "asset"
48+
},
49+
"bounds": {
50+
"point1": {
51+
"x": 5,
52+
"y": 15,
53+
"z": 30,
54+
"frame": "asset"
55+
},
56+
"point2": {
57+
"x": 80,
58+
"y": 90,
59+
"z": 100,
60+
"frame": "asset"
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)