How do I fill Matrix4 with translation, skew and scale values in flutter? - flutter

Suppose, I have these values for a container of height 200 and width 300:
scaleX = 0.9198
scaleY = 0.9198
skewX = -0.3923
skewY = 0.3923
translateX = 150
translateY = 150
Now, how do I fill this values in Matrix4 correctly?
I tried doing this:
Matrix4(
0.9198, 0, 0, 0, //
0, 0.9198, 0, 0, //
0, 0, 1, 0, //
150, 150, 0, 1,
)
which is,
Matrix4(
scaleX, 0, 0, 0, //
0, scaleY, 0, 0, //
0, 0, 1, 0, //
translateX, translateY, 0, 1,
)
But I am not sure where to put skewX and skewY values in this matrix. Please help me with this.

Skew Values
This is a bit of a nuanced topic, as it could be interpreted in a couple of different ways. There are specific cells of a matrix that are associated with specific names, as identified in your question, translate x, translate y, scale x, and scale y. In this context, you most likely mean the values from a matrix that are called skew x and skew y (also sometimes known as shear x and shear y), which refers to indices 4 and 1 (zero-based, column-major order). They're called these names because when put into an identity matrix by themselves, they do that operation (translate, scale, or skew), but it gets more complicated when there are multiple values.
On the other hand, this could also be interpreted as a series of operations (e.g. scale by (0.9198, 0.9198, 1), then skew by (-0.3923, 0.3923), then translate by (150, 150, 0)), and then it's a series of matrix multiplications that would ultimately result in a similar-looking, but numerically different matrix. I'll assume you don't mean this for this question. You can read more about it here though.
You can consult the Flutter Matrix4 documentation, which also provides implementation notes for Matrix4.skewX and Matrix4.skewY. The skews are stored in (zero-based) indices 4, and 1, as the tangent of the skewed angle.
Matrix4(
scaleX, skewY, 0, 0, // skewY could also be tan(ySkewAngle)
skewX, scaleY, 0, 0, // skewX could also be tan(xSkewAngle)
0, 0, 1, 0, //
translateX, translateY, 0, 1,
)
Note to those that aren't familiar with Flutter's data structures that values are stored in column-major order which means that each row in the above code is actually a column, so if you were to represent the matrix as a normal transformation matrix, it's transposed.
More information:
Transformation Matrices: https://en.wikipedia.org/wiki/Transformation_matrix
How matrices are used with CSS Transforms: How do I use the matrix transform and other transform CSS properties?

Related

How to minimise computation time for a matrix diagonalisation plotting script in matlab?

I am fairly new to matlab and was hoping for some assistance regarding the speeding up of computation and plotting of eigenvalues. I have a 'pentadiagonal' 40x40 matrix, with symbolic k entries.
I wish to compute all of the eigenvalues and plot these as a function of k. So I call eig(H), then I set up a loop which replaces the symbolic k entries with a numerical value. I then simply plot this array, which I have called 'list'.
This routine takes a long time to evaluate, and I do need all of the eigenvalues rather than just the n largest. Does anybody have any advice as to how I can speed up this calculation?
Things I have tried:
1) Preallocating my array. This didn't appear to speed it up much. I believe at this stage, the code still holds the eigenvalues as Root[#1^2+cos(1.0*3)...] objects, and the list is relatively small.
2) Rescaling the arguments of the trig functions cos((sqrt(3)/2)*k) -> cos(k). This also doesn't appear to speed it up a huge amount.
I would be very grateful for any further advice.
a=0.5;
b=0.5;
dq=0.01;
d=0.5;
syms k
H= [(-2)*a - b*d*sin(sqrt(3)*k), 2*a*cos((sqrt(3)/2)*k), b*d*sin((sqrt(3)/2)*k), 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; 2*a*cos((sqrt(3)/2)*k), -1, ...]
list = [];
for s=0:dq:3
list=[list,subs(energies,k,s)];
end
q=0:dq:3;
plot(q,transpose(list),'red')

Order of connecting points in paraview under table to structured grids

I'm a beginner in Paraview. I have a question about displaying CSV file in Paraview. If my data file looked like this
x coord, y coord, z coord, scalar
0, 0, 0, 1
1, 0, 0, 2
0, 1, 0, 3
1, 1, 0, 4
0, 0, 1, 5
1, 0, 1, 6
0, 1, 1, 7
1, 1, 1, 8
It will create a cubic grid. But if I switch the order of points like
x coord, y coord, z coord, scalar
0, 0, 0, 1
1, 0, 0, 2
1, 0, 1, 6
0, 1, 0, 3
1, 1, 1, 8
1, 1, 0, 4
0, 0, 1, 5
0, 1, 1, 7
It will give me a really messy connected wireframe. I want to know what's the order of connection? How does Paraview form those grids?
In ParaView (and indeed in the underlying VTK library it uses), structured grid points are ordered such that the index of the x dimension varies fastest, the index of the y dimension varies second-fastest, and the index of the z dimension varies slowest. Hence, your first example gives the expected result while the second example does not.

Robot arm programming, transformation of coordinate system

I have a project in which i need make a matlab based simulation of a robotic arm.
img http://img845.imageshack.us/img845/4512/l5mx.png
The first part sits in the origin, and can rotate around the Z axis in the world coordinate system. This joint is called joint1. The next joint, joint2 is displaced 0.8m in the Z direction of the coordinate system of part 1. It will rotate around the Y axis of the coordinate system of part2. Joint3 is displaced 0.6m in the Z direction of the csystem of part2. It will rotate around the y axis of the csystem of part3. The end of part3 is displaced 0.7m in the Z direction of csystem of part3.
Now, lets try to do some matrices of this. I'm quite sure i'm doing something wrong with these. The coordinates will be in homogenous form, so v = [v,1].
T_Wto1 = [cos(alpha(1)), -sin(alpha(1)), 0 , 0;
sin(alpha(1)), cos(alpha(1)), 0 , 0;
0, 0, 1 , 0.8;
0, 0, 0, 1];
T_1to2 = [cos(alpha(2)), 0, sin(alpha(2)), 0;
0, 1, 0, 0;
-sin(alpha(2)), 0, cos(alpha(2)), 0.6;
0, 0, 0, 1];
T_2to3 = [cos(alpha(3)), 0, sin(alpha(3)), 0;
0, 1, 0, 0;
-sin(alpha(3)), 0, cos(alpha(3)), 0.7;
0, 0, 0, 1];
For alpha(1) = 0, alpha(2) = alpha(3) = pi/2
First of all. If i use p1 = T_Wto1*[0,0,0,1]', i get [0,0,0.8,1]', so far so good. Then, T_1to2*[0,0,0.8,1]' gives [0.8,0,0.6,1]' (it is now displaced 0.8 in the X direction, which is really 0.8 in the Z direction, because of the rotation). Now, say that i want to transform this back to world coordinates. It should say [0.6,0,0.8], but i'm unsure on how to do that. If you just take the inverse of the matrix T_Wto2 (a product of T_Wto1 and T_1to2), you just get the origin [0,0,0,1] back. What are you supposed to do to make it back into world coordinates again?
Also, are the transformation matrices correct?

Program for specific sequence of Integers

I am solving steady state heat equation with the boundary condition varying like this 10,0,0,10,0,0,10,0,0,10,0,0,10.... and so on depending upon number of points i select.
I want to construct a matrix for these boundary conditions but unable to specify the logic for the sequence in terms of ith element for a matrix.
i am using mathematica for this however i need the formula only like for odd we can specify 2n+1 and for even 2n , something like this for the sequence 10,0,0,10,0,0,10,0,0,10,....
In MATLAB, it would be
M = zeros(1000, 1);
M(1:3:1000) = 10;
to make a 1000 long vector with such structure. 1:3:1000 is 1,4,7,....
Since you specifically want a mathematical formula let me suggest a method:
seq = PadRight[{}, 30, {10, 0, 0}];
func = FindSequenceFunction[seq]
10/3 (1 + Cos[2/3 \[Pi] (-1 + #1)] + Cos[4/3 \[Pi] (-1 + #1)]) &
Test it:
Array[func, 10]
{10, 0, 0, 10, 0, 0, 10, 0, 0, 10}
There are surely simpler programs to generate this sequence, such as:
Array[10 Boole[1 == Mod[#, 3]] &, 10]
{10, 0, 0, 10, 0, 0, 10, 0, 0, 10}
A way to do this in Mathematica:
Take[Flatten[ConstantArray[{10, 0, 0}, Ceiling[1000/3] ], 1],1000]
Another way
Table[Boole[Mod[i,3]==1]*10, {i,1,1000}]

How To Correctly Use glMultMatrix - iPhone - OpenGL ES

I wonder if anyone can help me learn how to correctly use glMultMatrix.
I have the following render code in my app, however it appears I cannot just feed a C struct with the matrix to glMultMatrix :
-(void)render
{
matrixStruct matrices[] = {1, 0, -0, -9.37988, 0, -0.651537, 0.758617, 1133.64, 0, 0.758617, 0.651537, 129730, 0, 0, 0, 1};
// clear the matrix
glPushMatrix();
glLoadIdentity();
//scale
glScalef(0.00001, 0.00001, 0.00001);
glMultMatrixf(matrices);
[mesh render];
//restore the matrix
glPopMatrix();
}
I understand from the documentation at http://www.opengl.org/sdk/docs/man/xhtml/glMultMatrix.xml that I need to provide :
void glMultMatrixd( const GLdouble * m);
void glMultMatrixf( const GLfloat * m);
m
Points to 16 consecutive values that are used as the elements of a 4 × 4 column-major matrix.
I am not sure how I should format my matrix and pass it to glMultMatrix. Also do I need to enable glMatrixMode before I call render ?
Thank you
Worked it out - I needed to pass an array of floats :
e.g
float matrices[] ={1,0,0,0,0,1,0,0,0,0,1,0,-578.556,7068.92,48.6953,1};
All working now - thanks.