Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

3D Rotation using Quaternions

ratwolf

Bronze Coder
Python:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import quaternion  # Requires 'numpy-quaternion' package


def setup_figure():
    fig = plt.figure(figsize=(10, 8), dpi=100)
    ax = fig.add_subplot(111, projection='3d')
    lim = (-1.5, 1.5)
    ax.set(xlim=lim, ylim=lim, zlim=lim,
           title='Combined Quaternion Rotation')

    ax.view_init(elev=25, azim=45)
    return fig, ax


def create_geometry():
    theta = np.linspace(0, 2 * np.pi, 100)
    x, y = np.cos(theta), np.sin(theta)
    return [
        np.column_stack((x, y, np.zeros_like(x))),     # XY plane
        np.column_stack((np.zeros_like(x), x, y)),     # YZ plane
        np.column_stack((x, np.zeros_like(x), y))      # XZ plane
    ]


def initialize_artists(ax):
    colors = ['r', 'g', 'b']
    return [ax.plot([], [], [], f'{c}-', linewidth=2, alpha=0.8)[0] for c in colors]


class CombinedQuaternionRotator:
    def __init__(self):
        self.speeds = np.array([0.03, 0.02, 0.04])
        self.angles = np.zeros(3)

    def update_rotations(self):
        self.angles += self.speeds

        cos_angles = np.cos(self.angles / 2)
        sin_angles = np.sin(self.angles / 2)

        q_x = np.quaternion(cos_angles[0], sin_angles[0], 0, 0)
        q_y = np.quaternion(cos_angles[1], 0, sin_angles[1], 0)
        q_z = np.quaternion(cos_angles[2], 0, 0, sin_angles[2])

        q_combined = q_z * q_y * q_x
        return q_combined.normalized()


def update(frame, rotator, rings, artists):
    q_combined = rotator.update_rotations()

    for artist, ring in zip(artists, rings):
        rotated = quaternion.rotate_vectors(q_combined, ring)
        artist.set_data(rotated[:, :2].T)
        artist.set_3d_properties(rotated[:, 2])

    return artists


def main():
    fig, ax = setup_figure()
    rings = create_geometry()
    artists = initialize_artists(ax)
    rotator = CombinedQuaternionRotator()

    ani = FuncAnimation(
        fig, update, frames=200,
        fargs=(rotator, rings, artists),
        interval=20, blit=False  # blit=False for 3D animations
    )

    plt.tight_layout()
    plt.show()


if __name__ == "__main__":
    main()

GitHub
 
Last edited:
Python:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import quaternion  # Requires 'numpy-quaternion' package


def setup_figure():
    fig = plt.figure(figsize=(10, 8), dpi=100)
    ax = fig.add_subplot(111, projection='3d')
    lim = (-1.5, 1.5)
    ax.set(xlim=lim, ylim=lim, zlim=lim,
           title='Combined Quaternion Rotation')

    ax.view_init(elev=25, azim=45)
    return fig, ax


def create_geometry():
    theta = np.linspace(0, 2 * np.pi, 100)
    x, y = np.cos(theta), np.sin(theta)
    return [
        np.column_stack((x, y, np.zeros_like(x))),     # XY plane
        np.column_stack((np.zeros_like(x), x, y)),     # YZ plane
        np.column_stack((x, np.zeros_like(x), y))      # XZ plane
    ]


def initialize_artists(ax):
    colors = ['r', 'g', 'b']
    return [ax.plot([], [], [], f'{c}-', linewidth=2, alpha=0.8)[0] for c in colors]


class CombinedQuaternionRotator:
    def __init__(self):
        # Rotation rates around X, Y, Z
        self.speeds = np.array([0.03, 0.02, 0.04])
        self.angles = np.zeros(3)

    def update_rotations(self):
        # Update internal angles
        self.angles += self.speeds

        # Create individual rotation quaternions
        q_x = np.quaternion(
            np.cos(self.angles[0] / 2), np.sin(self.angles[0] / 2), 0, 0)
        q_y = np.quaternion(
            np.cos(self.angles[1] / 2), 0, np.sin(self.angles[1] / 2), 0)
        q_z = np.quaternion(
            np.cos(self.angles[2] / 2), 0, 0, np.sin(self.angles[2] / 2))

        # Combine them in Z * Y * X order (important!)
        q_combined = q_z * q_y * q_x

        # Normalize to prevent drift due to numerical error
        return q_combined.normalized()


def update(frame, rotator, rings, artists):
    q_combined = rotator.update_rotations()

    for artist, ring in zip(artists, rings):
        rotated = quaternion.rotate_vectors(q_combined, ring)
        artist.set_data(rotated[:, :2].T)
        artist.set_3d_properties(rotated[:, 2])

    return artists


def main():
    fig, ax = setup_figure()
    rings = create_geometry()
    artists = initialize_artists(ax)
    rotator = CombinedQuaternionRotator()

    ani = FuncAnimation(
        fig, update, frames=200,
        fargs=(rotator, rings, artists),
        interval=20, blit=False  # blit=False for 3D animations
    )

    plt.tight_layout()
    plt.show()


if __name__ == "__main__":
    main()

GitHub

Awesome job! It looks great.
 
10q! 🙏
slight code improvement:
pre-calculate cos_angles = np.cos(self.angles / 2) and sin_angles = np.sin(self.angles / 2)
in class CombinedQuaternionRotator
More efficient and cleaner.


Python:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import quaternion  # Requires 'numpy-quaternion' package


def setup_figure():
    fig = plt.figure(figsize=(10, 8), dpi=100)
    ax = fig.add_subplot(111, projection='3d')
    lim = (-1.5, 1.5)
    ax.set(xlim=lim, ylim=lim, zlim=lim,
           title='Combined Quaternion Rotation')

    ax.view_init(elev=25, azim=45)
    return fig, ax


def create_geometry():
    theta = np.linspace(0, 2 * np.pi, 100)
    x, y = np.cos(theta), np.sin(theta)
    return [
        np.column_stack((x, y, np.zeros_like(x))),     # XY plane
        np.column_stack((np.zeros_like(x), x, y)),     # YZ plane
        np.column_stack((x, np.zeros_like(x), y))      # XZ plane
    ]


def initialize_artists(ax):
    colors = ['r', 'g', 'b']
    return [ax.plot([], [], [], f'{c}-', linewidth=2, alpha=0.8)[0] for c in colors]


class CombinedQuaternionRotator:
    def __init__(self):
        self.speeds = np.array([0.03, 0.02, 0.04])
        self.angles = np.zeros(3)

    def update_rotations(self):
        self.angles += self.speeds

        cos_angles = np.cos(self.angles / 2)
        sin_angles = np.sin(self.angles / 2)

        q_x = np.quaternion(cos_angles[0], sin_angles[0], 0, 0)
        q_y = np.quaternion(cos_angles[1], 0, sin_angles[1], 0)
        q_z = np.quaternion(cos_angles[2], 0, 0, sin_angles[2])

        q_combined = q_z * q_y * q_x
        return q_combined.normalized()


def update(frame, rotator, rings, artists):
    q_combined = rotator.update_rotations()

    for artist, ring in zip(artists, rings):
        rotated = quaternion.rotate_vectors(q_combined, ring)
        artist.set_data(rotated[:, :2].T)
        artist.set_3d_properties(rotated[:, 2])

    return artists


def main():
    fig, ax = setup_figure()
    rings = create_geometry()
    artists = initialize_artists(ax)
    rotator = CombinedQuaternionRotator()

    ani = FuncAnimation(
        fig, update, frames=200,
        fargs=(rotator, rings, artists),
        interval=20, blit=False  # blit=False for 3D animations
    )

    plt.tight_layout()
    plt.show()


if __name__ == "__main__":
    main()
 
Back
Top Bottom