Skip to content

Commit 79cb9bc

Browse files
authored
Merge pull request #118 from LucaKro/world_class_cleanup
World class cleanup
2 parents ddc1ae2 + 85b346f commit 79cb9bc

34 files changed

Lines changed: 1632 additions & 1535 deletions

scripts/generate_orm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import semantic_digital_twin.world_description.world_entity
3131
from semantic_digital_twin.datastructures.prefixed_name import PrefixedName
3232
from semantic_digital_twin.spatial_computations.forward_kinematics import (
33-
ForwardKinematicsVisitor,
33+
ForwardKinematicsManager,
3434
)
3535
from semantic_digital_twin.world import (
3636
ResetStateContextManager,
@@ -73,7 +73,7 @@
7373
ResetStateContextManager,
7474
WorldModelUpdateContextManager,
7575
HasUpdateState,
76-
ForwardKinematicsVisitor,
76+
ForwardKinematicsManager,
7777
}
7878

7979
# build the symbol graph

scripts/parse_procthor_files_and_save_to_database.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import re
55
import time
6+
from typing import List
67

78
import tqdm
89
from krrood.ormatic.dao import to_dao

src/semantic_digital_twin/adapters/multi_parser.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy
66
from typing_extensions import Optional, Set, List
77

8+
from ..exceptions import WorldEntityNotFoundError
89
from ..spatial_types.spatial_types import TransformationMatrix
910
from ..world_description.geometry import (
1011
Box,
@@ -18,36 +19,19 @@
1819
from ..world_description.shape_collection import ShapeCollection
1920
from ..world_description.world_entity import KinematicStructureEntity
2021

21-
try:
22-
from multiverse_parser import (
23-
InertiaSource,
24-
UsdImporter,
25-
MjcfImporter,
26-
UrdfImporter,
27-
BodyBuilder,
28-
JointBuilder,
29-
JointType,
30-
Factory,
31-
GeomType,
32-
)
33-
from multiverse_parser.utils import get_relative_transform
34-
from pxr import UsdUrdf, UsdGeom, UsdPhysics, Gf # type: ignore
35-
except ImportError as e:
36-
logging.info(e)
37-
InertiaSource = None
38-
UsdImporter = None
39-
MjcfImporter = None
40-
UrdfImporter = None
41-
BodyBuilder = None
42-
JointBuilder = None
43-
JointType = None
44-
Factory = None
45-
GeomType = None
46-
UsdUrdf = None
47-
UsdGeom = None
48-
UsdPhysics = None
49-
Gf = None
50-
get_relative_transform = None
22+
from multiverse_parser import (
23+
InertiaSource,
24+
UsdImporter,
25+
MjcfImporter,
26+
UrdfImporter,
27+
BodyBuilder,
28+
JointBuilder,
29+
JointType,
30+
Factory,
31+
GeomType,
32+
)
33+
from multiverse_parser.utils import get_relative_transform
34+
from pxr import UsdUrdf, UsdGeom, UsdPhysics, Gf # type: ignore
5135

5236
from ..world_description.connections import (
5337
RevoluteConnection,
@@ -319,7 +303,7 @@ def parse_dof(
319303
"""
320304
try:
321305
return world.get_degree_of_freedom_by_name(free_variable_name)
322-
except KeyError:
306+
except WorldEntityNotFoundError:
323307
if joint_builder.type == JointType.CONTINUOUS:
324308
dof = DegreeOfFreedom(
325309
name=PrefixedName(joint_name),

src/semantic_digital_twin/adapters/multi_sim.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,13 +1224,13 @@ class MultiSimSynchronizer(ModelChangeCallback, ABC):
12241224
"""
12251225

12261226
def _notify(self):
1227-
for modification in self.world._model_modification_blocks[-1]:
1227+
for modification in self.world._model_manager.model_modification_blocks[-1]:
12281228
if isinstance(modification, AddKinematicStructureEntityModification):
12291229
entity = modification.kinematic_structure_entity
12301230
self.entity_spawner.spawn(simulator=self.simulator, entity=entity)
12311231

12321232
def stop(self):
1233-
self.world.model_change_callbacks.remove(self)
1233+
self.world._model_manager.model_change_callbacks.remove(self)
12341234

12351235

12361236
@dataclass

src/semantic_digital_twin/adapters/procthor/procthor_parser.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ def get_world(self) -> Optional[World]:
507507
parent_T_connection_expression=obj_T_child,
508508
)
509509
body_world.merge_world(
510-
child_world, child_connection, handle_duplicates=True
510+
child_world, child_connection, assign_unique_name_to_duplicates=True
511511
)
512512

513513
return body_world
@@ -578,7 +578,9 @@ def parse(self) -> World:
578578
world = World(name=house_name)
579579
with world.modify_world():
580580
world_root = Body(name=PrefixedName(house_name))
581-
world.add_kinematic_structure_entity(world_root, handle_duplicates=True)
581+
world.add_kinematic_structure_entity(
582+
world_root, assign_unique_name_to_duplicates=True
583+
)
582584

583585
self.import_rooms(world, house["rooms"])
584586

@@ -607,7 +609,9 @@ def import_rooms(world: World, rooms: List[Dict]):
607609
child=room_world.root,
608610
parent_T_connection_expression=procthor_room.world_T_room,
609611
)
610-
world.merge_world(room_world, room_connection, handle_duplicates=True)
612+
world.merge_world(
613+
room_world, room_connection, assign_unique_name_to_duplicates=True
614+
)
611615

612616
def import_objects(self, world: World, objects: List[Dict]):
613617
"""
@@ -626,7 +630,9 @@ def import_objects(self, world: World, objects: List[Dict]):
626630
child=obj_world.root,
627631
parent_T_connection_expression=procthor_object.world_T_obj,
628632
)
629-
world.merge_world(obj_world, obj_connection, handle_duplicates=True)
633+
world.merge_world(
634+
obj_world, obj_connection, assign_unique_name_to_duplicates=True
635+
)
630636

631637
def import_walls_and_doors(
632638
self, world: World, walls: List[Dict], doors: List[Dict]
@@ -647,7 +653,9 @@ def import_walls_and_doors(
647653
child=wall_world.root,
648654
parent_T_connection_expression=procthor_wall.world_T_wall,
649655
)
650-
world.merge_world(wall_world, wall_connection, handle_duplicates=True)
656+
world.merge_world(
657+
wall_world, wall_connection, assign_unique_name_to_duplicates=True
658+
)
651659

652660
@staticmethod
653661
def _build_procthor_wall_from_polygon(

src/semantic_digital_twin/adapters/ros/world_fetcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def get_modifications_as_json(self) -> str:
6565
:return: JSON string containing all modification blocks.
6666
"""
6767
modifications_list = [
68-
block.to_json() for block in self.world._model_modification_blocks
68+
block.to_json()
69+
for block in self.world.get_world_model_manager().model_modification_blocks
6970
]
7071
return json.dumps(modifications_list)
7172

src/semantic_digital_twin/adapters/ros/world_synchronizer.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from abc import ABC, abstractmethod
44
from dataclasses import dataclass, field
55
from functools import cached_property
6-
from typing import ClassVar, Optional, Type, List
6+
from typing import ClassVar, Optional, Type, List, Dict
77

88
import numpy as np
99
import rclpy # type: ignore
@@ -18,6 +18,7 @@
1818

1919
from .messages import MetaData, WorldStateUpdate, Message, ModificationBlock, LoadModel
2020
from ...callbacks.callback import Callback, StateChangeCallback, ModelChangeCallback
21+
from ...datastructures.prefixed_name import PrefixedName
2122
from ...orm.ormatic_interface import *
2223
from ...world import World
2324

@@ -223,6 +224,18 @@ def world_callback(self):
223224
self.update_previous_world_state()
224225
self.publish(msg)
225226

227+
def compute_state_changes(self) -> Dict[PrefixedName, float]:
228+
changes = {
229+
name: current_state
230+
for name, previous_state, current_state in zip(
231+
self.world.state.keys(),
232+
self.previous_world_state_data,
233+
self.world.state.positions,
234+
)
235+
if not np.allclose(previous_state, current_state)
236+
}
237+
return changes
238+
226239

227240
@dataclass
228241
class ModelSynchronizer(
@@ -246,7 +259,9 @@ def apply_message(self, msg: ModificationBlock):
246259
def world_callback(self):
247260
msg = ModificationBlock(
248261
meta_data=self.meta_data,
249-
modifications=self.world._model_modification_blocks[-1],
262+
modifications=self.world.get_world_model_manager().model_modification_blocks[
263+
-1
264+
],
250265
)
251266
self.publish(msg)
252267

src/semantic_digital_twin/adapters/urdf.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from urdf_parser_py import urdf as urdfpy
88

99
from ..datastructures.prefixed_name import PrefixedName
10-
from ..exceptions import ParsingError
10+
from ..exceptions import ParsingError, WorldEntityNotFoundError
1111
from ..spatial_types import spatial_types as cas
1212
from ..spatial_types.derivatives import Derivatives, DerivativeMap
1313
from ..spatial_types.spatial_types import TransformationMatrix, Vector3
@@ -171,7 +171,6 @@ def parse(self) -> World:
171171
joints.append(parsed_joint)
172172

173173
[world.add_connection(joint) for joint in joints]
174-
[world.add_kinematic_structure_entity(link) for link in links]
175174

176175
return world
177176

@@ -228,7 +227,7 @@ def parse_joint(
228227

229228
try:
230229
dof = world.get_degree_of_freedom_by_name(dof_name)
231-
except KeyError as e:
230+
except WorldEntityNotFoundError as e:
232231
dof = DegreeOfFreedom(
233232
name=dof_name,
234233
lower_limits=lower_limits,

src/semantic_digital_twin/callbacks/callback.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ class StateChangeCallback(Callback, ABC):
8686
"""
8787

8888
def __post_init__(self):
89-
self.world.state_change_callbacks.append(self)
89+
self.world.state.state_change_callbacks.append(self)
9090
self.update_previous_world_state()
9191

9292
def stop(self):
9393
try:
94-
self.world.state_change_callbacks.remove(self)
94+
self.world.state.state_change_callbacks.remove(self)
9595
except ValueError:
9696
pass
9797

@@ -101,18 +101,6 @@ def update_previous_world_state(self):
101101
"""
102102
self.previous_world_state_data = np.copy(self.world.state.positions)
103103

104-
def compute_state_changes(self) -> Dict[PrefixedName, float]:
105-
changes = {
106-
name: current_state
107-
for name, previous_state, current_state in zip(
108-
self.world.state.keys(),
109-
self.previous_world_state_data,
110-
self.world.state.positions,
111-
)
112-
if not np.allclose(previous_state, current_state)
113-
}
114-
return changes
115-
116104

117105
@dataclass
118106
class ModelChangeCallback(Callback, ABC):
@@ -121,10 +109,10 @@ class ModelChangeCallback(Callback, ABC):
121109
"""
122110

123111
def __post_init__(self):
124-
self.world.model_change_callbacks.append(self)
112+
self.world.get_world_model_manager().model_change_callbacks.append(self)
125113

126114
def stop(self):
127115
try:
128-
self.world.model_change_callbacks.remove(self)
116+
self.world.get_world_model_manager().model_change_callbacks.remove(self)
129117
except ValueError:
130118
pass

src/semantic_digital_twin/collision_checking/trimesh_collision_detector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def sync_world_model(self) -> None:
3636
"""
3737
Synchronize the collision checker with the current world model
3838
"""
39-
if self._last_synced_model == self._world._model_version:
39+
if self._last_synced_model == self._world.get_world_model_manager().version:
4040
return
4141
bodies_to_be_added = set(self._world.bodies_with_enabled_collision) - set(
4242
self._collision_objects.keys()
@@ -58,7 +58,7 @@ def sync_world_state(self) -> None:
5858
"""
5959
Synchronize the collision checker with the current world state
6060
"""
61-
if self._last_synced_state == self._world._state_version:
61+
if self._last_synced_state == self._world.state.version:
6262
return
6363
for body, coll_obj in self._collision_objects.items():
6464
coll_obj.setTransform(

0 commit comments

Comments
 (0)