Sign in to follow this  

matrix transformation

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have 2 boxes in 3d space (that is, 2 six-tuples of 3d vertices). I know that i can apply translation, rotation and scale transforms on box A and get box B. the question is, how do i come up with the exact transforms? that is how much to rotate per axis, how much to scale etc... thanks

Share this post


Link to post
Share on other sites
Do you or don't you have the global co-ordinate frames of the 2 boxes? Since you said "2 six-tuples" you perhaps don't have them at hand.

However, if you have, and you want to map box 1 onto box 2, so that
M1 * T = M2
then you can calculate the transition as
T = M1-1 * M2
Then you have to decompose T into the wanted parts of translation, rotation, and scaling, e.g. using the method of polar decomposition.

If you have only the vertices, then obviously there are many possible solutions, since you don't know which of the 6 sides of box 2 corresponds to a particular side of box 1. Also the order of the particular transformations plays a role. Nevertheless, I would compute the translation as offset of the both boxes centers. After that the inverse translation can be performed, yielding in the both boxes overlapping. Computing the scaling can be done by looking at the boxes sizes in their respective local co-ordinate frame perpendicular to the sides. After shrinking them to unit size they can be rotated so that they are axis aligned with each other. To compute pivot and angle of this rotation one pair of matching axes of the both frame's is to be used. Make sure to use the same frame definition for the computations of both scaling and rotation. (Hmm, it seems me now that you can also un-rotate first and un-scale then.)

Perhaps there are some caveats I don't see from this theoretical point of view, but I think in general the ways above should be applyable.

Share this post


Link to post
Share on other sites
i'm sorry, before reading your answer i just want to correct myself - what i have per box is 3 vectors (these are the axis of the box) and for each axis 2 lengthes, max and min that represent the distances of the two parallel faces from the axis. i guess i can compute the 8 vectors which are the vertices of each box but i'd rather avoid it (as it'll require some more thinking :) )

Share this post


Link to post
Share on other sites
The 3 axes of a box give its orientation (I assume they're orthonormal already), the lengthes give the center (of the co-ordinate frame), the ratios of the lengthes along the same axes give the scaling, and the rotation is computable as a matrix from the axes (and can be converted into by the standard methods e.g. into pivot/angle pair, quaternion, ...).

All that above works fine for the standard order 1. scaling, 2. rotation, 3. translation.

If you say "lengthes" then I assume they are both positive, where the min length is going against the axis direction, and the max length is going along with it. Then e.g. for the x axis the minimum and maximum co-ordinate would be
cx - minx =: xmin
cx + maxx =: xmax
where cx is the x co-ordinate of the frame's origin of the box, minx and maxx are the minimum and maximum lengthes, and xmin and xmax are the said minimum and maximum co-ordinates.

Is it so? Or are xmin and xmax actually given?

If we assume the box being scaled, then it should be scaled in both directions homogenously. Hence we cannot guarantee to find a scaling factor if using cx. It would be possible only if minx and maxx are the same values.

Instead,
0.5 * ( xmin + xmax ) =: xmid
is a more suitable origin (x axis co-orindate shown only), since it is centered between the minimum and maximum co-ordinates.

The extent (in x axis direction) is
xmax - xmin =: xe
So, the ratio of the extents of the one box xe,1 to the other xe,2 define the scaling factor (in x direction). Similarly for y and z.

The translation is simple the difference of the mid points of both boxes.

Now with the rotation, the triple of x, y, and z axis vectors define a basis if they are 1st of unit length each and 2nd pairwise orthogonal. Then
[ x y z ] =: B
is the said basis. Determining such a basis B1 for box 1 and B2 for box 2, a mid point related rotation from box 1 to box 2 is given by
B1 * R = B2
so that
R = B1-1 * B2
is the (inverse) rotation searched for. Now use any standard method to convert the rotation matrix R if wanted into Euler angles, pivot/angle pair, ... or whatever you are looking for.

[EDIT: See my over next reply about that the rotation could not be done in one step. I've forgotten to mention that here. Sorry.]


Of course, double check this stuff since it is written down here from scratch. And notice the difference of e.g. minx and xmin.

[Edited by - haegarr on September 21, 2006 5:16:26 AM]

Share this post


Link to post
Share on other sites
yes, if i understand you correctly then you're right about Xmin and Xmax. here's a little drawing to make it clearer

http://hydepark.hevre.co.il/upload0906/2006917_1851555603_axis.JPG

min and max are not guaranteed to be exact. also each box may (and probebly will) have different base.
translation is easy as you said, as i have a center point for each box.
about scaling, i want to scale 3 times, each time on a diffetent axis. however i'm not sure how to sclae around an arbitrary axis - in my book all the transformations are around x, y, or z

Share this post


Link to post
Share on other sites
Quote:
Original post by kikkler
yes, if i understand you correctly then you're right about Xmin and Xmax. here's a little drawing to make it clearer

http://hydepark.hevre.co.il/upload0906/2006917_1851555603_axis.JPG

From your drawing minx and maxx are given, and as I have showen in my reply above xmin and xmax are simply computable.

Quote:
Original post by kikkler
min and max are not guaranteed to be exact. also each box may (and probebly will) have different base.

Well, we can use only what we got, don't we ;)

Quote:
Original post by kikkler
translation is easy as you said, as i have a center point for each box.

Yep (but notice my comment on the need of equally sized min and max values, or else please re-compute the center as I have shown).

Quote:
Original post by kikkler
about scaling, i want to scale 3 times, each time on a diffetent axis. however i'm not sure how to sclae around an arbitrary axis - in my book all the transformations are around x, y, or z

The method I've shown above is for scaling 3 times, each time for another principal axis. For clarity:
minx + maxx == xmax - xmin =: xe
gives the extent of a box in x axis direction. That can be computed for both boxes, yielding in
xe,1 and xe,2
Then the ratio
Sx := xe,2 / xe,1
gives the scaling factor for the x axis direction to come from box 1 to box 2. You can simply proof it. Similarly:
Sy := ye,2 / ye,1
Sz := ze,2 / ze,1
Then you have all 3 scaling factors.

To understand, you have to consider the order of transformations: You have box 1, scale it to the size of box 2, then rotate the result into the orientation of box 2, then translate the result to the position of box 2, and finally you have box 2. The applied particular ransformations are just what you are looking for.

Share this post


Link to post
Share on other sites
ok i read and reread your explanations and i think i understand them. however my results dont look so good so i thought i'll elaborate more on the practical problem i'm facing.
the basic intent was to design a color transfer algorithm - i get two pictures and output the first picture using the colors of the second. the algorithm computes the bounding boxes of each picture colors in RGB 3d space, and then maps the first box to the second

color map

i was able to compute the mean, base and the and the max's and min's values for each picture. then i assembled these matrices

matrices1
matrices2

the bases are orthonormal and so is the base change matrix.
afterwards i took every pixel from picture 1, constructed a color column vector C = (p.r, p.g, p.b)', calculated C' = C * Base_Change * Scale, built C'' = (C'.r, C'.g, C'.b, 1)' and finaly calculated C''' = C'' * Transformation, which i output.
here are some results -

results

1 and 2 shows pictures 1 and 2. 3 is the desired result.
4 shows only transformation, 5 shows transformation + scale and 6 shows transformation + scale + base change.

obviously the base change ruins the picture. i was wondering if you may have any idea about something i'm doing incorrectly

thanks for your help

Share this post


Link to post
Share on other sites
Okay, I see at least one problem with your solution. With your excellent explanations above I've now understood also the details of the problem. So I try to derive a full solution in the following.

What you want to do (looking at the way you've described) is the following (just be warned to check the stuff extensively due to its heavy use of math; notice that I use column vectors as are used by OpenGL; perhaps you want to use row vectors, then revers the order of particular transformations and drop any "transpose" operation):

(1) Scale the original box along its local principal axes, so that it has the extent of the result box. The scaling matrix is denoted as
S
and defined like "scale_matrix" you've written above. (However, to work well all of the mins and maxs are needed to be given measured on the respective local principal axes!)

(2) Rotate the scaled original box around its center so that it has the orientation of the result box. Assuming
R2 := [ ax,2 ay,2 az,2 ]
denotes the orientation of box 2 in global RGB space, assembled from the local principal axes ax,2, ay,2, and az,2 (they must build an ortho-normal basis, of course).

Then the local "normal" orientation of box 1 is transformed to the global orientation of box 2 by multiplying S at the left (remember: column vectors)
R2 * S

(3) Translate the rotated and scaled original box so that it covers the result box. The center of box 2 is given as
l2 := [ 0.5 ( min2.x + max2.x )   0.5 ( min2.y + max2.y )   0.5 ( min2.z + max2.z ) ]T
where the T exponent means "transpose" since I describe all the stuff in column vectors.

Then the transformation so far is expressed as
L2 * R2 * S
where L2 is the equivalent translation matrix of l2.

(4) Don't think that this was all ;) Notice that the transformation given above could be applied to RGB values given in the local co-ordinates of box 1, yielding in global RGB values. But perhaps you want to insert global RGB values. So you want to "localize" the RGB values first. The overall global transformation of box 1, expressed in the terms like above (and, again, column vectors), is
L1 * R1
where
L1 is the translation matrix of l1 := [ 0.5 ( min1.x + max1.x )   0.5 ( min1.y + max1.y )   0.5 ( min1.z + max1.z ) ]T
and
R1 := [ ax,1 ay,1 az,1 ]
as usual.

However, since interested in the global -> local way, in fact its inverse
R1-1 * L1-1
is needed. And since it has to happen first, it is needed to be multiplied at the right of the transformation of (3). This now yields in
L2 * R2 * S * R1-1 * L1-1

Just for completion, if using row vectors the order would be
L1-1 * R1-1 * S * R2 * L2

EDIT: Comparing this with your previous solution, you'll see that S avoids the both rotations to come together (I know I've suggested such by my attempt of explantion in one of the replies above; sorry for that). Furthur, for a similar reason the overall translation could not be done in one step (see your translation by meanDiff).


Err, hopefully I've made no errors. Don't hasitate to ask if something is unclear.

[Edited by - haegarr on September 28, 2006 11:43:18 AM]

Share this post


Link to post
Share on other sites
hello, and thanks a lot for the detailed explanation !
i wasn't able to continue working on this as i had to attend a cpp exam, but now it's over
i'll try to do the math as you described in your last post, but just a short question - if i understand correctly, L1 (and L2) are column vectors. what is L1^-1 then? i may be mistaken, but i believe the inverse is defined only for rectangular matrices and not for vectors

Share this post


Link to post
Share on other sites
Quote:
Original post by kikkler
if i understand correctly, L1 (and L2) are column vectors. what is L1^-1 then? i may be mistaken, but i believe the inverse is defined only for rectangular matrices and not for vectors

Vectors cannot be inverted, that's right. L denotes a translation matrix. E.g. for column vectors as used by OpenGL, this matrix looks like

[ 1 0 0 l_x ]
[ 0 1 0 l_y ]
[ 0 0 1 l_z ]
[ 0 0 0 1 ]

where the l's denote the co-ordinates of the translation. Then L-1 is

[ 1 0 0 -l_x ]
[ 0 1 0 -l_y ]
[ 0 0 1 -l_z ]
[ 0 0 0 1 ]


EDIT: Oh I see, I've used L another time also as a vector. I'd have chosen another name for it. Sorry. Think of it as the equivalent translation vector / translation matrix, whatever matches the needs. I've edited my previous reply accordingly, I hope ...

[Edited by - haegarr on September 28, 2006 12:31:59 PM]

Share this post


Link to post
Share on other sites

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this