---
title: Rotations and poses
summary: Score an attitude or a pose with the normal, give an unknown one the uniform distribution, draw it, observe one given from outside in whatever sign, and read Euler angles off it, with the crates skoro-spatial and skoro-uniform, from their own tests and examples.
order: 2
---

# Rotations and poses

The crate `skoro-spatial` gives a model its rotations, rigid motions and extended poses.
This page shows what a user writes with them: a quantity of one of these types, scored by [the normal](/concepts/the-normal.md) and drawn, a pose with its noise in its own frame, measured poses read from a file and observed as they come, and Euler angles read off a rotation and compared, and a rotation of which nothing is known, given the uniform distribution of `skoro-uniform`.
Each step quotes the crates' tests, so every line shown compiles and runs.
The [concepts page](/concepts/rotations-and-motions.md) explains how the types are made and what each map computes.

## A rotation in a model

A rotation is a quantity of the marker `Rotation`, and the normal takes it like any other space: its mean is a rotation, here a constant unit quaternion $(w, x, y, z)$, and its scale is given in the rotation's tangent coordinates, the rotation vector in the frame of the mean, in radians.
Generating the model draws the attitude exactly, one row per draw:

From `crates/skoro-spatial/tests/normal.rs`, lines 176–187:

```rust
let mut writer = Writer::new();
let q = writer
    .build(|| {
        let q = quantity("q", Rotation);
        Normal::on(Rotation, constant(Rotation, mean), Scale::sds(&sds)).score(q);
        q
    })
    .unwrap();
let generated = writer
    .snapshot()
    .generate(&Observations::new(), &Key::from_seed(703), rows, &target)
    .unwrap();
```

`generated.values.get(q)` holds the draws as unit quaternions, four numbers to a row.
A quaternion and its negation are the same rotation, and the types know it: every density, derivative and map is the same whichever of the two a value is stored as, so a draw may carry either sign, and a quaternion observed from outside the model is observed as it comes.
To report one sign, take `host::canonical`, the quaternion with $w \ge 0$.

## A rotation of which nothing is known

The scale of a normal on a rotation is in radians, and it is meant to be small.
A standard deviation past one radian does not say that little is known of the rotation: it moves the draws towards the half-turns from the mean, and at $\sigma = 100$ nearly all of them lie within a tenth of a radian of one ([the normal's page](/concepts/the-normal.md#a-large-scale-on-a-curved-type) has the numbers).
A rotation of which nothing is known is given the uniform distribution of the crate `skoro-uniform`, which takes every space of finite mass, the rotations and the categorical spaces among them.
Here a heading is unknown, a compass reads it with an error of 0.05 rad, and a coin is tossed beside it:

From `crates/skoro-uniform/tests/sample.rs`, lines 128–144:

```rust
let mut writer = Writer::new();
let (heading, compass) = writer
    .build(|| {
        let heading = sample("heading", Rotation, Uniform::on(Rotation));
        sample("outlier", Boolean, Uniform::on(Boolean));
        let noise = Normal::on(Rotation, heading, Scale::sd(0.05));
        let compass = sample("compass", Rotation, noise);
        (heading, compass)
    })
    .unwrap();
let snapshot = writer.snapshot();
let (rows, target) = (16, Target::host());
let reading = [0.5, 0.1, -0.3, 0.7];
let given = Observations::new().observe(compass, reading);
let generated = snapshot
    .generate(&given, &Key::from_seed(41), rows, &target)
    .unwrap();
```

Generation draws the heading uniformly and weighs each row by the compass alone.
Minimized, the heading is where the compass reads it, and with no compass at all a linearization reports the heading undetermined, since every rotation is as likely as any other.
The [concepts page](/concepts/the-uniform.md) explains the term and its sampler.

## A pose

A pose is a rotation and a position, made from host values as a pair: here `mean`, the quaternion `[0.3, 0.5, -0.7, 0.4]`, normalized when the constant is made, and the position `[1.0, -2.0, 0.5]`.
Its tangent coordinates are the rotation vector and then the displacement, both in the pose's own frame, so a normal on `Pose` is noise in the frame of the body, as odometry gives it.
Here a pose is moved from its mean through its chart by the tangent `q[0]`, and the normal's log-density is taken there, with a full covariance over the six coordinates:

From `crates/skoro-spatial/tests/normal.rs`, lines 148–151:

```rust
let mu = constant(Pose, mean);
let x = mu.retract(q[0]);
let parts = Normal::on(Pose, mu, Scale::covariance(&covariance, 6)).log_density(x);
(parts.log_density + mu.log_volume(q[0])).untyped()
```

With the chart's log-volume added back, as here, this is the Gaussian log-density of the tangent, which the test checks against the host's.

`*` composes poses and applies them to points, and `PoseMaps` gives a pose's rotation, position, inverse and relative motion, among others.

## Poses measured outside the model

Pose graphs are usually given as files in the format of g2o: each vertex an initial pose, each edge a measured motion between two vertices with its information matrix, the quaternions in whatever sign the program that wrote them chose.
The example `g2o` reads one, by a reader in its common module that converts each edge's information to the precision of skoro's residual, and writes the model: the first vertex held at its pose, every other a parameter, and each edge a measured pose scored by the normal of the relative motion between its vertices.
The measurements are observed as they were read, and the vertices start where the file puts them:

From `crates/skoro/examples/g2o.rs`, lines 46–84:

```rust
// The model, the measurements and the start.
let mut writer = Writer::new();
let (vertices, measured) = writer.build(|| {
    let vertices: BTreeMap<usize, Q<Pose>> = graph
        .vertices
        .iter()
        .map(|(&k, x)| {
            let q = if k == 0 {
                constant(Pose, *x)
            } else {
                quantity(format!("vertex/{k}"), Pose)
            };
            (k, q)
        })
        .collect();
    let measured: Vec<Q<Pose>> = graph
        .edges
        .iter()
        .enumerate()
        .map(|(e, edge)| {
            let z = quantity(format!("edge/{e}"), Pose);
            let mean = vertices[&edge.from].between(vertices[&edge.to]);
            Normal::on(Pose, mean, Scale::precision(&edge.precision, 6)).score(z);
            z
        })
        .collect();
    (vertices, measured)
})?;
let mut observations = Observations::new();
for (z, edge) in measured.iter().zip(&graph.edges) {
    observations = observations.observe(*z, edge.measurement);
}
let mut start = Values::new();
for (k, x) in graph.vertices.iter().filter(|(k, _)| **k != 0) {
    start = start.with(
        vertices[k].id(),
        Value::Continuous(Points::from_host(x, 1, 7, &target)),
    );
}
```

Without a file, the example writes a loop of a hundred poses that turns once, whose vertices are composed from the odometry, so that the last one's quaternion is near $-1$ while the loop closure is measured near the identity: the measurement and its prediction are the same rotation in opposite signs, and the estimate converges all the same.

```sh
cargo run --release -p skoro --example g2o                # its own loop
cargo run --release -p skoro --example g2o -- garage.g2o  # any 3D pose graph g2o writes
```

A measurement given exactly a half-turn from its prediction is the one value a rotation's chart cannot read: the estimator refuses it, naming the term, so a start is chosen away from a half-turn from the data.
The [concepts page](/concepts/rotations-and-motions.md#where-a-map-is-undefined) says why.

## Euler angles

`euler` reads the angles of a convention off a rotation, and `rotation::from_euler` and `host::from_euler` make a rotation from them.
The first and last angles jump by $2\pi$ at $\pm\pi$, so a model compares them with `angle_residual`, which takes their difference on the circle.
Here the yaw of a rotation about $z$ by just under and just over a half-turn jumps from near $\pi$ to near $-\pi$, while its residual against a reference yaw of $3$ changes by $2 \cdot 10^{-5}$:

From `crates/skoro-spatial/tests/euler.rs`, lines 336–348:

```rust
let qs = [PI - side, PI + side].map(|angle| common::about([0.0, 0.0, 1.0], angle));
let out = common::evaluate_many(
    |q| {
        let angles = q[0].narrow::<Rotation>().euler(Euler::YAW_PITCH_ROLL);
        let reference = skoro_core::scope::constant(Vector::<3>, [3.0, 0.0, 0.0]);
        vec![angles.untyped(), angle_residual(angles, reference).untyped()]
    },
    &[rotations(&qs)],
    &target,
);
let (yaw, residual) = ([out[0][0], out[0][3]], [out[1][0], out[1][3]]);
assert!((yaw[0] - yaw[1] - (2.0 * PI - 2.0 * side)).abs() < tol.max(1e-5), "{yaw:?}");
close(&residual, &[PI - side - 3.0, PI + side - 3.0], tol.max(1e-6));
```

Near gimbal lock the separate angles are ill-conditioned, so a model's noise belongs in the rotation's chart rather than in its Euler angles; the [concepts page](/concepts/rotations-and-motions.md#euler-angles) says why.
