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