Signed angle between two 3D vectors with same origin within the same plane
Solution 1
Use cross product of the two vectors to get the normal of the plane formed by the two vectors. Then check the dotproduct between that and the original plane normal to see if they are facing the same direction.
angle = acos(dotProduct(Va.normalize(), Vb.normalize()));
cross = crossProduct(Va, Vb);
if (dotProduct(Vn, cross) < 0) { // Or > 0
angle = -angle;
}
Solution 2
The solution I'm currently using seems to be missing here.
Assuming that the plane normal is normalized (|Vn| == 1
), the signed angle is simply:
For the right-handed rotation from Va to Vb:
atan2((Va x Vb) . Vn, Va . Vb)
For the left-handed rotation from Va to Vb:
atan2((Vb x Va) . Vn, Va . Vb)
which returns an angle in the range [-PI, +PI] (or whatever the available atan2 implementation returns).
.
and x
are the dot and cross product respectively.
No explicit branching and no division/vector length calculation is necessary.
Explanation for why this works: let alpha be the direct angle between the vectors (0° to 180°) and beta the angle we are looking for (0° to 360°) with beta == alpha
or beta == 360° - alpha
Va . Vb == |Va| * |Vb| * cos(alpha) (by definition)
== |Va| * |Vb| * cos(beta) (cos(alpha) == cos(-alpha) == cos(360° - alpha)
Va x Vb == |Va| * |Vb| * sin(alpha) * n1
(by definition; n1 is a unit vector perpendicular to Va and Vb with
orientation matching the right-hand rule)
Therefore (again assuming Vn is normalized):
n1 . Vn == 1 when beta < 180
n1 . Vn == -1 when beta > 180
==> (Va x Vb) . Vn == |Va| * |Vb| * sin(beta)
Finally
tan(beta) = sin(beta) / cos(beta) == ((Va x Vb) . Vn) / (Va . Vb)
Solution 3
You can do this in two steps:
Determine the angle between the two vectors
theta = acos(dot product of Va, Vb). Assuming Va, Vb are normalized. This will give the minimum angle between the two vectors
Determine the sign of the angle
Find vector V3 = cross product of Va, Vb. (the order is important)
If (dot product of V3, Vn) is negative, theta is negative. Otherwise, theta is positive.
Solution 4
You can get the angle up to sign using the dot product. To get the sign of the angle, take the sign of Vn * (Va x Vb)
. In the special case of the XY plane, this reduces to just Va_x*Vb_y - Va_y*Vb_x
.
Solution 5
Advanced Customer provided the following solution (originally an edit to the question):
SOLUTION:
sina = |Va x Vb| / ( |Va| * |Vb| )
cosa = (Va . Vb) / ( |Va| * |Vb| )
angle = atan2( sina, cosa )
sign = Vn . ( Va x Vb )
if(sign<0)
{
angle=-angle
}
Advanced Customer
Updated on July 05, 2022Comments
-
Advanced Customer almost 2 years
What I need is a signed angle of rotation between two vectors Va and Vb lying within the same 3D plane and having the same origin knowing that:
- The plane contatining both vectors is an arbitrary and is not parallel to XY or any other of cardinal planes
- Vn - is a plane normal
- Both vectors along with the normal have the same origin O = { 0, 0, 0 }
- Va - is a reference for measuring the left handed rotation at Vn
The angle should be measured in such a way so if the plane would be XY plane the Va would stand for X axis unit vector of it.
I guess I should perform a kind of coordinate space transformation by using the Va as the X-axis and the cross product of Vb and Vn as the Y-axis and then just using some 2d method like with atan2() or something. Any ideas? Formulas?
-
Advanced Customer about 13 yearsI guess this is for a 2d vector while there is a need to make it for 3d ones. The plane where both 3d vectros belong to is not parallel to XY so using just x and y components may not work in some cases.
-
Advanced Customer about 13 yearsThat way it works also with no sign becuase of magnitudes involved - angle is always positive as with acos(). It seems like the only proper and stable way to do so is to create a coordinate transformation matrix given Vn as Z-axis, Va as X-axis and their cross product as Y-axis so all of this would downgrade into a simple 2d case.
-
Stephen Canon about 13 years@Advanced Customer: You take the cross-product of Va and Vb dot-producted with Vn - the sign of that quantity is the sign of the angle.
-
Advanced Customer about 13 yearsActually it works but I guess Parag described it a bit more clear - so see above :)
-
duffymo about 13 yearsThe sign can be different because every 2D surface has two normal vectors, depending on which side you're interested in. Either one is equally valid. It's up to you to decide which one is appropriate for your problem. If I have a plane defined by two vectors for a wall in a room I can use the cross-product to get the normal vector that faces into the room or out of it, depending on my requirements and which vector comes first in the expression. Which one is correct? Both - it depends on context.
-
Advanced Customer about 13 yearsThat was useful info that lead to the final solution - thanks!
-
Advanced Customer about 13 yearsThis is impractical in terms of the signedness as all magnitudes are a product of power of two - thus always positive.
-
Advanced Customer about 13 yearsFor the sign it shouldn't probably be V3.Vb - produced unstable results. In step 2 it should be: Vn . ( Va x Vb) - to check if the original normal (Vn) is facing same direction as the cross of Va and Vb.
-
David Norman about 13 yearsThe sign comes from the dot product of C and Vn, which will be negative if they point in opposite directions.
-
David Norman almost 12 yearsFrom the question "Vn - is a plane normal."
-
Admin almost 12 years@Advanced Customer If this answer is correct please tick it? Otherwise what did you change to the above?
-
Jichao over 10 years@StephenCanon: should
dot product
becross product
? -
Stephen Canon over 10 years@Jichao: no; the dot product lets you compute the magnitude of the angle between two vectors.
-
Jichao over 10 years@StephenCanon: I guess i misunderstood your meaning(
up to sign
). I mean the sign of the angle is depends on thecross product
. -
Stephen Canon over 10 years@Jichao: There are two sentences. The first says you get the magnitude of the angle using the dot product. The second says that you get the sign of the angle using
Vn * (Va x Vb)
, which contains both a dot product and a cross product. The two sentences are independent. -
NauticalMile about 9 yearsPossible improvement: use
angle = angle*sgn(dotProduct(Vn,cross))
instead of theif
statement. Not sure if it would be less/more efficient but it looks a little nicer. -
Peter O. almost 9 years@leetNightshade: Edited based on your comments.
-
leetNightshade almost 9 years@PeterO. Awesome. I removed my downvote. Now it's pretty similar to stackoverflow.com/a/5190354/353094 but in pseudo code.
-
Anton Holmberg almost 8 yearsWorks Perfectly! The most elegant solution by far. Thank you Adrian.
-
illright over 7 yearsIt's better to edit the post and write that information there
-
Tolga Birdal over 7 yearsEven easier if you like that : angle *=sgn(dotProduct(Vn,cross))
-
PinkTurtle about 7 yearsVery nice answer - useful in 2D too to check if a polygon is strictly convex. I used it in a segment crossing test.
-
Eric about 7 yearsThis is by far the best answer here. I suspect this solution is also more numerically stable
-
MarcoM over 6 yearsAm I wrong of atan2((Vb x Va) . Vn, Va . Vb) contains a typo? It should be atan2((Va x Vb) . Vn, Va . Vb) IMHO
-
Adrian Leonhard over 6 years@MarcoM the original question asks for the left-handed rotation from Va to Vb. For the right-handed rotation Va x Vb is indeed correct. en.wikipedia.org/wiki/Right-hand_rule#Rotation
-
MarcoM over 6 yearsThanks for the clarification!
-
Prashant Tukadiya over 5 yearscould you please help me I am not good in math math.stackexchange.com/questions/2997836/…
-
Prashant Tukadiya over 5 yearscould you please help me I am not good in math math.stackexchange.com/questions/2997836/…
-
Ben about 5 yearsNote that when the vectors are nearly parallel, you loose precision, so if you have vector
x=array([ 1., 0., 0.], dtype=float32)
andy=array([ 1.00000000e+00, 9.99999975e-05, 0.00000000e+00], dtype=float32)
, you havenorm(y) == 1.0f
soy
is normalized sodot(x,y) == 1.0
even thoughnorm(cross(x,y)) == 9.9999997e-05
, andarcsin(9.9999997e-05) == 9.9999997e-05
. -
marczellm about 5 yearsIn my case the imprecision of this solution reached 30 degrees.
-
kaalus about 3 yearsThis solution is very imprecise when vectors are nearly parallel or nearly opposite. This is because cosine function is almost flat at these points, so acos function has very little precision. Use the solution from the answer below instead.
-
Alexko about 3 yearsThank you for the solution and the explanation! However, I do not understand the step from this: "Therefore (again assuming Vn is normalized): n1 . Vn == 1 when beta < 180 n1 . Vn == -1 when beta > 180" To that: "==> (Va x Vb) . Vn == |Va| * |Vb| * sin(beta)"
-
Adrian Leonhard about 3 years@Alexko
sin(alpha) * n1 . Vn == sin(alpha) == sin(beta)
whenbeta < 180
andsin(alpha) * n1 . Vn == -sin(alpha) == sin(beta)
otherwise. -
Alexko about 3 years@AdrianLeonhard Thank you! I got it now. In case others might need this, I took the liberty of suggesting an edit to your answer with this added step and equation numbers, but I'm not sure it did anything (I got no feedback). So in case it didn't work, I put the suggested revised answer here: pastebin.com/5eGwiquc
-
brethvoice almost 3 yearsI have MATLAB code available for download here: mathworks.com/matlabcentral/fileexchange/…
-
ThaNoob over 2 yearsIf there's anyone calculating Vn by Va x Vb and wondering why the result is always positive: You have to choose the direction of Vn to get the sign. If you calculate it by making the cross product the result will always be positive. (do correct me if this comment is wrong).