Difference between revisions of "Quaternion Class"
m Tag: 2021.10000 |
|||
(10 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | {{TDClassSummary|label=Quaternion|summary=Quaternion}} | + | {{TDClassSummary|label=Quaternion|summary=Holds a Quaternion object which can be used to manipulate rotations in various ways. Quaternions can be constructed using a few different ways to describe the initial rotation: |
+ | |||
+ | <syntaxhighlight lang=python> | ||
+ | # From Euler Angles | ||
+ | q = tdu.Quaternion(tdu.Vector(30, 5, -5)) | ||
+ | # From an angle and a rotation axis | ||
+ | q = tdu.Quaternion(30, tdu.Vector(0, 1, 0)) | ||
+ | # From two vectors, rotate from the first vector to the second vector | ||
+ | q = tdu.Quaternion(tdu.Vector(1, 0, 0), tdu.Vector(0, 1, 0)) | ||
+ | # From a set of 4 quaternion values | ||
+ | q = tdu.Quaternion(x, y, z, w) | ||
+ | |||
+ | </syntaxhighlight>}} | ||
+ | See also [[Transform CHOP]] which accepts, manipulates and outputs quaternions as sets of CHOP channels. | ||
<section begin=HELPBODY /> | <section begin=HELPBODY /> | ||
{{ClassMemberSection|empty= | {{ClassMemberSection|empty= | ||
Line 6: | Line 19: | ||
{{ClassMember | {{ClassMember | ||
|class=tdu.Quaternion | |class=tdu.Quaternion | ||
− | |name= | + | |name=x |
|type=float | |type=float | ||
− | |set= | + | |set=1 |
− | |text= | + | |text=Get or set the x component of the quaternion. |
}} | }} | ||
{{ClassMember | {{ClassMember | ||
|class=tdu.Quaternion | |class=tdu.Quaternion | ||
− | |name= | + | |name=y |
|type=float | |type=float | ||
− | |set= | + | |set=1 |
− | |text= | + | |text=Get or set the y component of the quaternion. |
}} | }} | ||
{{ClassMember | {{ClassMember | ||
|class=tdu.Quaternion | |class=tdu.Quaternion | ||
− | |name= | + | |name=z |
|type=float | |type=float | ||
− | |set= | + | |set=1 |
− | |text= | + | |text=Get or set the z component of the quaternion. |
}} | }} | ||
{{ClassMember | {{ClassMember | ||
|class=tdu.Quaternion | |class=tdu.Quaternion | ||
− | |name= | + | |name=w |
|type=float | |type=float | ||
− | |set= | + | |set=1 |
− | |text= | + | |text=Get or set the w component of the quaternion. |
}}}} | }}}} | ||
{{ClassMethodSection|empty= | {{ClassMethodSection|empty= | ||
Line 39: | Line 52: | ||
|class=tdu.Quaternion | |class=tdu.Quaternion | ||
|name=lerp | |name=lerp | ||
− | |call=lerp(q2, | + | |call=lerp(q2, factor) |
|returns=quaternion | |returns=quaternion | ||
− | |text=Returns | + | |text=Returns the linear interpolation of the quaternion with another quaternion and an interpolation factor. |
+ | The quaternion argument can be anything from which a quaternion can be derived ie. (x,y,z,w), Matrix, etc. | ||
+ | The interpolation factor must be between 0 and 1. | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
− | + | q3 = q.lerp(q2, factor) | |
</syntaxhighlight> | </syntaxhighlight> | ||
}} | }} | ||
Line 51: | Line 66: | ||
|call=length() | |call=length() | ||
|returns=float | |returns=float | ||
− | |text=Returns the length of the | + | |text=Returns the length of the quaternion. |
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
l = q.length() | l = q.length() | ||
Line 61: | Line 76: | ||
|call=cross(q2) | |call=cross(q2) | ||
|returns=vector | |returns=vector | ||
− | |text=Returns the cross product of the | + | |text=Returns the cross product of the quaternion and argument. |
+ | The quaternion argument can be anything from which a quaternion can be derived ie. (x,y,z,w), Matrix, etc. | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
l = q.cross(q2) | l = q.cross(q2) | ||
Line 71: | Line 87: | ||
|call=rotate(vec) | |call=rotate(vec) | ||
|returns=vector | |returns=vector | ||
− | |text=Rotates a vector using the current quaternion | + | |text=Rotates a vector using the current quaternion. Returns a new vector. |
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
− | + | v2 = q.rotate(v1) | |
</syntaxhighlight> | </syntaxhighlight> | ||
}} | }} | ||
Line 79: | Line 95: | ||
|class=tdu.Quaternion | |class=tdu.Quaternion | ||
|name=slerp | |name=slerp | ||
− | |call=slerp(q2, | + | |call=slerp(q2, factor) |
|returns=quaternion | |returns=quaternion | ||
− | |text=Returns | + | |text=Returns the spherical interpolation of the quaternion with another quaternion and an interpolation factor. |
+ | The quaternion argument can be anything from which a quaternion can be derived ie. (x,y,z,w), Matrix, etc. | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
− | + | q3 = q.slerp(q2, factor) | |
</syntaxhighlight> | </syntaxhighlight> | ||
}} | }} | ||
Line 91: | Line 108: | ||
|call=eulerAngles(order='xyz') | |call=eulerAngles(order='xyz') | ||
|returns=tuple | |returns=tuple | ||
− | |text=Returns euler angles in degrees as a tuple (i.e. pitch as x, yaw as y, roll as z) from current quaternion. The 'order' argument can be set to any valid rotation order which by default is set to 'xyz'. | + | |text=Returns euler angles in degrees as a tuple (i.e. pitch as x, yaw as y, roll as z) from current quaternion and a rotation order. The 'order' argument can be set to any valid rotation order which by default is set to 'xyz'. |
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
− | + | r = q.eulerAngles(order='xyz') | |
</syntaxhighlight> | </syntaxhighlight> | ||
}} | }} | ||
+ | {{ClassMethod | ||
+ | |class=tdu.Quaternion | ||
+ | |name=fromEuler | ||
+ | |call=fromEuler(order='xyz') | ||
+ | |returns=tuple | ||
+ | |text=Returns and set the current quaternion from euler angles in degrees as a 3 inputs argument (i.e. pitch as x, yaw as y, roll as z). The 'order' argument can be set to any valid rotation order which by default is set to 'xyz'. | ||
+ | <syntaxhighlight lang=python>r = q.fromEuler(order='xyz')</syntaxhighlight>}} | ||
{{ClassMethod | {{ClassMethod | ||
|class=tdu.Quaternion | |class=tdu.Quaternion | ||
Line 101: | Line 125: | ||
|call=axis() | |call=axis() | ||
|returns=vector | |returns=vector | ||
− | |text=Returns the rotation axis vector of the | + | |text=Returns the rotation axis vector of the quaternion. |
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
− | + | v = q.axis() | |
</syntaxhighlight> | </syntaxhighlight> | ||
}} | }} | ||
Line 111: | Line 135: | ||
|call=dot(q2) | |call=dot(q2) | ||
|returns=float | |returns=float | ||
− | |text=Returns the dot product | + | |text=Returns the dot product of the quaternion and the argument. |
+ | The quaternion argument can be anything from which a quaternion can be derived ie. (x,y,z,w), Matrix, etc. | ||
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
l = q.dot(q2) | l = q.dot(q2) | ||
Line 121: | Line 146: | ||
|call=exp() | |call=exp() | ||
|returns=quaternion | |returns=quaternion | ||
− | |text=Returns the exponential | + | |text=Returns the exponential of the quaternion as a new quaternion. |
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
− | + | q2 = q.exp() | |
</syntaxhighlight> | </syntaxhighlight> | ||
}} | }} | ||
Line 131: | Line 156: | ||
|call=copy() | |call=copy() | ||
|returns=quaternion | |returns=quaternion | ||
− | |text= | + | |text=Creates a copy of the quaternion with separate values. |
}} | }} | ||
{{ClassMethod | {{ClassMethod | ||
Line 138: | Line 163: | ||
|call=log() | |call=log() | ||
|returns=quaternion | |returns=quaternion | ||
− | |text=Returns the natural logarithm of the current as a new quaternion. | + | |text=Returns the natural logarithm of the current quaternion as a new quaternion. |
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
l = q.log() | l = q.log() | ||
Line 148: | Line 173: | ||
|call=inverse() | |call=inverse() | ||
|returns=None | |returns=None | ||
− | |text=Invert the | + | |text=Invert the quaternion in place. |
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
q.inverse() | q.inverse() | ||
Line 158: | Line 183: | ||
|call=angle() | |call=angle() | ||
|returns=float | |returns=float | ||
− | |text=Returns the rotation angle (in degrees) of the | + | |text=Returns the rotation angle (in degrees) of the quaternion. |
<syntaxhighlight lang=python> | <syntaxhighlight lang=python> | ||
− | + | a = q.angle() | |
</syntaxhighlight> | </syntaxhighlight> | ||
}}}} | }}}} | ||
+ | {{SubSection|title=Special Functions | ||
+ | |text= | ||
+ | }} | ||
+ | {{ClassMethod | ||
+ | |class=tdu.Quaternion | ||
+ | |name=*= | ||
+ | |call=Quaternion *= Quaternion | ||
+ | |returns=Quaternion | ||
+ | |text=Applies the rotation of one quaternion to another quaternion. | ||
+ | <syntaxhighlight lang=python> | ||
+ | # apply rotation of q2 to q1 | ||
+ | q1 *= q2 | ||
+ | </syntaxhighlight> | ||
+ | }} | ||
<section end=HELPBODY /> | <section end=HELPBODY /> | ||
{{History}} | {{History}} | ||
{{#invoke:Category|list|Python Reference}} | {{#invoke:Category|list|Python Reference}} |
Latest revision as of 12:32, 20 February 2020
Holds a Quaternion object which can be used to manipulate rotations in various ways. Quaternions can be constructed using a few different ways to describe the initial rotation:
# From Euler Angles
q = tdu.Quaternion(tdu.Vector(30, 5, -5))
# From an angle and a rotation axis
q = tdu.Quaternion(30, tdu.Vector(0, 1, 0))
# From two vectors, rotate from the first vector to the second vector
q = tdu.Quaternion(tdu.Vector(1, 0, 0), tdu.Vector(0, 1, 0))
# From a set of 4 quaternion values
q = tdu.Quaternion(x, y, z, w)
See also Transform CHOP which accepts, manipulates and outputs quaternions as sets of CHOP channels.
Members[edit]
x
→ float
:
Get or set the x component of the quaternion.
y
→ float
:
Get or set the y component of the quaternion.
z
→ float
:
Get or set the z component of the quaternion.
w
→ float
:
Get or set the w component of the quaternion.
Methods[edit]
lerp(q2, factor)
→ quaternion
:
Returns the linear interpolation of the quaternion with another quaternion and an interpolation factor.
The quaternion argument can be anything from which a quaternion can be derived ie. (x,y,z,w), Matrix, etc. The interpolation factor must be between 0 and 1.
q3 = q.lerp(q2, factor)
length()
→ float
:
Returns the length of the quaternion.
l = q.length()
cross(q2)
→ vector
:
Returns the cross product of the quaternion and argument.
The quaternion argument can be anything from which a quaternion can be derived ie. (x,y,z,w), Matrix, etc.
l = q.cross(q2)
rotate(vec)
→ vector
:
Rotates a vector using the current quaternion. Returns a new vector.
v2 = q.rotate(v1)
slerp(q2, factor)
→ quaternion
:
Returns the spherical interpolation of the quaternion with another quaternion and an interpolation factor.
The quaternion argument can be anything from which a quaternion can be derived ie. (x,y,z,w), Matrix, etc.
q3 = q.slerp(q2, factor)
eulerAngles(order='xyz')
→ tuple
:
Returns euler angles in degrees as a tuple (i.e. pitch as x, yaw as y, roll as z) from current quaternion and a rotation order. The 'order' argument can be set to any valid rotation order which by default is set to 'xyz'.
r = q.eulerAngles(order='xyz')
fromEuler(order='xyz')
→ tuple
:
Returns and set the current quaternion from euler angles in degrees as a 3 inputs argument (i.e. pitch as x, yaw as y, roll as z). The 'order' argument can be set to any valid rotation order which by default is set to 'xyz'.
r = q.fromEuler(order='xyz')
axis()
→ vector
:
Returns the rotation axis vector of the quaternion.
v = q.axis()
dot(q2)
→ float
:
Returns the dot product of the quaternion and the argument.
The quaternion argument can be anything from which a quaternion can be derived ie. (x,y,z,w), Matrix, etc.
l = q.dot(q2)
exp()
→ quaternion
:
Returns the exponential of the quaternion as a new quaternion.
q2 = q.exp()
copy()
→ quaternion
:
Creates a copy of the quaternion with separate values.
log()
→ quaternion
:
Returns the natural logarithm of the current quaternion as a new quaternion.
l = q.log()
inverse()
→ None
:
Invert the quaternion in place.
q.inverse()
angle()
→ float
:
Returns the rotation angle (in degrees) of the quaternion.
a = q.angle()
Special Functions[edit]
Quaternion *= Quaternion
→ Quaternion
:
Applies the rotation of one quaternion to another quaternion.
# apply rotation of q2 to q1 q1 *= q2
TouchDesigner Build: