Skip to content

Commit 7e7937d

Browse files
committed
collision config is jsonable
1 parent 260f0df commit 7e7937d

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/semantic_digital_twin/world_description/world_entity.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
from krrood.adapters.json_serializer import (
1919
SubclassJSONSerializer,
2020
JSON_TYPE_NAME,
21+
to_json,
22+
from_json,
2123
)
2224
from krrood.entity_query_language.predicate import Symbol
2325
from scipy.stats import geom
@@ -87,7 +89,7 @@ def __eq__(self, other):
8789

8890

8991
@dataclass
90-
class CollisionCheckingConfig:
92+
class CollisionCheckingConfig(SubclassJSONSerializer):
9193
buffer_zone_distance: Optional[float] = None
9294
"""
9395
Distance defining a buffer zone around the entity. The buffer zone represents a soft boundary where
@@ -111,6 +113,22 @@ class CollisionCheckingConfig:
111113
If more bodies than this are in the buffer zone, only the closest ones are avoided.
112114
"""
113115

116+
def to_json(self) -> Dict[str, Any]:
117+
json_data = super().to_json()
118+
for field_ in fields(self):
119+
value = getattr(self, field_.name)
120+
json_data[field_.name] = to_json(value)
121+
return json_data
122+
123+
@classmethod
124+
def _from_json(cls, data: Dict[str, Any], **kwargs) -> Self:
125+
cls_kwargs = {}
126+
for field_name, json_field_data in data.items():
127+
if field_name == JSON_TYPE_NAME:
128+
continue
129+
cls_kwargs[field_name] = from_json(json_field_data)
130+
return cls(**cls_kwargs)
131+
114132

115133
@dataclass(unsafe_hash=True, eq=False)
116134
class KinematicStructureEntity(WorldEntity, SubclassJSONSerializer, ABC):
@@ -393,6 +411,7 @@ def to_json(self) -> Dict[str, Any]:
393411
result["name"] = self.name.to_json()
394412
result["collision"] = self.collision.to_json()
395413
result["visual"] = self.visual.to_json()
414+
result["collision_config"] = to_json(self.collision_config)
396415
return result
397416

398417
@classmethod
@@ -414,6 +433,7 @@ def _from_json(cls, data: Dict[str, Any], **kwargs) -> Self:
414433

415434
result.collision = collision
416435
result.visual = visual
436+
result.collision_config = from_json(data["collision_config"])
417437

418438
return result
419439

test/test_adapters/test_json_parsing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ def test_body_json_serialization():
3333
body = Body(name=PrefixedName("body"))
3434
collision = [Box(origin=TransformationMatrix.from_xyz_rpy(0, 1, 0, 0, 0, 1, body))]
3535
body.collision = ShapeCollection(collision, reference_frame=body)
36+
body.collision_config.max_avoided_bodies = 69
37+
body.collision_config.disabled = False
38+
body.collision_config.buffer_zone_distance = 1.227
39+
body.collision_config.violated_distance = 0.23
3640

3741
json_data = body.to_json()
3842
body2 = Body.from_json(json_data)
@@ -57,6 +61,8 @@ def test_body_json_serialization():
5761

5862
assert body == body2
5963

64+
assert body.collision_config == body2.collision_config
65+
6066

6167
def test_transformation_matrix_json_serialization():
6268
body = Body(name=PrefixedName("body"))

0 commit comments

Comments
 (0)