LDraw.org Discussion Forums
Summing matrices - Printable Version

+- LDraw.org Discussion Forums (https://forums.ldraw.org)
+-- Forum: Models and Parts (https://forums.ldraw.org/forum-18.html)
+--- Forum: Parts Authoring (https://forums.ldraw.org/forum-19.html)
+--- Thread: Summing matrices (/thread-13359.html)



Summing matrices - Michael Horvath - 2014-07-04

I have model A contained within model B contained in model C. How do I determine the position of model A relative to model C's coordinate system? I know that the XYZ coordinates can simply be added. But what about rotation? Thanks!


Re: Summing matrices - Tim Gould - 2014-07-04

You can use

(x y z)_C = (x y z)_B + R_{BA} (x y z)_A
R_C = R_{CB} R_{BA}

where R_{BA} is the rotation of model A in the coordinates of B and (x y z)_B is the internal translation within B.

Tim


Re: Summing matrices - Michael Horvath - 2014-07-04

Could you post a spreadsheet or something? I don't know what to do with those equations. Thanks.


Re: Summing matrices - Tim Gould - 2014-07-06

Hi Michael,

Here's some pseudo-code. I don't think an excel spreadsheet would tell you anything much.

Michael Horvath Wrote:I know that the XYZ coordinates can simply be added.

Actually no they cannot. You must rotate the displacement of C relative to B.

Tim


Code:
# Multiply a vector by a matrix
function Xn=MatVec(R,X)
   for i from 1 to 3
      Xn(i)=0
      for j from 1 to 3
         Xn(i)=Xn(i)+R(i,j)*X(j)
      end
   end
endfunction

# Multiply two matrices
function Rn=MatMat(R1,R2)
   for i from 1 to 3
      for j from 1 to 3
         Rn(i,j)=0
         for k from 1 to 3
            Rn(i,j)=Rn(i,j)+R1(i,k)*R2(k,j)
         end
      end
   end
endfunction

# Find the rotation and displacement of A relative to C [XCA and RCA]
# using # the rotation and displacement of A relative to B [XBA and RBA]
# and B relative to C [XCB and RCB]
function [XCA,RCA]=Compound(XBA,RBA,XCB,RCB)
   RCA=MatMat(RCB,RBA)
   XCBp=MatVec(RBA,XCB)

   for i from 1 to 3
      XCA(i)=XBA(i) + XCBp(i)
   end
endfunction



Re: Summing matrices - Travis Cobbs - 2014-07-06

3D graphics are pretty much all about 4x4 and 1x4 matrix multiplication. (The last component of a coordinate's 1x4 matrix is 1, and can usually be ignored.) A typical 3D matrix will look like so:

Code:
| A B C X |
| D E F Y |
| G H I Z |
| 0 0 0 1 |

(Note that they are often mirrored across the diagonal axis of the matrix (so the last row would be X Y Z 1, instead of that being the last column. However, the above is the form I'm personally used to.)

A-I encode things like rotation and scale, and match the last 9 numbers in an LDraw type 1 line, while X, Y, and Z represent translations, and match the first 3 numbers in an LDraw type 1 line. The bottom row is typically exactly as I showed (0s and a 1). If you read the line type 1 portion of the LDraw spec, you will see this described.

What isn't explicitly given in the spec is equations to use for models nesting inside each other, but that is done by simply multiplying all the relevant 4x4 matrices together. Repeat that process for every submodel between the source and the destination, and you'll end up with a composite matrix that represents all the nested transformations. Multiply your original coordinate by that matrix, and you'll get the final transformed coordinate.

Note that with matrix multiplication, the order is important, unlike with scalar multiplication. (With matrices, A x B x C does not equal C x B x A.) Matrix multiplication is extremely simple, but very tedious, so I'm not going to write the 16 equations needed to multiply two 4x4 matrices together. (One equation for each element of the final 4x4 matrix.) But you can look it up and easily find those. Plus, Tim already gave code (or perhaps pseudo-code; I'm not sure) to do that.

Being honest, Tim answered the question fully, and my response here is perhaps redundant, but I'm trying to answer it using different phrasing that may (or may not) be easier to understand.


Re: Summing matrices - Philippe Hurbain - 2014-07-06

Just as a reminder, a handy on line matrix calculator:
http://www.mathsisfun.com/algebra/matrix-calculator.html
Useful when you have just a handful calculations to do and don't want to create a program just for that.


Re: Summing matrices - Tim Gould - 2014-07-07

Travis Cobbs Wrote:Being honest, Tim answered the question fully, and my response here is perhaps redundant, but I'm trying to answer it using different phrasing that may (or may not) be easier to understand.

The more answers, the merrier Smile And I think your explanation is better.

I should note that with my background (maths and physics) I tend towards the 3x3 + 3 interpretation. But the 4D notation certainly has its benefits and is popular with 3D graphics people for a reason.

Tim


Re: Summing matrices - Michael Horvath - 2014-07-08

So I just need to multiply the two matrices? Which matrix comes first? B or C?


Re: Summing matrices - Travis Cobbs - 2014-07-08

I'm pretty sure you would multiply B x A, but there is a chance I'm wrong about that and it's A x B. C doesn't have a matrix at all in this case, because you want A to be C's coordinate system. So even if C isn't the top-level model, its matrix is only meaningful if you wanted to get things into its parent model's coordinate system.