Vector Class

From TouchDesigner 099 Wiki

The vector class holds a single 3 component vector. A vector describes a direction in space, and it's important to use a vector or Position as appropriate for the data that is being calculated. When being multiplied by a Matrix, this class will implicitly have a 4th component (W component) of 0. A new vector can be created without any arguments, with 3 arguments for the x,y,z values, or with a single argument which is a variable that has 3 entries such as a list of length 3, or a position or vector. Examples of creating a vector:

 v = tdu.Vector() # starts as (0, 0, 0)
 v2 = tdu.Vector(0, 0, -1)
 values = [0, 1, 0]
 v3 = tdu.Vector(values)


Members

x Gets or sets the X component of the vector.
y Gets or sets the Y component of the vector.
z Gets or sets the Z component of the vector.


Methods

scale(x, y, z)

Scales each component of the vector by the specified values.
  • x, y, z - The values to scale each component of the vector by.
 v.scale(1, 2, 1)

normalize()

Makes the length of this vector 1.
 m.normalize()

length()float

Returns the length of this vector.
 l = m.length()

copy()Vector

Returns a new vector that is a copy of the vector.
 newV = v.copy()

dot(vec)float

Returns the dot product of this vector and the passed vector.
  • vec - The other vector to use to calculate the dot product
 d = v.dot(otherV)

cross(vec)Vector

Returns the cross product of this vector and the passed vector. The operation is self cross vec
  • vec - The other vector to use to calculate the cross product
 c = v.cross(otherV)

project(vec1, vec2)

Projects this vector onto the plan defined by vec1 and vec2. Both vec1 and vec2 must be normalized. The result will may not be normalized.
  • vec1, vec2 - The vectors that specify the plane to project onto. Must be normalized.
 v.project(v1, v2)

Special Functions

[i]float

Gets or sets the component of the vector specified by i, where i can be 0, 1, or 2.
   y = v[1]
   v[1] = y * 2.0

Vector * floatVector
float * VectorVector

Scales the vector by the give float scalar and returns a new vector as the result.
 v = v * 2.0

Vector + floatVector
float + VectorVector

Adds the given scalar to all 3 components of the vector and returns a new vector as the result.
 v = v + 5.0

Vector - floatVector
float - VectorVector

Subtracts the given scalar from all 3 components of the vector and returns a new vector as the result.

Vector + PositionPosition
Position + VectorPosition

Adds the vector to the position. ie. it displaces the given position by the vector. Returns a new position as the result.

Position - VectorPosition

Subtracts the vector from the position. Notice that the reverse is not a legal operation: subtracting a position from a vector does not have any meaning. Returns a new position with the results.

Vector + VectorVector

Adds the two vectors to create a new vector.
 v3 = v1 + v2

Vector - VectorVector

Subtracts the two vectors to create a new vector.

Vector += Vector

Adds the 2nd vector to the 1st vector, the 1st vector will contain the result of the operation.

Vector += float

Adds the given scalar to all 3 components of the vector, the vector will contain the result of the operation.
 v += 5.0

Vector -= Vector

Subtracts the 2nd vector from the 1st vector, the 1st vector will contain the result of the operation.

Vector -= float

Subtracts the given scalar from all 3 components of the vector, the vector will contain the result of the operation.

Matrix * VectorVector

Multiplies the vector by the matrix and returns the a new vector as the result.
 v = M * v

Vector / floatVector

Divides each component of the vector by the scalar and returns the a new vector as the result.

Vector *= Matrix

Multiplies the vector by the matrix, the vector will contain the result. The vector is multiplied on the right of the matrix. This is the same as doing v = M * v, although more efficient since it doesn't require assigning a new vector to v.
 v *= M

Vector *= float

Scales all 3 components of the vector by the given scalar. The vector will contain the result.

Vector *= Vector

Does a component-wise scale of all 3 components of the vector by the components of the 2nd vector. The vector will contain the result.

abs(Vector)Vector

Returns a new vector with all 3 components being the absolute value of the given vector's components.

-VectorVector

Returns a new vector with all 3 components being negated.

Miscellaneous

__getstate__()dict

Returns a dictionary with persistent data about the object suitable for pickling and deep copies.

__setstate__(dict)

Reads the dictionary to update persistent details about the object, suitable for unpickling and deep copies.