forked from stack-of-tasks/pinocchio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapsule-approximation.py
More file actions
154 lines (127 loc) · 5.27 KB
/
capsule-approximation.py
File metadata and controls
154 lines (127 loc) · 5.27 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
"""
Copyright (c) 2020 INRIA
Inspired from Antonio El Khoury PhD: https://tel.archives-ouvertes.fr/file/index/docid/833019/filename/thesis.pdf
Section 3.8.1 Computing minimum bounding capsules
"""
import numpy as np
import scipy.optimize as optimize
import hppfcl
"""
Capsule definition
a, b: the two extremities of the capsule segment
r: radius of the capsule
"""
EPSILON = 1e-8
CONSTRAINT_INFLATION_RATIO = 5e-3
def capsule_volume(a, b, r):
return np.linalg.norm(b - a) * np.pi * r ** 2 + 4 / 3 * np.pi * r ** 3
def distance_points_segment(p, a, b):
ap = p - a
ab = b - a
t = ap.dot(ab) / ab.dot(ab)
t = np.clip(t, 0, 1)
p_witness = a[None, :] + (b - a)[None, :] * t[:, None]
dist = np.linalg.norm(p - p_witness, axis=1).max()
return dist
def pca_approximation(vertices):
mean = vertices.mean(axis=0)
vertices -= mean
u, s, vh = np.linalg.svd(vertices, full_matrices=True)
components = vh
pca_proj = vertices.dot(components.T)
vertices += mean
a0 = mean + components[0] * (pca_proj[:, 0].min() - EPSILON)
b0 = mean + components[0] * (pca_proj[:, 0].max() + EPSILON)
radius = np.linalg.norm(pca_proj[:, 1:], axis=1).max()
return a0, b0, radius
def capsule_approximation(vertices):
a0, b0, r0 = pca_approximation(vertices)
constraint_inflation = CONSTRAINT_INFLATION_RATIO * r0
x0 = np.array(list(a0) + list(b0) + [r0])
constraint_cap = lambda x: distance_points_segment(vertices, x[:3], x[3:6]) - x[6]
capsule_vol = lambda x: capsule_volume(x[:3], x[3:6], x[6])
constraint = optimize.NonlinearConstraint(
constraint_cap, lb=-np.inf, ub=-constraint_inflation
)
res = optimize.minimize(capsule_vol, x0, constraints=constraint)
res_constraint = constraint_cap(res.x)
assert (
res_constraint <= 1e-4
), "The computed solution is invalid, a vertex is at a distance {:.5f} of the capsule.".format(
res_constraint
)
a, b, r = res.x[:3], res.x[3:6], res.x[6]
return a, b, r
def approximate_mesh(filename, lMg):
mesh_loader = hppfcl.MeshLoader()
mesh = mesh_loader.load(filename, np.ones(3))
vertices = np.array([ lMg * mesh.vertices(i) for i in range(mesh.num_vertices) ])
assert vertices.shape == (mesh.num_vertices, 3)
a, b, r = capsule_approximation(vertices)
return a, b, r
def parse_urdf(infile, outfile):
from lxml import etree
tree = etree.parse(infile)
def get_path(fn):
if fn.startswith('package://'):
relpath = fn[len('package://'):]
import os
for rospath in os.environ['ROS_PACKAGE_PATH'].split(':'):
abspath = os.path.join(rospath, relpath)
if os.path.isfile(abspath):
return abspath
raise ValueError("Could not find " + fn)
return fn
def get_transform(origin):
from pinocchio import SE3, rpy
_xyz = [ float(v) for v in origin.attrib.get('xyz', '0 0 0').split(' ') ]
_rpy = [ float(v) for v in origin.attrib.get('rpy', '0 0 0').split(' ') ]
return SE3 (rpy.rpyToMatrix(*_rpy), np.array(_xyz))
def set_transform(origin, a, b):
from pinocchio import rpy, Quaternion
length = np.linalg.norm(b-a)
z = (b - a) / length
R = Quaternion.FromTwoVectors(np.array([0, 0, 1]), z).matrix()
origin.attrib['xyz'] = " ".join([str(v) for v in ((a+b)/2) ])
origin.attrib['rpy'] = " ".join([str(v) for v in rpy.matrixToRpy(R) ])
from tqdm import tqdm
for mesh in tqdm(tree.xpath('/robot/link/collision/geometry/mesh'), desc="Generating capsules"):
geom = mesh.getparent()
coll = geom.getparent()
link = coll.getparent()
if coll.find('origin') is None:
o = etree.Element("origin")
o.tail = geom.tail
coll.insert(0, o)
origin = coll.find('origin')
lMg = get_transform(origin)
meshfile = get_path(mesh.attrib['filename'])
import os
name = os.path.basename(meshfile)
# Generate capsule
a, b, radius = approximate_mesh (meshfile, lMg)
length = np.linalg.norm(b-a)
set_transform(origin, a, b)
mesh.tag = "cylinder"
mesh.attrib.pop('filename')
mesh.attrib['radius'] = str(radius)
mesh.attrib['length'] = str(length)
coll.attrib['name'] = name
if link.find('collision_checking') is None:
link.append(etree.Element('collision_checking'))
collision_checking = link.find('collision_checking')
collision_checking.append(etree.Element('capsule'))
collision_checking[-1].attrib['name'] = name
tree.write(outfile)
if __name__ == "__main__":
# Example for a single capsule
#filename = "mesh.obj"
#mesh_loader = hppfcl.MeshLoader()
#mesh = mesh_loader.load(filename, np.ones(3))
#vertices = mesh.vertices()
#a, b, r = capsule_approximation(vertices)
# Example for a whole URDF model
# This path refers to Pinocchio source code but you can define your own directory here.
pinocchio_model_dir = join(dirname(dirname(str(abspath(__file__)))), "models")
urdf_filename = pinocchio_model_dir + "models/example-robot-data/robots/ur_description/urdf/ur5_gripper.urdf"
parse_urdf(urdf_filename, "ur5_gripper_with_capsules.urdf")