iterative closest points
CEV ICP algorithm library
Loading...
Searching...
No Matches
icp_math.py
Go to the documentation of this file.
1import sympy as sp
2
3Point = sp.Matrix
4Matrix = sp.Matrix
5
7 def __init__(self):
8 theta, tx, ty = sp.symbols('theta t_x t_y')
9 self.theta = theta
10 self.rotation = Matrix([[sp.cos(theta), -sp.sin(theta)], [sp.sin(theta), sp.cos(theta)]])
11 self.translation = Point([tx, ty])
12
13 def apply_to(self, point: Point, pivot: Point):
14 return self.rotation * (point - pivot) + pivot + self.translation
15
16class Match:
17 def __init__(self):
18 ax, ay, bx, by = sp.symbols('a_x a_y b_x b_y')
19 self.a = Point([ax, ay])
20 self.b = Point([bx, by])
21
22 def cost_with(self, rbt: RigidBodyTransform, pivot: Point):
23 v = rbt.apply_to(self.a, pivot) - self.b - pivot
24 return v[0] ** 2 + v[1] ** 2
25
27 cx, cy = sp.symbols('c_x c_y')
28 return Point([cx, cy])
29
30def latexify(f):
31 return str(f) \
32 .replace('theta', '\\theta') \
33 .replace('cos', '\\cos') \
34 .replace('sin', '\\sin') \
35 .replace('a_x - c_x', 'a\'_x') \
36 .replace('a_y - c_y', 'a\'_y') \
37 .replace('(a\'_x)', 'a\'_x') \
38 .replace('(a\'_y)', 'a\'_y') \
39 .replace('c_x', '\\bar{a}_x') \
40 .replace('c_y', '\\bar{a}_y') \
41 .replace('*', '')
42
43
45pair = Match()
47
48cost = pair.cost_with(rbt, pivot)
49
50print('cost function:')
51print(sp.simplify(cost))
52print()
53
54print('dL/dt_x = ')
55dLdt_x = sp.diff(cost, rbt.translation[0])
56print(latexify(dLdt_x))
57print()
58
59t_x_opt, = list(sp.linsolve([dLdt_x], rbt.translation[0]))[0]
60print('dL/dt_x = 0 when t_x = ')
61print(latexify(t_x_opt))
62print()
63assert(sp.simplify(dLdt_x.subs(rbt.translation[0], t_x_opt)) == 0)
64
65print('dL/dt_y = ')
66dLdt_y = sp.diff(cost, rbt.translation[1])
67print(latexify(dLdt_y))
68print()
69
70t_y_opt, = list(sp.linsolve([dLdt_y], rbt.translation[1]))[0]
71print('dL/dt_y = 0 when t_y = ')
72print(latexify(t_y_opt))
73print()
74assert(sp.simplify(dLdt_y.subs(rbt.translation[1], t_y_opt)) == 0)
75
76print('dL/dtheta = ')
77dLdtheta = sp.diff(cost, rbt.theta)
78print(latexify(sp.simplify(dLdtheta)))
79print()
80
__init__(self)
Definition icp_math.py:17
cost_with(self, RigidBodyTransform rbt, Point pivot)
Definition icp_math.py:22
apply_to(self, Point point, Point pivot)
Definition icp_math.py:13
latexify(f)
Definition icp_math.py:30
make_centroid()
Definition icp_math.py:26