How to use 4d rotors
Asked Answered
A

3

5

I'm trying to create a 4D environment, similar to Miegakure's.

I'm having trouble understanding how to represent rotations. The creator of Miegakure wrote this small article explaining he made a class for 4d rotors. http://marctenbosch.com/news/2011/05/4d-rotations-and-the-4d-equivalent-of-quaternions/

How can I implement the functions of this class ? In particular the functions to rotate vectors and other rotors, and getting the inverse ?

I would appreciate some pseudocode examples. Thanks a lot to anyone who bothers answering.

Asaasabi answered 14/7, 2017 at 17:6 Comment(2)
Didn't he already provide the implementation?Ventricular
@spug I do not see any ... those are just headers but I am too lazy to dig in the archives there ...Emmerie
A
3

I was able to use Rotors after learning more on the subject thanks to this youtube series about Geometric Algebra : https://www.youtube.com/watch?v=PNlgMPzj-7Q&list=PLpzmRsG7u_gqaTo_vEseQ7U8KFvtiJY4K

It's really well explained and I recommend it to whoever wants use geometric algebra.

If you already know about Quaternions multiplication, Rotor multiplication won't be any different, and the i, j, k units of quaternions are analog to the basis bivectors of Geometric Algebra : e12, e13, e23 (or e01, e02, e12)

So a Rotor in 4D will be (A + B*e12 + C*e13 + D*e14 + E*e23 + F*e24 + G*e34 + H*e1234).

A table showing how to multiply those units can be found on this page : http://www.euclideanspace.com/maths/algebra/clifford/d4/arithmetic/index.htm

To get the gist of it, consider the 2D Rotors.

They're of the form: R = A + B*e12

Now, if we compute product between 2 arbitrary rotors R_1 and R_2, we get:

R_1*R_2 = (
  R_1.a     * R_2.a
+ R_1.a     * R_2.b*e12
+ R_1.b*e12 * R_2.a
+ R_1.b*e12 * R_2.b*e12 )
// but: e12*e12 = e1e2e1e2 = -e1e2e2e1= -e1e1 = -1
// this is confirmed by the computation rules I linked above
=
( (R_1.a * R_1.a - R_2.b * R_2.b)
+ (R_1.a * R_2.b + R_1.b * R_2.a) * e12 )

So in code, you would do something like :

R_3.a = R_1.a * R_2.a - R_1.b * R_2.b
R_3.b = R_1.a * R_2.b + R_1.b * R_2.a

Now it's only a matter of doing the same with those big 4D rotors, applying the multiplication rules for dimension 4 as linked above.

Here is the resulting code (using e0, e1, e2, e3 as basis vectors):

e: self.e*other.e - self.e01*other.e01 - self.e02*other.e02 - self.e03*other.e03 - self.e12*other.e12 - self.e13*other.e13 - self.e23*other.e23 + self.e0123*other.e0123,
e01: self.e*other.e01 + self.e01*other.e - self.e02*other.e12 - self.e03*other.e13 + self.e12*other.e02 + self.e13*other.e03 - self.e23*other.e0123 - self.e0123*other.e23,
e02: self.e*other.e02 + self.e01*other.e12 + self.e02*other.e - self.e03*other.e23 - self.e12*other.e01 + self.e13*other.e0123 + self.e23*other.e03 + self.e0123*other.e13,
e03: self.e*other.e03 + self.e01*other.e13 + self.e02*other.e23 + self.e03*other.e - self.e12*other.e0123 - self.e13*other.e01 - self.e23*other.e02 - self.e0123*other.e12,
e12: self.e*other.e12 - self.e01*other.e02 + self.e02*other.e01 - self.e03*other.e0123 + self.e12*other.e - self.e13*other.e23 + self.e23*other.e13 - self.e0123*other.e03,
e13: self.e*other.e13 - self.e01*other.e03 + self.e02*other.e0123 + self.e03*other.e01 + self.e12*other.e23 + self.e13*other.e - self.e23*other.e12 + self.e0123*other.e02,
e23: self.e*other.e23 - self.e01*other.e0123 - self.e02*other.e03 + self.e03*other.e02 - self.e12*other.e13 + self.e13*other.e12 + self.e23*other.e - self.e0123*other.e01,
e0123: self.e*other.e0123 + self.e01*other.e23 - self.e02*other.e13 + self.e03*other.e12 + self.e12*other.e03 - self.e13*other.e02 + self.e23*other.e01 + self.e0123*other.e,
Asaasabi answered 28/2, 2018 at 13:47 Comment(2)
Can you provide a numerical example about this rotor in your answer?Cramoisy
if you compare this to simple matrix multiplication math you will see geometric algebra is more complicated and less suited for nowadays computing systems that is one of the reasons I use homogenuous matrices instead (they are much simpler and for ND even faster and can stack more than just rotations ...) that is also the reason why its usually native in most 3D gfx engines and libs (like OpneGL,DirectX,...) instead of geometric algebraEmmerie
U
6

The most common way to represent goemetric algebra multivectors (including rotors) in a computer is via an array of coefficients, one for each canonical form algebra basis element (canonical basis blade) ie. for a 4D basis space you will have a 2^4 dimensional algebra and have 2^4 dimensional array of coefficients. An alternate but probably faster way to represent them is with a dynamically resizing list with each element containing an index to a blade and the coefficient of the associated blade. In this case the multiplication of two multivectors will only use non zero basis blades and so should be algorithmically cheaper and lighter on memory usage.

In terms of practical usage I found the easiest place to get started with playing around with geometric algebra is probably in python with https://github.com/pygae/clifford . Full disclaimer I use this library daily and contribute to it extensively. This library uses the flat array of coefficients approach. With this python library you can apply 4D rotors via the sandwich product and do reversion (inversion of a rotor) via the tilde operator:

# Create a 4D geometric algebra with euclidean metric
from clifford.g4 import *

# Create a rotor
R = layout.randomRotor()

# Create a vector to rotate
V = layout.randomV()

# Apply the rotor to the vector
V2 = R*V*~R

The specific definition of the geometric product and reverse for multivectors from an N-dimensional geometric algebra can be found in Chapter 4 of Geometric algebra for Physicists by Chris Doran and Anthony Lasenby.

A good C++ GA reference implementation for N-dimensional GAs using the list of elements approach can be found in Leo Dorst's book Geometric Algebra for Physicists or on his website: http://www.geometricalgebra.net/code.html . In general this is a great resource for GA, especially the conformal model and numerical implementations and concerns.

Unsphere answered 7/12, 2018 at 15:11 Comment(0)
A
3

I was able to use Rotors after learning more on the subject thanks to this youtube series about Geometric Algebra : https://www.youtube.com/watch?v=PNlgMPzj-7Q&list=PLpzmRsG7u_gqaTo_vEseQ7U8KFvtiJY4K

It's really well explained and I recommend it to whoever wants use geometric algebra.

If you already know about Quaternions multiplication, Rotor multiplication won't be any different, and the i, j, k units of quaternions are analog to the basis bivectors of Geometric Algebra : e12, e13, e23 (or e01, e02, e12)

So a Rotor in 4D will be (A + B*e12 + C*e13 + D*e14 + E*e23 + F*e24 + G*e34 + H*e1234).

A table showing how to multiply those units can be found on this page : http://www.euclideanspace.com/maths/algebra/clifford/d4/arithmetic/index.htm

To get the gist of it, consider the 2D Rotors.

They're of the form: R = A + B*e12

Now, if we compute product between 2 arbitrary rotors R_1 and R_2, we get:

R_1*R_2 = (
  R_1.a     * R_2.a
+ R_1.a     * R_2.b*e12
+ R_1.b*e12 * R_2.a
+ R_1.b*e12 * R_2.b*e12 )
// but: e12*e12 = e1e2e1e2 = -e1e2e2e1= -e1e1 = -1
// this is confirmed by the computation rules I linked above
=
( (R_1.a * R_1.a - R_2.b * R_2.b)
+ (R_1.a * R_2.b + R_1.b * R_2.a) * e12 )

So in code, you would do something like :

R_3.a = R_1.a * R_2.a - R_1.b * R_2.b
R_3.b = R_1.a * R_2.b + R_1.b * R_2.a

Now it's only a matter of doing the same with those big 4D rotors, applying the multiplication rules for dimension 4 as linked above.

Here is the resulting code (using e0, e1, e2, e3 as basis vectors):

e: self.e*other.e - self.e01*other.e01 - self.e02*other.e02 - self.e03*other.e03 - self.e12*other.e12 - self.e13*other.e13 - self.e23*other.e23 + self.e0123*other.e0123,
e01: self.e*other.e01 + self.e01*other.e - self.e02*other.e12 - self.e03*other.e13 + self.e12*other.e02 + self.e13*other.e03 - self.e23*other.e0123 - self.e0123*other.e23,
e02: self.e*other.e02 + self.e01*other.e12 + self.e02*other.e - self.e03*other.e23 - self.e12*other.e01 + self.e13*other.e0123 + self.e23*other.e03 + self.e0123*other.e13,
e03: self.e*other.e03 + self.e01*other.e13 + self.e02*other.e23 + self.e03*other.e - self.e12*other.e0123 - self.e13*other.e01 - self.e23*other.e02 - self.e0123*other.e12,
e12: self.e*other.e12 - self.e01*other.e02 + self.e02*other.e01 - self.e03*other.e0123 + self.e12*other.e - self.e13*other.e23 + self.e23*other.e13 - self.e0123*other.e03,
e13: self.e*other.e13 - self.e01*other.e03 + self.e02*other.e0123 + self.e03*other.e01 + self.e12*other.e23 + self.e13*other.e - self.e23*other.e12 + self.e0123*other.e02,
e23: self.e*other.e23 - self.e01*other.e0123 - self.e02*other.e03 + self.e03*other.e02 - self.e12*other.e13 + self.e13*other.e12 + self.e23*other.e - self.e0123*other.e01,
e0123: self.e*other.e0123 + self.e01*other.e23 - self.e02*other.e13 + self.e03*other.e12 + self.e12*other.e03 - self.e13*other.e02 + self.e23*other.e01 + self.e0123*other.e,
Asaasabi answered 28/2, 2018 at 13:47 Comment(2)
Can you provide a numerical example about this rotor in your answer?Cramoisy
if you compare this to simple matrix multiplication math you will see geometric algebra is more complicated and less suited for nowadays computing systems that is one of the reasons I use homogenuous matrices instead (they are much simpler and for ND even faster and can stack more than just rotations ...) that is also the reason why its usually native in most 3D gfx engines and libs (like OpneGL,DirectX,...) instead of geometric algebraEmmerie
E
0

Solving rotation around arbitrary vector will make you insane in 4D. Yes there are equations for that out there like The Euler–Rodrigues formula for 3D rotations expansion to 4D but all of them need to solve system of equations and its use is really not intuitive for us in 4D.

I am using rotation parallel to planes instead (similar to rotations around main axises in 3D) In 4D there are 6 of them XY,YZ,ZX,XW,YW,ZW so just create rotation matrices (similar to 3D). I am using 5x5 homogenuous transform matrices for 4D so the rotations looks like this:

xy: 
( c , s ,0.0,0.0,0.0)
(-s , c ,0.0,0.0,0.0)
(0.0,0.0,1.0,0.0,0.0)
(0.0,0.0,0.0,1.0,0.0)
(0.0,0.0,0.0,0.0,1.0)
yz: 
(1.0,0.0,0.0,0.0,0.0)
(0.0, c , s ,0.0,0.0)
(0.0,-s , c ,0.0,0.0)
(0.0,0.0,0.0,1.0,0.0)
(0.0,0.0,0.0,0.0,1.0)
zx:
( c ,0.0,-s ,0.0,0.0)
(0.0,1.0,0.0,0.0,0.0)
( s ,0.0, c ,0.0,0.0)
(0.0,0.0,0.0,1.0,0.0)
(0.0,0.0,0.0,0.0,1.0)
xw:
( c ,0.0,0.0, s ,0.0)
(0.0,1.0,0.0,0.0,0.0)
(0.0,0.0,1.0,0.0,0.0)
(-s ,0.0,0.0, c ,0.0)
(0.0,0.0,0.0,0.0,1.0)
yw:
(1.0,0.0,0.0,0.0,0.0)
(0.0, c ,0.0,-s ,0.0)
(0.0,0.0,1.0,0.0,0.0)
(0.0, s ,0.0, c ,0.0)
(0.0,0.0,0.0,0.0,1.0)
zw:
(1.0,0.0,0.0,0.0,0.0)
(0.0,1.0,0.0,0.0,0.0)
(0.0,0.0, c ,-s ,0.0)
(0.0,0.0, s , c ,0.0)
(0.0,0.0,0.0,0.0,1.0)

Where c=cos(a),s=sin(a) and a is angle of rotation. The rotation axis goes through coordinate system origin (0,0,0,0). For more info take a look at these:

Emmerie answered 15/7, 2017 at 8:17 Comment(2)
Any ideas where minus signs comes in place? a link describing the general formula for the AB(i,j) (where A,B is one of XYZW) would be nice. I wonder if the signs are picked up based on some right-handedness of the rotation. If so, how is that defined in 4D?Headstrong
@Headstrong not sure what you mean by AB(i,j) and minus sign (as the comment suggest both are just 4D vectors)? If you mean matrix * vector then what minus sign you are mentioning? if the -s=-sin(dangle) then yes it determines rotation CW/CCW. You can use sign of dot product of rotated and unrotated vector to determine which way you are rotating. If you are writing about Euler–Rodrigues formula then there is no direct equation instead you need to solve a system of equations for each rotation on the run. That is why using incremental plane rotation is better/easier to implement.Emmerie

© 2022 - 2024 — McMap. All rights reserved.