dimforge / nalgebra

Linear algebra library for Rust.
https://nalgebra.org
Apache License 2.0
4.05k stars 485 forks source link

Dual Quaternions for Rigid Body Transforms #487

Open miakramer opened 6 years ago

miakramer commented 6 years ago

Dual quaternions can represent all of rigid body transformations (translation + rotation), with some advantages over matrixes:

They also keep the simplicity of combining transformations is just multiplication.

A downside is that they only work for 3 dimensions (if I understand correctly), but given a large portion of users likely will be using only 3D geometry (for graphics, etc), it might be worth it. Also, there is already support for non-dual quaternions, so there is some interest.

Many of you probably already know this, just thought I'd document the case for them as I understand it.

If people are interested in this, I'll create a fork and add the feature? This is my first time contributing to an open-source project on github, so I may need a little guidance... Or, if someone else is interested in writing it that would work as well.

sebcrozet commented 6 years ago

Hi! Adding dual quaternions would be a very interesting addition to nalgebra! As far as I know, nobody is working on this right now so your contribution is welcome! I'd be curious to see how dual quaternions compare to the existing Isometry3 structure which are representations of rigid-body transformations with 7 values and one algebraic constraint (one unit quaternion and one 3D vector).

If you need guidance, I suggest you open a pull-request even if you have not yet finished, and request for reviews when you need them. You should also see how quaternions are currently implemented. In particular, following the same file structure you should add:

Of course don't hesitate to ask questions on github, or on the nalgebra discord chanel!

arturoc commented 6 years ago

dual quaternions are also really useful to avoid some artifacts while doing mesh skinning compared to using matrices

miakramer commented 6 years ago

Should I work on the master branch or create a new feature branch? I'm also curious to see how they compare. I know they're used a lot in IK and robotics due to the numerical stability. Maybe after writing them I'll do some benchmarks for speed and stability (since using Isometry3 has the numerical stability in angle from the quaternion already).

sebcrozet commented 6 years ago

Should I work on the master branch or create a new feature branch?

Master is okay, we don't have specific rules for the source branch of pull-requests.

Maybe after writing them I'll do some benchmarks for speed and stability (since using Isometry3 has the numerical stability in angle from the quaternion already).

That would be great!

Nivek92 commented 4 years ago

I would love to see this revived. I believe DualQuaternions would make a great addition to nalgebra as they find more and more use in different engineering categories (robotics, aerospace, etc.) and game development.

Are there people interested in working on this together?

miakramer commented 4 years ago

I'm sorry I didn't get to the fork, finals came up right after this and it slipped my mind. I don't think I could head it but I would definitely be interested in contributing. It'd be very interesting to try out for games, and I bet CPUs with wide SIMD registers may be able to compute with them very efficiently.

On another note, for rotations I wonder if wedge products are a better fit than quaternions? They generalize to higher dimensions while iirc quaternions only represent rotations in three and four dimensions, and they're similar in practice

chinedufn commented 3 years ago

I'd be curious to see how dual quaternions compare to the existing Isometry3 structure

The main advantage of DQs is that they are excellent for linear blending / interpolation. In practice this means that they are excellent for vertex skinning.

If you interpolate two DQs you will get the shorted path rotation between them.

vs. you can't just blend two Isometry3 and get a shorted path rotation between them.


I was in the middle of writing a DQ multiply function in a project and thought... How come nalgebra doesn't have this.

Just found this issue - glad to see that there's interest.

Here's how I see the path forwards:

  1. PR a DualQuaternion struct that contains the 8 floats (perhaps stored as two quaternions). Release this. Now anyone that is writing their own DQ math is at least working against the same type.

  2. Over time people can contribute different functions such as multiplication ... linear blend ... etc

  3. As these functions get contributed we can replace out own math in our own projects with the math in nalgebra.

Basically I think that this will be more likely to progress as a lot of small additions instead of one big PR adding full DQ support.


I'll PR step number one and if it lands I'd be happy to follow up with PRs contributing misc DQ code I have stored in misc places.

sebcrozet commented 3 years ago

Here's how I see the path forwards:

1. PR a `DualQuaternion` struct that contains the 8 floats (perhaps stored as two quaternions). Release this. Now anyone that is writing their own DQ math is at least working against the same type.

2. Over time people can contribute different functions such as multiplication ... linear blend ... etc

3. As these functions get contributed we can replace out own math in our own projects with the math in `nalgebra`.

@chinedufn Sounds like a reasonable way forward, yes.

miakramer commented 3 years ago

I know I was the one to make this issue originally, but I've been learning about geometric algebra for my research and I think it's a far better domain than the hypercomplex numbers. They're largely equivalent in two and three dimensions (see this excellent blog post: https://marctenbosch.com/quaternions/), and they generalize efficiently to higher dimensions (rotations in $n$ dimensions require $n(n-1)/2 + 1$ floats, rather than the $n^2$ of a rotation matrix, plus the same benefits that quaternions have in terms of numerical stability). I'm not trying to discourage the use of dual quaternions, because they still seem very cool and useful, just suggesting that folks may be interested in the geometric algebra approach in addition. You can add homogeneous coordinates to get back all of E(n). Geometric algebra also extends well to non-Euclidean manifolds (see Grassman.jl).

chinedufn commented 3 years ago

I think that this can be closed

tpdickso commented 3 years ago

https://github.com/dimforge/nalgebra/pull/824 should add the majority of missing functionality; but let me know if there's something that I missed, because I'd be glad to help complete it.

ChristopherRabotin commented 3 years ago

Hi there, I am the maintainer of the hyperdual crate that defines multi-dimensional dual numbers used for automatic differentiation. I'd be happy to help out on this feature as well. Cheers