Loading...
Implement batch affine transformations on 2D points using PyTorch tensors.
Given a batch of 2D points and a 2×3 affine transformation matrix, apply the transformation to all points efficiently using matrix operations.
An affine transformation combines linear transformation (rotation, scaling, shearing) with translation:
(x′y′)=(acbdtxty)xy1
Or equivalently: x′=ax+by+tx, y′=cx+dy+ty
Key insight: For N points, convert to homogeneous coordinates (add column of 1s) and multiply with the transformation matrix transposed: P′=Ph⋅MT
where Ph is (N, 3) and MT is (3, 2), giving P′ as (N, 2).
points = [[1, 0], [0, 1], [1, 1]] transform = [[1, 0, 10], [0, 1, 20]] # Translation by (10, 20)
[[11.0, 20.0], [10.0, 21.0], [11.0, 21.0]]
Affine matrix (translation only): M=(10011020)
For each point [x, y], compute: x′=1⋅x+0⋅y+10=x+10 y′=0⋅x+1⋅y+20=y+20
Results: