# <font color="#18EE9D">Quaternion Orientation 四元數旋轉</font>
###### tags: `tensor` `quaternion orientation` `vector` `四元數`
---
## **[Quaternion Simulator](https://quaternions.online/)**
---
## Bloch Sphere

## basic concept
***<font color="#9539D6">$\left(
\begin{array}{ccc}
x" \\
y" \\
z" \\
\end{array}
\right)=\left(
\begin{array}{ccc}
1-2(y^2+z^2) & 2(xy-wz) & 2(wx+xz) \\
2(xy+wz) & 1-2(x^2+z^2) & 2(yz-wx) \\
2(xz-wy) & 2(yz+wx) & 1-2(x^2+y^2) \\
\end{array}
\right)\left(
\begin{array}{ccc}
x' \\
y' \\
z' \\
\end{array}
\right)$</font>***
'是原始向量
”是變成的向量
中間的矩陣:$Rot(P)=qpq^-1$
$q= 四元數:w+xi+yj+zk=[cosθ, u*sinθ]=[S+V]$
$p= 原始向量(w=0,所以沒有w項)$
$q^-1 = w - x i - y j -z k = [S - V]$
透過這個運算,使得q'繞著原始向量旋轉$2θ$
---
## $\overrightarrow{\rm n}$ and negative Quarternion Orientation
Ex:$(x,y,z,w)=(0,0.7,0,0.7)$
則 steps:
1) $(0,-0.7,0,0.7) or (0,0.7,0,-0.7)$
2) $qpq^-1$
3) 得轉移後向量即為相機應對法向量
---
## 用x,y,z求w
$sin(\dfrac{\theta}{2})(x',y',z')=(x,y,z)$
$sin(\dfrac{\theta}{2})|(x',y',z')|=|(x,y,z)|$
$sin(\dfrac{\theta}{2})*1=|(x,y,z)|$
Now, we know that
First, $sin(\dfrac{\theta}{2})=|(x,y,z)|$
Second, $cos(\dfrac{\theta}{2})=w$
third, $sin^2 (\dfrac{\theta}{2})+cos^2 (\dfrac{\theta}{2})=1$
$\therefore x^2+y^2+z^2+w^2=1$
result: $w=\sqrt{1-x^2-y^2-z^2}$
```java
public float w()
{
return (float)Math.sqrt(1-x*x-y*y-z*z);
}
```