Please excuse my ignorance about matrices, but I want to know if something
is possible before I delve too much into them.
Basically, I'm rotating vertices around a point in space and an arbitrary
axis, or translating those points.
Currently, I'm performing all the actions in sequence rather than compiling
them into a matrix.
So the question is, is it possible to perform rotations or translations
several times and each time add the current matrix to the previous one so
that I can use one matrix to return the vertices to their original
positions?
Regards,
Ron Francis
www.RonaldFrancis.com
you will have to use homogenous coordinates. You basically add a one to
your (x,y,z) vector (so it becomes (x,y,z,1)), and use a 4x4 matrix for
your transformations.
Now, a rotation matrix is written as a usual rotation matrix + one row
and one column with (0,0,1). And a translation matrix is written as the
identity matrix + one row and one column. The column is the vector of
the translation + a one at the end.
To get the original point back, just inverse the matrix (which can be
done with transposes and minus operations if you only have rotations and
translations).
Google for homogenous coordinates, it will be better explained ;)
--
Nicolas Bonneel
http://www-sop.inria.fr/reves/Nicolas.Bonneel/
Hi,
Nicolas's answer is a valid and a traditional one. Here's an
alternative. You are considering functions of the form
f(x) : R^n -> R^n : f(x) = Ax + b (1)
where
A in R^{n x n}
b in R^n
I.e. affine transformations.
Homogeneous coordinates are traditionally used in e.g. OpenGl because
they also incorporate projective transformations, a super-set of affine
transformations.
However, projective transformations are rarely needed in practice
compared to the use of affine transformations. The extra coordinate in
the vectors and the extra elements in the matrices are then redundant.
Therefore, I have found it much cleaner to use a class for affine
transformations. I.e., an affine transformation is a pair (A, b), with A
in R^{n x n} and b in R^n, and operations defined as follows.
Let f and g be affine transformations in R^n.
Concatenation f o g
-------------------
(f o g)(x)
= f(g(x))
= A(Cx + d) + b
= ACx + (Ad + b)
Thus (concatenation denoted by multiplication)
(A, b) * (C, d) = (AC, Ad + b)
Inverse f^-1
------------
To find the inverse (if it exists), require
f(g(x)) = ACx + (Ad + b) = x = Ix + 0
=>
(1) AC = I => C = A^-1
(2) Ad + b = 0 => d = -A^-1 b
Thus
(A, b)^-1 = (A^-1, -A^-1 b)
Addition f + g
--------------
(f + g)(x)
= f(x) + g(x)
= (Ax + b) + (Cx + d)
= (A + C)x + (b + d)
Thus
(A, b) + (C, d) = (A + C, b + d)
And so on..
Points, vectors, and normals
----------------------------
Keep in mind that points, vectors, and normals each transform
differently:
Points:
f(x) = Ax + b
Vectors:
f(x) = Ax
Normals:
f(x) = (A^-1)^T x
You can make the compiler do the right thing by giving points, vectors,
and normals their own types. I used this approach for years, but
recently gave up because there were some serious problems with
maintenance (and there is also some inevitable abstraction penalty). Now
I just have one vector type to handle all needs and make sure I use the
correct transformation.
Thanks Nicolas.
I was aware of being able to use an inverse matrix after a single
translation and rotation.
But my question is: if I use a matrix to translate and rotate, then use
another matrix for a different manipulation, and do this multiple times, is
it possible to add all these successive matrices into a single matrix by
adding them together, then using the inverse to return to the original
position?
(I know that sometimes a matrix is not able to be inversed.)
Regards,
Ron Francis
www.RonaldFrancis.com
Kaba,
I'll have to do a bit more reading before I will be able to understand much
of what you have written..
(From the little I have looked into already, it shouldn't be all that
difficult.)
But you have given me much to look into and I thank you for the time you
have put into your answer.
Regards,
Ron Francis
www.RonaldFrancis.com
Perhaps an example will help:
Consider R^2. As you probably know, a matrix that rotates a 2d-vector by
an angle 'alpha' counter-clockwise is given by:
A = [cos(alpha), -sin(alpha)]
[sin(alpha), cos(alpha)]
I.e. we can define a function
rotate_alpha : R^2 -> R^2 : rotate_alpha(x) = Ax
On the other hand, a function corresponding to a translation by 'dx'
(delta x) would be given by:
translate_dx : R^2 -> R^2: translate_dx(x) = x + dx
Both of these functions are clearly affine transformations: they are
both representable in the form Cx + d. In the first C = A, d = 0, in the
second C = I (identity), d = dx.
Let us take an example of rotating the space around a given point 'c'
(for center). To do this we compose functions as follows (composition
marked with 'o'):
rotate_alpha_c = translate_c o rotate_alpha o translate_{-c}
which gives an intuitive description of what's going on geometrically.
Reading from right to left: move 'c' to origin, rotate around the
origin, move 'c' back to its original position.
I.e.
rotate_alpha_c(x)
= translate_c(rotate_alpha(translate_{-c}(x)))
= translate_c(rotate_alpha(x - c))
= translate_c(A(x - c)}
= A(x - c) + c
= Ax + (c - Ac)
That's again an affine transformation. In general, given any two affine
transformations, their composition is an affine transformation, as shown
in my previous post. Similarly, the inverse of an affine transformation
(if it exists) is an affine transformation. And so on..
Thanks Kaba, that makes it clear. (Because it looks more like code!) :o)
Regards,
Ron Francis
www.RonaldFrancis.com
each time you apply a new transformation, you multiply the matrices
together. The result should be invertible since you only applied
rotations and transaltions.
Even better - every time you apply a forward transformation, compute and
accumulate the inverse. No need to invert matrices.
--
Kenneth Sloan Kennet...@gmail.com
Computer and Information Sciences +1-205-932-2213
University of Alabama at Birmingham FAX +1-205-934-5473
Birmingham, AL 35294-1170 http://KennethRSloan.com/
That's pretty much what I had in mind.
Thank you both.
--
Regards
Ron Francis
www.RonaldFrancis.com