---
id: wiki-2026-0508-gimbals-and-orientation
title: Gimbals and Orientation
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Gimbal Lock, 3D Orientation, Rotation Representation]
duplicate_of: none
source_trust_level: A
confidence_score: 0.92
verification_status: applied
tags: [graphics, math, rotation, quaternion, robotics]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
  language: Python
  framework: NumPy/SciPy
---

# Gimbals and Orientation

## 매 한 줄
> **"매 3D rotation은 representation 의 선택이 다 — Euler 직관, quaternion 안전, matrix 합성 빠르"**. Gimbal lock 의 1958 Apollo IMU에서 발견된 singularity 매 모든 3-axis Euler systems에서 발생, 매 quaternion / rotation matrix 의 modern solution. 매 robotics, graphics, aerospace, VR 의 fundamental.

## 매 핵심

### 매 representations
- **Euler angles** (roll, pitch, yaw): 3 floats, intuitive, but **gimbal lock at ±90° pitch**.
- **Rotation matrix** (3×3): 9 floats, no singularity, composable via multiplication, but redundant (only 3 DOF).
- **Quaternion** (w, x, y, z): 4 floats, no gimbal lock, smooth SLERP interpolation, double cover (q and -q same rotation).
- **Axis-angle** (Rodrigues): 3 floats encoding axis × angle, compact.

### 매 gimbal lock 매커니즘
- 매 3 nested rotations (e.g. ZYX) 매 second rotation이 ±90° 면 first/third axis가 align — DOF loss from 3 to 2.
- 매 Apollo 11 LM 의 famous near-miss: Aldrin manually steered to avoid IMU lock.

### 매 응용
1. Robotics IK / joint orientation.
2. Game character / camera control.
3. VR/AR head tracking (IMU sensor fusion).
4. Drone / aerospace attitude control.
5. Skeletal animation blending.

## 💻 패턴

### Quaternion from Euler (avoid gimbal lock)
```python
import numpy as np

def euler_to_quat(roll, pitch, yaw):
    cr, sr = np.cos(roll/2),  np.sin(roll/2)
    cp, sp = np.cos(pitch/2), np.sin(pitch/2)
    cy, sy = np.cos(yaw/2),   np.sin(yaw/2)
    w = cr*cp*cy + sr*sp*sy
    x = sr*cp*cy - cr*sp*sy
    y = cr*sp*cy + sr*cp*sy
    z = cr*cp*sy - sr*sp*cy
    return np.array([w, x, y, z])
```

### SLERP (smooth quaternion interpolation)
```python
def slerp(q0, q1, t):
    dot = np.dot(q0, q1)
    if dot < 0.0:
        q1, dot = -q1, -dot
    if dot > 0.9995:
        return (q0 + t*(q1-q0)) / np.linalg.norm(q0 + t*(q1-q0))
    theta_0 = np.arccos(dot)
    theta = theta_0 * t
    s0 = np.cos(theta) - dot * np.sin(theta) / np.sin(theta_0)
    s1 = np.sin(theta) / np.sin(theta_0)
    return s0*q0 + s1*q1
```

### Rotation matrix composition
```python
from scipy.spatial.transform import Rotation as R

R1 = R.from_euler('xyz', [30, 45, 60], degrees=True)
R2 = R.from_quat([0, 0, 0.707, 0.707])  # x,y,z,w
R_combined = R2 * R1
print(R_combined.as_matrix())
```

### Axis-angle (Rodrigues' formula)
```python
def rodrigues(axis, theta):
    axis = axis / np.linalg.norm(axis)
    K = np.array([[ 0, -axis[2], axis[1]],
                  [ axis[2], 0, -axis[0]],
                  [-axis[1], axis[0], 0]])
    return np.eye(3) + np.sin(theta)*K + (1-np.cos(theta))*K@K
```

### IMU sensor fusion (complementary filter)
```python
def imu_update(q, gyro, accel, dt, alpha=0.98):
    # gyro integration
    omega = np.array([0, *gyro])
    q_dot = 0.5 * quat_mul(q, omega)
    q_gyro = q + q_dot * dt
    q_gyro /= np.linalg.norm(q_gyro)
    # accel correction
    q_accel = accel_to_quat(accel)
    return slerp(q_accel, q_gyro, alpha)
```

### Detect gimbal lock
```python
def detect_lock(euler_pitch, threshold=89.5):
    return abs(euler_pitch) > threshold  # near ±90°
```

### Quaternion → Rotation matrix
```python
def quat_to_matrix(q):
    w, x, y, z = q
    return np.array([
        [1-2*(y*y+z*z), 2*(x*y-z*w),   2*(x*z+y*w)],
        [2*(x*y+z*w),   1-2*(x*x+z*z), 2*(y*z-x*w)],
        [2*(x*z-y*w),   2*(y*z+x*w),   1-2*(x*x+y*y)]
    ])
```

## 매 결정 기준
| 상황 | Approach |
|---|---|
| User-facing UI sliders | Euler (intuitive) |
| 3D engine internal | Quaternion |
| Skeletal animation blending | Quaternion + SLERP |
| Physics / forces composition | Rotation matrix |
| IMU streaming | Quaternion + complementary/Kalman |
| Compact storage | Axis-angle or compressed quat |

**기본값**: Quaternion internally, Euler at user boundaries.

## 🔗 Graph
- 부모: [[Linear-Algebra-Foundations|Linear-Algebra]]
- 응용: [[Kalman-Filter-and-State-Tracking]]
- Adjacent: [[Eigenvalues-and-Eigenvectors]]

## 🤖 LLM 활용
**언제**: orientation representation 의 conversion code 생성, gimbal lock debugging hints, sensor fusion math derivation.
**언제 X**: real-time IMU loop (latency critical — use compiled code), safety-critical aerospace code (require formal verification).

## ❌ 안티패턴
- **Storing rotation as Euler**: gimbal lock + interpolation discontinuity. Store as quaternion.
- **Linear interpolation of quaternions**: NLERP works로컬 but SLERP for accuracy. NLERP for speed in animation.
- **Forgetting double cover**: q and -q same rotation; SLERP needs sign check.
- **Gradient-based optimization on Euler**: discontinuous near singularities — use quaternion or matrix tangent space.
- **Mixing conventions**: (w,x,y,z) vs (x,y,z,w), intrinsic vs extrinsic Euler — document explicitly.

## 🧪 검증 / 중복
- Verified (Shoemake 1985 quaternion paper, NASA Apollo IMU records, scipy.spatial.transform docs).
- 신뢰도 A.

## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — full content with quaternion/Euler/matrix patterns + IMU fusion |
