Wings 3D Development Forum
Checking for parallel vectors - something is wrong. - Printable Version

+- Wings 3D Development Forum (https://www.wings3d.com/forum)
+-- Forum: Wings 3D (https://www.wings3d.com/forum/forumdisplay.php?fid=1)
+--- Forum: Programming (https://www.wings3d.com/forum/forumdisplay.php?fid=7)
+--- Thread: Checking for parallel vectors - something is wrong. (/showthread.php?tid=1645)



Checking for parallel vectors - something is wrong. - micheus - 03-25-2016

Hi there

In a code I'm working, at some point I have two vectors with these values:
Vec: {0.0,1.0,0.0}
VecRef: {1.0,0.0,0.0}


and by computing the dot product e3d_vec:dot(Vec, VecRef) it's returning 0.0 (zero).

By the Dot Product definition, I was expecting to get a value other than zero. It should happens only if both vectors were parallel to each other. (Am I wrong?)

The two vectors cannot be parallel, so I test this value to try fix that - I need them to be perpendicular each other, being the Vec1 the reference one.

Just in case needed, here is the code where I use it:
Code:
force_perpendicular(Vec, VecRef) ->
    DotProd = e3d_vec:dot(Vec, VecRef),
    if abs(DotProd) =< 0.001 -> % vectors are almost parallel
            Nor0 = e3d_vec:cross(VecRef, rot_axis_vec(main_axis(Vec))),
            Rot0 = e3d_mat:rotate(+90.0, Nor0),
            Nor = e3d_mat:mul_vector(Rot0, VecRef);
       true ->
            Nor = e3d_vec:cross(VecRef, Vec)
    end,
    Rot = e3d_mat:rotate(90.0, Nor),
    e3d_vec:norm(e3d_mat:mul_vector(Rot,VecRef)).

main_axis({X,Y,Z}) ->
    if (abs(X) > abs(Y)) ->
            if (abs(Z) > abs(X)) -> z;
               true -> x
            end;
       true ->
            if (abs(Z) > abs(Y)) -> z;
               true -> y
            end
    end.

rot_axis_vec(x) -> {0.0,0.0,1.0}; % rotate vector in x around of z axis
rot_axis_vec(y) -> {0.0,0.0,1.0}; % rotate vector in y around of x axis
rot_axis_vec(z) -> {1.0,0.0,0.0}. % rotate vector in z around of x axis

Any idea about what is wrong?


RE: Checking for parallel vectors - something is wrong. - ggaliens - 03-25-2016

More often than not ... if I need two vectors to be perpendicular ... I used the e3d_vec:cross( ) in order to ensure that.

For example ... I have a single vector ... and I need any of its perpendiclar vectors (family of vectors) ... I can take


Vector A

compute any old random vector which is not equal to A .... call it B.

C = Then e3d_vec:cross(A,B)

C will be perfectly perpendicular to A by definaition. And then you could of course compute the third mutually perp B prime B2 using {A,C}



Not sure if this helps at all.

If you believe MathcWorld ... then the ZERO VALUE for perpendiculars is correct.

So e3d_vec seems correct.


RE: Checking for parallel vectors - something is wrong. - micheus - 03-25-2016

The random vector doesn't interest me.

As I said, I have two vector - set by the user, where one is the reference and then I want to ensure the second one must be rotated from it by 90°. For that I'm using the cross operation - of course.

I think I'm going to computer the degree between them. It will be a little more expensive computationally, but I will be sue about what I'm evaluating.

Thanks.


RE: Checking for parallel vectors - something is wrong. - ggaliens - 03-25-2016

How could e3d_vec:degrees used one time
on user inputs be "expensive" ?

I don't get it. Used in a big loop. Yes. Used 1000 times. Almost instant.


RE: Checking for parallel vectors - something is wrong. - micheus - 03-25-2016

Not much I know. That is why I used "little".

As I'm going to use that during preview mode, I tend to use the "shortest path" I can take.

Anyway, I need to go ahead and I will not spend time here.

Thanks again.


RE: Checking for parallel vectors - something is wrong. - ggaliens - 03-27-2016

""By the Dot Product definition, I was expecting to get a value other than zero. It should happens only if both vectors were parallel to each other. (Am I wrong?)"


Yes ... you were wrong.


RE: Checking for parallel vectors - something is wrong. - micheus - 03-27-2016

Yeah. I know now.

For some reason I read and understood this text in the wrong way Blush:
Quote:The dot product gives us a very nice method for determining if two vectors are perpendicular and it will give another method for determining when two vectors are parallel. Note as well that often we will use the term orthogonal in place of perpendicular.

Now, if two vectors are orthogonal then we know that the angle between them is 90 degrees. From (2) this tells us that if two vectors are orthogonal then, [Image: eq0033M.gif]
...
ref. link

The Cross product have the behaviour I wanted:
Quote:Now, let’s address the one time where the cross product will not be orthogonal to the original vectors. If the two vectors, [Image: eq0029M.gif] and [Image: eq0030M.gif], are parallel then the angle between them is either 0 or 180 degrees. From (1) this implies that, [Image: eq0031M.gif]
ref. link

Sometimes, to learn, we need to know where we are going wrong. Not only that we are wrong. Wink


RE: Checking for parallel vectors - something is wrong. - ggaliens - 03-28-2016

orthogonal means perpendicular ... but you probably got that down by now.