BB.Math.Vec3 (bb v0.15.0)

View Source

3D vector backed by an Nx tensor.

All operations are performed using Nx for consistent performance and potential GPU acceleration.

Examples

iex> v = BB.Math.Vec3.new(1, 2, 3)
iex> BB.Math.Vec3.x(v)
1.0

iex> a = BB.Math.Vec3.new(1, 0, 0)
iex> b = BB.Math.Vec3.new(0, 1, 0)
iex> c = BB.Math.Vec3.cross(a, b)
iex> BB.Math.Vec3.z(c)
1.0

Summary

Functions

Adds two vectors.

Computes the cross product of two vectors.

Computes the distance between two points (as vectors).

Computes the dot product of two vectors.

Creates a vector from a list of three numbers.

Creates a vector from an existing {3} tensor.

Linearly interpolates between two vectors.

Computes the magnitude (length) of a vector.

Computes the squared magnitude of a vector.

Negates a vector.

Creates a new vector from x, y, z components.

Normalises a vector to unit length.

Scales a vector by a scalar.

Subtracts vector b from vector a.

Returns the underlying tensor.

Returns the components as a list [x, y, z].

Returns the unit X vector (1, 0, 0).

Returns the unit Y vector (0, 1, 0).

Returns the unit Z vector (0, 0, 1).

Returns the X component.

Returns the Y component.

Returns the Z component.

Returns the zero vector.

Types

t()

@type t() :: %BB.Math.Vec3{tensor: Nx.Tensor.t()}

Functions

add(vec31, vec32)

@spec add(t(), t()) :: t()

Adds two vectors.

Examples

iex> a = BB.Math.Vec3.new(1, 2, 3)
iex> b = BB.Math.Vec3.new(4, 5, 6)
iex> c = BB.Math.Vec3.add(a, b)
iex> BB.Math.Vec3.to_list(c)
[5.0, 7.0, 9.0]

cross(vec31, vec32)

@spec cross(t(), t()) :: t()

Computes the cross product of two vectors.

Examples

iex> a = BB.Math.Vec3.new(1, 0, 0)
iex> b = BB.Math.Vec3.new(0, 1, 0)
iex> c = BB.Math.Vec3.cross(a, b)
iex> BB.Math.Vec3.to_list(c)
[0.0, 0.0, 1.0]

distance(a, b)

@spec distance(t(), t()) :: float()

Computes the distance between two points (as vectors).

Examples

iex> a = BB.Math.Vec3.new(0, 0, 0)
iex> b = BB.Math.Vec3.new(3, 4, 0)
iex> BB.Math.Vec3.distance(a, b)
5.0

dot(vec31, vec32)

@spec dot(t(), t()) :: float()

Computes the dot product of two vectors.

Examples

iex> a = BB.Math.Vec3.new(1, 2, 3)
iex> b = BB.Math.Vec3.new(4, 5, 6)
iex> BB.Math.Vec3.dot(a, b)
32.0

from_list(list)

@spec from_list([number()]) :: t()

Creates a vector from a list of three numbers.

Examples

iex> v = BB.Math.Vec3.from_list([1, 2, 3])
iex> BB.Math.Vec3.to_list(v)
[1.0, 2.0, 3.0]

from_tensor(tensor)

@spec from_tensor(Nx.Tensor.t()) :: t()

Creates a vector from an existing {3} tensor.

lerp(vec31, vec32, t)

@spec lerp(t(), t(), number()) :: t()

Linearly interpolates between two vectors.

Examples

iex> a = BB.Math.Vec3.new(0, 0, 0)
iex> b = BB.Math.Vec3.new(10, 10, 10)
iex> c = BB.Math.Vec3.lerp(a, b, 0.5)
iex> BB.Math.Vec3.to_list(c)
[5.0, 5.0, 5.0]

magnitude(vec3)

@spec magnitude(t()) :: float()

Computes the magnitude (length) of a vector.

Examples

iex> v = BB.Math.Vec3.new(3, 4, 0)
iex> BB.Math.Vec3.magnitude(v)
5.0

magnitude_squared(vec3)

@spec magnitude_squared(t()) :: float()

Computes the squared magnitude of a vector.

More efficient than magnitude/1 when you only need to compare lengths.

Examples

iex> v = BB.Math.Vec3.new(3, 4, 0)
iex> BB.Math.Vec3.magnitude_squared(v)
25.0

negate(vec3)

@spec negate(t()) :: t()

Negates a vector.

Examples

iex> v = BB.Math.Vec3.new(1, -2, 3)
iex> n = BB.Math.Vec3.negate(v)
iex> BB.Math.Vec3.to_list(n)
[-1.0, 2.0, -3.0]

new(x, y, z)

@spec new(number(), number(), number()) :: t()

Creates a new vector from x, y, z components.

Examples

iex> v = BB.Math.Vec3.new(1, 2, 3)
iex> {BB.Math.Vec3.x(v), BB.Math.Vec3.y(v), BB.Math.Vec3.z(v)}
{1.0, 2.0, 3.0}

normalise(vec3)

@spec normalise(t()) :: t()

Normalises a vector to unit length.

Returns zero vector if input has zero magnitude.

Examples

iex> v = BB.Math.Vec3.new(3, 0, 0)
iex> n = BB.Math.Vec3.normalise(v)
iex> BB.Math.Vec3.to_list(n)
[1.0, 0.0, 0.0]

scale(vec3, scalar)

@spec scale(t(), number()) :: t()

Scales a vector by a scalar.

Examples

iex> v = BB.Math.Vec3.new(1, 2, 3)
iex> s = BB.Math.Vec3.scale(v, 2)
iex> BB.Math.Vec3.to_list(s)
[2.0, 4.0, 6.0]

subtract(vec31, vec32)

@spec subtract(t(), t()) :: t()

Subtracts vector b from vector a.

Examples

iex> a = BB.Math.Vec3.new(4, 5, 6)
iex> b = BB.Math.Vec3.new(1, 2, 3)
iex> c = BB.Math.Vec3.subtract(a, b)
iex> BB.Math.Vec3.to_list(c)
[3.0, 3.0, 3.0]

tensor(vec3)

@spec tensor(t()) :: Nx.Tensor.t()

Returns the underlying tensor.

to_list(vec3)

@spec to_list(t()) :: [float()]

Returns the components as a list [x, y, z].

unit_x()

@spec unit_x() :: t()

Returns the unit X vector (1, 0, 0).

unit_y()

@spec unit_y() :: t()

Returns the unit Y vector (0, 1, 0).

unit_z()

@spec unit_z() :: t()

Returns the unit Z vector (0, 0, 1).

x(vec3)

@spec x(t()) :: float()

Returns the X component.

y(vec3)

@spec y(t()) :: float()

Returns the Y component.

z(vec3)

@spec z(t()) :: float()

Returns the Z component.

zero()

@spec zero() :: t()

Returns the zero vector.

Examples

iex> v = BB.Math.Vec3.zero()
iex> {BB.Math.Vec3.x(v), BB.Math.Vec3.y(v), BB.Math.Vec3.z(v)}
{0.0, 0.0, 0.0}