discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Multmatrix (and its mysterious 4th row) for idiots

T
tdeagan
Fri, Dec 12, 2014 8:35 PM

I have struggled for a long time with the multmatrix() function.  Especially
with the apparently vestigial fourth row!  What do the various value
mean????

Starting with  a clue from clothbot in another thread
http://forum.openscad.org/Arbitrary-Transformation-tp3293p3300.html  to
go read up on forward kinematics, I began researching (sadly well above my
maths level,) the following is what I've found.  I post simply to save time
for other poor confused souls like myself.  Many readers will cringe at my
innumeracy and I can only express gratitude to any poster who is willing to
point out flaws, misunderstandings or hilarity in my description.

The multmatrix function uses  Denavit and Hartenberg (D-H) parameters
http://en.wikipedia.org/wiki/Denavit%E2%80%93Hartenberg_parameters  .
This is a 4x4 transformation matrix convention initially created for use in
working with  kinematic chains
http://en.wikipedia.org/wiki/Kinematic_chain  and robot manipulators.  It
has since found use in a variety of 3D graphics implementations.

Here is the OpenSCAD example of a multimatrix function to do the equivalent
of translate([10,20,30]):
multmatrix(m = [ [1, 0, 0, 10],
[0, 1, 0, 20],
[0, 0, 1, 30],
[0, 0, 0,  1]
])

The D-H matrix can be thought of as a 3x3 matrix (top right 3 rows and
columns) that define linear transformations such as scaling, shearing,
reflection and rotation combined with a 1x3 array (the first 3 rows of the
rightmost column,) that define translations in the x, y and z axes.

This is a breakdown of what you can do with the independent elements in the
matrix (for the first three rows):
[Scale X],[Scale X sheared along Y],[Scale X sheared along Z], [Translate X]
[Scale Y sheared along X],[Scale Y],[Scale Y sheared along Z], [Translate Y]
[Scale Z sheared along X],[Scale Z sheared along Y],[Scale Z], [Translate Z]

Combinations of changes to various elements result in an astonishing variety
of transformations. Use of trigonometric functions such as cosines and sines
result in  rotational effects
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#multmatrix
.

But what about that fourth row?  Nearly every place I found a D-H (or
related) matrix on the web had the same values: 0,0,0,1.  I occasionally
encountered oblique references to uses such as perspective transformation
(e.g.,the  OpenGL Frustum
http://www.songho.ca/opengl/gl_projectionmatrix.html  command,) but
nothing that I tried in the fourth row of the multmatrix function in
OpenSCAD appeared to do anything.

More research lead me to  DUAL LIGHT BARRIER METHOD FOR SIX DEGREES OF
FREEDOM TOOL CENTER POINT CALIBRATION OF AN INDUSTRIAL ROBOT
http://www.doria.fi/bitstream/handle/10024/99163/Paananen_Thesis_FINAL.pdf?sequence=2
, Veli-Joel Paananen's Master's thesis at the Lappeenranta University of
Technology's Department of Mechanical Engineering.  (light reading.)

Veli-Joel Paananen had this to say on page 20 while discussing the
homogeneous transformation matrix
http://en.wikipedia.org/wiki/Transformation_matrix  :
*Adding a fourth row of nulls and one to the general transformation does not
affect the result.

The added row is not just to create a quadratic matrix: in 3D-graphics or
computer vision
systems, the 1x3 vector below rotation matrix is used to produce perspective
transformations between frames and the scalar below position vector to
define scaling.
/However, these are neglected in basic manipulator geometry and kinematics/*
(italics are mine)

So, for the purposes of multmatrix, the fourth row is pretty much just there
to make a consistent 4x4 matrix.

Those of you with sufficient maths will have likely considered all this
obvious  but I apparently slept through this stuff.

Others like me who are bootstrapping themselves into this topic may do well
to follow the advice of
mabulous, whose excellent page on
http://mabulous.com/understanding-and-interpreting-the-homogeneous-transformation-matrix-in-3d-space
Understanding and interpreting the homogeneous transformation matrix in 3D
space
  suggests that those who want to learn more about  homogeneous
coordinate vectors http://en.wikipedia.org/wiki/Homogeneous_coordinates
and  affine transformations
http://en.wikipedia.org/wiki/Affine_transformation  should learn enough
to:

  1. know what vectors and matrices are
  2. understand matrixmatrix and matrixvector multiplications by performing
    a few of them on paper
  3. understand what a homogeneous coordinate vector is

Cheers!
--Tim

--
View this message in context: http://forum.openscad.org/Multmatrix-and-its-mysterious-4th-row-for-idiots-tp10506.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

I have struggled for a long time with the multmatrix() function. Especially with the apparently vestigial fourth row! What do the various value mean???? Starting with a clue from clothbot in another thread <http://forum.openscad.org/Arbitrary-Transformation-tp3293p3300.html> to go read up on forward kinematics, I began researching (sadly well above my maths level,) the following is what I've found. I post simply to save time for other poor confused souls like myself. Many readers will cringe at my innumeracy and I can only express gratitude to any poster who is willing to point out flaws, misunderstandings or hilarity in my description. The multmatrix function uses Denavit and Hartenberg (D-H) parameters <http://en.wikipedia.org/wiki/Denavit%E2%80%93Hartenberg_parameters> . This is a 4x4 transformation matrix convention initially created for use in working with kinematic chains <http://en.wikipedia.org/wiki/Kinematic_chain> and robot manipulators. It has since found use in a variety of 3D graphics implementations. Here is the OpenSCAD example of a multimatrix function to do the equivalent of translate([10,20,30]): multmatrix(m = [ [1, 0, 0, 10], [0, 1, 0, 20], [0, 0, 1, 30], [0, 0, 0, 1] ]) The D-H matrix can be thought of as a 3x3 matrix (top right 3 rows and columns) that define linear transformations such as scaling, shearing, reflection and rotation combined with a 1x3 array (the first 3 rows of the rightmost column,) that define translations in the x, y and z axes. This is a breakdown of what you can do with the independent elements in the matrix (for the first three rows): [Scale X],[Scale X sheared along Y],[Scale X sheared along Z], [Translate X] [Scale Y sheared along X],[Scale Y],[Scale Y sheared along Z], [Translate Y] [Scale Z sheared along X],[Scale Z sheared along Y],[Scale Z], [Translate Z] Combinations of changes to various elements result in an astonishing variety of transformations. Use of trigonometric functions such as cosines and sines result in rotational effects <https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Transformations#multmatrix> . But what about that fourth row? Nearly every place I found a D-H (or related) matrix on the web had the same values: 0,0,0,1. I occasionally encountered oblique references to uses such as perspective transformation (e.g.,the OpenGL Frustum <http://www.songho.ca/opengl/gl_projectionmatrix.html> command,) but nothing that I tried in the fourth row of the multmatrix function in OpenSCAD appeared to do anything. More research lead me to DUAL LIGHT BARRIER METHOD FOR SIX DEGREES OF FREEDOM TOOL CENTER POINT CALIBRATION OF AN INDUSTRIAL ROBOT <http://www.doria.fi/bitstream/handle/10024/99163/Paananen_Thesis_FINAL.pdf?sequence=2> , Veli-Joel Paananen's Master's thesis at the Lappeenranta University of Technology's Department of Mechanical Engineering. (light reading.) Veli-Joel Paananen had this to say on page 20 while discussing the homogeneous transformation matrix <http://en.wikipedia.org/wiki/Transformation_matrix> : *Adding a fourth row of nulls and one to the general transformation does not affect the result. The added row is not just to create a quadratic matrix: in 3D-graphics or computer vision systems, the 1x3 vector below rotation matrix is used to produce perspective transformations between frames and the scalar below position vector to define scaling. /However, these are neglected in basic manipulator geometry and kinematics/* (italics are mine) So, for the purposes of multmatrix, the fourth row is pretty much just there to make a consistent 4x4 matrix. Those of you with sufficient maths will have likely considered all this obvious but I apparently slept through this stuff. Others like me who are bootstrapping themselves into this topic may do well to follow the advice of mabulous, whose excellent page on <http://mabulous.com/understanding-and-interpreting-the-homogeneous-transformation-matrix-in-3d-space> *Understanding and interpreting the homogeneous transformation matrix in 3D space* suggests that those who want to learn more about homogeneous coordinate vectors <http://en.wikipedia.org/wiki/Homogeneous_coordinates> and affine transformations <http://en.wikipedia.org/wiki/Affine_transformation> should learn enough to: 1. know what vectors and matrices are 2. understand matrix*matrix and matrix*vector multiplications by performing a few of them on paper 3. understand what a homogeneous coordinate vector is Cheers! --Tim -- View this message in context: http://forum.openscad.org/Multmatrix-and-its-mysterious-4th-row-for-idiots-tp10506.html Sent from the OpenSCAD mailing list archive at Nabble.com.
TA
Taahir Ahmed
Fri, Dec 12, 2014 11:32 PM

The multmatrix function uses  Denavit and Hartenberg (D-H) parameters
...
The D-H matrix can be thought of as a 3x3 matrix

Denavit-Hartenberg parameters are not really related to any of this.

There is no such thing as a Denavit-Hartenberg matrix --- it is general called
an affine transform matrix, or perhaps a homogeneous transformation matrix.

Denavit-Hartenberg parameters are used in kinematics to compactly describe the
orientation of one link in a kinematic chain.  There is a simple mapping from
D-H parameters to an affine transformation matrix, but not every affine
transformation matrix can be represented by a set of D-H parameters.

This is because D-H parameters are used to encode transformations that can
actually be physically built.  (We can build a joint that rotates, but not one
that reflects or shears --- but reflection and shear are easily represented
with an affine transformation matrix).


But what about that fourth row?

The fourth row is a computational necessity, nothing more.  Matrix
transformations can only encode origin-preserving transformations
(transformations that map (0,0,0) to (0,0,0)).

Translation is not origin preserving (for example, translation by (1,0,0) maps
(0,0,0) to (1,0,0)).  However, we really want a compact notation where we can
write

y = M * x

and include translation.

The (somewhat inelegant) solution is to bump the problem up from 3 dimensions
to 4 dimensions.

If we had a point (x,y,z) in three dimensions, we replace it with the 4-
dimensional point (x,y,z,1).

We then write our transformation like this:

y = [      |  ]   [x]
    [  L   | t] * [y]
    [      |  ]   [z]
    [0 0 0 | 1]   [1]

Where L is 3-dimensional linear part (a 3x3 matrix that encodes rotation,
shear, scaling, reflection, etc.), and t is the 3-dimensional translation
part.

This is origin-preserving ((0,0,0,0) maps to itself), but it encodes the 3-
dimensional calculation:

y = Lx + t

which is what we wanted.


nothing that I tried in the fourth row of the multmatrix function

Most systems which are not interested in perspective calculations won't bother
to store the fourth row, since they "know" it will be [ 0 0 0 1 ].

I can't speak for OpenSCAD specifically, but many systems that have a
multmatrix()-like primitive will actually directly extract L and t, and do the
3-dimensional calculation above, since it's less work than a full 4x4
multiplication.

Taahir Ahmed

> The multmatrix function uses Denavit and Hartenberg (D-H) parameters > ... > The D-H matrix can be thought of as a 3x3 matrix Denavit-Hartenberg parameters are not really related to any of this. There is no such thing as a Denavit-Hartenberg matrix --- it is general called an affine transform matrix, or perhaps a homogeneous transformation matrix. Denavit-Hartenberg parameters are used in kinematics to compactly describe the orientation of one link in a kinematic chain. There is a simple mapping from D-H parameters to an affine transformation matrix, but not every affine transformation matrix can be represented by a set of D-H parameters. This is because D-H parameters are used to encode transformations that can actually be physically built. (We can build a joint that rotates, but not one that reflects or shears --- but reflection and shear are easily represented with an affine transformation matrix). ----------------------------------------------------------------------------- > But what about that fourth row? The fourth row is a computational necessity, nothing more. Matrix transformations can only encode origin-preserving transformations (transformations that map (0,0,0) to (0,0,0)). Translation is not origin preserving (for example, translation by (1,0,0) maps (0,0,0) to (1,0,0)). However, we really want a compact notation where we can write y = M * x and include translation. The (somewhat inelegant) solution is to bump the problem up from 3 dimensions to 4 dimensions. If we had a point (x,y,z) in three dimensions, we replace it with the 4- dimensional point (x,y,z,1). We then write our transformation like this: y = [ | ] [x] [ L | t] * [y] [ | ] [z] [0 0 0 | 1] [1] Where L is 3-dimensional linear part (a 3x3 matrix that encodes rotation, shear, scaling, reflection, etc.), and t is the 3-dimensional translation part. This is origin-preserving ((0,0,0,0) maps to itself), but it encodes the 3- dimensional calculation: y = Lx + t which is what we wanted. ---------------------------------------------------------------------------- > nothing that I tried in the fourth row of the multmatrix function Most systems which are not interested in perspective calculations won't bother to store the fourth row, since they "know" it will be [ 0 0 0 1 ]. I can't speak for OpenSCAD specifically, but many systems that have a multmatrix()-like primitive will actually directly extract L and t, and do the 3-dimensional calculation above, since it's less work than a full 4x4 multiplication. Taahir Ahmed
T
tdeagan
Sat, Dec 13, 2014 12:32 AM

Fantastic, thank you very much!!  I'm trying to collect various newbie FAQs
like this and see if good answers can be created.  This is clearly beyond
"cube(10);", but it's something that was bugging me!

--t

--
View this message in context: http://forum.openscad.org/Multmatrix-and-its-mysterious-4th-row-for-idiots-tp10506p10508.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

Fantastic, thank you very much!! I'm trying to collect various newbie FAQs like this and see if good answers can be created. This is clearly beyond "cube(10);", but it's something that was bugging me! --t -- View this message in context: http://forum.openscad.org/Multmatrix-and-its-mysterious-4th-row-for-idiots-tp10506p10508.html Sent from the OpenSCAD mailing list archive at Nabble.com.