kornia.geometry.vector#

Tensor wrappers for 3D vectors (*, 3), 2D vectors (*, 2) and scalars, which Hyperplane, the Lie groups of kornia.geometry.liegroup and the camera models of kornia.sensors.camera take and return. Vector3 and Vector2 add coordinate access, dot products and normalisation, and Scalar holds one value per vector. A wrapper does not copy its tensor and is not a torch.Tensor: torch functions and operators accept it, and kornia’s tensor functions take v.data.

class kornia.geometry.vector.Vector3(vector)[source]#

Wrap a (*, 3) tensor of 3D vectors, with coordinates, dot products and normalisation.

v = Vector3(points) wraps a (*, 3) tensor without copying it, and Vector3.from_coords(x, y, z) and Vector3.random(shape) build one; v.x, v.y and v.z are plain (*) tensors, v.dot(w) and v.squared_norm() return a Scalar (*), and v.normalized() the unit vectors, with the norm floored at 1e-12. A Vector3 is not a torch.Tensor: torch functions, tensor methods and operators accept it (torch.linalg.cross(v, w) is a Vector3), but a kornia function that checks for a tensor, such as transform_points(), raises, so pass it v.data. For 2D vectors, use Vector2 instead.

Convention:
  • The tensor is wrapped without a copy; any leading shape and dtype are accepted, and a last axis other than 3 raises BaseError. x, y and z are plain tensors of the leading shape (*), and dot() and squared_norm() return a Scalar of that shape (for dot(), the two operands’ broadcast leading shape).

  • random() draws vectors uniformly in the unit cube from torch’s global generator, so every vector lies in the first octant: it is not a random direction.

  • copy.deepcopy, copy.copy and pickle return a Vector3. An in-place operator such as v += 1 updates the wrapped tensor, so an alias and the tensor the vector was built from see the change; the operator rules are those of TensorWrapper.

  • A result is a Vector3 when it still holds 3D vectors, whether a torch function, a tensor method or an operator produced it, and whichever side a Scalar is on: torch.clone(v), v.clone(), s * v and v * s are Vector3. A result that does not end in 3, or that lost an axis, is a plain tensor: torch.linalg.norm(v, dim=-1) and v.norm(dim=-1), also for three vectors. The full rule is in TensorWrapper.

  • Indexing the leading axes returns a Vector3 (v[0], v[1:], v[mask]). An index into the coordinate axis returns a plain tensor, like the same index into the wrapped tensor: v[..., 0] and v[:, 0] have the leading shape, and an unbatched v[0] is 0-d.

See also

Example

>>> v = Vector3.from_coords(3.0, 0.0, 4.0)
>>> v.normalized().data
tensor([0.6000, 0.0000, 0.8000])
>>> v.dot(Vector3(torch.tensor([1.0, 1.0, 1.0]))).data
tensor(7.)
dot(right)[source]#

Compute dot products with another 3D vector wrapper.

Parameters:

right (Vector3) – Right-hand Vector3 operand with compatible leading dimensions.

Return type:

Scalar

Returns:

Scalar containing \(x_1 x_2 + y_1 y_2 + z_1 z_2\) for each leading element.

classmethod from_coords(x, y, z, device=None, dtype=None)[source]#

Construct a 3D vector wrapper from x, y, and z coordinates.

All coordinate inputs must share the same Python type (all floats or all tensors); Python ints raise, so write 1.0. For tensor inputs, coordinates are stacked along the last dimension to produce (..., 3) output.

Parameters:
  • x (Union[float, Tensor]) – X-coordinate value or tensor.

  • y (Union[float, Tensor]) – Y-coordinate value or tensor.

  • z (Union[float, Tensor]) – Z-coordinate value or tensor.

  • device (Union[str, device, None], optional) – Device used when scalar inputs are converted to tensor; ignored for tensors. Default: None

  • dtype (Optional[dtype], optional) – Dtype used when scalar inputs are converted to tensor; ignored for tensors. Default: None

Return type:

Vector3

Returns:

Vector3 containing the assembled coordinates.

normalized()[source]#

Return a copy with each vector divided by its Euclidean norm.

Return type:

Vector3

Returns:

New Vector3 of the same shape. The norm is floored at 1e-12, so a shorter vector is scaled by 1e12 instead of normalized, as in normalize_quaternion(). A zero vector stays zero in every dtype.

classmethod random(shape=None, device=None, dtype=None)[source]#

Create random 3D vectors with optional leading dimensions.

See the Convention block on Vector3.

Parameters:
  • shape (Optional[Tuple[int, ...]], optional) – Optional leading dimensions before the final coordinate dimension. For example, shape=(B, N) creates (B, N, 3) vectors. Default: None

  • device (Union[str, device, None], optional) – Target device for the generated tensor. Default: None

  • dtype (Optional[dtype], optional) – Target dtype for the generated tensor. Default: None

Return type:

Vector3

Returns:

Vector3 wrapping a random tensor with shape (*shape, 3).

squared_norm()[source]#

Compute squared Euclidean lengths of the wrapped vectors.

Return type:

Scalar

Returns:

Scalar containing \(x^2 + y^2 + z^2\) for each vector.

property x: Tensor#

Return the x-coordinate stored in the last tensor dimension.

property y: Tensor#

Return the y-coordinate stored in the last tensor dimension.

property z: Tensor#

Return the z-coordinate stored in the last tensor dimension.

class kornia.geometry.vector.Vector2(vector)[source]#

Wrap a (*, 2) tensor of 2D vectors, with coordinates, dot products and normalisation.

v = Vector2(points) wraps a (*, 2) tensor without copying it, and Vector2.from_coords(x, y) and Vector2.random(shape) build one; v.x and v.y are plain (*) tensors, v.dot(w) and v.squared_norm() return a Scalar (*), and v.normalized() the unit vectors, with the norm floored at 1e-12. For 3D vectors, use Vector3 instead.

See the Convention block on Vector3, which applies to (x, y) vectors; random() fills the unit square.

See also

dot(right)[source]#

Compute dot products with another 2D vector wrapper.

Parameters:

right (Vector2) – Right-hand Vector2 operand with compatible leading dimensions.

Return type:

Scalar

Returns:

Scalar containing \(x_1 x_2 + y_1 y_2\) for each leading element.

classmethod from_coords(x, y, device=None, dtype=None)[source]#

Construct a 2D vector wrapper from x and y coordinates.

Both coordinates must share the same Python type (both floats or both tensors); Python ints raise, so write 1.0. For tensor inputs, coordinates are stacked along the last dimension to produce (..., 2) output.

Parameters:
  • x (Union[float, Tensor]) – X-coordinate value or tensor.

  • y (Union[float, Tensor]) – Y-coordinate value or tensor.

  • device (Union[str, device, None], optional) – Device used when scalar inputs are converted to tensor; ignored for tensors. Default: None

  • dtype (Optional[dtype], optional) – Dtype used when scalar inputs are converted to tensor; ignored for tensors. Default: None

Return type:

Vector2

Returns:

Vector2 containing the assembled coordinates.

normalized()[source]#

Return a copy with each vector divided by its Euclidean norm.

Return type:

Vector2

Returns:

New Vector2 of the same shape, with the norm floored as in Vector3.normalized().

classmethod random(shape=None, device=None, dtype=None)[source]#

Create random 2D vectors with optional leading dimensions.

See the Convention block on Vector3.

Parameters:
  • shape (Optional[Tuple[int, ...]], optional) – Optional leading dimensions before the final coordinate dimension. For example, shape=(B, N) creates (B, N, 2) vectors. Default: None

  • device (Union[str, device, None], optional) – Target device for the generated tensor. Default: None

  • dtype (Optional[dtype], optional) – Target dtype for the generated tensor. Default: None

Return type:

Vector2

Returns:

Vector2 wrapping a random tensor with shape (*shape, 2).

squared_norm()[source]#

Compute squared Euclidean lengths of the wrapped vectors.

Return type:

Scalar

Returns:

Scalar containing \(x^2 + y^2\) for each vector.

property x: Tensor#

Return the x-coordinate stored in the last tensor dimension.

property y: Tensor#

Return the y-coordinate stored in the last tensor dimension.

class kornia.geometry.vector.Scalar(data)[source]#

Wrap a tensor of scalars of any shape, such as dot products and plane offsets.

s = Scalar(values) wraps a tensor of any shape without copying it; it adds no methods, and s.data is the tensor. Vector3.dot() and Vector3.squared_norm() return one, and Hyperplane takes its offset as one and returns its distances as one. An operation between a Scalar and a Vector3 follows the rule of TensorWrapper, so s * v is a Vector3 like v * s. Broadcasting follows the wrapped tensors and adds no coordinate axis: the (B,) result of Vector3.dot() meets the last axis of (B, 3) vectors, so s * v scales coordinates for B == 3, scales the one vector for B == 1 and raises for any other B. Scale vector by vector with s.data[..., None] * v.

See also

  • Vector3: 3D vectors (*, 3), whose dot products and squared norms come back as a Scalar.

  • Vector2: 2D vectors (*, 2), likewise.