機械学習基礎理論独習

誤りがあればご指摘いただけると幸いです。数式が整うまで少し時間かかります。リンクフリーです。

勉強ログです。リンクフリーです
目次へ戻る

2次元の剛体変換を任意点回りの回転に変換

はじめに

剛体変換って、「任意点回りの回転に変換できるんじゃないか」と思って、
まずは、2次元で計算してみるかって記事です。

2次元の剛体変換を任意点周りの回転に変換

剛体変換行列を {\bf M}\in{\mathbb R}^{3\times3} とします。

\begin{eqnarray}
{\bf M}=
\begin{pmatrix}
m_0 & m_2 & m_4\\
m_1 & m_3 & m_5\\
0 & 0 & 1\\
\end{pmatrix}\tag{1}
\end{eqnarray}

回転行列を {\bf R}\in{\mathbb R}^{3\times3}、平行移動行列を {\bf T}\in{\mathbb R}^{3\times3} とします。

\begin{eqnarray}
{\bf R}=
\begin{pmatrix}
r_0 & r_2 & 0\\
r_1 & r_3 & 0\\
0 & 0 & 1
\end{pmatrix}
,\ 
{\bf T}=
\begin{pmatrix}
1 & 0 & t_x\\
0 & 1 & t_y\\
0 & 0 & 1
\end{pmatrix}\tag{2}
\end{eqnarray}

任意点周りの行列は {\bf T}{\bf R}{\bf T}^{-1} と書けるので、以下が成り立ちます。

\begin{eqnarray}
&&{\bf T}{\bf R}{\bf T}^{-1}={\bf M}\\
&&\Leftrightarrow
\begin{pmatrix}
1 & 0 & t_x\\
0 & 1 & t_y\\
0 & 0 & 1
\end{pmatrix}
\begin{pmatrix}
r_0 & r_2 & 0\\
r_1 & r_3 & 0\\
0 & 0 & 1
\end{pmatrix}
\begin{pmatrix}
1 & 0 & -t_x\\
0 & 1 & -t_y\\
0 & 0 & 1
\end{pmatrix}
=
\begin{pmatrix}
m_0 & m_2 & m_4\\
m_1 & m_3 & m_5\\
0 & 0 & 1\\
\end{pmatrix}\tag{3}
\end{eqnarray}

(3) の左辺を計算します。

\begin{eqnarray}
\begin{pmatrix}
1 & 0 & t_x\\
0 & 1 & t_y\\
0 & 0 & 1
\end{pmatrix}
\begin{pmatrix}
r_0 & r_2 & 0\\
r_1 & r_3 & 0\\
0 & 0 & 1
\end{pmatrix}
\begin{pmatrix}
1 & 0 & -t_x\\
0 & 1 & -t_y\\
0 & 0 & 1
\end{pmatrix}
&=&
\begin{pmatrix}
r_0 & r_2 & t_x\\
r_1 & r_3 & t_y\\
0 & 0 & 1
\end{pmatrix}
\begin{pmatrix}
1 & 0 & -t_x\\
0 & 1 & -t_y\\
0 & 0 & 1
\end{pmatrix}\\
&=&
\begin{pmatrix}
r_0 & r_2 & -r_0t_x-r_2t_y+t_x\\
r_1 & r_3 & -r_1t_x-r_3t_y+t_y\\
0 & 0 & 1
\end{pmatrix}\tag{4}
\end{eqnarray}

(3),(4) より、以下が成り立ちます。

\begin{eqnarray}
\left\{
\begin{array}{l}
r_0=m_0\\
r_1=m_1\\
r_2=m_2\\
r_3=m_3\\
\end{array}
\right.\tag{5}
\end{eqnarray}

回転角を \theta\in{\mathbb R} とすると、\cos\theta=m_0,\sin\theta=m_1 であるので、\theta={\rm atan2}(m_1,m_0) と求まります。
ただし、\theta\in(-\pi,\pi]\backslash 0 です。0 を除いているのは、式(8) の計算における逆行列が存在するようにです。
\theta=0 の時は回転していないので、この時の剛体変換は単なる平行移動変換です。

r_0,r_1,r_2,r_3\theta を使って書き直します。

\begin{eqnarray}
\left\{
\begin{array}{l}
r_0=\cos\theta\\
r_1=\sin\theta\\
r_2=-\sin\theta\\
r_3=\cos\theta\\
\end{array}
\right.\tag{6}
\end{eqnarray}

(3),(4),(6) より、以下が成り立ちます。

\begin{eqnarray}
&&\left\{
\begin{array}{l}
 -\cos\theta t_x+\sin\theta t_y+t_x=m_4\\
 -\sin\theta t_x-\cos\theta t_y+t_y=m_5
\end{array}
\right.\\
&\Leftrightarrow&\left\{
\begin{array}{l}
 (1-\cos\theta)t_x+\sin\theta t_y=m_4\\
 -\sin\theta t_x+(1-\cos\theta)t_y=m_5
\end{array}
\right.\\
&\Leftrightarrow&
\begin{pmatrix}
 1-\cos\theta & \sin\theta\\
 -\sin\theta & 1-\cos\theta
\end{pmatrix}
\begin{pmatrix}
t_x \\ t_y
\end{pmatrix}
=
\begin{pmatrix}
m_4 \\ m_5
\end{pmatrix}\\
&\Leftrightarrow&
\begin{pmatrix}
t_x \\ t_y
\end{pmatrix}
=
\begin{pmatrix}
 1-\cos\theta & \sin\theta\\
 -\sin\theta & 1-\cos\theta
\end{pmatrix}^{-1}
\begin{pmatrix}
m_4 \\ m_5
\end{pmatrix}\tag{7}
\end{eqnarray}

\displaystyle\begin{pmatrix}
 1-\cos\theta & \sin\theta\\
 -\sin\theta & 1-\cos\theta
\end{pmatrix}^{-1}
を計算します。

\begin{eqnarray}
\begin{pmatrix}
 1-\cos\theta & \sin\theta\\
 -\sin\theta & 1-\cos\theta
\end{pmatrix}^{-1}
&=&
\frac{1}{(1-\cos\theta)^2+\sin^2\theta}
\begin{pmatrix}
 1-\cos\theta & -\sin\theta\\
 \sin\theta & 1-\cos\theta
\end{pmatrix}\\
&=&
\frac{1}{2(1-\cos\theta)}
\begin{pmatrix}
 1-\cos\theta & -\sin\theta\\
 \sin\theta & 1-\cos\theta
\end{pmatrix}\\
&=&
\frac{1}{4\sin^2\dfrac{\theta}{2}}
\begin{pmatrix}
 2\sin^2\dfrac{\theta}{2} & -2\sin\dfrac{\theta}{2}\cos\dfrac{\theta}{2}\\
 2\sin\dfrac{\theta}{2}\cos\dfrac{\theta}{2} & 2\sin^2\dfrac{\theta}{2}
\end{pmatrix}\\
&=&\dfrac{1}{2}
\begin{pmatrix}
 1 & -\cot\dfrac{\theta}{2}\\
 \cot\dfrac{\theta}{2} & 1
\end{pmatrix}\tag{8}
\end{eqnarray}

(8) を式 (7) に代入します。

\begin{eqnarray}
&&\begin{pmatrix}
t_x \\ t_y
\end{pmatrix}
=
\frac{1}{2}
\begin{pmatrix}
 1 & -\cot\dfrac{\theta}{2}\\
 \cot\dfrac{\theta}{2} & 1
\end{pmatrix}
\begin{pmatrix}
m_4 \\ m_5
\end{pmatrix}\\
&\Leftrightarrow&
\begin{pmatrix}
t_x \\ t_y
\end{pmatrix}
=
\frac{1}{2}
\begin{pmatrix}
 m_4 -\cot\dfrac{\theta}{2}m_5\\
 \cot\dfrac{\theta}{2}m_4 + m_5
\end{pmatrix}\\
&\Leftrightarrow&
\left\{
\begin{array}{l}
t_x=\dfrac{1}{2}\left(m_4 -\cot\dfrac{\theta}{2}m_5\right)\\
t_y=\dfrac{1}{2}\left(\cot\dfrac{\theta}{2}m_4 + m_5\right)
\end{array}
\right.\tag{9}
\end{eqnarray}

以上より、r_0,r_1,r_2,r_3,t_x,t_y が求まったので、任意点回りの回転行列が求まりました。

SymPyで検算

(8) をSymPyで検算してみました。

from sympy.algebras.quaternion import Quaternion
from sympy import *
init_printing()
var('r_0,r_1,r_2,r_3,theta')

r_0 = cos(theta)
r_1 = sin(theta)
r_2 = -sin(theta)
r_3 = cos(theta)

R = Matrix([
    [r_0, r_2 ],
    [r_1, r_3 ],
])
I = Matrix([
    [1, 0 ],
    [0, 1 ],
])
M = I - R

M_det = M.det()
M_det = factor(M_det)
answer = solve(M_det,theta)
display(answer)
display(m_det)
M_inv = M.inv()
M_inv = simplify(M_inv)
M_inv = M_inv.subs([(sin(theta), 2 * sin(theta/2) * cos(theta/2)), (2 - 2 * cos(theta), 4 * sin(theta/2) ** 2)])
M_inv = simplify(M_inv)
M_inv = M_inv.subs([(1/tan(theta/2),cot(theta/2))])
display(M_inv)

プログラムの実行結果

参考リンク

atan2 - wikipedia

目次へ戻る