BB.Math.Vec3 (bb v0.15.0)
View Source3D 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
@type t() :: %BB.Math.Vec3{tensor: Nx.Tensor.t()}
Functions
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]
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]
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
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
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]
@spec from_tensor(Nx.Tensor.t()) :: t()
Creates a vector from an existing {3} tensor.
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]
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
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
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]
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}
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]
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]
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]
@spec tensor(t()) :: Nx.Tensor.t()
Returns the underlying tensor.
Returns the components as a list [x, y, z].
@spec unit_x() :: t()
Returns the unit X vector (1, 0, 0).
@spec unit_y() :: t()
Returns the unit Y vector (0, 1, 0).
@spec unit_z() :: t()
Returns the unit Z vector (0, 0, 1).
Returns the X component.
Returns the Y component.
Returns the Z component.
@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}