Introduction:

In the field of computer graphics, robotics, and 3D animation, representing rotations accurately and efficiently is of paramount importance. Traditionally, rotations have been described using standard XYZ rotation matrices, which provide a straightforward mathematical representation. However, in recent years, quaternions have gained popularity as an alternative representation due to their advantages in terms of compactness, interpolation, and avoiding gimbal lock issues.

Standard XYZ Rotation Matrix:

The standard XYZ rotation matrix, also known as the Euler rotation matrix or Tait-Bryan angles, is a widely used representation for rotations. It consists of a 3x3 matrix that describes the transformation applied to a vector when rotated around the X, Y, and Z axes sequentially. Each rotation is performed relative to the world coordinate system or the object's local coordinate system, depending on the convention used. Here's an example of a 3x3 rotation matrix that represents rotations around the x-axis, y-axis, and z-axis in degrees:

$\begin{bmatrix}
\cos(\theta_y)\cos(\theta_z) & \cos(\theta_z)\sin(\theta_x)\sin(\theta_y) - \cos(\theta_x)\sin(\theta_z) & \cos(\theta_x)\cos(\theta_z)\sin(\theta_y) + \sin(\theta_x)\sin(\theta_z) \\
\cos(\theta_y)\sin(\theta_z) & \cos(\theta_x)\cos(\theta_z) + \sin(\theta_x)\sin(\theta_y)\sin(\theta_z) & -\cos(\theta_z)\sin(\theta_x) + \cos(\theta_x)\sin(\theta_y)\sin(\theta_z) \\
-\sin(\theta_y) & \cos(\theta_y)\sin(\theta_x) & \cos(\theta_x)\cos(\theta_y) \\
\end{bmatrix}$

In this example, $\theta_x$, $\theta_y$, and $\theta_z$ represent the rotation angles around the x-axis, y-axis, and z-axis, respectively, in degrees. The cosine and sine functions are used to compute the trigonometric values of the given angles. The elements of the rotation matrix correspond to the combination of rotations around each axis. This rotation matrix can be used to transform vectors or points in 3D space, applying rotations around the x-axis, y-axis, and z-axis based on the specified angles.n 3D space, applying a rotation of 45 degrees around the y-axis.

One of the advantages of the standard XYZ rotation matrix is its simplicity and intuitive interpretation. It allows for direct control over the individual rotation angles around the three axes, making it easy to understand and apply in certain scenarios. However, a major drawback of this representation is the issue of gimbal lock, which occurs when two rotation axes align, leading to a loss of one degree of freedom and undesired behavior in rotations.

Gimbal lock is a phenomenon that occurs when using the standard XYZ rotation matrix to represent rotations. It occurs when two rotation axes align, leading to the loss of one degree of freedom and causing undesirable behavior in rotations. In gimbal lock, rotations around certain axes become ambiguous, resulting in unexpected transformations and limitations in the range of achievable rotations. This issue can be problematic in scenarios that require complex and continuous rotations, such as 3D animation and robotics.

Quaternions:

Quaternions, on the other hand, offer an elegant and efficient representation for rotations. They are a four-dimensional extension of complex numbers and consist of a scalar component and three imaginary components. Quaternions have several advantages over standard XYZ rotation matrices, making them increasingly popular in various applications. The mathematical formulation to convert an XYZ angle rotation to quaternion representation :

$\textbf{q} = \begin{bmatrix}\cos\left(\frac{\theta_x}{2}\right) \\\sin\left(\frac{\theta_x}{2}\right) \cos\left(\frac{\theta_y}{2}\right) \\\sin\left(\frac{\theta_x}{2}\right) \sin\left(\frac{\theta_y}{2}\right) \cos\left(\frac{\theta_z}{2}\right) \\\sin\left(\frac{\theta_x}{2}\right) \sin\left(\frac{\theta_y}{2}\right) \sin\left(\frac{\theta_z}{2}\right)\end{bmatrix}$

In this expression, $\theta_x$, $\theta_y$, and $\theta_z$ represent the rotation angles around the X, Y, and Z axes, respectively. The matrix notation is used to represent the quaternion as a 4D vector, where the first component is the scalar part and the remaining three components represent the vector part.

Firstly, quaternions provide a compact representation, requiring only four scalar values, as opposed to the nine scalar values required for a 3x3 rotation matrix. This compactness leads to reduced storage requirements and faster computations, particularly in environments with limited resources.

Secondly, quaternions excel in interpolation and smoothly transitioning between rotations. Quaternion interpolation techniques, such as spherical linear interpolation (SLERP) and cubic interpolation among the others, ensure visually pleasing and mathematically accurate intermediate rotations, allowing for smooth animations and transformations.

Furthermore, quaternions avoid the issue of gimbal lock entirely, as they operate in a different mathematical space. This eliminates the risk of losing a degree of freedom and provides more stable and predictable rotations..

Considerations and Choosing the Right Representation:

When deciding between the standard XYZ rotation matrix and quaternions, several factors should be considered. If simplicity and intuitive control over individual rotation angles are crucial, the standard XYZ rotation matrix may be a suitable choice. However, it is essential to be aware of the gimbal lock issue and employ appropriate techniques to mitigate its effects when working with complex rotations. On the other hand, if compactness, interpolation capabilities, and gimbal lock avoidance are significant requirements, quaternions offer a compelling solution. Although understanding quaternion mathematics and transformations may involve a learning curve, the benefits they provide in terms of efficiency and stability make them an attractive option for many applications.

Rotation interpolation

In the realm of computer graphics and animation, smooth transitions between orientations or rotations are crucial for creating visually appealing and realistic effects. Having discussed the issue of choosing between the standard XYZ rotation matrix and quaternions as representations for rotations, let us now turn our attention to another powerful technique quaternions interpolations. Several technics exists allowing to combines the benefits of quaternions with the smoothness and control. In the following sections, we will delve into the concept of quaternions interpolations, explore its mathematical foundations, and demonstrate its practical implementation.

Spherical Linear Interpolation (SLERP) of Quaternions

Spherical Linear Interpolation (SLERP) is a fundamental technique used in computer graphics and animation to smoothly interpolate between two quaternions representing rotations. By providing seamless transitions between orientations, SLERP enables visually appealing and natural-looking animations. In this section, we will explore the concept of SLERP, discuss its mathematical foundations, and highlight its significance in achieving smooth quaternion interpolation.

Mathematical Foundations:

SLERP involves interpolating between two quaternions, $q_0$ and $q_1$, representing the starting and ending rotations, respectively. The interpolation is performed along the shortest path on the unit sphere, ensuring that the rotations are continuous and without any sudden flips.

The SLERP formula can be expressed as follows:

$q(t) = \frac{{\sin((1-t)\theta)}}{{\sin(\theta)}}q_0 + \frac{{\sin(t\theta)}}{{\sin(\theta)}}q_1$

where:

  • $q(t)$ represents the interpolated quaternion at time $t$.
  • $\theta$ is the angle between $q_0$ and $q_1$.

To calculate $\theta$, we can use the dot product between $q_0$ and $q_1$:

$\cos(\theta) = q_0 \cdot q_1$

Since the dot product can be in the range $[-1, 1]$, we need to ensure that $\theta$ lies within the range $[0, \pi]$ to maintain a valid interpolation.

Implementation and Example:

Let's explore a Python implementation of SLERP using the `numpy` library.

import numpy as np

def slerp(q0, q1, t):
    dot_product = np.dot(q0, q1)

    if dot_product < 0:
        q1 = -q1
        dot_product = -dot_product

    if dot_product > 0.95:
        return np.interp(t, [0, 1], [q0, q1])

    theta = np.arccos(dot_product)
    sin_theta = np.sin(theta)
    q = (np.sin((1 - t) * theta) * q0 + np.sin(t * theta) * q1) / sin_theta

    return q

# Example usage
q0 = np.array([1, 0, 0, 0])  # Start quaternion
q1 = np.array([0, 1, 0, 0])  # End quaternion
t = 0.5  # Interpolation parameter

interpolated_quaternion = slerp(q0, q1, t)

In this example, we define the start quaternion (`q0`), end quaternion (`q1`), and the interpolation parameter (`t`). The `slerp` function calculates the interpolated quaternion using the provided formula. It takes into account the dot product between the quaternions to ensure that the shortest path on the unit sphere is followed. Additionally, it includes special cases for efficient interpolation when the quaternions are close to each other or when they have a large dot product.

Spherical Linear Interpolation (SLERP) of quaternions provides a robust and efficient method for smoothly interpolating between rotations. By traversing the shortest path on the unit sphere, SLERP ensures visually pleasing and physically accurate transitions between orientations. In this section, we explored the mathematical foundations of SLERP and presented a Python implementation for practical usage. SLERP is a valuable tool in computer graphics, animation, and robotics, where smooth and seamless quaternion interpolation is desired. By utilizing the SLERP formula and taking into account the dot product between quaternions, we can achieve visually appealing and mathematically accurate intermediate rotations.

The implementation example showcased the usage of SLERP in Python, utilizing the numpy library for efficient quaternion calculations. The slerp function handles special cases, such as when the dot product is negative or when the quaternions are close to each other, to ensure robust and reliable interpolation.

SLERP finds wide application in various domains, including 3D animation, computer graphics, and robotics. It enables the creation of smooth transitions between different orientations, allowing for seamless and realistic motion. Whether it's animating characters, interpolating camera movements, or controlling robotic manipulators, SLERP plays a crucial role in achieving natural and visually pleasing rotations.

In conclusion, Spherical Linear Interpolation (SLERP) is a powerful technique for smoothly interpolating quaternions and maintaining continuous rotations. By following the shortest path on the unit sphere, SLERP provides seamless transitions between orientations. In this section, we explored the mathematical foundations of SLERP and presented a practical implementation in Python. Incorporating SLERP in your projects will enhance the quality and realism of rotations, contributing to more compelling and visually captivating animations and simulations.

Cubic Interpolation of Quaternions:

Quaternion interpolation involves generating intermediate rotations between two given orientations. The process of cubic interpolation goes a step further by utilizing a cubic polynomial to smoothly interpolate between quaternions, resulting in visually seamless transitions. By using a combination of position, velocity, and acceleration, cubic interpolation ensures that the changes in orientation are not only smooth but also exhibit realistic acceleration and deceleration.

Python Implementation:

To better understand the practical implementation of cubic interpolation of quaternions, let's consider a simple example in Python. We'll assume that we have two quaternions, `q0` and `q1`, representing the initial and final orientations, respectively.

import numpy as np
from scipy.spatial.transform import Rotation

def cubic_interp_quaternion(q0, q1, qm1, q2, t):
    t2 = t * t
    t3 = t2 * t

    q0_inv = np.array([q0[0], -q0[1], -q0[2], -q0[3]])
    qm1_inv = np.array([qm1[0], -qm1[1], -qm1[2], -qm1[3]])
    q2_inv = np.array([q2[0], -q2[1], -q2[2], -q2[3]])

    a = 2 * q0 - 2 * q1 + qm1 + q2
    b = -3 * q0 + 3 * q1 - 2 * qm1 - q2
    c = qm1
    d = q0

    q_interp = a * t3 + b * t2 + c * t + d
    q_interp = q_interp / np.linalg.norm(q_interp)

    return q_interp

# Example usage
q0 = np.array([1, 0, 0, 0]) # Initial quaternion
q1 = np.array([0, 1, 0, 0]) # Final quaternion
qm1 = np.array([0, 0, 1, 0]) # Quaternion before q0
q2 = np.array([0, 0, 0, 1]) # Quaternion after q1
t = np.linspace(0, 1, num=100) # Time parameter

quaternions = cubic_interp_quaternion(q0, q1, qm1, q2, t)

# Convert quaternions to rotations
rotations = Rotation.from_quat(quaternions)

In this example, the `cubic_interp_quaternion` function takes four quaternions: `q0`, `q1`, `qm1`, and `q2`, along with a time parameter `t` that specifies the interpolation progress. The function returns a quaternion representing the interpolated orientation at a particular time. By varying the `t` parameter, we can generate a sequence of intermediate quaternions, which can then be converted to rotations for further use in animation or rendering.

Mathematical Foundation:

The cubic interpolation of quaternions is based on the principles of cubic splines, which are widely used in interpolation techniques. A cubic spline is a piecewise-defined polynomial function that smoothly connects a set of data points. In the context of quaternion interpolation, we extend this concept to interpolate between quaternions.

Given the quaternions $q0$, $q1$, $qm1$, and $q2$, the cubic interpolation formula can be defined as follows:

$q(t) = a \cdot t^3 + b \cdot t^2 + c \cdot t + d$

Where:

  • $q(t)$ represents the interpolated quaternion at time $t$.
  • $a = 2 \cdot q_0 - 2 \cdot q_1 + q_{m1} + q_2$
  • $b = -3 \cdot q_0 + 3 \cdot q_1 - 2 \cdot q_{m1} - q_2$
  • $c = q_{m1}$
  • $d = q_0$

To ensure smooth interpolation, it's important to normalize the resulting quaternion $q(t)$ to unit length using the norm operation $(\lVert \cdot \rVert$.

In the Python implementation provided earlier, we utilized the numpy library for vector operations and the scipy.spatial.transform module for converting quaternions to rotations. The cubic_interp_quaternion function takes the initial quaternion $q0$, the final quaternion $q1$, and the adjacent quaternions $qm1$ and $q2$. It also accepts a time parameter $t$ that specifies the interpolation progress. The function returns the interpolated quaternion at the given time.

By applying the cubic_interp_quaternion function iteratively for different values of $t$, we generate a smooth sequence of quaternions. These interpolated quaternions can then be converted to rotations using the Rotation.from_quat function from the scipy.spatial.transform module.

Conclusion:

Cubic interpolation of quaternions offers a powerful technique for achieving smooth transitions between orientations in computer graphics and animation. By utilizing the principles of cubic splines, we can seamlessly blend quaternions and ensure visually appealing and realistic rotations. In this blog post, we explored the concept of cubic interpolation, provided a Python implementation, and discussed the underlying mathematical foundations. Incorporating cubic interpolation into your animation pipeline can significantly enhance the quality and fluidity of your rendered scenes.

  • https://theorangeduck.com/page/cubic-interpolation-quaternions

Interpolation de la courbe d'Hermite

In the field of computer graphics and mathematical modeling, Hermite curve interpolation is a widely used technique for smoothly connecting a set of data points. Named after the French mathematician Charles Hermite, this interpolation method allows us to define both the position and the tangent of the curve at each data point. The resulting curves exhibit desirable properties, such as continuity and smoothness, making them invaluable in various applications, including animation, design, and computer-aided manufacturing. In this section, we will explore the concept of Hermite curve interpolation, discuss its mathematical foundations, and provide examples of its implementation.

Mathematical Foundations:

In the field of computer graphics and mathematical modeling, Hermite curve interpolation is a widely used technique for smoothly connecting a set of data points. Named after the French mathematician Charles Hermite, this interpolation method allows us to define both the position and the tangent of the curve at each data point. The resulting curves exhibit desirable properties, such as continuity and smoothness, making them invaluable in various applications, including animation, design, and computer-aided manufacturing. In this section, we will explore the concept of Hermite curve interpolation, discuss its mathematical foundations, and provide examples of its implementation.

At the core of Hermite curve interpolation lies the Hermite polynomial, a basis function used to construct the interpolated curve.

The Hermite polynomial of degree $n$ is defined as:

$H_n(t) = (1 - 2t)H_{n-1}(t) - 2(n-1)H_{n-2}(t)$

with initial conditions $H_0(t) = 1$ and $H_1(t) = 2t$.

Given a set of $n+1$ data points $(x_i, y_i)$, where $i = 0, 1, \ldots, n$, and their corresponding tangent vectors $(v_i, w_i)$, Hermite curve interpolation generates a curve that smoothly passes through these points while aligning with the specified tangent vectors.

To construct the Hermite curve, we define a parametric equation in terms of the Hermite polynomials and the data points:

$C(t) = \sum_{i=0}^{n} \left[ H_{n,i}(t) \cdot x_i + H_{n,i}(t) \cdot y_i \right]$

where $H_{n,i}(t)$ is the $i$-th Hermite polynomial of degree $n$ evaluated at $t$.

The tangent vectors $v_i$ and $w_i$ represent the direction and magnitude of the curve at each data point. They influence the curvature and smoothness of the resulting curve. Typically, these tangent vectors are computed based on the local geometry or specific requirements of the application.

Mise en œuvre et exemple Python :

To better understand the practical implementation of Hermite curve interpolation, let's consider a simple example in Python using the `matplotlib` library.

import numpy as np
import matplotlib.pyplot as plt

def hermite_interpolation(x, y, v, w, num_points=100):
    n = len(x) - 1  # Degree of the Hermite polynomial
    t = np.linspace(0, 1, num=num_points)  # Parameter range

    # Evaluate the Hermite polynomial for each data point
    H = np.array([((1 - 2 * t) * ((1 - t) ** 2)) ** i for i in range(n + 1)])

    # Compute the interpolated curve
    Cx = np.dot(H, x)
    Cy = np.dot(H, y)

    # Plot the interpolated curve
    plt.plot(Cx, Cy, label='Hermite Interpolation')
    plt.scatter(x, y, color='red', label='Data Points')
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.title('Hermite Curve Interpolation')
    plt.legend()
    plt.show()

# Example usage
x = [0, 1, 2]  # X-coordinates of data points
y = [0, 2, 1]  # Y-coordinates of data points
v = [1, 2, 3] # Tangent vector in the x-direction
w = [0, -1, 1] # Tangent vector in the y-direction

hermite_interpolation(x, y, v, w)

In this example, we define the x and y coordinates of the data points (`x` and `y`) as well as the tangent vectors in the x and y directions (`v` and `w`). We pass these parameters to the `hermite_interpolation` function, which performs the Hermite curve interpolation. The function evaluates the Hermite polynomials for each data point and computes the interpolated curve using the dot product. Finally, the interpolated curve, along with the original data points, is plotted using `matplotlib`.

Hermite curve interpolation provides a powerful method for smoothly connecting data points while preserving continuity and controlling the curvature of the resulting curve. By incorporating tangent vectors, we can achieve fine-grained control over the shape and behavior of the interpolated curve. In this section, we discussed the mathematical foundations of Hermite curve interpolation and provided a Python implementation for practical usage. Hermite curves find widespread application in various fields, including computer graphics, animation, and design, enabling the creation of aesthetically pleasing and visually smooth curves.

Catmull-Rom Cubic Interpolation: Achieving Local Control and Smoothness

In the realm of computer graphics and animation, Catmull-Rom cubic interpolation is a widely used technique for smoothly interpolating between a sequence of data points. Named after Edwin Catmull and Raphael Rom, who introduced this method in 1974, Catmull-Rom interpolation offers local control, meaning that the interpolation of each segment depends only on neighboring data points. This property makes it particularly useful for creating smooth and visually appealing curves. In this section, we will delve into the concept of Catmull-Rom cubic interpolation, discuss its mathematical foundations, and explore examples of its implementation.

Mathematical Foundations:

Catmull-Rom cubic interpolation utilizes a piecewise-defined cubic polynomial to interpolate between data points. Given a set of data points $(x_i, y_i)$, where $i = 0, 1, \ldots, n$, the Catmull-Rom spline generates a smooth curve that passes through these points while preserving local control.

The Catmull-Rom interpolation formula for a segment between data points $P_1$ and $P_2$ is given by:

$C(t) = \frac{1}{2}\left((2P_1 - 2P_2 + M_1 + M_2)t^3 + (-3P_1 + 3P_2 - 2M_1 - M_2)t^2 + M_1t + P_1\right)$

where $t$ is the parameter within the range $[0, 1]$, and $M_1$ and $M_2$ are the tangent vectors calculated based on the neighboring data points.

The tangent vectors are computed as follows:

  • $M_1 = \frac{P_2 - P_0}{2}$
  • $M_2 = \frac{P_3 - P_1}{2}$

By adjusting the values of the tangent vectors, we can control the direction and curvature of the resulting curve.

Implementation and Python Example:

To gain a practical understanding of Catmull-Rom cubic interpolation, let's consider a Python example using the `matplotlib` library.

import numpy as np
import matplotlib.pyplot as plt

def catmull_rom_interpolation(x, y, num_points=100):
    n = len(x) - 1  # Number of data points
    t = np.linspace(0, 1, num=num_points)  # Parameter range

    # Calculate tangent vectors
    M1 = (x[2:] - x[:-2]) / 2
    M2 = (y[2:] - y[:-2]) / 2

    # Perform Catmull-Rom interpolation
    Cx = 0.5 * ((2 * x[1:-1]) - (2 * x[2:]) + M1 + M2) * t**3 + \
         (-3 * x[1:-1] + 3 * x[2:] - 2 * M1 - M2) * t**2 + \
         M1 * t + x[1:-1]

    Cy = 0.5 * ((2 * y[1:-1]) - (2 * y[2:]) + M1 + M2) * t**3 + \
         (-3 * y[1:-1] + 3 * y[2:] - 2 * M1 - M2) * t**2 + \
         M1 * t + y[1:-1]

    return Cx, Cy

# Example usage
x = [0, 1, 2, 3, 4] # X-coordinates of data points
y = [0, 3, 1, 2, 1] # Y-coordinates of data points
Cx, Cy = catmull_rom_interpolation(x, y)

# Plot the interpolated curve
plt.plot(Cx, Cy, label='Catmmull-Rom Interpolation')
plt.scatter(x, y, color='red', label='Data Points')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Catmull-Rom Cubic Interpolation')
plt.legend()
plt.show()

In this example, we provide a sequence of data points (`x` and `y`) and pass them to the `catmull_rom_interpolation` function. The function calculates the tangent vectors `M1` and `M2` based on the neighboring data points, and then performs Catmull-Rom cubic interpolation using the provided formula. The resulting interpolated curve is plotted using `matplotlib`, along with the original data points.

Catmull-Rom cubic interpolation is a powerful technique for generating smooth curves that pass through a sequence of data points while maintaining local control. By adjusting the tangent vectors, we can influence the direction and curvature of the interpolated curve, allowing for a high degree of customization. In this section, we explored the mathematical foundations of Catmull-Rom interpolation and provided a Python implementation for practical usage. Catmull-Rom interpolation finds widespread application in computer graphics, animation, and other domains where smooth and visually appealing curves are desired.