Applying Matrix from svg parsing.

Discuss SVG code, accessible via the XML Editor.
Sidar
Posts: 11
Joined: Fri Dec 25, 2009 10:37 am

Applying Matrix from svg parsing.

Postby Sidar » Sun Oct 30, 2011 8:22 pm

Hi there,

I'm trying to make inkscape a simple level editor for my team-mates but I'm kinda stuck on the transform attribute.
Basically we only use circles to position items, and I want to use the sodipodi:- rx ry cx cy properties. As I move them around they do not change, only the Matrix does ( I guess thats kinda logical).
But I need to get the position from cx cy and the radius from rx and ry. I have no idea how to apply the matrix to it.
Yes I did read the official site on matrices.

I know that for transformtion it's

Code: Select all

[1][0][tx]
[0][1][ty]
[0][0][1]


scaling is

Code: Select all

[sx][0][0]
[0][sy][0]
[0][0][1]


But these are 2 separate matrices.
And the "matrix" attribute is a combination of the translation, scaling and rotation right?

I'm really confused at how to find the individual scaling and translation in combination with the sodipodi attributes.

User avatar
tomh
Posts: 218
Joined: Sat Feb 14, 2009 10:14 pm

Re: Applying Matrix from svg parsing.

Postby tomh » Mon Oct 31, 2011 10:09 am

Have a look at this answer: http://stackoverflow.com/questions/4361 ... ion-matrix
Not all values of a,b,c,d,tx,ty will yield a valid rotation sequence. I assume the above values are part of a 3x3 homogeneous rotation matrix in 2D

Code: Select all

    | a  b  tx |
A = | c  d  ty |
    | 0  0  1  |

which transforms the coordinates [x,y,1] into:

Code: Select all

[x',y',1] = A * [x,y,1]

Thus set the traslation into [dx,dy]=[tx,ty], the scale is sx=sqrt(a^2+b^2) and sy=sqrt(c^2+d^2). Finaly the rotation angle is t=atan(c/d) or t=atan(-b/a) as also they should be the same.

Otherwise you don't have a valid rotation matrix.

The above transformation is expanded to:

Code: Select all

x' = tx + sx*(COS(t)*x-SIN(t)*y)
y' = ty + sy*(SIN(t)*x+COS(t)*y)



Nothing like going through something like this by hand to get things sorted in your head. ;-)

Oh, In case you missed it the spec is fairly good at describing how the matrixes work: http://www.w3.org/TR/SVG/coords.html

Sidar
Posts: 11
Joined: Fri Dec 25, 2009 10:37 am

Re: Applying Matrix from svg parsing.

Postby Sidar » Mon Oct 31, 2011 8:45 pm

Thanks for taking your time,

It looked like I already got the scaling right.
I just simply multiplied the sx and sy with rx and ry. I didn't use the Pythagorean theorem(?) though...not sure if that will bite me later on.
But the translations don't make sense at all...

Code: Select all

cx:312.96527
cx:502.0582
rx:195.60329
ry:195.60329
After matrix++++++++++
x:409522.25 //way out of screen scope
y:198539.0   //way out of screen scope
rx:50.000004 //correct
ry:50.000004 //correct


Ill look further into it, for now im just multiplying the cx and cy with the tx and ty....But I should add them?
or just assign them?

I'll give an update if 've figured it out.
Thanks though =)

User avatar
tomh
Posts: 218
Joined: Sat Feb 14, 2009 10:14 pm

Re: Applying Matrix from svg parsing.

Postby tomh » Mon Oct 31, 2011 10:51 pm

In your case if you only have scaling and translations (with no rotation or skew); then things simplify a bit.

From the 2 matrixes in the first post, when multiplied gives:

Code: Select all

[ 1 0 tx ]   [ sx  0  0 ]     [ sx 0   tx ]
[ 0 1 ty ] * [ 0  sy  0 ]  =  [ 0  sy  ty ]
[ 0 0 1  ]   [  0  0  1 ]     [ 0  0   1  ]



I didn't use the Pythagorean theorem(?) though

That comes in when you have rotations as well. In your case b and c are both 0, so you don't need to use it.

for now im just multiplying the cx and cy with the tx and ty....But I should add them?

Translations are additive ;=)

To find the new x and y coords, you have to use the last line of code above:

Code: Select all

x' = tx + sx*(COS(t)*x-SIN(t)*y)
y' = ty + sy*(SIN(t)*x+COS(t)*y)


If there are no rotations, then this simplifies to

Code: Select all

x' = tx + sx*x
y' = ty + sy*y


So you have to apply the scale factor to the two co-ordinates and then add the translation element on.

Seriously though, read through the entire section of the spec: http://www.w3.org/TR/SVG/coords.html#Tr ... rixDefined
and matrix maths.

Sidar
Posts: 11
Joined: Fri Dec 25, 2009 10:37 am

Re: Applying Matrix from svg parsing.

Postby Sidar » Tue Nov 01, 2011 7:49 am

like i said, I've already looked there.

I guess I was already on the right track. I just kept confusing myself with the fact that these matrices were smashed together.

I got it now! thanks for your help though =)!


Return to “SVG / XML Code”