Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Orthogonal Basis in 3d is flipping.

  1. Jul 30, 2011 #1
    I'm trying to create a circle in 3D based off of 4 inputs.

    Position1
    Position2
    LineLength1
    LineLength2

    The lines start at the positions, and they meet at their very ends.

    To do this I've gotten the distance between the points, found the radius of the circle, the position of the center of the circle, and I then create a Null Space matrix from the vector pointing from Position1 to Position2.

    This is the code I've written to create the basis

    Code (Text):

    public List<Vector3> createBasis (Vector3 vec)
        {
            //Calculate orthogonal basis
            int pivot = -1;
            int free1 = -1;
            int free2 = -1;
            bool taken = false;
           
            //Find Pivot and free variables
            for (int i = 0; i <= 2; i++) {
                taken = false;
                if (pivot == -1 && vec[i] != 0) {
                    pivot = i;
                    taken = true;
                }
                if (free1 == -1 && !taken) {
                    free1 = i;
                    taken = true;
                }
                if (free2 == -1 && !taken) {
                    free2 = i;
                    taken = true;
                }
            }
            if (pivot == -1)
                pivot = 2;
           
            Vector3 basis1 = new Vector3();
            Vector3 basis2 = new Vector3();
           
            //Setup basis vector 1
            basis1[free1] = 1;
            basis1[free2] = 0;
            basis1[pivot] = vec[free1] / -vec[pivot];
           
            //Setup basis vector 2
            basis2[free2] = 1;
            basis2[free1] = 0;
            basis2[pivot] = vec[free2] / -vec[pivot];
           
            //Calculate unit vector basis
            basis1.Normalize();
            basis2.Normalize();
            List<Vector3> basis = new List<Vector3> ();
            basis.Add (basis1);
            basis.Add (basis2);
           
            return basis;
        }
     
    This works perfectly for when the positions share any single axis e.g. x = x, y=y or z=z. But when ever they don't share any axis, something goes haywire when it get's close to sharing the x axis. The closer you get to sharing the x axis, the top of the circle starts to flip sides with the bottom.

    video may help.

    https://www.youtube.com/watch?v=yoDyyZUys8Y



    You see how the circle is being messed up when ever it approaches the x axis? But when it's on the x or z axis, it's perfect.

    What's wrong with my basis?
     
    Last edited by a moderator: Sep 25, 2014
  2. jcsd
  3. Jul 30, 2011 #2

    I like Serena

    User Avatar
    Homework Helper

    Hi DrSammyD! :smile:

    Your problem occurs with small values for the x-coordinate.
    In this case you pick your pivot to be the x-coordinate, where it swaps sign, which effectively inverts your basis.
    And when the coordinate becomes zero, you will pick another coordinate as the pivot, meaning the base vectors are suddenly swapped around.

    Btw, it's tricky to divide by values close to zero. That introduces the possibility of significant rounding errors.

    An easy fix is to pick the pivot as the coordinate with the greatest absolute value.
    This will yield consistent behavior and eliminate any significant rounding errors.
     
  4. Jul 30, 2011 #3
    That makes it even worse, and now it's not just the x axis.
     
  5. Jul 30, 2011 #4

    I like Serena

    User Avatar
    Homework Helper

    Hmm, now that I think about it, I guess you were trying to set up an orthonormal set of vectors, starting from your vector argument.
    What you would get however is not a proper orthonormal set.

    Say your vector is (x, y, z).

    Then use:
    basis1 = (0, -z, y)
    basis2 = (xz, xy, -y2-z2)
    These still need to be normalized of course to unit length.

    This will get you a proper orthonormal basis, although it doesn't work when you are exactly on the x axis.

    If you're exactly on the x axis, you can use for instance:
    basis1 = (z, 0, -x)
    basis2 = (xy, x2+z2, yz)
    These still need to be normalized to unit length.
     
    Last edited: Jul 30, 2011
  6. Jul 30, 2011 #5
    Now the top of the circle doesn't go through the bottom, but the circle still flips around. And it's not even orthogonal to the vector, It's askew. I need a way to calculate the null space consistently, since the null space is always orthogonal to the vector. How does matlab do it?
     
  7. Jul 30, 2011 #6

    I like Serena

    User Avatar
    Homework Helper

    Oops, sorry.
    The formula should be:

    vector = (x, y, z)
    basis1 = (0, -z, y)
    basis2 = (-y2-z2, xy, xz)

    You can check that this is an orthogonal basis, by calculating the inner products of each pair of vectors - they will turn out as zero.

    The second set should be:
    vector = (x, y, z)
    basis1 = (z, 0, -x)
    basis2 = (xy, -x2-z2, yz)
     
  8. Jul 30, 2011 #7
    Thanks a tril. Unfortunately, I have no idea why this is correct. Where would I learn more to truly grok it?
     
  9. Jul 30, 2011 #8

    I like Serena

    User Avatar
    Homework Helper

    This is linear algebra: calculations with vectors.
    More specifically usage of the cross product.

    The cross product of 2 vectors is always perpendicular to the vectors.
    I used that to construct an orthogonal set of vectors.
     
  10. Jul 30, 2011 #9
    How do you find the cross product of one vector? Just multiply it by itself? Or did you work backwards
     
  11. Jul 30, 2011 #10

    I like Serena

    User Avatar
    Homework Helper

    Ah no, I had to improvise a bit, since I needed a second vector. :wink:
    I picked (1,0,0), which was just the first vector I could think of.

    So I did:
    [tex]\begin{pmatrix}1\\0\\0\end{pmatrix} \times \begin{pmatrix}x\\y\\z\end{pmatrix} = \begin{pmatrix}0\\-z\\y\end{pmatrix}[/tex]

    Now I do have 2 vectors, and I can complete the orthogonal basis with:
    [tex]\begin{pmatrix}0\\-z\\y\end{pmatrix} \times \begin{pmatrix}x\\y\\z\end{pmatrix} = \begin{pmatrix}-y^2-z^2\\xy\\xz\end{pmatrix}[/tex]
     
  12. Jul 30, 2011 #11
    oh I get it, since the cross product of two vectors is orthogonal to both, you just picked one, and then used that to create the first one.
     
  13. Jul 30, 2011 #12

    I like Serena

    User Avatar
    Homework Helper

    Yep! :smile:

    Does that mean you are already truly grokking it?
     
  14. Jul 30, 2011 #13
    I had never used a cross product before, I thought dot products were the only meaningful thing to do with them.

    but yeah, other than that, i grok it.
     
  15. Jul 30, 2011 #14
    One last thing, for the second set of bases, did you use (0,1,0) as your first cross product to create those, in order to create something which would work on the x axis?
     
  16. Jul 30, 2011 #15

    I like Serena

    User Avatar
    Homework Helper

    Yep!
    The first set works except when x is insignificantly small, in which case either y or z have to be significant.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook