Vector functions

Common Vector Operations

Common Vector manipulation functions.

pyrr.vector.dot(*args, **kwargs)[source]

Calculates the dot product of two vectors.

Parameters:
  • v1 (numpy.array) – an Nd array with the final dimension being size 3. (a vector)
  • v2 (numpy.array) – an Nd array with the final dimension being size 3 (a vector)
Return type:

numpy.array

Returns:

The resulting dot product. If a 1d array was passed,

it will be a scalar. Otherwise the result will be an array of scalars storing the dot product of corresponding rows.

pyrr.vector.interpolate(*args, **kwargs)[source]

Interpolates between 2 arrays of vectors (shape = N,3) by the specified delta (0.0 <= delta <= 1.0).

Parameters:
  • v1 (numpy.array) – an Nd array with the final dimension being size 3. (a vector)
  • v2 (numpy.array) – an Nd array with the final dimension being size 3. (a vector)
  • delta (float) – The interpolation percentage to apply, where 0.0 <= delta <= 1.0. When delta is 0.0, the result will be v1. When delta is 1.0, the result will be v2. Values in between will be an interpolation.
Return type:

numpy.array

Returns:

The result of intperpolation between v1 and v2

pyrr.vector.length(*args, **kwargs)[source]

Returns the length of an Nd list of vectors or a single vector.

Parameters:vec (numpy.array) –

an Nd array with the final dimension being size 3 (a vector).

Single vector:

numpy.array([ x, y, z ])

Nd array:

numpy.array([
    [x1, y1, z1],
    [x2, y2, z2]
]).
Return type:np.array
Returns:The length of vec, if a 1D vector is input, the

result will be a scalar. If a Matrix is input, the result will be a row vector, with each element representing the length along the matrix’s corresponding row.

pyrr.vector.normalise(*args, **kwargs)[source]

normalizes an Nd list of vectors or a single vector to unit length.

The vector is not changed in place.

For zero-length vectors, the result will be np.nan.

Parameters:vec (numpy.array) –

an Nd array with the final dimension being vectors

numpy.array([ x, y, z ])

Or an NxM array:

numpy.array([
    [x1, y1, z1],
    [x2, y2, z2]
]).
Return type:numpy.array
Returns:The normalized vector/s
pyrr.vector.normalize(*args, **kwargs)[source]

normalizes an Nd list of vectors or a single vector to unit length.

The vector is not changed in place.

For zero-length vectors, the result will be np.nan.

Parameters:vec (numpy.array) –

an Nd array with the final dimension being vectors

numpy.array([ x, y, z ])

Or an NxM array:

numpy.array([
    [x1, y1, z1],
    [x2, y2, z2]
]).
Return type:numpy.array
Returns:The normalized vector/s
pyrr.vector.set_length(*args, **kwargs)[source]

Renormalizes an Nd list of vectors or a single vector to ‘length’.

The vector is not changed in place.

Parameters:vec (numpy.array) –

an Nd array with the final dimension being size 3 (a vector).

Single vector::
numpy.array([ x, y, z ])
Nd array::
numpy.array([
[x1, y1, z1], [x2, y2, z2]

]).

Return type:numpy.array
Returns:A renormalized copy of vec, normalized according to its

the last axis. If a vector is input, the result is a vector. If a Matrix is input, the result will be a Matrix, with each row renormalized to a length of len.

pyrr.vector.squared_length(*args, **kwargs)[source]

Calculates the squared length of a vector.

Useful when trying to avoid the performance penalty of a square root operation.

Parameters:vec (numpy.array) – An Nd numpy.array.
Return type:np.array
Returns:The squared length of vec, if a 1D vector is input, the

result will be a scalar. If a Matrix is input, the result will be a row vector, with each element representing the squared length along the matrix’s corresponding row.

Vector3

Provides functions for creating and manipulating 3D vectors.

pyrr.vector3.create(x=0.0, y=0.0, z=0.0, dtype=None)[source]
pyrr.vector3.create_from_matrix44_translation(*args, **kwargs)[source]
pyrr.vector3.create_from_vector4(*args, **kwargs)[source]

Returns a vector3 and the W component as a tuple.

pyrr.vector3.create_unit_length_x(dtype=None)[source]
pyrr.vector3.create_unit_length_y(dtype=None)[source]
pyrr.vector3.create_unit_length_z(dtype=None)[source]
pyrr.vector3.cross(v1, v2)[source]

Calculates the cross-product of two vectors.

Parameters:
  • v1 (numpy.array) – an Nd array with the final dimension being size 3. (a vector)
  • v2 (numpy.array) – an Nd array with the final dimension being size 3. (a vector)
Return type:

np.array

Returns:

The cross product of v1 and v2.

pyrr.vector3.generate_normals(v1, v2, v3, normalize_result=True)[source]

Generates a normal vector for 3 vertices.

The result is a normalized vector.

It is assumed the ordering is counter-clockwise starting at v1, v2 then v3:

v1      v3
  \    /
    v2

The vertices are Nd arrays and may be 1d or Nd. As long as the final axis is of size 3.

For 1d arrays::
>>> v1 = numpy.array( [ 1.0, 0.0, 0.0 ] )
>>> v2 = numpy.array( [ 0.0, 0.0, 0.0 ] )
>>> v3 = numpy.array( [ 0.0, 1.0, 0.0 ] )
>>> vector.generate_normals( v1, v2, v3 )
array([ 0.,  0., -1.])
For Nd arrays::
>>> v1 = numpy.array( [ [ 1.0, 0.0, 0.0 ], [ 1.0, 0.0, 0.0 ] ] )
>>> v2 = numpy.array( [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] )
>>> v3 = numpy.array( [ [ 0.0, 1.0, 0.0 ], [ 0.0, 1.0, 0.0 ] ] )
>>> vector.generate_normals( v1, v2, v3 )
array([[ 0.,  0., -1.],
       [ 0.,  0., -1.]])
Parameters:
  • v1 (numpy.array) – an Nd array with the final dimension being size 3. (a vector)
  • v2 (numpy.array) – an Nd array with the final dimension being size 3. (a vector)
  • v3 (numpy.array) – an Nd array with the final dimension being size 3. (a vector)
  • normalize_result (boolean) – Specifies if the result should be normalized before being returned.
pyrr.vector3.generate_vertex_normals(vertices, index, normalize_result=True)[source]

Generates a normal vector for each vertex.

The result is a normalized vector.

The index array should list the faces by indexing into the vertices array. It is assumed the ordering in index is counter-clockwise.

The vertices and index arrays are Nd arrays and must be 2d, where the final axis is of size 3.

An example::
>>> vertices = numpy.array( [ [ 1.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 1.0, 0.0 ] ] )
>>> index = numpy.array( [ [ 0, 2, 1 ] ] )
>>> vector.generate_vertex_normals( vertices, index )
array([[ 0.,  0., 1.], [ 0.,  0., 1.], [ 0.,  0., 1.]])
Parameters:
  • vertices (numpy.array) – an 2d array with the final dimension being size 3. (a vector)
  • index (numpy.array) – an Nd array with the final dimension being size 3. (a vector)
  • normalize_result (boolean) – Specifies if the result should be normalized before being returned.
class pyrr.vector3.index[source]
x = 0

The index of the X value within the vector

y = 1

The index of the Y value within the vector

z = 2

The index of the Z value within the vector

class pyrr.vector3.unit[source]
x = array([1., 0., 0.])

A vector of unit length in the X-axis. (1.0, 0.0, 0.0)

y = array([0., 1., 0.])

A vector of unit length in the Y-axis. (0.0, 1.0, 0.0)

z = array([0., 0., 1.])

A vector of unit length in the Z-axis. (0.0, 0.0, 1.0)

Vector4

Provides functions for creating and manipulating 4D vectors.

pyrr.vector4.create(x=0.0, y=0.0, z=0.0, w=0.0, dtype=None)[source]
pyrr.vector4.create_from_matrix44_translation(*args, **kwargs)[source]
pyrr.vector4.create_from_vector3(*args, **kwargs)[source]
pyrr.vector4.create_unit_length_w(dtype=None)[source]
pyrr.vector4.create_unit_length_x(dtype=None)[source]
pyrr.vector4.create_unit_length_y(dtype=None)[source]
pyrr.vector4.create_unit_length_z(dtype=None)[source]
class pyrr.vector4.index[source]
w = 3

The index of the W value within the vector

x = 0

The index of the X value within the vector

y = 1

The index of the Y value within the vector

z = 2

The index of the Z value within the vector

class pyrr.vector4.unit[source]
x = array([1., 0., 0., 0.])

A vector of unit length in the X-axis. (1.0, 0.0, 0.0, 0.0)

y = array([0., 1., 0., 0.])

A vector of unit length in the Y-axis. (0.0, 1.0, 0.0, 0.0)

z = array([0., 0., 1., 0.])

A vector of unit length in the Z-axis. (0.0, 0.0, 1.0, 0.0)