# Module 1.3: Transform Networks (TF Trees)

- Course: [Course 1: Frames, Transform Networks & Intro to Kinematics](https://roboticsconcepts.dev/courses/frames-transform-networks-kinematics.md)
- HTML Page: https://roboticsconcepts.dev/courses/frames-transform-networks-kinematics/transform-networks-tf-trees/
- Key Concepts: Homogeneous transform matrices, Directed TF trees & inversions, 3D orientations & quaternions

Once a robot has a base, a shoulder, an elbow, a wrist, a camera, and objects sitting on a table, keeping track of loose (x, y, θ) offsets by hand falls apart fast. You need a clean data structure that connects every frame together without creating contradictions.

## Packing rotation and translation into one matrix

Instead of applying a rotation and then adding a translation vector as two separate steps, we can tuck both into a single 3×3 homogeneous matrix in 2D (or 4×4 in 3D). That trick lets us chain ten coordinate changes together simply by multiplying matrices in order.

Even better: because a rigid motion never squishes or stretches space, every transform matrix has a clean inverse that walks backward from child to parent.

## Why a directed tree (and never a loop)

In systems like ROS (TF2), Drake, and MuJoCo, every frame has exactly one parent frame, forming a directed tree rooted at world. Why forbid loops? Imagine storing both world → camera and wrist → camera independently: the moment a joint angle updates slightly out of sync, the camera would disagree with itself on where it is.

With a tree, there is always one unique path between any two frames — say, from mug up to world and back down through the arm to camera.

## Interactive Sandbox: Transform Network Graph & Tabletop Inspector

Click frames in the TF Tree on the left (or use the Express / In Frame dropdowns) to ask questions like 'where is the mug in the camera frame?' Notice which edges turn amber (walked backward via T⁻¹) and which stay blue (walked forward via T).

## Stepping into 3D: why quaternions avoid gimbal lock

In 2D, orientation is a single angle θ, so things stay friendly. In 3D, engineers often visualize orientation using three sequential Euler angles (roll, pitch, yaw). They're great for humans to read, but they hide a nasty trap called gimbal lock: pitch a camera straight up 90°, and the roll and yaw axes align so you lose a degree of freedom and interpolation wobbles wildly.

That's why modern TF trees store 3D rotations internally as unit quaternions (four numbers representing an axis in 3D space plus a twist around it) or 3×3 rotation matrices. You can still print roll-pitch-yaw for debugging, while letting quaternions keep the math smooth everywhere.

## Chaining and inverting across a TF tree

```text
T_camera←mug = (T_wrist→camera)⁻¹ · (T_shoulder→wrist)⁻¹ · (T_base→shoulder)⁻¹ · (T_world→base)⁻¹ · T_world→table · T_table→mug
```

Climbing up toward the common ancestor uses inverse transforms (T⁻¹); walking down to the target frame uses forward transforms (T).

## Check Your Intuition

**Question:** In the TF tree world → base → shoulder → wrist → camera, you want to find the position of the shoulder expressed in the camera's frame. How does the lookup walk the tree?

- [ ] All forward edges from shoulder down to camera
- [✓] Inverted edges walking up from camera → wrist → shoulder
- [ ] All the way up to world and back down to shoulder

**Explanation:** The lowest common ancestor of camera and shoulder is shoulder itself, so you only invert the edges between camera, wrist, and shoulder.

## Where You'll See This in Modern Robotics

Whenever a real robot arm misses a grasp by 5 cm, the culprit is usually a stale TF static transform or a backwards parent/child order in a calibrated camera extrinsic.

## Companion Python Notebook: 03 · Homogeneous Matrices & TF Tree Lookup Lab

Implement make_se2(), invert_se2(), and a working TFTree class that chains forward and inverted edges between any two frames.

- Open in molab (WASM): https://molab.marimo.io/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/course-1/03_transform_networks_tf_trees.py/wasm
- Open in Google Colab: https://colab.research.google.com/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/colab/course-1/03_transform_networks_tf_trees.ipynb
- Download .py: https://roboticsconcepts.dev/downloads/notebooks/course-1/03_transform_networks_tf_trees.py
- Download .ipynb: https://roboticsconcepts.dev/downloads/notebooks/colab/course-1/03_transform_networks_tf_trees.ipynb
- Run locally: `uvx marimo edit --sandbox notebooks/course-1/03_transform_networks_tf_trees.py`
