##  3D Mobius transformation of  meshes. 4D Rotations 

### 3D Mobius transformations

$\newcommand{\R}{\mathbb{R}}$
$\newcommand{\C}{\mathbb{C}}$
$\newcommand{\th}{\theta}$
$\newcommand{\lam}{\lambda}$
$\newcommand{\ovl}{\overline}$
$\newcommand{\ds}{\displaystyle}$
$\newcommand{\barr}{\begin{array}}$
$\newcommand{\earr}{\end{array}}$
$\newcommand{\bea}{\begin{eqnarray}}$
$\newcommand{\eea}{\end{eqnarray}}$
$\newcommand{\beq}{\begin{equation}}$
$\newcommand{\eeq}{\end{equation}}$

The classical [Mobius transformation](https://en.wikipedia.org/wiki/M%C3%B6bius_transformation) is  known as acting on the extended complex plane, identified to the extended space, $\hat{\mathbb{R}}^2=\mathbb{R}^2\cup \{\infty\}$. But is there a Mobius transformation that acts on the extended space $\hat{\mathbb{R}}^n$, $n>2$? The answer is positive, and the definition and construction of such a transformation is presented in detail in the excellent book, by Alan F. Beardon -  *The geometry of discrete groups*, Springer, 1995 (chapter 3, Mobius transformation on $\mathbb{R}^n$).
The presentation from this book, and an article from Notices of AMS, [https://www.ams.org/notices/200810/tx081001226p.pdf](https://www.ams.org/notices/200810/tx081001226p.pdf), suggested how we can apply a Mobius transformation to a 3d mesh.

By these references   a  Möbius transformation  on the 3d extended space can be defined as the transformation that maps this  space through  the inverse of the stereographic projection onto an admissible sphere $S(a, R) =\{(x,y, z, u)\in\mathbb{R}^4\:|\: (x-a_1)^2+(y-a_2)^2+(z-a_3)^2+(u-a_u)^2=R^2\}$  in the space $\mathbb{R^4}$ (i.e. a sphere for which the  north pole $N(a_1, a_2, a_3, a_4+R)$, lies in the upper half-space $H=\{(x, y, z, u)\in\mathbb{R}^4\:|\: u>0\}$,
 followed by a rigid motion (rotation+translation) of the sphere in $\mathbb{R}^4$,  which maps it to another admissible sphere, and finally mapping the last sphere, back to the space $\mathbb{R}^3$,  by the stereographic projection.
 Following Chapter 3 from Beardon's book and this method of construction, valid for Mobius transformation in any dimension,  we can illustrate visually, how a Mobius transformation defined on the 3d space acts on a 3d mesh.

In Computer Graphics a 3d mesh is a numerical representation of a 3d model (surface or volume). It consists in two arrays,
the array of vertices and the array of polygons (usually quadrangles and triangles). 
The array of vertices (of shape (m, 3)) contains on rows their x, y, z coordinates, while the array of quadrangles/triangles is
and array of integers, of shape (n, 4) or (n, 3), where each row,  contains the indices in the array of vertices, of the points that define the corresponding polygon.

This repository is dedicated to  illustrating how a 3d mesh is deformed  by a Mobius transformation that acts as follows:
  - map the mesh vertices to the 3-dimesnional unit sphere, $S^3$,  in the 4d space, by the inverse of the stereographic projection.
  - rotate   the spherical mesh vertices, by a 4d rotation, which is known as  leaving the sphere invariant (maps points on sphere onto points on sphere);
  - eventually translate the initial unit sphere to an admissible unit (of radius =1) sphere in $\R^4$;
  -  map back to the 3d space the rotated and translated sphere vertices, through the stereographic projection from the corresponding sphere  to $\hat{\mathbb{R}}^3$.
  
  The projected points as vertices and the initial mesh polygons define a Mobius transformed 3d mesh.

A Mobius transformation is a conformal transformation (i.e. a transformation that preserves the angles, but not the lengths), because it is a composition of conformal mappings (the stereographic projection and its inverse) to a rigid transformation.

The stereographic projection from an admissible  unit sphere, of center $C(c_1, c_2, c_3, c_4)$, $S^3=\{(x, y, z, u)\in\mathbb{R}^4\:|\: (x-c_1)^2+(y-c_2)^2+(z-c_3)^2+(u-c_4)^2=1\}$,
to the euclidean space $\mathbb{R}^3$, is defined similarly to the stereographic projection from the sphere $S^2$ to $\mathbb{R}^2$. 
Namely, it associates to each point, $P(x,y,z, u)$, on the sphere, the point of intersection of the line through the north pole, 
$N(c_1, c_2, c_3, c_4+1)$, and P, to the space $\mathbb{R}^3$, identified with  the subspace of $\mathbb{R}^4$, defined by $\{(x,y,z,u)\:|\: u=0\}$. Similarly one defines the inverse of  the stereographic projection.

The functions `stereo_aS3_R3()`, `inv_stereo_aS3()`, from `mobius3d`, implement the stereographic projection and its inverse.

An important step in defining a 3d Mobius transformation is the 4d rotation. Below we present in details the definition and
the orthogonal decomposition of a 4d rotation, as a key point in defining  interesting Mobius 3d transformations of  3d meshes.

### 4D rotation - orthogonal decomposition

A 4d rotation, $Rot:\R^4\to\R^4$, is represented by an orientation preserving orthogonal matrix, Q, i.e. $Q^TQ=QQ^T = Id_4$, and determinant(Q)=1.
From orthogonality property it follows that the columns of such a matrix are real-orthonormal vectors. 

But the orthogonal matrices are particular normal matrices. That's why we are  pointing out, below, the basic properties of normal matrices.


Let $\C^n$  be the n-dimensional complex space, with the hermitian inner product defined by:
$$<v, w> =\sum_{k=1}^n \ovl{z}_kz'_k = v^*w, \forall\:\: v=(z_1, z_2,  \ldots, z_n)^T, w=(z'_1, z'_2, \ldots, z'_n)^T \in \C^n$$
($v^*$ denotes the conjugate transpose of v). This inner product induces the standard inner product on the space $\R^n$.

A complex $n\times n$ -  matrix, $A$,  is a normal matrix if $A^*A=AA^*$, where $A^*=\ovl{A}^T$, denotes the conjugate transpose of $A$.
A unitary matrix, $U$, i.e. a matrix with the property $U^*U=UU^*=Id$, and in particular an  orthogonal matrix, $Q$, is a normal matrix.

Spectral properties of normal matrices:

 1. The algebraic multiplicity of an eigenvalue coincides with its geometric multiplicity.
 2. To distinct eigenvalues, $\lam\neq \mu$,  correspond  orthogonal eigenvectors, $v, w$, i.e.  $v^*w=0$.
 
These two properties ensure that for any normal matrix, $A$, there is an orthonormal basis in $\C^n$, consisting in  eigenvectors of $A$, and the matrix $A$ can be decomposed as $A= V D V^*$, where  $D$ is the diagonal matrix of its eigenvalues, and $V$ is the unitary matrix with the corresponding orthonormal eigenvectors on its columns. Hence a normal matrix is diagonalizable by a unitary matrix, and $A=V D V^*$ is called unitary decomposition.

 Being an orientation preserving orthogonal matrix, a rotation  is a rigid transformation, i.e. $||Qv||=||v||$ (it preserves lengths and angles). In particular if $v$ is an eigenvector, $Qv=\lam v$, we get that  $||v||=||Qv|=||\lam v||= |\lam|||v||$, i.e. $|\lam|=1$. Hence the eigenvalues of a rotation matrix lie on the unit circle (they are of the form $\lam, \ovl{\lam}=a\pm ib$, $a^2+b^2=1$ or equivalently, $\lam, \ovl{\lam}=e^{\pm it}=\cos(t)+i\sin(t)$, for some $t\in (-\pi, \pi]$.
If $t=0$ we get  the eigenvalue 1 which can be   a double eigenvalue or an eigenvalue multiple of order 4, while for $t=\pi$, -1 is a double eigenvalue or  multiple of order 4.

From the spectral properties of normal matrices we get that for a 4d rotation, $Q$,  we have the  unitary decomposition:

$$Q= V D V^*$$
where $D$ is the diagonal matrix of its eigenvalues $\lam_1, \lam_2\lam_3\lam_4\in\C$, and $V$ has as columns the orthonormal   eigenvectors, with respect to the hermitian inner product in $\C^4$), corresponding to these eigenvalues.

The numpy function `np.linalg.eig` identifies orthogonal matrices and returns V, as a unitary matrix.


A method to generate an  example of 4d rotation is to get the $QR$ decomposition of a random  real $4\times 4$-matrix:


```python
import numpy as np

def get_rotation(A, det_tol=1.e-10):
    #returns the Q matrix from the QR-decomposition of the given matrix, A, and its eigenvals and eigenvect
    if A.ndim !=2 or A.shape[0] != A.shape[1] != 4:
        raise ValueError('A must have the shape (4,4)')
    Q, _ = np.linalg.qr(A)
    if abs(np.linalg.det(Q)+1) < det_tol: # if det(Q) is close to  -1 revert two columns in Q
        Q = np.array([Q[:, 1], Q[:, 0], Q[:, 2], Q[:, 3]]).T
    eigvalues, V = np.linalg.eig(Q)
    return Q, eigvalues, V
```


```python
A = np.array([[ 2, -1,  3, -1],
              [-3,  0,  2,  0],
              [ 1,  0,  0,  2],
              [ 0,  1,  3, -1]])
Q, lam, V = get_rotation(A)
```

Check orthogonality:


```python
np.linalg.norm(Q.T @ Q-np.eye(4)) #Frobenius norm of the matrix difference Q^TQ-Id_4
```




    5.177377601847236e-16




```python
np.linalg.det(Q) > 0
```




    True




```python
lam  #eiegenvalues of the matrix Q
```




    array([-0.84420342+0.53602294j, -0.84420342-0.53602294j,
            0.9288035 +0.37057261j,  0.9288035 -0.37057261j])



Hence our othogonal matrix has two distinct pairs of complex conjugate eigenvalues. The corresponding orthonormal vectors
are the columns of the unitary matrix V:

Check if  $V$ is a unitary matrix:


```python
P = np.matrix(V).getH()  @ V  #  np.matrix(V) converts V to a numpy.matrix W, W.getH() is the conjugate transpose of W
np.linalg.norm (P- np.eye(4))  #  distance to Id_4
```




    2.9249508704071502e-15



In order to point out how a 4d rotation,  $Q$, acts on points from  ℝ4, in particular on the sphere from  $\R^4$, we are converting the unitary decomposition of  𝑄  into an orthogonal decomposition,  $Q=UR_kU^T$ , where  $U$  is an orthogonal matrix,
and  $R_k$, $k=1, 2$  is a particular rotation matrix derived from the complex eigenvalues the matrix $Q$ .

The diagonal matrix $D$, from the unitary decomposition $Q=V D V^*$, is  the representative matrix of the rotation with respect to the basis $B'=(v_1, v_2, v, v_4)$, consisting in the columns of $V$

**a)** If $\lam_1, \lam_2=1, and \lam_3, \lam_4 = e^{\pm it}$, for some $t\in (-\pi, \pi]\setminus\{0\})$, 
then from the basis $\mathcal{B}'$ we extract an orthonormal basis in $\R^4$, $\mathcal{B}''=(u_1, u_2, u_3, u_4)$, as follows:
$$u_1=v_1, u_2=v_2, u_3 =\ds\frac{1}{\sqrt{2}} (v_3 +v_4), u_4 =\ds\frac{1}{i\sqrt{2}} (v_3 -v_4)$$                              where $v_4=\ovl{v}_3$ (to complex conjugate eigenvalues correspond complex conjugate eigenvectors). 

Note that although the complex conjugate vectors, $v_3, v_4$,  are unit vectors with respect to the norm in $\C^4$, their real part $(v_3+v_4)/2$ and imaginary part, $(v_3-v_4)/2i$, do not have unit norm. The factors $\ds\frac{1}{\sqrt{2}}$, $\ds\frac{1}{i\sqrt{2}}$ ensure that the corresponding real vectors are unit vectors.

It is a simple excercise to show that  $\mathcal{B}''$ is an orthonormal basis (the orthogonality of the first two vectors is obvious, while for example, $<u_1, u_3> = <v_1, \ds\frac{1}{\sqrt{2}} (v_3 +v_4)>=0$).   

The representative matrix, $R_1$,  of the rotation  with respect to the basis $\mathcal{B}''$ is derived from the matrix $D$ that represents the rotation with    respect to the basis $\mathcal{B}'$, and the unitary matrix 
$$W = \left(\barr{cccc} 1&0&0&0\\
                        0&1&0&0\\
                        0&0& \frac{1}{\sqrt{2}}&\frac{1}{i\sqrt{2}}\\
                        0&0& \frac{1}{\sqrt{2}}&-\frac{1}{i\sqrt{2}}\earr\right)$$
whose columns are the  coordinates of the vectors $u_1, u_2, u_3, u_4$ with respect to the basis $\mathcal{B}'$:
$$R_1  = W \left(\barr{cccc} 1&0&0&0\\
0&1&0&0\\
0&0&\cos(t)+i\sin(t)&0\\
0&0&0&\cos(t)-i\sin(t)\earr\right) W^*= \left(\barr{rrrr}1&0&0&0\\0&1&0&0\\
                   0&0&\cos(t)&\sin(t)\\0&0&-\sin(t)&\cos(t)\earr\right)$$

 As a consequence 
 the orthogonal decomposition of the rotation matrix $Q$,  is in this case,  $Q = U R_1 U^T$,
where $U$ has on its columns the coordinates of the vectors $u_1, u_2, u_3, u_4$ with respect to the standard basis in $\R^4$.

**b)**  Analogously we get  the matrix $R_{-1}$ corresponding to the case  $\lam_1, \lam_2 = -1$ and$\lam_4=\ovl{\lam}_3$, by replacing  the diagonal block $I_2$ in $R_1$ by  $-I_2$.

**c)** If the eigenvalues of the matrix $Q$ are $\lam_2=\ovl{\lam}_1$, $\lam_4=\ovl{\lam}_3$, i.e. $\lam_1,\lam_2=\cos(s)\pm i\sin(s)$, $\lam_3, \lam_4 = \cos(t)\pm i \sin(t)$, $s, t \in (-\pi, \pi)\setminus\{0\}$, and the corresponding orthonormal basis of eigenvectors is  $\mathcal{B}'=(v_1, \ovl{v}_1, v_2, \ovl{v}_2)$, then define the orthonormal real vectors:
$$u_1 = \ds\frac{1}{\sqrt{2}} (v_1 +\ovl{v}_1), u_2 = \ds\frac{1}{i\sqrt{2}} (v_1 -\ovl{v}_1),
u_3=\ds\frac{1}{\sqrt{2}} (v_2 +\ovl{v}_2), u_4 = \ds\frac{1}{i\sqrt{2}} (v_2 -\ovl{v}_2)$$
that define a basis $\mathcal{B}''$ in $\R^4\subset\C^4$.

The matrix of the initial rotation with respect to this basis, deduced similarly as in the previous case, is of the form:
$$R_2=\left(\barr{rrrr}\cos(s)&\sin(s)&0&0\\-\sin(s)&\cos(s)&0&0\\0&0&\cos(t)&\sin(t)\\0&0&-\sin(t)&\cos(t)\earr\right)$$    
and the corresponding orthogonal decomposition of the rotation $Q$ is:

$$Q = U R_2 U^T,\:\:    U=[u_1|u_2|u_3|u_4]$$

Hence the derivation of the orthogonal decomposition from the unitary decomposition of a rotation matrix exploited the property that the $\R^4$-vectors of the pairs  $(real(v_i), real(v_j$, $(real(v_i), imag(v_j))$ associated to  complex eigenvectors  $v_i, v_j$, respectively
$(real(v_i), imag(v_i)$, $i, j  =\ovl{1,4,  i\neq j$, are orthogonal vectors.

**d)** If in **a)** and **b)**, t=0, we get  $Q =  V Id_4 V^*=Id_4$, respectively  $Q=-Id_4$.

Let us point out how a 4D rotation that contains $R_1$, respectively $R_2$, as the central matrix in its orthogonal decomposition acts on different subspaces of $\R^4$.

- A 4d rotation matrix having the orthogonal decomposition $Q=UR_1U^T$ has the property that $Q(\alpha_1 u_1+\alpha_2u_2)=\alpha_1 u_1+\alpha_2u_2$, $\alpha_1, \alpha_2\in\R$,
i.e.  the vectors of the subspace of $\R^4$ generated by the first two columns in the matrix $U$, are fixed by the rotation $Q$.

On the other hand the vectors in the subspace generated by the last two columns (i.e. the vectors in the orthogonal complement of the former subspace) are rotated by an angle of $t$ radians.  Such a rotation matrix defines a rotation about the plane
defined by the origin $O$ and the vectors $u_1, u_2$. The fixed plane and the invariant plane (the rotated plane) are orthogonal. 
Hence, unlike the 3D rotation that occurs about an axis, in 4D we have rotations about a plane.

- A 4d rotation of the form $Q=UR_2U^T$ is called double rotation, because it rotates both the vectors in the subspace generated by the first two columns in $U$
and the vectors in the subspace generated by the last two columns of $U$.  If  the angles of rotation are equal, i.e. s=t, then Q is called  isoclinic rotation. 

Notice that from an  orthogonal decomposition of the form $Q=U R_2U^T$, we can recover any particular orthogonal decomposition, by setting suitable values for $s$ and $t$.

- for $s=t=0$ we get $Q=Id_4$;
- for $s=t=\pi$, $Q=-I_4$;
- for $s=0$, $t\in (-\pi, \pi]\setminus\{0\}$, $Q=U R_1U^T$;
- for $s=\pi$, $t\in (-\pi, \pi]\setminus\{0\}$, $Q=U R_{-1}U^T$, where  $R_{-1}$ is gotten from $R_1$, replacing the diagonal block $Id_2$, by $-Id_2$;
- for $s, t \in (-\pi, \pi]\setminus\{0\}$, $Q=UR_2U^T$;

That's why in `mobius3d` we defined a function `setup_rotation()`, which from particular values for s and t, and an orthogonal matrix $U$, returns the corresponding rotation matrix $Q=U R_2(s,t)U^T$.


```python

```

00a762f0b6327c1b56e5cda7edeb2bcb5f64ec9c
ipynb
Jupyter Notebook
3D-Moebius-transformations-of-meshes-4D-rotations.ipynb
empet/4d-rotations-and-Mobius-3d-transformations
5b620d2fb766b0329e4b470d8c6d06828af3c27e
MIT
3D-Moebius-transformations-of-meshes-4D-rotations.ipynb
empet/4d-rotations-and-Mobius-3d-transformations
5b620d2fb766b0329e4b470d8c6d06828af3c27e
MIT
3D-Moebius-transformations-of-meshes-4D-rotations.ipynb
empet/4d-rotations-and-Mobius-3d-transformations
5b620d2fb766b0329e4b470d8c6d06828af3c27e
MIT
Qwen/Qwen-72B
1. YES
2. YES