#### Archived

This topic is now archived and is closed to further replies.

# Simle matrix optimizations, are they valid???

## Recommended Posts

When multiplying a vector by a (homogenous) matrix (ie. doing 4 dot products), is it always safe to ommit the last dot product, and just hard-code the result.w to one? (not counting projection matrices). What will make the bottom matrix row become something else than {0,0,0,1} ? If so, I guess I can also apply the same technique to matrix multiplication? -meZ

##### Share on other sites
This is basically what I do. The vector classes I use are of length 3, and the matrix is 3x4. But they are logically homogenous, and will stay such as long as you don't do things such as perspective transforms or similar.

Partitioning matrices can help to see how this works. Let's say we're dealing with just rotation and translation (although scale should work as well):

| R R R t || R R R t || R R R t || 0 0 0 1 |

is the matrix. We can partition this into four submatrices: a 3x3 in the upper-left, a 3x1 in the upper-right, a 1x3 in the lower-left, and a 1x1 in the lower-right, like so:

| R R R | T || R R R | T || R R R | T |+-------+---+| 0 0 0 | 1 |

which in a simpler syntax is:

|R|T|+-+-+|0|1|

Now, when multiplying by another matrix, you get something that looks like this:

|R|T|   |r|t|+-+-+ * +-+-+|0|1|   |0|1|

Use your 2x2 matrix multiply rules to get:

| Rr + T0 | Rt + T1 |+---------+---------+| 0r + 10 | 0t + 11 |

And you can see that because both R and r are 3x3 they can multiply; likewise, Rt is a 3x3 matrix times 3x1 vector which is okay (creating a 3x1 vector); and so forth. The final result is:

| Rr | Rt+T |+----+------+|  0 |   1  |

So you can immediately see that the bottom row always remains [0 0 0 1]. Likewise, if you work out the matrix*vector, the 4th component remains 1. So in my code, the last row of the matrix and last component of the vector are never stored.

Obviously when you get to the end and need to do a perspective transform (or other "strange" transform), you need to handle things a little differently , but it's usually trivial to work it out.

---- --- -- -

Edited by - mossmoss on February 7, 2002 5:52:18 PM

##### Share on other sites
Thanks for the explanation. It''s more or less what I do, but it''s good to get confirmation that it''s actually correct.

• ### Forum Statistics

• Total Topics
628354
• Total Posts
2982243

• 10
• 9
• 11
• 24
• 11