-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurdf_metadata_extractor.py
More file actions
596 lines (483 loc) · 22.1 KB
/
urdf_metadata_extractor.py
File metadata and controls
596 lines (483 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#!/usr/bin/env python3
"""
URDF Metadata Extractor - Extract joint information and bounding boxes from URDF files
Enhanced version with bounding box calculation - uses only Python standard library
"""
import math
import os
import xml.etree.ElementTree as ET
from typing import Dict, List, Optional, Union
class URDFMetadataExtractor:
"""Extract joint information and bounding boxes from URDF files without external dependencies."""
def __init__(self):
self.joints = []
self.links = []
self.link_geometries = {}
self.link_bounding_boxes = {}
def parse_urdf(self, urdf_file: str) -> List[Dict[str, str]]:
"""
Parse a URDF file and extract joint information.
Args:
urdf_file: Path to the URDF file
Returns:
List of dictionaries containing joint information
"""
try:
tree = ET.parse(urdf_file)
root = tree.getroot()
# Extract joints (original functionality)
self.joints = []
for joint in root.findall('joint'):
joint_info = {'name': joint.get('name', 'unnamed'), 'type': joint.get('type', 'unknown')}
# Get parent and child links
parent = joint.find('parent')
child = joint.find('child')
if parent is not None:
joint_info['parent_link'] = parent.get('link', 'unknown')
if child is not None:
joint_info['child_link'] = child.get('link', 'unknown')
# Get joint limits if available
limit = joint.find('limit')
if limit is not None:
joint_info['lower_limit'] = limit.get('lower', 'N/A')
joint_info['upper_limit'] = limit.get('upper', 'N/A')
joint_info['effort'] = limit.get('effort', 'N/A')
joint_info['velocity'] = limit.get('velocity', 'N/A')
# Get axis if available
axis = joint.find('axis')
if axis is not None:
joint_info['axis'] = axis.get('xyz', '1 0 0')
self.joints.append(joint_info)
# Extract links for reference (original functionality)
self.links = [link.get('name', 'unnamed') for link in root.findall('link')]
# NEW: Extract link geometries and calculate bounding boxes
self._extract_link_geometries(root)
self._calculate_bounding_boxes()
return self.joints
except ET.ParseError as e:
print(f"Error parsing XML in {urdf_file}: {e}")
return []
except FileNotFoundError:
print(f"File not found: {urdf_file}")
return []
except Exception as e:
print(f"Unexpected error processing {urdf_file}: {e}")
return []
def get_joint_names(self) -> List[str]:
"""Get list of joint names in order."""
return [joint['name'] for joint in self.joints]
def get_movable_joints(self) -> List[Dict[str, str]]:
"""Get only movable joints (revolute, prismatic, continuous)."""
movable_types = ['revolute', 'prismatic', 'continuous']
return [joint for joint in self.joints if joint['type'] in movable_types]
def print_joint_summary(self, urdf_file: str):
"""Print a formatted summary of joints in the URDF."""
print(f"\n{'='*60}")
print(f"URDF File: {os.path.basename(urdf_file)}")
print(f"{'='*60}")
if not self.joints:
print("No joints found in this URDF file.")
return
print(f"\nTotal joints: {len(self.joints)}")
print(f"Total links: {len(self.links)}")
# Count joint types
joint_types = {}
for joint in self.joints:
jtype = joint['type']
joint_types[jtype] = joint_types.get(jtype, 0) + 1
print("\nJoint types:")
for jtype, count in joint_types.items():
print(f" - {jtype}: {count}")
print("\nJoints in order:")
for i, joint in enumerate(self.joints, 1):
print(f"\n {i}. {joint['name']}")
print(f" Type: {joint['type']}")
if 'parent_link' in joint:
print(f" Parent: {joint['parent_link']}")
if 'child_link' in joint:
print(f" Child: {joint['child_link']}")
if 'axis' in joint:
print(f" Axis: {joint['axis']}")
if joint['type'] in ['revolute', 'prismatic']:
if 'lower_limit' in joint:
print(f" Limits: [{joint['lower_limit']}, {joint['upper_limit']}]")
def _extract_link_geometries(self, root: ET.Element):
"""Extract geometry information from all links."""
self.link_geometries = {}
for link in root.findall('link'):
link_name = link.get('name', 'unnamed')
# Extract collision geometries (prioritize these for bounding boxes)
collision_geoms = []
for collision in link.findall('collision'):
geom_data = self._parse_geometry_element(collision)
if geom_data:
collision_geoms.append(geom_data)
# Extract visual geometries as fallback
visual_geoms = []
for visual in link.findall('visual'):
geom_data = self._parse_geometry_element(visual)
if geom_data:
visual_geoms.append(geom_data)
# Store geometry data
self.link_geometries[link_name] = {
'collision': collision_geoms,
'visual': visual_geoms
}
def _parse_geometry_element(self, element: ET.Element) -> Optional[Dict]:
"""Parse a collision or visual element to extract geometry and transform."""
geometry = element.find('geometry')
if geometry is None:
return None
# Parse origin (transformation)
origin = element.find('origin')
transform = self._parse_origin(origin)
# Parse geometry type
geom_info = None
# Box geometry
box = geometry.find('box')
if box is not None:
size_str = box.get('size', '1 1 1')
size = [float(x) for x in size_str.split()]
geom_info = {
'type': 'box',
'size': size
}
# Cylinder geometry
cylinder = geometry.find('cylinder')
if cylinder is not None:
radius = float(cylinder.get('radius', '1'))
length = float(cylinder.get('length', '1'))
geom_info = {
'type': 'cylinder',
'radius': radius,
'length': length
}
# Sphere geometry
sphere = geometry.find('sphere')
if sphere is not None:
radius = float(sphere.get('radius', '1'))
geom_info = {
'type': 'sphere',
'radius': radius
}
# Mesh geometry
mesh = geometry.find('mesh')
if mesh is not None:
filename = mesh.get('filename', '')
scale_str = mesh.get('scale', '1 1 1')
scale = [float(x) for x in scale_str.split()]
geom_info = {
'type': 'mesh',
'filename': filename,
'scale': scale
}
if geom_info:
geom_info['transform'] = transform
return geom_info
return None
def _parse_origin(self, origin: Optional[ET.Element]) -> Dict[str, List[float]]:
"""Parse origin element to get xyz translation and rpy rotation."""
if origin is None:
return {'xyz': [0.0, 0.0, 0.0], 'rpy': [0.0, 0.0, 0.0]}
xyz_str = origin.get('xyz', '0 0 0')
rpy_str = origin.get('rpy', '0 0 0')
xyz = [float(x) for x in xyz_str.split()]
rpy = [float(x) for x in rpy_str.split()]
return {'xyz': xyz, 'rpy': rpy}
def _calculate_bounding_boxes(self):
"""Calculate bounding boxes for all links."""
self.link_bounding_boxes = {}
for link_name, geometries in self.link_geometries.items():
# Prioritize collision geometries, fall back to visual
geom_list = geometries['collision'] if geometries['collision'] else geometries['visual']
if not geom_list:
continue
# Calculate bounding box for each geometry and combine
all_bounds = []
for geom in geom_list:
bounds = self._calculate_geometry_bounds(geom)
if bounds:
all_bounds.append(bounds)
if all_bounds:
combined_bounds = self._combine_bounds(all_bounds)
self.link_bounding_boxes[link_name] = combined_bounds
def _calculate_geometry_bounds(self, geom: Dict) -> Optional[Dict[str, List[float]]]:
"""Calculate bounding box for a single geometry."""
geom_type = geom['type']
transform = geom['transform']
# Get local bounding box based on geometry type
local_bounds = None
if geom_type == 'box':
size = geom['size']
local_bounds = {
'min': [-size[0]/2, -size[1]/2, -size[2]/2],
'max': [size[0]/2, size[1]/2, size[2]/2]
}
elif geom_type == 'cylinder':
radius = geom['radius']
length = geom['length']
local_bounds = {
'min': [-radius, -radius, -length/2],
'max': [radius, radius, length/2]
}
elif geom_type == 'sphere':
radius = geom['radius']
local_bounds = {
'min': [-radius, -radius, -radius],
'max': [radius, radius, radius]
}
elif geom_type == 'mesh':
# For mesh, we can't calculate exact bounds without loading the file
# Provide a reasonable default or indicate uncertainty
scale = geom['scale']
# Assume a unit cube and scale it
local_bounds = {
'min': [-scale[0]/2, -scale[1]/2, -scale[2]/2],
'max': [scale[0]/2, scale[1]/2, scale[2]/2]
}
# Add metadata to indicate this is estimated
local_bounds['estimated'] = True
local_bounds['mesh_file'] = geom['filename']
if local_bounds is None:
return None
# Apply transformation to bounds
transformed_bounds = self._transform_bounds(local_bounds, transform)
return transformed_bounds
def _transform_bounds(self, bounds: Dict[str, List[float]], transform: Dict[str, List[float]]) -> Dict[str, List[float]]:
"""Apply transformation to bounding box."""
xyz = transform['xyz']
rpy = transform['rpy']
# Get all 8 corners of the bounding box
min_pt = bounds['min']
max_pt = bounds['max']
corners = [
[min_pt[0], min_pt[1], min_pt[2]],
[min_pt[0], min_pt[1], max_pt[2]],
[min_pt[0], max_pt[1], min_pt[2]],
[min_pt[0], max_pt[1], max_pt[2]],
[max_pt[0], min_pt[1], min_pt[2]],
[max_pt[0], min_pt[1], max_pt[2]],
[max_pt[0], max_pt[1], min_pt[2]],
[max_pt[0], max_pt[1], max_pt[2]]
]
# Transform each corner
transformed_corners = []
for corner in corners:
transformed_corner = self._transform_point(corner, xyz, rpy)
transformed_corners.append(transformed_corner)
# Find new min/max
new_min = [min(corner[i] for corner in transformed_corners) for i in range(3)]
new_max = [max(corner[i] for corner in transformed_corners) for i in range(3)]
result = {
'min': new_min,
'max': new_max
}
# Preserve metadata
if 'estimated' in bounds:
result['estimated'] = bounds['estimated']
if 'mesh_file' in bounds:
result['mesh_file'] = bounds['mesh_file']
return result
def _transform_point(self, point: List[float], translation: List[float], rotation: List[float]) -> List[float]:
"""Transform a point by translation and rotation (RPY)."""
# Apply rotation first (RPY = Roll, Pitch, Yaw)
roll, pitch, yaw = rotation
# Rotation matrices
cos_r, sin_r = math.cos(roll), math.sin(roll)
cos_p, sin_p = math.cos(pitch), math.sin(pitch)
cos_y, sin_y = math.cos(yaw), math.sin(yaw)
# Combined rotation matrix (ZYX order)
r11 = cos_y * cos_p
r12 = cos_y * sin_p * sin_r - sin_y * cos_r
r13 = cos_y * sin_p * cos_r + sin_y * sin_r
r21 = sin_y * cos_p
r22 = sin_y * sin_p * sin_r + cos_y * cos_r
r23 = sin_y * sin_p * cos_r - cos_y * sin_r
r31 = -sin_p
r32 = cos_p * sin_r
r33 = cos_p * cos_r
# Apply rotation
x, y, z = point
rotated_x = r11 * x + r12 * y + r13 * z
rotated_y = r21 * x + r22 * y + r23 * z
rotated_z = r31 * x + r32 * y + r33 * z
# Apply translation
final_x = rotated_x + translation[0]
final_y = rotated_y + translation[1]
final_z = rotated_z + translation[2]
return [final_x, final_y, final_z]
def _combine_bounds(self, bounds_list: List[Dict[str, List[float]]]) -> Dict[str, List[float]]:
"""Combine multiple bounding boxes into one."""
if not bounds_list:
return None
all_mins = [bounds['min'] for bounds in bounds_list]
all_maxs = [bounds['max'] for bounds in bounds_list]
combined_min = [min(mins[i] for mins in all_mins) for i in range(3)]
combined_max = [max(maxs[i] for maxs in all_maxs) for i in range(3)]
result = {
'min': combined_min,
'max': combined_max
}
# Check if any bounds were estimated
if any(bounds.get('estimated', False) for bounds in bounds_list):
result['estimated'] = True
result['mesh_files'] = [bounds['mesh_file'] for bounds in bounds_list
if bounds.get('mesh_file')]
return result
# ==================== DOF COUNTING METHODS ====================
@staticmethod
def get_joint_dof_count(joint_type: str) -> int:
"""Get the number of DOFs for a specific joint type.
Args:
joint_type: Type of joint ('revolute', 'prismatic', 'continuous', 'planar', 'floating', 'fixed')
Returns:
Number of DOFs for this joint type
"""
dof_mapping = {
'revolute': 1,
'prismatic': 1,
'continuous': 1,
'planar': 2,
'floating': 6,
'fixed': 0
}
return dof_mapping.get(joint_type.lower(), 0)
def get_total_movable_dofs(self) -> int:
"""Get total number of DOFs for all movable joints.
Returns:
Total DOF count for movable joints
"""
total_dofs = 0
movable_joints = self.get_movable_joints()
for joint in movable_joints:
joint_type = joint['type']
total_dofs += self.get_joint_dof_count(joint_type)
return total_dofs
def get_dof_counts_by_joint(self) -> Dict[str, int]:
"""Get DOF count for each joint by name.
Returns:
Dictionary mapping joint_name -> dof_count
"""
dof_counts = {}
for joint in self.joints:
joint_name = joint['name']
joint_type = joint['type']
dof_counts[joint_name] = self.get_joint_dof_count(joint_type)
return dof_counts
def get_movable_joint_names_with_dofs(self) -> List[Dict[str, Union[str, int]]]:
"""Get movable joints with their DOF counts.
Returns:
List of dicts with 'name', 'type', and 'dofs' for each movable joint
"""
movable_joints = self.get_movable_joints()
result = []
for joint in movable_joints:
joint_info = joint.copy()
joint_info['dofs'] = self.get_joint_dof_count(joint['type'])
result.append(joint_info)
return result
# ==================== NEW PUBLIC METHODS ====================
def get_link_bounding_boxes(self) -> Dict[str, Dict[str, List[float]]]:
"""Get bounding boxes for all links."""
return self.link_bounding_boxes.copy()
def get_link_bounding_box(self, link_name: str) -> Optional[Dict[str, List[float]]]:
"""Get bounding box for a specific link."""
return self.link_bounding_boxes.get(link_name)
def get_overall_bounding_box(self) -> Optional[Dict[str, List[float]]]:
"""Get overall bounding box encompassing all links."""
if not self.link_bounding_boxes:
return None
all_bounds = list(self.link_bounding_boxes.values())
return self._combine_bounds(all_bounds)
def get_link_geometries(self) -> Dict[str, Dict[str, List[Dict]]]:
"""Get raw geometry data for all links."""
return self.link_geometries.copy()
def print_bounding_box_summary(self, urdf_file: str):
"""Print a formatted summary of bounding boxes."""
print(f"\n{'='*60}")
print(f"BOUNDING BOX SUMMARY: {os.path.basename(urdf_file)}")
print(f"{'='*60}")
if not self.link_bounding_boxes:
print("No bounding boxes calculated.")
return
print(f"\nLinks with bounding boxes: {len(self.link_bounding_boxes)}")
# Overall bounding box
overall = self.get_overall_bounding_box()
if overall:
print(f"\nOverall robot bounding box:")
print(f" Min: [{overall['min'][0]:.3f}, {overall['min'][1]:.3f}, {overall['min'][2]:.3f}]")
print(f" Max: [{overall['max'][0]:.3f}, {overall['max'][1]:.3f}, {overall['max'][2]:.3f}]")
size = [overall['max'][i] - overall['min'][i] for i in range(3)]
print(f" Size: [{size[0]:.3f}, {size[1]:.3f}, {size[2]:.3f}]")
if overall.get('estimated'):
print(f" Note: Contains estimated mesh bounds")
# Individual link bounding boxes
print(f"\nIndividual link bounding boxes:")
for link_name, bounds in self.link_bounding_boxes.items():
print(f"\n {link_name}:")
print(f" Min: [{bounds['min'][0]:.3f}, {bounds['min'][1]:.3f}, {bounds['min'][2]:.3f}]")
print(f" Max: [{bounds['max'][0]:.3f}, {bounds['max'][1]:.3f}, {bounds['max'][2]:.3f}]")
size = [bounds['max'][i] - bounds['min'][i] for i in range(3)]
print(f" Size: [{size[0]:.3f}, {size[1]:.3f}, {size[2]:.3f}]")
if bounds.get('estimated'):
print(f" Note: Estimated from mesh file(s)")
def print_full_summary(self, urdf_file: str):
"""Print both joint and bounding box summaries."""
self.print_joint_summary(urdf_file)
self.print_bounding_box_summary(urdf_file)
if __name__ == "__main__":
"""Example: Process a single URDF file with enhanced functionality."""
extractor = URDFMetadataExtractor()
# Replace with your actual URDF file path
urdf_file = "/home/yuhao2024/Documents/Genesis_latest/genesis/assets/urdf/go2/urdf/go2.urdf"
# Parse URDF (now extracts both joints and bounding boxes)
joints = extractor.parse_urdf(urdf_file)
# Original functionality still works
joint_names = extractor.get_joint_names()
print("Joint names in order:", joint_names)
movable = extractor.get_movable_joints()
print("\nMovable joints:")
for joint in movable:
print(f" - {joint['name']} ({joint['type']})")
# NEW: DOF counting functionality
print("\n" + "="*60)
print("NEW DOF COUNTING FUNCTIONALITY")
print("="*60)
# Test DOF counting methods
total_dofs = extractor.get_total_movable_dofs()
print(f"\nTotal movable DOFs: {total_dofs}")
# Show DOF count per joint type
joint_type_counts = {}
dof_counts = extractor.get_dof_counts_by_joint()
for joint_name, dof_count in dof_counts.items():
joint_info = next((j for j in extractor.joints if j['name'] == joint_name), None)
if joint_info:
joint_type = joint_info['type']
if joint_type not in joint_type_counts:
joint_type_counts[joint_type] = {'count': 0, 'total_dofs': 0}
joint_type_counts[joint_type]['count'] += 1
joint_type_counts[joint_type]['total_dofs'] += dof_count
print(f"\nJoint types and their DOF contributions:")
for joint_type, data in joint_type_counts.items():
dof_per_joint = extractor.get_joint_dof_count(joint_type)
print(f" - {joint_type}: {data['count']} joints × {dof_per_joint} DOF = {data['total_dofs']} DOFs")
# Show movable joints with DOF info
movable_with_dofs = extractor.get_movable_joint_names_with_dofs()
print(f"\nMovable joints with DOF counts:")
for joint in movable_with_dofs:
print(f" - {joint['name']} ({joint['type']}): {joint['dofs']} DOF(s)")
# NEW: Bounding box functionality
print("\n" + "="*60)
print("NEW BOUNDING BOX FUNCTIONALITY")
print("="*60)
# Get bounding boxes
bounding_boxes = extractor.get_link_bounding_boxes()
print(f"\nFound bounding boxes for {len(bounding_boxes)} links")
# Get overall bounding box
overall_bbox = extractor.get_overall_bounding_box()
if overall_bbox:
print(f"\nOverall robot dimensions:")
size = [overall_bbox['max'][i] - overall_bbox['min'][i] for i in range(3)]
print(f" Size: {size[0]:.3f} x {size[1]:.3f} x {size[2]:.3f}")
# Print detailed summaries
extractor.print_full_summary(urdf_file)