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, andVector3.from_coords(x, y, z)andVector3.random(shape)build one;v.x,v.yandv.zare plain(*)tensors,v.dot(w)andv.squared_norm()return aScalar(*), andv.normalized()the unit vectors, with the norm floored at1e-12. AVector3is not atorch.Tensor: torch functions, tensor methods and operators accept it (torch.linalg.cross(v, w)is aVector3), but a kornia function that checks for a tensor, such astransform_points(), raises, so pass itv.data. For 2D vectors, useVector2instead.- 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,yandzare plain tensors of the leading shape(*), anddot()andsquared_norm()return aScalarof that shape (fordot(), 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.copyand pickle return aVector3. An in-place operator such asv += 1updates the wrapped tensor, so an alias and the tensor the vector was built from see the change; the operator rules are those ofTensorWrapper.A result is a
Vector3when it still holds 3D vectors, whether a torch function, a tensor method or an operator produced it, and whichever side aScalaris on:torch.clone(v),v.clone(),s * vandv * sareVector3. A result that does not end in 3, or that lost an axis, is a plain tensor:torch.linalg.norm(v, dim=-1)andv.norm(dim=-1), also for three vectors. The full rule is inTensorWrapper.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]andv[:, 0]have the leading shape, and an unbatchedv[0]is 0-d.
See also
Vector2: the 2D vector(*, 2), withxandyonly.Scalar: the wrapper for one value per vector, whichdot()andsquared_norm()return.TensorWrapper: the base class, whose rule decides which results stay aVector3.
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.)
- 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:
- Return type:
- Returns:
Vector3containing the assembled coordinates.
- normalized()[source]#
Return a copy with each vector divided by its Euclidean norm.
- Return type:
- Returns:
New
Vector3of the same shape. The norm is floored at1e-12, so a shorter vector is scaled by1e12instead of normalized, as innormalize_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:Nonedevice (
Union[str,device,None], optional) – Target device for the generated tensor. Default:Nonedtype (
Optional[dtype], optional) – Target dtype for the generated tensor. Default:None
- Return type:
- Returns:
Vector3wrapping a random tensor with shape(*shape, 3).
- 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, andVector2.from_coords(x, y)andVector2.random(shape)build one;v.xandv.yare plain(*)tensors,v.dot(w)andv.squared_norm()return aScalar(*), andv.normalized()the unit vectors, with the norm floored at1e-12. For 3D vectors, useVector3instead.See the Convention block on
Vector3, which applies to(x, y)vectors;random()fills the unit square.See also
Vector3: the 3D vector(*, 3), whose conventions apply here.Scalar: the wrapper for one value per vector, whichdot()andsquared_norm()return.
- 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:
- Return type:
- Returns:
Vector2containing the assembled coordinates.
- normalized()[source]#
Return a copy with each vector divided by its Euclidean norm.
- Return type:
- Returns:
New
Vector2of the same shape, with the norm floored as inVector3.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:Nonedevice (
Union[str,device,None], optional) – Target device for the generated tensor. Default:Nonedtype (
Optional[dtype], optional) – Target dtype for the generated tensor. Default:None
- Return type:
- Returns:
Vector2wrapping a random tensor with shape(*shape, 2).
- 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, ands.datais the tensor.Vector3.dot()andVector3.squared_norm()return one, andHyperplanetakes its offset as one and returns its distances as one. An operation between aScalarand aVector3follows the rule ofTensorWrapper, sos * vis aVector3likev * s. Broadcasting follows the wrapped tensors and adds no coordinate axis: the(B,)result ofVector3.dot()meets the last axis of(B, 3)vectors, sos * vscales coordinates forB == 3, scales the one vector forB == 1and raises for any otherB. Scale vector by vector withs.data[..., None] * v.