4

I have a homogeneous transformation matrix of size (4x4) and a trajectory of size (nx3). Each row of this trajectory is a vector.

I want to multiply homogeneous transformation matrix by each row of trajectory. Below is the code:

#append zero column at last trajectory = np.hstack((trajectory, np.zeros((trajectory.shape[0], 1)))) #(nx3)->(nx4) trajectory_new = np.zeros((1, 3)) #(1x3) for row in trajectory: vect = row.reshape((-1,1)) #convert (1x4) to (4x1) vect = np.dot(HTM, vect) #(4x4) x (4x1) = (4x1) vect = vect.T #(1x4) vect = np.delete(vect, -1, axis=1) #remove last element from vector trajectory_new = np.vstack((trajectory_new, vect)) #(nx3) trajectory_new = np.delete(trajectory_new, 0, axis=0)#remove first row 

The above code works. However, I am looking for simpler solution, such as following:

trajectory_new = np.apply_along_axis(np.multiply, 0, trajectory, HTM) 

Any help, please.

Answer:

trajectory = np.hstack((trajectory, np.ones((trajectory.shape[0], 1))))#(nx3)->(nx4) trajectory_new = trajectory.dot(HTM.T)[:,:-1] 
8
  • You can do it without stacking, as shown in this post. Commented Jun 15, 2017 at 10:53
  • @Divakar: Are you talking about this? It is giving an error because of shape mismatch. Please see my comment in this post. Commented Jun 15, 2017 at 11:40
  • I am talking about my post. The link I have posted in my earlier comment links there. Commented Jun 15, 2017 at 11:41
  • @Divakar: Apologize however you have removed the last column from HTM, which is incorrect. You may remove the last row of HTM but not the last column. More precisely, HTM is (4x4), in which first (3x3) represents rotation matrix and the last column represents translation in 3D space. The last row of HTM is always [0, 0, 0, 1], which makes HTM (4x4) matrix. Commented Jun 15, 2017 at 11:50
  • But you are not using that last column off HTM for computing the output trajectory_new. That's why you need to add zeros at the start and remove the first row with the original approach. Commented Jun 15, 2017 at 11:52

4 Answers 4

1

Could you include an example of input and output? But it seems that np.dot(HTM, trajectory.T)[:3].T could do the trick?

Rather than appending a column of 0 to trajectory, why don't you drop the last row of HTM?

Sign up to request clarification or add additional context in comments.

1 Comment

np.dot(HTM, trajectory.T)[:3].T is returning following error: shapes (3,4) and (3,5) not aligned: 4 (dim 1) != 3 (dim 0)
1

I think what you want is something like:

trajectory_new = np.einsum('ij,kj->ik', HTM[:,:3], trajectory) 

Not sure about the order, but that should work much faster than for loops

Comments

1

You can simply use matrix-multiplication with np.dot on the input before stacking zeros -

trajectory.dot(HTM[:,:3].T)[:,:3] 

Approaches -

def dot_based(trajectory): return trajectory.dot(HTM[:,:3].T)[:,:3] def original_app(trajectory): # append zero column at last traj_stacked = np.hstack((trajectory, np.zeros((trajectory.shape[0], 1)))) trajectory_new = np.zeros((1, 3)) #(1x3) for row in traj_stacked: vect = row.reshape((-1,1)) #convert (1x4) to (4x1) vect = np.dot(HTM, vect) #(4x4) x (4x1) = (4x1) vect = vect.T #(1x4) vect = np.delete(vect, -1, axis=1) #remove last element from vector trajectory_new = np.vstack((trajectory_new, vect)) #(nx3) trajectory_new = np.delete(trajectory_new, 0, axis=0)#remove first row return trajectory_new 

Sample run -

In [37]: n = 5 ...: trajectory = np.random.rand(n,3) ...: HTM = np.random.rand(4,4) ...: In [38]: np.allclose(dot_based(trajectory), original_app(trajectory)) Out[38]: True 

Comments

0

Your trajectory is nx3, but in order to multiply correctly you need 3xn. Therefore you'll need to transpose twice. Once before multiply and once after.

Like this,

trajectory = (HTM[:3,:3] @ trajectory.T).T 

I assume that trajectory is a vector and you want to ignore the HTM origin? If you want to also add the origin, then you can do this.

trajectory = (HTM[:3,:3] @ trajectory.T).T + HTM[:3,3] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.