I have some points (3D) all on the same (known) plane. Now I want to scale these points within the plane as opposed to the whole 3D space.
Is there some quick solution for this e.g. a modified scaling matrix?
Can someone help me?
Thanks.
EDIT: I'm more looking for an idea/pseudocode how to do this. If you want use MatLab or some convenient language
Your plane can be known by three non-collinear points P0, P1, P2, or by its implicit equation,
A.x + B.y + C.z + D = 0
In the first case, consider the vector P0P1 and normalize it (U = P0P1/|P0P1|). Then compute a second vector orthogonal with the first, V = P0P2 - (P0P2.U).U and normalize it.
In the second case you can take the three intersection points with the axes, (-D/A, 0, 0), (0, -D/B, 0), (0, 0, -D/C) and you are back in the first case (but mind degenerate cases).
Use the two vectors to compute the desired 2D coordinates of any point P = (X, Y, Z) by the dot products
(x, y) = (P.U, P.V)
(This transform is a rotation that makes P0P1 parallel to the x axis and brings P0P1P2 in the plane xy.)
Related
I have been trying to figure out whether the following problem has a solution. Almost having given up, I would like to ask whether someone can confirm that there is no solution, or maybe give me a hint.
We have two vectors v and w in 3D space and know that the ratio of their magnitudes is ||v|| / ||w|| = 0.8019.
in 3D space an observer would see that they form an angle of 27.017 degrees.
on the other side, an observer in 2D (only seeing the x and z axis), observes an angle of 7.125 degrees between the vectors.
From their view, the vector coordinates are v = (x: 2, z: 1) and w = (x: 3, z: 2).
Is there somehow a way that the 2D observer can calculate the actual angle between these vectors in 3D space?
I would be more than happy for any input. All my tries have failed so far and I just want to know whether there could be a possible solution.
I have solved this problem and get that the values of y1 and y2 are given by this function:
eq1: 0.6439*y2^(2)-y1^(2)=9.785.
Therefore real angle can practically any value, the factor that would narrow this problem down to an actual solution would be the information about where the observer is in the 3d space so that he sees the angle of 27.017º, however, if this is the whole problem, then I can share my solution and process.
Some graphs that I created from my calculations:
The side view of the vectors is directly from the point of view of the x and z axis of the graph, therefore the coordinates of the (x1,z1) and (x2,z2) points(terminal points of the vectors), appear authentic, and not augmented, hence you can use them in your calculations to calculate the coordinates of z1 and z2, which you need to calculate the angle.
V = (x1, y1, z1) V = (2, y1, 1)
W = (x2, y2, z2) W = (3, y2, 2)
Since ||v|| / ||w|| = 0.8019
∴Then sqrt((x1^2)+(y1^2)+(z1^2))/sqrt((x2^2)+(y2^2)+(z2^2)) = 0.8019
∴(x1^2)+(y1^2)+(z1^2)/(x2^2)+(y2^2)+(z2^2) = 0.6430
∴4+(y1^2)+1/9+(y2^2)+4 = 0.6430
∴5+(y1^2) = 8.359 + 0.6430(y2^2)
∴13.359 = 0.6430(y2^2)-(y1^2)
This gives you therefore a function that calculates the other value of y given the some input y.
You can then graph this function using Geogebra.
For all the pairs of values on the curve, together with the fixed values of x and z for both of the vectors you can calculate that the ratio between the magnitudes of the two vectors is equal to 0.8019.
This problem has therefore infinitely many solutions for the angle as there are infinitely many values of z1 and z2 that satisfy the ratio; ||v|| / ||w|| = 0.8019.
Therefore the answer to this problem can be expressed as:
∀Θº∈R:Θº≥0
I have set of 3-dimensional dots, which lie on the plane with arbitrary normalized normal N' and the point through which this plane passes M (0, 0, 0) .
And to display it I need to map this coordinates to XY plane (convert from 3D to 2D).
First, what i do it's calculate dot product with N' and Z-axis as rotation angle Theta(N' ∙ Vec3f(0, 0, 1)) and multiply it by PI and divide by 180 (cos functions take radians).
Then i calculate cross product of N' and Z-axis (N' x Vec3f(0, 0, 1)) to get a new vector R (an arbitrary axis around which we will rotate our points).
Then i normalize R' = R / ||R||.
And pass it to next matrix:
But at the output, after matrix-vector multiplication, my points do not become two-dimensional(z is not 0), tell me please, what am I doing wrong?
To get angle between N and Z, you have to calculate arccosine of dot product (assuming N is unit length)
Theta = acos(N.dot.Z)
and result is in radians.
I have a point a point (x, y, z) that is on a plane defined by ax+by+cz+d=0. I'm trying to figure out what the (x', y') relative to the plane, where it has a starting point of (x0, y0, z0) and the x'-axis is defined by (1,0) and the y'-axis is defined by (0,1).
My major goal is to have the mouse click on a surface, and know the 2D co-ordinates on a particular surface. I've managed to intersect the ray onto a plane quite trivially.
As a side-note, I'm using DirectX 9 - my familiarity with matrix/vector math is limited by the APIs provided to me through the D3DX libraries.
One thought I had was to use the angle of between one of the axis vectors and find the distance from origin, and figure out the x/y using simple trig. But I'm not sure if that's really an ideal solution or not - or if it can actually solve the issue at hand.
Since you have a 2D image on that plane, you apparently want to match its coordinate system. To do so, determine the unit vectors of the picture. That is, take the 3d coordinates B for the picture position (x,0) for any x>0, and subtract from that the 3d coordinates A for the origin (0,0) of the picture. The resulting vector B − A will describe the positive x direction of your image. Do the same for the y direction. Then normalize both these vectors. This means dividing them by their length, sqrt(x²+y²+z²), but D3Dx has a function D3DXVec3Normalize for this. Let's call the resulting 3d vectors X and Y. To compute the x and y coordinate of any 3D point p, simply subtract the origin A from p, i.e. compute the vector p − A. Then compute the dot product between the result and the unit vectors X and Y. This will give you two numbers: the desired coordinates. This is because the dot product can be used to compute an orthogonal projection.
Translating this into D3Dx, it should look somewhat like the following. As I have never used it, this might have mistakes.
D3DXVECTOR3 *p; // input point
D3DXVECTOR3 a, b, c, ab, ac, ap; // helper vectors
FLOAT x, y; // output coordinates
imagePosTo3D(&a, 0, 0); // a = origin of image
imagePosTo3D(&b, 1, 0); // b = anywhere on positive x axis, perhaps a corner
imagePosTo3D(&c, 0, 1); // c = anywhere on positive y axis, perhaps a corner
D3DXVec3Subtract(&ab, &b, &a); // ab = b - a
D3DXVec3Subtract(&ac, &c, &a); // ac = c - a
D3DXVec3Normalize(&ab, &ab); // ab = ab / |ab|
D3DXVec3Normalize(&ac, &ac); // ac = ac / |ac|
// the above has to be done once for the image, the code below for every p
D3DXVec3Subtract(&ap, p, &a); // ap = p - a
x = D3DXVec3Dot(&ab, &ap); // x = ab∙ap
y = D3DXVec3Dot(&ac, &ap); // y = ac∙ap
How do you find the 3 euler angles between 2 3D vectors?
When I have one Vector and I want to get its rotation, this link can be usually used: Calculate rotations to look at a 3D point?
But how do I do it when calculating them according to one another?
As others have already pointed out, your question should be revised. Let's call your vectors a and b. I assume that length(a)==length(b) > 0 otherwise I cannot answer the question.
Calculate the cross product of your vectors v = a x b; v gives the axis of rotation. By computing the dot product, you can get the cosine of the angle you should rotate with cos(angle)=dot(a,b)/(length(a)length(b)), and with acos you can uniquely determine the angle (#Archie thanks for pointing out my earlier mistake). At this point you have the axis angle representation of your rotation.
The remaining work is to convert this representation to the representation you are looking for: Euler angles. Conversion Axis-Angle to Euler is a way to do it, as you have found it. You have to handle the degenerate case when v = [ 0, 0, 0], that is, when the angle is either 0 or 180 degrees.
I personally don't like Euler angles, they screw up the stability of your app and they are not appropriate for interpolation, see also
Strange behavior with android orientation sensor
Interpolating between rotation matrices
At first you would have to subtract vector one from vector two in order to get vector two relative to vector one. With these values you can calculate Euler angles.
To understand the calculation from vector to Euler intuitively, lets imagine a sphere with the radius of 1 and the origin at its center. A vector represents a point on its surface in 3D coordinates. This point can also be defined by spherical 2D coordinates: latitude and longitude, pitch and yaw respectively.
In order "roll <- pitch <- yaw" calculation can be done as follows:
To calculate the yaw you calculate the tangent of the two planar axes (x and z) considering the quadrant.
yaw = atan2(x, z) *180.0/PI;
Pitch is quite the same but as its plane is rotated along with yaw the 'adjacent' is on two axis. In order to find its length we will have to use the Pythagorean theorem.
float padj = sqrt(pow(x, 2) + pow(z, 2));
pitch = atan2(padj, y) *180.0/PI;
Notes:
Roll can not be calculated as a vector has no rotation around its own axis. I usually set it to 0.
The length of your vector is lost and can not be converted back.
In Euler the order of your axes matters, mix them up and you will get different results.
It took me a lot of time to find this answer so I would like to share it with you now.
first, you need to find the rotation matrix, and then with scipy you can easily find the angles you want.
There is no short way to do this.
so let's first declare some functions...
import numpy as np
from scipy.spatial.transform import Rotation
def normalize(v):
return v / np.linalg.norm(v)
def find_additional_vertical_vector(vector):
ez = np.array([0, 0, 1])
look_at_vector = normalize(vector)
up_vector = normalize(ez - np.dot(look_at_vector, ez) * look_at_vector)
return up_vector
def calc_rotation_matrix(v1_start, v2_start, v1_target, v2_target):
"""
calculating M the rotation matrix from base U to base V
M # U = V
M = V # U^-1
"""
def get_base_matrices():
u1_start = normalize(v1_start)
u2_start = normalize(v2_start)
u3_start = normalize(np.cross(u1_start, u2_start))
u1_target = normalize(v1_target)
u2_target = normalize(v2_target)
u3_target = normalize(np.cross(u1_target, u2_target))
U = np.hstack([u1_start.reshape(3, 1), u2_start.reshape(3, 1), u3_start.reshape(3, 1)])
V = np.hstack([u1_target.reshape(3, 1), u2_target.reshape(3, 1), u3_target.reshape(3, 1)])
return U, V
def calc_base_transition_matrix():
return np.dot(V, np.linalg.inv(U))
if not np.isclose(np.dot(v1_target, v2_target), 0, atol=1e-03):
raise ValueError("v1_target and v2_target must be vertical")
U, V = get_base_matrices()
return calc_base_transition_matrix()
def get_euler_rotation_angles(start_look_at_vector, target_look_at_vector, start_up_vector=None, target_up_vector=None):
if start_up_vector is None:
start_up_vector = find_additional_vertical_vector(start_look_at_vector)
if target_up_vector is None:
target_up_vector = find_additional_vertical_vector(target_look_at_vector)
rot_mat = calc_rotation_matrix(start_look_at_vector, start_up_vector, target_look_at_vector, target_up_vector)
is_equal = np.allclose(rot_mat # start_look_at_vector, target_look_at_vector, atol=1e-03)
print(f"rot_mat # start_look_at_vector1 == target_look_at_vector1 is {is_equal}")
rotation = Rotation.from_matrix(rot_mat)
return rotation.as_euler(seq="xyz", degrees=True)
Finding the XYZ Euler rotation angles from 1 vector to another might give you more than one answer.
Assuming what you are rotation is the look_at_vector of some kind of shape and you want this shape to stay not upside down and still look at the target_look_at_vector
if __name__ == "__main__":
# Example 1
start_look_at_vector = normalize(np.random.random(3))
target_look_at_vector = normalize(np.array([-0.70710688829422, 0.4156269133090973, -0.5720613598823547]))
phi, theta, psi = get_euler_rotation_angles(start_look_at_vector, target_look_at_vector)
print(f"phi_x_rotation={phi}, theta_y_rotation={theta}, psi_z_rotation={psi}")
Now if you want to have a specific role rotation to your shape, my code also supports that!
you just need to give the target_up_vector as a parameter as well.
just make sure it is vertical to the target_look_at_vector that you are giving.
if __name__ == "__main__":
# Example 2
# look and up must be vertical
start_look_at_vector = normalize(np.array([1, 2, 3]))
start_up_vector = normalize(np.array([1, -3, 2]))
target_look_at_vector = np.array([0.19283590755300162, 0.6597510192626469, -0.7263217228739983])
target_up_vector = np.array([-0.13225754322703182, 0.7509361508721898, 0.6469955018014842])
phi, theta, psi = get_euler_rotation_angles(
start_look_at_vector, target_look_at_vector, start_up_vector, target_up_vector
)
print(f"phi_x_rotation={phi}, theta_y_rotation={theta}, psi_z_rotation={psi}")
Getting Rotation Matrix in MATLAB is very easy
e.g.
A = [1.353553385, 0.200000003, 0.35]
B = [1 2 3]
[q] = vrrotvec(A,B)
Rot_mat = vrrotvec2mat(q)
I want to draw a plane which is given by the equation: Ax+By+Cz+D=0.
I first tried to draw him by setting x,y and then get z from the equation. this did not work fine because there are some planes like 0x+0y+z + 0 = 0 and etc...
my current solution is this:
- draw the plane on ZY plane by giving 4 coordinates which goes to infinity.
- find out to rotation that should be done in order to bring the normal of the given plane(a,b,c) to lay
on z axis.
- find the translation that should be done in order for that plane be on x axis.
- make the exactly opposite transformation to those rotation and to this translation hence i will get the
plane in his place.
ok
this is a great thing but I can make the proper math calculations(tried a lot of times...) with the dot product and etc....
can someone help me in understanding the exact way it should be done OR give me some formula in which I will put ABCD and get the right transformation?
You will want the following transformation matrix:
[ x0_x y0_x z0_x o_x ]
M = [ x0_y y0_y z0_y o_y ]
[ x0_z y0_z z0_z o_z ]
[ 0 0 0 1 ]
Here, z0 is the normal of your plane, and o is the origin of your plane, and x0 and y0 are two vectors within your plane orthogonal to z0 that define the rotation and skew of your projection.
Then any point (x,y) on your XY plane can be projected to a point (p_x, p_y, p_z) your new plane with the following:
(p_x, p_y, p_z, w) = M * (x, y, 0, 1)
now, z0 in your transformation matrix is easy, that's the normal of your plane and that is simply n = normalize(a,b,c).
In choosing the rest however you have distinctly more freedom. For the origin you could take the point that the plane intersects the Z axis, unless of course the plane is parallel to the Z axis, in which case you need something else.
So e.g.
if (c != 0) { //plane intersects Z axis
o_x = 0;
o_y = 0;
o_z = -d/c;
}
else if (b != 0) { // plane intersects Y axis
o_x = 0;
o_y = -d/b;
o_z = 0;
}
else { // plane must intersect the X axis
o_x = -d/a;
o_y = 0;
o_z = 0;
}
In practice you may want to prefer a different test than (c != 0), because with that test it will succeed even is c is very very small but just different from zero, leading your origin to be at say, x=0, y=0, z=10e100 which would probably not be desirable. So some test like (abs(c) > threshold) is probably preferable. However you could of course take an entirely different point in the plane to put the origin, perhaps the point closest to the origin of your original coordinate system, which would be:
o = n * (d / sqrt(a^2 + b^2 + c^2))
Then finally we need to figure out an x0 and y0. Which could be any two linearly independent vectors that are orthogonal to z0.
So first, let's choose a vector in the XY plane for our x0 vector:
x0 = normalize(z0_y, -z0_x, 0)
Now, this fails if your z0 happens to be of the form (0, 0, z0_z) so we need a special case for that:
if (z0_x == 0 && z0_y == 0) {
x0 = (1, 0, 0)
}
else {
x0 = normalize(z0_y, -z0_x, 0)
}
Finally let's say we do not want skew and choose y0 to be orthogonal to both x0, and y0, then, using the crossproduct
y0 = normalize(x0_y*y0_z-x0_z*y0_y, x0_z*y0_x-x0_z*y0_z, x0_x*y0_y-x0_y*y0_x)
Now you have all to fill your transformation matrix.
Disclaimer: Appropriate care should be taken when using floating point representations for your numbers, simple (foo == 0) tests are not sufficient in those cases. Read up on floating point math before you start implementing stuff.
Edit: renamed some variables for clarity
Is this what you're asking?
Transforming a simple plane like the xy plane to your plane is fairly simple:
your plane is Ax+By+Cz+D=0
the xy plane is simply z=0. i.e. A=B=D=0, while C=whatever you want. We'll say 1 for simplicity's sake.
When you have a plane in this form, the normal of the plane is defined by the vector (A,B,C)
so you want a rotation that will take you from (0,0,1) to (A,B,C)*
*Note that this will only work if {A,B,C} is unitary. so you may have to divide A B and C each by sqrt(A^2+B^2+C^2).
rotating around just two of the axes can get your from any direction to any other direction, so we'll pick x and y;
here are the rotation matrices for rotations by a about the x axis, and b about the y axis.
Rx := {{1, 0, 0}, {0, Cos[a], Sin[a]}, {0, -Sin[a], Cos[a]}}
Ry := {{Cos[b], 0, -Sin[b]}, {0, 1, 0}, {Sin[b], 0, Cos[b]}}
if we do a rotation about x, followed by a rotation about y, of the vector normal to the xy plane, (0,0,1), we get:
Ry.Rx.{0,0,1} = {-Cos[a] Sin[b], Sin[a], Cos[a] Cos[b]}
which are your A B C values.
i.e.
A = -Cos[a]Sin[b]
B = Sin[a]
C = Cos[a]Cos[b]
From here, it's simple.
a = aSin[B]
so now A = -Cos[aSin[B]]Sin[b]
Cos[aSin[x]] = sqrt(1-x^2)
so:
A = -Sqrt[1-B^2] * Sin[b]
b = aSin[-A/sqrt[1-B^2]]
a = aSin[B] (rotation about x axis)
b = aSin[-A/sqrt[1-B^2]] (rotation about y axis)
So we now have the angles about the x and y axes we need to rotate by.
After this, you just need to shift your plane up or down until it matches the one you already have.
The plane you have at the moment, (after those two rotations) is going to be Ax+By+Cz=0.
the plane you want is Ax+Bx+Cz+D=0. To find out d, we will see where the z axis crosses your plane.
i.e. Cz+D=0 -> z = -D/C
So we transform your z in Ax+By+Cz=0 by -D/C to give:
Ax+By+C(z+D/C) = Ax+By+Cz+D=0. Oh would you look at that!
It turns out you don't have to do any extra maths once you have the angles to rotate by!
The two angles will give you A,B, and C. To get D you just copy it from what you had.
Hope that's of some help, I'm not entirely sure how you plan on actually drawing the plane though...
Edited to fix some horrible formatting. hopefully it's better now.