Difference between revisions of "Quaternion Class"

From Derivative
Jump to navigation Jump to search
m
Tag: 2021.10000
 
(8 intermediate revisions by 3 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=
 
|Sectionsummary=
 
|Sectionsummary=
 
|items=
 
|items=
{{ClassMember
 
    |class=tdu.Quaternion
 
    |name=w
 
    |type=float
 
    |set=1
 
    |text=Get or set the w component of the quaternion.
 
}}
 
 
{{ClassMember
 
{{ClassMember
 
     |class=tdu.Quaternion
 
     |class=tdu.Quaternion
Line 31: Line 37:
 
     |set=1
 
     |set=1
 
     |text=Get or set the z component of the quaternion.
 
     |text=Get or set the z component of the quaternion.
 +
}}
 +
{{ClassMember
 +
    |class=tdu.Quaternion
 +
    |name=w
 +
    |type=float
 +
    |set=1
 +
    |text=Get or set the w component of the quaternion.
 
}}}}
 
}}}}
 
{{ClassMethodSection|empty=
 
{{ClassMethodSection|empty=
Line 100: Line 113:
 
</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 168: Line 188:
 
</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]

xfloat :

Get or set the x component of the quaternion.

yfloat :

Get or set the y component of the quaternion.

zfloat :

Get or set the z component of the quaternion.

wfloat :

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 *= QuaternionQuaternion:

Applies the rotation of one quaternion to another quaternion.

# apply rotation of q2 to q1
q1 *= q2

TouchDesigner Build: