@@ -14,7 +14,7 @@ class AffineError(ValueError):
1414 pass
1515
1616
17- def apply_affine (aff , pts ):
17+ def apply_affine (aff , pts , inplace = False ):
1818 """ Apply affine matrix `aff` to points `pts`
1919
2020 Returns result of application of `aff` to the *right* of `pts`. The
@@ -39,6 +39,9 @@ def apply_affine(aff, pts):
3939 pts : (..., N-1) array-like
4040 Points, where the last dimension contains the coordinates of each
4141 point. For 3D, the last dimension will be length 3.
42+ inplace : bool, optional
43+ If False, the affine operation is done on a copy of `pts`.
44+ Otherwise, `pts` gets modified directly.
4245
4346 Returns
4447 -------
@@ -80,8 +83,18 @@ def apply_affine(aff, pts):
8083 # rzs == rotations, zooms, shears
8184 rzs = aff [:- 1 , :- 1 ]
8285 trans = aff [:- 1 , - 1 ]
83- res = np .dot (pts , rzs .T ) + trans [None , :]
84- return res .reshape (shape )
86+
87+ if inplace :
88+ try :
89+ np .dot (pts , rzs .T , out = pts )
90+ except ValueError :
91+ inplace = False
92+ else :
93+ pts += trans [None , :]
94+ if not inplace :
95+ pts = pts @ rzs .T + trans [None , :]
96+
97+ return pts .reshape (shape )
8598
8699
87100def to_matvec (transform ):
0 commit comments