@@ -72,7 +72,9 @@ def vec_homogeneous(vectors, /, *, w=1, out=None, dtype=None) -> np.ndarray:
72
72
return out
73
73
74
74
75
- def vec_transform (vectors , matrix , / , * , w = 1 , out = None , dtype = None ) -> np .ndarray :
75
+ def vec_transform (
76
+ vectors , matrix , / , * , w = 1 , projection = True , out = None , dtype = None
77
+ ) -> np .ndarray :
76
78
"""
77
79
Apply a transformation matrix to a vector.
78
80
@@ -86,6 +88,9 @@ def vec_transform(vectors, matrix, /, *, w=1, out=None, dtype=None) -> np.ndarra
86
88
The value of the scale component of the homogeneous coordinate. This
87
89
affects the result of translation transforms. use 0 (vectors) if the
88
90
translation component should not be applied, 1 (positions) otherwise.
91
+ projection : bool, optional
92
+ If False, the matrix is assumed to be purely affine
93
+ and the homogeneous component is not applied. Default is True.
89
94
out : ndarray, optional
90
95
A location into which the result is stored. If provided, it must have a
91
96
shape that the inputs broadcast to. If not provided or None, a
@@ -102,17 +107,22 @@ def vec_transform(vectors, matrix, /, *, w=1, out=None, dtype=None) -> np.ndarra
102
107
103
108
matrix = np .asarray (matrix )
104
109
105
- vectors = vec_homogeneous (vectors , w = w , dtype = float )
106
- result = matrix @ vectors [..., None ]
107
- result /= result [..., - 1 , :][..., None , :]
108
- result = result [..., :- 1 , 0 ]
109
-
110
- if out is not None :
111
- out [:] = result
110
+ if projection :
111
+ vectors = vec_homogeneous (vectors , w = w , dtype = float )
112
+ vectors @= matrix .T
113
+ vectors [..., :- 1 ] /= vectors [..., - 1 , None ]
114
+ vectors = vectors [..., :- 1 ]
112
115
else :
113
- out = result
116
+ vectors = np .asarray (vectors , dtype = float , copy = True )
117
+ vectors @= matrix [:- 1 , :- 1 ].T
118
+ vectors += matrix [:- 1 , - 1 ]
119
+
120
+ if out is None :
121
+ out = vectors
114
122
if dtype is not None :
115
123
out = out .astype (dtype , copy = False )
124
+ else :
125
+ out [:] = vectors
116
126
117
127
return out
118
128
0 commit comments