Transform 3d triangle to new vertices - math

I have 3d triangle with vertices A, B, C and i want it to transform to new vertices A', B', C' with transformation matrix. Sorry for my bad english.
Any ideas?
edit:
I am working in godot and i am using multimesh, which require transformation matrix.

If the 3D points A, B, and C are linearly independent, you can find a transformation matrix T such that when you multiply it by X consisting of the column vectors X = [A, B, C] you arrive at Y = [A', B', C'] as in:
T·X = Y
To find T, simply post multiply both sides by the inverse of X, X^(-1) as in:
T·X·X^(-1) = Y·X^(-1)
T = Y·X^(-1)
You can either code the math to find the inverse of a 3x3 matrix or you may be able to use a library in your particular language to find the inverse of a 3x3 matrix.
I don't know godot, so I recommend looking at the documentation to see if there is something that will help you calculate the inverse of a 3x3 matrix.

Related

Point to the left or right of 3D vector in a specifc plane?

I am not sure if a question like this was asked before but i searched and didn't found what i am looking for.
I know how to determine if a point is to the left or right of a 2D line. but suppose we have a vector in 3D. of course a 3D vector passes through infinite planes, but suppose we chose one plane of them in which we are interested, and we have a specific point on this plane which we want to know if it lies to the left or right or on our vector (with respect to the chosen plane). how to do this ?
You should explicitly define orientation of that plane - for example, define main (forward) normal N - like OZ axis is normal for OXY plane.
If you have A,B,C triangle and claim that it is oriented counterclockwise, you can calculate forward plane normal as N = AB x BC
For points A, B, D in given plane calculate mixed product (vector product of AB and AD, then scalar product of result and N)
mp = (AB x AD) . dot. N
Sign of this value is positive, if vectors AB, AD, N form right-handed triplet and D lies left to AB direction
An intuitive solution is to define a coordinate system for the plane as follows. Let's normalize the 3d vector in your question and call the resulting unit vector v, and let x be a point on your plane, whose unit normal we will denote as n. You can now chose a coordinate system centered at x, that is made by the three 3*1 unit vectors v, n and b=v.crossProduct(n).
The idea is that if you express a point in this coordinate system, then if its b coordinate is negative, you can says that it is, say, on the left. So, if its b coordinate is positive, it will be on the right.
Obviously, if you have a point q expressed in this coordinates system, you can write its expression q_w in world coordinates using
q_w=R*q+x
where the rotation matrix R is the matrix whose columns are the unit axes of the plane coordinate system:
R=[v n b]
So, if you have a point Q in world coordinates, using the inverse of the relation above, you compute transpose(R)*(Q-x), and look at whether the b coordinate is positive or negative.

Mapping transformation from one coordinate space to another

I have two 4x4 transformation matrices that define two coordinate spaces. I am trying to express a translate transformation in one coordinate space in terms of the other. Can anyone point me in the right direction?
Let A be the matrix from space a to global space G.
Let B be the matrix from space b to global space G.
Then to get from a to b we first transform with A to get into G and then with B^-1 (the inverse of B) to get from G to b. Hence
v_b = B^-1 · A ·v_a

3D rotations of a plane

I'm doing something where I have a plane in a coord sys A with a set of points already on it. I also have a normal vector in space N. How can I rotate the points on coord sys A so that the underlying plane will have the same normal direction as N?
Wondering if any one has a good idea on how to do this. Thanks
If you have, or can easily compute, the normal vector to the plane that your points are currently in, I think the easiest way to do this will be to rotate around the axis common to the two planes. Here's how I'd go about it:
Let M be the vector normal to your current plane, and N be the vector normal to the plane you want to rotate into. If M == N you can stop now and leave the original points unchanged.
Calculate the rotation angle as
costheta = dot(M,N)/(norm(M)*norm(N))
Calculate the rotation axis as
axis = unitcross(M, N)
where unitcross is a function that performs the cross product and normalizes it to a unit vector, i.e. unitcross(a, b) = cross(a, b) / norm(cross(a, b)). As user1318499 pointed out in a comment, this step can cause an error if M == N, unless your implementation of unitcross returns (0,0,0) when a == b.
Compute the rotation matrix from the axis and angle as
c = costheta
s = sqrt(1-c*c)
C = 1-c
rmat = matrix([ x*x*C+c x*y*C-z*s x*z*C+y*s ],
[ y*x*C+z*s y*y*C+c y*z*C-x*s ]
[ z*x*C-y*s z*y*C+x*s z*z*C+c ])
where x, y, and z are the components of axis. This formula is described on Wikipedia.
For each point, compute its corresponding point on the new plane as
newpoint = dot(rmat, point)
where the function dot performs matrix multiplication.
This is not unique, of course; as mentioned in peterk's answer, there are an infinite number of possible rotations you could make that would transform the plane normal to M into the plane normal to N. This corresponds to the fact that, after you take the steps described above, you can then rotate the plane around N, and your points will be in different places while staying in the same plane. (In other words, each rotation you can make that satisfies your conditions corresponds to doing the procedure described above followed by another rotation around N.) But if you don't care where in the plane your points wind up, I think this rotation around the common axis is the simplest way to just get the points into the plane you want them in.
If you don't have M, but you do have the coordinates of the points in your starting plane relative to an origin in that plane, you can compute the starting normal vector from two points' positions x1 and x2 as
M = cross(x1, x2)
(you can also use unitcross here but it doesn't make any difference). If you have the points' coordinates relative to an origin that is not in the plane, you can still do it, but you'll need three points' positions:
M = cross(x3-x1, x3-x2)
A single vector (your normal - N) will not be enough. You will need another two vectors for the other two dimensions. (Imagine that your 3D space could still rotate/spin around the normal vector, and you need another 2 vectors to nail it down). Once you have the normal and another one on the plane, the 3rd one should be easy to find (left- or right-handed depending on your system).
Make sure all three are normalized (length of 1) and put them in a matrix; use that matrix to transform any point in your 3D space (use matrix multiplication). This should give you the new coordinates.
I'm thinking make a unit vector [0,0,1] and use the dot-product along two planes to find the angle of difference, and shift all your points by those angles. This is assuming you want the z-axis to align with the normal vector, else just use [1,0,0] or [0,1,0] for x and y respectively.

Transform 3D points to 2D

I have a set of points (x1, x2,..xn) that lie on the plane define by Ax+ By+Cz+d=0.
I would like to find the transformation matrix to translate and rotate to XY plane. So, the new point coordinate will be x1'=(xnew, ynew,0).
A lot of answer give quaternion, dot or cross product matrix. I am not sure which one is the correct way.
Thank you
First of all, unless in your plane equation, d=0, there is no linear transformation you can apply. You need to instead perform an affine transformation.
One way to do this is to determine an angle and vector about which to rotate to make your pointset lie in a plane parallel to the XY plane (ie. the Z component of your transformed pointset to all have the same values). Then you simply drop the Z component.
For this, let V be the normalized plane normal for the plane containing your points. For convenience define from your plane equation above Ax+By+Cz+d=0:
V = (A, B, C)
V' = V / ||V|| = (A', B', C')
Z = (0, 0, 1)
where
A' = A / ||V||
B' = B / ||V||
C' = C / ||V||
||V|| = (A2+B2+C2)1/2
The angle will simply be:
θ = cos-1(Z∙V / ||V||)
= cos-1(Z∙V')
= cos-1(C')
The axis R about which to rotate is just the cross product of the normalized plane normal V' and Z. That is
R = V'×Z
= (B', -A', 0)
You can now use this angle / axis pair to build the quaternion rotation needed to rotate all of the points in your dataset to a plane parallel to the XY plane. Then, a I said earlier, just drop the Z component to perform an orthogonal projection onto the XY plane.
Update: antonakos makes a good point about normalizing the R before using an API taking axis / angle pairs.

How to solve a symbolic non-linear vector equation? (Matlab or other)

I'm trying to find a solution to this symbolic non-linear vector equation:
P = a*(V0*t+P0) + b*(V1*t+P1) + (1-a-b)*(V2*t+P2) for a, b and t
where P, V0, V1, V2, P0, P1, P2 are known 3d vectors.
I attempted to do that in Matlab like this:
P = sym('P', [3,1])
P0 = sym('P0', [3,1])
P1 = sym('P1', [3,1])
P2 = sym('P2', [3,1])
V0 = sym('V0', [3,1])
V1 = sym('V1', [3,1])
V2 = sym('V2', [3,1])
syms a b t
F = a*(V0*t+P0) + b*(V1*t+P1) + (1-a-b)*(V2*t+P2) - P
solve(F,a,b,t)
I get
Warning: Explicit solution could not be found.
I'm starting to run out of ideas how to solve it, this isn't the first math package I tried.
The interesting bit is that this equation has a simple geometrical interpretation. If you imagine that points P0-P2 are vertices of a triangle, V0-V2 are roughly vertex normals and point P lies above the triangle, then the equation is satisfied for a triangle containing point P with it's three vertices on the three rays (V*t+P), sharing the same parameter t value. a, b and (1-a-b) become the barycentric coordinates of the point P.
So if the case is not degenerate, there should be only one well defined solution for t.
As symbolic equation, this one has 3 variables, so there is no way to have a single solution.
Imagine you pick any values for say b and t. Then in almost all cases you can solve for a, so you get many different solutions.
If you want to think geometrically, imagine that V0 and V1 point in the upper half-space regarding the (P0,P1,P2) triangle, but V2 point in the lower. Also V0,V1 are perpendicular to the plane of the triangle and V0 and V1 are unit vectors.
Now if you have a plane pinned at the point P, which intersects the rays P0+t*V0 and P1+t*V1 at the same distance above the triangle, you can move the plane in such a way that it stays pinned at P and intersecting the two rays at the same distance. It's only a matter of having had picked V2 in such a way that the point of intersection with this plane moves at the same velocity, so it will correspond to the same t, thus giving you infinitely many solutions.
Another example would be if all V0-V2 were colinear with the triangle P0,P1,P2. Then you trivially get a solution for any t.
So you need more equations to solve this symbolically.

Resources