@@ -76,6 +76,99 @@ def apply_affine(aff, pts):
76
76
return res .reshape (shape )
77
77
78
78
79
+ def to_matvec (transform ):
80
+ """Split a transform into its matrix and vector components.
81
+
82
+ The tranformation must be represented in homogeneous coordinates and is
83
+ split into its rotation matrix and translation vector components.
84
+
85
+ Parameters
86
+ ----------
87
+ transform : array-like
88
+ NxM transform matrix in homogeneous coordinates representing an affine
89
+ transformation from an (N-1)-dimensional space to an (M-1)-dimensional
90
+ space. An example is a 4x4 transform representing rotations and
91
+ translations in 3 dimensions. A 4x3 matrix can represent a 2-dimensional
92
+ plane embedded in 3 dimensional space.
93
+
94
+ Returns
95
+ -------
96
+ matrix : (N-1, M-1) array
97
+ Matrix component of `transform`
98
+ vector : (M-1,) array
99
+ Vector compoent of `transform`
100
+
101
+ See Also
102
+ --------
103
+ from_matvec
104
+
105
+ Examples
106
+ --------
107
+ >>> aff = np.diag([2, 3, 4, 1])
108
+ >>> aff[:3,3] = [9, 10, 11]
109
+ >>> to_matvec(aff)
110
+ (array([[2, 0, 0],
111
+ [0, 3, 0],
112
+ [0, 0, 4]]), array([ 9, 10, 11]))
113
+ """
114
+ transform = np .asarray (transform )
115
+ ndimin = transform .shape [0 ] - 1
116
+ ndimout = transform .shape [1 ] - 1
117
+ matrix = transform [0 :ndimin , 0 :ndimout ]
118
+ vector = transform [0 :ndimin , ndimout ]
119
+ return matrix , vector
120
+
121
+
122
+ def from_matvec (matrix , vector = None ):
123
+ """ Combine a matrix and vector into an homogeneous affine
124
+
125
+ Combine a rotation / scaling / shearing matrix and translation vector into a
126
+ transform in homogeneous coordinates.
127
+
128
+ Parameters
129
+ ----------
130
+ matrix : array-like
131
+ An NxM array representing the the linear part of the transform.
132
+ A transform from an M-dimensional space to an N-dimensional space.
133
+ vector : None or array-like, optional
134
+ None or an (N,) array representing the translation. None corresponds to
135
+ an (N,) array of zeros.
136
+
137
+ Returns
138
+ -------
139
+ xform : array
140
+ An (N+1, M+1) homogenous transform matrix.
141
+
142
+ See Also
143
+ --------
144
+ to_matvec
145
+
146
+ Examples
147
+ --------
148
+ >>> from_matvec(np.diag([2, 3, 4]), [9, 10, 11])
149
+ array([[ 2, 0, 0, 9],
150
+ [ 0, 3, 0, 10],
151
+ [ 0, 0, 4, 11],
152
+ [ 0, 0, 0, 1]])
153
+
154
+ The `vector` argument is optional:
155
+
156
+ >>> from_matvec(np.diag([2, 3, 4]))
157
+ array([[2, 0, 0, 0],
158
+ [0, 3, 0, 0],
159
+ [0, 0, 4, 0],
160
+ [0, 0, 0, 1]])
161
+ """
162
+ matrix = np .asarray (matrix )
163
+ nin , nout = matrix .shape
164
+ t = np .zeros ((nin + 1 ,nout + 1 ), matrix .dtype )
165
+ t [0 :nin , 0 :nout ] = matrix
166
+ t [nin , nout ] = 1.
167
+ if not vector is None :
168
+ t [0 :nin , nout ] = vector
169
+ return t
170
+
171
+
79
172
def append_diag (aff , steps , starts = ()):
80
173
""" Add diagonal elements `steps` and translations `starts` to affine
81
174
0 commit comments