Understanding Matrices - Reading Rotation - opengl-es

I am trying to learn more about matrices. If I have a 4x4 matrice such as :
0.005 0.978 -0.20 60.62
-0.98 -0.027 0.15 -18.942
-0.15 0.20 0.96 -287.13
0 0 0 1
Which part of the matrix tells me the rotation that is applied to an object ? I know that column 4 is the position of the object and suspect row 1,2 and 3 are the x,y and z rotation ?
Thanks in advance.

The first three columns are directional vectors in the x, y, z directions, possibly including scaling of the object. If you imagine a cube, the first column's vector points in the direction of the positive-x-face of the cube, the second in the direction of the positive-y-face and the third in the direction of the positive-z-face.
Note that when object-scaling was applied to the matrix (which doesn't appear to be the case in your example), those direction vectors are not normalized.
But this isn't "rotation" in the euler-angle or quaternion-rotation sense. In fact calculating any angles from this matrix is pretty tricky.
Here are some links that explain how to do it, but this comes with a lot of problems and you should avoid it if it's not absolutely necessary:
http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToEuler/index.htm
http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm

Related

Three.js How do you get a Plane from a vector and a constant?

In three.js, the constructor for the Math Plane takes 2 inputs:
normal -- (Vector3) normal vector defining the plane pointing towards the origin
constant -- (Float) the negative distance from the origin to the plane along the normal vector
Can someone provide an illustration or explain how this works? I can understand given a point and normal how to construct a plane, or 3 co-planar points, but can't figure out how a normal vector and constant can be used.
TL:DR?
Mathy-ness and linear algebra
So planes in 3 dimensional space can be defined as a 2-dimensional infinite rectangle that falls on 3 points (what you know)
It can also be defined by a perpendicular (normal) vector and a constant of how far from the origin the plane is.
Three.js take the normal vector (a vector that is perpendicular to the plane you want) and basically applies linear algebra to find the plane, then moves it the constant distance away from the origin.
Math Calculation Explanation:
If we have a vector A and B that are orthogonal (perpendicular) then their dot product is 0. SO if we use this principle we can actually take a known Vector X and find 2 orthogonal Vectors Y and Z that will be co-planar (due to orthogonality properties) by backwards solving X (dot) Y = 0 and X (dot) Z = 0
Now we have 2 co-planar vectors to make our plane that we set the distance of the constant away from the origin
(think how vectors have an origin and an endpoint. If the co-planar vectors share an origin, then we have 3 points: 2 ends, and 1 origin, aka 3 points to make a plane.)
Math Theory Explanation on why this works ahead:
I can't draw very well (without pen and paper to show), but basically, think about a vector in 3D space. Now think about all the vectors that can be perpendicular to it. Basically, that creates an infinite amount of perpendicular vectors rotated in a circle perpendicularly to the original, and if we span them infinitely, we have created a plane.
If you ever have an opportunity to take a linear algebra class, I would highly recommend it. It is extremely interesting, very related to computer graphics, and explains a lot of 3D space math that THREEjs uses

implement 3d sobel operator

I am currently working on inhomogeniety removal from MRI data volume which contains voxels.
I want to apply sobel operator on those volumes to find the gradient. I am familiar with 2d sobel mask and the neighbourhood of 2d images.
sobel mask:
1 2 1
0 0 0
-1 -2 -1
1 0 -1
2 0 -2
1 0 -1
neighbourhood of(x,y):
(x+1,y-1) (x+1,y) (x+1,y+1)
(x,y-1) (x,y) (x,y+1)
(x-1,y-1) (x-1,y) (x-1,y+1)
Now I want to apply it on 3d.
Please suggest me how should I proceed??
Thank you.
Wikipedia has a nice introduction about that : http://en.wikipedia.org/wiki/Sobel_operator
Basically, since the sobel filter is separable, you can apply 1D filters in each of x, y and z directions consecutively. Theses filters are h(x) and h'(x) as given on wikipedia. Doing so will allow you to get the edges in the direction where you applied h'(x).
For example, if you do h(x)*h(y)*h'(z), you'll get the edges in the direction z.
Alternatively (and more expensively), you can compute the whole 3D 3x3x3 kernel and apply the convolution in 3D. The kernel for the z direction is given on wikipedia as well.
Good question! For 3D images you have to use 3 different 3x3x3 sobel operators: 1 for each direction, that is x, y and z. Be aware that in digital image processing the x-coordinate axis points right, y-coordinate downwards, and z-coordinate into the screen!
I visualized all three 3D sobel operators to make it more intuitive. Here the sobel filters in X direction, Y direction and Z direction.
Furthermore, if you want to see the equations behind it (basically what your code has to compute) here you go: SobelFilterEquations

How can I inscribe a rectangle or circle inside an arbitrary quadrilateral

This may be a more math focused question, but wanted to ask here because it is in a CS context. I'm looking to inscribe a rectangle inside another (arbitrary) quad with the inscribed quad having the largest height and width possible. Since I think the algorithm will be similar, I'm looking to see if I can do this with a circle as well.
To be more clear hear is what I mean by the bounding quadrilateral as an example.
Here are 2 examples of the inscribed maximization I'm trying to achieve:
I have done some preliminary searching but have not found anything definitive. It seems that some form of dynamic programming could be the solution. It seems that this should be a linear optimization problem that should be more common than I have found, and perhaps I'm searching for the wrong terms.
Notes: For the inscribed square assume that we know a target w/h ratio we are looking for (e.g. 4:3). For the quad, assume that the sides will not cross and that it will be concave (if that simplifies the calculation).
1) Circle.
For a triangle, this is a standard math question from school program.
For quadrilateral, you can notice that maximum inner circle will touch at least three of its sides. So, take every combination of three sides and solve the problem for each triangle.
A case of parallel sides have to be considered separately (since they don't form a triangle), but it's not terribly difficult.
2) Rectangle.
You can't have "largest height and width", you need to choose another criteria. E.g., on your picture I can increase width by reducing height and vice versa.
4 year old thread, but I happened to stumble accross it when googling my problem.
I have a problem like this in a current CV application. I came up with a simple and somewhat clumsy solution for the finding the largest. Not exactly the same though, cause I maximize the area of the rectangle without a fixed ratio of sides.
I don't know yet wether my solutions finds the optimum or whether it works in all cases. I also think there should be a more efficient way, so I am looking forward to your input.
First, assume a set of 4 points forming our (convex) quadrilateral:
x y
P1 -2 -5
P2 1 7
P3 4 5
P4 3 -2
For this procedure the leftmost point is P1, the following points are numbered clockwise. It looks like this:
We then create the linear functions between the Points. For each function we have to know the slope k and the distance from 0: d.
k is simply the difference in Y of the two points divided by the difference in X.
d can be calculated by solving the linear function to d. So we have
k=dy/dx
d=y1-k*x1
We will also want the inverse functions.
k_inv = 1/k
d_inv = -d/k
We then create the function and inverse function for each side of the quadrilateral
k d k d
p1p2 4 3 p1p2_inv 0.25 -0.75
p2p3 -0.67 7.67 p2p3_inv -1.5 11.5
p3p4 7 -23 p3p4_inv 0.14 3.29
p4p1 0.6 -3.8 p4p1_inv 1.67 6.33
If we had completely horizontal or vertical lines we would end up with a DIV/0 in one of the functions or inverse functions, thus we would need to handle this case separately.
Now we go through all corners that are enclosed by two functions that have a k with a slope with a different sign. In our case that would be P2 and P3.
We start at P2 and iterate through the y values between P2 and the higher one of P1 and P3 with an appropriate step size and use the inverse functions to calculate the distance between the functions in horizontal direction. This would give us one side of the rectangle
a=p2p3_inv(y)-p1p2_inv(y)
At the two x values x = p2p3_inv(y) and x = p1p2_inv(y) we then calculate the difference in y to the two opposite functions and take the distance to our current y position as a candidate for the second side of our rectangle.
b_candidate_1 = y-p4p1(p2p3_inv(y))
b_candidate_2 = y-p4p1(p1p2_inv(y))
b_candidate_3 = y-P3p4(p2p3_inv(y))
b_candidate_4 = y-P3p4(p1p2_inv(y))
The lesser of the four parameters would be the solution for side b.
The area obviously becomes a*b.
I did a quick example in excel to demonstrate:
the minimum b here is 6.9, so the upper right corner of the solution is on p2p3 and the rectangle extends a in horizontal and b in vertical direction to the left and bottom respectively.
The four points of the rectangle are thus
Rect x y
R1 0.65 -1.3
R2 0.65 5.6
R3 3.1 5.6
R4 3.1 -1.3
I will have to put this into C++ code and will run a few tests to see if the solution generalizes or if this was just "luck".
I think it should also be possible to substitute a and b in A=a*b by the functions and put it into one linear formula that has to be maximized under the condition that p1p2 is only defined between P1 and P2 etc...

Is there any algorithm for determining 3d position in such case? (images below)

So first of all I have such image (and ofcourse I have all points coordinates in 2d so I can regenerate lines and check where they cross each other)
(source: narod.ru)
But hey, I have another Image of same lines (I know thay are same) and new coords of my points like on this image
(source: narod.ru)
So... now Having points (coords) on first image, How can I determin plane rotation and Z depth on second image (asuming first one's center was in point (0,0,0) with no rotation)?
What you're trying to find is called a projection matrix. Determining precise inverse projection usually requires that you have firmly established coordinates in both source and destination vectors, which the images above aren't going to give you. You can approximate using pixel positions, however.
This thread will give you a basic walkthrough of the techniques you need to use.
Let me say this up front: this problem is hard. There is a reason Dan Story's linked question has not been answered. Let provide an explanation for people who want to take a stab at it. I hope I'm wrong about how hard it is, though.
I will assume that the 2D screen coordinates and projection/perspective matrix is known to you. You need to know at least this much (if you don't know the projection matrix, essentially you are using a different camera to look at the world). Let's call each pair of 2D screen coordinates (a_i, b_i), and I will assume the projection matrix is of the form
P = [ px 0 0 0 ]
[ 0 py 0 0 ]
[ 0 0 pz pw]
[ 0 0 s 0 ], s = +/-1
Almost any reasonable projection has this form. Working through the rendering pipeline, you find that
a_i = px x_i / (s z_i)
b_i = py y_i / (s z_i)
where (x_i, y_i, z_i) are the original 3D coordinates of the point.
Now, let's assume you know your shape in a set of canonical coordinates (whatever you want), so that the vertices is (x0_i, y0_i, z0_i). We can arrange these as columns of a matrix C. The actual coordinates of the shape are a rigid transformation of these coordinates. Let's similarly organize the actual coordinates as columns of a matrix V. Then these are related by
V = R C + v 1^T (*)
where 1^T is a row vector of ones with the right length, R is an orthogonal rotation matrix of the rigid transformation, and v is the offset vector of the transformation.
Now, you have an expression for each column of V from above: the first column is { s a_1 z_1 / px, s b_1 z_1 / py, z_1 } and so on.
You must solve the set of equations (*) for the set of scalars z_i, and the rigid transformation defined R and v.
Difficulties
The equation is nonlinear in the unknowns, involving quotients of R and z_i
We have assumed up to now that you know which 2D coordinates correspond to which vertices of the original shape (if your shape is a square, this is slightly less of a problem).
We assume there is even a solution at all; if there are errors in the 2D data, then it's hard to say how well equation (*) will be satisfied; the transformation will be nonrigid or nonlinear.
It's called (digital) photogrammetry. Start Googling.
If you are really interested in this kind of problems (which are common in computer vision, tracking objects with cameras etc.), the following book contains a detailed treatment:
Ma, Soatto, Kosecka, Sastry, An Invitation to 3-D Vision, Springer 2004.
Beware: this is an advanced engineering text, and uses many techniques which are mathematical in nature. Skim through the sample chapters featured on the book's web page to get an idea.

skew matrix algorithm

I'm looking for skew algorithm, just like on photoshop, edit->transform->skew
is there any simple matrix which could do that?
what I've seen so far was basic skew matrix (shear) but its lack of control point, doesn't like on photoshop which have at least 4 points on each corner of rectangle and we can move each control point freely.
I need to implement it to transform a plane.
Looking at http://www.w3.org/TR/SVG11/coords.html, which talks about SVG, it says:
A skew transformation along the x-axis is equivalent to the matrix
or [1 0 tan(a) 1 0 0], which has the effect of skewing X coordinates by angle a.
A skew transformation along the y-axis is equivalent to the matrix
or [1 tan(a) 0 1 0 0], which has the effect of skewing Y coordinates by angle a.
Hope that helps! :)

Resources