---
title: "Compose and invert rigid transforms in C#"
url: https://recursiveai.org/onemath/cookbook/compose-rigid-transforms/
description: "Compose and invert rigid transforms in C# with OneMath: runnable .NET examples, real output and a downloadable project. Free and open source."
version: "1.0.0"
---

# Compose and invert rigid transforms in C#

**OneMath** is a free, open-source deterministic math library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMath) · [GitHub](https://github.com/recursive-ai-oss/onemath-csharp) · [Docs](https://recursiveai.org/onemath/docs/) · [Download example (.zip)](https://recursiveai.org/onemath/cookbook/compose-rigid-transforms/compose-rigid-transforms.zip)

A rigid transform rotates and translates 3D coordinates without adding scale or shear.

```shell
dotnet add package RecursiveAI.OneMath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneMath;

// Half-turn about Z (x, y, z, w).
var rotate = new RigidTransformD(
    new(0, 0, 1, 0), Vector3D.Zero);
var move = new RigidTransformD(
    QuaternionD.Identity, new(10, 0, 0));
var combined = move * rotate;
var point = combined.TransformPoint(
    new(1, 2, 3));
Console.WriteLine(point);
Console.WriteLine(combined.Inverted()
    .TransformPoint(point));
```

```text
(9, -2, 3)
(1, 2, 3)
```

Unzip [the example](https://recursiveai.org/onemath/cookbook/compose-rigid-transforms/compose-rigid-transforms.zip), then `dotnet run`.

## The full program

```csharp
using RecursiveAI.OneMath;

var rotation = QuaternionD.CreateFromAxisAngle(Vector3D.UnitZ, 0.7);
var rotate = new RigidTransformD(rotation, Vector3D.Zero);
var move = new RigidTransformD(QuaternionD.Identity, new(10, 0, 0));
var combined = move * rotate;
var original = new Vector3D(1, 2, 3);
var transformed = combined.TransformPoint(original);
var restored = combined.Inverted().TransformPoint(transformed);
Console.WriteLine($"Transformed: {transformed}");
Console.WriteLine($"Restored: {restored}");
double error = (restored - original).Length();
Console.WriteLine($"Within 1e-12: {error <= 1e-12}");
Console.WriteLine($"Reverse order: {(rotate * move).TransformPoint(original)}");
```

`dotnet run` prints:

```text
Transformed: (9.476406812809106, 2.173902061806668, 3)
Restored: (0.9999999999999991, 1.9999999999999991, 3)
Within 1e-12: True
Reverse order: (7.12482868565399, 8.616078934183578, 3)
```

`RigidTransformD` assumes a unit quaternion and does not renormalize it; an arbitrary non-unit quaternion is not a valid rigid rotation.

## Variations

Transform a direction without translating it:

```csharp
using RecursiveAI.OneMath;

var transform = new RigidTransformD(
    new(0, 0, 1, 0), new(10, 0, 0));
Console.WriteLine(transform.TransformPoint(Vector3D.UnitX));
Console.WriteLine(transform.TransformVector(Vector3D.UnitX));
```

```text
(9, 0, 0)
(-1, 0, 0)
```

## Related recipes

- [Invert a matrix](https://recursiveai.org/onemath/cookbook/invert-matrix/)
- [Vector and quaternion math](https://recursiveai.org/onemath/cookbook/vector-and-quaternion-math/)
- [Convert System.Numerics conventions](https://recursiveai.org/onemath/cookbook/port-from-system-numerics/)

## The short answer

Multiply `RigidTransformD` values to compose rotations and translations, then call `Inverted()` to reverse the combined transform. OneMath uses column vectors: `a * b` applies `b` first. For a rotation followed by a translation, write `move * rotate`. Apply it to a position with `TransformPoint`.

The snippet at the top uses the unit quaternion `(0, 0, 1, 0)`, a half-turn about Z. It turns `(1, 2, 3)` into `(−1, −2, 3)` before adding the translation `(10, 0, 0)`. The full program instead uses an angle of 0.7 radians so you can see a general rotation and a floating-point round-trip check.

## How do you compose rigid transforms in C#?

Construct each transform from its quaternion rotation and translation vector. Within one `RigidTransformD`, rotation always happens first: `Rotation.Rotate(point) + Translation`. A translation-only transform uses `QuaternionD.Identity`; a rotation-only transform uses a zero translation.

When composing `a * b`, OneMath multiplies the quaternions as `a.Rotation * b.Rotation`. The new translation is `a.Rotation.Rotate(b.Translation) + a.Translation`. In particular, you cannot generally add the translations and ignore the rotations. A parent’s rotation changes the direction of a child’s translation.

The full program prints the result for both orders. In `move * rotate`, the translation remains along the world X axis. In `rotate * move`, that translation is itself rotated. This makes the example useful when choosing between local-to-parent and parent-to-world operations: write down which coordinate frame each input and output belongs to before multiplying them. The [evaluation-order docs](https://recursiveai.org/onemath/docs/evaluation-order/) specify the column-vector convention and the transform formulas.

## How do you invert a rigid transformation?

Call `combined.Inverted()`. The [implementation](https://github.com/recursive-ai-oss/onemath-csharp/blob/1986f32ecb9f5f3001c0121484f104c489720334/src/OneMath/Types/RigidTransformD.cs) conjugates the quaternion and sets the inverse translation to `-conjugate.Rotate(Translation)`. Merely negating the original translation is not enough when rotation is present.

If you invert the pieces separately, reverse their order as well: the inverse of `a * b` is `b.Inverted() * a.Inverted()`. Stanford’s [rigid-transformation worked solution](https://graphics.stanford.edu/courses/cs248-98-fall/Final/q4.html) shows the same rule in matrix form. Inverting the already-combined value avoids having to maintain a second ordering expression in application code.

The rotation must be unit length. The constructor and multiplication do not renormalize it, and inversion uses conjugation rather than dividing by an arbitrary quaternion’s squared length. If your input contains scale or shear, it is not the rigid-transform task covered here; use an affine or matrix representation and its appropriate inversion method instead.

### Checking the restored point

Transform a known position, apply the inverse, and measure the distance from the original. The full program uses a Euclidean error threshold of `1e-12`. This is an example policy for double-precision coordinates of roughly unit-to-ten magnitude and a short chain, not a library guarantee or a universal physical tolerance. Choose a threshold in your application’s units and test representative coordinate magnitudes and chain lengths.

Deterministic floating-point operations do not make every inverse round-trip algebraically exact. Even when two runs produce the same bits, rounding can still separate the restored point from its starting position. Finally, use `TransformVector` for a direction: as the variation shows, it applies rotation but deliberately ignores translation.
