Skip to content

Instantly share code, notes, and snippets.

@maxattack
Created November 18, 2018 22:44
Show Gist options
  • Save maxattack/63ffa444193b98bb72e3f748801344ca to your computer and use it in GitHub Desktop.
Save maxattack/63ffa444193b98bb72e3f748801344ca to your computer and use it in GitHub Desktop.
Dual Quaternion Sample Implementation for Unity3D
using UnityEngine;
/*
Dual-Quaternions are an alternate encoding for rigid transforms (translation + rotation),
with the special property that linear combinations will "arc" the translation in response
to rotation, producing a skinning interpolation that prevents the kind of collapsing you
get from simple linear interpolation of translations in their native coordinates.
Copyright (c) 2018 Max Kaufmann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
public struct DualQuaternion
{
public Quaternion Real;
public Quaternion Dual;
public static DualQuaternion Zero {
get {
DualQuaternion q;
q.Real = new Quaternion(0f, 0f, 0f, 0f);
q.Dual = new Quaternion(0f, 0f, 0f, 0f);
return q;
}
}
public DualQuaternion(Quaternion aReal, Quaternion aDual) {
Real = aReal;
Dual = aDual;
}
public DualQuaternion(Vector3 aPosition, Quaternion aRotation) {
var v = 0.5f * aPosition;
var q1 = new DualQuaternion(new Quaternion(0f, 0f, 0f, 1f), new Quaternion(v.x, v.y, v.z, 0f));
var q2 = new DualQuaternion(aRotation, new Quaternion(0f, 0f, 0f, 0f));
var q = q1 * q2;
Real = q.Real;
Dual = q.Dual;
}
static Quaternion Add(Quaternion A, Quaternion B) {
return new Quaternion(
A.x + B.x,
A.y + B.y,
A.z + B.z,
A.w + B.w
);
}
public static DualQuaternion operator+(DualQuaternion A, DualQuaternion B) {
return new DualQuaternion(
Add(A.Real, B.Real),
Add(A.Dual, B.Dual)
);
}
public static DualQuaternion operator*(DualQuaternion A, DualQuaternion B) {
return new DualQuaternion(
A.Real * B.Real,
Add(A.Dual * B.Real, B.Dual * A.Real)
);
}
static Quaternion Scale(float A, Quaternion B) {
return new Quaternion(A * B.x, A * B.y, A * B.z, A * B.w);
}
public static DualQuaternion operator*(float A, DualQuaternion B) {
return new DualQuaternion(
Scale(A, B.Real),
Scale(A, B.Dual)
);
}
public DualQuaternion Normalized {
get {
var Norm = 1f / Mathf.Sqrt( Quaternion.Dot(Real, Real) );
return new DualQuaternion(Scale(Norm, Real), Scale(Norm, Dual));
}
}
public Vector3 Translation {
get {
var Conjugate = new Quaternion(-Real.x, -Real.y, -Real.z, Real.w);
var TQ = Dual * Conjugate;
return new Vector3(
TQ.x + TQ.x,
TQ.y + TQ.y,
TQ.z + TQ.z
);
}
}
public Vector3 TransformPosition(Vector3 Position) {
return Translation + Real * Position;
}
public Vector3 TransformVector(Vector3 Vector) {
return Real * Vector;
}
}
public static class DualQuaternionExt {
public static DualQuaternion GetDualQuaternion(this Transform xf) {
return new DualQuaternion(xf.position, xf.rotation);
}
public static void SetDualQuaternion(this Transform xf, DualQuaternion dq) {
xf.position = dq.Translation;
xf.rotation = dq.Real;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment