diff --git a/scene_graph_room_classification/scene_graph_fusion/scene_graph_fusion_node.py b/scene_graph_room_classification/scene_graph_fusion/scene_graph_fusion_node.py index 505f35e..385bfb2 100644 --- a/scene_graph_room_classification/scene_graph_fusion/scene_graph_fusion_node.py +++ b/scene_graph_room_classification/scene_graph_fusion/scene_graph_fusion_node.py @@ -376,7 +376,7 @@ class GraphManagementNode(Node): def yolo_cloud_callback(self, msg: PointCloud2): """ - Speichert die letzte YOLO-PointCloud als Nx3 numpy Array im Buffer. + Stores the latest YOLO point cloud as an Nx3 NumPy array in the buffer. """ try: cloud_np = GraphManagementNode.pointcloud2_to_xyz_array(msg) # Nx3 @@ -388,7 +388,7 @@ class GraphManagementNode(Node): self._try_process_yolo() except Exception as e: - self.get_logger().warn(f"Fehler beim Verarbeiten der YOLO-Cloud: {e}") + self.get_logger().warn(f"Error processing the YOLO cloud: {e}") def rtdetr_bboxes_callback(self, msg): if len(msg.bbox) == 0: @@ -400,7 +400,7 @@ class GraphManagementNode(Node): def rtdetr_cloud_callback(self, msg: PointCloud2): """ - Speichert die letzte RT-DETR-PointCloud als Nx3 numpy Array im Buffer. + Stores the latest RT-DETR point cloud as an Nx3 NumPy array in the buffer. """ try: cloud_np = GraphManagementNode.pointcloud2_to_xyz_array(msg) # Nx3 @@ -412,7 +412,7 @@ class GraphManagementNode(Node): self._try_process_rtdetr() except Exception as e: - self.get_logger().warn(f"Fehler beim Verarbeiten der RT-DETR-Cloud: {e}") + self.get_logger().warn(f"Error processing the RT-DETR cloud: {e}") def _try_process_yolo(self): # Cancel if Graph Updates are paused @@ -765,42 +765,42 @@ class GraphManagementNode(Node): int: Node ID if object exists in graph, -1 if new object """ nodes = list(self.scene_graph.nodes) - min_dist = 1000.0 # Startwert unendlich + min_dist = 1000.0 # Initial value (infinity) best_node = -1 - # Threshold: Wie nah müssen sich die Zentren sein? (in Metern) - # 0.5m ist meistens gut für Möbel. Bei kleineren Objekten evtl. 0.3m. + # Threshold: How close do the centers need to be? (in meters) + # 0.5m is usually good for furniture. For smaller objects, 0.3m might be better. DISTANCE_THRESHOLD = 1 - # Berechne Center der neuen Box + # Calculate center of the new box new_center = self.calculate_bounding_box_center(object.bounding_box) for node in nodes: if node in self.scene_graph and 'data' in self.scene_graph.nodes[node]: node_data = self.scene_graph.nodes[node]['data'] - # Nur Objekt-Nodes checken + # Only check object nodes if type(node_data) is ObjectNode: - # 1. Label Check: Ein Stuhl wird nicht zum Tisch + # 1. Label Check: A chair should not become a table if node_data.class_id != object.name.data: continue - # 2. Berechne Center der existierenden Box + # 2. Calculate center of the existing box old_center = self.calculate_bounding_box_center(node_data.bounding_box) - # 3. Euklidische Distanz berechnen + # 3. Calculate Euclidean distance dist = math.sqrt( (new_center.x - old_center.x)**2 + (new_center.y - old_center.y)**2 + (new_center.z - old_center.z)**2 ) - # Debugging (optional, falls es immer noch nicht geht): + # Debugging (optional, in case it still doesn't work): # self.get_logger().info(f"Check Node {node}: Dist={dist:.2f}, Label={node_data.class_id}") if dist < DISTANCE_THRESHOLD: - # Wir suchen den nähsten passenden Nachbarn + # Find the closest matching neighbor if dist < min_dist: min_dist = dist best_node = node @@ -809,15 +809,15 @@ class GraphManagementNode(Node): def find_object_by_class(self, target_class: str): """ - Durchsucht den Graphen nach dem ersten ObjectNode mit der angegebenen Klasse. + Searches the graph for the first ObjectNode with the specified class. Args: - target_class (str): Der gesuchte Klassenname (z.B. 'bottle'). + target_class (str): The class name being searched for (e.g., 'bottle'). Returns: - ObjectNode | None: Das gefundene Objekt oder None, wenn nichts gefunden wurde. + ObjectNode | None: The found object or None if no match was found. """ - # Lock erwerben, um thread-safe zu lesen + # Acquire lock for thread-safe reading while self.graph_lock: time.sleep(0.01) self.graph_lock = True @@ -826,13 +826,13 @@ class GraphManagementNode(Node): for node_id, attrs in self.scene_graph.nodes(data=True): data = attrs.get('data') - # Wir suchen nur nach ObjectNodes (oder auch FunctionalElementNode, falls gewünscht) + # We only search for ObjectNodes (or FunctionalElementNode, if desired) if isinstance(data, ObjectNode): if data.class_id == target_class: - self.get_logger().info(f"Objekt gefunden: ID {data.id}, Class '{data.class_id}'") + self.get_logger().info(f"Object found: ID {data.id}, Class '{data.class_id}'") return data - self.get_logger().info(f"Kein Objekt mit Class '{target_class}' gefunden.") + self.get_logger().info(f"No object with class '{target_class}' found.") return None finally: @@ -840,14 +840,14 @@ class GraphManagementNode(Node): def is_object_in_room(self, object_node: ObjectNode, room_node: RoomNode) -> bool: """ - Prüft, ob das Objekt (über seinen 2D-Zentroid) in der Raum-Polygonfläche liegt. + Checks if object's 2D centroid is inside the room polygon. """ # Does Room have info? if room_node.polygon is None or len(room_node.polygon) == 0: return False # Room-Polygon -> Shapely-Polygon - coords = [(p.x, p.y) for p in room_node.polygon] # Liste von Point32 → (x,y) + coords = [(p.x, p.y) for p in room_node.polygon] # List of Point32 → (x,y) poly = ShapelyPolygon(coords) # Get Position from object @@ -858,16 +858,16 @@ class GraphManagementNode(Node): def _find_matching_functional_node(self, centroid, class_id=None, threshold=0.15): """ - Findet einen bestehenden FunctionalElementNode, der gut zu einem neuen - funktionalen Objekt passt. + Finds an existing FunctionalElementNode that is a good match for a new + functional object. Args: - centroid (np.ndarray): 3D-Zentroid der neuen funktionalen Punktwolke. - class_id (str): Label des funktionalen Elements (z.B. "drawer_handle"). - threshold (float): maximale Distanz in Metern, damit ein Match akzeptiert wird. + centroid (np.ndarray): 3D centroid of the new functional point cloud. + class_id (str): Label of the functional element (e.g., "drawer_handle"). + threshold (float): Maximum distance in meters for a match to be accepted. Returns: - int | None: Node-ID eines passenden FunctionalElementNode oder None. + int | None: Node ID of a matching FunctionalElementNode, or None if no match is found. """ best_node_id = None min_dist = float('inf') @@ -903,18 +903,18 @@ class GraphManagementNode(Node): def _find_closest_object_node(self, centroid, label=None, threshold=1): """ - Hilfsfunktion: Findet den ObjectNode, der dem gegebenen Zentroid am nächsten ist. - Wird genutzt, um PointClouds oder FunctionalObjects dem richtigen Eltern-Objekt zuzuordnen. + Helper function: Finds the ObjectNode closest to the given centroid. + Used to associate point clouds or functional objects with the correct parent object. Args: - centroid (np.ndarray): 3D-Zentroid der neuen Punktwolke (x, y, z) - label (str, optional): Klassenlabel (z.B. YOLO-Label), wenn nur nach - Objekten mit diesem Label gesucht werden soll. - threshold (float): maximale Distanz in Metern, damit ein Objekt als Match gilt. + centroid (np.ndarray): 3D centroid of the new point cloud (x, y, z). + label (str, optional): Class label (e.g., YOLO label) if the search should be + restricted to objects with this label. + threshold (float): Maximum distance in meters for an object to be considered a match. Returns: - int | None: Node-ID des nächsten passenden ObjectNode oder None, wenn - kein Objekt innerhalb des Thresholds gefunden wurde. + int | None: Node ID of the nearest matching ObjectNode, or None if no object + was found within the threshold. """ closest_node = None min_dist = float('inf') @@ -955,8 +955,8 @@ class GraphManagementNode(Node): def _get_node_centroid(self, node): """ - Berechnet den 3D-Zentroid eines Objekt-Nodes aus seiner Open3D-Pointcloud. - Gibt None zurück, wenn die Wolke leer ist. + Calculates the 3D centroid of an ObjectNode from its Open3D point cloud. + Returns None if the cloud is empty. """ # Open3D → Numpy points = np.asarray(node.point_cloud.points) @@ -966,8 +966,8 @@ class GraphManagementNode(Node): def get_object_2d_position(self, object_node: ObjectNode): """ - Gibt die (x, y)-Position eines Objekts zurück, basierend auf dem - Zentroid der Pointcloud (z wird ignoriert). + Returns the (x, y) position of an object based on the point + cloud's centroid (z is ignored). """ centroid = self._get_node_centroid(object_node) if centroid is None: @@ -976,15 +976,15 @@ class GraphManagementNode(Node): def _merge_nearby_object_nodes(self, base_id: int, merge_threshold: float = None) -> int: """ - Merged andere ObjectNodes in der Nähe in den base_id Node hinein. - - Nur gleiche class_id - - Distanztest über Centroid - - Übernimmt Kanten (Edges) des entfernten Nodes - - Merge von graspable per OR - - Optional: vereinigt bounding_box (AABB) falls vorhanden + Merges other nearby ObjectNodes into the base_id node. + - Only nodes with the same class_id + - Distance check via centroid + - Inherits edges from the removed nodes + - Merges 'graspable' property via logical OR + - Optional: Merges bounding boxes (AABB) if available Returns: - int: Anzahl der gemergten/entfernten Nodes + int: Number of merged/removed nodes """ if merge_threshold is None: merge_threshold = getattr(self, "object_node_merge_threshold", 1.2) @@ -1073,24 +1073,22 @@ class GraphManagementNode(Node): base_data.bounding_box = [min_pt, max_pt] - # ================================================================ - # GEÄNDERT: Übernahme der funktionalen Kinder (N Kinder -> 1 Parent) - # ================================================================ + # Transferring functional children (N children -> 1 parent) children_ids = list(other_data.functional_element_ids) for f_id in children_ids: if f_id in self.scene_graph: func_node = self.scene_graph.nodes[f_id]['data'] if isinstance(func_node, FunctionalElementNode): - # Wir setzen den EINZIGEN Parent auf die neue base_id + # Set the SINGLE parent to the new base_id func_node.parent_object_id = base_id - # Kante aktualisieren + # Update edge if self.scene_graph.has_edge(other_id, f_id): self.scene_graph.remove_edge(other_id, f_id) if not self.scene_graph.has_edge(base_id, f_id): self.scene_graph.add_edge(base_id, f_id) - # Die IDs in den Base-Node (ObjectNode) übernehmen + # Transfer the IDs into the base node (ObjectNode) base_data.functional_element_ids.update(other_data.functional_element_ids) # copy edges (neighbors) @@ -1114,17 +1112,17 @@ class GraphManagementNode(Node): def _assign_parent_for_functional(self, func_node_id, centroid, object_threshold=0.5): """ - Weist einem funktionalen Node einen Elternknoten zu: - - bevorzugt ein räumlich nahes ObjectNode - - ansonsten den aktuellen Raum (Orphan-Fall, z.B. Flasche) + Assigns a parent node to a functional node: + - Prefers a spatially nearby ObjectNode. + - Otherwise, assigns to the current room (orphan case, e.g., a bottle). Args: - func_node_id (int): Node-ID des FunctionalElementNode im Graph - centroid (np.ndarray): Zentroid der funktionalen Punktwolke (x,y,z) - object_threshold (float): max. Distanz in Metern für Objekt-Matching + func_node_id (int): Node ID of the FunctionalElementNode in the graph. + centroid (np.ndarray): Centroid of the functional point cloud (x, y, z). + object_threshold (float): Max distance in meters for object matching. Returns: - int | None: Parent-ObjectNode-ID oder None (wenn Orphan im Raum hängt) + int | None: Parent ObjectNode ID or None (if it remains an orphan in the room). """ if func_node_id not in self.scene_graph: return None